9f66dd6e4c6491e6b2cbfc7866335b432c824502
[grub.git] / grub-core / fs / xfs.c
1 /* xfs.c - XFS.  */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 2005,2006,2007,2008,2009  Free Software Foundation, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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>
28
29 GRUB_MOD_LICENSE ("GPLv3+");
30
31 #define XFS_INODE_EXTENTS       9
32
33 #define XFS_INODE_FORMAT_INO    1
34 #define XFS_INODE_FORMAT_EXT    2
35 #define XFS_INODE_FORMAT_BTREE  3
36
37 /* Superblock version field flags */
38 #define XFS_SB_VERSION_NUMBITS          0x000f
39 #define XFS_SB_VERSION_ATTRBIT          0x0010
40 #define XFS_SB_VERSION_NLINKBIT         0x0020
41 #define XFS_SB_VERSION_QUOTABIT         0x0040
42 #define XFS_SB_VERSION_ALIGNBIT         0x0080
43 #define XFS_SB_VERSION_DALIGNBIT        0x0100
44 #define XFS_SB_VERSION_LOGV2BIT         0x0400
45 #define XFS_SB_VERSION_SECTORBIT        0x0800
46 #define XFS_SB_VERSION_EXTFLGBIT        0x1000
47 #define XFS_SB_VERSION_DIRV2BIT         0x2000
48 #define XFS_SB_VERSION_MOREBITSBIT      0x8000
49 #define XFS_SB_VERSION_BITS_SUPPORTED \
50         (XFS_SB_VERSION_NUMBITS | \
51          XFS_SB_VERSION_ATTRBIT | \
52          XFS_SB_VERSION_NLINKBIT | \
53          XFS_SB_VERSION_QUOTABIT | \
54          XFS_SB_VERSION_ALIGNBIT | \
55          XFS_SB_VERSION_DALIGNBIT | \
56          XFS_SB_VERSION_LOGV2BIT | \
57          XFS_SB_VERSION_SECTORBIT | \
58          XFS_SB_VERSION_EXTFLGBIT | \
59          XFS_SB_VERSION_DIRV2BIT | \
60          XFS_SB_VERSION_MOREBITSBIT)
61
62 /* Recognized xfs format versions */
63 #define XFS_SB_VERSION_4                4       /* Good old XFS filesystem */
64 #define XFS_SB_VERSION_5                5       /* CRC enabled filesystem */
65
66 /* features2 field flags */
67 #define XFS_SB_VERSION2_LAZYSBCOUNTBIT  0x00000002      /* Superblk counters */
68 #define XFS_SB_VERSION2_ATTR2BIT        0x00000008      /* Inline attr rework */
69 #define XFS_SB_VERSION2_PROJID32BIT     0x00000080      /* 32-bit project ids */
70 #define XFS_SB_VERSION2_FTYPE           0x00000200      /* inode type in dir */
71 #define XFS_SB_VERSION2_BITS_SUPPORTED \
72         (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
73          XFS_SB_VERSION2_ATTR2BIT | \
74          XFS_SB_VERSION2_PROJID32BIT | \
75          XFS_SB_VERSION2_FTYPE)
76
77 /* incompat feature flags */
78 #define XFS_SB_FEAT_INCOMPAT_FTYPE      (1 << 0)        /* filetype in dirent */
79 #define XFS_SB_FEAT_INCOMPAT_SPINODES   (1 << 1)        /* sparse inode chunks */
80 #define XFS_SB_FEAT_INCOMPAT_META_UUID  (1 << 2)        /* metadata UUID */
81
82 /* We do not currently verify metadata UUID so it is safe to read such filesystem */
83 #define XFS_SB_FEAT_INCOMPAT_SUPPORTED \
84         (XFS_SB_FEAT_INCOMPAT_FTYPE | \
85          XFS_SB_FEAT_INCOMPAT_META_UUID)
86
87 struct grub_xfs_sblock
88 {
89   grub_uint8_t magic[4];
90   grub_uint32_t bsize;
91   grub_uint8_t unused1[24];
92   grub_uint16_t uuid[8];
93   grub_uint8_t unused2[8];
94   grub_uint64_t rootino;
95   grub_uint8_t unused3[20];
96   grub_uint32_t agsize;
97   grub_uint8_t unused4[12];
98   grub_uint16_t version;
99   grub_uint8_t unused5[6];
100   grub_uint8_t label[12];
101   grub_uint8_t log2_bsize;
102   grub_uint8_t log2_sect;
103   grub_uint8_t log2_inode;
104   grub_uint8_t log2_inop;
105   grub_uint8_t log2_agblk;
106   grub_uint8_t unused6[67];
107   grub_uint8_t log2_dirblk;
108   grub_uint8_t unused7[7];
109   grub_uint32_t features2;
110   grub_uint8_t unused8[4];
111   grub_uint32_t sb_features_compat;
112   grub_uint32_t sb_features_ro_compat;
113   grub_uint32_t sb_features_incompat;
114   grub_uint32_t sb_features_log_incompat;
115 } GRUB_PACKED;
116
117 struct grub_xfs_dir_header
118 {
119   grub_uint8_t count;
120   grub_uint8_t largeino;
121   union
122   {
123     grub_uint32_t i4;
124     grub_uint64_t i8;
125   } GRUB_PACKED parent;
126 } GRUB_PACKED;
127
128 /* Structure for directory entry inlined in the inode */
129 struct grub_xfs_dir_entry
130 {
131   grub_uint8_t len;
132   grub_uint16_t offset;
133   char name[1];
134   /* Inode number follows, 32 / 64 bits.  */
135 } GRUB_PACKED;
136
137 /* Structure for directory entry in a block */
138 struct grub_xfs_dir2_entry
139 {
140   grub_uint64_t inode;
141   grub_uint8_t len;
142 } GRUB_PACKED;
143
144 struct grub_xfs_extent
145 {
146   /* This should be a bitfield but bietfields are unportable, so just have
147      a raw array and functions extracting useful info from it.
148    */
149   grub_uint32_t raw[4];
150 } GRUB_PACKED;
151
152 struct grub_xfs_btree_node
153 {
154   grub_uint8_t magic[4];
155   grub_uint16_t level;
156   grub_uint16_t numrecs;
157   grub_uint64_t left;
158   grub_uint64_t right;
159   /* In V5 here follow crc, uuid, etc. */
160   /* Then follow keys and block pointers */
161 } GRUB_PACKED;
162
163 struct grub_xfs_btree_root
164 {
165   grub_uint16_t level;
166   grub_uint16_t numrecs;
167   grub_uint64_t keys[1];
168 } GRUB_PACKED;
169
170 struct grub_xfs_time
171 {
172   grub_uint32_t sec;
173   grub_uint32_t nanosec;
174 } GRUB_PACKED;
175
176 struct grub_xfs_inode
177 {
178   grub_uint8_t magic[2];
179   grub_uint16_t mode;
180   grub_uint8_t version;
181   grub_uint8_t format;
182   grub_uint8_t unused2[26];
183   struct grub_xfs_time atime;
184   struct grub_xfs_time mtime;
185   struct grub_xfs_time ctime;
186   grub_uint64_t size;
187   grub_uint64_t nblocks;
188   grub_uint32_t extsize;
189   grub_uint32_t nextents;
190   grub_uint16_t unused3;
191   grub_uint8_t fork_offset;
192   grub_uint8_t unused4[17];
193 } GRUB_PACKED;
194
195 #define XFS_V2_INODE_SIZE sizeof(struct grub_xfs_inode)
196 #define XFS_V3_INODE_SIZE (XFS_V2_INODE_SIZE + 76)
197
198 struct grub_xfs_dirblock_tail
199 {
200   grub_uint32_t leaf_count;
201   grub_uint32_t leaf_stale;
202 } GRUB_PACKED;
203
204 struct grub_fshelp_node
205 {
206   struct grub_xfs_data *data;
207   grub_uint64_t ino;
208   int inode_read;
209   struct grub_xfs_inode inode;
210 };
211
212 struct grub_xfs_data
213 {
214   struct grub_xfs_sblock sblock;
215   grub_disk_t disk;
216   int pos;
217   int bsize;
218   grub_uint32_t agsize;
219   unsigned int hasftype:1;
220   unsigned int hascrc:1;
221   struct grub_fshelp_node diropen;
222 };
223
224 static grub_dl_t my_mod;
225
226 \f
227
228 static int grub_xfs_sb_hascrc(struct grub_xfs_data *data)
229 {
230   return (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
231           grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5);
232 }
233
234 static int grub_xfs_sb_hasftype(struct grub_xfs_data *data)
235 {
236   if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
237         grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5) &&
238       data->sblock.sb_features_incompat & grub_cpu_to_be32_compile_time(XFS_SB_FEAT_INCOMPAT_FTYPE))
239     return 1;
240   if (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
241       data->sblock.features2 & grub_cpu_to_be32_compile_time(XFS_SB_VERSION2_FTYPE))
242     return 1;
243   return 0;
244 }
245
246 static int grub_xfs_sb_valid(struct grub_xfs_data *data)
247 {
248   grub_dprintf("xfs", "Validating superblock\n");
249   if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4)
250       || data->sblock.log2_bsize < GRUB_DISK_SECTOR_BITS
251       || ((int) data->sblock.log2_bsize
252           + (int) data->sblock.log2_dirblk) >= 27)
253     {
254       grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem");
255       return 0;
256     }
257   if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
258        grub_cpu_to_be16_compile_time(XFS_SB_VERSION_5))
259     {
260       grub_dprintf("xfs", "XFS v5 superblock detected\n");
261       if (data->sblock.sb_features_incompat &
262           grub_cpu_to_be32_compile_time(~XFS_SB_FEAT_INCOMPAT_SUPPORTED))
263         {
264           grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported "
265                       "incompatible features");
266           return 0;
267         }
268       return 1;
269     }
270   else if ((data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_NUMBITS)) ==
271            grub_cpu_to_be16_compile_time(XFS_SB_VERSION_4))
272     {
273       grub_dprintf("xfs", "XFS v4 superblock detected\n");
274       if (!(data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_DIRV2BIT)))
275         {
276           grub_error (GRUB_ERR_BAD_FS, "XFS filesystem without V2 directories "
277                       "is unsupported");
278           return 0;
279         }
280       if (data->sblock.version & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION_BITS_SUPPORTED) ||
281           (data->sblock.version & grub_cpu_to_be16_compile_time(XFS_SB_VERSION_MOREBITSBIT) &&
282            data->sblock.features2 & grub_cpu_to_be16_compile_time(~XFS_SB_VERSION2_BITS_SUPPORTED)))
283         {
284           grub_error (GRUB_ERR_BAD_FS, "XFS filesystem has unsupported version "
285                       "bits");
286           return 0;
287         }
288       return 1;
289     }
290   return 0;
291 }
292
293 /* Filetype information as used in inodes.  */
294 #define FILETYPE_INO_MASK       0170000
295 #define FILETYPE_INO_REG        0100000
296 #define FILETYPE_INO_DIRECTORY  0040000
297 #define FILETYPE_INO_SYMLINK    0120000
298
299 static inline int
300 GRUB_XFS_INO_AGBITS(struct grub_xfs_data *data)
301 {
302   return ((data)->sblock.log2_agblk + (data)->sblock.log2_inop);
303 }
304
305 static inline grub_uint64_t
306 GRUB_XFS_INO_INOINAG (struct grub_xfs_data *data,
307                       grub_uint64_t ino)
308 {
309   return (ino & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1));
310 }
311
312 static inline grub_uint64_t
313 GRUB_XFS_INO_AG (struct grub_xfs_data *data,
314                  grub_uint64_t ino)
315 {
316   return (ino >> GRUB_XFS_INO_AGBITS (data));
317 }
318
319 static inline grub_disk_addr_t
320 GRUB_XFS_FSB_TO_BLOCK (struct grub_xfs_data *data, grub_disk_addr_t fsb)
321 {
322   return ((fsb >> data->sblock.log2_agblk) * data->agsize
323           + (fsb & ((1LL << data->sblock.log2_agblk) - 1)));
324 }
325
326 static inline grub_uint64_t
327 GRUB_XFS_EXTENT_OFFSET (struct grub_xfs_extent *exts, int ex)
328 {
329   return ((grub_be_to_cpu32 (exts[ex].raw[0]) & ~(1 << 31)) << 23
330           | grub_be_to_cpu32 (exts[ex].raw[1]) >> 9);
331 }
332
333 static inline grub_uint64_t
334 GRUB_XFS_EXTENT_BLOCK (struct grub_xfs_extent *exts, int ex)
335 {
336   return ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex].raw[1])
337                            & (0x1ff)) << 43
338           | (grub_uint64_t) grub_be_to_cpu32 (exts[ex].raw[2]) << 11
339           | grub_be_to_cpu32 (exts[ex].raw[3]) >> 21);
340 }
341
342 static inline grub_uint64_t
343 GRUB_XFS_EXTENT_SIZE (struct grub_xfs_extent *exts, int ex)
344 {
345   return (grub_be_to_cpu32 (exts[ex].raw[3]) & ((1 << 21) - 1));
346 }
347
348 \f
349 static inline grub_uint64_t
350 grub_xfs_inode_block (struct grub_xfs_data *data,
351                       grub_uint64_t ino)
352 {
353   long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino);
354   long long ag = GRUB_XFS_INO_AG (data, ino);
355   long long block;
356
357   block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize;
358   block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS);
359   return block;
360 }
361
362
363 static inline int
364 grub_xfs_inode_offset (struct grub_xfs_data *data,
365                        grub_uint64_t ino)
366 {
367   int inoag = GRUB_XFS_INO_INOINAG (data, ino);
368   return ((inoag & ((1 << data->sblock.log2_inop) - 1)) <<
369           data->sblock.log2_inode);
370 }
371
372 static inline grub_size_t
373 grub_xfs_inode_size(struct grub_xfs_data *data)
374 {
375   return (grub_size_t)1 << data->sblock.log2_inode;
376 }
377
378 /*
379  * Returns size occupied by XFS inode stored in memory - we store struct
380  * grub_fshelp_node there but on disk inode size may be actually larger than
381  * struct grub_xfs_inode so we need to account for that so that we can read
382  * from disk directly into in-memory structure.
383  */
384 static inline grub_size_t
385 grub_xfs_fshelp_size(struct grub_xfs_data *data)
386 {
387   return sizeof (struct grub_fshelp_node) - sizeof (struct grub_xfs_inode)
388                + grub_xfs_inode_size(data);
389 }
390
391 /* This should return void * but XFS code is error-prone with alignment, so
392    return char to retain cast-align.
393  */
394 static char *
395 grub_xfs_inode_data(struct grub_xfs_inode *inode)
396 {
397         if (inode->version <= 2)
398                 return ((char *)inode) + XFS_V2_INODE_SIZE;
399         return ((char *)inode) + XFS_V3_INODE_SIZE;
400 }
401
402 static struct grub_xfs_dir_entry *
403 grub_xfs_inline_de(struct grub_xfs_dir_header *head)
404 {
405   /*
406     With small inode numbers the header is 4 bytes smaller because of
407     smaller parent pointer
408   */
409   return (struct grub_xfs_dir_entry *)
410     (((char *) head) + sizeof(struct grub_xfs_dir_header) -
411      (head->largeino ? 0 : sizeof(grub_uint32_t)));
412 }
413
414 static grub_uint8_t *
415 grub_xfs_inline_de_inopos(struct grub_xfs_data *data,
416                           struct grub_xfs_dir_entry *de)
417 {
418   return ((grub_uint8_t *)(de + 1)) + de->len - 1 + (data->hasftype ? 1 : 0);
419 }
420
421 static struct grub_xfs_dir_entry *
422 grub_xfs_inline_next_de(struct grub_xfs_data *data,
423                         struct grub_xfs_dir_header *head,
424                         struct grub_xfs_dir_entry *de)
425 {
426   char *p = (char *)de + sizeof(struct grub_xfs_dir_entry) - 1 + de->len;
427
428   p += head->largeino ? sizeof(grub_uint64_t) : sizeof(grub_uint32_t);
429   if (data->hasftype)
430     p++;
431
432   return (struct grub_xfs_dir_entry *)p;
433 }
434
435 static struct grub_xfs_dirblock_tail *
436 grub_xfs_dir_tail(struct grub_xfs_data *data, void *dirblock)
437 {
438   int dirblksize = 1 << (data->sblock.log2_bsize + data->sblock.log2_dirblk);
439
440   return (struct grub_xfs_dirblock_tail *)
441     ((char *)dirblock + dirblksize - sizeof (struct grub_xfs_dirblock_tail));
442 }
443
444 static struct grub_xfs_dir2_entry *
445 grub_xfs_first_de(struct grub_xfs_data *data, void *dirblock)
446 {
447   if (data->hascrc)
448     return (struct grub_xfs_dir2_entry *)((char *)dirblock + 64);
449   return (struct grub_xfs_dir2_entry *)((char *)dirblock + 16);
450 }
451
452 static struct grub_xfs_dir2_entry *
453 grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
454 {
455   int size = sizeof (struct grub_xfs_dir2_entry) + de->len + 2 /* Tag */;
456
457   if (data->hasftype)
458     size++;             /* File type */
459   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
460 }
461
462 /* This should return void * but XFS code is error-prone with alignment, so
463    return char to retain cast-align.
464  */
465 static char *
466 grub_xfs_btree_keys(struct grub_xfs_data *data,
467                     struct grub_xfs_btree_node *leaf)
468 {
469   char *keys = (char *)(leaf + 1);
470
471   if (data->hascrc)
472     keys += 48; /* skip crc, uuid, ... */
473   return keys;
474 }
475
476 static grub_err_t
477 grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
478                      struct grub_xfs_inode *inode)
479 {
480   grub_uint64_t block = grub_xfs_inode_block (data, ino);
481   int offset = grub_xfs_inode_offset (data, ino);
482
483   grub_dprintf("xfs", "Reading inode (%"PRIuGRUB_UINT64_T") - %"PRIuGRUB_UINT64_T", %d\n",
484                ino, block, offset);
485   /* Read the inode.  */
486   if (grub_disk_read (data->disk, block, offset, grub_xfs_inode_size(data),
487                       inode))
488     return grub_errno;
489
490   if (grub_strncmp ((char *) inode->magic, "IN", 2))
491     return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode");
492
493   return 0;
494 }
495
496 static grub_uint64_t
497 get_fsb (const void *keys, int idx)
498 {
499   const char *p = (const char *) keys + sizeof(grub_uint64_t) * idx;
500   return grub_be_to_cpu64 (grub_get_unaligned64 (p));
501 }
502
503 static grub_disk_addr_t
504 grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
505 {
506   struct grub_xfs_btree_node *leaf = 0;
507   int ex, nrec;
508   struct grub_xfs_extent *exts;
509   grub_uint64_t ret = 0;
510
511   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
512     {
513       struct grub_xfs_btree_root *root;
514       const char *keys;
515       int recoffset;
516
517       leaf = grub_malloc (node->data->bsize);
518       if (leaf == 0)
519         return 0;
520
521       root = (struct grub_xfs_btree_root *) grub_xfs_inode_data(&node->inode);
522       nrec = grub_be_to_cpu16 (root->numrecs);
523       keys = (char *) &root->keys[0];
524       if (node->inode.fork_offset)
525         recoffset = (node->inode.fork_offset - 1) / 2;
526       else
527         recoffset = (grub_xfs_inode_size(node->data)
528                      - ((char *) keys - (char *) &node->inode))
529                                 / (2 * sizeof (grub_uint64_t));
530       do
531         {
532           int i;
533
534           for (i = 0; i < nrec; i++)
535             {
536               if (fileblock < get_fsb(keys, i))
537                 break;
538             }
539
540           /* Sparse block.  */
541           if (i == 0)
542             {
543               grub_free (leaf);
544               return 0;
545             }
546
547           if (grub_disk_read (node->data->disk,
548                               GRUB_XFS_FSB_TO_BLOCK (node->data, get_fsb (keys, i - 1 + recoffset)) << (node->data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS),
549                               0, node->data->bsize, leaf))
550             return 0;
551
552           if ((!node->data->hascrc &&
553                grub_strncmp ((char *) leaf->magic, "BMAP", 4)) ||
554               (node->data->hascrc &&
555                grub_strncmp ((char *) leaf->magic, "BMA3", 4)))
556             {
557               grub_free (leaf);
558               grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node");
559               return 0;
560             }
561
562           nrec = grub_be_to_cpu16 (leaf->numrecs);
563           keys = grub_xfs_btree_keys(node->data, leaf);
564           recoffset = ((node->data->bsize - ((char *) keys
565                                              - (char *) leaf))
566                        / (2 * sizeof (grub_uint64_t)));
567         }
568       while (leaf->level);
569       exts = (struct grub_xfs_extent *) keys;
570     }
571   else if (node->inode.format == XFS_INODE_FORMAT_EXT)
572     {
573       nrec = grub_be_to_cpu32 (node->inode.nextents);
574       exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode);
575     }
576   else
577     {
578       grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
579                   "XFS does not support inode format %d yet",
580                   node->inode.format);
581       return 0;
582     }
583
584   /* Iterate over each extent to figure out which extent has
585      the block we are looking for.  */
586   for (ex = 0; ex < nrec; ex++)
587     {
588       grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex);
589       grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex);
590       grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex);
591
592       /* Sparse block.  */
593       if (fileblock < offset)
594         break;
595       else if (fileblock < offset + size)
596         {
597           ret = (fileblock - offset + start);
598           break;
599         }
600     }
601
602   grub_free (leaf);
603
604   return GRUB_XFS_FSB_TO_BLOCK(node->data, ret);
605 }
606
607
608 /* Read LEN bytes from the file described by DATA starting with byte
609    POS.  Return the amount of read bytes in READ.  */
610 static grub_ssize_t
611 grub_xfs_read_file (grub_fshelp_node_t node,
612                     grub_disk_read_hook_t read_hook, void *read_hook_data,
613                     grub_off_t pos, grub_size_t len, char *buf, grub_uint32_t header_size)
614 {
615   return grub_fshelp_read_file (node->data->disk, node,
616                                 read_hook, read_hook_data,
617                                 pos, len, buf, grub_xfs_read_block,
618                                 grub_be_to_cpu64 (node->inode.size) + header_size,
619                                 node->data->sblock.log2_bsize
620                                 - GRUB_DISK_SECTOR_BITS, 0);
621 }
622
623
624 static char *
625 grub_xfs_read_symlink (grub_fshelp_node_t node)
626 {
627   grub_ssize_t size = grub_be_to_cpu64 (node->inode.size);
628
629   if (size < 0)
630     {
631       grub_error (GRUB_ERR_BAD_FS, "invalid symlink");
632       return 0;
633     }
634
635   switch (node->inode.format)
636     {
637     case XFS_INODE_FORMAT_INO:
638       return grub_strndup (grub_xfs_inode_data(&node->inode), size);
639
640     case XFS_INODE_FORMAT_EXT:
641       {
642         char *symlink;
643         grub_ssize_t numread;
644         int off = 0;
645
646         if (node->data->hascrc)
647           off = 56;
648
649         symlink = grub_malloc (size + 1);
650         if (!symlink)
651           return 0;
652
653         node->inode.size = grub_be_to_cpu64 (size + off);
654         numread = grub_xfs_read_file (node, 0, 0, off, size, symlink, off);
655         if (numread != size)
656           {
657             grub_free (symlink);
658             return 0;
659           }
660         symlink[size] = '\0';
661         return symlink;
662       }
663     }
664
665   return 0;
666 }
667
668
669 static enum grub_fshelp_filetype
670 grub_xfs_mode_to_filetype (grub_uint16_t mode)
671 {
672   if ((grub_be_to_cpu16 (mode)
673        & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
674     return GRUB_FSHELP_DIR;
675   else if ((grub_be_to_cpu16 (mode)
676             & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
677     return GRUB_FSHELP_SYMLINK;
678   else if ((grub_be_to_cpu16 (mode)
679             & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
680     return GRUB_FSHELP_REG;
681   return GRUB_FSHELP_UNKNOWN;
682 }
683
684
685 /* Context for grub_xfs_iterate_dir.  */
686 struct grub_xfs_iterate_dir_ctx
687 {
688   grub_fshelp_iterate_dir_hook_t hook;
689   void *hook_data;
690   struct grub_fshelp_node *diro;
691 };
692
693 /* Helper for grub_xfs_iterate_dir.  */
694 static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename,
695                                   struct grub_xfs_iterate_dir_ctx *ctx)
696 {
697   struct grub_fshelp_node *fdiro;
698   grub_err_t err;
699
700   fdiro = grub_malloc (grub_xfs_fshelp_size(ctx->diro->data) + 1);
701   if (!fdiro)
702     {
703       grub_print_error ();
704       return 0;
705     }
706
707   /* The inode should be read, otherwise the filetype can
708      not be determined.  */
709   fdiro->ino = ino;
710   fdiro->inode_read = 1;
711   fdiro->data = ctx->diro->data;
712   err = grub_xfs_read_inode (ctx->diro->data, ino, &fdiro->inode);
713   if (err)
714     {
715       grub_print_error ();
716       return 0;
717     }
718
719   return ctx->hook (filename, grub_xfs_mode_to_filetype (fdiro->inode.mode),
720                     fdiro, ctx->hook_data);
721 }
722
723 static int
724 grub_xfs_iterate_dir (grub_fshelp_node_t dir,
725                       grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
726 {
727   struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
728   struct grub_xfs_iterate_dir_ctx ctx = {
729     .hook = hook,
730     .hook_data = hook_data,
731     .diro = diro
732   };
733
734   switch (diro->inode.format)
735     {
736     case XFS_INODE_FORMAT_INO:
737       {
738         struct grub_xfs_dir_header *head = (struct grub_xfs_dir_header *) grub_xfs_inode_data(&diro->inode);
739         struct grub_xfs_dir_entry *de = grub_xfs_inline_de(head);
740         int smallino = !head->largeino;
741         int i;
742         grub_uint64_t parent;
743
744         /* If small inode numbers are used to pack the direntry, the
745            parent inode number is small too.  */
746         if (smallino)
747           parent = grub_be_to_cpu32 (head->parent.i4);
748         else
749           parent = grub_be_to_cpu64 (head->parent.i8);
750
751         /* Synthesize the direntries for `.' and `..'.  */
752         if (iterate_dir_call_hook (diro->ino, ".", &ctx))
753           return 1;
754
755         if (iterate_dir_call_hook (parent, "..", &ctx))
756           return 1;
757
758         for (i = 0; i < head->count; i++)
759           {
760             grub_uint64_t ino;
761             grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de);
762             grub_uint8_t c;
763
764             /* inopos might be unaligned.  */
765             if (smallino)
766               ino = (((grub_uint32_t) inopos[0]) << 24)
767                 | (((grub_uint32_t) inopos[1]) << 16)
768                 | (((grub_uint32_t) inopos[2]) << 8)
769                 | (((grub_uint32_t) inopos[3]) << 0);
770             else
771               ino = (((grub_uint64_t) inopos[0]) << 56)
772                 | (((grub_uint64_t) inopos[1]) << 48)
773                 | (((grub_uint64_t) inopos[2]) << 40)
774                 | (((grub_uint64_t) inopos[3]) << 32)
775                 | (((grub_uint64_t) inopos[4]) << 24)
776                 | (((grub_uint64_t) inopos[5]) << 16)
777                 | (((grub_uint64_t) inopos[6]) << 8)
778                 | (((grub_uint64_t) inopos[7]) << 0);
779
780             c = de->name[de->len];
781             de->name[de->len] = '\0';
782             if (iterate_dir_call_hook (ino, de->name, &ctx))
783               {
784                 de->name[de->len] = c;
785                 return 1;
786               }
787             de->name[de->len] = c;
788
789             de = grub_xfs_inline_next_de(dir->data, head, de);
790           }
791         break;
792       }
793
794     case XFS_INODE_FORMAT_BTREE:
795     case XFS_INODE_FORMAT_EXT:
796       {
797         grub_ssize_t numread;
798         char *dirblock;
799         grub_uint64_t blk;
800         int dirblk_size, dirblk_log2;
801
802         dirblk_log2 = (dir->data->sblock.log2_bsize
803                        + dir->data->sblock.log2_dirblk);
804         dirblk_size = 1 << dirblk_log2;
805
806         dirblock = grub_malloc (dirblk_size);
807         if (! dirblock)
808           return 0;
809
810         /* Iterate over every block the directory has.  */
811         for (blk = 0;
812              blk < (grub_be_to_cpu64 (dir->inode.size)
813                     >> dirblk_log2);
814              blk++)
815           {
816             struct grub_xfs_dir2_entry *direntry =
817                                         grub_xfs_first_de(dir->data, dirblock);
818             int entries;
819             struct grub_xfs_dirblock_tail *tail =
820                                         grub_xfs_dir_tail(dir->data, dirblock);
821
822             numread = grub_xfs_read_file (dir, 0, 0,
823                                           blk << dirblk_log2,
824                                           dirblk_size, dirblock, 0);
825             if (numread != dirblk_size)
826               return 0;
827
828             entries = (grub_be_to_cpu32 (tail->leaf_count)
829                        - grub_be_to_cpu32 (tail->leaf_stale));
830
831             /* Iterate over all entries within this block.  */
832             while ((char *)direntry < (char *)tail)
833               {
834                 grub_uint8_t *freetag;
835                 char *filename;
836
837                 freetag = (grub_uint8_t *) direntry;
838
839                 if (grub_get_unaligned16 (freetag) == 0XFFFF)
840                   {
841                     grub_uint8_t *skip = (freetag + sizeof (grub_uint16_t));
842
843                     /* This entry is not used, go to the next one.  */
844                     direntry = (struct grub_xfs_dir2_entry *)
845                                 (((char *)direntry) +
846                                 grub_be_to_cpu16 (grub_get_unaligned16 (skip)));
847
848                     continue;
849                   }
850
851                 filename = (char *)(direntry + 1);
852                 /* The byte after the filename is for the filetype, padding, or
853                    tag, which is not used by GRUB.  So it can be overwritten. */
854                 filename[direntry->len] = '\0';
855
856                 if (iterate_dir_call_hook (grub_be_to_cpu64(direntry->inode), 
857                                            filename, &ctx))
858                   {
859                     grub_free (dirblock);
860                     return 1;
861                   }
862
863                 /* Check if last direntry in this block is
864                    reached.  */
865                 entries--;
866                 if (!entries)
867                   break;
868
869                 /* Select the next directory entry.  */
870                 direntry = grub_xfs_next_de(dir->data, direntry);
871               }
872           }
873         grub_free (dirblock);
874         break;
875       }
876
877     default:
878       grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
879                   "XFS does not support inode format %d yet",
880                   diro->inode.format);
881     }
882   return 0;
883 }
884
885
886 static struct grub_xfs_data *
887 grub_xfs_mount (grub_disk_t disk)
888 {
889   struct grub_xfs_data *data = 0;
890
891   data = grub_zalloc (sizeof (struct grub_xfs_data));
892   if (!data)
893     return 0;
894
895   grub_dprintf("xfs", "Reading sb\n");
896   /* Read the superblock.  */
897   if (grub_disk_read (disk, 0, 0,
898                       sizeof (struct grub_xfs_sblock), &data->sblock))
899     goto fail;
900
901   if (!grub_xfs_sb_valid(data))
902     goto fail;
903
904   data = grub_realloc (data,
905                        sizeof (struct grub_xfs_data)
906                        - sizeof (struct grub_xfs_inode)
907                        + grub_xfs_inode_size(data) + 1);
908
909   if (! data)
910     goto fail;
911
912   data->diropen.data = data;
913   data->diropen.ino = grub_be_to_cpu64(data->sblock.rootino);
914   data->diropen.inode_read = 1;
915   data->bsize = grub_be_to_cpu32 (data->sblock.bsize);
916   data->agsize = grub_be_to_cpu32 (data->sblock.agsize);
917   data->hasftype = grub_xfs_sb_hasftype(data);
918   data->hascrc = grub_xfs_sb_hascrc(data);
919
920   data->disk = disk;
921   data->pos = 0;
922   grub_dprintf("xfs", "Reading root ino %"PRIuGRUB_UINT64_T"\n",
923                grub_cpu_to_be64(data->sblock.rootino));
924
925   grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode);
926
927   return data;
928  fail:
929
930   if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
931     grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem");
932
933   grub_free (data);
934
935   return 0;
936 }
937
938 \f
939 /* Context for grub_xfs_dir.  */
940 struct grub_xfs_dir_ctx
941 {
942   grub_fs_dir_hook_t hook;
943   void *hook_data;
944 };
945
946 /* Helper for grub_xfs_dir.  */
947 static int
948 grub_xfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
949                    grub_fshelp_node_t node, void *data)
950 {
951   struct grub_xfs_dir_ctx *ctx = data;
952   struct grub_dirhook_info info;
953
954   grub_memset (&info, 0, sizeof (info));
955   if (node->inode_read)
956     {
957       info.mtimeset = 1;
958       info.mtime = grub_be_to_cpu32 (node->inode.mtime.sec);
959     }
960   info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
961   grub_free (node);
962   return ctx->hook (filename, &info, ctx->hook_data);
963 }
964
965 static grub_err_t
966 grub_xfs_dir (grub_device_t device, const char *path,
967               grub_fs_dir_hook_t hook, void *hook_data)
968 {
969   struct grub_xfs_dir_ctx ctx = { hook, hook_data };
970   struct grub_xfs_data *data = 0;
971   struct grub_fshelp_node *fdiro = 0;
972
973   grub_dl_ref (my_mod);
974
975   data = grub_xfs_mount (device->disk);
976   if (!data)
977     goto mount_fail;
978
979   grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir,
980                          grub_xfs_read_symlink, GRUB_FSHELP_DIR);
981   if (grub_errno)
982     goto fail;
983
984   grub_xfs_iterate_dir (fdiro, grub_xfs_dir_iter, &ctx);
985
986  fail:
987   if (fdiro != &data->diropen)
988     grub_free (fdiro);
989   grub_free (data);
990
991  mount_fail:
992
993   grub_dl_unref (my_mod);
994
995   return grub_errno;
996 }
997
998
999 /* Open a file named NAME and initialize FILE.  */
1000 static grub_err_t
1001 grub_xfs_open (struct grub_file *file, const char *name)
1002 {
1003   struct grub_xfs_data *data;
1004   struct grub_fshelp_node *fdiro = 0;
1005
1006   grub_dl_ref (my_mod);
1007
1008   data = grub_xfs_mount (file->device->disk);
1009   if (!data)
1010     goto mount_fail;
1011
1012   grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir,
1013                          grub_xfs_read_symlink, GRUB_FSHELP_REG);
1014   if (grub_errno)
1015     goto fail;
1016
1017   if (!fdiro->inode_read)
1018     {
1019       grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode);
1020       if (grub_errno)
1021         goto fail;
1022     }
1023
1024   if (fdiro != &data->diropen)
1025     {
1026       grub_memcpy (&data->diropen, fdiro, grub_xfs_fshelp_size(data));
1027       grub_free (fdiro);
1028     }
1029
1030   file->size = grub_be_to_cpu64 (data->diropen.inode.size);
1031   file->data = data;
1032   file->offset = 0;
1033
1034   return 0;
1035
1036  fail:
1037   if (fdiro != &data->diropen)
1038     grub_free (fdiro);
1039   grub_free (data);
1040
1041  mount_fail:
1042   grub_dl_unref (my_mod);
1043
1044   return grub_errno;
1045 }
1046
1047
1048 static grub_ssize_t
1049 grub_xfs_read (grub_file_t file, char *buf, grub_size_t len)
1050 {
1051   struct grub_xfs_data *data =
1052     (struct grub_xfs_data *) file->data;
1053
1054   return grub_xfs_read_file (&data->diropen,
1055                              file->read_hook, file->read_hook_data,
1056                              file->offset, len, buf, 0);
1057 }
1058
1059
1060 static grub_err_t
1061 grub_xfs_close (grub_file_t file)
1062 {
1063   grub_free (file->data);
1064
1065   grub_dl_unref (my_mod);
1066
1067   return GRUB_ERR_NONE;
1068 }
1069
1070
1071 static grub_err_t
1072 grub_xfs_label (grub_device_t device, char **label)
1073 {
1074   struct grub_xfs_data *data;
1075   grub_disk_t disk = device->disk;
1076
1077   grub_dl_ref (my_mod);
1078
1079   data = grub_xfs_mount (disk);
1080   if (data)
1081     *label = grub_strndup ((char *) (data->sblock.label), 12);
1082   else
1083     *label = 0;
1084
1085   grub_dl_unref (my_mod);
1086
1087   grub_free (data);
1088
1089   return grub_errno;
1090 }
1091
1092 static grub_err_t
1093 grub_xfs_uuid (grub_device_t device, char **uuid)
1094 {
1095   struct grub_xfs_data *data;
1096   grub_disk_t disk = device->disk;
1097
1098   grub_dl_ref (my_mod);
1099
1100   data = grub_xfs_mount (disk);
1101   if (data)
1102     {
1103       *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
1104                              grub_be_to_cpu16 (data->sblock.uuid[0]),
1105                              grub_be_to_cpu16 (data->sblock.uuid[1]),
1106                              grub_be_to_cpu16 (data->sblock.uuid[2]),
1107                              grub_be_to_cpu16 (data->sblock.uuid[3]),
1108                              grub_be_to_cpu16 (data->sblock.uuid[4]),
1109                              grub_be_to_cpu16 (data->sblock.uuid[5]),
1110                              grub_be_to_cpu16 (data->sblock.uuid[6]),
1111                              grub_be_to_cpu16 (data->sblock.uuid[7]));
1112     }
1113   else
1114     *uuid = NULL;
1115
1116   grub_dl_unref (my_mod);
1117
1118   grub_free (data);
1119
1120   return grub_errno;
1121 }
1122
1123 \f
1124
1125 static struct grub_fs grub_xfs_fs =
1126   {
1127     .name = "xfs",
1128     .dir = grub_xfs_dir,
1129     .open = grub_xfs_open,
1130     .read = grub_xfs_read,
1131     .close = grub_xfs_close,
1132     .label = grub_xfs_label,
1133     .uuid = grub_xfs_uuid,
1134 #ifdef GRUB_UTIL
1135     .reserved_first_sector = 0,
1136     .blocklist_install = 1,
1137 #endif
1138     .next = 0
1139   };
1140
1141 GRUB_MOD_INIT(xfs)
1142 {
1143   grub_fs_register (&grub_xfs_fs);
1144   my_mod = mod;
1145 }
1146
1147 GRUB_MOD_FINI(xfs)
1148 {
1149   grub_fs_unregister (&grub_xfs_fs);
1150 }