1 /* fsys_xfs.c - an implementation for the SGI XFS file system */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2001,2002,2004 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <xenfsimage_grub.h>
21 #include "xfs.h"
22
23 #define MAX_LINK_COUNT 8
24
25 typedef struct xad {
26 xfs_fileoff_t offset;
27 xfs_fsblock_t start;
28 xfs_filblks_t len;
29 } xad_t;
30
31 struct xfs_info {
32 int bsize;
33 int dirbsize;
34 int isize;
35 unsigned int agblocks;
36 int bdlog;
37 int blklog;
38 int inopblog;
39 int agblklog;
40 int agnolog;
41 unsigned int nextents;
42 xfs_daddr_t next;
43 xfs_daddr_t daddr;
44 xfs_dablk_t forw;
45 xfs_dablk_t dablk;
46 xfs_bmbt_rec_32_t *xt;
47 xfs_bmbt_ptr_t ptr0;
48 int btnode_ptr0_off;
49 int i8param;
50 int dirpos;
51 int dirmax;
52 int blkoff;
53 int fpos;
54 xfs_ino_t rootino;
55 };
56
57 static struct xfs_info xfs;
58
59 #define dirbuf ((char *)FSYS_BUF)
60 #define filebuf ((char *)FSYS_BUF + 4096)
61 #define inode ((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
62 #define icore (inode->di_core)
63
64 #define mask32lo(n) (((xfs_uint32_t)1 << (n)) - 1)
65
66 #define XFS_INO_MASK(k) ((xfs_uint32_t)((1ULL << (k)) - 1))
67 #define XFS_INO_OFFSET_BITS xfs.inopblog
68 #define XFS_INO_AGBNO_BITS xfs.agblklog
69 #define XFS_INO_AGINO_BITS (xfs.agblklog + xfs.inopblog)
70 #define XFS_INO_AGNO_BITS xfs.agnolog
71
72 static inline xfs_agblock_t
agino2agbno(xfs_agino_t agino)73 agino2agbno (xfs_agino_t agino)
74 {
75 return agino >> XFS_INO_OFFSET_BITS;
76 }
77
78 static inline xfs_agnumber_t
ino2agno(xfs_ino_t ino)79 ino2agno (xfs_ino_t ino)
80 {
81 return ino >> XFS_INO_AGINO_BITS;
82 }
83
84 static inline xfs_agino_t
ino2agino(xfs_ino_t ino)85 ino2agino (xfs_ino_t ino)
86 {
87 return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
88 }
89
90 static inline int
ino2offset(xfs_ino_t ino)91 ino2offset (xfs_ino_t ino)
92 {
93 return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
94 }
95
96 static inline xfs_uint16_t
le16(xfs_uint16_t x)97 le16 (xfs_uint16_t x)
98 {
99 __asm__("xchgb %b0,%h0" \
100 : "=Q" (x) \
101 : "0" (x)); \
102 return x;
103 }
104
105 static inline xfs_uint32_t
le32(xfs_uint32_t x)106 le32 (xfs_uint32_t x)
107 {
108 #if 0
109 /* 386 doesn't have bswap. */
110 __asm__("bswap %0" : "=r" (x) : "0" (x));
111 #else
112 /* This is slower but this works on all x86 architectures. */
113 __asm__("xchgb %b0, %h0" \
114 "\n\troll $16, %0" \
115 "\n\txchgb %b0, %h0" \
116 : "=Q" (x) : "0" (x));
117 #endif
118 return x;
119 }
120
121 static inline xfs_uint64_t
le64(xfs_uint64_t x)122 le64 (xfs_uint64_t x)
123 {
124 xfs_uint32_t h = x >> 32;
125 xfs_uint32_t l = x & ((1ULL<<32)-1);
126 return (((xfs_uint64_t)le32(l)) << 32) | ((xfs_uint64_t)(le32(h)));
127 }
128
129
130 static xfs_fsblock_t
xt_start(xfs_bmbt_rec_32_t * r)131 xt_start (xfs_bmbt_rec_32_t *r)
132 {
133 return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
134 (((xfs_fsblock_t)le32 (r->l2)) << 11) |
135 (((xfs_fsblock_t)le32 (r->l3)) >> 21);
136 }
137
138 static xfs_fileoff_t
xt_offset(xfs_bmbt_rec_32_t * r)139 xt_offset (xfs_bmbt_rec_32_t *r)
140 {
141 return (((xfs_fileoff_t)le32 (r->l0) &
142 mask32lo(31)) << 23) |
143 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
144 }
145
146 static xfs_filblks_t
xt_len(xfs_bmbt_rec_32_t * r)147 xt_len (xfs_bmbt_rec_32_t *r)
148 {
149 return le32(r->l3) & mask32lo(21);
150 }
151
152 static inline int
xfs_highbit32(xfs_uint32_t v)153 xfs_highbit32(xfs_uint32_t v)
154 {
155 int i;
156
157 if (--v) {
158 for (i = 0; i < 31; i++, v >>= 1) {
159 if (v == 0)
160 return i;
161 }
162 }
163 return 0;
164 }
165
166 static int
isinxt(xfs_fileoff_t key,xfs_fileoff_t offset,xfs_filblks_t len)167 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
168 {
169 return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
170 }
171
172 static xfs_daddr_t
agb2daddr(xfs_agnumber_t agno,xfs_agblock_t agbno)173 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
174 {
175 return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
176 }
177
178 static xfs_daddr_t
fsb2daddr(xfs_fsblock_t fsbno)179 fsb2daddr (xfs_fsblock_t fsbno)
180 {
181 return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
182 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
183 }
184
185 #undef offsetof
186 #define offsetof(t,m) ((size_t)&(((t *)0)->m))
187
188 static inline int
btroot_maxrecs(fsi_file_t * ffi)189 btroot_maxrecs (fsi_file_t *ffi)
190 {
191 int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
192
193 return (tmp - sizeof(xfs_bmdr_block_t) - offsetof(xfs_dinode_t, di_u)) /
194 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
195 }
196
197 static int
di_read(fsi_file_t * ffi,xfs_ino_t ino)198 di_read (fsi_file_t *ffi, xfs_ino_t ino)
199 {
200 xfs_agino_t agino;
201 xfs_agnumber_t agno;
202 xfs_agblock_t agbno;
203 xfs_daddr_t daddr;
204 int offset;
205
206 agno = ino2agno (ino);
207 agino = ino2agino (ino);
208 agbno = agino2agbno (agino);
209 offset = ino2offset (ino);
210 daddr = agb2daddr (agno, agbno);
211
212 devread (ffi, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
213
214 xfs.ptr0 = *(xfs_bmbt_ptr_t *)
215 (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
216 + btroot_maxrecs (ffi)*sizeof(xfs_bmbt_key_t));
217
218 return 1;
219 }
220
221 static void
init_extents(fsi_file_t * ffi)222 init_extents (fsi_file_t *ffi)
223 {
224 xfs_bmbt_ptr_t ptr0;
225 xfs_btree_lblock_t h;
226
227 switch (icore.di_format) {
228 case XFS_DINODE_FMT_EXTENTS:
229 xfs.xt = inode->di_u.di_bmx;
230 xfs.nextents = le32 (icore.di_nextents);
231 break;
232 case XFS_DINODE_FMT_BTREE:
233 ptr0 = xfs.ptr0;
234 for (;;) {
235 xfs.daddr = fsb2daddr (le64(ptr0));
236 devread (ffi, xfs.daddr, 0,
237 sizeof(xfs_btree_lblock_t), (char *)&h);
238 if (!h.bb_level) {
239 xfs.nextents = le16(h.bb_numrecs);
240 xfs.next = fsb2daddr (le64(h.bb_rightsib));
241 xfs.fpos = sizeof(xfs_btree_block_t);
242 return;
243 }
244 devread (ffi, xfs.daddr, xfs.btnode_ptr0_off,
245 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
246 }
247 }
248 }
249
250 static xad_t *
next_extent(fsi_file_t * ffi)251 next_extent (fsi_file_t *ffi)
252 {
253 static xad_t xad;
254
255 switch (icore.di_format) {
256 case XFS_DINODE_FMT_EXTENTS:
257 if (xfs.nextents == 0)
258 return NULL;
259 break;
260 case XFS_DINODE_FMT_BTREE:
261 if (xfs.nextents == 0) {
262 xfs_btree_lblock_t h;
263 if (xfs.next == 0)
264 return NULL;
265 xfs.daddr = xfs.next;
266 devread (ffi, xfs.daddr, 0, sizeof(xfs_btree_lblock_t), (char *)&h);
267 xfs.nextents = le16(h.bb_numrecs);
268 xfs.next = fsb2daddr (le64(h.bb_rightsib));
269 xfs.fpos = sizeof(xfs_btree_block_t);
270 }
271 /* Yeah, I know that's slow, but I really don't care */
272 devread (ffi, xfs.daddr, xfs.fpos, sizeof(xfs_bmbt_rec_t), filebuf);
273 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
274 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
275 }
276 xad.offset = xt_offset (xfs.xt);
277 xad.start = xt_start (xfs.xt);
278 xad.len = xt_len (xfs.xt);
279 ++xfs.xt;
280 --xfs.nextents;
281
282 return &xad;
283 }
284
285 /*
286 * Name lies - the function reads only first 100 bytes
287 */
288 static void
xfs_dabread(fsi_file_t * ffi)289 xfs_dabread (fsi_file_t *ffi)
290 {
291 xad_t *xad;
292 xfs_fileoff_t offset;;
293
294 init_extents (ffi);
295 while ((xad = next_extent (ffi))) {
296 offset = xad->offset;
297 if (isinxt (xfs.dablk, offset, xad->len)) {
298 devread (ffi, fsb2daddr (xad->start + xfs.dablk - offset),
299 0, 100, dirbuf);
300 break;
301 }
302 }
303 }
304
305 static inline xfs_ino_t
sf_ino(char * sfe,int namelen)306 sf_ino (char *sfe, int namelen)
307 {
308 void *p = sfe + namelen + 3;
309
310 return (xfs.i8param == 0)
311 ? le64(*(xfs_ino_t *)p) : le32(*(xfs_uint32_t *)p);
312 }
313
314 static inline xfs_ino_t
sf_parent_ino(fsi_file_t * ffi)315 sf_parent_ino (fsi_file_t *ffi)
316 {
317 return (xfs.i8param == 0)
318 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
319 : le32(*(xfs_uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
320 }
321
322 static inline int
roundup8(int n)323 roundup8 (int n)
324 {
325 return ((n+7)&~7);
326 }
327
328 static int
329 xfs_read (fsi_file_t *ffi, char *buf, int len);
330
331 static char *
next_dentry(fsi_file_t * ffi,xfs_ino_t * ino)332 next_dentry (fsi_file_t *ffi, xfs_ino_t *ino)
333 {
334 int namelen = 1;
335 int toread;
336 static char usual[2][3] = {".", ".."};
337 static xfs_dir2_sf_entry_t *sfe;
338 char *name = usual[0];
339
340 if (xfs.dirpos >= xfs.dirmax) {
341 if (xfs.forw == 0)
342 return NULL;
343 xfs.dablk = xfs.forw;
344 xfs_dabread (ffi);
345 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
346 xfs.dirmax = le16 (h->count) - le16 (h->stale);
347 xfs.forw = le32 (h->info.forw);
348 #undef h
349 xfs.dirpos = 0;
350 }
351
352 switch (icore.di_format) {
353 case XFS_DINODE_FMT_LOCAL:
354 switch (xfs.dirpos) {
355 case -2:
356 *ino = 0;
357 break;
358 case -1:
359 *ino = sf_parent_ino (ffi);
360 ++name;
361 ++namelen;
362 sfe = (xfs_dir2_sf_entry_t *)
363 (inode->di_u.di_c
364 + sizeof(xfs_dir2_sf_hdr_t)
365 - xfs.i8param);
366 break;
367 default:
368 namelen = sfe->namelen;
369 *ino = sf_ino ((char *)sfe, namelen);
370 name = (char *)sfe->name;
371 sfe = (xfs_dir2_sf_entry_t *)
372 ((char *)sfe + namelen + 11 - xfs.i8param);
373 }
374 break;
375 case XFS_DINODE_FMT_BTREE:
376 case XFS_DINODE_FMT_EXTENTS:
377 #define dau ((xfs_dir2_data_union_t *)dirbuf)
378 for (;;) {
379 if (xfs.blkoff >= xfs.dirbsize) {
380 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
381 filepos &= ~(xfs.dirbsize - 1);
382 filepos |= xfs.blkoff;
383 }
384 xfs_read (ffi, dirbuf, 4);
385 xfs.blkoff += 4;
386 if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
387 toread = roundup8 (le16(dau->unused.length)) - 4;
388 xfs.blkoff += toread;
389 filepos += toread;
390 continue;
391 }
392 break;
393 }
394 xfs_read (ffi, (char *)dirbuf + 4, 5);
395 *ino = le64 (dau->entry.inumber);
396 namelen = dau->entry.namelen;
397 #undef dau
398 toread = roundup8 (namelen + 11) - 9;
399 xfs_read (ffi, dirbuf, toread);
400 name = (char *)dirbuf;
401 xfs.blkoff += toread + 5;
402 }
403 ++xfs.dirpos;
404 name[namelen] = 0;
405
406 return name;
407 }
408
409 static char *
first_dentry(fsi_file_t * ffi,xfs_ino_t * ino)410 first_dentry (fsi_file_t *ffi, xfs_ino_t *ino)
411 {
412 xfs.forw = 0;
413 switch (icore.di_format) {
414 case XFS_DINODE_FMT_LOCAL:
415 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
416 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
417 xfs.dirpos = -2;
418 break;
419 case XFS_DINODE_FMT_EXTENTS:
420 case XFS_DINODE_FMT_BTREE:
421 filepos = 0;
422 xfs_read (ffi, dirbuf, sizeof(xfs_dir2_data_hdr_t));
423 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
424 #define tail ((xfs_dir2_block_tail_t *)dirbuf)
425 filepos = xfs.dirbsize - sizeof(*tail);
426 xfs_read (ffi, dirbuf, sizeof(*tail));
427 xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
428 #undef tail
429 } else {
430 xfs.dablk = (1ULL << 35) >> xfs.blklog;
431 #define h ((xfs_dir2_leaf_hdr_t *)dirbuf)
432 #define n ((xfs_da_intnode_t *)dirbuf)
433 for (;;) {
434 xfs_dabread (ffi);
435 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
436 || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
437 xfs.dirmax = le16 (h->count) - le16 (h->stale);
438 xfs.forw = le32 (h->info.forw);
439 break;
440 }
441 xfs.dablk = le32 (n->btree[0].before);
442 }
443 #undef n
444 #undef h
445 }
446 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
447 filepos = xfs.blkoff;
448 xfs.dirpos = 0;
449 }
450 return next_dentry (ffi, ino);
451 }
452
453 static int
xfs_mount(fsi_file_t * ffi,const char * options)454 xfs_mount (fsi_file_t *ffi, const char *options)
455 {
456 xfs_sb_t super;
457
458 if (!devread (ffi, 0, 0, sizeof(super), (char *)&super)
459 || (le32(super.sb_magicnum) != XFS_SB_MAGIC)
460 || ((le16(super.sb_versionnum)
461 & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) ) {
462 return 0;
463 }
464
465 xfs.bsize = le32 (super.sb_blocksize);
466 xfs.blklog = super.sb_blocklog;
467 xfs.bdlog = xfs.blklog - SECTOR_BITS;
468 xfs.rootino = le64 (super.sb_rootino);
469 xfs.isize = le16 (super.sb_inodesize);
470 xfs.agblocks = le32 (super.sb_agblocks);
471 xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
472
473 xfs.inopblog = super.sb_inopblog;
474 xfs.agblklog = super.sb_agblklog;
475 xfs.agnolog = xfs_highbit32 (le32(super.sb_agcount));
476
477 xfs.btnode_ptr0_off =
478 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
479 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
480 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
481
482 return 1;
483 }
484
485 static int
xfs_read(fsi_file_t * ffi,char * buf,int len)486 xfs_read (fsi_file_t *ffi, char *buf, int len)
487 {
488 xad_t *xad;
489 xfs_fileoff_t endofprev, endofcur, offset;
490 xfs_filblks_t xadlen;
491 int toread, startpos, endpos;
492
493 if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
494 grub_memmove (buf, inode->di_u.di_c + filepos, len);
495 filepos += len;
496 return len;
497 }
498
499 startpos = filepos;
500 endpos = filepos + len;
501 endofprev = (xfs_fileoff_t)-1;
502 init_extents (ffi);
503 while (len > 0 && (xad = next_extent (ffi))) {
504 offset = xad->offset;
505 xadlen = xad->len;
506 if (isinxt (filepos >> xfs.blklog, offset, xadlen)) {
507 endofcur = (offset + xadlen) << xfs.blklog;
508 toread = (endofcur >= endpos)
509 ? len : (endofcur - filepos);
510
511 disk_read_func = disk_read_hook;
512 devread (ffi, fsb2daddr (xad->start),
513 filepos - (offset << xfs.blklog), toread, buf);
514 disk_read_func = NULL;
515
516 buf += toread;
517 len -= toread;
518 filepos += toread;
519 } else if (offset > endofprev) {
520 toread = ((offset << xfs.blklog) >= endpos)
521 ? len : ((offset - endofprev) << xfs.blklog);
522 len -= toread;
523 filepos += toread;
524 for (; toread; toread--) {
525 *buf++ = 0;
526 }
527 continue;
528 }
529 endofprev = offset + xadlen;
530 }
531
532 return filepos - startpos;
533 }
534
535 static int
xfs_dir(fsi_file_t * ffi,char * dirname)536 xfs_dir (fsi_file_t *ffi, char *dirname)
537 {
538 xfs_ino_t ino, parent_ino, new_ino;
539 xfs_fsize_t di_size;
540 int di_mode;
541 int cmp, n, link_count;
542 char linkbuf[xfs.bsize];
543 char *rest, *name, ch;
544
545 parent_ino = ino = xfs.rootino;
546 link_count = 0;
547 for (;;) {
548 di_read (ffi, ino);
549 di_size = le64 (icore.di_size);
550 di_mode = le16 (icore.di_mode);
551
552 if ((di_mode & IFMT) == IFLNK) {
553 if (++link_count > MAX_LINK_COUNT) {
554 errnum = ERR_SYMLINK_LOOP;
555 return 0;
556 }
557 if (di_size < xfs.bsize - 1) {
558 filepos = 0;
559 filemax = di_size;
560 n = xfs_read (ffi, linkbuf, filemax);
561 } else {
562 errnum = ERR_FILELENGTH;
563 return 0;
564 }
565
566 ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
567 while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
568 linkbuf[n] = 0;
569 dirname = linkbuf;
570 continue;
571 }
572
573 if (!*dirname || isspace ((uint8_t)*dirname)) {
574 if ((di_mode & IFMT) != IFREG) {
575 errnum = ERR_BAD_FILETYPE;
576 return 0;
577 }
578 filepos = 0;
579 filemax = di_size;
580 return 1;
581 }
582
583 if ((di_mode & IFMT) != IFDIR) {
584 errnum = ERR_BAD_FILETYPE;
585 return 0;
586 }
587
588 for (; *dirname == '/'; dirname++);
589
590 for (rest = dirname; (ch = *rest)
591 && !isspace ((uint8_t)ch) && ch != '/'; rest++);
592 *rest = 0;
593
594 name = first_dentry (ffi, &new_ino);
595 for (;;) {
596 cmp = (!*dirname) ? -1 : substring (dirname, name);
597 #ifndef STAGE1_5
598 if (print_possibilities && ch != '/' && cmp <= 0) {
599 if (print_possibilities > 0)
600 print_possibilities = -print_possibilities;
601 print_a_completion (name);
602 } else
603 #endif
604 if (cmp == 0) {
605 parent_ino = ino;
606 if (new_ino)
607 ino = new_ino;
608 *(dirname = rest) = ch;
609 break;
610 }
611 name = next_dentry (ffi, &new_ino);
612 if (name == NULL) {
613 if (print_possibilities < 0)
614 return 1;
615
616 errnum = ERR_FILE_NOT_FOUND;
617 *rest = ch;
618 return 0;
619 }
620 }
621 }
622 }
623
624 fsi_plugin_ops_t *
fsi_init_plugin(int version,fsi_plugin_t * fp,const char ** name)625 fsi_init_plugin(int version, fsi_plugin_t *fp, const char **name)
626 {
627 static fsig_plugin_ops_t ops = {
628 FSIMAGE_PLUGIN_VERSION,
629 .fpo_mount = xfs_mount,
630 .fpo_dir = xfs_dir,
631 .fpo_read = xfs_read
632 };
633
634 *name = "xfs";
635 return (fsig_init(fp, &ops));
636 }
637