Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / fs / jfs.c
blobaab3e8c7b7daf786434ecf352dfe6a9420ab5394
1 /* jfs.c - JFS. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/err.h>
21 #include <grub/file.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
25 #include <grub/dl.h>
26 #include <grub/types.h>
27 #include <grub/charset.h>
28 #include <grub/i18n.h>
30 GRUB_MOD_LICENSE ("GPLv3+");
32 #define GRUB_JFS_MAX_SYMLNK_CNT 8
33 #define GRUB_JFS_FILETYPE_MASK 0170000
34 #define GRUB_JFS_FILETYPE_REG 0100000
35 #define GRUB_JFS_FILETYPE_LNK 0120000
36 #define GRUB_JFS_FILETYPE_DIR 0040000
38 #define GRUB_JFS_SBLOCK 64
39 #define GRUB_JFS_AGGR_INODE 2
40 #define GRUB_JFS_FS1_INODE_BLK 104
42 #define GRUB_JFS_TREE_LEAF 2
44 struct grub_jfs_sblock
46 /* The magic for JFS. It should contain the string "JFS1". */
47 grub_uint8_t magic[4];
48 grub_uint32_t version;
49 grub_uint64_t ag_size;
51 /* The size of a filesystem block in bytes. XXX: currently only
52 4096 was tested. */
53 grub_uint32_t blksz;
54 grub_uint16_t log2_blksz;
55 grub_uint8_t unused[14];
56 grub_uint32_t flags;
57 grub_uint8_t unused3[61];
58 char volname[11];
59 grub_uint8_t unused2[24];
60 grub_uint8_t uuid[16];
61 char volname2[16];
64 struct grub_jfs_extent
66 /* The length of the extent in filesystem blocks. */
67 grub_uint16_t length;
68 grub_uint8_t length2;
70 /* The physical offset of the first block on the disk. */
71 grub_uint8_t blk1;
72 grub_uint32_t blk2;
73 } GRUB_PACKED;
75 #define GRUB_JFS_IAG_INODES_OFFSET 3072
76 #define GRUB_JFS_IAG_INODES_COUNT 128
78 struct grub_jfs_iag
80 grub_uint8_t unused[GRUB_JFS_IAG_INODES_OFFSET];
81 struct grub_jfs_extent inodes[GRUB_JFS_IAG_INODES_COUNT];
82 } GRUB_PACKED;
85 /* The head of the tree used to find extents. */
86 struct grub_jfs_treehead
88 grub_uint64_t next;
89 grub_uint64_t prev;
91 grub_uint8_t flags;
92 grub_uint8_t unused;
94 grub_uint16_t count;
95 grub_uint16_t max;
96 grub_uint8_t unused2[10];
97 } GRUB_PACKED;
99 /* A node in the extent tree. */
100 struct grub_jfs_tree_extent
102 grub_uint8_t flags;
103 grub_uint16_t unused;
105 /* The offset is the key used to lookup an extent. */
106 grub_uint8_t offset1;
107 grub_uint32_t offset2;
109 struct grub_jfs_extent extent;
110 } GRUB_PACKED;
112 /* The tree of directory entries. */
113 struct grub_jfs_tree_dir
115 /* Pointers to the previous and next tree headers of other nodes on
116 this level. */
117 grub_uint64_t nextb;
118 grub_uint64_t prevb;
120 grub_uint8_t flags;
122 /* The amount of dirents in this node. */
123 grub_uint8_t count;
124 grub_uint8_t freecnt;
125 grub_uint8_t freelist;
126 grub_uint8_t maxslot;
128 /* The location of the sorted array of pointers to dirents. */
129 grub_uint8_t sindex;
130 grub_uint8_t unused[10];
131 } GRUB_PACKED;
133 /* An internal node in the dirents tree. */
134 struct grub_jfs_internal_dirent
136 struct grub_jfs_extent ex;
137 grub_uint8_t next;
138 grub_uint8_t len;
139 grub_uint16_t namepart[11];
140 } GRUB_PACKED;
142 /* A leaf node in the dirents tree. */
143 struct grub_jfs_leaf_dirent
145 /* The inode for this dirent. */
146 grub_uint32_t inode;
147 grub_uint8_t next;
149 /* The size of the name. */
150 grub_uint8_t len;
151 grub_uint16_t namepart[11];
152 grub_uint32_t index;
153 } GRUB_PACKED;
155 /* A leaf in the dirents tree. This one is used if the previously
156 dirent was not big enough to store the name. */
157 struct grub_jfs_leaf_next_dirent
159 grub_uint8_t next;
160 grub_uint8_t len;
161 grub_uint16_t namepart[15];
162 } GRUB_PACKED;
164 struct grub_jfs_time
166 grub_int32_t sec;
167 grub_int32_t nanosec;
168 } GRUB_PACKED;
170 struct grub_jfs_inode
172 grub_uint32_t stamp;
173 grub_uint32_t fileset;
174 grub_uint32_t inode;
175 grub_uint8_t unused[12];
176 grub_uint64_t size;
177 grub_uint8_t unused2[20];
178 grub_uint32_t mode;
179 struct grub_jfs_time atime;
180 struct grub_jfs_time ctime;
181 struct grub_jfs_time mtime;
182 grub_uint8_t unused3[48];
183 grub_uint8_t unused4[96];
185 union
187 /* The tree describing the extents of the file. */
188 struct GRUB_PACKED
190 struct grub_jfs_treehead tree;
191 struct grub_jfs_tree_extent extents[16];
192 } file;
193 union
195 /* The tree describing the dirents. */
196 struct
198 grub_uint8_t unused[16];
199 grub_uint8_t flags;
201 /* Amount of dirents in this node. */
202 grub_uint8_t count;
203 grub_uint8_t freecnt;
204 grub_uint8_t freelist;
205 grub_uint32_t idotdot;
206 grub_uint8_t sorted[8];
207 } header;
208 struct grub_jfs_leaf_dirent dirents[8];
209 } GRUB_PACKED dir;
210 /* Fast symlink. */
211 struct
213 grub_uint8_t unused[32];
214 grub_uint8_t path[256];
215 } symlink;
216 } GRUB_PACKED;
217 } GRUB_PACKED;
219 struct grub_jfs_data
221 struct grub_jfs_sblock sblock;
222 grub_disk_t disk;
223 struct grub_jfs_inode fileset;
224 struct grub_jfs_inode currinode;
225 int caseins;
226 int pos;
227 int linknest;
228 int namecomponentlen;
229 } GRUB_PACKED;
231 struct grub_jfs_diropen
233 int index;
234 union
236 struct grub_jfs_tree_dir header;
237 struct grub_jfs_leaf_dirent dirent[0];
238 struct grub_jfs_leaf_next_dirent next_dirent[0];
239 grub_uint8_t sorted[0];
240 } GRUB_PACKED *dirpage;
241 struct grub_jfs_data *data;
242 struct grub_jfs_inode *inode;
243 int count;
244 grub_uint8_t *sorted;
245 struct grub_jfs_leaf_dirent *leaf;
246 struct grub_jfs_leaf_next_dirent *next_leaf;
248 /* The filename and inode of the last read dirent. */
249 /* On-disk name is at most 255 UTF-16 codepoints.
250 Every UTF-16 codepoint is at most 4 UTF-8 bytes.
252 char name[256 * GRUB_MAX_UTF8_PER_UTF16 + 1];
253 grub_uint32_t ino;
254 } GRUB_PACKED;
257 static grub_dl_t my_mod;
259 static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino);
261 static grub_int64_t
262 getblk (struct grub_jfs_treehead *treehead,
263 struct grub_jfs_tree_extent *extents,
264 struct grub_jfs_data *data,
265 grub_uint64_t blk)
267 int found = -1;
268 int i;
270 for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++)
272 if (treehead->flags & GRUB_JFS_TREE_LEAF)
274 /* Read the leafnode. */
275 if (grub_le_to_cpu32 (extents[i].offset2) <= blk
276 && ((grub_le_to_cpu16 (extents[i].extent.length))
277 + (extents[i].extent.length2 << 16)
278 + grub_le_to_cpu32 (extents[i].offset2)) > blk)
279 return (blk - grub_le_to_cpu32 (extents[i].offset2)
280 + grub_le_to_cpu32 (extents[i].extent.blk2));
282 else
283 if (blk >= grub_le_to_cpu32 (extents[i].offset2))
284 found = i;
287 if (found != -1)
289 grub_int64_t ret = -1;
290 struct
292 struct grub_jfs_treehead treehead;
293 struct grub_jfs_tree_extent extents[254];
294 } *tree;
296 tree = grub_zalloc (sizeof (*tree));
297 if (!tree)
298 return -1;
300 if (!grub_disk_read (data->disk,
301 ((grub_disk_addr_t) grub_le_to_cpu32 (extents[found].extent.blk2))
302 << (grub_le_to_cpu16 (data->sblock.log2_blksz)
303 - GRUB_DISK_SECTOR_BITS), 0,
304 sizeof (*tree), (char *) tree))
305 ret = getblk (&tree->treehead, &tree->extents[0], data, blk);
306 grub_free (tree);
307 return ret;
310 return -1;
313 /* Get the block number for the block BLK in the node INODE in the
314 mounted filesystem DATA. */
315 static grub_int64_t
316 grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
317 grub_uint64_t blk)
319 return getblk (&inode->file.tree, &inode->file.extents[0], data, blk);
323 static grub_err_t
324 grub_jfs_read_inode (struct grub_jfs_data *data, grub_uint32_t ino,
325 struct grub_jfs_inode *inode)
327 struct grub_jfs_extent iag_inodes[GRUB_JFS_IAG_INODES_COUNT];
328 grub_uint32_t iagnum = ino / 4096;
329 unsigned inoext = (ino % 4096) / 32;
330 unsigned inonum = (ino % 4096) % 32;
331 grub_uint64_t iagblk;
332 grub_uint64_t inoblk;
334 iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
335 if (grub_errno)
336 return grub_errno;
338 /* Read in the IAG. */
339 if (grub_disk_read (data->disk,
340 iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
341 - GRUB_DISK_SECTOR_BITS),
342 GRUB_JFS_IAG_INODES_OFFSET,
343 sizeof (iag_inodes), &iag_inodes))
344 return grub_errno;
346 inoblk = grub_le_to_cpu32 (iag_inodes[inoext].blk2);
347 inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
348 - GRUB_DISK_SECTOR_BITS);
349 inoblk += inonum;
351 if (grub_disk_read (data->disk, inoblk, 0,
352 sizeof (struct grub_jfs_inode), inode))
353 return grub_errno;
355 return 0;
359 static struct grub_jfs_data *
360 grub_jfs_mount (grub_disk_t disk)
362 struct grub_jfs_data *data = 0;
364 data = grub_malloc (sizeof (struct grub_jfs_data));
365 if (!data)
366 return 0;
368 /* Read the superblock. */
369 if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
370 sizeof (struct grub_jfs_sblock), &data->sblock))
371 goto fail;
373 if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
375 grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
376 goto fail;
379 if (data->sblock.blksz == 0
380 || grub_le_to_cpu32 (data->sblock.blksz)
381 != (1U << grub_le_to_cpu16 (data->sblock.log2_blksz))
382 || grub_le_to_cpu16 (data->sblock.log2_blksz) < GRUB_DISK_SECTOR_BITS)
384 grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
385 goto fail;
388 data->disk = disk;
389 data->pos = 0;
390 data->linknest = 0;
392 /* Read the inode of the first fileset. */
393 if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
394 sizeof (struct grub_jfs_inode), &data->fileset))
395 goto fail;
397 if (data->sblock.flags & grub_cpu_to_le32_compile_time (0x00200000))
398 data->namecomponentlen = 11;
399 else
400 data->namecomponentlen = 13;
402 if (data->sblock.flags & grub_cpu_to_le32_compile_time (0x40000000))
403 data->caseins = 1;
404 else
405 data->caseins = 0;
407 return data;
409 fail:
410 grub_free (data);
412 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
413 grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem");
415 return 0;
419 static struct grub_jfs_diropen *
420 grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
422 struct grub_jfs_internal_dirent *de;
423 struct grub_jfs_diropen *diro;
424 grub_disk_addr_t blk;
426 de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
428 if (!((grub_le_to_cpu32 (inode->mode)
429 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
431 grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
432 return 0;
435 diro = grub_zalloc (sizeof (struct grub_jfs_diropen));
436 if (!diro)
437 return 0;
439 diro->data = data;
440 diro->inode = inode;
442 /* Check if the entire tree is contained within the inode. */
443 if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
445 diro->leaf = inode->dir.dirents;
446 diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
447 diro->sorted = inode->dir.header.sorted;
448 diro->count = inode->dir.header.count;
450 return diro;
453 diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz));
454 if (!diro->dirpage)
456 grub_free (diro);
457 return 0;
460 blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
461 blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
463 /* Read in the nodes until we are on the leaf node level. */
466 int index;
467 if (grub_disk_read (data->disk, blk, 0,
468 grub_le_to_cpu32 (data->sblock.blksz),
469 diro->dirpage->sorted))
471 grub_free (diro->dirpage);
472 grub_free (diro);
473 return 0;
476 de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
477 index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
478 blk = (grub_le_to_cpu32 (de[index].ex.blk2)
479 << (grub_le_to_cpu16 (data->sblock.log2_blksz)
480 - GRUB_DISK_SECTOR_BITS));
481 } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
483 diro->leaf = diro->dirpage->dirent;
484 diro->next_leaf = diro->dirpage->next_dirent;
485 diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
486 diro->count = diro->dirpage->header.count;
488 return diro;
492 static void
493 grub_jfs_closedir (struct grub_jfs_diropen *diro)
495 if (!diro)
496 return;
497 grub_free (diro->dirpage);
498 grub_free (diro);
501 static void
502 le_to_cpu16_copy (grub_uint16_t *out, grub_uint16_t *in, grub_size_t len)
504 while (len--)
505 *out++ = grub_le_to_cpu16 (*in++);
509 /* Read in the next dirent from the directory described by DIRO. */
510 static grub_err_t
511 grub_jfs_getent (struct grub_jfs_diropen *diro)
513 int strpos = 0;
514 struct grub_jfs_leaf_dirent *leaf;
515 struct grub_jfs_leaf_next_dirent *next_leaf;
516 int len;
517 int nextent;
518 grub_uint16_t filename[256];
520 /* The last node, read in more. */
521 if (diro->index == diro->count)
523 grub_disk_addr_t next;
525 /* If the inode contains the entry tree or if this was the last
526 node, there is nothing to read. */
527 if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
528 || !grub_le_to_cpu64 (diro->dirpage->header.nextb))
529 return GRUB_ERR_OUT_OF_RANGE;
531 next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
532 next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
533 - GRUB_DISK_SECTOR_BITS);
535 if (grub_disk_read (diro->data->disk, next, 0,
536 grub_le_to_cpu32 (diro->data->sblock.blksz),
537 diro->dirpage->sorted))
538 return grub_errno;
540 diro->leaf = diro->dirpage->dirent;
541 diro->next_leaf = diro->dirpage->next_dirent;
542 diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
543 diro->count = diro->dirpage->header.count;
544 diro->index = 0;
547 leaf = &diro->leaf[diro->sorted[diro->index]];
548 next_leaf = &diro->next_leaf[diro->index];
550 len = leaf->len;
551 if (!len)
553 diro->index++;
554 return grub_jfs_getent (diro);
557 le_to_cpu16_copy (filename + strpos, leaf->namepart, len < diro->data->namecomponentlen ? len
558 : diro->data->namecomponentlen);
559 strpos += len < diro->data->namecomponentlen ? len
560 : diro->data->namecomponentlen;
561 diro->ino = grub_le_to_cpu32 (leaf->inode);
562 len -= diro->data->namecomponentlen;
564 /* Move down to the leaf level. */
565 nextent = leaf->next;
566 if (leaf->next != 255)
569 next_leaf = &diro->next_leaf[nextent];
570 le_to_cpu16_copy (filename + strpos, next_leaf->namepart, len < 15 ? len : 15);
571 strpos += len < 15 ? len : 15;
573 len -= 15;
574 nextent = next_leaf->next;
575 } while (next_leaf->next != 255 && len > 0);
577 diro->index++;
579 /* Convert the temporary UTF16 filename to UTF8. */
580 *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0';
582 return 0;
586 /* Read LEN bytes from the file described by DATA starting with byte
587 POS. Return the amount of read bytes in READ. */
588 static grub_ssize_t
589 grub_jfs_read_file (struct grub_jfs_data *data,
590 grub_disk_read_hook_t read_hook, void *read_hook_data,
591 grub_off_t pos, grub_size_t len, char *buf)
593 grub_off_t i;
594 grub_off_t blockcnt;
596 blockcnt = (len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
597 >> grub_le_to_cpu16 (data->sblock.log2_blksz);
599 for (i = pos >> grub_le_to_cpu16 (data->sblock.log2_blksz); i < blockcnt; i++)
601 grub_disk_addr_t blknr;
602 grub_uint32_t blockoff = pos & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
603 grub_uint32_t blockend = grub_le_to_cpu32 (data->sblock.blksz);
605 grub_uint64_t skipfirst = 0;
607 blknr = grub_jfs_blkno (data, &data->currinode, i);
608 if (grub_errno)
609 return -1;
611 /* Last block. */
612 if (i == blockcnt - 1)
614 blockend = (len + pos) & (grub_le_to_cpu32 (data->sblock.blksz) - 1);
616 if (!blockend)
617 blockend = grub_le_to_cpu32 (data->sblock.blksz);
620 /* First block. */
621 if (i == (pos >> grub_le_to_cpu16 (data->sblock.log2_blksz)))
623 skipfirst = blockoff;
624 blockend -= skipfirst;
627 data->disk->read_hook = read_hook;
628 data->disk->read_hook_data = read_hook_data;
629 grub_disk_read (data->disk,
630 blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
631 - GRUB_DISK_SECTOR_BITS),
632 skipfirst, blockend, buf);
634 data->disk->read_hook = 0;
635 if (grub_errno)
636 return -1;
638 buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
641 return len;
645 /* Find the file with the pathname PATH on the filesystem described by
646 DATA. */
647 static grub_err_t
648 grub_jfs_find_file (struct grub_jfs_data *data, const char *path,
649 grub_uint32_t start_ino)
651 const char *name;
652 const char *next = path;
653 struct grub_jfs_diropen *diro = NULL;
655 if (grub_jfs_read_inode (data, start_ino, &data->currinode))
656 return grub_errno;
658 while (1)
660 name = next;
661 while (*name == '/')
662 name++;
663 if (name[0] == 0)
664 return GRUB_ERR_NONE;
665 for (next = name; *next && *next != '/'; next++);
667 if (name[0] == '.' && name + 1 == next)
668 continue;
670 if (name[0] == '.' && name[1] == '.' && name + 2 == next)
672 grub_uint32_t ino = grub_le_to_cpu32 (data->currinode.dir.header.idotdot);
674 if (grub_jfs_read_inode (data, ino, &data->currinode))
675 return grub_errno;
677 continue;
680 diro = grub_jfs_opendir (data, &data->currinode);
681 if (!diro)
682 return grub_errno;
684 for (;;)
686 if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
688 grub_jfs_closedir (diro);
689 return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), path);
692 /* Check if the current direntry matches the current part of the
693 pathname. */
694 if ((data->caseins ? grub_strncasecmp (name, diro->name, next - name) == 0
695 : grub_strncmp (name, diro->name, next - name) == 0) && !diro->name[next - name])
697 grub_uint32_t ino = diro->ino;
698 grub_uint32_t dirino = grub_le_to_cpu32 (data->currinode.inode);
700 grub_jfs_closedir (diro);
701 diro = 0;
703 if (grub_jfs_read_inode (data, ino, &data->currinode))
704 break;
706 /* Check if this is a symlink. */
707 if ((grub_le_to_cpu32 (data->currinode.mode)
708 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
710 grub_jfs_lookup_symlink (data, dirino);
711 if (grub_errno)
712 return grub_errno;
715 break;
722 static grub_err_t
723 grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino)
725 grub_size_t size = grub_le_to_cpu64 (data->currinode.size);
726 char *symlink;
728 if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
729 return grub_error (GRUB_ERR_SYMLINK_LOOP, N_("too deep nesting of symlinks"));
731 symlink = grub_malloc (size + 1);
732 if (!symlink)
733 return grub_errno;
734 if (size <= sizeof (data->currinode.symlink.path))
735 grub_memcpy (symlink, (char *) (data->currinode.symlink.path), size);
736 else if (grub_jfs_read_file (data, 0, 0, 0, size, symlink) < 0)
738 grub_free (symlink);
739 return grub_errno;
742 symlink[size] = '\0';
744 /* The symlink is an absolute path, go back to the root inode. */
745 if (symlink[0] == '/')
746 ino = 2;
748 grub_jfs_find_file (data, symlink, ino);
750 grub_free (symlink);
752 return grub_errno;
756 static grub_err_t
757 grub_jfs_dir (grub_device_t device, const char *path,
758 grub_fs_dir_hook_t hook, void *hook_data)
760 struct grub_jfs_data *data = 0;
761 struct grub_jfs_diropen *diro = 0;
763 grub_dl_ref (my_mod);
765 data = grub_jfs_mount (device->disk);
766 if (!data)
767 goto fail;
769 if (grub_jfs_find_file (data, path, GRUB_JFS_AGGR_INODE))
770 goto fail;
772 diro = grub_jfs_opendir (data, &data->currinode);
773 if (!diro)
774 goto fail;
776 /* Iterate over the dirents in the directory that was found. */
777 while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
779 struct grub_jfs_inode inode;
780 struct grub_dirhook_info info;
781 grub_memset (&info, 0, sizeof (info));
783 if (grub_jfs_read_inode (data, diro->ino, &inode))
784 goto fail;
786 info.dir = (grub_le_to_cpu32 (inode.mode)
787 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
788 info.mtimeset = 1;
789 info.mtime = grub_le_to_cpu32 (inode.mtime.sec);
790 if (hook (diro->name, &info, hook_data))
791 goto fail;
794 /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
795 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
796 grub_errno = 0;
798 fail:
799 grub_jfs_closedir (diro);
800 grub_free (data);
802 grub_dl_unref (my_mod);
804 return grub_errno;
808 /* Open a file named NAME and initialize FILE. */
809 static grub_err_t
810 grub_jfs_open (struct grub_file *file, const char *name)
812 struct grub_jfs_data *data;
814 grub_dl_ref (my_mod);
816 data = grub_jfs_mount (file->device->disk);
817 if (!data)
818 goto fail;
820 grub_jfs_find_file (data, name, GRUB_JFS_AGGR_INODE);
821 if (grub_errno)
822 goto fail;
824 /* It is only possible for open regular files. */
825 if (! ((grub_le_to_cpu32 (data->currinode.mode)
826 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
828 grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
829 goto fail;
832 file->data = data;
833 file->size = grub_le_to_cpu64 (data->currinode.size);
835 return 0;
837 fail:
839 grub_dl_unref (my_mod);
841 grub_free (data);
843 return grub_errno;
847 static grub_ssize_t
848 grub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
850 struct grub_jfs_data *data =
851 (struct grub_jfs_data *) file->data;
853 return grub_jfs_read_file (data, file->read_hook, file->read_hook_data,
854 file->offset, len, buf);
858 static grub_err_t
859 grub_jfs_close (grub_file_t file)
861 grub_free (file->data);
863 grub_dl_unref (my_mod);
865 return GRUB_ERR_NONE;
868 static grub_err_t
869 grub_jfs_uuid (grub_device_t device, char **uuid)
871 struct grub_jfs_data *data;
872 grub_disk_t disk = device->disk;
874 grub_dl_ref (my_mod);
876 data = grub_jfs_mount (disk);
877 if (data)
879 *uuid = grub_xasprintf ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
880 "%02x%02x%02x%02x%02x%02x",
881 data->sblock.uuid[0], data->sblock.uuid[1],
882 data->sblock.uuid[2], data->sblock.uuid[3],
883 data->sblock.uuid[4], data->sblock.uuid[5],
884 data->sblock.uuid[6], data->sblock.uuid[7],
885 data->sblock.uuid[8], data->sblock.uuid[9],
886 data->sblock.uuid[10], data->sblock.uuid[11],
887 data->sblock.uuid[12], data->sblock.uuid[13],
888 data->sblock.uuid[14], data->sblock.uuid[15]);
890 else
891 *uuid = NULL;
893 grub_dl_unref (my_mod);
895 grub_free (data);
897 return grub_errno;
900 static grub_err_t
901 grub_jfs_label (grub_device_t device, char **label)
903 struct grub_jfs_data *data;
904 data = grub_jfs_mount (device->disk);
906 if (data)
908 if (data->sblock.volname2[0] < ' ')
910 char *ptr;
911 ptr = data->sblock.volname + sizeof (data->sblock.volname) - 1;
912 while (ptr >= data->sblock.volname && *ptr == ' ')
913 ptr--;
914 *label = grub_strndup (data->sblock.volname,
915 ptr - data->sblock.volname + 1);
917 else
918 *label = grub_strndup (data->sblock.volname2,
919 sizeof (data->sblock.volname2));
921 else
922 *label = 0;
924 grub_free (data);
926 return grub_errno;
930 static struct grub_fs grub_jfs_fs =
932 .name = "jfs",
933 .dir = grub_jfs_dir,
934 .open = grub_jfs_open,
935 .read = grub_jfs_read,
936 .close = grub_jfs_close,
937 .label = grub_jfs_label,
938 .uuid = grub_jfs_uuid,
939 #ifdef GRUB_UTIL
940 .reserved_first_sector = 1,
941 .blocklist_install = 1,
942 #endif
943 .next = 0
946 GRUB_MOD_INIT(jfs)
948 grub_fs_register (&grub_jfs_fs);
949 my_mod = mod;
952 GRUB_MOD_FINI(jfs)
954 grub_fs_unregister (&grub_jfs_fs);