1 /* fsys_xfs.c - an implementation for the SGI XFS file system */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2001,2002 Free Software Foundation, Inc.
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28 #define MAX_LINK_COUNT 8
40 unsigned int agblocks;
46 unsigned int nextents;
51 xfs_bmbt_rec_32_t *xt;
62 static struct xfs_info xfs;
64 #define dirbuf ((char *)FSYS_BUF)
65 #define filebuf ((char *)FSYS_BUF + 4096)
66 #define inode ((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
67 #define icore (inode->di_core)
69 #define mask32lo(n) (((__uint32_t)1 << (n)) - 1)
71 #define XFS_INO_MASK(k) ((__uint32_t)((1ULL << (k)) - 1))
72 #define XFS_INO_OFFSET_BITS xfs.inopblog
73 #define XFS_INO_AGBNO_BITS xfs.agblklog
74 #define XFS_INO_AGINO_BITS (xfs.agblklog + xfs.inopblog)
75 #define XFS_INO_AGNO_BITS xfs.agnolog
77 static inline xfs_agblock_t
78 agino2agbno (xfs_agino_t agino)
80 return agino >> XFS_INO_OFFSET_BITS;
83 static inline xfs_agnumber_t
84 ino2agno (xfs_ino_t ino)
86 return ino >> XFS_INO_AGINO_BITS;
89 static inline xfs_agino_t
90 ino2agino (xfs_ino_t ino)
92 return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
96 ino2offset (xfs_ino_t ino)
98 return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
101 static inline __uint16_t
105 __asm__("xchgb %b0,%h0" \
110 return __be16_to_cpu(x);
114 static inline __uint32_t
119 /* 386 doesn't have bswap. So what. */
120 __asm__("bswap %0" : "=r" (x) : "0" (x));
122 /* This is slower but this works on all x86 architectures. */
123 __asm__("xchgb %b0, %h0" \
125 "\n\txchgb %b0, %h0" \
126 : "=q" (x) : "0" (x));
130 return __be32_to_cpu(x);
134 static inline __uint64_t
137 __uint32_t h = x >> 32;
138 __uint32_t l = x & ((1ULL<<32)-1);
139 return (((__uint64_t)le32(l)) << 32) | ((__uint64_t)(le32(h)));
144 xt_start (xfs_bmbt_rec_32_t *r)
146 return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
147 (((xfs_fsblock_t)le32 (r->l2)) << 11) |
148 (((xfs_fsblock_t)le32 (r->l3)) >> 21);
152 xt_offset (xfs_bmbt_rec_32_t *r)
154 return (((xfs_fileoff_t)le32 (r->l0) &
155 mask32lo(31)) << 23) |
156 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
160 xt_len (xfs_bmbt_rec_32_t *r)
162 return le32(r->l3) & mask32lo(21);
166 xfs_highbit32(__uint32_t v)
171 for (i = 0; i < 31; i++, v >>= 1) {
180 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
182 return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
186 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
188 return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
192 fsb2daddr (xfs_fsblock_t fsbno)
194 return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
195 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
199 #define offsetof(t,m) ((long)&(((t *)0)->m))
202 btroot_maxrecs (void)
204 int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
206 return (tmp - sizeof(xfs_bmdr_block_t) - offsetof(xfs_dinode_t, di_u)) /
207 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
211 di_read (xfs_ino_t ino)
219 agno = ino2agno (ino);
220 agino = ino2agino (ino);
221 agbno = agino2agbno (agino);
222 offset = ino2offset (ino);
223 daddr = agb2daddr (agno, agbno);
225 devread (daddr, offset*xfs.isize, xfs.isize, (char *)inode);
227 xfs.ptr0 = *(xfs_bmbt_ptr_t *)
228 (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
229 + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
238 xfs_btree_lblock_t h;
240 switch (icore.di_format) {
241 case XFS_DINODE_FMT_EXTENTS:
242 xfs.xt = inode->di_u.di_bmx;
243 xfs.nextents = le32 (icore.di_nextents);
245 case XFS_DINODE_FMT_BTREE:
248 xfs.daddr = fsb2daddr (le64(ptr0));
249 devread (xfs.daddr, 0,
250 sizeof(xfs_btree_lblock_t), (char *)&h);
252 xfs.nextents = le16(h.bb_numrecs);
253 xfs.next = fsb2daddr (le64(h.bb_rightsib));
254 xfs.fpos = sizeof(xfs_btree_block_t);
257 devread (xfs.daddr, xfs.btnode_ptr0_off,
258 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
268 switch (icore.di_format) {
269 case XFS_DINODE_FMT_EXTENTS:
270 if (xfs.nextents == 0)
273 case XFS_DINODE_FMT_BTREE:
274 if (xfs.nextents == 0) {
275 xfs_btree_lblock_t h;
278 xfs.daddr = xfs.next;
279 devread (xfs.daddr, 0, sizeof(xfs_btree_lblock_t), (char *)&h);
280 xfs.nextents = le16(h.bb_numrecs);
281 xfs.next = fsb2daddr (le64(h.bb_rightsib));
282 xfs.fpos = sizeof(xfs_btree_block_t);
284 /* Yeah, I know that's slow, but I really don't care */
285 devread (xfs.daddr, xfs.fpos, sizeof(xfs_bmbt_rec_t), filebuf);
286 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
287 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
289 xad.offset = xt_offset (xfs.xt);
290 xad.start = xt_start (xfs.xt);
291 xad.len = xt_len (xfs.xt);
299 * Name lies - the function reads only first 100 bytes
305 xfs_fileoff_t offset;;
308 while ((xad = next_extent ())) {
309 offset = xad->offset;
310 if (isinxt (xfs.dablk, offset, xad->len)) {
311 devread (fsb2daddr (xad->start + xfs.dablk - offset),
318 static inline xfs_ino_t
319 sf_ino (char *sfe, int namelen)
321 void *p = sfe + namelen + 3;
323 return (xfs.i8param == 0)
324 ? le64(*(xfs_ino_t *)p) : le32(*(__uint32_t *)p);
327 static inline xfs_ino_t
330 return (xfs.i8param == 0)
331 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
332 : le32(*(__uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
342 next_dentry (xfs_ino_t *ino)
346 static char *usual[2];
347 static xfs_dir2_sf_entry_t *sfe;
351 usual[0] = strdup(".");
352 usual[1] = strdup("..");
356 if (xfs.dirpos >= xfs.dirmax) {
359 xfs.dablk = xfs.forw;
361 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
362 xfs.dirmax = le16 (h->count) - le16 (h->stale);
363 xfs.forw = le32 (h->info.forw);
368 switch (icore.di_format) {
369 case XFS_DINODE_FMT_LOCAL:
370 switch (xfs.dirpos) {
375 *ino = sf_parent_ino ();
378 sfe = (xfs_dir2_sf_entry_t *)
380 + sizeof(xfs_dir2_sf_hdr_t)
384 namelen = sfe->namelen;
385 *ino = sf_ino ((char *)sfe, namelen);
386 name = (char *)sfe->name;
387 sfe = (xfs_dir2_sf_entry_t *)
388 ((char *)sfe + namelen + 11 - xfs.i8param);
391 case XFS_DINODE_FMT_BTREE:
392 case XFS_DINODE_FMT_EXTENTS:
393 #define dau ((xfs_dir2_data_union_t *)dirbuf)
395 if (xfs.blkoff >= xfs.dirbsize) {
396 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
397 filepos &= ~(xfs.dirbsize - 1);
398 filepos |= xfs.blkoff;
400 xfs_read (dirbuf, 4);
402 if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
403 toread = roundup8 (le16(dau->unused.length)) - 4;
404 xfs.blkoff += toread;
410 xfs_read ((char *)dirbuf + 4, 5);
411 *ino = le64 (dau->entry.inumber);
412 namelen = dau->entry.namelen;
414 toread = roundup8 (namelen + 11) - 9;
415 xfs_read (dirbuf, toread);
416 name = (char *)dirbuf;
417 xfs.blkoff += toread + 5;
426 first_dentry (xfs_ino_t *ino)
429 switch (icore.di_format) {
430 case XFS_DINODE_FMT_LOCAL:
431 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
432 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
435 case XFS_DINODE_FMT_EXTENTS:
436 case XFS_DINODE_FMT_BTREE:
438 xfs_read (dirbuf, sizeof(xfs_dir2_data_hdr_t));
439 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
440 #define tail ((xfs_dir2_block_tail_t *)dirbuf)
441 filepos = xfs.dirbsize - sizeof(*tail);
442 xfs_read (dirbuf, sizeof(*tail));
443 xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
446 xfs.dablk = (1ULL << 35) >> xfs.blklog;
447 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
448 #define n ((xfs_da_intnode_t *)dirbuf)
451 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
452 || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
453 xfs.dirmax = le16 (h->count) - le16 (h->stale);
454 xfs.forw = le32 (h->info.forw);
457 xfs.dablk = le32 (n->btree[0].before);
462 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
463 filepos = xfs.blkoff;
466 return next_dentry (ino);
474 if (!devread (0, 0, sizeof(super), (char *)&super)
475 || (le32(super.sb_magicnum) != XFS_SB_MAGIC)
476 || ((le16(super.sb_versionnum)
477 & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) ) {
481 xfs.bsize = le32 (super.sb_blocksize);
482 xfs.blklog = super.sb_blocklog;
483 xfs.bdlog = xfs.blklog - SECTOR_BITS;
484 xfs.rootino = le64 (super.sb_rootino);
485 xfs.isize = le16 (super.sb_inodesize);
486 xfs.agblocks = le32 (super.sb_agblocks);
487 xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
489 xfs.inopblog = super.sb_inopblog;
490 xfs.agblklog = super.sb_agblklog;
491 xfs.agnolog = xfs_highbit32 (le32(super.sb_agcount));
493 xfs.btnode_ptr0_off =
494 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
495 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
496 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
502 xfs_read (char *buf, int len)
505 xfs_fileoff_t endofprev, endofcur, offset;
506 xfs_filblks_t xadlen;
507 int toread, startpos, endpos;
509 if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
510 grub_memmove (buf, inode->di_u.di_c + filepos, len);
516 endpos = filepos + len;
517 endofprev = (xfs_fileoff_t)-1;
519 while (len > 0 && (xad = next_extent ())) {
520 offset = xad->offset;
522 if (isinxt (filepos >> xfs.blklog, offset, xadlen)) {
523 endofcur = (offset + xadlen) << xfs.blklog;
524 toread = (endofcur >= endpos)
525 ? len : (endofcur - filepos);
527 disk_read_func = disk_read_hook;
528 devread (fsb2daddr (xad->start),
529 filepos - (offset << xfs.blklog), toread, buf);
530 disk_read_func = NULL;
535 } else if (offset > endofprev) {
536 toread = ((offset << xfs.blklog) >= endpos)
537 ? len : ((offset - endofprev) << xfs.blklog);
540 for (; toread; toread--) {
545 endofprev = offset + xadlen;
548 return filepos - startpos;
552 xfs_dir (char *dirname)
554 xfs_ino_t ino, parent_ino, new_ino;
557 int cmp, n, link_count;
558 char linkbuf[xfs.bsize];
559 char *rest, *name, ch;
561 parent_ino = ino = xfs.rootino;
565 di_size = le64 (icore.di_size);
566 di_mode = le16 (icore.di_mode);
568 if ((di_mode & IFMT) == IFLNK) {
569 if (++link_count > MAX_LINK_COUNT) {
570 errnum = ERR_SYMLINK_LOOP;
573 if (di_size < xfs.bsize - 1) {
576 n = xfs_read (linkbuf, filemax);
578 errnum = ERR_FILELENGTH;
582 ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
583 while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
589 if (!*dirname || isspace (*dirname)) {
590 if ((di_mode & IFMT) != IFREG) {
591 errnum = ERR_BAD_FILETYPE;
599 if ((di_mode & IFMT) != IFDIR) {
600 errnum = ERR_BAD_FILETYPE;
604 for (; *dirname == '/'; dirname++);
606 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
609 name = first_dentry (&new_ino);
611 cmp = (!*dirname) ? -1 : substring (dirname, name);
613 if (print_possibilities && ch != '/' && cmp <= 0) {
614 if (print_possibilities > 0)
615 print_possibilities = -print_possibilities;
616 print_a_completion (name);
623 *(dirname = rest) = ch;
626 name = next_dentry (&new_ino);
628 if (print_possibilities < 0)
631 errnum = ERR_FILE_NOT_FOUND;
639 #endif /* FSYS_XFS */