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