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