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