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