1 /* $NetBSD: sysvbfs_vfsops.c,v 1.20 2007/12/15 00:39:36 perry 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: sysvbfs_vfsops.c,v 1.20 2007/12/15 00:39:36 perry Exp $"); 41 42 #include <sys/types.h> 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/pool.h> 46 #include <sys/time.h> 47 #include <sys/ucred.h> 48 #include <sys/mount.h> 49 #include <sys/disklabel.h> 50 #include <sys/fcntl.h> 51 #include <sys/malloc.h> 52 #include <sys/kauth.h> 53 #include <sys/proc.h> 54 55 /* v-node */ 56 #include <sys/namei.h> 57 #include <sys/vnode.h> 58 /* devsw */ 59 #include <sys/conf.h> 60 61 #include <fs/sysvbfs/sysvbfs.h> /* external interface */ 62 #include <fs/sysvbfs/bfs.h> /* internal interface */ 63 64 #ifdef SYSVBFS_VNOPS_DEBUG 65 #define DPRINTF(fmt, args...) printf(fmt, ##args) 66 #else 67 #define DPRINTF(arg...) ((void)0) 68 #endif 69 70 MALLOC_JUSTDEFINE(M_SYSVBFS_VFS, "sysvbfs vfs", "sysvbfs vfs structures"); 71 72 struct pool sysvbfs_node_pool; 73 74 int sysvbfs_mountfs(struct vnode *, struct mount *, struct lwp *); 75 76 int 77 sysvbfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 78 { 79 struct lwp *l = curlwp; 80 struct nameidata nd; 81 struct sysvbfs_args *args = data; 82 struct sysvbfs_mount *bmp = NULL; 83 struct vnode *devvp = NULL; 84 int error; 85 bool update; 86 87 DPRINTF("%s: mnt_flag=%x\n", __func__, mp->mnt_flag); 88 89 if (*data_len < sizeof *args) 90 return EINVAL; 91 92 if (mp->mnt_flag & MNT_GETARGS) { 93 if ((bmp = (void *)mp->mnt_data) == NULL) 94 return EIO; 95 args->fspec = NULL; 96 *data_len = sizeof *args; 97 return 0; 98 } 99 100 101 DPRINTF("%s: args->fspec=%s\n", __func__, args->fspec); 102 update = mp->mnt_flag & MNT_UPDATE; 103 if (args->fspec == NULL) { 104 /* nothing to do. */ 105 return EINVAL; 106 } 107 108 if (args->fspec != NULL) { 109 /* Look up the name and verify that it's sane. */ 110 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec); 111 if ((error = namei(&nd)) != 0) 112 return (error); 113 devvp = nd.ni_vp; 114 115 if (!update) { 116 /* 117 * Be sure this is a valid block device 118 */ 119 if (devvp->v_type != VBLK) 120 error = ENOTBLK; 121 else if (bdevsw_lookup(devvp->v_specinfo->si_rdev) == 122 NULL) 123 error = ENXIO; 124 } else { 125 /* 126 * Be sure we're still naming the same device 127 * used for our initial mount 128 */ 129 if (devvp != bmp->devvp) 130 error = EINVAL; 131 } 132 } 133 134 /* 135 * If mount by non-root, then verify that user has necessary 136 * permissions on the device. 137 */ 138 if (error == 0 && kauth_authorize_generic(l->l_cred, 139 KAUTH_GENERIC_ISSUSER, NULL)) { 140 int accessmode = VREAD; 141 if (update ? 142 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 : 143 (mp->mnt_flag & MNT_RDONLY) == 0) 144 accessmode |= VWRITE; 145 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 146 error = VOP_ACCESS(devvp, accessmode, l->l_cred); 147 VOP_UNLOCK(devvp, 0); 148 } 149 150 if (error) { 151 vrele(devvp); 152 return error; 153 } 154 155 if (!update) { 156 if ((error = sysvbfs_mountfs(devvp, mp, l)) != 0) { 157 vrele(devvp); 158 return error; 159 } 160 } else if (mp->mnt_flag & MNT_RDONLY) { 161 /* XXX: r/w -> read only */ 162 } 163 164 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE, 165 mp->mnt_op->vfs_name, mp, l); 166 } 167 168 int 169 sysvbfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l) 170 { 171 kauth_cred_t cred = l->l_cred; 172 struct sysvbfs_mount *bmp; 173 struct partinfo dpart; 174 int error; 175 176 if ((error = vfs_mountedon(devvp)) != 0) 177 return error; /* Already mounted */ 178 if (vcount(devvp) > 1) 179 return EBUSY; /* Opened by other */ 180 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 181 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0); 182 VOP_UNLOCK(devvp, 0); 183 if (error) 184 return error; 185 186 /* Open block device */ 187 if ((error = VOP_OPEN(devvp, FREAD, NOCRED)) != 0) 188 return error; 189 190 /* Get partition information */ 191 if ((error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred)) != 0) 192 return error; 193 194 bmp = malloc(sizeof(struct sysvbfs_mount), M_SYSVBFS_VFS, M_WAITOK); 195 if (bmp == NULL) 196 return ENOMEM; 197 bmp->devvp = devvp; 198 bmp->mountp = mp; 199 if ((error = sysvbfs_bfs_init(&bmp->bfs, devvp)) != 0) { 200 free(bmp, M_SYSVBFS_VFS); 201 return error; 202 } 203 LIST_INIT(&bmp->bnode_head); 204 205 mp->mnt_data = bmp; 206 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev; 207 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_SYSVBFS); 208 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 209 mp->mnt_flag |= MNT_LOCAL; 210 mp->mnt_dev_bshift = BFS_BSHIFT; 211 mp->mnt_fs_bshift = BFS_BSHIFT; 212 213 DPRINTF("fstype=%d dtype=%d bsize=%d\n", dpart.part->p_fstype, 214 dpart.disklab->d_type, dpart.disklab->d_secsize); 215 216 return 0; 217 } 218 219 int 220 sysvbfs_start(struct mount *mp, int flags) 221 { 222 223 DPRINTF("%s:\n", __func__); 224 /* Nothing to do. */ 225 return 0; 226 } 227 228 int 229 sysvbfs_unmount(struct mount *mp, int mntflags) 230 { 231 struct sysvbfs_mount *bmp = (void *)mp->mnt_data; 232 int error; 233 234 DPRINTF("%s: %p\n", __func__, bmp); 235 236 if ((error = vflush(mp, NULLVP, 237 mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0) 238 return error; 239 240 vn_lock(bmp->devvp, LK_EXCLUSIVE | LK_RETRY); 241 error = VOP_CLOSE(bmp->devvp, FREAD, NOCRED); 242 vput(bmp->devvp); 243 244 sysvbfs_bfs_fini(bmp->bfs); 245 246 free(bmp, M_SYSVBFS_VFS); 247 mp->mnt_data = NULL; 248 mp->mnt_flag &= ~MNT_LOCAL; 249 250 return 0; 251 } 252 253 int 254 sysvbfs_root(struct mount *mp, struct vnode **vpp) 255 { 256 struct vnode *vp; 257 int error; 258 259 DPRINTF("%s:\n", __func__); 260 if ((error = VFS_VGET(mp, BFS_ROOT_INODE, &vp)) != 0) 261 return error; 262 *vpp = vp; 263 264 return 0; 265 } 266 267 int 268 sysvbfs_statvfs(struct mount *mp, struct statvfs *f) 269 { 270 struct sysvbfs_mount *bmp = mp->mnt_data; 271 struct bfs *bfs = bmp->bfs; 272 int free_block; 273 size_t data_block; 274 275 data_block = (bfs->data_end - bfs->data_start) >> BFS_BSHIFT; 276 if (bfs_inode_alloc(bfs, 0, 0, &free_block) != 0) 277 free_block = 0; 278 else 279 free_block = (bfs->data_end >> BFS_BSHIFT) - free_block; 280 281 DPRINTF("%s: %d %d %d\n", __func__, bfs->data_start, 282 bfs->data_end, free_block); 283 284 f->f_bsize = BFS_BSIZE; 285 f->f_frsize = BFS_BSIZE; 286 f->f_iosize = BFS_BSIZE; 287 f->f_blocks = data_block; 288 f->f_bfree = free_block; 289 f->f_bavail = f->f_bfree; 290 f->f_bresvd = 0; 291 f->f_files = bfs->n_inode; 292 f->f_ffree = bfs->max_inode - f->f_files; 293 f->f_favail = f->f_ffree; 294 f->f_fresvd = 0; 295 copy_statvfs_info(f, mp); 296 297 return 0; 298 } 299 300 int 301 sysvbfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 302 { 303 struct sysvbfs_mount *bmp = mp->mnt_data; 304 struct sysvbfs_node *bnode; 305 struct vnode *v; 306 int err, error; 307 308 DPRINTF("%s:\n", __func__); 309 error = 0; 310 simple_lock(&mntvnode_slock); 311 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL; 312 bnode = LIST_NEXT(bnode, link)) { 313 simple_unlock(&mntvnode_slock); 314 v = bnode->vnode; 315 err = vget(v, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK); 316 if (err == 0) { 317 err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0); 318 vput(v); 319 } 320 if (err != 0) 321 error = err; 322 simple_lock(&mntvnode_slock); 323 } 324 simple_unlock(&mntvnode_slock); 325 326 return error; 327 } 328 329 int 330 sysvbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 331 { 332 struct sysvbfs_mount *bmp = mp->mnt_data; 333 struct bfs *bfs = bmp->bfs; 334 struct vnode *vp; 335 struct sysvbfs_node *bnode; 336 struct bfs_inode *inode; 337 int error; 338 339 DPRINTF("%s: i-node=%d\n", __func__, ino); 340 /* Lookup requested i-node */ 341 if (!bfs_inode_lookup(bfs, ino, &inode)) { 342 DPRINTF("bfs_inode_lookup failed.\n"); 343 return ENOENT; 344 } 345 for (bnode = LIST_FIRST(&bmp->bnode_head); bnode != NULL; 346 bnode = LIST_NEXT(bnode, link)) { 347 if (bnode->inode->number == ino) { 348 *vpp = bnode->vnode; 349 } 350 } 351 352 /* Allocate v-node. */ 353 if ((error = getnewvnode(VT_SYSVBFS, mp, sysvbfs_vnodeop_p, &vp)) != 354 0) { 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 simple_lock(&mntvnode_slock); 366 LIST_INSERT_HEAD(&bmp->bnode_head, bnode, link); 367 simple_unlock(&mntvnode_slock); 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