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