2009-10-09 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / fs / jfs.c
blobb73f9bdd47065093dd7cca4b3bf5304397fed9f1
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>
28 #define GRUB_JFS_MAX_SYMLNK_CNT 8
29 #define GRUB_JFS_FILETYPE_MASK 0170000
30 #define GRUB_JFS_FILETYPE_REG 0100000
31 #define GRUB_JFS_FILETYPE_LNK 0120000
32 #define GRUB_JFS_FILETYPE_DIR 0040000
34 #define GRUB_JFS_SBLOCK 64
35 #define GRUB_JFS_AGGR_INODE 2
36 #define GRUB_JFS_FS1_INODE_BLK 104
38 #define GRUB_JFS_TREE_LEAF 2
40 struct grub_jfs_sblock
42 /* The magic for JFS. It should contain the string "JFS1". */
43 grub_uint8_t magic[4];
44 grub_uint32_t version;
45 grub_uint64_t ag_size;
47 /* The size of a filesystem block in bytes. XXX: currently only
48 4096 was tested. */
49 grub_uint32_t blksz;
50 grub_uint16_t log2_blksz;
52 grub_uint8_t unused[71];
53 grub_uint8_t volname[11];
54 grub_uint8_t unused2[32];
55 grub_uint8_t uuid[16];
58 struct grub_jfs_extent
60 /* The length of the extent in filesystem blocks. */
61 grub_uint16_t length;
62 grub_uint8_t length2;
64 /* The physical offset of the first block on the disk. */
65 grub_uint8_t blk1;
66 grub_uint32_t blk2;
67 } __attribute__ ((packed));
69 struct grub_jfs_iag
71 grub_uint8_t unused[3072];
72 struct grub_jfs_extent inodes[128];
73 } __attribute__ ((packed));
76 /* The head of the tree used to find extents. */
77 struct grub_jfs_treehead
79 grub_uint64_t next;
80 grub_uint64_t prev;
82 grub_uint8_t flags;
83 grub_uint8_t unused;
85 grub_uint16_t count;
86 grub_uint16_t max;
87 grub_uint8_t unused2[10];
88 } __attribute__ ((packed));
90 /* A node in the extent tree. */
91 struct grub_jfs_tree_extent
93 grub_uint8_t flags;
94 grub_uint16_t unused;
96 /* The offset is the key used to lookup an extent. */
97 grub_uint8_t offset1;
98 grub_uint32_t offset2;
100 struct grub_jfs_extent extent;
101 } __attribute__ ((packed));
103 /* The tree of directory entries. */
104 struct grub_jfs_tree_dir
106 /* Pointers to the previous and next tree headers of other nodes on
107 this level. */
108 grub_uint64_t nextb;
109 grub_uint64_t prevb;
111 grub_uint8_t flags;
113 /* The amount of dirents in this node. */
114 grub_uint8_t count;
115 grub_uint8_t freecnt;
116 grub_uint8_t freelist;
117 grub_uint8_t maxslot;
119 /* The location of the sorted array of pointers to dirents. */
120 grub_uint8_t sindex;
121 grub_uint8_t unused[10];
122 } __attribute__ ((packed));
124 /* An internal node in the dirents tree. */
125 struct grub_jfs_internal_dirent
127 struct grub_jfs_extent ex;
128 grub_uint8_t next;
129 grub_uint8_t len;
130 grub_uint16_t namepart[11];
131 } __attribute__ ((packed));
133 /* A leaf node in the dirents tree. */
134 struct grub_jfs_leaf_dirent
136 /* The inode for this dirent. */
137 grub_uint32_t inode;
138 grub_uint8_t next;
140 /* The size of the name. */
141 grub_uint8_t len;
142 grub_uint16_t namepart[11];
143 grub_uint32_t index;
144 } __attribute__ ((packed));
146 /* A leaf in the dirents tree. This one is used if the previously
147 dirent was not big enough to store the name. */
148 struct grub_jfs_leaf_next_dirent
150 grub_uint8_t next;
151 grub_uint8_t len;
152 grub_uint16_t namepart[15];
153 } __attribute__ ((packed));
155 struct grub_jfs_inode
157 grub_uint32_t stamp;
158 grub_uint32_t fileset;
159 grub_uint32_t inode;
160 grub_uint8_t unused[12];
161 grub_uint64_t size;
162 grub_uint8_t unused2[20];
163 grub_uint32_t mode;
164 grub_uint8_t unused3[72];
165 grub_uint8_t unused4[96];
167 union
169 /* The tree describing the extents of the file. */
170 struct __attribute__ ((packed))
172 struct grub_jfs_treehead tree;
173 struct grub_jfs_tree_extent extents[16];
174 } file;
175 union
177 /* The tree describing the dirents. */
178 struct
180 grub_uint8_t unused[16];
181 grub_uint8_t flags;
183 /* Amount of dirents in this node. */
184 grub_uint8_t count;
185 grub_uint8_t freecnt;
186 grub_uint8_t freelist;
187 grub_uint32_t idotdot;
188 grub_uint8_t sorted[8];
189 } header;
190 struct grub_jfs_leaf_dirent dirents[8];
191 } dir __attribute__ ((packed));
192 /* Fast symlink. */
193 struct
195 grub_uint8_t unused[32];
196 grub_uint8_t path[128];
197 } symlink;
198 } __attribute__ ((packed));
199 } __attribute__ ((packed));
201 struct grub_jfs_data
203 struct grub_jfs_sblock sblock;
204 grub_disk_t disk;
205 struct grub_jfs_inode fileset;
206 struct grub_jfs_inode currinode;
207 int pos;
208 int linknest;
209 } __attribute__ ((packed));
211 struct grub_jfs_diropen
213 int index;
214 union
216 struct grub_jfs_tree_dir header;
217 struct grub_jfs_leaf_dirent dirent[0];
218 struct grub_jfs_leaf_next_dirent next_dirent[0];
219 char sorted[0];
220 } *dirpage __attribute__ ((packed));
221 struct grub_jfs_data *data;
222 struct grub_jfs_inode *inode;
223 int count;
224 char *sorted;
225 struct grub_jfs_leaf_dirent *leaf;
226 struct grub_jfs_leaf_next_dirent *next_leaf;
228 /* The filename and inode of the last read dirent. */
229 char name[255];
230 grub_uint32_t ino;
231 } __attribute__ ((packed));
234 static grub_dl_t my_mod;
236 static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino);
238 /* Get the block number for the block BLK in the node INODE in the
239 mounted filesystem DATA. */
240 static int
241 grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
242 unsigned int blk)
244 auto int getblk (struct grub_jfs_treehead *treehead,
245 struct grub_jfs_tree_extent *extents);
247 int getblk (struct grub_jfs_treehead *treehead,
248 struct grub_jfs_tree_extent *extents)
250 int found = -1;
251 int i;
253 for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++)
255 if (treehead->flags & GRUB_JFS_TREE_LEAF)
257 /* Read the leafnode. */
258 if (grub_le_to_cpu32 (extents[i].offset2) <= blk
259 && ((grub_le_to_cpu16 (extents[i].extent.length))
260 + (extents[i].extent.length2 << 8)
261 + grub_le_to_cpu32 (extents[i].offset2)) > blk)
262 return (blk - grub_le_to_cpu32 (extents[i].offset2)
263 + grub_le_to_cpu32 (extents[i].extent.blk2));
265 else
266 if (blk >= grub_le_to_cpu32 (extents[i].offset2))
267 found = i;
270 if (found != -1)
272 struct
274 struct grub_jfs_treehead treehead;
275 struct grub_jfs_tree_extent extents[254];
276 } tree;
278 if (grub_disk_read (data->disk,
279 grub_le_to_cpu32 (extents[found].extent.blk2)
280 << (grub_le_to_cpu16 (data->sblock.log2_blksz)
281 - GRUB_DISK_SECTOR_BITS), 0,
282 sizeof (tree), (char *) &tree))
283 return -1;
285 return getblk (&tree.treehead, &tree.extents[0]);
288 return -1;
291 return getblk (&inode->file.tree, &inode->file.extents[0]);
295 static grub_err_t
296 grub_jfs_read_inode (struct grub_jfs_data *data, int ino,
297 struct grub_jfs_inode *inode)
299 struct grub_jfs_iag iag;
300 int iagnum = ino / 4096;
301 int inoext = (ino % 4096) / 32;
302 int inonum = (ino % 4096) % 32;
303 grub_uint32_t iagblk;
304 grub_uint32_t inoblk;
306 iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
307 if (grub_errno)
308 return grub_errno;
310 /* Read in the IAG. */
311 if (grub_disk_read (data->disk,
312 iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
313 - GRUB_DISK_SECTOR_BITS), 0,
314 sizeof (struct grub_jfs_iag), &iag))
315 return grub_errno;
317 inoblk = grub_le_to_cpu32 (iag.inodes[inoext].blk2);
318 inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
319 - GRUB_DISK_SECTOR_BITS);
320 inoblk += inonum;
322 if (grub_disk_read (data->disk, inoblk, 0,
323 sizeof (struct grub_jfs_inode), inode))
324 return grub_errno;
326 return 0;
330 static struct grub_jfs_data *
331 grub_jfs_mount (grub_disk_t disk)
333 struct grub_jfs_data *data = 0;
335 data = grub_malloc (sizeof (struct grub_jfs_data));
336 if (!data)
337 return 0;
339 /* Read the superblock. */
340 if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
341 sizeof (struct grub_jfs_sblock), &data->sblock))
342 goto fail;
344 if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
346 grub_error (GRUB_ERR_BAD_FS, "not a jfs filesystem");
347 goto fail;
350 data->disk = disk;
351 data->pos = 0;
352 data->linknest = 0;
354 /* Read the inode of the first fileset. */
355 if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
356 sizeof (struct grub_jfs_inode), &data->fileset))
357 goto fail;
359 return data;
361 fail:
362 grub_free (data);
364 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
365 grub_error (GRUB_ERR_BAD_FS, "not a jfs filesystem");
367 return 0;
371 static struct grub_jfs_diropen *
372 grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
374 struct grub_jfs_internal_dirent *de;
375 struct grub_jfs_diropen *diro;
376 int blk;
378 de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
380 if (!((grub_le_to_cpu32 (inode->mode)
381 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
383 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
384 return 0;
387 diro = grub_zalloc (sizeof (struct grub_jfs_diropen));
388 if (!diro)
389 return 0;
391 diro->data = data;
392 diro->inode = inode;
394 /* Check if the entire tree is contained within the inode. */
395 if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
397 diro->leaf = inode->dir.dirents;
398 diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
399 diro->sorted = (char *) (inode->dir.header.sorted);
400 diro->count = inode->dir.header.count;
402 return diro;
405 diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz));
406 if (!diro->dirpage)
408 grub_free (diro);
409 return 0;
412 blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
413 blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
415 /* Read in the nodes until we are on the leaf node level. */
418 int index;
419 if (grub_disk_read (data->disk, blk, 0,
420 grub_le_to_cpu32 (data->sblock.blksz),
421 diro->dirpage->sorted))
423 grub_free (diro->dirpage);
424 grub_free (diro);
425 return 0;
428 de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
429 index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
430 blk = (grub_le_to_cpu32 (de[index].ex.blk2)
431 << (grub_le_to_cpu16 (data->sblock.log2_blksz)
432 - GRUB_DISK_SECTOR_BITS));
433 } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
435 diro->leaf = diro->dirpage->dirent;
436 diro->next_leaf = diro->dirpage->next_dirent;
437 diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
438 diro->count = diro->dirpage->header.count;
440 return diro;
444 static void
445 grub_jfs_closedir (struct grub_jfs_diropen *diro)
447 if (!diro)
448 return;
449 grub_free (diro->dirpage);
450 grub_free (diro);
454 /* Read in the next dirent from the directory described by DIRO. */
455 static grub_err_t
456 grub_jfs_getent (struct grub_jfs_diropen *diro)
458 int strpos = 0;
459 struct grub_jfs_leaf_dirent *leaf;
460 struct grub_jfs_leaf_next_dirent *next_leaf;
461 int len;
462 int nextent;
463 grub_uint16_t filename[255];
465 auto void addstr (grub_uint16_t *uname, int ulen);
467 /* Add the unicode string to the utf16 filename buffer. */
468 void addstr (grub_uint16_t *name, int ulen)
470 while (ulen--)
471 filename[strpos++] = *(name++);
474 /* The last node, read in more. */
475 if (diro->index == diro->count)
477 unsigned int next;
479 /* If the inode contains the entry tree or if this was the last
480 node, there is nothing to read. */
481 if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
482 || !grub_le_to_cpu64 (diro->dirpage->header.nextb))
483 return GRUB_ERR_OUT_OF_RANGE;
485 next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
486 next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
487 - GRUB_DISK_SECTOR_BITS);
489 if (grub_disk_read (diro->data->disk, next, 0,
490 grub_le_to_cpu32 (diro->data->sblock.blksz),
491 diro->dirpage->sorted))
492 return grub_errno;
494 diro->leaf = diro->dirpage->dirent;
495 diro->next_leaf = diro->dirpage->next_dirent;
496 diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
497 diro->count = diro->dirpage->header.count;
498 diro->index = 0;
501 leaf = &diro->leaf[(int) diro->sorted[diro->index]];
502 next_leaf = &diro->next_leaf[diro->index];
504 len = leaf->len;
505 if (!len)
507 diro->index++;
508 return grub_jfs_getent (diro);
511 addstr (leaf->namepart, len < 11 ? len : 11);
512 diro->ino = grub_le_to_cpu32 (leaf->inode);
513 len -= 11;
515 /* Move down to the leaf level. */
516 nextent = leaf->next;
517 if (leaf->next != 255)
520 next_leaf = &diro->next_leaf[nextent];
521 addstr (next_leaf->namepart, len < 15 ? len : 15 );
523 len -= 15;
524 nextent = next_leaf->next;
525 } while (next_leaf->next != 255 && len > 0);
527 diro->index++;
529 /* Convert the temporary UTF16 filename to UTF8. */
530 *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0';
532 return 0;
536 /* Read LEN bytes from the file described by DATA starting with byte
537 POS. Return the amount of read bytes in READ. */
538 static grub_ssize_t
539 grub_jfs_read_file (struct grub_jfs_data *data,
540 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
541 unsigned offset, unsigned length),
542 int pos, grub_size_t len, char *buf)
544 int i;
545 int blockcnt;
547 blockcnt = ((len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
548 / grub_le_to_cpu32 (data->sblock.blksz));
550 for (i = pos / grub_le_to_cpu32 (data->sblock.blksz); i < blockcnt; i++)
552 int blknr;
553 int blockoff = pos % grub_le_to_cpu32 (data->sblock.blksz);
554 int blockend = grub_le_to_cpu32 (data->sblock.blksz);
556 int skipfirst = 0;
558 blknr = grub_jfs_blkno (data, &data->currinode, i);
559 if (grub_errno)
560 return -1;
562 /* Last block. */
563 if (i == blockcnt - 1)
565 blockend = (len + pos) % grub_le_to_cpu32 (data->sblock.blksz);
567 if (!blockend)
568 blockend = grub_le_to_cpu32 (data->sblock.blksz);
571 /* First block. */
572 if (i == (pos / (int) grub_le_to_cpu32 (data->sblock.blksz)))
574 skipfirst = blockoff;
575 blockend -= skipfirst;
578 data->disk->read_hook = read_hook;
579 grub_disk_read (data->disk,
580 blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
581 - GRUB_DISK_SECTOR_BITS),
582 skipfirst, blockend, buf);
584 data->disk->read_hook = 0;
585 if (grub_errno)
586 return -1;
588 buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
591 return len;
595 /* Find the file with the pathname PATH on the filesystem described by
596 DATA. */
597 static grub_err_t
598 grub_jfs_find_file (struct grub_jfs_data *data, const char *path)
600 char fpath[grub_strlen (path)];
601 char *name = fpath;
602 char *next;
603 unsigned int pos = 0;
604 struct grub_jfs_diropen *diro;
606 grub_strncpy (fpath, path, grub_strlen (path) + 1);
608 if (grub_jfs_read_inode (data, GRUB_JFS_AGGR_INODE, &data->currinode))
609 return grub_errno;
611 /* Skip the first slashes. */
612 while (*name == '/')
614 name++;
615 if (!*name)
616 return 0;
619 /* Extract the actual part from the pathname. */
620 next = grub_strchr (name, '/');
621 if (next)
623 while (*next == '/')
625 next[0] = '\0';
626 next++;
629 diro = grub_jfs_opendir (data, &data->currinode);
630 if (!diro)
631 return grub_errno;
633 for (;;)
635 if (grub_strlen (name) == 0)
636 return GRUB_ERR_NONE;
638 if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
639 break;
641 /* Check if the current direntry matches the current part of the
642 pathname. */
643 if (!grub_strcmp (name, diro->name))
645 int ino = diro->ino;
646 int dirino = grub_le_to_cpu32 (data->currinode.inode);
648 grub_jfs_closedir (diro);
649 diro = 0;
651 if (grub_jfs_read_inode (data, ino, &data->currinode))
652 break;
654 /* Check if this is a symlink. */
655 if ((grub_le_to_cpu32 (data->currinode.mode)
656 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
658 grub_jfs_lookup_symlink (data, dirino);
659 if (grub_errno)
660 return grub_errno;
663 if (!next)
664 return 0;
666 pos = 0;
668 name = next;
669 next = grub_strchr (name, '/');
670 if (next)
672 next[0] = '\0';
673 next++;
676 /* Open this directory for reading dirents. */
677 diro = grub_jfs_opendir (data, &data->currinode);
678 if (!diro)
679 return grub_errno;
681 continue;
685 grub_jfs_closedir (diro);
686 grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
687 return grub_errno;
691 static grub_err_t
692 grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino)
694 int size = grub_le_to_cpu64 (data->currinode.size);
695 char symlink[size + 1];
697 if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
698 return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
700 if (size <= 128)
701 grub_strncpy (symlink, (char *) (data->currinode.symlink.path), 128);
702 else if (grub_jfs_read_file (data, 0, 0, size, symlink) < 0)
703 return grub_errno;
705 symlink[size] = '\0';
707 /* The symlink is an absolute path, go back to the root inode. */
708 if (symlink[0] == '/')
709 ino = 2;
711 /* Now load in the old inode. */
712 if (grub_jfs_read_inode (data, ino, &data->currinode))
713 return grub_errno;
715 grub_jfs_find_file (data, symlink);
716 if (grub_errno)
717 grub_error (grub_errno, "Can not follow symlink `%s'.", symlink);
719 return grub_errno;
723 static grub_err_t
724 grub_jfs_dir (grub_device_t device, const char *path,
725 int (*hook) (const char *filename,
726 const struct grub_dirhook_info *info))
728 struct grub_jfs_data *data = 0;
729 struct grub_jfs_diropen *diro = 0;
731 grub_dl_ref (my_mod);
733 data = grub_jfs_mount (device->disk);
734 if (!data)
735 goto fail;
737 if (grub_jfs_find_file (data, path))
738 goto fail;
740 diro = grub_jfs_opendir (data, &data->currinode);
741 if (!diro)
742 goto fail;
744 /* Iterate over the dirents in the directory that was found. */
745 while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
747 struct grub_jfs_inode inode;
748 struct grub_dirhook_info info;
749 grub_memset (&info, 0, sizeof (info));
751 if (grub_jfs_read_inode (data, diro->ino, &inode))
752 goto fail;
754 info.dir = (grub_le_to_cpu32 (inode.mode)
755 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
756 if (hook (diro->name, &info))
757 goto fail;
760 /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
761 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
762 grub_errno = 0;
764 fail:
765 grub_jfs_closedir (diro);
766 grub_free (data);
768 grub_dl_unref (my_mod);
770 return grub_errno;
774 /* Open a file named NAME and initialize FILE. */
775 static grub_err_t
776 grub_jfs_open (struct grub_file *file, const char *name)
778 struct grub_jfs_data *data;
780 grub_dl_ref (my_mod);
782 data = grub_jfs_mount (file->device->disk);
783 if (!data)
784 goto fail;
786 grub_jfs_find_file (data, name);
787 if (grub_errno)
788 goto fail;
790 /* It is only possible for open regular files. */
791 if (! ((grub_le_to_cpu32 (data->currinode.mode)
792 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
794 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file");
795 goto fail;
798 file->data = data;
799 file->size = grub_le_to_cpu64 (data->currinode.size);
801 return 0;
803 fail:
805 grub_dl_unref (my_mod);
807 grub_free (data);
809 return grub_errno;
813 static grub_ssize_t
814 grub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
816 struct grub_jfs_data *data =
817 (struct grub_jfs_data *) file->data;
819 return grub_jfs_read_file (data, file->read_hook, file->offset, len, buf);
823 static grub_err_t
824 grub_jfs_close (grub_file_t file)
826 grub_free (file->data);
828 grub_dl_unref (my_mod);
830 return GRUB_ERR_NONE;
833 static grub_err_t
834 grub_jfs_uuid (grub_device_t device, char **uuid)
836 struct grub_jfs_data *data;
837 grub_disk_t disk = device->disk;
839 grub_dl_ref (my_mod);
841 data = grub_jfs_mount (disk);
842 if (data)
844 *uuid = grub_malloc (40 + sizeof ('\0'));
846 grub_sprintf (*uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
847 data->sblock.uuid[0], data->sblock.uuid[1],
848 data->sblock.uuid[2], data->sblock.uuid[3],
849 data->sblock.uuid[4], data->sblock.uuid[5],
850 data->sblock.uuid[6], data->sblock.uuid[7],
851 data->sblock.uuid[8], data->sblock.uuid[9],
852 data->sblock.uuid[10], data->sblock.uuid[11],
853 data->sblock.uuid[12], data->sblock.uuid[13],
854 data->sblock.uuid[14], data->sblock.uuid[15]);
856 else
857 *uuid = NULL;
859 grub_dl_unref (my_mod);
861 grub_free (data);
863 return grub_errno;
866 static grub_err_t
867 grub_jfs_label (grub_device_t device, char **label)
869 struct grub_jfs_data *data;
870 data = grub_jfs_mount (device->disk);
872 if (data)
873 *label = grub_strndup ((char *) (data->sblock.volname), 11);
874 else
875 *label = 0;
877 return grub_errno;
881 static struct grub_fs grub_jfs_fs =
883 .name = "jfs",
884 .dir = grub_jfs_dir,
885 .open = grub_jfs_open,
886 .read = grub_jfs_read,
887 .close = grub_jfs_close,
888 .label = grub_jfs_label,
889 .uuid = grub_jfs_uuid,
890 .next = 0
893 GRUB_MOD_INIT(jfs)
895 grub_fs_register (&grub_jfs_fs);
896 my_mod = mod;
899 GRUB_MOD_FINI(jfs)
901 grub_fs_unregister (&grub_jfs_fs);