rock.c 15 KB

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