Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / fs / xfs.c
blob2c6b00c2af197224001c2aa26538f831e25af958
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/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
38 struct grub_xfs_sblock
40 grub_uint8_t magic[4];
41 grub_uint32_t bsize;
42 grub_uint8_t unused1[24];
43 grub_uint16_t uuid[8];
44 grub_uint8_t unused2[8];
45 grub_uint64_t rootino;
46 grub_uint8_t unused3[20];
47 grub_uint32_t agsize;
48 grub_uint8_t unused4[20];
49 grub_uint8_t label[12];
50 grub_uint8_t log2_bsize;
51 grub_uint8_t log2_sect;
52 grub_uint8_t log2_inode;
53 grub_uint8_t log2_inop;
54 grub_uint8_t log2_agblk;
55 grub_uint8_t unused6[67];
56 grub_uint8_t log2_dirblk;
57 } __attribute__ ((packed));
59 struct grub_xfs_dir_header
61 grub_uint8_t count;
62 grub_uint8_t smallino;
63 union
65 grub_uint32_t i4;
66 grub_uint64_t i8;
67 } parent __attribute__ ((packed));
68 } __attribute__ ((packed));
70 struct grub_xfs_dir_entry
72 grub_uint8_t len;
73 grub_uint16_t offset;
74 char name[1];
75 /* Inode number follows, 32 bits. */
76 } __attribute__ ((packed));
78 struct grub_xfs_dir2_entry
80 grub_uint64_t inode;
81 grub_uint8_t len;
82 } __attribute__ ((packed));
84 typedef grub_uint32_t grub_xfs_extent[4];
86 struct grub_xfs_btree_node
88 grub_uint8_t magic[4];
89 grub_uint16_t level;
90 grub_uint16_t numrecs;
91 grub_uint64_t left;
92 grub_uint64_t right;
93 grub_uint64_t keys[1];
94 } __attribute__ ((packed));
96 struct grub_xfs_btree_root
98 grub_uint16_t level;
99 grub_uint16_t numrecs;
100 grub_uint64_t keys[1];
101 } __attribute__ ((packed));
103 struct grub_xfs_time
105 grub_uint32_t sec;
106 grub_uint32_t nanosec;
107 } __attribute__ ((packed));
109 struct grub_xfs_inode
111 grub_uint8_t magic[2];
112 grub_uint16_t mode;
113 grub_uint8_t version;
114 grub_uint8_t format;
115 grub_uint8_t unused2[26];
116 struct grub_xfs_time atime;
117 struct grub_xfs_time mtime;
118 struct grub_xfs_time ctime;
119 grub_uint64_t size;
120 grub_uint64_t nblocks;
121 grub_uint32_t extsize;
122 grub_uint32_t nextents;
123 grub_uint16_t unused3;
124 grub_uint8_t fork_offset;
125 grub_uint8_t unused4[17];
126 union
128 char raw[156];
129 struct dir
131 struct grub_xfs_dir_header dirhead;
132 struct grub_xfs_dir_entry direntry[1];
133 } dir;
134 grub_xfs_extent extents[XFS_INODE_EXTENTS];
135 struct grub_xfs_btree_root btree;
136 } data __attribute__ ((packed));
137 } __attribute__ ((packed));
139 struct grub_xfs_dirblock_tail
141 grub_uint32_t leaf_count;
142 grub_uint32_t leaf_stale;
143 } __attribute__ ((packed));
145 struct grub_fshelp_node
147 struct grub_xfs_data *data;
148 grub_uint64_t ino;
149 int inode_read;
150 struct grub_xfs_inode inode;
153 struct grub_xfs_data
155 struct grub_xfs_sblock sblock;
156 grub_disk_t disk;
157 int pos;
158 int bsize;
159 grub_uint32_t agsize;
160 struct grub_fshelp_node diropen;
163 static grub_dl_t my_mod;
167 /* Filetype information as used in inodes. */
168 #define FILETYPE_INO_MASK 0170000
169 #define FILETYPE_INO_REG 0100000
170 #define FILETYPE_INO_DIRECTORY 0040000
171 #define FILETYPE_INO_SYMLINK 0120000
173 static inline int
174 GRUB_XFS_INO_AGBITS(struct grub_xfs_data *data)
176 return ((data)->sblock.log2_agblk + (data)->sblock.log2_inop);
179 static inline grub_uint64_t
180 GRUB_XFS_INO_INOINAG (struct grub_xfs_data *data,
181 grub_uint64_t ino)
183 return (grub_be_to_cpu64 (ino) & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1));
186 static inline grub_uint64_t
187 GRUB_XFS_INO_AG (struct grub_xfs_data *data,
188 grub_uint64_t ino)
190 return (grub_be_to_cpu64 (ino) >> GRUB_XFS_INO_AGBITS (data));
193 static inline grub_disk_addr_t
194 GRUB_XFS_FSB_TO_BLOCK (struct grub_xfs_data *data, grub_disk_addr_t fsb)
196 return ((fsb >> data->sblock.log2_agblk) * data->agsize
197 + (fsb & ((1LL << data->sblock.log2_agblk) - 1)));
200 static inline grub_uint64_t
201 GRUB_XFS_EXTENT_OFFSET (grub_xfs_extent *exts, int ex)
203 return ((grub_be_to_cpu32 (exts[ex][0]) & ~(1 << 31)) << 23
204 | grub_be_to_cpu32 (exts[ex][1]) >> 9);
207 static inline grub_uint64_t
208 GRUB_XFS_EXTENT_BLOCK (grub_xfs_extent *exts, int ex)
210 return ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex][1])
211 & (0x1ff)) << 43
212 | (grub_uint64_t) grub_be_to_cpu32 (exts[ex][2]) << 11
213 | grub_be_to_cpu32 (exts[ex][3]) >> 21);
216 static inline grub_uint64_t
217 GRUB_XFS_EXTENT_SIZE (grub_xfs_extent *exts, int ex)
219 return (grub_be_to_cpu32 (exts[ex][3]) & ((1 << 21) - 1));
222 static inline int
223 GRUB_XFS_ROUND_TO_DIRENT (int pos)
225 return ((((pos) + 8 - 1) / 8) * 8);
228 static inline int
229 GRUB_XFS_NEXT_DIRENT (int pos, int len)
231 return (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2);
235 static inline grub_uint64_t
236 grub_xfs_inode_block (struct grub_xfs_data *data,
237 grub_uint64_t ino)
239 long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
240 long long ag = GRUB_XFS_INO_AG (data, ino);
241 long long block;
243 block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
244 block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
245 return block;
249 static inline int
250 grub_xfs_inode_offset (struct grub_xfs_data *data,
251 grub_uint64_t ino)
253 int inoag = GRUB_XFS_INO_INOINAG (data, ino);
254 return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
255 data->sblock.log2_inode);
259 static grub_err_t
260 grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
261 struct grub_xfs_inode *inode)
263 grub_uint64_t block = grub_xfs_inode_block (data, ino);
264 int offset = grub_xfs_inode_offset (data, ino);
266 /* Read the inode. */
267 if (grub_disk_read (data->disk, block, offset,
268 1 << data->sblock.log2_inode, inode))
269 return grub_errno;
271 if (grub_strncmp ((char *) inode->magic, "IN", 2))
272 return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
274 return 0;
278 static grub_disk_addr_t
279 grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
281 struct grub_xfs_btree_node *leaf = 0;
282 int ex, nrec;
283 grub_xfs_extent *exts;
284 grub_uint64_t ret = 0;
286 if (node->inode.format == XFS_INODE_FORMAT_BTREE)
288 grub_uint64_t *keys;
289 int recoffset;
291 leaf = grub_malloc (node->data->bsize);
292 if (leaf == 0)
293 return 0;
295 nrec = grub_be_to_cpu16 (node->inode.data.btree.numrecs);
296 keys = &node->inode.data.btree.keys[0];
297 if (node->inode.fork_offset)
298 recoffset = (node->inode.fork_offset
299 - ((char *) &node->inode.data.btree.keys - (char *) &node->inode))
300 / (2 * sizeof (grub_uint64_t));
301 else
302 recoffset = ((1 << node->data->sblock.log2_inode)
303 - ((char *) &node->inode.data.btree.keys
304 - (char *) &node->inode))
305 / (2 * sizeof (grub_uint64_t));
308 int i;
310 for (i = 0; i < nrec; i++)
312 if (fileblock < grub_be_to_cpu64 (keys[i]))
313 break;
316 /* Sparse block. */
317 if (i == 0)
319 grub_free (leaf);
320 return 0;
322 if (grub_disk_read (node->data->disk,
323 GRUB_XFS_FSB_TO_BLOCK (node->data, grub_be_to_cpu64 (keys[i - 1 + recoffset])) << (node->data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS),
324 0, node->data->bsize, leaf))
325 return 0;
327 if (grub_strncmp ((char *) leaf->magic, "BMAP", 4))
329 grub_free (leaf);
330 grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
331 return 0;
334 nrec = grub_be_to_cpu16 (leaf->numrecs);
335 keys = &leaf->keys[0];
336 recoffset = ((node->data->bsize - ((char *) &leaf->keys
337 - (char *) leaf))
338 / (2 * sizeof (grub_uint64_t)));
340 while (leaf->level);
341 exts = (grub_xfs_extent *) keys;
343 else if (node->inode.format == XFS_INODE_FORMAT_EXT)
345 nrec = grub_be_to_cpu32 (node->inode.nextents);
346 exts = &node->inode.data.extents[0];
348 else
350 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
351 "XFS does not support inode format %d yet",
352 node->inode.format);
353 return 0;
356 /* Iterate over each extent to figure out which extent has
357 the block we are looking for. */
358 for (ex = 0; ex < nrec; ex++)
360 grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
361 grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
362 grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
364 /* Sparse block. */
365 if (fileblock < offset)
366 break;
367 else if (fileblock < offset + size)
369 ret = (fileblock - offset + start);
370 break;
374 grub_free (leaf);
376 return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
380 /* Read LEN bytes from the file described by DATA starting with byte
381 POS. Return the amount of read bytes in READ. */
382 static grub_ssize_t
383 grub_xfs_read_file (grub_fshelp_node_t node,
384 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
385 unsigned offset, unsigned length),
386 grub_off_t pos, grub_size_t len, char *buf)
388 return grub_fshelp_read_file (node->data->disk, node, read_hook,
389 pos, len, buf, grub_xfs_read_block,
390 grub_be_to_cpu64 (node->inode.size),
391 node->data->sblock.log2_bsize
392 - GRUB_DISK_SECTOR_BITS, 0);
396 static char *
397 grub_xfs_read_symlink (grub_fshelp_node_t node)
399 int size = grub_be_to_cpu64 (node->inode.size);
401 switch (node->inode.format)
403 case XFS_INODE_FORMAT_INO:
404 return grub_strndup (node->inode.data.raw, size);
406 case XFS_INODE_FORMAT_EXT:
408 char *symlink;
409 grub_ssize_t numread;
411 symlink = grub_malloc (size + 1);
412 if (!symlink)
413 return 0;
415 numread = grub_xfs_read_file (node, 0, 0, size, symlink);
416 if (numread != size)
418 grub_free (symlink);
419 return 0;
421 symlink[size] = '\0';
422 return symlink;
426 return 0;
430 static enum grub_fshelp_filetype
431 grub_xfs_mode_to_filetype (grub_uint16_t mode)
433 if ((grub_be_to_cpu16 (mode)
434 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
435 return GRUB_FSHELP_DIR;
436 else if ((grub_be_to_cpu16 (mode)
437 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
438 return GRUB_FSHELP_SYMLINK;
439 else if ((grub_be_to_cpu16 (mode)
440 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
441 return GRUB_FSHELP_REG;
442 return GRUB_FSHELP_UNKNOWN;
446 static int
447 grub_xfs_iterate_dir (grub_fshelp_node_t dir,
448 int NESTED_FUNC_ATTR
449 (*hook) (const char *filename,
450 enum grub_fshelp_filetype filetype,
451 grub_fshelp_node_t node))
453 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
454 auto int NESTED_FUNC_ATTR call_hook (grub_uint64_t ino, const char *filename);
456 int NESTED_FUNC_ATTR call_hook (grub_uint64_t ino, const char *filename)
458 struct grub_fshelp_node *fdiro;
459 grub_err_t err;
461 fdiro = grub_malloc (sizeof (struct grub_fshelp_node)
462 - sizeof (struct grub_xfs_inode)
463 + (1 << diro->data->sblock.log2_inode));
464 if (!fdiro)
466 grub_print_error ();
467 return 0;
470 /* The inode should be read, otherwise the filetype can
471 not be determined. */
472 fdiro->ino = ino;
473 fdiro->inode_read = 1;
474 fdiro->data = diro->data;
475 err = grub_xfs_read_inode (diro->data, ino, &fdiro->inode);
476 if (err)
478 grub_print_error ();
479 return 0;
482 return hook (filename,
483 grub_xfs_mode_to_filetype (fdiro->inode.mode),
484 fdiro);
487 switch (diro->inode.format)
489 case XFS_INODE_FORMAT_INO:
491 struct grub_xfs_dir_entry *de = &diro->inode.data.dir.direntry[0];
492 int smallino = !diro->inode.data.dir.dirhead.smallino;
493 int i;
494 grub_uint64_t parent;
496 /* If small inode numbers are used to pack the direntry, the
497 parent inode number is small too. */
498 if (smallino)
500 parent = grub_be_to_cpu32 (diro->inode.data.dir.dirhead.parent.i4);
501 parent = grub_cpu_to_be64 (parent);
502 /* The header is a bit smaller than usual. */
503 de = (struct grub_xfs_dir_entry *) ((char *) de - 4);
505 else
507 parent = diro->inode.data.dir.dirhead.parent.i8;
510 /* Synthesize the direntries for `.' and `..'. */
511 if (call_hook (diro->ino, "."))
512 return 1;
514 if (call_hook (parent, ".."))
515 return 1;
517 for (i = 0; i < diro->inode.data.dir.dirhead.count; i++)
519 grub_uint64_t ino;
520 grub_uint8_t *inopos = (((grub_uint8_t *) de)
521 + sizeof (struct grub_xfs_dir_entry)
522 + de->len - 1);
523 char name[de->len + 1];
525 /* inopos might be unaligned. */
526 if (smallino)
527 ino = (((grub_uint32_t) inopos[0]) << 24)
528 | (((grub_uint32_t) inopos[1]) << 16)
529 | (((grub_uint32_t) inopos[2]) << 8)
530 | (((grub_uint32_t) inopos[3]) << 0);
531 else
532 ino = (((grub_uint64_t) inopos[0]) << 56)
533 | (((grub_uint64_t) inopos[1]) << 48)
534 | (((grub_uint64_t) inopos[2]) << 40)
535 | (((grub_uint64_t) inopos[3]) << 32)
536 | (((grub_uint64_t) inopos[4]) << 24)
537 | (((grub_uint64_t) inopos[5]) << 16)
538 | (((grub_uint64_t) inopos[6]) << 8)
539 | (((grub_uint64_t) inopos[7]) << 0);
540 ino = grub_cpu_to_be64 (ino);
542 grub_memcpy (name, de->name, de->len);
543 name[de->len] = '\0';
544 if (call_hook (ino, name))
545 return 1;
547 de = ((struct grub_xfs_dir_entry *)
548 (((char *) de)+ sizeof (struct grub_xfs_dir_entry) + de->len
549 + ((smallino ? sizeof (grub_uint32_t)
550 : sizeof (grub_uint64_t))) - 1));
552 break;
555 case XFS_INODE_FORMAT_BTREE:
556 case XFS_INODE_FORMAT_EXT:
558 grub_ssize_t numread;
559 char *dirblock;
560 grub_uint64_t blk;
561 int dirblk_size, dirblk_log2;
563 dirblk_log2 = (dir->data->sblock.log2_bsize
564 + dir->data->sblock.log2_dirblk);
565 dirblk_size = 1 << dirblk_log2;
567 dirblock = grub_malloc (dirblk_size);
568 if (! dirblock)
569 return 0;
571 /* Iterate over every block the directory has. */
572 for (blk = 0;
573 blk < (grub_be_to_cpu64 (dir->inode.size)
574 >> dirblk_log2);
575 blk++)
577 /* The header is skipped, the first direntry is stored
578 from byte 16. */
579 int pos = 16;
580 int entries;
581 int tail_start = (dirblk_size
582 - sizeof (struct grub_xfs_dirblock_tail));
584 struct grub_xfs_dirblock_tail *tail;
585 tail = (struct grub_xfs_dirblock_tail *) &dirblock[tail_start];
587 numread = grub_xfs_read_file (dir, 0,
588 blk << dirblk_log2,
589 dirblk_size, dirblock);
590 if (numread != dirblk_size)
591 return 0;
593 entries = (grub_be_to_cpu32 (tail->leaf_count)
594 - grub_be_to_cpu32 (tail->leaf_stale));
596 /* Iterate over all entries within this block. */
597 while (pos < (dirblk_size
598 - (int) sizeof (struct grub_xfs_dir2_entry)))
600 struct grub_xfs_dir2_entry *direntry;
601 grub_uint8_t *freetag;
602 char *filename;
604 direntry = (struct grub_xfs_dir2_entry *) &dirblock[pos];
605 freetag = (grub_uint8_t *) direntry;
607 if (grub_get_unaligned16 (freetag) == 0XFFFF)
609 grub_uint8_t *skip = (freetag + sizeof (grub_uint16_t));
611 /* This entry is not used, go to the next one. */
612 pos += grub_be_to_cpu16 (grub_get_unaligned16 (skip));
614 continue;
617 filename = &dirblock[pos + sizeof (*direntry)];
618 /* The byte after the filename is for the tag, which
619 is not used by GRUB. So it can be overwritten. */
620 filename[direntry->len] = '\0';
622 if (call_hook (direntry->inode, filename))
624 grub_free (dirblock);
625 return 1;
628 /* Check if last direntry in this block is
629 reached. */
630 entries--;
631 if (!entries)
632 break;
634 /* Select the next directory entry. */
635 pos = GRUB_XFS_NEXT_DIRENT (pos, direntry->len);
636 pos = GRUB_XFS_ROUND_TO_DIRENT (pos);
639 grub_free (dirblock);
640 break;
643 default:
644 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
645 "XFS does not support inode format %d yet",
646 diro->inode.format);
648 return 0;
652 static struct grub_xfs_data *
653 grub_xfs_mount (grub_disk_t disk)
655 struct grub_xfs_data *data = 0;
657 data = grub_zalloc (sizeof (struct grub_xfs_data));
658 if (!data)
659 return 0;
661 /* Read the superblock. */
662 if (grub_disk_read (disk, 0, 0,
663 sizeof (struct grub_xfs_sblock), &data->sblock))
664 goto fail;
666 if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4)
667 || data->sblock.log2_bsize < GRUB_DISK_SECTOR_BITS
668 || ((int) data->sblock.log2_bsize
669 + (int) data->sblock.log2_dirblk) >= 27)
671 grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem");
672 goto fail;
675 data = grub_realloc (data,
676 sizeof (struct grub_xfs_data)
677 - sizeof (struct grub_xfs_inode)
678 + (1 << data->sblock.log2_inode));
680 if (! data)
681 goto fail;
683 data->diropen.data = data;
684 data->diropen.ino = data->sblock.rootino;
685 data->diropen.inode_read = 1;
686 data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
687 data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
689 data->disk = disk;
690 data->pos = 0;
692 grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
694 return data;
695 fail:
697 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
698 grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
700 grub_free (data);
702 return 0;
706 static grub_err_t
707 grub_xfs_dir (grub_device_t device, const char *path,
708 int (*hook) (const char *filename,
709 const struct grub_dirhook_info *info))
711 struct grub_xfs_data *data = 0;
712 struct grub_fshelp_node *fdiro = 0;
714 auto int NESTED_FUNC_ATTR iterate (const char *filename,
715 enum grub_fshelp_filetype filetype,
716 grub_fshelp_node_t node);
718 int NESTED_FUNC_ATTR iterate (const char *filename,
719 enum grub_fshelp_filetype filetype,
720 grub_fshelp_node_t node)
722 struct grub_dirhook_info info;
723 grub_memset (&info, 0, sizeof (info));
724 if (node->inode_read)
726 info.mtimeset = 1;
727 info.mtime = grub_be_to_cpu32 (node->inode.mtime.sec);
729 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
730 grub_free (node);
731 return hook (filename, &info);
734 grub_dl_ref (my_mod);
736 data = grub_xfs_mount (device->disk);
737 if (!data)
738 goto mount_fail;
740 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
741 grub_xfs_read_symlink, GRUB_FSHELP_DIR);
742 if (grub_errno)
743 goto fail;
745 grub_xfs_iterate_dir (fdiro, iterate);
747 fail:
748 if (fdiro != &data->diropen)
749 grub_free (fdiro);
750 grub_free (data);
752 mount_fail:
754 grub_dl_unref (my_mod);
756 return grub_errno;
760 /* Open a file named NAME and initialize FILE. */
761 static grub_err_t
762 grub_xfs_open (struct grub_file *file, const char *name)
764 struct grub_xfs_data *data;
765 struct grub_fshelp_node *fdiro = 0;
767 grub_dl_ref (my_mod);
769 data = grub_xfs_mount (file->device->disk);
770 if (!data)
771 goto mount_fail;
773 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
774 grub_xfs_read_symlink, GRUB_FSHELP_REG);
775 if (grub_errno)
776 goto fail;
778 if (!fdiro->inode_read)
780 grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
781 if (grub_errno)
782 goto fail;
785 if (fdiro != &data->diropen)
787 grub_memcpy (&data->diropen, fdiro,
788 sizeof (struct grub_fshelp_node)
789 - sizeof (struct grub_xfs_inode)
790 + (1 << data->sblock.log2_inode));
791 grub_free (fdiro);
794 file->size = grub_be_to_cpu64 (data->diropen.inode.size);
795 file->data = data;
796 file->offset = 0;
798 return 0;
800 fail:
801 if (fdiro != &data->diropen)
802 grub_free (fdiro);
803 grub_free (data);
805 mount_fail:
806 grub_dl_unref (my_mod);
808 return grub_errno;
812 static grub_ssize_t
813 grub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
815 struct grub_xfs_data *data =
816 (struct grub_xfs_data *) file->data;
818 return grub_xfs_read_file (&data->diropen, file->read_hook,
819 file->offset, len, buf);
823 static grub_err_t
824 grub_xfs_close (grub_file_t file)
826 grub_free (file->data);
828 grub_dl_unref (my_mod);
830 return GRUB_ERR_NONE;
834 static grub_err_t
835 grub_xfs_label (grub_device_t device, char **label)
837 struct grub_xfs_data *data;
838 grub_disk_t disk = device->disk;
840 grub_dl_ref (my_mod);
842 data = grub_xfs_mount (disk);
843 if (data)
844 *label = grub_strndup ((char *) (data->sblock.label), 12);
845 else
846 *label = 0;
848 grub_dl_unref (my_mod);
850 grub_free (data);
852 return grub_errno;
855 static grub_err_t
856 grub_xfs_uuid (grub_device_t device, char **uuid)
858 struct grub_xfs_data *data;
859 grub_disk_t disk = device->disk;
861 grub_dl_ref (my_mod);
863 data = grub_xfs_mount (disk);
864 if (data)
866 *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
867 grub_be_to_cpu16 (data->sblock.uuid[0]),
868 grub_be_to_cpu16 (data->sblock.uuid[1]),
869 grub_be_to_cpu16 (data->sblock.uuid[2]),
870 grub_be_to_cpu16 (data->sblock.uuid[3]),
871 grub_be_to_cpu16 (data->sblock.uuid[4]),
872 grub_be_to_cpu16 (data->sblock.uuid[5]),
873 grub_be_to_cpu16 (data->sblock.uuid[6]),
874 grub_be_to_cpu16 (data->sblock.uuid[7]));
876 else
877 *uuid = NULL;
879 grub_dl_unref (my_mod);
881 grub_free (data);
883 return grub_errno;
888 static struct grub_fs grub_xfs_fs =
890 .name = "xfs",
891 .dir = grub_xfs_dir,
892 .open = grub_xfs_open,
893 .read = grub_xfs_read,
894 .close = grub_xfs_close,
895 .label = grub_xfs_label,
896 .uuid = grub_xfs_uuid,
897 #ifdef GRUB_UTIL
898 .reserved_first_sector = 0,
899 .blocklist_install = 1,
900 #endif
901 .next = 0
904 GRUB_MOD_INIT(xfs)
906 grub_fs_register (&grub_xfs_fs);
907 my_mod = mod;
910 GRUB_MOD_FINI(xfs)
912 grub_fs_unregister (&grub_xfs_fs);