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() */
26 * This is a way of ensuring that we have something in the system
27 * use fields that is compatible with Rock Ridge. Return zero on success.
30 static int check_sp(struct rock_ridge *rr, struct inode *inode)
32 if (rr->u.SP.magic[0] != 0xbe)
34 if (rr->u.SP.magic[1] != 0xef)
36 ISOFS_SB(inode->i_sb)->s_rock_offset = rr->u.SP.skip;
40 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
41 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
43 CHR = ((unsigned char *) DE) + LEN; \
44 LEN = *((unsigned char *) DE) - LEN; \
46 if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
48 LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
49 CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
54 #define MAYBE_CONTINUE(LABEL,DEV) \
55 {if (buffer) { kfree(buffer); buffer = NULL; } \
57 int block, offset, offset1; \
58 struct buffer_head * pbh; \
59 buffer = kmalloc(cont_size,GFP_KERNEL); \
60 if (!buffer) goto out; \
61 block = cont_extent; \
62 offset = cont_offset; \
64 pbh = sb_bread(DEV->i_sb, block); \
66 if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
70 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
72 chr = (unsigned char *) buffer; \
79 printk("Unable to read rock-ridge attributes\n"); \
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)
92 struct rock_ridge *rr;
97 if (!ISOFS_SB(inode->i_sb)->s_rock)
101 SETUP_ROCK_RIDGE(de, chr, len);
104 while (len > 2) { /* There may be one byte for padding somewhere */
105 rr = (struct rock_ridge *)chr;
107 goto out; /* Something got screwed up here */
108 sig = isonum_721(chr);
112 goto out; /* corrupted isofs */
116 if ((rr->u.RR.flags[0] & RR_NM) == 0)
120 if (check_sp(rr, inode))
124 cont_extent = isonum_733(rr->u.CE.extent);
125 cont_offset = isonum_733(rr->u.CE.offset);
126 cont_size = isonum_733(rr->u.CE.size);
134 * If the flags are 2 or 4, this indicates '.' or '..'.
135 * We don't want to do anything with this, because it
136 * screws up the code that calls us. We don't really
137 * care anyways, since we can just use the non-RR
140 if (rr->u.NM.flags & 6)
143 if (rr->u.NM.flags & ~1) {
144 printk("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;
163 MAYBE_CONTINUE(repeat, inode);
165 return retnamlen; /* If 0, this file did not have a NM field */
173 parse_rock_ridge_inode_internal(struct iso_directory_record *de,
174 struct inode *inode, int regard_xa)
181 struct rock_ridge *rr;
188 if (!ISOFS_SB(inode->i_sb)->s_rock)
191 SETUP_ROCK_RIDGE(de, chr, len);
200 while (len > 2) { /* There may be one byte for padding somewhere */
201 rr = (struct rock_ridge *)chr;
203 goto out; /* Something got screwed up here */
204 sig = isonum_721(chr);
208 goto out; /* corrupted isofs */
211 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
213 if ((rr->u.RR.flags[0] &
214 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0)
219 if (check_sp(rr, inode))
223 cont_extent = isonum_733(rr->u.CE.extent);
224 cont_offset = isonum_733(rr->u.CE.offset);
225 cont_size = isonum_733(rr->u.CE.size);
228 ISOFS_SB(inode->i_sb)->s_rock = 1;
229 printk(KERN_DEBUG "ISO 9660 Extensions: ");
232 for (p = 0; p < rr->u.ER.len_id; p++)
233 printk("%c", rr->u.ER.data[p]);
238 inode->i_mode = isonum_733(rr->u.PX.mode);
239 inode->i_nlink = isonum_733(rr->u.PX.n_links);
240 inode->i_uid = isonum_733(rr->u.PX.uid);
241 inode->i_gid = isonum_733(rr->u.PX.gid);
246 high = isonum_733(rr->u.PN.dev_high);
247 low = isonum_733(rr->u.PN.dev_low);
249 * The Rock Ridge standard specifies that if
250 * sizeof(dev_t) <= 4, then the high field is
251 * unused, and the device number is completely
252 * stored in the low field. Some writers may
253 * ignore this subtlety,
254 * and as a result we test to see if the entire
256 * stored in the low field, and use that.
258 if ((low & ~0xff) && high == 0) {
260 MKDEV(low >> 8, low & 0xff);
269 * Some RRIP writers incorrectly place ctime in the
270 * TF_CREATE field. Try to handle this correctly for
273 /* Rock ridge never appears on a High Sierra disk */
275 if (rr->u.TF.flags & TF_CREATE) {
276 inode->i_ctime.tv_sec =
277 iso_date(rr->u.TF.times[cnt++].time,
279 inode->i_ctime.tv_nsec = 0;
281 if (rr->u.TF.flags & TF_MODIFY) {
282 inode->i_mtime.tv_sec =
283 iso_date(rr->u.TF.times[cnt++].time,
285 inode->i_mtime.tv_nsec = 0;
287 if (rr->u.TF.flags & TF_ACCESS) {
288 inode->i_atime.tv_sec =
289 iso_date(rr->u.TF.times[cnt++].time,
291 inode->i_atime.tv_nsec = 0;
293 if (rr->u.TF.flags & TF_ATTRIBUTES) {
294 inode->i_ctime.tv_sec =
295 iso_date(rr->u.TF.times[cnt++].time,
297 inode->i_ctime.tv_nsec = 0;
303 struct SL_component *slp;
304 struct SL_component *oldslp;
306 slp = &rr->u.SL.link;
307 inode->i_size = symlink_len;
310 switch (slp->flags & ~1) {
326 printk("Symlink component flag "
327 "not implemented\n");
329 slen -= slp->len + 2;
331 slp = (struct SL_component *)
332 (((char *)slp) + slp->len + 2);
346 * If this component record isn't
347 * continued, then append a '/'.
350 && (oldslp->flags & 1) == 0)
354 symlink_len = inode->i_size;
357 printk(KERN_WARNING "Attempt to read inode for "
358 "relocated directory\n");
361 ISOFS_I(inode)->i_first_extent =
362 isonum_733(rr->u.CL.location);
364 isofs_iget(inode->i_sb,
365 ISOFS_I(inode)->i_first_extent,
369 inode->i_mode = reloc->i_mode;
370 inode->i_nlink = reloc->i_nlink;
371 inode->i_uid = reloc->i_uid;
372 inode->i_gid = reloc->i_gid;
373 inode->i_rdev = reloc->i_rdev;
374 inode->i_size = reloc->i_size;
375 inode->i_blocks = reloc->i_blocks;
376 inode->i_atime = reloc->i_atime;
377 inode->i_ctime = reloc->i_ctime;
378 inode->i_mtime = reloc->i_mtime;
382 case SIG('Z', 'F'): {
385 if (ISOFS_SB(inode->i_sb)->s_nocompress)
387 algo = isonum_721(rr->u.ZF.algorithm);
388 if (algo == SIG('p', 'z')) {
390 isonum_711(&rr->u.ZF.parms[1]);
391 if (block_shift < PAGE_CACHE_SHIFT
392 || block_shift > 17) {
393 printk(KERN_WARNING "isofs: "
394 "Can't handle ZF block "
399 * Note: we don't change
402 ISOFS_I(inode)->i_file_format =
403 isofs_file_compressed;
405 * Parameters to compression
406 * algorithm (header size,
409 ISOFS_I(inode)->i_format_parm[0] =
410 isonum_711(&rr->u.ZF.parms[0]);
411 ISOFS_I(inode)->i_format_parm[1] =
412 isonum_711(&rr->u.ZF.parms[1]);
419 "isofs: Unknown ZF compression "
421 rr->u.ZF.algorithm[0],
422 rr->u.ZF.algorithm[1]);
431 MAYBE_CONTINUE(repeat, inode);
438 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
442 struct SL_component *oldslp;
443 struct SL_component *slp;
445 slp = &rr->u.SL.link;
448 switch (slp->flags & ~1) {
450 if (slp->len > plimit - rpnt)
452 memcpy(rpnt, slp->text, slp->len);
461 if (2 > plimit - rpnt)
473 printk("Symlink component flag not implemented (%d)\n",
476 slen -= slp->len + 2;
478 slp = (struct SL_component *)((char *)slp + slp->len + 2);
482 * If there is another SL record, and this component
483 * record isn't continued, then add a slash.
485 if ((!rootflag) && (rr->u.SL.flags & 1) &&
486 !(oldslp->flags & 1)) {
495 * If this component record isn't continued, then append a '/'.
497 if (!rootflag && !(oldslp->flags & 1)) {
506 int parse_rock_ridge_inode(struct iso_directory_record *de, struct inode *inode)
508 int result = parse_rock_ridge_inode_internal(de, inode, 0);
509 /* if rockridge flag was reset and we didn't look for attributes
510 * behind eventual XA attributes, have a look there */
511 if ((ISOFS_SB(inode->i_sb)->s_rock_offset == -1)
512 && (ISOFS_SB(inode->i_sb)->s_rock == 2)) {
513 result = parse_rock_ridge_inode_internal(de, inode, 14);
518 /* readpage() for symlinks: reads symlink contents into the page and either
519 makes it uptodate and returns 0 or returns error (-EIO) */
521 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
523 struct inode *inode = page->mapping->host;
524 struct iso_inode_info *ei = ISOFS_I(inode);
525 char *link = kmap(page);
526 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
527 struct buffer_head *bh;
530 struct iso_directory_record *raw_inode;
535 unsigned long block, offset;
539 struct rock_ridge *rr;
541 if (!ISOFS_SB(inode->i_sb)->s_rock)
544 block = ei->i_iget5_block;
546 bh = sb_bread(inode->i_sb, block);
550 offset = ei->i_iget5_offset;
551 pnt = (unsigned char *)bh->b_data + offset;
553 raw_inode = (struct iso_directory_record *)pnt;
556 * If we go past the end of the buffer, there is some sort of error.
558 if (offset + *pnt > bufsize)
561 /* Now test for possible Rock Ridge extensions which will override
562 some of these numbers in the inode structure. */
564 SETUP_ROCK_RIDGE(raw_inode, chr, len);
567 while (len > 2) { /* There may be one byte for padding somewhere */
568 rr = (struct rock_ridge *)chr;
570 goto out; /* Something got screwed up here */
571 sig = isonum_721(chr);
575 goto out; /* corrupted isofs */
579 if ((rr->u.RR.flags[0] & RR_SL) == 0)
583 if (check_sp(rr, inode))
587 rpnt = get_symlink_chunk(rpnt, rr,
588 link + (PAGE_SIZE - 1));
593 /* This tells is if there is a continuation record */
594 cont_extent = isonum_733(rr->u.CE.extent);
595 cont_offset = isonum_733(rr->u.CE.offset);
596 cont_size = isonum_733(rr->u.CE.size);
601 MAYBE_CONTINUE(repeat, inode);
609 SetPageUptodate(page);
614 /* error exit from macro */
620 printk("unable to read i-node block");
623 printk("symlink spans iso9660 blocks\n");
634 struct address_space_operations isofs_symlink_aops = {
635 .readpage = rock_ridge_symlink_readpage