rock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * linux/fs/isofs/rock.c
  3. *
  4. * (C) 1992, 1993 Eric Youngdale
  5. *
  6. * Rock Ridge Extensions to iso9660
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/smp_lock.h>
  11. #include "isofs.h"
  12. #include "rock.h"
  13. /* These functions are designed to read the system areas of a directory record
  14. * and extract relevant information. There are different functions provided
  15. * depending upon what information we need at the time. One function fills
  16. * out an inode structure, a second one extracts a filename, a third one
  17. * returns a symbolic link name, and a fourth one returns the extent number
  18. * for the file. */
  19. #define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
  20. /* This is a way of ensuring that we have something in the system
  21. use fields that is compatible with Rock Ridge */
  22. #define CHECK_SP(FAIL) \
  23. if(rr->u.SP.magic[0] != 0xbe) FAIL; \
  24. if(rr->u.SP.magic[1] != 0xef) FAIL; \
  25. ISOFS_SB(inode->i_sb)->s_rock_offset=rr->u.SP.skip;
  26. /* We define a series of macros because each function must do exactly the
  27. same thing in certain places. We use the macros to ensure that everything
  28. is done correctly */
  29. #define CONTINUE_DECLS \
  30. int cont_extent = 0, cont_offset = 0, cont_size = 0; \
  31. void *buffer = NULL
  32. #define CHECK_CE \
  33. {cont_extent = isonum_733(rr->u.CE.extent); \
  34. cont_offset = isonum_733(rr->u.CE.offset); \
  35. cont_size = isonum_733(rr->u.CE.size);}
  36. #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
  37. {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
  38. if(LEN & 1) LEN++; \
  39. CHR = ((unsigned char *) DE) + LEN; \
  40. LEN = *((unsigned char *) DE) - LEN; \
  41. if (LEN<0) LEN=0; \
  42. if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
  43. { \
  44. LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
  45. CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
  46. if (LEN<0) LEN=0; \
  47. } \
  48. }
  49. #define MAYBE_CONTINUE(LABEL,DEV) \
  50. {if (buffer) { kfree(buffer); buffer = NULL; } \
  51. if (cont_extent){ \
  52. int block, offset, offset1; \
  53. struct buffer_head * pbh; \
  54. buffer = kmalloc(cont_size,GFP_KERNEL); \
  55. if (!buffer) goto out; \
  56. block = cont_extent; \
  57. offset = cont_offset; \
  58. offset1 = 0; \
  59. pbh = sb_bread(DEV->i_sb, block); \
  60. if(pbh){ \
  61. if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
  62. brelse(pbh); \
  63. goto out; \
  64. } \
  65. memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
  66. brelse(pbh); \
  67. chr = (unsigned char *) buffer; \
  68. len = cont_size; \
  69. cont_extent = 0; \
  70. cont_size = 0; \
  71. cont_offset = 0; \
  72. goto LABEL; \
  73. } \
  74. printk("Unable to read rock-ridge attributes\n"); \
  75. }}
  76. /* return length of name field; 0: not found, -1: to be ignored */
  77. int get_rock_ridge_filename(struct iso_directory_record *de,
  78. char *retname, struct inode *inode)
  79. {
  80. int len;
  81. unsigned char *chr;
  82. CONTINUE_DECLS;
  83. int retnamlen = 0, truncate = 0;
  84. if (!ISOFS_SB(inode->i_sb)->s_rock)
  85. return 0;
  86. *retname = 0;
  87. SETUP_ROCK_RIDGE(de, chr, len);
  88. repeat:
  89. {
  90. struct rock_ridge *rr;
  91. int sig;
  92. while (len > 2) { /* There may be one byte for padding somewhere */
  93. rr = (struct rock_ridge *)chr;
  94. if (rr->len < 3)
  95. goto out; /* Something got screwed up here */
  96. sig = isonum_721(chr);
  97. chr += rr->len;
  98. len -= rr->len;
  99. if (len < 0)
  100. goto out; /* corrupted isofs */
  101. switch (sig) {
  102. case SIG('R', 'R'):
  103. if ((rr->u.RR.flags[0] & RR_NM) == 0)
  104. goto out;
  105. break;
  106. case SIG('S', 'P'):
  107. CHECK_SP(goto out);
  108. break;
  109. case SIG('C', 'E'):
  110. CHECK_CE;
  111. break;
  112. case SIG('N', 'M'):
  113. if (truncate)
  114. break;
  115. if (rr->len < 5)
  116. break;
  117. /*
  118. * If the flags are 2 or 4, this indicates '.' or '..'.
  119. * We don't want to do anything with this, because it
  120. * screws up the code that calls us. We don't really
  121. * care anyways, since we can just use the non-RR
  122. * name.
  123. */
  124. if (rr->u.NM.flags & 6) {
  125. break;
  126. }
  127. if (rr->u.NM.flags & ~1) {
  128. printk
  129. ("Unsupported NM flag settings (%d)\n",
  130. rr->u.NM.flags);
  131. break;
  132. }
  133. if ((strlen(retname) + rr->len - 5) >= 254) {
  134. truncate = 1;
  135. break;
  136. }
  137. strncat(retname, rr->u.NM.name, rr->len - 5);
  138. retnamlen += rr->len - 5;
  139. break;
  140. case SIG('R', 'E'):
  141. if (buffer)
  142. kfree(buffer);
  143. return -1;
  144. default:
  145. break;
  146. }
  147. }
  148. }
  149. MAYBE_CONTINUE(repeat, inode);
  150. if (buffer)
  151. kfree(buffer);
  152. return retnamlen; /* If 0, this file did not have a NM field */
  153. out:
  154. if (buffer)
  155. kfree(buffer);
  156. return 0;
  157. }
  158. static int
  159. parse_rock_ridge_inode_internal(struct iso_directory_record *de,
  160. struct inode *inode, int regard_xa)
  161. {
  162. int len;
  163. unsigned char *chr;
  164. int symlink_len = 0;
  165. CONTINUE_DECLS;
  166. if (!ISOFS_SB(inode->i_sb)->s_rock)
  167. return 0;
  168. SETUP_ROCK_RIDGE(de, chr, len);
  169. if (regard_xa) {
  170. chr += 14;
  171. len -= 14;
  172. if (len < 0)
  173. len = 0;
  174. }
  175. repeat:
  176. {
  177. int cnt, sig;
  178. struct inode *reloc;
  179. struct rock_ridge *rr;
  180. int rootflag;
  181. while (len > 2) { /* There may be one byte for padding somewhere */
  182. rr = (struct rock_ridge *)chr;
  183. if (rr->len < 3)
  184. goto out; /* Something got screwed up here */
  185. sig = isonum_721(chr);
  186. chr += rr->len;
  187. len -= rr->len;
  188. if (len < 0)
  189. goto out; /* corrupted isofs */
  190. switch (sig) {
  191. #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
  192. case SIG('R', 'R'):
  193. if ((rr->u.RR.flags[0] &
  194. (RR_PX | RR_TF | RR_SL | RR_CL)) == 0)
  195. goto out;
  196. break;
  197. #endif
  198. case SIG('S', 'P'):
  199. CHECK_SP(goto out);
  200. break;
  201. case SIG('C', 'E'):
  202. CHECK_CE;
  203. break;
  204. case SIG('E', 'R'):
  205. ISOFS_SB(inode->i_sb)->s_rock = 1;
  206. printk(KERN_DEBUG "ISO 9660 Extensions: ");
  207. {
  208. int p;
  209. for (p = 0; p < rr->u.ER.len_id; p++)
  210. printk("%c", rr->u.ER.data[p]);
  211. }
  212. printk("\n");
  213. break;
  214. case SIG('P', 'X'):
  215. inode->i_mode = isonum_733(rr->u.PX.mode);
  216. inode->i_nlink = isonum_733(rr->u.PX.n_links);
  217. inode->i_uid = isonum_733(rr->u.PX.uid);
  218. inode->i_gid = isonum_733(rr->u.PX.gid);
  219. break;
  220. case SIG('P', 'N'):
  221. {
  222. int high, low;
  223. high = isonum_733(rr->u.PN.dev_high);
  224. low = isonum_733(rr->u.PN.dev_low);
  225. /*
  226. * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
  227. * then the high field is unused, and the device number is completely
  228. * stored in the low field. Some writers may ignore this subtlety,
  229. * and as a result we test to see if the entire device number is
  230. * stored in the low field, and use that.
  231. */
  232. if ((low & ~0xff) && high == 0) {
  233. inode->i_rdev =
  234. MKDEV(low >> 8, low & 0xff);
  235. } else {
  236. inode->i_rdev =
  237. MKDEV(high, low);
  238. }
  239. }
  240. break;
  241. case SIG('T', 'F'):
  242. /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
  243. Try to handle this correctly for either case. */
  244. cnt = 0; /* Rock ridge never appears on a High Sierra disk */
  245. if (rr->u.TF.flags & TF_CREATE) {
  246. inode->i_ctime.tv_sec =
  247. iso_date(rr->u.TF.times[cnt++].time,
  248. 0);
  249. inode->i_ctime.tv_nsec = 0;
  250. }
  251. if (rr->u.TF.flags & TF_MODIFY) {
  252. inode->i_mtime.tv_sec =
  253. iso_date(rr->u.TF.times[cnt++].time,
  254. 0);
  255. inode->i_mtime.tv_nsec = 0;
  256. }
  257. if (rr->u.TF.flags & TF_ACCESS) {
  258. inode->i_atime.tv_sec =
  259. iso_date(rr->u.TF.times[cnt++].time,
  260. 0);
  261. inode->i_atime.tv_nsec = 0;
  262. }
  263. if (rr->u.TF.flags & TF_ATTRIBUTES) {
  264. inode->i_ctime.tv_sec =
  265. iso_date(rr->u.TF.times[cnt++].time,
  266. 0);
  267. inode->i_ctime.tv_nsec = 0;
  268. }
  269. break;
  270. case SIG('S', 'L'):
  271. {
  272. int slen;
  273. struct SL_component *slp;
  274. struct SL_component *oldslp;
  275. slen = rr->len - 5;
  276. slp = &rr->u.SL.link;
  277. inode->i_size = symlink_len;
  278. while (slen > 1) {
  279. rootflag = 0;
  280. switch (slp->flags & ~1) {
  281. case 0:
  282. inode->i_size +=
  283. slp->len;
  284. break;
  285. case 2:
  286. inode->i_size += 1;
  287. break;
  288. case 4:
  289. inode->i_size += 2;
  290. break;
  291. case 8:
  292. rootflag = 1;
  293. inode->i_size += 1;
  294. break;
  295. default:
  296. printk
  297. ("Symlink component flag not implemented\n");
  298. }
  299. slen -= slp->len + 2;
  300. oldslp = slp;
  301. slp =
  302. (struct SL_component
  303. *)(((char *)slp) +
  304. slp->len + 2);
  305. if (slen < 2) {
  306. if (((rr->u.SL.
  307. flags & 1) != 0)
  308. &&
  309. ((oldslp->
  310. flags & 1) == 0))
  311. inode->i_size +=
  312. 1;
  313. break;
  314. }
  315. /*
  316. * If this component record isn't continued, then append a '/'.
  317. */
  318. if (!rootflag
  319. && (oldslp->flags & 1) == 0)
  320. inode->i_size += 1;
  321. }
  322. }
  323. symlink_len = inode->i_size;
  324. break;
  325. case SIG('R', 'E'):
  326. printk(KERN_WARNING
  327. "Attempt to read inode for relocated directory\n");
  328. goto out;
  329. case SIG('C', 'L'):
  330. ISOFS_I(inode)->i_first_extent =
  331. isonum_733(rr->u.CL.location);
  332. reloc =
  333. isofs_iget(inode->i_sb,
  334. ISOFS_I(inode)->i_first_extent,
  335. 0);
  336. if (!reloc)
  337. goto out;
  338. inode->i_mode = reloc->i_mode;
  339. inode->i_nlink = reloc->i_nlink;
  340. inode->i_uid = reloc->i_uid;
  341. inode->i_gid = reloc->i_gid;
  342. inode->i_rdev = reloc->i_rdev;
  343. inode->i_size = reloc->i_size;
  344. inode->i_blocks = reloc->i_blocks;
  345. inode->i_atime = reloc->i_atime;
  346. inode->i_ctime = reloc->i_ctime;
  347. inode->i_mtime = reloc->i_mtime;
  348. iput(reloc);
  349. break;
  350. #ifdef CONFIG_ZISOFS
  351. case SIG('Z', 'F'):
  352. if (!ISOFS_SB(inode->i_sb)->s_nocompress) {
  353. int algo;
  354. algo = isonum_721(rr->u.ZF.algorithm);
  355. if (algo == SIG('p', 'z')) {
  356. int block_shift =
  357. isonum_711(&rr->u.ZF.
  358. parms[1]);
  359. if (block_shift <
  360. PAGE_CACHE_SHIFT
  361. || block_shift > 17) {
  362. printk(KERN_WARNING
  363. "isofs: Can't handle ZF block size of 2^%d\n",
  364. block_shift);
  365. } else {
  366. /* Note: we don't change i_blocks here */
  367. ISOFS_I(inode)->
  368. i_file_format =
  369. isofs_file_compressed;
  370. /* Parameters to compression algorithm (header size, block size) */
  371. ISOFS_I(inode)->
  372. i_format_parm[0] =
  373. isonum_711(&rr->u.
  374. ZF.
  375. parms
  376. [0]);
  377. ISOFS_I(inode)->
  378. i_format_parm[1] =
  379. isonum_711(&rr->u.
  380. ZF.
  381. parms
  382. [1]);
  383. inode->i_size =
  384. isonum_733(rr->u.ZF.
  385. real_size);
  386. }
  387. } else {
  388. printk(KERN_WARNING
  389. "isofs: Unknown ZF compression algorithm: %c%c\n",
  390. rr->u.ZF.algorithm[0],
  391. rr->u.ZF.algorithm[1]);
  392. }
  393. }
  394. break;
  395. #endif
  396. default:
  397. break;
  398. }
  399. }
  400. }
  401. MAYBE_CONTINUE(repeat, inode);
  402. out:
  403. if (buffer)
  404. kfree(buffer);
  405. return 0;
  406. }
  407. static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
  408. {
  409. int slen;
  410. int rootflag;
  411. struct SL_component *oldslp;
  412. struct SL_component *slp;
  413. slen = rr->len - 5;
  414. slp = &rr->u.SL.link;
  415. while (slen > 1) {
  416. rootflag = 0;
  417. switch (slp->flags & ~1) {
  418. case 0:
  419. if (slp->len > plimit - rpnt)
  420. return NULL;
  421. memcpy(rpnt, slp->text, slp->len);
  422. rpnt += slp->len;
  423. break;
  424. case 2:
  425. if (rpnt >= plimit)
  426. return NULL;
  427. *rpnt++ = '.';
  428. break;
  429. case 4:
  430. if (2 > plimit - rpnt)
  431. return NULL;
  432. *rpnt++ = '.';
  433. *rpnt++ = '.';
  434. break;
  435. case 8:
  436. if (rpnt >= plimit)
  437. return NULL;
  438. rootflag = 1;
  439. *rpnt++ = '/';
  440. break;
  441. default:
  442. printk("Symlink component flag not implemented (%d)\n",
  443. slp->flags);
  444. }
  445. slen -= slp->len + 2;
  446. oldslp = slp;
  447. slp = (struct SL_component *)((char *)slp + slp->len + 2);
  448. if (slen < 2) {
  449. /*
  450. * If there is another SL record, and this component
  451. * record isn't continued, then add a slash.
  452. */
  453. if ((!rootflag) && (rr->u.SL.flags & 1) &&
  454. !(oldslp->flags & 1)) {
  455. if (rpnt >= plimit)
  456. return NULL;
  457. *rpnt++ = '/';
  458. }
  459. break;
  460. }
  461. /*
  462. * If this component record isn't continued, then append a '/'.
  463. */
  464. if (!rootflag && !(oldslp->flags & 1)) {
  465. if (rpnt >= plimit)
  466. return NULL;
  467. *rpnt++ = '/';
  468. }
  469. }
  470. return rpnt;
  471. }
  472. int parse_rock_ridge_inode(struct iso_directory_record *de, struct inode *inode)
  473. {
  474. int result = parse_rock_ridge_inode_internal(de, inode, 0);
  475. /* if rockridge flag was reset and we didn't look for attributes
  476. * behind eventual XA attributes, have a look there */
  477. if ((ISOFS_SB(inode->i_sb)->s_rock_offset == -1)
  478. && (ISOFS_SB(inode->i_sb)->s_rock == 2)) {
  479. result = parse_rock_ridge_inode_internal(de, inode, 14);
  480. }
  481. return result;
  482. }
  483. /* readpage() for symlinks: reads symlink contents into the page and either
  484. makes it uptodate and returns 0 or returns error (-EIO) */
  485. static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
  486. {
  487. struct inode *inode = page->mapping->host;
  488. struct iso_inode_info *ei = ISOFS_I(inode);
  489. char *link = kmap(page);
  490. unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
  491. struct buffer_head *bh;
  492. char *rpnt = link;
  493. unsigned char *pnt;
  494. struct iso_directory_record *raw_inode;
  495. CONTINUE_DECLS;
  496. unsigned long block, offset;
  497. int sig;
  498. int len;
  499. unsigned char *chr;
  500. struct rock_ridge *rr;
  501. if (!ISOFS_SB(inode->i_sb)->s_rock)
  502. goto error;
  503. block = ei->i_iget5_block;
  504. lock_kernel();
  505. bh = sb_bread(inode->i_sb, block);
  506. if (!bh)
  507. goto out_noread;
  508. offset = ei->i_iget5_offset;
  509. pnt = (unsigned char *)bh->b_data + offset;
  510. raw_inode = (struct iso_directory_record *)pnt;
  511. /*
  512. * If we go past the end of the buffer, there is some sort of error.
  513. */
  514. if (offset + *pnt > bufsize)
  515. goto out_bad_span;
  516. /* Now test for possible Rock Ridge extensions which will override
  517. some of these numbers in the inode structure. */
  518. SETUP_ROCK_RIDGE(raw_inode, chr, len);
  519. repeat:
  520. while (len > 2) { /* There may be one byte for padding somewhere */
  521. rr = (struct rock_ridge *)chr;
  522. if (rr->len < 3)
  523. goto out; /* Something got screwed up here */
  524. sig = isonum_721(chr);
  525. chr += rr->len;
  526. len -= rr->len;
  527. if (len < 0)
  528. goto out; /* corrupted isofs */
  529. switch (sig) {
  530. case SIG('R', 'R'):
  531. if ((rr->u.RR.flags[0] & RR_SL) == 0)
  532. goto out;
  533. break;
  534. case SIG('S', 'P'):
  535. CHECK_SP(goto out);
  536. break;
  537. case SIG('S', 'L'):
  538. rpnt = get_symlink_chunk(rpnt, rr,
  539. link + (PAGE_SIZE - 1));
  540. if (rpnt == NULL)
  541. goto out;
  542. break;
  543. case SIG('C', 'E'):
  544. /* This tells is if there is a continuation record */
  545. CHECK_CE;
  546. default:
  547. break;
  548. }
  549. }
  550. MAYBE_CONTINUE(repeat, inode);
  551. if (buffer)
  552. kfree(buffer);
  553. if (rpnt == link)
  554. goto fail;
  555. brelse(bh);
  556. *rpnt = '\0';
  557. unlock_kernel();
  558. SetPageUptodate(page);
  559. kunmap(page);
  560. unlock_page(page);
  561. return 0;
  562. /* error exit from macro */
  563. out:
  564. if (buffer)
  565. kfree(buffer);
  566. goto fail;
  567. out_noread:
  568. printk("unable to read i-node block");
  569. goto fail;
  570. out_bad_span:
  571. printk("symlink spans iso9660 blocks\n");
  572. fail:
  573. brelse(bh);
  574. unlock_kernel();
  575. error:
  576. SetPageError(page);
  577. kunmap(page);
  578. unlock_page(page);
  579. return -EIO;
  580. }
  581. struct address_space_operations isofs_symlink_aops = {
  582. .readpage = rock_ridge_symlink_readpage
  583. };