]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ufs/ffs/ffs_vfsops.c
UFS snapshots: properly set the vm object size.
[FreeBSD/FreeBSD.git] / sys / ufs / ffs / ffs_vfsops.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1991, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)ffs_vfsops.c        8.31 (Berkeley) 5/20/95
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_quota.h"
38 #include "opt_ufs.h"
39 #include "opt_ffs.h"
40 #include "opt_ddb.h"
41
42 #include <sys/param.h>
43 #include <sys/gsb_crc32.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/taskqueue.h>
49 #include <sys/kernel.h>
50 #include <sys/ktr.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/conf.h>
56 #include <sys/fcntl.h>
57 #include <sys/ioccom.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/rwlock.h>
61 #include <sys/sysctl.h>
62 #include <sys/vmmeter.h>
63
64 #include <security/mac/mac_framework.h>
65
66 #include <ufs/ufs/dir.h>
67 #include <ufs/ufs/extattr.h>
68 #include <ufs/ufs/gjournal.h>
69 #include <ufs/ufs/quota.h>
70 #include <ufs/ufs/ufsmount.h>
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/ufs_extern.h>
73
74 #include <ufs/ffs/fs.h>
75 #include <ufs/ffs/ffs_extern.h>
76
77 #include <vm/vm.h>
78 #include <vm/uma.h>
79 #include <vm/vm_page.h>
80
81 #include <geom/geom.h>
82 #include <geom/geom_vfs.h>
83
84 #include <ddb/ddb.h>
85
86 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
87 VFS_SMR_DECLARE;
88
89 static int      ffs_mountfs(struct vnode *, struct mount *, struct thread *);
90 static void     ffs_oldfscompat_read(struct fs *, struct ufsmount *,
91                     ufs2_daddr_t);
92 static void     ffs_ifree(struct ufsmount *ump, struct inode *ip);
93 static int      ffs_sync_lazy(struct mount *mp);
94 static int      ffs_use_bread(void *devfd, off_t loc, void **bufp, int size);
95 static int      ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size);
96
97 static vfs_init_t ffs_init;
98 static vfs_uninit_t ffs_uninit;
99 static vfs_extattrctl_t ffs_extattrctl;
100 static vfs_cmount_t ffs_cmount;
101 static vfs_unmount_t ffs_unmount;
102 static vfs_mount_t ffs_mount;
103 static vfs_statfs_t ffs_statfs;
104 static vfs_fhtovp_t ffs_fhtovp;
105 static vfs_sync_t ffs_sync;
106
107 static struct vfsops ufs_vfsops = {
108         .vfs_extattrctl =       ffs_extattrctl,
109         .vfs_fhtovp =           ffs_fhtovp,
110         .vfs_init =             ffs_init,
111         .vfs_mount =            ffs_mount,
112         .vfs_cmount =           ffs_cmount,
113         .vfs_quotactl =         ufs_quotactl,
114         .vfs_root =             vfs_cache_root,
115         .vfs_cachedroot =       ufs_root,
116         .vfs_statfs =           ffs_statfs,
117         .vfs_sync =             ffs_sync,
118         .vfs_uninit =           ffs_uninit,
119         .vfs_unmount =          ffs_unmount,
120         .vfs_vget =             ffs_vget,
121         .vfs_susp_clean =       process_deferred_inactive,
122 };
123
124 VFS_SET(ufs_vfsops, ufs, 0);
125 MODULE_VERSION(ufs, 1);
126
127 static b_strategy_t ffs_geom_strategy;
128 static b_write_t ffs_bufwrite;
129
130 static struct buf_ops ffs_ops = {
131         .bop_name =     "FFS",
132         .bop_write =    ffs_bufwrite,
133         .bop_strategy = ffs_geom_strategy,
134         .bop_sync =     bufsync,
135 #ifdef NO_FFS_SNAPSHOT
136         .bop_bdflush =  bufbdflush,
137 #else
138         .bop_bdflush =  ffs_bdflush,
139 #endif
140 };
141
142 /*
143  * Note that userquota and groupquota options are not currently used
144  * by UFS/FFS code and generally mount(8) does not pass those options
145  * from userland, but they can be passed by loader(8) via
146  * vfs.root.mountfrom.options.
147  */
148 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
149     "noclusterw", "noexec", "export", "force", "from", "groupquota",
150     "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
151     "nosymfollow", "sync", "union", "userquota", "untrusted", NULL };
152
153 static int ffs_enxio_enable = 1;
154 SYSCTL_DECL(_vfs_ffs);
155 SYSCTL_INT(_vfs_ffs, OID_AUTO, enxio_enable, CTLFLAG_RWTUN,
156     &ffs_enxio_enable, 0,
157     "enable mapping of other disk I/O errors to ENXIO");
158
159 /*
160  * Return buffer with the contents of block "offset" from the beginning of
161  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
162  * remaining space in the directory.
163  */
164 static int
165 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
166 {
167         struct inode *ip;
168         struct fs *fs;
169         struct buf *bp;
170         ufs_lbn_t lbn;
171         int bsize, error;
172
173         ip = VTOI(vp);
174         fs = ITOFS(ip);
175         lbn = lblkno(fs, offset);
176         bsize = blksize(fs, ip, lbn);
177
178         *bpp = NULL;
179         error = bread(vp, lbn, bsize, NOCRED, &bp);
180         if (error) {
181                 return (error);
182         }
183         if (res)
184                 *res = (char *)bp->b_data + blkoff(fs, offset);
185         *bpp = bp;
186         return (0);
187 }
188
189 /*
190  * Load up the contents of an inode and copy the appropriate pieces
191  * to the incore copy.
192  */
193 static int
194 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
195 {
196         struct ufs1_dinode *dip1;
197         struct ufs2_dinode *dip2;
198         int error;
199
200         if (I_IS_UFS1(ip)) {
201                 dip1 = ip->i_din1;
202                 *dip1 =
203                     *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
204                 ip->i_mode = dip1->di_mode;
205                 ip->i_nlink = dip1->di_nlink;
206                 ip->i_effnlink = dip1->di_nlink;
207                 ip->i_size = dip1->di_size;
208                 ip->i_flags = dip1->di_flags;
209                 ip->i_gen = dip1->di_gen;
210                 ip->i_uid = dip1->di_uid;
211                 ip->i_gid = dip1->di_gid;
212                 return (0);
213         }
214         dip2 = ((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
215         if ((error = ffs_verify_dinode_ckhash(fs, dip2)) != 0 &&
216             !ffs_fsfail_cleanup(ITOUMP(ip), error)) {
217                 printf("%s: inode %jd: check-hash failed\n", fs->fs_fsmnt,
218                     (intmax_t)ino);
219                 return (error);
220         }
221         *ip->i_din2 = *dip2;
222         dip2 = ip->i_din2;
223         ip->i_mode = dip2->di_mode;
224         ip->i_nlink = dip2->di_nlink;
225         ip->i_effnlink = dip2->di_nlink;
226         ip->i_size = dip2->di_size;
227         ip->i_flags = dip2->di_flags;
228         ip->i_gen = dip2->di_gen;
229         ip->i_uid = dip2->di_uid;
230         ip->i_gid = dip2->di_gid;
231         return (0);
232 }
233
234 /*
235  * Verify that a filesystem block number is a valid data block.
236  * This routine is only called on untrusted filesystems.
237  */
238 static int
239 ffs_check_blkno(struct mount *mp, ino_t inum, ufs2_daddr_t daddr, int blksize)
240 {
241         struct fs *fs;
242         struct ufsmount *ump;
243         ufs2_daddr_t end_daddr;
244         int cg, havemtx;
245
246         KASSERT((mp->mnt_flag & MNT_UNTRUSTED) != 0,
247             ("ffs_check_blkno called on a trusted file system"));
248         ump = VFSTOUFS(mp);
249         fs = ump->um_fs;
250         cg = dtog(fs, daddr);
251         end_daddr = daddr + numfrags(fs, blksize);
252         /*
253          * Verify that the block number is a valid data block. Also check
254          * that it does not point to an inode block or a superblock. Accept
255          * blocks that are unalloacted (0) or part of snapshot metadata
256          * (BLK_NOCOPY or BLK_SNAP).
257          *
258          * Thus, the block must be in a valid range for the filesystem and
259          * either in the space before a backup superblock (except the first
260          * cylinder group where that space is used by the bootstrap code) or
261          * after the inode blocks and before the end of the cylinder group.
262          */
263         if ((uint64_t)daddr <= BLK_SNAP ||
264             ((uint64_t)end_daddr <= fs->fs_size &&
265             ((cg > 0 && end_daddr <= cgsblock(fs, cg)) ||
266             (daddr >= cgdmin(fs, cg) &&
267             end_daddr <= cgbase(fs, cg) + fs->fs_fpg))))
268                 return (0);
269         if ((havemtx = mtx_owned(UFS_MTX(ump))) == 0)
270                 UFS_LOCK(ump);
271         if (ppsratecheck(&ump->um_last_integritymsg,
272             &ump->um_secs_integritymsg, 1)) {
273                 UFS_UNLOCK(ump);
274                 uprintf("\n%s: inode %jd, out-of-range indirect block "
275                     "number %jd\n", mp->mnt_stat.f_mntonname, inum, daddr);
276                 if (havemtx)
277                         UFS_LOCK(ump);
278         } else if (!havemtx)
279                 UFS_UNLOCK(ump);
280         return (EINTEGRITY);
281 }
282
283 /*
284  * Initiate a forcible unmount.
285  * Used to unmount filesystems whose underlying media has gone away.
286  */
287 static void
288 ffs_fsfail_unmount(void *v, int pending)
289 {
290         struct fsfail_task *etp;
291         struct mount *mp;
292
293         etp = v;
294
295         /*
296          * Find our mount and get a ref on it, then try to unmount.
297          */
298         mp = vfs_getvfs(&etp->fsid);
299         if (mp != NULL)
300                 dounmount(mp, MNT_FORCE, curthread);
301         free(etp, M_UFSMNT);
302 }
303
304 /*
305  * On first ENXIO error, start a task that forcibly unmounts the filesystem.
306  *
307  * Return true if a cleanup is in progress.
308  */
309 int
310 ffs_fsfail_cleanup(struct ufsmount *ump, int error)
311 {
312         int retval;
313
314         UFS_LOCK(ump);
315         retval = ffs_fsfail_cleanup_locked(ump, error);
316         UFS_UNLOCK(ump);
317         return (retval);
318 }
319
320 int
321 ffs_fsfail_cleanup_locked(struct ufsmount *ump, int error)
322 {
323         struct fsfail_task *etp;
324         struct task *tp;
325
326         mtx_assert(UFS_MTX(ump), MA_OWNED);
327         if (error == ENXIO && (ump->um_flags & UM_FSFAIL_CLEANUP) == 0) {
328                 ump->um_flags |= UM_FSFAIL_CLEANUP;
329                 /*
330                  * Queue an async forced unmount.
331                  */
332                 etp = ump->um_fsfail_task;
333                 ump->um_fsfail_task = NULL;
334                 if (etp != NULL) {
335                         tp = &etp->task;
336                         TASK_INIT(tp, 0, ffs_fsfail_unmount, etp);
337                         taskqueue_enqueue(taskqueue_thread, tp);
338                         printf("UFS: forcibly unmounting %s from %s\n",
339                             ump->um_mountp->mnt_stat.f_mntfromname,
340                             ump->um_mountp->mnt_stat.f_mntonname);
341                 }
342         }
343         return ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0);
344 }
345
346 /*
347  * Wrapper used during ENXIO cleanup to allocate empty buffers when
348  * the kernel is unable to read the real one. They are needed so that
349  * the soft updates code can use them to unwind its dependencies.
350  */
351 int
352 ffs_breadz(struct ufsmount *ump, struct vnode *vp, daddr_t lblkno,
353     daddr_t dblkno, int size, daddr_t *rablkno, int *rabsize, int cnt,
354     struct ucred *cred, int flags, void (*ckhashfunc)(struct buf *),
355     struct buf **bpp)
356 {
357         int error;
358
359         flags |= GB_CVTENXIO;
360         error = breadn_flags(vp, lblkno, dblkno, size, rablkno, rabsize, cnt,
361             cred, flags, ckhashfunc, bpp);
362         if (error != 0 && ffs_fsfail_cleanup(ump, error)) {
363                 error = getblkx(vp, lblkno, dblkno, size, 0, 0, flags, bpp);
364                 KASSERT(error == 0, ("getblkx failed"));
365                 vfs_bio_bzero_buf(*bpp, 0, size);
366         }
367         return (error);
368 }
369
370 static int
371 ffs_mount(struct mount *mp)
372 {
373         struct vnode *devvp, *odevvp;
374         struct thread *td;
375         struct ufsmount *ump = NULL;
376         struct fs *fs;
377         pid_t fsckpid = 0;
378         int error, error1, flags;
379         uint64_t mntorflags, saved_mnt_flag;
380         accmode_t accmode;
381         struct nameidata ndp;
382         char *fspec;
383
384         td = curthread;
385         if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
386                 return (EINVAL);
387         if (uma_inode == NULL) {
388                 uma_inode = uma_zcreate("FFS inode",
389                     sizeof(struct inode), NULL, NULL, NULL, NULL,
390                     UMA_ALIGN_PTR, 0);
391                 uma_ufs1 = uma_zcreate("FFS1 dinode",
392                     sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
393                     UMA_ALIGN_PTR, 0);
394                 uma_ufs2 = uma_zcreate("FFS2 dinode",
395                     sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
396                     UMA_ALIGN_PTR, 0);
397                 VFS_SMR_ZONE_SET(uma_inode);
398         }
399
400         vfs_deleteopt(mp->mnt_optnew, "groupquota");
401         vfs_deleteopt(mp->mnt_optnew, "userquota");
402
403         fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
404         if (error)
405                 return (error);
406
407         mntorflags = 0;
408         if (vfs_getopt(mp->mnt_optnew, "untrusted", NULL, NULL) == 0)
409                 mntorflags |= MNT_UNTRUSTED;
410
411         if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
412                 mntorflags |= MNT_ACLS;
413
414         if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
415                 mntorflags |= MNT_SNAPSHOT;
416                 /*
417                  * Once we have set the MNT_SNAPSHOT flag, do not
418                  * persist "snapshot" in the options list.
419                  */
420                 vfs_deleteopt(mp->mnt_optnew, "snapshot");
421                 vfs_deleteopt(mp->mnt_opt, "snapshot");
422         }
423
424         if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
425             vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
426                 /*
427                  * Once we have set the restricted PID, do not
428                  * persist "fsckpid" in the options list.
429                  */
430                 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
431                 vfs_deleteopt(mp->mnt_opt, "fsckpid");
432                 if (mp->mnt_flag & MNT_UPDATE) {
433                         if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
434                              vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
435                                 vfs_mount_error(mp,
436                                     "Checker enable: Must be read-only");
437                                 return (EINVAL);
438                         }
439                 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
440                         vfs_mount_error(mp,
441                             "Checker enable: Must be read-only");
442                         return (EINVAL);
443                 }
444                 /* Set to -1 if we are done */
445                 if (fsckpid == 0)
446                         fsckpid = -1;
447         }
448
449         if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
450                 if (mntorflags & MNT_ACLS) {
451                         vfs_mount_error(mp,
452                             "\"acls\" and \"nfsv4acls\" options "
453                             "are mutually exclusive");
454                         return (EINVAL);
455                 }
456                 mntorflags |= MNT_NFS4ACLS;
457         }
458
459         MNT_ILOCK(mp);
460         mp->mnt_kern_flag &= ~MNTK_FPLOOKUP;
461         mp->mnt_flag |= mntorflags;
462         MNT_IUNLOCK(mp);
463         /*
464          * If updating, check whether changing from read-only to
465          * read/write; if there is no device name, that's all we do.
466          */
467         if (mp->mnt_flag & MNT_UPDATE) {
468                 ump = VFSTOUFS(mp);
469                 fs = ump->um_fs;
470                 odevvp = ump->um_odevvp;
471                 devvp = ump->um_devvp;
472                 if (fsckpid == -1 && ump->um_fsckpid > 0) {
473                         if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
474                             (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
475                                 return (error);
476                         g_topology_lock();
477                         /*
478                          * Return to normal read-only mode.
479                          */
480                         error = g_access(ump->um_cp, 0, -1, 0);
481                         g_topology_unlock();
482                         ump->um_fsckpid = 0;
483                 }
484                 if (fs->fs_ronly == 0 &&
485                     vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
486                         /*
487                          * Flush any dirty data and suspend filesystem.
488                          */
489                         if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
490                                 return (error);
491                         error = vfs_write_suspend_umnt(mp);
492                         if (error != 0)
493                                 return (error);
494                         /*
495                          * Check for and optionally get rid of files open
496                          * for writing.
497                          */
498                         flags = WRITECLOSE;
499                         if (mp->mnt_flag & MNT_FORCE)
500                                 flags |= FORCECLOSE;
501                         if (MOUNTEDSOFTDEP(mp)) {
502                                 error = softdep_flushfiles(mp, flags, td);
503                         } else {
504                                 error = ffs_flushfiles(mp, flags, td);
505                         }
506                         if (error) {
507                                 vfs_write_resume(mp, 0);
508                                 return (error);
509                         }
510                         if (fs->fs_pendingblocks != 0 ||
511                             fs->fs_pendinginodes != 0) {
512                                 printf("WARNING: %s Update error: blocks %jd "
513                                     "files %d\n", fs->fs_fsmnt, 
514                                     (intmax_t)fs->fs_pendingblocks,
515                                     fs->fs_pendinginodes);
516                                 fs->fs_pendingblocks = 0;
517                                 fs->fs_pendinginodes = 0;
518                         }
519                         if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
520                                 fs->fs_clean = 1;
521                         if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
522                                 fs->fs_ronly = 0;
523                                 fs->fs_clean = 0;
524                                 vfs_write_resume(mp, 0);
525                                 return (error);
526                         }
527                         if (MOUNTEDSOFTDEP(mp))
528                                 softdep_unmount(mp);
529                         g_topology_lock();
530                         /*
531                          * Drop our write and exclusive access.
532                          */
533                         g_access(ump->um_cp, 0, -1, -1);
534                         g_topology_unlock();
535                         fs->fs_ronly = 1;
536                         MNT_ILOCK(mp);
537                         mp->mnt_flag |= MNT_RDONLY;
538                         MNT_IUNLOCK(mp);
539                         /*
540                          * Allow the writers to note that filesystem
541                          * is ro now.
542                          */
543                         vfs_write_resume(mp, 0);
544                 }
545                 if ((mp->mnt_flag & MNT_RELOAD) &&
546                     (error = ffs_reload(mp, td, 0)) != 0)
547                         return (error);
548                 if (fs->fs_ronly &&
549                     !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
550                         /*
551                          * If we are running a checker, do not allow upgrade.
552                          */
553                         if (ump->um_fsckpid > 0) {
554                                 vfs_mount_error(mp,
555                                     "Active checker, cannot upgrade to write");
556                                 return (EINVAL);
557                         }
558                         /*
559                          * If upgrade to read-write by non-root, then verify
560                          * that user has necessary permissions on the device.
561                          */
562                         vn_lock(odevvp, LK_EXCLUSIVE | LK_RETRY);
563                         error = VOP_ACCESS(odevvp, VREAD | VWRITE,
564                             td->td_ucred, td);
565                         if (error)
566                                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
567                         VOP_UNLOCK(odevvp);
568                         if (error) {
569                                 return (error);
570                         }
571                         fs->fs_flags &= ~FS_UNCLEAN;
572                         if (fs->fs_clean == 0) {
573                                 fs->fs_flags |= FS_UNCLEAN;
574                                 if ((mp->mnt_flag & MNT_FORCE) ||
575                                     ((fs->fs_flags &
576                                      (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
577                                      (fs->fs_flags & FS_DOSOFTDEP))) {
578                                         printf("WARNING: %s was not properly "
579                                            "dismounted\n", fs->fs_fsmnt);
580                                 } else {
581                                         vfs_mount_error(mp,
582                                            "R/W mount of %s denied. %s.%s",
583                                            fs->fs_fsmnt,
584                                            "Filesystem is not clean - run fsck",
585                                            (fs->fs_flags & FS_SUJ) == 0 ? "" :
586                                            " Forced mount will invalidate"
587                                            " journal contents");
588                                         return (EPERM);
589                                 }
590                         }
591                         g_topology_lock();
592                         /*
593                          * Request exclusive write access.
594                          */
595                         error = g_access(ump->um_cp, 0, 1, 1);
596                         g_topology_unlock();
597                         if (error)
598                                 return (error);
599                         if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
600                                 return (error);
601                         error = vfs_write_suspend_umnt(mp);
602                         if (error != 0)
603                                 return (error);
604                         fs->fs_ronly = 0;
605                         MNT_ILOCK(mp);
606                         saved_mnt_flag = MNT_RDONLY;
607                         if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
608                             MNT_ASYNC) != 0)
609                                 saved_mnt_flag |= MNT_ASYNC;
610                         mp->mnt_flag &= ~saved_mnt_flag;
611                         MNT_IUNLOCK(mp);
612                         fs->fs_mtime = time_second;
613                         /* check to see if we need to start softdep */
614                         if ((fs->fs_flags & FS_DOSOFTDEP) &&
615                             (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
616                                 fs->fs_ronly = 1;
617                                 MNT_ILOCK(mp);
618                                 mp->mnt_flag |= saved_mnt_flag;
619                                 MNT_IUNLOCK(mp);
620                                 vfs_write_resume(mp, 0);
621                                 return (error);
622                         }
623                         fs->fs_clean = 0;
624                         if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
625                                 fs->fs_ronly = 1;
626                                 MNT_ILOCK(mp);
627                                 mp->mnt_flag |= saved_mnt_flag;
628                                 MNT_IUNLOCK(mp);
629                                 vfs_write_resume(mp, 0);
630                                 return (error);
631                         }
632                         if (fs->fs_snapinum[0] != 0)
633                                 ffs_snapshot_mount(mp);
634                         vfs_write_resume(mp, 0);
635                 }
636                 /*
637                  * Soft updates is incompatible with "async",
638                  * so if we are doing softupdates stop the user
639                  * from setting the async flag in an update.
640                  * Softdep_mount() clears it in an initial mount
641                  * or ro->rw remount.
642                  */
643                 if (MOUNTEDSOFTDEP(mp)) {
644                         /* XXX: Reset too late ? */
645                         MNT_ILOCK(mp);
646                         mp->mnt_flag &= ~MNT_ASYNC;
647                         MNT_IUNLOCK(mp);
648                 }
649                 /*
650                  * Keep MNT_ACLS flag if it is stored in superblock.
651                  */
652                 if ((fs->fs_flags & FS_ACLS) != 0) {
653                         /* XXX: Set too late ? */
654                         MNT_ILOCK(mp);
655                         mp->mnt_flag |= MNT_ACLS;
656                         MNT_IUNLOCK(mp);
657                 }
658
659                 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
660                         /* XXX: Set too late ? */
661                         MNT_ILOCK(mp);
662                         mp->mnt_flag |= MNT_NFS4ACLS;
663                         MNT_IUNLOCK(mp);
664                 }
665                 /*
666                  * If this is a request from fsck to clean up the filesystem,
667                  * then allow the specified pid to proceed.
668                  */
669                 if (fsckpid > 0) {
670                         if (ump->um_fsckpid != 0) {
671                                 vfs_mount_error(mp,
672                                     "Active checker already running on %s",
673                                     fs->fs_fsmnt);
674                                 return (EINVAL);
675                         }
676                         KASSERT(MOUNTEDSOFTDEP(mp) == 0,
677                             ("soft updates enabled on read-only file system"));
678                         g_topology_lock();
679                         /*
680                          * Request write access.
681                          */
682                         error = g_access(ump->um_cp, 0, 1, 0);
683                         g_topology_unlock();
684                         if (error) {
685                                 vfs_mount_error(mp,
686                                     "Checker activation failed on %s",
687                                     fs->fs_fsmnt);
688                                 return (error);
689                         }
690                         ump->um_fsckpid = fsckpid;
691                         if (fs->fs_snapinum[0] != 0)
692                                 ffs_snapshot_mount(mp);
693                         fs->fs_mtime = time_second;
694                         fs->fs_fmod = 1;
695                         fs->fs_clean = 0;
696                         (void) ffs_sbupdate(ump, MNT_WAIT, 0);
697                 }
698
699                 /*
700                  * If this is a snapshot request, take the snapshot.
701                  */
702                 if (mp->mnt_flag & MNT_SNAPSHOT)
703                         return (ffs_snapshot(mp, fspec));
704
705                 /*
706                  * Must not call namei() while owning busy ref.
707                  */
708                 vfs_unbusy(mp);
709         }
710
711         /*
712          * Not an update, or updating the name: look up the name
713          * and verify that it refers to a sensible disk device.
714          */
715         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
716         error = namei(&ndp);
717         if ((mp->mnt_flag & MNT_UPDATE) != 0) {
718                 /*
719                  * Unmount does not start if MNT_UPDATE is set.  Mount
720                  * update busies mp before setting MNT_UPDATE.  We
721                  * must be able to retain our busy ref succesfully,
722                  * without sleep.
723                  */
724                 error1 = vfs_busy(mp, MBF_NOWAIT);
725                 MPASS(error1 == 0);
726         }
727         if (error != 0)
728                 return (error);
729         NDFREE(&ndp, NDF_ONLY_PNBUF);
730         devvp = ndp.ni_vp;
731         if (!vn_isdisk_error(devvp, &error)) {
732                 vput(devvp);
733                 return (error);
734         }
735
736         /*
737          * If mount by non-root, then verify that user has necessary
738          * permissions on the device.
739          */
740         accmode = VREAD;
741         if ((mp->mnt_flag & MNT_RDONLY) == 0)
742                 accmode |= VWRITE;
743         error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
744         if (error)
745                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
746         if (error) {
747                 vput(devvp);
748                 return (error);
749         }
750
751         if (mp->mnt_flag & MNT_UPDATE) {
752                 /*
753                  * Update only
754                  *
755                  * If it's not the same vnode, or at least the same device
756                  * then it's not correct.
757                  */
758
759                 if (devvp->v_rdev != ump->um_devvp->v_rdev)
760                         error = EINVAL; /* needs translation */
761                 vput(devvp);
762                 if (error)
763                         return (error);
764         } else {
765                 /*
766                  * New mount
767                  *
768                  * We need the name for the mount point (also used for
769                  * "last mounted on") copied in. If an error occurs,
770                  * the mount point is discarded by the upper level code.
771                  * Note that vfs_mount_alloc() populates f_mntonname for us.
772                  */
773                 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
774                         vrele(devvp);
775                         return (error);
776                 }
777                 if (fsckpid > 0) {
778                         KASSERT(MOUNTEDSOFTDEP(mp) == 0,
779                             ("soft updates enabled on read-only file system"));
780                         ump = VFSTOUFS(mp);
781                         fs = ump->um_fs;
782                         g_topology_lock();
783                         /*
784                          * Request write access.
785                          */
786                         error = g_access(ump->um_cp, 0, 1, 0);
787                         g_topology_unlock();
788                         if (error) {
789                                 printf("WARNING: %s: Checker activation "
790                                     "failed\n", fs->fs_fsmnt);
791                         } else { 
792                                 ump->um_fsckpid = fsckpid;
793                                 if (fs->fs_snapinum[0] != 0)
794                                         ffs_snapshot_mount(mp);
795                                 fs->fs_mtime = time_second;
796                                 fs->fs_clean = 0;
797                                 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
798                         }
799                 }
800         }
801
802         MNT_ILOCK(mp);
803         /*
804          * This is racy versus lookup, see ufs_fplookup_vexec for details.
805          */
806         if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) != 0)
807                 panic("MNTK_FPLOOKUP set on mount %p when it should not be", mp);
808         if ((mp->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS | MNT_UNION)) == 0)
809                 mp->mnt_kern_flag |= MNTK_FPLOOKUP;
810         MNT_IUNLOCK(mp);
811
812         vfs_mountedfrom(mp, fspec);
813         return (0);
814 }
815
816 /*
817  * Compatibility with old mount system call.
818  */
819
820 static int
821 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
822 {
823         struct ufs_args args;
824         int error;
825
826         if (data == NULL)
827                 return (EINVAL);
828         error = copyin(data, &args, sizeof args);
829         if (error)
830                 return (error);
831
832         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
833         ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
834         error = kernel_mount(ma, flags);
835
836         return (error);
837 }
838
839 /*
840  * Reload all incore data for a filesystem (used after running fsck on
841  * the root filesystem and finding things to fix). If the 'force' flag
842  * is 0, the filesystem must be mounted read-only.
843  *
844  * Things to do to update the mount:
845  *      1) invalidate all cached meta-data.
846  *      2) re-read superblock from disk.
847  *      3) re-read summary information from disk.
848  *      4) invalidate all inactive vnodes.
849  *      5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
850  *         writers, if requested.
851  *      6) invalidate all cached file data.
852  *      7) re-read inode data for all active vnodes.
853  */
854 int
855 ffs_reload(struct mount *mp, struct thread *td, int flags)
856 {
857         struct vnode *vp, *mvp, *devvp;
858         struct inode *ip;
859         void *space;
860         struct buf *bp;
861         struct fs *fs, *newfs;
862         struct ufsmount *ump;
863         ufs2_daddr_t sblockloc;
864         int i, blks, error;
865         u_long size;
866         int32_t *lp;
867
868         ump = VFSTOUFS(mp);
869
870         MNT_ILOCK(mp);
871         if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
872                 MNT_IUNLOCK(mp);
873                 return (EINVAL);
874         }
875         MNT_IUNLOCK(mp);
876
877         /*
878          * Step 1: invalidate all cached meta-data.
879          */
880         devvp = VFSTOUFS(mp)->um_devvp;
881         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
882         if (vinvalbuf(devvp, 0, 0, 0) != 0)
883                 panic("ffs_reload: dirty1");
884         VOP_UNLOCK(devvp);
885
886         /*
887          * Step 2: re-read superblock from disk.
888          */
889         fs = VFSTOUFS(mp)->um_fs;
890         if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
891             NOCRED, &bp)) != 0)
892                 return (error);
893         newfs = (struct fs *)bp->b_data;
894         if ((newfs->fs_magic != FS_UFS1_MAGIC &&
895              newfs->fs_magic != FS_UFS2_MAGIC) ||
896             newfs->fs_bsize > MAXBSIZE ||
897             newfs->fs_bsize < sizeof(struct fs)) {
898                         brelse(bp);
899                         return (EIO);           /* XXX needs translation */
900         }
901         /*
902          * Preserve the summary information, read-only status, and
903          * superblock location by copying these fields into our new
904          * superblock before using it to update the existing superblock.
905          */
906         newfs->fs_si = fs->fs_si;
907         newfs->fs_ronly = fs->fs_ronly;
908         sblockloc = fs->fs_sblockloc;
909         bcopy(newfs, fs, (u_int)fs->fs_sbsize);
910         brelse(bp);
911         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
912         ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
913         UFS_LOCK(ump);
914         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
915                 printf("WARNING: %s: reload pending error: blocks %jd "
916                     "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
917                     fs->fs_pendinginodes);
918                 fs->fs_pendingblocks = 0;
919                 fs->fs_pendinginodes = 0;
920         }
921         UFS_UNLOCK(ump);
922
923         /*
924          * Step 3: re-read summary information from disk.
925          */
926         size = fs->fs_cssize;
927         blks = howmany(size, fs->fs_fsize);
928         if (fs->fs_contigsumsize > 0)
929                 size += fs->fs_ncg * sizeof(int32_t);
930         size += fs->fs_ncg * sizeof(u_int8_t);
931         free(fs->fs_csp, M_UFSMNT);
932         space = malloc(size, M_UFSMNT, M_WAITOK);
933         fs->fs_csp = space;
934         for (i = 0; i < blks; i += fs->fs_frag) {
935                 size = fs->fs_bsize;
936                 if (i + fs->fs_frag > blks)
937                         size = (blks - i) * fs->fs_fsize;
938                 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
939                     NOCRED, &bp);
940                 if (error)
941                         return (error);
942                 bcopy(bp->b_data, space, (u_int)size);
943                 space = (char *)space + size;
944                 brelse(bp);
945         }
946         /*
947          * We no longer know anything about clusters per cylinder group.
948          */
949         if (fs->fs_contigsumsize > 0) {
950                 fs->fs_maxcluster = lp = space;
951                 for (i = 0; i < fs->fs_ncg; i++)
952                         *lp++ = fs->fs_contigsumsize;
953                 space = lp;
954         }
955         size = fs->fs_ncg * sizeof(u_int8_t);
956         fs->fs_contigdirs = (u_int8_t *)space;
957         bzero(fs->fs_contigdirs, size);
958         if ((flags & FFSR_UNSUSPEND) != 0) {
959                 MNT_ILOCK(mp);
960                 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
961                 wakeup(&mp->mnt_flag);
962                 MNT_IUNLOCK(mp);
963         }
964
965 loop:
966         MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
967                 /*
968                  * Skip syncer vnode.
969                  */
970                 if (vp->v_type == VNON) {
971                         VI_UNLOCK(vp);
972                         continue;
973                 }
974                 /*
975                  * Step 4: invalidate all cached file data.
976                  */
977                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
978                         MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
979                         goto loop;
980                 }
981                 if (vinvalbuf(vp, 0, 0, 0))
982                         panic("ffs_reload: dirty2");
983                 /*
984                  * Step 5: re-read inode data for all active vnodes.
985                  */
986                 ip = VTOI(vp);
987                 error =
988                     bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
989                     (int)fs->fs_bsize, NOCRED, &bp);
990                 if (error) {
991                         vput(vp);
992                         MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
993                         return (error);
994                 }
995                 if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) {
996                         brelse(bp);
997                         vput(vp);
998                         MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
999                         return (error);
1000                 }
1001                 ip->i_effnlink = ip->i_nlink;
1002                 brelse(bp);
1003                 vput(vp);
1004         }
1005         return (0);
1006 }
1007
1008 /*
1009  * Common code for mount and mountroot
1010  */
1011 static int
1012 ffs_mountfs(odevvp, mp, td)
1013         struct vnode *odevvp;
1014         struct mount *mp;
1015         struct thread *td;
1016 {
1017         struct ufsmount *ump;
1018         struct fs *fs;
1019         struct cdev *dev;
1020         int error, i, len, ronly;
1021         struct ucred *cred;
1022         struct g_consumer *cp;
1023         struct mount *nmp;
1024         struct vnode *devvp;
1025         struct fsfail_task *etp;
1026         int candelete, canspeedup;
1027         off_t loc;
1028
1029         fs = NULL;
1030         ump = NULL;
1031         cred = td ? td->td_ucred : NOCRED;
1032         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1033
1034         devvp = mntfs_allocvp(mp, odevvp);
1035         VOP_UNLOCK(odevvp);
1036         KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
1037         dev = devvp->v_rdev;
1038         KASSERT(dev->si_snapdata == NULL, ("non-NULL snapshot data"));
1039         if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
1040             (uintptr_t)mp) == 0) {
1041                 mntfs_freevp(devvp);
1042                 return (EBUSY);
1043         }
1044         g_topology_lock();
1045         error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
1046         g_topology_unlock();
1047         if (error != 0) {
1048                 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1049                 mntfs_freevp(devvp);
1050                 return (error);
1051         }
1052         dev_ref(dev);
1053         devvp->v_bufobj.bo_ops = &ffs_ops;
1054         BO_LOCK(&odevvp->v_bufobj);
1055         odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
1056         BO_UNLOCK(&odevvp->v_bufobj);
1057         if (dev->si_iosize_max != 0)
1058                 mp->mnt_iosize_max = dev->si_iosize_max;
1059         if (mp->mnt_iosize_max > maxphys)
1060                 mp->mnt_iosize_max = maxphys;
1061         if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
1062                 error = EINVAL;
1063                 vfs_mount_error(mp,
1064                     "Invalid sectorsize %d for superblock size %d",
1065                     cp->provider->sectorsize, SBLOCKSIZE);
1066                 goto out;
1067         }
1068         /* fetch the superblock and summary information */
1069         loc = STDSB;
1070         if ((mp->mnt_flag & MNT_ROOTFS) != 0)
1071                 loc = STDSB_NOHASHFAIL;
1072         if ((error = ffs_sbget(devvp, &fs, loc, M_UFSMNT, ffs_use_bread)) != 0)
1073                 goto out;
1074         fs->fs_flags &= ~FS_UNCLEAN;
1075         if (fs->fs_clean == 0) {
1076                 fs->fs_flags |= FS_UNCLEAN;
1077                 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
1078                     ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
1079                      (fs->fs_flags & FS_DOSOFTDEP))) {
1080                         printf("WARNING: %s was not properly dismounted\n",
1081                             fs->fs_fsmnt);
1082                 } else {
1083                         vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
1084                             fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
1085                             (fs->fs_flags & FS_SUJ) == 0 ? "" :
1086                             " Forced mount will invalidate journal contents");
1087                         error = EPERM;
1088                         goto out;
1089                 }
1090                 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
1091                     (mp->mnt_flag & MNT_FORCE)) {
1092                         printf("WARNING: %s: lost blocks %jd files %d\n",
1093                             fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1094                             fs->fs_pendinginodes);
1095                         fs->fs_pendingblocks = 0;
1096                         fs->fs_pendinginodes = 0;
1097                 }
1098         }
1099         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1100                 printf("WARNING: %s: mount pending error: blocks %jd "
1101                     "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1102                     fs->fs_pendinginodes);
1103                 fs->fs_pendingblocks = 0;
1104                 fs->fs_pendinginodes = 0;
1105         }
1106         if ((fs->fs_flags & FS_GJOURNAL) != 0) {
1107 #ifdef UFS_GJOURNAL
1108                 /*
1109                  * Get journal provider name.
1110                  */
1111                 len = 1024;
1112                 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
1113                 if (g_io_getattr("GJOURNAL::provider", cp, &len,
1114                     mp->mnt_gjprovider) == 0) {
1115                         mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
1116                             M_UFSMNT, M_WAITOK);
1117                         MNT_ILOCK(mp);
1118                         mp->mnt_flag |= MNT_GJOURNAL;
1119                         MNT_IUNLOCK(mp);
1120                 } else {
1121                         printf("WARNING: %s: GJOURNAL flag on fs "
1122                             "but no gjournal provider below\n",
1123                             mp->mnt_stat.f_mntonname);
1124                         free(mp->mnt_gjprovider, M_UFSMNT);
1125                         mp->mnt_gjprovider = NULL;
1126                 }
1127 #else
1128                 printf("WARNING: %s: GJOURNAL flag on fs but no "
1129                     "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
1130 #endif
1131         } else {
1132                 mp->mnt_gjprovider = NULL;
1133         }
1134         ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
1135         ump->um_cp = cp;
1136         ump->um_bo = &devvp->v_bufobj;
1137         ump->um_fs = fs;
1138         if (fs->fs_magic == FS_UFS1_MAGIC) {
1139                 ump->um_fstype = UFS1;
1140                 ump->um_balloc = ffs_balloc_ufs1;
1141         } else {
1142                 ump->um_fstype = UFS2;
1143                 ump->um_balloc = ffs_balloc_ufs2;
1144         }
1145         ump->um_blkatoff = ffs_blkatoff;
1146         ump->um_truncate = ffs_truncate;
1147         ump->um_update = ffs_update;
1148         ump->um_valloc = ffs_valloc;
1149         ump->um_vfree = ffs_vfree;
1150         ump->um_ifree = ffs_ifree;
1151         ump->um_rdonly = ffs_rdonly;
1152         ump->um_snapgone = ffs_snapgone;
1153         if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
1154                 ump->um_check_blkno = ffs_check_blkno;
1155         else
1156                 ump->um_check_blkno = NULL;
1157         mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
1158         ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
1159         fs->fs_ronly = ronly;
1160         fs->fs_active = NULL;
1161         mp->mnt_data = ump;
1162         mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
1163         mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
1164         nmp = NULL;
1165         if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
1166             (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
1167                 if (nmp)
1168                         vfs_rel(nmp);
1169                 vfs_getnewfsid(mp);
1170         }
1171         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
1172         MNT_ILOCK(mp);
1173         mp->mnt_flag |= MNT_LOCAL;
1174         MNT_IUNLOCK(mp);
1175         if ((fs->fs_flags & FS_MULTILABEL) != 0) {
1176 #ifdef MAC
1177                 MNT_ILOCK(mp);
1178                 mp->mnt_flag |= MNT_MULTILABEL;
1179                 MNT_IUNLOCK(mp);
1180 #else
1181                 printf("WARNING: %s: multilabel flag on fs but "
1182                     "no MAC support\n", mp->mnt_stat.f_mntonname);
1183 #endif
1184         }
1185         if ((fs->fs_flags & FS_ACLS) != 0) {
1186 #ifdef UFS_ACL
1187                 MNT_ILOCK(mp);
1188
1189                 if (mp->mnt_flag & MNT_NFS4ACLS)
1190                         printf("WARNING: %s: ACLs flag on fs conflicts with "
1191                             "\"nfsv4acls\" mount option; option ignored\n",
1192                             mp->mnt_stat.f_mntonname);
1193                 mp->mnt_flag &= ~MNT_NFS4ACLS;
1194                 mp->mnt_flag |= MNT_ACLS;
1195
1196                 MNT_IUNLOCK(mp);
1197 #else
1198                 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
1199                     mp->mnt_stat.f_mntonname);
1200 #endif
1201         }
1202         if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
1203 #ifdef UFS_ACL
1204                 MNT_ILOCK(mp);
1205
1206                 if (mp->mnt_flag & MNT_ACLS)
1207                         printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
1208                             "with \"acls\" mount option; option ignored\n",
1209                             mp->mnt_stat.f_mntonname);
1210                 mp->mnt_flag &= ~MNT_ACLS;
1211                 mp->mnt_flag |= MNT_NFS4ACLS;
1212
1213                 MNT_IUNLOCK(mp);
1214 #else
1215                 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
1216                     "ACLs support\n", mp->mnt_stat.f_mntonname);
1217 #endif
1218         }
1219         if ((fs->fs_flags & FS_TRIM) != 0) {
1220                 len = sizeof(int);
1221                 if (g_io_getattr("GEOM::candelete", cp, &len,
1222                     &candelete) == 0) {
1223                         if (candelete)
1224                                 ump->um_flags |= UM_CANDELETE;
1225                         else
1226                                 printf("WARNING: %s: TRIM flag on fs but disk "
1227                                     "does not support TRIM\n",
1228                                     mp->mnt_stat.f_mntonname);
1229                 } else {
1230                         printf("WARNING: %s: TRIM flag on fs but disk does "
1231                             "not confirm that it supports TRIM\n",
1232                             mp->mnt_stat.f_mntonname);
1233                 }
1234                 if (((ump->um_flags) & UM_CANDELETE) != 0) {
1235                         ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1236                             taskqueue_thread_enqueue, &ump->um_trim_tq);
1237                         taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1238                             "%s trim", mp->mnt_stat.f_mntonname);
1239                         ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1240                             &ump->um_trimlisthashsize);
1241                 }
1242         }
1243
1244         len = sizeof(int);
1245         if (g_io_getattr("GEOM::canspeedup", cp, &len, &canspeedup) == 0) {
1246                 if (canspeedup)
1247                         ump->um_flags |= UM_CANSPEEDUP;
1248         }
1249
1250         ump->um_mountp = mp;
1251         ump->um_dev = dev;
1252         ump->um_devvp = devvp;
1253         ump->um_odevvp = odevvp;
1254         ump->um_nindir = fs->fs_nindir;
1255         ump->um_bptrtodb = fs->fs_fsbtodb;
1256         ump->um_seqinc = fs->fs_frag;
1257         for (i = 0; i < MAXQUOTAS; i++)
1258                 ump->um_quotas[i] = NULLVP;
1259 #ifdef UFS_EXTATTR
1260         ufs_extattr_uepm_init(&ump->um_extattr);
1261 #endif
1262         /*
1263          * Set FS local "last mounted on" information (NULL pad)
1264          */
1265         bzero(fs->fs_fsmnt, MAXMNTLEN);
1266         strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1267         mp->mnt_stat.f_iosize = fs->fs_bsize;
1268
1269         if (mp->mnt_flag & MNT_ROOTFS) {
1270                 /*
1271                  * Root mount; update timestamp in mount structure.
1272                  * this will be used by the common root mount code
1273                  * to update the system clock.
1274                  */
1275                 mp->mnt_time = fs->fs_time;
1276         }
1277
1278         if (ronly == 0) {
1279                 fs->fs_mtime = time_second;
1280                 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1281                     (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1282                         ffs_flushfiles(mp, FORCECLOSE, td);
1283                         goto out;
1284                 }
1285                 if (fs->fs_snapinum[0] != 0)
1286                         ffs_snapshot_mount(mp);
1287                 fs->fs_fmod = 1;
1288                 fs->fs_clean = 0;
1289                 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1290         }
1291         /*
1292          * Initialize filesystem state information in mount struct.
1293          */
1294         MNT_ILOCK(mp);
1295         mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1296             MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1297         MNT_IUNLOCK(mp);
1298 #ifdef UFS_EXTATTR
1299 #ifdef UFS_EXTATTR_AUTOSTART
1300         /*
1301          *
1302          * Auto-starting does the following:
1303          *      - check for /.attribute in the fs, and extattr_start if so
1304          *      - for each file in .attribute, enable that file with
1305          *        an attribute of the same name.
1306          * Not clear how to report errors -- probably eat them.
1307          * This would all happen while the filesystem was busy/not
1308          * available, so would effectively be "atomic".
1309          */
1310         (void) ufs_extattr_autostart(mp, td);
1311 #endif /* !UFS_EXTATTR_AUTOSTART */
1312 #endif /* !UFS_EXTATTR */
1313         etp = malloc(sizeof *ump->um_fsfail_task, M_UFSMNT, M_WAITOK | M_ZERO);
1314         etp->fsid = mp->mnt_stat.f_fsid;
1315         ump->um_fsfail_task = etp;
1316         return (0);
1317 out:
1318         if (fs != NULL) {
1319                 free(fs->fs_csp, M_UFSMNT);
1320                 free(fs->fs_si, M_UFSMNT);
1321                 free(fs, M_UFSMNT);
1322         }
1323         if (cp != NULL) {
1324                 g_topology_lock();
1325                 g_vfs_close(cp);
1326                 g_topology_unlock();
1327         }
1328         if (ump) {
1329                 mtx_destroy(UFS_MTX(ump));
1330                 if (mp->mnt_gjprovider != NULL) {
1331                         free(mp->mnt_gjprovider, M_UFSMNT);
1332                         mp->mnt_gjprovider = NULL;
1333                 }
1334                 free(ump, M_UFSMNT);
1335                 mp->mnt_data = NULL;
1336         }
1337         BO_LOCK(&odevvp->v_bufobj);
1338         odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1339         BO_UNLOCK(&odevvp->v_bufobj);
1340         atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1341         mntfs_freevp(devvp);
1342         dev_rel(dev);
1343         return (error);
1344 }
1345
1346 /*
1347  * A read function for use by filesystem-layer routines.
1348  */
1349 static int
1350 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1351 {
1352         struct buf *bp;
1353         int error;
1354
1355         KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1356         *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1357         if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1358             &bp)) != 0)
1359                 return (error);
1360         bcopy(bp->b_data, *bufp, size);
1361         bp->b_flags |= B_INVAL | B_NOCACHE;
1362         brelse(bp);
1363         return (0);
1364 }
1365
1366 static int bigcgs = 0;
1367 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1368
1369 /*
1370  * Sanity checks for loading old filesystem superblocks.
1371  * See ffs_oldfscompat_write below for unwound actions.
1372  *
1373  * XXX - Parts get retired eventually.
1374  * Unfortunately new bits get added.
1375  */
1376 static void
1377 ffs_oldfscompat_read(fs, ump, sblockloc)
1378         struct fs *fs;
1379         struct ufsmount *ump;
1380         ufs2_daddr_t sblockloc;
1381 {
1382         off_t maxfilesize;
1383
1384         /*
1385          * If not yet done, update fs_flags location and value of fs_sblockloc.
1386          */
1387         if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1388                 fs->fs_flags = fs->fs_old_flags;
1389                 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1390                 fs->fs_sblockloc = sblockloc;
1391         }
1392         /*
1393          * If not yet done, update UFS1 superblock with new wider fields.
1394          */
1395         if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1396                 fs->fs_maxbsize = fs->fs_bsize;
1397                 fs->fs_time = fs->fs_old_time;
1398                 fs->fs_size = fs->fs_old_size;
1399                 fs->fs_dsize = fs->fs_old_dsize;
1400                 fs->fs_csaddr = fs->fs_old_csaddr;
1401                 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1402                 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1403                 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1404                 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1405         }
1406         if (fs->fs_magic == FS_UFS1_MAGIC &&
1407             fs->fs_old_inodefmt < FS_44INODEFMT) {
1408                 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1409                 fs->fs_qbmask = ~fs->fs_bmask;
1410                 fs->fs_qfmask = ~fs->fs_fmask;
1411         }
1412         if (fs->fs_magic == FS_UFS1_MAGIC) {
1413                 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1414                 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1415                 if (fs->fs_maxfilesize > maxfilesize)
1416                         fs->fs_maxfilesize = maxfilesize;
1417         }
1418         /* Compatibility for old filesystems */
1419         if (fs->fs_avgfilesize <= 0)
1420                 fs->fs_avgfilesize = AVFILESIZ;
1421         if (fs->fs_avgfpdir <= 0)
1422                 fs->fs_avgfpdir = AFPDIR;
1423         if (bigcgs) {
1424                 fs->fs_save_cgsize = fs->fs_cgsize;
1425                 fs->fs_cgsize = fs->fs_bsize;
1426         }
1427 }
1428
1429 /*
1430  * Unwinding superblock updates for old filesystems.
1431  * See ffs_oldfscompat_read above for details.
1432  *
1433  * XXX - Parts get retired eventually.
1434  * Unfortunately new bits get added.
1435  */
1436 void
1437 ffs_oldfscompat_write(fs, ump)
1438         struct fs *fs;
1439         struct ufsmount *ump;
1440 {
1441
1442         /*
1443          * Copy back UFS2 updated fields that UFS1 inspects.
1444          */
1445         if (fs->fs_magic == FS_UFS1_MAGIC) {
1446                 fs->fs_old_time = fs->fs_time;
1447                 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1448                 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1449                 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1450                 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1451                 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1452         }
1453         if (bigcgs) {
1454                 fs->fs_cgsize = fs->fs_save_cgsize;
1455                 fs->fs_save_cgsize = 0;
1456         }
1457 }
1458
1459 /*
1460  * unmount system call
1461  */
1462 static int
1463 ffs_unmount(mp, mntflags)
1464         struct mount *mp;
1465         int mntflags;
1466 {
1467         struct thread *td;
1468         struct ufsmount *ump = VFSTOUFS(mp);
1469         struct fs *fs;
1470         int error, flags, susp;
1471 #ifdef UFS_EXTATTR
1472         int e_restart;
1473 #endif
1474
1475         flags = 0;
1476         td = curthread;
1477         fs = ump->um_fs;
1478         if (mntflags & MNT_FORCE)
1479                 flags |= FORCECLOSE;
1480         susp = fs->fs_ronly == 0;
1481 #ifdef UFS_EXTATTR
1482         if ((error = ufs_extattr_stop(mp, td))) {
1483                 if (error != EOPNOTSUPP)
1484                         printf("WARNING: unmount %s: ufs_extattr_stop "
1485                             "returned errno %d\n", mp->mnt_stat.f_mntonname,
1486                             error);
1487                 e_restart = 0;
1488         } else {
1489                 ufs_extattr_uepm_destroy(&ump->um_extattr);
1490                 e_restart = 1;
1491         }
1492 #endif
1493         if (susp) {
1494                 error = vfs_write_suspend_umnt(mp);
1495                 if (error != 0)
1496                         goto fail1;
1497         }
1498         if (MOUNTEDSOFTDEP(mp))
1499                 error = softdep_flushfiles(mp, flags, td);
1500         else
1501                 error = ffs_flushfiles(mp, flags, td);
1502         if (error != 0 && !ffs_fsfail_cleanup(ump, error))
1503                 goto fail;
1504
1505         UFS_LOCK(ump);
1506         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1507                 printf("WARNING: unmount %s: pending error: blocks %jd "
1508                     "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1509                     fs->fs_pendinginodes);
1510                 fs->fs_pendingblocks = 0;
1511                 fs->fs_pendinginodes = 0;
1512         }
1513         UFS_UNLOCK(ump);
1514         if (MOUNTEDSOFTDEP(mp))
1515                 softdep_unmount(mp);
1516         if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1517                 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1518                 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1519                 if (ffs_fsfail_cleanup(ump, error))
1520                         error = 0;
1521                 if (error != 0 && !ffs_fsfail_cleanup(ump, error)) {
1522                         fs->fs_clean = 0;
1523                         goto fail;
1524                 }
1525         }
1526         if (susp)
1527                 vfs_write_resume(mp, VR_START_WRITE);
1528         if (ump->um_trim_tq != NULL) {
1529                 while (ump->um_trim_inflight != 0)
1530                         pause("ufsutr", hz);
1531                 taskqueue_drain_all(ump->um_trim_tq);
1532                 taskqueue_free(ump->um_trim_tq);
1533                 free (ump->um_trimhash, M_TRIM);
1534         }
1535         g_topology_lock();
1536         if (ump->um_fsckpid > 0) {
1537                 /*
1538                  * Return to normal read-only mode.
1539                  */
1540                 error = g_access(ump->um_cp, 0, -1, 0);
1541                 ump->um_fsckpid = 0;
1542         }
1543         g_vfs_close(ump->um_cp);
1544         g_topology_unlock();
1545         BO_LOCK(&ump->um_odevvp->v_bufobj);
1546         ump->um_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1547         BO_UNLOCK(&ump->um_odevvp->v_bufobj);
1548         atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1549         mntfs_freevp(ump->um_devvp);
1550         vrele(ump->um_odevvp);
1551         dev_rel(ump->um_dev);
1552         mtx_destroy(UFS_MTX(ump));
1553         if (mp->mnt_gjprovider != NULL) {
1554                 free(mp->mnt_gjprovider, M_UFSMNT);
1555                 mp->mnt_gjprovider = NULL;
1556         }
1557         free(fs->fs_csp, M_UFSMNT);
1558         free(fs->fs_si, M_UFSMNT);
1559         free(fs, M_UFSMNT);
1560         if (ump->um_fsfail_task != NULL)
1561                 free(ump->um_fsfail_task, M_UFSMNT);
1562         free(ump, M_UFSMNT);
1563         mp->mnt_data = NULL;
1564         MNT_ILOCK(mp);
1565         mp->mnt_flag &= ~MNT_LOCAL;
1566         MNT_IUNLOCK(mp);
1567         if (td->td_su == mp) {
1568                 td->td_su = NULL;
1569                 vfs_rel(mp);
1570         }
1571         return (error);
1572
1573 fail:
1574         if (susp)
1575                 vfs_write_resume(mp, VR_START_WRITE);
1576 fail1:
1577 #ifdef UFS_EXTATTR
1578         if (e_restart) {
1579                 ufs_extattr_uepm_init(&ump->um_extattr);
1580 #ifdef UFS_EXTATTR_AUTOSTART
1581                 (void) ufs_extattr_autostart(mp, td);
1582 #endif
1583         }
1584 #endif
1585
1586         return (error);
1587 }
1588
1589 /*
1590  * Flush out all the files in a filesystem.
1591  */
1592 int
1593 ffs_flushfiles(mp, flags, td)
1594         struct mount *mp;
1595         int flags;
1596         struct thread *td;
1597 {
1598         struct ufsmount *ump;
1599         int qerror, error;
1600
1601         ump = VFSTOUFS(mp);
1602         qerror = 0;
1603 #ifdef QUOTA
1604         if (mp->mnt_flag & MNT_QUOTA) {
1605                 int i;
1606                 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1607                 if (error)
1608                         return (error);
1609                 for (i = 0; i < MAXQUOTAS; i++) {
1610                         error = quotaoff(td, mp, i);
1611                         if (error != 0) {
1612                                 if ((flags & EARLYFLUSH) == 0)
1613                                         return (error);
1614                                 else
1615                                         qerror = error;
1616                         }
1617                 }
1618
1619                 /*
1620                  * Here we fall through to vflush again to ensure that
1621                  * we have gotten rid of all the system vnodes, unless
1622                  * quotas must not be closed.
1623                  */
1624         }
1625 #endif
1626         ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1627         if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1628                 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1629                         return (error);
1630                 ffs_snapshot_unmount(mp);
1631                 flags |= FORCECLOSE;
1632                 /*
1633                  * Here we fall through to vflush again to ensure
1634                  * that we have gotten rid of all the system vnodes.
1635                  */
1636         }
1637
1638         /*
1639          * Do not close system files if quotas were not closed, to be
1640          * able to sync the remaining dquots.  The freeblks softupdate
1641          * workitems might hold a reference on a dquot, preventing
1642          * quotaoff() from completing.  Next round of
1643          * softdep_flushworklist() iteration should process the
1644          * blockers, allowing the next run of quotaoff() to finally
1645          * flush held dquots.
1646          *
1647          * Otherwise, flush all the files.
1648          */
1649         if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1650                 return (error);
1651
1652         /*
1653          * Flush filesystem metadata.
1654          */
1655         vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1656         error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1657         VOP_UNLOCK(ump->um_devvp);
1658         return (error);
1659 }
1660
1661 /*
1662  * Get filesystem statistics.
1663  */
1664 static int
1665 ffs_statfs(mp, sbp)
1666         struct mount *mp;
1667         struct statfs *sbp;
1668 {
1669         struct ufsmount *ump;
1670         struct fs *fs;
1671
1672         ump = VFSTOUFS(mp);
1673         fs = ump->um_fs;
1674         if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1675                 panic("ffs_statfs");
1676         sbp->f_version = STATFS_VERSION;
1677         sbp->f_bsize = fs->fs_fsize;
1678         sbp->f_iosize = fs->fs_bsize;
1679         sbp->f_blocks = fs->fs_dsize;
1680         UFS_LOCK(ump);
1681         sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1682             fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1683         sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1684             dbtofsb(fs, fs->fs_pendingblocks);
1685         sbp->f_files =  fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1686         sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1687         UFS_UNLOCK(ump);
1688         sbp->f_namemax = UFS_MAXNAMLEN;
1689         return (0);
1690 }
1691
1692 static bool
1693 sync_doupdate(struct inode *ip)
1694 {
1695
1696         return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1697             IN_UPDATE)) != 0);
1698 }
1699
1700 static int
1701 ffs_sync_lazy_filter(struct vnode *vp, void *arg __unused)
1702 {
1703         struct inode *ip;
1704
1705         /*
1706          * Flags are safe to access because ->v_data invalidation
1707          * is held off by listmtx.
1708          */
1709         if (vp->v_type == VNON)
1710                 return (false);
1711         ip = VTOI(vp);
1712         if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0)
1713                 return (false);
1714         return (true);
1715 }
1716
1717 /*
1718  * For a lazy sync, we only care about access times, quotas and the
1719  * superblock.  Other filesystem changes are already converted to
1720  * cylinder group blocks or inode blocks updates and are written to
1721  * disk by syncer.
1722  */
1723 static int
1724 ffs_sync_lazy(mp)
1725      struct mount *mp;
1726 {
1727         struct vnode *mvp, *vp;
1728         struct inode *ip;
1729         struct thread *td;
1730         int allerror, error;
1731
1732         allerror = 0;
1733         td = curthread;
1734         if ((mp->mnt_flag & MNT_NOATIME) != 0) {
1735 #ifdef QUOTA
1736                 qsync(mp);
1737 #endif
1738                 goto sbupdate;
1739         }
1740         MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, ffs_sync_lazy_filter, NULL) {
1741                 if (vp->v_type == VNON) {
1742                         VI_UNLOCK(vp);
1743                         continue;
1744                 }
1745                 ip = VTOI(vp);
1746
1747                 /*
1748                  * The IN_ACCESS flag is converted to IN_MODIFIED by
1749                  * ufs_close() and ufs_getattr() by the calls to
1750                  * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1751                  * Test also all the other timestamp flags too, to pick up
1752                  * any other cases that could be missed.
1753                  */
1754                 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1755                         VI_UNLOCK(vp);
1756                         continue;
1757                 }
1758                 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK)) != 0)
1759                         continue;
1760 #ifdef QUOTA
1761                 qsyncvp(vp);
1762 #endif
1763                 if (sync_doupdate(ip))
1764                         error = ffs_update(vp, 0);
1765                 if (error != 0)
1766                         allerror = error;
1767                 vput(vp);
1768         }
1769 sbupdate:
1770         if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1771             (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1772                 allerror = error;
1773         return (allerror);
1774 }
1775
1776 /*
1777  * Go through the disk queues to initiate sandbagged IO;
1778  * go through the inodes to write those that have been modified;
1779  * initiate the writing of the super block if it has been modified.
1780  *
1781  * Note: we are always called with the filesystem marked busy using
1782  * vfs_busy().
1783  */
1784 static int
1785 ffs_sync(mp, waitfor)
1786         struct mount *mp;
1787         int waitfor;
1788 {
1789         struct vnode *mvp, *vp, *devvp;
1790         struct thread *td;
1791         struct inode *ip;
1792         struct ufsmount *ump = VFSTOUFS(mp);
1793         struct fs *fs;
1794         int error, count, lockreq, allerror = 0;
1795         int suspend;
1796         int suspended;
1797         int secondary_writes;
1798         int secondary_accwrites;
1799         int softdep_deps;
1800         int softdep_accdeps;
1801         struct bufobj *bo;
1802
1803         suspend = 0;
1804         suspended = 0;
1805         td = curthread;
1806         fs = ump->um_fs;
1807         if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1808                 panic("%s: ffs_sync: modification on read-only filesystem",
1809                     fs->fs_fsmnt);
1810         if (waitfor == MNT_LAZY) {
1811                 if (!rebooting)
1812                         return (ffs_sync_lazy(mp));
1813                 waitfor = MNT_NOWAIT;
1814         }
1815
1816         /*
1817          * Write back each (modified) inode.
1818          */
1819         lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1820         if (waitfor == MNT_SUSPEND) {
1821                 suspend = 1;
1822                 waitfor = MNT_WAIT;
1823         }
1824         if (waitfor == MNT_WAIT)
1825                 lockreq = LK_EXCLUSIVE;
1826         lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1827 loop:
1828         /* Grab snapshot of secondary write counts */
1829         MNT_ILOCK(mp);
1830         secondary_writes = mp->mnt_secondary_writes;
1831         secondary_accwrites = mp->mnt_secondary_accwrites;
1832         MNT_IUNLOCK(mp);
1833
1834         /* Grab snapshot of softdep dependency counts */
1835         softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1836
1837         MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1838                 /*
1839                  * Depend on the vnode interlock to keep things stable enough
1840                  * for a quick test.  Since there might be hundreds of
1841                  * thousands of vnodes, we cannot afford even a subroutine
1842                  * call unless there's a good chance that we have work to do.
1843                  */
1844                 if (vp->v_type == VNON) {
1845                         VI_UNLOCK(vp);
1846                         continue;
1847                 }
1848                 ip = VTOI(vp);
1849                 if ((ip->i_flag &
1850                     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1851                     vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1852                         VI_UNLOCK(vp);
1853                         continue;
1854                 }
1855                 if ((error = vget(vp, lockreq)) != 0) {
1856                         if (error == ENOENT || error == ENOLCK) {
1857                                 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1858                                 goto loop;
1859                         }
1860                         continue;
1861                 }
1862 #ifdef QUOTA
1863                 qsyncvp(vp);
1864 #endif
1865                 for (;;) {
1866                         error = ffs_syncvnode(vp, waitfor, 0);
1867                         if (error == ERELOOKUP)
1868                                 continue;
1869                         if (error != 0)
1870                                 allerror = error;
1871                         break;
1872                 }
1873                 vput(vp);
1874         }
1875         /*
1876          * Force stale filesystem control information to be flushed.
1877          */
1878         if (waitfor == MNT_WAIT || rebooting) {
1879                 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1880                         allerror = error;
1881                 if (ffs_fsfail_cleanup(ump, allerror))
1882                         allerror = 0;
1883                 /* Flushed work items may create new vnodes to clean */
1884                 if (allerror == 0 && count)
1885                         goto loop;
1886         }
1887
1888         devvp = ump->um_devvp;
1889         bo = &devvp->v_bufobj;
1890         BO_LOCK(bo);
1891         if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1892                 BO_UNLOCK(bo);
1893                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1894                 error = VOP_FSYNC(devvp, waitfor, td);
1895                 VOP_UNLOCK(devvp);
1896                 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1897                         error = ffs_sbupdate(ump, waitfor, 0);
1898                 if (error != 0)
1899                         allerror = error;
1900                 if (ffs_fsfail_cleanup(ump, allerror))
1901                         allerror = 0;
1902                 if (allerror == 0 && waitfor == MNT_WAIT)
1903                         goto loop;
1904         } else if (suspend != 0) {
1905                 if (softdep_check_suspend(mp,
1906                                           devvp,
1907                                           softdep_deps,
1908                                           softdep_accdeps,
1909                                           secondary_writes,
1910                                           secondary_accwrites) != 0) {
1911                         MNT_IUNLOCK(mp);
1912                         goto loop;      /* More work needed */
1913                 }
1914                 mtx_assert(MNT_MTX(mp), MA_OWNED);
1915                 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1916                 MNT_IUNLOCK(mp);
1917                 suspended = 1;
1918         } else
1919                 BO_UNLOCK(bo);
1920         /*
1921          * Write back modified superblock.
1922          */
1923         if (fs->fs_fmod != 0 &&
1924             (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1925                 allerror = error;
1926         if (ffs_fsfail_cleanup(ump, allerror))
1927                 allerror = 0;
1928         return (allerror);
1929 }
1930
1931 int
1932 ffs_vget(mp, ino, flags, vpp)
1933         struct mount *mp;
1934         ino_t ino;
1935         int flags;
1936         struct vnode **vpp;
1937 {
1938         return (ffs_vgetf(mp, ino, flags, vpp, 0));
1939 }
1940
1941 int
1942 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1943         struct mount *mp;
1944         ino_t ino;
1945         int flags;
1946         struct vnode **vpp;
1947         int ffs_flags;
1948 {
1949         struct fs *fs;
1950         struct inode *ip;
1951         struct ufsmount *ump;
1952         struct buf *bp;
1953         struct vnode *vp;
1954         daddr_t dbn;
1955         int error;
1956
1957         MPASS((ffs_flags & (FFSV_REPLACE | FFSV_REPLACE_DOOMED)) == 0 ||
1958             (flags & LK_EXCLUSIVE) != 0);
1959
1960         error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1961         if (error != 0)
1962                 return (error);
1963         if (*vpp != NULL) {
1964                 if ((ffs_flags & FFSV_REPLACE) == 0 ||
1965                     ((ffs_flags & FFSV_REPLACE_DOOMED) == 0 ||
1966                     !VN_IS_DOOMED(*vpp)))
1967                         return (0);
1968                 vgone(*vpp);
1969                 vput(*vpp);
1970         }
1971
1972         /*
1973          * We must promote to an exclusive lock for vnode creation.  This
1974          * can happen if lookup is passed LOCKSHARED.
1975          */
1976         if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1977                 flags &= ~LK_TYPE_MASK;
1978                 flags |= LK_EXCLUSIVE;
1979         }
1980
1981         /*
1982          * We do not lock vnode creation as it is believed to be too
1983          * expensive for such rare case as simultaneous creation of vnode
1984          * for same ino by different processes. We just allow them to race
1985          * and check later to decide who wins. Let the race begin!
1986          */
1987
1988         ump = VFSTOUFS(mp);
1989         fs = ump->um_fs;
1990         ip = uma_zalloc_smr(uma_inode, M_WAITOK | M_ZERO);
1991
1992         /* Allocate a new vnode/inode. */
1993         error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1994             &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1995         if (error) {
1996                 *vpp = NULL;
1997                 uma_zfree_smr(uma_inode, ip);
1998                 return (error);
1999         }
2000         /*
2001          * FFS supports recursive locking.
2002          */
2003         lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
2004         VN_LOCK_AREC(vp);
2005         vp->v_data = ip;
2006         vp->v_bufobj.bo_bsize = fs->fs_bsize;
2007         ip->i_vnode = vp;
2008         ip->i_ump = ump;
2009         ip->i_number = ino;
2010         ip->i_ea_refs = 0;
2011         ip->i_nextclustercg = -1;
2012         ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
2013         ip->i_mode = 0; /* ensure error cases below throw away vnode */
2014 #ifdef DIAGNOSTIC
2015         ufs_init_trackers(ip);
2016 #endif
2017 #ifdef QUOTA
2018         {
2019                 int i;
2020                 for (i = 0; i < MAXQUOTAS; i++)
2021                         ip->i_dquot[i] = NODQUOT;
2022         }
2023 #endif
2024
2025         if (ffs_flags & FFSV_FORCEINSMQ)
2026                 vp->v_vflag |= VV_FORCEINSMQ;
2027         error = insmntque(vp, mp);
2028         if (error != 0) {
2029                 uma_zfree_smr(uma_inode, ip);
2030                 *vpp = NULL;
2031                 return (error);
2032         }
2033         vp->v_vflag &= ~VV_FORCEINSMQ;
2034         error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
2035         if (error != 0)
2036                 return (error);
2037         if (*vpp != NULL) {
2038                 /*
2039                  * Calls from ffs_valloc() (i.e. FFSV_REPLACE set)
2040                  * operate on empty inode, which must not be found by
2041                  * other threads until fully filled.  Vnode for empty
2042                  * inode must be not re-inserted on the hash by other
2043                  * thread, after removal by us at the beginning.
2044                  */
2045                 MPASS((ffs_flags & FFSV_REPLACE) == 0);
2046                 return (0);
2047         }
2048
2049         /* Read in the disk contents for the inode, copy into the inode. */
2050         dbn = fsbtodb(fs, ino_to_fsba(fs, ino));
2051         error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
2052             NULL, NULL, 0, NOCRED, 0, NULL, &bp);
2053         if (error != 0) {
2054                 /*
2055                  * The inode does not contain anything useful, so it would
2056                  * be misleading to leave it on its hash chain. With mode
2057                  * still zero, it will be unlinked and returned to the free
2058                  * list by vput().
2059                  */
2060                 vgone(vp);
2061                 vput(vp);
2062                 *vpp = NULL;
2063                 return (error);
2064         }
2065         if (I_IS_UFS1(ip))
2066                 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
2067         else
2068                 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
2069         if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) {
2070                 bqrelse(bp);
2071                 vgone(vp);
2072                 vput(vp);
2073                 *vpp = NULL;
2074                 return (error);
2075         }
2076         if (DOINGSOFTDEP(vp))
2077                 softdep_load_inodeblock(ip);
2078         else
2079                 ip->i_effnlink = ip->i_nlink;
2080         bqrelse(bp);
2081
2082         /*
2083          * Initialize the vnode from the inode, check for aliases.
2084          * Note that the underlying vnode may have changed.
2085          */
2086         error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
2087             &vp);
2088         if (error) {
2089                 vgone(vp);
2090                 vput(vp);
2091                 *vpp = NULL;
2092                 return (error);
2093         }
2094
2095         /*
2096          * Finish inode initialization.
2097          */
2098         if (vp->v_type != VFIFO) {
2099                 /* FFS supports shared locking for all files except fifos. */
2100                 VN_LOCK_ASHARE(vp);
2101         }
2102
2103         /*
2104          * Set up a generation number for this inode if it does not
2105          * already have one. This should only happen on old filesystems.
2106          */
2107         if (ip->i_gen == 0) {
2108                 while (ip->i_gen == 0)
2109                         ip->i_gen = arc4random();
2110                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
2111                         UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
2112                         DIP_SET(ip, i_gen, ip->i_gen);
2113                 }
2114         }
2115 #ifdef MAC
2116         if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
2117                 /*
2118                  * If this vnode is already allocated, and we're running
2119                  * multi-label, attempt to perform a label association
2120                  * from the extended attributes on the inode.
2121                  */
2122                 error = mac_vnode_associate_extattr(mp, vp);
2123                 if (error) {
2124                         /* ufs_inactive will release ip->i_devvp ref. */
2125                         vgone(vp);
2126                         vput(vp);
2127                         *vpp = NULL;
2128                         return (error);
2129                 }
2130         }
2131 #endif
2132
2133         *vpp = vp;
2134         return (0);
2135 }
2136
2137 /*
2138  * File handle to vnode
2139  *
2140  * Have to be really careful about stale file handles:
2141  * - check that the inode number is valid
2142  * - for UFS2 check that the inode number is initialized
2143  * - call ffs_vget() to get the locked inode
2144  * - check for an unallocated inode (i_mode == 0)
2145  * - check that the given client host has export rights and return
2146  *   those rights via. exflagsp and credanonp
2147  */
2148 static int
2149 ffs_fhtovp(mp, fhp, flags, vpp)
2150         struct mount *mp;
2151         struct fid *fhp;
2152         int flags;
2153         struct vnode **vpp;
2154 {
2155         struct ufid *ufhp;
2156
2157         ufhp = (struct ufid *)fhp;
2158         return (ffs_inotovp(mp, ufhp->ufid_ino, ufhp->ufid_gen, flags,
2159             vpp, 0));
2160 }
2161
2162 int
2163 ffs_inotovp(mp, ino, gen, lflags, vpp, ffs_flags)
2164         struct mount *mp;
2165         ino_t ino;
2166         u_int64_t gen;
2167         int lflags;
2168         struct vnode **vpp;
2169         int ffs_flags;
2170 {
2171         struct ufsmount *ump;
2172         struct vnode *nvp;
2173         struct inode *ip;
2174         struct fs *fs;
2175         struct cg *cgp;
2176         struct buf *bp;
2177         u_int cg;
2178         int error;
2179
2180         ump = VFSTOUFS(mp);
2181         fs = ump->um_fs;
2182         *vpp = NULL;
2183
2184         if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
2185                 return (ESTALE);
2186
2187         /*
2188          * Need to check if inode is initialized because UFS2 does lazy
2189          * initialization and nfs_fhtovp can offer arbitrary inode numbers.
2190          */
2191         if (fs->fs_magic == FS_UFS2_MAGIC) {
2192                 cg = ino_to_cg(fs, ino);
2193                 error = ffs_getcg(fs, ump->um_devvp, cg, 0, &bp, &cgp);
2194                 if (error != 0)
2195                         return (error);
2196                 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
2197                         brelse(bp);
2198                         return (ESTALE);
2199                 }
2200                 brelse(bp);
2201         }
2202
2203         error = ffs_vgetf(mp, ino, lflags, &nvp, ffs_flags);
2204         if (error != 0)
2205                 return (error);
2206
2207         ip = VTOI(nvp);
2208         if (ip->i_mode == 0 || ip->i_gen != gen || ip->i_effnlink <= 0) {
2209                 if (ip->i_mode == 0)
2210                         vgone(nvp);
2211                 vput(nvp);
2212                 return (ESTALE);
2213         }
2214
2215         vnode_create_vobject(nvp, DIP(ip, i_size), curthread);
2216         *vpp = nvp;
2217         return (0);
2218 }
2219
2220 /*
2221  * Initialize the filesystem.
2222  */
2223 static int
2224 ffs_init(vfsp)
2225         struct vfsconf *vfsp;
2226 {
2227
2228         ffs_susp_initialize();
2229         softdep_initialize();
2230         return (ufs_init(vfsp));
2231 }
2232
2233 /*
2234  * Undo the work of ffs_init().
2235  */
2236 static int
2237 ffs_uninit(vfsp)
2238         struct vfsconf *vfsp;
2239 {
2240         int ret;
2241
2242         ret = ufs_uninit(vfsp);
2243         softdep_uninitialize();
2244         ffs_susp_uninitialize();
2245         taskqueue_drain_all(taskqueue_thread);
2246         return (ret);
2247 }
2248
2249 /*
2250  * Structure used to pass information from ffs_sbupdate to its
2251  * helper routine ffs_use_bwrite.
2252  */
2253 struct devfd {
2254         struct ufsmount *ump;
2255         struct buf      *sbbp;
2256         int              waitfor;
2257         int              suspended;
2258         int              error;
2259 };
2260
2261 /*
2262  * Write a superblock and associated information back to disk.
2263  */
2264 int
2265 ffs_sbupdate(ump, waitfor, suspended)
2266         struct ufsmount *ump;
2267         int waitfor;
2268         int suspended;
2269 {
2270         struct fs *fs;
2271         struct buf *sbbp;
2272         struct devfd devfd;
2273
2274         fs = ump->um_fs;
2275         if (fs->fs_ronly == 1 &&
2276             (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
2277             (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
2278                 panic("ffs_sbupdate: write read-only filesystem");
2279         /*
2280          * We use the superblock's buf to serialize calls to ffs_sbupdate().
2281          */
2282         sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
2283             (int)fs->fs_sbsize, 0, 0, 0);
2284         /*
2285          * Initialize info needed for write function.
2286          */
2287         devfd.ump = ump;
2288         devfd.sbbp = sbbp;
2289         devfd.waitfor = waitfor;
2290         devfd.suspended = suspended;
2291         devfd.error = 0;
2292         return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
2293 }
2294
2295 /*
2296  * Write function for use by filesystem-layer routines.
2297  */
2298 static int
2299 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
2300 {
2301         struct devfd *devfdp;
2302         struct ufsmount *ump;
2303         struct buf *bp;
2304         struct fs *fs;
2305         int error;
2306
2307         devfdp = devfd;
2308         ump = devfdp->ump;
2309         fs = ump->um_fs;
2310         /*
2311          * Writing the superblock summary information.
2312          */
2313         if (loc != fs->fs_sblockloc) {
2314                 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
2315                 bcopy(buf, bp->b_data, (u_int)size);
2316                 if (devfdp->suspended)
2317                         bp->b_flags |= B_VALIDSUSPWRT;
2318                 if (devfdp->waitfor != MNT_WAIT)
2319                         bawrite(bp);
2320                 else if ((error = bwrite(bp)) != 0)
2321                         devfdp->error = error;
2322                 return (0);
2323         }
2324         /*
2325          * Writing the superblock itself. We need to do special checks for it.
2326          */
2327         bp = devfdp->sbbp;
2328         if (ffs_fsfail_cleanup(ump, devfdp->error))
2329                 devfdp->error = 0;
2330         if (devfdp->error != 0) {
2331                 brelse(bp);
2332                 return (devfdp->error);
2333         }
2334         if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
2335             (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2336                 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2337                     fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
2338                 fs->fs_sblockloc = SBLOCK_UFS1;
2339         }
2340         if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
2341             (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2342                 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2343                     fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
2344                 fs->fs_sblockloc = SBLOCK_UFS2;
2345         }
2346         if (MOUNTEDSOFTDEP(ump->um_mountp))
2347                 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
2348         bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
2349         fs = (struct fs *)bp->b_data;
2350         ffs_oldfscompat_write(fs, ump);
2351         fs->fs_si = NULL;
2352         /* Recalculate the superblock hash */
2353         fs->fs_ckhash = ffs_calc_sbhash(fs);
2354         if (devfdp->suspended)
2355                 bp->b_flags |= B_VALIDSUSPWRT;
2356         if (devfdp->waitfor != MNT_WAIT)
2357                 bawrite(bp);
2358         else if ((error = bwrite(bp)) != 0)
2359                 devfdp->error = error;
2360         return (devfdp->error);
2361 }
2362
2363 static int
2364 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2365         int attrnamespace, const char *attrname)
2366 {
2367
2368 #ifdef UFS_EXTATTR
2369         return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2370             attrname));
2371 #else
2372         return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2373             attrname));
2374 #endif
2375 }
2376
2377 static void
2378 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2379 {
2380
2381         if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2382                 uma_zfree(uma_ufs1, ip->i_din1);
2383         else if (ip->i_din2 != NULL)
2384                 uma_zfree(uma_ufs2, ip->i_din2);
2385         uma_zfree_smr(uma_inode, ip);
2386 }
2387
2388 static int dobkgrdwrite = 1;
2389 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2390     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2391
2392 /*
2393  * Complete a background write started from bwrite.
2394  */
2395 static void
2396 ffs_backgroundwritedone(struct buf *bp)
2397 {
2398         struct bufobj *bufobj;
2399         struct buf *origbp;
2400
2401 #ifdef SOFTUPDATES
2402         if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) != 0)
2403                 softdep_handle_error(bp);
2404 #endif
2405
2406         /*
2407          * Find the original buffer that we are writing.
2408          */
2409         bufobj = bp->b_bufobj;
2410         BO_LOCK(bufobj);
2411         if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2412                 panic("backgroundwritedone: lost buffer");
2413
2414         /*
2415          * We should mark the cylinder group buffer origbp as
2416          * dirty, to not lose the failed write.
2417          */
2418         if ((bp->b_ioflags & BIO_ERROR) != 0)
2419                 origbp->b_vflags |= BV_BKGRDERR;
2420         BO_UNLOCK(bufobj);
2421         /*
2422          * Process dependencies then return any unfinished ones.
2423          */
2424         if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2425                 buf_complete(bp);
2426 #ifdef SOFTUPDATES
2427         if (!LIST_EMPTY(&bp->b_dep))
2428                 softdep_move_dependencies(bp, origbp);
2429 #endif
2430         /*
2431          * This buffer is marked B_NOCACHE so when it is released
2432          * by biodone it will be tossed.  Clear B_IOSTARTED in case of error.
2433          */
2434         bp->b_flags |= B_NOCACHE;
2435         bp->b_flags &= ~(B_CACHE | B_IOSTARTED);
2436         pbrelvp(bp);
2437
2438         /*
2439          * Prevent brelse() from trying to keep and re-dirtying bp on
2440          * errors. It causes b_bufobj dereference in
2441          * bdirty()/reassignbuf(), and b_bufobj was cleared in
2442          * pbrelvp() above.
2443          */
2444         if ((bp->b_ioflags & BIO_ERROR) != 0)
2445                 bp->b_flags |= B_INVAL;
2446         bufdone(bp);
2447         BO_LOCK(bufobj);
2448         /*
2449          * Clear the BV_BKGRDINPROG flag in the original buffer
2450          * and awaken it if it is waiting for the write to complete.
2451          * If BV_BKGRDINPROG is not set in the original buffer it must
2452          * have been released and re-instantiated - which is not legal.
2453          */
2454         KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2455             ("backgroundwritedone: lost buffer2"));
2456         origbp->b_vflags &= ~BV_BKGRDINPROG;
2457         if (origbp->b_vflags & BV_BKGRDWAIT) {
2458                 origbp->b_vflags &= ~BV_BKGRDWAIT;
2459                 wakeup(&origbp->b_xflags);
2460         }
2461         BO_UNLOCK(bufobj);
2462 }
2463
2464 /*
2465  * Write, release buffer on completion.  (Done by iodone
2466  * if async).  Do not bother writing anything if the buffer
2467  * is invalid.
2468  *
2469  * Note that we set B_CACHE here, indicating that buffer is
2470  * fully valid and thus cacheable.  This is true even of NFS
2471  * now so we set it generally.  This could be set either here
2472  * or in biodone() since the I/O is synchronous.  We put it
2473  * here.
2474  */
2475 static int
2476 ffs_bufwrite(struct buf *bp)
2477 {
2478         struct buf *newbp;
2479         struct cg *cgp;
2480
2481         CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2482         if (bp->b_flags & B_INVAL) {
2483                 brelse(bp);
2484                 return (0);
2485         }
2486
2487         if (!BUF_ISLOCKED(bp))
2488                 panic("bufwrite: buffer is not busy???");
2489         /*
2490          * If a background write is already in progress, delay
2491          * writing this block if it is asynchronous. Otherwise
2492          * wait for the background write to complete.
2493          */
2494         BO_LOCK(bp->b_bufobj);
2495         if (bp->b_vflags & BV_BKGRDINPROG) {
2496                 if (bp->b_flags & B_ASYNC) {
2497                         BO_UNLOCK(bp->b_bufobj);
2498                         bdwrite(bp);
2499                         return (0);
2500                 }
2501                 bp->b_vflags |= BV_BKGRDWAIT;
2502                 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2503                     "bwrbg", 0);
2504                 if (bp->b_vflags & BV_BKGRDINPROG)
2505                         panic("bufwrite: still writing");
2506         }
2507         bp->b_vflags &= ~BV_BKGRDERR;
2508         BO_UNLOCK(bp->b_bufobj);
2509
2510         /*
2511          * If this buffer is marked for background writing and we
2512          * do not have to wait for it, make a copy and write the
2513          * copy so as to leave this buffer ready for further use.
2514          *
2515          * This optimization eats a lot of memory.  If we have a page
2516          * or buffer shortfall we can't do it.
2517          */
2518         if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2519             (bp->b_flags & B_ASYNC) &&
2520             !vm_page_count_severe() &&
2521             !buf_dirty_count_severe()) {
2522                 KASSERT(bp->b_iodone == NULL,
2523                     ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2524
2525                 /* get a new block */
2526                 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2527                 if (newbp == NULL)
2528                         goto normal_write;
2529
2530                 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2531                 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2532                 BO_LOCK(bp->b_bufobj);
2533                 bp->b_vflags |= BV_BKGRDINPROG;
2534                 BO_UNLOCK(bp->b_bufobj);
2535                 newbp->b_xflags |=
2536                     (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2537                 newbp->b_lblkno = bp->b_lblkno;
2538                 newbp->b_blkno = bp->b_blkno;
2539                 newbp->b_offset = bp->b_offset;
2540                 newbp->b_iodone = ffs_backgroundwritedone;
2541                 newbp->b_flags |= B_ASYNC;
2542                 newbp->b_flags &= ~B_INVAL;
2543                 pbgetvp(bp->b_vp, newbp);
2544
2545 #ifdef SOFTUPDATES
2546                 /*
2547                  * Move over the dependencies.  If there are rollbacks,
2548                  * leave the parent buffer dirtied as it will need to
2549                  * be written again.
2550                  */
2551                 if (LIST_EMPTY(&bp->b_dep) ||
2552                     softdep_move_dependencies(bp, newbp) == 0)
2553                         bundirty(bp);
2554 #else
2555                 bundirty(bp);
2556 #endif
2557
2558                 /*
2559                  * Initiate write on the copy, release the original.  The
2560                  * BKGRDINPROG flag prevents it from going away until 
2561                  * the background write completes. We have to recalculate
2562                  * its check hash in case the buffer gets freed and then
2563                  * reconstituted from the buffer cache during a later read.
2564                  */
2565                 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2566                         cgp = (struct cg *)bp->b_data;
2567                         cgp->cg_ckhash = 0;
2568                         cgp->cg_ckhash =
2569                             calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2570                 }
2571                 bqrelse(bp);
2572                 bp = newbp;
2573         } else
2574                 /* Mark the buffer clean */
2575                 bundirty(bp);
2576
2577         /* Let the normal bufwrite do the rest for us */
2578 normal_write:
2579         /*
2580          * If we are writing a cylinder group, update its time.
2581          */
2582         if ((bp->b_xflags & BX_CYLGRP) != 0) {
2583                 cgp = (struct cg *)bp->b_data;
2584                 cgp->cg_old_time = cgp->cg_time = time_second;
2585         }
2586         return (bufwrite(bp));
2587 }
2588
2589 static void
2590 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2591 {
2592         struct vnode *vp;
2593         struct buf *tbp;
2594         int error, nocopy;
2595
2596         /*
2597          * This is the bufobj strategy for the private VCHR vnodes
2598          * used by FFS to access the underlying storage device.
2599          * We override the default bufobj strategy and thus bypass
2600          * VOP_STRATEGY() for these vnodes.
2601          */
2602         vp = bo2vnode(bo);
2603         KASSERT(bp->b_vp == NULL || bp->b_vp->v_type != VCHR ||
2604             bp->b_vp->v_rdev == NULL ||
2605             bp->b_vp->v_rdev->si_mountpt == NULL ||
2606             VFSTOUFS(bp->b_vp->v_rdev->si_mountpt) == NULL ||
2607             vp == VFSTOUFS(bp->b_vp->v_rdev->si_mountpt)->um_devvp,
2608             ("ffs_geom_strategy() with wrong vp"));
2609         if (bp->b_iocmd == BIO_WRITE) {
2610                 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2611                     bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2612                     (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2613                         panic("ffs_geom_strategy: bad I/O");
2614                 nocopy = bp->b_flags & B_NOCOPY;
2615                 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2616                 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2617                     vp->v_rdev->si_snapdata != NULL) {
2618                         if ((bp->b_flags & B_CLUSTER) != 0) {
2619                                 runningbufwakeup(bp);
2620                                 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2621                                               b_cluster.cluster_entry) {
2622                                         error = ffs_copyonwrite(vp, tbp);
2623                                         if (error != 0 &&
2624                                             error != EOPNOTSUPP) {
2625                                                 bp->b_error = error;
2626                                                 bp->b_ioflags |= BIO_ERROR;
2627                                                 bp->b_flags &= ~B_BARRIER;
2628                                                 bufdone(bp);
2629                                                 return;
2630                                         }
2631                                 }
2632                                 bp->b_runningbufspace = bp->b_bufsize;
2633                                 atomic_add_long(&runningbufspace,
2634                                                bp->b_runningbufspace);
2635                         } else {
2636                                 error = ffs_copyonwrite(vp, bp);
2637                                 if (error != 0 && error != EOPNOTSUPP) {
2638                                         bp->b_error = error;
2639                                         bp->b_ioflags |= BIO_ERROR;
2640                                         bp->b_flags &= ~B_BARRIER;
2641                                         bufdone(bp);
2642                                         return;
2643                                 }
2644                         }
2645                 }
2646 #ifdef SOFTUPDATES
2647                 if ((bp->b_flags & B_CLUSTER) != 0) {
2648                         TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2649                                       b_cluster.cluster_entry) {
2650                                 if (!LIST_EMPTY(&tbp->b_dep))
2651                                         buf_start(tbp);
2652                         }
2653                 } else {
2654                         if (!LIST_EMPTY(&bp->b_dep))
2655                                 buf_start(bp);
2656                 }
2657
2658 #endif
2659                 /*
2660                  * Check for metadata that needs check-hashes and update them.
2661                  */
2662                 switch (bp->b_xflags & BX_FSPRIV) {
2663                 case BX_CYLGRP:
2664                         ((struct cg *)bp->b_data)->cg_ckhash = 0;
2665                         ((struct cg *)bp->b_data)->cg_ckhash =
2666                             calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2667                         break;
2668
2669                 case BX_SUPERBLOCK:
2670                 case BX_INODE:
2671                 case BX_INDIR:
2672                 case BX_DIR:
2673                         printf("Check-hash write is unimplemented!!!\n");
2674                         break;
2675
2676                 case 0:
2677                         break;
2678
2679                 default:
2680                         printf("multiple buffer types 0x%b\n",
2681                             (u_int)(bp->b_xflags & BX_FSPRIV),
2682                             PRINT_UFS_BUF_XFLAGS);
2683                         break;
2684                 }
2685         }
2686         if (bp->b_iocmd != BIO_READ && ffs_enxio_enable)
2687                 bp->b_xflags |= BX_CVTENXIO;
2688         g_vfs_strategy(bo, bp);
2689 }
2690
2691 int
2692 ffs_own_mount(const struct mount *mp)
2693 {
2694
2695         if (mp->mnt_op == &ufs_vfsops)
2696                 return (1);
2697         return (0);
2698 }
2699
2700 #ifdef  DDB
2701 #ifdef SOFTUPDATES
2702
2703 /* defined in ffs_softdep.c */
2704 extern void db_print_ffs(struct ufsmount *ump);
2705
2706 DB_SHOW_COMMAND(ffs, db_show_ffs)
2707 {
2708         struct mount *mp;
2709         struct ufsmount *ump;
2710
2711         if (have_addr) {
2712                 ump = VFSTOUFS((struct mount *)addr);
2713                 db_print_ffs(ump);
2714                 return;
2715         }
2716
2717         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2718                 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2719                         db_print_ffs(VFSTOUFS(mp));
2720         }
2721 }
2722
2723 #endif  /* SOFTUPDATES */
2724 #endif  /* DDB */