2009-07-21 Vladimir Serbinenko <phcoder@gmail.com>
[grub2/phcoder.git] / fs / ufs.c
blob336507c6dd3cca43c4f21268f1a0b3d28151b845
1 /* ufs.c - Unix File System */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,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>
29 #define GRUB_UFS_MAGIC 0x11954
30 #define GRUB_UFS2_MAGIC 0x19540119
31 #define GRUB_UFS_INODE 2
32 #define GRUB_UFS_FILETYPE_DIR 4
33 #define GRUB_UFS_FILETYPE_LNK 10
34 #define GRUB_UFS_MAX_SYMLNK_CNT 8
36 #define GRUB_UFS_DIRBLKS 12
37 #define GRUB_UFS_INDIRBLKS 3
39 #define GRUB_UFS_ATTR_TYPE 0160000
40 #define GRUB_UFS_ATTR_FILE 0100000
41 #define GRUB_UFS_ATTR_DIR 0040000
42 #define GRUB_UFS_ATTR_LNK 0120000
44 #define GRUB_UFS_VOLNAME_LEN 32
46 /* Calculate in which group the inode can be found. */
47 #define inode_group(inode,sblock) ()
49 #define UFS_BLKSZ(sblock) (grub_le_to_cpu32 (sblock->bsize))
51 #define INODE(data,field) (data->ufs_type == UFS1 ? \
52 data->inode. field : data->inode2. field)
53 #define INODE_ENDIAN(data,field,bits1,bits2) (data->ufs_type == UFS1 ? \
54 grub_le_to_cpu##bits1 (data->inode.field) : \
55 grub_le_to_cpu##bits2 (data->inode2.field))
56 #define INODE_SIZE(data) INODE_ENDIAN (data,size,32,64)
57 #define INODE_NBLOCKS(data) INODE_ENDIAN (data,nblocks,32,64)
59 #define INODE_MODE(data) INODE_ENDIAN (data,mode,16,16)
60 #define INODE_BLKSZ(data) (data->ufs_type == UFS1 ? 4 : 8)
61 #define INODE_DIRBLOCKS(data,blk) INODE_ENDIAN \
62 (data,blocks.dir_blocks[blk],32,64)
63 #define INODE_INDIRBLOCKS(data,blk) INODE_ENDIAN \
64 (data,blocks.indir_blocks[blk],32,64)
66 /* The blocks on which the superblock can be found. */
67 static int sblocklist[] = { 128, 16, 0, 512, -1 };
69 struct grub_ufs_sblock
71 grub_uint8_t unused[16];
72 /* The offset of the inodes in the cylinder group. */
73 grub_uint32_t inoblk_offs;
75 grub_uint8_t unused2[4];
77 /* The start of the cylinder group. */
78 grub_uint32_t cylg_offset;
79 grub_uint8_t unused3[4];
81 grub_uint32_t mtime;
82 grub_uint8_t unused4[12];
84 /* The size of a block in bytes. */
85 grub_int32_t bsize;
86 grub_uint8_t unused5[48];
88 /* The size of filesystem blocks to disk blocks. */
89 grub_uint32_t log2_blksz;
90 grub_uint8_t unused6[40];
91 grub_uint32_t uuidhi;
92 grub_uint32_t uuidlow;
93 grub_uint8_t unused7[32];
95 /* Inodes stored per cylinder group. */
96 grub_uint32_t ino_per_group;
98 /* The frags per cylinder group. */
99 grub_uint32_t frags_per_group;
101 grub_uint8_t unused8[488];
103 /* Volume name for UFS2. */
104 grub_uint8_t volume_name[GRUB_UFS_VOLNAME_LEN];
105 grub_uint8_t unused9[360];
107 grub_uint64_t mtime2;
108 grub_uint8_t unused10[292];
110 /* Magic value to check if this is really a UFS filesystem. */
111 grub_uint32_t magic;
114 /* UFS inode. */
115 struct grub_ufs_inode
117 grub_uint16_t mode;
118 grub_uint16_t nlinks;
119 grub_uint16_t uid;
120 grub_uint16_t gid;
121 grub_int64_t size;
122 grub_uint64_t atime;
123 grub_uint64_t mtime;
124 grub_uint64_t ctime;
125 union
127 struct
129 grub_uint32_t dir_blocks[GRUB_UFS_DIRBLKS];
130 grub_uint32_t indir_blocks[GRUB_UFS_INDIRBLKS];
131 } blocks;
132 grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 4];
134 grub_uint32_t flags;
135 grub_uint32_t nblocks;
136 grub_uint32_t gen;
137 grub_uint32_t unused;
138 grub_uint8_t pad[12];
139 } __attribute__ ((packed));
141 /* UFS inode. */
142 struct grub_ufs2_inode
144 grub_uint16_t mode;
145 grub_uint16_t nlinks;
146 grub_uint32_t uid;
147 grub_uint32_t gid;
148 grub_uint32_t blocksize;
149 grub_int64_t size;
150 grub_int64_t nblocks;
151 grub_uint64_t atime;
152 grub_uint64_t mtime;
153 grub_uint64_t ctime;
154 grub_uint64_t create_time;
155 grub_uint32_t atime_sec;
156 grub_uint32_t mtime_sec;
157 grub_uint32_t ctime_sec;
158 grub_uint32_t create_time_sec;
159 grub_uint32_t gen;
160 grub_uint32_t kernel_flags;
161 grub_uint32_t flags;
162 grub_uint32_t extsz;
163 grub_uint64_t ext[2];
164 union
166 struct
168 grub_uint64_t dir_blocks[GRUB_UFS_DIRBLKS];
169 grub_uint64_t indir_blocks[GRUB_UFS_INDIRBLKS];
170 } blocks;
171 grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 8];
174 grub_uint8_t unused[24];
175 } __attribute__ ((packed));
177 /* Directory entry. */
178 struct grub_ufs_dirent
180 grub_uint32_t ino;
181 grub_uint16_t direntlen;
182 union
184 grub_uint16_t namelen;
185 struct
187 grub_uint8_t filetype_bsd;
188 grub_uint8_t namelen_bsd;
191 } __attribute__ ((packed));
193 /* Information about a "mounted" ufs filesystem. */
194 struct grub_ufs_data
196 struct grub_ufs_sblock sblock;
197 grub_disk_t disk;
198 union
200 struct grub_ufs_inode inode;
201 struct grub_ufs2_inode inode2;
203 enum
205 UFS1,
206 UFS2,
207 UNKNOWN
208 } ufs_type;
209 int ino;
210 int linknest;
213 static grub_dl_t my_mod;
215 /* Forward declaration. */
216 static grub_err_t grub_ufs_find_file (struct grub_ufs_data *data,
217 const char *path);
220 static int
221 grub_ufs_get_file_block (struct grub_ufs_data *data, unsigned int blk)
223 struct grub_ufs_sblock *sblock = &data->sblock;
224 unsigned int indirsz;
225 int log2_blksz;
227 /* Direct. */
228 if (blk < GRUB_UFS_DIRBLKS)
229 return INODE_DIRBLOCKS (data, blk);
231 log2_blksz = grub_le_to_cpu32 (data->sblock.log2_blksz);
233 blk -= GRUB_UFS_DIRBLKS;
235 indirsz = UFS_BLKSZ (sblock) / INODE_BLKSZ (data);
236 /* Single indirect block. */
237 if (blk < indirsz)
239 grub_uint32_t indir[UFS_BLKSZ (sblock) >> 2];
240 grub_disk_read (data->disk, INODE_INDIRBLOCKS (data, 0) << log2_blksz,
241 0, sizeof (indir), indir);
242 return (data->ufs_type == UFS1) ? indir[blk] : indir[blk << 1];
244 blk -= indirsz;
246 /* Double indirect block. */
247 if (blk < indirsz * indirsz)
249 grub_uint32_t indir[UFS_BLKSZ (sblock) >> 2];
251 grub_disk_read (data->disk, INODE_INDIRBLOCKS (data, 1) << log2_blksz,
252 0, sizeof (indir), indir);
253 grub_disk_read (data->disk,
254 ((data->ufs_type == UFS1) ?
255 indir[blk / indirsz] : indir [(blk / indirsz) << 1])
256 << log2_blksz,
257 0, sizeof (indir), indir);
259 return (data->ufs_type == UFS1) ?
260 indir[blk % indirsz] : indir[(blk % indirsz) << 1];
264 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
265 "ufs does not support triple indirect blocks");
266 return 0;
270 /* Read LEN bytes from the file described by DATA starting with byte
271 POS. Return the amount of read bytes in READ. */
272 static grub_ssize_t
273 grub_ufs_read_file (struct grub_ufs_data *data,
274 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
275 unsigned offset, unsigned length),
276 int pos, grub_size_t len, char *buf)
278 struct grub_ufs_sblock *sblock = &data->sblock;
279 int i;
280 int blockcnt;
282 /* Adjust len so it we can't read past the end of the file. */
283 if (len > INODE_SIZE (data))
284 len = INODE_SIZE (data);
286 blockcnt = (len + pos + UFS_BLKSZ (sblock) - 1) / UFS_BLKSZ (sblock);
288 for (i = pos / UFS_BLKSZ (sblock); i < blockcnt; i++)
290 int blknr;
291 int blockoff = pos % UFS_BLKSZ (sblock);
292 int blockend = UFS_BLKSZ (sblock);
294 int skipfirst = 0;
296 blknr = grub_ufs_get_file_block (data, i);
297 if (grub_errno)
298 return -1;
300 /* Last block. */
301 if (i == blockcnt - 1)
303 blockend = (len + pos) % UFS_BLKSZ (sblock);
305 if (!blockend)
306 blockend = UFS_BLKSZ (sblock);
309 /* First block. */
310 if (i == (pos / (int) UFS_BLKSZ (sblock)))
312 skipfirst = blockoff;
313 blockend -= skipfirst;
316 /* XXX: If the block number is 0 this block is not stored on
317 disk but is zero filled instead. */
318 if (blknr)
320 data->disk->read_hook = read_hook;
321 grub_disk_read (data->disk,
322 blknr << grub_le_to_cpu32 (data->sblock.log2_blksz),
323 skipfirst, blockend, buf);
324 data->disk->read_hook = 0;
325 if (grub_errno)
326 return -1;
328 else
329 grub_memset (buf, UFS_BLKSZ (sblock) - skipfirst, 0);
331 buf += UFS_BLKSZ (sblock) - skipfirst;
334 return len;
338 /* Read inode INO from the mounted filesystem described by DATA. This
339 inode is used by default now. */
340 static grub_err_t
341 grub_ufs_read_inode (struct grub_ufs_data *data, int ino, char *inode)
343 struct grub_ufs_sblock *sblock = &data->sblock;
345 /* Determine the group the inode is in. */
346 int group = ino / grub_le_to_cpu32 (sblock->ino_per_group);
348 /* Determine the inode within the group. */
349 int grpino = ino % grub_le_to_cpu32 (sblock->ino_per_group);
351 /* The first block of the group. */
352 int grpblk = group * (grub_le_to_cpu32 (sblock->frags_per_group));
354 if (data->ufs_type == UFS1)
356 if (!inode)
358 inode = (char *) &data->inode;
359 data->ino = ino;
362 grub_disk_read (data->disk,
363 (((grub_le_to_cpu32 (sblock->inoblk_offs) + grpblk)
364 << grub_le_to_cpu32 (data->sblock.log2_blksz)))
365 + grpino / 4,
366 (grpino % 4) * sizeof (struct grub_ufs_inode),
367 sizeof (struct grub_ufs_inode),
368 inode);
370 else
372 if (!inode)
374 inode = (char *) &data->inode2;
375 data->ino = ino;
378 grub_disk_read (data->disk,
379 (((grub_le_to_cpu32 (sblock->inoblk_offs) + grpblk)
380 << grub_le_to_cpu32 (data->sblock.log2_blksz)))
381 + grpino / 2,
382 (grpino % 2) * sizeof (struct grub_ufs2_inode),
383 sizeof (struct grub_ufs2_inode),
384 inode);
387 return grub_errno;
391 /* Lookup the symlink the current inode points to. INO is the inode
392 number of the directory the symlink is relative to. */
393 static grub_err_t
394 grub_ufs_lookup_symlink (struct grub_ufs_data *data, int ino)
396 char symlink[INODE_SIZE (data)];
398 if (++data->linknest > GRUB_UFS_MAX_SYMLNK_CNT)
399 return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks");
401 if (INODE_NBLOCKS (data) == 0)
402 grub_strcpy (symlink, (char *) INODE (data, symlink));
403 else
405 grub_disk_read (data->disk,
406 (INODE_DIRBLOCKS (data, 0)
407 << grub_le_to_cpu32 (data->sblock.log2_blksz)),
408 0, INODE_SIZE (data), symlink);
409 symlink[INODE_SIZE (data)] = '\0';
412 /* The symlink is an absolute path, go back to the root inode. */
413 if (symlink[0] == '/')
414 ino = GRUB_UFS_INODE;
416 /* Now load in the old inode. */
417 if (grub_ufs_read_inode (data, ino, 0))
418 return grub_errno;
420 grub_ufs_find_file (data, symlink);
421 if (grub_errno)
422 grub_error (grub_errno, "Can not follow symlink `%s'.", symlink);
424 return grub_errno;
428 /* Find the file with the pathname PATH on the filesystem described by
429 DATA. */
430 static grub_err_t
431 grub_ufs_find_file (struct grub_ufs_data *data, const char *path)
433 char fpath[grub_strlen (path) + 1];
434 char *name = fpath;
435 char *next;
436 unsigned int pos = 0;
437 int dirino;
439 grub_strcpy (fpath, path);
441 /* Skip the first slash. */
442 if (name[0] == '/')
444 name++;
445 if (!*name)
446 return 0;
449 /* Extract the actual part from the pathname. */
450 next = grub_strchr (name, '/');
451 if (next)
453 next[0] = '\0';
454 next++;
459 struct grub_ufs_dirent dirent;
460 int namelen;
462 if (grub_strlen (name) == 0)
463 return GRUB_ERR_NONE;
465 if (grub_ufs_read_file (data, 0, pos, sizeof (dirent),
466 (char *) &dirent) < 0)
467 return grub_errno;
469 namelen = (data->ufs_type == UFS2)
470 ? dirent.namelen_bsd : grub_le_to_cpu16 (dirent.namelen);
473 char filename[namelen + 1];
475 if (grub_ufs_read_file (data, 0, pos + sizeof (dirent),
476 namelen, filename) < 0)
477 return grub_errno;
479 filename[namelen] = '\0';
481 if (!grub_strcmp (name, filename))
483 dirino = data->ino;
484 grub_ufs_read_inode (data, grub_le_to_cpu32 (dirent.ino), 0);
486 if ((INODE_MODE(data) & GRUB_UFS_ATTR_TYPE)
487 == GRUB_UFS_ATTR_LNK)
489 grub_ufs_lookup_symlink (data, dirino);
490 if (grub_errno)
491 return grub_errno;
494 if (!next)
495 return 0;
497 pos = 0;
499 name = next;
500 next = grub_strchr (name, '/');
501 if (next)
503 next[0] = '\0';
504 next++;
507 if ((INODE_MODE(data) & GRUB_UFS_ATTR_TYPE) != GRUB_UFS_ATTR_DIR)
508 return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
510 continue;
514 pos += grub_le_to_cpu16 (dirent.direntlen);
515 } while (pos < INODE_SIZE (data));
517 grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
518 return grub_errno;
522 /* Mount the filesystem on the disk DISK. */
523 static struct grub_ufs_data *
524 grub_ufs_mount (grub_disk_t disk)
526 struct grub_ufs_data *data;
527 int *sblklist = sblocklist;
529 data = grub_malloc (sizeof (struct grub_ufs_data));
530 if (!data)
531 return 0;
533 /* Find a UFS1 or UFS2 sblock. */
534 data->ufs_type = UNKNOWN;
535 while (*sblklist != -1)
537 grub_disk_read (disk, *sblklist, 0, sizeof (struct grub_ufs_sblock),
538 &data->sblock);
539 if (grub_errno)
540 goto fail;
542 if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS_MAGIC)
544 data->ufs_type = UFS1;
545 break;
547 else if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS2_MAGIC)
549 data->ufs_type = UFS2;
550 break;
552 sblklist++;
554 if (data->ufs_type == UNKNOWN)
556 grub_error (GRUB_ERR_BAD_FS, "not an ufs filesystem");
557 goto fail;
560 data->disk = disk;
561 data->linknest = 0;
562 return data;
564 fail:
565 grub_free (data);
567 if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
568 grub_error (GRUB_ERR_BAD_FS, "not a ufs filesystem");
570 return 0;
574 static grub_err_t
575 grub_ufs_dir (grub_device_t device, const char *path,
576 int (*hook) (const char *filename,
577 const struct grub_dirhook_info *info))
579 struct grub_ufs_data *data;
580 struct grub_ufs_sblock *sblock;
581 unsigned int pos = 0;
583 data = grub_ufs_mount (device->disk);
584 if (!data)
585 return grub_errno;
587 grub_ufs_read_inode (data, GRUB_UFS_INODE, 0);
588 if (grub_errno)
589 return grub_errno;
591 sblock = &data->sblock;
593 if (!path || path[0] != '/')
595 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
596 return grub_errno;
599 grub_ufs_find_file (data, path);
600 if (grub_errno)
601 goto fail;
603 if ((INODE_MODE (data) & GRUB_UFS_ATTR_TYPE) != GRUB_UFS_ATTR_DIR)
605 grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
606 goto fail;
609 while (pos < INODE_SIZE (data))
611 struct grub_ufs_dirent dirent;
612 int namelen;
614 if (grub_ufs_read_file (data, 0, pos, sizeof (dirent),
615 (char *) &dirent) < 0)
616 break;
618 namelen = (data->ufs_type == UFS2)
619 ? dirent.namelen_bsd : grub_le_to_cpu16 (dirent.namelen);
622 char filename[namelen + 1];
623 struct grub_dirhook_info info;
624 grub_memset (&info, 0, sizeof (info));
626 if (grub_ufs_read_file (data, 0, pos + sizeof (dirent),
627 namelen, filename) < 0)
628 break;
630 filename[namelen] = '\0';
631 if (data->ufs_type == UFS1)
633 struct grub_ufs_inode inode;
634 grub_ufs_read_inode (data, dirent.ino, (char *) &inode);
635 info.dir = ((grub_le_to_cpu16 (inode.mode) & GRUB_UFS_ATTR_TYPE)
636 == GRUB_UFS_ATTR_DIR);
637 info.mtime = grub_le_to_cpu64 (inode.mtime);
638 info.mtimeset = 1;
640 else
642 struct grub_ufs2_inode inode;
643 grub_ufs_read_inode (data, dirent.ino, (char *) &inode);
644 info.dir = ((grub_le_to_cpu16 (inode.mode) & GRUB_UFS_ATTR_TYPE)
645 == GRUB_UFS_ATTR_DIR);
646 info.mtime = grub_le_to_cpu64 (inode.mtime);
647 info.mtimeset = 1;
650 if (hook (filename, &info))
651 break;
654 pos += grub_le_to_cpu16 (dirent.direntlen);
657 fail:
658 grub_free (data);
660 return grub_errno;
664 /* Open a file named NAME and initialize FILE. */
665 static grub_err_t
666 grub_ufs_open (struct grub_file *file, const char *name)
668 struct grub_ufs_data *data;
669 data = grub_ufs_mount (file->device->disk);
670 if (!data)
671 return grub_errno;
673 grub_ufs_read_inode (data, 2, 0);
674 if (grub_errno)
676 grub_free (data);
677 return grub_errno;
680 if (!name || name[0] != '/')
682 grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
683 return grub_errno;
686 grub_ufs_find_file (data, name);
687 if (grub_errno)
689 grub_free (data);
690 return grub_errno;
693 file->data = data;
694 file->size = INODE_SIZE (data);
696 return GRUB_ERR_NONE;
700 static grub_ssize_t
701 grub_ufs_read (grub_file_t file, char *buf, grub_size_t len)
703 struct grub_ufs_data *data =
704 (struct grub_ufs_data *) file->data;
706 return grub_ufs_read_file (data, file->read_hook, file->offset, len, buf);
710 static grub_err_t
711 grub_ufs_close (grub_file_t file)
713 grub_free (file->data);
715 return GRUB_ERR_NONE;
719 static grub_err_t
720 grub_ufs_label (grub_device_t device, char **label)
722 struct grub_ufs_data *data = 0;
724 grub_dl_ref (my_mod);
726 *label = 0;
728 data = grub_ufs_mount (device->disk);
729 if (data)
731 if (data->ufs_type == UFS2)
732 *label = grub_strdup ((char *) data->sblock.volume_name);
735 grub_dl_unref (my_mod);
737 grub_free (data);
739 return grub_errno;
742 static grub_err_t
743 grub_ufs_uuid (grub_device_t device, char **uuid)
745 struct grub_ufs_data *data;
746 grub_disk_t disk = device->disk;
748 grub_dl_ref (my_mod);
750 data = grub_ufs_mount (disk);
751 if (data)
753 *uuid = grub_malloc (16 + sizeof ('\0'));
754 grub_sprintf (*uuid, "%08x%08x",
755 (unsigned) grub_le_to_cpu32 (data->sblock.uuidhi),
756 (unsigned) grub_le_to_cpu32 (data->sblock.uuidlow));
758 else
759 *uuid = NULL;
761 grub_dl_unref (my_mod);
763 grub_free (data);
765 return grub_errno;
769 /* Get mtime. */
770 static grub_err_t
771 grub_ufs_mtime (grub_device_t device, grub_int32_t *tm)
773 struct grub_ufs_data *data = 0;
775 grub_dl_ref (my_mod);
777 data = grub_ufs_mount (device->disk);
778 if (!data)
779 *tm = 0;
780 else if (data->ufs_type == UFS1)
781 *tm = grub_le_to_cpu32 (data->sblock.mtime);
782 else
783 *tm = grub_le_to_cpu64 (data->sblock.mtime2);
785 grub_dl_unref (my_mod);
787 grub_free (data);
789 return grub_errno;
794 static struct grub_fs grub_ufs_fs =
796 .name = "ufs",
797 .dir = grub_ufs_dir,
798 .open = grub_ufs_open,
799 .read = grub_ufs_read,
800 .close = grub_ufs_close,
801 .label = grub_ufs_label,
802 .uuid = grub_ufs_uuid,
803 .mtime = grub_ufs_mtime,
804 .next = 0
807 GRUB_MOD_INIT(ufs)
809 grub_fs_register (&grub_ufs_fs);
810 my_mod = mod;
813 GRUB_MOD_FINI(ufs)
815 grub_fs_unregister (&grub_ufs_fs);