1 /* $NetBSD: filecore_vfsops.c,v 1.78 2015/03/28 19:24:05 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 1994 The Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * filecore_vfsops.c 1.1 1998/6/26
32 */
33
34 /*-
35 * Copyright (c) 1998 Andrew McMurry
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * filecore_vfsops.c 1.1 1998/6/26
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: filecore_vfsops.c,v 1.78 2015/03/28 19:24:05 maxv Exp $");
70
71 #if defined(_KERNEL_OPT)
72 #include "opt_compat_netbsd.h"
73 #endif
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/namei.h>
78 #include <sys/proc.h>
79 #include <sys/vnode.h>
80 #include <miscfs/genfs/genfs.h>
81 #include <miscfs/specfs/specdev.h>
82 #include <sys/mount.h>
83 #include <sys/buf.h>
84 #include <sys/file.h>
85 #include <sys/device.h>
86 #include <sys/errno.h>
87 #include <sys/pool.h>
88 #include <sys/conf.h>
89 #include <sys/sysctl.h>
90 #include <sys/kauth.h>
91 #include <sys/module.h>
92
93 #include <fs/filecorefs/filecore.h>
94 #include <fs/filecorefs/filecore_extern.h>
95 #include <fs/filecorefs/filecore_node.h>
96 #include <fs/filecorefs/filecore_mount.h>
97
98 MODULE(MODULE_CLASS_VFS, filecore, NULL);
99
100 static struct sysctllog *filecore_sysctl_log;
101
102 extern const struct vnodeopv_desc filecore_vnodeop_opv_desc;
103
104 const struct vnodeopv_desc * const filecore_vnodeopv_descs[] = {
105 &filecore_vnodeop_opv_desc,
106 NULL,
107 };
108
109 struct vfsops filecore_vfsops = {
110 .vfs_name = MOUNT_FILECORE,
111 .vfs_min_mount_data = sizeof (struct filecore_args),
112 .vfs_mount = filecore_mount,
113 .vfs_start = filecore_start,
114 .vfs_unmount = filecore_unmount,
115 .vfs_root = filecore_root,
116 .vfs_quotactl = (void *)eopnotsupp,
117 .vfs_statvfs = filecore_statvfs,
118 .vfs_sync = filecore_sync,
119 .vfs_vget = filecore_vget,
120 .vfs_loadvnode = filecore_loadvnode,
121 .vfs_fhtovp = filecore_fhtovp,
122 .vfs_vptofh = filecore_vptofh,
123 .vfs_init = filecore_init,
124 .vfs_reinit = filecore_reinit,
125 .vfs_done = filecore_done,
126 .vfs_snapshot = (void *)eopnotsupp,
127 .vfs_extattrctl = vfs_stdextattrctl,
128 .vfs_suspendctl = (void *)eopnotsupp,
129 .vfs_renamelock_enter = genfs_renamelock_enter,
130 .vfs_renamelock_exit = genfs_renamelock_exit,
131 .vfs_fsync = (void *)eopnotsupp,
132 .vfs_opv_descs = filecore_vnodeopv_descs
133 };
134
135 static int
filecore_modcmd(modcmd_t cmd,void * arg)136 filecore_modcmd(modcmd_t cmd, void *arg)
137 {
138 int error;
139
140 switch (cmd) {
141 case MODULE_CMD_INIT:
142 error = vfs_attach(&filecore_vfsops);
143 if (error != 0)
144 break;
145 sysctl_createv(&filecore_sysctl_log, 0, NULL, NULL,
146 CTLFLAG_PERMANENT,
147 CTLTYPE_NODE, "filecore",
148 SYSCTL_DESCR("Acorn FILECORE file system"),
149 NULL, 0, NULL, 0,
150 CTL_VFS, 19, CTL_EOL);
151 /*
152 * XXX the "19" above could be dynamic, thereby eliminating
153 * one more instance of the "number to vfs" mapping problem,
154 * but "19" is the order as taken from sys/mount.h
155 */
156 break;
157 case MODULE_CMD_FINI:
158 error = vfs_detach(&filecore_vfsops);
159 if (error != 0)
160 break;
161 sysctl_teardown(&filecore_sysctl_log);
162 break;
163 default:
164 error = ENOTTY;
165 break;
166 }
167
168 return (error);
169 }
170
171 /*
172 * Called by vfs_mountroot when iso is going to be mounted as root.
173 *
174 * Name is updated by mount(8) after booting.
175 */
176
177 static int filecore_mountfs(struct vnode *devvp, struct mount *mp,
178 struct lwp *l, struct filecore_args *argp);
179
180 #if 0
181 int
182 filecore_mountroot(void)
183 {
184 struct mount *mp;
185 extern struct vnode *rootvp;
186 struct proc *p = curproc; /* XXX */
187 int error;
188 struct filecore_args args;
189
190 if (device_class(root_device) != DV_DISK)
191 return (ENODEV);
192
193 /*
194 * Get vnodes for swapdev and rootdev.
195 */
196 if (bdevvp(rootdev, &rootvp))
197 panic("filecore_mountroot: can't setup rootvp");
198
199 if ((error = vfs_rootmountalloc(MOUNT_FILECORE, "root_device", &mp)) != 0)
200 return (error);
201
202 args.flags = FILECOREMNT_ROOT;
203 if ((error = filecore_mountfs(rootvp, mp, p, &args)) != 0) {
204 vfs_unbusy(mp, false, NULL);
205 vfs_destroy(mp);
206 return (error);
207 }
208 mountlist_append(mp);
209 (void)filecore_statvfs(mp, &mp->mnt_stat, p);
210 vfs_unbusy(mp, false, NULL);
211 return (0);
212 }
213 #endif
214
215 /*
216 * VFS Operations.
217 *
218 * mount system call
219 */
220 int
filecore_mount(struct mount * mp,const char * path,void * data,size_t * data_len)221 filecore_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
222 {
223 struct lwp *l = curlwp;
224 struct vnode *devvp;
225 struct filecore_args *args = data;
226 int error;
227 struct filecore_mnt *fcmp = NULL;
228
229 if (args == NULL)
230 return EINVAL;
231 if (*data_len < sizeof *args)
232 return EINVAL;
233
234 if (mp->mnt_flag & MNT_GETARGS) {
235 fcmp = VFSTOFILECORE(mp);
236 if (fcmp == NULL)
237 return EIO;
238 args->flags = fcmp->fc_mntflags;
239 args->uid = fcmp->fc_uid;
240 args->gid = fcmp->fc_gid;
241 args->fspec = NULL;
242 *data_len = sizeof *args;
243 return 0;
244 }
245
246 if ((mp->mnt_flag & MNT_RDONLY) == 0)
247 return (EROFS);
248
249 if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
250 return EINVAL;
251
252 /*
253 * Not an update, or updating the name: look up the name
254 * and verify that it refers to a sensible block device.
255 */
256 error = namei_simple_user(args->fspec,
257 NSM_FOLLOW_NOEMULROOT, &devvp);
258 if (error != 0)
259 return (error);
260
261 if (devvp->v_type != VBLK) {
262 vrele(devvp);
263 return ENOTBLK;
264 }
265 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
266 vrele(devvp);
267 return ENXIO;
268 }
269 /*
270 * If mount by non-root, then verify that user has necessary
271 * permissions on the device.
272 */
273 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
274 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
275 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(VREAD));
276 VOP_UNLOCK(devvp);
277 if (error) {
278 vrele(devvp);
279 return (error);
280 }
281 if ((mp->mnt_flag & MNT_UPDATE) == 0)
282 error = filecore_mountfs(devvp, mp, l, args);
283 else {
284 fcmp = VFSTOFILECORE(mp);
285 if (devvp != fcmp->fc_devvp)
286 error = EINVAL; /* needs translation */
287 else
288 vrele(devvp);
289 }
290 if (error) {
291 vrele(devvp);
292 return error;
293 }
294 fcmp = VFSTOFILECORE(mp);
295 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
296 mp->mnt_op->vfs_name, mp, l);
297 }
298
299 /*
300 * Common code for mount and mountroot
301 */
302 static int
filecore_mountfs(struct vnode * devvp,struct mount * mp,struct lwp * l,struct filecore_args * argp)303 filecore_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l, struct filecore_args *argp)
304 {
305 struct filecore_mnt *fcmp = (struct filecore_mnt *)0;
306 struct buf *bp = NULL;
307 dev_t dev = devvp->v_rdev;
308 int error = EINVAL;
309 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
310 struct filecore_disc_record *fcdr;
311 unsigned map;
312 unsigned log2secsize;
313
314 if (!ronly)
315 return EROFS;
316
317 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
318 return (error);
319
320 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
321 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED);
322 VOP_UNLOCK(devvp);
323 if (error)
324 return error;
325
326 /* Read the filecore boot block to check FS validity and to find the map */
327 error = bread(devvp, FILECORE_BOOTBLOCK_BLKN,
328 FILECORE_BOOTBLOCK_SIZE, 0, &bp);
329 #ifdef FILECORE_DEBUG_BR
330 printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
331 FILECORE_BOOTBLOCK_BLKN, FILECORE_BOOTBLOCK_SIZE,
332 bp, error);
333 #endif
334 if (error != 0)
335 goto out;
336
337 KASSERT(bp != NULL);
338 if (filecore_bbchecksum(bp->b_data) != 0) {
339 error = EINVAL;
340 goto out;
341 }
342 fcdr = (struct filecore_disc_record *)((char *)(bp->b_data) +
343 FILECORE_BB_DISCREC);
344 map = ((((8 << fcdr->log2secsize) - fcdr->zone_spare)
345 * (fcdr->nzones / 2) - 8 * FILECORE_DISCREC_SIZE)
346 << fcdr->log2bpmb) >> fcdr->log2secsize;
347 log2secsize = fcdr->log2secsize;
348 #ifdef FILECORE_DEBUG_BR
349 printf("brelse(%p) vf1\n", bp);
350 #endif
351 brelse(bp, BC_AGE);
352 bp = NULL;
353
354 /* Read the bootblock in the map */
355 error = bread(devvp, map, 1 << log2secsize, 0, &bp);
356 #ifdef FILECORE_DEBUG_BR
357 printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
358 map, 1 << log2secsize, bp, error);
359 #endif
360 if (error != 0)
361 goto out;
362 fcdr = (struct filecore_disc_record *)((char *)(bp->b_data) + 4);
363 fcmp = kmem_zalloc(sizeof(*fcmp), KM_SLEEP);
364 if (fcdr->log2bpmb > fcdr->log2secsize)
365 fcmp->log2bsize = fcdr->log2bpmb;
366 else fcmp->log2bsize = fcdr->log2secsize;
367 fcmp->blksize = 1 << fcmp->log2bsize;
368 memcpy(&fcmp->drec, fcdr, sizeof(*fcdr));
369 fcmp->map = map;
370 fcmp->idspz = ((8 << fcdr->log2secsize) - fcdr->zone_spare)
371 / (fcdr->idlen + 1);
372 fcmp->mask = (1 << fcdr->idlen) - 1;
373 if (fcdr->big_flag & 1) {
374 fcmp->nblks = ((((u_int64_t)fcdr->disc_size_2) << 32)
375 + fcdr->disc_size) / fcmp->blksize;
376 } else {
377 fcmp->nblks=fcdr->disc_size / fcmp->blksize;
378 }
379
380 #ifdef FILECORE_DEBUG_BR
381 printf("brelse(%p) vf2\n", bp);
382 #endif
383 brelse(bp, BC_AGE);
384 bp = NULL;
385
386 mp->mnt_data = fcmp;
387 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
388 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FILECORE);
389 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
390 mp->mnt_stat.f_namemax = 10;
391 mp->mnt_flag |= MNT_LOCAL;
392 mp->mnt_dev_bshift = fcdr->log2secsize;
393 mp->mnt_fs_bshift = fcmp->log2bsize;
394
395 fcmp->fc_mountp = mp;
396 fcmp->fc_dev = dev;
397 fcmp->fc_devvp = devvp;
398 fcmp->fc_mntflags = argp->flags;
399 if (argp->flags & FILECOREMNT_USEUID) {
400 fcmp->fc_uid = kauth_cred_getuid(l->l_cred);
401 fcmp->fc_gid = kauth_cred_getgid(l->l_cred);
402 } else {
403 fcmp->fc_uid = argp->uid;
404 fcmp->fc_gid = argp->gid;
405 }
406
407 return 0;
408 out:
409 if (bp) {
410 #ifdef FILECORE_DEBUG_BR
411 printf("brelse(%p) vf3\n", bp);
412 #endif
413 brelse(bp, 0);
414 }
415 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
416 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED);
417 VOP_UNLOCK(devvp);
418 return error;
419 }
420
421 /*
422 * Make a filesystem operational.
423 * Nothing to do at the moment.
424 */
425 /* ARGSUSED */
426 int
filecore_start(struct mount * mp,int flags)427 filecore_start(struct mount *mp, int flags)
428 {
429 return 0;
430 }
431
432 /*
433 * unmount system call
434 */
435 int
filecore_unmount(struct mount * mp,int mntflags)436 filecore_unmount(struct mount *mp, int mntflags)
437 {
438 struct filecore_mnt *fcmp;
439 int error, flags = 0;
440
441 if (mntflags & MNT_FORCE)
442 flags |= FORCECLOSE;
443 if ((error = vflush(mp, NULLVP, flags)) != 0)
444 return (error);
445
446 fcmp = VFSTOFILECORE(mp);
447
448 if (fcmp->fc_devvp->v_type != VBAD)
449 spec_node_setmountedfs(fcmp->fc_devvp, NULL);
450 vn_lock(fcmp->fc_devvp, LK_EXCLUSIVE | LK_RETRY);
451 error = VOP_CLOSE(fcmp->fc_devvp, FREAD, NOCRED);
452 vput(fcmp->fc_devvp);
453 kmem_free(fcmp, sizeof(*fcmp));
454 mp->mnt_data = NULL;
455 mp->mnt_flag &= ~MNT_LOCAL;
456 return (error);
457 }
458
459 /*
460 * Return root of a filesystem
461 */
462 int
filecore_root(struct mount * mp,struct vnode ** vpp)463 filecore_root(struct mount *mp, struct vnode **vpp)
464 {
465 struct vnode *nvp;
466 int error;
467
468 if ((error = VFS_VGET(mp, FILECORE_ROOTINO, &nvp)) != 0)
469 return (error);
470 *vpp = nvp;
471 return (0);
472 }
473
474 /*
475 * Get file system statistics.
476 */
477 int
filecore_statvfs(struct mount * mp,struct statvfs * sbp)478 filecore_statvfs(struct mount *mp, struct statvfs *sbp)
479 {
480 struct filecore_mnt *fcmp = VFSTOFILECORE(mp);
481
482 sbp->f_bsize = fcmp->blksize;
483 sbp->f_frsize = sbp->f_bsize; /* XXX */
484 sbp->f_iosize = sbp->f_bsize; /* XXX */
485 sbp->f_blocks = fcmp->nblks;
486 sbp->f_bfree = 0; /* total free blocks */
487 sbp->f_bavail = 0; /* blocks free for non superuser */
488 sbp->f_bresvd = 0; /* reserved blocks */
489 sbp->f_files = 0; /* total files */
490 sbp->f_ffree = 0; /* free file nodes for non superuser */
491 sbp->f_favail = 0; /* free file nodes */
492 sbp->f_fresvd = 0; /* reserved file nodes */
493 copy_statvfs_info(sbp, mp);
494 return 0;
495 }
496
497 /* ARGSUSED */
498 int
filecore_sync(struct mount * mp,int waitfor,kauth_cred_t cred)499 filecore_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
500 {
501 return (0);
502 }
503
504 /*
505 * File handle to vnode
506 *
507 * Have to be really careful about stale file handles:
508 * - check that the inode number is in range
509 * - call iget() to get the locked inode
510 * - check for an unallocated inode (i_mode == 0)
511 * - check that the generation number matches
512 */
513
514 struct ifid {
515 ushort ifid_len;
516 ushort ifid_pad;
517 u_int32_t ifid_ino;
518 };
519
520 /* ARGSUSED */
521 int
filecore_fhtovp(struct mount * mp,struct fid * fhp,struct vnode ** vpp)522 filecore_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
523 {
524 struct ifid ifh;
525 struct vnode *nvp;
526 struct filecore_node *ip;
527 int error;
528
529 if (fhp->fid_len != sizeof(struct ifid))
530 return EINVAL;
531
532 memcpy(&ifh, fhp, sizeof(ifh));
533 if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
534 *vpp = NULLVP;
535 return (error);
536 }
537 ip = VTOI(nvp);
538 if (filecore_staleinode(ip)) {
539 vput(nvp);
540 *vpp = NULLVP;
541 return (ESTALE);
542 }
543 *vpp = nvp;
544 return (0);
545 }
546
547 /* This looks complicated. Look at other vgets as well as the iso9660 one.
548 *
549 * The filecore inode number is made up of 1 byte directory entry index and
550 * 3 bytes of the internal disc address of the directory. On a read-only
551 * filesystem this is unique. For a read-write version we may not be able to
552 * do vget, see msdosfs.
553 */
554
555 int
filecore_vget(struct mount * mp,ino_t ino,struct vnode ** vpp)556 filecore_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
557 {
558 int error;
559
560 error = vcache_get(mp, &ino, sizeof(ino), vpp);
561 if (error)
562 return error;
563 error = vn_lock(*vpp, LK_EXCLUSIVE);
564 if (error) {
565 vrele(*vpp);
566 *vpp = NULL;
567 return error;
568 }
569 return 0;
570 }
571
572 /*
573 * Vnode pointer to File handle
574 */
575 /* ARGSUSED */
576 int
filecore_vptofh(struct vnode * vp,struct fid * fhp,size_t * fh_size)577 filecore_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
578 {
579 struct filecore_node *ip = VTOI(vp);
580 struct ifid ifh;
581
582 if (*fh_size < sizeof(struct ifid)) {
583 *fh_size = sizeof(struct ifid);
584 return E2BIG;
585 }
586 memset(&ifh, 0, sizeof(ifh));
587 ifh.ifid_len = sizeof(struct ifid);
588 ifh.ifid_ino = ip->i_number;
589 memcpy(fhp, &ifh, sizeof(ifh));
590 return 0;
591 }
592