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.
36 int xfs_read_data (char *buf, int len);
37 int xfs_dir (char *dirname);
39 /* Exported in struct fs_t */
40 static int xfs_open(struct boot_file_t *file, const char *dev_name,
41 struct partition_t *part, const char *file_name);
42 static int xfs_read(struct boot_file_t *file, unsigned int size, void *buffer);
43 static int xfs_seek(struct boot_file_t *file, unsigned int newpos);
44 static int xfs_close(struct boot_file_t *file);
46 struct fs_t xfs_filesystem = {
54 struct boot_file_t *xfs_file;
55 static char FSYS_BUF[32768];
56 __u64 partition_offset;
60 xfs_open(struct boot_file_t *file, const char *dev_name,
61 struct partition_t *part, const char *file_name)
63 static char buffer[1024];
70 DEBUG_F("Determining offset for partition %d\n", part->part_number);
71 partition_offset = ((__u64)(part->part_start)) * ((__u64)part->blocksize);
72 DEBUG_F("%Lu = %lu * %hu\n", partition_offset,
79 sprintf(buffer, "%s:%d", dev_name, 0); /* 0 is full disk in OF */
80 DEBUG_F("Trying to open dev_name=%s; filename=%s; partition offset=%Lu\n",
81 buffer, file_name, partition_offset);
82 file->of_device = prom_open(buffer);
84 if (file->of_device == PROM_INVALID_HANDLE || file->of_device == NULL)
86 DEBUG_F("Can't open device %p\n", file->of_device);
87 DEBUG_LEAVE(FILE_ERR_BADDEV);
88 return FILE_ERR_BADDEV;
91 DEBUG_F("%p was successfully opened\n", file->of_device);
97 DEBUG_F("Couldn't open XFS @ %s/%Lu\n", buffer, partition_offset);
98 prom_close(file->of_device);
99 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
101 return FILE_ERR_BAD_FSYS;
104 DEBUG_F("Attempting to open %s\n", file_name);
105 strcpy(buffer, file_name); /* xfs_dir modifies argument */
108 DEBUG_F("xfs_dir() failed. errnum = %d\n", errnum);
109 prom_close( file->of_device );
110 DEBUG_LEAVE_F(errnum);
115 DEBUG_F("Successfully opened %s\n", file_name);
117 DEBUG_LEAVE(FILE_ERR_OK);
122 xfs_read(struct boot_file_t *file, unsigned int size, void *buffer)
124 return xfs_read_data(buffer, size);
128 xfs_seek(struct boot_file_t *file, unsigned int newpos)
135 xfs_close(struct boot_file_t *file)
139 prom_close(file->of_device);
141 DEBUG_F("xfs_close called\n");
147 read_disk_block(struct boot_file_t *file, __u32 block, __u32 start,
148 __u32 length, void *buf)
150 unsigned long long pos = block * 512;
151 pos += partition_offset + start;
152 DEBUG_F("Reading %u bytes, starting at block %u, disk offset %Lu\n",
154 if (!prom_lseek(file->of_device, pos)) {
155 DEBUG_F("prom_lseek failed\n");
158 return prom_read(file->of_device, buf, length);
161 #define MAX_LINK_COUNT 8
164 xfs_fileoff_t offset;
173 unsigned int agblocks;
180 unsigned int nextents;
185 xfs_bmbt_rec_32_t *xt;
196 static struct xfs_info xfs;
198 #define dirbuf ((char *)FSYS_BUF)
199 #define filebuf ((char *)FSYS_BUF + 4096)
200 #define inode ((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
201 #define icore (inode->di_core)
203 #define mask32lo(n) (((__uint32_t)1 << (n)) - 1)
205 #define XFS_INO_MASK(k) ((__uint32_t)((1ULL << (k)) - 1))
206 #define XFS_INO_OFFSET_BITS xfs.inopblog
207 #define XFS_INO_AGBNO_BITS xfs.agblklog
208 #define XFS_INO_AGINO_BITS (xfs.agblklog + xfs.inopblog)
209 #define XFS_INO_AGNO_BITS xfs.agnolog
211 static inline xfs_agblock_t
212 agino2agbno (xfs_agino_t agino)
214 return agino >> XFS_INO_OFFSET_BITS;
217 static inline xfs_agnumber_t
218 ino2agno (xfs_ino_t ino)
220 return ino >> XFS_INO_AGINO_BITS;
223 static inline xfs_agino_t
224 ino2agino (xfs_ino_t ino)
226 return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
230 ino2offset (xfs_ino_t ino)
232 return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
235 /* XFS is big endian, powerpc is big endian */
237 static inline __const__ __uint16_t
243 static inline __const__ __uint32_t
249 static inline __const__ __uint64_t
256 xt_start (xfs_bmbt_rec_32_t *r)
258 return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
259 (((xfs_fsblock_t)le32 (r->l2)) << 11) |
260 (((xfs_fsblock_t)le32 (r->l3)) >> 21);
264 xt_offset (xfs_bmbt_rec_32_t *r)
266 return (((xfs_fileoff_t)le32 (r->l0) &
267 mask32lo(31)) << 23) |
268 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
272 xt_len (xfs_bmbt_rec_32_t *r)
274 return le32(r->l3) & mask32lo(21);
277 static const char xfs_highbit[256] = {
278 -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
279 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
280 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
281 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
282 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
283 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
284 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
285 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
286 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
287 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
288 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
289 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
290 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
291 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
292 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
293 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
294 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
295 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
296 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
297 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
298 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
299 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
300 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
301 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
302 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
303 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
304 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
305 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
306 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
307 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
308 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
309 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
313 xfs_highbit32(__uint32_t v)
322 else if (v & 0x0000ffff)
329 return i + xfs_highbit[(v >> i) & 0xff];
333 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
335 return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
339 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
341 return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
345 fsb2daddr (xfs_fsblock_t fsbno)
347 return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
348 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
352 btroot_maxrecs (void)
354 int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
356 return (tmp - sizeof(xfs_bmdr_block_t) -
357 (int)((char *)&inode->di_u - (char*)inode)) /
358 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
362 di_read (xfs_ino_t ino)
370 agno = ino2agno (ino);
371 agino = ino2agino (ino);
372 agbno = agino2agbno (agino);
373 offset = ino2offset (ino);
374 daddr = agb2daddr (agno, agbno);
376 read_disk_block(xfs_file, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
378 xfs.ptr0 = *(xfs_bmbt_ptr_t *)
379 (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
380 + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
389 xfs_btree_lblock_t h;
391 switch (icore.di_format) {
392 case XFS_DINODE_FMT_EXTENTS:
393 xfs.xt = inode->di_u.di_bmx;
394 xfs.nextents = le32 (icore.di_nextents);
396 case XFS_DINODE_FMT_BTREE:
399 xfs.daddr = fsb2daddr (le64(ptr0));
400 read_disk_block(xfs_file, xfs.daddr, 0,
401 sizeof(xfs_btree_lblock_t), (char *)&h);
403 xfs.nextents = le16(h.bb_numrecs);
404 xfs.next = fsb2daddr (le64(h.bb_leftsib));
405 xfs.fpos = sizeof(xfs_btree_block_t);
408 read_disk_block(xfs_file, xfs.daddr, xfs.btnode_ptr0_off,
409 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
419 switch (icore.di_format) {
420 case XFS_DINODE_FMT_EXTENTS:
421 if (xfs.nextents == 0)
424 case XFS_DINODE_FMT_BTREE:
425 if (xfs.nextents == 0) {
426 xfs_btree_lblock_t h;
429 xfs.daddr = xfs.next;
430 read_disk_block(xfs_file, xfs.daddr, 0,
431 sizeof(xfs_btree_lblock_t), (char *)&h);
432 xfs.nextents = le16(h.bb_numrecs);
433 xfs.next = fsb2daddr (le64(h.bb_leftsib));
434 xfs.fpos = sizeof(xfs_btree_block_t);
436 /* Yeah, I know that's slow, but I really don't care */
437 read_disk_block(xfs_file, xfs.daddr, xfs.fpos,
438 sizeof(xfs_bmbt_rec_t), filebuf);
439 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
440 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
445 xad.offset = xt_offset (xfs.xt);
446 xad.start = xt_start (xfs.xt);
447 xad.len = xt_len (xfs.xt);
455 * Name lies - the function reads only first 100 bytes
461 xfs_fileoff_t offset;;
464 while ((xad = next_extent ())) {
465 offset = xad->offset;
466 if (isinxt (xfs.dablk, offset, xad->len)) {
467 read_disk_block(xfs_file, fsb2daddr (xad->start + xfs.dablk - offset),
474 static inline xfs_ino_t
475 sf_ino (char *sfe, int namelen)
477 void *p = sfe + namelen + 3;
479 return (xfs.i8param == 0)
480 ? le64(*(xfs_ino_t *)p) : le32(*(__uint32_t *)p);
483 static inline xfs_ino_t
486 return (xfs.i8param == 0)
487 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
488 : le32(*(__uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
498 next_dentry (xfs_ino_t *ino)
502 static char *usual[2] = {".", ".."};
503 static xfs_dir2_sf_entry_t *sfe;
504 char *name = usual[0];
506 if (xfs.dirpos >= xfs.dirmax) {
509 xfs.dablk = xfs.forw;
511 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
512 xfs.dirmax = le16 (h->count) - le16 (h->stale);
513 xfs.forw = le32 (h->info.forw);
518 switch (icore.di_format) {
519 case XFS_DINODE_FMT_LOCAL:
520 switch (xfs.dirpos) {
525 *ino = sf_parent_ino ();
528 sfe = (xfs_dir2_sf_entry_t *)
530 + sizeof(xfs_dir2_sf_hdr_t)
534 namelen = sfe->namelen;
535 *ino = sf_ino ((char *)sfe, namelen);
537 sfe = (xfs_dir2_sf_entry_t *)
538 ((char *)sfe + namelen + 11 - xfs.i8param);
541 case XFS_DINODE_FMT_BTREE:
542 case XFS_DINODE_FMT_EXTENTS:
543 #define dau ((xfs_dir2_data_union_t *)dirbuf)
545 if (xfs.blkoff >= xfs.dirbsize) {
546 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
547 xfs_file->pos &= ~(xfs.dirbsize - 1);
548 xfs_file->pos |= xfs.blkoff;
550 xfs_read_data (dirbuf, 4);
552 if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
553 toread = roundup8 (le16(dau->unused.length)) - 4;
554 xfs.blkoff += toread;
555 xfs_file->pos += toread;
560 xfs_read_data ((char *)dirbuf + 4, 5);
561 *ino = le64 (dau->entry.inumber);
562 namelen = dau->entry.namelen;
564 toread = roundup8 (namelen + 11) - 9;
565 xfs_read_data (dirbuf, toread);
566 name = (char *)dirbuf;
567 xfs.blkoff += toread + 5;
577 first_dentry (xfs_ino_t *ino)
580 switch (icore.di_format) {
581 case XFS_DINODE_FMT_LOCAL:
582 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
583 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
586 case XFS_DINODE_FMT_EXTENTS:
587 case XFS_DINODE_FMT_BTREE:
589 xfs_file->len = le64 (icore.di_size);
590 xfs_read_data (dirbuf, sizeof(xfs_dir2_data_hdr_t));
591 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
592 #define tail ((xfs_dir2_block_tail_t *)dirbuf)
593 xfs_file->pos = xfs.dirbsize - sizeof(*tail);
594 xfs_read_data (dirbuf, sizeof(*tail));
595 xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
598 xfs.dablk = (1ULL << 35) >> xfs.blklog;
599 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
600 #define n ((xfs_da_intnode_t *)dirbuf)
603 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
604 || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
605 xfs.dirmax = le16 (h->count) - le16 (h->stale);
606 xfs.forw = le32 (h->info.forw);
609 xfs.dablk = le32 (n->btree[0].before);
614 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
615 xfs_file->pos = xfs.blkoff;
619 return next_dentry (ino);
627 if (read_disk_block(xfs_file, 0, 0, sizeof(super), &super) != sizeof(super)) {
628 DEBUG_F("read_disk_block failed!\n");
630 } else if (super.sb_magicnum != XFS_SB_MAGIC) {
631 DEBUG_F("xfs_mount: Bad magic: %x\n", super.sb_magicnum);
633 } else if ((super.sb_versionnum & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) {
634 DEBUG_F("xfs_mount: Bad version: %x\n", super.sb_versionnum);
638 xfs.bsize = le32 (super.sb_blocksize);
639 xfs.blklog = super.sb_blocklog;
640 xfs.bdlog = xfs.blklog - SECTOR_BITS;
641 xfs.rootino = le64 (super.sb_rootino);
642 xfs.isize = le16 (super.sb_inodesize);
643 xfs.agblocks = le32 (super.sb_agblocks);
644 xfs.dirblklog = super.sb_dirblklog;
645 xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
647 xfs.inopblog = super.sb_inopblog;
648 xfs.agblklog = super.sb_agblklog;
649 xfs.agnolog = xfs_highbit32 (le32 (super.sb_agcount) - 1) + 1;
651 xfs.btnode_ptr0_off =
652 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
653 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
654 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
660 xfs_read_data (char *buf, int len)
663 xfs_fileoff_t endofprev, endofcur, offset;
664 xfs_filblks_t xadlen;
665 int toread, startpos, endpos;
667 if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
668 memmove(buf, inode->di_u.di_c + xfs_file->pos, len);
669 xfs_file->pos += len;
673 startpos = xfs_file->pos;
674 endpos = xfs_file->pos + len;
675 if (endpos > xfs_file->len)
676 endpos = xfs_file->len;
677 endofprev = (xfs_fileoff_t)-1;
679 while (len > 0 && (xad = next_extent ())) {
680 offset = xad->offset;
682 if (isinxt (xfs_file->pos >> xfs.blklog, offset, xadlen)) {
683 endofcur = (offset + xadlen) << xfs.blklog;
684 toread = (endofcur >= endpos)
685 ? len : (endofcur - xfs_file->pos);
686 read_disk_block(xfs_file, fsb2daddr (xad->start),
687 xfs_file->pos - (offset << xfs.blklog), toread, buf);
690 xfs_file->pos += toread;
691 } else if (offset > endofprev) {
692 toread = ((offset << xfs.blklog) >= endpos)
693 ? len : ((offset - endofprev) << xfs.blklog);
695 xfs_file->pos += toread;
696 for (; toread; toread--) {
701 endofprev = offset + xadlen;
704 return xfs_file->pos - startpos;
708 xfs_dir (char *dirname)
710 xfs_ino_t ino, parent_ino, new_ino;
713 int cmp, n, link_count;
714 char linkbuf[xfs.bsize];
715 char *rest, *name, ch;
719 parent_ino = ino = xfs.rootino;
723 di_size = le64 (icore.di_size);
724 di_mode = le16 (icore.di_mode);
726 DEBUG_F("di_mode: %o\n", di_mode);
727 if ((di_mode & IFMT) == IFLNK) {
728 if (++link_count > MAX_LINK_COUNT) {
729 errnum = FILE_ERR_SYMLINK_LOOP;
730 DEBUG_LEAVE(FILE_ERR_SYMLINK_LOOP);
733 if (di_size < xfs.bsize - 1) {
735 xfs_file->len = di_size;
736 n = xfs_read_data (linkbuf, xfs_file->len);
738 errnum = FILE_ERR_LENGTH;
739 DEBUG_LEAVE(FILE_ERR_LENGTH);
743 ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
744 while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
750 DEBUG_F("*dirname: %s\n", dirname);
751 if (!*dirname || isspace (*dirname)) {
752 if ((di_mode & IFMT) != IFREG) {
753 errnum = FILE_ERR_BAD_TYPE;
754 DEBUG_LEAVE(FILE_ERR_BAD_TYPE);
758 xfs_file->len = di_size;
763 if ((di_mode & IFMT) != IFDIR) {
764 errnum = FILE_ERR_NOTDIR;
765 DEBUG_LEAVE(FILE_ERR_NOTDIR);
769 for (; *dirname == '/'; dirname++);
771 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
774 name = first_dentry (&new_ino);
776 cmp = (!*dirname) ? -1 : strcmp(dirname, name);
781 *(dirname = rest) = ch;
784 name = next_dentry (&new_ino);
786 errnum = FILE_ERR_NOTFOUND;
787 DEBUG_LEAVE(FILE_ERR_NOTFOUND);
797 * c-file-style: "K&R"