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 /* We define a series of macros because each function must do exactly the
41 same thing in certain places. We use the macros to ensure that everything
44 #define CONTINUE_DECLS \
45 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
49 {cont_extent = isonum_733(rr->u.CE.extent); \
50 cont_offset = isonum_733(rr->u.CE.offset); \
51 cont_size = isonum_733(rr->u.CE.size);}
53 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
54 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
56 CHR = ((unsigned char *) DE) + LEN; \
57 LEN = *((unsigned char *) DE) - LEN; \
59 if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
61 LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
62 CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
67 #define MAYBE_CONTINUE(LABEL,DEV) \
68 {if (buffer) { kfree(buffer); buffer = NULL; } \
70 int block, offset, offset1; \
71 struct buffer_head * pbh; \
72 buffer = kmalloc(cont_size,GFP_KERNEL); \
73 if (!buffer) goto out; \
74 block = cont_extent; \
75 offset = cont_offset; \
77 pbh = sb_bread(DEV->i_sb, block); \
79 if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
83 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
85 chr = (unsigned char *) buffer; \
92 printk("Unable to read rock-ridge attributes\n"); \
95 /* return length of name field; 0: not found, -1: to be ignored */
96 int get_rock_ridge_filename(struct iso_directory_record
*de
,
97 char *retname
, struct inode
*inode
)
102 struct rock_ridge
*rr
;
107 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
111 SETUP_ROCK_RIDGE(de
, chr
, len
);
114 while (len
> 2) { /* There may be one byte for padding somewhere */
115 rr
= (struct rock_ridge
*)chr
;
117 goto out
; /* Something got screwed up here */
118 sig
= isonum_721(chr
);
122 goto out
; /* corrupted isofs */
126 if ((rr
->u
.RR
.flags
[0] & RR_NM
) == 0)
130 if (check_sp(rr
, inode
))
142 * If the flags are 2 or 4, this indicates '.' or '..'.
143 * We don't want to do anything with this, because it
144 * screws up the code that calls us. We don't really
145 * care anyways, since we can just use the non-RR
148 if (rr
->u
.NM
.flags
& 6)
151 if (rr
->u
.NM
.flags
& ~1) {
152 printk("Unsupported NM flag settings (%d)\n",
156 if ((strlen(retname
) + rr
->len
- 5) >= 254) {
160 strncat(retname
, rr
->u
.NM
.name
, rr
->len
- 5);
161 retnamlen
+= rr
->len
- 5;
171 MAYBE_CONTINUE(repeat
, inode
);
173 return retnamlen
; /* If 0, this file did not have a NM field */
181 parse_rock_ridge_inode_internal(struct iso_directory_record
*de
,
182 struct inode
*inode
, int regard_xa
)
189 struct rock_ridge
*rr
;
194 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
197 SETUP_ROCK_RIDGE(de
, chr
, len
);
206 while (len
> 2) { /* There may be one byte for padding somewhere */
207 rr
= (struct rock_ridge
*)chr
;
209 goto out
; /* Something got screwed up here */
210 sig
= isonum_721(chr
);
214 goto out
; /* corrupted isofs */
217 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
219 if ((rr
->u
.RR
.flags
[0] &
220 (RR_PX
| RR_TF
| RR_SL
| RR_CL
)) == 0)
225 if (check_sp(rr
, inode
))
232 ISOFS_SB(inode
->i_sb
)->s_rock
= 1;
233 printk(KERN_DEBUG
"ISO 9660 Extensions: ");
236 for (p
= 0; p
< rr
->u
.ER
.len_id
; p
++)
237 printk("%c", rr
->u
.ER
.data
[p
]);
242 inode
->i_mode
= isonum_733(rr
->u
.PX
.mode
);
243 inode
->i_nlink
= isonum_733(rr
->u
.PX
.n_links
);
244 inode
->i_uid
= isonum_733(rr
->u
.PX
.uid
);
245 inode
->i_gid
= isonum_733(rr
->u
.PX
.gid
);
250 high
= isonum_733(rr
->u
.PN
.dev_high
);
251 low
= isonum_733(rr
->u
.PN
.dev_low
);
253 * The Rock Ridge standard specifies that if
254 * sizeof(dev_t) <= 4, then the high field is
255 * unused, and the device number is completely
256 * stored in the low field. Some writers may
257 * ignore this subtlety,
258 * and as a result we test to see if the entire
260 * stored in the low field, and use that.
262 if ((low
& ~0xff) && high
== 0) {
264 MKDEV(low
>> 8, low
& 0xff);
273 * Some RRIP writers incorrectly place ctime in the
274 * TF_CREATE field. Try to handle this correctly for
277 /* Rock ridge never appears on a High Sierra disk */
279 if (rr
->u
.TF
.flags
& TF_CREATE
) {
280 inode
->i_ctime
.tv_sec
=
281 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
283 inode
->i_ctime
.tv_nsec
= 0;
285 if (rr
->u
.TF
.flags
& TF_MODIFY
) {
286 inode
->i_mtime
.tv_sec
=
287 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
289 inode
->i_mtime
.tv_nsec
= 0;
291 if (rr
->u
.TF
.flags
& TF_ACCESS
) {
292 inode
->i_atime
.tv_sec
=
293 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
295 inode
->i_atime
.tv_nsec
= 0;
297 if (rr
->u
.TF
.flags
& TF_ATTRIBUTES
) {
298 inode
->i_ctime
.tv_sec
=
299 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
301 inode
->i_ctime
.tv_nsec
= 0;
307 struct SL_component
*slp
;
308 struct SL_component
*oldslp
;
310 slp
= &rr
->u
.SL
.link
;
311 inode
->i_size
= symlink_len
;
314 switch (slp
->flags
& ~1) {
330 printk("Symlink component flag "
331 "not implemented\n");
333 slen
-= slp
->len
+ 2;
335 slp
= (struct SL_component
*)
336 (((char *)slp
) + slp
->len
+ 2);
350 * If this component record isn't
351 * continued, then append a '/'.
354 && (oldslp
->flags
& 1) == 0)
358 symlink_len
= inode
->i_size
;
361 printk(KERN_WARNING
"Attempt to read inode for "
362 "relocated directory\n");
365 ISOFS_I(inode
)->i_first_extent
=
366 isonum_733(rr
->u
.CL
.location
);
368 isofs_iget(inode
->i_sb
,
369 ISOFS_I(inode
)->i_first_extent
,
373 inode
->i_mode
= reloc
->i_mode
;
374 inode
->i_nlink
= reloc
->i_nlink
;
375 inode
->i_uid
= reloc
->i_uid
;
376 inode
->i_gid
= reloc
->i_gid
;
377 inode
->i_rdev
= reloc
->i_rdev
;
378 inode
->i_size
= reloc
->i_size
;
379 inode
->i_blocks
= reloc
->i_blocks
;
380 inode
->i_atime
= reloc
->i_atime
;
381 inode
->i_ctime
= reloc
->i_ctime
;
382 inode
->i_mtime
= reloc
->i_mtime
;
386 case SIG('Z', 'F'): {
389 if (ISOFS_SB(inode
->i_sb
)->s_nocompress
)
391 algo
= isonum_721(rr
->u
.ZF
.algorithm
);
392 if (algo
== SIG('p', 'z')) {
394 isonum_711(&rr
->u
.ZF
.parms
[1]);
395 if (block_shift
< PAGE_CACHE_SHIFT
396 || block_shift
> 17) {
397 printk(KERN_WARNING
"isofs: "
398 "Can't handle ZF block "
403 * Note: we don't change
406 ISOFS_I(inode
)->i_file_format
=
407 isofs_file_compressed
;
409 * Parameters to compression
410 * algorithm (header size,
413 ISOFS_I(inode
)->i_format_parm
[0] =
414 isonum_711(&rr
->u
.ZF
.parms
[0]);
415 ISOFS_I(inode
)->i_format_parm
[1] =
416 isonum_711(&rr
->u
.ZF
.parms
[1]);
423 "isofs: Unknown ZF compression "
425 rr
->u
.ZF
.algorithm
[0],
426 rr
->u
.ZF
.algorithm
[1]);
435 MAYBE_CONTINUE(repeat
, inode
);
442 static char *get_symlink_chunk(char *rpnt
, struct rock_ridge
*rr
, char *plimit
)
446 struct SL_component
*oldslp
;
447 struct SL_component
*slp
;
449 slp
= &rr
->u
.SL
.link
;
452 switch (slp
->flags
& ~1) {
454 if (slp
->len
> plimit
- rpnt
)
456 memcpy(rpnt
, slp
->text
, slp
->len
);
465 if (2 > plimit
- rpnt
)
477 printk("Symlink component flag not implemented (%d)\n",
480 slen
-= slp
->len
+ 2;
482 slp
= (struct SL_component
*)((char *)slp
+ slp
->len
+ 2);
486 * If there is another SL record, and this component
487 * record isn't continued, then add a slash.
489 if ((!rootflag
) && (rr
->u
.SL
.flags
& 1) &&
490 !(oldslp
->flags
& 1)) {
499 * If this component record isn't continued, then append a '/'.
501 if (!rootflag
&& !(oldslp
->flags
& 1)) {
510 int parse_rock_ridge_inode(struct iso_directory_record
*de
, struct inode
*inode
)
512 int result
= parse_rock_ridge_inode_internal(de
, inode
, 0);
513 /* if rockridge flag was reset and we didn't look for attributes
514 * behind eventual XA attributes, have a look there */
515 if ((ISOFS_SB(inode
->i_sb
)->s_rock_offset
== -1)
516 && (ISOFS_SB(inode
->i_sb
)->s_rock
== 2)) {
517 result
= parse_rock_ridge_inode_internal(de
, inode
, 14);
522 /* readpage() for symlinks: reads symlink contents into the page and either
523 makes it uptodate and returns 0 or returns error (-EIO) */
525 static int rock_ridge_symlink_readpage(struct file
*file
, struct page
*page
)
527 struct inode
*inode
= page
->mapping
->host
;
528 struct iso_inode_info
*ei
= ISOFS_I(inode
);
529 char *link
= kmap(page
);
530 unsigned long bufsize
= ISOFS_BUFFER_SIZE(inode
);
531 struct buffer_head
*bh
;
534 struct iso_directory_record
*raw_inode
;
536 unsigned long block
, offset
;
540 struct rock_ridge
*rr
;
542 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
545 block
= ei
->i_iget5_block
;
547 bh
= sb_bread(inode
->i_sb
, block
);
551 offset
= ei
->i_iget5_offset
;
552 pnt
= (unsigned char *)bh
->b_data
+ offset
;
554 raw_inode
= (struct iso_directory_record
*)pnt
;
557 * If we go past the end of the buffer, there is some sort of error.
559 if (offset
+ *pnt
> bufsize
)
562 /* Now test for possible Rock Ridge extensions which will override
563 some of these numbers in the inode structure. */
565 SETUP_ROCK_RIDGE(raw_inode
, chr
, len
);
568 while (len
> 2) { /* There may be one byte for padding somewhere */
569 rr
= (struct rock_ridge
*)chr
;
571 goto out
; /* Something got screwed up here */
572 sig
= isonum_721(chr
);
576 goto out
; /* corrupted isofs */
580 if ((rr
->u
.RR
.flags
[0] & RR_SL
) == 0)
584 if (check_sp(rr
, inode
))
588 rpnt
= get_symlink_chunk(rpnt
, rr
,
589 link
+ (PAGE_SIZE
- 1));
594 /* This tells is if there is a continuation record */
600 MAYBE_CONTINUE(repeat
, inode
);
608 SetPageUptodate(page
);
613 /* error exit from macro */
619 printk("unable to read i-node block");
622 printk("symlink spans iso9660 blocks\n");
633 struct address_space_operations isofs_symlink_aops
= {
634 .readpage
= rock_ridge_symlink_readpage