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>
21 /* These functions are designed to read the system areas of a directory record
22 * and extract relevant information. There are different functions provided
23 * depending upon what information we need at the time. One function fills
24 * out an inode structure, a second one extracts a filename, a third one
25 * returns a symbolic link name, and a fourth one returns the extent number
28 #define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
31 /* This is a way of ensuring that we have something in the system
32 use fields that is compatible with Rock Ridge */
33 #define CHECK_SP(FAIL) \
34 if(rr->u.SP.magic[0] != 0xbe) FAIL; \
35 if(rr->u.SP.magic[1] != 0xef) FAIL; \
36 inode->i_sb->u.isofs_sb.s_rock_offset=rr->u.SP.skip;
37 /* We define a series of macros because each function must do exactly the
38 same thing in certain places. We use the macros to ensure that everything
41 #define CONTINUE_DECLS \
42 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
46 {cont_extent = isonum_733(rr->u.CE.extent); \
47 cont_offset = isonum_733(rr->u.CE.offset); \
48 cont_size = isonum_733(rr->u.CE.size);}
50 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
51 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
53 CHR = ((unsigned char *) DE) + LEN; \
54 LEN = *((unsigned char *) DE) - LEN; \
56 if (inode->i_sb->u.isofs_sb.s_rock_offset!=-1) \
58 LEN-=inode->i_sb->u.isofs_sb.s_rock_offset; \
59 CHR+=inode->i_sb->u.isofs_sb.s_rock_offset; \
64 #define MAYBE_CONTINUE(LABEL,DEV) \
65 {if (buffer) kfree(buffer); \
67 int block, offset, offset1; \
68 struct buffer_head * pbh; \
69 buffer = kmalloc(cont_size,GFP_KERNEL); \
70 if (!buffer) goto out; \
71 block = cont_extent; \
72 offset = cont_offset; \
74 pbh = sb_bread(DEV->i_sb, block); \
76 if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
80 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
82 chr = (unsigned char *) buffer; \
89 printk("Unable to read rock-ridge attributes\n"); \
92 /* This is the inner layer of the get filename routine, and is called
93 for each system area and continuation record related to the file */
95 int find_rock_ridge_relocation(struct iso_directory_record * de,
96 struct inode * inode) {
104 /* If this is a '..' then we are looking for the parent, otherwise we
105 are looking for the child */
107 if (de->name[0]==1 && de->name_len[0]==1) flag = 1;
108 /* Return value if we do not find appropriate record. */
109 retval = isonum_733 (de->extent);
111 if (!inode->i_sb->u.isofs_sb.s_rock) return retval;
113 SETUP_ROCK_RIDGE(de, chr, len);
117 struct rock_ridge * rr;
119 while (len > 1){ /* There may be one byte for padding somewhere */
120 rr = (struct rock_ridge *) chr;
121 if (rr->len == 0) goto out; /* Something got screwed up here */
122 sig = isonum_721(chr);
128 rrflag = rr->u.RR.flags[0];
129 if (flag && !(rrflag & RR_PL)) goto out;
130 if (!flag && !(rrflag & RR_CL)) goto out;
137 retval = isonum_733(rr->u.CL.location);
143 retval = isonum_733(rr->u.PL.location);
148 CHECK_CE; /* This tells is if there is a continuation record */
155 MAYBE_CONTINUE(repeat, inode);
158 if(buffer) kfree(buffer);
162 /* return length of name field; 0: not found, -1: to be ignored */
163 int get_rock_ridge_filename(struct iso_directory_record * de,
164 char * retname, struct inode * inode)
169 int retnamlen = 0, truncate=0;
171 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
174 SETUP_ROCK_RIDGE(de, chr, len);
177 struct rock_ridge * rr;
180 while (len > 2){ /* There may be one byte for padding somewhere */
181 rr = (struct rock_ridge *) chr;
182 if (rr->len < 3) goto out; /* Something got screwed up here */
183 sig = isonum_721(chr);
186 if (len < 0) goto out; /* corrupted isofs */
190 if((rr->u.RR.flags[0] & RR_NM) == 0) goto out;
200 if (rr->len < 5) break;
202 * If the flags are 2 or 4, this indicates '.' or '..'.
203 * We don't want to do anything with this, because it
204 * screws up the code that calls us. We don't really
205 * care anyways, since we can just use the non-RR
208 if (rr->u.NM.flags & 6) {
212 if (rr->u.NM.flags & ~1) {
213 printk("Unsupported NM flag settings (%d)\n",rr->u.NM.flags);
216 if((strlen(retname) + rr->len - 5) >= 254) {
220 strncat(retname, rr->u.NM.name, rr->len - 5);
221 retnamlen += rr->len - 5;
224 if (buffer) kfree(buffer);
231 MAYBE_CONTINUE(repeat,inode);
232 return retnamlen; /* If 0, this file did not have a NM field */
234 if(buffer) kfree(buffer);
238 int parse_rock_ridge_inode_internal(struct iso_directory_record * de,
239 struct inode * inode,int regard_xa){
245 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
247 SETUP_ROCK_RIDGE(de, chr, len);
258 struct inode * reloc;
259 struct rock_ridge * rr;
262 while (len > 2){ /* There may be one byte for padding somewhere */
263 rr = (struct rock_ridge *) chr;
264 if (rr->len < 3) goto out; /* Something got screwed up here */
265 sig = isonum_721(chr);
268 if (len < 0) goto out; /* corrupted isofs */
271 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
273 if((rr->u.RR.flags[0] &
274 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0) goto out;
284 inode->i_sb->u.isofs_sb.s_rock = 1;
285 printk(KERN_DEBUG "ISO 9660 Extensions: ");
287 for(p=0;p<rr->u.ER.len_id;p++) printk("%c",rr->u.ER.data[p]);
292 inode->i_mode = isonum_733(rr->u.PX.mode);
293 inode->i_nlink = isonum_733(rr->u.PX.n_links);
294 inode->i_uid = isonum_733(rr->u.PX.uid);
295 inode->i_gid = isonum_733(rr->u.PX.gid);
299 high = isonum_733(rr->u.PN.dev_high);
300 low = isonum_733(rr->u.PN.dev_low);
302 * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
303 * then the high field is unused, and the device number is completely
304 * stored in the low field. Some writers may ignore this subtlety,
305 * and as a result we test to see if the entire device number is
306 * stored in the low field, and use that.
308 if((low & ~0xff) && high == 0) {
309 inode->i_rdev = MKDEV(low >> 8, low & 0xff);
311 inode->i_rdev = MKDEV(high, low);
316 /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
317 Try to handle this correctly for either case. */
318 cnt = 0; /* Rock ridge never appears on a High Sierra disk */
319 if(rr->u.TF.flags & TF_CREATE)
320 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
321 if(rr->u.TF.flags & TF_MODIFY)
322 inode->i_mtime = iso_date(rr->u.TF.times[cnt++].time, 0);
323 if(rr->u.TF.flags & TF_ACCESS)
324 inode->i_atime = iso_date(rr->u.TF.times[cnt++].time, 0);
325 if(rr->u.TF.flags & TF_ATTRIBUTES)
326 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
330 struct SL_component * slp;
331 struct SL_component * oldslp;
333 slp = &rr->u.SL.link;
334 inode->i_size = symlink_len;
337 switch(slp->flags &~1){
339 inode->i_size += slp->len;
352 printk("Symlink component flag not implemented\n");
354 slen -= slp->len + 2;
356 slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
359 if( ((rr->u.SL.flags & 1) != 0)
360 && ((oldslp->flags & 1) == 0) ) inode->i_size += 1;
365 * If this component record isn't continued, then append a '/'.
367 if (!rootflag && (oldslp->flags & 1) == 0)
371 symlink_len = inode->i_size;
374 printk(KERN_WARNING "Attempt to read inode for relocated directory\n");
377 inode->u.isofs_i.i_first_extent = isonum_733(rr->u.CL.location);
378 reloc = iget(inode->i_sb,
379 (inode->u.isofs_i.i_first_extent <<
380 inode -> i_sb -> u.isofs_sb.s_log_zone_size));
383 inode->i_mode = reloc->i_mode;
384 inode->i_nlink = reloc->i_nlink;
385 inode->i_uid = reloc->i_uid;
386 inode->i_gid = reloc->i_gid;
387 inode->i_rdev = reloc->i_rdev;
388 inode->i_size = reloc->i_size;
389 inode->i_blocks = reloc->i_blocks;
390 inode->i_atime = reloc->i_atime;
391 inode->i_ctime = reloc->i_ctime;
392 inode->i_mtime = reloc->i_mtime;
397 if ( !inode->i_sb->u.isofs_sb.s_nocompress ) {
399 algo = isonum_721(rr->u.ZF.algorithm);
400 if ( algo == SIG('p','z') ) {
401 int block_shift = isonum_711(&rr->u.ZF.parms[1]);
402 if ( block_shift < PAGE_CACHE_SHIFT || block_shift > 17 ) {
403 printk(KERN_WARNING "isofs: Can't handle ZF block size of 2^%d\n", block_shift);
405 /* Note: we don't change i_blocks here */
406 inode->u.isofs_i.i_file_format = isofs_file_compressed;
407 /* Parameters to compression algorithm (header size, block size) */
408 inode->u.isofs_i.i_format_parm[0] = isonum_711(&rr->u.ZF.parms[0]);
409 inode->u.isofs_i.i_format_parm[1] = isonum_711(&rr->u.ZF.parms[1]);
410 inode->i_size = isonum_733(rr->u.ZF.real_size);
413 printk(KERN_WARNING "isofs: Unknown ZF compression algorithm: %c%c\n",
414 rr->u.ZF.algorithm[0], rr->u.ZF.algorithm[1]);
424 MAYBE_CONTINUE(repeat,inode);
427 if(buffer) kfree(buffer);
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,
500 struct inode * inode)
502 int result=parse_rock_ridge_inode_internal(de,inode,0);
503 /* if rockridge flag was reset and we didn't look for attributes
504 * behind eventual XA attributes, have a look there */
505 if ((inode->i_sb->u.isofs_sb.s_rock_offset==-1)
506 &&(inode->i_sb->u.isofs_sb.s_rock==2))
508 result=parse_rock_ridge_inode_internal(de,inode,14);
513 /* readpage() for symlinks: reads symlink contents into the page and either
514 makes it uptodate and returns 0 or returns error (-EIO) */
516 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
518 struct inode *inode = page->mapping->host;
519 char *link = kmap(page);
520 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
521 unsigned char bufbits = ISOFS_BUFFER_BITS(inode);
522 struct buffer_head *bh;
525 struct iso_directory_record *raw_inode;
531 struct rock_ridge *rr;
533 if (!inode->i_sb->u.isofs_sb.s_rock)
536 block = inode->i_ino >> bufbits;
538 bh = sb_bread(inode->i_sb, block);
542 pnt = (unsigned char *) bh->b_data + (inode->i_ino & (bufsize - 1));
544 raw_inode = (struct iso_directory_record *) pnt;
547 * If we go past the end of the buffer, there is some sort of error.
549 if ((inode->i_ino & (bufsize - 1)) + *pnt > bufsize)
552 /* Now test for possible Rock Ridge extensions which will override
553 some of these numbers in the inode structure. */
555 SETUP_ROCK_RIDGE(raw_inode, chr, len);
558 while (len > 2) { /* There may be one byte for padding somewhere */
559 rr = (struct rock_ridge *) chr;
561 goto out; /* Something got screwed up here */
562 sig = isonum_721(chr);
566 goto out; /* corrupted isofs */
570 if ((rr->u.RR.flags[0] & RR_SL) == 0)
577 rpnt = get_symlink_chunk(rpnt, rr,
578 link + (PAGE_SIZE - 1));
583 /* This tells is if there is a continuation record */
589 MAYBE_CONTINUE(repeat, inode);
596 SetPageUptodate(page);
601 /* error exit from macro */
607 printk("unable to read i-node block");
610 printk("symlink spans iso9660 blocks\n");
621 struct address_space_operations isofs_symlink_aops = {
622 readpage: rock_ridge_symlink_readpage