3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007 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 #define XFS_INODE_EXTENTS 9
31 #define XFS_INODE_FORMAT_INO 1
32 #define XFS_INODE_FORMAT_EXT 2
33 #define XFS_INODE_FORMAT_BTREE 3
36 struct grub_xfs_sblock
38 grub_uint8_t magic
[4];
40 grub_uint8_t unused1
[48];
41 grub_uint64_t rootino
;
42 grub_uint8_t unused2
[20];
44 grub_uint8_t unused3
[20];
45 grub_uint8_t label
[12];
46 grub_uint8_t log2_bsize
;
47 grub_uint8_t unused4
[2];
48 grub_uint8_t log2_inop
;
49 grub_uint8_t log2_agblk
;
50 grub_uint8_t unused5
[67];
51 grub_uint8_t log2_dirblk
;
52 } __attribute__ ((packed
));
54 struct grub_xfs_dir_header
57 grub_uint8_t smallino
;
59 } __attribute__ ((packed
));
61 struct grub_xfs_dir_entry
66 /* Inode number follows, 32 bits. */
67 } __attribute__ ((packed
));
69 struct grub_xfs_dir2_entry
73 } __attribute__ ((packed
));
75 typedef grub_uint32_t grub_xfs_extent
[4];
77 struct grub_xfs_btree_node
79 grub_uint8_t magic
[4];
81 grub_uint16_t numrecs
;
84 grub_uint64_t keys
[1];
85 } __attribute__ ((packed
));
87 struct grub_xfs_btree_root
90 grub_uint16_t numrecs
;
91 grub_uint64_t keys
[1];
92 } __attribute__ ((packed
));
96 grub_uint8_t magic
[2];
100 grub_uint8_t unused2
[50];
102 grub_uint64_t nblocks
;
103 grub_uint32_t extsize
;
104 grub_uint32_t nextents
;
105 grub_uint8_t unused3
[20];
111 struct grub_xfs_dir_header dirhead
;
112 struct grub_xfs_dir_entry direntry
[1];
114 grub_xfs_extent extents
[XFS_INODE_EXTENTS
];
115 struct grub_xfs_btree_root btree
;
116 } data
__attribute__ ((packed
));
117 } __attribute__ ((packed
));
119 struct grub_xfs_dirblock_tail
121 grub_uint32_t leaf_count
;
122 grub_uint32_t leaf_stale
;
123 } __attribute__ ((packed
));
125 struct grub_fshelp_node
127 struct grub_xfs_data
*data
;
128 struct grub_xfs_inode inode
;
135 struct grub_xfs_sblock sblock
;
136 struct grub_xfs_inode
*inode
;
141 struct grub_fshelp_node diropen
;
146 static grub_dl_t my_mod
;
151 /* Filetype information as used in inodes. */
152 #define FILETYPE_INO_MASK 0170000
153 #define FILETYPE_INO_REG 0100000
154 #define FILETYPE_INO_DIRECTORY 0040000
155 #define FILETYPE_INO_SYMLINK 0120000
157 #define GRUB_XFS_INO_AGBITS(data) \
158 ((data)->sblock.log2_agblk + (data)->sblock.log2_inop)
159 #define GRUB_XFS_INO_INOINAG(data, ino) \
160 (grub_be_to_cpu64 (ino) & ((1 << GRUB_XFS_INO_AGBITS (data)) - 1))
161 #define GRUB_XFS_INO_AG(data,ino) \
162 (grub_be_to_cpu64 (ino) >> GRUB_XFS_INO_AGBITS (data))
164 #define GRUB_XFS_EXTENT_OFFSET(exts,ex) \
165 ((grub_be_to_cpu32 (exts[ex][0]) & ~(1 << 31)) << 23 \
166 | grub_be_to_cpu32 (exts[ex][1]) >> 9)
168 #define GRUB_XFS_EXTENT_BLOCK(exts,ex) \
169 ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex][1]) \
171 | (grub_uint64_t) grub_be_to_cpu32 (exts[ex][2]) << 11 \
172 | grub_be_to_cpu32 (exts[ex][3]) >> 21)
174 #define GRUB_XFS_EXTENT_SIZE(exts,ex) \
175 (grub_be_to_cpu32 (exts[ex][3]) & ((1 << 20) - 1))
177 #define GRUB_XFS_ROUND_TO_DIRENT(pos) ((((pos) + 8 - 1) / 8) * 8)
178 #define GRUB_XFS_NEXT_DIRENT(pos,len) \
179 (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2)
182 grub_xfs_inode_block (struct grub_xfs_data
*data
,
185 long long int inoinag
= GRUB_XFS_INO_INOINAG (data
, ino
);
186 long long ag
= GRUB_XFS_INO_AG (data
, ino
);
189 block
= (inoinag
>> 4) + ag
* data
->agsize
;
190 block
<<= (data
->sblock
.log2_bsize
- GRUB_DISK_SECTOR_BITS
);
196 grub_xfs_inode_offset (struct grub_xfs_data
*data
,
199 int inoag
= GRUB_XFS_INO_INOINAG (data
, ino
);
200 return (inoag
& ((1 << 4) - 1)) << 8;
205 grub_xfs_read_inode (struct grub_xfs_data
*data
, grub_uint64_t ino
,
206 struct grub_xfs_inode
*inode
)
208 int block
= grub_xfs_inode_block (data
, ino
);
209 int offset
= grub_xfs_inode_offset (data
, ino
);
211 /* Read the inode. */
212 if (grub_disk_read (data
->disk
, block
, offset
,
213 sizeof (struct grub_xfs_inode
), (char *) inode
))
216 if (grub_strncmp ((char *) inode
->magic
, "IN", 2))
217 return grub_error (GRUB_ERR_BAD_FS
, "not a correct XFS inode.\n");
224 grub_xfs_read_block (grub_fshelp_node_t node
, int fileblock
)
226 struct grub_xfs_btree_node
*leaf
= 0;
228 grub_xfs_extent
*exts
;
229 grub_uint64_t ret
= 0;
231 if (node
->inode
.format
== XFS_INODE_FORMAT_BTREE
)
235 leaf
= grub_malloc (node
->data
->sblock
.bsize
);
239 nrec
= grub_be_to_cpu16 (node
->inode
.data
.btree
.numrecs
);
240 keys
= &node
->inode
.data
.btree
.keys
[0];
245 for (i
= 0; i
< nrec
; i
++)
247 if ((grub_uint64_t
) fileblock
< grub_be_to_cpu64 (keys
[i
]))
258 if (grub_disk_read (node
->data
->disk
,
259 grub_be_to_cpu64 (keys
[i
- 1 + XFS_INODE_EXTENTS
])
260 << (node
->data
->sblock
.log2_bsize
261 - GRUB_DISK_SECTOR_BITS
),
262 0, node
->data
->sblock
.bsize
, (char *) leaf
))
265 if (grub_strncmp ((char *) leaf
->magic
, "BMAP", 4))
268 grub_error (GRUB_ERR_BAD_FS
, "not a correct XFS BMAP node.\n");
272 nrec
= grub_be_to_cpu16 (leaf
->numrecs
);
273 keys
= &leaf
->keys
[0];
274 } while (leaf
->level
);
275 exts
= (grub_xfs_extent
*) keys
;
277 else if (node
->inode
.format
== XFS_INODE_FORMAT_EXT
)
279 nrec
= grub_be_to_cpu32 (node
->inode
.nextents
);
280 exts
= &node
->inode
.data
.extents
[0];
284 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
285 "xfs does not support inode format %d yet",
290 /* Iterate over each extent to figure out which extent has
291 the block we are looking for. */
292 for (ex
= 0; ex
< nrec
; ex
++)
294 grub_uint64_t start
= GRUB_XFS_EXTENT_BLOCK (exts
, ex
);
295 int offset
= GRUB_XFS_EXTENT_OFFSET (exts
, ex
);
296 int size
= GRUB_XFS_EXTENT_SIZE (exts
, ex
);
299 if (fileblock
< offset
)
301 else if (fileblock
< offset
+ size
)
303 ret
= (fileblock
- offset
+ start
);
315 /* Read LEN bytes from the file described by DATA starting with byte
316 POS. Return the amount of read bytes in READ. */
318 grub_xfs_read_file (grub_fshelp_node_t node
,
319 void NESTED_FUNC_ATTR (*read_hook
) (grub_disk_addr_t sector
,
320 unsigned offset
, unsigned length
),
321 int pos
, grub_size_t len
, char *buf
)
323 return grub_fshelp_read_file (node
->data
->disk
, node
, read_hook
,
324 pos
, len
, buf
, grub_xfs_read_block
,
325 grub_be_to_cpu64 (node
->inode
.size
),
326 node
->data
->sblock
.log2_bsize
327 - GRUB_DISK_SECTOR_BITS
);
332 grub_xfs_read_symlink (grub_fshelp_node_t node
)
334 int size
= grub_be_to_cpu64 (node
->inode
.size
);
336 switch (node
->inode
.format
)
338 case XFS_INODE_FORMAT_INO
:
339 return grub_strndup (node
->inode
.data
.raw
, size
);
341 case XFS_INODE_FORMAT_EXT
:
344 grub_ssize_t numread
;
346 symlink
= grub_malloc (size
+ 1);
350 numread
= grub_xfs_read_file (node
, 0, 0, size
, symlink
);
356 symlink
[size
] = '\0';
365 static enum grub_fshelp_filetype
366 grub_xfs_mode_to_filetype (grub_uint16_t mode
)
368 if ((grub_be_to_cpu16 (mode
)
369 & FILETYPE_INO_MASK
) == FILETYPE_INO_DIRECTORY
)
370 return GRUB_FSHELP_DIR
;
371 else if ((grub_be_to_cpu16 (mode
)
372 & FILETYPE_INO_MASK
) == FILETYPE_INO_SYMLINK
)
373 return GRUB_FSHELP_SYMLINK
;
374 else if ((grub_be_to_cpu16 (mode
)
375 & FILETYPE_INO_MASK
) == FILETYPE_INO_REG
)
376 return GRUB_FSHELP_REG
;
377 return GRUB_FSHELP_UNKNOWN
;
382 grub_xfs_iterate_dir (grub_fshelp_node_t dir
,
384 (*hook
) (const char *filename
,
385 enum grub_fshelp_filetype filetype
,
386 grub_fshelp_node_t node
))
388 struct grub_fshelp_node
*diro
= (struct grub_fshelp_node
*) dir
;
389 auto int NESTED_FUNC_ATTR
call_hook (grub_uint64_t ino
, char *filename
);
391 int NESTED_FUNC_ATTR
call_hook (grub_uint64_t ino
, char *filename
)
393 struct grub_fshelp_node
*fdiro
;
395 fdiro
= grub_malloc (sizeof (struct grub_fshelp_node
));
399 /* The inode should be read, otherwise the filetype can
400 not be determined. */
402 fdiro
->inode_read
= 1;
403 fdiro
->data
= diro
->data
;
404 grub_xfs_read_inode (diro
->data
, ino
, &fdiro
->inode
);
406 return hook (filename
,
407 grub_xfs_mode_to_filetype (fdiro
->inode
.mode
),
411 switch (diro
->inode
.format
)
413 case XFS_INODE_FORMAT_INO
:
415 struct grub_xfs_dir_entry
*de
= &diro
->inode
.data
.dir
.direntry
[0];
416 int smallino
= !diro
->inode
.data
.dir
.dirhead
.smallino
;
418 grub_uint64_t parent
;
420 /* If small inode numbers are used to pack the direntry, the
421 parent inode number is small too. */
424 parent
= grub_be_to_cpu32 (diro
->inode
.data
.dir
.dirhead
.parent
);
425 parent
= grub_cpu_to_be64 (parent
);
429 parent
= *(grub_uint64_t
*) &diro
->inode
.data
.dir
.dirhead
.parent
;
430 /* The header is a bit bigger than usual. */
431 de
= (struct grub_xfs_dir_entry
*) ((char *) de
+ 4);
434 /* Synthesize the direntries for `.' and `..'. */
435 if (call_hook (diro
->ino
, "."))
438 if (call_hook (parent
, ".."))
441 for (i
= 0; i
< diro
->inode
.data
.dir
.dirhead
.entries
; i
++)
444 void *inopos
= (((char *) de
)
445 + sizeof (struct grub_xfs_dir_entry
)
447 char name
[de
->len
+ 1];
451 ino
= grub_be_to_cpu32 (*(grub_uint32_t
*) inopos
);
452 ino
= grub_cpu_to_be64 (ino
);
455 ino
= *(grub_uint64_t
*) inopos
;
457 grub_memcpy (name
, de
->name
, de
->len
);
458 name
[de
->len
] = '\0';
459 if (call_hook (ino
, name
))
462 de
= ((struct grub_xfs_dir_entry
*)
463 (((char *) de
)+ sizeof (struct grub_xfs_dir_entry
) + de
->len
464 + ((smallino
? sizeof (grub_uint32_t
)
465 : sizeof (grub_uint64_t
))) - 1));
470 case XFS_INODE_FORMAT_BTREE
:
471 case XFS_INODE_FORMAT_EXT
:
473 grub_ssize_t numread
;
476 int dirblk_size
, dirblk_log2
;
478 dirblk_log2
= (dir
->data
->sblock
.log2_bsize
479 + dir
->data
->sblock
.log2_dirblk
);
480 dirblk_size
= 1 << dirblk_log2
;
482 dirblock
= grub_malloc (dirblk_size
);
486 /* Iterate over every block the directory has. */
488 blk
< (grub_be_to_cpu64 (dir
->inode
.size
)
492 /* The header is skipped, the first direntry is stored
496 int tail_start
= (dirblk_size
497 - sizeof (struct grub_xfs_dirblock_tail
));
499 struct grub_xfs_dirblock_tail
*tail
;
500 tail
= (struct grub_xfs_dirblock_tail
*) &dirblock
[tail_start
];
502 numread
= grub_xfs_read_file (dir
, 0,
504 dirblk_size
, dirblock
);
505 if (numread
!= dirblk_size
)
508 entries
= (grub_be_to_cpu32 (tail
->leaf_count
)
509 - grub_be_to_cpu32 (tail
->leaf_stale
));
511 /* Iterate over all entries within this block. */
512 while (pos
< (dirblk_size
513 - (int) sizeof (struct grub_xfs_dir2_entry
)))
515 struct grub_xfs_dir2_entry
*direntry
;
516 grub_uint16_t
*freetag
;
519 direntry
= (struct grub_xfs_dir2_entry
*) &dirblock
[pos
];
520 freetag
= (grub_uint16_t
*) direntry
;
522 if (*freetag
== 0XFFFF)
524 grub_uint16_t
*skip
= (grub_uint16_t
*) (freetag
+ 1);
526 /* This entry is not used, go to the next one. */
527 pos
+= grub_be_to_cpu16 (*skip
);
532 filename
= &dirblock
[pos
+ sizeof (*direntry
)];
533 /* The byte after the filename is for the tag, which
534 is not used by GRUB. So it can be overwritten. */
535 filename
[direntry
->len
] = '\0';
537 if (call_hook (direntry
->inode
, filename
))
539 grub_free (dirblock
);
543 /* Check if last direntry in this block is
549 /* Select the next directory entry. */
550 pos
= GRUB_XFS_NEXT_DIRENT (pos
, direntry
->len
);
551 pos
= GRUB_XFS_ROUND_TO_DIRENT (pos
);
554 grub_free (dirblock
);
559 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
560 "xfs does not support inode format %d yet",
567 static struct grub_xfs_data
*
568 grub_xfs_mount (grub_disk_t disk
)
570 struct grub_xfs_data
*data
= 0;
572 data
= grub_malloc (sizeof (struct grub_xfs_data
));
576 /* Read the superblock. */
577 if (grub_disk_read (disk
, 0, 0,
578 sizeof (struct grub_xfs_sblock
), (char *) &data
->sblock
))
581 if (grub_strncmp ((char *) (data
->sblock
.magic
), "XFSB", 4))
583 grub_error (GRUB_ERR_BAD_FS
, "not a xfs filesystem");
587 data
->diropen
.data
= data
;
588 data
->diropen
.ino
= data
->sblock
.rootino
;
589 data
->diropen
.inode_read
= 1;
590 data
->bsize
= grub_be_to_cpu32 (data
->sblock
.bsize
);
591 data
->agsize
= grub_be_to_cpu32 (data
->sblock
.agsize
);
594 data
->inode
= &data
->diropen
.inode
;
597 grub_xfs_read_inode (data
, data
->diropen
.ino
, data
->inode
);
602 if (grub_errno
== GRUB_ERR_OUT_OF_RANGE
)
603 grub_error (GRUB_ERR_BAD_FS
, "not an xfs filesystem");
612 grub_xfs_dir (grub_device_t device
, const char *path
,
613 int (*hook
) (const char *filename
, int dir
))
615 struct grub_xfs_data
*data
= 0;;
616 struct grub_fshelp_node
*fdiro
= 0;
618 auto int NESTED_FUNC_ATTR
iterate (const char *filename
,
619 enum grub_fshelp_filetype filetype
,
620 grub_fshelp_node_t node
);
622 int NESTED_FUNC_ATTR
iterate (const char *filename
,
623 enum grub_fshelp_filetype filetype
,
624 grub_fshelp_node_t node
)
628 if (filetype
== GRUB_FSHELP_DIR
)
629 return hook (filename
, 1);
631 return hook (filename
, 0);
637 grub_dl_ref (my_mod
);
640 data
= grub_xfs_mount (device
->disk
);
644 grub_fshelp_find_file (path
, &data
->diropen
, &fdiro
, grub_xfs_iterate_dir
,
645 grub_xfs_read_symlink
, GRUB_FSHELP_DIR
);
649 grub_xfs_iterate_dir (fdiro
, iterate
);
652 if (fdiro
!= &data
->diropen
)
657 grub_dl_unref (my_mod
);
666 /* Open a file named NAME and initialize FILE. */
668 grub_xfs_open (struct grub_file
*file
, const char *name
)
670 struct grub_xfs_data
*data
;
671 struct grub_fshelp_node
*fdiro
= 0;
674 grub_dl_ref (my_mod
);
677 data
= grub_xfs_mount (file
->device
->disk
);
681 grub_fshelp_find_file (name
, &data
->diropen
, &fdiro
, grub_xfs_iterate_dir
,
682 grub_xfs_read_symlink
, GRUB_FSHELP_REG
);
686 if (!fdiro
->inode_read
)
688 grub_xfs_read_inode (data
, fdiro
->ino
, &fdiro
->inode
);
693 grub_memcpy (data
->inode
,
695 sizeof (struct grub_xfs_inode
));
698 file
->size
= grub_be_to_cpu64 (data
->inode
->size
);
705 if (fdiro
!= &data
->diropen
)
710 grub_dl_unref (my_mod
);
718 grub_xfs_read (grub_file_t file
, char *buf
, grub_size_t len
)
720 struct grub_xfs_data
*data
=
721 (struct grub_xfs_data
*) file
->data
;
723 return grub_xfs_read_file (&data
->diropen
, file
->read_hook
,
724 file
->offset
, len
, buf
);
729 grub_xfs_close (grub_file_t file
)
731 grub_free (file
->data
);
734 grub_dl_unref (my_mod
);
737 return GRUB_ERR_NONE
;
742 grub_xfs_label (grub_device_t device
, char **label
)
744 struct grub_xfs_data
*data
;
745 grub_disk_t disk
= device
->disk
;
748 grub_dl_ref (my_mod
);
751 data
= grub_xfs_mount (disk
);
753 *label
= grub_strndup ((char *) (data
->sblock
.label
), 12);
758 grub_dl_unref (my_mod
);
768 static struct grub_fs grub_xfs_fs
=
772 .open
= grub_xfs_open
,
773 .read
= grub_xfs_read
,
774 .close
= grub_xfs_close
,
775 .label
= grub_xfs_label
,
781 grub_fs_register (&grub_xfs_fs
);
789 grub_fs_unregister (&grub_xfs_fs
);