xref: /netbsd-src/sys/fs/filecorefs/filecore_vnops.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: filecore_vnops.c,v 1.28 2008/11/26 20:17:33 pooka 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.28 2008/11/26 20:17:33 pooka 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(v)
101 	void *v;
102 {
103 	struct vop_access_args /* {
104 		struct vnode *a_vp;
105 		int  a_mode;
106 		kauth_cred_t a_cred;
107 	} */ *ap = v;
108 	struct vnode *vp = ap->a_vp;
109 	struct filecore_node *ip = VTOI(vp);
110 	struct filecore_mnt *fcmp = ip->i_mnt;
111 
112 	/*
113 	 * Disallow write attempts unless the file is a socket,
114 	 * fifo, or a block or character device resident on the
115 	 * file system.
116 	 */
117 	if (ap->a_mode & VWRITE) {
118 		switch (vp->v_type) {
119 		case VDIR:
120 		case VLNK:
121 		case VREG:
122 			return (EROFS);
123 		default:
124 			break;
125 		}
126 	}
127 
128 	return (vaccess(vp->v_type, filecore_mode(ip),
129 	    fcmp->fc_uid, fcmp->fc_gid, ap->a_mode, ap->a_cred));
130 }
131 
132 int
133 filecore_getattr(v)
134 	void *v;
135 {
136 	struct vop_getattr_args /* {
137 		struct vnode *a_vp;
138 		struct vattr *a_vap;
139 		kauth_cred_t a_cred;
140 	} */ *ap = v;
141 	struct vnode *vp = ap->a_vp;
142 	struct filecore_node *ip = VTOI(vp);
143 	struct vattr *vap = ap->a_vap;
144 	struct filecore_mnt *fcmp = ip->i_mnt;
145 
146 	vap->va_fsid	= ip->i_dev;
147 	vap->va_fileid	= ip->i_number;
148 
149 	vap->va_mode	= filecore_mode(ip);
150 	vap->va_nlink	= 1;
151 	vap->va_uid	= fcmp->fc_uid;
152 	vap->va_gid	= fcmp->fc_gid;
153 	vap->va_atime	= filecore_time(ip);
154 	vap->va_mtime	= filecore_time(ip);
155 	vap->va_ctime	= filecore_time(ip);
156 	vap->va_rdev	= 0;  /* We don't support specials */
157 
158 	vap->va_size	= (u_quad_t) ip->i_size;
159 	vap->va_flags	= 0;
160 	vap->va_gen	= 1;
161 	vap->va_blocksize = fcmp->blksize;
162 	vap->va_bytes	= vap->va_size;
163 	vap->va_type	= vp->v_type;
164 	return (0);
165 }
166 
167 /*
168  * Vnode op for reading.
169  */
170 int
171 filecore_read(v)
172 	void *v;
173 {
174 	struct vop_read_args /* {
175 		struct vnode *a_vp;
176 		struct uio *a_uio;
177 		int a_ioflag;
178 		kauth_cred_t a_cred;
179 	} */ *ap = v;
180 	struct vnode *vp = ap->a_vp;
181 	struct uio *uio = ap->a_uio;
182 	struct filecore_node *ip = VTOI(vp);
183 	struct filecore_mnt *fcmp;
184 	struct buf *bp;
185 	daddr_t lbn, rablock;
186 	off_t diff;
187 	int error = 0;
188 	long size, n, on;
189 
190 	if (uio->uio_resid == 0)
191 		return (0);
192 	if (uio->uio_offset < 0)
193 		return (EINVAL);
194 	if (uio->uio_offset >= ip->i_size)
195 		return (0);
196 	ip->i_flag |= IN_ACCESS;
197 	fcmp = ip->i_mnt;
198 
199 	if (vp->v_type == VREG) {
200 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
201 		error = 0;
202 
203 		while (uio->uio_resid > 0) {
204 			vsize_t bytelen = MIN(ip->i_size - uio->uio_offset,
205 					      uio->uio_resid);
206 
207 			if (bytelen == 0) {
208 				break;
209 			}
210 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
211 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
212 			if (error) {
213 				break;
214 			}
215 		}
216 		goto out;
217 	}
218 
219 	do {
220 		lbn = lblkno(fcmp, uio->uio_offset);
221 		on = blkoff(fcmp, uio->uio_offset);
222 		n = MIN(blksize(fcmp, ip, lbn) - on, uio->uio_resid);
223 		diff = (off_t)ip->i_size - uio->uio_offset;
224 		if (diff <= 0)
225 			return (0);
226 		if (diff < n)
227 			n = diff;
228 		size = blksize(fcmp, ip, lbn);
229 		rablock = lbn + 1;
230 		if (ip->i_dirent.attr & FILECORE_ATTR_DIR) {
231 			error = filecore_dbread(ip, &bp);
232 			on = uio->uio_offset;
233 			n = MIN(FILECORE_DIR_SIZE - on, uio->uio_resid);
234 			size = FILECORE_DIR_SIZE;
235 		} else {
236 			error = bread(vp, lbn, size, NOCRED, 0, &bp);
237 #ifdef FILECORE_DEBUG_BR
238 			printf("bread(%p, %llx, %ld, CRED, %p)=%d\n",
239 			    vp, (long long)lbn, size, bp, error);
240 #endif
241 		}
242 		n = MIN(n, size - bp->b_resid);
243 		if (error) {
244 #ifdef FILECORE_DEBUG_BR
245 			printf("brelse(%p) vn1\n", bp);
246 #endif
247 			brelse(bp, 0);
248 			return (error);
249 		}
250 
251 		error = uiomove((char *)(bp->b_data) + on, (int)n, uio);
252 #ifdef FILECORE_DEBUG_BR
253 		printf("brelse(%p) vn2\n", bp);
254 #endif
255 		brelse(bp, 0);
256 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
257 
258 out:
259 	return (error);
260 }
261 
262 /*
263  * Vnode op for readdir
264  */
265 int
266 filecore_readdir(v)
267 	void *v;
268 {
269 	struct vop_readdir_args /* {
270 		struct vnode *a_vp;
271 		struct uio *a_uio;
272 		kauth_cred_t a_cred;
273 		int *a_eofflag;
274 		off_t **a_cookies;
275 		int *a_ncookies;
276 	} */ *ap = v;
277 	struct uio *uio = ap->a_uio;
278 	struct vnode *vdp = ap->a_vp;
279 	struct filecore_node *dp;
280 	struct filecore_mnt *fcmp;
281 	struct buf *bp = NULL;
282 	struct dirent *de;
283 	struct filecore_direntry *dep = NULL;
284 	int error = 0;
285 	off_t *cookies = NULL;
286 	int ncookies = 0;
287 	int i;
288 	off_t uiooff;
289 
290 	dp = VTOI(vdp);
291 
292 	if ((dp->i_dirent.attr & FILECORE_ATTR_DIR) == 0)
293 		return (ENOTDIR);
294 
295 	if (uio->uio_offset % FILECORE_DIRENT_SIZE != 0)
296 		return (EINVAL);
297 	i = uio->uio_offset / FILECORE_DIRENT_SIZE;
298 	uiooff = uio->uio_offset;
299 
300 	*ap->a_eofflag = 0;
301 	fcmp = dp->i_mnt;
302 
303 	error = filecore_dbread(dp, &bp);
304 	if (error) {
305 		brelse(bp, 0);
306 		return error;
307 	}
308 
309 	if (ap->a_ncookies == NULL)
310 		cookies = NULL;
311 	else {
312 		*ap->a_ncookies = 0;
313 		ncookies = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
314 		cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
315 	}
316 
317 	de = malloc(sizeof(struct dirent), M_FILECORETMP, M_WAITOK | M_ZERO);
318 
319 	for (; ; i++) {
320 		switch (i) {
321 		case 0:
322 			/* Fake the '.' entry */
323 			de->d_fileno = dp->i_number;
324 			de->d_type = DT_DIR;
325 			de->d_namlen = 1;
326 			strlcpy(de->d_name, ".", sizeof(de->d_name));
327 			break;
328 		case 1:
329 			/* Fake the '..' entry */
330 			de->d_fileno = filecore_getparent(dp);
331 			de->d_type = DT_DIR;
332 			de->d_namlen = 2;
333 			strlcpy(de->d_name, "..", sizeof(de->d_name));
334 			break;
335 		default:
336 			de->d_fileno = dp->i_dirent.addr +
337 					((i - 2) << FILECORE_INO_INDEX);
338 			dep = fcdirentry(bp->b_data, i - 2);
339 			if (dep->attr & FILECORE_ATTR_DIR)
340 				de->d_type = DT_DIR;
341 			else
342 				de->d_type = DT_REG;
343 			if (filecore_fn2unix(dep->name, de->d_name,
344 /*###346 [cc] warning: passing arg 3 of `filecore_fn2unix' from incompatible pointer type%%%*/
345 			    &de->d_namlen)) {
346 				*ap->a_eofflag = 1;
347 				goto out;
348 			}
349 			break;
350 		}
351 		de->d_reclen = _DIRENT_SIZE(de);
352 		if (uio->uio_resid < de->d_reclen)
353 			goto out;
354 		error = uiomove(de, de->d_reclen, uio);
355 		if (error)
356 			goto out;
357 		uiooff += FILECORE_DIRENT_SIZE;
358 
359 		if (cookies) {
360 			*cookies++ = i*FILECORE_DIRENT_SIZE;
361 			(*ap->a_ncookies)++;
362 			if (--ncookies == 0) goto out;
363 		}
364 	}
365 out:
366 	if (cookies) {
367 		*ap->a_cookies = cookies;
368 		if (error) {
369 			free(cookies, M_TEMP);
370 			*ap->a_ncookies = 0;
371 			*ap->a_cookies = NULL;
372 		}
373 	}
374 	uio->uio_offset = uiooff;
375 
376 #ifdef FILECORE_DEBUG_BR
377 	printf("brelse(%p) vn3\n", bp);
378 #endif
379 	brelse (bp, 0);
380 
381 	free(de, M_FILECORETMP);
382 
383 	return (error);
384 }
385 
386 /*
387  * Return target name of a symbolic link
388  * Shouldn't we get the parent vnode and read the data from there?
389  * This could eventually result in deadlocks in filecore_lookup.
390  * But otherwise the block read here is in the block buffer two times.
391  */
392 int
393 filecore_readlink(v)
394 	void *v;
395 {
396 #if 0
397 	struct vop_readlink_args /* {
398 		struct vnode *a_vp;
399 		struct uio *a_uio;
400 		kauth_cred_t a_cred;
401 	} */ *ap = v;
402 #endif
403 
404 	return (EINVAL);
405 }
406 
407 int
408 filecore_link(v)
409 	void *v;
410 {
411 	struct vop_link_args /* {
412 		struct vnode *a_dvp;
413 		struct vnode *a_vp;
414 		struct componentname *a_cnp;
415 	} */ *ap = v;
416 
417 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
418 	vput(ap->a_dvp);
419 	return (EROFS);
420 }
421 
422 int
423 filecore_symlink(v)
424 	void *v;
425 {
426 	struct vop_symlink_args /* {
427 		struct vnode *a_dvp;
428 		struct vnode **a_vpp;
429 		struct componentname *a_cnp;
430 		struct vattr *a_vap;
431 		char *a_target;
432 	} */ *ap = v;
433 
434 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
435 	vput(ap->a_dvp);
436 	return (EROFS);
437 }
438 
439 /*
440  * Calculate the logical to physical mapping if not done already,
441  * then call the device strategy routine.
442  */
443 int
444 filecore_strategy(v)
445 	void *v;
446 {
447 	struct vop_strategy_args /* {
448 		struct vnode *a_vp;
449 		struct buf *a_bp;
450 	} */ *ap = v;
451 	struct buf *bp = ap->a_bp;
452 	struct vnode *vp = ap->a_vp;
453 	struct filecore_node *ip;
454 	int error;
455 
456 	ip = VTOI(vp);
457 	if (bp->b_blkno == bp->b_lblkno) {
458 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
459 		if (error) {
460 			bp->b_error = error;
461 			biodone(bp);
462 			return (error);
463 		}
464 		if ((long)bp->b_blkno == -1)
465 			clrbuf(bp);
466 	}
467 	if ((long)bp->b_blkno == -1) {
468 		biodone(bp);
469 		return (0);
470 	}
471 	vp = ip->i_devvp;
472 	return (VOP_STRATEGY(vp, bp));
473 }
474 
475 /*
476  * Print out the contents of an inode.
477  */
478 /*ARGSUSED*/
479 int
480 filecore_print(v)
481 	void *v;
482 {
483 
484 	printf("tag VT_FILECORE, filecore vnode\n");
485 	return (0);
486 }
487 
488 /*
489  * Return POSIX pathconf information applicable to filecore filesystems.
490  */
491 int
492 filecore_pathconf(v)
493 	void *v;
494 {
495 	struct vop_pathconf_args /* {
496 		struct vnode *a_vp;
497 		int a_name;
498 		register_t *a_retval;
499 	} */ *ap = v;
500 	switch (ap->a_name) {
501 	case _PC_LINK_MAX:
502 		*ap->a_retval = 1;
503 		return (0);
504 	case _PC_NAME_MAX:
505 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
506 		return (0);
507 	case _PC_PATH_MAX:
508 		*ap->a_retval = 256;
509 		return (0);
510 	case _PC_CHOWN_RESTRICTED:
511 		*ap->a_retval = 1;
512 		return (0);
513 	case _PC_NO_TRUNC:
514 		*ap->a_retval = 1;
515 		return (0);
516 	case _PC_SYNC_IO:
517 		*ap->a_retval = 1;
518 		return (0);
519 	case _PC_FILESIZEBITS:
520 		*ap->a_retval = 32;
521 		return (0);
522 	default:
523 		return (EINVAL);
524 	}
525 	/* NOTREACHED */
526 }
527 
528 /*
529  * Global vfs data structures for isofs
530  */
531 #define	filecore_create	genfs_eopnotsupp
532 #define	filecore_mknod	genfs_eopnotsupp
533 #define	filecore_write	genfs_eopnotsupp
534 #define	filecore_setattr	genfs_eopnotsupp
535 #define	filecore_fcntl	genfs_fcntl
536 #define	filecore_ioctl	genfs_enoioctl
537 #define	filecore_fsync	genfs_nullop
538 #define	filecore_remove	genfs_eopnotsupp
539 #define	filecore_rename	genfs_eopnotsupp
540 #define	filecore_mkdir	genfs_eopnotsupp
541 #define	filecore_rmdir	genfs_eopnotsupp
542 #define	filecore_advlock	genfs_eopnotsupp
543 #define	filecore_bwrite	genfs_eopnotsupp
544 #define filecore_revoke	genfs_revoke
545 
546 /*
547  * Global vfs data structures for filecore
548  */
549 int (**filecore_vnodeop_p) __P((void *));
550 const struct vnodeopv_entry_desc filecore_vnodeop_entries[] = {
551 	{ &vop_default_desc, vn_default_error },
552 	{ &vop_lookup_desc, filecore_lookup },		/* lookup */
553 	{ &vop_create_desc, filecore_create },		/* create */
554 	{ &vop_mknod_desc, filecore_mknod },		/* mknod */
555 	{ &vop_open_desc, filecore_open },		/* open */
556 	{ &vop_close_desc, filecore_close },		/* close */
557 	{ &vop_access_desc, filecore_access },		/* access */
558 	{ &vop_getattr_desc, filecore_getattr },	/* getattr */
559 	{ &vop_setattr_desc, filecore_setattr },	/* setattr */
560 	{ &vop_read_desc, filecore_read },		/* read */
561 	{ &vop_write_desc, filecore_write },		/* write */
562 	{ &vop_fcntl_desc, filecore_fcntl },		/* fcntl */
563 	{ &vop_ioctl_desc, filecore_ioctl },		/* ioctl */
564 	{ &vop_poll_desc, filecore_poll },		/* poll */
565 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
566 	{ &vop_revoke_desc, filecore_revoke },		/* revoke */
567 	{ &vop_mmap_desc, filecore_mmap },		/* mmap */
568 	{ &vop_fsync_desc, filecore_fsync },		/* fsync */
569 	{ &vop_seek_desc, filecore_seek },		/* seek */
570 	{ &vop_remove_desc, filecore_remove },		/* remove */
571 	{ &vop_link_desc, filecore_link },		/* link */
572 	{ &vop_rename_desc, filecore_rename },		/* rename */
573 	{ &vop_mkdir_desc, filecore_mkdir },		/* mkdir */
574 	{ &vop_rmdir_desc, filecore_rmdir },		/* rmdir */
575 	{ &vop_symlink_desc, filecore_symlink },	/* symlink */
576 	{ &vop_readdir_desc, filecore_readdir },      	/* readdir */
577 	{ &vop_readlink_desc, filecore_readlink },	/* readlink */
578 	{ &vop_abortop_desc, filecore_abortop },       	/* abortop */
579 	{ &vop_inactive_desc, filecore_inactive },	/* inactive */
580 	{ &vop_reclaim_desc, filecore_reclaim },       	/* reclaim */
581 	{ &vop_lock_desc, genfs_lock },			/* lock */
582 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
583 	{ &vop_bmap_desc, filecore_bmap },		/* bmap */
584 	{ &vop_strategy_desc, filecore_strategy },	/* strategy */
585 	{ &vop_print_desc, filecore_print },		/* print */
586 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
587 	{ &vop_pathconf_desc, filecore_pathconf },	/* pathconf */
588 	{ &vop_advlock_desc, filecore_advlock },       	/* advlock */
589 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
590 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
591 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
592 	{ NULL, NULL }
593 };
594 const struct vnodeopv_desc filecore_vnodeop_opv_desc =
595 	{ &filecore_vnodeop_p, filecore_vnodeop_entries };
596