filecore_vnops.c revision 1.14.2.2 1 /* $NetBSD: filecore_vnops.c,v 1.14.2.2 2005/11/18 08:44:54 yamt 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.14.2.2 2005/11/18 08:44:54 yamt 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
86 #include <miscfs/genfs/genfs.h>
87 #include <miscfs/specfs/specdev.h>
88
89 #include <fs/filecorefs/filecore.h>
90 #include <fs/filecorefs/filecore_extern.h>
91 #include <fs/filecorefs/filecore_node.h>
92
93 #include <uvm/uvm_readahead.h>
94
95 /*
96 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
97 * The mode is shifted to select the owner/group/other fields. The
98 * super user is granted all permissions.
99 */
100 int
101 filecore_access(v)
102 void *v;
103 {
104 struct vop_access_args /* {
105 struct vnode *a_vp;
106 int a_mode;
107 struct ucred *a_cred;
108 struct proc *a_p;
109 } */ *ap = v;
110 struct vnode *vp = ap->a_vp;
111 struct filecore_node *ip = VTOI(vp);
112 struct filecore_mnt *fcmp = ip->i_mnt;
113
114 /*
115 * Disallow write attempts unless the file is a socket,
116 * fifo, or a block or character device resident on the
117 * file system.
118 */
119 if (ap->a_mode & VWRITE) {
120 switch (vp->v_type) {
121 case VDIR:
122 case VLNK:
123 case VREG:
124 return (EROFS);
125 default:
126 break;
127 }
128 }
129
130 return (vaccess(vp->v_type, filecore_mode(ip),
131 fcmp->fc_uid, fcmp->fc_gid, ap->a_mode, ap->a_cred));
132 }
133
134 int
135 filecore_getattr(v)
136 void *v;
137 {
138 struct vop_getattr_args /* {
139 struct vnode *a_vp;
140 struct vattr *a_vap;
141 struct ucred *a_cred;
142 struct proc *a_p;
143 } */ *ap = v;
144 struct vnode *vp = ap->a_vp;
145 struct filecore_node *ip = VTOI(vp);
146 struct vattr *vap = ap->a_vap;
147 struct filecore_mnt *fcmp = ip->i_mnt;
148
149 vap->va_fsid = ip->i_dev;
150 vap->va_fileid = ip->i_number;
151
152 vap->va_mode = filecore_mode(ip);
153 vap->va_nlink = 1;
154 vap->va_uid = fcmp->fc_uid;
155 vap->va_gid = fcmp->fc_gid;
156 vap->va_atime = filecore_time(ip);
157 vap->va_mtime = filecore_time(ip);
158 vap->va_ctime = filecore_time(ip);
159 vap->va_rdev = 0; /* We don't support specials */
160
161 vap->va_size = (u_quad_t) ip->i_size;
162 vap->va_flags = 0;
163 vap->va_gen = 1;
164 vap->va_blocksize = fcmp->blksize;
165 vap->va_bytes = vap->va_size;
166 vap->va_type = vp->v_type;
167 return (0);
168 }
169
170 /*
171 * Vnode op for reading.
172 */
173 int
174 filecore_read(v)
175 void *v;
176 {
177 struct vop_read_args /* {
178 struct vnode *a_vp;
179 struct uio *a_uio;
180 int a_ioflag;
181 struct ucred *a_cred;
182 } */ *ap = v;
183 struct vnode *vp = ap->a_vp;
184 struct uio *uio = ap->a_uio;
185 struct filecore_node *ip = VTOI(vp);
186 struct filecore_mnt *fcmp;
187 struct buf *bp;
188 daddr_t lbn, rablock;
189 off_t diff;
190 int error = 0;
191 long size, n, on;
192
193 if (uio->uio_resid == 0)
194 return (0);
195 if (uio->uio_offset < 0)
196 return (EINVAL);
197 if (uio->uio_offset >= ip->i_size)
198 return (0);
199 ip->i_flag |= IN_ACCESS;
200 fcmp = ip->i_mnt;
201
202 if (vp->v_type == VREG) {
203 const int advice = IO_ADV_DECODE(ap->a_ioflag);
204 error = 0;
205
206 while (uio->uio_resid > 0) {
207 void *win;
208 int flags;
209 vsize_t bytelen = MIN(ip->i_size - uio->uio_offset,
210 uio->uio_resid);
211
212 if (bytelen == 0) {
213 break;
214 }
215 win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
216 &bytelen, UBC_READ);
217 uvm_ra_request(vp->v_ractx, advice, &vp->v_uobj,
218 uio->uio_offset, bytelen);
219 error = uiomove(win, bytelen, uio);
220 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
221 ubc_release(win, flags);
222 if (error) {
223 break;
224 }
225 }
226 goto out;
227 }
228
229 do {
230 lbn = lblkno(fcmp, uio->uio_offset);
231 on = blkoff(fcmp, uio->uio_offset);
232 n = MIN(blksize(fcmp, ip, lbn) - on, uio->uio_resid);
233 diff = (off_t)ip->i_size - uio->uio_offset;
234 if (diff <= 0)
235 return (0);
236 if (diff < n)
237 n = diff;
238 size = blksize(fcmp, ip, lbn);
239 rablock = lbn + 1;
240 if (ip->i_dirent.attr & FILECORE_ATTR_DIR) {
241 error = filecore_dbread(ip, &bp);
242 on = uio->uio_offset;
243 n = MIN(FILECORE_DIR_SIZE - on, uio->uio_resid);
244 size = FILECORE_DIR_SIZE;
245 } else {
246 error = bread(vp, lbn, size, NOCRED, &bp);
247 #ifdef FILECORE_DEBUG_BR
248 printf("bread(%p, %x, %ld, CRED, %p)=%d\n",
249 vp, lbn, size, bp, error);
250 #endif
251 }
252 n = MIN(n, size - bp->b_resid);
253 if (error) {
254 #ifdef FILECORE_DEBUG_BR
255 printf("brelse(%p) vn1\n", bp);
256 #endif
257 brelse(bp);
258 return (error);
259 }
260
261 error = uiomove(bp->b_data + on, (int)n, uio);
262 #ifdef FILECORE_DEBUG_BR
263 printf("brelse(%p) vn2\n", bp);
264 #endif
265 brelse(bp);
266 } while (error == 0 && uio->uio_resid > 0 && n != 0);
267
268 out:
269 return (error);
270 }
271
272 /*
273 * Vnode op for readdir
274 */
275 int
276 filecore_readdir(v)
277 void *v;
278 {
279 struct vop_readdir_args /* {
280 struct vnode *a_vp;
281 struct uio *a_uio;
282 struct ucred *a_cred;
283 int *a_eofflag;
284 off_t **a_cookies;
285 int *a_ncookies;
286 } */ *ap = v;
287 struct uio *uio = ap->a_uio;
288 struct vnode *vdp = ap->a_vp;
289 struct filecore_node *dp;
290 struct filecore_mnt *fcmp;
291 struct buf *bp = NULL;
292 struct dirent de;
293 struct filecore_direntry *dep = NULL;
294 int error = 0;
295 off_t *cookies = NULL;
296 int ncookies = 0;
297 int i;
298 off_t uiooff;
299
300 dp = VTOI(vdp);
301
302 if ((dp->i_dirent.attr & FILECORE_ATTR_DIR) == 0)
303 return (ENOTDIR);
304
305 if (uio->uio_offset % FILECORE_DIRENT_SIZE != 0)
306 return (EINVAL);
307 i = uio->uio_offset / FILECORE_DIRENT_SIZE;
308 uiooff = uio->uio_offset;
309
310 *ap->a_eofflag = 0;
311 fcmp = dp->i_mnt;
312
313 error = filecore_dbread(dp, &bp);
314 if (error) {
315 brelse(bp);
316 return error;
317 }
318
319 if (ap->a_ncookies == NULL)
320 cookies = NULL;
321 else {
322 *ap->a_ncookies = 0;
323 ncookies = uio->uio_resid/16;
324 cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
325 }
326
327 for (; ; i++) {
328 switch (i) {
329 case 0:
330 /* Fake the '.' entry */
331 de.d_fileno = dp->i_number;
332 de.d_type = DT_DIR;
333 de.d_namlen = 1;
334 strlcpy(de.d_name, ".", sizeof(de.d_name));
335 break;
336 case 1:
337 /* Fake the '..' entry */
338 de.d_fileno = filecore_getparent(dp);
339 de.d_type = DT_DIR;
340 de.d_namlen = 2;
341 strlcpy(de.d_name, "..", sizeof(de.d_name));
342 break;
343 default:
344 de.d_fileno = dp->i_dirent.addr +
345 ((i - 2) << FILECORE_INO_INDEX);
346 dep = fcdirentry(bp->b_data, i - 2);
347 if (dep->attr & FILECORE_ATTR_DIR)
348 de.d_type = DT_DIR;
349 else
350 de.d_type = DT_REG;
351 if (filecore_fn2unix(dep->name, de.d_name,
352 /*###346 [cc] warning: passing arg 3 of `filecore_fn2unix' from incompatible pointer type%%%*/
353 &de.d_namlen)) {
354 *ap->a_eofflag = 1;
355 goto out;
356 }
357 break;
358 }
359 de.d_reclen = _DIRENT_SIZE(&de);
360 if (uio->uio_resid < de.d_reclen)
361 goto out;
362 error = uiomove(&de, de.d_reclen, uio);
363 if (error)
364 goto out;
365 uiooff += FILECORE_DIRENT_SIZE;
366
367 if (cookies) {
368 *cookies++ = i*FILECORE_DIRENT_SIZE;
369 (*ap->a_ncookies)++;
370 if (--ncookies == 0) goto out;
371 }
372 }
373 out:
374 if (cookies) {
375 *ap->a_cookies = cookies;
376 if (error) {
377 free(cookies, M_TEMP);
378 *ap->a_ncookies = 0;
379 *ap->a_cookies = NULL;
380 }
381 }
382 uio->uio_offset = uiooff;
383
384 #ifdef FILECORE_DEBUG_BR
385 printf("brelse(%p) vn3\n", bp);
386 #endif
387 brelse (bp);
388
389 return (error);
390 }
391
392 /*
393 * Return target name of a symbolic link
394 * Shouldn't we get the parent vnode and read the data from there?
395 * This could eventually result in deadlocks in filecore_lookup.
396 * But otherwise the block read here is in the block buffer two times.
397 */
398 int
399 filecore_readlink(v)
400 void *v;
401 {
402 #if 0
403 struct vop_readlink_args /* {
404 struct vnode *a_vp;
405 struct uio *a_uio;
406 struct ucred *a_cred;
407 } */ *ap = v;
408 #endif
409
410 return (EINVAL);
411 }
412
413 int
414 filecore_link(v)
415 void *v;
416 {
417 struct vop_link_args /* {
418 struct vnode *a_dvp;
419 struct vnode *a_vp;
420 struct componentname *a_cnp;
421 } */ *ap = v;
422
423 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
424 vput(ap->a_dvp);
425 return (EROFS);
426 }
427
428 int
429 filecore_symlink(v)
430 void *v;
431 {
432 struct vop_symlink_args /* {
433 struct vnode *a_dvp;
434 struct vnode **a_vpp;
435 struct componentname *a_cnp;
436 struct vattr *a_vap;
437 char *a_target;
438 } */ *ap = v;
439
440 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
441 vput(ap->a_dvp);
442 return (EROFS);
443 }
444
445 /*
446 * Calculate the logical to physical mapping if not done already,
447 * then call the device strategy routine.
448 */
449 int
450 filecore_strategy(v)
451 void *v;
452 {
453 struct vop_strategy_args /* {
454 struct vnode *a_vp;
455 struct buf *a_bp;
456 } */ *ap = v;
457 struct buf *bp = ap->a_bp;
458 struct vnode *vp = ap->a_vp;
459 struct filecore_node *ip;
460 int error;
461
462 ip = VTOI(vp);
463 if (bp->b_blkno == bp->b_lblkno) {
464 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
465 if (error) {
466 bp->b_error = error;
467 bp->b_flags |= B_ERROR;
468 biodone(bp);
469 return (error);
470 }
471 if ((long)bp->b_blkno == -1)
472 clrbuf(bp);
473 }
474 if ((long)bp->b_blkno == -1) {
475 biodone(bp);
476 return (0);
477 }
478 vp = ip->i_devvp;
479 return (VOP_STRATEGY(vp, bp));
480 }
481
482 /*
483 * Print out the contents of an inode.
484 */
485 /*ARGSUSED*/
486 int
487 filecore_print(v)
488 void *v;
489 {
490
491 printf("tag VT_FILECORE, filecore vnode\n");
492 return (0);
493 }
494
495 /*
496 * Return POSIX pathconf information applicable to filecore filesystems.
497 */
498 int
499 filecore_pathconf(v)
500 void *v;
501 {
502 struct vop_pathconf_args /* {
503 struct vnode *a_vp;
504 int a_name;
505 register_t *a_retval;
506 } */ *ap = v;
507 switch (ap->a_name) {
508 case _PC_LINK_MAX:
509 *ap->a_retval = 1;
510 return (0);
511 case _PC_NAME_MAX:
512 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
513 return (0);
514 case _PC_PATH_MAX:
515 *ap->a_retval = 256;
516 return (0);
517 case _PC_CHOWN_RESTRICTED:
518 *ap->a_retval = 1;
519 return (0);
520 case _PC_NO_TRUNC:
521 *ap->a_retval = 1;
522 return (0);
523 case _PC_SYNC_IO:
524 *ap->a_retval = 1;
525 return (0);
526 case _PC_FILESIZEBITS:
527 *ap->a_retval = 32;
528 return (0);
529 default:
530 return (EINVAL);
531 }
532 /* NOTREACHED */
533 }
534
535 /*
536 * Global vfs data structures for isofs
537 */
538 #define filecore_create genfs_eopnotsupp
539 #define filecore_mknod genfs_eopnotsupp
540 #define filecore_write genfs_eopnotsupp
541 #define filecore_setattr genfs_eopnotsupp
542 #define filecore_lease_check genfs_lease_check
543 #define filecore_fcntl genfs_fcntl
544 #define filecore_ioctl genfs_enoioctl
545 #define filecore_fsync genfs_nullop
546 #define filecore_remove genfs_eopnotsupp
547 #define filecore_rename genfs_eopnotsupp
548 #define filecore_mkdir genfs_eopnotsupp
549 #define filecore_rmdir genfs_eopnotsupp
550 #define filecore_advlock genfs_eopnotsupp
551 #define filecore_bwrite genfs_eopnotsupp
552 #define filecore_revoke genfs_revoke
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_bwrite_desc, vn_bwrite }, /* bwrite */
599 { &vop_getpages_desc, genfs_getpages }, /* getpages */
600 { &vop_putpages_desc, genfs_putpages }, /* putpages */
601 { NULL, NULL }
602 };
603 const struct vnodeopv_desc filecore_vnodeop_opv_desc =
604 { &filecore_vnodeop_p, filecore_vnodeop_entries };
605