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