udf_vnops.c revision 1.105 1 /* $NetBSD: udf_vnops.c,v 1.105 2017/05/26 14:21:01 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2008 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Generic parts are derived from software contributed to The NetBSD Foundation
28 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
29 * 2005 program.
30 *
31 */
32
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.105 2017/05/26 14:21:01 riastradh Exp $");
36 #endif /* not lint */
37
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
43 #include <sys/kernel.h>
44 #include <sys/file.h> /* define FWRITE ... */
45 #include <sys/stat.h>
46 #include <sys/buf.h>
47 #include <sys/proc.h>
48 #include <sys/mount.h>
49 #include <sys/vnode.h>
50 #include <sys/signalvar.h>
51 #include <sys/malloc.h>
52 #include <sys/dirent.h>
53 #include <sys/lockf.h>
54 #include <sys/kauth.h>
55
56 #include <miscfs/genfs/genfs.h>
57 #include <uvm/uvm_extern.h>
58
59 #include <fs/udf/ecma167-udf.h>
60 #include <fs/udf/udf_mount.h>
61 #include <sys/dirhash.h>
62
63 #include "udf.h"
64 #include "udf_subr.h"
65 #include "udf_bswap.h"
66
67
68 #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
69
70 /* forward declarations */
71 static int udf_do_readlink(struct udf_node *udf_node, uint64_t filesize,
72 uint8_t *targetbuf, int *length);
73
74 /* externs */
75 extern int prtactive;
76
77 /* implementations of vnode functions; table follows at end */
78 /* --------------------------------------------------------------------- */
79
80 int
81 udf_inactive(void *v)
82 {
83 struct vop_inactive_v2_args /* {
84 struct vnode *a_vp;
85 bool *a_recycle;
86 } */ *ap = v;
87 struct vnode *vp = ap->a_vp;
88 struct udf_node *udf_node = VTOI(vp);
89 int refcnt;
90
91 DPRINTF(NODE, ("udf_inactive called for udf_node %p\n", VTOI(vp)));
92
93 if (udf_node == NULL) {
94 DPRINTF(NODE, ("udf_inactive: inactive NULL UDF node\n"));
95 return 0;
96 }
97
98 /*
99 * Optionally flush metadata to disc.
100 */
101 if (udf_node->fe) {
102 refcnt = udf_rw16(udf_node->fe->link_cnt);
103 } else {
104 assert(udf_node->efe);
105 refcnt = udf_rw16(udf_node->efe->link_cnt);
106 }
107
108 if ((refcnt == 0) && (vp->v_vflag & VV_SYSTEM)) {
109 DPRINTF(VOLUMES, ("UDF_INACTIVE deleting VV_SYSTEM\n"));
110 /* system nodes are not writen out on inactive, so flush */
111 udf_node->i_flags = 0;
112 }
113
114 *ap->a_recycle = false;
115 if ((refcnt == 0) && ((vp->v_vflag & VV_SYSTEM) == 0)) {
116 *ap->a_recycle = true;
117 return 0;
118 }
119
120 /* write out its node */
121 if (udf_node->i_flags & (IN_CHANGE | IN_UPDATE | IN_MODIFIED))
122 udf_update(vp, NULL, NULL, NULL, 0);
123
124 return 0;
125 }
126
127 /* --------------------------------------------------------------------- */
128
129 int
130 udf_reclaim(void *v)
131 {
132 struct vop_reclaim_v2_args /* {
133 struct vnode *a_vp;
134 } */ *ap = v;
135 struct vnode *vp = ap->a_vp;
136 struct udf_node *udf_node = VTOI(vp);
137 int refcnt;
138
139 VOP_UNLOCK(vp);
140
141 DPRINTF(NODE, ("udf_reclaim called for node %p\n", udf_node));
142 if (prtactive && vp->v_usecount > 1)
143 vprint("udf_reclaim(): pushing active", vp);
144
145 if (udf_node == NULL) {
146 DPRINTF(NODE, ("udf_reclaim(): null udfnode\n"));
147 return 0;
148 }
149
150 /*
151 * If the file has not been referenced anymore in a directory
152 * we ought to free up the resources on disc if applicable.
153 */
154 if (udf_node->fe) {
155 refcnt = udf_rw16(udf_node->fe->link_cnt);
156 } else {
157 assert(udf_node->efe);
158 refcnt = udf_rw16(udf_node->efe->link_cnt);
159 }
160
161 if ((refcnt == 0) && ((vp->v_vflag & VV_SYSTEM) == 0)) {
162 /* remove this file's allocation */
163 DPRINTF(NODE, ("udf_inactive deleting unlinked file\n"));
164 udf_delete_node(udf_node);
165 }
166
167 /* update note for closure */
168 udf_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
169
170 /* async check to see if all node descriptors are written out */
171 while ((volatile int) udf_node->outstanding_nodedscr > 0) {
172 vprint("udf_reclaim(): waiting for writeout\n", vp);
173 tsleep(&udf_node->outstanding_nodedscr, PRIBIO, "recl wait", hz/8);
174 }
175
176 /* dispose all node knowledge */
177 udf_dispose_node(udf_node);
178
179 return 0;
180 }
181
182 /* --------------------------------------------------------------------- */
183
184 int
185 udf_read(void *v)
186 {
187 struct vop_read_args /* {
188 struct vnode *a_vp;
189 struct uio *a_uio;
190 int a_ioflag;
191 kauth_cred_t a_cred;
192 } */ *ap = v;
193 struct vnode *vp = ap->a_vp;
194 struct uio *uio = ap->a_uio;
195 int ioflag = ap->a_ioflag;
196 int advice = IO_ADV_DECODE(ap->a_ioflag);
197 struct uvm_object *uobj;
198 struct udf_node *udf_node = VTOI(vp);
199 struct file_entry *fe;
200 struct extfile_entry *efe;
201 uint64_t file_size;
202 vsize_t len;
203 int error;
204
205 /*
206 * XXX reading from extended attributes not yet implemented. FreeBSD
207 * has it in mind to forward the IO_EXT read call to the
208 * VOP_READEXTATTR().
209 */
210
211 DPRINTF(READ, ("udf_read called\n"));
212
213 /* can this happen? some filingsystems have this check */
214 if (uio->uio_offset < 0)
215 return EINVAL;
216 if (uio->uio_resid == 0)
217 return 0;
218
219 /* protect against rogue programs reading raw directories and links */
220 if ((ioflag & IO_ALTSEMANTICS) == 0) {
221 if (vp->v_type == VDIR)
222 return EISDIR;
223 /* all but regular files just give EINVAL */
224 if (vp->v_type != VREG)
225 return EINVAL;
226 }
227
228 assert(udf_node);
229 assert(udf_node->fe || udf_node->efe);
230
231 /* get file/directory filesize */
232 if (udf_node->fe) {
233 fe = udf_node->fe;
234 file_size = udf_rw64(fe->inf_len);
235 } else {
236 assert(udf_node->efe);
237 efe = udf_node->efe;
238 file_size = udf_rw64(efe->inf_len);
239 }
240
241 /* read contents using buffercache */
242 uobj = &vp->v_uobj;
243 error = 0;
244 while (uio->uio_resid > 0) {
245 /* reached end? */
246 if (file_size <= uio->uio_offset)
247 break;
248
249 /* maximise length to file extremity */
250 len = MIN(file_size - uio->uio_offset, uio->uio_resid);
251 if (len == 0)
252 break;
253
254 /* ubc, here we come, prepare to trap */
255 error = ubc_uiomove(uobj, uio, len, advice,
256 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
257 if (error)
258 break;
259 }
260
261 /* note access time unless not requested */
262 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
263 udf_node->i_flags |= IN_ACCESS;
264 if ((ioflag & IO_SYNC) == IO_SYNC) {
265 int uerror;
266
267 uerror = udf_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
268 if (error == 0)
269 error = uerror;
270 }
271 }
272
273 return error;
274 }
275
276 /* --------------------------------------------------------------------- */
277
278 int
279 udf_write(void *v)
280 {
281 struct vop_write_args /* {
282 struct vnode *a_vp;
283 struct uio *a_uio;
284 int a_ioflag;
285 kauth_cred_t a_cred;
286 } */ *ap = v;
287 struct vnode *vp = ap->a_vp;
288 struct uio *uio = ap->a_uio;
289 int ioflag = ap->a_ioflag;
290 kauth_cred_t cred = ap->a_cred;
291 int advice = IO_ADV_DECODE(ap->a_ioflag);
292 struct uvm_object *uobj;
293 struct udf_node *udf_node = VTOI(vp);
294 struct file_entry *fe;
295 struct extfile_entry *efe;
296 uint64_t file_size, old_size, old_offset;
297 vsize_t len;
298 int aflag = ioflag & IO_SYNC ? B_SYNC : 0;
299 int error;
300 int resid, extended;
301
302 /*
303 * XXX writing to extended attributes not yet implemented. FreeBSD has
304 * it in mind to forward the IO_EXT read call to the
305 * VOP_READEXTATTR().
306 */
307
308 DPRINTF(WRITE, ("udf_write called\n"));
309
310 /* can this happen? some filingsystems have this check */
311 if (uio->uio_offset < 0)
312 return EINVAL;
313 if (uio->uio_resid == 0)
314 return 0;
315
316 /* protect against rogue programs writing raw directories or links */
317 if ((ioflag & IO_ALTSEMANTICS) == 0) {
318 if (vp->v_type == VDIR)
319 return EISDIR;
320 /* all but regular files just give EINVAL for now */
321 if (vp->v_type != VREG)
322 return EINVAL;
323 }
324
325 assert(udf_node);
326 assert(udf_node->fe || udf_node->efe);
327
328 /* get file/directory filesize */
329 if (udf_node->fe) {
330 fe = udf_node->fe;
331 file_size = udf_rw64(fe->inf_len);
332 } else {
333 assert(udf_node->efe);
334 efe = udf_node->efe;
335 file_size = udf_rw64(efe->inf_len);
336 }
337 old_size = file_size;
338
339 /* if explicitly asked to append, uio_offset can be wrong? */
340 if (ioflag & IO_APPEND)
341 uio->uio_offset = file_size;
342
343 extended = (uio->uio_offset + uio->uio_resid > file_size);
344 if (extended) {
345 DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n",
346 file_size, uio->uio_offset + uio->uio_resid));
347 error = udf_grow_node(udf_node, uio->uio_offset + uio->uio_resid);
348 if (error)
349 return error;
350 file_size = uio->uio_offset + uio->uio_resid;
351 }
352
353 /* write contents using buffercache */
354 uobj = &vp->v_uobj;
355 resid = uio->uio_resid;
356 error = 0;
357
358 uvm_vnp_setwritesize(vp, file_size);
359 old_offset = uio->uio_offset;
360 while (uio->uio_resid > 0) {
361 /* maximise length to file extremity */
362 len = MIN(file_size - uio->uio_offset, uio->uio_resid);
363 if (len == 0)
364 break;
365
366 genfs_node_wrlock(vp);
367 error = GOP_ALLOC(vp, uio->uio_offset, len, aflag, cred);
368 genfs_node_unlock(vp);
369 if (error)
370 break;
371
372 /* ubc, here we come, prepare to trap */
373 error = ubc_uiomove(uobj, uio, len, advice,
374 UBC_WRITE | UBC_UNMAP_FLAG(vp));
375 if (error)
376 break;
377
378 /*
379 * flush what we just wrote if necessary.
380 * XXXUBC simplistic async flushing.
381 *
382 * Directories are excluded since its file data that we want
383 * to purge.
384 */
385 if ((vp->v_type != VDIR) &&
386 (old_offset >> 16 != uio->uio_offset >> 16)) {
387 mutex_enter(vp->v_interlock);
388 error = VOP_PUTPAGES(vp, (old_offset >> 16) << 16,
389 (uio->uio_offset >> 16) << 16,
390 PGO_CLEANIT | PGO_LAZY);
391 old_offset = uio->uio_offset;
392 }
393 }
394 uvm_vnp_setsize(vp, file_size);
395
396 /* mark node changed and request update */
397 udf_node->i_flags |= IN_CHANGE | IN_UPDATE;
398 if (vp->v_mount->mnt_flag & MNT_RELATIME)
399 udf_node->i_flags |= IN_ACCESS;
400
401 /*
402 * XXX TODO FFS has code here to reset setuid & setgid when we're not
403 * the superuser as a precaution against tampering.
404 */
405
406 /* if we wrote a thing, note write action on vnode */
407 if (resid > uio->uio_resid)
408 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
409
410 if (error) {
411 /* bring back file size to its former size */
412 /* take notice of its errors? */
413 (void) udf_chsize(vp, (u_quad_t) old_size, cred);
414
415 /* roll back uio */
416 uio->uio_offset -= resid - uio->uio_resid;
417 uio->uio_resid = resid;
418 } else {
419 /* if we write and we're synchronous, update node */
420 if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC))
421 error = udf_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
422 }
423
424 return error;
425 }
426
427
428 /* --------------------------------------------------------------------- */
429
430 /*
431 * `Special' bmap functionality that translates all incomming requests to
432 * translate to vop_strategy() calls with the same blocknumbers effectively
433 * not translating at all.
434 */
435
436 int
437 udf_trivial_bmap(void *v)
438 {
439 struct vop_bmap_args /* {
440 struct vnode *a_vp;
441 daddr_t a_bn;
442 struct vnode **a_vpp;
443 daddr_t *a_bnp;
444 int *a_runp;
445 } */ *ap = v;
446 struct vnode *vp = ap->a_vp; /* our node */
447 struct vnode **vpp = ap->a_vpp; /* return node */
448 daddr_t *bnp = ap->a_bnp; /* translated */
449 daddr_t bn = ap->a_bn; /* origional */
450 int *runp = ap->a_runp;
451 struct udf_node *udf_node = VTOI(vp);
452 uint32_t lb_size;
453
454 /* get logical block size */
455 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
456
457 /* could return `-1' to indicate holes/zeros */
458 /* translate 1:1 */
459 *bnp = bn;
460
461 /* set the vnode to read the data from with strategy on itself */
462 if (vpp)
463 *vpp = vp;
464
465 /* set runlength of maximum block size */
466 if (runp)
467 *runp = MAXPHYS / lb_size; /* or with -1 ? */
468
469 /* return success */
470 return 0;
471 }
472
473 /* --------------------------------------------------------------------- */
474
475 int
476 udf_vfsstrategy(void *v)
477 {
478 struct vop_strategy_args /* {
479 struct vnode *a_vp;
480 struct buf *a_bp;
481 } */ *ap = v;
482 struct vnode *vp = ap->a_vp;
483 struct buf *bp = ap->a_bp;
484 struct udf_node *udf_node = VTOI(vp);
485 uint32_t lb_size, sectors;
486
487 DPRINTF(STRATEGY, ("udf_strategy called\n"));
488
489 /* check if we ought to be here */
490 if (vp->v_type == VBLK || vp->v_type == VCHR)
491 panic("udf_strategy: spec");
492
493 /* only filebuffers ought to be read/write by this, no descriptors */
494 assert(bp->b_blkno >= 0);
495
496 /* get sector size */
497 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
498
499 /* calculate length to fetch/store in sectors */
500 sectors = bp->b_bcount / lb_size;
501 assert(bp->b_bcount > 0);
502
503 /* NEVER assume later that this buffer is already translated */
504 /* bp->b_lblkno = bp->b_blkno; */
505
506 /* check assertions: we OUGHT to always get multiples of this */
507 assert(sectors * lb_size == bp->b_bcount);
508 __USE(sectors);
509
510 /* issue buffer */
511 if (bp->b_flags & B_READ) {
512 DPRINTF(STRATEGY, ("\tread vp %p buf %p (blk no %"PRIu64")"
513 ", for %d sectors\n",
514 vp, bp, bp->b_blkno, sectors));
515
516 /* read buffer from the udf_node, translate vtop on the way*/
517 udf_read_filebuf(udf_node, bp);
518 } else {
519 DPRINTF(STRATEGY, ("\twrite vp %p buf %p (blk no %"PRIu64")"
520 ", for %d sectors\n",
521 vp, bp, bp->b_blkno, sectors));
522
523 /* write buffer to the udf_node, translate vtop on the way*/
524 udf_write_filebuf(udf_node, bp);
525 }
526
527 return bp->b_error;
528 }
529
530 /* --------------------------------------------------------------------- */
531
532 int
533 udf_readdir(void *v)
534 {
535 struct vop_readdir_args /* {
536 struct vnode *a_vp;
537 struct uio *a_uio;
538 kauth_cred_t a_cred;
539 int *a_eofflag;
540 off_t **a_cookies;
541 int *a_ncookies;
542 } */ *ap = v;
543 struct uio *uio = ap->a_uio;
544 struct vnode *vp = ap->a_vp;
545 struct udf_node *udf_node = VTOI(vp);
546 struct file_entry *fe;
547 struct extfile_entry *efe;
548 struct fileid_desc *fid;
549 struct dirent *dirent;
550 uint64_t file_size, diroffset, transoffset;
551 uint32_t lb_size;
552 int error;
553
554 DPRINTF(READDIR, ("udf_readdir called\n"));
555
556 /* This operation only makes sense on directory nodes. */
557 if (vp->v_type != VDIR)
558 return ENOTDIR;
559
560 /* get directory filesize */
561 if (udf_node->fe) {
562 fe = udf_node->fe;
563 file_size = udf_rw64(fe->inf_len);
564 } else {
565 assert(udf_node->efe);
566 efe = udf_node->efe;
567 file_size = udf_rw64(efe->inf_len);
568 }
569
570 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK | M_ZERO);
571
572 /*
573 * Add `.' pseudo entry if at offset zero since its not in the fid
574 * stream
575 */
576 if (uio->uio_offset == 0) {
577 DPRINTF(READDIR, ("\t'.' inserted\n"));
578 strcpy(dirent->d_name, ".");
579 dirent->d_fileno = udf_get_node_id(&udf_node->loc);
580 dirent->d_type = DT_DIR;
581 dirent->d_namlen = strlen(dirent->d_name);
582 dirent->d_reclen = _DIRENT_SIZE(dirent);
583 uiomove(dirent, _DIRENT_SIZE(dirent), uio);
584
585 /* mark with magic value that we have done the dummy */
586 uio->uio_offset = UDF_DIRCOOKIE_DOT;
587 }
588
589 /* we are called just as long as we keep on pushing data in */
590 error = 0;
591 if (uio->uio_offset < file_size) {
592 /* allocate temporary space for fid */
593 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
594 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
595
596 if (uio->uio_offset == UDF_DIRCOOKIE_DOT)
597 uio->uio_offset = 0;
598
599 diroffset = uio->uio_offset;
600 transoffset = diroffset;
601 while (diroffset < file_size) {
602 DPRINTF(READDIR, ("\tread in fid stream\n"));
603 /* transfer a new fid/dirent */
604 error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
605 DPRINTFIF(READDIR, error, ("read error in read fid "
606 "stream : %d\n", error));
607 if (error)
608 break;
609
610 /*
611 * If there isn't enough space in the uio to return a
612 * whole dirent, break off read
613 */
614 if (uio->uio_resid < _DIRENT_SIZE(dirent))
615 break;
616
617 /* remember the last entry we transfered */
618 transoffset = diroffset;
619
620 /* skip deleted entries */
621 if (fid->file_char & UDF_FILE_CHAR_DEL)
622 continue;
623
624 /* skip not visible files */
625 if (fid->file_char & UDF_FILE_CHAR_VIS)
626 continue;
627
628 /* copy dirent to the caller */
629 DPRINTF(READDIR, ("\tread dirent `%s', type %d\n",
630 dirent->d_name, dirent->d_type));
631 uiomove(dirent, _DIRENT_SIZE(dirent), uio);
632 }
633
634 /* pass on last transfered offset */
635 uio->uio_offset = transoffset;
636 free(fid, M_UDFTEMP);
637 }
638
639 if (ap->a_eofflag)
640 *ap->a_eofflag = (uio->uio_offset >= file_size);
641
642 #ifdef DEBUG
643 if (udf_verbose & UDF_DEBUG_READDIR) {
644 printf("returning offset %d\n", (uint32_t) uio->uio_offset);
645 if (ap->a_eofflag)
646 printf("returning EOF ? %d\n", *ap->a_eofflag);
647 if (error)
648 printf("readdir returning error %d\n", error);
649 }
650 #endif
651
652 free(dirent, M_UDFTEMP);
653 return error;
654 }
655
656 /* --------------------------------------------------------------------- */
657
658 int
659 udf_lookup(void *v)
660 {
661 struct vop_lookup_v2_args /* {
662 struct vnode *a_dvp;
663 struct vnode **a_vpp;
664 struct componentname *a_cnp;
665 } */ *ap = v;
666 struct vnode *dvp = ap->a_dvp;
667 struct vnode **vpp = ap->a_vpp;
668 struct componentname *cnp = ap->a_cnp;
669 struct udf_node *dir_node, *res_node;
670 struct udf_mount *ump;
671 struct long_ad icb_loc;
672 mode_t mode;
673 uid_t d_uid;
674 gid_t d_gid;
675 const char *name;
676 int namelen, nameiop, islastcn, mounted_ro;
677 int error, found;
678
679 dir_node = VTOI(dvp);
680 ump = dir_node->ump;
681 *vpp = NULL;
682
683 DPRINTF(LOOKUP, ("udf_lookup called, lookup `%s`\n",
684 cnp->cn_nameptr));
685
686 /* simplify/clarification flags */
687 nameiop = cnp->cn_nameiop;
688 islastcn = cnp->cn_flags & ISLASTCN;
689 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY;
690
691 /* check exec/dirread permissions first */
692 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
693 if (error)
694 return error;
695
696 DPRINTF(LOOKUP, ("\taccess ok\n"));
697
698 /*
699 * If requesting a modify on the last path element on a read-only
700 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
701 */
702 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
703 return EROFS;
704
705 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
706 cnp->cn_nameptr));
707 /* look in the namecache */
708 if (cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
709 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
710 return *vpp == NULLVP ? ENOENT : 0;
711 }
712
713 DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
714
715 /*
716 * Obviously, the file is not (anymore) in the namecache, we have to
717 * search for it. There are three basic cases: '.', '..' and others.
718 *
719 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
720 */
721 error = 0;
722 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
723 DPRINTF(LOOKUP, ("\tlookup '.'\n"));
724 /* special case 1 '.' */
725 if (islastcn && cnp->cn_nameiop == RENAME) {
726 error = EISDIR;
727 goto out;
728 }
729 vref(dvp);
730 *vpp = dvp;
731 /* done */
732 goto done;
733 } else if (cnp->cn_flags & ISDOTDOT) {
734 /* special case 2 '..' */
735 DPRINTF(LOOKUP, ("\tlookup '..'\n"));
736
737 if (islastcn && cnp->cn_nameiop == RENAME) {
738 error = EINVAL;
739 goto out;
740 }
741
742 /* get our node */
743 name = "..";
744 namelen = 2;
745 error = udf_lookup_name_in_dir(dvp, name, namelen,
746 &icb_loc, &found);
747 if (error)
748 goto out;
749 if (!found)
750 error = ENOENT;
751
752 /* first unlock parent */
753 VOP_UNLOCK(dvp);
754
755 if (error == 0) {
756 DPRINTF(LOOKUP, ("\tfound '..'\n"));
757 /* try to create/reuse the node */
758 error = udf_get_node(ump, &icb_loc, &res_node);
759
760 if (!error) {
761 DPRINTF(LOOKUP,
762 ("\tnode retrieved/created OK\n"));
763 *vpp = res_node->vnode;
764 }
765 }
766
767 /* try to relock parent */
768 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
769 goto out;
770 }
771
772 /* all other files */
773 DPRINTF(LOOKUP, ("\tlookup file/dir in directory\n"));
774
775 /* lookup filename in the directory; location icb_loc */
776 name = cnp->cn_nameptr;
777 namelen = cnp->cn_namelen;
778 error = udf_lookup_name_in_dir(dvp, name, namelen,
779 &icb_loc, &found);
780 if (error)
781 goto out;
782 if (!found) {
783 DPRINTF(LOOKUP, ("\tNOT found\n"));
784 /*
785 * The entry was not found in the directory. This is
786 * valid if we are creating or renaming an entry and
787 * are working on the last component of the path name.
788 */
789 if (islastcn && (cnp->cn_nameiop == CREATE ||
790 cnp->cn_nameiop == RENAME)) {
791 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
792 if (error) {
793 goto out;
794 }
795 error = EJUSTRETURN;
796 } else {
797 error = ENOENT;
798 }
799 /* done */
800 goto done;
801 }
802
803 /*
804 * XXX NOTE tmpfs has a test here that tests that intermediate
805 * components i.e. not the last one ought to be either a directory or
806 * a link. It seems to function well without this code.
807 */
808
809 /* try to create/reuse the node */
810 error = udf_get_node(ump, &icb_loc, &res_node);
811 if (error)
812 goto out;
813
814 /* check permissions */
815 if (islastcn && (cnp->cn_nameiop == DELETE ||
816 cnp->cn_nameiop == RENAME) ) {
817 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
818 if (error) {
819 vput(res_node->vnode);
820 goto out;
821 }
822
823 /*
824 * Check if the directory has its sticky bit set. If so, ask
825 * for clearance since only the owner of a file or directory
826 * can remove/rename from taht directory.
827 */
828 mode = udf_getaccessmode(dir_node);
829 if ((mode & S_ISTXT) != 0) {
830 udf_getownership(dir_node, &d_uid, &d_gid);
831 error = kauth_authorize_vnode(cnp->cn_cred,
832 KAUTH_VNODE_DELETE, res_node->vnode,
833 dir_node->vnode, genfs_can_sticky(cnp->cn_cred,
834 d_uid, d_uid));
835 if (error) {
836 error = EPERM;
837 vput(res_node->vnode);
838 goto out;
839 }
840 }
841 }
842
843 *vpp = res_node->vnode;
844
845 done:
846 /*
847 * Store result in the cache if requested. If we are creating a file,
848 * the file might not be found and thus putting it into the namecache
849 * might be seen as negative caching.
850 */
851 if (nameiop != CREATE)
852 cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
853 cnp->cn_flags);
854
855 out:
856 if (error == 0 && *vpp != dvp)
857 VOP_UNLOCK(*vpp);
858 DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error));
859
860 return error;
861 }
862
863 /* --------------------------------------------------------------------- */
864
865 int
866 udf_getattr(void *v)
867 {
868 struct vop_getattr_args /* {
869 struct vnode *a_vp;
870 struct vattr *a_vap;
871 kauth_cred_t a_cred;
872 struct lwp *a_l;
873 } */ *ap = v;
874 struct vnode *vp = ap->a_vp;
875 struct udf_node *udf_node = VTOI(vp);
876 struct udf_mount *ump = udf_node->ump;
877 struct file_entry *fe = udf_node->fe;
878 struct extfile_entry *efe = udf_node->efe;
879 struct filetimes_extattr_entry *ft_extattr;
880 struct device_extattr_entry *devattr;
881 struct vattr *vap = ap->a_vap;
882 struct timestamp *atime, *mtime, *attrtime, *creatime;
883 uint64_t filesize, blkssize;
884 uint32_t nlink;
885 uint32_t offset, a_l;
886 uint8_t *filedata, *targetbuf;
887 uid_t uid;
888 gid_t gid;
889 int length, error;
890
891 DPRINTF(CALL, ("udf_getattr called\n"));
892
893 /* update times before we returning values */
894 udf_itimes(udf_node, NULL, NULL, NULL);
895
896 /* get descriptor information */
897 if (fe) {
898 nlink = udf_rw16(fe->link_cnt);
899 uid = (uid_t)udf_rw32(fe->uid);
900 gid = (gid_t)udf_rw32(fe->gid);
901 filesize = udf_rw64(fe->inf_len);
902 blkssize = udf_rw64(fe->logblks_rec);
903 atime = &fe->atime;
904 mtime = &fe->mtime;
905 attrtime = &fe->attrtime;
906 filedata = fe->data;
907
908 /* initial guess */
909 creatime = mtime;
910
911 /* check our extended attribute if present */
912 error = udf_extattr_search_intern(udf_node,
913 UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
914 if (!error) {
915 ft_extattr = (struct filetimes_extattr_entry *)
916 (filedata + offset);
917 if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
918 creatime = &ft_extattr->times[0];
919 }
920 } else {
921 assert(udf_node->efe);
922 nlink = udf_rw16(efe->link_cnt);
923 uid = (uid_t)udf_rw32(efe->uid);
924 gid = (gid_t)udf_rw32(efe->gid);
925 filesize = udf_rw64(efe->inf_len); /* XXX or obj_size? */
926 blkssize = udf_rw64(efe->logblks_rec);
927 atime = &efe->atime;
928 mtime = &efe->mtime;
929 attrtime = &efe->attrtime;
930 creatime = &efe->ctime;
931 filedata = efe->data;
932 }
933
934 /* do the uid/gid translation game */
935 if (uid == (uid_t) -1)
936 uid = ump->mount_args.anon_uid;
937 if (gid == (gid_t) -1)
938 gid = ump->mount_args.anon_gid;
939
940 /* fill in struct vattr with values from the node */
941 vattr_null(vap);
942 vap->va_type = vp->v_type;
943 vap->va_mode = udf_getaccessmode(udf_node);
944 vap->va_nlink = nlink;
945 vap->va_uid = uid;
946 vap->va_gid = gid;
947 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
948 vap->va_fileid = udf_get_node_id(&udf_node->loc); /* inode hash XXX */
949 vap->va_size = filesize;
950 vap->va_blocksize = udf_node->ump->discinfo.sector_size; /* wise? */
951
952 /*
953 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add
954 * 1 to the link count if its a directory we're requested attributes
955 * of.
956 */
957 if (vap->va_type == VDIR)
958 vap->va_nlink++;
959
960 /*
961 * BUG-ALERT: Posix requires the va_size to be pathlength for symbolic
962 * links.
963 */
964 if (vap->va_type == VLNK) {
965 /* claim temporary buffers for translation */
966 targetbuf = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
967 error = udf_do_readlink(udf_node, filesize, targetbuf, &length);
968 if (!error) {
969 vap->va_size = length;
970 KASSERT(length == strlen(targetbuf));
971 }
972 free(targetbuf, M_UDFTEMP);
973 /* XXX return error? */
974 }
975
976 /* access times */
977 udf_timestamp_to_timespec(ump, atime, &vap->va_atime);
978 udf_timestamp_to_timespec(ump, mtime, &vap->va_mtime);
979 udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime);
980 udf_timestamp_to_timespec(ump, creatime, &vap->va_birthtime);
981
982 vap->va_gen = 1; /* no multiple generations yes (!?) */
983 vap->va_flags = 0; /* no flags */
984 vap->va_bytes = blkssize * udf_node->ump->discinfo.sector_size;
985 vap->va_filerev = 1; /* TODO file revision numbers? */
986 vap->va_vaflags = 0;
987 /* TODO get vaflags from the extended attributes? */
988
989 if ((vap->va_type == VBLK) || (vap->va_type == VCHR)) {
990 error = udf_extattr_search_intern(udf_node,
991 UDF_DEVICESPEC_ATTR_NO, "",
992 &offset, &a_l);
993 /* if error, deny access */
994 if (error || (filedata == NULL)) {
995 vap->va_mode = 0; /* or v_type = VNON? */
996 } else {
997 devattr = (struct device_extattr_entry *)
998 filedata + offset;
999 vap->va_rdev = makedev(
1000 udf_rw32(devattr->major),
1001 udf_rw32(devattr->minor)
1002 );
1003 /* TODO we could check the implementator */
1004 }
1005 }
1006
1007 return 0;
1008 }
1009
1010 /* --------------------------------------------------------------------- */
1011
1012 static int
1013 udf_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
1014 kauth_cred_t cred)
1015 {
1016 struct udf_node *udf_node = VTOI(vp);
1017 uid_t uid;
1018 gid_t gid;
1019 int error;
1020
1021 #ifdef notyet
1022 /* TODO get vaflags from the extended attributes? */
1023 /* Immutable or append-only files cannot be modified, either. */
1024 if (udf_node->flags & (IMMUTABLE | APPEND))
1025 return EPERM;
1026 #endif
1027
1028 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1029 return EROFS;
1030
1031 /* retrieve old values */
1032 udf_getownership(udf_node, &uid, &gid);
1033
1034 /* only one could be specified */
1035 if (new_uid == VNOVAL)
1036 new_uid = uid;
1037 if (new_gid == VNOVAL)
1038 new_gid = gid;
1039
1040 /* check if we can fit it in an 32 bits */
1041 if ((uid_t) ((uint32_t) new_uid) != new_uid)
1042 return EINVAL;
1043 if ((gid_t) ((uint32_t) new_gid) != new_gid)
1044 return EINVAL;
1045
1046 /* check permissions */
1047 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP,
1048 vp, NULL, genfs_can_chown(cred, uid, gid, new_uid, new_gid));
1049 if (error)
1050 return (error);
1051
1052 /* change the ownership */
1053 udf_setownership(udf_node, new_uid, new_gid);
1054
1055 /* mark node changed */
1056 udf_node->i_flags |= IN_CHANGE;
1057
1058 return 0;
1059 }
1060
1061
1062 static int
1063 udf_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
1064 {
1065 struct udf_node *udf_node = VTOI(vp);
1066 uid_t uid;
1067 gid_t gid;
1068 int error;
1069
1070 #ifdef notyet
1071 /* TODO get vaflags from the extended attributes? */
1072 /* Immutable or append-only files cannot be modified, either. */
1073 if (udf_node->flags & (IMMUTABLE | APPEND))
1074 return EPERM;
1075 #endif
1076
1077 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1078 return EROFS;
1079
1080 /* retrieve uid/gid values */
1081 udf_getownership(udf_node, &uid, &gid);
1082
1083 /* check permissions */
1084 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
1085 NULL, genfs_can_chmod(vp->v_type, cred, uid, gid, mode));
1086 if (error)
1087 return (error);
1088
1089 /* change mode */
1090 udf_setaccessmode(udf_node, mode);
1091
1092 /* mark node changed */
1093 udf_node->i_flags |= IN_CHANGE;
1094
1095 return 0;
1096 }
1097
1098
1099 /* exported */
1100 int
1101 udf_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
1102 {
1103 struct udf_node *udf_node = VTOI(vp);
1104 int error, extended;
1105
1106 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1107 return EROFS;
1108
1109 /* Decide whether this is a valid operation based on the file type. */
1110 switch (vp->v_type) {
1111 case VDIR:
1112 return EISDIR;
1113 case VREG:
1114 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1115 return EROFS;
1116 break;
1117 case VBLK:
1118 /* FALLTHROUGH */
1119 case VCHR:
1120 /* FALLTHROUGH */
1121 case VFIFO:
1122 /* Allow modifications of special files even if in the file
1123 * system is mounted read-only (we are not modifying the
1124 * files themselves, but the objects they represent). */
1125 return 0;
1126 default:
1127 /* Anything else is unsupported. */
1128 return EOPNOTSUPP;
1129 }
1130
1131 #if notyet
1132 /* TODO get vaflags from the extended attributes? */
1133 /* Immutable or append-only files cannot be modified, either. */
1134 if (node->flags & (IMMUTABLE | APPEND))
1135 return EPERM;
1136 #endif
1137
1138 /* resize file to the requested size */
1139 error = udf_resize_node(udf_node, newsize, &extended);
1140
1141 if (error == 0) {
1142 /* mark change */
1143 udf_node->i_flags |= IN_CHANGE | IN_MODIFY;
1144 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1145 udf_node->i_flags |= IN_ACCESS;
1146 VN_KNOTE(vp, NOTE_ATTRIB | (extended ? NOTE_EXTEND : 0));
1147 udf_update(vp, NULL, NULL, NULL, 0);
1148 }
1149
1150 return error;
1151 }
1152
1153
1154 static int
1155 udf_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
1156 {
1157 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1158 return EROFS;
1159
1160 /*
1161 * XXX we can't do this yet, as its not described in the standard yet
1162 */
1163
1164 return EOPNOTSUPP;
1165 }
1166
1167
1168 static int
1169 udf_chtimes(struct vnode *vp,
1170 struct timespec *atime, struct timespec *mtime,
1171 struct timespec *birthtime, int setattrflags,
1172 kauth_cred_t cred)
1173 {
1174 struct udf_node *udf_node = VTOI(vp);
1175 uid_t uid;
1176 gid_t gid;
1177 int error;
1178
1179 #ifdef notyet
1180 /* TODO get vaflags from the extended attributes? */
1181 /* Immutable or append-only files cannot be modified, either. */
1182 if (udf_node->flags & (IMMUTABLE | APPEND))
1183 return EPERM;
1184 #endif
1185
1186 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1187 return EROFS;
1188
1189 /* retrieve uid/gid values */
1190 udf_getownership(udf_node, &uid, &gid);
1191
1192 /* check permissions */
1193 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
1194 NULL, genfs_can_chtimes(vp, setattrflags, uid, cred));
1195 if (error)
1196 return (error);
1197
1198 /* update node flags depending on what times are passed */
1199 if (atime->tv_sec != VNOVAL)
1200 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
1201 udf_node->i_flags |= IN_ACCESS;
1202 if ((mtime->tv_sec != VNOVAL) || (birthtime->tv_sec != VNOVAL)) {
1203 udf_node->i_flags |= IN_CHANGE | IN_UPDATE;
1204 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1205 udf_node->i_flags |= IN_ACCESS;
1206 }
1207
1208 return udf_update(vp, atime, mtime, birthtime, 0);
1209 }
1210
1211
1212 int
1213 udf_setattr(void *v)
1214 {
1215 struct vop_setattr_args /* {
1216 struct vnode *a_vp;
1217 struct vattr *a_vap;
1218 kauth_cred_t a_cred;
1219 struct lwp *a_l;
1220 } */ *ap = v;
1221 struct vnode *vp = ap->a_vp;
1222 /* struct udf_node *udf_node = VTOI(vp); */
1223 /* struct udf_mount *ump = udf_node->ump; */
1224 kauth_cred_t cred = ap->a_cred;
1225 struct vattr *vap = ap->a_vap;
1226 int error;
1227
1228 DPRINTF(CALL, ("udf_setattr called\n"));
1229
1230 /* Abort if any unsettable attribute is given. */
1231 error = 0;
1232 if (vap->va_type != VNON ||
1233 vap->va_nlink != VNOVAL ||
1234 vap->va_fsid != VNOVAL ||
1235 vap->va_fileid != VNOVAL ||
1236 vap->va_blocksize != VNOVAL ||
1237 #ifdef notyet
1238 /* checks are debated */
1239 vap->va_ctime.tv_sec != VNOVAL ||
1240 vap->va_ctime.tv_nsec != VNOVAL ||
1241 vap->va_birthtime.tv_sec != VNOVAL ||
1242 vap->va_birthtime.tv_nsec != VNOVAL ||
1243 #endif
1244 vap->va_gen != VNOVAL ||
1245 vap->va_rdev != VNOVAL ||
1246 vap->va_bytes != VNOVAL)
1247 error = EINVAL;
1248
1249 DPRINTF(ATTR, ("setattr changing:\n"));
1250 if (error == 0 && (vap->va_flags != VNOVAL)) {
1251 DPRINTF(ATTR, ("\tchflags\n"));
1252 error = udf_chflags(vp, vap->va_flags, cred);
1253 }
1254
1255 if (error == 0 && (vap->va_size != VNOVAL)) {
1256 DPRINTF(ATTR, ("\tchsize\n"));
1257 error = udf_chsize(vp, vap->va_size, cred);
1258 }
1259
1260 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) {
1261 DPRINTF(ATTR, ("\tchown\n"));
1262 error = udf_chown(vp, vap->va_uid, vap->va_gid, cred);
1263 }
1264
1265 if (error == 0 && (vap->va_mode != VNOVAL)) {
1266 DPRINTF(ATTR, ("\tchmod\n"));
1267 error = udf_chmod(vp, vap->va_mode, cred);
1268 }
1269
1270 if (error == 0 &&
1271 ((vap->va_atime.tv_sec != VNOVAL &&
1272 vap->va_atime.tv_nsec != VNOVAL) ||
1273 (vap->va_mtime.tv_sec != VNOVAL &&
1274 vap->va_mtime.tv_nsec != VNOVAL))
1275 ) {
1276 DPRINTF(ATTR, ("\tchtimes\n"));
1277 error = udf_chtimes(vp, &vap->va_atime, &vap->va_mtime,
1278 &vap->va_birthtime, vap->va_vaflags, cred);
1279 }
1280 VN_KNOTE(vp, NOTE_ATTRIB);
1281
1282 return error;
1283 }
1284
1285 /* --------------------------------------------------------------------- */
1286
1287 /*
1288 * Return POSIX pathconf information for UDF file systems.
1289 */
1290 int
1291 udf_pathconf(void *v)
1292 {
1293 struct vop_pathconf_args /* {
1294 struct vnode *a_vp;
1295 int a_name;
1296 register_t *a_retval;
1297 } */ *ap = v;
1298 uint32_t bits;
1299
1300 DPRINTF(CALL, ("udf_pathconf called\n"));
1301
1302 switch (ap->a_name) {
1303 case _PC_LINK_MAX:
1304 *ap->a_retval = (1<<16)-1; /* 16 bits */
1305 return 0;
1306 case _PC_NAME_MAX:
1307 *ap->a_retval = UDF_MAXNAMLEN;
1308 return 0;
1309 case _PC_PATH_MAX:
1310 *ap->a_retval = PATH_MAX;
1311 return 0;
1312 case _PC_PIPE_BUF:
1313 *ap->a_retval = PIPE_BUF;
1314 return 0;
1315 case _PC_CHOWN_RESTRICTED:
1316 *ap->a_retval = 1;
1317 return 0;
1318 case _PC_NO_TRUNC:
1319 *ap->a_retval = 1;
1320 return 0;
1321 case _PC_SYNC_IO:
1322 *ap->a_retval = 0; /* synchronised is off for performance */
1323 return 0;
1324 case _PC_FILESIZEBITS:
1325 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
1326 bits = 64; /* XXX ought to deliver 65 */
1327 #if 0
1328 if (udf_node)
1329 bits = 64 * vp->v_mount->mnt_dev_bshift;
1330 #endif
1331 *ap->a_retval = bits;
1332 return 0;
1333 }
1334
1335 return EINVAL;
1336 }
1337
1338
1339 /* --------------------------------------------------------------------- */
1340
1341 int
1342 udf_open(void *v)
1343 {
1344 struct vop_open_args /* {
1345 struct vnode *a_vp;
1346 int a_mode;
1347 kauth_cred_t a_cred;
1348 struct proc *a_p;
1349 } */ *ap = v;
1350 int flags;
1351
1352 DPRINTF(CALL, ("udf_open called\n"));
1353
1354 /*
1355 * Files marked append-only must be opened for appending.
1356 * TODO: get chflags(2) flags from extened attribute.
1357 */
1358 flags = 0;
1359 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
1360 return (EPERM);
1361
1362 return 0;
1363 }
1364
1365
1366 /* --------------------------------------------------------------------- */
1367
1368 int
1369 udf_close(void *v)
1370 {
1371 struct vop_close_args /* {
1372 struct vnode *a_vp;
1373 int a_fflag;
1374 kauth_cred_t a_cred;
1375 struct proc *a_p;
1376 } */ *ap = v;
1377 struct vnode *vp = ap->a_vp;
1378 struct udf_node *udf_node = VTOI(vp);
1379 int async = vp->v_mount->mnt_flag & MNT_ASYNC;
1380 int error;
1381
1382 DPRINTF(CALL, ("udf_close called\n"));
1383 udf_node = udf_node; /* shut up gcc */
1384
1385 if (!async && (vp->v_type != VDIR)) {
1386 mutex_enter(vp->v_interlock);
1387 error = VOP_PUTPAGES(vp, 0, 0, PGO_CLEANIT);
1388 if (error)
1389 return error;
1390 }
1391
1392 mutex_enter(vp->v_interlock);
1393 if (vp->v_usecount > 1)
1394 udf_itimes(udf_node, NULL, NULL, NULL);
1395 mutex_exit(vp->v_interlock);
1396
1397 return 0;
1398 }
1399
1400
1401 /* --------------------------------------------------------------------- */
1402
1403 static int
1404 udf_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
1405 {
1406 int flags;
1407
1408 /* check if we are allowed to write */
1409 switch (vap->va_type) {
1410 case VDIR:
1411 case VLNK:
1412 case VREG:
1413 /*
1414 * normal nodes: check if we're on a read-only mounted
1415 * filingsystem and bomb out if we're trying to write.
1416 */
1417 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
1418 return EROFS;
1419 break;
1420 case VBLK:
1421 case VCHR:
1422 case VSOCK:
1423 case VFIFO:
1424 /*
1425 * special nodes: even on read-only mounted filingsystems
1426 * these are allowed to be written to if permissions allow.
1427 */
1428 break;
1429 default:
1430 /* no idea what this is */
1431 return EINVAL;
1432 }
1433
1434 /* noone may write immutable files */
1435 /* TODO: get chflags(2) flags from extened attribute. */
1436 flags = 0;
1437 if ((mode & VWRITE) && (flags & IMMUTABLE))
1438 return EPERM;
1439
1440 return 0;
1441 }
1442
1443 static int
1444 udf_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
1445 kauth_cred_t cred)
1446 {
1447 /* ask the generic genfs_can_access to advice on security */
1448 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
1449 vp->v_type, vap->va_mode), vp, NULL, genfs_can_access(vp->v_type,
1450 vap->va_mode, vap->va_uid, vap->va_gid, mode, cred));
1451 }
1452
1453 int
1454 udf_access(void *v)
1455 {
1456 struct vop_access_args /* {
1457 struct vnode *a_vp;
1458 int a_mode;
1459 kauth_cred_t a_cred;
1460 struct proc *a_p;
1461 } */ *ap = v;
1462 struct vnode *vp = ap->a_vp;
1463 mode_t mode = ap->a_mode;
1464 kauth_cred_t cred = ap->a_cred;
1465 /* struct udf_node *udf_node = VTOI(vp); */
1466 struct vattr vap;
1467 int error;
1468
1469 DPRINTF(CALL, ("udf_access called\n"));
1470
1471 error = VOP_GETATTR(vp, &vap, NULL);
1472 if (error)
1473 return error;
1474
1475 error = udf_check_possible(vp, &vap, mode);
1476 if (error)
1477 return error;
1478
1479 error = udf_check_permitted(vp, &vap, mode, cred);
1480
1481 return error;
1482 }
1483
1484 /* --------------------------------------------------------------------- */
1485
1486 int
1487 udf_create(void *v)
1488 {
1489 struct vop_create_v3_args /* {
1490 struct vnode *a_dvp;
1491 struct vnode **a_vpp;
1492 struct componentname *a_cnp;
1493 struct vattr *a_vap;
1494 } */ *ap = v;
1495 struct vnode *dvp = ap->a_dvp;
1496 struct vnode **vpp = ap->a_vpp;
1497 struct vattr *vap = ap->a_vap;
1498 struct componentname *cnp = ap->a_cnp;
1499 int error;
1500
1501 DPRINTF(CALL, ("udf_create called\n"));
1502 error = udf_create_node(dvp, vpp, vap, cnp);
1503
1504 return error;
1505 }
1506
1507 /* --------------------------------------------------------------------- */
1508
1509 int
1510 udf_mknod(void *v)
1511 {
1512 struct vop_mknod_v3_args /* {
1513 struct vnode *a_dvp;
1514 struct vnode **a_vpp;
1515 struct componentname *a_cnp;
1516 struct vattr *a_vap;
1517 } */ *ap = v;
1518 struct vnode *dvp = ap->a_dvp;
1519 struct vnode **vpp = ap->a_vpp;
1520 struct vattr *vap = ap->a_vap;
1521 struct componentname *cnp = ap->a_cnp;
1522 int error;
1523
1524 DPRINTF(CALL, ("udf_mknod called\n"));
1525 error = udf_create_node(dvp, vpp, vap, cnp);
1526
1527 return error;
1528 }
1529
1530 /* --------------------------------------------------------------------- */
1531
1532 int
1533 udf_mkdir(void *v)
1534 {
1535 struct vop_mkdir_v3_args /* {
1536 struct vnode *a_dvp;
1537 struct vnode **a_vpp;
1538 struct componentname *a_cnp;
1539 struct vattr *a_vap;
1540 } */ *ap = v;
1541 struct vnode *dvp = ap->a_dvp;
1542 struct vnode **vpp = ap->a_vpp;
1543 struct vattr *vap = ap->a_vap;
1544 struct componentname *cnp = ap->a_cnp;
1545 int error;
1546
1547 DPRINTF(CALL, ("udf_mkdir called\n"));
1548 error = udf_create_node(dvp, vpp, vap, cnp);
1549
1550 return error;
1551 }
1552
1553 /* --------------------------------------------------------------------- */
1554
1555 static int
1556 udf_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1557 {
1558 struct udf_node *udf_node, *dir_node;
1559 struct vattr vap;
1560 int error;
1561
1562 DPRINTF(CALL, ("udf_link called\n"));
1563 KASSERT(dvp != vp);
1564 KASSERT(vp->v_type != VDIR);
1565 KASSERT(dvp->v_mount == vp->v_mount);
1566
1567 /* get attributes */
1568 dir_node = VTOI(dvp);
1569 udf_node = VTOI(vp);
1570
1571 error = VOP_GETATTR(vp, &vap, FSCRED);
1572 if (error) {
1573 VOP_UNLOCK(vp);
1574 return error;
1575 }
1576
1577 /* check link count overflow */
1578 if (vap.va_nlink >= (1<<16)-1) { /* uint16_t */
1579 VOP_UNLOCK(vp);
1580 return EMLINK;
1581 }
1582
1583 error = udf_dir_attach(dir_node->ump, dir_node, udf_node, &vap, cnp);
1584 if (error)
1585 VOP_UNLOCK(vp);
1586 return error;
1587 }
1588
1589 int
1590 udf_link(void *v)
1591 {
1592 struct vop_link_v2_args /* {
1593 struct vnode *a_dvp;
1594 struct vnode *a_vp;
1595 struct componentname *a_cnp;
1596 } */ *ap = v;
1597 struct vnode *dvp = ap->a_dvp;
1598 struct vnode *vp = ap->a_vp;
1599 struct componentname *cnp = ap->a_cnp;
1600 int error;
1601
1602 error = udf_do_link(dvp, vp, cnp);
1603 if (error)
1604 VOP_ABORTOP(dvp, cnp);
1605
1606 VN_KNOTE(vp, NOTE_LINK);
1607 VN_KNOTE(dvp, NOTE_WRITE);
1608
1609 return error;
1610 }
1611
1612 /* --------------------------------------------------------------------- */
1613
1614 static int
1615 udf_do_symlink(struct udf_node *udf_node, char *target)
1616 {
1617 struct pathcomp pathcomp;
1618 uint8_t *pathbuf, *pathpos, *compnamepos;
1619 char *mntonname;
1620 int pathlen, len, compnamelen, mntonnamelen;
1621 int error;
1622
1623 /* process `target' to an UDF structure */
1624 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
1625 pathpos = pathbuf;
1626 pathlen = 0;
1627
1628 if (*target == '/') {
1629 /* symlink starts from the root */
1630 len = UDF_PATH_COMP_SIZE;
1631 memset(&pathcomp, 0, len);
1632 pathcomp.type = UDF_PATH_COMP_ROOT;
1633
1634 /* check if its mount-point relative! */
1635 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
1636 mntonnamelen = strlen(mntonname);
1637 if (strlen(target) >= mntonnamelen) {
1638 if (strncmp(target, mntonname, mntonnamelen) == 0) {
1639 pathcomp.type = UDF_PATH_COMP_MOUNTROOT;
1640 target += mntonnamelen;
1641 }
1642 } else {
1643 target++;
1644 }
1645
1646 memcpy(pathpos, &pathcomp, len);
1647 pathpos += len;
1648 pathlen += len;
1649 }
1650
1651 error = 0;
1652 while (*target) {
1653 /* ignore multiple '/' */
1654 while (*target == '/') {
1655 target++;
1656 }
1657 if (!*target)
1658 break;
1659
1660 /* extract component name */
1661 compnamelen = 0;
1662 compnamepos = target;
1663 while ((*target) && (*target != '/')) {
1664 target++;
1665 compnamelen++;
1666 }
1667
1668 /* just trunc if too long ?? (security issue) */
1669 if (compnamelen >= 127) {
1670 error = ENAMETOOLONG;
1671 break;
1672 }
1673
1674 /* convert unix name to UDF name */
1675 len = sizeof(struct pathcomp);
1676 memset(&pathcomp, 0, len);
1677 pathcomp.type = UDF_PATH_COMP_NAME;
1678 len = UDF_PATH_COMP_SIZE;
1679
1680 if ((compnamelen == 2) && (strncmp(compnamepos, "..", 2) == 0))
1681 pathcomp.type = UDF_PATH_COMP_PARENTDIR;
1682 if ((compnamelen == 1) && (*compnamepos == '.'))
1683 pathcomp.type = UDF_PATH_COMP_CURDIR;
1684
1685 if (pathcomp.type == UDF_PATH_COMP_NAME) {
1686 unix_to_udf_name(
1687 (char *) &pathcomp.ident, &pathcomp.l_ci,
1688 compnamepos, compnamelen,
1689 &udf_node->ump->logical_vol->desc_charset);
1690 len = UDF_PATH_COMP_SIZE + pathcomp.l_ci;
1691 }
1692
1693 if (pathlen + len >= UDF_SYMLINKBUFLEN) {
1694 error = ENAMETOOLONG;
1695 break;
1696 }
1697
1698 memcpy(pathpos, &pathcomp, len);
1699 pathpos += len;
1700 pathlen += len;
1701 }
1702
1703 if (error) {
1704 /* aparently too big */
1705 free(pathbuf, M_UDFTEMP);
1706 return error;
1707 }
1708
1709 error = udf_grow_node(udf_node, pathlen);
1710 if (error) {
1711 /* failed to pregrow node */
1712 free(pathbuf, M_UDFTEMP);
1713 return error;
1714 }
1715
1716 /* write out structure on the new file */
1717 error = vn_rdwr(UIO_WRITE, udf_node->vnode,
1718 pathbuf, pathlen, 0,
1719 UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
1720 FSCRED, NULL, NULL);
1721
1722 /* return status of symlink contents writeout */
1723 free(pathbuf, M_UDFTEMP);
1724 return error;
1725 }
1726
1727
1728 int
1729 udf_symlink(void *v)
1730 {
1731 struct vop_symlink_v3_args /* {
1732 struct vnode *a_dvp;
1733 struct vnode **a_vpp;
1734 struct componentname *a_cnp;
1735 struct vattr *a_vap;
1736 char *a_target;
1737 } */ *ap = v;
1738 struct vnode *dvp = ap->a_dvp;
1739 struct vnode **vpp = ap->a_vpp;
1740 struct vattr *vap = ap->a_vap;
1741 struct componentname *cnp = ap->a_cnp;
1742 struct udf_node *dir_node;
1743 struct udf_node *udf_node;
1744 int error;
1745
1746 DPRINTF(CALL, ("udf_symlink called\n"));
1747 DPRINTF(CALL, ("\tlinking to `%s`\n", ap->a_target));
1748 error = udf_create_node(dvp, vpp, vap, cnp);
1749 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
1750 if (!error) {
1751 dir_node = VTOI(dvp);
1752 udf_node = VTOI(*vpp);
1753 KASSERT(udf_node);
1754 error = udf_do_symlink(udf_node, ap->a_target);
1755 if (error) {
1756 /* remove node */
1757 udf_dir_detach(udf_node->ump, dir_node, udf_node, cnp);
1758 vrele(*vpp);
1759 *vpp = NULL;
1760 }
1761 }
1762 return error;
1763 }
1764
1765 /* --------------------------------------------------------------------- */
1766
1767 static int
1768 udf_do_readlink(struct udf_node *udf_node, uint64_t filesize,
1769 uint8_t *targetbuf, int *length)
1770 {
1771 struct pathcomp pathcomp;
1772 uint8_t *pathbuf, *tmpname;
1773 uint8_t *pathpos, *targetpos;
1774 char *mntonname;
1775 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
1776 int first, error;
1777
1778 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
1779 tmpname = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
1780 memset(pathbuf, 0, UDF_SYMLINKBUFLEN);
1781 memset(targetbuf, 0, PATH_MAX);
1782
1783 /* read contents of file in our temporary buffer */
1784 error = vn_rdwr(UIO_READ, udf_node->vnode,
1785 pathbuf, filesize, 0,
1786 UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
1787 FSCRED, NULL, NULL);
1788 if (error) {
1789 /* failed to read in symlink contents */
1790 free(pathbuf, M_UDFTEMP);
1791 free(tmpname, M_UDFTEMP);
1792 return error;
1793 }
1794
1795 /* convert to a unix path */
1796 pathpos = pathbuf;
1797 pathlen = 0;
1798 targetpos = targetbuf;
1799 targetlen = PATH_MAX;
1800 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
1801 mntonnamelen = strlen(mntonname);
1802
1803 error = 0;
1804 first = 1;
1805 while (filesize - pathlen >= UDF_PATH_COMP_SIZE) {
1806 len = UDF_PATH_COMP_SIZE;
1807 memcpy(&pathcomp, pathpos, len);
1808 l_ci = pathcomp.l_ci;
1809 switch (pathcomp.type) {
1810 case UDF_PATH_COMP_ROOT :
1811 /* XXX should check for l_ci; bugcompatible now */
1812 if ((targetlen < 1) || !first) {
1813 error = EINVAL;
1814 break;
1815 }
1816 *targetpos++ = '/'; targetlen--;
1817 break;
1818 case UDF_PATH_COMP_MOUNTROOT :
1819 /* XXX what should it be if l_ci > 0 ? [4/48.16.1.2] */
1820 if (l_ci || (targetlen < mntonnamelen+1) || !first) {
1821 error = EINVAL;
1822 break;
1823 }
1824 memcpy(targetpos, mntonname, mntonnamelen);
1825 targetpos += mntonnamelen; targetlen -= mntonnamelen;
1826 if (filesize-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
1827 /* more follows, so must be directory */
1828 *targetpos++ = '/'; targetlen--;
1829 }
1830 break;
1831 case UDF_PATH_COMP_PARENTDIR :
1832 /* XXX should check for l_ci; bugcompatible now */
1833 if (targetlen < 3) {
1834 error = EINVAL;
1835 break;
1836 }
1837 *targetpos++ = '.'; targetlen--;
1838 *targetpos++ = '.'; targetlen--;
1839 *targetpos++ = '/'; targetlen--;
1840 break;
1841 case UDF_PATH_COMP_CURDIR :
1842 /* XXX should check for l_ci; bugcompatible now */
1843 if (targetlen < 2) {
1844 error = EINVAL;
1845 break;
1846 }
1847 *targetpos++ = '.'; targetlen--;
1848 *targetpos++ = '/'; targetlen--;
1849 break;
1850 case UDF_PATH_COMP_NAME :
1851 if (l_ci == 0) {
1852 error = EINVAL;
1853 break;
1854 }
1855 memset(tmpname, 0, PATH_MAX);
1856 memcpy(&pathcomp, pathpos, len + l_ci);
1857 udf_to_unix_name(tmpname, MAXPATHLEN,
1858 pathcomp.ident, l_ci,
1859 &udf_node->ump->logical_vol->desc_charset);
1860 namelen = strlen(tmpname);
1861 if (targetlen < namelen + 1) {
1862 error = EINVAL;
1863 break;
1864 }
1865 memcpy(targetpos, tmpname, namelen);
1866 targetpos += namelen; targetlen -= namelen;
1867 if (filesize-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
1868 /* more follows, so must be directory */
1869 *targetpos++ = '/'; targetlen--;
1870 }
1871 break;
1872 default :
1873 error = EINVAL;
1874 break;
1875 }
1876 first = 0;
1877 if (error)
1878 break;
1879 pathpos += UDF_PATH_COMP_SIZE + l_ci;
1880 pathlen += UDF_PATH_COMP_SIZE + l_ci;
1881
1882 }
1883 /* all processed? */
1884 if (filesize - pathlen > 0)
1885 error = EINVAL;
1886
1887 free(pathbuf, M_UDFTEMP);
1888 free(tmpname, M_UDFTEMP);
1889
1890 *length = PATH_MAX - targetlen;
1891 return error;
1892 }
1893
1894
1895 int
1896 udf_readlink(void *v)
1897 {
1898 struct vop_readlink_args /* {
1899 struct vnode *a_vp;
1900 struct uio *a_uio;
1901 kauth_cred_t a_cred;
1902 } */ *ap = v;
1903 struct vnode *vp = ap->a_vp;
1904 struct udf_node *udf_node = VTOI(vp);
1905 struct file_entry *fe = udf_node->fe;
1906 struct extfile_entry *efe = udf_node->efe;
1907 struct uio *uio = ap->a_uio;
1908 uint64_t filesize;
1909 uint8_t *targetbuf;
1910 int length;
1911 int error;
1912
1913 DPRINTF(CALL, ("udf_readlink called\n"));
1914
1915 if (fe) {
1916 filesize = udf_rw64(fe->inf_len);
1917 } else {
1918 assert(udf_node->efe);
1919 filesize = udf_rw64(efe->inf_len);
1920 }
1921
1922 /* claim temporary buffers for translation */
1923 targetbuf = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
1924
1925 error = udf_do_readlink(udf_node, filesize, targetbuf, &length);
1926
1927 /* uiomove() to destination */
1928 if (!error)
1929 uiomove(targetbuf, length, uio);
1930
1931 free(targetbuf, M_UDFTEMP);
1932 return error;
1933 }
1934
1935 /* --------------------------------------------------------------------- */
1936
1937 /*
1938 * udf_rename() moved to udf_rename.c
1939 */
1940
1941 /* --------------------------------------------------------------------- */
1942
1943 int
1944 udf_remove(void *v)
1945 {
1946 struct vop_remove_v2_args /* {
1947 struct vnode *a_dvp;
1948 struct vnode *a_vp;
1949 struct componentname *a_cnp;
1950 } */ *ap = v;
1951 struct vnode *dvp = ap->a_dvp;
1952 struct vnode *vp = ap->a_vp;
1953 struct componentname *cnp = ap->a_cnp;
1954 struct udf_node *dir_node = VTOI(dvp);
1955 struct udf_node *udf_node = VTOI(vp);
1956 struct udf_mount *ump = dir_node->ump;
1957 int error;
1958
1959 DPRINTF(CALL, ("udf_remove called\n"));
1960 if (vp->v_type != VDIR) {
1961 error = udf_dir_detach(ump, dir_node, udf_node, cnp);
1962 DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
1963 } else {
1964 DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
1965 error = EPERM;
1966 }
1967
1968 if (error == 0) {
1969 VN_KNOTE(vp, NOTE_DELETE);
1970 VN_KNOTE(dvp, NOTE_WRITE);
1971 }
1972
1973 if (dvp == vp)
1974 vrele(vp);
1975 else
1976 vput(vp);
1977
1978 return error;
1979 }
1980
1981 /* --------------------------------------------------------------------- */
1982
1983 int
1984 udf_rmdir(void *v)
1985 {
1986 struct vop_rmdir_v2_args /* {
1987 struct vnode *a_dvp;
1988 struct vnode *a_vp;
1989 struct componentname *a_cnp;
1990 } */ *ap = v;
1991 struct vnode *vp = ap->a_vp;
1992 struct vnode *dvp = ap->a_dvp;
1993 struct componentname *cnp = ap->a_cnp;
1994 struct udf_node *dir_node = VTOI(dvp);
1995 struct udf_node *udf_node = VTOI(vp);
1996 struct udf_mount *ump = dir_node->ump;
1997 int error, isempty;
1998
1999 DPRINTF(NOTIMPL, ("udf_rmdir '%s' called\n", cnp->cn_nameptr));
2000
2001 /* don't allow '.' to be deleted */
2002 if (dir_node == udf_node) {
2003 vrele(vp);
2004 return EINVAL;
2005 }
2006
2007 /* make sure our `leaf' node's hash is populated */
2008 dirhash_get(&udf_node->dir_hash);
2009 error = udf_dirhash_fill(udf_node);
2010 if (error) {
2011 dirhash_put(udf_node->dir_hash);
2012 return error;
2013 }
2014
2015 /* check to see if the directory is empty */
2016 isempty = dirhash_dir_isempty(udf_node->dir_hash);
2017 dirhash_put(udf_node->dir_hash);
2018
2019 if (!isempty) {
2020 vput(vp);
2021 return ENOTEMPTY;
2022 }
2023
2024 /* detach the node from the directory, udf_node is an empty dir here */
2025 error = udf_dir_detach(ump, dir_node, udf_node, cnp);
2026 if (error == 0) {
2027 cache_purge(vp);
2028 // cache_purge(dvp); /* XXX from msdosfs, why? */
2029 /*
2030 * Bug alert: we need to remove '..' from the detaching
2031 * udf_node so further lookups of this are not possible. This
2032 * prevents a process in a deleted directory from going to its
2033 * deleted parent. Since `udf_node' is garanteed to be empty
2034 * here, trunc it so no fids are there.
2035 */
2036 dirhash_purge(&udf_node->dir_hash);
2037 udf_shrink_node(udf_node, 0);
2038 VN_KNOTE(vp, NOTE_DELETE);
2039 }
2040 DPRINTFIF(NODE, error, ("\tgot error removing dir\n"));
2041
2042 /* put the node and exit */
2043 vput(vp);
2044
2045 return error;
2046 }
2047
2048 /* --------------------------------------------------------------------- */
2049
2050 int
2051 udf_fsync(void *v)
2052 {
2053 struct vop_fsync_args /* {
2054 struct vnode *a_vp;
2055 kauth_cred_t a_cred;
2056 int a_flags;
2057 off_t offlo;
2058 off_t offhi;
2059 struct proc *a_p;
2060 } */ *ap = v;
2061 struct vnode *vp = ap->a_vp;
2062 struct udf_node *udf_node = VTOI(vp);
2063 int error, flags, wait;
2064
2065 DPRINTF(SYNC, ("udf_fsync called on %p : %s, %s\n",
2066 udf_node,
2067 (ap->a_flags & FSYNC_WAIT) ? "wait":"no wait",
2068 (ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
2069
2070 /* flush data and wait for it when requested */
2071 wait = (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
2072 error = vflushbuf(vp, ap->a_flags);
2073 if (error)
2074 return error;
2075
2076 if (udf_node == NULL) {
2077 printf("udf_fsync() called on NULL udf_node!\n");
2078 return 0;
2079 }
2080 if (vp->v_tag != VT_UDF) {
2081 printf("udf_fsync() called on node not tagged as UDF node!\n");
2082 return 0;
2083 }
2084
2085 /* set our times */
2086 udf_itimes(udf_node, NULL, NULL, NULL);
2087
2088 /* if called when mounted readonly, never write back */
2089 if (vp->v_mount->mnt_flag & MNT_RDONLY)
2090 return 0;
2091
2092 /* if only data is requested, return */
2093 if (ap->a_flags & FSYNC_DATAONLY)
2094 return 0;
2095
2096 /* check if the node is dirty 'enough'*/
2097 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
2098 if (flags == 0)
2099 return 0;
2100
2101 /* if we don't have to wait, check for IO pending */
2102 if (!wait) {
2103 if (vp->v_numoutput > 0) {
2104 DPRINTF(SYNC, ("udf_fsync %p, rejecting on v_numoutput\n", udf_node));
2105 return 0;
2106 }
2107 if (udf_node->outstanding_bufs > 0) {
2108 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_bufs\n", udf_node));
2109 return 0;
2110 }
2111 if (udf_node->outstanding_nodedscr > 0) {
2112 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_nodedscr\n", udf_node));
2113 return 0;
2114 }
2115 }
2116
2117 /* wait until vp->v_numoutput reaches zero i.e. is finished */
2118 if (wait) {
2119 DPRINTF(SYNC, ("udf_fsync %p, waiting\n", udf_node));
2120 mutex_enter(vp->v_interlock);
2121 while (vp->v_numoutput) {
2122 DPRINTF(SYNC, ("udf_fsync %p, v_numoutput %d\n", udf_node, vp->v_numoutput));
2123 cv_timedwait(&vp->v_cv, vp->v_interlock, hz/8);
2124 }
2125 mutex_exit(vp->v_interlock);
2126 DPRINTF(SYNC, ("udf_fsync %p, fin wait\n", udf_node));
2127 }
2128
2129 /* write out node and wait for it if requested */
2130 DPRINTF(SYNC, ("udf_fsync %p, writeout node\n", udf_node));
2131 error = udf_writeout_node(udf_node, wait);
2132 if (error)
2133 return error;
2134
2135 /* TODO/XXX if ap->a_flags & FSYNC_CACHE, we ought to do a disc sync */
2136
2137 return 0;
2138 }
2139
2140 /* --------------------------------------------------------------------- */
2141
2142 int
2143 udf_advlock(void *v)
2144 {
2145 struct vop_advlock_args /* {
2146 struct vnode *a_vp;
2147 void *a_id;
2148 int a_op;
2149 struct flock *a_fl;
2150 int a_flags;
2151 } */ *ap = v;
2152 struct vnode *vp = ap->a_vp;
2153 struct udf_node *udf_node = VTOI(vp);
2154 struct file_entry *fe;
2155 struct extfile_entry *efe;
2156 uint64_t file_size;
2157
2158 DPRINTF(LOCKING, ("udf_advlock called\n"));
2159
2160 /* get directory filesize */
2161 if (udf_node->fe) {
2162 fe = udf_node->fe;
2163 file_size = udf_rw64(fe->inf_len);
2164 } else {
2165 assert(udf_node->efe);
2166 efe = udf_node->efe;
2167 file_size = udf_rw64(efe->inf_len);
2168 }
2169
2170 return lf_advlock(ap, &udf_node->lockf, file_size);
2171 }
2172
2173 /* --------------------------------------------------------------------- */
2174
2175 /* Global vfs vnode data structures for udfs */
2176 int (**udf_vnodeop_p)(void *);
2177
2178 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
2179 { &vop_default_desc, vn_default_error },
2180 { &vop_lookup_desc, udf_lookup }, /* lookup */
2181 { &vop_create_desc, udf_create }, /* create */
2182 { &vop_mknod_desc, udf_mknod }, /* mknod */ /* TODO */
2183 { &vop_open_desc, udf_open }, /* open */
2184 { &vop_close_desc, udf_close }, /* close */
2185 { &vop_access_desc, udf_access }, /* access */
2186 { &vop_getattr_desc, udf_getattr }, /* getattr */
2187 { &vop_setattr_desc, udf_setattr }, /* setattr */ /* TODO chflags */
2188 { &vop_read_desc, udf_read }, /* read */
2189 { &vop_write_desc, udf_write }, /* write */ /* WRITE */
2190 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
2191 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
2192 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */
2193 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */
2194 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */
2195 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */
2196 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */
2197 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */
2198 { &vop_fsync_desc, udf_fsync }, /* fsync */
2199 { &vop_seek_desc, genfs_seek }, /* seek */
2200 { &vop_remove_desc, udf_remove }, /* remove */
2201 { &vop_link_desc, udf_link }, /* link */ /* TODO */
2202 { &vop_rename_desc, udf_rename }, /* rename */ /* TODO */
2203 { &vop_mkdir_desc, udf_mkdir }, /* mkdir */
2204 { &vop_rmdir_desc, udf_rmdir }, /* rmdir */
2205 { &vop_symlink_desc, udf_symlink }, /* symlink */ /* TODO */
2206 { &vop_readdir_desc, udf_readdir }, /* readdir */
2207 { &vop_readlink_desc, udf_readlink }, /* readlink */ /* TEST ME */
2208 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */
2209 { &vop_inactive_desc, udf_inactive }, /* inactive */
2210 { &vop_reclaim_desc, udf_reclaim }, /* reclaim */
2211 { &vop_lock_desc, genfs_lock }, /* lock */
2212 { &vop_unlock_desc, genfs_unlock }, /* unlock */
2213 { &vop_bmap_desc, udf_trivial_bmap }, /* bmap */ /* 1:1 bmap */
2214 { &vop_strategy_desc, udf_vfsstrategy },/* strategy */
2215 /* { &vop_print_desc, udf_print }, */ /* print */
2216 { &vop_islocked_desc, genfs_islocked }, /* islocked */
2217 { &vop_pathconf_desc, udf_pathconf }, /* pathconf */
2218 { &vop_advlock_desc, udf_advlock }, /* advlock */ /* TEST ME */
2219 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */
2220 { &vop_getpages_desc, genfs_getpages }, /* getpages */
2221 { &vop_putpages_desc, genfs_putpages }, /* putpages */
2222 { NULL, NULL }
2223 };
2224
2225
2226 const struct vnodeopv_desc udf_vnodeop_opv_desc = {
2227 &udf_vnodeop_p, udf_vnodeop_entries
2228 };
2229