udf_vnops.c revision 1.10 1 /* $NetBSD: udf_vnops.c,v 1.10 2007/04/29 20:23:36 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 2006 Reinoud Zandijk
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: udf_vnops.c,v 1.10 2007/04/29 20:23:36 msaitoh Exp $");
40 #endif /* not lint */
41
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
47 #include <sys/kernel.h>
48 #include <sys/file.h> /* define FWRITE ... */
49 #include <sys/stat.h>
50 #include <sys/buf.h>
51 #include <sys/proc.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/signalvar.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57 #include <sys/lockf.h>
58
59 #include <miscfs/genfs/genfs.h>
60 #include <uvm/uvm_extern.h>
61
62 #include <fs/udf/ecma167-udf.h>
63 #include <fs/udf/udf_mount.h>
64 #include "udf.h"
65 #include "udf_subr.h"
66 #include "udf_bswap.h"
67
68
69 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
70
71
72 /* externs */
73 extern int prtactive;
74
75
76 /* implementations of vnode functions; table follows at end */
77 /* --------------------------------------------------------------------- */
78
79 int
80 udf_inactive(void *v)
81 {
82 struct vop_inactive_args /* {
83 struct vnode *a_vp;
84 struct proc *a_p;
85 } */ *ap = v;
86 struct vnode *vp = ap->a_vp;
87
88 if (prtactive && vp->v_usecount != 0)
89 vprint("udf_inactive(): pushing active", vp);
90
91 VOP_UNLOCK(vp, 0);
92
93 DPRINTF(LOCKING, ("udf_inactive called for node %p\n", VTOI(vp)));
94
95 /*
96 * Optionally flush metadata to disc. If the file has not been
97 * referenced anymore in a directory we ought to free up the resources
98 * on disc if applicable.
99 */
100
101 return 0;
102 }
103
104 /* --------------------------------------------------------------------- */
105
106 int
107 udf_reclaim(void *v)
108 {
109 struct vop_reclaim_args /* {
110 struct vnode *a_vp;
111 } */ *ap = v;
112 struct vnode *vp = ap->a_vp;
113 struct udf_node *node = VTOI(vp);
114
115 if (prtactive && vp->v_usecount != 0)
116 vprint("udf_reclaim(): pushing active", vp);
117
118 /* purge old data from namei */
119 cache_purge(vp);
120
121 DPRINTF(LOCKING, ("udf_reclaim called for node %p\n", node));
122 /* dispose all node knowledge */
123 udf_dispose_node(node);
124
125 return 0;
126 }
127
128 /* --------------------------------------------------------------------- */
129
130 int
131 udf_read(void *v)
132 {
133 struct vop_read_args /* {
134 struct vnode *a_vp;
135 struct uio *a_uio;
136 int a_ioflag;
137 kauth_cred_t a_cred;
138 } */ *ap = v;
139 struct vnode *vp = ap->a_vp;
140 struct uio *uio = ap->a_uio;
141 int ioflag = ap->a_ioflag;
142 struct uvm_object *uobj;
143 struct udf_node *udf_node = VTOI(vp);
144 struct file_entry *fe;
145 struct extfile_entry *efe;
146 uint64_t file_size;
147 vsize_t len;
148 void *win;
149 int error;
150 int flags;
151
152 /* XXX how to deal with xtended attributes (files) */
153
154 DPRINTF(READ, ("udf_read called\n"));
155
156 /* can this happen? some filingsystems have this check */
157 if (uio->uio_offset < 0)
158 return EINVAL;
159
160 assert(udf_node);
161 assert(udf_node->fe || udf_node->efe);
162
163 /* TODO set access time */
164
165 /* get directory filesize */
166 if (udf_node->fe) {
167 fe = udf_node->fe;
168 file_size = udf_rw64(fe->inf_len);
169 } else {
170 assert(udf_node->efe);
171 efe = udf_node->efe;
172 file_size = udf_rw64(efe->inf_len);
173 }
174
175 if (vp->v_type == VDIR) {
176 /* protect against rogue programs reading raw directories */
177 if ((ioflag & IO_ALTSEMANTICS) == 0)
178 return EISDIR;
179 }
180 if (vp->v_type == VREG || vp->v_type == VDIR) {
181 const int advice = IO_ADV_DECODE(ap->a_ioflag);
182
183 /* read contents using buffercache */
184 uobj = &vp->v_uobj;
185 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
186 error = 0;
187 while (uio->uio_resid > 0) {
188 /* reached end? */
189 if (file_size <= uio->uio_offset)
190 break;
191
192 /* maximise length to file extremity */
193 len = MIN(file_size - uio->uio_offset, uio->uio_resid);
194 if (len == 0)
195 break;
196
197 /* ubc, here we come, prepare to trap */
198 win = ubc_alloc(uobj, uio->uio_offset, &len,
199 advice, UBC_READ);
200 error = uiomove(win, len, uio);
201 ubc_release(win, flags);
202 if (error)
203 break;
204 }
205 /* TODO note access time */
206 return error;
207 }
208
209 return EINVAL;
210 }
211
212 /* --------------------------------------------------------------------- */
213
214 int
215 udf_write(void *v)
216 {
217 struct vop_write_args /* {
218 struct vnode *a_vp;
219 struct uio *a_uio;
220 int a_ioflag;
221 kauth_cred_t a_cred;
222 } */ *ap = v;
223
224 DPRINTF(NOTIMPL, ("udf_write called\n"));
225 ap = ap; /* shut up gcc */
226
227 /* TODO implement writing */
228 return EROFS;
229 }
230
231
232 /* --------------------------------------------------------------------- */
233
234 /*
235 * `Special' bmap functionality that translates all incomming requests to
236 * translate to vop_strategy() calls with the same blocknumbers effectively
237 * not translating at all.
238 */
239
240 int
241 udf_trivial_bmap(void *v)
242 {
243 struct vop_bmap_args /* {
244 struct vnode *a_vp;
245 daddr_t a_bn;
246 struct vnode **a_vpp;
247 daddr_t *a_bnp;
248 int *a_runp;
249 } */ *ap = v;
250 struct vnode *vp = ap->a_vp; /* our node */
251 struct vnode **vpp = ap->a_vpp; /* return node */
252 daddr_t *bnp = ap->a_bnp; /* translated */
253 daddr_t bn = ap->a_bn; /* origional */
254 int *runp = ap->a_runp;
255 struct udf_node *udf_node = VTOI(vp);
256 uint32_t lb_size;
257
258 /* get logical block size */
259 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
260
261 /* could return `-1' to indicate holes/zeros */
262 /* translate 1:1 */
263 *bnp = bn;
264
265 /* set the vnode to read the data from with strategy on itself */
266 if (vpp)
267 *vpp = vp;
268
269 /* set runlength of maximum block size */
270 if (runp)
271 *runp = MAXPHYS / lb_size; /* or with -1 ? */
272
273 /* return success */
274 return 0;
275 }
276
277 /* --------------------------------------------------------------------- */
278
279 int
280 udf_strategy(void *v)
281 {
282 struct vop_strategy_args /* {
283 struct vnode *a_vp;
284 struct buf *a_bp;
285 } */ *ap = v;
286 struct vnode *vp = ap->a_vp;
287 struct buf *bp = ap->a_bp;
288 struct udf_node *udf_node = VTOI(vp);
289 uint32_t lb_size, from, sectors;
290 int error;
291
292 DPRINTF(STRATEGY, ("udf_strategy called\n"));
293
294 /* check if we ought to be here */
295 if (vp->v_type == VBLK || vp->v_type == VCHR)
296 panic("udf_strategy: spec");
297
298 /* only filebuffers ought to be read by this, no descriptors */
299 assert(bp->b_blkno >= 0);
300
301 /* get sector size */
302 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
303
304 /* calculate sector to start from */
305 from = bp->b_blkno;
306
307 /* calculate length to fetch/store in sectors */
308 sectors = bp->b_bcount / lb_size;
309 assert(bp->b_bcount > 0);
310
311 /* NEVER assume later that this buffer is already translated */
312 /* bp->b_lblkno = bp->b_blkno; */
313
314 DPRINTF(STRATEGY, ("\tread vp %p buf %p (blk no %"PRIu64")"
315 ", sector %d for %d sectors\n",
316 vp, bp, bp->b_blkno, from, sectors));
317
318 /* check assertions: we OUGHT to always get multiples of this */
319 assert(sectors * lb_size == bp->b_bcount);
320
321 /* determine mode */
322 error = 0;
323 if (bp->b_flags & B_READ) {
324 /* read buffer from the udf_node, translate vtop on the way*/
325 udf_read_filebuf(udf_node, bp);
326 return bp->b_error;
327 }
328
329 printf("udf_strategy: can't write yet\n");
330 return ENOTSUP;
331 }
332
333 /* --------------------------------------------------------------------- */
334
335 int
336 udf_readdir(void *v)
337 {
338 struct vop_readdir_args /* {
339 struct vnode *a_vp;
340 struct uio *a_uio;
341 kauth_cred_t a_cred;
342 int *a_eofflag;
343 off_t **a_cookies;
344 int *a_ncookies;
345 } */ *ap = v;
346 struct uio *uio = ap->a_uio;
347 struct vnode *vp = ap->a_vp;
348 struct udf_node *udf_node = VTOI(vp);
349 struct file_entry *fe;
350 struct extfile_entry *efe;
351 struct fileid_desc *fid;
352 struct dirent dirent;
353 uint64_t file_size, diroffset, transoffset;
354 uint32_t lb_size;
355 int error;
356
357 DPRINTF(READDIR, ("udf_readdir called\n"));
358
359 /* This operation only makes sense on directory nodes. */
360 if (vp->v_type != VDIR)
361 return ENOTDIR;
362
363 /* get directory filesize */
364 if (udf_node->fe) {
365 fe = udf_node->fe;
366 file_size = udf_rw64(fe->inf_len);
367 } else {
368 assert(udf_node->efe);
369 efe = udf_node->efe;
370 file_size = udf_rw64(efe->inf_len);
371 }
372
373 /*
374 * Add `.' pseudo entry if at offset zero since its not in the fid
375 * stream
376 */
377 if (uio->uio_offset == 0) {
378 DPRINTF(READDIR, ("\t'.' inserted\n"));
379 memset(&dirent, 0, sizeof(struct dirent));
380 strcpy(dirent.d_name, ".");
381 dirent.d_fileno = udf_calchash(&udf_node->loc);
382 dirent.d_type = DT_DIR;
383 dirent.d_namlen = strlen(dirent.d_name);
384 dirent.d_reclen = _DIRENT_SIZE(&dirent);
385 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio);
386
387 /* mark with magic value that we have done the dummy */
388 uio->uio_offset = UDF_DIRCOOKIE_DOT;
389 }
390
391 /* we are called just as long as we keep on pushing data in */
392 error = 0;
393 if ((uio->uio_offset < file_size) &&
394 (uio->uio_resid >= sizeof(struct dirent))) {
395 /* allocate temporary space for fid */
396 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
397 fid = malloc(lb_size, M_TEMP, M_WAITOK);
398
399 if (uio->uio_offset == UDF_DIRCOOKIE_DOT)
400 uio->uio_offset = 0;
401
402 diroffset = uio->uio_offset;
403 transoffset = diroffset;
404 while (diroffset < file_size) {
405 DPRINTF(READDIR, ("\tread in fid stream\n"));
406 /* transfer a new fid/dirent */
407 error = udf_read_fid_stream(vp, &diroffset,
408 fid, &dirent);
409 DPRINTFIF(READDIR, error, ("read error in read fid "
410 "stream : %d\n", error));
411 if (error)
412 break;
413
414 /*
415 * If there isn't enough space in the uio to return a
416 * whole dirent, break off read
417 */
418 if (uio->uio_resid < _DIRENT_SIZE(&dirent))
419 break;
420
421 /* remember the last entry we transfered */
422 transoffset = diroffset;
423
424 /* skip deleted entries */
425 if (fid->file_char & UDF_FILE_CHAR_DEL)
426 continue;
427
428 /* skip not visible files */
429 if (fid->file_char & UDF_FILE_CHAR_VIS)
430 continue;
431
432 /* copy dirent to the caller */
433 DPRINTF(READDIR, ("\tread dirent `%s', type %d\n",
434 dirent.d_name, dirent.d_type));
435 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio);
436 }
437
438 /* pass on last transfered offset */
439 uio->uio_offset = transoffset;
440 free(fid, M_TEMP);
441 }
442
443 if (ap->a_eofflag)
444 *ap->a_eofflag = (uio->uio_offset == file_size);
445
446 #ifdef DEBUG
447 if (udf_verbose & UDF_DEBUG_READDIR) {
448 printf("returning offset %d\n", (uint32_t) uio->uio_offset);
449 if (ap->a_eofflag)
450 printf("returning EOF ? %d\n", *ap->a_eofflag);
451 if (error)
452 printf("readdir returning error %d\n", error);
453 }
454 #endif
455
456 return error;
457 }
458
459 /* --------------------------------------------------------------------- */
460
461 int
462 udf_lookup(void *v)
463 {
464 struct vop_lookup_args /* {
465 struct vnode *a_dvp;
466 struct vnode **a_vpp;
467 struct componentname *a_cnp;
468 } */ *ap = v;
469 struct vnode *dvp = ap->a_dvp;
470 struct vnode **vpp = ap->a_vpp;
471 struct componentname *cnp = ap->a_cnp;
472 struct udf_node *dir_node, *res_node;
473 struct udf_mount *ump;
474 struct long_ad icb_loc;
475 const char *name;
476 int namelen, nameiop, islastcn, mounted_ro;
477 int vnodetp;
478 int error, found;
479
480 dir_node = VTOI(dvp);
481 ump = dir_node->ump;
482 *vpp = NULL;
483
484 DPRINTF(LOOKUP, ("udf_lookup called\n"));
485
486 /* simplify/clarification flags */
487 nameiop = cnp->cn_nameiop;
488 islastcn = cnp->cn_flags & ISLASTCN;
489 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY;
490
491 /* check exec/dirread permissions first */
492 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_lwp);
493 if (error)
494 return error;
495
496 DPRINTF(LOOKUP, ("\taccess ok\n"));
497
498 /*
499 * If requesting a modify on the last path element on a read-only
500 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
501 */
502 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
503 return EROFS;
504
505 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
506 cnp->cn_nameptr));
507 /* look in the nami cache; returns 0 on success!! */
508 error = cache_lookup(dvp, vpp, cnp);
509 if (error >= 0)
510 return error;
511
512 DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
513
514 /*
515 * Obviously, the file is not (anymore) in the namecache, we have to
516 * search for it. There are three basic cases: '.', '..' and others.
517 *
518 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
519 */
520 error = 0;
521 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
522 DPRINTF(LOOKUP, ("\tlookup '.'\n"));
523 /* special case 1 '.' */
524 VREF(dvp);
525 *vpp = dvp;
526 /* done */
527 } else if (cnp->cn_flags & ISDOTDOT) {
528 /* special case 2 '..' */
529 DPRINTF(LOOKUP, ("\tlookup '..'\n"));
530
531 /* first unlock parent */
532 VOP_UNLOCK(dvp, 0);
533
534 /* get our node */
535 name = "..";
536 namelen = 2;
537 found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc);
538 if (!found)
539 error = ENOENT;
540
541 if (!error) {
542 DPRINTF(LOOKUP, ("\tfound '..'\n"));
543 /* try to create/reuse the node */
544 error = udf_get_node(ump, &icb_loc, &res_node);
545
546 if (!error) {
547 DPRINTF(LOOKUP, ("\tnode retrieved/created OK\n"));
548 *vpp = res_node->vnode;
549 }
550 }
551
552 /* try to relock parent */
553 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
554 } else {
555 DPRINTF(LOOKUP, ("\tlookup file\n"));
556 /* all other files */
557 /* lookup filename in the directory; location icb_loc */
558 name = cnp->cn_nameptr;
559 namelen = cnp->cn_namelen;
560 found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc);
561 if (!found) {
562 DPRINTF(LOOKUP, ("\tNOT found\n"));
563 /*
564 * UGH, didn't find name. If we're creating or naming
565 * on the last name this is OK and we ought to return
566 * EJUSTRETURN if its allowed to be created.
567 */
568 error = ENOENT;
569 if (islastcn &&
570 (nameiop == CREATE || nameiop == RENAME))
571 error = 0;
572 if (!error) {
573 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
574 cnp->cn_lwp);
575 if (!error) {
576 /* keep the component name */
577 cnp->cn_flags |= SAVENAME;
578 error = EJUSTRETURN;
579 }
580 }
581 /* done */
582 } else {
583 /* try to create/reuse the node */
584 error = udf_get_node(ump, &icb_loc, &res_node);
585 if (!error) {
586 /*
587 * If we are not at the last path component
588 * and found a non-directory or non-link entry
589 * (which may itself be pointing to a
590 * directory), raise an error.
591 */
592 vnodetp = res_node->vnode->v_type;
593 if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
594 if (!islastcn)
595 error = ENOTDIR;
596 }
597
598 }
599 if (!error) {
600 *vpp = res_node->vnode;
601 }
602 }
603 }
604
605 /*
606 * Store result in the cache if requested. If we are creating a file,
607 * the file might not be found and thus putting it into the namecache
608 * might be seen as negative caching.
609 */
610 if (cnp->cn_flags & MAKEENTRY)
611 cache_enter(dvp, *vpp, cnp);
612
613 DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error));
614
615 return error;
616 }
617
618 /* --------------------------------------------------------------------- */
619
620 int
621 udf_getattr(void *v)
622 {
623 struct vop_getattr_args /* {
624 struct vnode *a_vp;
625 struct vattr *a_vap;
626 kauth_cred_t a_cred;
627 struct proc *a_p;
628 } */ *ap = v;
629 struct vnode *vp = ap->a_vp;
630 struct udf_node *udf_node = VTOI(vp);
631 struct udf_mount *ump = udf_node->ump;
632 struct vattr *vap = ap->a_vap;
633 struct file_entry *fe;
634 struct extfile_entry *efe;
635 struct timestamp *atime, *mtime, *attrtime;
636 uint32_t nlink;
637 uint64_t filesize, blkssize;
638 uid_t uid;
639 gid_t gid;
640
641 DPRINTF(CALL, ("udf_getattr called\n"));
642
643 /* get descriptor information */
644 if (udf_node->fe) {
645 fe = udf_node->fe;
646 nlink = udf_rw16(fe->link_cnt);
647 uid = (uid_t)udf_rw32(fe->uid);
648 gid = (gid_t)udf_rw32(fe->gid);
649 filesize = udf_rw64(fe->inf_len);
650 blkssize = udf_rw64(fe->logblks_rec);
651 atime = &fe->atime;
652 mtime = &fe->mtime;
653 attrtime = &fe->attrtime;
654 } else {
655 assert(udf_node->efe);
656 efe = udf_node->efe;
657 nlink = udf_rw16(efe->link_cnt);
658 uid = (uid_t)udf_rw32(efe->uid);
659 gid = (gid_t)udf_rw32(efe->gid);
660 filesize = udf_rw64(efe->inf_len); /* XXX or obj_size? */
661 blkssize = udf_rw64(efe->logblks_rec);
662 atime = &efe->atime;
663 mtime = &efe->mtime;
664 attrtime = &efe->attrtime;
665 }
666
667 /* do the uid/gid translation game */
668 if ((uid == (uid_t) -1) && (gid == (gid_t) -1)) {
669 uid = ump->mount_args.anon_uid;
670 gid = ump->mount_args.anon_gid;
671 }
672
673 /* fill in struct vattr with values from the node */
674 VATTR_NULL(vap);
675 vap->va_type = vp->v_type;
676 vap->va_mode = udf_getaccessmode(udf_node);
677 vap->va_nlink = nlink;
678 vap->va_uid = uid;
679 vap->va_gid = gid;
680 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
681 vap->va_fileid = udf_calchash(&udf_node->loc); /* inode hash XXX */
682 vap->va_size = filesize;
683 vap->va_blocksize = udf_node->ump->discinfo.sector_size; /* wise? */
684
685 /*
686 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add
687 * 1 to the link count if its a directory we're requested attributes
688 * of.
689 */
690 if (vap->va_type == VDIR)
691 vap->va_nlink++;
692
693 /* access times */
694 udf_timestamp_to_timespec(ump, atime, &vap->va_atime);
695 udf_timestamp_to_timespec(ump, mtime, &vap->va_mtime);
696 udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime);
697
698 vap->va_gen = 1; /* no multiple generations yes (!?) */
699 vap->va_flags = 0; /* no flags */
700 vap->va_rdev = udf_node->rdev;
701 vap->va_bytes = blkssize * udf_node->ump->discinfo.sector_size;
702 vap->va_filerev = 1; /* TODO file revision numbers? */
703 vap->va_vaflags = 0; /* TODO which va_vaflags? */
704
705 return 0;
706 }
707
708 /* --------------------------------------------------------------------- */
709
710 int
711 udf_setattr(void *v)
712 {
713 struct vop_setattr_args /* {
714 struct vnode *a_vp;
715 struct vattr *a_vap;
716 kauth_cred_t a_cred;
717 struct proc *a_p;
718 } */ *ap = v;
719 struct vnode *vp = ap->a_vp;
720 struct udf_node *udf_node = VTOI(vp);
721 struct udf_mount *ump = udf_node->ump;
722 struct vattr *vap = ap->a_vap;
723 uid_t uid, nobody_uid;
724 gid_t gid, nobody_gid;
725
726 /* shut up gcc for now */
727 ap = ap;
728 vp = vp;
729 uid = 0; /* XXX gcc */
730 gid = 0; /* XXX gcc */
731 udf_node = udf_node;
732
733 DPRINTF(NOTIMPL, ("udf_setattr called\n"));
734
735 /*
736 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to
737 * subtract 1 to the link count if its a directory we're setting
738 * attributes on. See getattr.
739 */
740 if (vap->va_type == VDIR)
741 vap->va_nlink--;
742
743 /* do the uid/gid translation game */
744 nobody_uid = ump->mount_args.nobody_uid;
745 nobody_gid = ump->mount_args.nobody_gid;
746 if ((uid == nobody_uid) && (gid == nobody_gid)) {
747 uid = (uid_t) -1;
748 gid = (gid_t) -1;
749 }
750
751 /* TODO implement setattr!! NOT IMPLEMENTED yet */
752 return 0;
753 }
754
755 /* --------------------------------------------------------------------- */
756
757 /*
758 * Return POSIX pathconf information for UDF file systems.
759 */
760 int
761 udf_pathconf(void *v)
762 {
763 struct vop_pathconf_args /* {
764 struct vnode *a_vp;
765 int a_name;
766 register_t *a_retval;
767 } */ *ap = v;
768 struct vnode *vp = ap->a_vp;
769 struct udf_node *udf_node = VTOI(vp);
770 uint32_t bits;
771
772 DPRINTF(CALL, ("udf_pathconf called\n"));
773
774 switch (ap->a_name) {
775 case _PC_LINK_MAX:
776 *ap->a_retval = (1<<16)-1; /* 16 bits */
777 return 0;
778 case _PC_NAME_MAX:
779 *ap->a_retval = NAME_MAX;
780 return 0;
781 case _PC_PATH_MAX:
782 *ap->a_retval = PATH_MAX;
783 return 0;
784 case _PC_PIPE_BUF:
785 *ap->a_retval = PIPE_BUF;
786 return 0;
787 case _PC_CHOWN_RESTRICTED:
788 *ap->a_retval = 1;
789 return 0;
790 case _PC_NO_TRUNC:
791 *ap->a_retval = 1;
792 return 0;
793 case _PC_SYNC_IO:
794 *ap->a_retval = 0; /* synchronised is off for performance */
795 return 0;
796 case _PC_FILESIZEBITS:
797 bits = 32;
798 if (udf_node)
799 bits = 32 * vp->v_mount->mnt_dev_bshift;
800 *ap->a_retval = bits;
801 return 0;
802 }
803
804 return EINVAL;
805 }
806
807
808 /* --------------------------------------------------------------------- */
809
810 int
811 udf_open(void *v)
812 {
813 struct vop_open_args /* {
814 struct vnode *a_vp;
815 int a_mode;
816 kauth_cred_t a_cred;
817 struct proc *a_p;
818 } */ *ap = v;
819
820 DPRINTF(CALL, ("udf_open called\n"));
821 ap = 0; /* XXX gcc */
822
823 return 0;
824 }
825
826
827 /* --------------------------------------------------------------------- */
828
829 int
830 udf_close(void *v)
831 {
832 struct vop_close_args /* {
833 struct vnode *a_vp;
834 int a_fflag;
835 kauth_cred_t a_cred;
836 struct proc *a_p;
837 } */ *ap = v;
838 struct vnode *vp = ap->a_vp;
839 struct udf_node *udf_node = VTOI(vp);
840
841 DPRINTF(CALL, ("udf_close called\n"));
842 udf_node = udf_node; /* shut up gcc */
843
844 simple_lock(&vp->v_interlock);
845 if (vp->v_usecount > 1) {
846 /* TODO update times */
847 }
848 simple_unlock(&vp->v_interlock);
849
850 return 0;
851 }
852
853
854 /* --------------------------------------------------------------------- */
855
856 int
857 udf_access(void *v)
858 {
859 struct vop_access_args /* {
860 struct vnode *a_vp;
861 int a_mode;
862 kauth_cred_t a_cred;
863 struct proc *a_p;
864 } */ *ap = v;
865 struct vnode *vp = ap->a_vp;
866 mode_t mode = ap->a_mode;
867 kauth_cred_t cred = ap->a_cred;
868 struct udf_node *udf_node = VTOI(vp);
869 struct file_entry *fe;
870 struct extfile_entry *efe;
871 mode_t node_mode;
872 uid_t uid;
873 gid_t gid;
874
875 DPRINTF(CALL, ("udf_access called\n"));
876
877 /* get access mode to compare to */
878 node_mode = udf_getaccessmode(udf_node);
879
880 /* get uid/gid pair */
881 if (udf_node->fe) {
882 fe = udf_node->fe;
883 uid = (uid_t)udf_rw32(fe->uid);
884 gid = (gid_t)udf_rw32(fe->gid);
885 } else {
886 assert(udf_node->efe);
887 efe = udf_node->efe;
888 uid = (uid_t)udf_rw32(efe->uid);
889 gid = (gid_t)udf_rw32(efe->gid);
890 }
891
892 /* check if we are allowed to write */
893 switch (vp->v_type) {
894 case VDIR:
895 case VLNK:
896 case VREG:
897 /*
898 * normal nodes: check if we're on a read-only mounted
899 * filingsystem and bomb out if we're trying to write.
900 */
901 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
902 return EROFS;
903 break;
904 case VBLK:
905 case VCHR:
906 case VSOCK:
907 case VFIFO:
908 /*
909 * special nodes: even on read-only mounted filingsystems
910 * these are allowed to be written to if permissions allow.
911 */
912 break;
913 default:
914 /* no idea what this is */
915 return EINVAL;
916 }
917
918 /* TODO support for chflags checking i.e. IMMUTABLE flag */
919
920 /* ask the generic vaccess to advice on security */
921 return vaccess(vp->v_type, node_mode, uid, gid, mode, cred);
922 }
923
924 /* --------------------------------------------------------------------- */
925
926 int
927 udf_create(void *v)
928 {
929 struct vop_create_args /* {
930 struct vnode *a_dvp;
931 struct vnode **a_vpp;
932 struct componentname *a_cnp;
933 struct vattr *a_vap;
934 } */ *ap = v;
935 struct componentname *cnp = ap->a_cnp;
936
937 DPRINTF(NOTIMPL, ("udf_create called\n"));
938
939 /* error out */
940 PNBUF_PUT(cnp->cn_pnbuf);
941 vput(ap->a_dvp);
942 return ENOTSUP;
943 }
944
945 /* --------------------------------------------------------------------- */
946
947 int
948 udf_mknod(void *v)
949 {
950 struct vop_mknod_args /* {
951 struct vnode *a_dvp;
952 struct vnode **a_vpp;
953 struct componentname *a_cnp;
954 struct vattr *a_vap;
955 } */ *ap = v;
956
957 DPRINTF(NOTIMPL, ("udf_mknod called\n"));
958
959 /* error out */
960 PNBUF_PUT(ap->a_cnp->cn_pnbuf);
961 vput(ap->a_dvp);
962 return ENOTSUP;
963 }
964
965 /* --------------------------------------------------------------------- */
966
967 int
968 udf_remove(void *v)
969 {
970 struct vop_remove_args /* {
971 struct vnode *a_dvp;
972 struct vnode *a_vp;
973 struct componentname *a_cnp;
974 } */ *ap = v;
975 struct vnode *dvp = ap->a_dvp;
976 struct vnode *vp = ap->a_vp;
977
978 DPRINTF(NOTIMPL, ("udf_remove called\n"));
979
980 /* error out */
981 vput(dvp);
982 vput(vp);
983
984 return ENOTSUP;
985 }
986
987 /* --------------------------------------------------------------------- */
988
989 int
990 udf_link(void *v)
991 {
992 struct vop_link_args /* {
993 struct vnode *a_dvp;
994 struct vnode *a_vp;
995 struct componentname *a_cnp;
996 } */ *ap = v;
997 struct vnode *dvp = ap->a_dvp;
998 struct vnode *vp = ap->a_vp;
999 struct componentname *cnp = ap->a_cnp;
1000
1001 DPRINTF(NOTIMPL, ("udf_link called\n"));
1002
1003 /* error out */
1004 /* XXX or just VOP_ABORTOP(dvp, a_cnp); ? */
1005 if (VOP_ISLOCKED(vp))
1006 VOP_UNLOCK(vp, 0);
1007 PNBUF_PUT(cnp->cn_pnbuf);
1008 vput(dvp);
1009
1010 return ENOTSUP;
1011 }
1012
1013 /* --------------------------------------------------------------------- */
1014
1015 int
1016 udf_symlink(void *v)
1017 {
1018 struct vop_symlink_args /* {
1019 struct vnode *a_dvp;
1020 struct vnode **a_vpp;
1021 struct componentname *a_cnp;
1022 struct vattr *a_vap;
1023 char *a_target;
1024 } */ *ap = v;
1025 struct vnode *dvp = ap->a_dvp;
1026 struct componentname *cnp = ap->a_cnp;
1027
1028 DPRINTF(NOTIMPL, ("udf_symlink called\n"));
1029
1030 /* error out */
1031 VOP_ABORTOP(dvp, cnp);
1032 vput(dvp);
1033
1034 return ENOTSUP;
1035 }
1036
1037 /* --------------------------------------------------------------------- */
1038
1039 int
1040 udf_readlink(void *v)
1041 {
1042 struct vop_readlink_args /* {
1043 struct vnode *a_vp;
1044 struct uio *a_uio;
1045 kauth_cred_t a_cred;
1046 } */ *ap = v;
1047 #ifdef notyet
1048 struct vnode *vp = ap->a_vp;
1049 struct uio *uio = ap->a_uio;
1050 kauth_cred_t cred = ap->a_cred;
1051 #endif
1052
1053 ap = ap; /* shut up gcc */
1054
1055 DPRINTF(NOTIMPL, ("udf_readlink called\n"));
1056
1057 /* TODO read `file' contents and process pathcomponents into a path */
1058 return ENOTSUP;
1059 }
1060
1061 /* --------------------------------------------------------------------- */
1062
1063 int
1064 udf_rename(void *v)
1065 {
1066 struct vop_rename_args /* {
1067 struct vnode *a_fdvp;
1068 struct vnode *a_fvp;
1069 struct componentname *a_fcnp;
1070 struct vnode *a_tdvp;
1071 struct vnode *a_tvp;
1072 struct componentname *a_tcnp;
1073 } */ *ap = v;
1074 struct vnode *tvp = ap->a_tvp;
1075 struct vnode *tdvp = ap->a_tdvp;
1076 struct vnode *fvp = ap->a_fvp;
1077 struct vnode *fdvp = ap->a_fdvp;
1078
1079 DPRINTF(NOTIMPL, ("udf_rename called\n"));
1080
1081 /* error out */
1082 if (tdvp == tvp)
1083 vrele(tdvp);
1084 else
1085 vput(tdvp);
1086 if (tvp != NULL)
1087 vput(tvp);
1088
1089 /* release source nodes. */
1090 vrele(fdvp);
1091 vrele(fvp);
1092
1093 return ENOTSUP;
1094 }
1095
1096 /* --------------------------------------------------------------------- */
1097
1098 int
1099 udf_mkdir(void *v)
1100 {
1101 struct vop_mkdir_args /* {
1102 struct vnode *a_dvp;
1103 struct vnode **a_vpp;
1104 struct componentname *a_cnp;
1105 struct vattr *a_vap;
1106 } */ *ap = v;
1107 struct vnode *dvp = ap->a_dvp;
1108 struct componentname *cnp = ap->a_cnp;
1109
1110 DPRINTF(NOTIMPL, ("udf_mkdir called\n"));
1111
1112 /* error out */
1113 PNBUF_PUT(cnp->cn_pnbuf);
1114 vput(dvp);
1115
1116 return ENOTSUP;
1117 }
1118
1119 /* --------------------------------------------------------------------- */
1120
1121 int
1122 udf_rmdir(void *v)
1123 {
1124 struct vop_rmdir_args /* {
1125 struct vnode *a_dvp;
1126 struct vnode *a_vp;
1127 struct componentname *a_cnp;
1128 } */ *ap = v;
1129 struct vnode *vp = ap->a_vp;
1130 struct vnode *dvp = ap->a_dvp;
1131 struct componentname *cnp = ap->a_cnp;
1132 int error;
1133
1134 cnp = cnp; /* shut up gcc */
1135
1136 DPRINTF(NOTIMPL, ("udf_rmdir called\n"));
1137
1138 error = ENOTSUP;
1139 /* error out */
1140 if (error != 0) {
1141 vput(dvp);
1142 vput(vp);
1143 }
1144
1145 return error;
1146 }
1147
1148 /* --------------------------------------------------------------------- */
1149
1150 int
1151 udf_fsync(void *v)
1152 {
1153 struct vop_fsync_args /* {
1154 struct vnode *a_vp;
1155 kauth_cred_t a_cred;
1156 int a_flags;
1157 off_t offlo;
1158 off_t offhi;
1159 struct proc *a_p;
1160 } */ *ap = v;
1161 struct vnode *vp = ap->a_vp;
1162
1163 DPRINTF(NOTIMPL, ("udf_fsync called\n"));
1164
1165 vp = vp; /* shut up gcc */
1166
1167 return 0;
1168 }
1169
1170 /* --------------------------------------------------------------------- */
1171
1172 int
1173 udf_advlock(void *v)
1174 {
1175 struct vop_advlock_args /* {
1176 struct vnode *a_vp;
1177 void *a_id;
1178 int a_op;
1179 struct flock *a_fl;
1180 int a_flags;
1181 } */ *ap = v;
1182 struct vnode *vp = ap->a_vp;
1183 struct udf_node *udf_node = VTOI(vp);
1184 struct file_entry *fe;
1185 struct extfile_entry *efe;
1186 uint64_t file_size;
1187
1188 DPRINTF(LOCKING, ("udf_advlock called\n"));
1189
1190 /* get directory filesize */
1191 if (udf_node->fe) {
1192 fe = udf_node->fe;
1193 file_size = udf_rw64(fe->inf_len);
1194 } else {
1195 assert(udf_node->efe);
1196 efe = udf_node->efe;
1197 file_size = udf_rw64(efe->inf_len);
1198 }
1199
1200 return lf_advlock(ap, &udf_node->lockf, file_size);
1201 }
1202
1203 /* --------------------------------------------------------------------- */
1204
1205
1206 /* Global vfs vnode data structures for udfs */
1207 int (**udf_vnodeop_p) __P((void *));
1208
1209 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
1210 { &vop_default_desc, vn_default_error },
1211 { &vop_lookup_desc, udf_lookup }, /* lookup */
1212 { &vop_create_desc, udf_create }, /* create */ /* TODO */
1213 { &vop_mknod_desc, udf_mknod }, /* mknod */ /* TODO */
1214 { &vop_open_desc, udf_open }, /* open */
1215 { &vop_close_desc, udf_close }, /* close */
1216 { &vop_access_desc, udf_access }, /* access */
1217 { &vop_getattr_desc, udf_getattr }, /* getattr */
1218 { &vop_setattr_desc, udf_setattr }, /* setattr */ /* TODO */
1219 { &vop_read_desc, udf_read }, /* read */
1220 { &vop_write_desc, udf_write }, /* write */ /* WRITE */
1221 { &vop_lease_desc, genfs_lease_check }, /* lease */ /* TODO? */
1222 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */
1223 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */
1224 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */
1225 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */
1226 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */
1227 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */
1228 { &vop_fsync_desc, udf_fsync }, /* fsync */ /* TODO */
1229 { &vop_seek_desc, genfs_seek }, /* seek */
1230 { &vop_remove_desc, udf_remove }, /* remove */ /* TODO */
1231 { &vop_link_desc, udf_link }, /* link */ /* TODO */
1232 { &vop_rename_desc, udf_rename }, /* rename */ /* TODO */
1233 { &vop_mkdir_desc, udf_mkdir }, /* mkdir */ /* TODO */
1234 { &vop_rmdir_desc, udf_rmdir }, /* rmdir */ /* TODO */
1235 { &vop_symlink_desc, udf_symlink }, /* symlink */ /* TODO */
1236 { &vop_readdir_desc, udf_readdir }, /* readdir */
1237 { &vop_readlink_desc, udf_readlink }, /* readlink */ /* TEST ME */
1238 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */
1239 { &vop_inactive_desc, udf_inactive }, /* inactive */
1240 { &vop_reclaim_desc, udf_reclaim }, /* reclaim */
1241 { &vop_lock_desc, genfs_lock }, /* lock */
1242 { &vop_unlock_desc, genfs_unlock }, /* unlock */
1243 { &vop_bmap_desc, udf_trivial_bmap }, /* bmap */ /* 1:1 bmap */
1244 { &vop_strategy_desc, udf_strategy }, /* strategy */
1245 /* { &vop_print_desc, udf_print }, */ /* print */
1246 { &vop_islocked_desc, genfs_islocked }, /* islocked */
1247 { &vop_pathconf_desc, udf_pathconf }, /* pathconf */
1248 { &vop_advlock_desc, udf_advlock }, /* advlock */ /* TEST ME */
1249 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */
1250 { &vop_getpages_desc, genfs_getpages }, /* getpages */
1251 { &vop_putpages_desc, genfs_putpages }, /* putpages */
1252 { NULL, NULL }
1253 };
1254
1255
1256 const struct vnodeopv_desc udf_vnodeop_opv_desc = {
1257 &udf_vnodeop_p, udf_vnodeop_entries
1258 };
1259
1260