make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / fs / xfs.c
blob88d22be6d150cd98350b156e08e8a3289191fa8e
1 /* xfs.c - XFS. */
2 /*
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/>.
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 #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];
39 grub_uint32_t bsize;
40 grub_uint8_t unused1[48];
41 grub_uint64_t rootino;
42 grub_uint8_t unused2[20];
43 grub_uint32_t agsize;
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
56 grub_uint8_t entries;
57 grub_uint8_t smallino;
58 grub_uint32_t parent;
59 } __attribute__ ((packed));
61 struct grub_xfs_dir_entry
63 grub_uint8_t len;
64 grub_uint16_t offset;
65 char name[1];
66 /* Inode number follows, 32 bits. */
67 } __attribute__ ((packed));
69 struct grub_xfs_dir2_entry
71 grub_uint64_t inode;
72 grub_uint8_t len;
73 } __attribute__ ((packed));
75 typedef grub_uint32_t grub_xfs_extent[4];
77 struct grub_xfs_btree_node
79 grub_uint8_t magic[4];
80 grub_uint16_t level;
81 grub_uint16_t numrecs;
82 grub_uint64_t left;
83 grub_uint64_t right;
84 grub_uint64_t keys[1];
85 } __attribute__ ((packed));
87 struct grub_xfs_btree_root
89 grub_uint16_t level;
90 grub_uint16_t numrecs;
91 grub_uint64_t keys[1];
92 } __attribute__ ((packed));
94 struct grub_xfs_inode
96 grub_uint8_t magic[2];
97 grub_uint16_t mode;
98 grub_uint8_t version;
99 grub_uint8_t format;
100 grub_uint8_t unused2[50];
101 grub_uint64_t size;
102 grub_uint64_t nblocks;
103 grub_uint32_t extsize;
104 grub_uint32_t nextents;
105 grub_uint8_t unused3[20];
106 union
108 char raw[156];
109 struct dir
111 struct grub_xfs_dir_header dirhead;
112 struct grub_xfs_dir_entry direntry[1];
113 } dir;
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;
129 grub_uint64_t ino;
130 int inode_read;
133 struct grub_xfs_data
135 struct grub_xfs_sblock sblock;
136 struct grub_xfs_inode *inode;
137 grub_disk_t disk;
138 int pos;
139 int bsize;
140 int agsize;
141 struct grub_fshelp_node diropen;
145 #ifndef GRUB_UTIL
146 static grub_dl_t my_mod;
147 #endif
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]) \
170 & (0x1ff)) << 43 \
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)
181 static inline int
182 grub_xfs_inode_block (struct grub_xfs_data *data,
183 grub_uint64_t ino)
185 long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
186 long long ag = GRUB_XFS_INO_AG (data, ino);
187 long long block;
189 block = (inoinag >> 4) + ag * data->agsize;
190 block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
191 return block;
195 static inline int
196 grub_xfs_inode_offset (struct grub_xfs_data *data,
197 grub_uint64_t ino)
199 int inoag = GRUB_XFS_INO_INOINAG (data, ino);
200 return (inoag & ((1 << 4) - 1)) << 8;
204 static grub_err_t
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))
214 return grub_errno;
216 if (grub_strncmp ((char *) inode->magic, "IN", 2))
217 return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode.\n");
219 return 0;
223 static int
224 grub_xfs_read_block (grub_fshelp_node_t node, int fileblock)
226 struct grub_xfs_btree_node *leaf = 0;
227 int ex, nrec;
228 grub_xfs_extent *exts;
229 grub_uint64_t ret = 0;
231 if (node->inode.format == XFS_INODE_FORMAT_BTREE)
233 grub_uint64_t *keys;
235 leaf = grub_malloc (node->data->sblock.bsize);
236 if (leaf == 0)
237 return 0;
239 nrec = grub_be_to_cpu16 (node->inode.data.btree.numrecs);
240 keys = &node->inode.data.btree.keys[0];
243 int i;
245 for (i = 0; i < nrec; i++)
247 if ((grub_uint64_t) fileblock < grub_be_to_cpu64 (keys[i]))
248 break;
251 /* Sparse block. */
252 if (i == 0)
254 grub_free (leaf);
255 return 0;
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))
263 return 0;
265 if (grub_strncmp ((char *) leaf->magic, "BMAP", 4))
267 grub_free (leaf);
268 grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node.\n");
269 return 0;
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];
282 else
284 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
285 "xfs does not support inode format %d yet",
286 node->inode.format);
287 return 0;
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);
298 /* Sparse block. */
299 if (fileblock < offset)
300 break;
301 else if (fileblock < offset + size)
303 ret = (fileblock - offset + start);
304 break;
308 if (leaf)
309 grub_free (leaf);
311 return ret;
315 /* Read LEN bytes from the file described by DATA starting with byte
316 POS. Return the amount of read bytes in READ. */
317 static grub_ssize_t
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);
331 static char *
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:
343 char *symlink;
344 grub_ssize_t numread;
346 symlink = grub_malloc (size + 1);
347 if (!symlink)
348 return 0;
350 numread = grub_xfs_read_file (node, 0, 0, size, symlink);
351 if (numread != size)
353 grub_free (symlink);
354 return 0;
356 symlink[size] = '\0';
357 return symlink;
361 return 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;
381 static int
382 grub_xfs_iterate_dir (grub_fshelp_node_t dir,
383 int NESTED_FUNC_ATTR
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));
396 if (!fdiro)
397 return 0;
399 /* The inode should be read, otherwise the filetype can
400 not be determined. */
401 fdiro->ino = ino;
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),
408 fdiro);
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;
417 int i;
418 grub_uint64_t parent;
420 /* If small inode numbers are used to pack the direntry, the
421 parent inode number is small too. */
422 if (smallino)
424 parent = grub_be_to_cpu32 (diro->inode.data.dir.dirhead.parent);
425 parent = grub_cpu_to_be64 (parent);
427 else
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, "."))
436 return 1;
438 if (call_hook (parent, ".."))
439 return 1;
441 for (i = 0; i < diro->inode.data.dir.dirhead.entries; i++)
443 grub_uint64_t ino;
444 void *inopos = (((char *) de)
445 + sizeof (struct grub_xfs_dir_entry)
446 + de->len - 1);
447 char name[de->len + 1];
449 if (smallino)
451 ino = grub_be_to_cpu32 (*(grub_uint32_t *) inopos);
452 ino = grub_cpu_to_be64 (ino);
454 else
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))
460 return 1;
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));
467 break;
470 case XFS_INODE_FORMAT_BTREE:
471 case XFS_INODE_FORMAT_EXT:
473 grub_ssize_t numread;
474 char *dirblock;
475 grub_uint64_t blk;
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);
483 if (! dirblock)
484 return 0;
486 /* Iterate over every block the directory has. */
487 for (blk = 0;
488 blk < (grub_be_to_cpu64 (dir->inode.size)
489 >> dirblk_log2);
490 blk++)
492 /* The header is skipped, the first direntry is stored
493 from byte 16. */
494 int pos = 16;
495 int entries;
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,
503 blk << dirblk_log2,
504 dirblk_size, dirblock);
505 if (numread != dirblk_size)
506 return 0;
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;
517 char *filename;
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);
529 continue;
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);
540 return 1;
543 /* Check if last direntry in this block is
544 reached. */
545 entries--;
546 if (!entries)
547 break;
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);
555 break;
558 default:
559 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
560 "xfs does not support inode format %d yet",
561 diro->inode.format);
563 return 0;
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));
573 if (!data)
574 return 0;
576 /* Read the superblock. */
577 if (grub_disk_read (disk, 0, 0,
578 sizeof (struct grub_xfs_sblock), (char *) &data->sblock))
579 goto fail;
581 if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4))
583 grub_error (GRUB_ERR_BAD_FS, "not a xfs filesystem");
584 goto fail;
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);
593 data->disk = disk;
594 data->inode = &data->diropen.inode;
595 data->pos = 0;
597 grub_xfs_read_inode (data, data->diropen.ino, data->inode);
599 return data;
600 fail:
602 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
603 grub_error (GRUB_ERR_BAD_FS, "not an xfs filesystem");
605 grub_free (data);
607 return 0;
611 static grub_err_t
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)
626 grub_free (node);
628 if (filetype == GRUB_FSHELP_DIR)
629 return hook (filename, 1);
630 else
631 return hook (filename, 0);
633 return 0;
636 #ifndef GRUB_UTIL
637 grub_dl_ref (my_mod);
638 #endif
640 data = grub_xfs_mount (device->disk);
641 if (!data)
642 goto fail;
644 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
645 grub_xfs_read_symlink, GRUB_FSHELP_DIR);
646 if (grub_errno)
647 goto fail;
649 grub_xfs_iterate_dir (fdiro, iterate);
651 fail:
652 if (fdiro != &data->diropen)
653 grub_free (fdiro);
654 grub_free (data);
656 #ifndef GRUB_UTIL
657 grub_dl_unref (my_mod);
658 #endif
660 return grub_errno;
662 return 0;
666 /* Open a file named NAME and initialize FILE. */
667 static grub_err_t
668 grub_xfs_open (struct grub_file *file, const char *name)
670 struct grub_xfs_data *data;
671 struct grub_fshelp_node *fdiro = 0;
673 #ifndef GRUB_UTIL
674 grub_dl_ref (my_mod);
675 #endif
677 data = grub_xfs_mount (file->device->disk);
678 if (!data)
679 goto fail;
681 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
682 grub_xfs_read_symlink, GRUB_FSHELP_REG);
683 if (grub_errno)
684 goto fail;
686 if (!fdiro->inode_read)
688 grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
689 if (grub_errno)
690 goto fail;
693 grub_memcpy (data->inode,
694 &fdiro->inode,
695 sizeof (struct grub_xfs_inode));
696 grub_free (fdiro);
698 file->size = grub_be_to_cpu64 (data->inode->size);
699 file->data = data;
700 file->offset = 0;
702 return 0;
704 fail:
705 if (fdiro != &data->diropen)
706 grub_free (fdiro);
707 grub_free (data);
709 #ifndef GRUB_UTIL
710 grub_dl_unref (my_mod);
711 #endif
713 return grub_errno;
717 static grub_ssize_t
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);
728 static grub_err_t
729 grub_xfs_close (grub_file_t file)
731 grub_free (file->data);
733 #ifndef GRUB_UTIL
734 grub_dl_unref (my_mod);
735 #endif
737 return GRUB_ERR_NONE;
741 static grub_err_t
742 grub_xfs_label (grub_device_t device, char **label)
744 struct grub_xfs_data *data;
745 grub_disk_t disk = device->disk;
747 #ifndef GRUB_UTIL
748 grub_dl_ref (my_mod);
749 #endif
751 data = grub_xfs_mount (disk);
752 if (data)
753 *label = grub_strndup ((char *) (data->sblock.label), 12);
754 else
755 *label = 0;
757 #ifndef GRUB_UTIL
758 grub_dl_unref (my_mod);
759 #endif
761 grub_free (data);
763 return grub_errno;
768 static struct grub_fs grub_xfs_fs =
770 .name = "xfs",
771 .dir = grub_xfs_dir,
772 .open = grub_xfs_open,
773 .read = grub_xfs_read,
774 .close = grub_xfs_close,
775 .label = grub_xfs_label,
776 .next = 0
779 GRUB_MOD_INIT(xfs)
781 grub_fs_register (&grub_xfs_fs);
782 #ifndef GRUB_UTIL
783 my_mod = mod;
784 #endif
787 GRUB_MOD_FINI(xfs)
789 grub_fs_unregister (&grub_xfs_fs);