]> git.proxmox.com Git - grub2.git/blame - fs/xfs.c
2005-11-13 Marco Gerards <mgerards@xs4all.nl>
[grub2.git] / fs / xfs.c
CommitLineData
b2499b29 1/* xfs.c - XFS. */
2/*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005 Free Software Foundation, Inc.
5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <grub/err.h>
22#include <grub/file.h>
23#include <grub/mm.h>
24#include <grub/misc.h>
25#include <grub/disk.h>
26#include <grub/dl.h>
27#include <grub/types.h>
28#include <grub/fshelp.h>
29
30#define XFS_INODE_EXTENTS 9
31
32#define XFS_INODE_FORMAT_INO 1
33#define XFS_INODE_FORMAT_EXT 2
34#define XFS_INODE_FORMAT_BTREE 3
35
36
37struct grub_xfs_sblock
38{
39 grub_uint8_t magic[4];
40 grub_uint32_t bsize;
41 grub_uint8_t unused1[48];
42 grub_uint64_t rootino;
43 grub_uint8_t unused2[20];
44 grub_uint32_t agsize;
45 grub_uint8_t unused3[20];
46 grub_uint8_t label[12];
47 grub_uint8_t log2_bsize;
48 grub_uint8_t unused4[2];
49 grub_uint8_t log2_inop;
50 grub_uint8_t log2_agblk;
51} __attribute__ ((packed));
52
53struct grub_xfs_dir_header
54{
55 grub_uint8_t entries;
56 grub_uint8_t smallino;
57 grub_uint32_t parent;
58} __attribute__ ((packed));
59
60struct grub_xfs_dir_entry
61{
62 grub_uint8_t len;
63 grub_uint16_t offset;
64 char name[1];
65 /* Inode number follows, 32 bits. */
66} __attribute__ ((packed));
67
68struct grub_xfs_dir2_entry
69{
70 grub_uint64_t inode;
71 grub_uint8_t len;
72} __attribute__ ((packed));
73
74typedef grub_uint32_t grub_xfs_extent[4];
75
76struct grub_xfs_inode
77{
78 grub_uint8_t magic[2];
79 grub_uint16_t mode;
80 grub_uint8_t version;
81 grub_uint8_t format;
82 grub_uint8_t unused2[50];
83 grub_uint64_t size;
84 grub_uint8_t unused3[36];
85 union
86 {
87 char raw[156];
88 struct dir
89 {
90 struct grub_xfs_dir_header dirhead;
91 struct grub_xfs_dir_entry direntry[1];
92 } dir;
93 grub_xfs_extent extents[XFS_INODE_EXTENTS];
94 } data __attribute__ ((packed));
95} __attribute__ ((packed));
96
97struct grub_xfs_dirblock_tail
98{
99 grub_uint32_t leaf_count;
100 grub_uint32_t leaf_stale;
101} __attribute__ ((packed));
102
103struct grub_fshelp_node
104{
105 struct grub_xfs_data *data;
106 struct grub_xfs_inode inode;
107 grub_uint64_t ino;
108 int inode_read;
109};
110
111struct grub_xfs_data
112{
113 struct grub_xfs_sblock sblock;
114 struct grub_xfs_inode *inode;
115 grub_disk_t disk;
116 int pos;
117 int bsize;
118 int agsize;
119 struct grub_fshelp_node diropen;
120
121};
b4093103 122
123#ifndef GRUB_UTIL
124static grub_dl_t my_mod;
125#endif
126
b2499b29 127\f
128
129/* Filetype information as used in inodes. */
130#define FILETYPE_INO_MASK 0170000
131#define FILETYPE_INO_REG 0100000
132#define FILETYPE_INO_DIRECTORY 0040000
133#define FILETYPE_INO_SYMLINK 0120000
134
135#define GRUB_XFS_INO_AGBITS(data) \
136 ((data)->sblock.log2_agblk + (data)->sblock.log2_inop)
137#define GRUB_XFS_INO_INOINAG(data, ino) \
138 (grub_be_to_cpu64 (ino) & ((1 << GRUB_XFS_INO_AGBITS (data)) - 1))
139#define GRUB_XFS_INO_AG(data,ino) \
140 (grub_be_to_cpu64 (ino) >> GRUB_XFS_INO_AGBITS (data))
141
142#define GRUB_XFS_EXTENT_OFFSET(inode,ex) \
143 ((grub_be_to_cpu32 ((inode)->data.extents[ex][0]) & ~(1 << 31)) << 23 \
144 | grub_be_to_cpu32 ((inode)->data.extents[ex][1]) >> 9)
145
146#define GRUB_XFS_EXTENT_BLOCK(inode,ex) \
147 ((grub_uint64_t) (grub_be_to_cpu32 ((inode)->data.extents[ex][1]) \
148 & (~255)) << 43 \
149 | (grub_uint64_t) grub_be_to_cpu32 ((inode)->data.extents[ex][2]) << 11 \
150 | grub_be_to_cpu32 ((inode)->data.extents[ex][3]) >> 21)
151
152#define GRUB_XFS_EXTENT_SIZE(inode,ex) \
153 (grub_be_to_cpu32 ((inode)->data.extents[ex][3]) & ((1 << 20) - 1))
154
155#define GRUB_XFS_ROUND_TO_DIRENT(pos) ((((pos) + 8 - 1) / 8) * 8)
156#define GRUB_XFS_NEXT_DIRENT(pos,len) \
157 (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2)
158\f
688e5699 159static inline int
160grub_xfs_inode_block (struct grub_xfs_data *data,
161 grub_uint64_t ino)
b2499b29 162{
163 long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
164 long long ag = GRUB_XFS_INO_AG (data, ino);
165 long long block;
166
167 block = (inoinag >> 4) + ag * data->agsize;
168 block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
169 return block;
170}
171
172
688e5699 173static inline int
174grub_xfs_inode_offset (struct grub_xfs_data *data,
175 grub_uint64_t ino)
b2499b29 176{
177 int inoag = GRUB_XFS_INO_INOINAG (data, ino);
178 return (inoag & ((1 << 4) - 1)) << 8;
179}
180
181
182static grub_err_t
183grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
184 struct grub_xfs_inode *inode)
185{
186 int block = grub_xfs_inode_block (data, ino);
187 int offset = grub_xfs_inode_offset (data, ino);
188
189 /* Read the inode. */
190 if (grub_disk_read (data->disk, block, offset,
191 sizeof (struct grub_xfs_inode), (char *) inode))
192 return grub_errno;
193
194 if (grub_strncmp (inode->magic, "IN", 2))
195 return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode.\n");
196
197 return 0;
198}
199
200
201static int
202grub_xfs_read_block (grub_fshelp_node_t node, int fileblock)
203{
204 int ex;
205
206 if (node->inode.format != XFS_INODE_FORMAT_EXT)
207 {
208 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
209 "xfs does not support inode format %d yet",
210 node->inode.format);
211 return 0;
212 }
213
214 /* Iterate over each extent to figure out which extent has
215 the block we are looking for. */
216 for (ex = 0; ex < XFS_INODE_EXTENTS; ex++)
217 {
218 grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (&node->inode, ex);
219 int offset = GRUB_XFS_EXTENT_OFFSET (&node->inode, ex);
220 int size = GRUB_XFS_EXTENT_SIZE (&node->inode, ex);
221
222 unsigned int ag = start >> node->data->sblock.log2_agblk;
223 unsigned int block = start & ((1 << node->data->sblock.log2_agblk) - 1);
224
225 if (fileblock < offset + size)
226 return (fileblock - offset + block) + ag * node->data->agsize;
227 }
228
229 grub_error (GRUB_ERR_FILE_READ_ERROR,
230 "xfs block %d for inode %d is not in an extent.\n",
231 fileblock, grub_be_to_cpu64 (node->ino));
232 return 0;
233}
234
235
236/* Read LEN bytes from the file described by DATA starting with byte
237 POS. Return the amount of read bytes in READ. */
238static grub_ssize_t
239grub_xfs_read_file (grub_fshelp_node_t node,
240 void (*read_hook) (unsigned long sector,
241 unsigned offset, unsigned length),
242 int pos, unsigned int len, char *buf)
243{
244 return grub_fshelp_read_file (node->data->disk, node, read_hook,
245 pos, len, buf, grub_xfs_read_block,
246 grub_be_to_cpu64 (node->inode.size),
247 node->data->sblock.log2_bsize
248 - GRUB_DISK_SECTOR_BITS);
249}
250
251
252static char *
253grub_xfs_read_symlink (grub_fshelp_node_t node)
254{
255 int size = grub_be_to_cpu64 (node->inode.size);
256
257 switch (node->inode.format)
258 {
259 case XFS_INODE_FORMAT_INO:
260 return grub_strndup (node->inode.data.raw, size);
261
262 case XFS_INODE_FORMAT_EXT:
263 {
264 char *symlink;
265 grub_ssize_t numread;
266
267 symlink = grub_malloc (size + 1);
268 if (!symlink)
269 return 0;
270
271 numread = grub_xfs_read_file (node, 0, 0, size, symlink);
272 if (numread != size)
273 {
274 grub_free (symlink);
275 return 0;
276 }
277 symlink[size] = '\0';
278 return symlink;
279 }
280 }
281
282 return 0;
283}
284
285
286static enum grub_fshelp_filetype
287grub_xfs_mode_to_filetype (grub_uint16_t mode)
288{
289 if ((grub_be_to_cpu16 (mode)
290 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
291 return GRUB_FSHELP_DIR;
292 else if ((grub_be_to_cpu16 (mode)
293 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
294 return GRUB_FSHELP_SYMLINK;
295 else if ((grub_be_to_cpu16 (mode)
296 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
297 return GRUB_FSHELP_REG;
298 return GRUB_FSHELP_UNKNOWN;
299}
300
301
302static int
303grub_xfs_iterate_dir (grub_fshelp_node_t dir,
304 int NESTED_FUNC_ATTR
305 (*hook) (const char *filename,
306 enum grub_fshelp_filetype filetype,
307 grub_fshelp_node_t node))
308{
309 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
b4093103 310 auto int call_hook (grub_uint64_t ino, char *filename);
b2499b29 311
312 int call_hook (grub_uint64_t ino, char *filename)
313 {
314 struct grub_fshelp_node *fdiro;
315
316 fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
317 if (!fdiro)
318 return 0;
319
320 /* The inode should be read, otherwise the filetype can
321 not be determined. */
322 fdiro->ino = ino;
323 fdiro->inode_read = 1;
324 fdiro->data = diro->data;
325 grub_xfs_read_inode (diro->data, ino, &fdiro->inode);
326
327 return hook (filename,
328 grub_xfs_mode_to_filetype (fdiro->inode.mode),
329 fdiro);
330 }
331
332 switch (diro->inode.format)
333 {
334 case XFS_INODE_FORMAT_INO:
335 {
336 struct grub_xfs_dir_entry *de = &diro->inode.data.dir.direntry[0];
337 int smallino = !diro->inode.data.dir.dirhead.smallino;
338 int i;
339 grub_uint64_t parent;
340
341 /* If small inode numbers are used to pack the direntry, the
342 parent inode number is small too. */
343 if (smallino)
344 {
345 parent = grub_be_to_cpu32 (diro->inode.data.dir.dirhead.parent);
346 parent = grub_cpu_to_be64 (parent);
347 }
348 else
349 {
350 parent = *(grub_uint64_t *) &diro->inode.data.dir.dirhead.parent;
351 /* The header is a bit bigger than usual. */
352 de = (struct grub_xfs_dir_entry *) ((char *) de + 4);
353 }
354
355 /* Synthesize the direntries for `.' and `..'. */
356 if (call_hook (diro->ino, "."))
357 return 1;
358
359 if (call_hook (parent, ".."))
360 return 1;
361
362 for (i = 0; i < diro->inode.data.dir.dirhead.entries; i++)
363 {
364 grub_uint64_t ino;
365 void *inopos = (((char *) de)
688e5699 366 + sizeof (struct grub_xfs_dir_entry)
367 + de->len - 1);
368 char name[de->len + 1];
369
b2499b29 370 if (smallino)
371 {
372 ino = grub_be_to_cpu32 (*(grub_uint32_t *) inopos);
373 ino = grub_cpu_to_be64 (ino);
374 }
375 else
376 ino = *(grub_uint64_t *) inopos;
377
688e5699 378 grub_memcpy (name, de->name, de->len);
379 name[de->len] = '\0';
380 if (call_hook (ino, name))
b2499b29 381 return 1;
382
383 de = ((struct grub_xfs_dir_entry *)
384 (((char *) de)+ sizeof (struct grub_xfs_dir_entry) + de->len
047b67e0 385 + ((smallino ? sizeof (grub_uint32_t)
386 : sizeof (grub_uint64_t))) - 1));
b2499b29 387 }
388 break;
389 }
390
391 case XFS_INODE_FORMAT_BTREE:
392 case XFS_INODE_FORMAT_EXT:
393 {
394 grub_ssize_t numread;
395 char *dirblock;
688e5699 396 grub_uint64_t blk;
b2499b29 397
398 dirblock = grub_malloc (dir->data->bsize);
688e5699 399 if (! dirblock)
b2499b29 400 return 0;
401
402 /* Iterate over every block the directory has. */
403 for (blk = 0;
404 blk < (grub_be_to_cpu64 (dir->inode.size)
405 >> dir->data->sblock.log2_bsize);
406 blk++)
407 {
408 /* The header is skipped, the first direntry is stored
409 from byte 16. */
410 int pos = 16;
411 int entries;
412 int tail_start = (dir->data->bsize
413 - sizeof (struct grub_xfs_dirblock_tail));
414
415 struct grub_xfs_dirblock_tail *tail;
416 tail = (struct grub_xfs_dirblock_tail *) &dirblock[tail_start];
417
418 numread = grub_xfs_read_file (dir, 0,
419 blk << dir->data->sblock.log2_bsize,
420 dir->data->bsize, dirblock);
6fa1251a 421 if (numread != dir->data->bsize)
422 return 0;
b2499b29 423
424 entries = (grub_be_to_cpu32 (tail->leaf_count)
425 - grub_be_to_cpu32 (tail->leaf_stale));
426
427 /* Iterate over all entries within this block. */
428 while (pos < (dir->data->bsize
429 - (int) sizeof (struct grub_xfs_dir2_entry)))
430 {
431 struct grub_xfs_dir2_entry *direntry;
432 grub_uint16_t *freetag;
433 char *filename;
434
435 direntry = (struct grub_xfs_dir2_entry *) &dirblock[pos];
436 freetag = (grub_uint16_t *) direntry;
437
438 if (*freetag == 0XFFFF)
439 {
440 grub_uint16_t *skip = (grub_uint16_t *) (freetag + 1);
441
442 /* This entry is not used, go to the next one. */
443 pos += grub_be_to_cpu16 (*skip);
444
445 continue;
446 }
447
448 filename = &dirblock[pos + sizeof (*direntry)];
449 /* The byte after the filename is for the tag, which
450 is not used by GRUB. So it can be overwritten. */
451 filename[direntry->len] = '\0';
452
453 if (call_hook (direntry->inode, filename))
454 {
455 grub_free (dirblock);
456 return 1;
457 }
458
459 /* Check if last direntry in this block is
460 reached. */
461 entries--;
462 if (!entries)
463 break;
464
465 /* Select the next directory entry. */
466 pos = GRUB_XFS_NEXT_DIRENT (pos, direntry->len);
467 pos = GRUB_XFS_ROUND_TO_DIRENT (pos);
468 }
469 }
470 grub_free (dirblock);
471 break;
472 }
473
474 default:
475 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
476 "xfs does not support inode format %d yet",
477 diro->inode.format);
478 }
479 return 0;
480}
481
482
483static struct grub_xfs_data *
484grub_xfs_mount (grub_disk_t disk)
485{
486 struct grub_xfs_data *data = 0;
487
488 data = grub_malloc (sizeof (struct grub_xfs_data));
489 if (!data)
490 return 0;
491
492 /* Read the superblock. */
493 if (grub_disk_read (disk, 0, 0,
494 sizeof (struct grub_xfs_sblock), (char *) &data->sblock))
495 goto fail;
496
497 if (grub_strncmp (data->sblock.magic, "XFSB", 4))
498 {
499 grub_error (GRUB_ERR_BAD_FS, "not a xfs filesystem");
500 goto fail;
501 }
502
503 data->diropen.data = data;
504 data->diropen.ino = data->sblock.rootino;
505 data->diropen.inode_read = 1;
688e5699 506 data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
507 data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
b2499b29 508
509 data->disk = disk;
510 data->inode = &data->diropen.inode;
511 data->pos = 0;
512
513 grub_xfs_read_inode (data, data->diropen.ino, data->inode);
514
515 return data;
516 fail:
517
518 grub_free (data);
519
520 return 0;
521}
522
523\f
524static grub_err_t
525grub_xfs_dir (grub_device_t device, const char *path,
526 int (*hook) (const char *filename, int dir))
527{
528 struct grub_xfs_data *data = 0;;
529 struct grub_fshelp_node *fdiro = 0;
530
531 auto int NESTED_FUNC_ATTR iterate (const char *filename,
532 enum grub_fshelp_filetype filetype,
533 grub_fshelp_node_t node);
534
535 int NESTED_FUNC_ATTR iterate (const char *filename,
536 enum grub_fshelp_filetype filetype,
537 grub_fshelp_node_t node)
538 {
539 grub_free (node);
540
541 if (filetype == GRUB_FSHELP_DIR)
542 return hook (filename, 1);
543 else
544 return hook (filename, 0);
545
546 return 0;
547 }
548
549#ifndef GRUB_UTIL
550 grub_dl_ref (my_mod);
551#endif
552
553 data = grub_xfs_mount (device->disk);
554 if (!data)
555 goto fail;
556
557 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
558 grub_xfs_read_symlink, GRUB_FSHELP_DIR);
559 if (grub_errno)
560 goto fail;
561
562 grub_xfs_iterate_dir (fdiro, iterate);
563
564 fail:
565 if (fdiro != &data->diropen)
566 grub_free (fdiro);
567 grub_free (data);
568
569#ifndef GRUB_UTIL
570 grub_dl_unref (my_mod);
571#endif
572
573 return grub_errno;
574
575 return 0;
576}
577
578
579/* Open a file named NAME and initialize FILE. */
580static grub_err_t
581grub_xfs_open (struct grub_file *file, const char *name)
582{
583 struct grub_xfs_data *data;
584 struct grub_fshelp_node *fdiro = 0;
585
586#ifndef GRUB_UTIL
587 grub_dl_ref (my_mod);
588#endif
589
590 data = grub_xfs_mount (file->device->disk);
591 if (!data)
592 goto fail;
593
594 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
595 grub_xfs_read_symlink, GRUB_FSHELP_REG);
596 if (grub_errno)
597 goto fail;
598
599 if (!fdiro->inode_read)
600 {
601 grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
602 if (grub_errno)
603 goto fail;
604 }
605
606 grub_memcpy (data->inode,
607 &fdiro->inode,
608 sizeof (struct grub_xfs_inode));
609 grub_free (fdiro);
610
611 file->size = grub_be_to_cpu64 (data->inode->size);
612 file->data = data;
613 file->offset = 0;
614
615 return 0;
616
617 fail:
618 if (fdiro != &data->diropen)
619 grub_free (fdiro);
620 grub_free (data);
621
622#ifndef GRUB_UTIL
623 grub_dl_unref (my_mod);
624#endif
625
626 return grub_errno;
627}
628
629
630static grub_ssize_t
631grub_xfs_read (grub_file_t file, char *buf, grub_ssize_t len)
632{
633 struct grub_xfs_data *data =
634 (struct grub_xfs_data *) file->data;
635
636 return grub_xfs_read_file (&data->diropen, file->read_hook,
637 file->offset, len, buf);
638}
639
640
641static grub_err_t
642grub_xfs_close (grub_file_t file)
643{
644 grub_free (file->data);
645
646#ifndef GRUB_UTIL
647 grub_dl_unref (my_mod);
648#endif
649
650 return GRUB_ERR_NONE;
651}
652
653
654static grub_err_t
655grub_xfs_label (grub_device_t device, char **label)
656{
657 struct grub_xfs_data *data;
658 grub_disk_t disk = device->disk;
659
660#ifndef GRUB_UTIL
661 grub_dl_ref (my_mod);
662#endif
663
664 data = grub_xfs_mount (disk);
665 if (data)
666 *label = grub_strndup (data->sblock.label, 12);
667 else
668 *label = 0;
669
670#ifndef GRUB_UTIL
671 grub_dl_unref (my_mod);
672#endif
673
674 grub_free (data);
675
676 return grub_errno;
677}
678
679\f
680
681static struct grub_fs grub_xfs_fs =
682 {
683 .name = "xfs",
684 .dir = grub_xfs_dir,
685 .open = grub_xfs_open,
686 .read = grub_xfs_read,
687 .close = grub_xfs_close,
688 .label = grub_xfs_label,
689 .next = 0
690 };
691
6d099807 692GRUB_MOD_INIT(xfs)
b2499b29 693{
694 grub_fs_register (&grub_xfs_fs);
6d099807 695#ifndef GRUB_UTIL
b2499b29 696 my_mod = mod;
6d099807 697#endif
b2499b29 698}
699
6d099807 700GRUB_MOD_FINI(xfs)
b2499b29 701{
702 grub_fs_unregister (&grub_xfs_fs);
703}
6d099807 704