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