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