ffs_vfsops.c revision 1.241 1 /* $NetBSD: ffs_vfsops.c,v 1.241 2008/11/13 11:09:45 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1989, 1991, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.241 2008/11/13 11:09:45 ad Exp $");
65
66 #if defined(_KERNEL_OPT)
67 #include "opt_ffs.h"
68 #include "opt_quota.h"
69 #include "opt_softdep.h"
70 #include "opt_wapbl.h"
71 #endif
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/namei.h>
76 #include <sys/proc.h>
77 #include <sys/kernel.h>
78 #include <sys/vnode.h>
79 #include <sys/socket.h>
80 #include <sys/mount.h>
81 #include <sys/buf.h>
82 #include <sys/device.h>
83 #include <sys/mbuf.h>
84 #include <sys/file.h>
85 #include <sys/disklabel.h>
86 #include <sys/ioctl.h>
87 #include <sys/errno.h>
88 #include <sys/malloc.h>
89 #include <sys/pool.h>
90 #include <sys/lock.h>
91 #include <sys/sysctl.h>
92 #include <sys/conf.h>
93 #include <sys/kauth.h>
94 #include <sys/wapbl.h>
95 #include <sys/fstrans.h>
96 #include <sys/module.h>
97
98 #include <miscfs/genfs/genfs.h>
99 #include <miscfs/specfs/specdev.h>
100
101 #include <ufs/ufs/quota.h>
102 #include <ufs/ufs/ufsmount.h>
103 #include <ufs/ufs/inode.h>
104 #include <ufs/ufs/dir.h>
105 #include <ufs/ufs/ufs_extern.h>
106 #include <ufs/ufs/ufs_bswap.h>
107 #include <ufs/ufs/ufs_wapbl.h>
108
109 #include <ufs/ffs/fs.h>
110 #include <ufs/ffs/ffs_extern.h>
111
112 MODULE(MODULE_CLASS_VFS, ffs, NULL);
113
114 static struct sysctllog *ffs_sysctl_log;
115
116 /* how many times ffs_init() was called */
117 int ffs_initcount = 0;
118
119 extern kmutex_t ufs_hashlock;
120
121 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
122 extern const struct vnodeopv_desc ffs_specop_opv_desc;
123 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
124
125 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
126 &ffs_vnodeop_opv_desc,
127 &ffs_specop_opv_desc,
128 &ffs_fifoop_opv_desc,
129 NULL,
130 };
131
132 struct vfsops ffs_vfsops = {
133 MOUNT_FFS,
134 sizeof (struct ufs_args),
135 ffs_mount,
136 ufs_start,
137 ffs_unmount,
138 ufs_root,
139 ufs_quotactl,
140 ffs_statvfs,
141 ffs_sync,
142 ffs_vget,
143 ffs_fhtovp,
144 ffs_vptofh,
145 ffs_init,
146 ffs_reinit,
147 ffs_done,
148 ffs_mountroot,
149 ffs_snapshot,
150 ffs_extattrctl,
151 ffs_suspendctl,
152 genfs_renamelock_enter,
153 genfs_renamelock_exit,
154 ffs_full_fsync,
155 ffs_vnodeopv_descs,
156 0,
157 { NULL, NULL },
158 };
159
160 static const struct genfs_ops ffs_genfsops = {
161 .gop_size = ffs_gop_size,
162 .gop_alloc = ufs_gop_alloc,
163 .gop_write = genfs_gop_write,
164 .gop_markupdate = ufs_gop_markupdate,
165 };
166
167 static const struct ufs_ops ffs_ufsops = {
168 .uo_itimes = ffs_itimes,
169 .uo_update = ffs_update,
170 .uo_truncate = ffs_truncate,
171 .uo_valloc = ffs_valloc,
172 .uo_vfree = ffs_vfree,
173 .uo_balloc = ffs_balloc,
174 .uo_unmark_vnode = (void (*)(vnode_t *))nullop,
175 };
176
177 static int
178 ffs_modcmd(modcmd_t cmd, void *arg)
179 {
180 int error;
181
182 #if 0
183 extern int doasyncfree;
184 #endif
185 extern int ffs_log_changeopt;
186
187 switch (cmd) {
188 case MODULE_CMD_INIT:
189 error = vfs_attach(&ffs_vfsops);
190 if (error != 0)
191 break;
192
193 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
194 CTLFLAG_PERMANENT,
195 CTLTYPE_NODE, "vfs", NULL,
196 NULL, 0, NULL, 0,
197 CTL_VFS, CTL_EOL);
198 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
199 CTLFLAG_PERMANENT,
200 CTLTYPE_NODE, "ffs",
201 SYSCTL_DESCR("Berkeley Fast File System"),
202 NULL, 0, NULL, 0,
203 CTL_VFS, 1, CTL_EOL);
204
205 /*
206 * @@@ should we even bother with these first three?
207 */
208 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
209 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
210 CTLTYPE_INT, "doclusterread", NULL,
211 sysctl_notavail, 0, NULL, 0,
212 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
213 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
214 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
215 CTLTYPE_INT, "doclusterwrite", NULL,
216 sysctl_notavail, 0, NULL, 0,
217 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
218 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
219 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
220 CTLTYPE_INT, "doreallocblks", NULL,
221 sysctl_notavail, 0, NULL, 0,
222 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
223 #if 0
224 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
225 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
226 CTLTYPE_INT, "doasyncfree",
227 SYSCTL_DESCR("Release dirty blocks asynchronously"),
228 NULL, 0, &doasyncfree, 0,
229 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
230 #endif
231 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
232 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
233 CTLTYPE_INT, "log_changeopt",
234 SYSCTL_DESCR("Log changes in optimization strategy"),
235 NULL, 0, &ffs_log_changeopt, 0,
236 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
237 break;
238 case MODULE_CMD_FINI:
239 error = vfs_detach(&ffs_vfsops);
240 if (error != 0)
241 break;
242 sysctl_teardown(&ffs_sysctl_log);
243 break;
244 default:
245 error = ENOTTY;
246 break;
247 }
248
249 return (error);
250 }
251
252 pool_cache_t ffs_inode_cache;
253 pool_cache_t ffs_dinode1_cache;
254 pool_cache_t ffs_dinode2_cache;
255
256 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
257 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
258
259 /*
260 * Called by main() when ffs is going to be mounted as root.
261 */
262
263 int
264 ffs_mountroot(void)
265 {
266 struct fs *fs;
267 struct mount *mp;
268 struct lwp *l = curlwp; /* XXX */
269 struct ufsmount *ump;
270 int error;
271
272 if (device_class(root_device) != DV_DISK)
273 return (ENODEV);
274
275 if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
276 vrele(rootvp);
277 return (error);
278 }
279
280 /*
281 * We always need to be able to mount the root file system.
282 */
283 mp->mnt_flag |= MNT_FORCE;
284 if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
285 vfs_unbusy(mp, false, NULL);
286 vfs_destroy(mp);
287 return (error);
288 }
289 mp->mnt_flag &= ~MNT_FORCE;
290 mutex_enter(&mountlist_lock);
291 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
292 mutex_exit(&mountlist_lock);
293 ump = VFSTOUFS(mp);
294 fs = ump->um_fs;
295 memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
296 (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
297 (void)ffs_statvfs(mp, &mp->mnt_stat);
298 vfs_unbusy(mp, false, NULL);
299 setrootfstime((time_t)fs->fs_time);
300 return (0);
301 }
302
303 /*
304 * VFS Operations.
305 *
306 * mount system call
307 */
308 int
309 ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
310 {
311 struct lwp *l = curlwp;
312 struct nameidata nd;
313 struct vnode *vp, *devvp = NULL;
314 struct ufs_args *args = data;
315 struct ufsmount *ump = NULL;
316 struct fs *fs;
317 int error = 0, flags, update;
318 mode_t accessmode;
319
320 if (*data_len < sizeof *args)
321 return EINVAL;
322
323 if (mp->mnt_flag & MNT_GETARGS) {
324 ump = VFSTOUFS(mp);
325 if (ump == NULL)
326 return EIO;
327 args->fspec = NULL;
328 *data_len = sizeof *args;
329 return 0;
330 }
331
332 #if !defined(SOFTDEP)
333 mp->mnt_flag &= ~MNT_SOFTDEP;
334 #endif
335
336 update = mp->mnt_flag & MNT_UPDATE;
337
338 /* Check arguments */
339 if (args->fspec != NULL) {
340 /*
341 * Look up the name and verify that it's sane.
342 */
343 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
344 if ((error = namei(&nd)) != 0)
345 return (error);
346 devvp = nd.ni_vp;
347
348 if (!update) {
349 /*
350 * Be sure this is a valid block device
351 */
352 if (devvp->v_type != VBLK)
353 error = ENOTBLK;
354 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
355 error = ENXIO;
356 } else {
357 /*
358 * Be sure we're still naming the same device
359 * used for our initial mount
360 */
361 ump = VFSTOUFS(mp);
362 if (devvp != ump->um_devvp) {
363 if (devvp->v_rdev != ump->um_devvp->v_rdev)
364 error = EINVAL;
365 else {
366 vrele(devvp);
367 devvp = ump->um_devvp;
368 vref(devvp);
369 }
370 }
371 }
372 } else {
373 if (!update) {
374 /* New mounts must have a filename for the device */
375 return (EINVAL);
376 } else {
377 /* Use the extant mount */
378 ump = VFSTOUFS(mp);
379 devvp = ump->um_devvp;
380 vref(devvp);
381 }
382 }
383
384 /*
385 * Mark the device and any existing vnodes as involved in
386 * softdep processing.
387 */
388 if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
389 devvp->v_uflag |= VU_SOFTDEP;
390 mutex_enter(&mntvnode_lock);
391 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
392 if (vp->v_mount != mp || vismarker(vp))
393 continue;
394 vp->v_uflag |= VU_SOFTDEP;
395 }
396 mutex_exit(&mntvnode_lock);
397 }
398
399 /*
400 * If mount by non-root, then verify that user has necessary
401 * permissions on the device.
402 */
403 if (error == 0 && kauth_authorize_generic(l->l_cred,
404 KAUTH_GENERIC_ISSUSER, NULL) != 0) {
405 accessmode = VREAD;
406 if (update ?
407 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
408 (mp->mnt_flag & MNT_RDONLY) == 0)
409 accessmode |= VWRITE;
410 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
411 error = VOP_ACCESS(devvp, accessmode, l->l_cred);
412 VOP_UNLOCK(devvp, 0);
413 }
414
415 if (error) {
416 vrele(devvp);
417 return (error);
418 }
419
420 #ifdef WAPBL
421 /*
422 * WAPBL can only be enabled on a r/w mount
423 * that does not use softdep.
424 */
425 if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
426 mp->mnt_flag &= ~MNT_LOG;
427 }
428 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_LOG)) ==
429 (MNT_SOFTDEP | MNT_LOG)) {
430 printf("%s fs is journalled, ignoring soft update mode\n",
431 VFSTOUFS(mp)->um_fs->fs_fsmnt);
432 mp->mnt_flag &= ~MNT_SOFTDEP;
433 }
434 #else /* !WAPBL */
435 mp->mnt_flag &= ~MNT_LOG;
436 #endif /* !WAPBL */
437
438 if (!update) {
439 int xflags;
440
441 if (mp->mnt_flag & MNT_RDONLY)
442 xflags = FREAD;
443 else
444 xflags = FREAD | FWRITE;
445 error = VOP_OPEN(devvp, xflags, FSCRED);
446 if (error)
447 goto fail;
448 error = ffs_mountfs(devvp, mp, l);
449 if (error) {
450 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
451 (void)VOP_CLOSE(devvp, xflags, NOCRED);
452 VOP_UNLOCK(devvp, 0);
453 goto fail;
454 }
455
456 ump = VFSTOUFS(mp);
457 fs = ump->um_fs;
458 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
459 (MNT_SOFTDEP | MNT_ASYNC)) {
460 printf("%s fs uses soft updates, "
461 "ignoring async mode\n",
462 fs->fs_fsmnt);
463 mp->mnt_flag &= ~MNT_ASYNC;
464 }
465 } else {
466 /*
467 * Update the mount.
468 */
469
470 /*
471 * The initial mount got a reference on this
472 * device, so drop the one obtained via
473 * namei(), above.
474 */
475 vrele(devvp);
476
477 ump = VFSTOUFS(mp);
478 fs = ump->um_fs;
479 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
480 /*
481 * Changing from r/w to r/o
482 */
483 flags = WRITECLOSE;
484 if (mp->mnt_flag & MNT_FORCE)
485 flags |= FORCECLOSE;
486 if (mp->mnt_flag & MNT_SOFTDEP)
487 error = softdep_flushfiles(mp, flags, l);
488 else
489 error = ffs_flushfiles(mp, flags, l);
490 if (fs->fs_pendingblocks != 0 ||
491 fs->fs_pendinginodes != 0) {
492 printf("%s: update error: blocks %" PRId64
493 " files %d\n",
494 fs->fs_fsmnt, fs->fs_pendingblocks,
495 fs->fs_pendinginodes);
496 fs->fs_pendingblocks = 0;
497 fs->fs_pendinginodes = 0;
498 }
499 if (error == 0)
500 error = UFS_WAPBL_BEGIN(mp);
501 if (error == 0 &&
502 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
503 fs->fs_clean & FS_WASCLEAN) {
504 if (mp->mnt_flag & MNT_SOFTDEP)
505 fs->fs_flags &= ~FS_DOSOFTDEP;
506 fs->fs_clean = FS_ISCLEAN;
507 (void) ffs_sbupdate(ump, MNT_WAIT);
508 }
509 if (error == 0)
510 UFS_WAPBL_END(mp);
511 if (error)
512 return (error);
513 }
514
515 #ifdef WAPBL
516 if ((mp->mnt_flag & MNT_LOG) == 0) {
517 error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
518 if (error)
519 return error;
520 }
521 #endif /* WAPBL */
522
523 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
524 /*
525 * Finish change from r/w to r/o
526 */
527 fs->fs_ronly = 1;
528 fs->fs_fmod = 0;
529 }
530
531 /*
532 * Flush soft dependencies if disabling it via an update
533 * mount. This may leave some items to be processed,
534 * so don't do this yet XXX.
535 */
536 if ((fs->fs_flags & FS_DOSOFTDEP) &&
537 !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
538 #ifdef notyet
539 flags = WRITECLOSE;
540 if (mp->mnt_flag & MNT_FORCE)
541 flags |= FORCECLOSE;
542 error = softdep_flushfiles(mp, flags, l);
543 if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
544 fs->fs_flags &= ~FS_DOSOFTDEP;
545 (void) ffs_sbupdate(ump, MNT_WAIT);
546 #elif defined(SOFTDEP)
547 mp->mnt_flag |= MNT_SOFTDEP;
548 #endif
549 }
550
551 /*
552 * When upgrading to a softdep mount, we must first flush
553 * all vnodes. (not done yet -- see above)
554 */
555 if (!(fs->fs_flags & FS_DOSOFTDEP) &&
556 (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
557 #ifdef notyet
558 flags = WRITECLOSE;
559 if (mp->mnt_flag & MNT_FORCE)
560 flags |= FORCECLOSE;
561 error = ffs_flushfiles(mp, flags, l);
562 #else
563 mp->mnt_flag &= ~MNT_SOFTDEP;
564 #endif
565 }
566
567 if (mp->mnt_flag & MNT_RELOAD) {
568 error = ffs_reload(mp, l->l_cred, l);
569 if (error)
570 return (error);
571 }
572
573 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
574 /*
575 * Changing from read-only to read/write
576 */
577 fs->fs_ronly = 0;
578 fs->fs_clean <<= 1;
579 fs->fs_fmod = 1;
580 if ((fs->fs_flags & FS_DOSOFTDEP)) {
581 error = softdep_mount(devvp, mp, fs,
582 l->l_cred);
583 if (error)
584 return (error);
585 }
586 #ifdef WAPBL
587 if (fs->fs_flags & FS_DOWAPBL) {
588 printf("%s: replaying log to disk\n",
589 fs->fs_fsmnt);
590 KDASSERT(mp->mnt_wapbl_replay);
591 error = wapbl_replay_write(mp->mnt_wapbl_replay,
592 devvp);
593 if (error) {
594 return error;
595 }
596 wapbl_replay_stop(mp->mnt_wapbl_replay);
597 fs->fs_clean = FS_WASCLEAN;
598 }
599 #endif /* WAPBL */
600 if (fs->fs_snapinum[0] != 0)
601 ffs_snapshot_mount(mp);
602 }
603
604 #ifdef WAPBL
605 error = ffs_wapbl_start(mp);
606 if (error)
607 return error;
608 #endif /* WAPBL */
609
610 if (args->fspec == NULL)
611 return EINVAL;
612 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
613 (MNT_SOFTDEP | MNT_ASYNC)) {
614 printf("%s fs uses soft updates, ignoring async mode\n",
615 fs->fs_fsmnt);
616 mp->mnt_flag &= ~MNT_ASYNC;
617 }
618 }
619
620 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
621 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
622 if (error == 0)
623 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
624 sizeof(fs->fs_fsmnt));
625 if (mp->mnt_flag & MNT_SOFTDEP)
626 fs->fs_flags |= FS_DOSOFTDEP;
627 else
628 fs->fs_flags &= ~FS_DOSOFTDEP;
629 if (fs->fs_fmod != 0) { /* XXX */
630 int err;
631
632 fs->fs_fmod = 0;
633 if (fs->fs_clean & FS_WASCLEAN)
634 fs->fs_time = time_second;
635 else {
636 printf("%s: file system not clean (fs_clean=%#x); "
637 "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
638 fs->fs_clean);
639 printf("%s: lost blocks %" PRId64 " files %d\n",
640 mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
641 fs->fs_pendinginodes);
642 }
643 err = UFS_WAPBL_BEGIN(mp);
644 if (err == 0) {
645 (void) ffs_cgupdate(ump, MNT_WAIT);
646 UFS_WAPBL_END(mp);
647 }
648 }
649 return (error);
650
651 fail:
652 vrele(devvp);
653 return (error);
654 }
655
656 /*
657 * Reload all incore data for a filesystem (used after running fsck on
658 * the root filesystem and finding things to fix). The filesystem must
659 * be mounted read-only.
660 *
661 * Things to do to update the mount:
662 * 1) invalidate all cached meta-data.
663 * 2) re-read superblock from disk.
664 * 3) re-read summary information from disk.
665 * 4) invalidate all inactive vnodes.
666 * 5) invalidate all cached file data.
667 * 6) re-read inode data for all active vnodes.
668 */
669 int
670 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
671 {
672 struct vnode *vp, *mvp, *devvp;
673 struct inode *ip;
674 void *space;
675 struct buf *bp;
676 struct fs *fs, *newfs;
677 struct partinfo dpart;
678 int i, blks, size, error;
679 int32_t *lp;
680 struct ufsmount *ump;
681 daddr_t sblockloc;
682
683 if ((mp->mnt_flag & MNT_RDONLY) == 0)
684 return (EINVAL);
685
686 ump = VFSTOUFS(mp);
687 /*
688 * Step 1: invalidate all cached meta-data.
689 */
690 devvp = ump->um_devvp;
691 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
692 error = vinvalbuf(devvp, 0, cred, l, 0, 0);
693 VOP_UNLOCK(devvp, 0);
694 if (error)
695 panic("ffs_reload: dirty1");
696 /*
697 * Step 2: re-read superblock from disk.
698 */
699 fs = ump->um_fs;
700 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED) != 0)
701 size = DEV_BSIZE;
702 else
703 size = dpart.disklab->d_secsize;
704 /* XXX we don't handle possibility that superblock moved. */
705 error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
706 NOCRED, 0, &bp);
707 if (error) {
708 brelse(bp, 0);
709 return (error);
710 }
711 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
712 memcpy(newfs, bp->b_data, fs->fs_sbsize);
713 #ifdef FFS_EI
714 if (ump->um_flags & UFS_NEEDSWAP) {
715 ffs_sb_swap((struct fs*)bp->b_data, newfs);
716 fs->fs_flags |= FS_SWAPPED;
717 } else
718 #endif
719 fs->fs_flags &= ~FS_SWAPPED;
720 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
721 newfs->fs_magic != FS_UFS2_MAGIC)||
722 newfs->fs_bsize > MAXBSIZE ||
723 newfs->fs_bsize < sizeof(struct fs)) {
724 brelse(bp, 0);
725 free(newfs, M_UFSMNT);
726 return (EIO); /* XXX needs translation */
727 }
728 /* Store off old fs_sblockloc for fs_oldfscompat_read. */
729 sblockloc = fs->fs_sblockloc;
730 /*
731 * Copy pointer fields back into superblock before copying in XXX
732 * new superblock. These should really be in the ufsmount. XXX
733 * Note that important parameters (eg fs_ncg) are unchanged.
734 */
735 newfs->fs_csp = fs->fs_csp;
736 newfs->fs_maxcluster = fs->fs_maxcluster;
737 newfs->fs_contigdirs = fs->fs_contigdirs;
738 newfs->fs_ronly = fs->fs_ronly;
739 newfs->fs_active = fs->fs_active;
740 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
741 brelse(bp, 0);
742 free(newfs, M_UFSMNT);
743
744 /* Recheck for apple UFS filesystem */
745 ump->um_flags &= ~UFS_ISAPPLEUFS;
746 /* First check to see if this is tagged as an Apple UFS filesystem
747 * in the disklabel
748 */
749 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
750 (dpart.part->p_fstype == FS_APPLEUFS)) {
751 ump->um_flags |= UFS_ISAPPLEUFS;
752 }
753 #ifdef APPLE_UFS
754 else {
755 /* Manually look for an apple ufs label, and if a valid one
756 * is found, then treat it like an Apple UFS filesystem anyway
757 */
758 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
759 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
760 if (error) {
761 brelse(bp, 0);
762 return (error);
763 }
764 error = ffs_appleufs_validate(fs->fs_fsmnt,
765 (struct appleufslabel *)bp->b_data, NULL);
766 if (error == 0)
767 ump->um_flags |= UFS_ISAPPLEUFS;
768 brelse(bp, 0);
769 bp = NULL;
770 }
771 #else
772 if (ump->um_flags & UFS_ISAPPLEUFS)
773 return (EIO);
774 #endif
775
776 if (UFS_MPISAPPLEUFS(ump)) {
777 /* see comment about NeXT below */
778 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
779 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
780 mp->mnt_iflag |= IMNT_DTYPE;
781 } else {
782 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
783 ump->um_dirblksiz = DIRBLKSIZ;
784 if (ump->um_maxsymlinklen > 0)
785 mp->mnt_iflag |= IMNT_DTYPE;
786 else
787 mp->mnt_iflag &= ~IMNT_DTYPE;
788 }
789 ffs_oldfscompat_read(fs, ump, sblockloc);
790 mutex_enter(&ump->um_lock);
791 ump->um_maxfilesize = fs->fs_maxfilesize;
792
793 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
794 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
795 mp->mnt_stat.f_mntonname, fs->fs_flags,
796 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
797 if ((mp->mnt_flag & MNT_FORCE) == 0) {
798 mutex_exit(&ump->um_lock);
799 return (EINVAL);
800 }
801 }
802
803 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
804 fs->fs_pendingblocks = 0;
805 fs->fs_pendinginodes = 0;
806 }
807 mutex_exit(&ump->um_lock);
808
809 ffs_statvfs(mp, &mp->mnt_stat);
810 /*
811 * Step 3: re-read summary information from disk.
812 */
813 blks = howmany(fs->fs_cssize, fs->fs_fsize);
814 space = fs->fs_csp;
815 for (i = 0; i < blks; i += fs->fs_frag) {
816 size = fs->fs_bsize;
817 if (i + fs->fs_frag > blks)
818 size = (blks - i) * fs->fs_fsize;
819 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
820 NOCRED, 0, &bp);
821 if (error) {
822 brelse(bp, 0);
823 return (error);
824 }
825 #ifdef FFS_EI
826 if (UFS_FSNEEDSWAP(fs))
827 ffs_csum_swap((struct csum *)bp->b_data,
828 (struct csum *)space, size);
829 else
830 #endif
831 memcpy(space, bp->b_data, (size_t)size);
832 space = (char *)space + size;
833 brelse(bp, 0);
834 }
835 if ((fs->fs_flags & FS_DOSOFTDEP))
836 softdep_mount(devvp, mp, fs, cred);
837 if (fs->fs_snapinum[0] != 0)
838 ffs_snapshot_mount(mp);
839 /*
840 * We no longer know anything about clusters per cylinder group.
841 */
842 if (fs->fs_contigsumsize > 0) {
843 lp = fs->fs_maxcluster;
844 for (i = 0; i < fs->fs_ncg; i++)
845 *lp++ = fs->fs_contigsumsize;
846 }
847
848 /* Allocate a marker vnode. */
849 if ((mvp = vnalloc(mp)) == NULL)
850 return ENOMEM;
851 /*
852 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
853 * and vclean() can be called indirectly
854 */
855 mutex_enter(&mntvnode_lock);
856 loop:
857 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
858 vmark(mvp, vp);
859 if (vp->v_mount != mp || vismarker(vp))
860 continue;
861 /*
862 * Step 4: invalidate all inactive vnodes.
863 */
864 if (vrecycle(vp, &mntvnode_lock, l)) {
865 mutex_enter(&mntvnode_lock);
866 (void)vunmark(mvp);
867 goto loop;
868 }
869 /*
870 * Step 5: invalidate all cached file data.
871 */
872 mutex_enter(&vp->v_interlock);
873 mutex_exit(&mntvnode_lock);
874 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
875 (void)vunmark(mvp);
876 goto loop;
877 }
878 if (vinvalbuf(vp, 0, cred, l, 0, 0))
879 panic("ffs_reload: dirty2");
880 /*
881 * Step 6: re-read inode data for all active vnodes.
882 */
883 ip = VTOI(vp);
884 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
885 (int)fs->fs_bsize, NOCRED, 0, &bp);
886 if (error) {
887 brelse(bp, 0);
888 vput(vp);
889 (void)vunmark(mvp);
890 break;
891 }
892 ffs_load_inode(bp, ip, fs, ip->i_number);
893 ip->i_ffs_effnlink = ip->i_nlink;
894 brelse(bp, 0);
895 vput(vp);
896 mutex_enter(&mntvnode_lock);
897 }
898 mutex_exit(&mntvnode_lock);
899 vnfree(mvp);
900 return (error);
901 }
902
903 /*
904 * Possible superblock locations ordered from most to least likely.
905 */
906 static const int sblock_try[] = SBLOCKSEARCH;
907
908 /*
909 * Common code for mount and mountroot
910 */
911 int
912 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
913 {
914 struct ufsmount *ump;
915 struct buf *bp;
916 struct fs *fs;
917 dev_t dev;
918 struct partinfo dpart;
919 void *space;
920 daddr_t sblockloc, fsblockloc;
921 int blks, fstype;
922 int error, i, size, ronly, bset = 0;
923 #ifdef FFS_EI
924 int needswap = 0; /* keep gcc happy */
925 #endif
926 int32_t *lp;
927 kauth_cred_t cred;
928 u_int32_t sbsize = 8192; /* keep gcc happy*/
929
930 dev = devvp->v_rdev;
931 cred = l ? l->l_cred : NOCRED;
932
933 /* Flush out any old buffers remaining from a previous use. */
934 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
935 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
936 VOP_UNLOCK(devvp, 0);
937 if (error)
938 return (error);
939
940 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
941 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) != 0)
942 size = DEV_BSIZE;
943 else
944 size = dpart.disklab->d_secsize;
945
946 bp = NULL;
947 ump = NULL;
948 fs = NULL;
949 sblockloc = 0;
950 fstype = 0;
951
952 error = fstrans_mount(mp);
953 if (error)
954 return error;
955
956 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
957 memset(ump, 0, sizeof *ump);
958 mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
959 error = ffs_snapshot_init(ump);
960 if (error)
961 goto out;
962 ump->um_ops = &ffs_ufsops;
963
964 #ifdef WAPBL
965 sbagain:
966 #endif
967 /*
968 * Try reading the superblock in each of its possible locations.
969 */
970 for (i = 0; ; i++) {
971 if (bp != NULL) {
972 brelse(bp, BC_NOCACHE);
973 bp = NULL;
974 }
975 if (sblock_try[i] == -1) {
976 error = EINVAL;
977 fs = NULL;
978 goto out;
979 }
980 error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
981 0, &bp);
982 if (error) {
983 fs = NULL;
984 goto out;
985 }
986 fs = (struct fs*)bp->b_data;
987 fsblockloc = sblockloc = sblock_try[i];
988 if (fs->fs_magic == FS_UFS1_MAGIC) {
989 sbsize = fs->fs_sbsize;
990 fstype = UFS1;
991 #ifdef FFS_EI
992 needswap = 0;
993 } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
994 sbsize = bswap32(fs->fs_sbsize);
995 fstype = UFS1;
996 needswap = 1;
997 #endif
998 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
999 sbsize = fs->fs_sbsize;
1000 fstype = UFS2;
1001 #ifdef FFS_EI
1002 needswap = 0;
1003 } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
1004 sbsize = bswap32(fs->fs_sbsize);
1005 fstype = UFS2;
1006 needswap = 1;
1007 #endif
1008 } else
1009 continue;
1010
1011
1012 /* fs->fs_sblockloc isn't defined for old filesystems */
1013 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
1014 if (sblockloc == SBLOCK_UFS2)
1015 /*
1016 * This is likely to be the first alternate
1017 * in a filesystem with 64k blocks.
1018 * Don't use it.
1019 */
1020 continue;
1021 fsblockloc = sblockloc;
1022 } else {
1023 fsblockloc = fs->fs_sblockloc;
1024 #ifdef FFS_EI
1025 if (needswap)
1026 fsblockloc = bswap64(fsblockloc);
1027 #endif
1028 }
1029
1030 /* Check we haven't found an alternate superblock */
1031 if (fsblockloc != sblockloc)
1032 continue;
1033
1034 /* Validate size of superblock */
1035 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
1036 continue;
1037
1038 /* Ok seems to be a good superblock */
1039 break;
1040 }
1041
1042 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
1043 memcpy(fs, bp->b_data, sbsize);
1044 ump->um_fs = fs;
1045
1046 #ifdef FFS_EI
1047 if (needswap) {
1048 ffs_sb_swap((struct fs*)bp->b_data, fs);
1049 fs->fs_flags |= FS_SWAPPED;
1050 } else
1051 #endif
1052 fs->fs_flags &= ~FS_SWAPPED;
1053
1054 #ifdef WAPBL
1055 if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
1056 error = ffs_wapbl_replay_start(mp, fs, devvp);
1057 if (error)
1058 goto out;
1059
1060 if (!ronly) {
1061 /* XXX fsmnt may be stale. */
1062 printf("%s: replaying log to disk\n", fs->fs_fsmnt);
1063 error = wapbl_replay_write(mp->mnt_wapbl_replay, devvp);
1064 if (error)
1065 goto out;
1066 wapbl_replay_stop(mp->mnt_wapbl_replay);
1067 fs->fs_clean = FS_WASCLEAN;
1068 } else {
1069 /* XXX fsmnt may be stale */
1070 printf("%s: replaying log to memory\n", fs->fs_fsmnt);
1071 }
1072
1073 /* Force a re-read of the superblock */
1074 brelse(bp, BC_INVAL);
1075 bp = NULL;
1076 free(fs, M_UFSMNT);
1077 fs = NULL;
1078 goto sbagain;
1079 }
1080 #else /* !WAPBL */
1081 if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
1082 error = EPERM;
1083 goto out;
1084 }
1085 #endif /* !WAPBL */
1086
1087 ffs_oldfscompat_read(fs, ump, sblockloc);
1088 ump->um_maxfilesize = fs->fs_maxfilesize;
1089
1090 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
1091 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
1092 mp->mnt_stat.f_mntonname, fs->fs_flags,
1093 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1094 if ((mp->mnt_flag & MNT_FORCE) == 0) {
1095 error = EINVAL;
1096 goto out;
1097 }
1098 }
1099
1100 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1101 fs->fs_pendingblocks = 0;
1102 fs->fs_pendinginodes = 0;
1103 }
1104
1105 ump->um_fstype = fstype;
1106 if (fs->fs_sbsize < SBLOCKSIZE)
1107 brelse(bp, BC_INVAL);
1108 else
1109 brelse(bp, 0);
1110 bp = NULL;
1111
1112 /* First check to see if this is tagged as an Apple UFS filesystem
1113 * in the disklabel
1114 */
1115 if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
1116 (dpart.part->p_fstype == FS_APPLEUFS)) {
1117 ump->um_flags |= UFS_ISAPPLEUFS;
1118 }
1119 #ifdef APPLE_UFS
1120 else {
1121 /* Manually look for an apple ufs label, and if a valid one
1122 * is found, then treat it like an Apple UFS filesystem anyway
1123 */
1124 error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
1125 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
1126 if (error)
1127 goto out;
1128 error = ffs_appleufs_validate(fs->fs_fsmnt,
1129 (struct appleufslabel *)bp->b_data, NULL);
1130 if (error == 0) {
1131 ump->um_flags |= UFS_ISAPPLEUFS;
1132 }
1133 brelse(bp, 0);
1134 bp = NULL;
1135 }
1136 #else
1137 if (ump->um_flags & UFS_ISAPPLEUFS) {
1138 error = EINVAL;
1139 goto out;
1140 }
1141 #endif
1142
1143 #if 0
1144 /*
1145 * XXX This code changes the behaviour of mounting dirty filesystems, to
1146 * XXX require "mount -f ..." to mount them. This doesn't match what
1147 * XXX mount(8) describes and is disabled for now.
1148 */
1149 /*
1150 * If the file system is not clean, don't allow it to be mounted
1151 * unless MNT_FORCE is specified. (Note: MNT_FORCE is always set
1152 * for the root file system.)
1153 */
1154 if (fs->fs_flags & FS_DOWAPBL) {
1155 /*
1156 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
1157 * bit is set, although there's a window in unmount where it
1158 * could be FS_ISCLEAN
1159 */
1160 if ((mp->mnt_flag & MNT_FORCE) == 0 &&
1161 (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
1162 error = EPERM;
1163 goto out;
1164 }
1165 } else
1166 if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
1167 (mp->mnt_flag & MNT_FORCE) == 0) {
1168 error = EPERM;
1169 goto out;
1170 }
1171 #endif
1172
1173 /*
1174 * verify that we can access the last block in the fs
1175 * if we're mounting read/write.
1176 */
1177
1178 if (!ronly) {
1179 error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
1180 cred, 0, &bp);
1181 if (bp->b_bcount != fs->fs_fsize)
1182 error = EINVAL;
1183 if (error) {
1184 bset = BC_INVAL;
1185 goto out;
1186 }
1187 brelse(bp, BC_INVAL);
1188 bp = NULL;
1189 }
1190
1191 fs->fs_ronly = ronly;
1192 /* Don't bump fs_clean if we're replaying journal */
1193 if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
1194 if (ronly == 0) {
1195 fs->fs_clean <<= 1;
1196 fs->fs_fmod = 1;
1197 }
1198 size = fs->fs_cssize;
1199 blks = howmany(size, fs->fs_fsize);
1200 if (fs->fs_contigsumsize > 0)
1201 size += fs->fs_ncg * sizeof(int32_t);
1202 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1203 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
1204 fs->fs_csp = space;
1205 for (i = 0; i < blks; i += fs->fs_frag) {
1206 size = fs->fs_bsize;
1207 if (i + fs->fs_frag > blks)
1208 size = (blks - i) * fs->fs_fsize;
1209 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
1210 cred, 0, &bp);
1211 if (error) {
1212 free(fs->fs_csp, M_UFSMNT);
1213 goto out;
1214 }
1215 #ifdef FFS_EI
1216 if (needswap)
1217 ffs_csum_swap((struct csum *)bp->b_data,
1218 (struct csum *)space, size);
1219 else
1220 #endif
1221 memcpy(space, bp->b_data, (u_int)size);
1222
1223 space = (char *)space + size;
1224 brelse(bp, 0);
1225 bp = NULL;
1226 }
1227 if (fs->fs_contigsumsize > 0) {
1228 fs->fs_maxcluster = lp = space;
1229 for (i = 0; i < fs->fs_ncg; i++)
1230 *lp++ = fs->fs_contigsumsize;
1231 space = lp;
1232 }
1233 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1234 fs->fs_contigdirs = space;
1235 space = (char *)space + size;
1236 memset(fs->fs_contigdirs, 0, size);
1237 /* Compatibility for old filesystems - XXX */
1238 if (fs->fs_avgfilesize <= 0)
1239 fs->fs_avgfilesize = AVFILESIZ;
1240 if (fs->fs_avgfpdir <= 0)
1241 fs->fs_avgfpdir = AFPDIR;
1242 fs->fs_active = NULL;
1243 mp->mnt_data = ump;
1244 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1245 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
1246 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1247 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
1248 if (UFS_MPISAPPLEUFS(ump)) {
1249 /* NeXT used to keep short symlinks in the inode even
1250 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
1251 * is probably -1, but we still need to be able to identify
1252 * short symlinks.
1253 */
1254 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
1255 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
1256 mp->mnt_iflag |= IMNT_DTYPE;
1257 } else {
1258 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
1259 ump->um_dirblksiz = DIRBLKSIZ;
1260 if (ump->um_maxsymlinklen > 0)
1261 mp->mnt_iflag |= IMNT_DTYPE;
1262 else
1263 mp->mnt_iflag &= ~IMNT_DTYPE;
1264 }
1265 mp->mnt_fs_bshift = fs->fs_bshift;
1266 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
1267 mp->mnt_flag |= MNT_LOCAL;
1268 mp->mnt_iflag |= IMNT_MPSAFE;
1269 #ifdef FFS_EI
1270 if (needswap)
1271 ump->um_flags |= UFS_NEEDSWAP;
1272 #endif
1273 ump->um_mountp = mp;
1274 ump->um_dev = dev;
1275 ump->um_devvp = devvp;
1276 ump->um_nindir = fs->fs_nindir;
1277 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
1278 ump->um_bptrtodb = fs->fs_fsbtodb;
1279 ump->um_seqinc = fs->fs_frag;
1280 for (i = 0; i < MAXQUOTAS; i++)
1281 ump->um_quotas[i] = NULLVP;
1282 devvp->v_specmountpoint = mp;
1283 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
1284 error = softdep_mount(devvp, mp, fs, cred);
1285 if (error) {
1286 free(fs->fs_csp, M_UFSMNT);
1287 goto out;
1288 }
1289 }
1290 if (ronly == 0 && fs->fs_snapinum[0] != 0)
1291 ffs_snapshot_mount(mp);
1292
1293 #ifdef WAPBL
1294 if (!ronly) {
1295 KDASSERT(fs->fs_ronly == 0);
1296 /*
1297 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
1298 * needs to create a new log file in-filesystem.
1299 */
1300 ffs_statvfs(mp, &mp->mnt_stat);
1301
1302 error = ffs_wapbl_start(mp);
1303 if (error) {
1304 free(fs->fs_csp, M_UFSMNT);
1305 goto out;
1306 }
1307 }
1308 #endif /* WAPBL */
1309 #ifdef UFS_EXTATTR
1310 /*
1311 * Initialize file-backed extended attributes on UFS1 file
1312 * systems.
1313 */
1314 if (ump->um_fstype == UFS1) {
1315 ufs_extattr_uepm_init(&ump->um_extattr);
1316 #ifdef UFS_EXTATTR_AUTOSTART
1317 /*
1318 * XXX Just ignore errors. Not clear that we should
1319 * XXX fail the mount in this case.
1320 */
1321 (void) ufs_extattr_autostart(mp, l);
1322 #endif
1323 }
1324 #endif /* UFS_EXTATTR */
1325 return (0);
1326 out:
1327 #ifdef WAPBL
1328 if (mp->mnt_wapbl_replay) {
1329 wapbl_replay_stop(mp->mnt_wapbl_replay);
1330 wapbl_replay_free(mp->mnt_wapbl_replay);
1331 mp->mnt_wapbl_replay = 0;
1332 }
1333 #endif
1334
1335 fstrans_unmount(mp);
1336 if (fs)
1337 free(fs, M_UFSMNT);
1338 devvp->v_specmountpoint = NULL;
1339 if (bp)
1340 brelse(bp, bset);
1341 if (ump) {
1342 if (ump->um_oldfscompat)
1343 free(ump->um_oldfscompat, M_UFSMNT);
1344 mutex_destroy(&ump->um_lock);
1345 free(ump, M_UFSMNT);
1346 mp->mnt_data = NULL;
1347 }
1348 return (error);
1349 }
1350
1351 /*
1352 * Sanity checks for loading old filesystem superblocks.
1353 * See ffs_oldfscompat_write below for unwound actions.
1354 *
1355 * XXX - Parts get retired eventually.
1356 * Unfortunately new bits get added.
1357 */
1358 static void
1359 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1360 {
1361 off_t maxfilesize;
1362 int32_t *extrasave;
1363
1364 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1365 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1366 return;
1367
1368 if (!ump->um_oldfscompat)
1369 ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
1370 M_UFSMNT, M_WAITOK);
1371
1372 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1373 extrasave = ump->um_oldfscompat;
1374 extrasave += 512/sizeof(int32_t);
1375 extrasave[0] = fs->fs_old_npsect;
1376 extrasave[1] = fs->fs_old_interleave;
1377 extrasave[2] = fs->fs_old_trackskew;
1378
1379 /* These fields will be overwritten by their
1380 * original values in fs_oldfscompat_write, so it is harmless
1381 * to modify them here.
1382 */
1383 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1384 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1385 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1386 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1387
1388 fs->fs_maxbsize = fs->fs_bsize;
1389 fs->fs_time = fs->fs_old_time;
1390 fs->fs_size = fs->fs_old_size;
1391 fs->fs_dsize = fs->fs_old_dsize;
1392 fs->fs_csaddr = fs->fs_old_csaddr;
1393 fs->fs_sblockloc = sblockloc;
1394
1395 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1396
1397 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1398 fs->fs_old_nrpos = 8;
1399 fs->fs_old_npsect = fs->fs_old_nsect;
1400 fs->fs_old_interleave = 1;
1401 fs->fs_old_trackskew = 0;
1402 }
1403
1404 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1405 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
1406 fs->fs_qbmask = ~fs->fs_bmask;
1407 fs->fs_qfmask = ~fs->fs_fmask;
1408 }
1409
1410 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1411 if (fs->fs_maxfilesize > maxfilesize)
1412 fs->fs_maxfilesize = maxfilesize;
1413
1414 /* Compatibility for old filesystems */
1415 if (fs->fs_avgfilesize <= 0)
1416 fs->fs_avgfilesize = AVFILESIZ;
1417 if (fs->fs_avgfpdir <= 0)
1418 fs->fs_avgfpdir = AFPDIR;
1419
1420 #if 0
1421 if (bigcgs) {
1422 fs->fs_save_cgsize = fs->fs_cgsize;
1423 fs->fs_cgsize = fs->fs_bsize;
1424 }
1425 #endif
1426 }
1427
1428 /*
1429 * Unwinding superblock updates for old filesystems.
1430 * See ffs_oldfscompat_read above for details.
1431 *
1432 * XXX - Parts get retired eventually.
1433 * Unfortunately new bits get added.
1434 */
1435 static void
1436 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1437 {
1438 int32_t *extrasave;
1439
1440 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1441 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1442 return;
1443
1444 fs->fs_old_time = fs->fs_time;
1445 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1446 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1447 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1448 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1449 fs->fs_old_flags = fs->fs_flags;
1450
1451 #if 0
1452 if (bigcgs) {
1453 fs->fs_cgsize = fs->fs_save_cgsize;
1454 }
1455 #endif
1456
1457 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1458 extrasave = ump->um_oldfscompat;
1459 extrasave += 512/sizeof(int32_t);
1460 fs->fs_old_npsect = extrasave[0];
1461 fs->fs_old_interleave = extrasave[1];
1462 fs->fs_old_trackskew = extrasave[2];
1463
1464 }
1465
1466 /*
1467 * unmount system call
1468 */
1469 int
1470 ffs_unmount(struct mount *mp, int mntflags)
1471 {
1472 struct lwp *l = curlwp;
1473 struct ufsmount *ump = VFSTOUFS(mp);
1474 struct fs *fs = ump->um_fs;
1475 int error, flags, penderr;
1476 #ifdef WAPBL
1477 extern int doforce;
1478 #endif
1479
1480 penderr = 0;
1481 flags = 0;
1482 if (mntflags & MNT_FORCE)
1483 flags |= FORCECLOSE;
1484 #ifdef UFS_EXTATTR
1485 if (ump->um_fstype == UFS1) {
1486 ufs_extattr_stop(mp, l);
1487 ufs_extattr_uepm_destroy(&ump->um_extattr);
1488 }
1489 #endif /* UFS_EXTATTR */
1490 if (mp->mnt_flag & MNT_SOFTDEP) {
1491 if ((error = softdep_flushfiles(mp, flags, l)) != 0)
1492 return (error);
1493 } else {
1494 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1495 return (error);
1496 }
1497 mutex_enter(&ump->um_lock);
1498 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1499 printf("%s: unmount pending error: blocks %" PRId64
1500 " files %d\n",
1501 fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
1502 fs->fs_pendingblocks = 0;
1503 fs->fs_pendinginodes = 0;
1504 penderr = 1;
1505 }
1506 mutex_exit(&ump->um_lock);
1507 error = UFS_WAPBL_BEGIN(mp);
1508 if (error == 0)
1509 if (fs->fs_ronly == 0 &&
1510 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1511 fs->fs_clean & FS_WASCLEAN) {
1512 /*
1513 * XXXX don't mark fs clean in the case of softdep
1514 * pending block errors, until they are fixed.
1515 */
1516 if (penderr == 0) {
1517 if (mp->mnt_flag & MNT_SOFTDEP)
1518 fs->fs_flags &= ~FS_DOSOFTDEP;
1519 fs->fs_clean = FS_ISCLEAN;
1520 }
1521 fs->fs_fmod = 0;
1522 (void) ffs_sbupdate(ump, MNT_WAIT);
1523 }
1524 if (error == 0)
1525 UFS_WAPBL_END(mp);
1526 #ifdef WAPBL
1527 KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
1528 if (mp->mnt_wapbl_replay) {
1529 KDASSERT(fs->fs_ronly);
1530 wapbl_replay_stop(mp->mnt_wapbl_replay);
1531 wapbl_replay_free(mp->mnt_wapbl_replay);
1532 mp->mnt_wapbl_replay = 0;
1533 }
1534 error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
1535 if (error) {
1536 return error;
1537 }
1538 #endif /* WAPBL */
1539 if (ump->um_devvp->v_type != VBAD)
1540 ump->um_devvp->v_specmountpoint = NULL;
1541 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1542 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
1543 NOCRED);
1544 vput(ump->um_devvp);
1545 free(fs->fs_csp, M_UFSMNT);
1546 free(fs, M_UFSMNT);
1547 if (ump->um_oldfscompat != NULL)
1548 free(ump->um_oldfscompat, M_UFSMNT);
1549 softdep_unmount(mp);
1550 mutex_destroy(&ump->um_lock);
1551 ffs_snapshot_fini(ump);
1552 free(ump, M_UFSMNT);
1553 mp->mnt_data = NULL;
1554 mp->mnt_flag &= ~MNT_LOCAL;
1555 fstrans_unmount(mp);
1556 return (0);
1557 }
1558
1559 /*
1560 * Flush out all the files in a filesystem.
1561 */
1562 int
1563 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1564 {
1565 extern int doforce;
1566 struct ufsmount *ump;
1567 int error;
1568
1569 if (!doforce)
1570 flags &= ~FORCECLOSE;
1571 ump = VFSTOUFS(mp);
1572 #ifdef QUOTA
1573 if (mp->mnt_flag & MNT_QUOTA) {
1574 int i;
1575 if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
1576 return (error);
1577 for (i = 0; i < MAXQUOTAS; i++) {
1578 if (ump->um_quotas[i] == NULLVP)
1579 continue;
1580 quotaoff(l, mp, i);
1581 }
1582 /*
1583 * Here we fall through to vflush again to ensure
1584 * that we have gotten rid of all the system vnodes.
1585 */
1586 }
1587 #endif
1588 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1589 return (error);
1590 ffs_snapshot_unmount(mp);
1591 /*
1592 * Flush all the files.
1593 */
1594 error = vflush(mp, NULLVP, flags);
1595 if (error)
1596 return (error);
1597 /*
1598 * Flush filesystem metadata.
1599 */
1600 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1601 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
1602 VOP_UNLOCK(ump->um_devvp, 0);
1603 if (flags & FORCECLOSE) /* XXXDBJ */
1604 error = 0;
1605
1606 #ifdef WAPBL
1607 if (error)
1608 return error;
1609 if (mp->mnt_wapbl) {
1610 error = wapbl_flush(mp->mnt_wapbl, 1);
1611 if (flags & FORCECLOSE)
1612 error = 0;
1613 }
1614 #endif
1615
1616 return (error);
1617 }
1618
1619 /*
1620 * Get file system statistics.
1621 */
1622 int
1623 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
1624 {
1625 struct ufsmount *ump;
1626 struct fs *fs;
1627
1628 ump = VFSTOUFS(mp);
1629 fs = ump->um_fs;
1630 mutex_enter(&ump->um_lock);
1631 sbp->f_bsize = fs->fs_bsize;
1632 sbp->f_frsize = fs->fs_fsize;
1633 sbp->f_iosize = fs->fs_bsize;
1634 sbp->f_blocks = fs->fs_dsize;
1635 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1636 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1637 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1638 fs->fs_minfree) / (u_int64_t) 100;
1639 if (sbp->f_bfree > sbp->f_bresvd)
1640 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1641 else
1642 sbp->f_bavail = 0;
1643 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1644 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1645 sbp->f_favail = sbp->f_ffree;
1646 sbp->f_fresvd = 0;
1647 mutex_exit(&ump->um_lock);
1648 copy_statvfs_info(sbp, mp);
1649
1650 return (0);
1651 }
1652
1653 /*
1654 * Go through the disk queues to initiate sandbagged IO;
1655 * go through the inodes to write those that have been modified;
1656 * initiate the writing of the super block if it has been modified.
1657 *
1658 * Note: we are always called with the filesystem marked `MPBUSY'.
1659 */
1660 int
1661 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1662 {
1663 struct lwp *l = curlwp;
1664 struct vnode *vp, *mvp;
1665 struct inode *ip;
1666 struct ufsmount *ump = VFSTOUFS(mp);
1667 struct fs *fs;
1668 int error, count, allerror = 0;
1669
1670 fs = ump->um_fs;
1671 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1672 printf("fs = %s\n", fs->fs_fsmnt);
1673 panic("update: rofs mod");
1674 }
1675
1676 /* Allocate a marker vnode. */
1677 if ((mvp = vnalloc(mp)) == NULL)
1678 return (ENOMEM);
1679
1680 fstrans_start(mp, FSTRANS_SHARED);
1681 /*
1682 * Write back each (modified) inode.
1683 */
1684 mutex_enter(&mntvnode_lock);
1685 loop:
1686 /*
1687 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1688 * and vclean() can be called indirectly
1689 */
1690 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
1691 vmark(mvp, vp);
1692 /*
1693 * If the vnode that we are about to sync is no longer
1694 * associated with this mount point, start over.
1695 */
1696 if (vp->v_mount != mp || vismarker(vp))
1697 continue;
1698 mutex_enter(&vp->v_interlock);
1699 ip = VTOI(vp);
1700 /* XXXpooka: why wapbl check? */
1701 if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
1702 vp->v_type == VNON || ((ip->i_flag &
1703 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1704 (LIST_EMPTY(&vp->v_dirtyblkhd) || (mp->mnt_wapbl)) &&
1705 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
1706 {
1707 mutex_exit(&vp->v_interlock);
1708 continue;
1709 }
1710 if (vp->v_type == VBLK &&
1711 fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
1712 mutex_exit(&vp->v_interlock);
1713 continue;
1714 }
1715 mutex_exit(&mntvnode_lock);
1716 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1717 if (error) {
1718 mutex_enter(&mntvnode_lock);
1719 if (error == ENOENT) {
1720 (void)vunmark(mvp);
1721 goto loop;
1722 }
1723 continue;
1724 }
1725 if (vp->v_type == VREG && waitfor == MNT_LAZY) {
1726 error = UFS_WAPBL_BEGIN(vp->v_mount);
1727 if (!error) {
1728 error = ffs_update(vp, NULL, NULL, 0);
1729 UFS_WAPBL_END(vp->v_mount);
1730 }
1731 } else {
1732 error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
1733 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
1734 }
1735 if (error)
1736 allerror = error;
1737 vput(vp);
1738 mutex_enter(&mntvnode_lock);
1739 }
1740 mutex_exit(&mntvnode_lock);
1741 /*
1742 * Force stale file system control information to be flushed.
1743 */
1744 if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
1745 if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
1746 allerror = error;
1747 /* Flushed work items may create new vnodes to clean */
1748 if (allerror == 0 && count) {
1749 mutex_enter(&mntvnode_lock);
1750 goto loop;
1751 }
1752 }
1753 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1754 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1755 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1756 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1757 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
1758 0, 0)) != 0)
1759 allerror = error;
1760 VOP_UNLOCK(ump->um_devvp, 0);
1761 if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
1762 mutex_enter(&mntvnode_lock);
1763 goto loop;
1764 }
1765 }
1766 #ifdef QUOTA
1767 qsync(mp);
1768 #endif
1769 /*
1770 * Write back modified superblock.
1771 */
1772 if (fs->fs_fmod != 0) {
1773 fs->fs_fmod = 0;
1774 fs->fs_time = time_second;
1775 error = UFS_WAPBL_BEGIN(mp);
1776 if (error)
1777 allerror = error;
1778 else {
1779 if ((error = ffs_cgupdate(ump, waitfor)))
1780 allerror = error;
1781 UFS_WAPBL_END(mp);
1782 }
1783 }
1784
1785 #ifdef WAPBL
1786 if (mp->mnt_wapbl) {
1787 error = wapbl_flush(mp->mnt_wapbl, 0);
1788 if (error)
1789 allerror = error;
1790 }
1791 #endif
1792
1793 fstrans_done(mp);
1794 vnfree(mvp);
1795 return (allerror);
1796 }
1797
1798 /*
1799 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1800 * in from disk. If it is in core, wait for the lock bit to clear, then
1801 * return the inode locked. Detection and handling of mount points must be
1802 * done by the calling routine.
1803 */
1804 int
1805 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1806 {
1807 struct fs *fs;
1808 struct inode *ip;
1809 struct ufsmount *ump;
1810 struct buf *bp;
1811 struct vnode *vp;
1812 dev_t dev;
1813 int error;
1814
1815 ump = VFSTOUFS(mp);
1816 dev = ump->um_dev;
1817
1818 retry:
1819 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1820 return (0);
1821
1822 /* Allocate a new vnode/inode. */
1823 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1824 *vpp = NULL;
1825 return (error);
1826 }
1827 ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
1828
1829 /*
1830 * If someone beat us to it, put back the freshly allocated
1831 * vnode/inode pair and retry.
1832 */
1833 mutex_enter(&ufs_hashlock);
1834 if (ufs_ihashget(dev, ino, 0) != NULL) {
1835 mutex_exit(&ufs_hashlock);
1836 ungetnewvnode(vp);
1837 pool_cache_put(ffs_inode_cache, ip);
1838 goto retry;
1839 }
1840
1841 vp->v_vflag |= VV_LOCKSWORK;
1842 if ((mp->mnt_flag & MNT_SOFTDEP) != 0)
1843 vp->v_uflag |= VU_SOFTDEP;
1844
1845 /*
1846 * XXX MFS ends up here, too, to allocate an inode. Should we
1847 * XXX create another pool for MFS inodes?
1848 */
1849
1850 memset(ip, 0, sizeof(struct inode));
1851 vp->v_data = ip;
1852 ip->i_vnode = vp;
1853 ip->i_ump = ump;
1854 ip->i_fs = fs = ump->um_fs;
1855 ip->i_dev = dev;
1856 ip->i_number = ino;
1857 LIST_INIT(&ip->i_pcbufhd);
1858 #ifdef QUOTA
1859 ufsquota_init(ip);
1860 #endif
1861
1862 /*
1863 * Initialize genfs node, we might proceed to destroy it in
1864 * error branches.
1865 */
1866 genfs_node_init(vp, &ffs_genfsops);
1867
1868 /*
1869 * Put it onto its hash chain and lock it so that other requests for
1870 * this inode will block if they arrive while we are sleeping waiting
1871 * for old data structures to be purged or for the contents of the
1872 * disk portion of this inode to be read.
1873 */
1874
1875 ufs_ihashins(ip);
1876 mutex_exit(&ufs_hashlock);
1877
1878 /* Read in the disk contents for the inode, copy into the inode. */
1879 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1880 (int)fs->fs_bsize, NOCRED, 0, &bp);
1881 if (error) {
1882
1883 /*
1884 * The inode does not contain anything useful, so it would
1885 * be misleading to leave it on its hash chain. With mode
1886 * still zero, it will be unlinked and returned to the free
1887 * list by vput().
1888 */
1889
1890 vput(vp);
1891 brelse(bp, 0);
1892 *vpp = NULL;
1893 return (error);
1894 }
1895 if (ip->i_ump->um_fstype == UFS1)
1896 ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
1897 PR_WAITOK);
1898 else
1899 ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
1900 PR_WAITOK);
1901 ffs_load_inode(bp, ip, fs, ino);
1902 if (DOINGSOFTDEP(vp))
1903 softdep_load_inodeblock(ip);
1904 else
1905 ip->i_ffs_effnlink = ip->i_nlink;
1906 brelse(bp, 0);
1907
1908 /*
1909 * Initialize the vnode from the inode, check for aliases.
1910 * Note that the underlying vnode may have changed.
1911 */
1912
1913 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1914
1915 /*
1916 * Finish inode initialization now that aliasing has been resolved.
1917 */
1918
1919 ip->i_devvp = ump->um_devvp;
1920 VREF(ip->i_devvp);
1921
1922 /*
1923 * Ensure that uid and gid are correct. This is a temporary
1924 * fix until fsck has been changed to do the update.
1925 */
1926
1927 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1928 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1929 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1930 } /* XXX */
1931 uvm_vnp_setsize(vp, ip->i_size);
1932 *vpp = vp;
1933 return (0);
1934 }
1935
1936 /*
1937 * File handle to vnode
1938 *
1939 * Have to be really careful about stale file handles:
1940 * - check that the inode number is valid
1941 * - call ffs_vget() to get the locked inode
1942 * - check for an unallocated inode (i_mode == 0)
1943 * - check that the given client host has export rights and return
1944 * those rights via. exflagsp and credanonp
1945 */
1946 int
1947 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1948 {
1949 struct ufid ufh;
1950 struct fs *fs;
1951
1952 if (fhp->fid_len != sizeof(struct ufid))
1953 return EINVAL;
1954
1955 memcpy(&ufh, fhp, sizeof(ufh));
1956 fs = VFSTOUFS(mp)->um_fs;
1957 if (ufh.ufid_ino < ROOTINO ||
1958 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1959 return (ESTALE);
1960 return (ufs_fhtovp(mp, &ufh, vpp));
1961 }
1962
1963 /*
1964 * Vnode pointer to File handle
1965 */
1966 /* ARGSUSED */
1967 int
1968 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1969 {
1970 struct inode *ip;
1971 struct ufid ufh;
1972
1973 if (*fh_size < sizeof(struct ufid)) {
1974 *fh_size = sizeof(struct ufid);
1975 return E2BIG;
1976 }
1977 ip = VTOI(vp);
1978 *fh_size = sizeof(struct ufid);
1979 memset(&ufh, 0, sizeof(ufh));
1980 ufh.ufid_len = sizeof(struct ufid);
1981 ufh.ufid_ino = ip->i_number;
1982 ufh.ufid_gen = ip->i_gen;
1983 memcpy(fhp, &ufh, sizeof(ufh));
1984 return (0);
1985 }
1986
1987 void
1988 ffs_init(void)
1989 {
1990 if (ffs_initcount++ > 0)
1991 return;
1992
1993 ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
1994 "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
1995 ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
1996 "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
1997 ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
1998 "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
1999 softdep_initialize();
2000 ufs_init();
2001 }
2002
2003 void
2004 ffs_reinit(void)
2005 {
2006 softdep_reinitialize();
2007 ufs_reinit();
2008 }
2009
2010 void
2011 ffs_done(void)
2012 {
2013 if (--ffs_initcount > 0)
2014 return;
2015
2016 /* XXX softdep cleanup ? */
2017 ufs_done();
2018 pool_cache_destroy(ffs_dinode2_cache);
2019 pool_cache_destroy(ffs_dinode1_cache);
2020 pool_cache_destroy(ffs_inode_cache);
2021 }
2022
2023 /*
2024 * Write a superblock and associated information back to disk.
2025 */
2026 int
2027 ffs_sbupdate(struct ufsmount *mp, int waitfor)
2028 {
2029 struct fs *fs = mp->um_fs;
2030 struct buf *bp;
2031 int error = 0;
2032 u_int32_t saveflag;
2033
2034 error = ffs_getblk(mp->um_devvp,
2035 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb), FFS_NOBLK,
2036 fs->fs_sbsize, false, &bp);
2037 if (error)
2038 return error;
2039 saveflag = fs->fs_flags & FS_INTERNAL;
2040 fs->fs_flags &= ~FS_INTERNAL;
2041
2042 memcpy(bp->b_data, fs, fs->fs_sbsize);
2043
2044 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
2045 #ifdef FFS_EI
2046 if (mp->um_flags & UFS_NEEDSWAP)
2047 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
2048 #endif
2049 fs->fs_flags |= saveflag;
2050
2051 if (waitfor == MNT_WAIT)
2052 error = bwrite(bp);
2053 else
2054 bawrite(bp);
2055 return (error);
2056 }
2057
2058 int
2059 ffs_cgupdate(struct ufsmount *mp, int waitfor)
2060 {
2061 struct fs *fs = mp->um_fs;
2062 struct buf *bp;
2063 int blks;
2064 void *space;
2065 int i, size, error = 0, allerror = 0;
2066
2067 allerror = ffs_sbupdate(mp, waitfor);
2068 blks = howmany(fs->fs_cssize, fs->fs_fsize);
2069 space = fs->fs_csp;
2070 for (i = 0; i < blks; i += fs->fs_frag) {
2071 size = fs->fs_bsize;
2072 if (i + fs->fs_frag > blks)
2073 size = (blks - i) * fs->fs_fsize;
2074 error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
2075 FFS_NOBLK, size, false, &bp);
2076 if (error)
2077 break;
2078 #ifdef FFS_EI
2079 if (mp->um_flags & UFS_NEEDSWAP)
2080 ffs_csum_swap((struct csum*)space,
2081 (struct csum*)bp->b_data, size);
2082 else
2083 #endif
2084 memcpy(bp->b_data, space, (u_int)size);
2085 space = (char *)space + size;
2086 if (waitfor == MNT_WAIT)
2087 error = bwrite(bp);
2088 else
2089 bawrite(bp);
2090 }
2091 if (!allerror && error)
2092 allerror = error;
2093 return (allerror);
2094 }
2095
2096 int
2097 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
2098 int attrnamespace, const char *attrname)
2099 {
2100 #ifdef UFS_EXTATTR
2101 /*
2102 * File-backed extended attributes are only supported on UFS1.
2103 * UFS2 has native extended attributes.
2104 */
2105 if (VFSTOUFS(mp)->um_fstype == UFS1)
2106 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
2107 #endif
2108 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
2109 }
2110
2111 int
2112 ffs_suspendctl(struct mount *mp, int cmd)
2113 {
2114 int error;
2115 struct lwp *l = curlwp;
2116
2117 switch (cmd) {
2118 case SUSPEND_SUSPEND:
2119 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
2120 return error;
2121 error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
2122 if (error == 0)
2123 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
2124 #ifdef WAPBL
2125 if (error == 0 && mp->mnt_wapbl)
2126 error = wapbl_flush(mp->mnt_wapbl, 1);
2127 #endif
2128 if (error != 0) {
2129 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
2130 return error;
2131 }
2132 return 0;
2133
2134 case SUSPEND_RESUME:
2135 return fstrans_setstate(mp, FSTRANS_NORMAL);
2136
2137 default:
2138 return EINVAL;
2139 }
2140 }
2141