nilfs_subr.c revision 1.2 1 /* $NetBSD: nilfs_subr.c,v 1.2 2009/07/28 15:31:21 reinoud 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_subr.c,v 1.2 2009/07/28 15:31:21 reinoud Exp $");
32 #endif /* not lint */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
38 #include <sys/kernel.h>
39 #include <sys/file.h> /* define FWRITE ... */
40 #include <sys/stat.h>
41 #include <sys/buf.h>
42 #include <sys/proc.h>
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 #include <sys/signalvar.h>
46 #include <sys/malloc.h>
47 #include <sys/dirent.h>
48 #include <sys/lockf.h>
49 #include <sys/kauth.h>
50 #include <sys/dirhash.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 /* basic calculators */
64 uint64_t nilfs_get_segnum_of_block(struct nilfs_device *nilfsdev,
65 uint64_t blocknr)
66 {
67 return blocknr / nilfs_rw32(nilfsdev->super.s_blocks_per_segment);
68 }
69
70
71 void
72 nilfs_get_segment_range(struct nilfs_device *nilfsdev, uint64_t segnum,
73 uint64_t *seg_start, uint64_t *seg_end)
74 {
75 uint64_t blks_per_seg;
76
77 blks_per_seg = nilfs_rw64(nilfsdev->super.s_blocks_per_segment);
78 *seg_start = blks_per_seg * segnum;
79 *seg_end = *seg_start + blks_per_seg -1;
80 if (segnum == 0)
81 *seg_start = nilfs_rw64(nilfsdev->super.s_first_data_block);
82 }
83
84
85 void nilfs_calc_mdt_consts(struct nilfs_device *nilfsdev,
86 struct nilfs_mdt *mdt, int entry_size)
87 {
88 uint32_t blocksize = nilfsdev->blocksize;
89
90 mdt->entries_per_group = blocksize * 8; /* bits in sector */
91 mdt->entries_per_block = blocksize / entry_size;
92
93 mdt->blocks_per_group =
94 (mdt->entries_per_group -1) / mdt->entries_per_block + 1 + 1;
95 mdt->groups_per_desc_block =
96 blocksize / sizeof(struct nilfs_block_group_desc);
97 mdt->blocks_per_desc_block =
98 mdt->groups_per_desc_block * mdt->blocks_per_group + 1;
99 }
100
101
102 /* from NetBSD's src/sys/net/if_ethersubr.c */
103 uint32_t
104 crc32_le(uint32_t crc, const uint8_t *buf, size_t len)
105 {
106 static const uint32_t crctab[] = {
107 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
108 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
109 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
110 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
111 };
112 size_t i;
113
114 for (i = 0; i < len; i++) {
115 crc ^= buf[i];
116 crc = (crc >> 4) ^ crctab[crc & 0xf];
117 crc = (crc >> 4) ^ crctab[crc & 0xf];
118 }
119
120 return (crc);
121 }
122
123
124 static int
125 nilfs_calchash(uint64_t ino)
126 {
127 return (int) ino;
128 }
129
130
131 /* dev reading */
132 static int
133 nilfs_dev_bread(struct nilfs_device *nilfsdev, uint64_t blocknr,
134 struct kauth_cred *cred, int flags, struct buf **bpp)
135 {
136 int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
137
138 return bread(nilfsdev->devvp, blocknr * blk2dev, nilfsdev->blocksize,
139 NOCRED, 0, bpp);
140 }
141
142
143 /* read on a node */
144 int
145 nilfs_bread(struct nilfs_node *node, uint64_t blocknr,
146 struct kauth_cred *cred, int flags, struct buf **bpp)
147 {
148 return bread(node->vnode, blocknr, node->nilfsdev->blocksize,
149 cred, flags, bpp);
150 }
151
152
153 /* segment-log reading */
154 int
155 nilfs_get_segment_log(struct nilfs_device *nilfsdev, uint64_t *blocknr,
156 uint64_t *offset, struct buf **bpp, int len, void *blob)
157 {
158 int blocksize = nilfsdev->blocksize;
159 int error;
160
161 KASSERT(len <= blocksize);
162
163 if (*offset + len > blocksize) {
164 *blocknr = *blocknr + 1;
165 *offset = 0;
166 }
167 if (*offset == 0) {
168 if (*bpp)
169 brelse(*bpp, BC_AGE);
170 /* read in block */
171 error = nilfs_dev_bread(nilfsdev, *blocknr, NOCRED, 0, bpp);
172 if (error)
173 return error;
174 }
175 memcpy(blob, ((uint8_t *) (*bpp)->b_data) + *offset, len);
176 *offset += len;
177
178 return 0;
179 }
180
181 /* -------------------------------------------------------------------------- */
182
183 /* btree operations */
184
185 static int
186 nilfs_btree_lookup_level(struct nilfs_node *node, uint64_t lblocknr,
187 uint64_t btree_vblknr, int level, uint64_t *vblocknr)
188 {
189 struct nilfs_device *nilfsdev = node->nilfsdev;
190 struct nilfs_btree_node *btree_hdr;
191 struct buf *bp;
192 uint64_t btree_blknr;
193 uint64_t *dkeys, *dptrs, child_btree_blk;
194 uint8_t *pos;
195 int i, error, selected;
196
197 DPRINTF(TRANSLATE, ("nilfs_btree_lookup_level ino %"PRIu64", "
198 "lblocknr %"PRIu64", btree_vblknr %"PRIu64", level %d\n",
199 node->ino, lblocknr, btree_vblknr, level));
200
201 /* translate btree_vblknr */
202 error = nilfs_nvtop(node, 1, &btree_vblknr, &btree_blknr);
203 if (error)
204 return error;
205
206 /* get our block */
207 error = nilfs_dev_bread(nilfsdev, btree_blknr, NOCRED, 0, &bp);
208 if (error) {
209 brelse(bp, BC_AGE);
210 return error;
211 }
212
213 btree_hdr = (struct nilfs_btree_node *) bp->b_data;
214 pos = (uint8_t *) bp->b_data +
215 sizeof(struct nilfs_btree_node) +
216 NILFS_BTREE_NODE_EXTRA_PAD_SIZE;
217 dkeys = (uint64_t *) pos;
218 dptrs = dkeys + NILFS_BTREE_NODE_NCHILDREN_MAX(nilfsdev->blocksize);
219
220 assert((btree_hdr->bn_flags & NILFS_BTREE_NODE_ROOT) == 0);
221
222 /* select matching child XXX could use binary search */
223 selected = 0;
224 for (i = 0; i < nilfs_rw16(btree_hdr->bn_nchildren); i++) {
225 if (dkeys[i] > lblocknr)
226 break;
227 selected = i;
228 }
229
230 if (level == 1) {
231 /* if found it mapped */
232 if (dkeys[selected] == lblocknr)
233 *vblocknr = dptrs[selected];
234 brelse(bp, BC_AGE);
235 return 0;
236 }
237
238 /* lookup in selected child */
239 assert(dkeys[selected] <= lblocknr);
240 child_btree_blk = dptrs[selected];
241 brelse(bp, BC_AGE);
242
243 return nilfs_btree_lookup_level(node, lblocknr,
244 child_btree_blk, btree_hdr->bn_level-1, vblocknr);
245 }
246
247
248 /* internal function */
249 static int
250 nilfs_btree_lookup(struct nilfs_node *node, uint64_t lblocknr,
251 uint64_t *vblocknr)
252 {
253 struct nilfs_inode *inode = &node->inode;
254 struct nilfs_btree_node *btree_hdr;
255 uint64_t *dkeys, *dptrs, *dtrans;
256 int i, selected;
257 int error;
258
259 DPRINTF(TRANSLATE, ("nilfs_btree_lookup ino %"PRIu64", "
260 "lblocknr %"PRIu64"\n", node->ino, lblocknr));
261
262 btree_hdr = (struct nilfs_btree_node *) &inode->i_bmap[0];
263 dkeys = &inode->i_bmap[1];
264 dptrs = dkeys + NILFS_BTREE_ROOT_NCHILDREN_MAX;
265 dtrans = &inode->i_bmap[1];
266
267 /* SMALL, direct lookup */
268 *vblocknr = 0;
269 if ((btree_hdr->bn_flags & NILFS_BMAP_LARGE) == 0) {
270 if (lblocknr < NILFS_DIRECT_NBLOCKS) {
271 *vblocknr = dtrans[lblocknr];
272 return 0;
273 }
274 /* not mapped XXX could be considered error here */
275 return 0;
276 }
277
278 /* LARGE, select matching child; XXX could use binary search */
279 dtrans = NULL;
280 error = 0;
281 selected = 0;
282 for (i = 0; i < nilfs_rw16(btree_hdr->bn_nchildren); i++) {
283 if (dkeys[i] > lblocknr)
284 break;
285 selected = i;
286 }
287
288 /* if selected key > lblocknr, its not mapped */
289 if (dkeys[selected] > lblocknr)
290 return 0;
291
292 /* overshooting? then not mapped */
293 if (selected == nilfs_rw16(btree_hdr->bn_nchildren))
294 return 0;
295
296 /* level should be > 1 or otherwise it should be a direct one */
297 assert(btree_hdr->bn_level > 1);
298
299 /* lookup in selected child */
300 assert(dkeys[selected] <= lblocknr);
301 error = nilfs_btree_lookup_level(node, lblocknr,
302 dptrs[selected], btree_hdr->bn_level-1, vblocknr);
303
304 return error;
305 }
306
307
308 /* node should be locked on entry to prevent btree changes (unlikely) */
309 int
310 nilfs_btree_nlookup(struct nilfs_node *node, uint64_t from, uint64_t blks,
311 uint64_t *l2vmap)
312 {
313 uint64_t lblocknr, *vblocknr;
314 int i, error;
315
316 /* TODO / OPTI multiple translations in one go possible */
317 error = EINVAL;
318 for (i = 0; i < blks; i++) {
319 lblocknr = from + i;
320 vblocknr = l2vmap + i;
321 error = nilfs_btree_lookup(node, lblocknr, vblocknr);
322
323 DPRINTF(TRANSLATE, ("btree_nlookup ino %"PRIu64", "
324 "lblocknr %"PRIu64" -> %"PRIu64"\n",
325 node->ino, lblocknr, *vblocknr));
326 if (error)
327 break;
328 }
329
330 return error;
331 }
332
333 /* --------------------------------------------------------------------- */
334
335 /* vtop operations */
336
337 /* translate index to a file block number and an entry */
338 static void
339 nilfs_mdt_trans(struct nilfs_mdt *mdt, uint64_t index,
340 uint64_t *blocknr, uint32_t *entry_in_block)
341 {
342 uint64_t blknr;
343 uint64_t group, group_offset, blocknr_in_group;
344 uint64_t desc_block, desc_offset;
345
346 /* calculate our offset in the file */
347 group = index / mdt->entries_per_group;
348 group_offset = index % mdt->entries_per_group;
349 desc_block = group / mdt->groups_per_desc_block;
350 desc_offset = group % mdt->groups_per_desc_block;
351 blocknr_in_group = group_offset / mdt->entries_per_block;
352
353 /* to descgroup offset */
354 blknr = 1 + desc_block * mdt->blocks_per_desc_block;
355
356 /* to group offset */
357 blknr += desc_offset * mdt->blocks_per_group;
358
359 /* to actual file block */
360 blknr += 1 + blocknr_in_group;
361
362 *blocknr = blknr;
363 *entry_in_block = group_offset % mdt->entries_per_block;
364 }
365
366
367 static int
368 nilfs_vtop(struct nilfs_device *nilfsdev, uint64_t vblocknr, uint64_t *pblocknr)
369 {
370 struct nilfs_dat_entry *entry;
371 struct buf *bp;
372 uint64_t ldatblknr;
373 uint32_t entry_in_block;
374 int error;
375
376 nilfs_mdt_trans(&nilfsdev->dat_mdt, vblocknr,
377 &ldatblknr, &entry_in_block);
378
379 error = nilfs_bread(nilfsdev->dat_node, ldatblknr, NOCRED, 0, &bp);
380 if (error) {
381 printf("vtop: can't read in DAT block %"PRIu64"!\n", ldatblknr);
382 brelse(bp, BC_AGE);
383 return error;
384 }
385
386 /* get our translation */
387 entry = ((struct nilfs_dat_entry *) bp->b_data) + entry_in_block;
388 #if 0
389 printf("\tvblk %4"PRIu64" -> %"PRIu64" for "
390 "checkpoint %"PRIu64" to %"PRIu64"\n",
391 vblocknr,
392 nilfs_rw64(entry->de_blocknr),
393 nilfs_rw64(entry->de_start),
394 nilfs_rw64(entry->de_end));
395 #endif
396
397 *pblocknr = nilfs_rw64(entry->de_blocknr);
398 brelse(bp, BC_AGE);
399
400 return 0;
401 }
402
403
404 int
405 nilfs_nvtop(struct nilfs_node *node, uint64_t blks, uint64_t *l2vmap,
406 uint64_t *v2pmap)
407 {
408 uint64_t vblocknr, *pblocknr;
409 int i, error;
410
411 /* the DAT inode is the only one not mapped virtual */
412 if (node->ino == NILFS_DAT_INO) {
413 memcpy(v2pmap, l2vmap, blks * sizeof(uint64_t));
414 return 0;
415 }
416
417 /* TODO / OPTI more translations in one go */
418 error = 0;
419 for (i = 0; i < blks; i++) {
420 vblocknr = l2vmap[i];
421 pblocknr = v2pmap + i;
422 *pblocknr = 0;
423
424 /* only translate valid vblocknrs */
425 if (vblocknr == 0)
426 continue;
427 error = nilfs_vtop(node->nilfsdev, vblocknr, pblocknr);
428 if (error)
429 break;
430 }
431
432 return error;
433 }
434
435 /* --------------------------------------------------------------------- */
436
437 struct nilfs_recover_info {
438 uint64_t segnum;
439 uint64_t pseg;
440
441 struct nilfs_segment_summary segsum;
442 struct nilfs_super_root super_root;
443 STAILQ_ENTRY(nilfs_recover_info) next;
444 };
445
446
447 /*
448 * Helper functions of nilfs_mount() that actually mounts the disc.
449 */
450 static int
451 nilfs_load_segsum(struct nilfs_device *nilfsdev,
452 struct nilfs_recover_info *ri)
453 {
454 struct buf *bp;
455 uint64_t blocknr, offset;
456 uint32_t segsum_struct_size;
457 uint32_t magic;
458 int error;
459
460 segsum_struct_size = sizeof(struct nilfs_segment_summary);
461
462 /* read in segsum structure */
463 bp = NULL;
464 blocknr = ri->pseg;
465 offset = 0;
466 error = nilfs_get_segment_log(nilfsdev,
467 &blocknr, &offset, &bp,
468 segsum_struct_size, (void *) &ri->segsum);
469 if (error)
470 goto out;
471
472 /* sanity checks */
473 magic = nilfs_rw32(ri->segsum.ss_magic);
474 if (magic != NILFS_SEGSUM_MAGIC) {
475 DPRINTF(VOLUMES, ("nilfs: bad magic in pseg %"PRIu64"\n",
476 ri->pseg));
477 error = EINVAL;
478 goto out;
479 }
480
481 /* TODO check segment summary checksum */
482 /* TODO check data checksum */
483
484 /* adjust our walking point if we have an odd size */
485 if (segsum_struct_size != nilfs_rw32(ri->segsum.ss_bytes)) {
486 printf("nilfs: WARNING encountered segsum_struct size %d in "
487 "pseg %"PRIu64"\n",
488 nilfs_rw32(ri->segsum.ss_bytes), ri->pseg);
489 /* XXX report as an error? */
490 }
491
492 out:
493 if (bp)
494 brelse(bp, BC_AGE);
495
496 return error;
497 }
498
499
500 static int
501 nilfs_load_super_root(struct nilfs_device *nilfsdev,
502 struct nilfs_recover_info *ri)
503 {
504 struct nilfs_segment_summary *segsum = &ri->segsum;
505 struct buf *bp;
506 uint64_t blocknr, offset;
507 uint32_t segsum_size, size;
508 uint32_t nsumblk, nfileblk;
509 int error;
510
511 bp = NULL;
512
513 /* process segment summary */
514 segsum_size = nilfs_rw32(segsum->ss_sumbytes);
515 nsumblk = (segsum_size - 1) / nilfsdev->blocksize + 1;
516 nfileblk = nilfs_rw32(segsum->ss_nblocks) - nsumblk;
517
518 /* check if there is a superroot */
519 if ((nilfs_rw16(segsum->ss_flags) & NILFS_SS_SR) == 0) {
520 DPRINTF(VOLUMES, ("nilfs: no super root in pseg %"PRIu64"\n",
521 ri->pseg));
522 error = ENOENT;
523 goto out;
524 }
525
526 /* get our super root, located at the end of the pseg */
527 blocknr = ri->pseg + nsumblk + nfileblk - 1;
528 offset = 0;
529 size = sizeof(struct nilfs_super_root);
530 error = nilfs_get_segment_log(nilfsdev,
531 &blocknr, &offset, &bp,
532 size, (void *) &nilfsdev->super_root);
533 if (error) {
534 printf("read in of superroot failed\n");
535 error = EIO;
536 }
537 /* else got our super root! */
538
539 out:
540 if (bp)
541 brelse(bp, BC_AGE);
542
543 return error;
544 }
545
546 /*
547 * Search for the last super root recorded.
548 */
549 void
550 nilfs_search_super_root(struct nilfs_device *nilfsdev)
551 {
552 struct nilfs_super_block *super;
553 struct nilfs_segment_summary *segsum;
554 struct nilfs_recover_info *ri, *ori, *i_ri;
555 STAILQ_HEAD(,nilfs_recover_info) ri_list;
556 uint64_t seg_start, seg_end, cno;
557 uint32_t segsum_size;
558 uint32_t nsumblk, nfileblk;
559 int error;
560
561 STAILQ_INIT(&ri_list);
562
563 /* search for last super root */
564 ri = malloc(sizeof(struct nilfs_recover_info), M_NILFSTEMP, M_WAITOK);
565 memset(ri, 0, sizeof(struct nilfs_recover_info));
566
567 /* if enabled, start from the specified position */
568 if (0) {
569 /* start from set start */
570 nilfsdev->super.s_last_pseg = nilfsdev->super.s_first_data_block;
571 nilfsdev->super.s_last_cno = nilfs_rw64(1);
572 }
573
574 ri->pseg = nilfs_rw64(nilfsdev->super.s_last_pseg); /* blknr */
575 ri->segnum = nilfs_get_segnum_of_block(nilfsdev, ri->pseg);
576
577 error = 0;
578 cno = nilfs_rw64(nilfsdev->super.s_last_cno);
579 DPRINTF(VOLUMES, ("nilfs: seach_super_root start in pseg %"PRIu64"\n",
580 ri->pseg));
581 for (;;) {
582 DPRINTF(VOLUMES, (" at pseg %"PRIu64"\n", ri->pseg));
583 error = nilfs_load_segsum(nilfsdev, ri);
584 if (error)
585 break;
586
587 segsum = &ri->segsum;
588
589 /* try to load super root */
590 if (nilfs_rw16(segsum->ss_flags) & NILFS_SS_SR) {
591 DPRINTF(VOLUMES, (" try super root\n"));
592 error = nilfs_load_super_root(nilfsdev, ri);
593 if (error)
594 break; /* confused */
595 /* wipe current list of ri */
596 while (!STAILQ_EMPTY(&ri_list)) {
597 i_ri = STAILQ_FIRST(&ri_list);
598 STAILQ_REMOVE_HEAD(&ri_list, next);
599 free(i_ri, M_NILFSTEMP);
600 }
601 super = &nilfsdev->super;
602
603 super->s_last_pseg = nilfs_rw64(ri->pseg);
604 super->s_last_cno = cno++;
605 super->s_last_seq = segsum->ss_seq;
606 super->s_state = nilfs_rw16(NILFS_VALID_FS);
607 } else {
608 STAILQ_INSERT_TAIL(&ri_list, ri, next);
609 ori = ri;
610 ri = malloc(sizeof(struct nilfs_recover_info),
611 M_NILFSTEMP, M_WAITOK);
612 memset(ri, 0, sizeof(struct nilfs_recover_info));
613 ri->segnum = ori->segnum;
614 ri->pseg = ori->pseg;
615 /* segsum keeps pointing to the `old' ri */
616 }
617
618 /* continue to the next pseg */
619 segsum_size = nilfs_rw32(segsum->ss_sumbytes);
620 nsumblk = (segsum_size - 1) / nilfsdev->blocksize + 1;
621 nfileblk = nilfs_rw32(segsum->ss_nblocks) - nsumblk;
622
623 /* calculate next partial segment location */
624 ri->pseg += nsumblk + nfileblk;
625
626 /* did we reach the end of the segment? if so, go to the next */
627 nilfs_get_segment_range(nilfsdev, ri->segnum, &seg_start, &seg_end);
628 if (ri->pseg >= seg_end)
629 ri->pseg = nilfs_rw64(segsum->ss_next);
630 ri->segnum = nilfs_get_segnum_of_block(nilfsdev, ri->pseg);
631 }
632
633 /*
634 * XXX No roll-forward yet of the remaining partial segments.
635 */
636
637 /* wipe current list of ri */
638 while (!STAILQ_EMPTY(&ri_list)) {
639 i_ri = STAILQ_FIRST(&ri_list);
640 STAILQ_REMOVE_HEAD(&ri_list, next);
641 printf("nilfs: ignoring pseg at %"PRIu64"\n", i_ri->pseg);
642 free(i_ri, M_NILFSTEMP);
643 }
644 free(ri, M_NILFSTEMP);
645 }
646
647 /* --------------------------------------------------------------------- */
648
649 /*
650 * Genfs interfacing
651 *
652 * static const struct genfs_ops nilfs_genfsops = {
653 * .gop_size = genfs_size,
654 * size of transfers
655 * .gop_alloc = nilfs_gop_alloc,
656 * allocate len bytes at offset
657 * .gop_write = genfs_gop_write,
658 * putpages interface code
659 * .gop_markupdate = nilfs_gop_markupdate,
660 * set update/modify flags etc.
661 * }
662 */
663
664 /*
665 * Callback from genfs to allocate len bytes at offset off; only called when
666 * filling up gaps in the allocation.
667 */
668 static int
669 nilfs_gop_alloc(struct vnode *vp, off_t off,
670 off_t len, int flags, kauth_cred_t cred)
671 {
672 DPRINTF(NOTIMPL, ("nilfs_gop_alloc not implemented\n"));
673 DPRINTF(ALLOC, ("nilfs_gop_alloc called for %"PRIu64" bytes\n", len));
674
675 return 0;
676 }
677
678
679 /*
680 * callback from genfs to update our flags
681 */
682 static void
683 nilfs_gop_markupdate(struct vnode *vp, int flags)
684 {
685 struct nilfs_node *nilfs_node = VTOI(vp);
686 u_long mask = 0;
687
688 if ((flags & GOP_UPDATE_ACCESSED) != 0) {
689 mask = IN_ACCESS;
690 }
691 if ((flags & GOP_UPDATE_MODIFIED) != 0) {
692 if (vp->v_type == VREG) {
693 mask |= IN_CHANGE | IN_UPDATE;
694 } else {
695 mask |= IN_MODIFY;
696 }
697 }
698 if (mask) {
699 nilfs_node->i_flags |= mask;
700 }
701 }
702
703
704 static const struct genfs_ops nilfs_genfsops = {
705 .gop_size = genfs_size,
706 .gop_alloc = nilfs_gop_alloc,
707 .gop_write = genfs_gop_write_rwmap,
708 .gop_markupdate = nilfs_gop_markupdate,
709 };
710
711 /* --------------------------------------------------------------------- */
712
713 static void
714 nilfs_register_node(struct nilfs_node *node)
715 {
716 struct nilfs_mount *ump;
717 struct nilfs_node *chk;
718 uint32_t hashline;
719
720 ump = node->ump;
721 mutex_enter(&ump->ihash_lock);
722
723 /* add to our hash table */
724 hashline = nilfs_calchash(node->ino) & NILFS_INODE_HASHMASK;
725 #ifdef DEBUG
726 LIST_FOREACH(chk, &ump->nilfs_nodes[hashline], hashchain) {
727 assert(chk);
728 if (chk->ino == node->ino)
729 panic("Double node entered\n");
730 }
731 #else
732 chk = NULL;
733 #endif
734 LIST_INSERT_HEAD(&ump->nilfs_nodes[hashline], node, hashchain);
735
736 mutex_exit(&ump->ihash_lock);
737 }
738
739
740 static void
741 nilfs_deregister_node(struct nilfs_node *node)
742 {
743 struct nilfs_mount *ump;
744
745 ump = node->ump;
746 mutex_enter(&ump->ihash_lock);
747
748 /* remove from hash list */
749 LIST_REMOVE(node, hashchain);
750
751 mutex_exit(&ump->ihash_lock);
752 }
753
754
755 static struct nilfs_node *
756 nilfs_hash_lookup(struct nilfs_mount *ump, ino_t ino)
757 {
758 struct nilfs_node *node;
759 struct vnode *vp;
760 uint32_t hashline;
761
762 loop:
763 mutex_enter(&ump->ihash_lock);
764
765 /* search our hash table */
766 hashline = nilfs_calchash(ino) & NILFS_INODE_HASHMASK;
767 LIST_FOREACH(node, &ump->nilfs_nodes[hashline], hashchain) {
768 assert(node);
769 if (node->ino == ino) {
770 vp = node->vnode;
771 assert(vp);
772 mutex_enter(&vp->v_interlock);
773 mutex_exit(&ump->ihash_lock);
774 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
775 goto loop;
776 return node;
777 }
778 }
779 mutex_exit(&ump->ihash_lock);
780
781 return NULL;
782 }
783
784
785 /* node action implementators */
786 extern int (**nilfs_vnodeop_p)(void *);
787
788 int
789 nilfs_get_node_raw(struct nilfs_device *nilfsdev, struct nilfs_mount *ump,
790 uint64_t ino, struct nilfs_inode *inode, struct nilfs_node **nodep)
791 {
792 struct nilfs_node *node;
793 struct vnode *nvp;
794 struct mount *mp;
795 int (**vnodeops)(void *);
796 int error;
797
798 *nodep = NULL;
799 vnodeops = nilfs_vnodeop_p;
800
801 /* associate with mountpoint if present*/
802 mp = ump? ump->vfs_mountp : NULL;
803 error = getnewvnode(VT_NILFS, mp, vnodeops, &nvp);
804 if (error)
805 return error;
806
807 /* lock node */
808 error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
809 if (error) {
810 nvp->v_data = NULL;
811 ungetnewvnode(nvp);
812 return error;
813 }
814
815 node = pool_get(&nilfs_node_pool, PR_WAITOK);
816 memset(node, 0, sizeof(struct nilfs_node));
817
818 /* crosslink */
819 node->vnode = nvp;
820 node->ump = ump;
821 node->nilfsdev = nilfsdev;
822 nvp->v_data = node;
823
824 /* initiase nilfs node */
825 node->ino = ino;
826 node->inode = *inode;
827 node->lockf = NULL;
828
829 /* needed? */
830 mutex_init(&node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
831 cv_init(&node->node_lock, "nilfs_nlk");
832
833 /* initialise genfs */
834 genfs_node_init(nvp, &nilfs_genfsops);
835
836 /* check if we're fetching the root */
837 if (ino == NILFS_ROOT_INO)
838 nvp->v_vflag |= VV_ROOT;
839
840 /* update vnode's file type XXX is there a function for this? */
841 nvp->v_type = VREG;
842 if (S_ISDIR(inode->i_mode))
843 nvp->v_type = VDIR;
844 if (S_ISLNK(inode->i_mode))
845 nvp->v_type = VLNK;
846 #if 0
847 if (S_ISCHR(inode->i_mode))
848 nvp->v_type = VCHR;
849 if (S_ISBLK(inode->i_mode))
850 nvp->v_type = VBLK;
851 #endif
852 /* XXX what else? */
853
854 /* fixup inode size for system nodes */
855 if ((ino < NILFS_USER_INO) && (ino != NILFS_ROOT_INO)) {
856 DPRINTF(VOLUMES, ("NEED TO GET my size for inode %"PRIu64"\n",
857 ino));
858 /* for now set it to maximum, -1 is illegal */
859 inode->i_size = nilfs_rw64(((uint64_t) -2));
860 }
861
862 uvm_vnp_setsize(nvp, nilfs_rw64(inode->i_size));
863
864 if (ump)
865 nilfs_register_node(node);
866
867 /* return node */
868 *nodep = node;
869 return 0;
870 }
871
872
873 int
874 nilfs_get_node(struct nilfs_mount *ump, uint64_t ino, struct nilfs_node **nodep)
875 {
876 struct nilfs_device *nilfsdev;
877 struct nilfs_inode inode, *entry;
878 struct buf *bp;
879 uint64_t ivblocknr;
880 uint32_t entry_in_block;
881 int error;
882
883 /* lookup node in hash table */
884 *nodep = nilfs_hash_lookup(ump, ino);
885 if (*nodep)
886 return 0;
887
888 /* lock to disallow simultanious creation of same udf_node */
889 mutex_enter(&ump->get_node_lock);
890
891 /* relookup since it could be created while waiting for the mutex */
892 *nodep = nilfs_hash_lookup(ump, ino);
893 if (*nodep) {
894 mutex_exit(&ump->get_node_lock);
895 return 0;
896 }
897
898 /* create new inode; XXX check could be handier */
899 if ((ino < NILFS_ATIME_INO) && (ino != NILFS_ROOT_INO)) {
900 printf("nilfs_get_node: system ino %"PRIu64" not in mount "
901 "point!\n", ino);
902 mutex_exit(&ump->get_node_lock);
903 return ENOENT;
904 }
905
906 /* lookup inode in the ifile */
907 DPRINTF(NODE, ("lookup ino %"PRIu64"\n", ino));
908
909 /* lookup inode structure in mountpoints ifile */
910 nilfsdev = ump->nilfsdev;
911 nilfs_mdt_trans(&nilfsdev->ifile_mdt, ino, &ivblocknr, &entry_in_block);
912
913 error = nilfs_bread(ump->ifile_node, ivblocknr, NOCRED, 0, &bp);
914 if (error) {
915 mutex_exit(&ump->get_node_lock);
916 return ENOENT;
917 }
918
919 /* get inode entry */
920 entry = (struct nilfs_inode *) bp->b_data + entry_in_block;
921 inode = *entry;
922 brelse(bp, BC_AGE);
923
924 /* get node */
925 error = nilfs_get_node_raw(ump->nilfsdev, ump, ino, &inode, nodep);
926 mutex_exit(&ump->get_node_lock);
927
928 return error;
929 }
930
931
932 void
933 nilfs_dispose_node(struct nilfs_node **nodep)
934 {
935 struct vnode *vp;
936 struct nilfs_node *node;
937
938 /* protect against rogue values */
939 if (!*nodep)
940 return;
941
942 node = *nodep;
943 vp = node->vnode;
944
945 /* remove dirhash if present */
946 dirhash_purge(&node->dir_hash);
947
948 /* remove from our hash lookup table */
949 if (node->ump)
950 nilfs_deregister_node(node);
951
952 /* destroy our locks */
953 mutex_destroy(&node->node_mutex);
954 cv_destroy(&node->node_lock);
955
956 /* dissociate from our vnode */
957 genfs_node_destroy(node->vnode);
958 vp->v_data = NULL;
959
960 /* free our associated memory */
961 pool_put(&nilfs_node_pool, node);
962
963 *nodep = NULL;
964 }
965
966
967 void
968 nilfs_itimes(struct nilfs_node *node, struct timespec *acc,
969 struct timespec *mod, struct timespec *birth)
970 {
971 }
972
973
974 int
975 nilfs_update(struct vnode *node, struct timespec *acc,
976 struct timespec *mod, struct timespec *birth, int updflags)
977 {
978 return EROFS;
979 }
980
981
982 int
983 nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
984 {
985 return EROFS;
986 }
987
988
989
990 int
991 nilfs_grow_node(struct nilfs_node *node, uint64_t new_size)
992 {
993 return EROFS;
994 }
995
996
997 int
998 nilfs_shrink_node(struct nilfs_node *node, uint64_t new_size)
999 {
1000 return EROFS;
1001 }
1002
1003
1004 static int
1005 dirhash_fill(struct nilfs_node *dir_node)
1006 {
1007 struct vnode *dvp = dir_node->vnode;
1008 struct dirhash *dirh;
1009 struct nilfs_dir_entry *ndirent;
1010 struct dirent dirent;
1011 struct buf *bp;
1012 uint64_t file_size, diroffset, blkoff;
1013 uint64_t blocknr;
1014 uint32_t blocksize = dir_node->nilfsdev->blocksize;
1015 uint8_t *pos, name_len;
1016 int error;
1017
1018 DPRINTF(CALL, ("dirhash_fill called\n"));
1019
1020 if (dvp->v_type != VDIR)
1021 return ENOTDIR;
1022
1023 /* make sure we have a dirhash to work on */
1024 dirh = dir_node->dir_hash;
1025 KASSERT(dirh);
1026 KASSERT(dirh->refcnt > 0);
1027
1028 if (dirh->flags & DIRH_BROKEN)
1029 return EIO;
1030
1031 if (dirh->flags & DIRH_COMPLETE)
1032 return 0;
1033
1034 DPRINTF(DIRHASH, ("Filling directory hash\n"));
1035
1036 /* make sure we have a clean dirhash to add to */
1037 dirhash_purge_entries(dirh);
1038
1039 /* get directory filesize */
1040 file_size = nilfs_rw64(dir_node->inode.i_size);
1041
1042 /* walk the directory */
1043 error = 0;
1044 diroffset = 0;
1045
1046 blocknr = diroffset / blocksize;
1047 blkoff = diroffset % blocksize;
1048 error = nilfs_bread(dir_node, blocknr, NOCRED, 0, &bp);
1049 if (error) {
1050 dirh->flags |= DIRH_BROKEN;
1051 dirhash_purge_entries(dirh);
1052 return EIO;
1053 }
1054 while (diroffset < file_size) {
1055 DPRINTF(READDIR, ("filldir : offset = %"PRIu64"\n",
1056 diroffset));
1057 if (blkoff >= blocksize) {
1058 blkoff = 0; blocknr++;
1059 brelse(bp, BC_AGE);
1060 error = nilfs_bread(dir_node, blocknr, NOCRED, 0,
1061 &bp);
1062 if (error) {
1063 dirh->flags |= DIRH_BROKEN;
1064 dirhash_purge_entries(dirh);
1065 return EIO;
1066 }
1067 }
1068
1069 /* read in one dirent */
1070 pos = (uint8_t *) bp->b_data + blkoff;
1071 ndirent = (struct nilfs_dir_entry *) pos;
1072 name_len = ndirent->name_len;
1073
1074 memset(&dirent, 0, sizeof(struct dirent));
1075 dirent.d_fileno = nilfs_rw64(ndirent->inode);
1076 dirent.d_type = ndirent->file_type; /* 1:1 ? */
1077 dirent.d_namlen = name_len;
1078 strncpy(dirent.d_name, ndirent->name, name_len);
1079 dirent.d_reclen = _DIRENT_SIZE(&dirent);
1080 DPRINTF(DIRHASH, ("copying `%*.*s`\n", name_len,
1081 name_len, dirent.d_name));
1082
1083 /* XXX is it deleted? extra free space? */
1084 dirhash_enter(dirh, &dirent, diroffset,
1085 nilfs_rw16(ndirent->rec_len), 0);
1086
1087 /* advance */
1088 diroffset += nilfs_rw16(ndirent->rec_len);
1089 blkoff += nilfs_rw16(ndirent->rec_len);
1090 }
1091 brelse(bp, BC_AGE);
1092
1093 dirh->flags |= DIRH_COMPLETE;
1094
1095 return 0;
1096 }
1097
1098
1099 int
1100 nilfs_lookup_name_in_dir(struct vnode *dvp, const char *name, int namelen,
1101 uint64_t *ino, int *found)
1102 {
1103 struct nilfs_node *dir_node = VTOI(dvp);
1104 struct nilfs_dir_entry *ndirent;
1105 struct dirhash *dirh;
1106 struct dirhash_entry *dirh_ep;
1107 struct buf *bp;
1108 uint64_t diroffset, blkoff;
1109 uint64_t blocknr;
1110 uint32_t blocksize = dir_node->nilfsdev->blocksize;
1111 uint8_t *pos;
1112 int hit, error;
1113
1114 /* set default return */
1115 *found = 0;
1116
1117 /* get our dirhash and make sure its read in */
1118 dirhash_get(&dir_node->dir_hash);
1119 error = dirhash_fill(dir_node);
1120 if (error) {
1121 dirhash_put(dir_node->dir_hash);
1122 return error;
1123 }
1124 dirh = dir_node->dir_hash;
1125
1126 /* allocate temporary space for fid */
1127
1128 DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
1129 namelen, namelen, name));
1130
1131 /* search our dirhash hits */
1132 *ino = 0;
1133 dirh_ep = NULL;
1134 for (;;) {
1135 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
1136 /* if no hit, abort the search */
1137 if (!hit)
1138 break;
1139
1140 /* check this hit */
1141 diroffset = dirh_ep->offset;
1142
1143 blocknr = diroffset / blocksize;
1144 blkoff = diroffset % blocksize;
1145 error = nilfs_bread(dir_node, blocknr, NOCRED, 0, &bp);
1146 if (error)
1147 return EIO;
1148
1149 /* read in one dirent */
1150 pos = (uint8_t *) bp->b_data + blkoff;
1151 ndirent = (struct nilfs_dir_entry *) pos;
1152
1153 DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
1154 ndirent->name_len, ndirent->name_len, ndirent->name));
1155
1156 /* see if its our entry */
1157 KASSERT(ndirent->name_len == namelen);
1158 if (strncmp(ndirent->name, name, namelen) == 0) {
1159 *found = 1;
1160 *ino = nilfs_rw64(ndirent->inode);
1161 brelse(bp, BC_AGE);
1162 break;
1163 }
1164 brelse(bp, BC_AGE);
1165 }
1166
1167 dirhash_put(dir_node->dir_hash);
1168
1169 return error;
1170 }
1171
1172
1173 int
1174 nilfs_dir_detach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *node, struct componentname *cnp)
1175 {
1176 return EROFS;
1177 }
1178
1179
1180 int
1181 nilfs_dir_attach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *node, struct vattr *vap, struct componentname *cnp)
1182 {
1183 return EROFS;
1184 }
1185
1186
1187 /* XXX return vnode? */
1188 int
1189 nilfs_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, struct componentname *cnp)
1190 {
1191 return EROFS;
1192 }
1193
1194
1195 void
1196 nilfs_delete_node(struct nilfs_node *node)
1197 {
1198 }
1199
1200
1201