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