rock.c 15 KB

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