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