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