nilfs_vnops.c revision 1.8 1 /* $NetBSD: nilfs_vnops.c,v 1.8 2010/11/30 10:29:59 dholland Exp $ */
2
3 /*
4 * Copyright (c) 2008, 2009 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 */
28
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __KERNEL_RCSID(0, "$NetBSD: nilfs_vnops.c,v 1.8 2010/11/30 10:29:59 dholland Exp $");
32 #endif /* not lint */
33
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/namei.h>
38 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
39 #include <sys/kernel.h>
40 #include <sys/file.h> /* define FWRITE ... */
41 #include <sys/stat.h>
42 #include <sys/buf.h>
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 #include <sys/signalvar.h>
47 #include <sys/malloc.h>
48 #include <sys/dirent.h>
49 #include <sys/lockf.h>
50 #include <sys/kauth.h>
51
52 #include <miscfs/genfs/genfs.h>
53 #include <uvm/uvm_extern.h>
54
55 #include <fs/nilfs/nilfs_mount.h>
56 #include "nilfs.h"
57 #include "nilfs_subr.h"
58 #include "nilfs_bswap.h"
59
60
61 #define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data)
62
63
64 /* externs */
65 extern int prtactive;
66
67 /* implementations of vnode functions; table follows at end */
68 /* --------------------------------------------------------------------- */
69
70 int
71 nilfs_inactive(void *v)
72 {
73 struct vop_inactive_args /* {
74 struct vnode *a_vp;
75 bool *a_recycle;
76 } */ *ap = v;
77 struct vnode *vp = ap->a_vp;
78 struct nilfs_node *nilfs_node = VTOI(vp);
79
80 DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp)));
81
82 if (nilfs_node == NULL) {
83 DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n"));
84 VOP_UNLOCK(vp);
85 return 0;
86 }
87
88 /*
89 * Optionally flush metadata to disc. If the file has not been
90 * referenced anymore in a directory we ought to free up the resources
91 * on disc if applicable.
92 */
93 VOP_UNLOCK(vp);
94
95 return 0;
96 }
97
98 /* --------------------------------------------------------------------- */
99
100 int
101 nilfs_reclaim(void *v)
102 {
103 struct vop_reclaim_args /* {
104 struct vnode *a_vp;
105 } */ *ap = v;
106 struct vnode *vp = ap->a_vp;
107 struct nilfs_node *nilfs_node = VTOI(vp);
108
109 DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node));
110 if (prtactive && vp->v_usecount > 1)
111 vprint("nilfs_reclaim(): pushing active", vp);
112
113 if (nilfs_node == NULL) {
114 DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n"));
115 return 0;
116 }
117
118 /* update note for closure */
119 nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
120
121 /* purge old data from namei */
122 cache_purge(vp);
123
124 /* dispose all node knowledge */
125 nilfs_dispose_node(&nilfs_node);
126
127 return 0;
128 }
129
130 /* --------------------------------------------------------------------- */
131
132 int
133 nilfs_read(void *v)
134 {
135 struct vop_read_args /* {
136 struct vnode *a_vp;
137 struct uio *a_uio;
138 int a_ioflag;
139 kauth_cred_t a_cred;
140 } */ *ap = v;
141 struct vnode *vp = ap->a_vp;
142 struct uio *uio = ap->a_uio;
143 int ioflag = ap->a_ioflag;
144 int advice = IO_ADV_DECODE(ap->a_ioflag);
145 struct uvm_object *uobj;
146 struct nilfs_node *nilfs_node = VTOI(vp);
147 uint64_t file_size;
148 vsize_t len;
149 int error;
150 int flags;
151
152 DPRINTF(READ, ("nilfs_read called\n"));
153
154 /* can this happen? some filingsystems have this check */
155 if (uio->uio_offset < 0)
156 return EINVAL;
157 if (uio->uio_resid == 0)
158 return 0;
159
160 /* protect against rogue programs reading raw directories and links */
161 if ((ioflag & IO_ALTSEMANTICS) == 0) {
162 if (vp->v_type == VDIR)
163 return EISDIR;
164 /* all but regular files just give EINVAL */
165 if (vp->v_type != VREG)
166 return EINVAL;
167 }
168
169 assert(nilfs_node);
170 file_size = nilfs_rw64(nilfs_node->inode.i_size);
171
172 /* read contents using buffercache */
173 uobj = &vp->v_uobj;
174 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
175 error = 0;
176 while (uio->uio_resid > 0) {
177 /* reached end? */
178 if (file_size <= uio->uio_offset)
179 break;
180
181 /* maximise length to file extremity */
182 len = MIN(file_size - uio->uio_offset, uio->uio_resid);
183 if (len == 0)
184 break;
185
186 /* ubc, here we come, prepare to trap */
187 error = ubc_uiomove(uobj, uio, len, advice,
188 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
189 if (error)
190 break;
191 }
192
193 /* note access time unless not requested */
194 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
195 nilfs_node->i_flags |= IN_ACCESS;
196 if ((ioflag & IO_SYNC) == IO_SYNC)
197 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
198 }
199
200 return error;
201 }
202
203 /* --------------------------------------------------------------------- */
204
205 int
206 nilfs_write(void *v)
207 {
208 struct vop_write_args /* {
209 struct vnode *a_vp;
210 struct uio *a_uio;
211 int a_ioflag;
212 kauth_cred_t a_cred;
213 } */ *ap = v;
214 struct vnode *vp = ap->a_vp;
215 struct uio *uio = ap->a_uio;
216 int ioflag = ap->a_ioflag;
217 int advice = IO_ADV_DECODE(ap->a_ioflag);
218 struct uvm_object *uobj;
219 struct nilfs_node *nilfs_node = VTOI(vp);
220 uint64_t file_size, old_size;
221 vsize_t len;
222 int error;
223 int flags, resid, extended;
224
225 DPRINTF(WRITE, ("nilfs_write called\n"));
226
227 /* can this happen? some filingsystems have this check */
228 if (uio->uio_offset < 0)
229 return EINVAL;
230 if (uio->uio_resid == 0)
231 return 0;
232
233 /* protect against rogue programs writing raw directories or links */
234 if ((ioflag & IO_ALTSEMANTICS) == 0) {
235 if (vp->v_type == VDIR)
236 return EISDIR;
237 /* all but regular files just give EINVAL for now */
238 if (vp->v_type != VREG)
239 return EINVAL;
240 }
241
242 assert(nilfs_node);
243 panic("nilfs_write() called\n");
244 return EIO;
245
246 /* remember old file size */
247 assert(nilfs_node);
248 file_size = nilfs_rw64(nilfs_node->inode.i_size);
249 old_size = file_size;
250
251 /* if explicitly asked to append, uio_offset can be wrong? */
252 if (ioflag & IO_APPEND)
253 uio->uio_offset = file_size;
254
255 #if 0
256 extended = (uio->uio_offset + uio->uio_resid > file_size);
257 if (extended) {
258 DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n",
259 file_size, uio->uio_offset + uio->uio_resid));
260 error = nilfs_grow_node(nilfs_node, uio->uio_offset + uio->uio_resid);
261 if (error)
262 return error;
263 file_size = uio->uio_offset + uio->uio_resid;
264 }
265 #endif
266
267 /* write contents using buffercache */
268 uobj = &vp->v_uobj;
269 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
270 resid = uio->uio_resid;
271 error = 0;
272
273 uvm_vnp_setwritesize(vp, file_size);
274 while (uio->uio_resid > 0) {
275 /* maximise length to file extremity */
276 len = MIN(file_size - uio->uio_offset, uio->uio_resid);
277 if (len == 0)
278 break;
279
280 /* ubc, here we come, prepare to trap */
281 error = ubc_uiomove(uobj, uio, len, advice,
282 UBC_WRITE | UBC_UNMAP_FLAG(vp));
283 if (error)
284 break;
285 }
286 uvm_vnp_setsize(vp, file_size);
287
288 /* mark node changed and request update */
289 nilfs_node->i_flags |= IN_CHANGE | IN_UPDATE;
290
291 /*
292 * XXX TODO FFS has code here to reset setuid & setgid when we're not
293 * the superuser as a precaution against tampering.
294 */
295
296 /* if we wrote a thing, note write action on vnode */
297 if (resid > uio->uio_resid)
298 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
299
300 if (error) {
301 /* bring back file size to its former size */
302 /* take notice of its errors? */
303 // (void) nilfs_chsize(vp, (u_quad_t) old_size, NOCRED);
304
305 /* roll back uio */
306 uio->uio_offset -= resid - uio->uio_resid;
307 uio->uio_resid = resid;
308 } else {
309 /* if we write and we're synchronous, update node */
310 if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC))
311 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
312 }
313
314 return error;
315 }
316
317
318 /* --------------------------------------------------------------------- */
319
320 /*
321 * bmap functionality that translates logical block numbers to the virtual
322 * block numbers to be stored on the vnode itself.
323 */
324
325 int
326 nilfs_trivial_bmap(void *v)
327 {
328 struct vop_bmap_args /* {
329 struct vnode *a_vp;
330 daddr_t a_bn;
331 struct vnode **a_vpp;
332 daddr_t *a_bnp;
333 int *a_runp;
334 } */ *ap = v;
335 struct vnode *vp = ap->a_vp; /* our node */
336 struct vnode **vpp = ap->a_vpp; /* return node */
337 daddr_t *bnp = ap->a_bnp; /* translated */
338 daddr_t bn = ap->a_bn; /* origional */
339 int *runp = ap->a_runp;
340 struct nilfs_node *node = VTOI(vp);
341 uint64_t *l2vmap;
342 uint32_t blocksize;
343 int blks, run, error;
344
345 DPRINTF(TRANSLATE, ("nilfs_bmap() called\n"));
346 /* XXX could return `-1' to indicate holes/zero's */
347
348 blocksize = node->nilfsdev->blocksize;
349 blks = MAXPHYS / blocksize;
350
351 /* get mapping memory */
352 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
353
354 /* get virtual block numbers for the vnode's buffer span */
355 error = nilfs_btree_nlookup(node, bn, blks, l2vmap);
356 if (error) {
357 free(l2vmap, M_TEMP);
358 return error;
359 }
360
361 /* store virtual blocks on our own vp */
362 if (vpp)
363 *vpp = vp;
364
365 /* start at virt[0] */
366 *bnp = l2vmap[0];
367
368 /* get runlength */
369 run = 1;
370 while ((run < blks) && (l2vmap[run] == *bnp + run))
371 run++;
372
373 /* set runlength */
374 if (runp)
375 *runp = run;
376
377 DPRINTF(TRANSLATE, ("\tstart %"PRIu64" -> %"PRIu64" run %d\n",
378 bn, *bnp, run));
379
380 /* mark not translated on virtual block number 0 */
381 if (*bnp == 0)
382 *bnp = -1;
383
384 /* return success */
385 free(l2vmap, M_TEMP);
386 return 0;
387 }
388
389 /* --------------------------------------------------------------------- */
390
391 static void
392 nilfs_read_filebuf(struct nilfs_node *node, struct buf *bp)
393 {
394 struct nilfs_device *nilfsdev = node->nilfsdev;
395 struct buf *nbp;
396 uint64_t *l2vmap, *v2pmap;
397 uint64_t from, blks;
398 uint32_t blocksize, buf_offset;
399 uint8_t *buf_pos;
400 int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
401 int i, error;
402
403 /*
404 * Translate all the block sectors into a series of buffers to read
405 * asynchronously from the nilfs device. Note that this lookup may
406 * induce readin's too.
407 */
408
409 blocksize = nilfsdev->blocksize;
410
411 from = bp->b_blkno;
412 blks = bp->b_bcount / blocksize;
413
414 DPRINTF(READ, ("\tread in from inode %"PRIu64" blkno %"PRIu64" "
415 "+ %"PRIu64" blocks\n", node->ino, from, blks));
416
417 DPRINTF(READ, ("\t\tblkno %"PRIu64" "
418 "+ %d bytes\n", bp->b_blkno, bp->b_bcount));
419
420 /* get mapping memory */
421 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
422 v2pmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
423
424 /* get virtual block numbers for the vnode's buffer span */
425 for (i = 0; i < blks; i++)
426 l2vmap[i] = from + i;
427
428 /* translate virtual block numbers to physical block numbers */
429 error = nilfs_nvtop(node, blks, l2vmap, v2pmap);
430 if (error)
431 goto out;
432
433 /* issue translated blocks */
434 bp->b_resid = bp->b_bcount;
435 for (i = 0; i < blks; i++) {
436 DPRINTF(READ, ("read_filebuf : ino %"PRIu64" blk %d -> "
437 "%"PRIu64" -> %"PRIu64"\n",
438 node->ino, i, l2vmap[i], v2pmap[i]));
439
440 buf_offset = i * blocksize;
441 buf_pos = (uint8_t *) bp->b_data + buf_offset;
442
443 /* note virtual block 0 marks not mapped */
444 if (l2vmap[i] == 0) {
445 memset(buf_pos, 0, blocksize);
446 nestiobuf_done(bp, blocksize, 0);
447 continue;
448 }
449
450 /* nest iobuf */
451 nbp = getiobuf(NULL, true);
452 nestiobuf_setup(bp, nbp, buf_offset, blocksize);
453 KASSERT(nbp->b_vp == node->vnode);
454 /* nbp is B_ASYNC */
455
456 nbp->b_lblkno = i;
457 nbp->b_blkno = v2pmap[i] * blk2dev; /* in DEV_BSIZE */
458 nbp->b_rawblkno = nbp->b_blkno;
459
460 VOP_STRATEGY(nilfsdev->devvp, nbp);
461 }
462
463 if ((bp->b_flags & B_ASYNC) == 0)
464 biowait(bp);
465
466 out:
467 free(l2vmap, M_TEMP);
468 free(v2pmap, M_TEMP);
469 if (error) {
470 bp->b_error = EIO;
471 biodone(bp);
472 }
473 }
474
475
476 static void
477 nilfs_write_filebuf(struct nilfs_node *node, struct buf *bp)
478 {
479 /* TODO pass on to segment collector */
480 panic("nilfs_strategy writing called\n");
481 }
482
483
484 int
485 nilfs_vfsstrategy(void *v)
486 {
487 struct vop_strategy_args /* {
488 struct vnode *a_vp;
489 struct buf *a_bp;
490 } */ *ap = v;
491 struct vnode *vp = ap->a_vp;
492 struct buf *bp = ap->a_bp;
493 struct nilfs_node *node = VTOI(vp);
494
495 DPRINTF(STRATEGY, ("nilfs_strategy called\n"));
496
497 /* check if we ought to be here */
498 if (vp->v_type == VBLK || vp->v_type == VCHR)
499 panic("nilfs_strategy: spec");
500
501 /* translate if needed and pass on */
502 if (bp->b_flags & B_READ) {
503 nilfs_read_filebuf(node, bp);
504 return bp->b_error;
505 }
506
507 /* send to segment collector */
508 nilfs_write_filebuf(node, bp);
509 return bp->b_error;
510 }
511
512 /* --------------------------------------------------------------------- */
513
514 int
515 nilfs_readdir(void *v)
516 {
517 struct vop_readdir_args /* {
518 struct vnode *a_vp;
519 struct uio *a_uio;
520 kauth_cred_t a_cred;
521 int *a_eofflag;
522 off_t **a_cookies;
523 int *a_ncookies;
524 } */ *ap = v;
525 struct uio *uio = ap->a_uio;
526 struct vnode *vp = ap->a_vp;
527 struct nilfs_node *node = VTOI(vp);
528 struct nilfs_dir_entry *ndirent;
529 struct dirent dirent;
530 struct buf *bp;
531 uint64_t file_size, diroffset, transoffset, blkoff;
532 uint64_t blocknr;
533 uint32_t blocksize = node->nilfsdev->blocksize;
534 uint8_t *pos, name_len;
535 int error;
536
537 DPRINTF(READDIR, ("nilfs_readdir called\n"));
538
539 if (vp->v_type != VDIR)
540 return ENOTDIR;
541
542 file_size = nilfs_rw64(node->inode.i_size);
543
544 /* we are called just as long as we keep on pushing data in */
545 error = 0;
546 if ((uio->uio_offset < file_size) &&
547 (uio->uio_resid >= sizeof(struct dirent))) {
548 diroffset = uio->uio_offset;
549 transoffset = diroffset;
550
551 blocknr = diroffset / blocksize;
552 blkoff = diroffset % blocksize;
553 error = nilfs_bread(node, blocknr, NOCRED, 0, &bp);
554 if (error)
555 return EIO;
556 while (diroffset < file_size) {
557 DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n",
558 diroffset));
559 if (blkoff >= blocksize) {
560 blkoff = 0; blocknr++;
561 brelse(bp, BC_AGE);
562 error = nilfs_bread(node, blocknr, NOCRED, 0,
563 &bp);
564 if (error)
565 return EIO;
566 }
567
568 /* read in one dirent */
569 pos = (uint8_t *) bp->b_data + blkoff;
570 ndirent = (struct nilfs_dir_entry *) pos;
571
572 name_len = ndirent->name_len;
573 memset(&dirent, 0, sizeof(struct dirent));
574 dirent.d_fileno = nilfs_rw64(ndirent->inode);
575 dirent.d_type = ndirent->file_type; /* 1:1 ? */
576 dirent.d_namlen = name_len;
577 strncpy(dirent.d_name, ndirent->name, name_len);
578 dirent.d_reclen = _DIRENT_SIZE(&dirent);
579 DPRINTF(READDIR, ("copying `%*.*s`\n", name_len,
580 name_len, dirent.d_name));
581
582 /*
583 * If there isn't enough space in the uio to return a
584 * whole dirent, break off read
585 */
586 if (uio->uio_resid < _DIRENT_SIZE(&dirent))
587 break;
588
589 /* transfer */
590 if (name_len)
591 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio);
592
593 /* advance */
594 diroffset += nilfs_rw16(ndirent->rec_len);
595 blkoff += nilfs_rw16(ndirent->rec_len);
596
597 /* remember the last entry we transfered */
598 transoffset = diroffset;
599 }
600 brelse(bp, BC_AGE);
601
602 /* pass on last transfered offset */
603 uio->uio_offset = transoffset;
604 }
605
606 if (ap->a_eofflag)
607 *ap->a_eofflag = (uio->uio_offset >= file_size);
608
609 return error;
610 }
611
612 /* --------------------------------------------------------------------- */
613
614 int
615 nilfs_lookup(void *v)
616 {
617 struct vop_lookup_args /* {
618 struct vnode *a_dvp;
619 struct vnode **a_vpp;
620 struct componentname *a_cnp;
621 } */ *ap = v;
622 struct vnode *dvp = ap->a_dvp;
623 struct vnode **vpp = ap->a_vpp;
624 struct componentname *cnp = ap->a_cnp;
625 struct nilfs_node *dir_node, *res_node;
626 struct nilfs_mount *ump;
627 uint64_t ino;
628 const char *name;
629 int namelen, nameiop, islastcn, mounted_ro;
630 int vnodetp;
631 int error, found;
632
633 dir_node = VTOI(dvp);
634 ump = dir_node->ump;
635 *vpp = NULL;
636
637 DPRINTF(LOOKUP, ("nilfs_lookup called\n"));
638
639 /* simplify/clarification flags */
640 nameiop = cnp->cn_nameiop;
641 islastcn = cnp->cn_flags & ISLASTCN;
642 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY;
643
644 /* check exec/dirread permissions first */
645 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
646 if (error)
647 return error;
648
649 DPRINTF(LOOKUP, ("\taccess ok\n"));
650
651 /*
652 * If requesting a modify on the last path element on a read-only
653 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
654 */
655 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
656 return EROFS;
657
658 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
659 cnp->cn_nameptr));
660 /* look in the nami cache; returns 0 on success!! */
661 error = cache_lookup(dvp, vpp, cnp);
662 if (error >= 0)
663 return error;
664
665 DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
666
667 /*
668 * Obviously, the file is not (anymore) in the namecache, we have to
669 * search for it. There are three basic cases: '.', '..' and others.
670 *
671 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
672 */
673 error = 0;
674 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
675 DPRINTF(LOOKUP, ("\tlookup '.'\n"));
676 /* special case 1 '.' */
677 vref(dvp);
678 *vpp = dvp;
679 /* done */
680 } else if (cnp->cn_flags & ISDOTDOT) {
681 /* special case 2 '..' */
682 DPRINTF(LOOKUP, ("\tlookup '..'\n"));
683
684 /* get our node */
685 name = "..";
686 namelen = 2;
687 error = nilfs_lookup_name_in_dir(dvp, name, namelen,
688 &ino, &found);
689 if (error)
690 goto out;
691 if (!found)
692 error = ENOENT;
693
694 /* first unlock parent */
695 VOP_UNLOCK(dvp);
696
697 if (error == 0) {
698 DPRINTF(LOOKUP, ("\tfound '..'\n"));
699 /* try to create/reuse the node */
700 error = nilfs_get_node(ump, ino, &res_node);
701
702 if (!error) {
703 DPRINTF(LOOKUP,
704 ("\tnode retrieved/created OK\n"));
705 *vpp = res_node->vnode;
706 }
707 }
708
709 /* try to relock parent */
710 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
711 } else {
712 DPRINTF(LOOKUP, ("\tlookup file\n"));
713 /* all other files */
714 /* lookup filename in the directory returning its inode */
715 name = cnp->cn_nameptr;
716 namelen = cnp->cn_namelen;
717 error = nilfs_lookup_name_in_dir(dvp, name, namelen,
718 &ino, &found);
719 if (error)
720 goto out;
721 if (!found) {
722 DPRINTF(LOOKUP, ("\tNOT found\n"));
723 /*
724 * UGH, didn't find name. If we're creating or
725 * renaming on the last name this is OK and we ought
726 * to return EJUSTRETURN if its allowed to be created.
727 */
728 error = ENOENT;
729 if (islastcn &&
730 (nameiop == CREATE || nameiop == RENAME))
731 error = 0;
732 if (!error) {
733 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
734 if (!error) {
735 /* keep the component name */
736 cnp->cn_flags |= SAVENAME;
737 error = EJUSTRETURN;
738 }
739 }
740 /* done */
741 } else {
742 /* try to create/reuse the node */
743 error = nilfs_get_node(ump, ino, &res_node);
744 if (!error) {
745 /*
746 * If we are not at the last path component
747 * and found a non-directory or non-link entry
748 * (which may itself be pointing to a
749 * directory), raise an error.
750 */
751 vnodetp = res_node->vnode->v_type;
752 if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
753 if (!islastcn)
754 error = ENOTDIR;
755 }
756
757 }
758 if (!error) {
759 *vpp = res_node->vnode;
760 }
761 }
762 }
763
764 out:
765 /*
766 * Store result in the cache if requested. If we are creating a file,
767 * the file might not be found and thus putting it into the namecache
768 * might be seen as negative caching.
769 */
770 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
771 cache_enter(dvp, *vpp, cnp);
772
773 DPRINTFIF(LOOKUP, error, ("nilfs_lookup returing error %d\n", error));
774
775 return error;
776 }
777
778 /* --------------------------------------------------------------------- */
779
780 static void
781 nilfs_ctime_to_timespec(struct timespec *ts, uint64_t ctime)
782 {
783 ts->tv_sec = ctime;
784 ts->tv_nsec = 0;
785 }
786
787
788 int
789 nilfs_getattr(void *v)
790 {
791 struct vop_getattr_args /* {
792 struct vnode *a_vp;
793 struct vattr *a_vap;
794 kauth_cred_t a_cred;
795 struct lwp *a_l;
796 } */ *ap = v;
797 struct vnode *vp = ap->a_vp;
798 struct vattr *vap = ap->a_vap;
799 struct nilfs_node *node = VTOI(vp);
800 struct nilfs_inode *inode = &node->inode;
801
802 DPRINTF(VFSCALL, ("nilfs_getattr called\n"));
803
804 /* basic info */
805 vattr_null(vap);
806 vap->va_type = vp->v_type;
807 vap->va_mode = nilfs_rw16(inode->i_mode); /* XXX same? */
808 vap->va_nlink = nilfs_rw16(inode->i_links_count);
809 vap->va_uid = nilfs_rw32(inode->i_uid);
810 vap->va_gid = nilfs_rw32(inode->i_gid);
811 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
812 vap->va_fileid = node->ino;
813 vap->va_size = nilfs_rw64(inode->i_size);
814 vap->va_blocksize = node->nilfsdev->blocksize;
815
816 /* times */
817 nilfs_ctime_to_timespec(&vap->va_atime, nilfs_rw64(inode->i_mtime));
818 nilfs_ctime_to_timespec(&vap->va_mtime, nilfs_rw64(inode->i_mtime));
819 nilfs_ctime_to_timespec(&vap->va_ctime, nilfs_rw64(inode->i_ctime));
820 nilfs_ctime_to_timespec(&vap->va_birthtime, nilfs_rw64(inode->i_ctime));
821
822 vap->va_gen = nilfs_rw32(inode->i_generation);
823 vap->va_flags = 0; /* vattr flags */
824 vap->va_bytes = nilfs_rw64(inode->i_blocks) * vap->va_blocksize;
825 vap->va_filerev = vap->va_gen; /* XXX file revision? same as gen? */
826 vap->va_vaflags = 0; /* XXX chflags flags */
827
828 return 0;
829 }
830
831 /* --------------------------------------------------------------------- */
832
833 #if 0
834 static int
835 nilfs_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
836 kauth_cred_t cred)
837 {
838 return EINVAL;
839 }
840
841
842 static int
843 nilfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
844 {
845
846 return EINVAL;
847 }
848
849
850 /* exported */
851 int
852 nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
853 {
854 return EINVAL;
855 }
856
857
858 static int
859 nilfs_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
860 {
861 return EINVAL;
862 }
863
864
865 static int
866 nilfs_chtimes(struct vnode *vp,
867 struct timespec *atime, struct timespec *mtime,
868 struct timespec *birthtime, int setattrflags,
869 kauth_cred_t cred)
870 {
871 return EINVAL;
872 }
873 #endif
874
875
876 int
877 nilfs_setattr(void *v)
878 {
879 struct vop_setattr_args /* {
880 struct vnode *a_vp;
881 struct vattr *a_vap;
882 kauth_cred_t a_cred;
883 struct lwp *a_l;
884 } */ *ap = v;
885 struct vnode *vp = ap->a_vp;
886
887 vp = vp;
888 DPRINTF(VFSCALL, ("nilfs_setattr called\n"));
889 return EINVAL;
890 }
891
892 /* --------------------------------------------------------------------- */
893
894 /*
895 * Return POSIX pathconf information for NILFS file systems.
896 */
897 int
898 nilfs_pathconf(void *v)
899 {
900 struct vop_pathconf_args /* {
901 struct vnode *a_vp;
902 int a_name;
903 register_t *a_retval;
904 } */ *ap = v;
905 uint32_t bits;
906
907 DPRINTF(VFSCALL, ("nilfs_pathconf called\n"));
908
909 switch (ap->a_name) {
910 case _PC_LINK_MAX:
911 *ap->a_retval = (1<<16)-1; /* 16 bits */
912 return 0;
913 case _PC_NAME_MAX:
914 *ap->a_retval = NAME_MAX;
915 return 0;
916 case _PC_PATH_MAX:
917 *ap->a_retval = PATH_MAX;
918 return 0;
919 case _PC_PIPE_BUF:
920 *ap->a_retval = PIPE_BUF;
921 return 0;
922 case _PC_CHOWN_RESTRICTED:
923 *ap->a_retval = 1;
924 return 0;
925 case _PC_NO_TRUNC:
926 *ap->a_retval = 1;
927 return 0;
928 case _PC_SYNC_IO:
929 *ap->a_retval = 0; /* synchronised is off for performance */
930 return 0;
931 case _PC_FILESIZEBITS:
932 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
933 bits = 64; /* XXX ought to deliver 65 */
934 #if 0
935 if (nilfs_node)
936 bits = 64 * vp->v_mount->mnt_dev_bshift;
937 #endif
938 *ap->a_retval = bits;
939 return 0;
940 }
941
942 return EINVAL;
943 }
944
945
946 /* --------------------------------------------------------------------- */
947
948 int
949 nilfs_open(void *v)
950 {
951 struct vop_open_args /* {
952 struct vnode *a_vp;
953 int a_mode;
954 kauth_cred_t a_cred;
955 struct proc *a_p;
956 } */ *ap = v;
957 int flags;
958
959 DPRINTF(VFSCALL, ("nilfs_open called\n"));
960
961 /*
962 * Files marked append-only must be opened for appending.
963 */
964 flags = 0;
965 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
966 return (EPERM);
967
968 return 0;
969 }
970
971
972 /* --------------------------------------------------------------------- */
973
974 int
975 nilfs_close(void *v)
976 {
977 struct vop_close_args /* {
978 struct vnode *a_vp;
979 int a_fflag;
980 kauth_cred_t a_cred;
981 struct proc *a_p;
982 } */ *ap = v;
983 struct vnode *vp = ap->a_vp;
984 struct nilfs_node *nilfs_node = VTOI(vp);
985
986 DPRINTF(VFSCALL, ("nilfs_close called\n"));
987 nilfs_node = nilfs_node; /* shut up gcc */
988
989 mutex_enter(&vp->v_interlock);
990 if (vp->v_usecount > 1)
991 nilfs_itimes(nilfs_node, NULL, NULL, NULL);
992 mutex_exit(&vp->v_interlock);
993
994 return 0;
995 }
996
997
998 /* --------------------------------------------------------------------- */
999
1000 static int
1001 nilfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
1002 {
1003 int flags;
1004
1005 /* check if we are allowed to write */
1006 switch (vap->va_type) {
1007 case VDIR:
1008 case VLNK:
1009 case VREG:
1010 /*
1011 * normal nodes: check if we're on a read-only mounted
1012 * filingsystem and bomb out if we're trying to write.
1013 */
1014 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
1015 return EROFS;
1016 break;
1017 case VBLK:
1018 case VCHR:
1019 case VSOCK:
1020 case VFIFO:
1021 /*
1022 * special nodes: even on read-only mounted filingsystems
1023 * these are allowed to be written to if permissions allow.
1024 */
1025 break;
1026 default:
1027 /* no idea what this is */
1028 return EINVAL;
1029 }
1030
1031 /* noone may write immutable files */
1032 /* TODO: get chflags(2) flags */
1033 flags = 0;
1034 if ((mode & VWRITE) && (flags & IMMUTABLE))
1035 return EPERM;
1036
1037 return 0;
1038 }
1039
1040 static int
1041 nilfs_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
1042 kauth_cred_t cred)
1043 {
1044
1045 /* ask the generic genfs_can_access to advice on security */
1046 return genfs_can_access(vp->v_type,
1047 vap->va_mode, vap->va_uid, vap->va_gid,
1048 mode, cred);
1049 }
1050
1051 int
1052 nilfs_access(void *v)
1053 {
1054 struct vop_access_args /* {
1055 struct vnode *a_vp;
1056 int a_mode;
1057 kauth_cred_t a_cred;
1058 struct proc *a_p;
1059 } */ *ap = v;
1060 struct vnode *vp = ap->a_vp;
1061 mode_t mode = ap->a_mode;
1062 kauth_cred_t cred = ap->a_cred;
1063 /* struct nilfs_node *nilfs_node = VTOI(vp); */
1064 struct vattr vap;
1065 int error;
1066
1067 DPRINTF(VFSCALL, ("nilfs_access called\n"));
1068
1069 error = VOP_GETATTR(vp, &vap, NULL);
1070 if (error)
1071 return error;
1072
1073 error = nilfs_check_possible(vp, &vap, mode);
1074 if (error)
1075 return error;
1076
1077 error = nilfs_check_permitted(vp, &vap, mode, cred);
1078
1079 return error;
1080 }
1081
1082 /* --------------------------------------------------------------------- */
1083
1084 int
1085 nilfs_create(void *v)
1086 {
1087 struct vop_create_args /* {
1088 struct vnode *a_dvp;
1089 struct vnode **a_vpp;
1090 struct componentname *a_cnp;
1091 struct vattr *a_vap;
1092 } */ *ap = v;
1093 struct vnode *dvp = ap->a_dvp;
1094 struct vnode **vpp = ap->a_vpp;
1095 struct vattr *vap = ap->a_vap;
1096 struct componentname *cnp = ap->a_cnp;
1097 int error;
1098
1099 DPRINTF(VFSCALL, ("nilfs_create called\n"));
1100 error = nilfs_create_node(dvp, vpp, vap, cnp);
1101
1102 vput(dvp);
1103 return error;
1104 }
1105
1106 /* --------------------------------------------------------------------- */
1107
1108 int
1109 nilfs_mknod(void *v)
1110 {
1111 struct vop_mknod_args /* {
1112 struct vnode *a_dvp;
1113 struct vnode **a_vpp;
1114 struct componentname *a_cnp;
1115 struct vattr *a_vap;
1116 } */ *ap = v;
1117 struct vnode *dvp = ap->a_dvp;
1118 struct vnode **vpp = ap->a_vpp;
1119 struct vattr *vap = ap->a_vap;
1120 struct componentname *cnp = ap->a_cnp;
1121 int error;
1122
1123 DPRINTF(VFSCALL, ("nilfs_mknod called\n"));
1124 error = nilfs_create_node(dvp, vpp, vap, cnp);
1125
1126 vput(dvp);
1127 return error;
1128 }
1129
1130 /* --------------------------------------------------------------------- */
1131
1132 int
1133 nilfs_mkdir(void *v)
1134 {
1135 struct vop_mkdir_args /* {
1136 struct vnode *a_dvp;
1137 struct vnode **a_vpp;
1138 struct componentname *a_cnp;
1139 struct vattr *a_vap;
1140 } */ *ap = v;
1141 struct vnode *dvp = ap->a_dvp;
1142 struct vnode **vpp = ap->a_vpp;
1143 struct vattr *vap = ap->a_vap;
1144 struct componentname *cnp = ap->a_cnp;
1145 int error;
1146
1147 DPRINTF(VFSCALL, ("nilfs_mkdir called\n"));
1148 error = nilfs_create_node(dvp, vpp, vap, cnp);
1149
1150 vput(dvp);
1151 return error;
1152 }
1153
1154 /* --------------------------------------------------------------------- */
1155
1156 static int
1157 nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1158 {
1159 struct nilfs_node *nilfs_node, *dir_node;
1160 struct vattr vap;
1161 int error;
1162
1163 DPRINTF(VFSCALL, ("nilfs_link called\n"));
1164 error = 0;
1165
1166 /* some quick checks */
1167 if (vp->v_type == VDIR)
1168 return EPERM; /* can't link a directory */
1169 if (dvp->v_mount != vp->v_mount)
1170 return EXDEV; /* can't link across devices */
1171 if (dvp == vp)
1172 return EPERM; /* can't be the same */
1173
1174 /* lock node */
1175 error = vn_lock(vp, LK_EXCLUSIVE);
1176 if (error)
1177 return error;
1178
1179 /* get attributes */
1180 dir_node = VTOI(dvp);
1181 nilfs_node = VTOI(vp);
1182
1183 error = VOP_GETATTR(vp, &vap, FSCRED);
1184 if (error) {
1185 VOP_UNLOCK(vp);
1186 return error;
1187 }
1188
1189 /* check link count overflow */
1190 if (vap.va_nlink >= (1<<16)-1) { /* uint16_t */
1191 VOP_UNLOCK(vp);
1192 return EMLINK;
1193 }
1194
1195 error = nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node,
1196 &vap, cnp);
1197 if (error)
1198 VOP_UNLOCK(vp);
1199 return error;
1200 }
1201
1202 int
1203 nilfs_link(void *v)
1204 {
1205 struct vop_link_args /* {
1206 struct vnode *a_dvp;
1207 struct vnode *a_vp;
1208 struct componentname *a_cnp;
1209 } */ *ap = v;
1210 struct vnode *dvp = ap->a_dvp;
1211 struct vnode *vp = ap->a_vp;
1212 struct componentname *cnp = ap->a_cnp;
1213 int error;
1214
1215 error = nilfs_do_link(dvp, vp, cnp);
1216 if (error)
1217 VOP_ABORTOP(dvp, cnp);
1218
1219 VN_KNOTE(vp, NOTE_LINK);
1220 VN_KNOTE(dvp, NOTE_WRITE);
1221 vput(dvp);
1222
1223 return error;
1224 }
1225
1226 /* --------------------------------------------------------------------- */
1227
1228 static int
1229 nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target)
1230 {
1231 return EROFS;
1232 }
1233
1234
1235 int
1236 nilfs_symlink(void *v)
1237 {
1238 struct vop_symlink_args /* {
1239 struct vnode *a_dvp;
1240 struct vnode **a_vpp;
1241 struct componentname *a_cnp;
1242 struct vattr *a_vap;
1243 char *a_target;
1244 } */ *ap = v;
1245 struct vnode *dvp = ap->a_dvp;
1246 struct vnode **vpp = ap->a_vpp;
1247 struct vattr *vap = ap->a_vap;
1248 struct componentname *cnp = ap->a_cnp;
1249 struct nilfs_node *dir_node;
1250 struct nilfs_node *nilfs_node;
1251 int error;
1252
1253 DPRINTF(VFSCALL, ("nilfs_symlink called\n"));
1254 DPRINTF(VFSCALL, ("\tlinking to `%s`\n", ap->a_target));
1255 error = nilfs_create_node(dvp, vpp, vap, cnp);
1256 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
1257 if (!error) {
1258 dir_node = VTOI(dvp);
1259 nilfs_node = VTOI(*vpp);
1260 KASSERT(nilfs_node);
1261 error = nilfs_do_symlink(nilfs_node, ap->a_target);
1262 if (error) {
1263 /* remove node */
1264 nilfs_shrink_node(nilfs_node, 0);
1265 nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp);
1266 }
1267 }
1268 vput(dvp);
1269 return error;
1270 }
1271
1272 /* --------------------------------------------------------------------- */
1273
1274 int
1275 nilfs_readlink(void *v)
1276 {
1277 struct vop_readlink_args /* {
1278 struct vnode *a_vp;
1279 struct uio *a_uio;
1280 kauth_cred_t a_cred;
1281 } */ *ap = v;
1282 #if 0
1283 struct vnode *vp = ap->a_vp;
1284 struct uio *uio = ap->a_uio;
1285 kauth_cred_t cred = ap->a_cred;
1286 struct nilfs_node *nilfs_node;
1287 struct pathcomp pathcomp;
1288 struct vattr vattr;
1289 uint8_t *pathbuf, *targetbuf, *tmpname;
1290 uint8_t *pathpos, *targetpos;
1291 char *mntonname;
1292 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
1293 int first, error;
1294 #endif
1295 ap = ap;
1296
1297 DPRINTF(VFSCALL, ("nilfs_readlink called\n"));
1298
1299 return EROFS;
1300 }
1301
1302 /* --------------------------------------------------------------------- */
1303
1304 /* note: i tried to follow the logics of the tmpfs rename code */
1305 int
1306 nilfs_rename(void *v)
1307 {
1308 struct vop_rename_args /* {
1309 struct vnode *a_fdvp;
1310 struct vnode *a_fvp;
1311 struct componentname *a_fcnp;
1312 struct vnode *a_tdvp;
1313 struct vnode *a_tvp;
1314 struct componentname *a_tcnp;
1315 } */ *ap = v;
1316 struct vnode *tvp = ap->a_tvp;
1317 struct vnode *tdvp = ap->a_tdvp;
1318 struct vnode *fvp = ap->a_fvp;
1319 struct vnode *fdvp = ap->a_fdvp;
1320 struct componentname *tcnp = ap->a_tcnp;
1321 struct componentname *fcnp = ap->a_fcnp;
1322 struct nilfs_node *fnode, *fdnode, *tnode, *tdnode;
1323 struct vattr fvap, tvap;
1324 int error;
1325
1326 DPRINTF(VFSCALL, ("nilfs_rename called\n"));
1327
1328 /* disallow cross-device renames */
1329 if (fvp->v_mount != tdvp->v_mount ||
1330 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1331 error = EXDEV;
1332 goto out_unlocked;
1333 }
1334
1335 fnode = VTOI(fvp);
1336 fdnode = VTOI(fdvp);
1337 tnode = (tvp == NULL) ? NULL : VTOI(tvp);
1338 tdnode = VTOI(tdvp);
1339
1340 /* lock our source dir */
1341 if (fdnode != tdnode) {
1342 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1343 if (error != 0)
1344 goto out_unlocked;
1345 }
1346
1347 /* get info about the node to be moved */
1348 error = VOP_GETATTR(fvp, &fvap, FSCRED);
1349 KASSERT(error == 0);
1350
1351 /* check when to delete the old already existing entry */
1352 if (tvp) {
1353 /* get info about the node to be moved to */
1354 error = VOP_GETATTR(fvp, &tvap, FSCRED);
1355 KASSERT(error == 0);
1356
1357 /* if both dirs, make sure the destination is empty */
1358 if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
1359 if (tvap.va_nlink > 2) {
1360 error = ENOTEMPTY;
1361 goto out;
1362 }
1363 }
1364 /* if moving dir, make sure destination is dir too */
1365 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1366 error = ENOTDIR;
1367 goto out;
1368 }
1369 /* if we're moving a non-directory, make sure dest is no dir */
1370 if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1371 error = EISDIR;
1372 goto out;
1373 }
1374 }
1375
1376 /* dont allow renaming directories acros directory for now */
1377 if (fdnode != tdnode) {
1378 if (fvp->v_type == VDIR) {
1379 error = EINVAL;
1380 goto out;
1381 }
1382 }
1383
1384 /* remove existing entry if present */
1385 if (tvp)
1386 nilfs_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
1387
1388 /* create new directory entry for the node */
1389 error = nilfs_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
1390 if (error)
1391 goto out;
1392
1393 /* unlink old directory entry for the node, if failing, unattach new */
1394 error = nilfs_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
1395 if (error)
1396 nilfs_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
1397
1398 out:
1399 if (fdnode != tdnode)
1400 VOP_UNLOCK(fdvp);
1401
1402 out_unlocked:
1403 VOP_ABORTOP(tdvp, tcnp);
1404 if (tdvp == tvp)
1405 vrele(tdvp);
1406 else
1407 vput(tdvp);
1408 if (tvp)
1409 vput(tvp);
1410 VOP_ABORTOP(fdvp, fcnp);
1411
1412 /* release source nodes. */
1413 vrele(fdvp);
1414 vrele(fvp);
1415
1416 return error;
1417 }
1418
1419 /* --------------------------------------------------------------------- */
1420
1421 int
1422 nilfs_remove(void *v)
1423 {
1424 struct vop_remove_args /* {
1425 struct vnode *a_dvp;
1426 struct vnode *a_vp;
1427 struct componentname *a_cnp;
1428 } */ *ap = v;
1429 struct vnode *dvp = ap->a_dvp;
1430 struct vnode *vp = ap->a_vp;
1431 struct componentname *cnp = ap->a_cnp;
1432 struct nilfs_node *dir_node = VTOI(dvp);
1433 struct nilfs_node *nilfs_node = VTOI(vp);
1434 struct nilfs_mount *ump = dir_node->ump;
1435 int error;
1436
1437 DPRINTF(VFSCALL, ("nilfs_remove called\n"));
1438 if (vp->v_type != VDIR) {
1439 error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
1440 DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
1441 } else {
1442 DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
1443 error = EPERM;
1444 }
1445
1446 if (error == 0) {
1447 VN_KNOTE(vp, NOTE_DELETE);
1448 VN_KNOTE(dvp, NOTE_WRITE);
1449 }
1450
1451 if (dvp == vp)
1452 vrele(vp);
1453 else
1454 vput(vp);
1455 vput(dvp);
1456
1457 return error;
1458 }
1459
1460 /* --------------------------------------------------------------------- */
1461
1462 int
1463 nilfs_rmdir(void *v)
1464 {
1465 struct vop_rmdir_args /* {
1466 struct vnode *a_dvp;
1467 struct vnode *a_vp;
1468 struct componentname *a_cnp;
1469 } */ *ap = v;
1470 struct vnode *vp = ap->a_vp;
1471 struct vnode *dvp = ap->a_dvp;
1472 struct componentname *cnp = ap->a_cnp;
1473 struct nilfs_node *dir_node = VTOI(dvp);
1474 struct nilfs_node *nilfs_node = VTOI(vp);
1475 struct nilfs_mount *ump = dir_node->ump;
1476 int refcnt, error;
1477
1478 DPRINTF(NOTIMPL, ("nilfs_rmdir called\n"));
1479
1480 /* don't allow '.' to be deleted */
1481 if (dir_node == nilfs_node) {
1482 vrele(dvp);
1483 vput(vp);
1484 return EINVAL;
1485 }
1486
1487 /* check to see if the directory is empty */
1488 error = 0;
1489 refcnt = 2; /* XXX */
1490 if (refcnt > 1) {
1491 /* NOT empty */
1492 vput(dvp);
1493 vput(vp);
1494 return ENOTEMPTY;
1495 }
1496
1497 /* detach the node from the directory */
1498 error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp);
1499 if (error == 0) {
1500 cache_purge(vp);
1501 // cache_purge(dvp); /* XXX from msdosfs, why? */
1502 VN_KNOTE(vp, NOTE_DELETE);
1503 }
1504 DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
1505
1506 /* unput the nodes and exit */
1507 vput(dvp);
1508 vput(vp);
1509
1510 return error;
1511 }
1512
1513 /* --------------------------------------------------------------------- */
1514
1515 int
1516 nilfs_fsync(void *v)
1517 {
1518 struct vop_fsync_args /* {
1519 struct vnode *a_vp;
1520 kauth_cred_t a_cred;
1521 int a_flags;
1522 off_t offlo;
1523 off_t offhi;
1524 struct proc *a_p;
1525 } */ *ap = v;
1526 struct vnode *vp = ap->a_vp;
1527 // struct nilfs_node *nilfs_node = VTOI(vp);
1528 // int error, flags, wait;
1529
1530 DPRINTF(STRATEGY, ("nilfs_fsync called : %s, %s\n",
1531 (ap->a_flags & FSYNC_WAIT) ? "wait":"no wait",
1532 (ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
1533
1534 vp = vp;
1535 return 0;
1536 }
1537
1538 /* --------------------------------------------------------------------- */
1539
1540 int
1541 nilfs_advlock(void *v)
1542 {
1543 struct vop_advlock_args /* {
1544 struct vnode *a_vp;
1545 void *a_id;
1546 int a_op;
1547 struct flock *a_fl;
1548 int a_flags;
1549 } */ *ap = v;
1550 struct vnode *vp = ap->a_vp;
1551 struct nilfs_node *nilfs_node = VTOI(vp);
1552 uint64_t file_size;
1553
1554 DPRINTF(LOCKING, ("nilfs_advlock called\n"));
1555
1556 assert(nilfs_node);
1557 file_size = nilfs_rw64(nilfs_node->inode.i_size);
1558
1559 return lf_advlock(ap, &nilfs_node->lockf, file_size);
1560 }
1561
1562 /* --------------------------------------------------------------------- */
1563
1564
1565 /* Global vfs vnode data structures for nilfss */
1566 int (**nilfs_vnodeop_p) __P((void *));
1567
1568 const struct vnodeopv_entry_desc nilfs_vnodeop_entries[] = {
1569 { &vop_default_desc, vn_default_error },
1570 { &vop_lookup_desc, nilfs_lookup }, /* lookup */
1571 { &vop_create_desc, nilfs_create }, /* create */
1572 { &vop_mknod_desc, nilfs_mknod }, /* mknod */ /* TODO */
1573 { &vop_open_desc, nilfs_open }, /* open */
1574 { &vop_close_desc, nilfs_close }, /* close */
1575 { &vop_access_desc, nilfs_access }, /* access */
1576 { &vop_getattr_desc, nilfs_getattr }, /* getattr */
1577 { &vop_setattr_desc, nilfs_setattr }, /* setattr */ /* TODO chflags */
1578 { &vop_read_desc, nilfs_read }, /* read */
1579 { &vop_write_desc, nilfs_write }, /* write */ /* WRITE */
1580 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */
1581 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */
1582 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */
1583 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */
1584 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */
1585 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */
1586 { &vop_fsync_desc, nilfs_fsync }, /* fsync */
1587 { &vop_seek_desc, genfs_seek }, /* seek */
1588 { &vop_remove_desc, nilfs_remove }, /* remove */
1589 { &vop_link_desc, nilfs_link }, /* link */ /* TODO */
1590 { &vop_rename_desc, nilfs_rename }, /* rename */ /* TODO */
1591 { &vop_mkdir_desc, nilfs_mkdir }, /* mkdir */
1592 { &vop_rmdir_desc, nilfs_rmdir }, /* rmdir */
1593 { &vop_symlink_desc, nilfs_symlink }, /* symlink */ /* TODO */
1594 { &vop_readdir_desc, nilfs_readdir }, /* readdir */
1595 { &vop_readlink_desc, nilfs_readlink }, /* readlink */ /* TEST ME */
1596 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */
1597 { &vop_inactive_desc, nilfs_inactive }, /* inactive */
1598 { &vop_reclaim_desc, nilfs_reclaim }, /* reclaim */
1599 { &vop_lock_desc, genfs_lock }, /* lock */
1600 { &vop_unlock_desc, genfs_unlock }, /* unlock */
1601 { &vop_bmap_desc, nilfs_trivial_bmap }, /* bmap */ /* 1:1 bmap */
1602 { &vop_strategy_desc, nilfs_vfsstrategy },/* strategy */
1603 /* { &vop_print_desc, nilfs_print }, */ /* print */
1604 { &vop_islocked_desc, genfs_islocked }, /* islocked */
1605 { &vop_pathconf_desc, nilfs_pathconf }, /* pathconf */
1606 { &vop_advlock_desc, nilfs_advlock }, /* advlock */ /* TEST ME */
1607 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */
1608 { &vop_getpages_desc, genfs_getpages }, /* getpages */
1609 { &vop_putpages_desc, genfs_putpages }, /* putpages */
1610 { NULL, NULL }
1611 };
1612
1613
1614 const struct vnodeopv_desc nilfs_vnodeop_opv_desc = {
1615 &nilfs_vnodeop_p, nilfs_vnodeop_entries
1616 };
1617
1618