MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / isofs / rock.c
blob19d999fd450c2c7892e5ef00045482f889fbf5c1
1 /*
2 * linux/fs/isofs/rock.c
4 * (C) 1992, 1993 Eric Youngdale
6 * Rock Ridge Extensions to iso9660
7 */
9 #include <linux/stat.h>
10 #include <linux/time.h>
11 #include <linux/iso_fs.h>
12 #include <linux/string.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
17 #include <linux/buffer_head.h>
18 #include <asm/page.h>
20 #include "rock.h"
22 /* These functions are designed to read the system areas of a directory record
23 * and extract relevant information. There are different functions provided
24 * depending upon what information we need at the time. One function fills
25 * out an inode structure, a second one extracts a filename, a third one
26 * returns a symbolic link name, and a fourth one returns the extent number
27 * for the file. */
29 #define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
32 /* This is a way of ensuring that we have something in the system
33 use fields that is compatible with Rock Ridge */
34 #define CHECK_SP(FAIL) \
35 if(rr->u.SP.magic[0] != 0xbe) FAIL; \
36 if(rr->u.SP.magic[1] != 0xef) FAIL; \
37 ISOFS_SB(inode->i_sb)->s_rock_offset=rr->u.SP.skip;
38 /* We define a series of macros because each function must do exactly the
39 same thing in certain places. We use the macros to ensure that everything
40 is done correctly */
42 #define CONTINUE_DECLS \
43 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
44 void *buffer = NULL
46 #define CHECK_CE \
47 {cont_extent = isonum_733(rr->u.CE.extent); \
48 cont_offset = isonum_733(rr->u.CE.offset); \
49 cont_size = isonum_733(rr->u.CE.size);}
51 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
52 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
53 if(LEN & 1) LEN++; \
54 CHR = ((unsigned char *) DE) + LEN; \
55 LEN = *((unsigned char *) DE) - LEN; \
56 if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
57 { \
58 LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
59 CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
60 if (LEN<0) LEN=0; \
61 } \
64 #define MAYBE_CONTINUE(LABEL,DEV) \
65 {if (buffer) { kfree(buffer); buffer = NULL; } \
66 if (cont_extent){ \
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; \
73 offset1 = 0; \
74 pbh = sb_bread(DEV->i_sb, block); \
75 if(pbh){ \
76 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
77 brelse(pbh); \
78 chr = (unsigned char *) buffer; \
79 len = cont_size; \
80 cont_extent = 0; \
81 cont_size = 0; \
82 cont_offset = 0; \
83 goto LABEL; \
84 } \
85 printk("Unable to read rock-ridge attributes\n"); \
88 /* return length of name field; 0: not found, -1: to be ignored */
89 int get_rock_ridge_filename(struct iso_directory_record * de,
90 char * retname, struct inode * inode)
92 int len;
93 unsigned char * chr;
94 CONTINUE_DECLS;
95 int retnamlen = 0, truncate=0;
97 if (!ISOFS_SB(inode->i_sb)->s_rock) return 0;
98 *retname = 0;
100 SETUP_ROCK_RIDGE(de, chr, len);
101 repeat:
103 struct rock_ridge * rr;
104 int sig;
106 while (len > 1){ /* There may be one byte for padding somewhere */
107 rr = (struct rock_ridge *) chr;
108 if (rr->len == 0) goto out; /* Something got screwed up here */
109 sig = isonum_721(chr);
110 chr += rr->len;
111 len -= rr->len;
113 switch(sig){
114 case SIG('R','R'):
115 if((rr->u.RR.flags[0] & RR_NM) == 0) goto out;
116 break;
117 case SIG('S','P'):
118 CHECK_SP(goto out);
119 break;
120 case SIG('C','E'):
121 CHECK_CE;
122 break;
123 case SIG('N','M'):
124 if (truncate) break;
126 * If the flags are 2 or 4, this indicates '.' or '..'.
127 * We don't want to do anything with this, because it
128 * screws up the code that calls us. We don't really
129 * care anyways, since we can just use the non-RR
130 * name.
132 if (rr->u.NM.flags & 6) {
133 break;
136 if (rr->u.NM.flags & ~1) {
137 printk("Unsupported NM flag settings (%d)\n",rr->u.NM.flags);
138 break;
140 if((strlen(retname) + rr->len - 5) >= 254) {
141 truncate = 1;
142 break;
144 strncat(retname, rr->u.NM.name, rr->len - 5);
145 retnamlen += rr->len - 5;
146 break;
147 case SIG('R','E'):
148 if (buffer) kfree(buffer);
149 return -1;
150 default:
151 break;
155 MAYBE_CONTINUE(repeat,inode);
156 if (buffer) kfree(buffer);
157 return retnamlen; /* If 0, this file did not have a NM field */
158 out:
159 if(buffer) kfree(buffer);
160 return 0;
163 static int
164 parse_rock_ridge_inode_internal(struct iso_directory_record *de,
165 struct inode *inode, int regard_xa)
167 int len;
168 unsigned char * chr;
169 int symlink_len = 0;
170 CONTINUE_DECLS;
172 if (!ISOFS_SB(inode->i_sb)->s_rock) return 0;
174 SETUP_ROCK_RIDGE(de, chr, len);
175 if (regard_xa)
177 chr+=14;
178 len-=14;
179 if (len<0) len=0;
182 repeat:
184 int cnt, sig;
185 struct inode * reloc;
186 struct rock_ridge * rr;
187 int rootflag;
189 while (len > 1){ /* There may be one byte for padding somewhere */
190 rr = (struct rock_ridge *) chr;
191 if (rr->len == 0) goto out; /* Something got screwed up here */
192 sig = isonum_721(chr);
193 chr += rr->len;
194 len -= rr->len;
196 switch(sig){
197 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
198 case SIG('R','R'):
199 if((rr->u.RR.flags[0] &
200 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0) goto out;
201 break;
202 #endif
203 case SIG('S','P'):
204 CHECK_SP(goto out);
205 break;
206 case SIG('C','E'):
207 CHECK_CE;
208 break;
209 case SIG('E','R'):
210 ISOFS_SB(inode->i_sb)->s_rock = 1;
211 printk(KERN_DEBUG "ISO 9660 Extensions: ");
212 { int p;
213 for(p=0;p<rr->u.ER.len_id;p++) printk("%c",rr->u.ER.data[p]);
215 printk("\n");
216 break;
217 case SIG('P','X'):
218 inode->i_mode = isonum_733(rr->u.PX.mode);
219 inode->i_nlink = isonum_733(rr->u.PX.n_links);
220 inode->i_uid = isonum_733(rr->u.PX.uid);
221 inode->i_gid = isonum_733(rr->u.PX.gid);
222 break;
223 case SIG('P','N'):
224 { int high, low;
225 high = isonum_733(rr->u.PN.dev_high);
226 low = isonum_733(rr->u.PN.dev_low);
228 * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
229 * then the high field is unused, and the device number is completely
230 * stored in the low field. Some writers may ignore this subtlety,
231 * and as a result we test to see if the entire device number is
232 * stored in the low field, and use that.
234 if((low & ~0xff) && high == 0) {
235 inode->i_rdev = MKDEV(low >> 8, low & 0xff);
236 } else {
237 inode->i_rdev = MKDEV(high, low);
240 break;
241 case SIG('T','F'):
242 /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
243 Try to handle this correctly for either case. */
244 cnt = 0; /* Rock ridge never appears on a High Sierra disk */
245 if(rr->u.TF.flags & TF_CREATE) {
246 inode->i_ctime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
247 inode->i_ctime.tv_nsec = 0;
249 if(rr->u.TF.flags & TF_MODIFY) {
250 inode->i_mtime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
251 inode->i_mtime.tv_nsec = 0;
253 if(rr->u.TF.flags & TF_ACCESS) {
254 inode->i_atime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
255 inode->i_atime.tv_nsec = 0;
257 if(rr->u.TF.flags & TF_ATTRIBUTES) {
258 inode->i_ctime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
259 inode->i_ctime.tv_nsec = 0;
261 break;
262 case SIG('S','L'):
263 {int slen;
264 struct SL_component * slp;
265 struct SL_component * oldslp;
266 slen = rr->len - 5;
267 slp = &rr->u.SL.link;
268 inode->i_size = symlink_len;
269 while (slen > 1){
270 rootflag = 0;
271 switch(slp->flags &~1){
272 case 0:
273 inode->i_size += slp->len;
274 break;
275 case 2:
276 inode->i_size += 1;
277 break;
278 case 4:
279 inode->i_size += 2;
280 break;
281 case 8:
282 rootflag = 1;
283 inode->i_size += 1;
284 break;
285 default:
286 printk("Symlink component flag not implemented\n");
288 slen -= slp->len + 2;
289 oldslp = slp;
290 slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
292 if(slen < 2) {
293 if( ((rr->u.SL.flags & 1) != 0)
294 && ((oldslp->flags & 1) == 0) ) inode->i_size += 1;
295 break;
299 * If this component record isn't continued, then append a '/'.
301 if (!rootflag && (oldslp->flags & 1) == 0)
302 inode->i_size += 1;
305 symlink_len = inode->i_size;
306 break;
307 case SIG('R','E'):
308 printk(KERN_WARNING "Attempt to read inode for relocated directory\n");
309 goto out;
310 case SIG('C','L'):
311 ISOFS_I(inode)->i_first_extent = isonum_733(rr->u.CL.location);
312 reloc = isofs_iget(inode->i_sb, ISOFS_I(inode)->i_first_extent, 0);
313 if (!reloc)
314 goto out;
315 inode->i_mode = reloc->i_mode;
316 inode->i_nlink = reloc->i_nlink;
317 inode->i_uid = reloc->i_uid;
318 inode->i_gid = reloc->i_gid;
319 inode->i_rdev = reloc->i_rdev;
320 inode->i_size = reloc->i_size;
321 inode->i_blocks = reloc->i_blocks;
322 inode->i_atime = reloc->i_atime;
323 inode->i_ctime = reloc->i_ctime;
324 inode->i_mtime = reloc->i_mtime;
325 iput(reloc);
326 break;
327 #ifdef CONFIG_ZISOFS
328 case SIG('Z','F'):
329 if ( !ISOFS_SB(inode->i_sb)->s_nocompress ) {
330 int algo;
331 algo = isonum_721(rr->u.ZF.algorithm);
332 if ( algo == SIG('p','z') ) {
333 int block_shift = isonum_711(&rr->u.ZF.parms[1]);
334 if ( block_shift < PAGE_CACHE_SHIFT || block_shift > 17 ) {
335 printk(KERN_WARNING "isofs: Can't handle ZF block size of 2^%d\n", block_shift);
336 } else {
337 /* Note: we don't change i_blocks here */
338 ISOFS_I(inode)->i_file_format = isofs_file_compressed;
339 /* Parameters to compression algorithm (header size, block size) */
340 ISOFS_I(inode)->i_format_parm[0] = isonum_711(&rr->u.ZF.parms[0]);
341 ISOFS_I(inode)->i_format_parm[1] = isonum_711(&rr->u.ZF.parms[1]);
342 inode->i_size = isonum_733(rr->u.ZF.real_size);
344 } else {
345 printk(KERN_WARNING "isofs: Unknown ZF compression algorithm: %c%c\n",
346 rr->u.ZF.algorithm[0], rr->u.ZF.algorithm[1]);
349 break;
350 #endif
351 default:
352 break;
356 MAYBE_CONTINUE(repeat,inode);
357 out:
358 if(buffer) kfree(buffer);
359 return 0;
362 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
364 int slen;
365 int rootflag;
366 struct SL_component *oldslp;
367 struct SL_component *slp;
368 slen = rr->len - 5;
369 slp = &rr->u.SL.link;
370 while (slen > 1) {
371 rootflag = 0;
372 switch (slp->flags & ~1) {
373 case 0:
374 if (slp->len > plimit - rpnt)
375 return NULL;
376 memcpy(rpnt, slp->text, slp->len);
377 rpnt+=slp->len;
378 break;
379 case 2:
380 if (rpnt >= plimit)
381 return NULL;
382 *rpnt++='.';
383 break;
384 case 4:
385 if (2 > plimit - rpnt)
386 return NULL;
387 *rpnt++='.';
388 *rpnt++='.';
389 break;
390 case 8:
391 if (rpnt >= plimit)
392 return NULL;
393 rootflag = 1;
394 *rpnt++='/';
395 break;
396 default:
397 printk("Symlink component flag not implemented (%d)\n",
398 slp->flags);
400 slen -= slp->len + 2;
401 oldslp = slp;
402 slp = (struct SL_component *) ((char *) slp + slp->len + 2);
404 if (slen < 2) {
406 * If there is another SL record, and this component
407 * record isn't continued, then add a slash.
409 if ((!rootflag) && (rr->u.SL.flags & 1) &&
410 !(oldslp->flags & 1)) {
411 if (rpnt >= plimit)
412 return NULL;
413 *rpnt++='/';
415 break;
419 * If this component record isn't continued, then append a '/'.
421 if (!rootflag && !(oldslp->flags & 1)) {
422 if (rpnt >= plimit)
423 return NULL;
424 *rpnt++='/';
427 return rpnt;
430 int parse_rock_ridge_inode(struct iso_directory_record * de,
431 struct inode * inode)
433 int result=parse_rock_ridge_inode_internal(de,inode,0);
434 /* if rockridge flag was reset and we didn't look for attributes
435 * behind eventual XA attributes, have a look there */
436 if ((ISOFS_SB(inode->i_sb)->s_rock_offset==-1)
437 &&(ISOFS_SB(inode->i_sb)->s_rock==2))
439 result=parse_rock_ridge_inode_internal(de,inode,14);
441 return result;
444 /* readpage() for symlinks: reads symlink contents into the page and either
445 makes it uptodate and returns 0 or returns error (-EIO) */
447 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
449 struct inode *inode = page->mapping->host;
450 struct iso_inode_info *ei = ISOFS_I(inode);
451 char *link = kmap(page);
452 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
453 struct buffer_head *bh;
454 char *rpnt = link;
455 unsigned char *pnt;
456 struct iso_directory_record *raw_inode;
457 CONTINUE_DECLS;
458 unsigned long block, offset;
459 int sig;
460 int len;
461 unsigned char *chr;
462 struct rock_ridge *rr;
464 if (!ISOFS_SB(inode->i_sb)->s_rock)
465 panic ("Cannot have symlink with high sierra variant of iso filesystem\n");
467 block = ei->i_iget5_block;
468 lock_kernel();
469 bh = sb_bread(inode->i_sb, block);
470 if (!bh)
471 goto out_noread;
473 offset = ei->i_iget5_offset;
474 pnt = (unsigned char *) bh->b_data + offset;
476 raw_inode = (struct iso_directory_record *) pnt;
479 * If we go past the end of the buffer, there is some sort of error.
481 if (offset + *pnt > bufsize)
482 goto out_bad_span;
484 /* Now test for possible Rock Ridge extensions which will override
485 some of these numbers in the inode structure. */
487 SETUP_ROCK_RIDGE(raw_inode, chr, len);
489 repeat:
490 while (len > 1) { /* There may be one byte for padding somewhere */
491 rr = (struct rock_ridge *) chr;
492 if (rr->len == 0)
493 goto out; /* Something got screwed up here */
494 sig = isonum_721(chr);
495 chr += rr->len;
496 len -= rr->len;
498 switch (sig) {
499 case SIG('R', 'R'):
500 if ((rr->u.RR.flags[0] & RR_SL) == 0)
501 goto out;
502 break;
503 case SIG('S', 'P'):
504 CHECK_SP(goto out);
505 break;
506 case SIG('S', 'L'):
507 rpnt = get_symlink_chunk(rpnt, rr,
508 link + (PAGE_SIZE - 1));
509 if (rpnt == NULL)
510 goto out;
511 break;
512 case SIG('C', 'E'):
513 /* This tells is if there is a continuation record */
514 CHECK_CE;
515 default:
516 break;
519 MAYBE_CONTINUE(repeat, inode);
520 if (buffer)
521 kfree(buffer);
523 if (rpnt == link)
524 goto fail;
525 brelse(bh);
526 *rpnt = '\0';
527 unlock_kernel();
528 SetPageUptodate(page);
529 kunmap(page);
530 unlock_page(page);
531 return 0;
533 /* error exit from macro */
534 out:
535 if (buffer)
536 kfree(buffer);
537 goto fail;
538 out_noread:
539 printk("unable to read i-node block");
540 goto fail;
541 out_bad_span:
542 printk("symlink spans iso9660 blocks\n");
543 fail:
544 brelse(bh);
545 unlock_kernel();
546 SetPageError(page);
547 kunmap(page);
548 unlock_page(page);
549 return -EIO;
552 struct address_space_operations isofs_symlink_aops = {
553 .readpage = rock_ridge_symlink_readpage