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