]> git.proxmox.com Git - grub2.git/blame - fs/xfs.c
2005-09-29 Yoshinori K. Okuji <okuji@enbug.org>
[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
159static inline int grub_xfs_inode_block (struct grub_xfs_data *data,
160 grub_uint64_t ino)
161{
162 long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
163 long long ag = GRUB_XFS_INO_AG (data, ino);
164 long long block;
165
166 block = (inoinag >> 4) + ag * data->agsize;
167 block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
168 return block;
169}
170
171
172static inline int grub_xfs_inode_offset (struct grub_xfs_data *data,
173 grub_uint64_t ino)
174{
175 int inoag = GRUB_XFS_INO_INOINAG (data, ino);
176 return (inoag & ((1 << 4) - 1)) << 8;
177}
178
179
180static grub_err_t
181grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
182 struct grub_xfs_inode *inode)
183{
184 int block = grub_xfs_inode_block (data, ino);
185 int offset = grub_xfs_inode_offset (data, ino);
186
187 /* Read the inode. */
188 if (grub_disk_read (data->disk, block, offset,
189 sizeof (struct grub_xfs_inode), (char *) inode))
190 return grub_errno;
191
192 if (grub_strncmp (inode->magic, "IN", 2))
193 return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode.\n");
194
195 return 0;
196}
197
198
199static int
200grub_xfs_read_block (grub_fshelp_node_t node, int fileblock)
201{
202 int ex;
203
204 if (node->inode.format != XFS_INODE_FORMAT_EXT)
205 {
206 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
207 "xfs does not support inode format %d yet",
208 node->inode.format);
209 return 0;
210 }
211
212 /* Iterate over each extent to figure out which extent has
213 the block we are looking for. */
214 for (ex = 0; ex < XFS_INODE_EXTENTS; ex++)
215 {
216 grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (&node->inode, ex);
217 int offset = GRUB_XFS_EXTENT_OFFSET (&node->inode, ex);
218 int size = GRUB_XFS_EXTENT_SIZE (&node->inode, ex);
219
220 unsigned int ag = start >> node->data->sblock.log2_agblk;
221 unsigned int block = start & ((1 << node->data->sblock.log2_agblk) - 1);
222
223 if (fileblock < offset + size)
224 return (fileblock - offset + block) + ag * node->data->agsize;
225 }
226
227 grub_error (GRUB_ERR_FILE_READ_ERROR,
228 "xfs block %d for inode %d is not in an extent.\n",
229 fileblock, grub_be_to_cpu64 (node->ino));
230 return 0;
231}
232
233
234/* Read LEN bytes from the file described by DATA starting with byte
235 POS. Return the amount of read bytes in READ. */
236static grub_ssize_t
237grub_xfs_read_file (grub_fshelp_node_t node,
238 void (*read_hook) (unsigned long sector,
239 unsigned offset, unsigned length),
240 int pos, unsigned int len, char *buf)
241{
242 return grub_fshelp_read_file (node->data->disk, node, read_hook,
243 pos, len, buf, grub_xfs_read_block,
244 grub_be_to_cpu64 (node->inode.size),
245 node->data->sblock.log2_bsize
246 - GRUB_DISK_SECTOR_BITS);
247}
248
249
250static char *
251grub_xfs_read_symlink (grub_fshelp_node_t node)
252{
253 int size = grub_be_to_cpu64 (node->inode.size);
254
255 switch (node->inode.format)
256 {
257 case XFS_INODE_FORMAT_INO:
258 return grub_strndup (node->inode.data.raw, size);
259
260 case XFS_INODE_FORMAT_EXT:
261 {
262 char *symlink;
263 grub_ssize_t numread;
264
265 symlink = grub_malloc (size + 1);
266 if (!symlink)
267 return 0;
268
269 numread = grub_xfs_read_file (node, 0, 0, size, symlink);
270 if (numread != size)
271 {
272 grub_free (symlink);
273 return 0;
274 }
275 symlink[size] = '\0';
276 return symlink;
277 }
278 }
279
280 return 0;
281}
282
283
284static enum grub_fshelp_filetype
285grub_xfs_mode_to_filetype (grub_uint16_t mode)
286{
287 if ((grub_be_to_cpu16 (mode)
288 & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
289 return GRUB_FSHELP_DIR;
290 else if ((grub_be_to_cpu16 (mode)
291 & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
292 return GRUB_FSHELP_SYMLINK;
293 else if ((grub_be_to_cpu16 (mode)
294 & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
295 return GRUB_FSHELP_REG;
296 return GRUB_FSHELP_UNKNOWN;
297}
298
299
300static int
301grub_xfs_iterate_dir (grub_fshelp_node_t dir,
302 int NESTED_FUNC_ATTR
303 (*hook) (const char *filename,
304 enum grub_fshelp_filetype filetype,
305 grub_fshelp_node_t node))
306{
307 struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
b4093103 308 auto int call_hook (grub_uint64_t ino, char *filename);
b2499b29 309
310 int call_hook (grub_uint64_t ino, char *filename)
311 {
312 struct grub_fshelp_node *fdiro;
313
314 fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
315 if (!fdiro)
316 return 0;
317
318 /* The inode should be read, otherwise the filetype can
319 not be determined. */
320 fdiro->ino = ino;
321 fdiro->inode_read = 1;
322 fdiro->data = diro->data;
323 grub_xfs_read_inode (diro->data, ino, &fdiro->inode);
324
325 return hook (filename,
326 grub_xfs_mode_to_filetype (fdiro->inode.mode),
327 fdiro);
328 }
329
330 switch (diro->inode.format)
331 {
332 case XFS_INODE_FORMAT_INO:
333 {
334 struct grub_xfs_dir_entry *de = &diro->inode.data.dir.direntry[0];
335 int smallino = !diro->inode.data.dir.dirhead.smallino;
336 int i;
337 grub_uint64_t parent;
338
339 /* If small inode numbers are used to pack the direntry, the
340 parent inode number is small too. */
341 if (smallino)
342 {
343 parent = grub_be_to_cpu32 (diro->inode.data.dir.dirhead.parent);
344 parent = grub_cpu_to_be64 (parent);
345 }
346 else
347 {
348 parent = *(grub_uint64_t *) &diro->inode.data.dir.dirhead.parent;
349 /* The header is a bit bigger than usual. */
350 de = (struct grub_xfs_dir_entry *) ((char *) de + 4);
351 }
352
353 /* Synthesize the direntries for `.' and `..'. */
354 if (call_hook (diro->ino, "."))
355 return 1;
356
357 if (call_hook (parent, ".."))
358 return 1;
359
360 for (i = 0; i < diro->inode.data.dir.dirhead.entries; i++)
361 {
362 grub_uint64_t ino;
363 void *inopos = (((char *) de)
364 + sizeof (struct grub_xfs_dir_entry) + de->len - 1);
365
366 if (smallino)
367 {
368 ino = grub_be_to_cpu32 (*(grub_uint32_t *) inopos);
369 ino = grub_cpu_to_be64 (ino);
370 }
371 else
372 ino = *(grub_uint64_t *) inopos;
373
374 if (call_hook (ino, de->name))
375 return 1;
376
377 de = ((struct grub_xfs_dir_entry *)
378 (((char *) de)+ sizeof (struct grub_xfs_dir_entry) + de->len
b4093103 379 + (smallino ? sizeof (grub_uint32_t)
380 : sizeof (grub_uint64_t))) - 1);
b2499b29 381 }
382 break;
383 }
384
385 case XFS_INODE_FORMAT_BTREE:
386 case XFS_INODE_FORMAT_EXT:
387 {
388 grub_ssize_t numread;
389 char *dirblock;
390 unsigned int blk;
391
392 dirblock = grub_malloc (dir->data->bsize);
393 if (!dirblock)
394 return 0;
395
396 /* Iterate over every block the directory has. */
397 for (blk = 0;
398 blk < (grub_be_to_cpu64 (dir->inode.size)
399 >> dir->data->sblock.log2_bsize);
400 blk++)
401 {
402 /* The header is skipped, the first direntry is stored
403 from byte 16. */
404 int pos = 16;
405 int entries;
406 int tail_start = (dir->data->bsize
407 - sizeof (struct grub_xfs_dirblock_tail));
408
409 struct grub_xfs_dirblock_tail *tail;
410 tail = (struct grub_xfs_dirblock_tail *) &dirblock[tail_start];
411
412 numread = grub_xfs_read_file (dir, 0,
413 blk << dir->data->sblock.log2_bsize,
414 dir->data->bsize, dirblock);
415
416 entries = (grub_be_to_cpu32 (tail->leaf_count)
417 - grub_be_to_cpu32 (tail->leaf_stale));
418
419 /* Iterate over all entries within this block. */
420 while (pos < (dir->data->bsize
421 - (int) sizeof (struct grub_xfs_dir2_entry)))
422 {
423 struct grub_xfs_dir2_entry *direntry;
424 grub_uint16_t *freetag;
425 char *filename;
426
427 direntry = (struct grub_xfs_dir2_entry *) &dirblock[pos];
428 freetag = (grub_uint16_t *) direntry;
429
430 if (*freetag == 0XFFFF)
431 {
432 grub_uint16_t *skip = (grub_uint16_t *) (freetag + 1);
433
434 /* This entry is not used, go to the next one. */
435 pos += grub_be_to_cpu16 (*skip);
436
437 continue;
438 }
439
440 filename = &dirblock[pos + sizeof (*direntry)];
441 /* The byte after the filename is for the tag, which
442 is not used by GRUB. So it can be overwritten. */
443 filename[direntry->len] = '\0';
444
445 if (call_hook (direntry->inode, filename))
446 {
447 grub_free (dirblock);
448 return 1;
449 }
450
451 /* Check if last direntry in this block is
452 reached. */
453 entries--;
454 if (!entries)
455 break;
456
457 /* Select the next directory entry. */
458 pos = GRUB_XFS_NEXT_DIRENT (pos, direntry->len);
459 pos = GRUB_XFS_ROUND_TO_DIRENT (pos);
460 }
461 }
462 grub_free (dirblock);
463 break;
464 }
465
466 default:
467 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
468 "xfs does not support inode format %d yet",
469 diro->inode.format);
470 }
471 return 0;
472}
473
474
475static struct grub_xfs_data *
476grub_xfs_mount (grub_disk_t disk)
477{
478 struct grub_xfs_data *data = 0;
479
480 data = grub_malloc (sizeof (struct grub_xfs_data));
481 if (!data)
482 return 0;
483
484 /* Read the superblock. */
485 if (grub_disk_read (disk, 0, 0,
486 sizeof (struct grub_xfs_sblock), (char *) &data->sblock))
487 goto fail;
488
489 if (grub_strncmp (data->sblock.magic, "XFSB", 4))
490 {
491 grub_error (GRUB_ERR_BAD_FS, "not a xfs filesystem");
492 goto fail;
493 }
494
495 data->diropen.data = data;
496 data->diropen.ino = data->sblock.rootino;
497 data->diropen.inode_read = 1;
498 data->bsize = grub_cpu_to_be32 (data->sblock.bsize);
499 data->agsize = grub_cpu_to_be32 (data->sblock.agsize);
500
501 data->disk = disk;
502 data->inode = &data->diropen.inode;
503 data->pos = 0;
504
505 grub_xfs_read_inode (data, data->diropen.ino, data->inode);
506
507 return data;
508 fail:
509
510 grub_free (data);
511
512 return 0;
513}
514
515\f
516static grub_err_t
517grub_xfs_dir (grub_device_t device, const char *path,
518 int (*hook) (const char *filename, int dir))
519{
520 struct grub_xfs_data *data = 0;;
521 struct grub_fshelp_node *fdiro = 0;
522
523 auto int NESTED_FUNC_ATTR iterate (const char *filename,
524 enum grub_fshelp_filetype filetype,
525 grub_fshelp_node_t node);
526
527 int NESTED_FUNC_ATTR iterate (const char *filename,
528 enum grub_fshelp_filetype filetype,
529 grub_fshelp_node_t node)
530 {
531 grub_free (node);
532
533 if (filetype == GRUB_FSHELP_DIR)
534 return hook (filename, 1);
535 else
536 return hook (filename, 0);
537
538 return 0;
539 }
540
541#ifndef GRUB_UTIL
542 grub_dl_ref (my_mod);
543#endif
544
545 data = grub_xfs_mount (device->disk);
546 if (!data)
547 goto fail;
548
549 grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
550 grub_xfs_read_symlink, GRUB_FSHELP_DIR);
551 if (grub_errno)
552 goto fail;
553
554 grub_xfs_iterate_dir (fdiro, iterate);
555
556 fail:
557 if (fdiro != &data->diropen)
558 grub_free (fdiro);
559 grub_free (data);
560
561#ifndef GRUB_UTIL
562 grub_dl_unref (my_mod);
563#endif
564
565 return grub_errno;
566
567 return 0;
568}
569
570
571/* Open a file named NAME and initialize FILE. */
572static grub_err_t
573grub_xfs_open (struct grub_file *file, const char *name)
574{
575 struct grub_xfs_data *data;
576 struct grub_fshelp_node *fdiro = 0;
577
578#ifndef GRUB_UTIL
579 grub_dl_ref (my_mod);
580#endif
581
582 data = grub_xfs_mount (file->device->disk);
583 if (!data)
584 goto fail;
585
586 grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
587 grub_xfs_read_symlink, GRUB_FSHELP_REG);
588 if (grub_errno)
589 goto fail;
590
591 if (!fdiro->inode_read)
592 {
593 grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
594 if (grub_errno)
595 goto fail;
596 }
597
598 grub_memcpy (data->inode,
599 &fdiro->inode,
600 sizeof (struct grub_xfs_inode));
601 grub_free (fdiro);
602
603 file->size = grub_be_to_cpu64 (data->inode->size);
604 file->data = data;
605 file->offset = 0;
606
607 return 0;
608
609 fail:
610 if (fdiro != &data->diropen)
611 grub_free (fdiro);
612 grub_free (data);
613
614#ifndef GRUB_UTIL
615 grub_dl_unref (my_mod);
616#endif
617
618 return grub_errno;
619}
620
621
622static grub_ssize_t
623grub_xfs_read (grub_file_t file, char *buf, grub_ssize_t len)
624{
625 struct grub_xfs_data *data =
626 (struct grub_xfs_data *) file->data;
627
628 return grub_xfs_read_file (&data->diropen, file->read_hook,
629 file->offset, len, buf);
630}
631
632
633static grub_err_t
634grub_xfs_close (grub_file_t file)
635{
636 grub_free (file->data);
637
638#ifndef GRUB_UTIL
639 grub_dl_unref (my_mod);
640#endif
641
642 return GRUB_ERR_NONE;
643}
644
645
646static grub_err_t
647grub_xfs_label (grub_device_t device, char **label)
648{
649 struct grub_xfs_data *data;
650 grub_disk_t disk = device->disk;
651
652#ifndef GRUB_UTIL
653 grub_dl_ref (my_mod);
654#endif
655
656 data = grub_xfs_mount (disk);
657 if (data)
658 *label = grub_strndup (data->sblock.label, 12);
659 else
660 *label = 0;
661
662#ifndef GRUB_UTIL
663 grub_dl_unref (my_mod);
664#endif
665
666 grub_free (data);
667
668 return grub_errno;
669}
670
671\f
672
673static struct grub_fs grub_xfs_fs =
674 {
675 .name = "xfs",
676 .dir = grub_xfs_dir,
677 .open = grub_xfs_open,
678 .read = grub_xfs_read,
679 .close = grub_xfs_close,
680 .label = grub_xfs_label,
681 .next = 0
682 };
683
684#ifdef GRUB_UTIL
685void
686grub_xfs_init (void)
687{
688 grub_fs_register (&grub_xfs_fs);
689}
690
691void
692grub_xfs_fini (void)
693{
694 grub_fs_unregister (&grub_xfs_fs);
695}
696#else /* ! GRUB_UTIL */
697GRUB_MOD_INIT
698{
699 grub_fs_register (&grub_xfs_fs);
700 my_mod = mod;
701}
702
703GRUB_MOD_FINI
704{
705 grub_fs_unregister (&grub_xfs_fs);
706}
707#endif /* ! GRUB_UTIL */