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