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