1 /* $NetBSD: nilfs_subr.c,v 1.16 2021/12/05 07:47:40 msaitoh 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.16 2021/12/05 07:47:40 msaitoh 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 /* forwards */
64 static int nilfs_btree_lookup(struct nilfs_node *node, uint64_t lblocknr,
65 uint64_t *vblocknr);
66
67 /* basic calculators */
nilfs_get_segnum_of_block(struct nilfs_device * nilfsdev,uint64_t blocknr)68 uint64_t nilfs_get_segnum_of_block(struct nilfs_device *nilfsdev,
69 uint64_t blocknr)
70 {
71 return blocknr / nilfs_rw32(nilfsdev->super.s_blocks_per_segment);
72 }
73
74
75 void
nilfs_get_segment_range(struct nilfs_device * nilfsdev,uint64_t segnum,uint64_t * seg_start,uint64_t * seg_end)76 nilfs_get_segment_range(struct nilfs_device *nilfsdev, uint64_t segnum,
77 uint64_t *seg_start, uint64_t *seg_end)
78 {
79 uint64_t blks_per_seg;
80
81 blks_per_seg = nilfs_rw64(nilfsdev->super.s_blocks_per_segment);
82 *seg_start = blks_per_seg * segnum;
83 *seg_end = *seg_start + blks_per_seg -1;
84 if (segnum == 0)
85 *seg_start = nilfs_rw64(nilfsdev->super.s_first_data_block);
86 }
87
88
nilfs_calc_mdt_consts(struct nilfs_device * nilfsdev,struct nilfs_mdt * mdt,int entry_size)89 void nilfs_calc_mdt_consts(struct nilfs_device *nilfsdev,
90 struct nilfs_mdt *mdt, int entry_size)
91 {
92 uint32_t blocksize = nilfsdev->blocksize;
93
94 mdt->entries_per_group = blocksize * 8; /* bits in sector */
95 mdt->entries_per_block = blocksize / entry_size;
96
97 mdt->blocks_per_group =
98 (mdt->entries_per_group -1) / mdt->entries_per_block + 1 + 1;
99 mdt->groups_per_desc_block =
100 blocksize / sizeof(struct nilfs_block_group_desc);
101 mdt->blocks_per_desc_block =
102 mdt->groups_per_desc_block * mdt->blocks_per_group + 1;
103 }
104
105
106 /* from NetBSD's src/sys/net/if_ethersubr.c */
107 uint32_t
crc32_le(uint32_t crc,const uint8_t * buf,size_t len)108 crc32_le(uint32_t crc, const uint8_t *buf, size_t len)
109 {
110 static const uint32_t crctab[] = {
111 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
112 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
113 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
114 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
115 };
116 size_t i;
117
118 for (i = 0; i < len; i++) {
119 crc ^= buf[i];
120 crc = (crc >> 4) ^ crctab[crc & 0xf];
121 crc = (crc >> 4) ^ crctab[crc & 0xf];
122 }
123
124 return (crc);
125 }
126
127
128 /* dev reading */
129 static int
nilfs_dev_bread(struct nilfs_device * nilfsdev,uint64_t blocknr,int flags,struct buf ** bpp)130 nilfs_dev_bread(struct nilfs_device *nilfsdev, uint64_t blocknr,
131 int flags, struct buf **bpp)
132 {
133 int blk2dev = nilfsdev->blocksize / DEV_BSIZE;
134
135 return bread(nilfsdev->devvp, blocknr * blk2dev, nilfsdev->blocksize,
136 0, bpp);
137 }
138
139
140 /* read on a node */
141 int
nilfs_bread(struct nilfs_node * node,uint64_t blocknr,int flags,struct buf ** bpp)142 nilfs_bread(struct nilfs_node *node, uint64_t blocknr,
143 int flags, struct buf **bpp)
144 {
145 struct nilfs_device *nilfsdev = node->nilfsdev;
146 uint64_t vblocknr, pblockno;
147 int error;
148
149 error = nilfs_btree_lookup(node, blocknr, &vblocknr);
150 if (error)
151 return error;
152
153 /* Read special files through devvp as they have no vnode attached. */
154 if (node->ino < NILFS_USER_INO && node->ino != NILFS_ROOT_INO) {
155 error = nilfs_nvtop(node, 1, &vblocknr, &pblockno);
156 if (error)
157 return error;
158 return nilfs_dev_bread(nilfsdev, pblockno, flags, bpp);
159 }
160
161 return bread(node->vnode, vblocknr, node->nilfsdev->blocksize,
162 flags, bpp);
163 }
164
165
166 /* segment-log reading */
167 int
nilfs_get_segment_log(struct nilfs_device * nilfsdev,uint64_t * blocknr,uint64_t * offset,struct buf ** bpp,int len,void * blob)168 nilfs_get_segment_log(struct nilfs_device *nilfsdev, uint64_t *blocknr,
169 uint64_t *offset, struct buf **bpp, int len, void *blob)
170 {
171 int blocksize = nilfsdev->blocksize;
172 int error;
173
174 KASSERT(len <= blocksize);
175
176 if (*offset + len > blocksize) {
177 *blocknr = *blocknr + 1;
178 *offset = 0;
179 }
180 if (*offset == 0) {
181 if (*bpp)
182 brelse(*bpp, BC_AGE);
183 /* read in block */
184 error = nilfs_dev_bread(nilfsdev, *blocknr, 0, bpp);
185 if (error)
186 return error;
187 }
188 memcpy(blob, ((uint8_t *) (*bpp)->b_data) + *offset, len);
189 *offset += len;
190
191 return 0;
192 }
193
194 /* -------------------------------------------------------------------------- */
195
196 /* btree operations */
197
198 static int
nilfs_btree_lookup_level(struct nilfs_node * node,uint64_t lblocknr,uint64_t btree_vblknr,int level,uint64_t * vblocknr)199 nilfs_btree_lookup_level(struct nilfs_node *node, uint64_t lblocknr,
200 uint64_t btree_vblknr, int level, uint64_t *vblocknr)
201 {
202 struct nilfs_device *nilfsdev = node->nilfsdev;
203 struct nilfs_btree_node *btree_hdr;
204 struct buf *bp;
205 uint64_t btree_blknr;
206 uint64_t *dkeys, *dptrs, child_btree_blk;
207 uint8_t *pos;
208 int i, error, selected;
209
210 DPRINTF(TRANSLATE, ("nilfs_btree_lookup_level ino %"PRIu64", "
211 "lblocknr %"PRIu64", btree_vblknr %"PRIu64", level %d\n",
212 node->ino, lblocknr, btree_vblknr, level));
213
214 /* translate btree_vblknr */
215 error = nilfs_nvtop(node, 1, &btree_vblknr, &btree_blknr);
216 if (error)
217 return error;
218
219 /* get our block */
220 error = nilfs_dev_bread(nilfsdev, btree_blknr, 0, &bp);
221 if (error) {
222 return error;
223 }
224
225 btree_hdr = (struct nilfs_btree_node *) bp->b_data;
226 pos = (uint8_t *) bp->b_data +
227 sizeof(struct nilfs_btree_node) +
228 NILFS_BTREE_NODE_EXTRA_PAD_SIZE;
229 dkeys = (uint64_t *) pos;
230 dptrs = dkeys + NILFS_BTREE_NODE_NCHILDREN_MAX(nilfsdev->blocksize);
231
232 assert((btree_hdr->bn_flags & NILFS_BTREE_NODE_ROOT) == 0);
233 assert((btree_hdr->bn_level == level));
234
235 /* select matching child XXX could use binary search */
236 selected = 0;
237 for (i = 0; i < nilfs_rw16(btree_hdr->bn_nchildren); i++) {
238 if (dkeys[i] > lblocknr)
239 break;
240 selected = i;
241 }
242
243 if (level == 1) {
244 /* if found it mapped */
245 if (dkeys[selected] == lblocknr)
246 *vblocknr = dptrs[selected];
247 brelse(bp, BC_AGE);
248 return 0;
249 }
250
251 /* lookup in selected child */
252 assert(dkeys[selected] <= lblocknr);
253 child_btree_blk = dptrs[selected];
254 brelse(bp, BC_AGE);
255
256 return nilfs_btree_lookup_level(node, lblocknr,
257 child_btree_blk, level-1, vblocknr);
258 }
259
260
261 /* internal function */
262 static int
nilfs_btree_lookup(struct nilfs_node * node,uint64_t lblocknr,uint64_t * vblocknr)263 nilfs_btree_lookup(struct nilfs_node *node, uint64_t lblocknr,
264 uint64_t *vblocknr)
265 {
266 struct nilfs_inode *inode = &node->inode;
267 struct nilfs_btree_node *btree_hdr;
268 uint64_t *dkeys, *dptrs, *dtrans;
269 int i, selected;
270 int error;
271
272 DPRINTF(TRANSLATE, ("nilfs_btree_lookup ino %"PRIu64", "
273 "lblocknr %"PRIu64"\n", node->ino, lblocknr));
274
275 btree_hdr = (struct nilfs_btree_node *) &inode->i_bmap[0];
276 dkeys = &inode->i_bmap[1];
277 dptrs = dkeys + NILFS_BTREE_ROOT_NCHILDREN_MAX;
278 dtrans = &inode->i_bmap[1];
279
280 /* SMALL, direct lookup */
281 *vblocknr = 0;
282 if ((btree_hdr->bn_flags & NILFS_BMAP_LARGE) == 0) {
283 if (lblocknr < NILFS_DIRECT_NBLOCKS) {
284 *vblocknr = dtrans[lblocknr];
285 return 0;
286 }
287 /* not mapped XXX could be considered error here */
288 return 0;
289 }
290
291 /* LARGE, select matching child; XXX could use binary search */
292 dtrans = NULL;
293 error = 0;
294 selected = 0;
295 for (i = 0; i < nilfs_rw16(btree_hdr->bn_nchildren); i++) {
296 if (dkeys[i] > lblocknr)
297 break;
298 selected = i;
299 }
300
301 /* if selected key > lblocknr, its not mapped */
302 if (dkeys[selected] > lblocknr)
303 return 0;
304
305 /* overshooting? then not mapped */
306 if (selected == nilfs_rw16(btree_hdr->bn_nchildren))
307 return 0;
308
309 /* level should be > 1 or otherwise it should be a direct one */
310 assert(btree_hdr->bn_level > 1);
311
312 /* lookup in selected child */
313 assert(dkeys[selected] <= lblocknr);
314 error = nilfs_btree_lookup_level(node, lblocknr,
315 dptrs[selected], btree_hdr->bn_level-1, vblocknr);
316
317 return error;
318 }
319
320
321 /* node should be locked on entry to prevent btree changes (unlikely) */
322 int
nilfs_btree_nlookup(struct nilfs_node * node,uint64_t from,uint64_t blks,uint64_t * l2vmap)323 nilfs_btree_nlookup(struct nilfs_node *node, uint64_t from, uint64_t blks,
324 uint64_t *l2vmap)
325 {
326 uint64_t lblocknr, *vblocknr;
327 int i, error;
328
329 /* TODO / OPTI multiple translations in one go possible */
330 error = EINVAL;
331 for (i = 0; i < blks; i++) {
332 lblocknr = from + i;
333 vblocknr = l2vmap + i;
334 error = nilfs_btree_lookup(node, lblocknr, vblocknr);
335
336 DPRINTF(TRANSLATE, ("btree_nlookup ino %"PRIu64", "
337 "lblocknr %"PRIu64" -> %"PRIu64"\n",
338 node->ino, lblocknr, *vblocknr));
339 if (error)
340 break;
341 }
342
343 return error;
344 }
345
346 /* --------------------------------------------------------------------- */
347
348 /* vtop operations */
349
350 /* translate index to a file block number and an entry */
351 void
nilfs_mdt_trans(struct nilfs_mdt * mdt,uint64_t index,uint64_t * blocknr,uint32_t * entry_in_block)352 nilfs_mdt_trans(struct nilfs_mdt *mdt, uint64_t index,
353 uint64_t *blocknr, uint32_t *entry_in_block)
354 {
355 uint64_t blknr;
356 uint64_t group, group_offset, blocknr_in_group;
357 uint64_t desc_block, desc_offset;
358
359 /* calculate our offset in the file */
360 group = index / mdt->entries_per_group;
361 group_offset = index % mdt->entries_per_group;
362 desc_block = group / mdt->groups_per_desc_block;
363 desc_offset = group % mdt->groups_per_desc_block;
364 blocknr_in_group = group_offset / mdt->entries_per_block;
365
366 /* to descgroup offset */
367 blknr = 1 + desc_block * mdt->blocks_per_desc_block;
368
369 /* to group offset */
370 blknr += desc_offset * mdt->blocks_per_group;
371
372 /* to actual file block */
373 blknr += 1 + blocknr_in_group;
374
375 *blocknr = blknr;
376 *entry_in_block = group_offset % mdt->entries_per_block;
377 }
378
379
380 static int
nilfs_vtop(struct nilfs_device * nilfsdev,uint64_t vblocknr,uint64_t * pblocknr)381 nilfs_vtop(struct nilfs_device *nilfsdev, uint64_t vblocknr, uint64_t *pblocknr)
382 {
383 struct nilfs_dat_entry *entry;
384 struct buf *bp;
385 uint64_t ldatblknr;
386 uint32_t entry_in_block;
387 int error;
388
389 nilfs_mdt_trans(&nilfsdev->dat_mdt, vblocknr,
390 &ldatblknr, &entry_in_block);
391
392 error = nilfs_bread(nilfsdev->dat_node, ldatblknr, 0, &bp);
393 if (error) {
394 printf("vtop: can't read in DAT block %"PRIu64"!\n", ldatblknr);
395 return error;
396 }
397
398 /* get our translation */
399 entry = ((struct nilfs_dat_entry *) bp->b_data) + entry_in_block;
400 #if 0
401 printf("\tvblk %4"PRIu64" -> %"PRIu64" for "
402 "checkpoint %"PRIu64" to %"PRIu64"\n",
403 vblocknr,
404 nilfs_rw64(entry->de_blocknr),
405 nilfs_rw64(entry->de_start),
406 nilfs_rw64(entry->de_end));
407 #endif
408
409 *pblocknr = nilfs_rw64(entry->de_blocknr);
410 brelse(bp, BC_AGE);
411
412 return 0;
413 }
414
415
416 int
nilfs_nvtop(struct nilfs_node * node,uint64_t blks,uint64_t * l2vmap,uint64_t * v2pmap)417 nilfs_nvtop(struct nilfs_node *node, uint64_t blks, uint64_t *l2vmap,
418 uint64_t *v2pmap)
419 {
420 uint64_t vblocknr, *pblocknr;
421 int i, error;
422
423 /* the DAT inode is the only one not mapped virtual */
424 if (node->ino == NILFS_DAT_INO) {
425 memcpy(v2pmap, l2vmap, blks * sizeof(uint64_t));
426 return 0;
427 }
428
429 /* TODO / OPTI more translations in one go */
430 error = 0;
431 for (i = 0; i < blks; i++) {
432 vblocknr = l2vmap[i];
433 pblocknr = v2pmap + i;
434 *pblocknr = 0;
435
436 /* only translate valid vblocknrs */
437 if (vblocknr == 0)
438 continue;
439 error = nilfs_vtop(node->nilfsdev, vblocknr, pblocknr);
440 if (error)
441 break;
442 }
443
444 return error;
445 }
446
447 /* --------------------------------------------------------------------- */
448
449 struct nilfs_recover_info {
450 uint64_t segnum;
451 uint64_t pseg;
452
453 struct nilfs_segment_summary segsum;
454 struct nilfs_super_root super_root;
455 STAILQ_ENTRY(nilfs_recover_info) next;
456 };
457
458
459 /*
460 * Helper functions of nilfs_mount() that actually mounts the disc.
461 */
462 static int
nilfs_load_segsum(struct nilfs_device * nilfsdev,struct nilfs_recover_info * ri)463 nilfs_load_segsum(struct nilfs_device *nilfsdev,
464 struct nilfs_recover_info *ri)
465 {
466 struct buf *bp;
467 uint64_t blocknr, offset;
468 uint32_t segsum_struct_size;
469 uint32_t magic;
470 int error;
471
472 segsum_struct_size = sizeof(struct nilfs_segment_summary);
473
474 /* read in segsum structure */
475 bp = NULL;
476 blocknr = ri->pseg;
477 offset = 0;
478 error = nilfs_get_segment_log(nilfsdev,
479 &blocknr, &offset, &bp,
480 segsum_struct_size, (void *) &ri->segsum);
481 if (error)
482 goto out;
483
484 /* sanity checks */
485 magic = nilfs_rw32(ri->segsum.ss_magic);
486 if (magic != NILFS_SEGSUM_MAGIC) {
487 DPRINTF(VOLUMES, ("nilfs: bad magic in pseg %"PRIu64"\n",
488 ri->pseg));
489 error = EINVAL;
490 goto out;
491 }
492
493 /* TODO check segment summary checksum */
494 /* TODO check data checksum */
495
496 out:
497 if (bp)
498 brelse(bp, BC_AGE);
499
500 return error;
501 }
502
503
504 static int
nilfs_load_super_root(struct nilfs_device * nilfsdev,struct nilfs_recover_info * ri)505 nilfs_load_super_root(struct nilfs_device *nilfsdev,
506 struct nilfs_recover_info *ri)
507 {
508 struct nilfs_segment_summary *segsum = &ri->segsum;
509 struct nilfs_super_root *super_root;
510 struct buf *bp;
511 uint64_t blocknr, offset;
512 uint32_t segsum_size, size;
513 uint32_t nsumblk, nfileblk;
514 uint32_t super_root_crc, comp_crc;
515 int off, error;
516
517 /* process segment summary */
518 segsum_size = nilfs_rw32(segsum->ss_sumbytes);
519 nsumblk = (segsum_size - 1) / nilfsdev->blocksize + 1;
520 nfileblk = nilfs_rw32(segsum->ss_nblocks) - nsumblk;
521
522 /* check if there is a superroot */
523 if ((nilfs_rw16(segsum->ss_flags) & NILFS_SS_SR) == 0) {
524 DPRINTF(VOLUMES, ("nilfs: no super root in pseg %"PRIu64"\n",
525 ri->pseg));
526 return ENOENT;
527 }
528
529 /* get our super root, located at the end of the pseg */
530 blocknr = ri->pseg + nsumblk + nfileblk - 1;
531 offset = 0;
532 size = sizeof(struct nilfs_super_root);
533 bp = NULL;
534 error = nilfs_get_segment_log(nilfsdev,
535 &blocknr, &offset, &bp,
536 size, (void *) &nilfsdev->super_root);
537 if (bp)
538 brelse(bp, BC_AGE);
539 if (error) {
540 printf("read in of superroot failed\n");
541 return EIO;
542 }
543
544 /* check super root crc */
545 super_root = &nilfsdev->super_root;
546 super_root_crc = nilfs_rw32(super_root->sr_sum);
547 off = sizeof(super_root->sr_sum);
548 comp_crc = crc32_le(nilfs_rw32(nilfsdev->super.s_crc_seed),
549 (uint8_t *) super_root + off,
550 NILFS_SR_BYTES - off);
551 if (super_root_crc != comp_crc) {
552 DPRINTF(VOLUMES, (" invalid superroot, likely from old format\n"));
553 return EINVAL;
554 }
555
556 DPRINTF(VOLUMES, (" got valid superroot\n"));
557
558 return 0;
559 }
560
561 /*
562 * Search for the last super root recorded.
563 */
564 void
nilfs_search_super_root(struct nilfs_device * nilfsdev)565 nilfs_search_super_root(struct nilfs_device *nilfsdev)
566 {
567 struct nilfs_super_block *super;
568 struct nilfs_segment_summary *segsum;
569 struct nilfs_recover_info *ri, *ori, *i_ri;
570 STAILQ_HEAD(,nilfs_recover_info) ri_list;
571 uint64_t seg_start, seg_end, cno;
572 uint32_t segsum_size;
573 uint32_t nsumblk, nfileblk;
574 int error;
575
576 STAILQ_INIT(&ri_list);
577
578 /* search for last super root */
579 ri = malloc(sizeof(struct nilfs_recover_info), M_NILFSTEMP, M_WAITOK);
580 memset(ri, 0, sizeof(struct nilfs_recover_info));
581
582 /* if enabled, start from the specified position */
583 if (0) {
584 /* start from set start */
585 nilfsdev->super.s_last_pseg = nilfsdev->super.s_first_data_block;
586 nilfsdev->super.s_last_cno = nilfs_rw64(1);
587 }
588
589 ri->pseg = nilfs_rw64(nilfsdev->super.s_last_pseg); /* blknr */
590 ri->segnum = nilfs_get_segnum_of_block(nilfsdev, ri->pseg);
591
592 error = 0;
593 cno = nilfs_rw64(nilfsdev->super.s_last_cno);
594 DPRINTF(VOLUMES, ("nilfs: search_super_root start in pseg %"PRIu64"\n",
595 ri->pseg));
596 for (;;) {
597 DPRINTF(VOLUMES, (" at pseg %"PRIu64"\n", ri->pseg));
598 error = nilfs_load_segsum(nilfsdev, ri);
599 if (error)
600 break;
601
602 segsum = &ri->segsum;
603
604 /* try to load super root */
605 if (nilfs_rw16(segsum->ss_flags) & NILFS_SS_SR) {
606 DPRINTF(VOLUMES, (" try super root\n"));
607 error = nilfs_load_super_root(nilfsdev, ri);
608 if (error)
609 break; /* confused */
610 /* wipe current list of ri */
611 while (!STAILQ_EMPTY(&ri_list)) {
612 i_ri = STAILQ_FIRST(&ri_list);
613 STAILQ_REMOVE_HEAD(&ri_list, next);
614 free(i_ri, M_NILFSTEMP);
615 }
616 super = &nilfsdev->super;
617
618 super->s_last_pseg = nilfs_rw64(ri->pseg);
619 super->s_last_cno = cno++;
620 super->s_last_seq = segsum->ss_seq;
621 super->s_state = nilfs_rw16(NILFS_VALID_FS);
622 } else {
623 STAILQ_INSERT_TAIL(&ri_list, ri, next);
624 ori = ri;
625 ri = malloc(sizeof(struct nilfs_recover_info),
626 M_NILFSTEMP, M_WAITOK);
627 memset(ri, 0, sizeof(struct nilfs_recover_info));
628 ri->segnum = ori->segnum;
629 ri->pseg = ori->pseg;
630 /* segsum keeps pointing to the `old' ri */
631 }
632
633 /* continue to the next pseg */
634 segsum_size = nilfs_rw32(segsum->ss_sumbytes);
635 nsumblk = (segsum_size - 1) / nilfsdev->blocksize + 1;
636 nfileblk = nilfs_rw32(segsum->ss_nblocks) - nsumblk;
637
638 /* calculate next partial segment location */
639 ri->pseg += nsumblk + nfileblk;
640
641 /* did we reach the end of the segment? if so, go to the next */
642 nilfs_get_segment_range(nilfsdev, ri->segnum, &seg_start, &seg_end);
643 if (ri->pseg >= seg_end)
644 ri->pseg = nilfs_rw64(segsum->ss_next);
645 ri->segnum = nilfs_get_segnum_of_block(nilfsdev, ri->pseg);
646 }
647
648 /*
649 * XXX No roll-forward yet of the remaining partial segments.
650 */
651
652 /* wipe current list of ri */
653 while (!STAILQ_EMPTY(&ri_list)) {
654 i_ri = STAILQ_FIRST(&ri_list);
655 STAILQ_REMOVE_HEAD(&ri_list, next);
656 printf("nilfs: ignoring pseg at %"PRIu64"\n", i_ri->pseg);
657 free(i_ri, M_NILFSTEMP);
658 }
659 free(ri, M_NILFSTEMP);
660 }
661
662 /* --------------------------------------------------------------------- */
663
664 int
nilfs_get_node_raw(struct nilfs_device * nilfsdev,struct nilfs_mount * ump,uint64_t ino,struct nilfs_inode * inode,struct nilfs_node ** nodep)665 nilfs_get_node_raw(struct nilfs_device *nilfsdev, struct nilfs_mount *ump,
666 uint64_t ino, struct nilfs_inode *inode, struct nilfs_node **nodep)
667 {
668 struct nilfs_node *node;
669
670 *nodep = NULL;
671
672 node = pool_get(&nilfs_node_pool, PR_WAITOK);
673 memset(node, 0, sizeof(struct nilfs_node));
674
675 /* crosslink */
676 node->ump = ump;
677 node->nilfsdev = nilfsdev;
678
679 /* initiase nilfs node */
680 node->ino = ino;
681 node->inode = *inode;
682 node->lockf = NULL;
683
684 /* initialise locks */
685 mutex_init(&node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
686 cv_init(&node->node_lock, "nilfsnlk");
687
688 /* fixup inode size for system nodes */
689 if ((ino < NILFS_USER_INO) && (ino != NILFS_ROOT_INO)) {
690 DPRINTF(VOLUMES, ("NEED TO GET my size for inode %"PRIu64"?\n",
691 ino));
692 /* for now set it to maximum, -1 is illegal */
693 DPRINTF(VOLUMES, (" current size of inode is %"PRIu64"\n", inode->i_size));
694 inode->i_size = nilfs_rw64(((uint64_t) -2));
695 }
696
697 /* return node */
698 *nodep = node;
699 return 0;
700 }
701
702 void
nilfs_dispose_node(struct nilfs_node ** nodep)703 nilfs_dispose_node(struct nilfs_node **nodep)
704 {
705 struct nilfs_node *node;
706
707 /* protect against rogue values */
708 if (!*nodep)
709 return;
710
711 node = *nodep;
712
713 /* remove dirhash if present */
714 dirhash_purge(&node->dir_hash);
715
716 /* destroy our locks */
717 mutex_destroy(&node->node_mutex);
718 cv_destroy(&node->node_lock);
719
720 /* free our associated memory */
721 pool_put(&nilfs_node_pool, node);
722
723 *nodep = NULL;
724 }
725
726
727 void
nilfs_itimes(struct nilfs_node * node,struct timespec * acc,struct timespec * mod,struct timespec * birth)728 nilfs_itimes(struct nilfs_node *node, struct timespec *acc,
729 struct timespec *mod, struct timespec *birth)
730 {
731 }
732
733
734 int
nilfs_update(struct vnode * node,struct timespec * acc,struct timespec * mod,struct timespec * birth,int updflags)735 nilfs_update(struct vnode *node, struct timespec *acc,
736 struct timespec *mod, struct timespec *birth, int updflags)
737 {
738 return EROFS;
739 }
740
741
742 int
nilfs_chsize(struct vnode * vp,u_quad_t newsize,kauth_cred_t cred)743 nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
744 {
745 return EROFS;
746 }
747
748
749
750 int
nilfs_grow_node(struct nilfs_node * node,uint64_t new_size)751 nilfs_grow_node(struct nilfs_node *node, uint64_t new_size)
752 {
753 return EROFS;
754 }
755
756
757 int
nilfs_shrink_node(struct nilfs_node * node,uint64_t new_size)758 nilfs_shrink_node(struct nilfs_node *node, uint64_t new_size)
759 {
760 return EROFS;
761 }
762
763
764 static int
dirhash_fill(struct nilfs_node * dir_node)765 dirhash_fill(struct nilfs_node *dir_node)
766 {
767 struct vnode *dvp = dir_node->vnode;
768 struct dirhash *dirh;
769 struct nilfs_dir_entry *ndirent;
770 struct dirent dirent;
771 struct buf *bp;
772 uint64_t file_size, diroffset, blkoff;
773 uint64_t blocknr;
774 uint32_t blocksize = dir_node->nilfsdev->blocksize;
775 uint8_t *pos, name_len;
776 int error;
777
778 DPRINTF(CALL, ("dirhash_fill called\n"));
779
780 if (dvp->v_type != VDIR)
781 return ENOTDIR;
782
783 /* make sure we have a dirhash to work on */
784 dirh = dir_node->dir_hash;
785 KASSERT(dirh);
786 KASSERT(dirh->refcnt > 0);
787
788 if (dirh->flags & DIRH_BROKEN)
789 return EIO;
790
791 if (dirh->flags & DIRH_COMPLETE)
792 return 0;
793
794 DPRINTF(DIRHASH, ("Filling directory hash\n"));
795
796 /* make sure we have a clean dirhash to add to */
797 dirhash_purge_entries(dirh);
798
799 /* get directory filesize */
800 file_size = nilfs_rw64(dir_node->inode.i_size);
801
802 /* walk the directory */
803 error = 0;
804 diroffset = 0;
805
806 blocknr = diroffset / blocksize;
807 blkoff = diroffset % blocksize;
808 error = nilfs_bread(dir_node, blocknr, 0, &bp);
809 if (error) {
810 dirh->flags |= DIRH_BROKEN;
811 dirhash_purge_entries(dirh);
812 return EIO;
813 }
814 while (diroffset < file_size) {
815 DPRINTF(READDIR, ("filldir : offset = %"PRIu64"\n",
816 diroffset));
817 if (blkoff >= blocksize) {
818 blkoff = 0; blocknr++;
819 brelse(bp, BC_AGE);
820 error = nilfs_bread(dir_node, blocknr, 0, &bp);
821 if (error) {
822 dirh->flags |= DIRH_BROKEN;
823 dirhash_purge_entries(dirh);
824 return EIO;
825 }
826 }
827
828 /* read in one dirent */
829 pos = (uint8_t *) bp->b_data + blkoff;
830 ndirent = (struct nilfs_dir_entry *) pos;
831 name_len = ndirent->name_len;
832
833 memset(&dirent, 0, sizeof(struct dirent));
834 dirent.d_fileno = nilfs_rw64(ndirent->inode);
835 dirent.d_type = ndirent->file_type; /* 1:1 ? */
836 dirent.d_namlen = name_len;
837 strncpy(dirent.d_name, ndirent->name, name_len);
838 dirent.d_reclen = _DIRENT_SIZE(&dirent);
839 DPRINTF(DIRHASH, ("copying `%*.*s`\n", name_len,
840 name_len, dirent.d_name));
841
842 /* XXX is it deleted? extra free space? */
843 dirhash_enter(dirh, &dirent, diroffset,
844 nilfs_rw16(ndirent->rec_len), 0);
845
846 /* advance */
847 diroffset += nilfs_rw16(ndirent->rec_len);
848 blkoff += nilfs_rw16(ndirent->rec_len);
849 }
850 brelse(bp, BC_AGE);
851
852 dirh->flags |= DIRH_COMPLETE;
853
854 return 0;
855 }
856
857
858 int
nilfs_lookup_name_in_dir(struct vnode * dvp,const char * name,int namelen,uint64_t * ino,int * found)859 nilfs_lookup_name_in_dir(struct vnode *dvp, const char *name, int namelen,
860 uint64_t *ino, int *found)
861 {
862 struct nilfs_node *dir_node = VTOI(dvp);
863 struct nilfs_dir_entry *ndirent;
864 struct dirhash *dirh;
865 struct dirhash_entry *dirh_ep;
866 struct buf *bp;
867 uint64_t diroffset, blkoff;
868 uint64_t blocknr;
869 uint32_t blocksize = dir_node->nilfsdev->blocksize;
870 uint8_t *pos;
871 int hit, error;
872
873 /* set default return */
874 *found = 0;
875
876 /* get our dirhash and make sure its read in */
877 dirhash_get(&dir_node->dir_hash);
878 error = dirhash_fill(dir_node);
879 if (error) {
880 dirhash_put(dir_node->dir_hash);
881 return error;
882 }
883 dirh = dir_node->dir_hash;
884
885 /* allocate temporary space for fid */
886
887 DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
888 namelen, namelen, name));
889
890 /* search our dirhash hits */
891 *ino = 0;
892 dirh_ep = NULL;
893 for (;;) {
894 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
895 /* if no hit, abort the search */
896 if (!hit)
897 break;
898
899 /* check this hit */
900 diroffset = dirh_ep->offset;
901
902 blocknr = diroffset / blocksize;
903 blkoff = diroffset % blocksize;
904 error = nilfs_bread(dir_node, blocknr, 0, &bp);
905 if (error)
906 return EIO;
907
908 /* read in one dirent */
909 pos = (uint8_t *) bp->b_data + blkoff;
910 ndirent = (struct nilfs_dir_entry *) pos;
911
912 DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
913 ndirent->name_len, ndirent->name_len, ndirent->name));
914
915 /* see if its our entry */
916 KASSERT(ndirent->name_len == namelen);
917 if (strncmp(ndirent->name, name, namelen) == 0) {
918 *found = 1;
919 *ino = nilfs_rw64(ndirent->inode);
920 brelse(bp, BC_AGE);
921 break;
922 }
923 brelse(bp, BC_AGE);
924 }
925
926 dirhash_put(dir_node->dir_hash);
927
928 return error;
929 }
930
931
932 int
nilfs_dir_detach(struct nilfs_mount * ump,struct nilfs_node * dir_node,struct nilfs_node * node,struct componentname * cnp)933 nilfs_dir_detach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *node, struct componentname *cnp)
934 {
935 return EROFS;
936 }
937
938
939 int
nilfs_dir_attach(struct nilfs_mount * ump,struct nilfs_node * dir_node,struct nilfs_node * node,struct vattr * vap,struct componentname * cnp)940 nilfs_dir_attach(struct nilfs_mount *ump, struct nilfs_node *dir_node, struct nilfs_node *node, struct vattr *vap, struct componentname *cnp)
941 {
942 return EROFS;
943 }
944
945
946 /* XXX return vnode? */
947 int
nilfs_create_node(struct vnode * dvp,struct vnode ** vpp,struct vattr * vap,struct componentname * cnp)948 nilfs_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, struct componentname *cnp)
949 {
950 return EROFS;
951 }
952
953
954 void
nilfs_delete_node(struct nilfs_node * node)955 nilfs_delete_node(struct nilfs_node *node)
956 {
957 }
958
959
960