[PATCH] rock: remove CONTINUE_DECLS
[linux-2.6/mini2440.git] / fs / isofs / rock.c
blob9f2aaa27f8faf5bb68a3acc71b81cbdf1e3c2da5
1 /*
2 * linux/fs/isofs/rock.c
4 * (C) 1992, 1993 Eric Youngdale
6 * Rock Ridge Extensions to iso9660
7 */
9 #include <linux/slab.h>
10 #include <linux/pagemap.h>
11 #include <linux/smp_lock.h>
13 #include "isofs.h"
14 #include "rock.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
21 * for the file. */
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)
33 return -1;
34 if (rr->u.SP.magic[1] != 0xef)
35 return -1;
36 ISOFS_SB(inode->i_sb)->s_rock_offset = rr->u.SP.skip;
37 return 0;
40 #define CHECK_CE \
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]; \
47 if(LEN & 1) LEN++; \
48 CHR = ((unsigned char *) DE) + LEN; \
49 LEN = *((unsigned char *) DE) - LEN; \
50 if (LEN<0) LEN=0; \
51 if (ISOFS_SB(inode->i_sb)->s_rock_offset!=-1) \
52 { \
53 LEN-=ISOFS_SB(inode->i_sb)->s_rock_offset; \
54 CHR+=ISOFS_SB(inode->i_sb)->s_rock_offset; \
55 if (LEN<0) LEN=0; \
56 } \
59 #define MAYBE_CONTINUE(LABEL,DEV) \
60 {if (buffer) { kfree(buffer); buffer = NULL; } \
61 if (cont_extent){ \
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; \
68 offset1 = 0; \
69 pbh = sb_bread(DEV->i_sb, block); \
70 if(pbh){ \
71 if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
72 brelse(pbh); \
73 goto out; \
74 } \
75 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
76 brelse(pbh); \
77 chr = (unsigned char *) buffer; \
78 len = cont_size; \
79 cont_extent = 0; \
80 cont_size = 0; \
81 cont_offset = 0; \
82 goto LABEL; \
83 } \
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)
91 int len;
92 unsigned char *chr;
93 int cont_extent = 0;
94 int cont_offset = 0;
95 int cont_size = 0;
96 void *buffer = NULL;
97 struct rock_ridge *rr;
98 int sig;
99 int retnamlen = 0;
100 int truncate = 0;
102 if (!ISOFS_SB(inode->i_sb)->s_rock)
103 return 0;
104 *retname = 0;
106 SETUP_ROCK_RIDGE(de, chr, len);
107 repeat:
109 while (len > 2) { /* There may be one byte for padding somewhere */
110 rr = (struct rock_ridge *)chr;
111 if (rr->len < 3)
112 goto out; /* Something got screwed up here */
113 sig = isonum_721(chr);
114 chr += rr->len;
115 len -= rr->len;
116 if (len < 0)
117 goto out; /* corrupted isofs */
119 switch (sig) {
120 case SIG('R', 'R'):
121 if ((rr->u.RR.flags[0] & RR_NM) == 0)
122 goto out;
123 break;
124 case SIG('S', 'P'):
125 if (check_sp(rr, inode))
126 goto out;
127 break;
128 case SIG('C', 'E'):
129 CHECK_CE;
130 break;
131 case SIG('N', 'M'):
132 if (truncate)
133 break;
134 if (rr->len < 5)
135 break;
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
141 * name.
143 if (rr->u.NM.flags & 6)
144 break;
146 if (rr->u.NM.flags & ~1) {
147 printk("Unsupported NM flag settings (%d)\n",
148 rr->u.NM.flags);
149 break;
151 if ((strlen(retname) + rr->len - 5) >= 254) {
152 truncate = 1;
153 break;
155 strncat(retname, rr->u.NM.name, rr->len - 5);
156 retnamlen += rr->len - 5;
157 break;
158 case SIG('R', 'E'):
159 if (buffer)
160 kfree(buffer);
161 return -1;
162 default:
163 break;
166 MAYBE_CONTINUE(repeat, inode);
167 kfree(buffer);
168 return retnamlen; /* If 0, this file did not have a NM field */
169 out:
170 if (buffer)
171 kfree(buffer);
172 return 0;
175 static int
176 parse_rock_ridge_inode_internal(struct iso_directory_record *de,
177 struct inode *inode, int regard_xa)
179 int len;
180 unsigned char *chr;
181 int symlink_len = 0;
182 int cnt, sig;
183 struct inode *reloc;
184 struct rock_ridge *rr;
185 int rootflag;
186 int cont_extent = 0;
187 int cont_offset = 0;
188 int cont_size = 0;
189 void *buffer = NULL;
191 if (!ISOFS_SB(inode->i_sb)->s_rock)
192 return 0;
194 SETUP_ROCK_RIDGE(de, chr, len);
195 if (regard_xa) {
196 chr += 14;
197 len -= 14;
198 if (len < 0)
199 len = 0;
202 repeat:
203 while (len > 2) { /* There may be one byte for padding somewhere */
204 rr = (struct rock_ridge *)chr;
205 if (rr->len < 3)
206 goto out; /* Something got screwed up here */
207 sig = isonum_721(chr);
208 chr += rr->len;
209 len -= rr->len;
210 if (len < 0)
211 goto out; /* corrupted isofs */
213 switch (sig) {
214 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
215 case SIG('R', 'R'):
216 if ((rr->u.RR.flags[0] &
217 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0)
218 goto out;
219 break;
220 #endif
221 case SIG('S', 'P'):
222 if (check_sp(rr, inode))
223 goto out;
224 break;
225 case SIG('C', 'E'):
226 CHECK_CE;
227 break;
228 case SIG('E', 'R'):
229 ISOFS_SB(inode->i_sb)->s_rock = 1;
230 printk(KERN_DEBUG "ISO 9660 Extensions: ");
232 int p;
233 for (p = 0; p < rr->u.ER.len_id; p++)
234 printk("%c", rr->u.ER.data[p]);
236 printk("\n");
237 break;
238 case SIG('P', 'X'):
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);
243 break;
244 case SIG('P', 'N'):
246 int high, low;
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
256 * device number is
257 * stored in the low field, and use that.
259 if ((low & ~0xff) && high == 0) {
260 inode->i_rdev =
261 MKDEV(low >> 8, low & 0xff);
262 } else {
263 inode->i_rdev =
264 MKDEV(high, low);
267 break;
268 case SIG('T', 'F'):
270 * Some RRIP writers incorrectly place ctime in the
271 * TF_CREATE field. Try to handle this correctly for
272 * either case.
274 /* Rock ridge never appears on a High Sierra disk */
275 cnt = 0;
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;
300 break;
301 case SIG('S', 'L'):
303 int slen;
304 struct SL_component *slp;
305 struct SL_component *oldslp;
306 slen = rr->len - 5;
307 slp = &rr->u.SL.link;
308 inode->i_size = symlink_len;
309 while (slen > 1) {
310 rootflag = 0;
311 switch (slp->flags & ~1) {
312 case 0:
313 inode->i_size +=
314 slp->len;
315 break;
316 case 2:
317 inode->i_size += 1;
318 break;
319 case 4:
320 inode->i_size += 2;
321 break;
322 case 8:
323 rootflag = 1;
324 inode->i_size += 1;
325 break;
326 default:
327 printk("Symlink component flag "
328 "not implemented\n");
330 slen -= slp->len + 2;
331 oldslp = slp;
332 slp = (struct SL_component *)
333 (((char *)slp) + slp->len + 2);
335 if (slen < 2) {
336 if (((rr->u.SL.
337 flags & 1) != 0)
339 ((oldslp->
340 flags & 1) == 0))
341 inode->i_size +=
343 break;
347 * If this component record isn't
348 * continued, then append a '/'.
350 if (!rootflag
351 && (oldslp->flags & 1) == 0)
352 inode->i_size += 1;
355 symlink_len = inode->i_size;
356 break;
357 case SIG('R', 'E'):
358 printk(KERN_WARNING "Attempt to read inode for "
359 "relocated directory\n");
360 goto out;
361 case SIG('C', 'L'):
362 ISOFS_I(inode)->i_first_extent =
363 isonum_733(rr->u.CL.location);
364 reloc =
365 isofs_iget(inode->i_sb,
366 ISOFS_I(inode)->i_first_extent,
368 if (!reloc)
369 goto out;
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;
380 iput(reloc);
381 break;
382 #ifdef CONFIG_ZISOFS
383 case SIG('Z', 'F'): {
384 int algo;
386 if (ISOFS_SB(inode->i_sb)->s_nocompress)
387 break;
388 algo = isonum_721(rr->u.ZF.algorithm);
389 if (algo == SIG('p', 'z')) {
390 int block_shift =
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 "
396 "size of 2^%d\n",
397 block_shift);
398 } else {
400 * Note: we don't change
401 * i_blocks here
403 ISOFS_I(inode)->i_file_format =
404 isofs_file_compressed;
406 * Parameters to compression
407 * algorithm (header size,
408 * block 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]);
414 inode->i_size =
415 isonum_733(rr->u.ZF.
416 real_size);
418 } else {
419 printk(KERN_WARNING
420 "isofs: Unknown ZF compression "
421 "algorithm: %c%c\n",
422 rr->u.ZF.algorithm[0],
423 rr->u.ZF.algorithm[1]);
425 break;
427 #endif
428 default:
429 break;
432 MAYBE_CONTINUE(repeat, inode);
433 out:
434 if (buffer)
435 kfree(buffer);
436 return 0;
439 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
441 int slen;
442 int rootflag;
443 struct SL_component *oldslp;
444 struct SL_component *slp;
445 slen = rr->len - 5;
446 slp = &rr->u.SL.link;
447 while (slen > 1) {
448 rootflag = 0;
449 switch (slp->flags & ~1) {
450 case 0:
451 if (slp->len > plimit - rpnt)
452 return NULL;
453 memcpy(rpnt, slp->text, slp->len);
454 rpnt += slp->len;
455 break;
456 case 2:
457 if (rpnt >= plimit)
458 return NULL;
459 *rpnt++ = '.';
460 break;
461 case 4:
462 if (2 > plimit - rpnt)
463 return NULL;
464 *rpnt++ = '.';
465 *rpnt++ = '.';
466 break;
467 case 8:
468 if (rpnt >= plimit)
469 return NULL;
470 rootflag = 1;
471 *rpnt++ = '/';
472 break;
473 default:
474 printk("Symlink component flag not implemented (%d)\n",
475 slp->flags);
477 slen -= slp->len + 2;
478 oldslp = slp;
479 slp = (struct SL_component *)((char *)slp + slp->len + 2);
481 if (slen < 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)) {
488 if (rpnt >= plimit)
489 return NULL;
490 *rpnt++ = '/';
492 break;
496 * If this component record isn't continued, then append a '/'.
498 if (!rootflag && !(oldslp->flags & 1)) {
499 if (rpnt >= plimit)
500 return NULL;
501 *rpnt++ = '/';
504 return rpnt;
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);
516 return result;
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;
529 char *rpnt = link;
530 unsigned char *pnt;
531 struct iso_directory_record *raw_inode;
532 int cont_extent = 0;
533 int cont_offset = 0;
534 int cont_size = 0;
535 void *buffer = NULL;
536 unsigned long block, offset;
537 int sig;
538 int len;
539 unsigned char *chr;
540 struct rock_ridge *rr;
542 if (!ISOFS_SB(inode->i_sb)->s_rock)
543 goto error;
545 block = ei->i_iget5_block;
546 lock_kernel();
547 bh = sb_bread(inode->i_sb, block);
548 if (!bh)
549 goto out_noread;
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)
560 goto out_bad_span;
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);
567 repeat:
568 while (len > 2) { /* There may be one byte for padding somewhere */
569 rr = (struct rock_ridge *)chr;
570 if (rr->len < 3)
571 goto out; /* Something got screwed up here */
572 sig = isonum_721(chr);
573 chr += rr->len;
574 len -= rr->len;
575 if (len < 0)
576 goto out; /* corrupted isofs */
578 switch (sig) {
579 case SIG('R', 'R'):
580 if ((rr->u.RR.flags[0] & RR_SL) == 0)
581 goto out;
582 break;
583 case SIG('S', 'P'):
584 if (check_sp(rr, inode))
585 goto out;
586 break;
587 case SIG('S', 'L'):
588 rpnt = get_symlink_chunk(rpnt, rr,
589 link + (PAGE_SIZE - 1));
590 if (rpnt == NULL)
591 goto out;
592 break;
593 case SIG('C', 'E'):
594 /* This tells is if there is a continuation record */
595 CHECK_CE;
596 default:
597 break;
600 MAYBE_CONTINUE(repeat, inode);
601 kfree(buffer);
603 if (rpnt == link)
604 goto fail;
605 brelse(bh);
606 *rpnt = '\0';
607 unlock_kernel();
608 SetPageUptodate(page);
609 kunmap(page);
610 unlock_page(page);
611 return 0;
613 /* error exit from macro */
614 out:
615 if (buffer)
616 kfree(buffer);
617 goto fail;
618 out_noread:
619 printk("unable to read i-node block");
620 goto fail;
621 out_bad_span:
622 printk("symlink spans iso9660 blocks\n");
623 fail:
624 brelse(bh);
625 unlock_kernel();
626 error:
627 SetPageError(page);
628 kunmap(page);
629 unlock_page(page);
630 return -EIO;
633 struct address_space_operations isofs_symlink_aops = {
634 .readpage = rock_ridge_symlink_readpage