3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/file.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
26 #include <grub/types.h>
27 #include <grub/fshelp.h>
29 GRUB_MOD_LICENSE ("GPLv3+");
31 #define XFS_INODE_EXTENTS 9
33 #define XFS_INODE_FORMAT_INO 1
34 #define XFS_INODE_FORMAT_EXT 2
35 #define XFS_INODE_FORMAT_BTREE 3
37 /* Superblock version field flags */
38 #define XFS_SB_VERSION_NUMBITS 0x000f
39 #define XFS_SB_VERSION_ATTRBIT 0x0010
40 #define XFS_SB_VERSION_NLINKBIT 0x0020
41 #define XFS_SB_VERSION_QUOTABIT 0x0040
42 #define XFS_SB_VERSION_ALIGNBIT 0x0080
43 #define XFS_SB_VERSION_DALIGNBIT 0x0100
44 #define XFS_SB_VERSION_LOGV2BIT 0x0400
45 #define XFS_SB_VERSION_SECTORBIT 0x0800
46 #define XFS_SB_VERSION_EXTFLGBIT 0x1000
47 #define XFS_SB_VERSION_DIRV2BIT 0x2000
48 #define XFS_SB_VERSION_MOREBITSBIT 0x8000
49 #define XFS_SB_VERSION_BITS_SUPPORTED \
50 (XFS_SB_VERSION_NUMBITS | \
51 XFS_SB_VERSION_ATTRBIT | \
52 XFS_SB_VERSION_NLINKBIT | \
53 XFS_SB_VERSION_QUOTABIT | \
54 XFS_SB_VERSION_ALIGNBIT | \
55 XFS_SB_VERSION_DALIGNBIT | \
56 XFS_SB_VERSION_LOGV2BIT | \
57 XFS_SB_VERSION_SECTORBIT | \
58 XFS_SB_VERSION_EXTFLGBIT | \
59 XFS_SB_VERSION_DIRV2BIT | \
60 XFS_SB_VERSION_MOREBITSBIT)
62 /* Recognized xfs format versions */
63 #define XFS_SB_VERSION_4 4 /* Good old XFS filesystem */
64 #define XFS_SB_VERSION_5 5 /* CRC enabled filesystem */
66 /* features2 field flags */
67 #define XFS_SB_VERSION2_LAZYSBCOUNTBIT 0x00000002 /* Superblk counters */
68 #define XFS_SB_VERSION2_ATTR2BIT 0x00000008 /* Inline attr rework */
69 #define XFS_SB_VERSION2_PROJID32BIT 0x00000080 /* 32-bit project ids */
70 #define XFS_SB_VERSION2_FTYPE 0x00000200 /* inode type in dir */
71 #define XFS_SB_VERSION2_BITS_SUPPORTED \
72 (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
73 XFS_SB_VERSION2_ATTR2BIT | \
74 XFS_SB_VERSION2_PROJID32BIT | \
75 XFS_SB_VERSION2_FTYPE)
77 /* incompat feature flags */
78 #define XFS_SB_FEAT_INCOMPAT_FTYPE (1 << 0) /* filetype in dirent */
79 #define XFS_SB_FEAT_INCOMPAT_SPINODES (1 << 1) /* sparse inode chunks */
80 #define XFS_SB_FEAT_INCOMPAT_META_UUID (1 << 2) /* metadata UUID */
82 /* We do not currently verify metadata UUID so it is safe to read such filesystem */
83 #define XFS_SB_FEAT_INCOMPAT_SUPPORTED \
84 (XFS_SB_FEAT_INCOMPAT_FTYPE | \
85 XFS_SB_FEAT_INCOMPAT_META_UUID)
87 struct grub_xfs_sblock
89 grub_uint8_t magic[4];
91 grub_uint8_t unused1[24];
92 grub_uint16_t uuid[8];
93 grub_uint8_t unused2[8];
94 grub_uint64_t rootino;
95 grub_uint8_t unused3[20];
97 grub_uint8_t unused4[12];
98 grub_uint16_t version;
99 grub_uint8_t unused5[6];
100 grub_uint8_t label[12];
101 grub_uint8_t log2_bsize;
102 grub_uint8_t log2_sect;
103 grub_uint8_t log2_inode;
104 grub_uint8_t log2_inop;
105 grub_uint8_t log2_agblk;
106 grub_uint8_t unused6[67];
107 grub_uint8_t log2_dirblk;
108 grub_uint8_t unused7[7];
109 grub_uint32_t features2;
110 grub_uint8_t unused8[4];
111 grub_uint32_t sb_features_compat;
112 grub_uint32_t sb_features_ro_compat;
113 grub_uint32_t sb_features_incompat;
114 grub_uint32_t sb_features_log_incompat;
117 struct grub_xfs_dir_header
120 grub_uint8_t largeino;
125 } GRUB_PACKED parent;
128 /* Structure for directory entry inlined in the inode */
129 struct grub_xfs_dir_entry
132 grub_uint16_t offset;
134 /* Inode number follows, 32 / 64 bits. */
137 /* Structure for directory entry in a block */
138 struct grub_xfs_dir2_entry
144 struct grub_xfs_extent
146 /* This should be a bitfield but bietfields are unportable, so just have
147 a raw array and functions extracting useful info from it.
149 grub_uint32_t raw[4];
152 struct grub_xfs_btree_node
154 grub_uint8_t magic[4];
156 grub_uint16_t numrecs;
159 /* In V5 here follow crc, uuid, etc. */
160 /* Then follow keys and block pointers */
163 struct grub_xfs_btree_root
166 grub_uint16_t numrecs;
167 grub_uint64_t keys[1];
173 grub_uint32_t nanosec;
176 struct grub_xfs_inode
178 grub_uint8_t magic[2];
180 grub_uint8_t version;
182 grub_uint8_t unused2[26];
183 struct grub_xfs_time atime;
184 struct grub_xfs_time mtime;
185 struct grub_xfs_time ctime;
187 grub_uint64_t nblocks;
188 grub_uint32_t extsize;
189 grub_uint32_t nextents;
190 grub_uint16_t unused3;
191 grub_uint8_t fork_offset;
192 grub_uint8_t unused4[17];
195 #define XFS_V2_INODE_SIZE sizeof(struct grub_xfs_inode)
196 #define XFS_V3_INODE_SIZE (XFS_V2_INODE_SIZE + 76)
198 struct grub_xfs_dirblock_tail
200 grub_uint32_t leaf_count;
201 grub_uint32_t leaf_stale;
204 struct grub_fshelp_node
206 struct grub_xfs_data *data;
209 struct grub_xfs_inode inode;
214 struct grub_xfs_sblock sblock;
218 grub_uint32_t agsize;
219 unsigned int hasftype:1;
220 unsigned int hascrc:1;
221 struct grub_fshelp_node diropen;
224 static grub_dl_t my_mod;
228 static int grub_xfs_sb_hascrc(struct grub_xfs_data *data)
230 return (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
231 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5);
234 static int grub_xfs_sb_hasftype(struct grub_xfs_data *data)
236 if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
237 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5) &&
238 data->sblock.sb_features_incompat & grub_cpu_to_be32_compile_time(XFS_SB_FEAT_INCOMPAT_FTYPE))
240 if (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
241 data->sblock.features2 & grub_cpu_to_be32_compile_time(XFS_SB_VERSION2_FTYPE))
246 static int grub_xfs_sb_valid(struct grub_xfs_data *data)
248 grub_dprintf("xfs", "Validating superblock\n");
249 if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4)
250 || data->sblock.log2_bsize < GRUB_DISK_SECTOR_BITS
251 || ((int) data->sblock.log2_bsize
252 + (int) data->sblock.log2_dirblk) >= 27)
254 grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem");
257 if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
258 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5))
260 grub_dprintf("xfs", "XFS v5 superblock detected\n");
261 if (data->sblock.sb_features_incompat &
262 grub_cpu_to_be32_compile_time(~XFS_SB_FEAT_INCOMPAT_SUPPORTED))
264 grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported "
265 "incompatible features");
270 else if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
271 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_4))
273 grub_dprintf("xfs", "XFS v4 superblock detected\n");
274 if (!(data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_DIRV2BIT)))
276 grub_error (GRUB_ERR_BAD_FS, "XFS filesystem without V2 directories "
280 if (data->sblock.version & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION_BITS_SUPPORTED) ||
281 (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
282 data->sblock.features2 & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION2_BITS_SUPPORTED)))
284 grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported version "
293 /* Filetype information as used in inodes. */
294 #define FILETYPE_INO_MASK 0170000
295 #define FILETYPE_INO_REG 0100000
296 #define FILETYPE_INO_DIRECTORY 0040000
297 #define FILETYPE_INO_SYMLINK 0120000
300 GRUB_XFS_INO_AGBITS(struct grub_xfs_data *data)
302 return ((data)->sblock.log2_agblk + (data)->sblock.log2_inop);
305 static inline grub_uint64_t
306 GRUB_XFS_INO_INOINAG (struct grub_xfs_data *data,
309 return (ino & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1));
312 static inline grub_uint64_t
313 GRUB_XFS_INO_AG (struct grub_xfs_data *data,
316 return (ino >> GRUB_XFS_INO_AGBITS (data));
319 static inline grub_disk_addr_t
320 GRUB_XFS_FSB_TO_BLOCK (struct grub_xfs_data *data, grub_disk_addr_t fsb)
322 return ((fsb >> data->sblock.log2_agblk) * data->agsize
323 + (fsb & ((1LL << data->sblock.log2_agblk) - 1)));
326 static inline grub_uint64_t
327 GRUB_XFS_EXTENT_OFFSET (struct grub_xfs_extent *exts, int ex)
329 return ((grub_be_to_cpu32 (exts[ex].raw[0]) & ~(1 << 31)) << 23
330 | grub_be_to_cpu32 (exts[ex].raw[1]) >> 9);
333 static inline grub_uint64_t
334 GRUB_XFS_EXTENT_BLOCK (struct grub_xfs_extent *exts, int ex)
336 return ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex].raw[1])
338 | (grub_uint64_t) grub_be_to_cpu32 (exts[ex].raw[2]) << 11
339 | grub_be_to_cpu32 (exts[ex].raw[3]) >> 21);
342 static inline grub_uint64_t
343 GRUB_XFS_EXTENT_SIZE (struct grub_xfs_extent *exts, int ex)
345 return (grub_be_to_cpu32 (exts[ex].raw[3]) & ((1 << 21) - 1));
349 static inline grub_uint64_t
350 grub_xfs_inode_block (struct grub_xfs_data *data,
353 long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
354 long long ag = GRUB_XFS_INO_AG (data, ino);
357 block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
358 block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
364 grub_xfs_inode_offset (struct grub_xfs_data *data,
367 int inoag = GRUB_XFS_INO_INOINAG (data, ino);
368 return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
369 data->sblock.log2_inode);
372 static inline grub_size_t
373 grub_xfs_inode_size(struct grub_xfs_data *data)
375 return (grub_size_t)1 << data->sblock.log2_inode;
379 * Returns size occupied by XFS inode stored in memory - we store struct
380 * grub_fshelp_node there but on disk inode size may be actually larger than
381 * struct grub_xfs_inode so we need to account for that so that we can read
382 * from disk directly into in-memory structure.
384 static inline grub_size_t
385 grub_xfs_fshelp_size(struct grub_xfs_data *data)
387 return sizeof (struct grub_fshelp_node) - sizeof (struct grub_xfs_inode)
388 + grub_xfs_inode_size(data);
391 /* This should return void * but XFS code is error-prone with alignment, so
392 return char to retain cast-align.
395 grub_xfs_inode_data(struct grub_xfs_inode *inode)
397 if (inode->version <= 2)
398 return ((char *)inode) + XFS_V2_INODE_SIZE;
399 return ((char *)inode) + XFS_V3_INODE_SIZE;
402 static struct grub_xfs_dir_entry *
403 grub_xfs_inline_de(struct grub_xfs_dir_header *head)
406 With small inode numbers the header is 4 bytes smaller because of
407 smaller parent pointer
409 return (struct grub_xfs_dir_entry *)
410 (((char *) head) + sizeof(struct grub_xfs_dir_header) -
411 (head->largeino ? 0 : sizeof(grub_uint32_t)));
414 static grub_uint8_t *
415 grub_xfs_inline_de_inopos(struct grub_xfs_data *data,
416 struct grub_xfs_dir_entry *de)
418 return ((grub_uint8_t *)(de + 1)) + de->len - 1 + (data->hasftype ? 1 : 0);
421 static struct grub_xfs_dir_entry *
422 grub_xfs_inline_next_de(struct grub_xfs_data *data,
423 struct grub_xfs_dir_header *head,
424 struct grub_xfs_dir_entry *de)
426 char *p = (char *)de + sizeof(struct grub_xfs_dir_entry) - 1 + de->len;
428 p += head->largeino ? sizeof(grub_uint64_t) : sizeof(grub_uint32_t);
432 return (struct grub_xfs_dir_entry *)p;
435 static struct grub_xfs_dirblock_tail *
436 grub_xfs_dir_tail(struct grub_xfs_data *data, void *dirblock)
438 int dirblksize = 1 << (data->sblock.log2_bsize + data->sblock.log2_dirblk);
440 return (struct grub_xfs_dirblock_tail *)
441 ((char *)dirblock + dirblksize - sizeof (struct grub_xfs_dirblock_tail));
444 static struct grub_xfs_dir2_entry *
445 grub_xfs_first_de(struct grub_xfs_data *data, void *dirblock)
448 return (struct grub_xfs_dir2_entry *)((char *)dirblock + 64);
449 return (struct grub_xfs_dir2_entry *)((char *)dirblock + 16);
452 static struct grub_xfs_dir2_entry *
453 grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
455 int size = sizeof (struct grub_xfs_dir2_entry) + de->len + 2 /* Tag */;
458 size++; /* File type */
459 return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
462 /* This should return void * but XFS code is error-prone with alignment, so
463 return char to retain cast-align.
466 grub_xfs_btree_keys(struct grub_xfs_data *data,
467 struct grub_xfs_btree_node *leaf)
469 char *keys = (char *)(leaf + 1);
472 keys += 48; /* skip crc, uuid, ... */
477 grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
478 struct grub_xfs_inode *inode)
480 grub_uint64_t block = grub_xfs_inode_block (data, ino);
481 int offset = grub_xfs_inode_offset (data, ino);
483 grub_dprintf("xfs", "Reading inode (%"PRIuGRUB_UINT64_T") - %"PRIuGRUB_UINT64_T", %d\n",
485 /* Read the inode. */
486 if (grub_disk_read (data->disk, block, offset, grub_xfs_inode_size(data),
490 if (grub_strncmp ((char *) inode->magic, "IN", 2))
491 return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
497 get_fsb (const void *keys, int idx)
499 const char *p = (const char *) keys + sizeof(grub_uint64_t) * idx;
500 return grub_be_to_cpu64 (grub_get_unaligned64 (p));
503 static grub_disk_addr_t
504 grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
506 struct grub_xfs_btree_node *leaf = 0;
508 struct grub_xfs_extent *exts;
509 grub_uint64_t ret = 0;
511 if (node->inode.format == XFS_INODE_FORMAT_BTREE)
513 struct grub_xfs_btree_root *root;
517 leaf = grub_malloc (node->data->bsize);
521 root = (struct grub_xfs_btree_root *) grub_xfs_inode_data(&node->inode);
522 nrec = grub_be_to_cpu16 (root->numrecs);
523 keys = (char *) &root->keys[0];
524 if (node->inode.fork_offset)
525 recoffset = (node->inode.fork_offset - 1) / 2;
527 recoffset = (grub_xfs_inode_size(node->data)
528 - ((char *) keys - (char *) &node->inode))
529 / (2 * sizeof (grub_uint64_t));
534 for (i = 0; i < nrec; i++)
536 if (fileblock < get_fsb(keys, i))
547 if (grub_disk_read (node->data->disk,
548 GRUB_XFS_FSB_TO_BLOCK (node->data, get_fsb (keys, i - 1 + recoffset)) << (node->data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS),
549 0, node->data->bsize, leaf))
552 if ((!node->data->hascrc &&
553 grub_strncmp ((char *) leaf->magic, "BMAP", 4)) ||
554 (node->data->hascrc &&
555 grub_strncmp ((char *) leaf->magic, "BMA3", 4)))
558 grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
562 nrec = grub_be_to_cpu16 (leaf->numrecs);
563 keys = grub_xfs_btree_keys(node->data, leaf);
564 recoffset = ((node->data->bsize - ((char *) keys
566 / (2 * sizeof (grub_uint64_t)));
569 exts = (struct grub_xfs_extent *) keys;
571 else if (node->inode.format == XFS_INODE_FORMAT_EXT)
573 nrec = grub_be_to_cpu32 (node->inode.nextents);
574 exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode);
578 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
579 "XFS does not support inode format %d yet",
584 /* Iterate over each extent to figure out which extent has
585 the block we are looking for. */
586 for (ex = 0; ex < nrec; ex++)
588 grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
589 grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
590 grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
593 if (fileblock < offset)
595 else if (fileblock < offset + size)
597 ret = (fileblock - offset + start);
604 return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
608 /* Read LEN bytes from the file described by DATA starting with byte
609 POS. Return the amount of read bytes in READ. */
611 grub_xfs_read_file (grub_fshelp_node_t node,
612 grub_disk_read_hook_t read_hook, void *read_hook_data,
613 grub_off_t pos, grub_size_t len, char *buf, grub_uint32_t header_size)
615 return grub_fshelp_read_file (node->data->disk, node,
616 read_hook, read_hook_data,
617 pos, len, buf, grub_xfs_read_block,
618 grub_be_to_cpu64 (node->inode.size) + header_size,
619 node->data->sblock.log2_bsize
620 - GRUB_DISK_SECTOR_BITS, 0);
625 grub_xfs_read_symlink (grub_fshelp_node_t node)
627 grub_ssize_t size = grub_be_to_cpu64 (node->inode.size);
631 grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
635 switch (node->inode.format)
637 case XFS_INODE_FORMAT_INO:
638 return grub_strndup (grub_xfs_inode_data(&node->inode), size);
640 case XFS_INODE_FORMAT_EXT:
643 grub_ssize_t numread;
646 if (node->data->hascrc)
649 symlink = grub_malloc (size + 1);
653 node->inode.size = grub_be_to_cpu64 (size + off);
654 numread = grub_xfs_read_file (node, 0, 0, off, size, symlink, off);
660 symlink[size] = '\0';
669 static enum grub_fshelp_filetype
670 grub_xfs_mode_to_filetype (grub_uint16_t mode)
672 if ((grub_be_to_cpu16 (mode)
673 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
674 return GRUB_FSHELP_DIR;
675 else if ((grub_be_to_cpu16 (mode)
676 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
677 return GRUB_FSHELP_SYMLINK;
678 else if ((grub_be_to_cpu16 (mode)
679 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
680 return GRUB_FSHELP_REG;
681 return GRUB_FSHELP_UNKNOWN;
685 /* Context for grub_xfs_iterate_dir. */
686 struct grub_xfs_iterate_dir_ctx
688 grub_fshelp_iterate_dir_hook_t hook;
690 struct grub_fshelp_node *diro;
693 /* Helper for grub_xfs_iterate_dir. */
694 static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename,
695 struct grub_xfs_iterate_dir_ctx *ctx)
697 struct grub_fshelp_node *fdiro;
700 fdiro = grub_malloc (grub_xfs_fshelp_size(ctx->diro->data) + 1);
707 /* The inode should be read, otherwise the filetype can
708 not be determined. */
710 fdiro->inode_read = 1;
711 fdiro->data = ctx->diro->data;
712 err = grub_xfs_read_inode (ctx->diro->data, ino, &fdiro->inode);
719 return ctx->hook (filename, grub_xfs_mode_to_filetype (fdiro->inode.mode),
720 fdiro, ctx->hook_data);
724 grub_xfs_iterate_dir (grub_fshelp_node_t dir,
725 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
727 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
728 struct grub_xfs_iterate_dir_ctx ctx = {
730 .hook_data = hook_data,
734 switch (diro->inode.format)
736 case XFS_INODE_FORMAT_INO:
738 struct grub_xfs_dir_header *head = (struct grub_xfs_dir_header *) grub_xfs_inode_data(&diro->inode);
739 struct grub_xfs_dir_entry *de = grub_xfs_inline_de(head);
740 int smallino = !head->largeino;
742 grub_uint64_t parent;
744 /* If small inode numbers are used to pack the direntry, the
745 parent inode number is small too. */
747 parent = grub_be_to_cpu32 (head->parent.i4);
749 parent = grub_be_to_cpu64 (head->parent.i8);
751 /* Synthesize the direntries for `.' and `..'. */
752 if (iterate_dir_call_hook (diro->ino, ".", &ctx))
755 if (iterate_dir_call_hook (parent, "..", &ctx))
758 for (i = 0; i < head->count; i++)
761 grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de);
764 /* inopos might be unaligned. */
766 ino = (((grub_uint32_t) inopos[0]) << 24)
767 | (((grub_uint32_t) inopos[1]) << 16)
768 | (((grub_uint32_t) inopos[2]) << 8)
769 | (((grub_uint32_t) inopos[3]) << 0);
771 ino = (((grub_uint64_t) inopos[0]) << 56)
772 | (((grub_uint64_t) inopos[1]) << 48)
773 | (((grub_uint64_t) inopos[2]) << 40)
774 | (((grub_uint64_t) inopos[3]) << 32)
775 | (((grub_uint64_t) inopos[4]) << 24)
776 | (((grub_uint64_t) inopos[5]) << 16)
777 | (((grub_uint64_t) inopos[6]) << 8)
778 | (((grub_uint64_t) inopos[7]) << 0);
780 c = de->name[de->len];
781 de->name[de->len] = '\0';
782 if (iterate_dir_call_hook (ino, de->name, &ctx))
784 de->name[de->len] = c;
787 de->name[de->len] = c;
789 de = grub_xfs_inline_next_de(dir->data, head, de);
794 case XFS_INODE_FORMAT_BTREE:
795 case XFS_INODE_FORMAT_EXT:
797 grub_ssize_t numread;
800 int dirblk_size, dirblk_log2;
802 dirblk_log2 = (dir->data->sblock.log2_bsize
803 + dir->data->sblock.log2_dirblk);
804 dirblk_size = 1 << dirblk_log2;
806 dirblock = grub_malloc (dirblk_size);
810 /* Iterate over every block the directory has. */
812 blk < (grub_be_to_cpu64 (dir->inode.size)
816 struct grub_xfs_dir2_entry *direntry =
817 grub_xfs_first_de(dir->data, dirblock);
819 struct grub_xfs_dirblock_tail *tail =
820 grub_xfs_dir_tail(dir->data, dirblock);
822 numread = grub_xfs_read_file (dir, 0, 0,
824 dirblk_size, dirblock, 0);
825 if (numread != dirblk_size)
828 entries = (grub_be_to_cpu32 (tail->leaf_count)
829 - grub_be_to_cpu32 (tail->leaf_stale));
834 /* Iterate over all entries within this block. */
835 while ((char *)direntry < (char *)tail)
837 grub_uint8_t *freetag;
840 freetag = (grub_uint8_t *) direntry;
842 if (grub_get_unaligned16 (freetag) == 0XFFFF)
844 grub_uint8_t *skip = (freetag + sizeof (grub_uint16_t));
846 /* This entry is not used, go to the next one. */
847 direntry = (struct grub_xfs_dir2_entry *)
848 (((char *)direntry) +
849 grub_be_to_cpu16 (grub_get_unaligned16 (skip)));
854 filename = (char *)(direntry + 1);
855 /* The byte after the filename is for the filetype, padding, or
856 tag, which is not used by GRUB. So it can be overwritten. */
857 filename[direntry->len] = '\0';
859 if (iterate_dir_call_hook (grub_be_to_cpu64(direntry->inode),
862 grub_free (dirblock);
866 /* Check if last direntry in this block is
872 /* Select the next directory entry. */
873 direntry = grub_xfs_next_de(dir->data, direntry);
876 grub_free (dirblock);
881 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
882 "XFS does not support inode format %d yet",
889 static struct grub_xfs_data *
890 grub_xfs_mount (grub_disk_t disk)
892 struct grub_xfs_data *data = 0;
894 data = grub_zalloc (sizeof (struct grub_xfs_data));
898 grub_dprintf("xfs", "Reading sb\n");
899 /* Read the superblock. */
900 if (grub_disk_read (disk, 0, 0,
901 sizeof (struct grub_xfs_sblock), &data->sblock))
904 if (!grub_xfs_sb_valid(data))
907 data = grub_realloc (data,
908 sizeof (struct grub_xfs_data)
909 - sizeof (struct grub_xfs_inode)
910 + grub_xfs_inode_size(data) + 1);
915 data->diropen.data = data;
916 data->diropen.ino = grub_be_to_cpu64(data->sblock.rootino);
917 data->diropen.inode_read = 1;
918 data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
919 data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
920 data->hasftype = grub_xfs_sb_hasftype(data);
921 data->hascrc = grub_xfs_sb_hascrc(data);
925 grub_dprintf("xfs", "Reading root ino %"PRIuGRUB_UINT64_T"\n",
926 grub_cpu_to_be64(data->sblock.rootino));
928 grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
933 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
934 grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
942 /* Context for grub_xfs_dir. */
943 struct grub_xfs_dir_ctx
945 grub_fs_dir_hook_t hook;
949 /* Helper for grub_xfs_dir. */
951 grub_xfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
952 grub_fshelp_node_t node, void *data)
954 struct grub_xfs_dir_ctx *ctx = data;
955 struct grub_dirhook_info info;
957 grub_memset (&info, 0, sizeof (info));
958 if (node->inode_read)
961 info.mtime = grub_be_to_cpu32 (node->inode.mtime.sec);
963 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
965 return ctx->hook (filename, &info, ctx->hook_data);
969 grub_xfs_dir (grub_device_t device, const char *path,
970 grub_fs_dir_hook_t hook, void *hook_data)
972 struct grub_xfs_dir_ctx ctx = { hook, hook_data };
973 struct grub_xfs_data *data = 0;
974 struct grub_fshelp_node *fdiro = 0;
976 grub_dl_ref (my_mod);
978 data = grub_xfs_mount (device->disk);
982 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
983 grub_xfs_read_symlink, GRUB_FSHELP_DIR);
987 grub_xfs_iterate_dir (fdiro, grub_xfs_dir_iter, &ctx);
990 if (fdiro != &data->diropen)
996 grub_dl_unref (my_mod);
1002 /* Open a file named NAME and initialize FILE. */
1004 grub_xfs_open (struct grub_file *file, const char *name)
1006 struct grub_xfs_data *data;
1007 struct grub_fshelp_node *fdiro = 0;
1009 grub_dl_ref (my_mod);
1011 data = grub_xfs_mount (file->device->disk);
1015 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
1016 grub_xfs_read_symlink, GRUB_FSHELP_REG);
1020 if (!fdiro->inode_read)
1022 grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
1027 if (fdiro != &data->diropen)
1029 grub_memcpy (&data->diropen, fdiro, grub_xfs_fshelp_size(data));
1033 file->size = grub_be_to_cpu64 (data->diropen.inode.size);
1040 if (fdiro != &data->diropen)
1045 grub_dl_unref (my_mod);
1052 grub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
1054 struct grub_xfs_data *data =
1055 (struct grub_xfs_data *) file->data;
1057 return grub_xfs_read_file (&data->diropen,
1058 file->read_hook, file->read_hook_data,
1059 file->offset, len, buf, 0);
1064 grub_xfs_close (grub_file_t file)
1066 grub_free (file->data);
1068 grub_dl_unref (my_mod);
1070 return GRUB_ERR_NONE;
1075 grub_xfs_label (grub_device_t device, char **label)
1077 struct grub_xfs_data *data;
1078 grub_disk_t disk = device->disk;
1080 grub_dl_ref (my_mod);
1082 data = grub_xfs_mount (disk);
1084 *label = grub_strndup ((char *) (data->sblock.label), 12);
1088 grub_dl_unref (my_mod);
1096 grub_xfs_uuid (grub_device_t device, char **uuid)
1098 struct grub_xfs_data *data;
1099 grub_disk_t disk = device->disk;
1101 grub_dl_ref (my_mod);
1103 data = grub_xfs_mount (disk);
1106 *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
1107 grub_be_to_cpu16 (data->sblock.uuid[0]),
1108 grub_be_to_cpu16 (data->sblock.uuid[1]),
1109 grub_be_to_cpu16 (data->sblock.uuid[2]),
1110 grub_be_to_cpu16 (data->sblock.uuid[3]),
1111 grub_be_to_cpu16 (data->sblock.uuid[4]),
1112 grub_be_to_cpu16 (data->sblock.uuid[5]),
1113 grub_be_to_cpu16 (data->sblock.uuid[6]),
1114 grub_be_to_cpu16 (data->sblock.uuid[7]));
1119 grub_dl_unref (my_mod);
1128 static struct grub_fs grub_xfs_fs =
1131 .dir = grub_xfs_dir,
1132 .open = grub_xfs_open,
1133 .read = grub_xfs_read,
1134 .close = grub_xfs_close,
1135 .label = grub_xfs_label,
1136 .uuid = grub_xfs_uuid,
1138 .reserved_first_sector = 0,
1139 .blocklist_install = 1,
1146 grub_fs_register (&grub_xfs_fs);
1152 grub_fs_unregister (&grub_xfs_fs);