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 __u64 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 = ((__u64)(part->part_start)) * ((__u64)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, __u32 block, __u32 start,
149 __u32 length, void *buf)
151 unsigned long long pos = block * 512;
152 pos += partition_offset + start;
153 DEBUG_F("Reading %u bytes, starting at block %u, 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 */
238 static inline __const__ __uint16_t
244 static inline __const__ __uint32_t
250 static inline __const__ __uint64_t
257 xt_start (xfs_bmbt_rec_32_t *r)
259 return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
260 (((xfs_fsblock_t)le32 (r->l2)) << 11) |
261 (((xfs_fsblock_t)le32 (r->l3)) >> 21);
265 xt_offset (xfs_bmbt_rec_32_t *r)
267 return (((xfs_fileoff_t)le32 (r->l0) &
268 mask32lo(31)) << 23) |
269 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
273 xt_len (xfs_bmbt_rec_32_t *r)
275 return le32(r->l3) & mask32lo(21);
278 static const char xfs_highbit[256] = {
279 -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
280 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
281 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
282 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
283 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
284 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
285 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
286 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
287 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
288 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
289 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
290 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
291 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
292 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
293 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
294 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
295 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
296 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
297 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
298 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
299 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
300 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
301 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
302 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
303 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
304 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
305 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
306 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
307 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
308 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
309 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
310 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
314 xfs_highbit32(__uint32_t v)
323 else if (v & 0x0000ffff)
330 return i + xfs_highbit[(v >> i) & 0xff];
334 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
336 return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
340 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
342 return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
346 fsb2daddr (xfs_fsblock_t fsbno)
348 return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
349 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
353 btroot_maxrecs (void)
355 int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
357 return (tmp - sizeof(xfs_bmdr_block_t) -
358 (int)((char *)&inode->di_u - (char*)inode)) /
359 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
363 di_read (xfs_ino_t ino)
371 agno = ino2agno (ino);
372 agino = ino2agino (ino);
373 agbno = agino2agbno (agino);
374 offset = ino2offset (ino);
375 daddr = agb2daddr (agno, agbno);
377 read_disk_block(xfs_file, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
379 xfs.ptr0 = *(xfs_bmbt_ptr_t *)
380 (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
381 + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
390 xfs_btree_lblock_t h;
392 switch (icore.di_format) {
393 case XFS_DINODE_FMT_EXTENTS:
394 xfs.xt = inode->di_u.di_bmx;
395 xfs.nextents = le32 (icore.di_nextents);
397 case XFS_DINODE_FMT_BTREE:
400 xfs.daddr = fsb2daddr (le64(ptr0));
401 read_disk_block(xfs_file, xfs.daddr, 0,
402 sizeof(xfs_btree_lblock_t), (char *)&h);
404 xfs.nextents = le16(h.bb_numrecs);
405 xfs.next = fsb2daddr (le64(h.bb_leftsib));
406 xfs.fpos = sizeof(xfs_btree_block_t);
409 read_disk_block(xfs_file, xfs.daddr, xfs.btnode_ptr0_off,
410 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
420 switch (icore.di_format) {
421 case XFS_DINODE_FMT_EXTENTS:
422 if (xfs.nextents == 0)
425 case XFS_DINODE_FMT_BTREE:
426 if (xfs.nextents == 0) {
427 xfs_btree_lblock_t h;
430 xfs.daddr = xfs.next;
431 read_disk_block(xfs_file, xfs.daddr, 0,
432 sizeof(xfs_btree_lblock_t), (char *)&h);
433 xfs.nextents = le16(h.bb_numrecs);
434 xfs.next = fsb2daddr (le64(h.bb_leftsib));
435 xfs.fpos = sizeof(xfs_btree_block_t);
437 /* Yeah, I know that's slow, but I really don't care */
438 read_disk_block(xfs_file, xfs.daddr, xfs.fpos,
439 sizeof(xfs_bmbt_rec_t), filebuf);
440 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
441 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
446 xad.offset = xt_offset (xfs.xt);
447 xad.start = xt_start (xfs.xt);
448 xad.len = xt_len (xfs.xt);
456 * Name lies - the function reads only first 100 bytes
462 xfs_fileoff_t offset;;
465 while ((xad = next_extent ())) {
466 offset = xad->offset;
467 if (isinxt (xfs.dablk, offset, xad->len)) {
468 read_disk_block(xfs_file, fsb2daddr (xad->start + xfs.dablk - offset),
475 static inline xfs_ino_t
476 sf_ino (char *sfe, int namelen)
478 void *p = sfe + namelen + 3;
480 return (xfs.i8param == 0)
481 ? le64(*(xfs_ino_t *)p) : le32(*(__uint32_t *)p);
484 static inline xfs_ino_t
487 return (xfs.i8param == 0)
488 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
489 : le32(*(__uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
499 next_dentry (xfs_ino_t *ino)
503 static char *usual[2] = {".", ".."};
504 static xfs_dir2_sf_entry_t *sfe;
505 char *name = usual[0];
507 if (xfs.dirpos >= xfs.dirmax) {
510 xfs.dablk = xfs.forw;
512 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
513 xfs.dirmax = le16 (h->count) - le16 (h->stale);
514 xfs.forw = le32 (h->info.forw);
519 switch (icore.di_format) {
520 case XFS_DINODE_FMT_LOCAL:
521 switch (xfs.dirpos) {
526 *ino = sf_parent_ino ();
529 sfe = (xfs_dir2_sf_entry_t *)
531 + sizeof(xfs_dir2_sf_hdr_t)
535 namelen = sfe->namelen;
536 *ino = sf_ino ((char *)sfe, namelen);
538 sfe = (xfs_dir2_sf_entry_t *)
539 ((char *)sfe + namelen + 11 - xfs.i8param);
542 case XFS_DINODE_FMT_BTREE:
543 case XFS_DINODE_FMT_EXTENTS:
544 #define dau ((xfs_dir2_data_union_t *)dirbuf)
546 if (xfs.blkoff >= xfs.dirbsize) {
547 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
548 xfs_file->pos &= ~(xfs.dirbsize - 1);
549 xfs_file->pos |= xfs.blkoff;
551 xfs_read_data (dirbuf, 4);
553 if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
554 toread = roundup8 (le16(dau->unused.length)) - 4;
555 xfs.blkoff += toread;
556 xfs_file->pos += toread;
561 xfs_read_data ((char *)dirbuf + 4, 5);
562 *ino = le64 (dau->entry.inumber);
563 namelen = dau->entry.namelen;
565 toread = roundup8 (namelen + 11) - 9;
566 xfs_read_data (dirbuf, toread);
567 name = (char *)dirbuf;
568 xfs.blkoff += toread + 5;
578 first_dentry (xfs_ino_t *ino)
581 switch (icore.di_format) {
582 case XFS_DINODE_FMT_LOCAL:
583 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
584 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
587 case XFS_DINODE_FMT_EXTENTS:
588 case XFS_DINODE_FMT_BTREE:
590 xfs_file->len = le64 (icore.di_size);
591 xfs_read_data (dirbuf, sizeof(xfs_dir2_data_hdr_t));
592 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
593 #define tail ((xfs_dir2_block_tail_t *)dirbuf)
594 xfs_file->pos = xfs.dirbsize - sizeof(*tail);
595 xfs_read_data (dirbuf, sizeof(*tail));
596 xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
599 xfs.dablk = (1ULL << 35) >> xfs.blklog;
600 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
601 #define n ((xfs_da_intnode_t *)dirbuf)
604 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
605 || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
606 xfs.dirmax = le16 (h->count) - le16 (h->stale);
607 xfs.forw = le32 (h->info.forw);
610 xfs.dablk = le32 (n->btree[0].before);
615 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
616 xfs_file->pos = xfs.blkoff;
620 return next_dentry (ino);
628 if (read_disk_block(xfs_file, 0, 0, sizeof(super), &super) != sizeof(super)) {
629 DEBUG_F("read_disk_block failed!\n");
631 } else if (super.sb_magicnum != XFS_SB_MAGIC) {
632 DEBUG_F("xfs_mount: Bad magic: %x\n", super.sb_magicnum);
634 } else if ((super.sb_versionnum & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) {
635 DEBUG_F("xfs_mount: Bad version: %x\n", super.sb_versionnum);
639 xfs.bsize = le32 (super.sb_blocksize);
640 xfs.blklog = super.sb_blocklog;
641 xfs.bdlog = xfs.blklog - SECTOR_BITS;
642 xfs.rootino = le64 (super.sb_rootino);
643 xfs.isize = le16 (super.sb_inodesize);
644 xfs.agblocks = le32 (super.sb_agblocks);
645 xfs.dirblklog = super.sb_dirblklog;
646 xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
648 xfs.inopblog = super.sb_inopblog;
649 xfs.agblklog = super.sb_agblklog;
650 xfs.agnolog = xfs_highbit32 (le32 (super.sb_agcount) - 1) + 1;
652 xfs.btnode_ptr0_off =
653 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
654 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
655 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
661 xfs_read_data (char *buf, int len)
664 xfs_fileoff_t endofprev, endofcur, offset;
665 xfs_filblks_t xadlen;
666 int toread, startpos, endpos;
668 if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
669 memmove(buf, inode->di_u.di_c + xfs_file->pos, len);
670 xfs_file->pos += len;
674 startpos = xfs_file->pos;
675 endpos = xfs_file->pos + len;
676 if (endpos > xfs_file->len)
677 endpos = xfs_file->len;
678 endofprev = (xfs_fileoff_t)-1;
680 while (len > 0 && (xad = next_extent ())) {
681 offset = xad->offset;
683 if (isinxt (xfs_file->pos >> xfs.blklog, offset, xadlen)) {
684 endofcur = (offset + xadlen) << xfs.blklog;
685 toread = (endofcur >= endpos)
686 ? len : (endofcur - xfs_file->pos);
687 read_disk_block(xfs_file, fsb2daddr (xad->start),
688 xfs_file->pos - (offset << xfs.blklog), toread, buf);
691 xfs_file->pos += toread;
692 } else if (offset > endofprev) {
693 toread = ((offset << xfs.blklog) >= endpos)
694 ? len : ((offset - endofprev) << xfs.blklog);
696 xfs_file->pos += toread;
697 for (; toread; toread--) {
702 endofprev = offset + xadlen;
705 return xfs_file->pos - startpos;
709 xfs_dir (char *dirname)
711 xfs_ino_t ino, parent_ino, new_ino;
714 int cmp, n, link_count;
715 char linkbuf[xfs.bsize];
716 char *rest, *name, ch;
720 parent_ino = ino = xfs.rootino;
724 di_size = le64 (icore.di_size);
725 di_mode = le16 (icore.di_mode);
727 DEBUG_F("di_mode: %o\n", di_mode);
728 if ((di_mode & IFMT) == IFLNK) {
729 if (++link_count > MAX_LINK_COUNT) {
730 errnum = FILE_ERR_SYMLINK_LOOP;
731 DEBUG_LEAVE(FILE_ERR_SYMLINK_LOOP);
734 if (di_size < xfs.bsize - 1) {
736 xfs_file->len = di_size;
737 n = xfs_read_data (linkbuf, xfs_file->len);
739 errnum = FILE_ERR_LENGTH;
740 DEBUG_LEAVE(FILE_ERR_LENGTH);
744 ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
745 while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
751 DEBUG_F("*dirname: %s\n", dirname);
752 if (!*dirname || isspace (*dirname)) {
753 if ((di_mode & IFMT) != IFREG) {
754 errnum = FILE_ERR_BAD_TYPE;
755 DEBUG_LEAVE(FILE_ERR_BAD_TYPE);
759 xfs_file->len = di_size;
764 if ((di_mode & IFMT) != IFDIR) {
765 errnum = FILE_ERR_NOTDIR;
766 DEBUG_LEAVE(FILE_ERR_NOTDIR);
770 for (; *dirname == '/'; dirname++);
772 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
775 name = first_dentry (&new_ino);
777 cmp = (!*dirname) ? -1 : strcmp(dirname, name);
782 *(dirname = rest) = ch;
785 name = next_dentry (&new_ino);
787 errnum = FILE_ERR_NOTFOUND;
788 DEBUG_LEAVE(FILE_ERR_NOTFOUND);
798 * c-file-style: "K&R"