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 */
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
36 #define CONTINUE_DECLS \
37 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
41 {cont_extent = isonum_733(rr->u.CE.extent); \
42 cont_offset = isonum_733(rr->u.CE.offset); \
43 cont_size = isonum_733(rr->u.CE.size);}
45 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
46 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
48 CHR = ((unsigned char *) DE) + LEN; \
49 LEN = *((unsigned char *) DE) - LEN; \
51 if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
53 LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
54 CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
59 #define MAYBE_CONTINUE(LABEL,DEV) \
60 {if (buffer) { kfree(buffer); buffer = NULL; } \
62 int block, offset, offset1; \
63 struct buffer_head * pbh; \
64 buffer = kmalloc(cont_size,GFP_KERNEL); \
65 if (!buffer) goto out; \
66 block = cont_extent; \
67 offset = cont_offset; \
69 pbh = sb_bread(DEV->i_sb, block); \
71 if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
75 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
77 chr = (unsigned char *) buffer; \
84 printk("Unable to read rock-ridge attributes\n"); \
87 /* return length of name field; 0: not found, -1: to be ignored */
88 int get_rock_ridge_filename(struct iso_directory_record
* de
,
89 char * retname
, struct inode
* inode
)
94 int retnamlen
= 0, truncate
=0;
96 if (!ISOFS_SB(inode
->i_sb
)->s_rock
) return 0;
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
;
107 if (rr
->len
< 3) goto out
; /* Something got screwed up here */
108 sig
= isonum_721(chr
);
111 if (len
< 0) goto out
; /* corrupted isofs */
115 if((rr
->u
.RR
.flags
[0] & RR_NM
) == 0) goto out
;
125 if (rr
->len
< 5) break;
127 * If the flags are 2 or 4, this indicates '.' or '..'.
128 * We don't want to do anything with this, because it
129 * screws up the code that calls us. We don't really
130 * care anyways, since we can just use the non-RR
133 if (rr
->u
.NM
.flags
& 6) {
137 if (rr
->u
.NM
.flags
& ~1) {
138 printk("Unsupported NM flag settings (%d)\n",rr
->u
.NM
.flags
);
141 if((strlen(retname
) + rr
->len
- 5) >= 254) {
145 strncat(retname
, rr
->u
.NM
.name
, rr
->len
- 5);
146 retnamlen
+= rr
->len
- 5;
149 if (buffer
) kfree(buffer
);
156 MAYBE_CONTINUE(repeat
,inode
);
157 if (buffer
) kfree(buffer
);
158 return retnamlen
; /* If 0, this file did not have a NM field */
160 if(buffer
) kfree(buffer
);
165 parse_rock_ridge_inode_internal(struct iso_directory_record
*de
,
166 struct inode
*inode
, int regard_xa
)
173 if (!ISOFS_SB(inode
->i_sb
)->s_rock
) return 0;
175 SETUP_ROCK_RIDGE(de
, chr
, len
);
186 struct inode
* reloc
;
187 struct rock_ridge
* rr
;
190 while (len
> 2){ /* There may be one byte for padding somewhere */
191 rr
= (struct rock_ridge
*) chr
;
192 if (rr
->len
< 3) goto out
; /* Something got screwed up here */
193 sig
= isonum_721(chr
);
196 if (len
< 0) goto out
; /* corrupted isofs */
199 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
201 if((rr
->u
.RR
.flags
[0] &
202 (RR_PX
| RR_TF
| RR_SL
| RR_CL
)) == 0) goto out
;
212 ISOFS_SB(inode
->i_sb
)->s_rock
= 1;
213 printk(KERN_DEBUG
"ISO 9660 Extensions: ");
215 for(p
=0;p
<rr
->u
.ER
.len_id
;p
++) printk("%c",rr
->u
.ER
.data
[p
]);
220 inode
->i_mode
= isonum_733(rr
->u
.PX
.mode
);
221 inode
->i_nlink
= isonum_733(rr
->u
.PX
.n_links
);
222 inode
->i_uid
= isonum_733(rr
->u
.PX
.uid
);
223 inode
->i_gid
= isonum_733(rr
->u
.PX
.gid
);
227 high
= isonum_733(rr
->u
.PN
.dev_high
);
228 low
= isonum_733(rr
->u
.PN
.dev_low
);
230 * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
231 * then the high field is unused, and the device number is completely
232 * stored in the low field. Some writers may ignore this subtlety,
233 * and as a result we test to see if the entire device number is
234 * stored in the low field, and use that.
236 if((low
& ~0xff) && high
== 0) {
237 inode
->i_rdev
= MKDEV(low
>> 8, low
& 0xff);
239 inode
->i_rdev
= MKDEV(high
, low
);
244 /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
245 Try to handle this correctly for either case. */
246 cnt
= 0; /* Rock ridge never appears on a High Sierra disk */
247 if(rr
->u
.TF
.flags
& TF_CREATE
) {
248 inode
->i_ctime
.tv_sec
= iso_date(rr
->u
.TF
.times
[cnt
++].time
, 0);
249 inode
->i_ctime
.tv_nsec
= 0;
251 if(rr
->u
.TF
.flags
& TF_MODIFY
) {
252 inode
->i_mtime
.tv_sec
= iso_date(rr
->u
.TF
.times
[cnt
++].time
, 0);
253 inode
->i_mtime
.tv_nsec
= 0;
255 if(rr
->u
.TF
.flags
& TF_ACCESS
) {
256 inode
->i_atime
.tv_sec
= iso_date(rr
->u
.TF
.times
[cnt
++].time
, 0);
257 inode
->i_atime
.tv_nsec
= 0;
259 if(rr
->u
.TF
.flags
& TF_ATTRIBUTES
) {
260 inode
->i_ctime
.tv_sec
= iso_date(rr
->u
.TF
.times
[cnt
++].time
, 0);
261 inode
->i_ctime
.tv_nsec
= 0;
266 struct SL_component
* slp
;
267 struct SL_component
* oldslp
;
269 slp
= &rr
->u
.SL
.link
;
270 inode
->i_size
= symlink_len
;
273 switch(slp
->flags
&~1){
275 inode
->i_size
+= slp
->len
;
288 printk("Symlink component flag not implemented\n");
290 slen
-= slp
->len
+ 2;
292 slp
= (struct SL_component
*) (((char *) slp
) + slp
->len
+ 2);
295 if( ((rr
->u
.SL
.flags
& 1) != 0)
296 && ((oldslp
->flags
& 1) == 0) ) inode
->i_size
+= 1;
301 * If this component record isn't continued, then append a '/'.
303 if (!rootflag
&& (oldslp
->flags
& 1) == 0)
307 symlink_len
= inode
->i_size
;
310 printk(KERN_WARNING
"Attempt to read inode for relocated directory\n");
313 ISOFS_I(inode
)->i_first_extent
= isonum_733(rr
->u
.CL
.location
);
314 reloc
= isofs_iget(inode
->i_sb
, ISOFS_I(inode
)->i_first_extent
, 0);
317 inode
->i_mode
= reloc
->i_mode
;
318 inode
->i_nlink
= reloc
->i_nlink
;
319 inode
->i_uid
= reloc
->i_uid
;
320 inode
->i_gid
= reloc
->i_gid
;
321 inode
->i_rdev
= reloc
->i_rdev
;
322 inode
->i_size
= reloc
->i_size
;
323 inode
->i_blocks
= reloc
->i_blocks
;
324 inode
->i_atime
= reloc
->i_atime
;
325 inode
->i_ctime
= reloc
->i_ctime
;
326 inode
->i_mtime
= reloc
->i_mtime
;
331 if ( !ISOFS_SB(inode
->i_sb
)->s_nocompress
) {
333 algo
= isonum_721(rr
->u
.ZF
.algorithm
);
334 if ( algo
== SIG('p','z') ) {
335 int block_shift
= isonum_711(&rr
->u
.ZF
.parms
[1]);
336 if ( block_shift
< PAGE_CACHE_SHIFT
|| block_shift
> 17 ) {
337 printk(KERN_WARNING
"isofs: Can't handle ZF block size of 2^%d\n", block_shift
);
339 /* Note: we don't change i_blocks here */
340 ISOFS_I(inode
)->i_file_format
= isofs_file_compressed
;
341 /* Parameters to compression algorithm (header size, block size) */
342 ISOFS_I(inode
)->i_format_parm
[0] = isonum_711(&rr
->u
.ZF
.parms
[0]);
343 ISOFS_I(inode
)->i_format_parm
[1] = isonum_711(&rr
->u
.ZF
.parms
[1]);
344 inode
->i_size
= isonum_733(rr
->u
.ZF
.real_size
);
347 printk(KERN_WARNING
"isofs: Unknown ZF compression algorithm: %c%c\n",
348 rr
->u
.ZF
.algorithm
[0], rr
->u
.ZF
.algorithm
[1]);
358 MAYBE_CONTINUE(repeat
,inode
);
360 if(buffer
) kfree(buffer
);
364 static char *get_symlink_chunk(char *rpnt
, struct rock_ridge
*rr
, char *plimit
)
368 struct SL_component
*oldslp
;
369 struct SL_component
*slp
;
371 slp
= &rr
->u
.SL
.link
;
374 switch (slp
->flags
& ~1) {
376 if (slp
->len
> plimit
- rpnt
)
378 memcpy(rpnt
, slp
->text
, slp
->len
);
387 if (2 > plimit
- rpnt
)
399 printk("Symlink component flag not implemented (%d)\n",
402 slen
-= slp
->len
+ 2;
404 slp
= (struct SL_component
*) ((char *) slp
+ slp
->len
+ 2);
408 * If there is another SL record, and this component
409 * record isn't continued, then add a slash.
411 if ((!rootflag
) && (rr
->u
.SL
.flags
& 1) &&
412 !(oldslp
->flags
& 1)) {
421 * If this component record isn't continued, then append a '/'.
423 if (!rootflag
&& !(oldslp
->flags
& 1)) {
432 int parse_rock_ridge_inode(struct iso_directory_record
* de
,
433 struct inode
* inode
)
435 int result
=parse_rock_ridge_inode_internal(de
,inode
,0);
436 /* if rockridge flag was reset and we didn't look for attributes
437 * behind eventual XA attributes, have a look there */
438 if ((ISOFS_SB(inode
->i_sb
)->s_rock_offset
==-1)
439 &&(ISOFS_SB(inode
->i_sb
)->s_rock
==2))
441 result
=parse_rock_ridge_inode_internal(de
,inode
,14);
446 /* readpage() for symlinks: reads symlink contents into the page and either
447 makes it uptodate and returns 0 or returns error (-EIO) */
449 static int rock_ridge_symlink_readpage(struct file
*file
, struct page
*page
)
451 struct inode
*inode
= page
->mapping
->host
;
452 struct iso_inode_info
*ei
= ISOFS_I(inode
);
453 char *link
= kmap(page
);
454 unsigned long bufsize
= ISOFS_BUFFER_SIZE(inode
);
455 struct buffer_head
*bh
;
458 struct iso_directory_record
*raw_inode
;
460 unsigned long block
, offset
;
464 struct rock_ridge
*rr
;
466 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
469 block
= ei
->i_iget5_block
;
471 bh
= sb_bread(inode
->i_sb
, block
);
475 offset
= ei
->i_iget5_offset
;
476 pnt
= (unsigned char *) bh
->b_data
+ offset
;
478 raw_inode
= (struct iso_directory_record
*) pnt
;
481 * If we go past the end of the buffer, there is some sort of error.
483 if (offset
+ *pnt
> bufsize
)
486 /* Now test for possible Rock Ridge extensions which will override
487 some of these numbers in the inode structure. */
489 SETUP_ROCK_RIDGE(raw_inode
, chr
, len
);
492 while (len
> 2) { /* There may be one byte for padding somewhere */
493 rr
= (struct rock_ridge
*) chr
;
495 goto out
; /* Something got screwed up here */
496 sig
= isonum_721(chr
);
500 goto out
; /* corrupted isofs */
504 if ((rr
->u
.RR
.flags
[0] & RR_SL
) == 0)
511 rpnt
= get_symlink_chunk(rpnt
, rr
,
512 link
+ (PAGE_SIZE
- 1));
517 /* This tells is if there is a continuation record */
523 MAYBE_CONTINUE(repeat
, inode
);
532 SetPageUptodate(page
);
537 /* error exit from macro */
543 printk("unable to read i-node block");
546 printk("symlink spans iso9660 blocks\n");
557 struct address_space_operations isofs_symlink_aops
= {
558 .readpage
= rock_ridge_symlink_readpage