No empty .Rs/.Re
[netbsd-mini2440.git] / sys / fs / filecorefs / filecore_vnops.c
blob73fdc55eb92440d7fd4b78d88f7165853d35a4ab
1 /* $NetBSD: filecore_vnops.c,v 1.31 2009/06/23 19:36:39 elad Exp $ */
3 /*-
4 * Copyright (c) 1994 The Regents of the University of California.
5 * All rights reserved.
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.
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.
31 * filecore_vnops.c 1.2 1998/8/18
34 /*-
35 * Copyright (c) 1998 Andrew McMurry
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.
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.
65 * filecore_vnops.c 1.2 1998/8/18
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: filecore_vnops.c,v 1.31 2009/06/23 19:36:39 elad Exp $");
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>
87 #include <miscfs/genfs/genfs.h>
88 #include <miscfs/specfs/specdev.h>
90 #include <fs/filecorefs/filecore.h>
91 #include <fs/filecorefs/filecore_extern.h>
92 #include <fs/filecorefs/filecore_node.h>
94 static int
95 filecore_check_possible(struct vnode *vp, struct filecore_node *ip,
96 mode_t mode)
100 * Disallow write attempts unless the file is a socket,
101 * fifo, or a block or character device resident on the
102 * file system.
104 if (mode & VWRITE) {
105 switch (vp->v_type) {
106 case VDIR:
107 case VLNK:
108 case VREG:
109 return (EROFS);
110 default:
111 break;
115 return 0;
119 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
120 * The mode is shifted to select the owner/group/other fields. The
121 * super user is granted all permissions.
123 static int
124 filecore_check_permitted(struct vnode *vp, struct filecore_node *ip,
125 mode_t mode, kauth_cred_t cred)
127 struct filecore_mnt *fcmp = ip->i_mnt;
129 return genfs_can_access(vp->v_type, filecore_mode(ip),
130 fcmp->fc_uid, fcmp->fc_gid, mode, cred);
134 filecore_access(void *v)
136 struct vop_access_args /* {
137 struct vnode *a_vp;
138 int a_mode;
139 kauth_cred_t a_cred;
140 } */ *ap = v;
141 struct vnode *vp = ap->a_vp;
142 struct filecore_node *ip = VTOI(vp);
143 int error;
145 error = filecore_check_possible(vp, ip, ap->a_mode);
146 if (error)
147 return error;
149 error = filecore_check_permitted(vp, ip, ap->a_mode, ap->a_cred);
151 return error;
155 filecore_getattr(void *v)
157 struct vop_getattr_args /* {
158 struct vnode *a_vp;
159 struct vattr *a_vap;
160 kauth_cred_t a_cred;
161 } */ *ap = v;
162 struct vnode *vp = ap->a_vp;
163 struct filecore_node *ip = VTOI(vp);
164 struct vattr *vap = ap->a_vap;
165 struct filecore_mnt *fcmp = ip->i_mnt;
167 vap->va_fsid = ip->i_dev;
168 vap->va_fileid = ip->i_number;
170 vap->va_mode = filecore_mode(ip);
171 vap->va_nlink = 1;
172 vap->va_uid = fcmp->fc_uid;
173 vap->va_gid = fcmp->fc_gid;
174 vap->va_atime = filecore_time(ip);
175 vap->va_mtime = filecore_time(ip);
176 vap->va_ctime = filecore_time(ip);
177 vap->va_rdev = 0; /* We don't support specials */
179 vap->va_size = (u_quad_t) ip->i_size;
180 vap->va_flags = 0;
181 vap->va_gen = 1;
182 vap->va_blocksize = fcmp->blksize;
183 vap->va_bytes = vap->va_size;
184 vap->va_type = vp->v_type;
185 return (0);
189 * Vnode op for reading.
192 filecore_read(void *v)
194 struct vop_read_args /* {
195 struct vnode *a_vp;
196 struct uio *a_uio;
197 int a_ioflag;
198 kauth_cred_t a_cred;
199 } */ *ap = v;
200 struct vnode *vp = ap->a_vp;
201 struct uio *uio = ap->a_uio;
202 struct filecore_node *ip = VTOI(vp);
203 struct filecore_mnt *fcmp;
204 struct buf *bp;
205 daddr_t lbn, rablock;
206 off_t diff;
207 int error = 0;
208 long size, n, on;
210 if (uio->uio_resid == 0)
211 return (0);
212 if (uio->uio_offset < 0)
213 return (EINVAL);
214 if (uio->uio_offset >= ip->i_size)
215 return (0);
216 ip->i_flag |= IN_ACCESS;
217 fcmp = ip->i_mnt;
219 if (vp->v_type == VREG) {
220 const int advice = IO_ADV_DECODE(ap->a_ioflag);
221 error = 0;
223 while (uio->uio_resid > 0) {
224 vsize_t bytelen = MIN(ip->i_size - uio->uio_offset,
225 uio->uio_resid);
227 if (bytelen == 0) {
228 break;
230 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
231 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
232 if (error) {
233 break;
236 goto out;
239 do {
240 lbn = lblkno(fcmp, uio->uio_offset);
241 on = blkoff(fcmp, uio->uio_offset);
242 n = MIN(blksize(fcmp, ip, lbn) - on, uio->uio_resid);
243 diff = (off_t)ip->i_size - uio->uio_offset;
244 if (diff <= 0)
245 return (0);
246 if (diff < n)
247 n = diff;
248 size = blksize(fcmp, ip, lbn);
249 rablock = lbn + 1;
250 if (ip->i_dirent.attr & FILECORE_ATTR_DIR) {
251 error = filecore_dbread(ip, &bp);
252 on = uio->uio_offset;
253 n = MIN(FILECORE_DIR_SIZE - on, uio->uio_resid);
254 size = FILECORE_DIR_SIZE;
255 } else {
256 error = bread(vp, lbn, size, NOCRED, 0, &bp);
257 #ifdef FILECORE_DEBUG_BR
258 printf("bread(%p, %llx, %ld, CRED, %p)=%d\n",
259 vp, (long long)lbn, size, bp, error);
260 #endif
262 n = MIN(n, size - bp->b_resid);
263 if (error) {
264 #ifdef FILECORE_DEBUG_BR
265 printf("brelse(%p) vn1\n", bp);
266 #endif
267 brelse(bp, 0);
268 return (error);
271 error = uiomove((char *)(bp->b_data) + on, (int)n, uio);
272 #ifdef FILECORE_DEBUG_BR
273 printf("brelse(%p) vn2\n", bp);
274 #endif
275 brelse(bp, 0);
276 } while (error == 0 && uio->uio_resid > 0 && n != 0);
278 out:
279 return (error);
283 * Vnode op for readdir
286 filecore_readdir(void *v)
288 struct vop_readdir_args /* {
289 struct vnode *a_vp;
290 struct uio *a_uio;
291 kauth_cred_t a_cred;
292 int *a_eofflag;
293 off_t **a_cookies;
294 int *a_ncookies;
295 } */ *ap = v;
296 struct uio *uio = ap->a_uio;
297 struct vnode *vdp = ap->a_vp;
298 struct filecore_node *dp;
299 struct filecore_mnt *fcmp;
300 struct buf *bp = NULL;
301 struct dirent *de;
302 struct filecore_direntry *dep = NULL;
303 int error = 0;
304 off_t *cookies = NULL;
305 int ncookies = 0;
306 int i;
307 off_t uiooff;
309 dp = VTOI(vdp);
311 if ((dp->i_dirent.attr & FILECORE_ATTR_DIR) == 0)
312 return (ENOTDIR);
314 if (uio->uio_offset % FILECORE_DIRENT_SIZE != 0)
315 return (EINVAL);
316 i = uio->uio_offset / FILECORE_DIRENT_SIZE;
317 uiooff = uio->uio_offset;
319 *ap->a_eofflag = 0;
320 fcmp = dp->i_mnt;
322 error = filecore_dbread(dp, &bp);
323 if (error) {
324 brelse(bp, 0);
325 return error;
328 if (ap->a_ncookies == NULL)
329 cookies = NULL;
330 else {
331 *ap->a_ncookies = 0;
332 ncookies = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
333 cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
336 de = malloc(sizeof(struct dirent), M_FILECORETMP, M_WAITOK | M_ZERO);
338 for (; ; i++) {
339 switch (i) {
340 case 0:
341 /* Fake the '.' entry */
342 de->d_fileno = dp->i_number;
343 de->d_type = DT_DIR;
344 de->d_namlen = 1;
345 strlcpy(de->d_name, ".", sizeof(de->d_name));
346 break;
347 case 1:
348 /* Fake the '..' entry */
349 de->d_fileno = filecore_getparent(dp);
350 de->d_type = DT_DIR;
351 de->d_namlen = 2;
352 strlcpy(de->d_name, "..", sizeof(de->d_name));
353 break;
354 default:
355 de->d_fileno = dp->i_dirent.addr +
356 ((i - 2) << FILECORE_INO_INDEX);
357 dep = fcdirentry(bp->b_data, i - 2);
358 if (dep->attr & FILECORE_ATTR_DIR)
359 de->d_type = DT_DIR;
360 else
361 de->d_type = DT_REG;
362 if (filecore_fn2unix(dep->name, de->d_name,
363 /*###346 [cc] warning: passing arg 3 of `filecore_fn2unix' from incompatible pointer type%%%*/
364 &de->d_namlen)) {
365 *ap->a_eofflag = 1;
366 goto out;
368 break;
370 de->d_reclen = _DIRENT_SIZE(de);
371 if (uio->uio_resid < de->d_reclen)
372 goto out;
373 error = uiomove(de, de->d_reclen, uio);
374 if (error)
375 goto out;
376 uiooff += FILECORE_DIRENT_SIZE;
378 if (cookies) {
379 *cookies++ = i*FILECORE_DIRENT_SIZE;
380 (*ap->a_ncookies)++;
381 if (--ncookies == 0) goto out;
384 out:
385 if (cookies) {
386 *ap->a_cookies = cookies;
387 if (error) {
388 free(cookies, M_TEMP);
389 *ap->a_ncookies = 0;
390 *ap->a_cookies = NULL;
393 uio->uio_offset = uiooff;
395 #ifdef FILECORE_DEBUG_BR
396 printf("brelse(%p) vn3\n", bp);
397 #endif
398 brelse (bp, 0);
400 free(de, M_FILECORETMP);
402 return (error);
406 * Return target name of a symbolic link
407 * Shouldn't we get the parent vnode and read the data from there?
408 * This could eventually result in deadlocks in filecore_lookup.
409 * But otherwise the block read here is in the block buffer two times.
412 filecore_readlink(void *v)
414 #if 0
415 struct vop_readlink_args /* {
416 struct vnode *a_vp;
417 struct uio *a_uio;
418 kauth_cred_t a_cred;
419 } */ *ap = v;
420 #endif
422 return (EINVAL);
426 filecore_link(void *v)
428 struct vop_link_args /* {
429 struct vnode *a_dvp;
430 struct vnode *a_vp;
431 struct componentname *a_cnp;
432 } */ *ap = v;
434 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
435 vput(ap->a_dvp);
436 return (EROFS);
440 filecore_symlink(void *v)
442 struct vop_symlink_args /* {
443 struct vnode *a_dvp;
444 struct vnode **a_vpp;
445 struct componentname *a_cnp;
446 struct vattr *a_vap;
447 char *a_target;
448 } */ *ap = v;
450 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
451 vput(ap->a_dvp);
452 return (EROFS);
456 * Calculate the logical to physical mapping if not done already,
457 * then call the device strategy routine.
460 filecore_strategy(void *v)
462 struct vop_strategy_args /* {
463 struct vnode *a_vp;
464 struct buf *a_bp;
465 } */ *ap = v;
466 struct buf *bp = ap->a_bp;
467 struct vnode *vp = ap->a_vp;
468 struct filecore_node *ip;
469 int error;
471 ip = VTOI(vp);
472 if (bp->b_blkno == bp->b_lblkno) {
473 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
474 if (error) {
475 bp->b_error = error;
476 biodone(bp);
477 return (error);
479 if ((long)bp->b_blkno == -1)
480 clrbuf(bp);
482 if ((long)bp->b_blkno == -1) {
483 biodone(bp);
484 return (0);
486 vp = ip->i_devvp;
487 return (VOP_STRATEGY(vp, bp));
491 * Print out the contents of an inode.
493 /*ARGSUSED*/
495 filecore_print(void *v)
498 printf("tag VT_FILECORE, filecore vnode\n");
499 return (0);
503 * Return POSIX pathconf information applicable to filecore filesystems.
506 filecore_pathconf(void *v)
508 struct vop_pathconf_args /* {
509 struct vnode *a_vp;
510 int a_name;
511 register_t *a_retval;
512 } */ *ap = v;
513 switch (ap->a_name) {
514 case _PC_LINK_MAX:
515 *ap->a_retval = 1;
516 return (0);
517 case _PC_NAME_MAX:
518 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
519 return (0);
520 case _PC_PATH_MAX:
521 *ap->a_retval = 256;
522 return (0);
523 case _PC_CHOWN_RESTRICTED:
524 *ap->a_retval = 1;
525 return (0);
526 case _PC_NO_TRUNC:
527 *ap->a_retval = 1;
528 return (0);
529 case _PC_SYNC_IO:
530 *ap->a_retval = 1;
531 return (0);
532 case _PC_FILESIZEBITS:
533 *ap->a_retval = 32;
534 return (0);
535 default:
536 return (EINVAL);
538 /* NOTREACHED */
542 * Global vfs data structures for isofs
544 #define filecore_create genfs_eopnotsupp
545 #define filecore_mknod genfs_eopnotsupp
546 #define filecore_write genfs_eopnotsupp
547 #define filecore_setattr genfs_eopnotsupp
548 #define filecore_fcntl genfs_fcntl
549 #define filecore_ioctl genfs_enoioctl
550 #define filecore_fsync genfs_nullop
551 #define filecore_remove genfs_eopnotsupp
552 #define filecore_rename genfs_eopnotsupp
553 #define filecore_mkdir genfs_eopnotsupp
554 #define filecore_rmdir genfs_eopnotsupp
555 #define filecore_advlock genfs_eopnotsupp
556 #define filecore_bwrite genfs_eopnotsupp
557 #define filecore_revoke genfs_revoke
560 * Global vfs data structures for filecore
562 int (**filecore_vnodeop_p)(void *);
563 const struct vnodeopv_entry_desc filecore_vnodeop_entries[] = {
564 { &vop_default_desc, vn_default_error },
565 { &vop_lookup_desc, filecore_lookup }, /* lookup */
566 { &vop_create_desc, filecore_create }, /* create */
567 { &vop_mknod_desc, filecore_mknod }, /* mknod */
568 { &vop_open_desc, filecore_open }, /* open */
569 { &vop_close_desc, filecore_close }, /* close */
570 { &vop_access_desc, filecore_access }, /* access */
571 { &vop_getattr_desc, filecore_getattr }, /* getattr */
572 { &vop_setattr_desc, filecore_setattr }, /* setattr */
573 { &vop_read_desc, filecore_read }, /* read */
574 { &vop_write_desc, filecore_write }, /* write */
575 { &vop_fcntl_desc, filecore_fcntl }, /* fcntl */
576 { &vop_ioctl_desc, filecore_ioctl }, /* ioctl */
577 { &vop_poll_desc, filecore_poll }, /* poll */
578 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
579 { &vop_revoke_desc, filecore_revoke }, /* revoke */
580 { &vop_mmap_desc, filecore_mmap }, /* mmap */
581 { &vop_fsync_desc, filecore_fsync }, /* fsync */
582 { &vop_seek_desc, filecore_seek }, /* seek */
583 { &vop_remove_desc, filecore_remove }, /* remove */
584 { &vop_link_desc, filecore_link }, /* link */
585 { &vop_rename_desc, filecore_rename }, /* rename */
586 { &vop_mkdir_desc, filecore_mkdir }, /* mkdir */
587 { &vop_rmdir_desc, filecore_rmdir }, /* rmdir */
588 { &vop_symlink_desc, filecore_symlink }, /* symlink */
589 { &vop_readdir_desc, filecore_readdir }, /* readdir */
590 { &vop_readlink_desc, filecore_readlink }, /* readlink */
591 { &vop_abortop_desc, filecore_abortop }, /* abortop */
592 { &vop_inactive_desc, filecore_inactive }, /* inactive */
593 { &vop_reclaim_desc, filecore_reclaim }, /* reclaim */
594 { &vop_lock_desc, genfs_lock }, /* lock */
595 { &vop_unlock_desc, genfs_unlock }, /* unlock */
596 { &vop_bmap_desc, filecore_bmap }, /* bmap */
597 { &vop_strategy_desc, filecore_strategy }, /* strategy */
598 { &vop_print_desc, filecore_print }, /* print */
599 { &vop_islocked_desc, genfs_islocked }, /* islocked */
600 { &vop_pathconf_desc, filecore_pathconf }, /* pathconf */
601 { &vop_advlock_desc, filecore_advlock }, /* advlock */
602 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
603 { &vop_getpages_desc, genfs_getpages }, /* getpages */
604 { &vop_putpages_desc, genfs_putpages }, /* putpages */
605 { NULL, NULL }
607 const struct vnodeopv_desc filecore_vnodeop_opv_desc =
608 { &filecore_vnodeop_p, filecore_vnodeop_entries };