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