rock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
  34. {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
  35. if(LEN & 1) LEN++; \
  36. CHR = ((unsigned char *) DE) + LEN; \
  37. LEN = *((unsigned char *) DE) - LEN; \
  38. if (LEN<0) LEN=0; \
  39. if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
  40. { \
  41. LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
  42. CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
  43. if (LEN<0) LEN=0; \
  44. } \
  45. }
  46. #define MAYBE_CONTINUE(LABEL,DEV) \
  47. {if (buffer) { kfree(buffer); buffer = NULL; } \
  48. if (cont_extent){ \
  49. int block, offset, offset1; \
  50. struct buffer_head * pbh; \
  51. buffer = kmalloc(cont_size,GFP_KERNEL); \
  52. if (!buffer) goto out; \
  53. block = cont_extent; \
  54. offset = cont_offset; \
  55. offset1 = 0; \
  56. pbh = sb_bread(DEV->i_sb, block); \
  57. if(pbh){ \
  58. if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
  59. brelse(pbh); \
  60. goto out; \
  61. } \
  62. memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
  63. brelse(pbh); \
  64. chr = (unsigned char *) buffer; \
  65. len = cont_size; \
  66. cont_extent = 0; \
  67. cont_size = 0; \
  68. cont_offset = 0; \
  69. goto LABEL; \
  70. } \
  71. printk("Unable to read rock-ridge attributes\n"); \
  72. }}
  73. /* return length of name field; 0: not found, -1: to be ignored */
  74. int get_rock_ridge_filename(struct iso_directory_record *de,
  75. char *retname, struct inode *inode)
  76. {
  77. int len;
  78. unsigned char *chr;
  79. int cont_extent = 0;
  80. int cont_offset = 0;
  81. int cont_size = 0;
  82. void *buffer = NULL;
  83. struct rock_ridge *rr;
  84. int sig;
  85. int retnamlen = 0;
  86. int truncate = 0;
  87. if (!ISOFS_SB(inode->i_sb)->s_rock)
  88. return 0;
  89. *retname = 0;
  90. SETUP_ROCK_RIDGE(de, chr, len);
  91. repeat:
  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. if (check_sp(rr, inode))
  108. goto out;
  109. break;
  110. case SIG('C', 'E'):
  111. cont_extent = isonum_733(rr->u.CE.extent);
  112. cont_offset = isonum_733(rr->u.CE.offset);
  113. cont_size = isonum_733(rr->u.CE.size);
  114. break;
  115. case SIG('N', 'M'):
  116. if (truncate)
  117. break;
  118. if (rr->len < 5)
  119. break;
  120. /*
  121. * If the flags are 2 or 4, this indicates '.' or '..'.
  122. * We don't want to do anything with this, because it
  123. * screws up the code that calls us. We don't really
  124. * care anyways, since we can just use the non-RR
  125. * name.
  126. */
  127. if (rr->u.NM.flags & 6)
  128. break;
  129. if (rr->u.NM.flags & ~1) {
  130. printk("Unsupported NM flag settings (%d)\n",
  131. rr->u.NM.flags);
  132. break;
  133. }
  134. if ((strlen(retname) + rr->len - 5) >= 254) {
  135. truncate = 1;
  136. break;
  137. }
  138. strncat(retname, rr->u.NM.name, rr->len - 5);
  139. retnamlen += rr->len - 5;
  140. break;
  141. case SIG('R', 'E'):
  142. if (buffer)
  143. kfree(buffer);
  144. return -1;
  145. default:
  146. break;
  147. }
  148. }
  149. MAYBE_CONTINUE(repeat, inode);
  150. kfree(buffer);
  151. return retnamlen; /* If 0, this file did not have a NM field */
  152. out:
  153. if (buffer)
  154. kfree(buffer);
  155. return 0;
  156. }
  157. static int
  158. parse_rock_ridge_inode_internal(struct iso_directory_record *de,
  159. struct inode *inode, int regard_xa)
  160. {
  161. int len;
  162. unsigned char *chr;
  163. int symlink_len = 0;
  164. int cnt, sig;
  165. struct inode *reloc;
  166. struct rock_ridge *rr;
  167. int rootflag;
  168. int cont_extent = 0;
  169. int cont_offset = 0;
  170. int cont_size = 0;
  171. void *buffer = NULL;
  172. if (!ISOFS_SB(inode->i_sb)->s_rock)
  173. return 0;
  174. SETUP_ROCK_RIDGE(de, chr, len);
  175. if (regard_xa) {
  176. chr += 14;
  177. len -= 14;
  178. if (len < 0)
  179. len = 0;
  180. }
  181. repeat:
  182. while (len > 2) { /* There may be one byte for padding somewhere */
  183. rr = (struct rock_ridge *)chr;
  184. if (rr->len < 3)
  185. goto out; /* Something got screwed up here */
  186. sig = isonum_721(chr);
  187. chr += rr->len;
  188. len -= rr->len;
  189. if (len < 0)
  190. goto out; /* corrupted isofs */
  191. switch (sig) {
  192. #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
  193. case SIG('R', 'R'):
  194. if ((rr->u.RR.flags[0] &
  195. (RR_PX | RR_TF | RR_SL | RR_CL)) == 0)
  196. goto out;
  197. break;
  198. #endif
  199. case SIG('S', 'P'):
  200. if (check_sp(rr, inode))
  201. goto out;
  202. break;
  203. case SIG('C', 'E'):
  204. cont_extent = isonum_733(rr->u.CE.extent);
  205. cont_offset = isonum_733(rr->u.CE.offset);
  206. cont_size = isonum_733(rr->u.CE.size);
  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. int cont_extent = 0;
  504. int cont_offset = 0;
  505. int cont_size = 0;
  506. void *buffer = NULL;
  507. unsigned long block, offset;
  508. int sig;
  509. int len;
  510. unsigned char *chr;
  511. struct rock_ridge *rr;
  512. if (!ISOFS_SB(inode->i_sb)->s_rock)
  513. goto error;
  514. block = ei->i_iget5_block;
  515. lock_kernel();
  516. bh = sb_bread(inode->i_sb, block);
  517. if (!bh)
  518. goto out_noread;
  519. offset = ei->i_iget5_offset;
  520. pnt = (unsigned char *)bh->b_data + offset;
  521. raw_inode = (struct iso_directory_record *)pnt;
  522. /*
  523. * If we go past the end of the buffer, there is some sort of error.
  524. */
  525. if (offset + *pnt > bufsize)
  526. goto out_bad_span;
  527. /* Now test for possible Rock Ridge extensions which will override
  528. some of these numbers in the inode structure. */
  529. SETUP_ROCK_RIDGE(raw_inode, chr, len);
  530. repeat:
  531. while (len > 2) { /* There may be one byte for padding somewhere */
  532. rr = (struct rock_ridge *)chr;
  533. if (rr->len < 3)
  534. goto out; /* Something got screwed up here */
  535. sig = isonum_721(chr);
  536. chr += rr->len;
  537. len -= rr->len;
  538. if (len < 0)
  539. goto out; /* corrupted isofs */
  540. switch (sig) {
  541. case SIG('R', 'R'):
  542. if ((rr->u.RR.flags[0] & RR_SL) == 0)
  543. goto out;
  544. break;
  545. case SIG('S', 'P'):
  546. if (check_sp(rr, inode))
  547. goto out;
  548. break;
  549. case SIG('S', 'L'):
  550. rpnt = get_symlink_chunk(rpnt, rr,
  551. link + (PAGE_SIZE - 1));
  552. if (rpnt == NULL)
  553. goto out;
  554. break;
  555. case SIG('C', 'E'):
  556. /* This tells is if there is a continuation record */
  557. cont_extent = isonum_733(rr->u.CE.extent);
  558. cont_offset = isonum_733(rr->u.CE.offset);
  559. cont_size = isonum_733(rr->u.CE.size);
  560. default:
  561. break;
  562. }
  563. }
  564. MAYBE_CONTINUE(repeat, inode);
  565. kfree(buffer);
  566. if (rpnt == link)
  567. goto fail;
  568. brelse(bh);
  569. *rpnt = '\0';
  570. unlock_kernel();
  571. SetPageUptodate(page);
  572. kunmap(page);
  573. unlock_page(page);
  574. return 0;
  575. /* error exit from macro */
  576. out:
  577. if (buffer)
  578. kfree(buffer);
  579. goto fail;
  580. out_noread:
  581. printk("unable to read i-node block");
  582. goto fail;
  583. out_bad_span:
  584. printk("symlink spans iso9660 blocks\n");
  585. fail:
  586. brelse(bh);
  587. unlock_kernel();
  588. error:
  589. SetPageError(page);
  590. kunmap(page);
  591. unlock_page(page);
  592. return -EIO;
  593. }
  594. struct address_space_operations isofs_symlink_aops = {
  595. .readpage = rock_ridge_symlink_readpage
  596. };