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