v7fs_vfsops.c revision 1.12.2.2 1 /* $NetBSD: v7fs_vfsops.c,v 1.12.2.2 2017/04/26 02:53:26 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.c,v 1.12.2.2 2017/04/26 02:53:26 pgoyette Exp $");
34 #if defined _KERNEL_OPT
35 #include "opt_v7fs.h"
36 #endif
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/pool.h>
42 #include <sys/time.h>
43 #include <sys/ucred.h>
44 #include <sys/mount.h>
45 #include <sys/disk.h>
46 #include <sys/device.h>
47 #include <sys/fcntl.h>
48 #include <sys/kmem.h>
49 #include <sys/kauth.h>
50 #include <sys/proc.h>
51
52 /* v-node */
53 #include <sys/namei.h>
54 #include <sys/vnode.h>
55 /* devsw */
56 #include <sys/conf.h>
57
58 #include "v7fs_extern.h"
59 #include "v7fs.h"
60 #include "v7fs_impl.h"
61 #include "v7fs_inode.h"
62 #include "v7fs_superblock.h"
63
64 #ifdef V7FS_VFSOPS_DEBUG
65 #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
66 #else
67 #define DPRINTF(arg...) ((void)0)
68 #endif
69
70 struct pool v7fs_node_pool;
71
72 static int v7fs_mountfs(struct vnode *, struct mount *, int);
73 static int v7fs_openfs(struct vnode *, struct mount *, struct lwp *);
74 static void v7fs_closefs(struct vnode *, struct mount *);
75 static int is_v7fs_partition(struct vnode *);
76 static enum vtype v7fs_mode_to_vtype(v7fs_mode_t mode);
77
78 int
79 v7fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
80 {
81 struct lwp *l = curlwp;
82 struct v7fs_args *args = data;
83 struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
84 struct vnode *devvp = NULL;
85 int error = 0;
86 bool update = mp->mnt_flag & MNT_UPDATE;
87 const struct bdevsw *bdev;
88
89 DPRINTF("mnt_flag=%x %s\n", mp->mnt_flag, update ? "update" : "");
90
91 if (args == NULL)
92 return EINVAL;
93 if (*data_len < sizeof(*args))
94 return EINVAL;
95
96 if (mp->mnt_flag & MNT_GETARGS) {
97 if (!v7fsmount)
98 return EIO;
99 args->fspec = NULL;
100 args->endian = v7fsmount->core->endian;
101 *data_len = sizeof(*args);
102 return 0;
103 }
104
105 DPRINTF("args->fspec=%s endian=%d\n", args->fspec, args->endian);
106 if (args->fspec == NULL) {
107 /* nothing to do. */
108 return EINVAL;
109 }
110
111 bdev = NULL;
112 if (args->fspec != NULL) {
113 /* Look up the name and verify that it's sane. */
114 error = namei_simple_user(args->fspec,
115 NSM_FOLLOW_NOEMULROOT, &devvp);
116 if (error != 0)
117 return (error);
118 DPRINTF("mount device=%lx\n", (long)devvp->v_rdev);
119
120 if (!update) {
121 /*
122 * Be sure this is a valid block device
123 */
124 if (devvp->v_type != VBLK)
125 error = ENOTBLK;
126 else if ((bdev = bdevsw_lookup_acquire(devvp->v_rdev))
127 == NULL)
128 error = ENXIO;
129 } else {
130 KDASSERT(v7fsmount);
131 /*
132 * Be sure we're still naming the same device
133 * used for our initial mount
134 */
135 if (devvp != v7fsmount->devvp) {
136 DPRINTF("devvp %p != %p rootvp=%p\n", devvp,
137 v7fsmount->devvp, rootvp);
138 if (rootvp == v7fsmount->devvp) {
139 vrele(devvp);
140 devvp = rootvp;
141 vref(devvp);
142 } else {
143 error = EINVAL;
144 }
145 }
146 }
147 }
148
149 /*
150 * If mount by non-root, then verify that user has necessary
151 * permissions on the device.
152 *
153 * Permission to update a mount is checked higher, so here we presume
154 * updating the mount is okay (for example, as far as securelevel goes)
155 * which leaves us with the normal check.
156 */
157 if (error == 0) {
158 int accessmode = VREAD;
159 if (update ?
160 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
161 (mp->mnt_flag & MNT_RDONLY) == 0)
162 accessmode |= VWRITE;
163 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
164 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
165 KAUTH_ARG(accessmode));
166 }
167
168 if (error) {
169 vrele(devvp);
170 if (bdev != NULL)
171 bdevsw_release(bdev);
172 return error;
173 }
174
175 if (!update) {
176 if ((error = v7fs_openfs(devvp, mp, l))) {
177 vrele(devvp);
178 if (bdev != NULL)
179 bdevsw_release(bdev);
180 return error;
181 }
182
183 if ((error = v7fs_mountfs(devvp, mp, args->endian))) {
184 v7fs_closefs(devvp, mp);
185 VOP_UNLOCK(devvp);
186 vrele(devvp);
187 if (bdev != NULL)
188 bdevsw_release(bdev);
189 return error;
190 }
191 VOP_UNLOCK(devvp);
192 } else if (mp->mnt_flag & MNT_RDONLY) {
193 /* XXX: r/w -> read only */
194 }
195
196 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
197 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
198 if (bdev != NULL)
199 bdevsw_release(bdev);
200 return error;
201 }
202
203 static int
204 is_v7fs_partition(struct vnode *devvp)
205 {
206 struct dkwedge_info dkw;
207 int error;
208
209 if ((error = getdiskinfo(devvp, &dkw)) != 0) {
210 DPRINTF("getdiskinfo=%d\n", error);
211 return error;
212 }
213 DPRINTF("ptype=%s size=%" PRIu64 "\n", dkw.dkw_ptype, dkw->dkw_size);
214
215 return strcmp(dkw.dkw_ptype, DKW_PTYPE_V7) == 0 ? 0 : EINVAL;
216 }
217
218 static int
219 v7fs_openfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
220 {
221 kauth_cred_t cred = l->l_cred;
222 int oflags;
223 int error;
224
225 /* Flush buffer */
226 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
227 if ((error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0)))
228 goto unlock_exit;
229
230 /* Open block device */
231 oflags = FREAD;
232 if ((mp->mnt_flag & MNT_RDONLY) == 0)
233 oflags |= FWRITE;
234
235 if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0) {
236 DPRINTF("VOP_OPEN=%d\n", error);
237 goto unlock_exit;
238 }
239
240 return 0; /* lock held */
241
242 unlock_exit:
243 VOP_UNLOCK(devvp);
244
245 return error;
246 }
247
248 static void
249 v7fs_closefs(struct vnode *devvp, struct mount *mp)
250 {
251 int oflags = FREAD;
252
253 if ((mp->mnt_flag & MNT_RDONLY) == 0)
254 oflags |= FWRITE;
255
256 VOP_CLOSE(devvp, oflags, NOCRED);
257 }
258
259 static int
260 v7fs_mountfs(struct vnode *devvp, struct mount *mp, int endian)
261 {
262 struct v7fs_mount *v7fsmount;
263 int error;
264 struct v7fs_mount_device mount;
265
266 DPRINTF("%d\n",endian);
267
268 v7fsmount = kmem_zalloc(sizeof(*v7fsmount), KM_SLEEP);
269 if (v7fsmount == NULL) {
270 return ENOMEM;
271 }
272 v7fsmount->devvp = devvp;
273 v7fsmount->mountp = mp;
274
275 mount.device.vnode = devvp;
276 mount.endian = endian;
277
278 if ((error = v7fs_io_init(&v7fsmount->core, &mount, V7FS_BSIZE))) {
279 goto err_exit;
280 }
281 struct v7fs_self *fs = v7fsmount->core;
282
283 if ((error = v7fs_superblock_load(fs))) {
284 v7fs_io_fini(fs);
285 goto err_exit;
286 }
287
288 mp->mnt_data = v7fsmount;
289 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
290 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_V7FS);
291 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
292 mp->mnt_stat.f_namemax = V7FS_NAME_MAX;
293 mp->mnt_flag |= MNT_LOCAL;
294 mp->mnt_dev_bshift = V7FS_BSHIFT;
295 mp->mnt_fs_bshift = V7FS_BSHIFT;
296
297 return 0;
298
299 err_exit:
300 kmem_free(v7fsmount, sizeof(*v7fsmount));
301 return error;
302 }
303
304 int
305 v7fs_start(struct mount *mp, int flags)
306 {
307
308 DPRINTF("\n");
309 /* Nothing to do. */
310 return 0;
311 }
312
313 int
314 v7fs_unmount(struct mount *mp, int mntflags)
315 {
316 struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
317 int error;
318
319 DPRINTF("%p\n", v7fsmount);
320
321 if ((error = vflush(mp, NULLVP,
322 mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
323 return error;
324
325 vn_lock(v7fsmount->devvp, LK_EXCLUSIVE | LK_RETRY);
326 error = VOP_CLOSE(v7fsmount->devvp, FREAD, NOCRED);
327 vput(v7fsmount->devvp);
328
329 v7fs_io_fini(v7fsmount->core);
330
331 kmem_free(v7fsmount, sizeof(*v7fsmount));
332 mp->mnt_data = NULL;
333 mp->mnt_flag &= ~MNT_LOCAL;
334
335 return 0;
336 }
337
338 int
339 v7fs_root(struct mount *mp, struct vnode **vpp)
340 {
341 struct vnode *vp;
342 int error;
343
344 DPRINTF("\n");
345 if ((error = VFS_VGET(mp, V7FS_ROOT_INODE, &vp)) != 0) {
346 DPRINTF("error=%d\n", error);
347 return error;
348 }
349 *vpp = vp;
350 DPRINTF("done.\n");
351
352 return 0;
353 }
354
355 int
356 v7fs_statvfs(struct mount *mp, struct statvfs *f)
357 {
358 struct v7fs_mount *v7fsmount = mp->mnt_data;
359 struct v7fs_self *fs = v7fsmount->core;
360
361 DPRINTF("scratch remain=%d\n", fs->scratch_remain);
362
363 v7fs_superblock_status(fs);
364
365 f->f_bsize = V7FS_BSIZE;
366 f->f_frsize = V7FS_BSIZE;
367 f->f_iosize = V7FS_BSIZE;
368 f->f_blocks = fs->stat.total_blocks;
369 f->f_bfree = fs->stat.free_blocks;
370 f->f_bavail = fs->stat.free_blocks;
371 f->f_bresvd = 0;
372 f->f_files = fs->stat.total_files;
373 f->f_ffree = fs->stat.free_inode;
374 f->f_favail = f->f_ffree;
375 f->f_fresvd = 0;
376 copy_statvfs_info(f, mp);
377
378 return 0;
379 }
380
381 static bool
382 v7fs_sync_selector(void *cl, struct vnode *vp)
383 {
384 struct v7fs_node *v7fs_node;
385
386 KASSERT(mutex_owned(vp->v_interlock));
387
388 v7fs_node = vp->v_data;
389 if (v7fs_node == NULL)
390 return false;
391 if (!v7fs_inode_allocated(&v7fs_node->inode))
392 return false;
393
394 return true;
395 }
396
397 int
398 v7fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
399 {
400 struct v7fs_mount *v7fsmount = mp->mnt_data;
401 struct v7fs_self *fs = v7fsmount->core;
402 struct vnode_iterator *marker;
403 struct vnode *vp;
404 int err, error;
405
406 DPRINTF("\n");
407
408 v7fs_superblock_writeback(fs);
409 error = 0;
410 vfs_vnode_iterator_init(mp, &marker);
411 while ((vp = vfs_vnode_iterator_next(marker,
412 v7fs_sync_selector, NULL)) != NULL) {
413 err = vn_lock(vp, LK_EXCLUSIVE);
414 if (err) {
415 vrele(vp);
416 continue;
417 }
418 err = VOP_FSYNC(vp, cred, FSYNC_WAIT, 0, 0);
419 vput(vp);
420 if (err != 0)
421 error = err;
422 }
423 vfs_vnode_iterator_destroy(marker);
424
425 return error;
426 }
427
428 static enum vtype
429 v7fs_mode_to_vtype (v7fs_mode_t mode)
430 {
431 enum vtype table[] = { VCHR, VDIR, VBLK, VREG, VLNK, VSOCK };
432
433 if ((mode & V7FS_IFMT) == V7FSBSD_IFFIFO)
434 return VFIFO;
435
436 return table[((mode >> 13) & 7) - 1];
437 }
438
439 int
440 v7fs_loadvnode(struct mount *mp, struct vnode *vp,
441 const void *key, size_t key_len, const void **new_key)
442 {
443 struct v7fs_mount *v7fsmount;
444 struct v7fs_self *fs;
445 struct v7fs_node *v7fs_node;
446 struct v7fs_inode inode;
447 v7fs_ino_t number;
448 int error;
449
450 KASSERT(key_len == sizeof(number));
451 memcpy(&number, key, key_len);
452
453 v7fsmount = mp->mnt_data;
454 fs = v7fsmount->core;
455
456 /* Lookup requested i-node */
457 if ((error = v7fs_inode_load(fs, &inode, number))) {
458 DPRINTF("v7fs_inode_load failed.\n");
459 return error;
460 }
461
462 v7fs_node = pool_get(&v7fs_node_pool, PR_WAITOK);
463 memset(v7fs_node, 0, sizeof(*v7fs_node));
464
465 vp->v_tag = VT_V7FS;
466 vp->v_data = v7fs_node;
467 v7fs_node->vnode = vp;
468 v7fs_node->v7fsmount = v7fsmount;
469 v7fs_node->inode = inode;/*structure copy */
470 v7fs_node->lockf = NULL; /* advlock */
471
472 genfs_node_init(vp, &v7fs_genfsops);
473 uvm_vnp_setsize(vp, v7fs_inode_filesize(&inode));
474
475 if (number == V7FS_ROOT_INODE) {
476 vp->v_type = VDIR;
477 vp->v_vflag |= VV_ROOT;
478 vp->v_op = v7fs_vnodeop_p;
479 } else {
480 vp->v_type = v7fs_mode_to_vtype(inode.mode);
481
482 if (vp->v_type == VBLK || vp->v_type == VCHR) {
483 dev_t rdev = inode.device;
484 vp->v_op = v7fs_specop_p;
485 spec_node_init(vp, rdev);
486 } else if (vp->v_type == VFIFO) {
487 vp->v_op = v7fs_fifoop_p;
488 } else {
489 vp->v_op = v7fs_vnodeop_p;
490 }
491 }
492
493 *new_key = &v7fs_node->inode.inode_number;
494
495 return 0;
496 }
497
498
499 int
500 v7fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
501 {
502 int error;
503 v7fs_ino_t number;
504 struct vnode *vp;
505
506 KASSERT(ino <= UINT16_MAX);
507 number = ino;
508
509 error = vcache_get(mp, &number, sizeof(number), &vp);
510 if (error)
511 return error;
512 error = vn_lock(vp, LK_EXCLUSIVE);
513 if (error) {
514 vrele(vp);
515 return error;
516 }
517
518 *vpp = vp;
519
520 return 0;
521 }
522
523 int
524 v7fs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
525 {
526
527 DPRINTF("\n");
528 /* notyet */
529 return EOPNOTSUPP;
530 }
531
532 int
533 v7fs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
534 {
535
536 DPRINTF("\n");
537 /* notyet */
538 return EOPNOTSUPP;
539 }
540
541 void
542 v7fs_init(void)
543 {
544
545 DPRINTF("\n");
546 pool_init(&v7fs_node_pool, sizeof(struct v7fs_node), 0, 0, 0,
547 "v7fs_node_pool", &pool_allocator_nointr, IPL_NONE);
548 }
549
550 void
551 v7fs_reinit(void)
552 {
553
554 /* Nothing to do. */
555 DPRINTF("\n");
556 }
557
558 void
559 v7fs_done(void)
560 {
561
562 DPRINTF("\n");
563 pool_destroy(&v7fs_node_pool);
564 }
565
566 int
567 v7fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
568 kauth_cred_t cred)
569 {
570
571 DPRINTF("\n");
572 return 0;
573 }
574
575 int
576 v7fs_mountroot(void)
577 {
578 struct mount *mp;
579 int error;
580
581 DPRINTF("");
582 /* On mountroot, devvp (rootdev) is opened by vfs_mountroot */
583 if ((error = is_v7fs_partition (rootvp)))
584 return error;
585
586 if ((error = vfs_rootmountalloc(MOUNT_V7FS, "root_device", &mp))) {
587 DPRINTF("mountalloc error=%d\n", error);
588 vrele(rootvp);
589 return error;
590 }
591
592 if ((error = v7fs_mountfs(rootvp, mp, _BYTE_ORDER))) {
593 DPRINTF("mountfs error=%d\n", error);
594 vfs_unbusy(mp);
595 vfs_rele(mp);
596 return error;
597 }
598
599 mountlist_append(mp);
600
601 vfs_unbusy(mp);
602
603 return 0;
604 }
605