1 /* $NetBSD: filecore_vfsops.c,v 1.83 2020/03/16 21:20:10 pgoyette 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.83 2020/03/16 21:20:10 pgoyette 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 extern const struct vnodeopv_desc filecore_vnodeop_opv_desc;
101
102 const struct vnodeopv_desc * const filecore_vnodeopv_descs[] = {
103 &filecore_vnodeop_opv_desc,
104 NULL,
105 };
106
107 struct vfsops filecore_vfsops = {
108 .vfs_name = MOUNT_FILECORE,
109 .vfs_min_mount_data = sizeof (struct filecore_args),
110 .vfs_mount = filecore_mount,
111 .vfs_start = filecore_start,
112 .vfs_unmount = filecore_unmount,
113 .vfs_root = filecore_root,
114 .vfs_quotactl = (void *)eopnotsupp,
115 .vfs_statvfs = filecore_statvfs,
116 .vfs_sync = filecore_sync,
117 .vfs_vget = filecore_vget,
118 .vfs_loadvnode = filecore_loadvnode,
119 .vfs_fhtovp = filecore_fhtovp,
120 .vfs_vptofh = filecore_vptofh,
121 .vfs_init = filecore_init,
122 .vfs_reinit = filecore_reinit,
123 .vfs_done = filecore_done,
124 .vfs_snapshot = (void *)eopnotsupp,
125 .vfs_extattrctl = vfs_stdextattrctl,
126 .vfs_suspendctl = genfs_suspendctl,
127 .vfs_renamelock_enter = genfs_renamelock_enter,
128 .vfs_renamelock_exit = genfs_renamelock_exit,
129 .vfs_fsync = (void *)eopnotsupp,
130 .vfs_opv_descs = filecore_vnodeopv_descs
131 };
132
133 SYSCTL_SETUP(filecore_sysctl_setup, "filecore fs sysctl")
134 {
135
136 sysctl_createv(clog, 0, NULL, NULL,
137 CTLFLAG_PERMANENT,
138 CTLTYPE_NODE, "filecore",
139 SYSCTL_DESCR("Acorn FILECORE file system"),
140 NULL, 0, NULL, 0,
141 CTL_VFS, 19, CTL_EOL);
142 /*
143 * XXX the "19" above could be dynamic, thereby eliminating
144 * one more instance of the "number to vfs" mapping problem,
145 * but "19" is the order as taken from sys/mount.h
146 */
147 }
148
149 static int
filecore_modcmd(modcmd_t cmd,void * arg)150 filecore_modcmd(modcmd_t cmd, void *arg)
151 {
152 int error;
153
154 switch (cmd) {
155 case MODULE_CMD_INIT:
156 error = vfs_attach(&filecore_vfsops);
157 if (error != 0)
158 break;
159 break;
160 case MODULE_CMD_FINI:
161 error = vfs_detach(&filecore_vfsops);
162 if (error != 0)
163 break;
164 break;
165 default:
166 error = ENOTTY;
167 break;
168 }
169
170 return (error);
171 }
172
173 /*
174 * Called by vfs_mountroot when iso is going to be mounted as root.
175 *
176 * Name is updated by mount(8) after booting.
177 */
178
179 static int filecore_mountfs(struct vnode *devvp, struct mount *mp,
180 struct lwp *l, struct filecore_args *argp);
181
182 #if 0
183 int
184 filecore_mountroot(void)
185 {
186 struct mount *mp;
187 extern struct vnode *rootvp;
188 struct proc *p = curproc; /* XXX */
189 int error;
190 struct filecore_args args;
191
192 if (device_class(root_device) != DV_DISK)
193 return (ENODEV);
194
195 /*
196 * Get vnodes for swapdev and rootdev.
197 */
198 if (bdevvp(rootdev, &rootvp))
199 panic("filecore_mountroot: can't setup rootvp");
200
201 if ((error = vfs_rootmountalloc(MOUNT_FILECORE, "root_device", &mp)) != 0)
202 return (error);
203
204 args.flags = FILECOREMNT_ROOT;
205 if ((error = filecore_mountfs(rootvp, mp, p, &args)) != 0) {
206 vfs_unbusy(mp);
207 vfs_rele(mp);
208 return (error);
209 }
210 mountlist_append(mp);
211 (void)filecore_statvfs(mp, &mp->mnt_stat, p);
212 vfs_unbusy(mp);
213 return (0);
214 }
215 #endif
216
217 /*
218 * VFS Operations.
219 *
220 * mount system call
221 */
222 int
filecore_mount(struct mount * mp,const char * path,void * data,size_t * data_len)223 filecore_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
224 {
225 struct lwp *l = curlwp;
226 struct vnode *devvp;
227 struct filecore_args *args = data;
228 int error;
229 struct filecore_mnt *fcmp = NULL;
230
231 if (args == NULL)
232 return EINVAL;
233 if (*data_len < sizeof *args)
234 return EINVAL;
235
236 if (mp->mnt_flag & MNT_GETARGS) {
237 fcmp = VFSTOFILECORE(mp);
238 if (fcmp == NULL)
239 return EIO;
240 args->flags = fcmp->fc_mntflags;
241 args->uid = fcmp->fc_uid;
242 args->gid = fcmp->fc_gid;
243 args->fspec = NULL;
244 *data_len = sizeof *args;
245 return 0;
246 }
247
248 if ((mp->mnt_flag & MNT_RDONLY) == 0)
249 return (EROFS);
250
251 if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
252 return EINVAL;
253
254 /*
255 * Not an update, or updating the name: look up the name
256 * and verify that it refers to a sensible block device.
257 */
258 error = namei_simple_user(args->fspec,
259 NSM_FOLLOW_NOEMULROOT, &devvp);
260 if (error != 0)
261 return (error);
262
263 if (devvp->v_type != VBLK) {
264 vrele(devvp);
265 return ENOTBLK;
266 }
267 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
268 vrele(devvp);
269 return ENXIO;
270 }
271 /*
272 * If mount by non-root, then verify that user has necessary
273 * permissions on the device.
274 */
275 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
276 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
277 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(VREAD));
278 VOP_UNLOCK(devvp);
279 if (error) {
280 vrele(devvp);
281 return (error);
282 }
283 if ((mp->mnt_flag & MNT_UPDATE) == 0)
284 error = filecore_mountfs(devvp, mp, l, args);
285 else {
286 fcmp = VFSTOFILECORE(mp);
287 if (devvp != fcmp->fc_devvp)
288 error = EINVAL; /* needs translation */
289 else
290 vrele(devvp);
291 }
292 if (error) {
293 vrele(devvp);
294 return error;
295 }
296 fcmp = VFSTOFILECORE(mp);
297 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
298 mp->mnt_op->vfs_name, mp, l);
299 }
300
301 /*
302 * Common code for mount and mountroot
303 */
304 static int
filecore_mountfs(struct vnode * devvp,struct mount * mp,struct lwp * l,struct filecore_args * argp)305 filecore_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l, struct filecore_args *argp)
306 {
307 struct filecore_mnt *fcmp = (struct filecore_mnt *)0;
308 struct buf *bp = NULL;
309 dev_t dev = devvp->v_rdev;
310 int error = EINVAL;
311 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
312 struct filecore_disc_record *fcdr;
313 unsigned map;
314 unsigned log2secsize;
315
316 if (!ronly)
317 return EROFS;
318
319 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
320 return (error);
321
322 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
323 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED);
324 VOP_UNLOCK(devvp);
325 if (error)
326 return error;
327
328 /* Read the filecore boot block to check FS validity and to find the map */
329 error = bread(devvp, FILECORE_BOOTBLOCK_BLKN,
330 FILECORE_BOOTBLOCK_SIZE, 0, &bp);
331 #ifdef FILECORE_DEBUG_BR
332 printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
333 FILECORE_BOOTBLOCK_BLKN, FILECORE_BOOTBLOCK_SIZE,
334 bp, error);
335 #endif
336 if (error != 0)
337 goto out;
338
339 KASSERT(bp != NULL);
340 if (filecore_bbchecksum(bp->b_data) != 0) {
341 error = EINVAL;
342 goto out;
343 }
344 fcdr = (struct filecore_disc_record *)((char *)(bp->b_data) +
345 FILECORE_BB_DISCREC);
346 map = ((((8 << fcdr->log2secsize) - fcdr->zone_spare)
347 * (fcdr->nzones / 2) - 8 * FILECORE_DISCREC_SIZE)
348 << fcdr->log2bpmb) >> fcdr->log2secsize;
349 log2secsize = fcdr->log2secsize;
350 #ifdef FILECORE_DEBUG_BR
351 printf("brelse(%p) vf1\n", bp);
352 #endif
353 brelse(bp, BC_AGE);
354 bp = NULL;
355
356 /* Read the bootblock in the map */
357 error = bread(devvp, map, 1 << log2secsize, 0, &bp);
358 #ifdef FILECORE_DEBUG_BR
359 printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
360 map, 1 << log2secsize, bp, error);
361 #endif
362 if (error != 0)
363 goto out;
364 fcdr = (struct filecore_disc_record *)((char *)(bp->b_data) + 4);
365 fcmp = kmem_zalloc(sizeof(*fcmp), KM_SLEEP);
366 if (fcdr->log2bpmb > fcdr->log2secsize)
367 fcmp->log2bsize = fcdr->log2bpmb;
368 else fcmp->log2bsize = fcdr->log2secsize;
369 fcmp->blksize = 1 << fcmp->log2bsize;
370 memcpy(&fcmp->drec, fcdr, sizeof(*fcdr));
371 fcmp->map = map;
372 fcmp->idspz = ((8 << fcdr->log2secsize) - fcdr->zone_spare)
373 / (fcdr->idlen + 1);
374 fcmp->mask = (1 << fcdr->idlen) - 1;
375 if (fcdr->big_flag & 1) {
376 fcmp->nblks = ((((u_int64_t)fcdr->disc_size_2) << 32)
377 + fcdr->disc_size) / fcmp->blksize;
378 } else {
379 fcmp->nblks=fcdr->disc_size / fcmp->blksize;
380 }
381
382 #ifdef FILECORE_DEBUG_BR
383 printf("brelse(%p) vf2\n", bp);
384 #endif
385 brelse(bp, BC_AGE);
386 bp = NULL;
387
388 mp->mnt_data = fcmp;
389 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
390 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FILECORE);
391 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
392 mp->mnt_stat.f_namemax = 10;
393 mp->mnt_flag |= MNT_LOCAL;
394 mp->mnt_dev_bshift = fcdr->log2secsize;
395 mp->mnt_fs_bshift = fcmp->log2bsize;
396
397 fcmp->fc_mountp = mp;
398 fcmp->fc_dev = dev;
399 fcmp->fc_devvp = devvp;
400 fcmp->fc_mntflags = argp->flags;
401 if (argp->flags & FILECOREMNT_USEUID) {
402 fcmp->fc_uid = kauth_cred_getuid(l->l_cred);
403 fcmp->fc_gid = kauth_cred_getgid(l->l_cred);
404 } else {
405 fcmp->fc_uid = argp->uid;
406 fcmp->fc_gid = argp->gid;
407 }
408
409 return 0;
410 out:
411 if (bp) {
412 #ifdef FILECORE_DEBUG_BR
413 printf("brelse(%p) vf3\n", bp);
414 #endif
415 brelse(bp, 0);
416 }
417 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
418 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED);
419 VOP_UNLOCK(devvp);
420 return error;
421 }
422
423 /*
424 * Make a filesystem operational.
425 * Nothing to do at the moment.
426 */
427 /* ARGSUSED */
428 int
filecore_start(struct mount * mp,int flags)429 filecore_start(struct mount *mp, int flags)
430 {
431 return 0;
432 }
433
434 /*
435 * unmount system call
436 */
437 int
filecore_unmount(struct mount * mp,int mntflags)438 filecore_unmount(struct mount *mp, int mntflags)
439 {
440 struct filecore_mnt *fcmp;
441 int error, flags = 0;
442
443 if (mntflags & MNT_FORCE)
444 flags |= FORCECLOSE;
445 if ((error = vflush(mp, NULLVP, flags)) != 0)
446 return (error);
447
448 fcmp = VFSTOFILECORE(mp);
449
450 if (fcmp->fc_devvp->v_type != VBAD)
451 spec_node_setmountedfs(fcmp->fc_devvp, NULL);
452 vn_lock(fcmp->fc_devvp, LK_EXCLUSIVE | LK_RETRY);
453 error = VOP_CLOSE(fcmp->fc_devvp, FREAD, NOCRED);
454 vput(fcmp->fc_devvp);
455 kmem_free(fcmp, sizeof(*fcmp));
456 mp->mnt_data = NULL;
457 mp->mnt_flag &= ~MNT_LOCAL;
458 return (error);
459 }
460
461 /*
462 * Return root of a filesystem
463 */
464 int
filecore_root(struct mount * mp,int lktype,struct vnode ** vpp)465 filecore_root(struct mount *mp, int lktype, struct vnode **vpp)
466 {
467 struct vnode *nvp;
468 int error;
469
470 if ((error = VFS_VGET(mp, FILECORE_ROOTINO, lktype, &nvp)) != 0)
471 return (error);
472 *vpp = nvp;
473 return (0);
474 }
475
476 /*
477 * Get file system statistics.
478 */
479 int
filecore_statvfs(struct mount * mp,struct statvfs * sbp)480 filecore_statvfs(struct mount *mp, struct statvfs *sbp)
481 {
482 struct filecore_mnt *fcmp = VFSTOFILECORE(mp);
483
484 sbp->f_bsize = fcmp->blksize;
485 sbp->f_frsize = sbp->f_bsize; /* XXX */
486 sbp->f_iosize = sbp->f_bsize; /* XXX */
487 sbp->f_blocks = fcmp->nblks;
488 sbp->f_bfree = 0; /* total free blocks */
489 sbp->f_bavail = 0; /* blocks free for non superuser */
490 sbp->f_bresvd = 0; /* reserved blocks */
491 sbp->f_files = 0; /* total files */
492 sbp->f_ffree = 0; /* free file nodes for non superuser */
493 sbp->f_favail = 0; /* free file nodes */
494 sbp->f_fresvd = 0; /* reserved file nodes */
495 copy_statvfs_info(sbp, mp);
496 return 0;
497 }
498
499 /* ARGSUSED */
500 int
filecore_sync(struct mount * mp,int waitfor,kauth_cred_t cred)501 filecore_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
502 {
503 return (0);
504 }
505
506 /*
507 * File handle to vnode
508 *
509 * Have to be really careful about stale file handles:
510 * - check that the inode number is in range
511 * - call iget() to get the locked inode
512 * - check for an unallocated inode (i_mode == 0)
513 * - check that the generation number matches
514 */
515
516 struct ifid {
517 ushort ifid_len;
518 ushort ifid_pad;
519 u_int32_t ifid_ino;
520 };
521
522 /* ARGSUSED */
523 int
filecore_fhtovp(struct mount * mp,struct fid * fhp,int lktype,struct vnode ** vpp)524 filecore_fhtovp(struct mount *mp, struct fid *fhp, int lktype,
525 struct vnode **vpp)
526 {
527 struct ifid ifh;
528 struct vnode *nvp;
529 struct filecore_node *ip;
530 int error;
531
532 if (fhp->fid_len != sizeof(struct ifid))
533 return EINVAL;
534
535 memcpy(&ifh, fhp, sizeof(ifh));
536 if ((error = VFS_VGET(mp, ifh.ifid_ino, lktype, &nvp)) != 0) {
537 *vpp = NULLVP;
538 return (error);
539 }
540 ip = VTOI(nvp);
541 if (filecore_staleinode(ip)) {
542 vput(nvp);
543 *vpp = NULLVP;
544 return (ESTALE);
545 }
546 *vpp = nvp;
547 return (0);
548 }
549
550 /* This looks complicated. Look at other vgets as well as the iso9660 one.
551 *
552 * The filecore inode number is made up of 1 byte directory entry index and
553 * 3 bytes of the internal disc address of the directory. On a read-only
554 * filesystem this is unique. For a read-write version we may not be able to
555 * do vget, see msdosfs.
556 */
557
558 int
filecore_vget(struct mount * mp,ino_t ino,int lktype,struct vnode ** vpp)559 filecore_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
560 {
561 int error;
562
563 error = vcache_get(mp, &ino, sizeof(ino), vpp);
564 if (error)
565 return error;
566 error = vn_lock(*vpp, lktype);
567 if (error) {
568 vrele(*vpp);
569 *vpp = NULL;
570 return error;
571 }
572 return 0;
573 }
574
575 /*
576 * Vnode pointer to File handle
577 */
578 /* ARGSUSED */
579 int
filecore_vptofh(struct vnode * vp,struct fid * fhp,size_t * fh_size)580 filecore_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
581 {
582 struct filecore_node *ip = VTOI(vp);
583 struct ifid ifh;
584
585 if (*fh_size < sizeof(struct ifid)) {
586 *fh_size = sizeof(struct ifid);
587 return E2BIG;
588 }
589 memset(&ifh, 0, sizeof(ifh));
590 ifh.ifid_len = sizeof(struct ifid);
591 ifh.ifid_ino = ip->i_number;
592 memcpy(fhp, &ifh, sizeof(ifh));
593 return 0;
594 }
595