forgot to check this in...
[unix-history] / usr / src / sys / ufs / lfs / lfs_vfsops.c
CommitLineData
da7c5cc6 1/*
52ff95c2 2 * Copyright (c) 1989, 1991, 1993, 1994
ad0f93d2 3 * The Regents of the University of California. All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
7188ac27 6 *
06ce3734 7 * @(#)lfs_vfsops.c 8.12 (Berkeley) %G%
da7c5cc6 8 */
71e4e98b 9
c7e94c3a
KB
10#include <sys/param.h>
11#include <sys/systm.h>
12#include <sys/namei.h>
13#include <sys/proc.h>
14#include <sys/kernel.h>
15#include <sys/vnode.h>
c7e94c3a
KB
16#include <sys/mount.h>
17#include <sys/buf.h>
3a0a9bdc 18#include <sys/mbuf.h>
c7e94c3a
KB
19#include <sys/file.h>
20#include <sys/disklabel.h>
21#include <sys/ioctl.h>
22#include <sys/errno.h>
23#include <sys/malloc.h>
3a0a9bdc 24#include <sys/socket.h>
cff1d696
KM
25
26#include <miscfs/specfs/specdev.h>
609e7cfa
MK
27#include "ioctl.h"
28#include "disklabel.h"
29#include "stat.h"
71e4e98b 30
d9f5f868
KB
31#include <ufs/ufs/quota.h>
32#include <ufs/ufs/inode.h>
33#include <ufs/ufs/ufsmount.h>
34#include <ufs/ufs/ufs_extern.h>
c6f5111d 35
d9f5f868
KB
36#include <ufs/lfs/lfs.h>
37#include <ufs/lfs/lfs_extern.h>
275ca4f0 38
79a0d4bf 39int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
0b4d6502
KB
40
41struct vfsops lfs_vfsops = {
42 lfs_mount,
5bf9d21f 43 ufs_start,
0b4d6502 44 lfs_unmount,
52ff95c2 45 ufs_root,
8dc876c1 46 ufs_quotactl,
0b4d6502
KB
47 lfs_statfs,
48 lfs_sync,
f7802306 49 lfs_vget,
b16ed5f4
KM
50 lfs_fhtovp,
51 lfs_vptofh,
c7e94c3a 52 lfs_init,
06ce3734 53 lfs_sysctl,
7188ac27
KM
54};
55
c7e94c3a 56int
0b4d6502 57lfs_mountroot()
71e4e98b 58{
c7e94c3a 59 panic("lfs_mountroot"); /* XXX -- implement */
7188ac27
KM
60}
61
62/*
63 * VFS Operations.
64 *
65 * mount system call
66 */
0b4d6502 67lfs_mount(mp, path, data, ndp, p)
d45de50d 68 register struct mount *mp;
7188ac27
KM
69 char *path;
70 caddr_t data;
71 struct nameidata *ndp;
0eb6f54a 72 struct proc *p;
7188ac27
KM
73{
74 struct vnode *devvp;
75 struct ufs_args args;
76 struct ufsmount *ump;
d9f5f868 77 register struct lfs *fs; /* LFS */
7188ac27
KM
78 u_int size;
79 int error;
38047d55 80 mode_t accessmode;
7188ac27
KM
81
82 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
83 return (error);
c7e94c3a
KB
84
85 /* Until LFS can do NFS right. XXX */
ea7e18a7 86 if (args.export.ex_flags & MNT_EXPORTED)
c7e94c3a 87 return (EINVAL);
ea7e18a7 88
190244fb
MK
89 /*
90 * If updating, check whether changing from read-only to
91 * read/write; if there is no device name, that's all we do.
92 */
93 if (mp->mnt_flag & MNT_UPDATE) {
d48157d5 94 ump = VFSTOUFS(mp);
38047d55
KM
95 if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
96 /*
97 * If upgrade to read-write by non-root, then verify
98 * that user has necessary permissions on the device.
99 */
100 if (p->p_ucred->cr_uid != 0) {
101 VOP_LOCK(ump->um_devvp);
102 if (error = VOP_ACCESS(ump->um_devvp,
103 VREAD | VWRITE, p->p_ucred, p)) {
104 VOP_UNLOCK(ump->um_devvp);
105 return (error);
106 }
107 VOP_UNLOCK(ump->um_devvp);
108 }
0b4d6502 109 fs->lfs_ronly = 0;
38047d55 110 }
64583792
KM
111 if (args.fspec == 0) {
112 /*
113 * Process export requests.
114 */
ea7e18a7 115 return (vfs_export(mp, &ump->um_export, &args.export));
64583792 116 }
190244fb
MK
117 }
118 /*
119 * Not an update, or updating the name: look up the name
120 * and verify that it refers to a sensible block device.
121 */
995e59df
KM
122 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
123 if (error = namei(ndp))
190244fb
MK
124 return (error);
125 devvp = ndp->ni_vp;
126 if (devvp->v_type != VBLK) {
127 vrele(devvp);
128 return (ENOTBLK);
129 }
130 if (major(devvp->v_rdev) >= nblkdev) {
131 vrele(devvp);
132 return (ENXIO);
133 }
38047d55
KM
134 /*
135 * If mount by non-root, then verify that user has necessary
136 * permissions on the device.
137 */
138 if (p->p_ucred->cr_uid != 0) {
139 accessmode = VREAD;
140 if ((mp->mnt_flag & MNT_RDONLY) == 0)
141 accessmode |= VWRITE;
142 VOP_LOCK(devvp);
143 if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) {
144 vput(devvp);
145 return (error);
146 }
147 VOP_UNLOCK(devvp);
148 }
190244fb 149 if ((mp->mnt_flag & MNT_UPDATE) == 0)
0b4d6502 150 error = lfs_mountfs(devvp, mp, p); /* LFS */
190244fb 151 else {
d48157d5
KM
152 if (devvp != ump->um_devvp)
153 error = EINVAL; /* needs translation */
2a7dbb09
KM
154 else
155 vrele(devvp);
d48157d5 156 }
7188ac27
KM
157 if (error) {
158 vrele(devvp);
159 return (error);
27d00e76 160 }
7188ac27 161 ump = VFSTOUFS(mp);
0b4d6502
KB
162 fs = ump->um_lfs; /* LFS */
163#ifdef NOTLFS /* LFS */
7188ac27
KM
164 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
165 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
82161bc8
KM
166 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
167 MNAMELEN);
79a0d4bf 168 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
82161bc8
KM
169 &size);
170 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
0eb6f54a 171 (void) ufs_statfs(mp, &mp->mnt_stat, p);
0b4d6502
KB
172#else
173 (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
174 bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
175 bcopy((caddr_t)fs->lfs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
176 MNAMELEN);
79a0d4bf 177 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
0b4d6502
KB
178 &size);
179 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
180 (void) lfs_statfs(mp, &mp->mnt_stat, p);
181#endif
7188ac27 182 return (0);
71e4e98b
SL
183}
184
7188ac27
KM
185/*
186 * Common code for mount and mountroot
0b4d6502 187 * LFS specific
7188ac27 188 */
79a0d4bf 189int
0b4d6502 190lfs_mountfs(devvp, mp, p)
1182ae61 191 register struct vnode *devvp;
7188ac27 192 struct mount *mp;
0eb6f54a 193 struct proc *p;
71e4e98b 194{
1c26e003 195 extern struct vnode *rootvp;
d9f5f868 196 register struct lfs *fs;
0b4d6502 197 register struct ufsmount *ump;
0b4d6502
KB
198 struct vnode *vp;
199 struct buf *bp;
200 struct partinfo dpart;
0b4d6502
KB
201 dev_t dev;
202 int error, i, ronly, size;
9f246d5e 203 struct ucred *cred;
71e4e98b 204
9f246d5e 205 cred = p ? p->p_ucred : NOCRED;
1feabdab 206 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
7188ac27 207 return (error);
0b4d6502 208
9f246d5e 209 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
609e7cfa 210 size = DEV_BSIZE;
0eb6f54a 211 else {
ec67a3ce 212 size = dpart.disklab->d_secsize;
0b4d6502
KB
213#ifdef NEVER_USED
214 dpart.part->p_fstype = FS_LFS;
215 dpart.part->p_fsize = fs->lfs_fsize; /* frag size */
216 dpart.part->p_frag = fs->lfs_frag; /* frags per block */
217 dpart.part->p_cpg = fs->lfs_segshift; /* segment shift */
218#endif
7188ac27 219 }
0b4d6502
KB
220
221 /* Don't free random space on error. */
222 bp = NULL;
223 ump = NULL;
224
225 /* Read in the superblock. */
9f246d5e 226 if (error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp))
71e4e98b 227 goto out;
8dc876c1 228 error = EINVAL; /* XXX needs translation */
1c281610
MK
229 goto out;
230 }
0b4d6502
KB
231
232 /* Allocate the mount structure, copy the superblock into it. */
8dc876c1 233 ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
0c0b26ec 234 fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
cb84e0ab 235 bcopy(bp->b_data, fs, sizeof(struct lfs));
d9f5f868 236 if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */
5d96a9ad 237 bp->b_flags |= B_INVAL;
e018935f
MK
238 brelse(bp);
239 bp = NULL;
0b4d6502 240
d5075120
KB
241 /* Set up the I/O information */
242 fs->lfs_iocount = 0;
d5075120 243
0c0b26ec 244 /* Set up the ifile and lock aflags */
3ce71481
KB
245 fs->lfs_doifile = 0;
246 fs->lfs_writer = 0;
247 fs->lfs_dirops = 0;
0c0b26ec 248 fs->lfs_seglock = 0;
3ce71481 249
0b4d6502 250 /* Set the file system readonly/modify bits. */
0b4d6502 251 fs->lfs_ronly = ronly;
71e4e98b 252 if (ronly == 0)
0b4d6502
KB
253 fs->lfs_fmod = 1;
254
255 /* Initialize the mount structure. */
256 dev = devvp->v_rdev;
82161bc8
KM
257 mp->mnt_data = (qaddr_t)ump;
258 mp->mnt_stat.f_fsid.val[0] = (long)dev;
06ce3734 259 mp->mnt_stat.f_fsid.val[1] = lfs_mount_type;
b390cbb0 260 mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
82161bc8 261 mp->mnt_flag |= MNT_LOCAL;
7188ac27
KM
262 ump->um_mountp = mp;
263 ump->um_dev = dev;
264 ump->um_devvp = devvp;
5e539b49
MS
265 ump->um_bptrtodb = 0;
266 ump->um_seqinc = 1 << fs->lfs_fsbtodb;
267 ump->um_nindir = fs->lfs_nindir;
8dc876c1
KM
268 for (i = 0; i < MAXQUOTAS; i++)
269 ump->um_quotas[i] = NULLVP;
f9c367e8 270 devvp->v_specflags |= SI_MOUNTEDON;
7188ac27 271
f9c367e8
KB
272 /*
273 * We use the ifile vnode for almost every operation. Instead of
274 * retrieving it from the hash table each time we retrieve it here,
275 * artificially increment the reference count and keep a pointer
276 * to it in the incore copy of the superblock.
277 */
f7802306 278 if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp))
0b4d6502 279 goto out;
0b4d6502 280 fs->lfs_ivnode = vp;
f9c367e8
KB
281 VREF(vp);
282 vput(vp);
0b4d6502 283
609e7cfa 284
7188ac27 285 return (0);
71e4e98b 286out:
c86c9b6e
KM
287 if (bp)
288 brelse(bp);
9f246d5e 289 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
8dc876c1 290 if (ump) {
ece51ee9 291 free(ump->um_lfs, M_UFSMNT);
d9f5f868 292 free(ump, M_UFSMNT);
82161bc8 293 mp->mnt_data = (qaddr_t)0;
27d00e76 294 }
7188ac27 295 return (error);
71e4e98b
SL
296}
297
7188ac27
KM
298/*
299 * unmount system call
300 */
0b4d6502 301lfs_unmount(mp, mntflags, p)
7188ac27 302 struct mount *mp;
8dc876c1 303 int mntflags;
0eb6f54a 304 struct proc *p;
71e4e98b 305{
d9f5f868 306 extern int doforce;
7188ac27 307 register struct ufsmount *ump;
0c0b26ec 308 register struct lfs *fs;
f7802306 309 int i, error, flags, ronly;
71e4e98b 310
f7802306 311 flags = 0;
87be6db2 312 if (mntflags & MNT_FORCE) {
eb49bed5 313 if (!doforce || (mp->mnt_flag & MNT_ROOTFS))
87be6db2 314 return (EINVAL);
8dc876c1 315 flags |= FORCECLOSE;
87be6db2 316 }
3ce71481 317
7188ac27 318 ump = VFSTOUFS(mp);
3ce71481 319 fs = ump->um_lfs;
ec67a3ce 320 return (error);
71e4e98b 321#ifdef QUOTA
82161bc8 322 if (mp->mnt_flag & MNT_QUOTA) {
3ce71481 323 if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags))
6d943995 324 return (error);
8dc876c1
KM
325 for (i = 0; i < MAXQUOTAS; i++) {
326 if (ump->um_quotas[i] == NULLVP)
327 continue;
70a360ba 328 quotaoff(p, mp, i);
8dc876c1 329 }
6d943995 330 /*
8dc876c1
KM
331 * Here we fall through to vflush again to ensure
332 * that we have gotten rid of all the system vnodes.
6d943995 333 */
8dc876c1 334 }
71e4e98b 335#endif
f7802306 336 if (error = vflush(mp, fs->lfs_ivnode, flags))
6d943995 337 return (error);
0c0b26ec 338 fs->lfs_clean = 1;
f7802306
KB
339 if (error = VFS_SYNC(mp, 1, p->p_ucred, p))
340 return (error);
eb49bed5 341 if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
f7802306 342 panic("lfs_unmount: still dirty blocks on ifile vnode\n");
4c9381d8 343 vrele(fs->lfs_ivnode);
f7802306
KB
344 vgone(fs->lfs_ivnode);
345
0b4d6502 346 ronly = !fs->lfs_ronly;
7188ac27
KM
347 * Get file system statistics.
348 */
0b4d6502 349lfs_statfs(mp, sbp, p)
7188ac27
KM
350 struct mount *mp;
351 register struct statfs *sbp;
0eb6f54a 352 struct proc *p;
7188ac27 353{
d9f5f868 354 register struct lfs *fs;
7188ac27 355 register struct ufsmount *ump;
7188ac27
KM
356
357 ump = VFSTOUFS(mp);
0b4d6502
KB
358 fs = ump->um_lfs;
359 if (fs->lfs_magic != LFS_MAGIC)
360 panic("lfs_statfs: magic");
0b4d6502 361 sbp->f_bsize = fs->lfs_bsize;
cc173077 362 sbp->f_iosize = fs->lfs_bsize;
4d3cf006 363 sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
435a74c9 364 sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
0b4d6502 365 sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) -
76353702 366 (fs->lfs_dsize - fs->lfs_bfree);
4d3cf006 367 sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
0b4d6502 368 sbp->f_files = fs->lfs_nfiles;
435a74c9 369 sbp->f_ffree = sbp->f_bfree * INOPB(fs);
82161bc8 370 if (sbp != &mp->mnt_stat) {
06ce3734 371 sbp->f_type = mp->mnt_vfc->vfc_typenum;
82161bc8 372 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
d45de50d 373 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
82161bc8 374 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
d45de50d
KM
375 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
376 }
7188ac27
KM
377 return (0);
378}
379
7188ac27
KM
380/*
381 * Go through the disk queues to initiate sandbagged IO;
382 * go through the inodes to write those that have been modified;
383 * initiate the writing of the super block if it has been modified.
8dc876c1
KM
384 *
385 * Note: we are always called with the filesystem marked `MPBUSY'.
7188ac27 386 */
f7802306 387lfs_sync(mp, waitfor, cred, p)
71e4e98b 388 struct mount *mp;
7188ac27 389 int waitfor;
f7802306
KB
390 struct ucred *cred;
391 struct proc *p;
71e4e98b 392{
275ca4f0 393 int error;
7188ac27 394
c9cbb645 395 /* All syncs must be checkpoints until roll-forward is implemented. */
d2495626 396 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
8dc876c1
KM
397#ifdef QUOTA
398 qsync(mp);
399#endif
275ca4f0 400 return (error);
7188ac27 401}
b16ed5f4 402
f7802306
KB
403/*
404 * Look up an LFS dinode number to find its incore vnode. If not already
405 * in core, read it in from the specified device. Return the inode locked.
406 * Detection and handling of mount points must be done by the calling routine.
407 */
408int
409lfs_vget(mp, ino, vpp)
410 struct mount *mp;
411 ino_t ino;
412 struct vnode **vpp;
413{
414 register struct lfs *fs;
415 register struct inode *ip;
416 struct buf *bp;
417 struct ifile *ifp;
418 struct vnode *vp;
419 struct ufsmount *ump;
89e2bb79 420 ufs_daddr_t daddr;
f7802306
KB
421 dev_t dev;
422 int error;
423
f7802306
KB
424 ump = VFSTOUFS(mp);
425 dev = ump->um_dev;
426 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
427 return (0);
428
429 /* Translate the inode number to a disk address. */
430 fs = ump->um_lfs;
431 if (ino == LFS_IFILE_INUM)
432 daddr = fs->lfs_idaddr;
433 else {
434 LFS_IENTRY(ifp, fs, ino, bp);
435 daddr = ifp->if_daddr;
436 brelse(bp);
437 if (daddr == LFS_UNUSED_DADDR)
438 return (ENOENT);
439 }
440
441 /* Allocate new vnode/inode. */
442 if (error = lfs_vcreate(mp, ino, &vp)) {
443 *vpp = NULL;
444 return (error);
445 }
446
447 /*
448 * Put it onto its hash chain and lock it so that other requests for
449 * this inode will block if they arrive while we are sleeping waiting
450 * for old data structures to be purged or for the contents of the
451 * disk portion of this inode to be read.
452 */
453 ip = VTOI(vp);
454 ufs_ihashins(ip);
455
456 /*
457 * XXX
458 * This may not need to be here, logically it should go down with
459 * the i_devvp initialization.
460 * Ask Kirk.
461 */
462 ip->i_lfs = ump->um_lfs;
463
464 /* Read in the disk contents for the inode, copy into the inode. */
465 if (error =
466 bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
467 /*
f345d060
KM
468 * The inode does not contain anything useful, so it would
469 * be misleading to leave it on its hash chain. With mode
470 * still zero, it will be unlinked and returned to the free
471 * list by vput().
f7802306 472 */
eca11e88 473 vput(vp);
f7802306
KB
474 brelse(bp);
475 *vpp = NULL;
476 return (error);
477 }
cb84e0ab 478 ip->i_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
f7802306
KB
479 brelse(bp);
480
481 /*
482 * Initialize the vnode from the inode, check for aliases. In all
483 * cases re-init ip, the underlying vnode/inode may have changed.
484 */
485 if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
eca11e88 486 vput(vp);
f7802306
KB
487 *vpp = NULL;
488 return (error);
489 }
490 /*
491 * Finish inode initialization now that aliasing has been resolved.
492 */
493 ip->i_devvp = ump->um_devvp;
494 VREF(ip->i_devvp);
495 *vpp = vp;
496 return (0);
497}
498
b16ed5f4
KM
499/*
500 * File handle to vnode
501 *
502 * Have to be really careful about stale file handles:
503 * - check that the inode number is valid
504 * - call lfs_vget() to get the locked inode
505 * - check for an unallocated inode (i_mode == 0)
9e4a3a4a
KM
506 * - check that the given client host has export rights and return
507 * those rights via. exflagsp and credanonp
b16ed5f4
KM
508 *
509 * XXX
510 * use ifile to see if inode is allocated instead of reading off disk
511 * what is the relationship between my generational number and the NFS
512 * generational number.
513 */
514int
3a0a9bdc 515lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
b16ed5f4
KM
516 register struct mount *mp;
517 struct fid *fhp;
3a0a9bdc 518 struct mbuf *nam;
b16ed5f4 519 struct vnode **vpp;
3a0a9bdc
KM
520 int *exflagsp;
521 struct ucred **credanonp;
b16ed5f4 522{
b16ed5f4 523 register struct ufid *ufhp;
b16ed5f4
KM
524
525 ufhp = (struct ufid *)fhp;
526 if (ufhp->ufid_ino < ROOTINO)
3a0a9bdc 527 return (ESTALE);
485cf2ee 528 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
b16ed5f4
KM
529}
530
531/*
532 * Vnode pointer to File handle
533 */
534/* ARGSUSED */
535lfs_vptofh(vp, fhp)
536 struct vnode *vp;
537 struct fid *fhp;
538{
539 register struct inode *ip;
540 register struct ufid *ufhp;
541
542 ip = VTOI(vp);
543 ufhp = (struct ufid *)fhp;
544 ufhp->ufid_len = sizeof(struct ufid);
545 ufhp->ufid_ino = ip->i_number;
546 ufhp->ufid_gen = ip->i_gen;
547 return (0);
548}
06ce3734
KM
549
550/*
551 * Initialize the filesystem, most work done by ufs_init.
552 */
553int lfs_mount_type;
554
555int
556lfs_init(vfsp)
557 struct vfsconf *vfsp;
558{
559
560 lfs_mount_type = vfsp->vfc_typenum;
561 return (ufs_init(vfsp));
562}