SECURITY: Add SECURITY file
[grub.git] / grub-core / fs / xfs.c
blob0f524c3a8a61e36a1df613aa5d1c17c19bb67c36
1 /* xfs.c - XFS. */
2 /*
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/>.
20 #include <grub/err.h>
21 #include <grub/file.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
25 #include <grub/dl.h>
26 #include <grub/time.h>
27 #include <grub/types.h>
28 #include <grub/fshelp.h>
29 #include <grub/safemath.h>
31 GRUB_MOD_LICENSE ("GPLv3+");
33 #define XFS_INODE_EXTENTS 9
35 #define XFS_INODE_FORMAT_INO 1
36 #define XFS_INODE_FORMAT_EXT 2
37 #define XFS_INODE_FORMAT_BTREE 3
39 /* Superblock version field flags */
40 #define XFS_SB_VERSION_NUMBITS 0x000f
41 #define XFS_SB_VERSION_ATTRBIT 0x0010
42 #define XFS_SB_VERSION_NLINKBIT 0x0020
43 #define XFS_SB_VERSION_QUOTABIT 0x0040
44 #define XFS_SB_VERSION_ALIGNBIT 0x0080
45 #define XFS_SB_VERSION_DALIGNBIT 0x0100
46 #define XFS_SB_VERSION_LOGV2BIT 0x0400
47 #define XFS_SB_VERSION_SECTORBIT 0x0800
48 #define XFS_SB_VERSION_EXTFLGBIT 0x1000
49 #define XFS_SB_VERSION_DIRV2BIT 0x2000
50 #define XFS_SB_VERSION_MOREBITSBIT 0x8000
51 #define XFS_SB_VERSION_BITS_SUPPORTED \
52 (XFS_SB_VERSION_NUMBITS | \
53 XFS_SB_VERSION_ATTRBIT | \
54 XFS_SB_VERSION_NLINKBIT | \
55 XFS_SB_VERSION_QUOTABIT | \
56 XFS_SB_VERSION_ALIGNBIT | \
57 XFS_SB_VERSION_DALIGNBIT | \
58 XFS_SB_VERSION_LOGV2BIT | \
59 XFS_SB_VERSION_SECTORBIT | \
60 XFS_SB_VERSION_EXTFLGBIT | \
61 XFS_SB_VERSION_DIRV2BIT | \
62 XFS_SB_VERSION_MOREBITSBIT)
64 /* Recognized xfs format versions */
65 #define XFS_SB_VERSION_4 4 /* Good old XFS filesystem */
66 #define XFS_SB_VERSION_5 5 /* CRC enabled filesystem */
68 /* features2 field flags */
69 #define XFS_SB_VERSION2_LAZYSBCOUNTBIT 0x00000002 /* Superblk counters */
70 #define XFS_SB_VERSION2_ATTR2BIT 0x00000008 /* Inline attr rework */
71 #define XFS_SB_VERSION2_PROJID32BIT 0x00000080 /* 32-bit project ids */
72 #define XFS_SB_VERSION2_FTYPE 0x00000200 /* inode type in dir */
73 #define XFS_SB_VERSION2_BITS_SUPPORTED \
74 (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
75 XFS_SB_VERSION2_ATTR2BIT | \
76 XFS_SB_VERSION2_PROJID32BIT | \
77 XFS_SB_VERSION2_FTYPE)
79 /* Inode flags2 flags */
80 #define XFS_DIFLAG2_BIGTIME_BIT 3
81 #define XFS_DIFLAG2_BIGTIME (1 << XFS_DIFLAG2_BIGTIME_BIT)
83 /* incompat feature flags */
84 #define XFS_SB_FEAT_INCOMPAT_FTYPE (1 << 0) /* filetype in dirent */
85 #define XFS_SB_FEAT_INCOMPAT_SPINODES (1 << 1) /* sparse inode chunks */
86 #define XFS_SB_FEAT_INCOMPAT_META_UUID (1 << 2) /* metadata UUID */
87 #define XFS_SB_FEAT_INCOMPAT_BIGTIME (1 << 3) /* large timestamps */
88 #define XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR (1 << 4) /* needs xfs_repair */
91 * Directory entries with ftype are explicitly handled by GRUB code.
93 * We do not currently read the inode btrees, so it is safe to read filesystems
94 * with the XFS_SB_FEAT_INCOMPAT_SPINODES feature.
96 * We do not currently verify metadata UUID, so it is safe to read filesystems
97 * with the XFS_SB_FEAT_INCOMPAT_META_UUID feature.
99 #define XFS_SB_FEAT_INCOMPAT_SUPPORTED \
100 (XFS_SB_FEAT_INCOMPAT_FTYPE | \
101 XFS_SB_FEAT_INCOMPAT_SPINODES | \
102 XFS_SB_FEAT_INCOMPAT_META_UUID | \
103 XFS_SB_FEAT_INCOMPAT_BIGTIME | \
104 XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
106 struct grub_xfs_sblock
108 grub_uint8_t magic[4];
109 grub_uint32_t bsize;
110 grub_uint8_t unused1[24];
111 grub_uint16_t uuid[8];
112 grub_uint8_t unused2[8];
113 grub_uint64_t rootino;
114 grub_uint8_t unused3[20];
115 grub_uint32_t agsize;
116 grub_uint8_t unused4[12];
117 grub_uint16_t version;
118 grub_uint8_t unused5[6];
119 grub_uint8_t label[12];
120 grub_uint8_t log2_bsize;
121 grub_uint8_t log2_sect;
122 grub_uint8_t log2_inode;
123 grub_uint8_t log2_inop;
124 grub_uint8_t log2_agblk;
125 grub_uint8_t unused6[67];
126 grub_uint8_t log2_dirblk;
127 grub_uint8_t unused7[7];
128 grub_uint32_t features2;
129 grub_uint8_t unused8[4];
130 grub_uint32_t sb_features_compat;
131 grub_uint32_t sb_features_ro_compat;
132 grub_uint32_t sb_features_incompat;
133 grub_uint32_t sb_features_log_incompat;
134 } GRUB_PACKED;
136 struct grub_xfs_dir_header
138 grub_uint8_t count;
139 grub_uint8_t largeino;
140 union
142 grub_uint32_t i4;
143 grub_uint64_t i8;
144 } GRUB_PACKED parent;
145 } GRUB_PACKED;
147 /* Structure for directory entry inlined in the inode */
148 struct grub_xfs_dir_entry
150 grub_uint8_t len;
151 grub_uint16_t offset;
152 char name[1];
153 /* Inode number follows, 32 / 64 bits. */
154 } GRUB_PACKED;
156 /* Structure for directory entry in a block */
157 struct grub_xfs_dir2_entry
159 grub_uint64_t inode;
160 grub_uint8_t len;
161 } GRUB_PACKED;
163 struct grub_xfs_extent
165 /* This should be a bitfield but bietfields are unportable, so just have
166 a raw array and functions extracting useful info from it.
168 grub_uint32_t raw[4];
169 } GRUB_PACKED;
171 struct grub_xfs_btree_node
173 grub_uint8_t magic[4];
174 grub_uint16_t level;
175 grub_uint16_t numrecs;
176 grub_uint64_t left;
177 grub_uint64_t right;
178 /* In V5 here follow crc, uuid, etc. */
179 /* Then follow keys and block pointers */
180 } GRUB_PACKED;
182 struct grub_xfs_btree_root
184 grub_uint16_t level;
185 grub_uint16_t numrecs;
186 grub_uint64_t keys[1];
187 } GRUB_PACKED;
189 struct grub_xfs_time_legacy
191 grub_uint32_t sec;
192 grub_uint32_t nanosec;
193 } GRUB_PACKED;
195 struct grub_xfs_inode
197 grub_uint8_t magic[2];
198 grub_uint16_t mode;
199 grub_uint8_t version;
200 grub_uint8_t format;
201 grub_uint8_t unused2[26];
202 grub_uint64_t atime;
203 grub_uint64_t mtime;
204 grub_uint64_t ctime;
205 grub_uint64_t size;
206 grub_uint64_t nblocks;
207 grub_uint32_t extsize;
208 grub_uint32_t nextents;
209 grub_uint16_t unused3;
210 grub_uint8_t fork_offset;
211 grub_uint8_t unused4[37];
212 grub_uint64_t flags2;
213 grub_uint8_t unused5[48];
214 } GRUB_PACKED;
216 #define XFS_V3_INODE_SIZE sizeof(struct grub_xfs_inode)
217 /* Size of struct grub_xfs_inode until fork_offset (included). */
218 #define XFS_V2_INODE_SIZE (XFS_V3_INODE_SIZE - 92)
220 struct grub_xfs_dirblock_tail
222 grub_uint32_t leaf_count;
223 grub_uint32_t leaf_stale;
224 } GRUB_PACKED;
226 struct grub_fshelp_node
228 struct grub_xfs_data *data;
229 grub_uint64_t ino;
230 int inode_read;
231 struct grub_xfs_inode inode;
234 struct grub_xfs_data
236 struct grub_xfs_sblock sblock;
237 grub_disk_t disk;
238 int pos;
239 int bsize;
240 grub_uint32_t agsize;
241 unsigned int hasftype:1;
242 unsigned int hascrc:1;
243 struct grub_fshelp_node diropen;
246 static grub_dl_t my_mod;
250 static int grub_xfs_sb_hascrc(struct grub_xfs_data *data)
252 return (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
253 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5);
256 static int grub_xfs_sb_hasftype(struct grub_xfs_data *data)
258 if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
259 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5) &&
260 data->sblock.sb_features_incompat & grub_cpu_to_be32_compile_time(XFS_SB_FEAT_INCOMPAT_FTYPE))
261 return 1;
262 if (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
263 data->sblock.features2 & grub_cpu_to_be32_compile_time(XFS_SB_VERSION2_FTYPE))
264 return 1;
265 return 0;
268 static int grub_xfs_sb_valid(struct grub_xfs_data *data)
270 grub_dprintf("xfs", "Validating superblock\n");
271 if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4)
272 || data->sblock.log2_bsize < GRUB_DISK_SECTOR_BITS
273 || ((int) data->sblock.log2_bsize
274 + (int) data->sblock.log2_dirblk) >= 27)
276 grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem");
277 return 0;
279 if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
280 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5))
282 grub_dprintf("xfs", "XFS v5 superblock detected\n");
283 if (data->sblock.sb_features_incompat &
284 grub_cpu_to_be32_compile_time(~XFS_SB_FEAT_INCOMPAT_SUPPORTED))
286 grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported "
287 "incompatible features");
288 return 0;
290 return 1;
292 else if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
293 grub_cpu_to_be16_compile_time(XFS_SB_VERSION_4))
295 grub_dprintf("xfs", "XFS v4 superblock detected\n");
296 if (!(data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_DIRV2BIT)))
298 grub_error (GRUB_ERR_BAD_FS, "XFS filesystem without V2 directories "
299 "is unsupported");
300 return 0;
302 if (data->sblock.version & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION_BITS_SUPPORTED) ||
303 (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
304 data->sblock.features2 & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION2_BITS_SUPPORTED)))
306 grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported version "
307 "bits");
308 return 0;
310 return 1;
312 return 0;
315 static int
316 grub_xfs_sb_needs_repair (struct grub_xfs_data *data)
318 return ((data->sblock.version &
319 grub_cpu_to_be16_compile_time (XFS_SB_VERSION_NUMBITS)) ==
320 grub_cpu_to_be16_compile_time (XFS_SB_VERSION_5) &&
321 (data->sblock.sb_features_incompat &
322 grub_cpu_to_be32_compile_time (XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)));
325 /* Filetype information as used in inodes. */
326 #define FILETYPE_INO_MASK 0170000
327 #define FILETYPE_INO_REG 0100000
328 #define FILETYPE_INO_DIRECTORY 0040000
329 #define FILETYPE_INO_SYMLINK 0120000
331 static inline int
332 GRUB_XFS_INO_AGBITS(struct grub_xfs_data *data)
334 return ((data)->sblock.log2_agblk + (data)->sblock.log2_inop);
337 static inline grub_uint64_t
338 GRUB_XFS_INO_INOINAG (struct grub_xfs_data *data,
339 grub_uint64_t ino)
341 return (ino & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1));
344 static inline grub_uint64_t
345 GRUB_XFS_INO_AG (struct grub_xfs_data *data,
346 grub_uint64_t ino)
348 return (ino >> GRUB_XFS_INO_AGBITS (data));
351 static inline grub_disk_addr_t
352 GRUB_XFS_FSB_TO_BLOCK (struct grub_xfs_data *data, grub_disk_addr_t fsb)
354 return ((fsb >> data->sblock.log2_agblk) * data->agsize
355 + (fsb & ((1LL << data->sblock.log2_agblk) - 1)));
358 static inline grub_uint64_t
359 GRUB_XFS_EXTENT_OFFSET (struct grub_xfs_extent *exts, int ex)
361 return ((grub_be_to_cpu32 (exts[ex].raw[0]) & ~(1 << 31)) << 23
362 | grub_be_to_cpu32 (exts[ex].raw[1]) >> 9);
365 static inline grub_uint64_t
366 GRUB_XFS_EXTENT_BLOCK (struct grub_xfs_extent *exts, int ex)
368 return ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex].raw[1])
369 & (0x1ff)) << 43
370 | (grub_uint64_t) grub_be_to_cpu32 (exts[ex].raw[2]) << 11
371 | grub_be_to_cpu32 (exts[ex].raw[3]) >> 21);
374 static inline grub_uint64_t
375 GRUB_XFS_EXTENT_SIZE (struct grub_xfs_extent *exts, int ex)
377 return (grub_be_to_cpu32 (exts[ex].raw[3]) & ((1 << 21) - 1));
381 static inline grub_uint64_t
382 grub_xfs_inode_block (struct grub_xfs_data *data,
383 grub_uint64_t ino)
385 long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
386 long long ag = GRUB_XFS_INO_AG (data, ino);
387 long long block;
389 block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
390 block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
391 return block;
395 static inline int
396 grub_xfs_inode_offset (struct grub_xfs_data *data,
397 grub_uint64_t ino)
399 int inoag = GRUB_XFS_INO_INOINAG (data, ino);
400 return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
401 data->sblock.log2_inode);
404 static inline grub_size_t
405 grub_xfs_inode_size(struct grub_xfs_data *data)
407 return (grub_size_t)1 << data->sblock.log2_inode;
411 * Returns size occupied by XFS inode stored in memory - we store struct
412 * grub_fshelp_node there but on disk inode size may be actually larger than
413 * struct grub_xfs_inode so we need to account for that so that we can read
414 * from disk directly into in-memory structure.
416 static inline grub_size_t
417 grub_xfs_fshelp_size(struct grub_xfs_data *data)
419 return sizeof (struct grub_fshelp_node) - sizeof (struct grub_xfs_inode)
420 + grub_xfs_inode_size(data);
423 /* This should return void * but XFS code is error-prone with alignment, so
424 return char to retain cast-align.
426 static char *
427 grub_xfs_inode_data(struct grub_xfs_inode *inode)
429 if (inode->version <= 2)
430 return ((char *)inode) + XFS_V2_INODE_SIZE;
431 return ((char *)inode) + XFS_V3_INODE_SIZE;
434 static struct grub_xfs_dir_entry *
435 grub_xfs_inline_de(struct grub_xfs_dir_header *head)
438 With small inode numbers the header is 4 bytes smaller because of
439 smaller parent pointer
441 return (struct grub_xfs_dir_entry *)
442 (((char *) head) + sizeof(struct grub_xfs_dir_header) -
443 (head->largeino ? 0 : sizeof(grub_uint32_t)));
446 static grub_uint8_t *
447 grub_xfs_inline_de_inopos(struct grub_xfs_data *data,
448 struct grub_xfs_dir_entry *de)
450 return ((grub_uint8_t *)(de + 1)) + de->len - 1 + (data->hasftype ? 1 : 0);
453 static struct grub_xfs_dir_entry *
454 grub_xfs_inline_next_de(struct grub_xfs_data *data,
455 struct grub_xfs_dir_header *head,
456 struct grub_xfs_dir_entry *de)
458 char *p = (char *)de + sizeof(struct grub_xfs_dir_entry) - 1 + de->len;
460 p += head->largeino ? sizeof(grub_uint64_t) : sizeof(grub_uint32_t);
461 if (data->hasftype)
462 p++;
464 return (struct grub_xfs_dir_entry *)p;
467 static struct grub_xfs_dirblock_tail *
468 grub_xfs_dir_tail(struct grub_xfs_data *data, void *dirblock)
470 int dirblksize = 1 << (data->sblock.log2_bsize + data->sblock.log2_dirblk);
472 return (struct grub_xfs_dirblock_tail *)
473 ((char *)dirblock + dirblksize - sizeof (struct grub_xfs_dirblock_tail));
476 static struct grub_xfs_dir2_entry *
477 grub_xfs_first_de(struct grub_xfs_data *data, void *dirblock)
479 if (data->hascrc)
480 return (struct grub_xfs_dir2_entry *)((char *)dirblock + 64);
481 return (struct grub_xfs_dir2_entry *)((char *)dirblock + 16);
484 static struct grub_xfs_dir2_entry *
485 grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
487 int size = sizeof (struct grub_xfs_dir2_entry) + de->len + 2 /* Tag */;
489 if (data->hasftype)
490 size++; /* File type */
491 return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
494 /* This should return void * but XFS code is error-prone with alignment, so
495 return char to retain cast-align.
497 static char *
498 grub_xfs_btree_keys(struct grub_xfs_data *data,
499 struct grub_xfs_btree_node *leaf)
501 char *keys = (char *)(leaf + 1);
503 if (data->hascrc)
504 keys += 48; /* skip crc, uuid, ... */
505 return keys;
508 static grub_err_t
509 grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
510 struct grub_xfs_inode *inode)
512 grub_uint64_t block = grub_xfs_inode_block (data, ino);
513 int offset = grub_xfs_inode_offset (data, ino);
515 grub_dprintf("xfs", "Reading inode (%" PRIuGRUB_UINT64_T ") - %" PRIuGRUB_UINT64_T ", %d\n",
516 ino, block, offset);
517 /* Read the inode. */
518 if (grub_disk_read (data->disk, block, offset, grub_xfs_inode_size(data),
519 inode))
520 return grub_errno;
522 if (grub_strncmp ((char *) inode->magic, "IN", 2))
523 return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
525 return 0;
528 static grub_uint64_t
529 get_fsb (const void *keys, int idx)
531 const char *p = (const char *) keys + sizeof(grub_uint64_t) * idx;
532 return grub_be_to_cpu64 (grub_get_unaligned64 (p));
535 static grub_disk_addr_t
536 grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
538 struct grub_xfs_btree_node *leaf = 0;
539 int ex, nrec;
540 struct grub_xfs_extent *exts;
541 grub_uint64_t ret = 0;
543 if (node->inode.format == XFS_INODE_FORMAT_BTREE)
545 struct grub_xfs_btree_root *root;
546 const char *keys;
547 int recoffset;
549 leaf = grub_malloc (node->data->bsize);
550 if (leaf == 0)
551 return 0;
553 root = (struct grub_xfs_btree_root *) grub_xfs_inode_data(&node->inode);
554 nrec = grub_be_to_cpu16 (root->numrecs);
555 keys = (char *) &root->keys[0];
556 if (node->inode.fork_offset)
557 recoffset = (node->inode.fork_offset - 1) / 2;
558 else
559 recoffset = (grub_xfs_inode_size(node->data)
560 - ((char *) keys - (char *) &node->inode))
561 / (2 * sizeof (grub_uint64_t));
564 int i;
566 for (i = 0; i < nrec; i++)
568 if (fileblock < get_fsb(keys, i))
569 break;
572 /* Sparse block. */
573 if (i == 0)
575 grub_free (leaf);
576 return 0;
579 if (grub_disk_read (node->data->disk,
580 GRUB_XFS_FSB_TO_BLOCK (node->data, get_fsb (keys, i - 1 + recoffset)) << (node->data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS),
581 0, node->data->bsize, leaf))
582 return 0;
584 if ((!node->data->hascrc &&
585 grub_strncmp ((char *) leaf->magic, "BMAP", 4)) ||
586 (node->data->hascrc &&
587 grub_strncmp ((char *) leaf->magic, "BMA3", 4)))
589 grub_free (leaf);
590 grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
591 return 0;
594 nrec = grub_be_to_cpu16 (leaf->numrecs);
595 keys = grub_xfs_btree_keys(node->data, leaf);
596 recoffset = ((node->data->bsize - ((char *) keys
597 - (char *) leaf))
598 / (2 * sizeof (grub_uint64_t)));
600 while (leaf->level);
601 exts = (struct grub_xfs_extent *) keys;
603 else if (node->inode.format == XFS_INODE_FORMAT_EXT)
605 nrec = grub_be_to_cpu32 (node->inode.nextents);
606 exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode);
608 else
610 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
611 "XFS does not support inode format %d yet",
612 node->inode.format);
613 return 0;
616 /* Iterate over each extent to figure out which extent has
617 the block we are looking for. */
618 for (ex = 0; ex < nrec; ex++)
620 grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
621 grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
622 grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
624 /* Sparse block. */
625 if (fileblock < offset)
626 break;
627 else if (fileblock < offset + size)
629 ret = (fileblock - offset + start);
630 break;
634 grub_free (leaf);
636 return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
640 /* Read LEN bytes from the file described by DATA starting with byte
641 POS. Return the amount of read bytes in READ. */
642 static grub_ssize_t
643 grub_xfs_read_file (grub_fshelp_node_t node,
644 grub_disk_read_hook_t read_hook, void *read_hook_data,
645 grub_off_t pos, grub_size_t len, char *buf, grub_uint32_t header_size)
647 return grub_fshelp_read_file (node->data->disk, node,
648 read_hook, read_hook_data,
649 pos, len, buf, grub_xfs_read_block,
650 grub_be_to_cpu64 (node->inode.size) + header_size,
651 node->data->sblock.log2_bsize
652 - GRUB_DISK_SECTOR_BITS, 0);
656 static char *
657 grub_xfs_read_symlink (grub_fshelp_node_t node)
659 grub_ssize_t size = grub_be_to_cpu64 (node->inode.size);
661 if (size < 0)
663 grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
664 return 0;
667 switch (node->inode.format)
669 case XFS_INODE_FORMAT_INO:
670 return grub_strndup (grub_xfs_inode_data(&node->inode), size);
672 case XFS_INODE_FORMAT_EXT:
674 char *symlink;
675 grub_ssize_t numread;
676 int off = 0;
678 if (node->data->hascrc)
679 off = 56;
681 symlink = grub_malloc (size + 1);
682 if (!symlink)
683 return 0;
685 node->inode.size = grub_be_to_cpu64 (size + off);
686 numread = grub_xfs_read_file (node, 0, 0, off, size, symlink, off);
687 if (numread != size)
689 grub_free (symlink);
690 return 0;
692 symlink[size] = '\0';
693 return symlink;
697 return 0;
701 static enum grub_fshelp_filetype
702 grub_xfs_mode_to_filetype (grub_uint16_t mode)
704 if ((grub_be_to_cpu16 (mode)
705 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
706 return GRUB_FSHELP_DIR;
707 else if ((grub_be_to_cpu16 (mode)
708 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
709 return GRUB_FSHELP_SYMLINK;
710 else if ((grub_be_to_cpu16 (mode)
711 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
712 return GRUB_FSHELP_REG;
713 return GRUB_FSHELP_UNKNOWN;
717 /* Context for grub_xfs_iterate_dir. */
718 struct grub_xfs_iterate_dir_ctx
720 grub_fshelp_iterate_dir_hook_t hook;
721 void *hook_data;
722 struct grub_fshelp_node *diro;
725 /* Helper for grub_xfs_iterate_dir. */
726 static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename,
727 struct grub_xfs_iterate_dir_ctx *ctx)
729 struct grub_fshelp_node *fdiro;
730 grub_err_t err;
732 fdiro = grub_malloc (grub_xfs_fshelp_size(ctx->diro->data) + 1);
733 if (!fdiro)
735 grub_print_error ();
736 return 0;
739 /* The inode should be read, otherwise the filetype can
740 not be determined. */
741 fdiro->ino = ino;
742 fdiro->inode_read = 1;
743 fdiro->data = ctx->diro->data;
744 err = grub_xfs_read_inode (ctx->diro->data, ino, &fdiro->inode);
745 if (err)
747 grub_print_error ();
748 return 0;
751 return ctx->hook (filename, grub_xfs_mode_to_filetype (fdiro->inode.mode),
752 fdiro, ctx->hook_data);
755 static int
756 grub_xfs_iterate_dir (grub_fshelp_node_t dir,
757 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
759 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
760 struct grub_xfs_iterate_dir_ctx ctx = {
761 .hook = hook,
762 .hook_data = hook_data,
763 .diro = diro
766 switch (diro->inode.format)
768 case XFS_INODE_FORMAT_INO:
770 struct grub_xfs_dir_header *head = (struct grub_xfs_dir_header *) grub_xfs_inode_data(&diro->inode);
771 struct grub_xfs_dir_entry *de = grub_xfs_inline_de(head);
772 int smallino = !head->largeino;
773 int i;
774 grub_uint64_t parent;
776 /* If small inode numbers are used to pack the direntry, the
777 parent inode number is small too. */
778 if (smallino)
779 parent = grub_be_to_cpu32 (head->parent.i4);
780 else
781 parent = grub_be_to_cpu64 (head->parent.i8);
783 /* Synthesize the direntries for `.' and `..'. */
784 if (iterate_dir_call_hook (diro->ino, ".", &ctx))
785 return 1;
787 if (iterate_dir_call_hook (parent, "..", &ctx))
788 return 1;
790 for (i = 0; i < head->count; i++)
792 grub_uint64_t ino;
793 grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de);
794 grub_uint8_t c;
796 /* inopos might be unaligned. */
797 if (smallino)
798 ino = (((grub_uint32_t) inopos[0]) << 24)
799 | (((grub_uint32_t) inopos[1]) << 16)
800 | (((grub_uint32_t) inopos[2]) << 8)
801 | (((grub_uint32_t) inopos[3]) << 0);
802 else
803 ino = (((grub_uint64_t) inopos[0]) << 56)
804 | (((grub_uint64_t) inopos[1]) << 48)
805 | (((grub_uint64_t) inopos[2]) << 40)
806 | (((grub_uint64_t) inopos[3]) << 32)
807 | (((grub_uint64_t) inopos[4]) << 24)
808 | (((grub_uint64_t) inopos[5]) << 16)
809 | (((grub_uint64_t) inopos[6]) << 8)
810 | (((grub_uint64_t) inopos[7]) << 0);
812 c = de->name[de->len];
813 de->name[de->len] = '\0';
814 if (iterate_dir_call_hook (ino, de->name, &ctx))
816 de->name[de->len] = c;
817 return 1;
819 de->name[de->len] = c;
821 de = grub_xfs_inline_next_de(dir->data, head, de);
823 break;
826 case XFS_INODE_FORMAT_BTREE:
827 case XFS_INODE_FORMAT_EXT:
829 grub_ssize_t numread;
830 char *dirblock;
831 grub_uint64_t blk;
832 int dirblk_size, dirblk_log2;
834 dirblk_log2 = (dir->data->sblock.log2_bsize
835 + dir->data->sblock.log2_dirblk);
836 dirblk_size = 1 << dirblk_log2;
838 dirblock = grub_malloc (dirblk_size);
839 if (! dirblock)
840 return 0;
842 /* Iterate over every block the directory has. */
843 for (blk = 0;
844 blk < (grub_be_to_cpu64 (dir->inode.size)
845 >> dirblk_log2);
846 blk++)
848 struct grub_xfs_dir2_entry *direntry =
849 grub_xfs_first_de(dir->data, dirblock);
850 int entries;
851 struct grub_xfs_dirblock_tail *tail =
852 grub_xfs_dir_tail(dir->data, dirblock);
854 numread = grub_xfs_read_file (dir, 0, 0,
855 blk << dirblk_log2,
856 dirblk_size, dirblock, 0);
857 if (numread != dirblk_size)
858 return 0;
860 entries = (grub_be_to_cpu32 (tail->leaf_count)
861 - grub_be_to_cpu32 (tail->leaf_stale));
863 if (!entries)
864 continue;
866 /* Iterate over all entries within this block. */
867 while ((char *)direntry < (char *)tail)
869 grub_uint8_t *freetag;
870 char *filename;
872 freetag = (grub_uint8_t *) direntry;
874 if (grub_get_unaligned16 (freetag) == 0XFFFF)
876 grub_uint8_t *skip = (freetag + sizeof (grub_uint16_t));
878 /* This entry is not used, go to the next one. */
879 direntry = (struct grub_xfs_dir2_entry *)
880 (((char *)direntry) +
881 grub_be_to_cpu16 (grub_get_unaligned16 (skip)));
883 continue;
886 filename = (char *)(direntry + 1);
887 /* The byte after the filename is for the filetype, padding, or
888 tag, which is not used by GRUB. So it can be overwritten. */
889 filename[direntry->len] = '\0';
891 if (iterate_dir_call_hook (grub_be_to_cpu64(direntry->inode),
892 filename, &ctx))
894 grub_free (dirblock);
895 return 1;
898 /* Check if last direntry in this block is
899 reached. */
900 entries--;
901 if (!entries)
902 break;
904 /* Select the next directory entry. */
905 direntry = grub_xfs_next_de(dir->data, direntry);
908 grub_free (dirblock);
909 break;
912 default:
913 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
914 "XFS does not support inode format %d yet",
915 diro->inode.format);
917 return 0;
921 static struct grub_xfs_data *
922 grub_xfs_mount (grub_disk_t disk)
924 struct grub_xfs_data *data = 0;
925 grub_size_t sz;
927 data = grub_zalloc (sizeof (struct grub_xfs_data));
928 if (!data)
929 return 0;
931 grub_dprintf("xfs", "Reading sb\n");
932 /* Read the superblock. */
933 if (grub_disk_read (disk, 0, 0,
934 sizeof (struct grub_xfs_sblock), &data->sblock))
935 goto fail;
937 if (!grub_xfs_sb_valid(data))
938 goto fail;
940 if (grub_xfs_sb_needs_repair (data))
941 grub_dprintf ("xfs", "XFS filesystem needs repair, boot may fail\n");
943 if (grub_add (grub_xfs_inode_size (data),
944 sizeof (struct grub_xfs_data) - sizeof (struct grub_xfs_inode) + 1, &sz))
945 goto fail;
947 data = grub_realloc (data, sz);
949 if (! data)
950 goto fail;
952 data->diropen.data = data;
953 data->diropen.ino = grub_be_to_cpu64(data->sblock.rootino);
954 data->diropen.inode_read = 1;
955 data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
956 data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
957 data->hasftype = grub_xfs_sb_hasftype(data);
958 data->hascrc = grub_xfs_sb_hascrc(data);
960 data->disk = disk;
961 data->pos = 0;
962 grub_dprintf("xfs", "Reading root ino %" PRIuGRUB_UINT64_T "\n",
963 grub_cpu_to_be64(data->sblock.rootino));
965 grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
967 return data;
968 fail:
970 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
971 grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
973 grub_free (data);
975 return 0;
979 /* Context for grub_xfs_dir. */
980 struct grub_xfs_dir_ctx
982 grub_fs_dir_hook_t hook;
983 void *hook_data;
986 /* Bigtime inodes helpers. */
987 #define XFS_BIGTIME_EPOCH_OFFSET (-(grub_int64_t) GRUB_INT32_MIN)
989 static int grub_xfs_inode_has_bigtime (const struct grub_xfs_inode *inode)
991 return inode->version >= 3 &&
992 (inode->flags2 & grub_cpu_to_be64_compile_time (XFS_DIFLAG2_BIGTIME));
995 static grub_int64_t
996 grub_xfs_get_inode_time (struct grub_xfs_inode *inode)
998 struct grub_xfs_time_legacy *lts;
1000 if (grub_xfs_inode_has_bigtime (inode))
1001 return grub_divmod64 (grub_be_to_cpu64 (inode->mtime), NSEC_PER_SEC, NULL) - XFS_BIGTIME_EPOCH_OFFSET;
1003 lts = (struct grub_xfs_time_legacy *) &inode->mtime;
1004 return grub_be_to_cpu32 (lts->sec);
1007 /* Helper for grub_xfs_dir. */
1008 static int
1009 grub_xfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
1010 grub_fshelp_node_t node, void *data)
1012 struct grub_xfs_dir_ctx *ctx = data;
1013 struct grub_dirhook_info info;
1015 grub_memset (&info, 0, sizeof (info));
1016 if (node->inode_read)
1018 info.mtimeset = 1;
1019 info.mtime = grub_xfs_get_inode_time (&node->inode);
1021 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
1022 grub_free (node);
1023 return ctx->hook (filename, &info, ctx->hook_data);
1026 static grub_err_t
1027 grub_xfs_dir (grub_device_t device, const char *path,
1028 grub_fs_dir_hook_t hook, void *hook_data)
1030 struct grub_xfs_dir_ctx ctx = { hook, hook_data };
1031 struct grub_xfs_data *data = 0;
1032 struct grub_fshelp_node *fdiro = 0;
1034 grub_dl_ref (my_mod);
1036 data = grub_xfs_mount (device->disk);
1037 if (!data)
1038 goto mount_fail;
1040 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
1041 grub_xfs_read_symlink, GRUB_FSHELP_DIR);
1042 if (grub_errno)
1043 goto fail;
1045 grub_xfs_iterate_dir (fdiro, grub_xfs_dir_iter, &ctx);
1047 fail:
1048 if (fdiro != &data->diropen)
1049 grub_free (fdiro);
1050 grub_free (data);
1052 mount_fail:
1054 grub_dl_unref (my_mod);
1056 return grub_errno;
1060 /* Open a file named NAME and initialize FILE. */
1061 static grub_err_t
1062 grub_xfs_open (struct grub_file *file, const char *name)
1064 struct grub_xfs_data *data;
1065 struct grub_fshelp_node *fdiro = 0;
1067 grub_dl_ref (my_mod);
1069 data = grub_xfs_mount (file->device->disk);
1070 if (!data)
1071 goto mount_fail;
1073 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
1074 grub_xfs_read_symlink, GRUB_FSHELP_REG);
1075 if (grub_errno)
1076 goto fail;
1078 if (!fdiro->inode_read)
1080 grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
1081 if (grub_errno)
1082 goto fail;
1085 if (fdiro != &data->diropen)
1087 grub_memcpy (&data->diropen, fdiro, grub_xfs_fshelp_size(data));
1088 grub_free (fdiro);
1091 file->size = grub_be_to_cpu64 (data->diropen.inode.size);
1092 file->data = data;
1093 file->offset = 0;
1095 return 0;
1097 fail:
1098 if (fdiro != &data->diropen)
1099 grub_free (fdiro);
1100 grub_free (data);
1102 mount_fail:
1103 grub_dl_unref (my_mod);
1105 return grub_errno;
1109 static grub_ssize_t
1110 grub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
1112 struct grub_xfs_data *data =
1113 (struct grub_xfs_data *) file->data;
1115 return grub_xfs_read_file (&data->diropen,
1116 file->read_hook, file->read_hook_data,
1117 file->offset, len, buf, 0);
1121 static grub_err_t
1122 grub_xfs_close (grub_file_t file)
1124 grub_free (file->data);
1126 grub_dl_unref (my_mod);
1128 return GRUB_ERR_NONE;
1132 static grub_err_t
1133 grub_xfs_label (grub_device_t device, char **label)
1135 struct grub_xfs_data *data;
1136 grub_disk_t disk = device->disk;
1138 grub_dl_ref (my_mod);
1140 data = grub_xfs_mount (disk);
1141 if (data)
1142 *label = grub_strndup ((char *) (data->sblock.label), 12);
1143 else
1144 *label = 0;
1146 grub_dl_unref (my_mod);
1148 grub_free (data);
1150 return grub_errno;
1153 static grub_err_t
1154 grub_xfs_uuid (grub_device_t device, char **uuid)
1156 struct grub_xfs_data *data;
1157 grub_disk_t disk = device->disk;
1159 grub_dl_ref (my_mod);
1161 data = grub_xfs_mount (disk);
1162 if (data)
1164 *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
1165 grub_be_to_cpu16 (data->sblock.uuid[0]),
1166 grub_be_to_cpu16 (data->sblock.uuid[1]),
1167 grub_be_to_cpu16 (data->sblock.uuid[2]),
1168 grub_be_to_cpu16 (data->sblock.uuid[3]),
1169 grub_be_to_cpu16 (data->sblock.uuid[4]),
1170 grub_be_to_cpu16 (data->sblock.uuid[5]),
1171 grub_be_to_cpu16 (data->sblock.uuid[6]),
1172 grub_be_to_cpu16 (data->sblock.uuid[7]));
1174 else
1175 *uuid = NULL;
1177 grub_dl_unref (my_mod);
1179 grub_free (data);
1181 return grub_errno;
1186 static struct grub_fs grub_xfs_fs =
1188 .name = "xfs",
1189 .fs_dir = grub_xfs_dir,
1190 .fs_open = grub_xfs_open,
1191 .fs_read = grub_xfs_read,
1192 .fs_close = grub_xfs_close,
1193 .fs_label = grub_xfs_label,
1194 .fs_uuid = grub_xfs_uuid,
1195 #ifdef GRUB_UTIL
1196 .reserved_first_sector = 0,
1197 .blocklist_install = 1,
1198 #endif
1199 .next = 0
1202 GRUB_MOD_INIT(xfs)
1204 grub_fs_register (&grub_xfs_fs);
1205 my_mod = mod;
1208 GRUB_MOD_FINI(xfs)
1210 grub_fs_unregister (&grub_xfs_fs);