Simplify vn_lock(), VOP_LOCK(), and VOP_UNLOCK() by removing the thread_t
[dragonfly.git] / sys / vfs / ufs / ffs_vfsops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1989, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
34 * $FreeBSD: src/sys/ufs/ffs/ffs_vfsops.c,v 1.117.2.10 2002/06/23 22:34:52 iedowse Exp $
ca466bae 35 * $DragonFly: src/sys/vfs/ufs/ffs_vfsops.c,v 1.40 2006/05/05 21:15:10 dillon Exp $
984263bc
MD
36 */
37
38#include "opt_quota.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
984263bc 42#include <sys/proc.h>
fad57d0e 43#include <sys/nlookup.h>
984263bc
MD
44#include <sys/kernel.h>
45#include <sys/vnode.h>
46#include <sys/mount.h>
47#include <sys/buf.h>
48#include <sys/conf.h>
49#include <sys/fcntl.h>
50#include <sys/disklabel.h>
51#include <sys/malloc.h>
52
1f2de5d4
MD
53#include "quota.h"
54#include "ufsmount.h"
55#include "inode.h"
56#include "ufs_extern.h"
984263bc 57
1f2de5d4
MD
58#include "fs.h"
59#include "ffs_extern.h"
984263bc
MD
60
61#include <vm/vm.h>
62#include <vm/vm_page.h>
63#include <vm/vm_zone.h>
64
65static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
66
a6ee311a
RG
67static int ffs_sbupdate (struct ufsmount *, int);
68static int ffs_reload (struct mount *,struct ucred *,struct thread *);
69static int ffs_oldfscompat (struct fs *);
21739618 70static int ffs_mount (struct mount *, char *, caddr_t, struct thread *);
a6ee311a 71static int ffs_init (struct vfsconf *);
984263bc
MD
72
73static struct vfsops ufs_vfsops = {
43c45e8f
HP
74 .vfs_mount = ffs_mount,
75 .vfs_unmount = ffs_unmount,
76 .vfs_root = ufs_root,
77 .vfs_quotactl = ufs_quotactl,
78 .vfs_statfs = ffs_statfs,
79 .vfs_sync = ffs_sync,
80 .vfs_vget = ffs_vget,
81 .vfs_fhtovp = ffs_fhtovp,
82 .vfs_checkexp = ufs_check_export,
83 .vfs_vptofh = ffs_vptofh,
84 .vfs_init = ffs_init,
85 .vfs_uninit = ufs_uninit
984263bc
MD
86};
87
88VFS_SET(ufs_vfsops, ufs, 0);
89
0961aa92
MD
90extern struct vnodeopv_entry_desc ffs_vnodeop_entries[];
91extern struct vnodeopv_entry_desc ffs_specop_entries[];
92extern struct vnodeopv_entry_desc ffs_fifoop_entries[];
93
94
984263bc
MD
95/*
96 * ffs_mount
97 *
98 * Called when mounting local physical media
99 *
100 * PARAMETERS:
101 * mountroot
102 * mp mount point structure
103 * path NULL (flag for root mount!!!)
104 * data <unused>
984263bc
MD
105 * p process (user credentials check [statfs])
106 *
107 * mount
108 * mp mount point structure
109 * path path to mount point
110 * data pointer to argument struct in user space
984263bc
MD
111 * p process (user credentials check)
112 *
113 * RETURNS: 0 Success
114 * !0 error number (errno.h)
115 *
116 * LOCK STATE:
117 *
118 * ENTRY
119 * mount point is locked
120 * EXIT
121 * mount point is locked
122 *
123 * NOTES:
124 * A NULL path can be used for a flag since the mount
125 * system call will fail with EFAULT in copyinstr in
fad57d0e 126 * nlookup() if it is a genuine NULL from the user.
984263bc
MD
127 */
128static int
0973c589
CP
129ffs_mount(struct mount *mp, /* mount struct pointer */
130 char *path, /* path to mount point */
131 caddr_t data, /* arguments to FS specific mount */
0973c589 132 struct thread *td) /* process requesting mount */
984263bc
MD
133{
134 size_t size;
fad57d0e 135 int error;
984263bc
MD
136 struct vnode *devvp;
137
138 struct ufs_args args;
139 struct ufsmount *ump = 0;
3ff2135f 140 struct fs *fs;
fad57d0e 141 int flags, ronly = 0;
984263bc 142 mode_t accessmode;
dadab5e9 143 struct ucred *cred;
fad57d0e 144 struct nlookupdata nd;
21739618 145 struct vnode *rootvp;
dadab5e9
MD
146
147 KKASSERT(td->td_proc);
148 cred = td->td_proc->p_ucred;
8ddc6004 149 devvp = NULL;
fad57d0e 150 error = 0;
984263bc
MD
151
152 /*
153 * Use NULL path to flag a root mount
154 */
fad57d0e 155 if (path == NULL) {
984263bc
MD
156 /*
157 ***
f719c866 158 * Mounting root filesystem
984263bc
MD
159 ***
160 */
161
fad57d0e 162 if ((error = bdevvp(rootdev, &rootvp))) {
984263bc 163 printf("ffs_mountroot: can't find rootvp\n");
fad57d0e 164 return (error);
984263bc
MD
165 }
166
fad57d0e 167 if( ( error = ffs_mountfs(rootvp, mp, td, M_FFSNODE)) != 0) {
984263bc
MD
168 /* fs specific cleanup (if any)*/
169 goto error_1;
170 }
8ddc6004 171 devvp = rootvp;
984263bc
MD
172
173 goto dostatfs; /* success*/
174
175 }
176
177 /*
178 ***
f719c866 179 * Mounting non-root filesystem or updating a filesystem
984263bc
MD
180 ***
181 */
182
183 /* copy in user arguments*/
fad57d0e
MD
184 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
185 if (error)
984263bc
MD
186 goto error_1; /* can't get arguments*/
187
188 /*
189 * If updating, check whether changing from read-only to
190 * read/write; if there is no device name, that's all we do.
191 */
192 if (mp->mnt_flag & MNT_UPDATE) {
193 ump = VFSTOUFS(mp);
194 fs = ump->um_fs;
195 devvp = ump->um_devvp;
fad57d0e 196 error = 0;
984263bc
MD
197 ronly = fs->fs_ronly; /* MNT_RELOAD might change this */
198 if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
199 /*
200 * Flush any dirty data.
201 */
3b568787 202 VFS_SYNC(mp, MNT_WAIT, td);
984263bc
MD
203 /*
204 * Check for and optionally get rid of files open
205 * for writing.
206 */
207 flags = WRITECLOSE;
208 if (mp->mnt_flag & MNT_FORCE)
209 flags |= FORCECLOSE;
210 if (mp->mnt_flag & MNT_SOFTDEP) {
fad57d0e 211 error = softdep_flushfiles(mp, flags, td);
984263bc 212 } else {
fad57d0e 213 error = ffs_flushfiles(mp, flags, td);
984263bc
MD
214 }
215 ronly = 1;
216 }
8ddc6004 217 if (!error && (mp->mnt_flag & MNT_RELOAD)) {
fad57d0e 218 error = ffs_reload(mp, NULL, td);
8ddc6004 219 }
fad57d0e 220 if (error) {
984263bc
MD
221 goto error_1;
222 }
223 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
224 /*
225 * If upgrade to read-write by non-root, then verify
226 * that user has necessary permissions on the device.
227 */
dadab5e9 228 if (cred->cr_uid != 0) {
ca466bae 229 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
984263bc 230 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
dadab5e9 231 cred, td)) != 0) {
ca466bae 232 VOP_UNLOCK(devvp, 0);
984263bc
MD
233 return (error);
234 }
ca466bae 235 VOP_UNLOCK(devvp, 0);
984263bc
MD
236 }
237
238 fs->fs_flags &= ~FS_UNCLEAN;
239 if (fs->fs_clean == 0) {
240 fs->fs_flags |= FS_UNCLEAN;
241 if (mp->mnt_flag & MNT_FORCE) {
242 printf(
243"WARNING: %s was not properly dismounted\n",
244 fs->fs_fsmnt);
245 } else {
246 printf(
247"WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
248 fs->fs_fsmnt);
fad57d0e 249 error = EPERM;
984263bc
MD
250 goto error_1;
251 }
252 }
253
254 /* check to see if we need to start softdep */
255 if (fs->fs_flags & FS_DOSOFTDEP) {
fad57d0e
MD
256 error = softdep_mount(devvp, mp, fs);
257 if (error)
984263bc
MD
258 goto error_1;
259 }
984263bc
MD
260 ronly = 0;
261 }
262 /*
263 * Soft updates is incompatible with "async",
264 * so if we are doing softupdates stop the user
265 * from setting the async flag in an update.
266 * Softdep_mount() clears it in an initial mount
267 * or ro->rw remount.
268 */
269 if (mp->mnt_flag & MNT_SOFTDEP) {
270 mp->mnt_flag &= ~MNT_ASYNC;
271 }
272 /* if not updating name...*/
273 if (args.fspec == 0) {
274 /*
275 * Process export requests. Jumping to "success"
276 * will return the vfs_export() error code.
277 */
fad57d0e 278 error = vfs_export(mp, &ump->um_export, &args.export);
984263bc
MD
279 goto success;
280 }
281 }
282
283 /*
284 * Not an update, or updating the name: look up the name
285 * and verify that it refers to a sensible block device.
286 */
fad57d0e
MD
287 devvp = NULL;
288 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
289 if (error == 0)
290 error = nlookup(&nd);
291 if (error == 0)
292 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
293 nlookup_done(&nd);
294 if (error)
984263bc 295 goto error_1;
984263bc 296
fad57d0e 297 if (!vn_isdisk(devvp, &error))
984263bc
MD
298 goto error_2;
299
300 /*
301 * If mount by non-root, then verify that user has necessary
302 * permissions on the device.
303 */
dadab5e9 304 if (cred->cr_uid != 0) {
984263bc
MD
305 accessmode = VREAD;
306 if ((mp->mnt_flag & MNT_RDONLY) == 0)
307 accessmode |= VWRITE;
ca466bae 308 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
dadab5e9 309 if ((error = VOP_ACCESS(devvp, accessmode, cred, td)) != 0) {
984263bc
MD
310 vput(devvp);
311 return (error);
312 }
ca466bae 313 VOP_UNLOCK(devvp, 0);
984263bc
MD
314 }
315
316 if (mp->mnt_flag & MNT_UPDATE) {
317 /*
8ddc6004
MD
318 * UPDATE - make sure the resolved vnode represents the same
319 * device. Note that devvp->v_rdev may be NULL since we
320 * haven't opened it, so compare udev instead.
321 *
322 * Our current open/writecount state is associated with
323 * um_devvp, so continue using um_devvp and throw away devvp.
984263bc 324 */
984263bc 325 if (devvp != ump->um_devvp) {
e4c9c0c8 326 if (devvp->v_udev == ump->um_devvp->v_udev) {
984263bc 327 vrele(devvp);
8ddc6004 328 devvp = ump->um_devvp;
984263bc 329 } else {
e4c9c0c8
MD
330 printf("cannot update mount, udev does"
331 " not match %08x vs %08x\n",
332 devvp->v_udev, ump->um_devvp->v_udev);
fad57d0e 333 error = EINVAL; /* needs translation */
984263bc 334 }
e4c9c0c8 335 } else {
984263bc 336 vrele(devvp);
e4c9c0c8 337 }
984263bc
MD
338 /*
339 * Update device name only on success
340 */
fad57d0e 341 if (!error) {
984263bc
MD
342 /* Save "mounted from" info for mount point (NULL pad)*/
343 copyinstr( args.fspec,
344 mp->mnt_stat.f_mntfromname,
345 MNAMELEN - 1,
346 &size);
347 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
348 }
349 } else {
350 /*
351 ********************
352 * NEW MOUNT
353 ********************
354 */
355
984263bc
MD
356 /* Save "mounted from" info for mount point (NULL pad)*/
357 copyinstr( args.fspec, /* device name*/
358 mp->mnt_stat.f_mntfromname, /* save area*/
359 MNAMELEN - 1, /* max size*/
360 &size); /* real size*/
361 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
362
fad57d0e 363 error = ffs_mountfs(devvp, mp, td, M_FFSNODE);
984263bc 364 }
fad57d0e 365 if (error) {
984263bc
MD
366 goto error_2;
367 }
368
369dostatfs:
370 /*
75ffff0d
JS
371 * Initialize FS stat information in mount struct; uses
372 * mp->mnt_stat.f_mntfromname.
984263bc
MD
373 *
374 * This code is common to root and non-root mounts
375 */
dadab5e9 376 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
984263bc
MD
377
378 goto success;
379
380
381error_2: /* error with devvp held*/
382
383 /* release devvp before failing*/
384 vrele(devvp);
385
386error_1: /* no state to back out*/
387
388success:
fad57d0e 389 if (!error && path && (mp->mnt_flag & MNT_UPDATE)) {
984263bc
MD
390 /* Update clean flag after changing read-onlyness. */
391 fs = ump->um_fs;
392 if (ronly != fs->fs_ronly) {
393 fs->fs_ronly = ronly;
394 fs->fs_clean = ronly &&
395 (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
8ddc6004
MD
396
397 /*
398 * The device must be re-opened as appropriate or
399 * the device close at unmount time will panic.
400 */
ca466bae 401 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
8ddc6004
MD
402 if (ronly) {
403 VOP_OPEN(devvp, FREAD, FSCRED, NULL, td);
404 VOP_CLOSE(devvp, FREAD|FWRITE, td);
405 } else {
406 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL, td);
407 VOP_CLOSE(devvp, FREAD, td);
408 }
ca466bae 409 VOP_UNLOCK(devvp, 0);
984263bc
MD
410 ffs_sbupdate(ump, MNT_WAIT);
411 }
412 }
fad57d0e 413 return (error);
984263bc
MD
414}
415
416/*
417 * Reload all incore data for a filesystem (used after running fsck on
418 * the root filesystem and finding things to fix). The filesystem must
419 * be mounted read-only.
420 *
421 * Things to do to update the mount:
422 * 1) invalidate all cached meta-data.
423 * 2) re-read superblock from disk.
424 * 3) re-read summary information from disk.
425 * 4) invalidate all inactive vnodes.
426 * 5) invalidate all cached file data.
427 * 6) re-read inode data for all active vnodes.
428 */
41a01a4d 429
5fd012e0 430static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data);
41a01a4d
MD
431
432struct scaninfo {
433 int rescan;
434 struct fs *fs;
435 struct vnode *devvp;
436 thread_t td;
437 int waitfor;
438 int allerror;
439};
440
984263bc 441static int
dadab5e9 442ffs_reload(struct mount *mp, struct ucred *cred, struct thread *td)
984263bc 443{
41a01a4d 444 struct vnode *devvp;
984263bc
MD
445 void *space;
446 struct buf *bp;
447 struct fs *fs, *newfs;
448 struct partinfo dpart;
449 dev_t dev;
450 int i, blks, size, error;
41a01a4d 451 struct scaninfo scaninfo;
984263bc
MD
452 int32_t *lp;
453
454 if ((mp->mnt_flag & MNT_RDONLY) == 0)
455 return (EINVAL);
456 /*
457 * Step 1: invalidate all cached meta-data.
458 */
459 devvp = VFSTOUFS(mp)->um_devvp;
ca466bae 460 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
3b568787 461 error = vinvalbuf(devvp, 0, td, 0, 0);
ca466bae 462 VOP_UNLOCK(devvp, 0);
984263bc
MD
463 if (error)
464 panic("ffs_reload: dirty1");
465
466 dev = devvp->v_rdev;
7540ab49 467
984263bc 468 /*
7540ab49
MD
469 * The backing device must be VMIO-capable because we use getblk().
470 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
984263bc 471 */
7540ab49
MD
472 if (devvp->v_object == NULL)
473 panic("ffs_reload: devvp has no VM object!");
984263bc
MD
474
475 /*
476 * Step 2: re-read superblock from disk.
477 */
dadab5e9 478 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, td) != 0)
984263bc
MD
479 size = DEV_BSIZE;
480 else
481 size = dpart.disklab->d_secsize;
54078292 482 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
8a1e3a1a 483 brelse(bp);
984263bc 484 return (error);
8a1e3a1a 485 }
984263bc
MD
486 newfs = (struct fs *)bp->b_data;
487 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
488 newfs->fs_bsize < sizeof(struct fs)) {
489 brelse(bp);
490 return (EIO); /* XXX needs translation */
491 }
492 fs = VFSTOUFS(mp)->um_fs;
493 /*
494 * Copy pointer fields back into superblock before copying in XXX
495 * new superblock. These should really be in the ufsmount. XXX
496 * Note that important parameters (eg fs_ncg) are unchanged.
497 */
498 newfs->fs_csp = fs->fs_csp;
499 newfs->fs_maxcluster = fs->fs_maxcluster;
500 newfs->fs_contigdirs = fs->fs_contigdirs;
f719c866 501 /* The filesystem is still read-only. */
a414e93f 502 newfs->fs_ronly = 1;
f719c866 503 bcopy(newfs, fs, (uint)fs->fs_sbsize);
984263bc
MD
504 if (fs->fs_sbsize < SBSIZE)
505 bp->b_flags |= B_INVAL;
506 brelse(bp);
507 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
508 ffs_oldfscompat(fs);
509 /* An old fsck may have zeroed these fields, so recheck them. */
510 if (fs->fs_avgfilesize <= 0) /* XXX */
511 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
512 if (fs->fs_avgfpdir <= 0) /* XXX */
513 fs->fs_avgfpdir = AFPDIR; /* XXX */
514
515 /*
516 * Step 3: re-read summary information from disk.
517 */
518 blks = howmany(fs->fs_cssize, fs->fs_fsize);
519 space = fs->fs_csp;
520 for (i = 0; i < blks; i += fs->fs_frag) {
521 size = fs->fs_bsize;
522 if (i + fs->fs_frag > blks)
523 size = (blks - i) * fs->fs_fsize;
54078292 524 error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), size, &bp);
3446c007 525 if (error) {
8a1e3a1a 526 brelse(bp);
984263bc 527 return (error);
8a1e3a1a 528 }
f719c866 529 bcopy(bp->b_data, space, (uint)size);
984263bc
MD
530 space = (char *)space + size;
531 brelse(bp);
532 }
533 /*
534 * We no longer know anything about clusters per cylinder group.
535 */
536 if (fs->fs_contigsumsize > 0) {
537 lp = fs->fs_maxcluster;
538 for (i = 0; i < fs->fs_ncg; i++)
539 *lp++ = fs->fs_contigsumsize;
540 }
541
41a01a4d
MD
542 scaninfo.rescan = 0;
543 scaninfo.fs = fs;
544 scaninfo.devvp = devvp;
545 scaninfo.td = td;
546 while (error == 0 && scaninfo.rescan) {
547 scaninfo.rescan = 0;
5fd012e0
MD
548 error = vmntvnodescan(mp, VMSC_GETVX,
549 NULL, ffs_reload_scan2, &scaninfo);
41a01a4d
MD
550 }
551 return(error);
552}
553
f719c866 554static int
5fd012e0 555ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
41a01a4d
MD
556{
557 struct scaninfo *info = data;
558 struct inode *ip;
559 struct buf *bp;
560 int error;
561
562 /*
5fd012e0 563 * Try to recycle
41a01a4d 564 */
5fd012e0 565 if (vrecycle(vp, info->td))
41a01a4d 566 return(0);
5fd012e0 567
41a01a4d
MD
568 if (vinvalbuf(vp, 0, info->td, 0, 0))
569 panic("ffs_reload: dirty2");
570 /*
571 * Step 6: re-read inode data for all active vnodes.
572 */
573 ip = VTOI(vp);
574 error = bread(info->devvp,
54078292
MD
575 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
576 (int)info->fs->fs_bsize, &bp);
41a01a4d 577 if (error) {
8a1e3a1a 578 brelse(bp);
41a01a4d 579 return (error);
984263bc 580 }
50e58362 581 ip->i_din = *((struct ufs1_dinode *)bp->b_data +
41a01a4d
MD
582 ino_to_fsbo(info->fs, ip->i_number));
583 ip->i_effnlink = ip->i_nlink;
584 brelse(bp);
41a01a4d 585 return(0);
984263bc
MD
586}
587
588/*
589 * Common code for mount and mountroot
590 */
591int
0973c589
CP
592ffs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
593 struct malloc_type *malloctype)
984263bc 594{
3ff2135f 595 struct ufsmount *ump;
984263bc 596 struct buf *bp;
3ff2135f 597 struct fs *fs;
984263bc
MD
598 dev_t dev;
599 struct partinfo dpart;
600 void *space;
601 int error, i, blks, size, ronly;
602 int32_t *lp;
f719c866 603 uint64_t maxfilesize; /* XXX */
984263bc 604
984263bc
MD
605 /*
606 * Disallow multiple mounts of the same device.
607 * Disallow mounting of a device that is currently in use
984263bc
MD
608 * Flush out any old buffers remaining from a previous use.
609 */
610 error = vfs_mountedon(devvp);
611 if (error)
612 return (error);
21739618 613 if (count_udev(devvp->v_udev) > 0)
984263bc 614 return (EBUSY);
ca466bae 615 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
3b568787 616 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
ca466bae 617 VOP_UNLOCK(devvp, 0);
984263bc
MD
618 if (error)
619 return (error);
620
984263bc 621 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
ca466bae 622 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
fad57d0e 623 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
ca466bae 624 VOP_UNLOCK(devvp, 0);
984263bc
MD
625 if (error)
626 return (error);
e4c9c0c8
MD
627 dev = devvp->v_rdev;
628 if (dev->si_iosize_max != 0)
629 mp->mnt_iosize_max = dev->si_iosize_max;
984263bc
MD
630 if (mp->mnt_iosize_max > MAXPHYS)
631 mp->mnt_iosize_max = MAXPHYS;
632
bbac59c4 633 /*
7540ab49
MD
634 * The backing device must be VMIO-capable because we use getblk().
635 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
636 * The VOP_OPEN() call above should have associated a VM object
637 * with devvp.
bbac59c4 638 */
7540ab49
MD
639 if (devvp->v_object == NULL)
640 panic("ffs_reload: devvp has no VM object!");
bbac59c4 641
3b568787 642 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, proc0.p_ucred, td) != 0)
984263bc
MD
643 size = DEV_BSIZE;
644 else
645 size = dpart.disklab->d_secsize;
646
647 bp = NULL;
648 ump = NULL;
54078292 649 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
984263bc
MD
650 goto out;
651 fs = (struct fs *)bp->b_data;
652 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
653 fs->fs_bsize < sizeof(struct fs)) {
654 error = EINVAL; /* XXX needs translation */
655 goto out;
656 }
657 fs->fs_fmod = 0;
658 fs->fs_flags &= ~FS_UNCLEAN;
659 if (fs->fs_clean == 0) {
660 fs->fs_flags |= FS_UNCLEAN;
661 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
662 printf(
663"WARNING: %s was not properly dismounted\n",
664 fs->fs_fsmnt);
665 } else {
666 printf(
667"WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
668 fs->fs_fsmnt);
669 error = EPERM;
670 goto out;
671 }
672 }
673 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
674 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
675 error = EROFS; /* needs translation */
676 goto out;
677 }
678 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
679 bzero((caddr_t)ump, sizeof *ump);
680 ump->um_malloctype = malloctype;
681 ump->um_i_effnlink_valid = 1;
682 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
683 M_WAITOK);
684 ump->um_blkatoff = ffs_blkatoff;
685 ump->um_truncate = ffs_truncate;
686 ump->um_update = ffs_update;
687 ump->um_valloc = ffs_valloc;
688 ump->um_vfree = ffs_vfree;
f719c866 689 bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
984263bc
MD
690 if (fs->fs_sbsize < SBSIZE)
691 bp->b_flags |= B_INVAL;
692 brelse(bp);
693 bp = NULL;
694 fs = ump->um_fs;
695 fs->fs_ronly = ronly;
696 size = fs->fs_cssize;
697 blks = howmany(size, fs->fs_fsize);
698 if (fs->fs_contigsumsize > 0)
699 size += fs->fs_ncg * sizeof(int32_t);
f719c866 700 size += fs->fs_ncg * sizeof(uint8_t);
984263bc
MD
701 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
702 fs->fs_csp = space;
703 for (i = 0; i < blks; i += fs->fs_frag) {
704 size = fs->fs_bsize;
705 if (i + fs->fs_frag > blks)
706 size = (blks - i) * fs->fs_fsize;
54078292
MD
707 if ((error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i),
708 size, &bp)) != 0) {
984263bc
MD
709 free(fs->fs_csp, M_UFSMNT);
710 goto out;
711 }
f719c866 712 bcopy(bp->b_data, space, (uint)size);
984263bc
MD
713 space = (char *)space + size;
714 brelse(bp);
715 bp = NULL;
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 }
f719c866
DR
723 size = fs->fs_ncg * sizeof(uint8_t);
724 fs->fs_contigdirs = (uint8_t *)space;
984263bc
MD
725 bzero(fs->fs_contigdirs, size);
726 /* Compatibility for old filesystems XXX */
727 if (fs->fs_avgfilesize <= 0) /* XXX */
728 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
729 if (fs->fs_avgfpdir <= 0) /* XXX */
730 fs->fs_avgfpdir = AFPDIR; /* XXX */
731 mp->mnt_data = (qaddr_t)ump;
732 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
733 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
734 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
735 vfs_getvfs(&mp->mnt_stat.f_fsid))
736 vfs_getnewfsid(mp);
737 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
738 mp->mnt_flag |= MNT_LOCAL;
739 ump->um_mountp = mp;
740 ump->um_dev = dev;
741 ump->um_devvp = devvp;
742 ump->um_nindir = fs->fs_nindir;
743 ump->um_bptrtodb = fs->fs_fsbtodb;
744 ump->um_seqinc = fs->fs_frag;
745 for (i = 0; i < MAXQUOTAS; i++)
746 ump->um_quotas[i] = NULLVP;
e4c9c0c8 747 dev->si_mountpoint = mp;
984263bc
MD
748 ffs_oldfscompat(fs);
749
984263bc
MD
750 if( mp->mnt_flag & MNT_ROOTFS) {
751 /*
752 * Root mount; update timestamp in mount structure.
753 * this will be used by the common root mount code
754 * to update the system clock.
755 */
756 mp->mnt_time = fs->fs_time;
757 }
758
759 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
f719c866 760 maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
984263bc 761 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
f719c866
DR
762 if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
763 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
984263bc
MD
764 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
765 fs->fs_maxfilesize = maxfilesize; /* XXX */
766 if (ronly == 0) {
767 if ((fs->fs_flags & FS_DOSOFTDEP) &&
3b568787 768 (error = softdep_mount(devvp, mp, fs)) != 0) {
984263bc
MD
769 free(fs->fs_csp, M_UFSMNT);
770 goto out;
771 }
772 fs->fs_fmod = 1;
773 fs->fs_clean = 0;
774 (void) ffs_sbupdate(ump, MNT_WAIT);
775 }
dc1be39c
MD
776 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, ffs_vnodeop_entries, VVF_SUPPORTS_FSMID);
777 vfs_add_vnodeops(mp, &mp->mnt_vn_spec_ops, ffs_specop_entries, VVF_SUPPORTS_FSMID);
778 vfs_add_vnodeops(mp, &mp->mnt_vn_fifo_ops, ffs_fifoop_entries, VVF_SUPPORTS_FSMID);
0961aa92 779
984263bc
MD
780 return (0);
781out:
e4c9c0c8 782 dev->si_mountpoint = NULL;
984263bc
MD
783 if (bp)
784 brelse(bp);
e4c9c0c8 785 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
984263bc
MD
786 if (ump) {
787 free(ump->um_fs, M_UFSMNT);
788 free(ump, M_UFSMNT);
789 mp->mnt_data = (qaddr_t)0;
790 }
791 return (error);
792}
793
794/*
f719c866 795 * Sanity checks for old filesystems.
984263bc
MD
796 *
797 * XXX - goes away some day.
798 */
799static int
0973c589 800ffs_oldfscompat(struct fs *fs)
984263bc 801{
984263bc
MD
802 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
803 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
804 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
805 fs->fs_nrpos = 8; /* XXX */
806 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
807#if 0
808 int i; /* XXX */
f719c866 809 uint64_t sizepb = fs->fs_bsize; /* XXX */
984263bc
MD
810 /* XXX */
811 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
812 for (i = 0; i < NIADDR; i++) { /* XXX */
813 sizepb *= NINDIR(fs); /* XXX */
814 fs->fs_maxfilesize += sizepb; /* XXX */
815 } /* XXX */
816#endif
817 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
818 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
819 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
820 } /* XXX */
821 return (0);
822}
823
824/*
825 * unmount system call
826 */
827int
dadab5e9 828ffs_unmount(struct mount *mp, int mntflags, struct thread *td)
984263bc 829{
3ff2135f
RG
830 struct ufsmount *ump;
831 struct fs *fs;
984263bc
MD
832 int error, flags;
833
834 flags = 0;
835 if (mntflags & MNT_FORCE) {
836 flags |= FORCECLOSE;
837 }
838 if (mp->mnt_flag & MNT_SOFTDEP) {
dadab5e9 839 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
984263bc
MD
840 return (error);
841 } else {
dadab5e9 842 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
984263bc
MD
843 return (error);
844 }
845 ump = VFSTOUFS(mp);
846 fs = ump->um_fs;
847 if (fs->fs_ronly == 0) {
848 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
849 error = ffs_sbupdate(ump, MNT_WAIT);
850 if (error) {
851 fs->fs_clean = 0;
852 return (error);
853 }
854 }
e4c9c0c8 855 ump->um_devvp->v_rdev->si_mountpoint = NULL;
984263bc 856
3b568787
MD
857 vinvalbuf(ump->um_devvp, V_SAVE, td, 0, 0);
858 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, td);
984263bc
MD
859
860 vrele(ump->um_devvp);
861
862 free(fs->fs_csp, M_UFSMNT);
863 free(fs, M_UFSMNT);
864 free(ump, M_UFSMNT);
865 mp->mnt_data = (qaddr_t)0;
866 mp->mnt_flag &= ~MNT_LOCAL;
867 return (error);
868}
869
870/*
871 * Flush out all the files in a filesystem.
872 */
873int
dadab5e9 874ffs_flushfiles(struct mount *mp, int flags, struct thread *td)
984263bc 875{
dadab5e9 876 struct ufsmount *ump;
984263bc
MD
877 int error;
878
879 ump = VFSTOUFS(mp);
880#ifdef QUOTA
881 if (mp->mnt_flag & MNT_QUOTA) {
882 int i;
883 error = vflush(mp, 0, SKIPSYSTEM|flags);
884 if (error)
885 return (error);
0f1737b3 886 /* Find out how many quota files we have open. */
984263bc
MD
887 for (i = 0; i < MAXQUOTAS; i++) {
888 if (ump->um_quotas[i] == NULLVP)
889 continue;
50e58362 890 ufs_quotaoff(td, mp, i);
984263bc
MD
891 }
892 /*
893 * Here we fall through to vflush again to ensure
894 * that we have gotten rid of all the system vnodes.
895 */
896 }
897#endif
898 /*
899 * Flush all the files.
900 */
901 if ((error = vflush(mp, 0, flags)) != 0)
902 return (error);
903 /*
904 * Flush filesystem metadata.
905 */
ca466bae 906 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
3b568787 907 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
ca466bae 908 VOP_UNLOCK(ump->um_devvp, 0);
984263bc
MD
909 return (error);
910}
911
912/*
f719c866 913 * Get filesystem statistics.
984263bc
MD
914 */
915int
dadab5e9 916ffs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
984263bc 917{
3ff2135f
RG
918 struct ufsmount *ump;
919 struct fs *fs;
984263bc
MD
920
921 ump = VFSTOUFS(mp);
922 fs = ump->um_fs;
923 if (fs->fs_magic != FS_MAGIC)
924 panic("ffs_statfs");
925 sbp->f_bsize = fs->fs_fsize;
926 sbp->f_iosize = fs->fs_bsize;
927 sbp->f_blocks = fs->fs_dsize;
928 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
929 fs->fs_cstotal.cs_nffree;
930 sbp->f_bavail = freespace(fs, fs->fs_minfree);
931 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
932 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
933 if (sbp != &mp->mnt_stat) {
934 sbp->f_type = mp->mnt_vfc->vfc_typenum;
984263bc
MD
935 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
936 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
937 }
938 return (0);
939}
940
941/*
942 * Go through the disk queues to initiate sandbagged IO;
943 * go through the inodes to write those that have been modified;
944 * initiate the writing of the super block if it has been modified.
945 *
946 * Note: we are always called with the filesystem marked `MPBUSY'.
947 */
41a01a4d
MD
948
949
950static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
5fd012e0 951static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
41a01a4d 952
984263bc 953int
3b568787 954ffs_sync(struct mount *mp, int waitfor, struct thread *td)
984263bc 955{
984263bc
MD
956 struct ufsmount *ump = VFSTOUFS(mp);
957 struct fs *fs;
41a01a4d
MD
958 int error;
959 struct scaninfo scaninfo;
984263bc
MD
960
961 fs = ump->um_fs;
962 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
963 printf("fs = %s\n", fs->fs_fsmnt);
964 panic("ffs_sync: rofs mod");
965 }
41a01a4d 966
984263bc
MD
967 /*
968 * Write back each (modified) inode.
969 */
41a01a4d
MD
970 scaninfo.allerror = 0;
971 scaninfo.rescan = 1;
972 scaninfo.waitfor = waitfor;
973 while (scaninfo.rescan) {
974 scaninfo.rescan = 0;
5fd012e0
MD
975 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
976 ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
984263bc 977 }
41a01a4d 978
984263bc 979 /*
f719c866 980 * Force stale filesystem control information to be flushed.
984263bc
MD
981 */
982 if (waitfor != MNT_LAZY) {
983 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
984 waitfor = MNT_NOWAIT;
ca466bae 985 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
3b568787 986 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
41a01a4d 987 scaninfo.allerror = error;
ca466bae 988 VOP_UNLOCK(ump->um_devvp, 0);
984263bc
MD
989 }
990#ifdef QUOTA
50e58362 991 ufs_qsync(mp);
984263bc
MD
992#endif
993 /*
994 * Write back modified superblock.
995 */
996 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
41a01a4d
MD
997 scaninfo.allerror = error;
998 return (scaninfo.allerror);
999}
1000
f719c866 1001static int
41a01a4d
MD
1002ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1003{
1004 struct inode *ip;
1005
1006 /*
1007 * Depend on the mount list's vnode lock to keep things stable
1008 * enough for a quick test. Since there might be hundreds of
1009 * thousands of vnodes, we cannot afford even a subroutine
1010 * call unless there's a good chance that we have work to do.
1011 */
1012 ip = VTOI(vp);
0f1737b3
DR
1013 /* Restart out whole search if this guy is locked
1014 * or is being reclaimed.
1015 */
41a01a4d
MD
1016 if (vp->v_type == VNON || ((ip->i_flag &
1017 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
6bae6177 1018 RB_EMPTY(&vp->v_rbdirty_tree))) {
41a01a4d
MD
1019 return(-1);
1020 }
1021 return(0);
1022}
1023
f719c866 1024static int
5fd012e0 1025ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
41a01a4d
MD
1026{
1027 struct scaninfo *info = data;
1028 thread_t td = curthread; /* XXX */
1029 struct inode *ip;
1030 int error;
1031
1032 /*
1033 * We have to recheck after having obtained the vnode interlock.
1034 */
1035 ip = VTOI(vp);
1036 if (vp->v_type == VNON || ((ip->i_flag &
1037 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
6bae6177 1038 RB_EMPTY(&vp->v_rbdirty_tree))) {
41a01a4d
MD
1039 return(0);
1040 }
1041 if (vp->v_type != VCHR) {
5fd012e0
MD
1042 if ((error = VOP_FSYNC(vp, info->waitfor, td)) != 0)
1043 info->allerror = error;
41a01a4d
MD
1044 } else {
1045 /*
1046 * We must reference the vp to prevent it from
1047 * getting ripped out from under UFS_UPDATE, since
1048 * we are not holding a vnode lock.
1049 */
41a01a4d
MD
1050 /* UFS_UPDATE(vp, waitfor == MNT_WAIT); */
1051 UFS_UPDATE(vp, 0);
41a01a4d
MD
1052 }
1053 return(0);
984263bc
MD
1054}
1055
1056/*
1057 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1058 * in from disk. If it is in core, wait for the lock bit to clear, then
1059 * return the inode locked. Detection and handling of mount points must be
1060 * done by the calling routine.
1061 */
984263bc
MD
1062
1063int
0973c589 1064ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
984263bc
MD
1065{
1066 struct fs *fs;
1067 struct inode *ip;
1068 struct ufsmount *ump;
1069 struct buf *bp;
1070 struct vnode *vp;
1071 dev_t dev;
1072 int error;
1073
1074 ump = VFSTOUFS(mp);
1075 dev = ump->um_dev;
1076restart:
1077 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1078 return (0);
1079 }
1080
984263bc
MD
1081 /*
1082 * If this MALLOC() is performed after the getnewvnode()
1083 * it might block, leaving a vnode with a NULL v_data to be
1084 * found by ffs_sync() if a sync happens to fire right then,
1085 * which will cause a panic because ffs_sync() blindly
1086 * dereferences vp->v_data (as well it should).
5fd012e0
MD
1087 *
1088 * XXX this may no longer be true since getnewvnode returns a
1089 * VX locked vnode now.
984263bc
MD
1090 */
1091 MALLOC(ip, struct inode *, sizeof(struct inode),
1092 ump->um_malloctype, M_WAITOK);
1093
1094 /* Allocate a new vnode/inode. */
6ddb7618 1095 error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
984263bc 1096 if (error) {
984263bc 1097 *vpp = NULL;
3446c007 1098 free(ip, ump->um_malloctype);
984263bc
MD
1099 return (error);
1100 }
1101 bzero((caddr_t)ip, sizeof(struct inode));
984263bc
MD
1102 ip->i_vnode = vp;
1103 ip->i_fs = fs = ump->um_fs;
1104 ip->i_dev = dev;
1105 ip->i_number = ino;
1106#ifdef QUOTA
1107 {
1108 int i;
1109 for (i = 0; i < MAXQUOTAS; i++)
1110 ip->i_dquot[i] = NODQUOT;
1111 }
1112#endif
0f1737b3 1113
984263bc 1114 /*
3446c007
MD
1115 * Insert it into the inode hash table and check for a collision.
1116 * If a collision occurs, throw away the vnode and try again.
984263bc 1117 */
3446c007
MD
1118 if (ufs_ihashins(ip) != 0) {
1119 printf("debug: ufs ihashins collision, retrying inode %ld\n",
1120 (long)ip->i_number);
5fd012e0 1121 vx_put(vp);
3446c007
MD
1122 free(ip, ump->um_malloctype);
1123 goto restart;
1124 }
1125 vp->v_data = ip;
984263bc
MD
1126
1127 /* Read in the disk contents for the inode, copy into the inode. */
54078292 1128 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
3b568787 1129 (int)fs->fs_bsize, &bp);
984263bc
MD
1130 if (error) {
1131 /*
1132 * The inode does not contain anything useful, so it would
1133 * be misleading to leave it on its hash chain. With mode
1134 * still zero, it will be unlinked and returned to the free
1135 * list by vput().
1136 */
1137 brelse(bp);
5fd012e0 1138 vx_put(vp);
984263bc
MD
1139 *vpp = NULL;
1140 return (error);
1141 }
50e58362 1142 ip->i_din = *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
984263bc
MD
1143 if (DOINGSOFTDEP(vp))
1144 softdep_load_inodeblock(ip);
1145 else
1146 ip->i_effnlink = ip->i_nlink;
1147 bqrelse(bp);
1148
1149 /*
1150 * Initialize the vnode from the inode, check for aliases.
1151 * Note that the underlying vnode may have changed.
1152 */
0961aa92 1153 error = ufs_vinit(mp, &vp);
984263bc 1154 if (error) {
5fd012e0 1155 vx_put(vp);
984263bc
MD
1156 *vpp = NULL;
1157 return (error);
1158 }
1159 /*
1160 * Finish inode initialization now that aliasing has been resolved.
1161 */
1162 ip->i_devvp = ump->um_devvp;
597aea93 1163 vref(ip->i_devvp);
984263bc
MD
1164 /*
1165 * Set up a generation number for this inode if it does not
1166 * already have one. This should only happen on old filesystems.
1167 */
1168 if (ip->i_gen == 0) {
1169 ip->i_gen = random() / 2 + 1;
1170 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1171 ip->i_flag |= IN_MODIFIED;
1172 }
1173 /*
1174 * Ensure that uid and gid are correct. This is a temporary
1175 * fix until fsck has been changed to do the update.
1176 */
1177 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1178 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1179 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1180 } /* XXX */
1181
5fd012e0
MD
1182 /*
1183 * return a VX locked and refd vnode (VX == same as normal vget()
1184 * vnode so we are ok)
1185 */
984263bc
MD
1186 *vpp = vp;
1187 return (0);
1188}
1189
1190/*
1191 * File handle to vnode
1192 *
1193 * Have to be really careful about stale file handles:
1194 * - check that the inode number is valid
1195 * - call ffs_vget() to get the locked inode
1196 * - check for an unallocated inode (i_mode == 0)
1197 * - check that the given client host has export rights and return
1198 * those rights via. exflagsp and credanonp
1199 */
1200int
0973c589 1201ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
984263bc 1202{
3ff2135f 1203 struct ufid *ufhp;
984263bc
MD
1204 struct fs *fs;
1205
1206 ufhp = (struct ufid *)fhp;
1207 fs = VFSTOUFS(mp)->um_fs;
1208 if (ufhp->ufid_ino < ROOTINO ||
1209 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1210 return (ESTALE);
1211 return (ufs_fhtovp(mp, ufhp, vpp));
1212}
1213
1214/*
1215 * Vnode pointer to File handle
1216 */
1217/* ARGSUSED */
1218int
0973c589 1219ffs_vptofh(struct vnode *vp, struct fid *fhp)
984263bc 1220{
3ff2135f
RG
1221 struct inode *ip;
1222 struct ufid *ufhp;
984263bc
MD
1223
1224 ip = VTOI(vp);
1225 ufhp = (struct ufid *)fhp;
1226 ufhp->ufid_len = sizeof(struct ufid);
1227 ufhp->ufid_ino = ip->i_number;
1228 ufhp->ufid_gen = ip->i_gen;
1229 return (0);
1230}
1231
1232/*
1233 * Initialize the filesystem; just use ufs_init.
1234 */
1235static int
0973c589 1236ffs_init(struct vfsconf *vfsp)
984263bc 1237{
984263bc
MD
1238 softdep_initialize();
1239 return (ufs_init(vfsp));
1240}
1241
1242/*
1243 * Write a superblock and associated information back to disk.
1244 */
1245static int
0973c589 1246ffs_sbupdate(struct ufsmount *mp, int waitfor)
984263bc 1247{
3ff2135f
RG
1248 struct fs *dfs, *fs = mp->um_fs;
1249 struct buf *bp;
984263bc
MD
1250 int blks;
1251 void *space;
1252 int i, size, error, allerror = 0;
1253
1254 /*
1255 * First write back the summary information.
81b5c339
MD
1256 *
1257 * NOTE: the getblk is relative to the device vnode so bio1
1258 * contains the device block number.
984263bc
MD
1259 */
1260 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1261 space = fs->fs_csp;
1262 for (i = 0; i < blks; i += fs->fs_frag) {
1263 size = fs->fs_bsize;
1264 if (i + fs->fs_frag > blks)
1265 size = (blks - i) * fs->fs_fsize;
54078292 1266 bp = getblk(mp->um_devvp, fsbtodoff(fs, fs->fs_csaddr + i),
81b5c339 1267 size, 0, 0);
f719c866 1268 bcopy(space, bp->b_data, (uint)size);
984263bc
MD
1269 space = (char *)space + size;
1270 if (waitfor != MNT_WAIT)
1271 bawrite(bp);
1272 else if ((error = bwrite(bp)) != 0)
1273 allerror = error;
1274 }
1275 /*
1276 * Now write back the superblock itself. If any errors occurred
1277 * up to this point, then fail so that the superblock avoids
1278 * being written out as clean.
1279 */
1280 if (allerror)
1281 return (allerror);
54078292 1282 bp = getblk(mp->um_devvp, SBOFF, (int)fs->fs_sbsize, 0, 0);
984263bc
MD
1283 fs->fs_fmod = 0;
1284 fs->fs_time = time_second;
f719c866
DR
1285 bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1286 /* Restore compatibility to old filesystems. XXX */
984263bc
MD
1287 dfs = (struct fs *)bp->b_data; /* XXX */
1288 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1289 dfs->fs_nrpos = -1; /* XXX */
1290 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1291 int32_t *lp, tmp; /* XXX */
1292 /* XXX */
1293 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1294 tmp = lp[4]; /* XXX */
1295 for (i = 4; i > 0; i--) /* XXX */
1296 lp[i] = lp[i-1]; /* XXX */
1297 lp[0] = tmp; /* XXX */
1298 } /* XXX */
1299 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1300 if (waitfor != MNT_WAIT)
1301 bawrite(bp);
1302 else if ((error = bwrite(bp)) != 0)
1303 allerror = error;
1304 return (allerror);
1305}