2 * linux/fs/isofs/rock.c
4 * (C) 1992, 1993 Eric Youngdale
6 * Rock Ridge Extensions to iso9660
9 #include <linux/slab.h>
10 #include <linux/pagemap.h>
11 #include <linux/smp_lock.h>
16 /* These functions are designed to read the system areas of a directory record
17 * and extract relevant information. There are different functions provided
18 * depending upon what information we need at the time. One function fills
19 * out an inode structure, a second one extracts a filename, a third one
20 * returns a symbolic link name, and a fourth one returns the extent number
23 #define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
25 /* This is a way of ensuring that we have something in the system
26 use fields that is compatible with Rock Ridge */
27 #define CHECK_SP(FAIL) \
28 if(rr->u.SP.magic[0] != 0xbe) FAIL; \
29 if(rr->u.SP.magic[1] != 0xef) FAIL; \
30 ISOFS_SB(inode->i_sb)->s_rock_offset=rr->u.SP.skip;
31 /* We define a series of macros because each function must do exactly the
32 same thing in certain places. We use the macros to ensure that everything
35 #define CONTINUE_DECLS \
36 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
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);}
44 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
45 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
47 CHR = ((unsigned char *) DE) + LEN; \
48 LEN = *((unsigned char *) DE) - LEN; \
50 if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
52 LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
53 CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
58 #define MAYBE_CONTINUE(LABEL,DEV) \
59 {if (buffer) { kfree(buffer); buffer = NULL; } \
61 int block, offset, offset1; \
62 struct buffer_head * pbh; \
63 buffer = kmalloc(cont_size,GFP_KERNEL); \
64 if (!buffer) goto out; \
65 block = cont_extent; \
66 offset = cont_offset; \
68 pbh = sb_bread(DEV->i_sb, block); \
70 if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
74 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
76 chr = (unsigned char *) buffer; \
83 printk("Unable to read rock-ridge attributes\n"); \
86 /* return length of name field; 0: not found, -1: to be ignored */
87 int get_rock_ridge_filename(struct iso_directory_record *de,
88 char *retname, struct inode *inode)
93 int retnamlen = 0, truncate = 0;
95 if (!ISOFS_SB(inode->i_sb)->s_rock)
99 SETUP_ROCK_RIDGE(de, chr, len);
102 struct rock_ridge *rr;
105 while (len > 2) { /* There may be one byte for padding somewhere */
106 rr = (struct rock_ridge *)chr;
108 goto out; /* Something got screwed up here */
109 sig = isonum_721(chr);
113 goto out; /* corrupted isofs */
117 if ((rr->u.RR.flags[0] & RR_NM) == 0)
132 * If the flags are 2 or 4, this indicates '.' or '..'.
133 * We don't want to do anything with this, because it
134 * screws up the code that calls us. We don't really
135 * care anyways, since we can just use the non-RR
138 if (rr->u.NM.flags & 6) {
142 if (rr->u.NM.flags & ~1) {
144 ("Unsupported NM flag settings (%d)\n",
148 if ((strlen(retname) + rr->len - 5) >= 254) {
152 strncat(retname, rr->u.NM.name, rr->len - 5);
153 retnamlen += rr->len - 5;
164 MAYBE_CONTINUE(repeat, inode);
167 return retnamlen; /* If 0, this file did not have a NM field */
175 parse_rock_ridge_inode_internal(struct iso_directory_record *de,
176 struct inode *inode, int regard_xa)
183 if (!ISOFS_SB(inode->i_sb)->s_rock)
186 SETUP_ROCK_RIDGE(de, chr, len);
198 struct rock_ridge *rr;
201 while (len > 2) { /* There may be one byte for padding somewhere */
202 rr = (struct rock_ridge *)chr;
204 goto out; /* Something got screwed up here */
205 sig = isonum_721(chr);
209 goto out; /* corrupted isofs */
212 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
214 if ((rr->u.RR.flags[0] &
215 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0)
226 ISOFS_SB(inode->i_sb)->s_rock = 1;
227 printk(KERN_DEBUG "ISO 9660 Extensions: ");
230 for (p = 0; p < rr->u.ER.len_id; p++)
231 printk("%c", rr->u.ER.data[p]);
236 inode->i_mode = isonum_733(rr->u.PX.mode);
237 inode->i_nlink = isonum_733(rr->u.PX.n_links);
238 inode->i_uid = isonum_733(rr->u.PX.uid);
239 inode->i_gid = isonum_733(rr->u.PX.gid);
244 high = isonum_733(rr->u.PN.dev_high);
245 low = isonum_733(rr->u.PN.dev_low);
247 * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
248 * then the high field is unused, and the device number is completely
249 * stored in the low field. Some writers may ignore this subtlety,
250 * and as a result we test to see if the entire device number is
251 * stored in the low field, and use that.
253 if ((low & ~0xff) && high == 0) {
255 MKDEV(low >> 8, low & 0xff);
263 /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
264 Try to handle this correctly for either case. */
265 cnt = 0; /* Rock ridge never appears on a High Sierra disk */
266 if (rr->u.TF.flags & TF_CREATE) {
267 inode->i_ctime.tv_sec =
268 iso_date(rr->u.TF.times[cnt++].time,
270 inode->i_ctime.tv_nsec = 0;
272 if (rr->u.TF.flags & TF_MODIFY) {
273 inode->i_mtime.tv_sec =
274 iso_date(rr->u.TF.times[cnt++].time,
276 inode->i_mtime.tv_nsec = 0;
278 if (rr->u.TF.flags & TF_ACCESS) {
279 inode->i_atime.tv_sec =
280 iso_date(rr->u.TF.times[cnt++].time,
282 inode->i_atime.tv_nsec = 0;
284 if (rr->u.TF.flags & TF_ATTRIBUTES) {
285 inode->i_ctime.tv_sec =
286 iso_date(rr->u.TF.times[cnt++].time,
288 inode->i_ctime.tv_nsec = 0;
294 struct SL_component *slp;
295 struct SL_component *oldslp;
297 slp = &rr->u.SL.link;
298 inode->i_size = symlink_len;
301 switch (slp->flags & ~1) {
318 ("Symlink component flag not implemented\n");
320 slen -= slp->len + 2;
339 * If this component record isn't continued, then append a '/'.
342 && (oldslp->flags & 1) == 0)
346 symlink_len = inode->i_size;
350 "Attempt to read inode for relocated directory\n");
353 ISOFS_I(inode)->i_first_extent =
354 isonum_733(rr->u.CL.location);
356 isofs_iget(inode->i_sb,
357 ISOFS_I(inode)->i_first_extent,
361 inode->i_mode = reloc->i_mode;
362 inode->i_nlink = reloc->i_nlink;
363 inode->i_uid = reloc->i_uid;
364 inode->i_gid = reloc->i_gid;
365 inode->i_rdev = reloc->i_rdev;
366 inode->i_size = reloc->i_size;
367 inode->i_blocks = reloc->i_blocks;
368 inode->i_atime = reloc->i_atime;
369 inode->i_ctime = reloc->i_ctime;
370 inode->i_mtime = reloc->i_mtime;
375 if (!ISOFS_SB(inode->i_sb)->s_nocompress) {
377 algo = isonum_721(rr->u.ZF.algorithm);
378 if (algo == SIG('p', 'z')) {
380 isonum_711(&rr->u.ZF.
384 || block_shift > 17) {
386 "isofs: Can't handle ZF block size of 2^%d\n",
389 /* Note: we don't change i_blocks here */
392 isofs_file_compressed;
393 /* Parameters to compression algorithm (header size, block size) */
412 "isofs: Unknown ZF compression algorithm: %c%c\n",
413 rr->u.ZF.algorithm[0],
414 rr->u.ZF.algorithm[1]);
424 MAYBE_CONTINUE(repeat, inode);
431 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
435 struct SL_component *oldslp;
436 struct SL_component *slp;
438 slp = &rr->u.SL.link;
441 switch (slp->flags & ~1) {
443 if (slp->len > plimit - rpnt)
445 memcpy(rpnt, slp->text, slp->len);
454 if (2 > plimit - rpnt)
466 printk("Symlink component flag not implemented (%d)\n",
469 slen -= slp->len + 2;
471 slp = (struct SL_component *)((char *)slp + slp->len + 2);
475 * If there is another SL record, and this component
476 * record isn't continued, then add a slash.
478 if ((!rootflag) && (rr->u.SL.flags & 1) &&
479 !(oldslp->flags & 1)) {
488 * If this component record isn't continued, then append a '/'.
490 if (!rootflag && !(oldslp->flags & 1)) {
499 int parse_rock_ridge_inode(struct iso_directory_record *de, struct inode *inode)
501 int result = parse_rock_ridge_inode_internal(de, inode, 0);
502 /* if rockridge flag was reset and we didn't look for attributes
503 * behind eventual XA attributes, have a look there */
504 if ((ISOFS_SB(inode->i_sb)->s_rock_offset == -1)
505 && (ISOFS_SB(inode->i_sb)->s_rock == 2)) {
506 result = parse_rock_ridge_inode_internal(de, inode, 14);
511 /* readpage() for symlinks: reads symlink contents into the page and either
512 makes it uptodate and returns 0 or returns error (-EIO) */
514 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
516 struct inode *inode = page->mapping->host;
517 struct iso_inode_info *ei = ISOFS_I(inode);
518 char *link = kmap(page);
519 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
520 struct buffer_head *bh;
523 struct iso_directory_record *raw_inode;
525 unsigned long block, offset;
529 struct rock_ridge *rr;
531 if (!ISOFS_SB(inode->i_sb)->s_rock)
534 block = ei->i_iget5_block;
536 bh = sb_bread(inode->i_sb, block);
540 offset = ei->i_iget5_offset;
541 pnt = (unsigned char *)bh->b_data + offset;
543 raw_inode = (struct iso_directory_record *)pnt;
546 * If we go past the end of the buffer, there is some sort of error.
548 if (offset + *pnt > bufsize)
551 /* Now test for possible Rock Ridge extensions which will override
552 some of these numbers in the inode structure. */
554 SETUP_ROCK_RIDGE(raw_inode, chr, len);
557 while (len > 2) { /* There may be one byte for padding somewhere */
558 rr = (struct rock_ridge *)chr;
560 goto out; /* Something got screwed up here */
561 sig = isonum_721(chr);
565 goto out; /* corrupted isofs */
569 if ((rr->u.RR.flags[0] & RR_SL) == 0)
576 rpnt = get_symlink_chunk(rpnt, rr,
577 link + (PAGE_SIZE - 1));
582 /* This tells is if there is a continuation record */
588 MAYBE_CONTINUE(repeat, inode);
597 SetPageUptodate(page);
602 /* error exit from macro */
608 printk("unable to read i-node block");
611 printk("symlink spans iso9660 blocks\n");
622 struct address_space_operations isofs_symlink_aops = {
623 .readpage = rock_ridge_symlink_readpage