Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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