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