2 * fs_xfs.c - an implementation for the SGI XFS file system
4 * Copyright (C) 2001, 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 int xfs_read_data (char *buf, int len);
39 int xfs_dir (char *dirname);
41 /* Exported in struct fs_t */
42 static int xfs_open(struct boot_file_t *file,
43 struct partition_t *part, struct boot_fspec_t *fspec);
44 static int xfs_read(struct boot_file_t *file, unsigned int size, void *buffer);
45 static int xfs_seek(struct boot_file_t *file, unsigned int newpos);
46 static int xfs_close(struct boot_file_t *file);
48 struct fs_t xfs_filesystem = {
56 struct boot_file_t *xfs_file;
57 static char FSYS_BUF[32768];
58 uint64_t partition_offset;
62 xfs_open(struct boot_file_t *file,
63 struct partition_t *part, struct boot_fspec_t *fspec)
65 static char buffer[1024];
72 DEBUG_F("Determining offset for partition %d\n", part->part_number);
73 partition_offset = ((uint64_t) part->part_start) * part->blocksize;
74 DEBUG_F("%Lu = %lu * %hu\n", partition_offset,
81 strncpy(buffer, fspec->dev, 1020);
82 if (_machine != _MACH_bplan)
83 strcat(buffer, ":0"); /* 0 is full disk in (non-buggy) OF */
84 DEBUG_F("Trying to open dev_name=%s; filename=%s; partition offset=%Lu\n",
85 buffer, fspec->file, partition_offset);
86 file->of_device = prom_open(buffer);
88 if (file->of_device == PROM_INVALID_HANDLE || file->of_device == NULL)
90 DEBUG_F("Can't open device %p\n", file->of_device);
91 DEBUG_LEAVE(FILE_ERR_BADDEV);
92 return FILE_ERR_BADDEV;
95 DEBUG_F("%p was successfully opened\n", file->of_device);
101 DEBUG_F("Couldn't open XFS @ %s/%Lu\n", buffer, partition_offset);
102 prom_close(file->of_device);
103 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
105 return FILE_ERR_BAD_FSYS;
108 DEBUG_F("Attempting to open %s\n", fspec->file);
109 strcpy(buffer, fspec->file); /* xfs_dir modifies argument */
112 DEBUG_F("xfs_dir() failed. errnum = %d\n", errnum);
113 prom_close( file->of_device );
114 DEBUG_LEAVE_F(errnum);
119 DEBUG_F("Successfully opened %s\n", fspec->file);
121 DEBUG_LEAVE(FILE_ERR_OK);
126 xfs_read(struct boot_file_t *file, unsigned int size, void *buffer)
128 return xfs_read_data(buffer, size);
132 xfs_seek(struct boot_file_t *file, unsigned int newpos)
139 xfs_close(struct boot_file_t *file)
143 prom_close(file->of_device);
145 DEBUG_F("xfs_close called\n");
151 read_disk_block(struct boot_file_t *file, uint64_t block, int start,
152 int length, void *buf)
154 uint64_t pos = block * 512;
155 pos += partition_offset + start;
156 DEBUG_F("Reading %d bytes, starting at block %Lu, disk offset %Lu\n",
158 if (!prom_lseek(file->of_device, pos)) {
159 DEBUG_F("prom_lseek failed\n");
162 return prom_read(file->of_device, buf, length);
165 #define MAX_LINK_COUNT 8
168 xfs_fileoff_t offset;
177 unsigned int agblocks;
184 unsigned int nextents;
189 xfs_bmbt_rec_32_t *xt;
200 static struct xfs_info xfs;
202 #define dirbuf ((char *)FSYS_BUF)
203 #define filebuf ((char *)FSYS_BUF + 4096)
204 #define inode ((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
205 #define icore (inode->di_core)
207 #define mask32lo(n) (((__uint32_t)1 << (n)) - 1)
209 #define XFS_INO_MASK(k) ((__uint32_t)((1ULL << (k)) - 1))
210 #define XFS_INO_OFFSET_BITS xfs.inopblog
211 #define XFS_INO_AGBNO_BITS xfs.agblklog
212 #define XFS_INO_AGINO_BITS (xfs.agblklog + xfs.inopblog)
213 #define XFS_INO_AGNO_BITS xfs.agnolog
215 static inline xfs_agblock_t
216 agino2agbno (xfs_agino_t agino)
218 return agino >> XFS_INO_OFFSET_BITS;
221 static inline xfs_agnumber_t
222 ino2agno (xfs_ino_t ino)
224 return ino >> XFS_INO_AGINO_BITS;
227 static inline xfs_agino_t
228 ino2agino (xfs_ino_t ino)
230 return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
234 ino2offset (xfs_ino_t ino)
236 return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
239 /* XFS is big endian, powerpc is big endian */
245 xt_start (xfs_bmbt_rec_32_t *r)
247 return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
248 (((xfs_fsblock_t)le32 (r->l2)) << 11) |
249 (((xfs_fsblock_t)le32 (r->l3)) >> 21);
253 xt_offset (xfs_bmbt_rec_32_t *r)
255 return (((xfs_fileoff_t)le32 (r->l0) &
256 mask32lo(31)) << 23) |
257 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
261 xt_len (xfs_bmbt_rec_32_t *r)
263 return le32(r->l3) & mask32lo(21);
266 static const char xfs_highbit[256] = {
267 -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
268 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
269 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
270 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
271 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
272 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
273 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
274 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
275 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
276 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
277 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
278 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
279 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
280 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
281 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
282 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
283 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
284 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
285 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
286 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
287 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
288 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
289 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
290 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
291 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
292 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
293 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
294 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
295 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
296 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
297 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
298 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
302 xfs_highbit32(__uint32_t v)
311 else if (v & 0x0000ffff)
318 return i + xfs_highbit[(v >> i) & 0xff];
322 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
324 return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
328 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
330 return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
334 fsb2daddr (xfs_fsblock_t fsbno)
336 return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
337 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
341 btroot_maxrecs (void)
343 int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
345 return (tmp - sizeof(xfs_bmdr_block_t) -
346 (int)((char *)&inode->di_u - (char*)inode)) /
347 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
351 di_read (xfs_ino_t ino)
359 agno = ino2agno (ino);
360 agino = ino2agino (ino);
361 agbno = agino2agbno (agino);
362 offset = ino2offset (ino);
363 daddr = agb2daddr (agno, agbno);
365 read_disk_block(xfs_file, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
367 xfs.ptr0 = *(xfs_bmbt_ptr_t *)
368 (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
369 + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
378 xfs_btree_lblock_t h;
380 switch (icore.di_format) {
381 case XFS_DINODE_FMT_EXTENTS:
382 xfs.xt = inode->di_u.di_bmx;
383 xfs.nextents = le32 (icore.di_nextents);
385 case XFS_DINODE_FMT_BTREE:
388 xfs.daddr = fsb2daddr (le64(ptr0));
389 read_disk_block(xfs_file, xfs.daddr, 0,
390 sizeof(xfs_btree_lblock_t), (char *)&h);
392 xfs.nextents = le16(h.bb_numrecs);
393 xfs.next = fsb2daddr (le64(h.bb_leftsib));
394 xfs.fpos = sizeof(xfs_btree_block_t);
397 read_disk_block(xfs_file, xfs.daddr, xfs.btnode_ptr0_off,
398 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
408 switch (icore.di_format) {
409 case XFS_DINODE_FMT_EXTENTS:
410 if (xfs.nextents == 0)
413 case XFS_DINODE_FMT_BTREE:
414 if (xfs.nextents == 0) {
415 xfs_btree_lblock_t h;
418 xfs.daddr = xfs.next;
419 read_disk_block(xfs_file, xfs.daddr, 0,
420 sizeof(xfs_btree_lblock_t), (char *)&h);
421 xfs.nextents = le16(h.bb_numrecs);
422 xfs.next = fsb2daddr (le64(h.bb_leftsib));
423 xfs.fpos = sizeof(xfs_btree_block_t);
425 /* Yeah, I know that's slow, but I really don't care */
426 read_disk_block(xfs_file, xfs.daddr, xfs.fpos,
427 sizeof(xfs_bmbt_rec_t), filebuf);
428 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
429 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
434 xad.offset = xt_offset (xfs.xt);
435 xad.start = xt_start (xfs.xt);
436 xad.len = xt_len (xfs.xt);
444 * Name lies - the function reads only first 100 bytes
450 xfs_fileoff_t offset;;
453 while ((xad = next_extent ())) {
454 offset = xad->offset;
455 if (isinxt (xfs.dablk, offset, xad->len)) {
456 read_disk_block(xfs_file, fsb2daddr (xad->start + xfs.dablk - offset),
463 static inline xfs_ino_t
464 sf_ino (char *sfe, int namelen)
466 void *p = sfe + namelen + 3;
468 return (xfs.i8param == 0)
469 ? le64(*(xfs_ino_t *)p) : le32(*(__uint32_t *)p);
472 static inline xfs_ino_t
475 return (xfs.i8param == 0)
476 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
477 : le32(*(__uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
487 next_dentry (xfs_ino_t *ino)
491 static char *usual[2] = {".", ".."};
492 static xfs_dir2_sf_entry_t *sfe;
493 char *name = usual[0];
495 if (xfs.dirpos >= xfs.dirmax) {
498 xfs.dablk = xfs.forw;
500 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
501 xfs.dirmax = le16 (h->count) - le16 (h->stale);
502 xfs.forw = le32 (h->info.forw);
507 switch (icore.di_format) {
508 case XFS_DINODE_FMT_LOCAL:
509 switch (xfs.dirpos) {
514 *ino = sf_parent_ino ();
517 sfe = (xfs_dir2_sf_entry_t *)
519 + sizeof(xfs_dir2_sf_hdr_t)
523 namelen = sfe->namelen;
524 *ino = sf_ino ((char *)sfe, namelen);
526 sfe = (xfs_dir2_sf_entry_t *)
527 ((char *)sfe + namelen + 11 - xfs.i8param);
530 case XFS_DINODE_FMT_BTREE:
531 case XFS_DINODE_FMT_EXTENTS:
532 #define dau ((xfs_dir2_data_union_t *)dirbuf)
534 if (xfs.blkoff >= xfs.dirbsize) {
535 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
536 xfs_file->pos &= ~(xfs.dirbsize - 1);
537 xfs_file->pos |= xfs.blkoff;
539 xfs_read_data (dirbuf, 4);
541 if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
542 toread = roundup8 (le16(dau->unused.length)) - 4;
543 xfs.blkoff += toread;
544 xfs_file->pos += toread;
549 xfs_read_data ((char *)dirbuf + 4, 5);
550 *ino = le64 (dau->entry.inumber);
551 namelen = dau->entry.namelen;
553 toread = roundup8 (namelen + 11) - 9;
554 xfs_read_data (dirbuf, toread);
555 name = (char *)dirbuf;
556 xfs.blkoff += toread + 5;
566 first_dentry (xfs_ino_t *ino)
569 switch (icore.di_format) {
570 case XFS_DINODE_FMT_LOCAL:
571 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
572 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
575 case XFS_DINODE_FMT_EXTENTS:
576 case XFS_DINODE_FMT_BTREE:
578 xfs_file->len = le64 (icore.di_size);
579 xfs_read_data (dirbuf, sizeof(xfs_dir2_data_hdr_t));
580 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
581 #define tail ((xfs_dir2_block_tail_t *)dirbuf)
582 xfs_file->pos = xfs.dirbsize - sizeof(*tail);
583 xfs_read_data (dirbuf, sizeof(*tail));
584 xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
587 xfs.dablk = (1ULL << 35) >> xfs.blklog;
588 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
589 #define n ((xfs_da_intnode_t *)dirbuf)
592 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
593 || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
594 xfs.dirmax = le16 (h->count) - le16 (h->stale);
595 xfs.forw = le32 (h->info.forw);
598 xfs.dablk = le32 (n->btree[0].before);
603 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
604 xfs_file->pos = xfs.blkoff;
608 return next_dentry (ino);
616 if (read_disk_block(xfs_file, 0, 0, sizeof(super), &super) != sizeof(super)) {
617 DEBUG_F("read_disk_block failed!\n");
619 } else if (super.sb_magicnum != XFS_SB_MAGIC) {
620 DEBUG_F("xfs_mount: Bad magic: %x\n", super.sb_magicnum);
622 } else if ((super.sb_versionnum & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) {
623 DEBUG_F("xfs_mount: Bad version: %x\n", super.sb_versionnum);
627 xfs.bsize = le32 (super.sb_blocksize);
628 xfs.blklog = super.sb_blocklog;
629 xfs.bdlog = xfs.blklog - SECTOR_BITS;
630 xfs.rootino = le64 (super.sb_rootino);
631 xfs.isize = le16 (super.sb_inodesize);
632 xfs.agblocks = le32 (super.sb_agblocks);
633 xfs.dirblklog = super.sb_dirblklog;
634 xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
636 xfs.inopblog = super.sb_inopblog;
637 xfs.agblklog = super.sb_agblklog;
638 xfs.agnolog = xfs_highbit32 (le32 (super.sb_agcount) - 1) + 1;
640 xfs.btnode_ptr0_off =
641 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
642 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
643 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
649 xfs_read_data (char *buf, int len)
652 xfs_fileoff_t endofprev, endofcur, offset;
653 xfs_filblks_t xadlen;
654 int toread, startpos, endpos;
656 if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
657 memmove(buf, inode->di_u.di_c + xfs_file->pos, len);
658 xfs_file->pos += len;
662 startpos = xfs_file->pos;
663 endpos = xfs_file->pos + len;
664 if (endpos > xfs_file->len)
665 endpos = xfs_file->len;
666 endofprev = (xfs_fileoff_t)-1;
668 while (len > 0 && (xad = next_extent ())) {
669 offset = xad->offset;
671 if (isinxt (xfs_file->pos >> xfs.blklog, offset, xadlen)) {
672 endofcur = (offset + xadlen) << xfs.blklog;
673 toread = (endofcur >= endpos)
674 ? len : (endofcur - xfs_file->pos);
675 read_disk_block(xfs_file, fsb2daddr (xad->start),
676 xfs_file->pos - (offset << xfs.blklog), toread, buf);
679 xfs_file->pos += toread;
680 } else if (offset > endofprev) {
681 toread = ((offset << xfs.blklog) >= endpos)
682 ? len : ((offset - endofprev) << xfs.blklog);
684 xfs_file->pos += toread;
685 for (; toread; toread--) {
690 endofprev = offset + xadlen;
693 return xfs_file->pos - startpos;
697 xfs_dir (char *dirname)
699 xfs_ino_t ino, parent_ino, new_ino;
702 int cmp, n, link_count;
703 char linkbuf[xfs.bsize];
704 char *rest, *name, ch;
708 parent_ino = ino = xfs.rootino;
712 di_size = le64 (icore.di_size);
713 di_mode = le16 (icore.di_mode);
715 DEBUG_F("di_mode: %o\n", di_mode);
716 if ((di_mode & IFMT) == IFLNK) {
717 if (++link_count > MAX_LINK_COUNT) {
718 errnum = FILE_ERR_SYMLINK_LOOP;
719 DEBUG_LEAVE(FILE_ERR_SYMLINK_LOOP);
722 if (di_size < xfs.bsize - 1) {
724 xfs_file->len = di_size;
725 n = xfs_read_data (linkbuf, xfs_file->len);
727 errnum = FILE_ERR_LENGTH;
728 DEBUG_LEAVE(FILE_ERR_LENGTH);
732 ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
733 while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
739 DEBUG_F("*dirname: %s\n", dirname);
740 if (!*dirname || isspace (*dirname)) {
741 if ((di_mode & IFMT) != IFREG) {
742 errnum = FILE_ERR_BAD_TYPE;
743 DEBUG_LEAVE(FILE_ERR_BAD_TYPE);
747 xfs_file->len = di_size;
752 if ((di_mode & IFMT) != IFDIR) {
753 errnum = FILE_ERR_NOTDIR;
754 DEBUG_LEAVE(FILE_ERR_NOTDIR);
758 for (; *dirname == '/'; dirname++);
760 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
763 name = first_dentry (&new_ino);
765 cmp = (!*dirname) ? -1 : strcmp(dirname, name);
770 *(dirname = rest) = ch;
773 name = next_dentry (&new_ino);
775 errnum = FILE_ERR_NOTFOUND;
776 DEBUG_LEAVE(FILE_ERR_NOTFOUND);
786 * c-file-style: "k&r"