lfs_vfsops.c revision 1.3 1 /* $NetBSD: lfs_vfsops.c,v 1.3 1994/12/14 13:03:49 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)lfs_vfsops.c 8.10 (Berkeley) 11/21/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/proc.h>
42 #include <sys/kernel.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/buf.h>
46 #include <sys/mbuf.h>
47 #include <sys/file.h>
48 #include <sys/disklabel.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/malloc.h>
52 #include <sys/socket.h>
53
54 #include <miscfs/specfs/specdev.h>
55
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60
61 #include <ufs/lfs/lfs.h>
62 #include <ufs/lfs/lfs_extern.h>
63
64 int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
65
66 struct vfsops lfs_vfsops = {
67 MOUNT_LFS,
68 lfs_mount,
69 ufs_start,
70 lfs_unmount,
71 ufs_root,
72 ufs_quotactl,
73 lfs_statfs,
74 lfs_sync,
75 lfs_vget,
76 lfs_fhtovp,
77 lfs_vptofh,
78 lfs_init,
79 };
80
81 int
82 lfs_mountroot()
83 {
84 panic("lfs_mountroot"); /* XXX -- implement */
85 }
86
87 /*
88 * VFS Operations.
89 *
90 * mount system call
91 */
92 lfs_mount(mp, path, data, ndp, p)
93 register struct mount *mp;
94 char *path;
95 caddr_t data;
96 struct nameidata *ndp;
97 struct proc *p;
98 {
99 struct vnode *devvp;
100 struct ufs_args args;
101 struct ufsmount *ump;
102 register struct lfs *fs; /* LFS */
103 u_int size;
104 int error;
105 mode_t accessmode;
106
107 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
108 return (error);
109
110 /* Until LFS can do NFS right. XXX */
111 if (args.export.ex_flags & MNT_EXPORTED)
112 return (EINVAL);
113
114 /*
115 * If updating, check whether changing from read-only to
116 * read/write; if there is no device name, that's all we do.
117 */
118 if (mp->mnt_flag & MNT_UPDATE) {
119 ump = VFSTOUFS(mp);
120 if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
121 /*
122 * If upgrade to read-write by non-root, then verify
123 * that user has necessary permissions on the device.
124 */
125 if (p->p_ucred->cr_uid != 0) {
126 VOP_LOCK(ump->um_devvp);
127 if (error = VOP_ACCESS(ump->um_devvp,
128 VREAD | VWRITE, p->p_ucred, p)) {
129 VOP_UNLOCK(ump->um_devvp);
130 return (error);
131 }
132 VOP_UNLOCK(ump->um_devvp);
133 }
134 fs->lfs_ronly = 0;
135 }
136 if (args.fspec == 0) {
137 /*
138 * Process export requests.
139 */
140 return (vfs_export(mp, &ump->um_export, &args.export));
141 }
142 }
143 /*
144 * Not an update, or updating the name: look up the name
145 * and verify that it refers to a sensible block device.
146 */
147 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
148 if (error = namei(ndp))
149 return (error);
150 devvp = ndp->ni_vp;
151 if (devvp->v_type != VBLK) {
152 vrele(devvp);
153 return (ENOTBLK);
154 }
155 if (major(devvp->v_rdev) >= nblkdev) {
156 vrele(devvp);
157 return (ENXIO);
158 }
159 /*
160 * If mount by non-root, then verify that user has necessary
161 * permissions on the device.
162 */
163 if (p->p_ucred->cr_uid != 0) {
164 accessmode = VREAD;
165 if ((mp->mnt_flag & MNT_RDONLY) == 0)
166 accessmode |= VWRITE;
167 VOP_LOCK(devvp);
168 if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) {
169 vput(devvp);
170 return (error);
171 }
172 VOP_UNLOCK(devvp);
173 }
174 if ((mp->mnt_flag & MNT_UPDATE) == 0)
175 error = lfs_mountfs(devvp, mp, p); /* LFS */
176 else {
177 if (devvp != ump->um_devvp)
178 error = EINVAL; /* needs translation */
179 else
180 vrele(devvp);
181 }
182 if (error) {
183 vrele(devvp);
184 return (error);
185 }
186 ump = VFSTOUFS(mp);
187 fs = ump->um_lfs; /* LFS */
188 #ifdef NOTLFS /* LFS */
189 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
190 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
191 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
192 MNAMELEN);
193 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
194 &size);
195 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
196 (void) ufs_statfs(mp, &mp->mnt_stat, p);
197 #else
198 (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
199 bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
200 bcopy((caddr_t)fs->lfs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
201 MNAMELEN);
202 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
203 &size);
204 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
205 (void) lfs_statfs(mp, &mp->mnt_stat, p);
206 #endif
207 return (0);
208 }
209
210 /*
211 * Common code for mount and mountroot
212 * LFS specific
213 */
214 int
215 lfs_mountfs(devvp, mp, p)
216 register struct vnode *devvp;
217 struct mount *mp;
218 struct proc *p;
219 {
220 extern struct vnode *rootvp;
221 register struct lfs *fs;
222 register struct ufsmount *ump;
223 struct vnode *vp;
224 struct buf *bp;
225 struct partinfo dpart;
226 dev_t dev;
227 int error, i, ronly, size;
228 struct ucred *cred;
229
230 cred = p ? p->p_ucred : NOCRED;
231 /*
232 * Disallow multiple mounts of the same device.
233 * Disallow mounting of a device that is currently in use
234 * (except for root, which might share swap device for miniroot).
235 * Flush out any old buffers remaining from a previous use.
236 */
237 if (error = vfs_mountedon(devvp))
238 return (error);
239 if (vcount(devvp) > 1 && devvp != rootvp)
240 return (EBUSY);
241 if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0))
242 return (error);
243
244 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
245 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
246 return (error);
247
248 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
249 size = DEV_BSIZE;
250 else {
251 size = dpart.disklab->d_secsize;
252 #ifdef NEVER_USED
253 dpart.part->p_fstype = FS_LFS;
254 dpart.part->p_fsize = fs->lfs_fsize; /* frag size */
255 dpart.part->p_frag = fs->lfs_frag; /* frags per block */
256 dpart.part->p_cpg = fs->lfs_segshift; /* segment shift */
257 #endif
258 }
259
260 /* Don't free random space on error. */
261 bp = NULL;
262 ump = NULL;
263
264 /* Read in the superblock. */
265 if (error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp))
266 goto out;
267 fs = (struct lfs *)bp->b_data;
268
269 /* Check the basics. */
270 if (fs->lfs_magic != LFS_MAGIC || fs->lfs_bsize > MAXBSIZE ||
271 fs->lfs_bsize < sizeof(struct lfs)) {
272 error = EINVAL; /* XXX needs translation */
273 goto out;
274 }
275
276 /* Allocate the mount structure, copy the superblock into it. */
277 ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
278 fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
279 bcopy(bp->b_data, fs, sizeof(struct lfs));
280 if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */
281 bp->b_flags |= B_INVAL;
282 brelse(bp);
283 bp = NULL;
284
285 /* Set up the I/O information */
286 fs->lfs_iocount = 0;
287
288 /* Set up the ifile and lock aflags */
289 fs->lfs_doifile = 0;
290 fs->lfs_writer = 0;
291 fs->lfs_dirops = 0;
292 fs->lfs_seglock = 0;
293
294 /* Set the file system readonly/modify bits. */
295 fs->lfs_ronly = ronly;
296 if (ronly == 0)
297 fs->lfs_fmod = 1;
298
299 /* Initialize the mount structure. */
300 dev = devvp->v_rdev;
301 mp->mnt_data = (qaddr_t)ump;
302 mp->mnt_stat.f_fsid.val[0] = (long)dev;
303 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
304 mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
305 mp->mnt_flag |= MNT_LOCAL;
306 ump->um_mountp = mp;
307 ump->um_dev = dev;
308 ump->um_devvp = devvp;
309 ump->um_bptrtodb = 0;
310 ump->um_seqinc = 1 << fs->lfs_fsbtodb;
311 ump->um_nindir = fs->lfs_nindir;
312 for (i = 0; i < MAXQUOTAS; i++)
313 ump->um_quotas[i] = NULLVP;
314 devvp->v_specflags |= SI_MOUNTEDON;
315
316 /*
317 * We use the ifile vnode for almost every operation. Instead of
318 * retrieving it from the hash table each time we retrieve it here,
319 * artificially increment the reference count and keep a pointer
320 * to it in the incore copy of the superblock.
321 */
322 if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp))
323 goto out;
324 fs->lfs_ivnode = vp;
325 VREF(vp);
326 vput(vp);
327
328 return (0);
329 out:
330 if (bp)
331 brelse(bp);
332 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
333 if (ump) {
334 free(ump->um_lfs, M_UFSMNT);
335 free(ump, M_UFSMNT);
336 mp->mnt_data = (qaddr_t)0;
337 }
338 return (error);
339 }
340
341 /*
342 * unmount system call
343 */
344 lfs_unmount(mp, mntflags, p)
345 struct mount *mp;
346 int mntflags;
347 struct proc *p;
348 {
349 extern int doforce;
350 register struct ufsmount *ump;
351 register struct lfs *fs;
352 int i, error, flags, ronly;
353
354 flags = 0;
355 if (mntflags & MNT_FORCE) {
356 if (!doforce || (mp->mnt_flag & MNT_ROOTFS))
357 return (EINVAL);
358 flags |= FORCECLOSE;
359 }
360
361 ump = VFSTOUFS(mp);
362 fs = ump->um_lfs;
363 #ifdef QUOTA
364 if (mp->mnt_flag & MNT_QUOTA) {
365 if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags))
366 return (error);
367 for (i = 0; i < MAXQUOTAS; i++) {
368 if (ump->um_quotas[i] == NULLVP)
369 continue;
370 quotaoff(p, mp, i);
371 }
372 /*
373 * Here we fall through to vflush again to ensure
374 * that we have gotten rid of all the system vnodes.
375 */
376 }
377 #endif
378 if (error = vflush(mp, fs->lfs_ivnode, flags))
379 return (error);
380 fs->lfs_clean = 1;
381 if (error = VFS_SYNC(mp, 1, p->p_ucred, p))
382 return (error);
383 if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
384 panic("lfs_unmount: still dirty blocks on ifile vnode\n");
385 vrele(fs->lfs_ivnode);
386 vgone(fs->lfs_ivnode);
387
388 ronly = !fs->lfs_ronly;
389 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
390 error = VOP_CLOSE(ump->um_devvp,
391 ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
392 vrele(ump->um_devvp);
393 free(fs, M_UFSMNT);
394 free(ump, M_UFSMNT);
395 mp->mnt_data = (qaddr_t)0;
396 mp->mnt_flag &= ~MNT_LOCAL;
397 return (error);
398 }
399
400 /*
401 * Get file system statistics.
402 */
403 lfs_statfs(mp, sbp, p)
404 struct mount *mp;
405 register struct statfs *sbp;
406 struct proc *p;
407 {
408 register struct lfs *fs;
409 register struct ufsmount *ump;
410
411 ump = VFSTOUFS(mp);
412 fs = ump->um_lfs;
413 if (fs->lfs_magic != LFS_MAGIC)
414 panic("lfs_statfs: magic");
415 sbp->f_type = 0;
416 sbp->f_bsize = fs->lfs_bsize;
417 sbp->f_iosize = fs->lfs_bsize;
418 sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
419 sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
420 sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) -
421 (fs->lfs_dsize - fs->lfs_bfree);
422 sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
423 sbp->f_files = fs->lfs_nfiles;
424 sbp->f_ffree = sbp->f_bfree * INOPB(fs);
425 if (sbp != &mp->mnt_stat) {
426 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
427 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
428 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
429 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
430 }
431 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
432 sbp->f_fstypename[MFSNAMELEN] = '\0';
433 return (0);
434 }
435
436 /*
437 * Go through the disk queues to initiate sandbagged IO;
438 * go through the inodes to write those that have been modified;
439 * initiate the writing of the super block if it has been modified.
440 *
441 * Note: we are always called with the filesystem marked `MPBUSY'.
442 */
443 lfs_sync(mp, waitfor, cred, p)
444 struct mount *mp;
445 int waitfor;
446 struct ucred *cred;
447 struct proc *p;
448 {
449 int error;
450
451 /* All syncs must be checkpoints until roll-forward is implemented. */
452 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
453 #ifdef QUOTA
454 qsync(mp);
455 #endif
456 return (error);
457 }
458
459 /*
460 * Look up an LFS dinode number to find its incore vnode. If not already
461 * in core, read it in from the specified device. Return the inode locked.
462 * Detection and handling of mount points must be done by the calling routine.
463 */
464 int
465 lfs_vget(mp, ino, vpp)
466 struct mount *mp;
467 ino_t ino;
468 struct vnode **vpp;
469 {
470 register struct lfs *fs;
471 register struct inode *ip;
472 struct buf *bp;
473 struct ifile *ifp;
474 struct vnode *vp;
475 struct ufsmount *ump;
476 daddr_t daddr;
477 dev_t dev;
478 int error;
479
480 ump = VFSTOUFS(mp);
481 dev = ump->um_dev;
482 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
483 return (0);
484
485 /* Translate the inode number to a disk address. */
486 fs = ump->um_lfs;
487 if (ino == LFS_IFILE_INUM)
488 daddr = fs->lfs_idaddr;
489 else {
490 LFS_IENTRY(ifp, fs, ino, bp);
491 daddr = ifp->if_daddr;
492 brelse(bp);
493 if (daddr == LFS_UNUSED_DADDR)
494 return (ENOENT);
495 }
496
497 /* Allocate new vnode/inode. */
498 if (error = lfs_vcreate(mp, ino, &vp)) {
499 *vpp = NULL;
500 return (error);
501 }
502
503 /*
504 * Put it onto its hash chain and lock it so that other requests for
505 * this inode will block if they arrive while we are sleeping waiting
506 * for old data structures to be purged or for the contents of the
507 * disk portion of this inode to be read.
508 */
509 ip = VTOI(vp);
510 ufs_ihashins(ip);
511
512 /*
513 * XXX
514 * This may not need to be here, logically it should go down with
515 * the i_devvp initialization.
516 * Ask Kirk.
517 */
518 ip->i_lfs = ump->um_lfs;
519
520 /* Read in the disk contents for the inode, copy into the inode. */
521 if (error =
522 bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
523 /*
524 * The inode does not contain anything useful, so it would
525 * be misleading to leave it on its hash chain. With mode
526 * still zero, it will be unlinked and returned to the free
527 * list by vput().
528 */
529 vput(vp);
530 brelse(bp);
531 *vpp = NULL;
532 return (error);
533 }
534 ip->i_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
535 brelse(bp);
536
537 /*
538 * Initialize the vnode from the inode, check for aliases. In all
539 * cases re-init ip, the underlying vnode/inode may have changed.
540 */
541 if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
542 vput(vp);
543 *vpp = NULL;
544 return (error);
545 }
546 /*
547 * Finish inode initialization now that aliasing has been resolved.
548 */
549 ip->i_devvp = ump->um_devvp;
550 VREF(ip->i_devvp);
551 *vpp = vp;
552 return (0);
553 }
554
555 /*
556 * File handle to vnode
557 *
558 * Have to be really careful about stale file handles:
559 * - check that the inode number is valid
560 * - call lfs_vget() to get the locked inode
561 * - check for an unallocated inode (i_mode == 0)
562 * - check that the given client host has export rights and return
563 * those rights via. exflagsp and credanonp
564 *
565 * XXX
566 * use ifile to see if inode is allocated instead of reading off disk
567 * what is the relationship between my generational number and the NFS
568 * generational number.
569 */
570 int
571 lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
572 register struct mount *mp;
573 struct fid *fhp;
574 struct mbuf *nam;
575 struct vnode **vpp;
576 int *exflagsp;
577 struct ucred **credanonp;
578 {
579 register struct ufid *ufhp;
580
581 ufhp = (struct ufid *)fhp;
582 if (ufhp->ufid_ino < ROOTINO)
583 return (ESTALE);
584 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
585 }
586
587 /*
588 * Vnode pointer to File handle
589 */
590 /* ARGSUSED */
591 lfs_vptofh(vp, fhp)
592 struct vnode *vp;
593 struct fid *fhp;
594 {
595 register struct inode *ip;
596 register struct ufid *ufhp;
597
598 ip = VTOI(vp);
599 ufhp = (struct ufid *)fhp;
600 ufhp->ufid_len = sizeof(struct ufid);
601 ufhp->ufid_ino = ip->i_number;
602 ufhp->ufid_gen = ip->i_gen;
603 return (0);
604 }
605