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 struct rock_ridge
*rr
;
98 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
102 SETUP_ROCK_RIDGE(de
, chr
, len
);
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)
141 if (rr
->u
.NM
.flags
& ~1) {
142 printk("Unsupported NM flag settings (%d)\n",
146 if ((strlen(retname
) + rr
->len
- 5) >= 254) {
150 strncat(retname
, rr
->u
.NM
.name
, rr
->len
- 5);
151 retnamlen
+= rr
->len
- 5;
161 MAYBE_CONTINUE(repeat
, inode
);
163 return retnamlen
; /* If 0, this file did not have a NM field */
171 parse_rock_ridge_inode_internal(struct iso_directory_record
*de
,
172 struct inode
*inode
, int regard_xa
)
179 struct rock_ridge
*rr
;
184 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
187 SETUP_ROCK_RIDGE(de
, chr
, len
);
196 while (len
> 2) { /* There may be one byte for padding somewhere */
197 rr
= (struct rock_ridge
*)chr
;
199 goto out
; /* Something got screwed up here */
200 sig
= isonum_721(chr
);
204 goto out
; /* corrupted isofs */
207 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
209 if ((rr
->u
.RR
.flags
[0] &
210 (RR_PX
| RR_TF
| RR_SL
| RR_CL
)) == 0)
221 ISOFS_SB(inode
->i_sb
)->s_rock
= 1;
222 printk(KERN_DEBUG
"ISO 9660 Extensions: ");
225 for (p
= 0; p
< rr
->u
.ER
.len_id
; p
++)
226 printk("%c", rr
->u
.ER
.data
[p
]);
231 inode
->i_mode
= isonum_733(rr
->u
.PX
.mode
);
232 inode
->i_nlink
= isonum_733(rr
->u
.PX
.n_links
);
233 inode
->i_uid
= isonum_733(rr
->u
.PX
.uid
);
234 inode
->i_gid
= isonum_733(rr
->u
.PX
.gid
);
239 high
= isonum_733(rr
->u
.PN
.dev_high
);
240 low
= isonum_733(rr
->u
.PN
.dev_low
);
242 * The Rock Ridge standard specifies that if
243 * sizeof(dev_t) <= 4, then the high field is
244 * unused, and the device number is completely
245 * stored in the low field. Some writers may
246 * ignore this subtlety,
247 * and as a result we test to see if the entire
249 * stored in the low field, and use that.
251 if ((low
& ~0xff) && high
== 0) {
253 MKDEV(low
>> 8, low
& 0xff);
262 * Some RRIP writers incorrectly place ctime in the
263 * TF_CREATE field. Try to handle this correctly for
266 /* Rock ridge never appears on a High Sierra disk */
268 if (rr
->u
.TF
.flags
& TF_CREATE
) {
269 inode
->i_ctime
.tv_sec
=
270 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
272 inode
->i_ctime
.tv_nsec
= 0;
274 if (rr
->u
.TF
.flags
& TF_MODIFY
) {
275 inode
->i_mtime
.tv_sec
=
276 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
278 inode
->i_mtime
.tv_nsec
= 0;
280 if (rr
->u
.TF
.flags
& TF_ACCESS
) {
281 inode
->i_atime
.tv_sec
=
282 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
284 inode
->i_atime
.tv_nsec
= 0;
286 if (rr
->u
.TF
.flags
& TF_ATTRIBUTES
) {
287 inode
->i_ctime
.tv_sec
=
288 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
290 inode
->i_ctime
.tv_nsec
= 0;
296 struct SL_component
*slp
;
297 struct SL_component
*oldslp
;
299 slp
= &rr
->u
.SL
.link
;
300 inode
->i_size
= symlink_len
;
303 switch (slp
->flags
& ~1) {
319 printk("Symlink component flag "
320 "not implemented\n");
322 slen
-= slp
->len
+ 2;
324 slp
= (struct SL_component
*)
325 (((char *)slp
) + slp
->len
+ 2);
339 * If this component record isn't
340 * continued, then append a '/'.
343 && (oldslp
->flags
& 1) == 0)
347 symlink_len
= inode
->i_size
;
350 printk(KERN_WARNING
"Attempt to read inode for "
351 "relocated directory\n");
354 ISOFS_I(inode
)->i_first_extent
=
355 isonum_733(rr
->u
.CL
.location
);
357 isofs_iget(inode
->i_sb
,
358 ISOFS_I(inode
)->i_first_extent
,
362 inode
->i_mode
= reloc
->i_mode
;
363 inode
->i_nlink
= reloc
->i_nlink
;
364 inode
->i_uid
= reloc
->i_uid
;
365 inode
->i_gid
= reloc
->i_gid
;
366 inode
->i_rdev
= reloc
->i_rdev
;
367 inode
->i_size
= reloc
->i_size
;
368 inode
->i_blocks
= reloc
->i_blocks
;
369 inode
->i_atime
= reloc
->i_atime
;
370 inode
->i_ctime
= reloc
->i_ctime
;
371 inode
->i_mtime
= reloc
->i_mtime
;
375 case SIG('Z', 'F'): {
378 if (ISOFS_SB(inode
->i_sb
)->s_nocompress
)
380 algo
= isonum_721(rr
->u
.ZF
.algorithm
);
381 if (algo
== SIG('p', 'z')) {
383 isonum_711(&rr
->u
.ZF
.parms
[1]);
384 if (block_shift
< PAGE_CACHE_SHIFT
385 || block_shift
> 17) {
386 printk(KERN_WARNING
"isofs: "
387 "Can't handle ZF block "
392 * Note: we don't change
395 ISOFS_I(inode
)->i_file_format
=
396 isofs_file_compressed
;
398 * Parameters to compression
399 * algorithm (header size,
402 ISOFS_I(inode
)->i_format_parm
[0] =
403 isonum_711(&rr
->u
.ZF
.parms
[0]);
404 ISOFS_I(inode
)->i_format_parm
[1] =
405 isonum_711(&rr
->u
.ZF
.parms
[1]);
412 "isofs: Unknown ZF compression "
414 rr
->u
.ZF
.algorithm
[0],
415 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
);
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