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
;
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
)
97 struct rock_ridge
*rr
;
102 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
106 SETUP_ROCK_RIDGE(de
, chr
, len
);
109 while (len
> 2) { /* There may be one byte for padding somewhere */
110 rr
= (struct rock_ridge
*)chr
;
112 goto out
; /* Something got screwed up here */
113 sig
= isonum_721(chr
);
117 goto out
; /* corrupted isofs */
121 if ((rr
->u
.RR
.flags
[0] & RR_NM
) == 0)
125 if (check_sp(rr
, inode
))
137 * If the flags are 2 or 4, this indicates '.' or '..'.
138 * We don't want to do anything with this, because it
139 * screws up the code that calls us. We don't really
140 * care anyways, since we can just use the non-RR
143 if (rr
->u
.NM
.flags
& 6)
146 if (rr
->u
.NM
.flags
& ~1) {
147 printk("Unsupported NM flag settings (%d)\n",
151 if ((strlen(retname
) + rr
->len
- 5) >= 254) {
155 strncat(retname
, rr
->u
.NM
.name
, rr
->len
- 5);
156 retnamlen
+= rr
->len
- 5;
166 MAYBE_CONTINUE(repeat
, inode
);
168 return retnamlen
; /* If 0, this file did not have a NM field */
176 parse_rock_ridge_inode_internal(struct iso_directory_record
*de
,
177 struct inode
*inode
, int regard_xa
)
184 struct rock_ridge
*rr
;
191 if (!ISOFS_SB(inode
->i_sb
)->s_rock
)
194 SETUP_ROCK_RIDGE(de
, chr
, len
);
203 while (len
> 2) { /* There may be one byte for padding somewhere */
204 rr
= (struct rock_ridge
*)chr
;
206 goto out
; /* Something got screwed up here */
207 sig
= isonum_721(chr
);
211 goto out
; /* corrupted isofs */
214 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
216 if ((rr
->u
.RR
.flags
[0] &
217 (RR_PX
| RR_TF
| RR_SL
| RR_CL
)) == 0)
222 if (check_sp(rr
, inode
))
229 ISOFS_SB(inode
->i_sb
)->s_rock
= 1;
230 printk(KERN_DEBUG
"ISO 9660 Extensions: ");
233 for (p
= 0; p
< rr
->u
.ER
.len_id
; p
++)
234 printk("%c", rr
->u
.ER
.data
[p
]);
239 inode
->i_mode
= isonum_733(rr
->u
.PX
.mode
);
240 inode
->i_nlink
= isonum_733(rr
->u
.PX
.n_links
);
241 inode
->i_uid
= isonum_733(rr
->u
.PX
.uid
);
242 inode
->i_gid
= isonum_733(rr
->u
.PX
.gid
);
247 high
= isonum_733(rr
->u
.PN
.dev_high
);
248 low
= isonum_733(rr
->u
.PN
.dev_low
);
250 * The Rock Ridge standard specifies that if
251 * sizeof(dev_t) <= 4, then the high field is
252 * unused, and the device number is completely
253 * stored in the low field. Some writers may
254 * ignore this subtlety,
255 * and as a result we test to see if the entire
257 * stored in the low field, and use that.
259 if ((low
& ~0xff) && high
== 0) {
261 MKDEV(low
>> 8, low
& 0xff);
270 * Some RRIP writers incorrectly place ctime in the
271 * TF_CREATE field. Try to handle this correctly for
274 /* Rock ridge never appears on a High Sierra disk */
276 if (rr
->u
.TF
.flags
& TF_CREATE
) {
277 inode
->i_ctime
.tv_sec
=
278 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
280 inode
->i_ctime
.tv_nsec
= 0;
282 if (rr
->u
.TF
.flags
& TF_MODIFY
) {
283 inode
->i_mtime
.tv_sec
=
284 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
286 inode
->i_mtime
.tv_nsec
= 0;
288 if (rr
->u
.TF
.flags
& TF_ACCESS
) {
289 inode
->i_atime
.tv_sec
=
290 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
292 inode
->i_atime
.tv_nsec
= 0;
294 if (rr
->u
.TF
.flags
& TF_ATTRIBUTES
) {
295 inode
->i_ctime
.tv_sec
=
296 iso_date(rr
->u
.TF
.times
[cnt
++].time
,
298 inode
->i_ctime
.tv_nsec
= 0;
304 struct SL_component
*slp
;
305 struct SL_component
*oldslp
;
307 slp
= &rr
->u
.SL
.link
;
308 inode
->i_size
= symlink_len
;
311 switch (slp
->flags
& ~1) {
327 printk("Symlink component flag "
328 "not implemented\n");
330 slen
-= slp
->len
+ 2;
332 slp
= (struct SL_component
*)
333 (((char *)slp
) + slp
->len
+ 2);
347 * If this component record isn't
348 * continued, then append a '/'.
351 && (oldslp
->flags
& 1) == 0)
355 symlink_len
= inode
->i_size
;
358 printk(KERN_WARNING
"Attempt to read inode for "
359 "relocated directory\n");
362 ISOFS_I(inode
)->i_first_extent
=
363 isonum_733(rr
->u
.CL
.location
);
365 isofs_iget(inode
->i_sb
,
366 ISOFS_I(inode
)->i_first_extent
,
370 inode
->i_mode
= reloc
->i_mode
;
371 inode
->i_nlink
= reloc
->i_nlink
;
372 inode
->i_uid
= reloc
->i_uid
;
373 inode
->i_gid
= reloc
->i_gid
;
374 inode
->i_rdev
= reloc
->i_rdev
;
375 inode
->i_size
= reloc
->i_size
;
376 inode
->i_blocks
= reloc
->i_blocks
;
377 inode
->i_atime
= reloc
->i_atime
;
378 inode
->i_ctime
= reloc
->i_ctime
;
379 inode
->i_mtime
= reloc
->i_mtime
;
383 case SIG('Z', 'F'): {
386 if (ISOFS_SB(inode
->i_sb
)->s_nocompress
)
388 algo
= isonum_721(rr
->u
.ZF
.algorithm
);
389 if (algo
== SIG('p', 'z')) {
391 isonum_711(&rr
->u
.ZF
.parms
[1]);
392 if (block_shift
< PAGE_CACHE_SHIFT
393 || block_shift
> 17) {
394 printk(KERN_WARNING
"isofs: "
395 "Can't handle ZF block "
400 * Note: we don't change
403 ISOFS_I(inode
)->i_file_format
=
404 isofs_file_compressed
;
406 * Parameters to compression
407 * algorithm (header size,
410 ISOFS_I(inode
)->i_format_parm
[0] =
411 isonum_711(&rr
->u
.ZF
.parms
[0]);
412 ISOFS_I(inode
)->i_format_parm
[1] =
413 isonum_711(&rr
->u
.ZF
.parms
[1]);
420 "isofs: Unknown ZF compression "
422 rr
->u
.ZF
.algorithm
[0],
423 rr
->u
.ZF
.algorithm
[1]);
432 MAYBE_CONTINUE(repeat
, inode
);
439 static char *get_symlink_chunk(char *rpnt
, struct rock_ridge
*rr
, char *plimit
)
443 struct SL_component
*oldslp
;
444 struct SL_component
*slp
;
446 slp
= &rr
->u
.SL
.link
;
449 switch (slp
->flags
& ~1) {
451 if (slp
->len
> plimit
- rpnt
)
453 memcpy(rpnt
, slp
->text
, slp
->len
);
462 if (2 > plimit
- rpnt
)
474 printk("Symlink component flag not implemented (%d)\n",
477 slen
-= slp
->len
+ 2;
479 slp
= (struct SL_component
*)((char *)slp
+ slp
->len
+ 2);
483 * If there is another SL record, and this component
484 * record isn't continued, then add a slash.
486 if ((!rootflag
) && (rr
->u
.SL
.flags
& 1) &&
487 !(oldslp
->flags
& 1)) {
496 * If this component record isn't continued, then append a '/'.
498 if (!rootflag
&& !(oldslp
->flags
& 1)) {
507 int parse_rock_ridge_inode(struct iso_directory_record
*de
, struct inode
*inode
)
509 int result
= parse_rock_ridge_inode_internal(de
, inode
, 0);
510 /* if rockridge flag was reset and we didn't look for attributes
511 * behind eventual XA attributes, have a look there */
512 if ((ISOFS_SB(inode
->i_sb
)->s_rock_offset
== -1)
513 && (ISOFS_SB(inode
->i_sb
)->s_rock
== 2)) {
514 result
= parse_rock_ridge_inode_internal(de
, inode
, 14);
519 /* readpage() for symlinks: reads symlink contents into the page and either
520 makes it uptodate and returns 0 or returns error (-EIO) */
522 static int rock_ridge_symlink_readpage(struct file
*file
, struct page
*page
)
524 struct inode
*inode
= page
->mapping
->host
;
525 struct iso_inode_info
*ei
= ISOFS_I(inode
);
526 char *link
= kmap(page
);
527 unsigned long bufsize
= ISOFS_BUFFER_SIZE(inode
);
528 struct buffer_head
*bh
;
531 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