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