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