]> git.proxmox.com Git - grub2.git/blob - fs/jfs.c
merge mtrunk into xnu
[grub2.git] / fs / jfs.c
1 /* jfs.c - JFS. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,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/charset.h>
28
29 #define GRUB_JFS_MAX_SYMLNK_CNT 8
30 #define GRUB_JFS_FILETYPE_MASK 0170000
31 #define GRUB_JFS_FILETYPE_REG 0100000
32 #define GRUB_JFS_FILETYPE_LNK 0120000
33 #define GRUB_JFS_FILETYPE_DIR 0040000
34
35 #define GRUB_JFS_SBLOCK 64
36 #define GRUB_JFS_AGGR_INODE 2
37 #define GRUB_JFS_FS1_INODE_BLK 104
38
39 #define GRUB_JFS_TREE_LEAF 2
40
41 struct grub_jfs_sblock
42 {
43 /* The magic for JFS. It should contain the string "JFS1". */
44 grub_uint8_t magic[4];
45 grub_uint32_t version;
46 grub_uint64_t ag_size;
47
48 /* The size of a filesystem block in bytes. XXX: currently only
49 4096 was tested. */
50 grub_uint32_t blksz;
51 grub_uint16_t log2_blksz;
52
53 grub_uint8_t unused[71];
54 grub_uint8_t volname[11];
55 grub_uint8_t unused2[32];
56 grub_uint8_t uuid[16];
57 };
58
59 struct grub_jfs_extent
60 {
61 /* The length of the extent in filesystem blocks. */
62 grub_uint16_t length;
63 grub_uint8_t length2;
64
65 /* The physical offset of the first block on the disk. */
66 grub_uint8_t blk1;
67 grub_uint32_t blk2;
68 } __attribute__ ((packed));
69
70 struct grub_jfs_iag
71 {
72 grub_uint8_t unused[3072];
73 struct grub_jfs_extent inodes[128];
74 } __attribute__ ((packed));
75
76
77 /* The head of the tree used to find extents. */
78 struct grub_jfs_treehead
79 {
80 grub_uint64_t next;
81 grub_uint64_t prev;
82
83 grub_uint8_t flags;
84 grub_uint8_t unused;
85
86 grub_uint16_t count;
87 grub_uint16_t max;
88 grub_uint8_t unused2[10];
89 } __attribute__ ((packed));
90
91 /* A node in the extent tree. */
92 struct grub_jfs_tree_extent
93 {
94 grub_uint8_t flags;
95 grub_uint16_t unused;
96
97 /* The offset is the key used to lookup an extent. */
98 grub_uint8_t offset1;
99 grub_uint32_t offset2;
100
101 struct grub_jfs_extent extent;
102 } __attribute__ ((packed));
103
104 /* The tree of directory entries. */
105 struct grub_jfs_tree_dir
106 {
107 /* Pointers to the previous and next tree headers of other nodes on
108 this level. */
109 grub_uint64_t nextb;
110 grub_uint64_t prevb;
111
112 grub_uint8_t flags;
113
114 /* The amount of dirents in this node. */
115 grub_uint8_t count;
116 grub_uint8_t freecnt;
117 grub_uint8_t freelist;
118 grub_uint8_t maxslot;
119
120 /* The location of the sorted array of pointers to dirents. */
121 grub_uint8_t sindex;
122 grub_uint8_t unused[10];
123 } __attribute__ ((packed));
124
125 /* An internal node in the dirents tree. */
126 struct grub_jfs_internal_dirent
127 {
128 struct grub_jfs_extent ex;
129 grub_uint8_t next;
130 grub_uint8_t len;
131 grub_uint16_t namepart[11];
132 } __attribute__ ((packed));
133
134 /* A leaf node in the dirents tree. */
135 struct grub_jfs_leaf_dirent
136 {
137 /* The inode for this dirent. */
138 grub_uint32_t inode;
139 grub_uint8_t next;
140
141 /* The size of the name. */
142 grub_uint8_t len;
143 grub_uint16_t namepart[11];
144 grub_uint32_t index;
145 } __attribute__ ((packed));
146
147 /* A leaf in the dirents tree. This one is used if the previously
148 dirent was not big enough to store the name. */
149 struct grub_jfs_leaf_next_dirent
150 {
151 grub_uint8_t next;
152 grub_uint8_t len;
153 grub_uint16_t namepart[15];
154 } __attribute__ ((packed));
155
156 struct grub_jfs_inode
157 {
158 grub_uint32_t stamp;
159 grub_uint32_t fileset;
160 grub_uint32_t inode;
161 grub_uint8_t unused[12];
162 grub_uint64_t size;
163 grub_uint8_t unused2[20];
164 grub_uint32_t mode;
165 grub_uint8_t unused3[72];
166 grub_uint8_t unused4[96];
167
168 union
169 {
170 /* The tree describing the extents of the file. */
171 struct __attribute__ ((packed))
172 {
173 struct grub_jfs_treehead tree;
174 struct grub_jfs_tree_extent extents[16];
175 } file;
176 union
177 {
178 /* The tree describing the dirents. */
179 struct
180 {
181 grub_uint8_t unused[16];
182 grub_uint8_t flags;
183
184 /* Amount of dirents in this node. */
185 grub_uint8_t count;
186 grub_uint8_t freecnt;
187 grub_uint8_t freelist;
188 grub_uint32_t idotdot;
189 grub_uint8_t sorted[8];
190 } header;
191 struct grub_jfs_leaf_dirent dirents[8];
192 } dir __attribute__ ((packed));
193 /* Fast symlink. */
194 struct
195 {
196 grub_uint8_t unused[32];
197 grub_uint8_t path[128];
198 } symlink;
199 } __attribute__ ((packed));
200 } __attribute__ ((packed));
201
202 struct grub_jfs_data
203 {
204 struct grub_jfs_sblock sblock;
205 grub_disk_t disk;
206 struct grub_jfs_inode fileset;
207 struct grub_jfs_inode currinode;
208 int pos;
209 int linknest;
210 } __attribute__ ((packed));
211
212 struct grub_jfs_diropen
213 {
214 int index;
215 union
216 {
217 struct grub_jfs_tree_dir header;
218 struct grub_jfs_leaf_dirent dirent[0];
219 struct grub_jfs_leaf_next_dirent next_dirent[0];
220 char sorted[0];
221 } *dirpage __attribute__ ((packed));
222 struct grub_jfs_data *data;
223 struct grub_jfs_inode *inode;
224 int count;
225 char *sorted;
226 struct grub_jfs_leaf_dirent *leaf;
227 struct grub_jfs_leaf_next_dirent *next_leaf;
228
229 /* The filename and inode of the last read dirent. */
230 char name[255];
231 grub_uint32_t ino;
232 } __attribute__ ((packed));
233
234
235 static grub_dl_t my_mod;
236 \f
237 static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino);
238
239 /* Get the block number for the block BLK in the node INODE in the
240 mounted filesystem DATA. */
241 static int
242 grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode,
243 unsigned int blk)
244 {
245 auto int getblk (struct grub_jfs_treehead *treehead,
246 struct grub_jfs_tree_extent *extents);
247
248 int getblk (struct grub_jfs_treehead *treehead,
249 struct grub_jfs_tree_extent *extents)
250 {
251 int found = -1;
252 int i;
253
254 for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++)
255 {
256 if (treehead->flags & GRUB_JFS_TREE_LEAF)
257 {
258 /* Read the leafnode. */
259 if (grub_le_to_cpu32 (extents[i].offset2) <= blk
260 && ((grub_le_to_cpu16 (extents[i].extent.length))
261 + (extents[i].extent.length2 << 8)
262 + grub_le_to_cpu32 (extents[i].offset2)) > blk)
263 return (blk - grub_le_to_cpu32 (extents[i].offset2)
264 + grub_le_to_cpu32 (extents[i].extent.blk2));
265 }
266 else
267 if (blk >= grub_le_to_cpu32 (extents[i].offset2))
268 found = i;
269 }
270
271 if (found != -1)
272 {
273 struct
274 {
275 struct grub_jfs_treehead treehead;
276 struct grub_jfs_tree_extent extents[254];
277 } tree;
278
279 if (grub_disk_read (data->disk,
280 grub_le_to_cpu32 (extents[found].extent.blk2)
281 << (grub_le_to_cpu16 (data->sblock.log2_blksz)
282 - GRUB_DISK_SECTOR_BITS), 0,
283 sizeof (tree), (char *) &tree))
284 return -1;
285
286 return getblk (&tree.treehead, &tree.extents[0]);
287 }
288
289 return -1;
290 }
291
292 return getblk (&inode->file.tree, &inode->file.extents[0]);
293 }
294
295
296 static grub_err_t
297 grub_jfs_read_inode (struct grub_jfs_data *data, int ino,
298 struct grub_jfs_inode *inode)
299 {
300 struct grub_jfs_iag iag;
301 int iagnum = ino / 4096;
302 int inoext = (ino % 4096) / 32;
303 int inonum = (ino % 4096) % 32;
304 grub_uint32_t iagblk;
305 grub_uint32_t inoblk;
306
307 iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1);
308 if (grub_errno)
309 return grub_errno;
310
311 /* Read in the IAG. */
312 if (grub_disk_read (data->disk,
313 iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz)
314 - GRUB_DISK_SECTOR_BITS), 0,
315 sizeof (struct grub_jfs_iag), &iag))
316 return grub_errno;
317
318 inoblk = grub_le_to_cpu32 (iag.inodes[inoext].blk2);
319 inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz)
320 - GRUB_DISK_SECTOR_BITS);
321 inoblk += inonum;
322
323 if (grub_disk_read (data->disk, inoblk, 0,
324 sizeof (struct grub_jfs_inode), inode))
325 return grub_errno;
326
327 return 0;
328 }
329
330
331 static struct grub_jfs_data *
332 grub_jfs_mount (grub_disk_t disk)
333 {
334 struct grub_jfs_data *data = 0;
335
336 data = grub_malloc (sizeof (struct grub_jfs_data));
337 if (!data)
338 return 0;
339
340 /* Read the superblock. */
341 if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0,
342 sizeof (struct grub_jfs_sblock), &data->sblock))
343 goto fail;
344
345 if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4))
346 {
347 grub_error (GRUB_ERR_BAD_FS, "not a jfs filesystem");
348 goto fail;
349 }
350
351 data->disk = disk;
352 data->pos = 0;
353 data->linknest = 0;
354
355 /* Read the inode of the first fileset. */
356 if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0,
357 sizeof (struct grub_jfs_inode), &data->fileset))
358 goto fail;
359
360 return data;
361
362 fail:
363 grub_free (data);
364
365 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
366 grub_error (GRUB_ERR_BAD_FS, "not a jfs filesystem");
367
368 return 0;
369 }
370
371
372 static struct grub_jfs_diropen *
373 grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode)
374 {
375 struct grub_jfs_internal_dirent *de;
376 struct grub_jfs_diropen *diro;
377 int blk;
378
379 de = (struct grub_jfs_internal_dirent *) inode->dir.dirents;
380
381 if (!((grub_le_to_cpu32 (inode->mode)
382 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR))
383 {
384 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
385 return 0;
386 }
387
388 diro = grub_zalloc (sizeof (struct grub_jfs_diropen));
389 if (!diro)
390 return 0;
391
392 diro->data = data;
393 diro->inode = inode;
394
395 /* Check if the entire tree is contained within the inode. */
396 if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
397 {
398 diro->leaf = inode->dir.dirents;
399 diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de;
400 diro->sorted = (char *) (inode->dir.header.sorted);
401 diro->count = inode->dir.header.count;
402
403 return diro;
404 }
405
406 diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz));
407 if (!diro->dirpage)
408 {
409 grub_free (diro);
410 return 0;
411 }
412
413 blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2);
414 blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS);
415
416 /* Read in the nodes until we are on the leaf node level. */
417 do
418 {
419 int index;
420 if (grub_disk_read (data->disk, blk, 0,
421 grub_le_to_cpu32 (data->sblock.blksz),
422 diro->dirpage->sorted))
423 {
424 grub_free (diro->dirpage);
425 grub_free (diro);
426 return 0;
427 }
428
429 de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent;
430 index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
431 blk = (grub_le_to_cpu32 (de[index].ex.blk2)
432 << (grub_le_to_cpu16 (data->sblock.log2_blksz)
433 - GRUB_DISK_SECTOR_BITS));
434 } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF));
435
436 diro->leaf = diro->dirpage->dirent;
437 diro->next_leaf = diro->dirpage->next_dirent;
438 diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
439 diro->count = diro->dirpage->header.count;
440
441 return diro;
442 }
443
444
445 static void
446 grub_jfs_closedir (struct grub_jfs_diropen *diro)
447 {
448 if (!diro)
449 return;
450 grub_free (diro->dirpage);
451 grub_free (diro);
452 }
453
454
455 /* Read in the next dirent from the directory described by DIRO. */
456 static grub_err_t
457 grub_jfs_getent (struct grub_jfs_diropen *diro)
458 {
459 int strpos = 0;
460 struct grub_jfs_leaf_dirent *leaf;
461 struct grub_jfs_leaf_next_dirent *next_leaf;
462 int len;
463 int nextent;
464 grub_uint16_t filename[255];
465
466 auto void addstr (grub_uint16_t *uname, int ulen);
467
468 /* Add the unicode string to the utf16 filename buffer. */
469 void addstr (grub_uint16_t *name, int ulen)
470 {
471 while (ulen--)
472 filename[strpos++] = *(name++);
473 }
474
475 /* The last node, read in more. */
476 if (diro->index == diro->count)
477 {
478 unsigned int next;
479
480 /* If the inode contains the entry tree or if this was the last
481 node, there is nothing to read. */
482 if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF)
483 || !grub_le_to_cpu64 (diro->dirpage->header.nextb))
484 return GRUB_ERR_OUT_OF_RANGE;
485
486 next = grub_le_to_cpu64 (diro->dirpage->header.nextb);
487 next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz)
488 - GRUB_DISK_SECTOR_BITS);
489
490 if (grub_disk_read (diro->data->disk, next, 0,
491 grub_le_to_cpu32 (diro->data->sblock.blksz),
492 diro->dirpage->sorted))
493 return grub_errno;
494
495 diro->leaf = diro->dirpage->dirent;
496 diro->next_leaf = diro->dirpage->next_dirent;
497 diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32];
498 diro->count = diro->dirpage->header.count;
499 diro->index = 0;
500 }
501
502 leaf = &diro->leaf[(int) diro->sorted[diro->index]];
503 next_leaf = &diro->next_leaf[diro->index];
504
505 len = leaf->len;
506 if (!len)
507 {
508 diro->index++;
509 return grub_jfs_getent (diro);
510 }
511
512 addstr (leaf->namepart, len < 11 ? len : 11);
513 diro->ino = grub_le_to_cpu32 (leaf->inode);
514 len -= 11;
515
516 /* Move down to the leaf level. */
517 nextent = leaf->next;
518 if (leaf->next != 255)
519 do
520 {
521 next_leaf = &diro->next_leaf[nextent];
522 addstr (next_leaf->namepart, len < 15 ? len : 15 );
523
524 len -= 15;
525 nextent = next_leaf->next;
526 } while (next_leaf->next != 255 && len > 0);
527
528 diro->index++;
529
530 /* Convert the temporary UTF16 filename to UTF8. */
531 *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0';
532
533 return 0;
534 }
535
536
537 /* Read LEN bytes from the file described by DATA starting with byte
538 POS. Return the amount of read bytes in READ. */
539 static grub_ssize_t
540 grub_jfs_read_file (struct grub_jfs_data *data,
541 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
542 unsigned offset, unsigned length),
543 int pos, grub_size_t len, char *buf)
544 {
545 int i;
546 int blockcnt;
547
548 blockcnt = ((len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1)
549 / grub_le_to_cpu32 (data->sblock.blksz));
550
551 for (i = pos / grub_le_to_cpu32 (data->sblock.blksz); i < blockcnt; i++)
552 {
553 int blknr;
554 int blockoff = pos % grub_le_to_cpu32 (data->sblock.blksz);
555 int blockend = grub_le_to_cpu32 (data->sblock.blksz);
556
557 int skipfirst = 0;
558
559 blknr = grub_jfs_blkno (data, &data->currinode, i);
560 if (grub_errno)
561 return -1;
562
563 /* Last block. */
564 if (i == blockcnt - 1)
565 {
566 blockend = (len + pos) % grub_le_to_cpu32 (data->sblock.blksz);
567
568 if (!blockend)
569 blockend = grub_le_to_cpu32 (data->sblock.blksz);
570 }
571
572 /* First block. */
573 if (i == (pos / (int) grub_le_to_cpu32 (data->sblock.blksz)))
574 {
575 skipfirst = blockoff;
576 blockend -= skipfirst;
577 }
578
579 data->disk->read_hook = read_hook;
580 grub_disk_read (data->disk,
581 blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz)
582 - GRUB_DISK_SECTOR_BITS),
583 skipfirst, blockend, buf);
584
585 data->disk->read_hook = 0;
586 if (grub_errno)
587 return -1;
588
589 buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst;
590 }
591
592 return len;
593 }
594
595
596 /* Find the file with the pathname PATH on the filesystem described by
597 DATA. */
598 static grub_err_t
599 grub_jfs_find_file (struct grub_jfs_data *data, const char *path)
600 {
601 char fpath[grub_strlen (path)];
602 char *name = fpath;
603 char *next;
604 unsigned int pos = 0;
605 struct grub_jfs_diropen *diro;
606
607 grub_strncpy (fpath, path, grub_strlen (path) + 1);
608
609 if (grub_jfs_read_inode (data, GRUB_JFS_AGGR_INODE, &data->currinode))
610 return grub_errno;
611
612 /* Skip the first slashes. */
613 while (*name == '/')
614 {
615 name++;
616 if (!*name)
617 return 0;
618 }
619
620 /* Extract the actual part from the pathname. */
621 next = grub_strchr (name, '/');
622 if (next)
623 {
624 while (*next == '/')
625 {
626 next[0] = '\0';
627 next++;
628 }
629 }
630 diro = grub_jfs_opendir (data, &data->currinode);
631 if (!diro)
632 return grub_errno;
633
634 for (;;)
635 {
636 if (grub_strlen (name) == 0)
637 return GRUB_ERR_NONE;
638
639 if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE)
640 break;
641
642 /* Check if the current direntry matches the current part of the
643 pathname. */
644 if (!grub_strcmp (name, diro->name))
645 {
646 int ino = diro->ino;
647 int dirino = grub_le_to_cpu32 (data->currinode.inode);
648
649 grub_jfs_closedir (diro);
650 diro = 0;
651
652 if (grub_jfs_read_inode (data, ino, &data->currinode))
653 break;
654
655 /* Check if this is a symlink. */
656 if ((grub_le_to_cpu32 (data->currinode.mode)
657 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK)
658 {
659 grub_jfs_lookup_symlink (data, dirino);
660 if (grub_errno)
661 return grub_errno;
662 }
663
664 if (!next)
665 return 0;
666
667 pos = 0;
668
669 name = next;
670 next = grub_strchr (name, '/');
671 if (next)
672 {
673 next[0] = '\0';
674 next++;
675 }
676
677 /* Open this directory for reading dirents. */
678 diro = grub_jfs_opendir (data, &data->currinode);
679 if (!diro)
680 return grub_errno;
681
682 continue;
683 }
684 }
685
686 grub_jfs_closedir (diro);
687 grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
688 return grub_errno;
689 }
690
691
692 static grub_err_t
693 grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino)
694 {
695 int size = grub_le_to_cpu64 (data->currinode.size);
696 char symlink[size + 1];
697
698 if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT)
699 return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
700
701 if (size <= 128)
702 grub_strncpy (symlink, (char *) (data->currinode.symlink.path), 128);
703 else if (grub_jfs_read_file (data, 0, 0, size, symlink) < 0)
704 return grub_errno;
705
706 symlink[size] = '\0';
707
708 /* The symlink is an absolute path, go back to the root inode. */
709 if (symlink[0] == '/')
710 ino = 2;
711
712 /* Now load in the old inode. */
713 if (grub_jfs_read_inode (data, ino, &data->currinode))
714 return grub_errno;
715
716 grub_jfs_find_file (data, symlink);
717 if (grub_errno)
718 grub_error (grub_errno, "Can not follow symlink `%s'.", symlink);
719
720 return grub_errno;
721 }
722 \f
723
724 static grub_err_t
725 grub_jfs_dir (grub_device_t device, const char *path,
726 int (*hook) (const char *filename,
727 const struct grub_dirhook_info *info))
728 {
729 struct grub_jfs_data *data = 0;
730 struct grub_jfs_diropen *diro = 0;
731
732 grub_dl_ref (my_mod);
733
734 data = grub_jfs_mount (device->disk);
735 if (!data)
736 goto fail;
737
738 if (grub_jfs_find_file (data, path))
739 goto fail;
740
741 diro = grub_jfs_opendir (data, &data->currinode);
742 if (!diro)
743 goto fail;
744
745 /* Iterate over the dirents in the directory that was found. */
746 while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE)
747 {
748 struct grub_jfs_inode inode;
749 struct grub_dirhook_info info;
750 grub_memset (&info, 0, sizeof (info));
751
752 if (grub_jfs_read_inode (data, diro->ino, &inode))
753 goto fail;
754
755 info.dir = (grub_le_to_cpu32 (inode.mode)
756 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR;
757 if (hook (diro->name, &info))
758 goto fail;
759 }
760
761 /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */
762 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
763 grub_errno = 0;
764
765 fail:
766 grub_jfs_closedir (diro);
767 grub_free (data);
768
769 grub_dl_unref (my_mod);
770
771 return grub_errno;
772 }
773
774
775 /* Open a file named NAME and initialize FILE. */
776 static grub_err_t
777 grub_jfs_open (struct grub_file *file, const char *name)
778 {
779 struct grub_jfs_data *data;
780
781 grub_dl_ref (my_mod);
782
783 data = grub_jfs_mount (file->device->disk);
784 if (!data)
785 goto fail;
786
787 grub_jfs_find_file (data, name);
788 if (grub_errno)
789 goto fail;
790
791 /* It is only possible for open regular files. */
792 if (! ((grub_le_to_cpu32 (data->currinode.mode)
793 & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG))
794 {
795 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file");
796 goto fail;
797 }
798
799 file->data = data;
800 file->size = grub_le_to_cpu64 (data->currinode.size);
801
802 return 0;
803
804 fail:
805
806 grub_dl_unref (my_mod);
807
808 grub_free (data);
809
810 return grub_errno;
811 }
812
813
814 static grub_ssize_t
815 grub_jfs_read (grub_file_t file, char *buf, grub_size_t len)
816 {
817 struct grub_jfs_data *data =
818 (struct grub_jfs_data *) file->data;
819
820 return grub_jfs_read_file (data, file->read_hook, file->offset, len, buf);
821 }
822
823
824 static grub_err_t
825 grub_jfs_close (grub_file_t file)
826 {
827 grub_free (file->data);
828
829 grub_dl_unref (my_mod);
830
831 return GRUB_ERR_NONE;
832 }
833
834 static grub_err_t
835 grub_jfs_uuid (grub_device_t device, char **uuid)
836 {
837 struct grub_jfs_data *data;
838 grub_disk_t disk = device->disk;
839
840 grub_dl_ref (my_mod);
841
842 data = grub_jfs_mount (disk);
843 if (data)
844 {
845 *uuid = grub_malloc (40 + sizeof ('\0'));
846
847 grub_sprintf (*uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
848 data->sblock.uuid[0], data->sblock.uuid[1],
849 data->sblock.uuid[2], data->sblock.uuid[3],
850 data->sblock.uuid[4], data->sblock.uuid[5],
851 data->sblock.uuid[6], data->sblock.uuid[7],
852 data->sblock.uuid[8], data->sblock.uuid[9],
853 data->sblock.uuid[10], data->sblock.uuid[11],
854 data->sblock.uuid[12], data->sblock.uuid[13],
855 data->sblock.uuid[14], data->sblock.uuid[15]);
856 }
857 else
858 *uuid = NULL;
859
860 grub_dl_unref (my_mod);
861
862 grub_free (data);
863
864 return grub_errno;
865 }
866
867 static grub_err_t
868 grub_jfs_label (grub_device_t device, char **label)
869 {
870 struct grub_jfs_data *data;
871 data = grub_jfs_mount (device->disk);
872
873 if (data)
874 *label = grub_strndup ((char *) (data->sblock.volname), 11);
875 else
876 *label = 0;
877
878 return grub_errno;
879 }
880 \f
881
882 static struct grub_fs grub_jfs_fs =
883 {
884 .name = "jfs",
885 .dir = grub_jfs_dir,
886 .open = grub_jfs_open,
887 .read = grub_jfs_read,
888 .close = grub_jfs_close,
889 .label = grub_jfs_label,
890 .uuid = grub_jfs_uuid,
891 .next = 0
892 };
893
894 GRUB_MOD_INIT(jfs)
895 {
896 grub_fs_register (&grub_jfs_fs);
897 my_mod = mod;
898 }
899
900 GRUB_MOD_FINI(jfs)
901 {
902 grub_fs_unregister (&grub_jfs_fs);
903 }