2 * fsys_xfs.c - an implementation for the SGI XFS file system
4 * Copyright (C) 2001 Ethan Benson
8 * Copyright (C) 2001 Serguei Tzukanov
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 int xfs_read_data (char *buf, int len);
38 int xfs_dir (char *dirname);
40 /* Exported in struct fs_t */
41 static int xfs_open(struct boot_file_t *file, const char *dev_name,
42 struct partition_t *part, const char *file_name);
43 static int xfs_read(struct boot_file_t *file, unsigned int size, void *buffer);
44 static int xfs_seek(struct boot_file_t *file, unsigned int newpos);
45 static int xfs_close(struct boot_file_t *file);
47 struct fs_t xfs_filesystem = {
55 struct boot_file_t *xfs_file;
56 static char FSYS_BUF[32768];
57 uint64_t partition_offset;
61 xfs_open(struct boot_file_t *file, const char *dev_name,
62 struct partition_t *part, const char *file_name)
64 static char buffer[1024];
71 DEBUG_F("Determining offset for partition %d\n", part->part_number);
72 partition_offset = ((uint64_t) part->part_start) * part->blocksize;
73 DEBUG_F("%Lu = %lu * %hu\n", partition_offset,
80 sprintf(buffer, "%s:%d", dev_name, 0); /* 0 is full disk in OF */
81 DEBUG_F("Trying to open dev_name=%s; filename=%s; partition offset=%Lu\n",
82 buffer, file_name, partition_offset);
83 file->of_device = prom_open(buffer);
85 if (file->of_device == PROM_INVALID_HANDLE || file->of_device == NULL)
87 DEBUG_F("Can't open device %p\n", file->of_device);
88 DEBUG_LEAVE(FILE_ERR_BADDEV);
89 return FILE_ERR_BADDEV;
92 DEBUG_F("%p was successfully opened\n", file->of_device);
98 DEBUG_F("Couldn't open XFS @ %s/%Lu\n", buffer, partition_offset);
99 prom_close(file->of_device);
100 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
102 return FILE_ERR_BAD_FSYS;
105 DEBUG_F("Attempting to open %s\n", file_name);
106 strcpy(buffer, file_name); /* xfs_dir modifies argument */
109 DEBUG_F("xfs_dir() failed. errnum = %d\n", errnum);
110 prom_close( file->of_device );
111 DEBUG_LEAVE_F(errnum);
116 DEBUG_F("Successfully opened %s\n", file_name);
118 DEBUG_LEAVE(FILE_ERR_OK);
123 xfs_read(struct boot_file_t *file, unsigned int size, void *buffer)
125 return xfs_read_data(buffer, size);
129 xfs_seek(struct boot_file_t *file, unsigned int newpos)
136 xfs_close(struct boot_file_t *file)
140 prom_close(file->of_device);
142 DEBUG_F("xfs_close called\n");
148 read_disk_block(struct boot_file_t *file, uint64_t block, int start,
149 int length, void *buf)
151 uint64_t pos = block * 512;
152 pos += partition_offset + start;
153 DEBUG_F("Reading %d bytes, starting at block %Lu, disk offset %Lu\n",
155 if (!prom_lseek(file->of_device, pos)) {
156 DEBUG_F("prom_lseek failed\n");
159 return prom_read(file->of_device, buf, length);
162 #define MAX_LINK_COUNT 8
165 xfs_fileoff_t offset;
174 unsigned int agblocks;
181 unsigned int nextents;
186 xfs_bmbt_rec_32_t *xt;
197 static struct xfs_info xfs;
199 #define dirbuf ((char *)FSYS_BUF)
200 #define filebuf ((char *)FSYS_BUF + 4096)
201 #define inode ((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
202 #define icore (inode->di_core)
204 #define mask32lo(n) (((__uint32_t)1 << (n)) - 1)
206 #define XFS_INO_MASK(k) ((__uint32_t)((1ULL << (k)) - 1))
207 #define XFS_INO_OFFSET_BITS xfs.inopblog
208 #define XFS_INO_AGBNO_BITS xfs.agblklog
209 #define XFS_INO_AGINO_BITS (xfs.agblklog + xfs.inopblog)
210 #define XFS_INO_AGNO_BITS xfs.agnolog
212 static inline xfs_agblock_t
213 agino2agbno (xfs_agino_t agino)
215 return agino >> XFS_INO_OFFSET_BITS;
218 static inline xfs_agnumber_t
219 ino2agno (xfs_ino_t ino)
221 return ino >> XFS_INO_AGINO_BITS;
224 static inline xfs_agino_t
225 ino2agino (xfs_ino_t ino)
227 return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
231 ino2offset (xfs_ino_t ino)
233 return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
236 /* XFS is big endian, powerpc is big endian */
242 xt_start (xfs_bmbt_rec_32_t *r)
244 return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
245 (((xfs_fsblock_t)le32 (r->l2)) << 11) |
246 (((xfs_fsblock_t)le32 (r->l3)) >> 21);
250 xt_offset (xfs_bmbt_rec_32_t *r)
252 return (((xfs_fileoff_t)le32 (r->l0) &
253 mask32lo(31)) << 23) |
254 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
258 xt_len (xfs_bmbt_rec_32_t *r)
260 return le32(r->l3) & mask32lo(21);
263 static const char xfs_highbit[256] = {
264 -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
265 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
266 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
267 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
268 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
269 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
270 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
271 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
272 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
273 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
274 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
275 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
276 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
277 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
278 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
279 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
280 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
281 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
282 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
283 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
284 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
285 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
286 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
287 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
288 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
289 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
290 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
291 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
292 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
293 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
294 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
295 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
299 xfs_highbit32(__uint32_t v)
308 else if (v & 0x0000ffff)
315 return i + xfs_highbit[(v >> i) & 0xff];
319 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
321 return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
325 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
327 return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
331 fsb2daddr (xfs_fsblock_t fsbno)
333 return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
334 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
338 btroot_maxrecs (void)
340 int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
342 return (tmp - sizeof(xfs_bmdr_block_t) -
343 (int)((char *)&inode->di_u - (char*)inode)) /
344 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
348 di_read (xfs_ino_t ino)
356 agno = ino2agno (ino);
357 agino = ino2agino (ino);
358 agbno = agino2agbno (agino);
359 offset = ino2offset (ino);
360 daddr = agb2daddr (agno, agbno);
362 read_disk_block(xfs_file, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
364 xfs.ptr0 = *(xfs_bmbt_ptr_t *)
365 (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
366 + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
375 xfs_btree_lblock_t h;
377 switch (icore.di_format) {
378 case XFS_DINODE_FMT_EXTENTS:
379 xfs.xt = inode->di_u.di_bmx;
380 xfs.nextents = le32 (icore.di_nextents);
382 case XFS_DINODE_FMT_BTREE:
385 xfs.daddr = fsb2daddr (le64(ptr0));
386 read_disk_block(xfs_file, xfs.daddr, 0,
387 sizeof(xfs_btree_lblock_t), (char *)&h);
389 xfs.nextents = le16(h.bb_numrecs);
390 xfs.next = fsb2daddr (le64(h.bb_leftsib));
391 xfs.fpos = sizeof(xfs_btree_block_t);
394 read_disk_block(xfs_file, xfs.daddr, xfs.btnode_ptr0_off,
395 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
405 switch (icore.di_format) {
406 case XFS_DINODE_FMT_EXTENTS:
407 if (xfs.nextents == 0)
410 case XFS_DINODE_FMT_BTREE:
411 if (xfs.nextents == 0) {
412 xfs_btree_lblock_t h;
415 xfs.daddr = xfs.next;
416 read_disk_block(xfs_file, xfs.daddr, 0,
417 sizeof(xfs_btree_lblock_t), (char *)&h);
418 xfs.nextents = le16(h.bb_numrecs);
419 xfs.next = fsb2daddr (le64(h.bb_leftsib));
420 xfs.fpos = sizeof(xfs_btree_block_t);
422 /* Yeah, I know that's slow, but I really don't care */
423 read_disk_block(xfs_file, xfs.daddr, xfs.fpos,
424 sizeof(xfs_bmbt_rec_t), filebuf);
425 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
426 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
431 xad.offset = xt_offset (xfs.xt);
432 xad.start = xt_start (xfs.xt);
433 xad.len = xt_len (xfs.xt);
441 * Name lies - the function reads only first 100 bytes
447 xfs_fileoff_t offset;;
450 while ((xad = next_extent ())) {
451 offset = xad->offset;
452 if (isinxt (xfs.dablk, offset, xad->len)) {
453 read_disk_block(xfs_file, fsb2daddr (xad->start + xfs.dablk - offset),
460 static inline xfs_ino_t
461 sf_ino (char *sfe, int namelen)
463 void *p = sfe + namelen + 3;
465 return (xfs.i8param == 0)
466 ? le64(*(xfs_ino_t *)p) : le32(*(__uint32_t *)p);
469 static inline xfs_ino_t
472 return (xfs.i8param == 0)
473 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
474 : le32(*(__uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
484 next_dentry (xfs_ino_t *ino)
488 static char *usual[2] = {".", ".."};
489 static xfs_dir2_sf_entry_t *sfe;
490 char *name = usual[0];
492 if (xfs.dirpos >= xfs.dirmax) {
495 xfs.dablk = xfs.forw;
497 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
498 xfs.dirmax = le16 (h->count) - le16 (h->stale);
499 xfs.forw = le32 (h->info.forw);
504 switch (icore.di_format) {
505 case XFS_DINODE_FMT_LOCAL:
506 switch (xfs.dirpos) {
511 *ino = sf_parent_ino ();
514 sfe = (xfs_dir2_sf_entry_t *)
516 + sizeof(xfs_dir2_sf_hdr_t)
520 namelen = sfe->namelen;
521 *ino = sf_ino ((char *)sfe, namelen);
523 sfe = (xfs_dir2_sf_entry_t *)
524 ((char *)sfe + namelen + 11 - xfs.i8param);
527 case XFS_DINODE_FMT_BTREE:
528 case XFS_DINODE_FMT_EXTENTS:
529 #define dau ((xfs_dir2_data_union_t *)dirbuf)
531 if (xfs.blkoff >= xfs.dirbsize) {
532 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
533 xfs_file->pos &= ~(xfs.dirbsize - 1);
534 xfs_file->pos |= xfs.blkoff;
536 xfs_read_data (dirbuf, 4);
538 if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
539 toread = roundup8 (le16(dau->unused.length)) - 4;
540 xfs.blkoff += toread;
541 xfs_file->pos += toread;
546 xfs_read_data ((char *)dirbuf + 4, 5);
547 *ino = le64 (dau->entry.inumber);
548 namelen = dau->entry.namelen;
550 toread = roundup8 (namelen + 11) - 9;
551 xfs_read_data (dirbuf, toread);
552 name = (char *)dirbuf;
553 xfs.blkoff += toread + 5;
563 first_dentry (xfs_ino_t *ino)
566 switch (icore.di_format) {
567 case XFS_DINODE_FMT_LOCAL:
568 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
569 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
572 case XFS_DINODE_FMT_EXTENTS:
573 case XFS_DINODE_FMT_BTREE:
575 xfs_file->len = le64 (icore.di_size);
576 xfs_read_data (dirbuf, sizeof(xfs_dir2_data_hdr_t));
577 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
578 #define tail ((xfs_dir2_block_tail_t *)dirbuf)
579 xfs_file->pos = xfs.dirbsize - sizeof(*tail);
580 xfs_read_data (dirbuf, sizeof(*tail));
581 xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
584 xfs.dablk = (1ULL << 35) >> xfs.blklog;
585 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
586 #define n ((xfs_da_intnode_t *)dirbuf)
589 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
590 || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
591 xfs.dirmax = le16 (h->count) - le16 (h->stale);
592 xfs.forw = le32 (h->info.forw);
595 xfs.dablk = le32 (n->btree[0].before);
600 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
601 xfs_file->pos = xfs.blkoff;
605 return next_dentry (ino);
613 if (read_disk_block(xfs_file, 0, 0, sizeof(super), &super) != sizeof(super)) {
614 DEBUG_F("read_disk_block failed!\n");
616 } else if (super.sb_magicnum != XFS_SB_MAGIC) {
617 DEBUG_F("xfs_mount: Bad magic: %x\n", super.sb_magicnum);
619 } else if ((super.sb_versionnum & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) {
620 DEBUG_F("xfs_mount: Bad version: %x\n", super.sb_versionnum);
624 xfs.bsize = le32 (super.sb_blocksize);
625 xfs.blklog = super.sb_blocklog;
626 xfs.bdlog = xfs.blklog - SECTOR_BITS;
627 xfs.rootino = le64 (super.sb_rootino);
628 xfs.isize = le16 (super.sb_inodesize);
629 xfs.agblocks = le32 (super.sb_agblocks);
630 xfs.dirblklog = super.sb_dirblklog;
631 xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
633 xfs.inopblog = super.sb_inopblog;
634 xfs.agblklog = super.sb_agblklog;
635 xfs.agnolog = xfs_highbit32 (le32 (super.sb_agcount) - 1) + 1;
637 xfs.btnode_ptr0_off =
638 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
639 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
640 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
646 xfs_read_data (char *buf, int len)
649 xfs_fileoff_t endofprev, endofcur, offset;
650 xfs_filblks_t xadlen;
651 int toread, startpos, endpos;
653 if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
654 memmove(buf, inode->di_u.di_c + xfs_file->pos, len);
655 xfs_file->pos += len;
659 startpos = xfs_file->pos;
660 endpos = xfs_file->pos + len;
661 if (endpos > xfs_file->len)
662 endpos = xfs_file->len;
663 endofprev = (xfs_fileoff_t)-1;
665 while (len > 0 && (xad = next_extent ())) {
666 offset = xad->offset;
668 if (isinxt (xfs_file->pos >> xfs.blklog, offset, xadlen)) {
669 endofcur = (offset + xadlen) << xfs.blklog;
670 toread = (endofcur >= endpos)
671 ? len : (endofcur - xfs_file->pos);
672 read_disk_block(xfs_file, fsb2daddr (xad->start),
673 xfs_file->pos - (offset << xfs.blklog), toread, buf);
676 xfs_file->pos += toread;
677 } else if (offset > endofprev) {
678 toread = ((offset << xfs.blklog) >= endpos)
679 ? len : ((offset - endofprev) << xfs.blklog);
681 xfs_file->pos += toread;
682 for (; toread; toread--) {
687 endofprev = offset + xadlen;
690 return xfs_file->pos - startpos;
694 xfs_dir (char *dirname)
696 xfs_ino_t ino, parent_ino, new_ino;
699 int cmp, n, link_count;
700 char linkbuf[xfs.bsize];
701 char *rest, *name, ch;
705 parent_ino = ino = xfs.rootino;
709 di_size = le64 (icore.di_size);
710 di_mode = le16 (icore.di_mode);
712 DEBUG_F("di_mode: %o\n", di_mode);
713 if ((di_mode & IFMT) == IFLNK) {
714 if (++link_count > MAX_LINK_COUNT) {
715 errnum = FILE_ERR_SYMLINK_LOOP;
716 DEBUG_LEAVE(FILE_ERR_SYMLINK_LOOP);
719 if (di_size < xfs.bsize - 1) {
721 xfs_file->len = di_size;
722 n = xfs_read_data (linkbuf, xfs_file->len);
724 errnum = FILE_ERR_LENGTH;
725 DEBUG_LEAVE(FILE_ERR_LENGTH);
729 ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
730 while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
736 DEBUG_F("*dirname: %s\n", dirname);
737 if (!*dirname || isspace (*dirname)) {
738 if ((di_mode & IFMT) != IFREG) {
739 errnum = FILE_ERR_BAD_TYPE;
740 DEBUG_LEAVE(FILE_ERR_BAD_TYPE);
744 xfs_file->len = di_size;
749 if ((di_mode & IFMT) != IFDIR) {
750 errnum = FILE_ERR_NOTDIR;
751 DEBUG_LEAVE(FILE_ERR_NOTDIR);
755 for (; *dirname == '/'; dirname++);
757 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
760 name = first_dentry (&new_ino);
762 cmp = (!*dirname) ? -1 : strcmp(dirname, name);
767 *(dirname = rest) = ch;
770 name = next_dentry (&new_ino);
772 errnum = FILE_ERR_NOTFOUND;
773 DEBUG_LEAVE(FILE_ERR_NOTFOUND);
783 * c-file-style: "k&r"