2009-06-13 Jun Inoue <jun.lambda@gmail.com>
[grub2/phcoder.git] / fs / jfs.c
blob4f9182576fbc26687a4aa3e1c077855bf168d2c4
1 /* jfs.c - JFS. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,2006,2007,2008 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];
56 struct grub_jfs_extent
58 /* The length of the extent in filesystem blocks. */
59 grub_uint16_t length;
60 grub_uint8_t length2;
62 /* The physical offset of the first block on the disk. */
63 grub_uint8_t blk1;
64 grub_uint32_t blk2;
65 } __attribute__ ((packed));
67 struct grub_jfs_iag
69 grub_uint8_t unused[3072];
70 struct grub_jfs_extent inodes[128];
71 } __attribute__ ((packed));
74 /* The head of the tree used to find extents. */
75 struct grub_jfs_treehead
77 grub_uint64_t next;
78 grub_uint64_t prev;
80 grub_uint8_t flags;
81 grub_uint8_t unused;
83 grub_uint16_t count;
84 grub_uint16_t max;
85 grub_uint8_t unused2[10];
86 } __attribute__ ((packed));
88 /* A node in the extent tree. */
89 struct grub_jfs_tree_extent
91 grub_uint8_t flags;
92 grub_uint16_t unused;
94 /* The offset is the key used to lookup an extent. */
95 grub_uint8_t offset1;
96 grub_uint32_t offset2;
98 struct grub_jfs_extent extent;
99 } __attribute__ ((packed));
101 /* The tree of directory entries. */
102 struct grub_jfs_tree_dir
104 /* Pointers to the previous and next tree headers of other nodes on
105 this level. */
106 grub_uint64_t nextb;
107 grub_uint64_t prevb;
109 grub_uint8_t flags;
111 /* The amount of dirents in this node. */
112 grub_uint8_t count;
113 grub_uint8_t freecnt;
114 grub_uint8_t freelist;
115 grub_uint8_t maxslot;
117 /* The location of the sorted array of pointers to dirents. */
118 grub_uint8_t sindex;
119 grub_uint8_t unused[10];
120 } __attribute__ ((packed));
122 /* An internal node in the dirents tree. */
123 struct grub_jfs_internal_dirent
125 struct grub_jfs_extent ex;
126 grub_uint8_t next;
127 grub_uint8_t len;
128 grub_uint16_t namepart[11];
129 } __attribute__ ((packed));
131 /* A leaf node in the dirents tree. */
132 struct grub_jfs_leaf_dirent
134 /* The inode for this dirent. */
135 grub_uint32_t inode;
136 grub_uint8_t next;
138 /* The size of the name. */
139 grub_uint8_t len;
140 grub_uint16_t namepart[11];
141 grub_uint32_t index;
142 } __attribute__ ((packed));
144 /* A leaf in the dirents tree. This one is used if the previously
145 dirent was not big enough to store the name. */
146 struct grub_jfs_leaf_next_dirent
148 grub_uint8_t next;
149 grub_uint8_t len;
150 grub_uint16_t namepart[15];
151 } __attribute__ ((packed));
153 struct grub_jfs_inode
155 grub_uint32_t stamp;
156 grub_uint32_t fileset;
157 grub_uint32_t inode;
158 grub_uint8_t unused[12];
159 grub_uint64_t size;
160 grub_uint8_t unused2[20];
161 grub_uint32_t mode;
162 grub_uint8_t unused3[72];
163 grub_uint8_t unused4[96];
165 union
167 /* The tree describing the extents of the file. */
168 struct __attribute__ ((packed))
170 struct grub_jfs_treehead tree;
171 struct grub_jfs_tree_extent extents[16];
172 } file;
173 union
175 /* The tree describing the dirents. */
176 struct
178 grub_uint8_t unused[16];
179 grub_uint8_t flags;
181 /* Amount of dirents in this node. */
182 grub_uint8_t count;
183 grub_uint8_t freecnt;
184 grub_uint8_t freelist;
185 grub_uint32_t idotdot;
186 grub_uint8_t sorted[8];
187 } header;
188 struct grub_jfs_leaf_dirent dirents[8];
189 } dir __attribute__ ((packed));
190 /* Fast symlink. */
191 struct
193 grub_uint8_t unused[32];
194 grub_uint8_t path[128];
195 } symlink;
196 } __attribute__ ((packed));
197 } __attribute__ ((packed));
199 struct grub_jfs_data
201 struct grub_jfs_sblock sblock;
202 grub_disk_t disk;
203 struct grub_jfs_inode fileset;
204 struct grub_jfs_inode currinode;
205 int pos;
206 int linknest;
207 } __attribute__ ((packed));
209 struct grub_jfs_diropen
211 int index;
212 union
214 struct grub_jfs_tree_dir header;
215 struct grub_jfs_leaf_dirent dirent[0];
216 struct grub_jfs_leaf_next_dirent next_dirent[0];
217 char sorted[0];
218 } *dirpage __attribute__ ((packed));
219 struct grub_jfs_data *data;
220 struct grub_jfs_inode *inode;
221 int count;
222 char *sorted;
223 struct grub_jfs_leaf_dirent *leaf;
224 struct grub_jfs_leaf_next_dirent *next_leaf;
226 /* The filename and inode of the last read dirent. */
227 char name[255];
228 grub_uint32_t ino;
229 } __attribute__ ((packed));
232 static grub_dl_t my_mod;
234 static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino);
236 /* Get the block number for the block BLK in the node INODE in the
237 mounted filesystem DATA. */
238 static int
239 grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
240 unsigned int blk)
242 auto int getblk (struct grub_jfs_treehead *treehead,
243 struct grub_jfs_tree_extent *extents);
245 int getblk (struct grub_jfs_treehead *treehead,
246 struct grub_jfs_tree_extent *extents)
248 int found = -1;
249 int i;
251 for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++)
253 if (treehead->flags & GRUB_JFS_TREE_LEAF)
255 /* Read the leafnode. */
256 if (grub_le_to_cpu32 (extents[i].offset2) <= blk
257 && ((grub_le_to_cpu16 (extents[i].extent.length))
258 + (extents[i].extent.length2 << 8)
259 + grub_le_to_cpu32 (extents[i].offset2)) > blk)
260 return (blk - grub_le_to_cpu32 (extents[i].offset2)
261 + grub_le_to_cpu32 (extents[i].extent.blk2));
263 else
264 if (blk >= grub_le_to_cpu32 (extents[i].offset2))
265 found = i;
268 if (found != -1)
270 struct
272 struct grub_jfs_treehead treehead;
273 struct grub_jfs_tree_extent extents[254];
274 } tree;
276 if (grub_disk_read (data->disk,
277 grub_le_to_cpu32 (extents[found].extent.blk2)
278 << (grub_le_to_cpu16 (data->sblock.log2_blksz)
279 - GRUB_DISK_SECTOR_BITS), 0,
280 sizeof (tree), (char *) &tree))
281 return -1;
283 return getblk (&tree.treehead, &tree.extents[0]);
286 return -1;
289 return getblk (&inode->file.tree, &inode->file.extents[0]);
293 static grub_err_t
294 grub_jfs_read_inode (struct grub_jfs_data *data, int ino,
295 struct grub_jfs_inode *inode)
297 struct grub_jfs_iag iag;
298 int iagnum = ino / 4096;
299 int inoext = (ino % 4096) / 32;
300 int inonum = (ino % 4096) % 32;
301 grub_uint32_t iagblk;
302 grub_uint32_t inoblk;
304 iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
305 if (grub_errno)
306 return grub_errno;
308 /* Read in the IAG. */
309 if (grub_disk_read (data->disk,
310 iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
311 - GRUB_DISK_SECTOR_BITS), 0,
312 sizeof (struct grub_jfs_iag), &iag))
313 return grub_errno;
315 inoblk = grub_le_to_cpu32 (iag.inodes[inoext].blk2);
316 inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
317 - GRUB_DISK_SECTOR_BITS);
318 inoblk += inonum;
320 if (grub_disk_read (data->disk, inoblk, 0,
321 sizeof (struct grub_jfs_inode), inode))
322 return grub_errno;
324 return 0;
328 static struct grub_jfs_data *
329 grub_jfs_mount (grub_disk_t disk)
331 struct grub_jfs_data *data = 0;
333 data = grub_malloc (sizeof (struct grub_jfs_data));
334 if (!data)
335 return 0;
337 /* Read the superblock. */
338 if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
339 sizeof (struct grub_jfs_sblock), &data->sblock))
340 goto fail;
342 if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
344 grub_error (GRUB_ERR_BAD_FS, "not a jfs filesystem");
345 goto fail;
348 data->disk = disk;
349 data->pos = 0;
350 data->linknest = 0;
352 /* Read the inode of the first fileset. */
353 if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
354 sizeof (struct grub_jfs_inode), &data->fileset))
355 goto fail;
357 return data;
359 fail:
360 grub_free (data);
362 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
363 grub_error (GRUB_ERR_BAD_FS, "not a jfs filesystem");
365 return 0;
369 static struct grub_jfs_diropen *
370 grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
372 struct grub_jfs_internal_dirent *de;
373 struct grub_jfs_diropen *diro;
374 int blk;
376 de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
378 if (!((grub_le_to_cpu32 (inode->mode)
379 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
381 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
382 return 0;
385 diro = grub_malloc (sizeof (struct grub_jfs_diropen));
386 if (!diro)
387 return 0;
389 diro->index = 0;
390 diro->data = data;
391 diro->inode = inode;
393 /* Check if the entire tree is contained within the inode. */
394 if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
396 diro->leaf = inode->dir.dirents;
397 diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
398 diro->sorted = (char *) (inode->dir.header.sorted);
399 diro->count = inode->dir.header.count;
400 diro->dirpage = 0;
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 /* Adjust len so it we can't read past the end of the file. */
548 if (len > data->currinode.size)
549 len = data->currinode.size;
551 blockcnt = ((len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
552 / grub_le_to_cpu32 (data->sblock.blksz));
554 for (i = pos / grub_le_to_cpu32 (data->sblock.blksz); i < blockcnt; i++)
556 int blknr;
557 int blockoff = pos % grub_le_to_cpu32 (data->sblock.blksz);
558 int blockend = grub_le_to_cpu32 (data->sblock.blksz);
560 int skipfirst = 0;
562 blknr = grub_jfs_blkno (data, &data->currinode, i);
563 if (grub_errno)
564 return -1;
566 /* Last block. */
567 if (i == blockcnt - 1)
569 blockend = (len + pos) % grub_le_to_cpu32 (data->sblock.blksz);
571 if (!blockend)
572 blockend = grub_le_to_cpu32 (data->sblock.blksz);
575 /* First block. */
576 if (i == (pos / (int) grub_le_to_cpu32 (data->sblock.blksz)))
578 skipfirst = blockoff;
579 blockend -= skipfirst;
582 data->disk->read_hook = read_hook;
583 grub_disk_read (data->disk,
584 blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
585 - GRUB_DISK_SECTOR_BITS),
586 skipfirst, blockend, buf);
588 data->disk->read_hook = 0;
589 if (grub_errno)
590 return -1;
592 buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
595 return len;
599 /* Find the file with the pathname PATH on the filesystem described by
600 DATA. */
601 static grub_err_t
602 grub_jfs_find_file (struct grub_jfs_data *data, const char *path)
604 char fpath[grub_strlen (path)];
605 char *name = fpath;
606 char *next;
607 unsigned int pos = 0;
608 struct grub_jfs_diropen *diro;
610 grub_strncpy (fpath, path, grub_strlen (path) + 1);
612 if (grub_jfs_read_inode (data, GRUB_JFS_AGGR_INODE, &data->currinode))
613 return grub_errno;
615 /* Skip the first slashes. */
616 while (*name == '/')
618 name++;
619 if (!*name)
620 return 0;
623 /* Extract the actual part from the pathname. */
624 next = grub_strchr (name, '/');
625 if (next)
627 while (*next == '/')
629 next[0] = '\0';
630 next++;
633 diro = grub_jfs_opendir (data, &data->currinode);
634 if (!diro)
635 return grub_errno;
637 for (;;)
639 if (grub_strlen (name) == 0)
640 return GRUB_ERR_NONE;
642 if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
643 break;
645 /* Check if the current direntry matches the current part of the
646 pathname. */
647 if (!grub_strcmp (name, diro->name))
649 int ino = diro->ino;
650 int dirino = grub_le_to_cpu32 (data->currinode.inode);
652 grub_jfs_closedir (diro);
653 diro = 0;
655 if (grub_jfs_read_inode (data, ino, &data->currinode))
656 break;
658 /* Check if this is a symlink. */
659 if ((grub_le_to_cpu32 (data->currinode.mode)
660 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
662 grub_jfs_lookup_symlink (data, dirino);
663 if (grub_errno)
664 return grub_errno;
667 if (!next)
668 return 0;
670 pos = 0;
672 name = next;
673 next = grub_strchr (name, '/');
674 if (next)
676 next[0] = '\0';
677 next++;
680 /* Open this directory for reading dirents. */
681 diro = grub_jfs_opendir (data, &data->currinode);
682 if (!diro)
683 return grub_errno;
685 continue;
689 grub_jfs_closedir (diro);
690 grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
691 return grub_errno;
695 static grub_err_t
696 grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino)
698 int size = grub_le_to_cpu64 (data->currinode.size);
699 char symlink[size + 1];
701 if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
702 return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
704 if (size <= 128)
705 grub_strncpy (symlink, (char *) (data->currinode.symlink.path), 128);
706 else if (grub_jfs_read_file (data, 0, 0, size, symlink) < 0)
707 return grub_errno;
709 symlink[size] = '\0';
711 /* The symlink is an absolute path, go back to the root inode. */
712 if (symlink[0] == '/')
713 ino = 2;
715 /* Now load in the old inode. */
716 if (grub_jfs_read_inode (data, ino, &data->currinode))
717 return grub_errno;
719 grub_jfs_find_file (data, symlink);
720 if (grub_errno)
721 grub_error (grub_errno, "Can not follow symlink `%s'.", symlink);
723 return grub_errno;
727 static grub_err_t
728 grub_jfs_dir (grub_device_t device, const char *path,
729 int (*hook) (const char *filename,
730 const struct grub_dirhook_info *info))
732 struct grub_jfs_data *data = 0;
733 struct grub_jfs_diropen *diro = 0;
735 grub_dl_ref (my_mod);
737 data = grub_jfs_mount (device->disk);
738 if (!data)
739 goto fail;
741 if (grub_jfs_find_file (data, path))
742 goto fail;
744 diro = grub_jfs_opendir (data, &data->currinode);
745 if (!diro)
746 goto fail;
748 /* Iterate over the dirents in the directory that was found. */
749 while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
751 struct grub_jfs_inode inode;
752 struct grub_dirhook_info info;
753 grub_memset (&info, 0, sizeof (info));
755 if (grub_jfs_read_inode (data, diro->ino, &inode))
756 goto fail;
758 info.dir = (grub_le_to_cpu32 (inode.mode)
759 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
760 if (hook (diro->name, &info))
761 goto fail;
764 /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
765 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
766 grub_errno = 0;
768 fail:
769 grub_jfs_closedir (diro);
770 grub_free (data);
772 grub_dl_unref (my_mod);
774 return grub_errno;
778 /* Open a file named NAME and initialize FILE. */
779 static grub_err_t
780 grub_jfs_open (struct grub_file *file, const char *name)
782 struct grub_jfs_data *data;
784 grub_dl_ref (my_mod);
786 data = grub_jfs_mount (file->device->disk);
787 if (!data)
788 goto fail;
790 grub_jfs_find_file (data, name);
791 if (grub_errno)
792 goto fail;
794 /* It is only possible for open regular files. */
795 if (! ((grub_le_to_cpu32 (data->currinode.mode)
796 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
798 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file");
799 goto fail;
802 file->data = data;
803 file->size = grub_le_to_cpu64 (data->currinode.size);
805 return 0;
807 fail:
809 grub_dl_unref (my_mod);
811 grub_free (data);
813 return grub_errno;;
817 static grub_ssize_t
818 grub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
820 struct grub_jfs_data *data =
821 (struct grub_jfs_data *) file->data;
823 return grub_jfs_read_file (data, file->read_hook, file->offset, len, buf);
827 static grub_err_t
828 grub_jfs_close (grub_file_t file)
830 grub_free (file->data);
832 grub_dl_unref (my_mod);
834 return GRUB_ERR_NONE;
838 static grub_err_t
839 grub_jfs_label (grub_device_t device, char **label)
841 struct grub_jfs_data *data;
842 data = grub_jfs_mount (device->disk);
844 if (data)
845 *label = grub_strndup ((char *) (data->sblock.volname), 11);
846 else
847 *label = 0;
849 return grub_errno;
853 static struct grub_fs grub_jfs_fs =
855 .name = "jfs",
856 .dir = grub_jfs_dir,
857 .open = grub_jfs_open,
858 .read = grub_jfs_read,
859 .close = grub_jfs_close,
860 .label = grub_jfs_label,
861 .next = 0
864 GRUB_MOD_INIT(jfs)
866 grub_fs_register (&grub_jfs_fs);
867 my_mod = mod;
870 GRUB_MOD_FINI(jfs)
872 grub_fs_unregister (&grub_jfs_fs);