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