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