xref: /netbsd-src/sys/fs/filecorefs/filecore_vnops.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: filecore_vnops.c,v 1.30 2009/03/14 21:04:23 dsl Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	filecore_vnops.c	1.2	1998/8/18
32  */
33 
34 /*-
35  * Copyright (c) 1998 Andrew McMurry
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	filecore_vnops.c	1.2	1998/8/18
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: filecore_vnops.c,v 1.30 2009/03/14 21:04:23 dsl Exp $");
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/namei.h>
74 #include <sys/resourcevar.h>
75 #include <sys/kernel.h>
76 #include <sys/file.h>
77 #include <sys/stat.h>
78 #include <sys/buf.h>
79 #include <sys/proc.h>
80 #include <sys/conf.h>
81 #include <sys/mount.h>
82 #include <sys/vnode.h>
83 #include <sys/malloc.h>
84 #include <sys/dirent.h>
85 #include <sys/kauth.h>
86 
87 #include <miscfs/genfs/genfs.h>
88 #include <miscfs/specfs/specdev.h>
89 
90 #include <fs/filecorefs/filecore.h>
91 #include <fs/filecorefs/filecore_extern.h>
92 #include <fs/filecorefs/filecore_node.h>
93 
94 /*
95  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
96  * The mode is shifted to select the owner/group/other fields. The
97  * super user is granted all permissions.
98  */
99 int
100 filecore_access(void *v)
101 {
102 	struct vop_access_args /* {
103 		struct vnode *a_vp;
104 		int  a_mode;
105 		kauth_cred_t a_cred;
106 	} */ *ap = v;
107 	struct vnode *vp = ap->a_vp;
108 	struct filecore_node *ip = VTOI(vp);
109 	struct filecore_mnt *fcmp = ip->i_mnt;
110 
111 	/*
112 	 * Disallow write attempts unless the file is a socket,
113 	 * fifo, or a block or character device resident on the
114 	 * file system.
115 	 */
116 	if (ap->a_mode & VWRITE) {
117 		switch (vp->v_type) {
118 		case VDIR:
119 		case VLNK:
120 		case VREG:
121 			return (EROFS);
122 		default:
123 			break;
124 		}
125 	}
126 
127 	return (vaccess(vp->v_type, filecore_mode(ip),
128 	    fcmp->fc_uid, fcmp->fc_gid, ap->a_mode, ap->a_cred));
129 }
130 
131 int
132 filecore_getattr(void *v)
133 {
134 	struct vop_getattr_args /* {
135 		struct vnode *a_vp;
136 		struct vattr *a_vap;
137 		kauth_cred_t a_cred;
138 	} */ *ap = v;
139 	struct vnode *vp = ap->a_vp;
140 	struct filecore_node *ip = VTOI(vp);
141 	struct vattr *vap = ap->a_vap;
142 	struct filecore_mnt *fcmp = ip->i_mnt;
143 
144 	vap->va_fsid	= ip->i_dev;
145 	vap->va_fileid	= ip->i_number;
146 
147 	vap->va_mode	= filecore_mode(ip);
148 	vap->va_nlink	= 1;
149 	vap->va_uid	= fcmp->fc_uid;
150 	vap->va_gid	= fcmp->fc_gid;
151 	vap->va_atime	= filecore_time(ip);
152 	vap->va_mtime	= filecore_time(ip);
153 	vap->va_ctime	= filecore_time(ip);
154 	vap->va_rdev	= 0;  /* We don't support specials */
155 
156 	vap->va_size	= (u_quad_t) ip->i_size;
157 	vap->va_flags	= 0;
158 	vap->va_gen	= 1;
159 	vap->va_blocksize = fcmp->blksize;
160 	vap->va_bytes	= vap->va_size;
161 	vap->va_type	= vp->v_type;
162 	return (0);
163 }
164 
165 /*
166  * Vnode op for reading.
167  */
168 int
169 filecore_read(void *v)
170 {
171 	struct vop_read_args /* {
172 		struct vnode *a_vp;
173 		struct uio *a_uio;
174 		int a_ioflag;
175 		kauth_cred_t a_cred;
176 	} */ *ap = v;
177 	struct vnode *vp = ap->a_vp;
178 	struct uio *uio = ap->a_uio;
179 	struct filecore_node *ip = VTOI(vp);
180 	struct filecore_mnt *fcmp;
181 	struct buf *bp;
182 	daddr_t lbn, rablock;
183 	off_t diff;
184 	int error = 0;
185 	long size, n, on;
186 
187 	if (uio->uio_resid == 0)
188 		return (0);
189 	if (uio->uio_offset < 0)
190 		return (EINVAL);
191 	if (uio->uio_offset >= ip->i_size)
192 		return (0);
193 	ip->i_flag |= IN_ACCESS;
194 	fcmp = ip->i_mnt;
195 
196 	if (vp->v_type == VREG) {
197 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
198 		error = 0;
199 
200 		while (uio->uio_resid > 0) {
201 			vsize_t bytelen = MIN(ip->i_size - uio->uio_offset,
202 					      uio->uio_resid);
203 
204 			if (bytelen == 0) {
205 				break;
206 			}
207 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
208 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
209 			if (error) {
210 				break;
211 			}
212 		}
213 		goto out;
214 	}
215 
216 	do {
217 		lbn = lblkno(fcmp, uio->uio_offset);
218 		on = blkoff(fcmp, uio->uio_offset);
219 		n = MIN(blksize(fcmp, ip, lbn) - on, uio->uio_resid);
220 		diff = (off_t)ip->i_size - uio->uio_offset;
221 		if (diff <= 0)
222 			return (0);
223 		if (diff < n)
224 			n = diff;
225 		size = blksize(fcmp, ip, lbn);
226 		rablock = lbn + 1;
227 		if (ip->i_dirent.attr & FILECORE_ATTR_DIR) {
228 			error = filecore_dbread(ip, &bp);
229 			on = uio->uio_offset;
230 			n = MIN(FILECORE_DIR_SIZE - on, uio->uio_resid);
231 			size = FILECORE_DIR_SIZE;
232 		} else {
233 			error = bread(vp, lbn, size, NOCRED, 0, &bp);
234 #ifdef FILECORE_DEBUG_BR
235 			printf("bread(%p, %llx, %ld, CRED, %p)=%d\n",
236 			    vp, (long long)lbn, size, bp, error);
237 #endif
238 		}
239 		n = MIN(n, size - bp->b_resid);
240 		if (error) {
241 #ifdef FILECORE_DEBUG_BR
242 			printf("brelse(%p) vn1\n", bp);
243 #endif
244 			brelse(bp, 0);
245 			return (error);
246 		}
247 
248 		error = uiomove((char *)(bp->b_data) + on, (int)n, uio);
249 #ifdef FILECORE_DEBUG_BR
250 		printf("brelse(%p) vn2\n", bp);
251 #endif
252 		brelse(bp, 0);
253 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
254 
255 out:
256 	return (error);
257 }
258 
259 /*
260  * Vnode op for readdir
261  */
262 int
263 filecore_readdir(void *v)
264 {
265 	struct vop_readdir_args /* {
266 		struct vnode *a_vp;
267 		struct uio *a_uio;
268 		kauth_cred_t a_cred;
269 		int *a_eofflag;
270 		off_t **a_cookies;
271 		int *a_ncookies;
272 	} */ *ap = v;
273 	struct uio *uio = ap->a_uio;
274 	struct vnode *vdp = ap->a_vp;
275 	struct filecore_node *dp;
276 	struct filecore_mnt *fcmp;
277 	struct buf *bp = NULL;
278 	struct dirent *de;
279 	struct filecore_direntry *dep = NULL;
280 	int error = 0;
281 	off_t *cookies = NULL;
282 	int ncookies = 0;
283 	int i;
284 	off_t uiooff;
285 
286 	dp = VTOI(vdp);
287 
288 	if ((dp->i_dirent.attr & FILECORE_ATTR_DIR) == 0)
289 		return (ENOTDIR);
290 
291 	if (uio->uio_offset % FILECORE_DIRENT_SIZE != 0)
292 		return (EINVAL);
293 	i = uio->uio_offset / FILECORE_DIRENT_SIZE;
294 	uiooff = uio->uio_offset;
295 
296 	*ap->a_eofflag = 0;
297 	fcmp = dp->i_mnt;
298 
299 	error = filecore_dbread(dp, &bp);
300 	if (error) {
301 		brelse(bp, 0);
302 		return error;
303 	}
304 
305 	if (ap->a_ncookies == NULL)
306 		cookies = NULL;
307 	else {
308 		*ap->a_ncookies = 0;
309 		ncookies = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
310 		cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
311 	}
312 
313 	de = malloc(sizeof(struct dirent), M_FILECORETMP, M_WAITOK | M_ZERO);
314 
315 	for (; ; i++) {
316 		switch (i) {
317 		case 0:
318 			/* Fake the '.' entry */
319 			de->d_fileno = dp->i_number;
320 			de->d_type = DT_DIR;
321 			de->d_namlen = 1;
322 			strlcpy(de->d_name, ".", sizeof(de->d_name));
323 			break;
324 		case 1:
325 			/* Fake the '..' entry */
326 			de->d_fileno = filecore_getparent(dp);
327 			de->d_type = DT_DIR;
328 			de->d_namlen = 2;
329 			strlcpy(de->d_name, "..", sizeof(de->d_name));
330 			break;
331 		default:
332 			de->d_fileno = dp->i_dirent.addr +
333 					((i - 2) << FILECORE_INO_INDEX);
334 			dep = fcdirentry(bp->b_data, i - 2);
335 			if (dep->attr & FILECORE_ATTR_DIR)
336 				de->d_type = DT_DIR;
337 			else
338 				de->d_type = DT_REG;
339 			if (filecore_fn2unix(dep->name, de->d_name,
340 /*###346 [cc] warning: passing arg 3 of `filecore_fn2unix' from incompatible pointer type%%%*/
341 			    &de->d_namlen)) {
342 				*ap->a_eofflag = 1;
343 				goto out;
344 			}
345 			break;
346 		}
347 		de->d_reclen = _DIRENT_SIZE(de);
348 		if (uio->uio_resid < de->d_reclen)
349 			goto out;
350 		error = uiomove(de, de->d_reclen, uio);
351 		if (error)
352 			goto out;
353 		uiooff += FILECORE_DIRENT_SIZE;
354 
355 		if (cookies) {
356 			*cookies++ = i*FILECORE_DIRENT_SIZE;
357 			(*ap->a_ncookies)++;
358 			if (--ncookies == 0) goto out;
359 		}
360 	}
361 out:
362 	if (cookies) {
363 		*ap->a_cookies = cookies;
364 		if (error) {
365 			free(cookies, M_TEMP);
366 			*ap->a_ncookies = 0;
367 			*ap->a_cookies = NULL;
368 		}
369 	}
370 	uio->uio_offset = uiooff;
371 
372 #ifdef FILECORE_DEBUG_BR
373 	printf("brelse(%p) vn3\n", bp);
374 #endif
375 	brelse (bp, 0);
376 
377 	free(de, M_FILECORETMP);
378 
379 	return (error);
380 }
381 
382 /*
383  * Return target name of a symbolic link
384  * Shouldn't we get the parent vnode and read the data from there?
385  * This could eventually result in deadlocks in filecore_lookup.
386  * But otherwise the block read here is in the block buffer two times.
387  */
388 int
389 filecore_readlink(void *v)
390 {
391 #if 0
392 	struct vop_readlink_args /* {
393 		struct vnode *a_vp;
394 		struct uio *a_uio;
395 		kauth_cred_t a_cred;
396 	} */ *ap = v;
397 #endif
398 
399 	return (EINVAL);
400 }
401 
402 int
403 filecore_link(void *v)
404 {
405 	struct vop_link_args /* {
406 		struct vnode *a_dvp;
407 		struct vnode *a_vp;
408 		struct componentname *a_cnp;
409 	} */ *ap = v;
410 
411 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
412 	vput(ap->a_dvp);
413 	return (EROFS);
414 }
415 
416 int
417 filecore_symlink(void *v)
418 {
419 	struct vop_symlink_args /* {
420 		struct vnode *a_dvp;
421 		struct vnode **a_vpp;
422 		struct componentname *a_cnp;
423 		struct vattr *a_vap;
424 		char *a_target;
425 	} */ *ap = v;
426 
427 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
428 	vput(ap->a_dvp);
429 	return (EROFS);
430 }
431 
432 /*
433  * Calculate the logical to physical mapping if not done already,
434  * then call the device strategy routine.
435  */
436 int
437 filecore_strategy(void *v)
438 {
439 	struct vop_strategy_args /* {
440 		struct vnode *a_vp;
441 		struct buf *a_bp;
442 	} */ *ap = v;
443 	struct buf *bp = ap->a_bp;
444 	struct vnode *vp = ap->a_vp;
445 	struct filecore_node *ip;
446 	int error;
447 
448 	ip = VTOI(vp);
449 	if (bp->b_blkno == bp->b_lblkno) {
450 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
451 		if (error) {
452 			bp->b_error = error;
453 			biodone(bp);
454 			return (error);
455 		}
456 		if ((long)bp->b_blkno == -1)
457 			clrbuf(bp);
458 	}
459 	if ((long)bp->b_blkno == -1) {
460 		biodone(bp);
461 		return (0);
462 	}
463 	vp = ip->i_devvp;
464 	return (VOP_STRATEGY(vp, bp));
465 }
466 
467 /*
468  * Print out the contents of an inode.
469  */
470 /*ARGSUSED*/
471 int
472 filecore_print(void *v)
473 {
474 
475 	printf("tag VT_FILECORE, filecore vnode\n");
476 	return (0);
477 }
478 
479 /*
480  * Return POSIX pathconf information applicable to filecore filesystems.
481  */
482 int
483 filecore_pathconf(void *v)
484 {
485 	struct vop_pathconf_args /* {
486 		struct vnode *a_vp;
487 		int a_name;
488 		register_t *a_retval;
489 	} */ *ap = v;
490 	switch (ap->a_name) {
491 	case _PC_LINK_MAX:
492 		*ap->a_retval = 1;
493 		return (0);
494 	case _PC_NAME_MAX:
495 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
496 		return (0);
497 	case _PC_PATH_MAX:
498 		*ap->a_retval = 256;
499 		return (0);
500 	case _PC_CHOWN_RESTRICTED:
501 		*ap->a_retval = 1;
502 		return (0);
503 	case _PC_NO_TRUNC:
504 		*ap->a_retval = 1;
505 		return (0);
506 	case _PC_SYNC_IO:
507 		*ap->a_retval = 1;
508 		return (0);
509 	case _PC_FILESIZEBITS:
510 		*ap->a_retval = 32;
511 		return (0);
512 	default:
513 		return (EINVAL);
514 	}
515 	/* NOTREACHED */
516 }
517 
518 /*
519  * Global vfs data structures for isofs
520  */
521 #define	filecore_create	genfs_eopnotsupp
522 #define	filecore_mknod	genfs_eopnotsupp
523 #define	filecore_write	genfs_eopnotsupp
524 #define	filecore_setattr	genfs_eopnotsupp
525 #define	filecore_fcntl	genfs_fcntl
526 #define	filecore_ioctl	genfs_enoioctl
527 #define	filecore_fsync	genfs_nullop
528 #define	filecore_remove	genfs_eopnotsupp
529 #define	filecore_rename	genfs_eopnotsupp
530 #define	filecore_mkdir	genfs_eopnotsupp
531 #define	filecore_rmdir	genfs_eopnotsupp
532 #define	filecore_advlock	genfs_eopnotsupp
533 #define	filecore_bwrite	genfs_eopnotsupp
534 #define filecore_revoke	genfs_revoke
535 
536 /*
537  * Global vfs data structures for filecore
538  */
539 int (**filecore_vnodeop_p)(void *);
540 const struct vnodeopv_entry_desc filecore_vnodeop_entries[] = {
541 	{ &vop_default_desc, vn_default_error },
542 	{ &vop_lookup_desc, filecore_lookup },		/* lookup */
543 	{ &vop_create_desc, filecore_create },		/* create */
544 	{ &vop_mknod_desc, filecore_mknod },		/* mknod */
545 	{ &vop_open_desc, filecore_open },		/* open */
546 	{ &vop_close_desc, filecore_close },		/* close */
547 	{ &vop_access_desc, filecore_access },		/* access */
548 	{ &vop_getattr_desc, filecore_getattr },	/* getattr */
549 	{ &vop_setattr_desc, filecore_setattr },	/* setattr */
550 	{ &vop_read_desc, filecore_read },		/* read */
551 	{ &vop_write_desc, filecore_write },		/* write */
552 	{ &vop_fcntl_desc, filecore_fcntl },		/* fcntl */
553 	{ &vop_ioctl_desc, filecore_ioctl },		/* ioctl */
554 	{ &vop_poll_desc, filecore_poll },		/* poll */
555 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
556 	{ &vop_revoke_desc, filecore_revoke },		/* revoke */
557 	{ &vop_mmap_desc, filecore_mmap },		/* mmap */
558 	{ &vop_fsync_desc, filecore_fsync },		/* fsync */
559 	{ &vop_seek_desc, filecore_seek },		/* seek */
560 	{ &vop_remove_desc, filecore_remove },		/* remove */
561 	{ &vop_link_desc, filecore_link },		/* link */
562 	{ &vop_rename_desc, filecore_rename },		/* rename */
563 	{ &vop_mkdir_desc, filecore_mkdir },		/* mkdir */
564 	{ &vop_rmdir_desc, filecore_rmdir },		/* rmdir */
565 	{ &vop_symlink_desc, filecore_symlink },	/* symlink */
566 	{ &vop_readdir_desc, filecore_readdir },      	/* readdir */
567 	{ &vop_readlink_desc, filecore_readlink },	/* readlink */
568 	{ &vop_abortop_desc, filecore_abortop },       	/* abortop */
569 	{ &vop_inactive_desc, filecore_inactive },	/* inactive */
570 	{ &vop_reclaim_desc, filecore_reclaim },       	/* reclaim */
571 	{ &vop_lock_desc, genfs_lock },			/* lock */
572 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
573 	{ &vop_bmap_desc, filecore_bmap },		/* bmap */
574 	{ &vop_strategy_desc, filecore_strategy },	/* strategy */
575 	{ &vop_print_desc, filecore_print },		/* print */
576 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
577 	{ &vop_pathconf_desc, filecore_pathconf },	/* pathconf */
578 	{ &vop_advlock_desc, filecore_advlock },       	/* advlock */
579 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
580 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
581 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
582 	{ NULL, NULL }
583 };
584 const struct vnodeopv_desc filecore_vnodeop_opv_desc =
585 	{ &filecore_vnodeop_p, filecore_vnodeop_entries };
586