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