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