/
nilfs_vnops.c
1612 lines (1349 loc) · 38.8 KB
/
nilfs_vnops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: nilfs_vnops.c,v 1.16.6.2 2013/06/13 06:49:08 msaitoh Exp $ */
/*
* Copyright (c) 2008, 2009 Reinoud Zandijk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/cdefs.h>
#ifndef lint
__KERNEL_RCSID(0, "$NetBSD: nilfs_vnops.c,v 1.16.6.2 2013/06/13 06:49:08 msaitoh Exp $");
#endif /* not lint */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/namei.h>
#include <sys/resourcevar.h> /* defines plimit structure in proc struct */
#include <sys/kernel.h>
#include <sys/file.h> /* define FWRITE ... */
#include <sys/stat.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/signalvar.h>
#include <sys/malloc.h>
#include <sys/dirent.h>
#include <sys/lockf.h>
#include <sys/kauth.h>
#include <miscfs/genfs/genfs.h>
#include <uvm/uvm_extern.h>
#include <fs/nilfs/nilfs_mount.h>
#include "nilfs.h"
#include "nilfs_subr.h"
#include "nilfs_bswap.h"
#define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data)
/* externs */
extern int prtactive;
/* implementations of vnode functions; table follows at end */
/* --------------------------------------------------------------------- */
int
nilfs_inactive(void *v)
{
struct vop_inactive_args /* {
struct vnode *a_vp;
bool *a_recycle;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct nilfs_node *nilfs_node = VTOI(vp);
DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp)));
if (nilfs_node == NULL) {
DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n"));
VOP_UNLOCK(vp);
return 0;
}
/*
* Optionally flush metadata to disc. If the file has not been
* referenced anymore in a directory we ought to free up the resources
* on disc if applicable.
*/
VOP_UNLOCK(vp);
return 0;
}
/* --------------------------------------------------------------------- */
int
nilfs_reclaim(void *v)
{
struct vop_reclaim_args /* {
struct vnode *a_vp;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct nilfs_node *nilfs_node = VTOI(vp);
DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node));
if (prtactive && vp->v_usecount > 1)
vprint("nilfs_reclaim(): pushing active", vp);
if (nilfs_node == NULL) {
DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n"));
return 0;
}
/* update note for closure */
nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
/* dispose all node knowledge */
nilfs_dispose_node(&nilfs_node);
return 0;
}
/* --------------------------------------------------------------------- */
int
nilfs_read(void *v)
{
struct vop_read_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
kauth_cred_t a_cred;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
int ioflag = ap->a_ioflag;
int advice = IO_ADV_DECODE(ap->a_ioflag);
struct uvm_object *uobj;
struct nilfs_node *nilfs_node = VTOI(vp);
uint64_t file_size;
vsize_t len;
int error;
DPRINTF(READ, ("nilfs_read called\n"));
/* can this happen? some filingsystems have this check */
if (uio->uio_offset < 0)
return EINVAL;
if (uio->uio_resid == 0)
return 0;
/* protect against rogue programs reading raw directories and links */
if ((ioflag & IO_ALTSEMANTICS) == 0) {
if (vp->v_type == VDIR)
return EISDIR;
/* all but regular files just give EINVAL */
if (vp->v_type != VREG)
return EINVAL;
}
assert(nilfs_node);
file_size = nilfs_rw64(nilfs_node->inode.i_size);
/* read contents using buffercache */
uobj = &vp->v_uobj;
error = 0;
while (uio->uio_resid > 0) {
/* reached end? */
if (file_size <= uio->uio_offset)
break;
/* maximise length to file extremity */
len = MIN(file_size - uio->uio_offset, uio->uio_resid);
if (len == 0)
break;
/* ubc, here we come, prepare to trap */
error = ubc_uiomove(uobj, uio, len, advice,
UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
if (error)
break;
}
/* note access time unless not requested */
if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
nilfs_node->i_flags |= IN_ACCESS;
if ((ioflag & IO_SYNC) == IO_SYNC)
error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
}
return error;
}
/* --------------------------------------------------------------------- */
int
nilfs_write(void *v)
{
struct vop_write_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
kauth_cred_t a_cred;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
int ioflag = ap->a_ioflag;
int advice = IO_ADV_DECODE(ap->a_ioflag);
struct uvm_object *uobj;
struct nilfs_node *nilfs_node = VTOI(vp);
uint64_t file_size, old_size;
vsize_t len;
int error, resid, extended;
DPRINTF(WRITE, ("nilfs_write called\n"));
/* can this happen? some filingsystems have this check */
if (uio->uio_offset < 0)
return EINVAL;
if (uio->uio_resid == 0)
return 0;
/* protect against rogue programs writing raw directories or links */
if ((ioflag & IO_ALTSEMANTICS) == 0) {
if (vp->v_type == VDIR)
return EISDIR;
/* all but regular files just give EINVAL for now */
if (vp->v_type != VREG)
return EINVAL;
}
assert(nilfs_node);
panic("nilfs_write() called\n");
return EIO;
/* remember old file size */
assert(nilfs_node);
file_size = nilfs_rw64(nilfs_node->inode.i_size);
old_size = file_size;
/* if explicitly asked to append, uio_offset can be wrong? */
if (ioflag & IO_APPEND)
uio->uio_offset = file_size;
#if 0
extended = (uio->uio_offset + uio->uio_resid > file_size);
if (extended) {
DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n",
file_size, uio->uio_offset + uio->uio_resid));
error = nilfs_grow_node(nilfs_node, uio->uio_offset + uio->uio_resid);
if (error)
return error;
file_size = uio->uio_offset + uio->uio_resid;
}
#endif
/* write contents using buffercache */
uobj = &vp->v_uobj;
resid = uio->uio_resid;
error = 0;
uvm_vnp_setwritesize(vp, file_size);
while (uio->uio_resid > 0) {
/* maximise length to file extremity */
len = MIN(file_size - uio->uio_offset, uio->uio_resid);
if (len == 0)
break;
/* ubc, here we come, prepare to trap */
error = ubc_uiomove(uobj, uio, len, advice,
UBC_WRITE | UBC_UNMAP_FLAG(vp));
if (error)
break;
}
uvm_vnp_setsize(vp, file_size);
/* mark node changed and request update */
nilfs_node->i_flags |= IN_CHANGE | IN_UPDATE;
if (vp->v_mount->mnt_flag & MNT_RELATIME)
nilfs_node->i_flags |= IN_ACCESS;
/*
* XXX TODO FFS has code here to reset setuid & setgid when we're not
* the superuser as a precaution against tampering.
*/
/* if we wrote a thing, note write action on vnode */
if (resid > uio->uio_resid)
VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
if (error) {
/* bring back file size to its former size */
/* take notice of its errors? */
// (void) nilfs_chsize(vp, (u_quad_t) old_size, NOCRED);
/* roll back uio */
uio->uio_offset -= resid - uio->uio_resid;
uio->uio_resid = resid;
} else {
/* if we write and we're synchronous, update node */
if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC))
error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
}
return error;
}
/* --------------------------------------------------------------------- */
/*
* bmap functionality that translates logical block numbers to the virtual
* block numbers to be stored on the vnode itself.
*
* Important alert!
*
* If runp is not NULL, the number of contiguous blocks __starting from the
* next block after the queried block__ will be returned in runp.
*/
int
nilfs_trivial_bmap(void *v)
{
struct vop_bmap_args /* {
struct vnode *a_vp;
daddr_t a_bn;
struct vnode **a_vpp;
daddr_t *a_bnp;
int *a_runp;
} */ *ap = v;
struct vnode *vp = ap->a_vp; /* our node */
struct vnode **vpp = ap->a_vpp; /* return node */
daddr_t *bnp = ap->a_bnp; /* translated */
daddr_t bn = ap->a_bn; /* origional */
int *runp = ap->a_runp;
struct nilfs_node *node = VTOI(vp);
uint64_t *l2vmap;
uint32_t blocksize;
int blks, run, error;
DPRINTF(TRANSLATE, ("nilfs_bmap() called\n"));
/* XXX could return `-1' to indicate holes/zero's */
blocksize = node->nilfsdev->blocksize;
blks = MAXPHYS / blocksize;
/* get mapping memory */
l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
/* get virtual block numbers for the vnode's buffer span */
error = nilfs_btree_nlookup(node, bn, blks, l2vmap);
if (error) {
free(l2vmap, M_TEMP);
return error;
}
/* store virtual blocks on our own vp */
if (vpp)
*vpp = vp;
/* start at virt[0] */
*bnp = l2vmap[0];
/* get runlength */
run = 1;
while ((run < blks) && (l2vmap[run] == *bnp + run))
run++;
run--; /* see comment at start of function */
/* set runlength */
if (runp)
*runp = run;
DPRINTF(TRANSLATE, ("\tstart %"PRIu64" -> %"PRIu64" run %d\n",
bn, *bnp, run));
/* mark not translated on virtual block number 0 */
if (*bnp == 0)
*bnp = -1;
/* return success */
free(l2vmap, M_TEMP);
return 0;
}
/* --------------------------------------------------------------------- */
static void
nilfs_read_filebuf(struct nilfs_node *node, struct buf *bp)
{
struct nilfs_device *nilfsdev = node->nilfsdev;
struct buf *nbp;
uint64_t *l2vmap, *v2pmap;
uint64_t from, blks;
uint32_t blocksize, buf_offset;
uint8_t *buf_pos;
int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
int i, error;
/*
* Translate all the block sectors into a series of buffers to read
* asynchronously from the nilfs device. Note that this lookup may
* induce readin's too.
*/
blocksize = nilfsdev->blocksize;
from = bp->b_blkno;
blks = bp->b_bcount / blocksize;
DPRINTF(READ, ("\tread in from inode %"PRIu64" blkno %"PRIu64" "
"+ %"PRIu64" blocks\n", node->ino, from, blks));
DPRINTF(READ, ("\t\tblkno %"PRIu64" "
"+ %d bytes\n", bp->b_blkno, bp->b_bcount));
/* get mapping memory */
l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
v2pmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK);
/* get virtual block numbers for the vnode's buffer span */
for (i = 0; i < blks; i++)
l2vmap[i] = from + i;
/* translate virtual block numbers to physical block numbers */
error = nilfs_nvtop(node, blks, l2vmap, v2pmap);
if (error)
goto out;
/* issue translated blocks */
bp->b_resid = bp->b_bcount;
for (i = 0; i < blks; i++) {
DPRINTF(READ, ("read_filebuf : ino %"PRIu64" blk %d -> "
"%"PRIu64" -> %"PRIu64"\n",
node->ino, i, l2vmap[i], v2pmap[i]));
buf_offset = i * blocksize;
buf_pos = (uint8_t *) bp->b_data + buf_offset;
/* note virtual block 0 marks not mapped */
if (l2vmap[i] == 0) {
memset(buf_pos, 0, blocksize);
nestiobuf_done(bp, blocksize, 0);
continue;
}
/* nest iobuf */
nbp = getiobuf(NULL, true);
nestiobuf_setup(bp, nbp, buf_offset, blocksize);
KASSERT(nbp->b_vp == node->vnode);
/* nbp is B_ASYNC */
nbp->b_lblkno = i;
nbp->b_blkno = v2pmap[i] * blk2dev; /* in DEV_BSIZE */
nbp->b_rawblkno = nbp->b_blkno;
VOP_STRATEGY(nilfsdev->devvp, nbp);
}
if ((bp->b_flags & B_ASYNC) == 0)
biowait(bp);
out:
free(l2vmap, M_TEMP);
free(v2pmap, M_TEMP);
if (error) {
bp->b_error = EIO;
biodone(bp);
}
}
static void
nilfs_write_filebuf(struct nilfs_node *node, struct buf *bp)
{
/* TODO pass on to segment collector */
panic("nilfs_strategy writing called\n");
}
int
nilfs_vfsstrategy(void *v)
{
struct vop_strategy_args /* {
struct vnode *a_vp;
struct buf *a_bp;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct buf *bp = ap->a_bp;
struct nilfs_node *node = VTOI(vp);
DPRINTF(STRATEGY, ("nilfs_strategy called\n"));
/* check if we ought to be here */
if (vp->v_type == VBLK || vp->v_type == VCHR)
panic("nilfs_strategy: spec");
/* translate if needed and pass on */
if (bp->b_flags & B_READ) {
nilfs_read_filebuf(node, bp);
return bp->b_error;
}
/* send to segment collector */
nilfs_write_filebuf(node, bp);
return bp->b_error;
}
/* --------------------------------------------------------------------- */
int
nilfs_readdir(void *v)
{
struct vop_readdir_args /* {
struct vnode *a_vp;
struct uio *a_uio;
kauth_cred_t a_cred;
int *a_eofflag;
off_t **a_cookies;
int *a_ncookies;
} */ *ap = v;
struct uio *uio = ap->a_uio;
struct vnode *vp = ap->a_vp;
struct nilfs_node *node = VTOI(vp);
struct nilfs_dir_entry *ndirent;
struct dirent dirent;
struct buf *bp;
uint64_t file_size, diroffset, transoffset, blkoff;
uint64_t blocknr;
uint32_t blocksize = node->nilfsdev->blocksize;
uint8_t *pos, name_len;
int error;
DPRINTF(READDIR, ("nilfs_readdir called\n"));
if (vp->v_type != VDIR)
return ENOTDIR;
file_size = nilfs_rw64(node->inode.i_size);
/* we are called just as long as we keep on pushing data in */
error = 0;
if ((uio->uio_offset < file_size) &&
(uio->uio_resid >= sizeof(struct dirent))) {
diroffset = uio->uio_offset;
transoffset = diroffset;
blocknr = diroffset / blocksize;
blkoff = diroffset % blocksize;
error = nilfs_bread(node, blocknr, NOCRED, 0, &bp);
if (error)
return EIO;
while (diroffset < file_size) {
DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n",
diroffset));
if (blkoff >= blocksize) {
blkoff = 0; blocknr++;
brelse(bp, BC_AGE);
error = nilfs_bread(node, blocknr, NOCRED, 0,
&bp);
if (error)
return EIO;
}
/* read in one dirent */
pos = (uint8_t *) bp->b_data + blkoff;
ndirent = (struct nilfs_dir_entry *) pos;
name_len = ndirent->name_len;
memset(&dirent, 0, sizeof(struct dirent));
dirent.d_fileno = nilfs_rw64(ndirent->inode);
dirent.d_type = ndirent->file_type; /* 1:1 ? */
dirent.d_namlen = name_len;
strncpy(dirent.d_name, ndirent->name, name_len);
dirent.d_reclen = _DIRENT_SIZE(&dirent);
DPRINTF(READDIR, ("copying `%*.*s`\n", name_len,
name_len, dirent.d_name));
/*
* If there isn't enough space in the uio to return a
* whole dirent, break off read
*/
if (uio->uio_resid < _DIRENT_SIZE(&dirent))
break;
/* transfer */
if (name_len)
uiomove(&dirent, _DIRENT_SIZE(&dirent), uio);
/* advance */
diroffset += nilfs_rw16(ndirent->rec_len);
blkoff += nilfs_rw16(ndirent->rec_len);
/* remember the last entry we transfered */
transoffset = diroffset;
}
brelse(bp, BC_AGE);
/* pass on last transfered offset */
uio->uio_offset = transoffset;
}
if (ap->a_eofflag)
*ap->a_eofflag = (uio->uio_offset >= file_size);
return error;
}
/* --------------------------------------------------------------------- */
int
nilfs_lookup(void *v)
{
struct vop_lookup_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
} */ *ap = v;
struct vnode *dvp = ap->a_dvp;
struct vnode **vpp = ap->a_vpp;
struct componentname *cnp = ap->a_cnp;
struct nilfs_node *dir_node, *res_node;
struct nilfs_mount *ump;
uint64_t ino;
const char *name;
int namelen, nameiop, islastcn, mounted_ro;
int vnodetp;
int error, found;
dir_node = VTOI(dvp);
ump = dir_node->ump;
*vpp = NULL;
DPRINTF(LOOKUP, ("nilfs_lookup called\n"));
/* simplify/clarification flags */
nameiop = cnp->cn_nameiop;
islastcn = cnp->cn_flags & ISLASTCN;
mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY;
/* check exec/dirread permissions first */
error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
if (error)
return error;
DPRINTF(LOOKUP, ("\taccess ok\n"));
/*
* If requesting a modify on the last path element on a read-only
* filingsystem, reject lookup; XXX why is this repeated in every FS ?
*/
if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
return EROFS;
DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
cnp->cn_nameptr));
/* look in the nami cache; returns 0 on success!! */
error = cache_lookup(dvp, vpp, cnp);
if (error >= 0)
return error;
DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
/*
* Obviously, the file is not (anymore) in the namecache, we have to
* search for it. There are three basic cases: '.', '..' and others.
*
* Following the guidelines of VOP_LOOKUP manpage and tmpfs.
*/
error = 0;
if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
DPRINTF(LOOKUP, ("\tlookup '.'\n"));
/* special case 1 '.' */
vref(dvp);
*vpp = dvp;
/* done */
} else if (cnp->cn_flags & ISDOTDOT) {
/* special case 2 '..' */
DPRINTF(LOOKUP, ("\tlookup '..'\n"));
/* get our node */
name = "..";
namelen = 2;
error = nilfs_lookup_name_in_dir(dvp, name, namelen,
&ino, &found);
if (error)
goto out;
if (!found)
error = ENOENT;
/* first unlock parent */
VOP_UNLOCK(dvp);
if (error == 0) {
DPRINTF(LOOKUP, ("\tfound '..'\n"));
/* try to create/reuse the node */
error = nilfs_get_node(ump, ino, &res_node);
if (!error) {
DPRINTF(LOOKUP,
("\tnode retrieved/created OK\n"));
*vpp = res_node->vnode;
}
}
/* try to relock parent */
vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
} else {
DPRINTF(LOOKUP, ("\tlookup file\n"));
/* all other files */
/* lookup filename in the directory returning its inode */
name = cnp->cn_nameptr;
namelen = cnp->cn_namelen;
error = nilfs_lookup_name_in_dir(dvp, name, namelen,
&ino, &found);
if (error)
goto out;
if (!found) {
DPRINTF(LOOKUP, ("\tNOT found\n"));
/*
* UGH, didn't find name. If we're creating or
* renaming on the last name this is OK and we ought
* to return EJUSTRETURN if its allowed to be created.
*/
error = ENOENT;
if (islastcn &&
(nameiop == CREATE || nameiop == RENAME))
error = 0;
if (!error) {
error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
if (!error) {
error = EJUSTRETURN;
}
}
/* done */
} else {
/* try to create/reuse the node */
error = nilfs_get_node(ump, ino, &res_node);
if (!error) {
/*
* If we are not at the last path component
* and found a non-directory or non-link entry
* (which may itself be pointing to a
* directory), raise an error.
*/
vnodetp = res_node->vnode->v_type;
if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
if (!islastcn)
error = ENOTDIR;
}
}
if (!error) {
*vpp = res_node->vnode;
}
}
}
out:
/*
* Store result in the cache if requested. If we are creating a file,
* the file might not be found and thus putting it into the namecache
* might be seen as negative caching.
*/
if (nameiop != CREATE)
cache_enter(dvp, *vpp, cnp);
DPRINTFIF(LOOKUP, error, ("nilfs_lookup returing error %d\n", error));
return error;
}
/* --------------------------------------------------------------------- */
static void
nilfs_ctime_to_timespec(struct timespec *ts, uint64_t ctime)
{
ts->tv_sec = ctime;
ts->tv_nsec = 0;
}
int
nilfs_getattr(void *v)
{
struct vop_getattr_args /* {
struct vnode *a_vp;
struct vattr *a_vap;
kauth_cred_t a_cred;
struct lwp *a_l;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct vattr *vap = ap->a_vap;
struct nilfs_node *node = VTOI(vp);
struct nilfs_inode *inode = &node->inode;
DPRINTF(VFSCALL, ("nilfs_getattr called\n"));
/* basic info */
vattr_null(vap);
vap->va_type = vp->v_type;
vap->va_mode = nilfs_rw16(inode->i_mode); /* XXX same? */
vap->va_nlink = nilfs_rw16(inode->i_links_count);
vap->va_uid = nilfs_rw32(inode->i_uid);
vap->va_gid = nilfs_rw32(inode->i_gid);
vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
vap->va_fileid = node->ino;
vap->va_size = nilfs_rw64(inode->i_size);
vap->va_blocksize = node->nilfsdev->blocksize;
/* times */
nilfs_ctime_to_timespec(&vap->va_atime, nilfs_rw64(inode->i_mtime));
nilfs_ctime_to_timespec(&vap->va_mtime, nilfs_rw64(inode->i_mtime));
nilfs_ctime_to_timespec(&vap->va_ctime, nilfs_rw64(inode->i_ctime));
nilfs_ctime_to_timespec(&vap->va_birthtime, nilfs_rw64(inode->i_ctime));
vap->va_gen = nilfs_rw32(inode->i_generation);
vap->va_flags = 0; /* vattr flags */
vap->va_bytes = nilfs_rw64(inode->i_blocks) * vap->va_blocksize;
vap->va_filerev = vap->va_gen; /* XXX file revision? same as gen? */
vap->va_vaflags = 0; /* XXX chflags flags */
return 0;
}
/* --------------------------------------------------------------------- */
#if 0
static int
nilfs_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
kauth_cred_t cred)
{
return EINVAL;
}
static int
nilfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
{
return EINVAL;
}
/* exported */
int
nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
{
return EINVAL;
}
static int
nilfs_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
{
return EINVAL;
}
static int
nilfs_chtimes(struct vnode *vp,
struct timespec *atime, struct timespec *mtime,
struct timespec *birthtime, int setattrflags,
kauth_cred_t cred)
{
return EINVAL;
}
#endif
int
nilfs_setattr(void *v)
{
struct vop_setattr_args /* {
struct vnode *a_vp;
struct vattr *a_vap;
kauth_cred_t a_cred;
struct lwp *a_l;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
vp = vp;
DPRINTF(VFSCALL, ("nilfs_setattr called\n"));
return EINVAL;
}
/* --------------------------------------------------------------------- */
/*
* Return POSIX pathconf information for NILFS file systems.
*/
int
nilfs_pathconf(void *v)
{
struct vop_pathconf_args /* {
struct vnode *a_vp;
int a_name;
register_t *a_retval;
} */ *ap = v;
uint32_t bits;
DPRINTF(VFSCALL, ("nilfs_pathconf called\n"));
switch (ap->a_name) {
case _PC_LINK_MAX:
*ap->a_retval = (1<<16)-1; /* 16 bits */
return 0;
case _PC_NAME_MAX:
*ap->a_retval = NILFS_MAXNAMLEN;
return 0;
case _PC_PATH_MAX:
*ap->a_retval = PATH_MAX;
return 0;
case _PC_PIPE_BUF:
*ap->a_retval = PIPE_BUF;
return 0;
case _PC_CHOWN_RESTRICTED:
*ap->a_retval = 1;
return 0;
case _PC_NO_TRUNC:
*ap->a_retval = 1;
return 0;
case _PC_SYNC_IO:
*ap->a_retval = 0; /* synchronised is off for performance */
return 0;
case _PC_FILESIZEBITS:
/* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
bits = 64; /* XXX ought to deliver 65 */
#if 0
if (nilfs_node)
bits = 64 * vp->v_mount->mnt_dev_bshift;
#endif
*ap->a_retval = bits;
return 0;
}
return EINVAL;
}
/* --------------------------------------------------------------------- */
int
nilfs_open(void *v)
{
struct vop_open_args /* {
struct vnode *a_vp;
int a_mode;
kauth_cred_t a_cred;
struct proc *a_p;
} */ *ap = v;
int flags;
DPRINTF(VFSCALL, ("nilfs_open called\n"));
/*
* Files marked append-only must be opened for appending.
*/
flags = 0;
if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
return (EPERM);
return 0;
}
/* --------------------------------------------------------------------- */
int
nilfs_close(void *v)
{
struct vop_close_args /* {
struct vnode *a_vp;
int a_fflag;
kauth_cred_t a_cred;
struct proc *a_p;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct nilfs_node *nilfs_node = VTOI(vp);
DPRINTF(VFSCALL, ("nilfs_close called\n"));
nilfs_node = nilfs_node; /* shut up gcc */
mutex_enter(vp->v_interlock);
if (vp->v_usecount > 1)
nilfs_itimes(nilfs_node, NULL, NULL, NULL);
mutex_exit(vp->v_interlock);
return 0;
}
/* --------------------------------------------------------------------- */
static int
nilfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)