For now, take out directory operation locking. Get rid of buffer
[unix-history] / usr / src / sys / ufs / lfs / lfs_vfsops.c
CommitLineData
da7c5cc6 1/*
190244fb 2 * Copyright (c) 1989, 1991 The Regents of the University of California.
7188ac27 3 * All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
7188ac27 6 *
eca11e88 7 * @(#)lfs_vfsops.c 7.88 (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
0eb6f54a 189 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, 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 214#ifdef DEBUG
c7e94c3a 215 lfs_dump_super(fs);
0b4d6502
KB
216#endif
217
218 /* Allocate the mount structure, copy the superblock into it. */
8dc876c1 219 ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
0c0b26ec
KB
220 fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
221 bcopy(bp->b_un.b_addr, fs, sizeof(struct lfs));
d9f5f868 222 if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */
5d96a9ad 223 bp->b_flags |= B_INVAL;
e018935f
MK
224 brelse(bp);
225 bp = NULL;
0b4d6502 226
d5075120
KB
227 /* Set up the I/O information */
228 fs->lfs_iocount = 0;
d5075120 229
0c0b26ec 230 /* Set up the ifile and lock aflags */
3ce71481
KB
231 fs->lfs_doifile = 0;
232 fs->lfs_writer = 0;
233 fs->lfs_dirops = 0;
0c0b26ec 234 fs->lfs_seglock = 0;
3ce71481 235
0b4d6502 236 /* Set the file system readonly/modify bits. */
0b4d6502 237 fs->lfs_ronly = ronly;
71e4e98b 238 if (ronly == 0)
0b4d6502
KB
239 fs->lfs_fmod = 1;
240
241 /* Initialize the mount structure. */
242 dev = devvp->v_rdev;
82161bc8
KM
243 mp->mnt_data = (qaddr_t)ump;
244 mp->mnt_stat.f_fsid.val[0] = (long)dev;
79a0d4bf 245 mp->mnt_stat.f_fsid.val[1] = MOUNT_LFS;
82161bc8 246 mp->mnt_flag |= MNT_LOCAL;
7188ac27
KM
247 ump->um_mountp = mp;
248 ump->um_dev = dev;
249 ump->um_devvp = devvp;
5e539b49
MS
250 ump->um_bptrtodb = 0;
251 ump->um_seqinc = 1 << fs->lfs_fsbtodb;
252 ump->um_nindir = fs->lfs_nindir;
8dc876c1
KM
253 for (i = 0; i < MAXQUOTAS; i++)
254 ump->um_quotas[i] = NULLVP;
f9c367e8 255 devvp->v_specflags |= SI_MOUNTEDON;
7188ac27 256
f9c367e8
KB
257 /*
258 * We use the ifile vnode for almost every operation. Instead of
259 * retrieving it from the hash table each time we retrieve it here,
260 * artificially increment the reference count and keep a pointer
261 * to it in the incore copy of the superblock.
262 */
f7802306 263 if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp))
0b4d6502 264 goto out;
0b4d6502 265 fs->lfs_ivnode = vp;
f9c367e8
KB
266 VREF(vp);
267 vput(vp);
0b4d6502 268
609e7cfa 269
7188ac27 270 return (0);
71e4e98b 271out:
c86c9b6e
KM
272 if (bp)
273 brelse(bp);
0b4d6502 274 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
8dc876c1 275 if (ump) {
ece51ee9 276 free(ump->um_lfs, M_UFSMNT);
d9f5f868 277 free(ump, M_UFSMNT);
82161bc8 278 mp->mnt_data = (qaddr_t)0;
27d00e76 279 }
7188ac27 280 return (error);
71e4e98b
SL
281}
282
7188ac27
KM
283/*
284 * unmount system call
285 */
0b4d6502 286lfs_unmount(mp, mntflags, p)
7188ac27 287 struct mount *mp;
8dc876c1 288 int mntflags;
0eb6f54a 289 struct proc *p;
71e4e98b 290{
d9f5f868 291 extern int doforce;
7188ac27 292 register struct ufsmount *ump;
0c0b26ec 293 register struct lfs *fs;
f7802306 294 int i, error, flags, ronly;
71e4e98b 295
f7802306 296 flags = 0;
87be6db2 297 if (mntflags & MNT_FORCE) {
d85a9d1b 298 if (!doforce || mp == rootfs)
87be6db2 299 return (EINVAL);
8dc876c1 300 flags |= FORCECLOSE;
87be6db2 301 }
3ce71481 302
7188ac27 303 ump = VFSTOUFS(mp);
3ce71481 304 fs = ump->um_lfs;
ec67a3ce 305 return (error);
71e4e98b 306#ifdef QUOTA
82161bc8 307 if (mp->mnt_flag & MNT_QUOTA) {
3ce71481 308 if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags))
6d943995 309 return (error);
8dc876c1
KM
310 for (i = 0; i < MAXQUOTAS; i++) {
311 if (ump->um_quotas[i] == NULLVP)
312 continue;
70a360ba 313 quotaoff(p, mp, i);
8dc876c1 314 }
6d943995 315 /*
8dc876c1
KM
316 * Here we fall through to vflush again to ensure
317 * that we have gotten rid of all the system vnodes.
6d943995 318 */
8dc876c1 319 }
71e4e98b 320#endif
f7802306 321 if (error = vflush(mp, fs->lfs_ivnode, flags))
6d943995 322 return (error);
0c0b26ec 323 fs->lfs_clean = 1;
f7802306
KB
324 if (error = VFS_SYNC(mp, 1, p->p_ucred, p))
325 return (error);
5e539b49 326 if (fs->lfs_ivnode->v_dirtyblkhd.le_next)
f7802306 327 panic("lfs_unmount: still dirty blocks on ifile vnode\n");
4c9381d8 328 vrele(fs->lfs_ivnode);
f7802306
KB
329 vgone(fs->lfs_ivnode);
330
0b4d6502 331 ronly = !fs->lfs_ronly;
7188ac27
KM
332 * Get file system statistics.
333 */
0b4d6502 334lfs_statfs(mp, sbp, p)
7188ac27
KM
335 struct mount *mp;
336 register struct statfs *sbp;
0eb6f54a 337 struct proc *p;
7188ac27 338{
d9f5f868 339 register struct lfs *fs;
7188ac27 340 register struct ufsmount *ump;
7188ac27
KM
341
342 ump = VFSTOUFS(mp);
0b4d6502
KB
343 fs = ump->um_lfs;
344 if (fs->lfs_magic != LFS_MAGIC)
345 panic("lfs_statfs: magic");
346 sbp->f_type = MOUNT_LFS;
0b4d6502 347 sbp->f_bsize = fs->lfs_bsize;
cc173077 348 sbp->f_iosize = fs->lfs_bsize;
4d3cf006 349 sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
435a74c9 350 sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
0b4d6502 351 sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) -
76353702 352 (fs->lfs_dsize - fs->lfs_bfree);
4d3cf006 353 sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
0b4d6502 354 sbp->f_files = fs->lfs_nfiles;
435a74c9 355 sbp->f_ffree = sbp->f_bfree * INOPB(fs);
82161bc8
KM
356 if (sbp != &mp->mnt_stat) {
357 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
d45de50d 358 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
82161bc8 359 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
d45de50d
KM
360 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
361 }
7188ac27
KM
362 return (0);
363}
364
7188ac27
KM
365/*
366 * Go through the disk queues to initiate sandbagged IO;
367 * go through the inodes to write those that have been modified;
368 * initiate the writing of the super block if it has been modified.
8dc876c1
KM
369 *
370 * Note: we are always called with the filesystem marked `MPBUSY'.
7188ac27 371 */
f7802306 372lfs_sync(mp, waitfor, cred, p)
71e4e98b 373 struct mount *mp;
7188ac27 374 int waitfor;
f7802306
KB
375 struct ucred *cred;
376 struct proc *p;
71e4e98b 377{
4c9381d8 378 extern int syncprt;
275ca4f0 379 int error;
7188ac27 380
c9cbb645 381 /* All syncs must be checkpoints until roll-forward is implemented. */
79a0d4bf 382 error = lfs_segwrite(mp, 1);
8dc876c1
KM
383#ifdef QUOTA
384 qsync(mp);
385#endif
275ca4f0 386 return (error);
7188ac27 387}
b16ed5f4 388
f7802306
KB
389/*
390 * Look up an LFS dinode number to find its incore vnode. If not already
391 * in core, read it in from the specified device. Return the inode locked.
392 * Detection and handling of mount points must be done by the calling routine.
393 */
394int
395lfs_vget(mp, ino, vpp)
396 struct mount *mp;
397 ino_t ino;
398 struct vnode **vpp;
399{
400 register struct lfs *fs;
401 register struct inode *ip;
402 struct buf *bp;
403 struct ifile *ifp;
404 struct vnode *vp;
405 struct ufsmount *ump;
406 daddr_t daddr;
407 dev_t dev;
408 int error;
409
f7802306
KB
410 ump = VFSTOUFS(mp);
411 dev = ump->um_dev;
412 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
413 return (0);
414
415 /* Translate the inode number to a disk address. */
416 fs = ump->um_lfs;
417 if (ino == LFS_IFILE_INUM)
418 daddr = fs->lfs_idaddr;
419 else {
420 LFS_IENTRY(ifp, fs, ino, bp);
421 daddr = ifp->if_daddr;
422 brelse(bp);
423 if (daddr == LFS_UNUSED_DADDR)
424 return (ENOENT);
425 }
426
427 /* Allocate new vnode/inode. */
428 if (error = lfs_vcreate(mp, ino, &vp)) {
429 *vpp = NULL;
430 return (error);
431 }
432
433 /*
434 * Put it onto its hash chain and lock it so that other requests for
435 * this inode will block if they arrive while we are sleeping waiting
436 * for old data structures to be purged or for the contents of the
437 * disk portion of this inode to be read.
438 */
439 ip = VTOI(vp);
440 ufs_ihashins(ip);
441
442 /*
443 * XXX
444 * This may not need to be here, logically it should go down with
445 * the i_devvp initialization.
446 * Ask Kirk.
447 */
448 ip->i_lfs = ump->um_lfs;
449
450 /* Read in the disk contents for the inode, copy into the inode. */
451 if (error =
452 bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
453 /*
454 * The inode does not contain anything useful, so it
455 * would be misleading to leave it on its hash chain.
456 * Iput() will return it to the free list.
457 */
af9ac048 458 ufs_ihashrem(ip);
f7802306
KB
459
460 /* Unlock and discard unneeded inode. */
eca11e88 461 vput(vp);
f7802306
KB
462 brelse(bp);
463 *vpp = NULL;
464 return (error);
465 }
466 ip->i_din = *lfs_ifind(fs, ino, bp->b_un.b_dino);
467 brelse(bp);
468
469 /*
470 * Initialize the vnode from the inode, check for aliases. In all
471 * cases re-init ip, the underlying vnode/inode may have changed.
472 */
473 if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
eca11e88 474 vput(vp);
f7802306
KB
475 *vpp = NULL;
476 return (error);
477 }
478 /*
479 * Finish inode initialization now that aliasing has been resolved.
480 */
481 ip->i_devvp = ump->um_devvp;
482 VREF(ip->i_devvp);
483 *vpp = vp;
484 return (0);
485}
486
b16ed5f4
KM
487/*
488 * File handle to vnode
489 *
490 * Have to be really careful about stale file handles:
491 * - check that the inode number is valid
492 * - call lfs_vget() to get the locked inode
493 * - check for an unallocated inode (i_mode == 0)
9e4a3a4a
KM
494 * - check that the given client host has export rights and return
495 * those rights via. exflagsp and credanonp
b16ed5f4
KM
496 *
497 * XXX
498 * use ifile to see if inode is allocated instead of reading off disk
499 * what is the relationship between my generational number and the NFS
500 * generational number.
501 */
502int
3a0a9bdc 503lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
b16ed5f4
KM
504 register struct mount *mp;
505 struct fid *fhp;
3a0a9bdc 506 struct mbuf *nam;
b16ed5f4 507 struct vnode **vpp;
3a0a9bdc
KM
508 int *exflagsp;
509 struct ucred **credanonp;
b16ed5f4 510{
b16ed5f4 511 register struct ufid *ufhp;
b16ed5f4
KM
512
513 ufhp = (struct ufid *)fhp;
514 if (ufhp->ufid_ino < ROOTINO)
3a0a9bdc 515 return (ESTALE);
485cf2ee 516 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
b16ed5f4
KM
517}
518
519/*
520 * Vnode pointer to File handle
521 */
522/* ARGSUSED */
523lfs_vptofh(vp, fhp)
524 struct vnode *vp;
525 struct fid *fhp;
526{
527 register struct inode *ip;
528 register struct ufid *ufhp;
529
530 ip = VTOI(vp);
531 ufhp = (struct ufid *)fhp;
532 ufhp->ufid_len = sizeof(struct ufid);
533 ufhp->ufid_ino = ip->i_number;
534 ufhp->ufid_gen = ip->i_gen;
535 return (0);
536}