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