v7fs_vfsops.c revision 1.11 1 /* $NetBSD: v7fs_vfsops.c,v 1.11 2014/12/29 15:28:58 hannken 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.11 2014/12/29 15:28:58 hannken 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
88 DPRINTF("mnt_flag=%x %s\n", mp->mnt_flag, update ? "update" : "");
89
90 if (args == NULL)
91 return EINVAL;
92 if (*data_len < sizeof(*args))
93 return EINVAL;
94
95 if (mp->mnt_flag & MNT_GETARGS) {
96 if (!v7fsmount)
97 return EIO;
98 args->fspec = NULL;
99 args->endian = v7fsmount->core->endian;
100 *data_len = sizeof(*args);
101 return 0;
102 }
103
104 DPRINTF("args->fspec=%s endian=%d\n", args->fspec, args->endian);
105 if (args->fspec == NULL) {
106 /* nothing to do. */
107 return EINVAL;
108 }
109
110 if (args->fspec != NULL) {
111 /* Look up the name and verify that it's sane. */
112 error = namei_simple_user(args->fspec,
113 NSM_FOLLOW_NOEMULROOT, &devvp);
114 if (error != 0)
115 return (error);
116 DPRINTF("mount device=%lx\n", (long)devvp->v_rdev);
117
118 if (!update) {
119 /*
120 * Be sure this is a valid block device
121 */
122 if (devvp->v_type != VBLK)
123 error = ENOTBLK;
124 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
125 error = ENXIO;
126 } else {
127 KDASSERT(v7fsmount);
128 /*
129 * Be sure we're still naming the same device
130 * used for our initial mount
131 */
132 if (devvp != v7fsmount->devvp) {
133 DPRINTF("devvp %p != %p rootvp=%p\n", devvp,
134 v7fsmount->devvp, rootvp);
135 if (rootvp == v7fsmount->devvp) {
136 vrele(devvp);
137 devvp = rootvp;
138 vref(devvp);
139 } else {
140 error = EINVAL;
141 }
142 }
143 }
144 }
145
146 /*
147 * If mount by non-root, then verify that user has necessary
148 * permissions on the device.
149 *
150 * Permission to update a mount is checked higher, so here we presume
151 * updating the mount is okay (for example, as far as securelevel goes)
152 * which leaves us with the normal check.
153 */
154 if (error == 0) {
155 int accessmode = VREAD;
156 if (update ?
157 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
158 (mp->mnt_flag & MNT_RDONLY) == 0)
159 accessmode |= VWRITE;
160 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
161 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
162 KAUTH_ARG(accessmode));
163 }
164
165 if (error) {
166 vrele(devvp);
167 return error;
168 }
169
170 if (!update) {
171 if ((error = v7fs_openfs(devvp, mp, l))) {
172 vrele(devvp);
173 return error;
174 }
175
176 if ((error = v7fs_mountfs(devvp, mp, args->endian))) {
177 v7fs_closefs(devvp, mp);
178 VOP_UNLOCK(devvp);
179 vrele(devvp);
180 return error;
181 }
182 VOP_UNLOCK(devvp);
183 } else if (mp->mnt_flag & MNT_RDONLY) {
184 /* XXX: r/w -> read only */
185 }
186
187 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
188 mp->mnt_op->vfs_name, mp, l);
189 }
190
191 static int
192 is_v7fs_partition(struct vnode *devvp)
193 {
194 struct dkwedge_info dkw;
195 int error;
196
197 if ((error = getdiskinfo(devvp, &dkw)) != 0) {
198 DPRINTF("getdiskinfo=%d\n", error);
199 return error;
200 }
201 DPRINTF("ptype=%s size=%" PRIu64 "\n", dkw.dkw_ptype, dkw->dkw_size);
202
203 return strcmp(dkw.dkw_ptype, DKW_PTYPE_V7) == 0 ? 0 : EINVAL;
204 }
205
206 static int
207 v7fs_openfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
208 {
209 kauth_cred_t cred = l->l_cred;
210 int oflags;
211 int error;
212
213 /* Flush buffer */
214 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
215 if ((error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0)))
216 goto unlock_exit;
217
218 /* Open block device */
219 oflags = FREAD;
220 if ((mp->mnt_flag & MNT_RDONLY) == 0)
221 oflags |= FWRITE;
222
223 if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0) {
224 DPRINTF("VOP_OPEN=%d\n", error);
225 goto unlock_exit;
226 }
227
228 return 0; /* lock held */
229
230 unlock_exit:
231 VOP_UNLOCK(devvp);
232
233 return error;
234 }
235
236 static void
237 v7fs_closefs(struct vnode *devvp, struct mount *mp)
238 {
239 int oflags = FREAD;
240
241 if ((mp->mnt_flag & MNT_RDONLY) == 0)
242 oflags |= FWRITE;
243
244 VOP_CLOSE(devvp, oflags, NOCRED);
245 }
246
247 static int
248 v7fs_mountfs(struct vnode *devvp, struct mount *mp, int endian)
249 {
250 struct v7fs_mount *v7fsmount;
251 int error;
252 struct v7fs_mount_device mount;
253
254 DPRINTF("%d\n",endian);
255
256 v7fsmount = kmem_zalloc(sizeof(*v7fsmount), KM_SLEEP);
257 if (v7fsmount == NULL) {
258 return ENOMEM;
259 }
260 v7fsmount->devvp = devvp;
261 v7fsmount->mountp = mp;
262
263 mount.device.vnode = devvp;
264 mount.endian = endian;
265
266 if ((error = v7fs_io_init(&v7fsmount->core, &mount, V7FS_BSIZE))) {
267 goto err_exit;
268 }
269 struct v7fs_self *fs = v7fsmount->core;
270
271 if ((error = v7fs_superblock_load(fs))) {
272 v7fs_io_fini(fs);
273 goto err_exit;
274 }
275
276 LIST_INIT(&v7fsmount->v7fs_node_head);
277
278 mp->mnt_data = v7fsmount;
279 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
280 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_V7FS);
281 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
282 mp->mnt_stat.f_namemax = V7FS_NAME_MAX;
283 mp->mnt_flag |= MNT_LOCAL;
284 mp->mnt_dev_bshift = V7FS_BSHIFT;
285 mp->mnt_fs_bshift = V7FS_BSHIFT;
286
287 return 0;
288
289 err_exit:
290 kmem_free(v7fsmount, sizeof(*v7fsmount));
291 return error;
292 }
293
294 int
295 v7fs_start(struct mount *mp, int flags)
296 {
297
298 DPRINTF("\n");
299 /* Nothing to do. */
300 return 0;
301 }
302
303 int
304 v7fs_unmount(struct mount *mp, int mntflags)
305 {
306 struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
307 int error;
308
309 DPRINTF("%p\n", v7fsmount);
310
311 if ((error = vflush(mp, NULLVP,
312 mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
313 return error;
314
315 vn_lock(v7fsmount->devvp, LK_EXCLUSIVE | LK_RETRY);
316 error = VOP_CLOSE(v7fsmount->devvp, FREAD, NOCRED);
317 vput(v7fsmount->devvp);
318
319 v7fs_io_fini(v7fsmount->core);
320
321 kmem_free(v7fsmount, sizeof(*v7fsmount));
322 mp->mnt_data = NULL;
323 mp->mnt_flag &= ~MNT_LOCAL;
324
325 return 0;
326 }
327
328 int
329 v7fs_root(struct mount *mp, struct vnode **vpp)
330 {
331 struct vnode *vp;
332 int error;
333
334 DPRINTF("\n");
335 if ((error = VFS_VGET(mp, V7FS_ROOT_INODE, &vp)) != 0) {
336 DPRINTF("error=%d\n", error);
337 return error;
338 }
339 *vpp = vp;
340 DPRINTF("done.\n");
341
342 return 0;
343 }
344
345 int
346 v7fs_statvfs(struct mount *mp, struct statvfs *f)
347 {
348 struct v7fs_mount *v7fsmount = mp->mnt_data;
349 struct v7fs_self *fs = v7fsmount->core;
350
351 DPRINTF("scratch remain=%d\n", fs->scratch_remain);
352
353 v7fs_superblock_status(fs);
354
355 f->f_bsize = V7FS_BSIZE;
356 f->f_frsize = V7FS_BSIZE;
357 f->f_iosize = V7FS_BSIZE;
358 f->f_blocks = fs->stat.total_blocks;
359 f->f_bfree = fs->stat.free_blocks;
360 f->f_bavail = fs->stat.free_blocks;
361 f->f_bresvd = 0;
362 f->f_files = fs->stat.total_files;
363 f->f_ffree = fs->stat.free_inode;
364 f->f_favail = f->f_ffree;
365 f->f_fresvd = 0;
366 copy_statvfs_info(f, mp);
367
368 return 0;
369 }
370
371 int
372 v7fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
373 {
374 struct v7fs_mount *v7fsmount = mp->mnt_data;
375 struct v7fs_self *fs = v7fsmount->core;
376 struct v7fs_node *v7fs_node;
377 struct v7fs_inode *inode;
378 struct vnode *v;
379 int err, error;
380 int retry_cnt;
381
382 DPRINTF("\n");
383
384 v7fs_superblock_writeback(fs);
385 for (retry_cnt = 0; retry_cnt < 2; retry_cnt++) {
386 error = 0;
387
388 mutex_enter(&mntvnode_lock);
389 for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
390 v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
391 inode = &v7fs_node->inode;
392 if (!v7fs_inode_allocated(inode)) {
393 continue;
394 }
395 v = v7fs_node->vnode;
396 mutex_enter(v->v_interlock);
397 mutex_exit(&mntvnode_lock);
398 err = vget(v, LK_EXCLUSIVE | LK_NOWAIT);
399 if (err == 0) {
400 err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0);
401 vput(v);
402 }
403 if (err != 0)
404 error = err;
405 mutex_enter(&mntvnode_lock);
406 }
407 mutex_exit(&mntvnode_lock);
408
409 if (error == 0)
410 break;
411 }
412
413 return error;
414 }
415
416 static enum vtype
417 v7fs_mode_to_vtype (v7fs_mode_t mode)
418 {
419 enum vtype table[] = { VCHR, VDIR, VBLK, VREG, VLNK, VSOCK };
420
421 if ((mode & V7FS_IFMT) == V7FSBSD_IFFIFO)
422 return VFIFO;
423
424 return table[((mode >> 13) & 7) - 1];
425 }
426
427 int
428 v7fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
429 {
430 struct v7fs_mount *v7fsmount = mp->mnt_data;
431 struct v7fs_self *fs = v7fsmount->core;
432 struct vnode *vp;
433 struct v7fs_node *v7fs_node;
434 struct v7fs_inode inode;
435 int error;
436
437 /* Lookup requested i-node */
438 if ((error = v7fs_inode_load(fs, &inode, ino))) {
439 DPRINTF("v7fs_inode_load failed.\n");
440 return error;
441 }
442
443 retry:
444 mutex_enter(&mntvnode_lock);
445 for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
446 v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
447 if (v7fs_node->inode.inode_number == ino) {
448 vp = v7fs_node->vnode;
449 mutex_enter(vp->v_interlock);
450 mutex_exit(&mntvnode_lock);
451 if (vget(vp, LK_EXCLUSIVE) == 0) {
452 *vpp = vp;
453 return 0;
454 } else {
455 DPRINTF("retry!\n");
456 goto retry;
457 }
458 }
459 }
460 mutex_exit(&mntvnode_lock);
461
462 /* Allocate v-node. */
463 if ((error = getnewvnode(VT_V7FS, mp, v7fs_vnodeop_p, NULL, &vp))) {
464 DPRINTF("getnewvnode error.\n");
465 return error;
466 }
467 /* Lock vnode here */
468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
469
470 /* Allocate i-node */
471 vp->v_data = pool_get(&v7fs_node_pool, PR_WAITOK);
472 memset(vp->v_data, 0, sizeof(*v7fs_node));
473 v7fs_node = vp->v_data;
474 mutex_enter(&mntvnode_lock);
475 LIST_INSERT_HEAD(&v7fsmount->v7fs_node_head, v7fs_node, link);
476 mutex_exit(&mntvnode_lock);
477 v7fs_node->vnode = vp;
478 v7fs_node->v7fsmount = v7fsmount;
479 v7fs_node->inode = inode;/*structure copy */
480 v7fs_node->lockf = NULL; /* advlock */
481
482 genfs_node_init(vp, &v7fs_genfsops);
483 uvm_vnp_setsize(vp, v7fs_inode_filesize(&inode));
484
485 if (ino == V7FS_ROOT_INODE) {
486 vp->v_type = VDIR;
487 vp->v_vflag |= VV_ROOT;
488 } else {
489 vp->v_type = v7fs_mode_to_vtype(inode.mode);
490
491 if (vp->v_type == VBLK || vp->v_type == VCHR) {
492 dev_t rdev = inode.device;
493 vp->v_op = v7fs_specop_p;
494 spec_node_init(vp, rdev);
495 } else if (vp->v_type == VFIFO) {
496 vp->v_op = v7fs_fifoop_p;
497 }
498 }
499
500 *vpp = vp;
501
502 return 0;
503 }
504
505
506 int
507 v7fs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
508 {
509
510 DPRINTF("\n");
511 /* notyet */
512 return EOPNOTSUPP;
513 }
514
515 int
516 v7fs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
517 {
518
519 DPRINTF("\n");
520 /* notyet */
521 return EOPNOTSUPP;
522 }
523
524 void
525 v7fs_init(void)
526 {
527
528 DPRINTF("\n");
529 pool_init(&v7fs_node_pool, sizeof(struct v7fs_node), 0, 0, 0,
530 "v7fs_node_pool", &pool_allocator_nointr, IPL_NONE);
531 }
532
533 void
534 v7fs_reinit(void)
535 {
536
537 /* Nothing to do. */
538 DPRINTF("\n");
539 }
540
541 void
542 v7fs_done(void)
543 {
544
545 DPRINTF("\n");
546 pool_destroy(&v7fs_node_pool);
547 }
548
549 int
550 v7fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
551 kauth_cred_t cred)
552 {
553
554 DPRINTF("\n");
555 return 0;
556 }
557
558 int
559 v7fs_mountroot(void)
560 {
561 struct mount *mp;
562 int error;
563
564 DPRINTF("");
565 /* On mountroot, devvp (rootdev) is opened by vfs_mountroot */
566 if ((error = is_v7fs_partition (rootvp)))
567 return error;
568
569 if ((error = vfs_rootmountalloc(MOUNT_V7FS, "root_device", &mp))) {
570 DPRINTF("mountalloc error=%d\n", error);
571 vrele(rootvp);
572 return error;
573 }
574
575 if ((error = v7fs_mountfs(rootvp, mp, _BYTE_ORDER))) {
576 DPRINTF("mountfs error=%d\n", error);
577 vfs_unbusy(mp, false, NULL);
578 vfs_destroy(mp);
579 return error;
580 }
581
582 mountlist_append(mp);
583
584 vfs_unbusy(mp, false, NULL);
585
586 return 0;
587 }
588