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