rock.c 15 KB

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