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