2 * linux/fs/isofs/rock.c
4 * (C) 1992, 1993 Eric Youngdale
6 * Rock Ridge Extensions to iso9660
9 #include <linux/stat.h>
10 #include <linux/sched.h>
11 #include <linux/iso_fs.h>
12 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
20 /* These functions are designed to read the system areas of a directory record
21 * and extract relevant information. There are different functions provided
22 * depending upon what information we need at the time. One function fills
23 * out an inode structure, a second one extracts a filename, a third one
24 * returns a symbolic link name, and a fourth one returns the extent number
27 #define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
30 /* This is a way of ensuring that we have something in the system
31 use fields that is compatible with Rock Ridge */
32 #define CHECK_SP(FAIL) \
33 if(rr->u.SP.magic[0] != 0xbe) FAIL; \
34 if(rr->u.SP.magic[1] != 0xef) FAIL; \
35 inode->i_sb->u.isofs_sb.s_rock_offset=rr->u.SP.skip;
36 /* We define a series of macros because each function must do exactly the
37 same thing in certain places. We use the macros to ensure that everything
40 #define CONTINUE_DECLS \
41 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
45 {cont_extent = isonum_733(rr->u.CE.extent); \
46 cont_offset = isonum_733(rr->u.CE.offset); \
47 cont_size = isonum_733(rr->u.CE.size);}
49 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
50 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
52 CHR = ((unsigned char *) DE) + LEN; \
53 LEN = *((unsigned char *) DE) - LEN; \
54 if (inode->i_sb->u.isofs_sb.s_rock_offset!=-1) \
56 LEN-=inode->i_sb->u.isofs_sb.s_rock_offset; \
57 CHR+=inode->i_sb->u.isofs_sb.s_rock_offset; \
62 #define MAYBE_CONTINUE(LABEL,DEV) \
63 {if (buffer) kfree(buffer); \
65 int block, offset, offset1; \
66 struct buffer_head * pbh; \
67 buffer = kmalloc(cont_size,GFP_KERNEL); \
68 if (!buffer) goto out; \
69 block = cont_extent; \
70 offset = cont_offset; \
72 pbh = sb_bread(DEV->i_sb, block); \
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 /* This is the inner layer of the get filename routine, and is called
87 for each system area and continuation record related to the file */
89 int find_rock_ridge_relocation(struct iso_directory_record * de,
90 struct inode * inode) {
98 /* If this is a '..' then we are looking for the parent, otherwise we
99 are looking for the child */
101 if (de->name[0]==1 && de->name_len[0]==1) flag = 1;
102 /* Return value if we do not find appropriate record. */
103 retval = isonum_733 (de->extent);
105 if (!inode->i_sb->u.isofs_sb.s_rock) return retval;
107 SETUP_ROCK_RIDGE(de, chr, len);
111 struct rock_ridge * rr;
113 while (len > 1){ /* There may be one byte for padding somewhere */
114 rr = (struct rock_ridge *) chr;
115 if (rr->len == 0) goto out; /* Something got screwed up here */
116 sig = isonum_721(chr);
122 rrflag = rr->u.RR.flags[0];
123 if (flag && !(rrflag & RR_PL)) goto out;
124 if (!flag && !(rrflag & RR_CL)) goto out;
131 retval = isonum_733(rr->u.CL.location);
137 retval = isonum_733(rr->u.PL.location);
142 CHECK_CE; /* This tells is if there is a continuation record */
149 MAYBE_CONTINUE(repeat, inode);
152 if(buffer) kfree(buffer);
156 /* return length of name field; 0: not found, -1: to be ignored */
157 int get_rock_ridge_filename(struct iso_directory_record * de,
158 char * retname, struct inode * inode)
163 int retnamlen = 0, truncate=0;
165 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
168 SETUP_ROCK_RIDGE(de, chr, len);
171 struct rock_ridge * rr;
174 while (len > 1){ /* There may be one byte for padding somewhere */
175 rr = (struct rock_ridge *) chr;
176 if (rr->len == 0) goto out; /* Something got screwed up here */
177 sig = isonum_721(chr);
183 if((rr->u.RR.flags[0] & RR_NM) == 0) goto out;
194 * If the flags are 2 or 4, this indicates '.' or '..'.
195 * We don't want to do anything with this, because it
196 * screws up the code that calls us. We don't really
197 * care anyways, since we can just use the non-RR
200 if (rr->u.NM.flags & 6) {
204 if (rr->u.NM.flags & ~1) {
205 printk("Unsupported NM flag settings (%d)\n",rr->u.NM.flags);
208 if((strlen(retname) + rr->len - 5) >= 254) {
212 strncat(retname, rr->u.NM.name, rr->len - 5);
213 retnamlen += rr->len - 5;
216 if (buffer) kfree(buffer);
223 MAYBE_CONTINUE(repeat,inode);
224 return retnamlen; /* If 0, this file did not have a NM field */
226 if(buffer) kfree(buffer);
230 int parse_rock_ridge_inode_internal(struct iso_directory_record * de,
231 struct inode * inode,int regard_xa){
237 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
239 SETUP_ROCK_RIDGE(de, chr, len);
250 struct inode * reloc;
251 struct rock_ridge * rr;
254 while (len > 1){ /* There may be one byte for padding somewhere */
255 rr = (struct rock_ridge *) chr;
256 if (rr->len == 0) goto out; /* Something got screwed up here */
257 sig = isonum_721(chr);
262 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
264 if((rr->u.RR.flags[0] &
265 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0) goto out;
275 inode->i_sb->u.isofs_sb.s_rock = 1;
276 printk(KERN_DEBUG "ISO 9660 Extensions: ");
278 for(p=0;p<rr->u.ER.len_id;p++) printk("%c",rr->u.ER.data[p]);
283 inode->i_mode = isonum_733(rr->u.PX.mode);
284 inode->i_nlink = isonum_733(rr->u.PX.n_links);
285 inode->i_uid = isonum_733(rr->u.PX.uid);
286 inode->i_gid = isonum_733(rr->u.PX.gid);
290 high = isonum_733(rr->u.PN.dev_high);
291 low = isonum_733(rr->u.PN.dev_low);
293 * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
294 * then the high field is unused, and the device number is completely
295 * stored in the low field. Some writers may ignore this subtlety,
296 * and as a result we test to see if the entire device number is
297 * stored in the low field, and use that.
299 if((low & ~0xff) && high == 0) {
300 inode->i_rdev = MKDEV(low >> 8, low & 0xff);
302 inode->i_rdev = MKDEV(high, low);
307 /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
308 Try to handle this correctly for either case. */
309 cnt = 0; /* Rock ridge never appears on a High Sierra disk */
310 if(rr->u.TF.flags & TF_CREATE)
311 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
312 if(rr->u.TF.flags & TF_MODIFY)
313 inode->i_mtime = iso_date(rr->u.TF.times[cnt++].time, 0);
314 if(rr->u.TF.flags & TF_ACCESS)
315 inode->i_atime = iso_date(rr->u.TF.times[cnt++].time, 0);
316 if(rr->u.TF.flags & TF_ATTRIBUTES)
317 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
321 struct SL_component * slp;
322 struct SL_component * oldslp;
324 slp = &rr->u.SL.link;
325 inode->i_size = symlink_len;
328 switch(slp->flags &~1){
330 inode->i_size += slp->len;
343 printk("Symlink component flag not implemented\n");
345 slen -= slp->len + 2;
347 slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
350 if( ((rr->u.SL.flags & 1) != 0)
351 && ((oldslp->flags & 1) == 0) ) inode->i_size += 1;
356 * If this component record isn't continued, then append a '/'.
358 if (!rootflag && (oldslp->flags & 1) == 0)
362 symlink_len = inode->i_size;
365 printk(KERN_WARNING "Attempt to read inode for relocated directory\n");
368 inode->u.isofs_i.i_first_extent = isonum_733(rr->u.CL.location);
369 reloc = iget(inode->i_sb,
370 (inode->u.isofs_i.i_first_extent <<
371 inode -> i_sb -> u.isofs_sb.s_log_zone_size));
374 inode->i_mode = reloc->i_mode;
375 inode->i_nlink = reloc->i_nlink;
376 inode->i_uid = reloc->i_uid;
377 inode->i_gid = reloc->i_gid;
378 inode->i_rdev = reloc->i_rdev;
379 inode->i_size = reloc->i_size;
380 inode->i_blocks = reloc->i_blocks;
381 inode->i_atime = reloc->i_atime;
382 inode->i_ctime = reloc->i_ctime;
383 inode->i_mtime = reloc->i_mtime;
388 if ( !inode->i_sb->u.isofs_sb.s_nocompress ) {
390 algo = isonum_721(rr->u.ZF.algorithm);
391 if ( algo == SIG('p','z') ) {
392 int block_shift = isonum_711(&rr->u.ZF.parms[1]);
393 if ( block_shift < PAGE_CACHE_SHIFT || block_shift > 17 ) {
394 printk(KERN_WARNING "isofs: Can't handle ZF block size of 2^%d\n", block_shift);
396 /* Note: we don't change i_blocks here */
397 inode->u.isofs_i.i_file_format = isofs_file_compressed;
398 /* Parameters to compression algorithm (header size, block size) */
399 inode->u.isofs_i.i_format_parm[0] = isonum_711(&rr->u.ZF.parms[0]);
400 inode->u.isofs_i.i_format_parm[1] = isonum_711(&rr->u.ZF.parms[1]);
401 inode->i_size = isonum_733(rr->u.ZF.real_size);
404 printk(KERN_WARNING "isofs: Unknown ZF compression algorithm: %c%c\n",
405 rr->u.ZF.algorithm[0], rr->u.ZF.algorithm[1]);
415 MAYBE_CONTINUE(repeat,inode);
418 if(buffer) kfree(buffer);
422 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr)
426 struct SL_component *oldslp;
427 struct SL_component *slp;
429 slp = &rr->u.SL.link;
432 switch (slp->flags & ~1) {
434 memcpy(rpnt, slp->text, slp->len);
448 printk("Symlink component flag not implemented (%d)\n",
451 slen -= slp->len + 2;
453 slp = (struct SL_component *) ((char *) slp + slp->len + 2);
457 * If there is another SL record, and this component
458 * record isn't continued, then add a slash.
460 if ((!rootflag) && (rr->u.SL.flags & 1) && !(oldslp->flags & 1))
466 * If this component record isn't continued, then append a '/'.
468 if (!rootflag && !(oldslp->flags & 1))
475 int parse_rock_ridge_inode(struct iso_directory_record * de,
476 struct inode * inode)
478 int result=parse_rock_ridge_inode_internal(de,inode,0);
479 /* if rockridge flag was reset and we didn't look for attributes
480 * behind eventual XA attributes, have a look there */
481 if ((inode->i_sb->u.isofs_sb.s_rock_offset==-1)
482 &&(inode->i_sb->u.isofs_sb.s_rock==2))
484 result=parse_rock_ridge_inode_internal(de,inode,14);
489 /* readpage() for symlinks: reads symlink contents into the page and either
490 makes it uptodate and returns 0 or returns error (-EIO) */
492 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
494 struct inode *inode = page->mapping->host;
495 char *link = kmap(page);
496 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
497 unsigned char bufbits = ISOFS_BUFFER_BITS(inode);
498 struct buffer_head *bh;
501 struct iso_directory_record *raw_inode;
507 struct rock_ridge *rr;
509 if (!inode->i_sb->u.isofs_sb.s_rock)
510 panic ("Cannot have symlink with high sierra variant of iso filesystem\n");
512 block = inode->i_ino >> bufbits;
514 bh = sb_bread(inode->i_sb, block);
518 pnt = (unsigned char *) bh->b_data + (inode->i_ino & (bufsize - 1));
520 raw_inode = (struct iso_directory_record *) pnt;
523 * If we go past the end of the buffer, there is some sort of error.
525 if ((inode->i_ino & (bufsize - 1)) + *pnt > bufsize)
528 /* Now test for possible Rock Ridge extensions which will override
529 some of these numbers in the inode structure. */
531 SETUP_ROCK_RIDGE(raw_inode, chr, len);
534 while (len > 1) { /* There may be one byte for padding somewhere */
535 rr = (struct rock_ridge *) chr;
537 goto out; /* Something got screwed up here */
538 sig = isonum_721(chr);
544 if ((rr->u.RR.flags[0] & RR_SL) == 0)
551 rpnt = get_symlink_chunk(rpnt, rr);
554 /* This tells is if there is a continuation record */
560 MAYBE_CONTINUE(repeat, inode);
567 SetPageUptodate(page);
572 /* error exit from macro */
578 printk("unable to read i-node block");
581 printk("symlink spans iso9660 blocks\n");
591 struct address_space_operations isofs_symlink_aops = {
592 readpage: rock_ridge_symlink_readpage