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