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