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