udf_vnops.c revision 1.82 1 /* $NetBSD: udf_vnops.c,v 1.82 2013/07/07 19:49:44 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.82 2013/07/07 19:49:44 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 mode_t mode;
660 uid_t d_uid;
661 gid_t d_gid;
662 const char *name;
663 int namelen, nameiop, islastcn, mounted_ro;
664 int error, found;
665
666 dir_node = VTOI(dvp);
667 ump = dir_node->ump;
668 *vpp = NULL;
669
670 DPRINTF(LOOKUP, ("udf_lookup called\n"));
671
672 /* simplify/clarification flags */
673 nameiop = cnp->cn_nameiop;
674 islastcn = cnp->cn_flags & ISLASTCN;
675 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY;
676
677 /* check exec/dirread permissions first */
678 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
679 if (error)
680 return error;
681
682 DPRINTF(LOOKUP, ("\taccess ok\n"));
683
684 /*
685 * If requesting a modify on the last path element on a read-only
686 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
687 */
688 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
689 return EROFS;
690
691 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
692 cnp->cn_nameptr));
693 /* look in the namecache */
694 if (cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
695 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
696 return *vpp == NULLVP ? ENOENT : 0;
697 }
698
699 DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
700
701 /*
702 * Obviously, the file is not (anymore) in the namecache, we have to
703 * search for it. There are three basic cases: '.', '..' and others.
704 *
705 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
706 */
707 error = 0;
708 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
709 DPRINTF(LOOKUP, ("\tlookup '.'\n"));
710 /* special case 1 '.' */
711 if (islastcn && cnp->cn_nameiop == RENAME) {
712 error = EISDIR;
713 goto out;
714 }
715 vref(dvp);
716 *vpp = dvp;
717 /* done */
718 goto done;
719 } else if (cnp->cn_flags & ISDOTDOT) {
720 /* special case 2 '..' */
721 DPRINTF(LOOKUP, ("\tlookup '..'\n"));
722
723 /* get our node */
724 name = "..";
725 namelen = 2;
726 error = udf_lookup_name_in_dir(dvp, name, namelen,
727 &icb_loc, &found);
728 if (error)
729 goto out;
730 if (!found)
731 error = ENOENT;
732
733 /* first unlock parent */
734 VOP_UNLOCK(dvp);
735
736 if (error == 0) {
737 DPRINTF(LOOKUP, ("\tfound '..'\n"));
738 /* try to create/reuse the node */
739 error = udf_get_node(ump, &icb_loc, &res_node);
740
741 if (!error) {
742 DPRINTF(LOOKUP,
743 ("\tnode retrieved/created OK\n"));
744 *vpp = res_node->vnode;
745 }
746 }
747
748 /* try to relock parent */
749 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
750 goto out;
751 }
752
753 /* all other files */
754 DPRINTF(LOOKUP, ("\tlookup file/dir in directory\n"));
755
756 /* lookup filename in the directory; location icb_loc */
757 name = cnp->cn_nameptr;
758 namelen = cnp->cn_namelen;
759 error = udf_lookup_name_in_dir(dvp, name, namelen,
760 &icb_loc, &found);
761 if (error)
762 goto out;
763 if (!found) {
764 DPRINTF(LOOKUP, ("\tNOT found\n"));
765 /*
766 * The entry was not found in the directory. This is
767 * valid if we are creating or renaming an entry and
768 * are working on the last component of the path name.
769 */
770 if (islastcn && (cnp->cn_nameiop == CREATE ||
771 cnp->cn_nameiop == RENAME)) {
772 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
773 if (error) {
774 goto out;
775 }
776 error = EJUSTRETURN;
777 } else {
778 error = ENOENT;
779 }
780 /* done */
781 goto done;
782 }
783
784 /*
785 * XXX NOTE tmpfs has a test here that tests that intermediate
786 * components i.e. not the last one ought to be either a directory or
787 * a link. It seems to function well without this code.
788 */
789
790 /* check the permissions */
791 if (islastcn && (cnp->cn_nameiop == DELETE ||
792 cnp->cn_nameiop == RENAME) ) {
793 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
794 if (error)
795 goto out;
796
797 /* get node attributes */
798 mode = udf_getaccessmode(dir_node);
799 udf_getownership(dir_node, &d_uid, &d_gid);
800 if ((mode & S_ISTXT) != 0) {
801 error = kauth_authorize_vnode(cnp->cn_cred,
802 KAUTH_VNODE_DELETE, res_node->vnode,
803 dir_node->vnode, genfs_can_sticky(cnp->cn_cred,
804 d_uid, d_uid));
805 if (error) {
806 error = EPERM;
807 goto out;
808 }
809 }
810 }
811 /* try to create/reuse the node */
812 error = udf_get_node(ump, &icb_loc, &res_node);
813 if (error)
814 goto out;
815 *vpp = res_node->vnode;
816
817 done:
818 /*
819 * Store result in the cache if requested. If we are creating a file,
820 * the file might not be found and thus putting it into the namecache
821 * might be seen as negative caching.
822 */
823 if (nameiop != CREATE)
824 cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
825 cnp->cn_flags);
826
827 out:
828 DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error));
829
830 return error;
831 }
832
833 /* --------------------------------------------------------------------- */
834
835 int
836 udf_getattr(void *v)
837 {
838 struct vop_getattr_args /* {
839 struct vnode *a_vp;
840 struct vattr *a_vap;
841 kauth_cred_t a_cred;
842 struct lwp *a_l;
843 } */ *ap = v;
844 struct vnode *vp = ap->a_vp;
845 struct udf_node *udf_node = VTOI(vp);
846 struct udf_mount *ump = udf_node->ump;
847 struct file_entry *fe = udf_node->fe;
848 struct extfile_entry *efe = udf_node->efe;
849 struct filetimes_extattr_entry *ft_extattr;
850 struct device_extattr_entry *devattr;
851 struct vattr *vap = ap->a_vap;
852 struct timestamp *atime, *mtime, *attrtime, *creatime;
853 uint64_t filesize, blkssize;
854 uint32_t nlink;
855 uint32_t offset, a_l;
856 uint8_t *filedata;
857 uid_t uid;
858 gid_t gid;
859 int error;
860
861 DPRINTF(CALL, ("udf_getattr called\n"));
862
863 /* update times before we returning values */
864 udf_itimes(udf_node, NULL, NULL, NULL);
865
866 /* get descriptor information */
867 if (fe) {
868 nlink = udf_rw16(fe->link_cnt);
869 uid = (uid_t)udf_rw32(fe->uid);
870 gid = (gid_t)udf_rw32(fe->gid);
871 filesize = udf_rw64(fe->inf_len);
872 blkssize = udf_rw64(fe->logblks_rec);
873 atime = &fe->atime;
874 mtime = &fe->mtime;
875 attrtime = &fe->attrtime;
876 filedata = fe->data;
877
878 /* initial guess */
879 creatime = mtime;
880
881 /* check our extended attribute if present */
882 error = udf_extattr_search_intern(udf_node,
883 UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
884 if (!error) {
885 ft_extattr = (struct filetimes_extattr_entry *)
886 (filedata + offset);
887 if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
888 creatime = &ft_extattr->times[0];
889 }
890 } else {
891 assert(udf_node->efe);
892 nlink = udf_rw16(efe->link_cnt);
893 uid = (uid_t)udf_rw32(efe->uid);
894 gid = (gid_t)udf_rw32(efe->gid);
895 filesize = udf_rw64(efe->inf_len); /* XXX or obj_size? */
896 blkssize = udf_rw64(efe->logblks_rec);
897 atime = &efe->atime;
898 mtime = &efe->mtime;
899 attrtime = &efe->attrtime;
900 creatime = &efe->ctime;
901 filedata = efe->data;
902 }
903
904 /* do the uid/gid translation game */
905 if (uid == (uid_t) -1)
906 uid = ump->mount_args.anon_uid;
907 if (gid == (gid_t) -1)
908 gid = ump->mount_args.anon_gid;
909
910 /* fill in struct vattr with values from the node */
911 vattr_null(vap);
912 vap->va_type = vp->v_type;
913 vap->va_mode = udf_getaccessmode(udf_node);
914 vap->va_nlink = nlink;
915 vap->va_uid = uid;
916 vap->va_gid = gid;
917 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
918 vap->va_fileid = udf_get_node_id(&udf_node->loc); /* inode hash XXX */
919 vap->va_size = filesize;
920 vap->va_blocksize = udf_node->ump->discinfo.sector_size; /* wise? */
921
922 /*
923 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add
924 * 1 to the link count if its a directory we're requested attributes
925 * of.
926 */
927 if (vap->va_type == VDIR)
928 vap->va_nlink++;
929
930 /* access times */
931 udf_timestamp_to_timespec(ump, atime, &vap->va_atime);
932 udf_timestamp_to_timespec(ump, mtime, &vap->va_mtime);
933 udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime);
934 udf_timestamp_to_timespec(ump, creatime, &vap->va_birthtime);
935
936 vap->va_gen = 1; /* no multiple generations yes (!?) */
937 vap->va_flags = 0; /* no flags */
938 vap->va_bytes = blkssize * udf_node->ump->discinfo.sector_size;
939 vap->va_filerev = 1; /* TODO file revision numbers? */
940 vap->va_vaflags = 0;
941 /* TODO get vaflags from the extended attributes? */
942
943 if ((vap->va_type == VBLK) || (vap->va_type == VCHR)) {
944 error = udf_extattr_search_intern(udf_node,
945 UDF_DEVICESPEC_ATTR_NO, "",
946 &offset, &a_l);
947 /* if error, deny access */
948 if (error || (filedata == NULL)) {
949 vap->va_mode = 0; /* or v_type = VNON? */
950 } else {
951 devattr = (struct device_extattr_entry *)
952 filedata + offset;
953 vap->va_rdev = makedev(
954 udf_rw32(devattr->major),
955 udf_rw32(devattr->minor)
956 );
957 /* TODO we could check the implementator */
958 }
959 }
960
961 return 0;
962 }
963
964 /* --------------------------------------------------------------------- */
965
966 static int
967 udf_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
968 kauth_cred_t cred)
969 {
970 struct udf_node *udf_node = VTOI(vp);
971 uid_t uid;
972 gid_t gid;
973 int error;
974
975 #ifdef notyet
976 /* TODO get vaflags from the extended attributes? */
977 /* Immutable or append-only files cannot be modified, either. */
978 if (udf_node->flags & (IMMUTABLE | APPEND))
979 return EPERM;
980 #endif
981
982 if (vp->v_mount->mnt_flag & MNT_RDONLY)
983 return EROFS;
984
985 /* retrieve old values */
986 udf_getownership(udf_node, &uid, &gid);
987
988 /* only one could be specified */
989 if (new_uid == VNOVAL)
990 new_uid = uid;
991 if (new_gid == VNOVAL)
992 new_gid = gid;
993
994 /* check if we can fit it in an 32 bits */
995 if ((uid_t) ((uint32_t) new_uid) != new_uid)
996 return EINVAL;
997 if ((gid_t) ((uint32_t) new_gid) != new_gid)
998 return EINVAL;
999
1000 /* check permissions */
1001 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP,
1002 vp, NULL, genfs_can_chown(cred, uid, gid, new_uid, new_gid));
1003 if (error)
1004 return (error);
1005
1006 /* change the ownership */
1007 udf_setownership(udf_node, new_uid, new_gid);
1008
1009 /* mark node changed */
1010 udf_node->i_flags |= IN_CHANGE;
1011 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1012 udf_node->i_flags |= IN_ACCESS;
1013
1014 return 0;
1015 }
1016
1017
1018 static int
1019 udf_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
1020 {
1021 struct udf_node *udf_node = VTOI(vp);
1022 uid_t uid;
1023 gid_t gid;
1024 int error;
1025
1026 #ifdef notyet
1027 /* TODO get vaflags from the extended attributes? */
1028 /* Immutable or append-only files cannot be modified, either. */
1029 if (udf_node->flags & (IMMUTABLE | APPEND))
1030 return EPERM;
1031 #endif
1032
1033 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1034 return EROFS;
1035
1036 /* retrieve uid/gid values */
1037 udf_getownership(udf_node, &uid, &gid);
1038
1039 /* check permissions */
1040 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
1041 NULL, genfs_can_chmod(vp->v_type, cred, uid, gid, mode));
1042 if (error)
1043 return (error);
1044
1045 /* change mode */
1046 udf_setaccessmode(udf_node, mode);
1047
1048 /* mark node changed */
1049 udf_node->i_flags |= IN_CHANGE;
1050 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1051 udf_node->i_flags |= IN_ACCESS;
1052
1053 return 0;
1054 }
1055
1056
1057 /* exported */
1058 int
1059 udf_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
1060 {
1061 struct udf_node *udf_node = VTOI(vp);
1062 int error, extended;
1063
1064 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1065 return EROFS;
1066
1067 /* Decide whether this is a valid operation based on the file type. */
1068 switch (vp->v_type) {
1069 case VDIR:
1070 return EISDIR;
1071 case VREG:
1072 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1073 return EROFS;
1074 break;
1075 case VBLK:
1076 /* FALLTHROUGH */
1077 case VCHR:
1078 /* FALLTHROUGH */
1079 case VFIFO:
1080 /* Allow modifications of special files even if in the file
1081 * system is mounted read-only (we are not modifying the
1082 * files themselves, but the objects they represent). */
1083 return 0;
1084 default:
1085 /* Anything else is unsupported. */
1086 return EOPNOTSUPP;
1087 }
1088
1089 #if notyet
1090 /* TODO get vaflags from the extended attributes? */
1091 /* Immutable or append-only files cannot be modified, either. */
1092 if (node->flags & (IMMUTABLE | APPEND))
1093 return EPERM;
1094 #endif
1095
1096 /* resize file to the requested size */
1097 error = udf_resize_node(udf_node, newsize, &extended);
1098
1099 if (error == 0) {
1100 /* mark change */
1101 udf_node->i_flags |= IN_CHANGE | IN_MODIFY;
1102 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1103 udf_node->i_flags |= IN_ACCESS;
1104 VN_KNOTE(vp, NOTE_ATTRIB | (extended ? NOTE_EXTEND : 0));
1105 udf_update(vp, NULL, NULL, NULL, 0);
1106 }
1107
1108 return error;
1109 }
1110
1111
1112 static int
1113 udf_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
1114 {
1115 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1116 return EROFS;
1117
1118 /*
1119 * XXX we can't do this yet, as its not described in the standard yet
1120 */
1121
1122 return EOPNOTSUPP;
1123 }
1124
1125
1126 static int
1127 udf_chtimes(struct vnode *vp,
1128 struct timespec *atime, struct timespec *mtime,
1129 struct timespec *birthtime, int setattrflags,
1130 kauth_cred_t cred)
1131 {
1132 struct udf_node *udf_node = VTOI(vp);
1133 uid_t uid;
1134 gid_t gid;
1135 int error;
1136
1137 #ifdef notyet
1138 /* TODO get vaflags from the extended attributes? */
1139 /* Immutable or append-only files cannot be modified, either. */
1140 if (udf_node->flags & (IMMUTABLE | APPEND))
1141 return EPERM;
1142 #endif
1143
1144 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1145 return EROFS;
1146
1147 /* retrieve uid/gid values */
1148 udf_getownership(udf_node, &uid, &gid);
1149
1150 /* check permissions */
1151 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
1152 NULL, genfs_can_chtimes(vp, setattrflags, uid, cred));
1153 if (error)
1154 return (error);
1155
1156 /* update node flags depending on what times are passed */
1157 if (atime->tv_sec != VNOVAL)
1158 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
1159 udf_node->i_flags |= IN_ACCESS;
1160 if ((mtime->tv_sec != VNOVAL) || (birthtime->tv_sec != VNOVAL)) {
1161 udf_node->i_flags |= IN_CHANGE | IN_UPDATE;
1162 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1163 udf_node->i_flags |= IN_ACCESS;
1164 }
1165
1166 return udf_update(vp, atime, mtime, birthtime, 0);
1167 }
1168
1169
1170 int
1171 udf_setattr(void *v)
1172 {
1173 struct vop_setattr_args /* {
1174 struct vnode *a_vp;
1175 struct vattr *a_vap;
1176 kauth_cred_t a_cred;
1177 struct lwp *a_l;
1178 } */ *ap = v;
1179 struct vnode *vp = ap->a_vp;
1180 /* struct udf_node *udf_node = VTOI(vp); */
1181 /* struct udf_mount *ump = udf_node->ump; */
1182 kauth_cred_t cred = ap->a_cred;
1183 struct vattr *vap = ap->a_vap;
1184 int error;
1185
1186 DPRINTF(CALL, ("udf_setattr called\n"));
1187
1188 /* Abort if any unsettable attribute is given. */
1189 error = 0;
1190 if (vap->va_type != VNON ||
1191 vap->va_nlink != VNOVAL ||
1192 vap->va_fsid != VNOVAL ||
1193 vap->va_fileid != VNOVAL ||
1194 vap->va_blocksize != VNOVAL ||
1195 #ifdef notyet
1196 /* checks are debated */
1197 vap->va_ctime.tv_sec != VNOVAL ||
1198 vap->va_ctime.tv_nsec != VNOVAL ||
1199 vap->va_birthtime.tv_sec != VNOVAL ||
1200 vap->va_birthtime.tv_nsec != VNOVAL ||
1201 #endif
1202 vap->va_gen != VNOVAL ||
1203 vap->va_rdev != VNOVAL ||
1204 vap->va_bytes != VNOVAL)
1205 error = EINVAL;
1206
1207 DPRINTF(ATTR, ("setattr changing:\n"));
1208 if (error == 0 && (vap->va_flags != VNOVAL)) {
1209 DPRINTF(ATTR, ("\tchflags\n"));
1210 error = udf_chflags(vp, vap->va_flags, cred);
1211 }
1212
1213 if (error == 0 && (vap->va_size != VNOVAL)) {
1214 DPRINTF(ATTR, ("\tchsize\n"));
1215 error = udf_chsize(vp, vap->va_size, cred);
1216 }
1217
1218 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) {
1219 DPRINTF(ATTR, ("\tchown\n"));
1220 error = udf_chown(vp, vap->va_uid, vap->va_gid, cred);
1221 }
1222
1223 if (error == 0 && (vap->va_mode != VNOVAL)) {
1224 DPRINTF(ATTR, ("\tchmod\n"));
1225 error = udf_chmod(vp, vap->va_mode, cred);
1226 }
1227
1228 if (error == 0 &&
1229 ((vap->va_atime.tv_sec != VNOVAL &&
1230 vap->va_atime.tv_nsec != VNOVAL) ||
1231 (vap->va_mtime.tv_sec != VNOVAL &&
1232 vap->va_mtime.tv_nsec != VNOVAL))
1233 ) {
1234 DPRINTF(ATTR, ("\tchtimes\n"));
1235 error = udf_chtimes(vp, &vap->va_atime, &vap->va_mtime,
1236 &vap->va_birthtime, vap->va_vaflags, cred);
1237 }
1238 VN_KNOTE(vp, NOTE_ATTRIB);
1239
1240 return error;
1241 }
1242
1243 /* --------------------------------------------------------------------- */
1244
1245 /*
1246 * Return POSIX pathconf information for UDF file systems.
1247 */
1248 int
1249 udf_pathconf(void *v)
1250 {
1251 struct vop_pathconf_args /* {
1252 struct vnode *a_vp;
1253 int a_name;
1254 register_t *a_retval;
1255 } */ *ap = v;
1256 uint32_t bits;
1257
1258 DPRINTF(CALL, ("udf_pathconf called\n"));
1259
1260 switch (ap->a_name) {
1261 case _PC_LINK_MAX:
1262 *ap->a_retval = (1<<16)-1; /* 16 bits */
1263 return 0;
1264 case _PC_NAME_MAX:
1265 *ap->a_retval = UDF_MAXNAMLEN;
1266 return 0;
1267 case _PC_PATH_MAX:
1268 *ap->a_retval = PATH_MAX;
1269 return 0;
1270 case _PC_PIPE_BUF:
1271 *ap->a_retval = PIPE_BUF;
1272 return 0;
1273 case _PC_CHOWN_RESTRICTED:
1274 *ap->a_retval = 1;
1275 return 0;
1276 case _PC_NO_TRUNC:
1277 *ap->a_retval = 1;
1278 return 0;
1279 case _PC_SYNC_IO:
1280 *ap->a_retval = 0; /* synchronised is off for performance */
1281 return 0;
1282 case _PC_FILESIZEBITS:
1283 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
1284 bits = 64; /* XXX ought to deliver 65 */
1285 #if 0
1286 if (udf_node)
1287 bits = 64 * vp->v_mount->mnt_dev_bshift;
1288 #endif
1289 *ap->a_retval = bits;
1290 return 0;
1291 }
1292
1293 return EINVAL;
1294 }
1295
1296
1297 /* --------------------------------------------------------------------- */
1298
1299 int
1300 udf_open(void *v)
1301 {
1302 struct vop_open_args /* {
1303 struct vnode *a_vp;
1304 int a_mode;
1305 kauth_cred_t a_cred;
1306 struct proc *a_p;
1307 } */ *ap = v;
1308 int flags;
1309
1310 DPRINTF(CALL, ("udf_open called\n"));
1311
1312 /*
1313 * Files marked append-only must be opened for appending.
1314 * TODO: get chflags(2) flags from extened attribute.
1315 */
1316 flags = 0;
1317 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
1318 return (EPERM);
1319
1320 return 0;
1321 }
1322
1323
1324 /* --------------------------------------------------------------------- */
1325
1326 int
1327 udf_close(void *v)
1328 {
1329 struct vop_close_args /* {
1330 struct vnode *a_vp;
1331 int a_fflag;
1332 kauth_cred_t a_cred;
1333 struct proc *a_p;
1334 } */ *ap = v;
1335 struct vnode *vp = ap->a_vp;
1336 struct udf_node *udf_node = VTOI(vp);
1337 int async = vp->v_mount->mnt_flag & MNT_ASYNC;
1338 int error;
1339
1340 DPRINTF(CALL, ("udf_close called\n"));
1341 udf_node = udf_node; /* shut up gcc */
1342
1343 if (!async && (vp->v_type != VDIR)) {
1344 mutex_enter(vp->v_interlock);
1345 error = VOP_PUTPAGES(vp, 0, 0, PGO_CLEANIT);
1346 if (error)
1347 return error;
1348 }
1349
1350 mutex_enter(vp->v_interlock);
1351 if (vp->v_usecount > 1)
1352 udf_itimes(udf_node, NULL, NULL, NULL);
1353 mutex_exit(vp->v_interlock);
1354
1355 return 0;
1356 }
1357
1358
1359 /* --------------------------------------------------------------------- */
1360
1361 static int
1362 udf_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
1363 {
1364 int flags;
1365
1366 /* check if we are allowed to write */
1367 switch (vap->va_type) {
1368 case VDIR:
1369 case VLNK:
1370 case VREG:
1371 /*
1372 * normal nodes: check if we're on a read-only mounted
1373 * filingsystem and bomb out if we're trying to write.
1374 */
1375 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
1376 return EROFS;
1377 break;
1378 case VBLK:
1379 case VCHR:
1380 case VSOCK:
1381 case VFIFO:
1382 /*
1383 * special nodes: even on read-only mounted filingsystems
1384 * these are allowed to be written to if permissions allow.
1385 */
1386 break;
1387 default:
1388 /* no idea what this is */
1389 return EINVAL;
1390 }
1391
1392 /* noone may write immutable files */
1393 /* TODO: get chflags(2) flags from extened attribute. */
1394 flags = 0;
1395 if ((mode & VWRITE) && (flags & IMMUTABLE))
1396 return EPERM;
1397
1398 return 0;
1399 }
1400
1401 static int
1402 udf_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
1403 kauth_cred_t cred)
1404 {
1405 /* ask the generic genfs_can_access to advice on security */
1406 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
1407 vp->v_type, vap->va_mode), vp, NULL, genfs_can_access(vp->v_type,
1408 vap->va_mode, vap->va_uid, vap->va_gid, mode, cred));
1409 }
1410
1411 int
1412 udf_access(void *v)
1413 {
1414 struct vop_access_args /* {
1415 struct vnode *a_vp;
1416 int a_mode;
1417 kauth_cred_t a_cred;
1418 struct proc *a_p;
1419 } */ *ap = v;
1420 struct vnode *vp = ap->a_vp;
1421 mode_t mode = ap->a_mode;
1422 kauth_cred_t cred = ap->a_cred;
1423 /* struct udf_node *udf_node = VTOI(vp); */
1424 struct vattr vap;
1425 int error;
1426
1427 DPRINTF(CALL, ("udf_access called\n"));
1428
1429 error = VOP_GETATTR(vp, &vap, NULL);
1430 if (error)
1431 return error;
1432
1433 error = udf_check_possible(vp, &vap, mode);
1434 if (error)
1435 return error;
1436
1437 error = udf_check_permitted(vp, &vap, mode, cred);
1438
1439 return error;
1440 }
1441
1442 /* --------------------------------------------------------------------- */
1443
1444 int
1445 udf_create(void *v)
1446 {
1447 struct vop_create_args /* {
1448 struct vnode *a_dvp;
1449 struct vnode **a_vpp;
1450 struct componentname *a_cnp;
1451 struct vattr *a_vap;
1452 } */ *ap = v;
1453 struct vnode *dvp = ap->a_dvp;
1454 struct vnode **vpp = ap->a_vpp;
1455 struct vattr *vap = ap->a_vap;
1456 struct componentname *cnp = ap->a_cnp;
1457 int error;
1458
1459 DPRINTF(CALL, ("udf_create called\n"));
1460 error = udf_create_node(dvp, vpp, vap, cnp);
1461
1462 vput(dvp);
1463 return error;
1464 }
1465
1466 /* --------------------------------------------------------------------- */
1467
1468 int
1469 udf_mknod(void *v)
1470 {
1471 struct vop_mknod_args /* {
1472 struct vnode *a_dvp;
1473 struct vnode **a_vpp;
1474 struct componentname *a_cnp;
1475 struct vattr *a_vap;
1476 } */ *ap = v;
1477 struct vnode *dvp = ap->a_dvp;
1478 struct vnode **vpp = ap->a_vpp;
1479 struct vattr *vap = ap->a_vap;
1480 struct componentname *cnp = ap->a_cnp;
1481 int error;
1482
1483 DPRINTF(CALL, ("udf_mknod called\n"));
1484 error = udf_create_node(dvp, vpp, vap, cnp);
1485
1486 vput(dvp);
1487 return error;
1488 }
1489
1490 /* --------------------------------------------------------------------- */
1491
1492 int
1493 udf_mkdir(void *v)
1494 {
1495 struct vop_mkdir_args /* {
1496 struct vnode *a_dvp;
1497 struct vnode **a_vpp;
1498 struct componentname *a_cnp;
1499 struct vattr *a_vap;
1500 } */ *ap = v;
1501 struct vnode *dvp = ap->a_dvp;
1502 struct vnode **vpp = ap->a_vpp;
1503 struct vattr *vap = ap->a_vap;
1504 struct componentname *cnp = ap->a_cnp;
1505 int error;
1506
1507 DPRINTF(CALL, ("udf_mkdir called\n"));
1508 error = udf_create_node(dvp, vpp, vap, cnp);
1509
1510 vput(dvp);
1511 return error;
1512 }
1513
1514 /* --------------------------------------------------------------------- */
1515
1516 static int
1517 udf_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1518 {
1519 struct udf_node *udf_node, *dir_node;
1520 struct vattr vap;
1521 int error;
1522
1523 DPRINTF(CALL, ("udf_link called\n"));
1524 KASSERT(dvp != vp);
1525 KASSERT(vp->v_type != VDIR);
1526 KASSERT(dvp->v_mount == vp->v_mount);
1527
1528 /* lock node */
1529 error = vn_lock(vp, LK_EXCLUSIVE);
1530 if (error)
1531 return error;
1532
1533 /* get attributes */
1534 dir_node = VTOI(dvp);
1535 udf_node = VTOI(vp);
1536
1537 error = VOP_GETATTR(vp, &vap, FSCRED);
1538 if (error) {
1539 VOP_UNLOCK(vp);
1540 return error;
1541 }
1542
1543 /* check link count overflow */
1544 if (vap.va_nlink >= (1<<16)-1) { /* uint16_t */
1545 VOP_UNLOCK(vp);
1546 return EMLINK;
1547 }
1548
1549 error = udf_dir_attach(dir_node->ump, dir_node, udf_node, &vap, cnp);
1550 if (error)
1551 VOP_UNLOCK(vp);
1552 return error;
1553 }
1554
1555 int
1556 udf_link(void *v)
1557 {
1558 struct vop_link_args /* {
1559 struct vnode *a_dvp;
1560 struct vnode *a_vp;
1561 struct componentname *a_cnp;
1562 } */ *ap = v;
1563 struct vnode *dvp = ap->a_dvp;
1564 struct vnode *vp = ap->a_vp;
1565 struct componentname *cnp = ap->a_cnp;
1566 int error;
1567
1568 error = udf_do_link(dvp, vp, cnp);
1569 if (error)
1570 VOP_ABORTOP(dvp, cnp);
1571
1572 VN_KNOTE(vp, NOTE_LINK);
1573 VN_KNOTE(dvp, NOTE_WRITE);
1574 vput(dvp);
1575
1576 return error;
1577 }
1578
1579 /* --------------------------------------------------------------------- */
1580
1581 static int
1582 udf_do_symlink(struct udf_node *udf_node, char *target)
1583 {
1584 struct pathcomp pathcomp;
1585 uint8_t *pathbuf, *pathpos, *compnamepos;
1586 char *mntonname;
1587 int pathlen, len, compnamelen, mntonnamelen;
1588 int error;
1589
1590 /* process `target' to an UDF structure */
1591 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
1592 pathpos = pathbuf;
1593 pathlen = 0;
1594
1595 if (*target == '/') {
1596 /* symlink starts from the root */
1597 len = UDF_PATH_COMP_SIZE;
1598 memset(&pathcomp, 0, len);
1599 pathcomp.type = UDF_PATH_COMP_ROOT;
1600
1601 /* check if its mount-point relative! */
1602 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
1603 mntonnamelen = strlen(mntonname);
1604 if (strlen(target) >= mntonnamelen) {
1605 if (strncmp(target, mntonname, mntonnamelen) == 0) {
1606 pathcomp.type = UDF_PATH_COMP_MOUNTROOT;
1607 target += mntonnamelen;
1608 }
1609 } else {
1610 target++;
1611 }
1612
1613 memcpy(pathpos, &pathcomp, len);
1614 pathpos += len;
1615 pathlen += len;
1616 }
1617
1618 error = 0;
1619 while (*target) {
1620 /* ignore multiple '/' */
1621 while (*target == '/') {
1622 target++;
1623 }
1624 if (!*target)
1625 break;
1626
1627 /* extract component name */
1628 compnamelen = 0;
1629 compnamepos = target;
1630 while ((*target) && (*target != '/')) {
1631 target++;
1632 compnamelen++;
1633 }
1634
1635 /* just trunc if too long ?? (security issue) */
1636 if (compnamelen >= 127) {
1637 error = ENAMETOOLONG;
1638 break;
1639 }
1640
1641 /* convert unix name to UDF name */
1642 len = sizeof(struct pathcomp);
1643 memset(&pathcomp, 0, len);
1644 pathcomp.type = UDF_PATH_COMP_NAME;
1645 len = UDF_PATH_COMP_SIZE;
1646
1647 if ((compnamelen == 2) && (strncmp(compnamepos, "..", 2) == 0))
1648 pathcomp.type = UDF_PATH_COMP_PARENTDIR;
1649 if ((compnamelen == 1) && (*compnamepos == '.'))
1650 pathcomp.type = UDF_PATH_COMP_CURDIR;
1651
1652 if (pathcomp.type == UDF_PATH_COMP_NAME) {
1653 unix_to_udf_name(
1654 (char *) &pathcomp.ident, &pathcomp.l_ci,
1655 compnamepos, compnamelen,
1656 &udf_node->ump->logical_vol->desc_charset);
1657 len = UDF_PATH_COMP_SIZE + pathcomp.l_ci;
1658 }
1659
1660 if (pathlen + len >= UDF_SYMLINKBUFLEN) {
1661 error = ENAMETOOLONG;
1662 break;
1663 }
1664
1665 memcpy(pathpos, &pathcomp, len);
1666 pathpos += len;
1667 pathlen += len;
1668 }
1669
1670 if (error) {
1671 /* aparently too big */
1672 free(pathbuf, M_UDFTEMP);
1673 return error;
1674 }
1675
1676 error = udf_grow_node(udf_node, pathlen);
1677 if (error) {
1678 /* failed to pregrow node */
1679 free(pathbuf, M_UDFTEMP);
1680 return error;
1681 }
1682
1683 /* write out structure on the new file */
1684 error = vn_rdwr(UIO_WRITE, udf_node->vnode,
1685 pathbuf, pathlen, 0,
1686 UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
1687 FSCRED, NULL, NULL);
1688
1689 /* return status of symlink contents writeout */
1690 free(pathbuf, M_UDFTEMP);
1691 return error;
1692 }
1693
1694
1695 int
1696 udf_symlink(void *v)
1697 {
1698 struct vop_symlink_args /* {
1699 struct vnode *a_dvp;
1700 struct vnode **a_vpp;
1701 struct componentname *a_cnp;
1702 struct vattr *a_vap;
1703 char *a_target;
1704 } */ *ap = v;
1705 struct vnode *dvp = ap->a_dvp;
1706 struct vnode **vpp = ap->a_vpp;
1707 struct vattr *vap = ap->a_vap;
1708 struct componentname *cnp = ap->a_cnp;
1709 struct udf_node *dir_node;
1710 struct udf_node *udf_node;
1711 int error;
1712
1713 DPRINTF(CALL, ("udf_symlink called\n"));
1714 DPRINTF(CALL, ("\tlinking to `%s`\n", ap->a_target));
1715 error = udf_create_node(dvp, vpp, vap, cnp);
1716 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
1717 if (!error) {
1718 dir_node = VTOI(dvp);
1719 udf_node = VTOI(*vpp);
1720 KASSERT(udf_node);
1721 error = udf_do_symlink(udf_node, ap->a_target);
1722 if (error) {
1723 /* remove node */
1724 udf_shrink_node(udf_node, 0);
1725 udf_dir_detach(udf_node->ump, dir_node, udf_node, cnp);
1726 }
1727 }
1728 vput(dvp);
1729 return error;
1730 }
1731
1732 /* --------------------------------------------------------------------- */
1733
1734 int
1735 udf_readlink(void *v)
1736 {
1737 struct vop_readlink_args /* {
1738 struct vnode *a_vp;
1739 struct uio *a_uio;
1740 kauth_cred_t a_cred;
1741 } */ *ap = v;
1742 struct vnode *vp = ap->a_vp;
1743 struct uio *uio = ap->a_uio;
1744 kauth_cred_t cred = ap->a_cred;
1745 struct udf_node *udf_node;
1746 struct pathcomp pathcomp;
1747 struct vattr vattr;
1748 uint8_t *pathbuf, *targetbuf, *tmpname;
1749 uint8_t *pathpos, *targetpos;
1750 char *mntonname;
1751 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
1752 int first, error;
1753
1754 DPRINTF(CALL, ("udf_readlink called\n"));
1755
1756 udf_node = VTOI(vp);
1757 error = VOP_GETATTR(vp, &vattr, cred);
1758 if (error)
1759 return error;
1760
1761 /* claim temporary buffers for translation */
1762 pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
1763 targetbuf = malloc(PATH_MAX+1, 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, vattr.va_size, 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(targetbuf, M_UDFTEMP);
1777 free(tmpname, M_UDFTEMP);
1778 return error;
1779 }
1780
1781 /* convert to a unix path */
1782 pathpos = pathbuf;
1783 pathlen = 0;
1784 targetpos = targetbuf;
1785 targetlen = PATH_MAX;
1786 mntonname = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
1787 mntonnamelen = strlen(mntonname);
1788
1789 error = 0;
1790 first = 1;
1791 while (vattr.va_size - pathlen >= UDF_PATH_COMP_SIZE) {
1792 len = UDF_PATH_COMP_SIZE;
1793 memcpy(&pathcomp, pathpos, len);
1794 l_ci = pathcomp.l_ci;
1795 switch (pathcomp.type) {
1796 case UDF_PATH_COMP_ROOT :
1797 /* XXX should check for l_ci; bugcompatible now */
1798 if ((targetlen < 1) || !first) {
1799 error = EINVAL;
1800 break;
1801 }
1802 *targetpos++ = '/'; targetlen--;
1803 break;
1804 case UDF_PATH_COMP_MOUNTROOT :
1805 /* XXX what should it be if l_ci > 0 ? [4/48.16.1.2] */
1806 if (l_ci || (targetlen < mntonnamelen+1) || !first) {
1807 error = EINVAL;
1808 break;
1809 }
1810 memcpy(targetpos, mntonname, mntonnamelen);
1811 targetpos += mntonnamelen; targetlen -= mntonnamelen;
1812 if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
1813 /* more follows, so must be directory */
1814 *targetpos++ = '/'; targetlen--;
1815 }
1816 break;
1817 case UDF_PATH_COMP_PARENTDIR :
1818 /* XXX should check for l_ci; bugcompatible now */
1819 if (targetlen < 3) {
1820 error = EINVAL;
1821 break;
1822 }
1823 *targetpos++ = '.'; targetlen--;
1824 *targetpos++ = '.'; targetlen--;
1825 *targetpos++ = '/'; targetlen--;
1826 break;
1827 case UDF_PATH_COMP_CURDIR :
1828 /* XXX should check for l_ci; bugcompatible now */
1829 if (targetlen < 2) {
1830 error = EINVAL;
1831 break;
1832 }
1833 *targetpos++ = '.'; targetlen--;
1834 *targetpos++ = '/'; targetlen--;
1835 break;
1836 case UDF_PATH_COMP_NAME :
1837 if (l_ci == 0) {
1838 error = EINVAL;
1839 break;
1840 }
1841 memset(tmpname, 0, PATH_MAX);
1842 memcpy(&pathcomp, pathpos, len + l_ci);
1843 udf_to_unix_name(tmpname, MAXPATHLEN,
1844 pathcomp.ident, l_ci,
1845 &udf_node->ump->logical_vol->desc_charset);
1846 namelen = strlen(tmpname);
1847 if (targetlen < namelen + 1) {
1848 error = EINVAL;
1849 break;
1850 }
1851 memcpy(targetpos, tmpname, namelen);
1852 targetpos += namelen; targetlen -= namelen;
1853 if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
1854 /* more follows, so must be directory */
1855 *targetpos++ = '/'; targetlen--;
1856 }
1857 break;
1858 default :
1859 error = EINVAL;
1860 break;
1861 }
1862 first = 0;
1863 if (error)
1864 break;
1865 pathpos += UDF_PATH_COMP_SIZE + l_ci;
1866 pathlen += UDF_PATH_COMP_SIZE + l_ci;
1867
1868 }
1869 /* all processed? */
1870 if (vattr.va_size - pathlen > 0)
1871 error = EINVAL;
1872
1873 /* uiomove() to destination */
1874 if (!error)
1875 uiomove(targetbuf, PATH_MAX - targetlen, uio);
1876
1877 free(pathbuf, M_UDFTEMP);
1878 free(targetbuf, M_UDFTEMP);
1879 free(tmpname, M_UDFTEMP);
1880
1881 return error;
1882 }
1883
1884 /* --------------------------------------------------------------------- */
1885
1886 /*
1887 * Check if source directory is in the path of the target directory. Target
1888 * is supplied locked, source is unlocked. The target is always vput before
1889 * returning. Modeled after UFS.
1890 *
1891 * If source is on the path from target to the root, return error.
1892 */
1893
1894 static int
1895 udf_on_rootpath(struct udf_node *source, struct udf_node *target)
1896 {
1897 struct udf_mount *ump = target->ump;
1898 struct udf_node *res_node;
1899 struct long_ad icb_loc, *root_icb;
1900 const char *name;
1901 int namelen;
1902 int error, found;
1903
1904 name = "..";
1905 namelen = 2;
1906 error = 0;
1907 res_node = target;
1908
1909 root_icb = &ump->fileset_desc->rootdir_icb;
1910
1911 /* if nodes are equal, it is no use looking */
1912 if (udf_compare_icb(&source->loc, &target->loc) == 0) {
1913 error = EEXIST;
1914 goto out;
1915 }
1916
1917 /* nothing can exist before the root */
1918 if (udf_compare_icb(root_icb, &target->loc) == 0) {
1919 error = 0;
1920 goto out;
1921 }
1922
1923 for (;;) {
1924 DPRINTF(NODE, ("udf_on_rootpath : "
1925 "source vp %p, looking at vp %p\n",
1926 source->vnode, res_node->vnode));
1927
1928 /* sanity check */
1929 if (res_node->vnode->v_type != VDIR) {
1930 error = ENOTDIR;
1931 goto out;
1932 }
1933
1934 /* go down one level */
1935 error = udf_lookup_name_in_dir(res_node->vnode, name, namelen,
1936 &icb_loc, &found);
1937 DPRINTF(NODE, ("\tlookup of '..' resulted in error %d, "
1938 "found %d\n", error, found));
1939
1940 if (!found)
1941 error = ENOENT;
1942 if (error)
1943 goto out;
1944
1945 /* did we encounter source node? */
1946 if (udf_compare_icb(&icb_loc, &source->loc) == 0) {
1947 error = EINVAL;
1948 goto out;
1949 }
1950
1951 /* did we encounter the root node? */
1952 if (udf_compare_icb(&icb_loc, root_icb) == 0) {
1953 error = 0;
1954 goto out;
1955 }
1956
1957 /* push our intermediate node, we're done with it */
1958 /* DPRINTF(NODE, ("\tvput %p\n", target->vnode)); */
1959 vput(res_node->vnode);
1960
1961 DPRINTF(NODE, ("\tgetting the .. node\n"));
1962 error = udf_get_node(ump, &icb_loc, &res_node);
1963
1964 if (error) { /* argh, bail out */
1965 KASSERT(res_node == NULL);
1966 // res_node = NULL;
1967 goto out;
1968 }
1969 }
1970 out:
1971 DPRINTF(NODE, ("\tresult: %svalid, error = %d\n", error?"in":"", error));
1972
1973 /* put our last node */
1974 if (res_node)
1975 vput(res_node->vnode);
1976
1977 return error;
1978 }
1979
1980 /* note: i tried to follow the logics of the tmpfs rename code */
1981 int
1982 udf_rename(void *v)
1983 {
1984 struct vop_rename_args /* {
1985 struct vnode *a_fdvp;
1986 struct vnode *a_fvp;
1987 struct componentname *a_fcnp;
1988 struct vnode *a_tdvp;
1989 struct vnode *a_tvp;
1990 struct componentname *a_tcnp;
1991 } */ *ap = v;
1992 struct vnode *tvp = ap->a_tvp;
1993 struct vnode *tdvp = ap->a_tdvp;
1994 struct vnode *fvp = ap->a_fvp;
1995 struct vnode *fdvp = ap->a_fdvp;
1996 struct componentname *tcnp = ap->a_tcnp;
1997 struct componentname *fcnp = ap->a_fcnp;
1998 struct udf_node *fnode, *fdnode, *tnode, *tdnode;
1999 struct vattr fvap, tvap;
2000 int error;
2001
2002 DPRINTF(CALL, ("udf_rename called\n"));
2003
2004 /* disallow cross-device renames */
2005 if (fvp->v_mount != tdvp->v_mount ||
2006 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
2007 error = EXDEV;
2008 goto out_unlocked;
2009 }
2010
2011 fnode = VTOI(fvp);
2012 fdnode = VTOI(fdvp);
2013 tnode = (tvp == NULL) ? NULL : VTOI(tvp);
2014 tdnode = VTOI(tdvp);
2015
2016 /* lock our source dir */
2017 if (fdnode != tdnode) {
2018 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
2019 if (error != 0)
2020 goto out_unlocked;
2021 }
2022
2023 /* get info about the node to be moved */
2024 vn_lock(fvp, LK_SHARED | LK_RETRY);
2025 error = VOP_GETATTR(fvp, &fvap, FSCRED);
2026 VOP_UNLOCK(fvp);
2027 KASSERT(error == 0);
2028
2029 /* check when to delete the old already existing entry */
2030 if (tvp) {
2031 /* get info about the node to be moved to */
2032 error = VOP_GETATTR(tvp, &tvap, FSCRED);
2033 KASSERT(error == 0);
2034
2035 /* if both dirs, make sure the destination is empty */
2036 if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
2037 if (tvap.va_nlink > 2) {
2038 error = ENOTEMPTY;
2039 goto out;
2040 }
2041 }
2042 /* if moving dir, make sure destination is dir too */
2043 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2044 error = ENOTDIR;
2045 goto out;
2046 }
2047 /* if we're moving a non-directory, make sure dest is no dir */
2048 if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2049 error = EISDIR;
2050 goto out;
2051 }
2052 }
2053
2054 /* check if moving a directory to a new parent is allowed */
2055 if ((fdnode != tdnode) && (fvp->v_type == VDIR)) {
2056 /* release tvp since we might encounter it and lock up */
2057 if (tvp)
2058 vput(tvp);
2059
2060 /* vref tdvp since we lose its ref in udf_on_rootpath */
2061 vref(tdvp);
2062
2063 /* search if fnode is a component of tdnode's path to root */
2064 error = udf_on_rootpath(fnode, tdnode);
2065
2066 DPRINTF(NODE, ("Dir rename allowed ? %s\n", error ? "NO":"YES"));
2067
2068 if (error) {
2069 /* compensate for our vref earlier */
2070 vrele(tdvp);
2071 goto out;
2072 }
2073
2074 /* relock tdvp; its still here due to the vref earlier */
2075 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
2076
2077 /*
2078 * re-lookup tvp since the parent has been unlocked, so could
2079 * have changed/removed in the meantime.
2080 */
2081 error = relookup(tdvp, &tvp, tcnp, 0);
2082 if (error) {
2083 vput(tdvp);
2084 goto out;
2085 }
2086 tnode = (tvp == NULL) ? NULL : VTOI(tvp);
2087 }
2088
2089 /* remove existing entry if present */
2090 if (tvp)
2091 udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
2092
2093 /* create new directory entry for the node */
2094 error = udf_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
2095 if (error)
2096 goto out;
2097
2098 /* unlink old directory entry for the node, if failing, unattach new */
2099 error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
2100 if (error)
2101 udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
2102 if (error)
2103 goto out;
2104
2105 /* update tnode's '..' if moving directory to new parent */
2106 if ((fdnode != tdnode) && (fvp->v_type == VDIR)) {
2107 /* update fnode's '..' entry */
2108 error = udf_dir_update_rootentry(fnode->ump, fnode, tdnode);
2109 if (error) {
2110 /* 'try' to recover from this situation */
2111 udf_dir_attach(tdnode->ump, fdnode, fnode, &fvap, fcnp);
2112 udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
2113 }
2114 }
2115
2116 out:
2117 if (fdnode != tdnode)
2118 VOP_UNLOCK(fdvp);
2119
2120 out_unlocked:
2121 VOP_ABORTOP(tdvp, tcnp);
2122 if (tdvp == tvp)
2123 vrele(tdvp);
2124 else
2125 vput(tdvp);
2126 if (tvp)
2127 vput(tvp);
2128 VOP_ABORTOP(fdvp, fcnp);
2129
2130 /* release source nodes. */
2131 vrele(fdvp);
2132 vrele(fvp);
2133
2134 return error;
2135 }
2136
2137 /* --------------------------------------------------------------------- */
2138
2139 int
2140 udf_remove(void *v)
2141 {
2142 struct vop_remove_args /* {
2143 struct vnode *a_dvp;
2144 struct vnode *a_vp;
2145 struct componentname *a_cnp;
2146 } */ *ap = v;
2147 struct vnode *dvp = ap->a_dvp;
2148 struct vnode *vp = ap->a_vp;
2149 struct componentname *cnp = ap->a_cnp;
2150 struct udf_node *dir_node = VTOI(dvp);
2151 struct udf_node *udf_node = VTOI(vp);
2152 struct udf_mount *ump = dir_node->ump;
2153 int error;
2154
2155 DPRINTF(CALL, ("udf_remove called\n"));
2156 if (vp->v_type != VDIR) {
2157 error = udf_dir_detach(ump, dir_node, udf_node, cnp);
2158 DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
2159 } else {
2160 DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
2161 error = EPERM;
2162 }
2163
2164 if (error == 0) {
2165 VN_KNOTE(vp, NOTE_DELETE);
2166 VN_KNOTE(dvp, NOTE_WRITE);
2167 }
2168
2169 if (dvp == vp)
2170 vrele(vp);
2171 else
2172 vput(vp);
2173 vput(dvp);
2174
2175 return error;
2176 }
2177
2178 /* --------------------------------------------------------------------- */
2179
2180 int
2181 udf_rmdir(void *v)
2182 {
2183 struct vop_rmdir_args /* {
2184 struct vnode *a_dvp;
2185 struct vnode *a_vp;
2186 struct componentname *a_cnp;
2187 } */ *ap = v;
2188 struct vnode *vp = ap->a_vp;
2189 struct vnode *dvp = ap->a_dvp;
2190 struct componentname *cnp = ap->a_cnp;
2191 struct udf_node *dir_node = VTOI(dvp);
2192 struct udf_node *udf_node = VTOI(vp);
2193 struct udf_mount *ump = dir_node->ump;
2194 int error, isempty;
2195
2196 DPRINTF(NOTIMPL, ("udf_rmdir '%s' called\n", cnp->cn_nameptr));
2197
2198 /* don't allow '.' to be deleted */
2199 if (dir_node == udf_node) {
2200 vrele(dvp);
2201 vput(vp);
2202 return EINVAL;
2203 }
2204
2205 /* make sure our `leaf' node's hash is populated */
2206 dirhash_get(&udf_node->dir_hash);
2207 error = udf_dirhash_fill(udf_node);
2208 if (error) {
2209 dirhash_put(udf_node->dir_hash);
2210 return error;
2211 }
2212
2213 /* check to see if the directory is empty */
2214 isempty = dirhash_dir_isempty(udf_node->dir_hash);
2215 dirhash_put(udf_node->dir_hash);
2216
2217 if (!isempty) {
2218 vput(dvp);
2219 vput(vp);
2220 return ENOTEMPTY;
2221 }
2222
2223 /* detach the node from the directory, udf_node is an empty dir here */
2224 error = udf_dir_detach(ump, dir_node, udf_node, cnp);
2225 if (error == 0) {
2226 cache_purge(vp);
2227 // cache_purge(dvp); /* XXX from msdosfs, why? */
2228 /*
2229 * Bug alert: we need to remove '..' from the detaching
2230 * udf_node so further lookups of this are not possible. This
2231 * prevents a process in a deleted directory from going to its
2232 * deleted parent. Since `udf_node' is garanteed to be empty
2233 * here, trunc it so no fids are there.
2234 */
2235 dirhash_purge(&udf_node->dir_hash);
2236 udf_shrink_node(udf_node, 0);
2237 VN_KNOTE(vp, NOTE_DELETE);
2238 }
2239 DPRINTFIF(NODE, error, ("\tgot error removing dir\n"));
2240
2241 /* unput the nodes and exit */
2242 vput(dvp);
2243 vput(vp);
2244
2245 return error;
2246 }
2247
2248 /* --------------------------------------------------------------------- */
2249
2250 int
2251 udf_fsync(void *v)
2252 {
2253 struct vop_fsync_args /* {
2254 struct vnode *a_vp;
2255 kauth_cred_t a_cred;
2256 int a_flags;
2257 off_t offlo;
2258 off_t offhi;
2259 struct proc *a_p;
2260 } */ *ap = v;
2261 struct vnode *vp = ap->a_vp;
2262 struct udf_node *udf_node = VTOI(vp);
2263 int error, flags, wait;
2264
2265 DPRINTF(SYNC, ("udf_fsync called on %p : %s, %s\n",
2266 udf_node,
2267 (ap->a_flags & FSYNC_WAIT) ? "wait":"no wait",
2268 (ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
2269
2270 /* flush data and wait for it when requested */
2271 wait = (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
2272 error = vflushbuf(vp, ap->a_flags);
2273 if (error)
2274 return error;
2275
2276 if (udf_node == NULL) {
2277 printf("udf_fsync() called on NULL udf_node!\n");
2278 return 0;
2279 }
2280 if (vp->v_tag != VT_UDF) {
2281 printf("udf_fsync() called on node not tagged as UDF node!\n");
2282 return 0;
2283 }
2284
2285 /* set our times */
2286 udf_itimes(udf_node, NULL, NULL, NULL);
2287
2288 /* if called when mounted readonly, never write back */
2289 if (vp->v_mount->mnt_flag & MNT_RDONLY)
2290 return 0;
2291
2292 /* if only data is requested, return */
2293 if (ap->a_flags & FSYNC_DATAONLY)
2294 return 0;
2295
2296 /* check if the node is dirty 'enough'*/
2297 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
2298 if (flags == 0)
2299 return 0;
2300
2301 /* if we don't have to wait, check for IO pending */
2302 if (!wait) {
2303 if (vp->v_numoutput > 0) {
2304 DPRINTF(SYNC, ("udf_fsync %p, rejecting on v_numoutput\n", udf_node));
2305 return 0;
2306 }
2307 if (udf_node->outstanding_bufs > 0) {
2308 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_bufs\n", udf_node));
2309 return 0;
2310 }
2311 if (udf_node->outstanding_nodedscr > 0) {
2312 DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_nodedscr\n", udf_node));
2313 return 0;
2314 }
2315 }
2316
2317 /* wait until vp->v_numoutput reaches zero i.e. is finished */
2318 if (wait) {
2319 DPRINTF(SYNC, ("udf_fsync %p, waiting\n", udf_node));
2320 mutex_enter(vp->v_interlock);
2321 while (vp->v_numoutput) {
2322 DPRINTF(SYNC, ("udf_fsync %p, v_numoutput %d\n", udf_node, vp->v_numoutput));
2323 cv_timedwait(&vp->v_cv, vp->v_interlock, hz/8);
2324 }
2325 mutex_exit(vp->v_interlock);
2326 DPRINTF(SYNC, ("udf_fsync %p, fin wait\n", udf_node));
2327 }
2328
2329 /* write out node and wait for it if requested */
2330 DPRINTF(SYNC, ("udf_fsync %p, writeout node\n", udf_node));
2331 error = udf_writeout_node(udf_node, wait);
2332 if (error)
2333 return error;
2334
2335 /* TODO/XXX if ap->a_flags & FSYNC_CACHE, we ought to do a disc sync */
2336
2337 return 0;
2338 }
2339
2340 /* --------------------------------------------------------------------- */
2341
2342 int
2343 udf_advlock(void *v)
2344 {
2345 struct vop_advlock_args /* {
2346 struct vnode *a_vp;
2347 void *a_id;
2348 int a_op;
2349 struct flock *a_fl;
2350 int a_flags;
2351 } */ *ap = v;
2352 struct vnode *vp = ap->a_vp;
2353 struct udf_node *udf_node = VTOI(vp);
2354 struct file_entry *fe;
2355 struct extfile_entry *efe;
2356 uint64_t file_size;
2357
2358 DPRINTF(LOCKING, ("udf_advlock called\n"));
2359
2360 /* get directory filesize */
2361 if (udf_node->fe) {
2362 fe = udf_node->fe;
2363 file_size = udf_rw64(fe->inf_len);
2364 } else {
2365 assert(udf_node->efe);
2366 efe = udf_node->efe;
2367 file_size = udf_rw64(efe->inf_len);
2368 }
2369
2370 return lf_advlock(ap, &udf_node->lockf, file_size);
2371 }
2372
2373 /* --------------------------------------------------------------------- */
2374
2375 /* Global vfs vnode data structures for udfs */
2376 int (**udf_vnodeop_p)(void *);
2377
2378 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
2379 { &vop_default_desc, vn_default_error },
2380 { &vop_lookup_desc, udf_lookup }, /* lookup */
2381 { &vop_create_desc, udf_create }, /* create */
2382 { &vop_mknod_desc, udf_mknod }, /* mknod */ /* TODO */
2383 { &vop_open_desc, udf_open }, /* open */
2384 { &vop_close_desc, udf_close }, /* close */
2385 { &vop_access_desc, udf_access }, /* access */
2386 { &vop_getattr_desc, udf_getattr }, /* getattr */
2387 { &vop_setattr_desc, udf_setattr }, /* setattr */ /* TODO chflags */
2388 { &vop_read_desc, udf_read }, /* read */
2389 { &vop_write_desc, udf_write }, /* write */ /* WRITE */
2390 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */
2391 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */
2392 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */
2393 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */
2394 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */
2395 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */
2396 { &vop_fsync_desc, udf_fsync }, /* fsync */
2397 { &vop_seek_desc, genfs_seek }, /* seek */
2398 { &vop_remove_desc, udf_remove }, /* remove */
2399 { &vop_link_desc, udf_link }, /* link */ /* TODO */
2400 { &vop_rename_desc, udf_rename }, /* rename */ /* TODO */
2401 { &vop_mkdir_desc, udf_mkdir }, /* mkdir */
2402 { &vop_rmdir_desc, udf_rmdir }, /* rmdir */
2403 { &vop_symlink_desc, udf_symlink }, /* symlink */ /* TODO */
2404 { &vop_readdir_desc, udf_readdir }, /* readdir */
2405 { &vop_readlink_desc, udf_readlink }, /* readlink */ /* TEST ME */
2406 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */
2407 { &vop_inactive_desc, udf_inactive }, /* inactive */
2408 { &vop_reclaim_desc, udf_reclaim }, /* reclaim */
2409 { &vop_lock_desc, genfs_lock }, /* lock */
2410 { &vop_unlock_desc, genfs_unlock }, /* unlock */
2411 { &vop_bmap_desc, udf_trivial_bmap }, /* bmap */ /* 1:1 bmap */
2412 { &vop_strategy_desc, udf_vfsstrategy },/* strategy */
2413 /* { &vop_print_desc, udf_print }, */ /* print */
2414 { &vop_islocked_desc, genfs_islocked }, /* islocked */
2415 { &vop_pathconf_desc, udf_pathconf }, /* pathconf */
2416 { &vop_advlock_desc, udf_advlock }, /* advlock */ /* TEST ME */
2417 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */
2418 { &vop_getpages_desc, genfs_getpages }, /* getpages */
2419 { &vop_putpages_desc, genfs_putpages }, /* putpages */
2420 { NULL, NULL }
2421 };
2422
2423
2424 const struct vnodeopv_desc udf_vnodeop_opv_desc = {
2425 &udf_vnodeop_p, udf_vnodeop_entries
2426 };
2427