1 /* $NetBSD: sysvbfs_vfsops.c,v 1.29 2009/04/25 18:53:44 elad Exp $ */ 2 3 /*- 4 * Copyright (c) 2004 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: sysvbfs_vfsops.c,v 1.29 2009/04/25 18:53:44 elad Exp $"); 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/pool.h> 39 #include <sys/time.h> 40 #include <sys/ucred.h> 41 #include <sys/mount.h> 42 #include <sys/disklabel.h> 43 #include <sys/fcntl.h> 44 #include <sys/malloc.h> 45 #include <sys/kauth.h> 46 #include <sys/proc.h> 47 48 /* v-node */ 49 #include <sys/namei.h> 50 #include <sys/vnode.h> 51 /* devsw */ 52 #include <sys/conf.h> 53 54 #include <fs/sysvbfs/sysvbfs.h> /* external interface */ 55 #include <fs/sysvbfs/bfs.h> /* internal interface */ 56 57 #ifdef SYSVBFS_VNOPS_DEBUG 58 #define DPRINTF(fmt, args...) printf(fmt, ##args) 59 #else 60 #define DPRINTF(arg...) ((void)0) 61 #endif 62 63 MALLOC_JUSTDEFINE(M_SYSVBFS_VFS, "sysvbfs vfs", "sysvbfs vfs structures"); 64 65 struct pool sysvbfs_node_pool; 66 67 int sysvbfs_mountfs(struct vnode *, struct mount *, struct lwp *); 68 69 int 70 sysvbfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 71 { 72 struct lwp *l = curlwp; 73 struct nameidata nd; 74 struct sysvbfs_args *args = data; 75 struct sysvbfs_mount *bmp = NULL; 76 struct vnode *devvp = NULL; 77 int error; 78 bool update; 79 80 DPRINTF("%s: mnt_flag=%x\n", __func__, mp->mnt_flag); 81 82 if (*data_len < sizeof *args) 83 return EINVAL; 84 85 if (mp->mnt_flag & MNT_GETARGS) { 86 if ((bmp = (void *)mp->mnt_data) == NULL) 87 return EIO; 88 args->fspec = NULL; 89 *data_len = sizeof *args; 90 return 0; 91 } 92 93 94 DPRINTF("%s: args->fspec=%s\n", __func__, args->fspec); 95 update = mp->mnt_flag & MNT_UPDATE; 96 if (args->fspec == NULL) { 97 /* nothing to do. */ 98 return EINVAL; 99 } 100 101 if (args->fspec != NULL) { 102 /* Look up the name and verify that it's sane. */ 103 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec); 104 if ((error = namei(&nd)) != 0) 105 return (error); 106 devvp = nd.ni_vp; 107 108 if (!update) { 109 /* 110 * Be sure this is a valid block device 111 */ 112 if (devvp->v_type != VBLK) 113 error = ENOTBLK; 114 else if (bdevsw_lookup(devvp->v_rdev) == NULL) 115 error = ENXIO; 116 } else { 117 /* 118 * Be sure we're still naming the same device 119 * used for our initial mount 120 */ 121 if (devvp != bmp->devvp) 122 error = EINVAL; 123 } 124 } 125 126 /* 127 * If mount by non-root, then verify that user has necessary 128 * permissions on the device. 129 * 130 * Permission to update a mount is checked higher, so here we presume 131 * updating the mount is okay (for example, as far as securelevel goes) 132 * which leaves us with the normal check. 133 */ 134 if (error == 0) { 135 int accessmode = VREAD; 136 if (update ? 137 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 : 138 (mp->mnt_flag & MNT_RDONLY) == 0) 139 accessmode |= VWRITE; 140 141 error = genfs_can_mount(devvp, accessmode, l->l_cred); 142 } 143 144 if (error) { 145 vrele(devvp); 146 return error; 147 } 148 149 if (!update) { 150 if ((error = sysvbfs_mountfs(devvp, mp, l)) != 0) { 151 vrele(devvp); 152 return error; 153 } 154 } else if (mp->mnt_flag & MNT_RDONLY) { 155 /* XXX: r/w -> read only */ 156 } 157 158 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE, 159 mp->mnt_op->vfs_name, mp, l); 160 } 161 162 int 163 sysvbfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l) 164 { 165 kauth_cred_t cred = l->l_cred; 166 struct sysvbfs_mount *bmp; 167 struct partinfo dpart; 168 int error, oflags; 169 170 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 171 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0); 172 VOP_UNLOCK(devvp, 0); 173 if (error) 174 return error; 175 176 /* Open block device */ 177 oflags = FREAD; 178 if ((mp->mnt_flag & MNT_RDONLY) == 0) 179 oflags |= FWRITE; 180 if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0) 181 return error; 182 183 /* Get partition information */ 184 if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred)) != 0) { 185 VOP_CLOSE(devvp, oflags, NOCRED); 186 return error; 187 } 188 189 bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK); 190 if (bmp == NULL) { 191 VOP_CLOSE(devvp, oflags, NOCRED); 192 return ENOMEM; 193 } 194 bmp->devvp = devvp; 195 bmp->mountp = mp; 196 if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) { 197 VOP_CLOSE(devvp, oflags, NOCRED); 198 free(bmp, M_SYSVBFS_VFS); 199 return error; 200 } 201 LIST_INIT(&bmp->bnode_head); 202 203 mp->mnt_data = bmp; 204 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev; 205 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS); 206 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 207 mp->mnt_flag |= MNT_LOCAL; 208 mp->mnt_dev_bshift = BFS_BSHIFT; 209 mp->mnt_fs_bshift = BFS_BSHIFT; 210 211 DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype, 212 dpart.disklab->d_type, dpart.disklab->d_secsize); 213 214 return 0; 215 } 216 217 int 218 sysvbfs_start(struct mount *mp, int flags) 219 { 220 221 DPRINTF("%s:\n", __func__); 222 /* Nothing to do. */ 223 return 0; 224 } 225 226 int 227 sysvbfs_unmount(struct mount *mp, int mntflags) 228 { 229 struct sysvbfs_mount *bmp = (void *)mp->mnt_data; 230 int error; 231 232 DPRINTF("%s: %p\n", __func__, bmp); 233 234 if ((error = vflush(mp, NULLVP, 235 mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0) 236 return error; 237 238 vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY); 239 error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED); 240 vput(bmp->devvp); 241 242 sysvbfs_bfs_fini(bmp->bfs); 243 244 free(bmp, M_SYSVBFS_VFS); 245 mp->mnt_data = NULL; 246 mp->mnt_flag &= ~MNT_LOCAL; 247 248 return 0; 249 } 250 251 int 252 sysvbfs_root(struct mount *mp, struct vnode **vpp) 253 { 254 struct vnode *vp; 255 int error; 256 257 DPRINTF("%s:\n", __func__); 258 if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0) 259 return error; 260 *vpp = vp; 261 262 return 0; 263 } 264 265 int 266 sysvbfs_statvfs(struct mount *mp, struct statvfs *f) 267 { 268 struct sysvbfs_mount *bmp = mp->mnt_data; 269 struct bfs *bfs = bmp->bfs; 270 int free_block; 271 size_t data_block; 272 273 data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT; 274 if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0) 275 free_block = 0; 276 else 277 free_block = (bfs->data_end >> BFS_BSHIFT) - free_block; 278 279 DPRINTF("%s: %d %d %d\n", __func__, bfs->data_start, 280 bfs->data_end, free_block); 281 282 f->f_bsize = BFS_BSIZE; 283 f->f_frsize = BFS_BSIZE; 284 f->f_iosize = BFS_BSIZE; 285 f->f_blocks = data_block; 286 f->f_bfree = free_block; 287 f->f_bavail = f->f_bfree; 288 f->f_bresvd = 0; 289 f->f_files = bfs->n_inode; 290 f->f_ffree = bfs->max_inode - f->f_files; 291 f->f_favail = f->f_ffree; 292 f->f_fresvd = 0; 293 copy_statvfs_info(f, mp); 294 295 return 0; 296 } 297 298 int 299 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 300 { 301 struct sysvbfs_mount *bmp = mp->mnt_data; 302 struct sysvbfs_node *bnode; 303 struct vnode *v; 304 int err, error; 305 306 DPRINTF("%s:\n", __func__); 307 error = 0; 308 mutex_enter(&mntvnode_lock); 309 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL; 310 bnode = LIST_NEXT(bnode, link)) { 311 v = bnode->vnode; 312 mutex_enter(&v->v_interlock); 313 mutex_exit(&mntvnode_lock); 314 err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK); 315 if (err == 0) { 316 err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0); 317 vput(v); 318 } 319 if (err != 0) 320 error = err; 321 mutex_enter(&mntvnode_lock); 322 } 323 mutex_exit(&mntvnode_lock); 324 325 return error; 326 } 327 328 int 329 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 330 { 331 struct sysvbfs_mount *bmp = mp->mnt_data; 332 struct bfs *bfs = bmp->bfs; 333 struct vnode *vp; 334 struct sysvbfs_node *bnode; 335 struct bfs_inode *inode; 336 int error; 337 338 DPRINTF("%s: i-node=%lld\n", __func__, (long long)ino); 339 /* Lookup requested i-node */ 340 if (!bfs_inode_lookup(bfs, ino, &inode)) { 341 DPRINTF("bfs_inode_lookup failed.\n"); 342 return ENOENT; 343 } 344 345 retry: 346 mutex_enter(&mntvnode_lock); 347 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL; 348 bnode = LIST_NEXT(bnode, link)) { 349 if (bnode->inode->number == ino) { 350 vp = bnode->vnode; 351 mutex_enter(&vp->v_interlock); 352 mutex_exit(&mntvnode_lock); 353 if (vget(vp, LK_EXCLUSIVE|LK_RETRY|LK_INTERLOCK) == 0) { 354 *vpp = vp; 355 return 0; 356 } else { 357 goto retry; 358 } 359 } 360 } 361 mutex_exit(&mntvnode_lock); 362 363 /* Allocate v-node. */ 364 if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) != 365 0) { 366 DPRINTF("%s: getnewvnode error.\n", __func__); 367 return error; 368 } 369 /* Lock vnode here */ 370 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 371 372 /* Allocate i-node */ 373 vp->v_data = pool_get(&sysvbfs_node_pool, PR_WAITOK); 374 memset(vp->v_data, 0, sizeof(struct sysvbfs_node)); 375 bnode = vp->v_data; 376 mutex_enter(&mntvnode_lock); 377 LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link); 378 mutex_exit(&mntvnode_lock); 379 bnode->vnode = vp; 380 bnode->bmp = bmp; 381 bnode->inode = inode; 382 bnode->lockf = NULL; /* advlock */ 383 384 if (ino == BFS_ROOT_INODE) { /* BFS is flat filesystem */ 385 vp->v_type = VDIR; 386 vp->v_vflag |= VV_ROOT; 387 } else { 388 vp->v_type = VREG; 389 } 390 391 genfs_node_init(vp, &sysvbfs_genfsops); 392 uvm_vnp_setsize(vp, bfs_file_size(inode)); 393 *vpp = vp; 394 395 return 0; 396 } 397 398 int 399 sysvbfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp) 400 { 401 402 DPRINTF("%s:\n", __func__); 403 /* notyet */ 404 return EOPNOTSUPP; 405 } 406 407 int 408 sysvbfs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size) 409 { 410 411 DPRINTF("%s:\n", __func__); 412 /* notyet */ 413 return EOPNOTSUPP; 414 } 415 416 MALLOC_DECLARE(M_BFS); 417 MALLOC_DECLARE(M_SYSVBFS_VNODE); 418 419 void 420 sysvbfs_init(void) 421 { 422 423 DPRINTF("%s:\n", __func__); 424 malloc_type_attach(M_SYSVBFS_VFS); 425 malloc_type_attach(M_BFS); 426 malloc_type_attach(M_SYSVBFS_VNODE); 427 pool_init(&sysvbfs_node_pool, sizeof(struct sysvbfs_node), 0, 0, 0, 428 "sysvbfs_node_pool", &pool_allocator_nointr, IPL_NONE); 429 } 430 431 void 432 sysvbfs_reinit(void) 433 { 434 435 /* Nothing to do. */ 436 DPRINTF("%s:\n", __func__); 437 } 438 439 void 440 sysvbfs_done(void) 441 { 442 443 DPRINTF("%s:\n", __func__); 444 pool_destroy(&sysvbfs_node_pool); 445 malloc_type_detach(M_BFS); 446 malloc_type_detach(M_SYSVBFS_VFS); 447 malloc_type_detach(M_SYSVBFS_VNODE); 448 } 449 450 int 451 sysvbfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags, 452 kauth_cred_t cred) 453 { 454 455 return 0; 456 } 457