Home | History | Annotate | Line # | Download | only in efs
efs_vnops.c revision 1.6.8.6
      1 /*	$NetBSD: efs_vnops.c,v 1.6.8.6 2008/02/04 09:23:44 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Stephen M. Rumble <rumble (at) ephemeral.org>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 #include <sys/cdefs.h>
     20 __KERNEL_RCSID(0, "$NetBSD: efs_vnops.c,v 1.6.8.6 2008/02/04 09:23:44 yamt Exp $");
     21 
     22 #include <sys/param.h>
     23 #include <sys/systm.h>
     24 #include <sys/proc.h>
     25 #include <sys/kernel.h>
     26 #include <sys/vnode.h>
     27 #include <sys/mount.h>
     28 #include <sys/malloc.h>
     29 #include <sys/namei.h>
     30 #include <sys/dirent.h>
     31 #include <sys/lockf.h>
     32 #include <sys/unistd.h>
     33 
     34 #include <miscfs/genfs/genfs.h>
     35 #include <miscfs/genfs/genfs_node.h>
     36 
     37 #include <fs/efs/efs.h>
     38 #include <fs/efs/efs_sb.h>
     39 #include <fs/efs/efs_dir.h>
     40 #include <fs/efs/efs_genfs.h>
     41 #include <fs/efs/efs_mount.h>
     42 #include <fs/efs/efs_extent.h>
     43 #include <fs/efs/efs_dinode.h>
     44 #include <fs/efs/efs_inode.h>
     45 #include <fs/efs/efs_subr.h>
     46 #include <fs/efs/efs_ihash.h>
     47 
     48 MALLOC_DECLARE(M_EFSTMP);
     49 
     50 /*
     51  * Lookup a pathname component in the given directory.
     52  *
     53  * Returns 0 on success.
     54  */
     55 static int
     56 efs_lookup(void *v)
     57 {
     58 	struct vop_lookup_args /* {
     59 		struct vnode *a_dvp;
     60 		struct vnode **a_vpp;
     61 		struct componentname *a_cnp;
     62 	} */ *ap = v;
     63 	struct componentname *cnp = ap->a_cnp;
     64 	struct vnode *vp;
     65 	ino_t ino;
     66 	int err, nameiop = cnp->cn_nameiop;
     67 
     68 	/* ensure that the directory can be accessed first */
     69         err = VOP_ACCESS(ap->a_dvp, VEXEC, cnp->cn_cred);
     70 	if (err)
     71 		return (err);
     72 
     73 	err = cache_lookup(ap->a_dvp, ap->a_vpp, cnp);
     74 	if (err != -1)
     75 		return (err);
     76 
     77 	/*
     78 	 * Handle the three lookup types: '.', '..', and everything else.
     79 	 */
     80 	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
     81 		vref(ap->a_dvp);
     82 		*ap->a_vpp = ap->a_dvp;
     83 	} else if (cnp->cn_flags & ISDOTDOT) {
     84 		err = efs_inode_lookup(VFSTOEFS(ap->a_dvp->v_mount),
     85 		    EFS_VTOI(ap->a_dvp), ap->a_cnp, &ino);
     86 		if (err)
     87 			return (err);
     88 
     89 		VOP_UNLOCK(ap->a_dvp, 0);	/* preserve lock order */
     90 
     91 		err = VFS_VGET(ap->a_dvp->v_mount, ino, &vp);
     92 		if (err) {
     93 			vn_lock(ap->a_dvp, LK_EXCLUSIVE | LK_RETRY);
     94 			return (err);
     95 		}
     96 		vn_lock(ap->a_dvp, LK_EXCLUSIVE | LK_RETRY);
     97 		*ap->a_vpp = vp;
     98 	} else {
     99 		err = efs_inode_lookup(VFSTOEFS(ap->a_dvp->v_mount),
    100 		    EFS_VTOI(ap->a_dvp), ap->a_cnp, &ino);
    101 		if (err) {
    102 			if (err == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
    103 			    nameiop != CREATE)
    104 				cache_enter(ap->a_dvp, NULL, cnp);
    105 			if (err == ENOENT && (nameiop == CREATE ||
    106 			    nameiop == RENAME)) {
    107 				err = VOP_ACCESS(ap->a_dvp, VWRITE,
    108 				    cnp->cn_cred);
    109 				if (err)
    110 					return (err);
    111 				cnp->cn_flags |= SAVENAME;
    112 				return (EJUSTRETURN);
    113 			}
    114 			return (err);
    115 		}
    116 		err = VFS_VGET(ap->a_dvp->v_mount, ino, &vp);
    117 		if (err)
    118 			return (err);
    119 		*ap->a_vpp = vp;
    120 	}
    121 
    122 	if (cnp->cn_flags & MAKEENTRY)
    123 		cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
    124 
    125 	return (0);
    126 }
    127 
    128 /*
    129  * Determine the accessiblity of a file based on the permissions allowed by the
    130  * specified credentials.
    131  *
    132  * Returns 0 on success.
    133  */
    134 static int
    135 efs_access(void *v)
    136 {
    137 	struct vop_access_args /* {
    138 		const struct vnodeop_desc *a_desc;
    139 		struct vnode *a_vp;
    140 		int a_mode;
    141 		struct ucred *a_cred;
    142 	} */ *ap = v;
    143 	struct vnode *vp = ap->a_vp;
    144 	struct efs_inode *eip = EFS_VTOI(vp);
    145 
    146 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
    147 		return (EROFS);
    148 
    149 	return (vaccess(vp->v_type, eip->ei_mode, eip->ei_uid, eip->ei_gid,
    150 	    ap->a_mode, ap->a_cred));
    151 }
    152 
    153 /*
    154  * Get specific vnode attributes on a file. See vattr(9).
    155  *
    156  * Returns 0 on success.
    157  */
    158 static int
    159 efs_getattr(void *v)
    160 {
    161 	struct vop_getattr_args /* {
    162 		const struct vnodeop_desc *a_desc;
    163 		struct vnode *a_vp;
    164 		struct vattr *a_vap;
    165 		struct ucred *a_cred;
    166 	} */ *ap = v;
    167 
    168 	struct vattr *vap = ap->a_vap;
    169 	struct efs_inode *eip = EFS_VTOI(ap->a_vp);
    170 
    171 	vattr_null(ap->a_vap);
    172 	vap->va_type		= ap->a_vp->v_type;
    173 	vap->va_mode		= eip->ei_mode;
    174 	vap->va_nlink		= eip->ei_nlink;
    175 	vap->va_uid		= eip->ei_uid;
    176 	vap->va_gid		= eip->ei_gid;
    177 	vap->va_fsid 		= ap->a_vp->v_mount->mnt_stat.f_fsid;
    178 	vap->va_fileid 		= eip->ei_number;
    179 	vap->va_size 		= eip->ei_size;
    180 
    181 	if (ap->a_vp->v_type == VBLK)
    182 		vap->va_blocksize = BLKDEV_IOSIZE;
    183 	else if (ap->a_vp->v_type == VCHR)
    184 		vap->va_blocksize = MAXBSIZE;
    185 	else
    186 		vap->va_blocksize = EFS_BB_SIZE;
    187 
    188 	vap->va_atime.tv_sec	= eip->ei_atime;
    189 	vap->va_mtime.tv_sec	= eip->ei_mtime;
    190 	vap->va_ctime.tv_sec	= eip->ei_ctime;
    191 /*	vap->va_birthtime 	= */
    192 	vap->va_gen		= eip->ei_gen;
    193 	vap->va_flags		= ap->a_vp->v_vflag |
    194 	    ap->a_vp->v_iflag | ap->a_vp->v_uflag;
    195 
    196 	if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR) {
    197 		uint32_t dmaj, dmin;
    198 
    199 		if (be16toh(eip->ei_di.di_odev) != EFS_DINODE_ODEV_INVALID) {
    200 			dmaj = EFS_DINODE_ODEV_MAJ(be16toh(eip->ei_di.di_odev));
    201 			dmin = EFS_DINODE_ODEV_MIN(be16toh(eip->ei_di.di_odev));
    202 		} else {
    203 			dmaj = EFS_DINODE_NDEV_MAJ(be32toh(eip->ei_di.di_ndev));
    204 			dmin = EFS_DINODE_NDEV_MIN(be32toh(eip->ei_di.di_ndev));
    205 		}
    206 
    207 		vap->va_rdev = makedev(dmaj, dmin);
    208 	}
    209 
    210 	vap->va_bytes		= eip->ei_size;
    211 /*	vap->va_filerev		= */
    212 /*	vap->va_vaflags		= */
    213 
    214 	return (0);
    215 }
    216 
    217 /*
    218  * Read a file.
    219  *
    220  * Returns 0 on success.
    221  */
    222 static int
    223 efs_read(void *v)
    224 {
    225 	struct vop_read_args /* {
    226 		const struct vnodeop_desc *a_desc;
    227 		struct vnode *a_vp;
    228 		struct uio *a_uio;
    229 		int a_ioflag;
    230 		struct ucred *a_cred;
    231 	} */ *ap = v;
    232 	struct efs_extent ex;
    233 	struct efs_extent_iterator exi;
    234 	void *win;
    235 	struct uio *uio = ap->a_uio;
    236 	struct efs_inode *eip = EFS_VTOI(ap->a_vp);
    237 	off_t start;
    238 	vsize_t len;
    239 	int err, ret, flags;
    240 	const int advice = IO_ADV_DECODE(ap->a_ioflag);
    241 
    242 	if (ap->a_vp->v_type == VDIR)
    243 		return (EISDIR);
    244 
    245 	if (ap->a_vp->v_type != VREG)
    246 		return (EINVAL);
    247 
    248 	efs_extent_iterator_init(&exi, eip, uio->uio_offset);
    249 	ret = efs_extent_iterator_next(&exi, &ex);
    250 	while (ret == 0) {
    251 		if (uio->uio_offset < 0 || uio->uio_offset >= eip->ei_size ||
    252 		    uio->uio_resid == 0)
    253 			break;
    254 
    255 		start = ex.ex_offset * EFS_BB_SIZE;
    256 		len   = ex.ex_length * EFS_BB_SIZE;
    257 
    258 		if (!(uio->uio_offset >= start &&
    259 		      uio->uio_offset < (start + len))) {
    260 			ret = efs_extent_iterator_next(&exi, &ex);
    261 			continue;
    262 		}
    263 
    264 		start = uio->uio_offset - start;
    265 
    266 		len = MIN(len - start, uio->uio_resid);
    267 		len = MIN(len, eip->ei_size - uio->uio_offset);
    268 
    269 		win = ubc_alloc(&ap->a_vp->v_uobj, uio->uio_offset,
    270 		    &len, advice, UBC_READ);
    271 
    272 		flags = UBC_WANT_UNMAP(ap->a_vp) ? UBC_UNMAP : 0;
    273 
    274 		err = uiomove(win, len, uio);
    275 		ubc_release(win, flags);
    276 		if (err) {
    277 			EFS_DPRINTF(("efs_read: uiomove error %d\n",
    278 			    err));
    279 			return (err);
    280 		}
    281 	}
    282 
    283 	return ((ret == -1) ? 0 : ret);
    284 }
    285 
    286 static int
    287 efs_readdir(void *v)
    288 {
    289 	struct vop_readdir_args /* {
    290 		const struct vnodeop_desc *a_desc;
    291 		struct vnode *a_vp;
    292 		struct uio *a_uio;
    293 		struct ucred *a_cred;
    294 		int *a_eofflag;
    295 		off_t **a_cookies;
    296 		int *a_ncookies;
    297 	} */ *ap = v;
    298 	struct dirent *dp;
    299 	struct efs_dinode edi;
    300 	struct efs_extent ex;
    301 	struct efs_extent_iterator exi;
    302 	struct buf *bp;
    303 	struct efs_dirent *de;
    304 	struct efs_dirblk *db;
    305 	struct uio *uio = ap->a_uio;
    306 	struct efs_inode *ei = EFS_VTOI(ap->a_vp);
    307 	off_t *cookies = NULL;
    308 	off_t offset;
    309 	int i, j, err, ret, s, slot, ncookies, maxcookies = 0;
    310 
    311 	if (ap->a_vp->v_type != VDIR)
    312 		return (ENOTDIR);
    313 
    314 	if (ap->a_eofflag != NULL)
    315 		*ap->a_eofflag = false;
    316 
    317 	if (ap->a_ncookies != NULL) {
    318 		ncookies = 0;
    319 		maxcookies =
    320 		    uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
    321 		cookies = malloc(maxcookies * sizeof(off_t), M_TEMP, M_WAITOK);
    322  	}
    323 
    324 	dp = malloc(sizeof(struct dirent), M_EFSTMP, M_WAITOK | M_ZERO);
    325 
    326 	offset = 0;
    327 	efs_extent_iterator_init(&exi, ei, 0);
    328 	while ((ret = efs_extent_iterator_next(&exi, &ex)) == 0) {
    329 		for (i = 0; i < ex.ex_length; i++) {
    330 			err = efs_bread(VFSTOEFS(ap->a_vp->v_mount),
    331 			    ex.ex_bn + i, NULL, &bp);
    332 			if (err) {
    333 				brelse(bp, 0);
    334 				goto exit_err;
    335 			}
    336 
    337 			db = (struct efs_dirblk *)bp->b_data;
    338 
    339 			if (be16toh(db->db_magic) != EFS_DIRBLK_MAGIC) {
    340 				printf("efs_readdir: bad dirblk\n");
    341 				brelse(bp, 0);
    342 				continue;
    343 			}
    344 
    345 			for (j = 0; j < db->db_slots; j++) {
    346 				slot = EFS_DIRENT_OFF_EXPND(db->db_space[j]);
    347 				if (slot == EFS_DIRBLK_SLOT_FREE)
    348 					continue;
    349 
    350 				if (!EFS_DIRENT_OFF_VALID(slot)) {
    351 					printf("efs_readdir: bad dirent\n");
    352 					continue;
    353 				}
    354 
    355 				de = EFS_DIRBLK_TO_DIRENT(db, slot);
    356 				s = _DIRENT_RECLEN(dp, de->de_namelen);
    357 
    358 				if (offset < uio->uio_offset) {
    359 					offset += s;
    360 					continue;
    361 				}
    362 
    363 				/* XXX - shouldn't happen, right? */
    364 				if (offset > uio->uio_offset ||
    365 				    s > uio->uio_resid) {
    366 					brelse(bp, 0);
    367 					goto exit_ok;
    368 				}
    369 
    370 				/* de_namelen is uint8_t, d.d_name is 512b */
    371 				KASSERT(sizeof(dp->d_name)-de->de_namelen > 0);
    372 				dp->d_fileno = be32toh(de->de_inumber);
    373 				dp->d_reclen = s;
    374 				dp->d_namlen = de->de_namelen;
    375 				memcpy(dp->d_name, de->de_name,
    376 				    de->de_namelen);
    377 				dp->d_name[de->de_namelen] = '\0';
    378 
    379 				/* look up inode to get type */
    380 				err = efs_read_inode(
    381 				    VFSTOEFS(ap->a_vp->v_mount),
    382 				    dp->d_fileno, NULL, &edi);
    383 				if (err) {
    384 					brelse(bp, 0);
    385 					goto exit_err;
    386 				}
    387 
    388 				switch (be16toh(edi.di_mode) & EFS_IFMT) {
    389 				case EFS_IFIFO:
    390 					dp->d_type = DT_FIFO;
    391 					break;
    392 				case EFS_IFCHR:
    393 					dp->d_type = DT_CHR;
    394 					break;
    395 				case EFS_IFDIR:
    396 					dp->d_type = DT_DIR;
    397 					break;
    398 				case EFS_IFBLK:
    399 					dp->d_type = DT_BLK;
    400 					break;
    401 				case EFS_IFREG:
    402 					dp->d_type = DT_REG;
    403 					break;
    404 				case EFS_IFLNK:
    405 					dp->d_type = DT_LNK;
    406 					break;
    407 				case EFS_IFSOCK:
    408 					dp->d_type = DT_SOCK;
    409 					break;
    410 				default:
    411 					dp->d_type = DT_UNKNOWN;
    412 					break;
    413 				}
    414 
    415 				err = uiomove(dp, s, uio);
    416 				if (err) {
    417 					brelse(bp, 0);
    418 					goto exit_err;
    419 				}
    420 
    421 				offset += s;
    422 
    423 				if (cookies != NULL && maxcookies != 0) {
    424 					cookies[ncookies++] = offset;
    425 					if (ncookies == maxcookies) {
    426 						brelse(bp, 0);
    427 						goto exit_ok;
    428 					}
    429 				}
    430 			}
    431 
    432 			brelse(bp, 0);
    433 		}
    434 	}
    435 
    436 	if (ret != -1) {
    437 		err = ret;
    438 		goto exit_err;
    439 	}
    440 
    441 	if (ap->a_eofflag != NULL)
    442 		*ap->a_eofflag = true;
    443 
    444  exit_ok:
    445 	if (cookies != NULL) {
    446 		*ap->a_cookies = cookies;
    447 		*ap->a_ncookies = ncookies;
    448 	}
    449 
    450 	uio->uio_offset = offset;
    451 
    452 	free(dp, M_EFSTMP);
    453 
    454 	return (0);
    455 
    456  exit_err:
    457 	if (cookies != NULL)
    458 		free(cookies, M_TEMP);
    459 
    460 	free(dp, M_EFSTMP);
    461 
    462 	return (err);
    463 }
    464 
    465 static int
    466 efs_readlink(void *v)
    467 {
    468 	struct vop_readlink_args /* {
    469 		const struct vnodeop_desc *a_desc;
    470 		struct vnode *a_vp;
    471 		struct uio *a_uio;
    472 		struct ucred *a_cred;
    473 	} */ *ap = v;
    474 	struct uio *uio = ap->a_uio;
    475 	struct efs_inode *eip = EFS_VTOI(ap->a_vp);
    476 	char *buf;
    477 	size_t len;
    478 	int err, i;
    479 
    480 	if ((eip->ei_mode & EFS_IFMT) != EFS_IFLNK)
    481 		return (EINVAL);
    482 
    483 	if (uio->uio_resid < 1)
    484 		return (EINVAL);
    485 
    486 	buf = malloc(eip->ei_size + 1, M_EFSTMP, M_ZERO | M_WAITOK);
    487 
    488 	/* symlinks are either inlined in the inode, or in extents */
    489 	if (eip->ei_numextents == 0) {
    490 		if (eip->ei_size > sizeof(eip->ei_di.di_symlink)) {
    491 			EFS_DPRINTF(("efs_readlink: too big for inline\n"));
    492 			free(buf, M_EFSTMP);
    493 			return (EBADF);
    494 		}
    495 
    496 		memcpy(buf, eip->ei_di.di_symlink, eip->ei_size);
    497 		len = MIN(uio->uio_resid, eip->ei_size + 1);
    498 	} else {
    499 		struct efs_extent_iterator exi;
    500 		struct efs_extent ex;
    501 		struct buf *bp;
    502 		int resid, off, ret;
    503 
    504 		off = 0;
    505 		resid = eip->ei_size;
    506 
    507 		efs_extent_iterator_init(&exi, eip, 0);
    508 		while ((ret = efs_extent_iterator_next(&exi, &ex)) == 0) {
    509 			for (i = 0; i < ex.ex_length; i++) {
    510 				err = efs_bread(VFSTOEFS(ap->a_vp->v_mount),
    511 				    ex.ex_bn + i, NULL, &bp);
    512 				if (err) {
    513 					brelse(bp, 0);
    514 					free(buf, M_EFSTMP);
    515 					return (err);
    516 				}
    517 
    518 				len = MIN(resid, bp->b_bcount);
    519 				memcpy(buf + off, bp->b_data, len);
    520 				brelse(bp, 0);
    521 
    522 				off += len;
    523 				resid -= len;
    524 
    525 				if (resid == 0)
    526 					break;
    527 			}
    528 
    529 			if (resid == 0)
    530 				break;
    531 		}
    532 
    533 		if (ret != 0 && ret != -1) {
    534 			free(buf, M_EFSTMP);
    535 			return (ret);
    536 		}
    537 
    538 		len = off + 1;
    539 	}
    540 
    541 	KASSERT(len >= 1 && len <= (eip->ei_size + 1));
    542 	buf[len - 1] = '\0';
    543 	err = uiomove(buf, len, uio);
    544 	free(buf, M_EFSTMP);
    545 
    546 	return (err);
    547 }
    548 
    549 /*
    550  * Release an inactive vnode. The vnode _must_ be unlocked on return.
    551  * It is either nolonger being used by the kernel, or an unmount is being
    552  * forced.
    553  *
    554  * Returns 0 on success.
    555  */
    556 static int
    557 efs_inactive(void *v)
    558 {
    559 	struct vop_inactive_args /* {
    560 		const struct vnodeop_desc *a_desc;
    561 		struct vnode *a_vp;
    562 		bool *a_recycle
    563 	} */ *ap = v;
    564 	struct efs_inode *eip = EFS_VTOI(ap->a_vp);
    565 
    566 	*ap->a_recycle = (eip->ei_mode == 0);
    567 	VOP_UNLOCK(ap->a_vp, 0);
    568 
    569 	return (0);
    570 }
    571 
    572 static int
    573 efs_reclaim(void *v)
    574 {
    575 	struct vop_reclaim_args /* {
    576 		const struct vnodeop_desc *a_desc;
    577 		struct vnode *a_vp;
    578 	} */ *ap = v;
    579 	struct vnode *vp = ap->a_vp;
    580 
    581 	efs_ihashrem(EFS_VTOI(vp));
    582 	cache_purge(vp);
    583 	genfs_node_destroy(vp);
    584 	pool_put(&efs_inode_pool, vp->v_data);
    585 	vp->v_data = NULL;
    586 
    587 	return (0);
    588 }
    589 
    590 static int
    591 efs_bmap(void *v)
    592 {
    593 	struct vop_bmap_args /* {
    594 		const struct vnodeop_desc *a_desc;
    595 		struct vnode *a_vp;
    596 		daddr_t a_bn;
    597 		struct vnode **a_vpp;
    598 		daddr_t *a_bnp;
    599 		int *a_runp;
    600 	} */ *ap = v;
    601 	struct efs_extent ex;
    602 	struct efs_extent_iterator exi;
    603 	struct vnode *vp = ap->a_vp;
    604 	struct efs_inode *eip = EFS_VTOI(vp);
    605 	bool found;
    606 	int ret;
    607 
    608 	if (ap->a_vpp != NULL)
    609 		*ap->a_vpp = VFSTOEFS(vp->v_mount)->em_devvp;
    610 
    611 	found = false;
    612 	efs_extent_iterator_init(&exi, eip, ap->a_bn * EFS_BB_SIZE);
    613 	while ((ret = efs_extent_iterator_next(&exi, &ex)) == 0) {
    614 		if (ap->a_bn >= ex.ex_offset &&
    615 		    ap->a_bn < (ex.ex_offset + ex.ex_length)) {
    616 			found = true;
    617 			break;
    618 		}
    619 	}
    620 
    621 	KASSERT(!found || (found && ret == 0));
    622 
    623 	if (!found) {
    624 		EFS_DPRINTF(("efs_bmap: ap->a_bn not in extents\n"));
    625 		return ((ret == -1) ? EIO : ret);
    626 	}
    627 
    628 	if (ex.ex_magic != EFS_EXTENT_MAGIC) {
    629 		EFS_DPRINTF(("efs_bmap: exn.ex_magic != EFS_EXTENT_MAGIC\n"));
    630 		return (EIO);
    631 	}
    632 
    633 	if (ap->a_bn < ex.ex_offset) {
    634 		EFS_DPRINTF(("efs_bmap: ap->a_bn < exn.ex_offset\n"));
    635 		return (EIO);
    636 	}
    637 
    638 	KASSERT(ap->a_bn >= ex.ex_offset);
    639 	KASSERT(ex.ex_length > ap->a_bn - ex.ex_offset);
    640 
    641 	*ap->a_bnp = ex.ex_bn + (ap->a_bn - ex.ex_offset);
    642 	if (ap->a_runp != NULL)
    643 		*ap->a_runp = ex.ex_length - (ap->a_bn - ex.ex_offset) - 1;
    644 
    645 	return (0);
    646 }
    647 
    648 static int
    649 efs_strategy(void *v)
    650 {
    651 	struct vop_strategy_args /* {
    652 		const struct vnodeop_desc *a_desc;
    653 		struct vnode *a_vp;
    654 		struct buf *a_bp;
    655 	} */ *ap = v;
    656 	struct vnode *vp = ap->a_vp;
    657 	struct buf *bp = ap->a_bp;
    658 	int error;
    659 
    660 	if (vp == NULL) {
    661 		bp->b_error = EIO;
    662 		biodone(bp);
    663 		return (EIO);
    664 	}
    665 
    666 	if (bp->b_blkno == bp->b_lblkno) {
    667 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
    668 		if (error) {
    669 			bp->b_error = error;
    670 			biodone(bp);
    671 			return (error);
    672 		}
    673 		if ((long)bp->b_blkno == -1)
    674 			clrbuf(bp);
    675 	}
    676 
    677 	if ((long)bp->b_blkno == -1) {
    678 		biodone(bp);
    679 		return (0);
    680 	}
    681 
    682 	return (VOP_STRATEGY(VFSTOEFS(vp->v_mount)->em_devvp, bp));
    683 }
    684 
    685 static int
    686 efs_print(void *v)
    687 {
    688 	struct vop_print_args /* {
    689 		const struct vnodeop_desc *a_desc;
    690 		struct vnode *a_vp;
    691 	} */ *ap = v;
    692 	struct efs_inode *eip = EFS_VTOI(ap->a_vp);
    693 
    694 	printf(	"efs_inode (ino %lu):\n"
    695 		"    ei_mode:         %07o\n"
    696 		"    ei_nlink:        %d\n"
    697 		"    ei_uid:          %d\n"
    698 		"    ei_gid:          %d\n"
    699 		"    ei_size:         %d\n"
    700 		"    ei_atime:        %d\n"
    701 		"    ei_mtime:        %d\n"
    702 		"    ei_ctime:        %d\n"
    703 		"    ei_gen:          %d\n"
    704 		"    ei_numextents:   %d\n"
    705 		"    ei_version:      %d\n",
    706 		(unsigned long)eip->ei_number,
    707 		(unsigned int)eip->ei_mode,
    708 		eip->ei_nlink,
    709 		eip->ei_uid,
    710 		eip->ei_gid,
    711 		eip->ei_size,
    712 		(int32_t)eip->ei_atime,
    713 		(int32_t)eip->ei_mtime,
    714 		(int32_t)eip->ei_ctime,
    715 		eip->ei_gen,
    716 		eip->ei_numextents,
    717 		eip->ei_version);
    718 
    719 	return (0);
    720 }
    721 
    722 static int
    723 efs_pathconf(void *v)
    724 {
    725 	struct vop_pathconf_args /* {
    726 		const struct vnodeop_desc *a_desc;
    727 		struct vnode *a_vp;
    728 		int a_name;
    729 		register_t *a_retval;
    730 	} */ *ap = v;
    731 
    732 	/* IRIX 4 values */
    733 	switch (ap->a_name) {
    734 	case _PC_LINK_MAX:
    735 		*ap->a_retval = 30000;
    736 		break;
    737 	case _PC_NAME_MAX:
    738 		*ap->a_retval = 255;
    739 		break;
    740 	case _PC_PATH_MAX:
    741 		*ap->a_retval = 1024;
    742 		break;
    743 	case _PC_NO_TRUNC:
    744 		*ap->a_retval = 1;
    745 		break;
    746 	case _PC_CHOWN_RESTRICTED:
    747 		*ap->a_retval = 1;
    748 		break;
    749 	case _PC_SYNC_IO:
    750 		*ap->a_retval = 1;
    751 		break;
    752 	case _PC_FILESIZEBITS:
    753 		*ap->a_retval = 32;
    754 		break;
    755 	default:
    756 		return (EINVAL);
    757 	}
    758 
    759 	return (0);
    760 }
    761 
    762 static int
    763 efs_advlock(void *v)
    764 {
    765 	struct vop_advlock_args /* {
    766 		const struct vnodeop_desc *a_desc;
    767 		struct vnode *a_vp;
    768 		void *a_id;
    769 		int a_op;
    770 		struct flock *a_fl;
    771 		int a_flags;
    772 	} */ *ap = v;
    773 	struct efs_inode *eip = EFS_VTOI(ap->a_vp);
    774 
    775 	return (lf_advlock(ap, &eip->ei_lockf, eip->ei_size));
    776 }
    777 
    778 /* Global vfs data structures for efs */
    779 int (**efs_vnodeop_p)(void *);
    780 const struct vnodeopv_entry_desc efs_vnodeop_entries[] = {
    781 	{ &vop_default_desc,	vn_default_error},	/* error handler */
    782 	{ &vop_lookup_desc,	efs_lookup	},	/* lookup */
    783 	{ &vop_create_desc,	genfs_eopnotsupp},	/* create */
    784 	{ &vop_mknod_desc,	genfs_eopnotsupp},	/* mknod */
    785 	{ &vop_open_desc,	genfs_nullop	},	/* open */
    786 	{ &vop_close_desc,	genfs_nullop	},	/* close */
    787 	{ &vop_access_desc,	efs_access	},	/* access */
    788 	{ &vop_getattr_desc,	efs_getattr	},	/* getattr */
    789 	{ &vop_setattr_desc,	genfs_eopnotsupp},	/* setattr */
    790 	{ &vop_read_desc,	efs_read	},	/* read */
    791 	{ &vop_write_desc,	genfs_eopnotsupp},	/* write */
    792 	{ &vop_ioctl_desc,	genfs_enoioctl	},	/* ioctl */
    793 	{ &vop_fcntl_desc,	genfs_fcntl	},	/* fcntl */
    794 	{ &vop_poll_desc,	genfs_poll	},	/* poll */
    795 	{ &vop_kqfilter_desc,	genfs_kqfilter	},	/* kqfilter */
    796 	{ &vop_revoke_desc,	genfs_revoke	},	/* revoke */
    797 	{ &vop_mmap_desc,	genfs_mmap	},	/* mmap */
    798 	{ &vop_fsync_desc,	genfs_eopnotsupp},	/* fsync */
    799 	{ &vop_seek_desc,	genfs_seek	},	/* seek */
    800 	{ &vop_remove_desc,	genfs_eopnotsupp},	/* remove */
    801 	{ &vop_link_desc,	genfs_eopnotsupp},	/* link */
    802 	{ &vop_rename_desc,	genfs_eopnotsupp},	/* rename */
    803 	{ &vop_mkdir_desc,	genfs_eopnotsupp},	/* mkdir */
    804 	{ &vop_rmdir_desc,	genfs_eopnotsupp},	/* rmdir */
    805 	{ &vop_symlink_desc,	genfs_eopnotsupp},	/* symlink */
    806 	{ &vop_readdir_desc,	efs_readdir	},	/* readdir */
    807 	{ &vop_readlink_desc,	efs_readlink	},	/* readlink */
    808 	{ &vop_abortop_desc,	genfs_abortop	},	/* abortop */
    809 	{ &vop_inactive_desc,	efs_inactive	},	/* inactive */
    810 	{ &vop_reclaim_desc,	efs_reclaim	},	/* reclaim */
    811 	{ &vop_lock_desc,	genfs_lock,	},	/* lock */
    812 	{ &vop_unlock_desc,	genfs_unlock,	},	/* unlock */
    813 	{ &vop_islocked_desc,	genfs_islocked,	},	/* islocked */
    814 	{ &vop_bmap_desc,	efs_bmap	},	/* bmap */
    815 	{ &vop_print_desc,	efs_print	},	/* print */
    816 	{ &vop_pathconf_desc,	efs_pathconf	},	/* pathconf */
    817 	{ &vop_advlock_desc,	efs_advlock	},	/* advlock */
    818 							/* blkatoff */
    819 							/* valloc */
    820 							/* balloc */
    821 							/* vfree */
    822 							/* truncate */
    823 							/* whiteout */
    824 	{ &vop_getpages_desc,	genfs_getpages	},	/* getpages */
    825 	{ &vop_putpages_desc,	genfs_putpages	},	/* putpages */
    826 	{ &vop_bwrite_desc,	vn_bwrite	},	/* bwrite */
    827 	{ &vop_strategy_desc,	efs_strategy	},	/* strategy */
    828 	{ NULL, NULL }
    829 };
    830 const struct vnodeopv_desc efs_vnodeop_opv_desc = {
    831 	&efs_vnodeop_p,
    832 	efs_vnodeop_entries
    833 };
    834