~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/fs/xfs.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

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