2
* nilfs2.c - New Implementation of Log filesystem
4
* Written by Jiro SEKIBA <jir@unicus.jp>
6
* Copyright (C) 2003,2004,2005,2007,2008,2010 Free Software Foundation, Inc.
8
* GRUB is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
13
* GRUB is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
23
/* Filetype information as used in inodes. */
24
#define FILETYPE_INO_MASK 0170000
25
#define FILETYPE_INO_REG 0100000
26
#define FILETYPE_INO_DIRECTORY 0040000
27
#define FILETYPE_INO_SYMLINK 0120000
30
#include <grub/file.h>
32
#include <grub/misc.h>
33
#include <grub/disk.h>
35
#include <grub/types.h>
36
#include <grub/fshelp.h>
38
#define NILFS_INODE_BMAP_SIZE 7
40
#define NILFS_SUPORT_REV 2
42
/* Magic value used to identify an nilfs2 filesystem. */
43
#define NILFS2_SUPER_MAGIC 0x3434
44
/* nilfs btree node flag. */
45
#define NILFS_BTREE_NODE_ROOT 0x01
47
/* nilfs btree node level. */
48
#define NILFS_BTREE_LEVEL_DATA 0
49
#define NILFS_BTREE_LEVEL_NODE_MIN (NILFS_BTREE_LEVEL_DATA + 1)
50
#define NILFS_BTREE_LEVEL_MAX 14
52
/* nilfs 1st super block posission from beginning of the partition
54
#define NILFS_1ST_SUPER_BLOCK 2
55
/* nilfs 2nd super block posission from beginning of the partition
57
#define NILFS_2ND_SUPER_BLOCK(devsize) (((devsize >> 3) - 1) << 3)
59
struct grub_nilfs2_inode
61
grub_uint64_t i_blocks;
63
grub_uint64_t i_ctime;
64
grub_uint64_t i_mtime;
65
grub_uint32_t i_ctime_nsec;
66
grub_uint32_t i_mtime_nsec;
70
grub_uint16_t i_links_count;
71
grub_uint32_t i_flags;
72
grub_uint64_t i_bmap[NILFS_INODE_BMAP_SIZE];
73
#define i_device_code i_bmap[0]
74
grub_uint64_t i_xattr;
75
grub_uint32_t i_generation;
79
struct grub_nilfs2_super_root
82
grub_uint16_t sr_bytes;
83
grub_uint16_t sr_flags;
84
grub_uint64_t sr_nongc_ctime;
85
struct grub_nilfs2_inode sr_dat;
86
struct grub_nilfs2_inode sr_cpfile;
87
struct grub_nilfs2_inode sr_sufile;
90
struct grub_nilfs2_super_block
92
grub_uint32_t s_rev_level;
93
grub_uint16_t s_minor_rev_level;
94
grub_uint16_t s_magic;
95
grub_uint16_t s_bytes;
96
grub_uint16_t s_flags;
97
grub_uint32_t s_crc_seed;
99
grub_uint32_t s_log_block_size;
100
grub_uint64_t s_nsegments;
101
grub_uint64_t s_dev_size;
102
grub_uint64_t s_first_data_block;
103
grub_uint32_t s_blocks_per_segment;
104
grub_uint32_t s_r_segments_percentage;
105
grub_uint64_t s_last_cno;
106
grub_uint64_t s_last_pseg;
107
grub_uint64_t s_last_seq;
108
grub_uint64_t s_free_blocks_count;
109
grub_uint64_t s_ctime;
110
grub_uint64_t s_mtime;
111
grub_uint64_t s_wtime;
112
grub_uint16_t s_mnt_count;
113
grub_uint16_t s_max_mnt_count;
114
grub_uint16_t s_state;
115
grub_uint16_t s_errors;
116
grub_uint64_t s_lastcheck;
117
grub_uint32_t s_checkinterval;
118
grub_uint32_t s_creator_os;
119
grub_uint16_t s_def_resuid;
120
grub_uint16_t s_def_resgid;
121
grub_uint32_t s_first_ino;
122
grub_uint16_t s_inode_size;
123
grub_uint16_t s_dat_entry_size;
124
grub_uint16_t s_checkpoint_size;
125
grub_uint16_t s_segment_usage_size;
126
grub_uint8_t s_uuid[16];
127
char s_volume_name[16];
128
char s_last_mounted[64];
129
grub_uint32_t s_c_interval;
130
grub_uint32_t s_c_block_max;
131
grub_uint32_t s_reserved[192];
134
struct grub_nilfs2_dir_entry
137
grub_uint16_t rec_len;
138
grub_uint8_t name_len;
139
grub_uint8_t file_type;
140
#if 0 /* followed by file name. */
141
char name[NILFS_NAME_LEN];
144
} __attribute__ ((packed));
159
struct grub_nilfs2_finfo
161
grub_uint64_t fi_ino;
162
grub_uint64_t fi_cno;
163
grub_uint32_t fi_nblocks;
164
grub_uint32_t fi_ndatablk;
167
struct grub_nilfs2_binfo_v
169
grub_uint64_t bi_vblocknr;
170
grub_uint64_t bi_blkoff;
173
struct grub_nilfs2_binfo_dat
175
grub_uint64_t bi_blkoff;
176
grub_uint8_t bi_level;
177
grub_uint8_t bi_pad[7];
180
union grub_nilfs2_binfo
182
struct grub_nilfs2_binfo_v bi_v;
183
struct grub_nilfs2_binfo_dat bi_dat;
186
struct grub_nilfs2_segment_summary
188
grub_uint32_t ss_datasum;
189
grub_uint32_t ss_sumsum;
190
grub_uint32_t ss_magic;
191
grub_uint16_t ss_bytes;
192
grub_uint16_t ss_flags;
193
grub_uint64_t ss_seq;
194
grub_uint64_t ss_create;
195
grub_uint64_t ss_next;
196
grub_uint32_t ss_nblocks;
197
grub_uint32_t ss_nfinfo;
198
grub_uint32_t ss_sumbytes;
199
grub_uint32_t ss_pad;
202
struct grub_nilfs2_btree_node
204
grub_uint8_t bn_flags;
205
grub_uint8_t bn_level;
206
grub_uint16_t bn_nchildren;
207
grub_uint32_t bn_pad;
210
struct grub_nilfs2_palloc_group_desc
212
grub_uint32_t pg_nfrees;
215
struct grub_nilfs2_dat_entry
217
grub_uint64_t de_blocknr;
218
grub_uint64_t de_start;
219
grub_uint64_t de_end;
220
grub_uint64_t de_rsv;
223
struct grub_nilfs2_snapshot_list
225
grub_uint64_t ssl_next;
226
grub_uint64_t ssl_prev;
229
struct grub_nilfs2_cpfile_header
231
grub_uint64_t ch_ncheckpoints;
232
grub_uint64_t ch_nsnapshots;
233
struct grub_nilfs2_snapshot_list ch_snapshot_list;
236
struct grub_nilfs2_checkpoint
238
grub_uint32_t cp_flags;
239
grub_uint32_t cp_checkpoints_count;
240
struct grub_nilfs2_snapshot_list cp_snapshot_list;
241
grub_uint64_t cp_cno;
242
grub_uint64_t cp_create;
243
grub_uint64_t cp_nblk_inc;
244
grub_uint64_t cp_inodes_count;
245
grub_uint64_t cp_blocks_count;
246
struct grub_nilfs2_inode cp_ifile_inode;
250
#define NILFS_BMAP_LARGE 0x1
251
#define NILFS_BMAP_SIZE (NILFS_INODE_BMAP_SIZE * sizeof(grub_uint64_t))
253
/* nilfs extra padding for nonroot btree node. */
254
#define NILFS_BTREE_NODE_EXTRA_PAD_SIZE (sizeof(grub_uint64_t))
255
#define NILFS_BTREE_ROOT_SIZE NILFS_BMAP_SIZE
256
#define NILFS_BTREE_ROOT_NCHILDREN_MAX \
257
((NILFS_BTREE_ROOT_SIZE - sizeof(struct nilfs_btree_node)) / \
258
(sizeof(grub_uint64_t) + sizeof(grub_uint64_t)) )
261
struct grub_fshelp_node
263
struct grub_nilfs2_data *data;
264
struct grub_nilfs2_inode inode;
269
struct grub_nilfs2_data
271
struct grub_nilfs2_super_block sblock;
272
struct grub_nilfs2_super_root sroot;
273
struct grub_nilfs2_inode ifile;
275
struct grub_nilfs2_inode *inode;
276
struct grub_fshelp_node diropen;
279
/* Log2 size of nilfs2 block in 512 blocks. */
280
#define LOG2_NILFS2_BLOCK_SIZE(data) \
281
(grub_le_to_cpu32 (data->sblock.s_log_block_size) + 1)
283
/* Log2 size of nilfs2 block in bytes. */
284
#define LOG2_BLOCK_SIZE(data) \
285
(grub_le_to_cpu32 (data->sblock.s_log_block_size) + 10)
287
/* The size of an nilfs2 block in bytes. */
288
#define NILFS2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE (data))
291
grub_nilfs2_dat_translate (struct grub_nilfs2_data *data, grub_uint64_t key);
292
static grub_dl_t my_mod;
296
static inline unsigned long
297
grub_nilfs2_palloc_entries_per_group (struct grub_nilfs2_data *data)
299
return 1UL << (LOG2_BLOCK_SIZE (data) + 3);
302
static inline grub_uint64_t
303
grub_nilfs2_palloc_group (struct grub_nilfs2_data *data,
304
grub_uint64_t nr, grub_uint32_t * offset)
306
return grub_divmod64 (nr, grub_nilfs2_palloc_entries_per_group (data),
310
static inline grub_uint32_t
311
grub_nilfs2_palloc_groups_per_desc_block (struct grub_nilfs2_data *data)
313
return NILFS2_BLOCK_SIZE (data) /
314
sizeof (struct grub_nilfs2_palloc_group_desc);
317
static inline grub_uint32_t
318
grub_nilfs2_entries_per_block (struct grub_nilfs2_data *data,
319
unsigned long entry_size)
321
return NILFS2_BLOCK_SIZE (data) / entry_size;
325
static inline grub_uint32_t
326
grub_nilfs2_blocks_per_group (struct grub_nilfs2_data *data,
327
unsigned long entry_size)
329
return grub_div_roundup (grub_nilfs2_palloc_entries_per_group (data),
330
grub_nilfs2_entries_per_block (data,
334
static inline grub_uint32_t
335
grub_nilfs2_blocks_per_desc_block (struct grub_nilfs2_data *data,
336
unsigned long entry_size)
338
return grub_nilfs2_palloc_groups_per_desc_block (data) *
339
grub_nilfs2_blocks_per_group (data, entry_size) + 1;
342
static inline grub_uint32_t
343
grub_nilfs2_palloc_desc_block_offset (struct grub_nilfs2_data *data,
345
unsigned long entry_size)
347
grub_uint32_t desc_block =
348
group / grub_nilfs2_palloc_groups_per_desc_block (data);
349
return desc_block * grub_nilfs2_blocks_per_desc_block (data, entry_size);
352
static inline grub_uint32_t
353
grub_nilfs2_palloc_bitmap_block_offset (struct grub_nilfs2_data *data,
355
unsigned long entry_size)
357
unsigned long desc_offset = group %
358
grub_nilfs2_palloc_groups_per_desc_block (data);
360
return grub_nilfs2_palloc_desc_block_offset (data, group, entry_size) + 1 +
361
desc_offset * grub_nilfs2_blocks_per_group (data, entry_size);
364
static inline grub_uint32_t
365
grub_nilfs2_palloc_entry_offset (struct grub_nilfs2_data *data,
366
grub_uint64_t nr, unsigned long entry_size)
369
grub_uint32_t group_offset;
371
group = grub_nilfs2_palloc_group (data, nr, &group_offset);
373
return grub_nilfs2_palloc_bitmap_block_offset (data, group,
375
group_offset / grub_nilfs2_entries_per_block (data, entry_size);
379
static inline struct grub_nilfs2_btree_node *
380
grub_nilfs2_btree_get_root (struct grub_nilfs2_inode *inode)
382
return (struct grub_nilfs2_btree_node *) &inode->i_bmap[0];
386
grub_nilfs2_btree_get_level (struct grub_nilfs2_btree_node *node)
388
return node->bn_level;
391
static inline grub_uint64_t *
392
grub_nilfs2_btree_node_dkeys (struct grub_nilfs2_btree_node *node)
394
return (grub_uint64_t *) ((char *) (node + 1) +
395
((node->bn_flags & NILFS_BTREE_NODE_ROOT) ?
396
0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
399
static inline grub_uint64_t
400
grub_nilfs2_btree_node_get_key (struct grub_nilfs2_btree_node *node,
403
return grub_le_to_cpu64 (*(grub_nilfs2_btree_node_dkeys (node) + index));
407
grub_nilfs2_btree_node_lookup (struct grub_nilfs2_btree_node *node,
408
grub_uint64_t key, int *indexp)
411
int index, low, high, s;
414
high = grub_le_to_cpu16 (node->bn_nchildren) - 1;
419
index = (low + high) / 2;
420
nkey = grub_nilfs2_btree_node_get_key (node, index);
438
if (node->bn_level > NILFS_BTREE_LEVEL_NODE_MIN)
440
if (s > 0 && index > 0)
451
grub_nilfs2_btree_node_nchildren_max (struct grub_nilfs2_data *data,
452
struct grub_nilfs2_btree_node *node)
454
int node_children_max = ((NILFS2_BLOCK_SIZE (data) -
455
sizeof (struct grub_nilfs2_btree_node) -
456
NILFS_BTREE_NODE_EXTRA_PAD_SIZE) /
457
(sizeof (grub_uint64_t) + sizeof (grub_uint64_t)));
459
return (node->bn_flags & NILFS_BTREE_NODE_ROOT) ? 3 : node_children_max;
462
static inline grub_uint64_t *
463
grub_nilfs2_btree_node_dptrs (struct grub_nilfs2_data *data,
464
struct grub_nilfs2_btree_node *node)
466
return (grub_uint64_t *) (grub_nilfs2_btree_node_dkeys (node) +
467
grub_nilfs2_btree_node_nchildren_max (data,
471
static inline grub_uint64_t
472
grub_nilfs2_btree_node_get_ptr (struct grub_nilfs2_data *data,
473
struct grub_nilfs2_btree_node *node,
477
grub_le_to_cpu64 (*(grub_nilfs2_btree_node_dptrs (data, node) + index));
481
grub_nilfs2_btree_get_nonroot_node (struct grub_nilfs2_data *data,
482
grub_uint64_t ptr, void *block)
484
grub_disk_t disk = data->disk;
485
unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
487
return grub_disk_read (disk, ptr * nilfs2_block_count, 0,
488
NILFS2_BLOCK_SIZE (data), block);
492
grub_nilfs2_btree_lookup (struct grub_nilfs2_data *data,
493
struct grub_nilfs2_inode *inode,
494
grub_uint64_t key, int need_translate)
496
struct grub_nilfs2_btree_node *node;
497
unsigned char block[NILFS2_BLOCK_SIZE (data)];
499
int level, found, index;
501
node = grub_nilfs2_btree_get_root (inode);
502
level = grub_nilfs2_btree_get_level (node);
504
found = grub_nilfs2_btree_node_lookup (node, key, &index);
505
ptr = grub_nilfs2_btree_node_get_ptr (data, node, index);
507
ptr = grub_nilfs2_dat_translate (data, ptr);
509
for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--)
511
grub_nilfs2_btree_get_nonroot_node (data, ptr, block);
516
node = (struct grub_nilfs2_btree_node *) block;
518
if (node->bn_level != level)
520
grub_error (GRUB_ERR_BAD_FS, "btree level mismatch\n");
525
found = grub_nilfs2_btree_node_lookup (node, key, &index);
529
if (index < grub_nilfs2_btree_node_nchildren_max (data, node))
531
ptr = grub_nilfs2_btree_node_get_ptr (data, node, index);
533
ptr = grub_nilfs2_dat_translate (data, ptr);
537
grub_error (GRUB_ERR_BAD_FS, "btree corruption\n");
548
static inline grub_uint64_t
549
grub_nilfs2_direct_lookup (struct grub_nilfs2_inode *inode, grub_uint64_t key)
551
return grub_le_to_cpu64 (inode->i_bmap[1 + key]);
554
static inline grub_uint64_t
555
grub_nilfs2_bmap_lookup (struct grub_nilfs2_data *data,
556
struct grub_nilfs2_inode *inode,
557
grub_uint64_t key, int need_translate)
559
struct grub_nilfs2_btree_node *root = grub_nilfs2_btree_get_root (inode);
560
if (root->bn_flags & NILFS_BMAP_LARGE)
561
return grub_nilfs2_btree_lookup (data, inode, key, need_translate);
565
ptr = grub_nilfs2_direct_lookup (inode, key);
567
ptr = grub_nilfs2_dat_translate (data, ptr);
573
grub_nilfs2_dat_translate (struct grub_nilfs2_data *data, grub_uint64_t key)
575
struct grub_nilfs2_dat_entry entry;
576
grub_disk_t disk = data->disk;
578
grub_uint32_t blockno, offset;
579
unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
581
blockno = grub_nilfs2_palloc_entry_offset (data, key,
583
grub_nilfs2_dat_entry));
585
grub_divmod64 (key * sizeof (struct grub_nilfs2_dat_entry),
586
NILFS2_BLOCK_SIZE (data), &offset);
588
pptr = grub_nilfs2_bmap_lookup (data, &data->sroot.sr_dat, blockno, 0);
589
if (pptr == (grub_uint64_t) - 1)
591
grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
595
grub_disk_read (disk, pptr * nilfs2_block_count, offset,
596
sizeof (struct grub_nilfs2_dat_entry), &entry);
598
return grub_le_to_cpu64 (entry.de_blocknr);
602
static grub_disk_addr_t
603
grub_nilfs2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
605
struct grub_nilfs2_data *data = node->data;
606
struct grub_nilfs2_inode *inode = &node->inode;
607
grub_uint64_t pptr = -1;
609
pptr = grub_nilfs2_bmap_lookup (data, inode, fileblock, 1);
610
if (pptr == (grub_uint64_t) - 1)
612
grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
619
/* Read LEN bytes from the file described by DATA starting with byte
620
POS. Return the amount of read bytes in READ. */
622
grub_nilfs2_read_file (grub_fshelp_node_t node,
623
void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t
627
int pos, grub_size_t len, char *buf)
629
return grub_fshelp_read_file (node->data->disk, node, read_hook,
630
pos, len, buf, grub_nilfs2_read_block,
631
grub_le_to_cpu64 (node->inode.i_size),
632
LOG2_NILFS2_BLOCK_SIZE (node->data));
637
grub_nilfs2_read_checkpoint (struct grub_nilfs2_data *data,
639
struct grub_nilfs2_checkpoint *cpp)
641
grub_uint64_t blockno;
642
grub_uint32_t offset;
644
grub_disk_t disk = data->disk;
645
unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
647
/* Assume sizeof(struct grub_nilfs2_cpfile_header) <
648
sizeof(struct grub_nilfs2_checkpoint).
650
blockno = grub_divmod64 (cpno, NILFS2_BLOCK_SIZE (data) /
651
sizeof (struct grub_nilfs2_checkpoint), &offset);
653
pptr = grub_nilfs2_bmap_lookup (data, &data->sroot.sr_cpfile, blockno, 1);
654
if (pptr == (grub_uint64_t) - 1)
656
return grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
659
return grub_disk_read (disk, pptr * nilfs2_block_count,
660
offset * sizeof (struct grub_nilfs2_checkpoint),
661
sizeof (struct grub_nilfs2_checkpoint), cpp);
664
static inline grub_err_t
665
grub_nilfs2_read_last_checkpoint (struct grub_nilfs2_data *data,
666
struct grub_nilfs2_checkpoint *cpp)
668
return grub_nilfs2_read_checkpoint (data,
669
grub_le_to_cpu64 (data->
674
/* Read the inode INO for the file described by DATA into INODE. */
676
grub_nilfs2_read_inode (struct grub_nilfs2_data *data,
677
grub_uint64_t ino, struct grub_nilfs2_inode *inodep)
679
grub_uint64_t blockno;
682
grub_disk_t disk = data->disk;
683
unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
685
blockno = grub_nilfs2_palloc_entry_offset (data, ino,
689
grub_divmod64 (sizeof (struct grub_nilfs2_inode) * ino,
690
NILFS2_BLOCK_SIZE (data), &offset);
691
pptr = grub_nilfs2_bmap_lookup (data, &data->ifile, blockno, 1);
692
if (pptr == (grub_uint64_t) - 1)
694
return grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
697
return grub_disk_read (disk, pptr * nilfs2_block_count, offset,
698
sizeof (struct grub_nilfs2_inode), inodep);
702
grub_nilfs2_valid_sb (struct grub_nilfs2_super_block *sbp)
704
if (grub_le_to_cpu16 (sbp->s_magic) != NILFS2_SUPER_MAGIC)
707
if (grub_le_to_cpu32 (sbp->s_rev_level) != NILFS_SUPORT_REV)
714
grub_nilfs2_load_sb (struct grub_nilfs2_data *data)
716
grub_disk_t disk = data->disk;
717
struct grub_nilfs2_super_block sb2;
718
grub_uint64_t partition_size;
722
/* Read first super block. */
723
grub_disk_read (disk, NILFS_1ST_SUPER_BLOCK, 0,
724
sizeof (struct grub_nilfs2_super_block), &data->sblock);
725
/* Make sure if 1st super block is valid. */
726
valid[0] = grub_nilfs2_valid_sb (&data->sblock);
728
partition_size = grub_disk_get_size (disk);
729
if (partition_size != GRUB_DISK_SIZE_UNKNOWN)
731
/* Read second super block. */
732
grub_disk_read (disk, NILFS_2ND_SUPER_BLOCK (partition_size), 0,
733
sizeof (struct grub_nilfs2_super_block), &sb2);
734
/* Make sure if 2nd super block is valid. */
735
valid[1] = grub_nilfs2_valid_sb (&sb2);
738
/* 2nd super block may not exist, so it's invalid. */
743
if (!valid[0] && !valid[1])
744
return grub_error (GRUB_ERR_BAD_FS, "not a nilfs2 filesystem");
746
swp = valid[1] && (!valid[0] ||
747
grub_le_to_cpu64 (data->sblock.s_last_cno) <
748
grub_le_to_cpu64 (sb2.s_last_cno));
750
/* swap if first super block is invalid or older than second one. */
752
grub_memcpy (&data->sblock, &sb2,
753
sizeof (struct grub_nilfs2_super_block));
755
grub_errno = GRUB_ERR_NONE;
759
static struct grub_nilfs2_data *
760
grub_nilfs2_mount (grub_disk_t disk)
762
struct grub_nilfs2_data *data;
763
struct grub_nilfs2_segment_summary ss;
764
struct grub_nilfs2_checkpoint last_checkpoint;
765
grub_uint64_t last_pseg;
766
grub_uint32_t nblocks;
767
unsigned int nilfs2_block_count;
769
data = grub_malloc (sizeof (struct grub_nilfs2_data));
775
/* Read the superblock. */
776
grub_nilfs2_load_sb (data);
780
nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
782
/* Read the last segment summary. */
783
last_pseg = grub_le_to_cpu64 (data->sblock.s_last_pseg);
784
grub_disk_read (disk, last_pseg * nilfs2_block_count, 0,
785
sizeof (struct grub_nilfs2_segment_summary), &ss);
790
/* Read the super root block. */
791
nblocks = grub_le_to_cpu32 (ss.ss_nblocks);
792
grub_disk_read (disk, (last_pseg + (nblocks - 1)) * nilfs2_block_count, 0,
793
sizeof (struct grub_nilfs2_super_root), &data->sroot);
798
grub_nilfs2_read_last_checkpoint (data, &last_checkpoint);
803
grub_memcpy (&data->ifile, &last_checkpoint.cp_ifile_inode,
804
sizeof (struct grub_nilfs2_inode));
806
data->diropen.data = data;
807
data->diropen.ino = 2;
808
data->diropen.inode_read = 1;
809
data->inode = &data->diropen.inode;
811
grub_nilfs2_read_inode (data, 2, data->inode);
816
if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
817
grub_error (GRUB_ERR_BAD_FS, "not a nilfs2 filesystem");
824
grub_nilfs2_read_symlink (grub_fshelp_node_t node)
827
struct grub_fshelp_node *diro = node;
829
if (!diro->inode_read)
831
grub_nilfs2_read_inode (diro->data, diro->ino, &diro->inode);
836
symlink = grub_malloc (grub_le_to_cpu64 (diro->inode.i_size) + 1);
840
grub_nilfs2_read_file (diro, 0, 0,
841
grub_le_to_cpu64 (diro->inode.i_size), symlink);
848
symlink[grub_le_to_cpu64 (diro->inode.i_size)] = '\0';
853
grub_nilfs2_iterate_dir (grub_fshelp_node_t dir,
855
(*hook) (const char *filename,
856
enum grub_fshelp_filetype filetype,
857
grub_fshelp_node_t node))
859
unsigned int fpos = 0;
860
struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
862
if (!diro->inode_read)
864
grub_nilfs2_read_inode (diro->data, diro->ino, &diro->inode);
870
while (fpos < grub_le_to_cpu64 (diro->inode.i_size))
872
struct grub_nilfs2_dir_entry dirent;
874
grub_nilfs2_read_file (diro, 0, fpos,
875
sizeof (struct grub_nilfs2_dir_entry),
880
if (dirent.rec_len == 0)
883
if (dirent.name_len != 0)
885
char filename[dirent.name_len + 1];
886
struct grub_fshelp_node *fdiro;
887
enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
889
grub_nilfs2_read_file (diro, 0,
890
fpos + sizeof (struct grub_nilfs2_dir_entry),
891
dirent.name_len, filename);
895
fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
899
fdiro->data = diro->data;
900
fdiro->ino = grub_le_to_cpu64 (dirent.inode);
902
filename[dirent.name_len] = '\0';
904
if (dirent.file_type != NILFS_FT_UNKNOWN)
906
fdiro->inode_read = 0;
908
if (dirent.file_type == NILFS_FT_DIR)
909
type = GRUB_FSHELP_DIR;
910
else if (dirent.file_type == NILFS_FT_SYMLINK)
911
type = GRUB_FSHELP_SYMLINK;
912
else if (dirent.file_type == NILFS_FT_REG_FILE)
913
type = GRUB_FSHELP_REG;
917
/* The filetype can not be read from the dirent, read
918
the inode to get more information. */
919
grub_nilfs2_read_inode (diro->data,
920
grub_le_to_cpu64 (dirent.inode),
928
fdiro->inode_read = 1;
930
if ((grub_le_to_cpu16 (fdiro->inode.i_mode)
931
& FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
932
type = GRUB_FSHELP_DIR;
933
else if ((grub_le_to_cpu16 (fdiro->inode.i_mode)
934
& FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
935
type = GRUB_FSHELP_SYMLINK;
936
else if ((grub_le_to_cpu16 (fdiro->inode.i_mode)
937
& FILETYPE_INO_MASK) == FILETYPE_INO_REG)
938
type = GRUB_FSHELP_REG;
941
if (hook (filename, type, fdiro))
945
fpos += grub_le_to_cpu16 (dirent.rec_len);
951
/* Open a file named NAME and initialize FILE. */
953
grub_nilfs2_open (struct grub_file *file, const char *name)
955
struct grub_nilfs2_data *data = NULL;
956
struct grub_fshelp_node *fdiro = 0;
958
grub_dl_ref (my_mod);
960
data = grub_nilfs2_mount (file->device->disk);
964
grub_fshelp_find_file (name, &data->diropen, &fdiro,
965
grub_nilfs2_iterate_dir, grub_nilfs2_read_symlink,
970
if (!fdiro->inode_read)
972
grub_nilfs2_read_inode (data, fdiro->ino, &fdiro->inode);
977
grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_nilfs2_inode));
980
file->size = grub_le_to_cpu64 (data->inode->i_size);
987
if (fdiro != &data->diropen)
991
grub_dl_unref (my_mod);
997
grub_nilfs2_close (grub_file_t file)
999
grub_free (file->data);
1001
grub_dl_unref (my_mod);
1003
return GRUB_ERR_NONE;
1006
/* Read LEN bytes data from FILE into BUF. */
1008
grub_nilfs2_read (grub_file_t file, char *buf, grub_size_t len)
1010
struct grub_nilfs2_data *data = (struct grub_nilfs2_data *) file->data;
1012
return grub_nilfs2_read_file (&data->diropen, file->read_hook,
1013
file->offset, len, buf);
1017
grub_nilfs2_dir (grub_device_t device, const char *path,
1018
int (*hook) (const char *filename,
1019
const struct grub_dirhook_info * info))
1021
struct grub_nilfs2_data *data = 0;
1022
struct grub_fshelp_node *fdiro = 0;
1024
auto int NESTED_FUNC_ATTR iterate (const char *filename,
1025
enum grub_fshelp_filetype filetype,
1026
grub_fshelp_node_t node);
1028
int NESTED_FUNC_ATTR iterate (const char *filename,
1029
enum grub_fshelp_filetype filetype,
1030
grub_fshelp_node_t node)
1032
struct grub_dirhook_info info;
1033
grub_memset (&info, 0, sizeof (info));
1034
if (!node->inode_read)
1036
grub_nilfs2_read_inode (data, node->ino, &node->inode);
1038
node->inode_read = 1;
1039
grub_errno = GRUB_ERR_NONE;
1041
if (node->inode_read)
1044
info.mtime = grub_le_to_cpu64 (node->inode.i_mtime);
1047
info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
1049
return hook (filename, &info);
1052
grub_dl_ref (my_mod);
1054
data = grub_nilfs2_mount (device->disk);
1058
grub_fshelp_find_file (path, &data->diropen, &fdiro,
1059
grub_nilfs2_iterate_dir, grub_nilfs2_read_symlink,
1064
grub_nilfs2_iterate_dir (fdiro, iterate);
1067
if (fdiro != &data->diropen)
1071
grub_dl_unref (my_mod);
1077
grub_nilfs2_label (grub_device_t device, char **label)
1079
struct grub_nilfs2_data *data;
1080
grub_disk_t disk = device->disk;
1082
grub_dl_ref (my_mod);
1084
data = grub_nilfs2_mount (disk);
1086
*label = grub_strndup (data->sblock.s_volume_name, 14);
1090
grub_dl_unref (my_mod);
1098
grub_nilfs2_uuid (grub_device_t device, char **uuid)
1100
struct grub_nilfs2_data *data;
1101
grub_disk_t disk = device->disk;
1103
grub_dl_ref (my_mod);
1105
data = grub_nilfs2_mount (disk);
1110
("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%0x-%02x%02x%02x%02x%02x%02x",
1111
data->sblock.s_uuid[0], data->sblock.s_uuid[1],
1112
data->sblock.s_uuid[2], data->sblock.s_uuid[3],
1113
data->sblock.s_uuid[4], data->sblock.s_uuid[5],
1114
data->sblock.s_uuid[6], data->sblock.s_uuid[7],
1115
data->sblock.s_uuid[8], data->sblock.s_uuid[9],
1116
data->sblock.s_uuid[10], data->sblock.s_uuid[11],
1117
data->sblock.s_uuid[12], data->sblock.s_uuid[13],
1118
data->sblock.s_uuid[14], data->sblock.s_uuid[15]);
1123
grub_dl_unref (my_mod);
1132
grub_nilfs2_mtime (grub_device_t device, grub_int32_t * tm)
1134
struct grub_nilfs2_data *data;
1135
grub_disk_t disk = device->disk;
1137
grub_dl_ref (my_mod);
1139
data = grub_nilfs2_mount (disk);
1143
*tm = (grub_int32_t) grub_le_to_cpu64 (data->sblock.s_mtime);
1145
grub_dl_unref (my_mod);
1154
static struct grub_fs grub_nilfs2_fs = {
1156
.dir = grub_nilfs2_dir,
1157
.open = grub_nilfs2_open,
1158
.read = grub_nilfs2_read,
1159
.close = grub_nilfs2_close,
1160
.label = grub_nilfs2_label,
1161
.uuid = grub_nilfs2_uuid,
1162
.mtime = grub_nilfs2_mtime,
1164
.reserved_first_sector = 1,
1169
GRUB_MOD_INIT (nilfs2)
1171
grub_fs_register (&grub_nilfs2_fs);
1175
GRUB_MOD_FINI (nilfs2)
1177
grub_fs_unregister (&grub_nilfs2_fs);