1 /*
2 * linux/fs/isofs/rock.c
3 *
4 * (C) 1992, 1993 Eric Youngdale
5 *
6 * Rock Ridge Extensions to iso9660
7 */
8
9 #include <linux/stat.h>
10 #include <linux/sched.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 <asm/page.h>
18
19 #include "rock.h"
20
21 /* These functions are designed to read the system areas of a directory record
22 * and extract relevant information. There are different functions provided
23 * depending upon what information we need at the time. One function fills
24 * out an inode structure, a second one extracts a filename, a third one
25 * returns a symbolic link name, and a fourth one returns the extent number
26 * for the file. */
27
28 #define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
29
30
31 /* This is a way of ensuring that we have something in the system
32 use fields that is compatible with Rock Ridge */
33 #define CHECK_SP(FAIL) \
34 if(rr->u.SP.magic[0] != 0xbe) FAIL; \
35 if(rr->u.SP.magic[1] != 0xef) FAIL; \
36 inode->i_sb->u.isofs_sb.s_rock_offset=rr->u.SP.skip;
37 /* We define a series of macros because each function must do exactly the
38 same thing in certain places. We use the macros to ensure that everything
39 is done correctly */
40
41 #define CONTINUE_DECLS \
42 int cont_extent = 0, cont_offset = 0, cont_size = 0; \
43 void * buffer = 0
44
45 #define CHECK_CE \
46 {cont_extent = isonum_733(rr->u.CE.extent); \
47 cont_offset = isonum_733(rr->u.CE.offset); \
48 cont_size = isonum_733(rr->u.CE.size);}
49
50 #define SETUP_ROCK_RIDGE(DE,CHR,LEN) \
51 {LEN= sizeof(struct iso_directory_record) + DE->name_len[0]; \
52 if(LEN & 1) LEN++; \
53 CHR = ((unsigned char *) DE) + LEN; \
54 LEN = *((unsigned char *) DE) - LEN; \
55 if (LEN<0) LEN=0; \
56 if (inode->i_sb->u.isofs_sb.s_rock_offset!=-1) \
57 { \
58 LEN-=inode->i_sb->u.isofs_sb.s_rock_offset; \
59 CHR+=inode->i_sb->u.isofs_sb.s_rock_offset; \
60 if (LEN<0) LEN=0; \
61 } \
62 }
63
64 #define MAYBE_CONTINUE(LABEL,DEV) \
65 {if (buffer) kfree(buffer); \
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 if (offset > pbh->b_size || offset + cont_size > pbh->b_size){ \
77 brelse(pbh); \
78 goto out; \
79 } \
80 memcpy(buffer + offset1, pbh->b_data + offset, cont_size - offset1); \
81 brelse(pbh); \
82 chr = (unsigned char *) buffer; \
83 len = cont_size; \
84 cont_extent = 0; \
85 cont_size = 0; \
86 cont_offset = 0; \
87 goto LABEL; \
88 } \
89 printk("Unable to read rock-ridge attributes\n"); \
90 }}
91
92 /* This is the inner layer of the get filename routine, and is called
93 for each system area and continuation record related to the file */
94
find_rock_ridge_relocation(struct iso_directory_record * de,struct inode * inode)95 int find_rock_ridge_relocation(struct iso_directory_record * de,
96 struct inode * inode) {
97 int flag;
98 int len;
99 int retval;
100 unsigned char * chr;
101 CONTINUE_DECLS;
102 flag = 0;
103
104 /* If this is a '..' then we are looking for the parent, otherwise we
105 are looking for the child */
106
107 if (de->name[0]==1 && de->name_len[0]==1) flag = 1;
108 /* Return value if we do not find appropriate record. */
109 retval = isonum_733 (de->extent);
110
111 if (!inode->i_sb->u.isofs_sb.s_rock) return retval;
112
113 SETUP_ROCK_RIDGE(de, chr, len);
114 repeat:
115 {
116 int rrflag, sig;
117 struct rock_ridge * rr;
118
119 while (len > 1){ /* There may be one byte for padding somewhere */
120 rr = (struct rock_ridge *) chr;
121 if (rr->len == 0) goto out; /* Something got screwed up here */
122 sig = isonum_721(chr);
123 chr += rr->len;
124 len -= rr->len;
125
126 switch(sig){
127 case SIG('R','R'):
128 rrflag = rr->u.RR.flags[0];
129 if (flag && !(rrflag & RR_PL)) goto out;
130 if (!flag && !(rrflag & RR_CL)) goto out;
131 break;
132 case SIG('S','P'):
133 CHECK_SP(goto out);
134 break;
135 case SIG('C','L'):
136 if (flag == 0) {
137 retval = isonum_733(rr->u.CL.location);
138 goto out;
139 }
140 break;
141 case SIG('P','L'):
142 if (flag != 0) {
143 retval = isonum_733(rr->u.PL.location);
144 goto out;
145 }
146 break;
147 case SIG('C','E'):
148 CHECK_CE; /* This tells is if there is a continuation record */
149 break;
150 default:
151 break;
152 }
153 }
154 }
155 MAYBE_CONTINUE(repeat, inode);
156 return retval;
157 out:
158 if(buffer) kfree(buffer);
159 return retval;
160 }
161
162 /* return length of name field; 0: not found, -1: to be ignored */
get_rock_ridge_filename(struct iso_directory_record * de,char * retname,struct inode * inode)163 int get_rock_ridge_filename(struct iso_directory_record * de,
164 char * retname, struct inode * inode)
165 {
166 int len;
167 unsigned char * chr;
168 CONTINUE_DECLS;
169 int retnamlen = 0, truncate=0;
170
171 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
172 *retname = 0;
173
174 SETUP_ROCK_RIDGE(de, chr, len);
175 repeat:
176 {
177 struct rock_ridge * rr;
178 int sig;
179
180 while (len > 2){ /* There may be one byte for padding somewhere */
181 rr = (struct rock_ridge *) chr;
182 if (rr->len < 3) goto out; /* Something got screwed up here */
183 sig = isonum_721(chr);
184 chr += rr->len;
185 len -= rr->len;
186 if (len < 0) goto out; /* corrupted isofs */
187
188 switch(sig){
189 case SIG('R','R'):
190 if((rr->u.RR.flags[0] & RR_NM) == 0) goto out;
191 break;
192 case SIG('S','P'):
193 CHECK_SP(goto out);
194 break;
195 case SIG('C','E'):
196 CHECK_CE;
197 break;
198 case SIG('N','M'):
199 if (truncate) break;
200 if (rr->len < 5) break;
201 /*
202 * If the flags are 2 or 4, this indicates '.' or '..'.
203 * We don't want to do anything with this, because it
204 * screws up the code that calls us. We don't really
205 * care anyways, since we can just use the non-RR
206 * name.
207 */
208 if (rr->u.NM.flags & 6) {
209 break;
210 }
211
212 if (rr->u.NM.flags & ~1) {
213 printk("Unsupported NM flag settings (%d)\n",rr->u.NM.flags);
214 break;
215 }
216 if((strlen(retname) + rr->len - 5) >= 254) {
217 truncate = 1;
218 break;
219 }
220 strncat(retname, rr->u.NM.name, rr->len - 5);
221 retnamlen += rr->len - 5;
222 break;
223 case SIG('R','E'):
224 if (buffer) kfree(buffer);
225 return -1;
226 default:
227 break;
228 }
229 }
230 }
231 MAYBE_CONTINUE(repeat,inode);
232 return retnamlen; /* If 0, this file did not have a NM field */
233 out:
234 if(buffer) kfree(buffer);
235 return 0;
236 }
237
parse_rock_ridge_inode_internal(struct iso_directory_record * de,struct inode * inode,int regard_xa)238 int parse_rock_ridge_inode_internal(struct iso_directory_record * de,
239 struct inode * inode,int regard_xa){
240 int len;
241 unsigned char * chr;
242 int symlink_len = 0;
243 CONTINUE_DECLS;
244
245 if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
246
247 SETUP_ROCK_RIDGE(de, chr, len);
248 if (regard_xa)
249 {
250 chr+=14;
251 len-=14;
252 if (len<0) len=0;
253 };
254
255 repeat:
256 {
257 int cnt, sig;
258 struct inode * reloc;
259 struct rock_ridge * rr;
260 int rootflag;
261
262 while (len > 2){ /* There may be one byte for padding somewhere */
263 rr = (struct rock_ridge *) chr;
264 if (rr->len < 3) goto out; /* Something got screwed up here */
265 sig = isonum_721(chr);
266 chr += rr->len;
267 len -= rr->len;
268 if (len < 0) goto out; /* corrupted isofs */
269
270 switch(sig){
271 #ifndef CONFIG_ZISOFS /* No flag for SF or ZF */
272 case SIG('R','R'):
273 if((rr->u.RR.flags[0] &
274 (RR_PX | RR_TF | RR_SL | RR_CL)) == 0) goto out;
275 break;
276 #endif
277 case SIG('S','P'):
278 CHECK_SP(goto out);
279 break;
280 case SIG('C','E'):
281 CHECK_CE;
282 break;
283 case SIG('E','R'):
284 inode->i_sb->u.isofs_sb.s_rock = 1;
285 printk(KERN_DEBUG "ISO 9660 Extensions: ");
286 { int p;
287 for(p=0;p<rr->u.ER.len_id;p++) printk("%c",rr->u.ER.data[p]);
288 }
289 printk("\n");
290 break;
291 case SIG('P','X'):
292 inode->i_mode = isonum_733(rr->u.PX.mode);
293 inode->i_nlink = isonum_733(rr->u.PX.n_links);
294 inode->i_uid = isonum_733(rr->u.PX.uid);
295 inode->i_gid = isonum_733(rr->u.PX.gid);
296 break;
297 case SIG('P','N'):
298 { int high, low;
299 high = isonum_733(rr->u.PN.dev_high);
300 low = isonum_733(rr->u.PN.dev_low);
301 /*
302 * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
303 * then the high field is unused, and the device number is completely
304 * stored in the low field. Some writers may ignore this subtlety,
305 * and as a result we test to see if the entire device number is
306 * stored in the low field, and use that.
307 */
308 if((low & ~0xff) && high == 0) {
309 inode->i_rdev = MKDEV(low >> 8, low & 0xff);
310 } else {
311 inode->i_rdev = MKDEV(high, low);
312 }
313 }
314 break;
315 case SIG('T','F'):
316 /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
317 Try to handle this correctly for either case. */
318 cnt = 0; /* Rock ridge never appears on a High Sierra disk */
319 if(rr->u.TF.flags & TF_CREATE)
320 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
321 if(rr->u.TF.flags & TF_MODIFY)
322 inode->i_mtime = iso_date(rr->u.TF.times[cnt++].time, 0);
323 if(rr->u.TF.flags & TF_ACCESS)
324 inode->i_atime = iso_date(rr->u.TF.times[cnt++].time, 0);
325 if(rr->u.TF.flags & TF_ATTRIBUTES)
326 inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
327 break;
328 case SIG('S','L'):
329 {int slen;
330 struct SL_component * slp;
331 struct SL_component * oldslp;
332 slen = rr->len - 5;
333 slp = &rr->u.SL.link;
334 inode->i_size = symlink_len;
335 while (slen > 1){
336 rootflag = 0;
337 switch(slp->flags &~1){
338 case 0:
339 inode->i_size += slp->len;
340 break;
341 case 2:
342 inode->i_size += 1;
343 break;
344 case 4:
345 inode->i_size += 2;
346 break;
347 case 8:
348 rootflag = 1;
349 inode->i_size += 1;
350 break;
351 default:
352 printk("Symlink component flag not implemented\n");
353 }
354 slen -= slp->len + 2;
355 oldslp = slp;
356 slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
357
358 if(slen < 2) {
359 if( ((rr->u.SL.flags & 1) != 0)
360 && ((oldslp->flags & 1) == 0) ) inode->i_size += 1;
361 break;
362 }
363
364 /*
365 * If this component record isn't continued, then append a '/'.
366 */
367 if (!rootflag && (oldslp->flags & 1) == 0)
368 inode->i_size += 1;
369 }
370 }
371 symlink_len = inode->i_size;
372 break;
373 case SIG('R','E'):
374 printk(KERN_WARNING "Attempt to read inode for relocated directory\n");
375 goto out;
376 case SIG('C','L'):
377 inode->u.isofs_i.i_first_extent = isonum_733(rr->u.CL.location);
378 reloc = iget(inode->i_sb,
379 (inode->u.isofs_i.i_first_extent <<
380 inode -> i_sb -> u.isofs_sb.s_log_zone_size));
381 if (!reloc)
382 goto out;
383 inode->i_mode = reloc->i_mode;
384 inode->i_nlink = reloc->i_nlink;
385 inode->i_uid = reloc->i_uid;
386 inode->i_gid = reloc->i_gid;
387 inode->i_rdev = reloc->i_rdev;
388 inode->i_size = reloc->i_size;
389 inode->i_blocks = reloc->i_blocks;
390 inode->i_atime = reloc->i_atime;
391 inode->i_ctime = reloc->i_ctime;
392 inode->i_mtime = reloc->i_mtime;
393 iput(reloc);
394 break;
395 #ifdef CONFIG_ZISOFS
396 case SIG('Z','F'):
397 if ( !inode->i_sb->u.isofs_sb.s_nocompress ) {
398 int algo;
399 algo = isonum_721(rr->u.ZF.algorithm);
400 if ( algo == SIG('p','z') ) {
401 int block_shift = isonum_711(&rr->u.ZF.parms[1]);
402 if ( block_shift < PAGE_CACHE_SHIFT || block_shift > 17 ) {
403 printk(KERN_WARNING "isofs: Can't handle ZF block size of 2^%d\n", block_shift);
404 } else {
405 /* Note: we don't change i_blocks here */
406 inode->u.isofs_i.i_file_format = isofs_file_compressed;
407 /* Parameters to compression algorithm (header size, block size) */
408 inode->u.isofs_i.i_format_parm[0] = isonum_711(&rr->u.ZF.parms[0]);
409 inode->u.isofs_i.i_format_parm[1] = isonum_711(&rr->u.ZF.parms[1]);
410 inode->i_size = isonum_733(rr->u.ZF.real_size);
411 }
412 } else {
413 printk(KERN_WARNING "isofs: Unknown ZF compression algorithm: %c%c\n",
414 rr->u.ZF.algorithm[0], rr->u.ZF.algorithm[1]);
415 }
416 }
417 break;
418 #endif
419 default:
420 break;
421 }
422 }
423 }
424 MAYBE_CONTINUE(repeat,inode);
425 return 0;
426 out:
427 if(buffer) kfree(buffer);
428 return 0;
429 }
430
get_symlink_chunk(char * rpnt,struct rock_ridge * rr,char * plimit)431 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
432 {
433 int slen;
434 int rootflag;
435 struct SL_component *oldslp;
436 struct SL_component *slp;
437 slen = rr->len - 5;
438 slp = &rr->u.SL.link;
439 while (slen > 1) {
440 rootflag = 0;
441 switch (slp->flags & ~1) {
442 case 0:
443 if (slp->len > plimit - rpnt)
444 return NULL;
445 memcpy(rpnt, slp->text, slp->len);
446 rpnt+=slp->len;
447 break;
448 case 2:
449 if (rpnt >= plimit)
450 return NULL;
451 *rpnt++='.';
452 break;
453 case 4:
454 if (2 > plimit - rpnt)
455 return NULL;
456 *rpnt++='.';
457 *rpnt++='.';
458 break;
459 case 8:
460 if (rpnt >= plimit)
461 return NULL;
462 rootflag = 1;
463 *rpnt++='/';
464 break;
465 default:
466 printk("Symlink component flag not implemented (%d)\n",
467 slp->flags);
468 }
469 slen -= slp->len + 2;
470 oldslp = slp;
471 slp = (struct SL_component *) ((char *) slp + slp->len + 2);
472
473 if (slen < 2) {
474 /*
475 * If there is another SL record, and this component
476 * record isn't continued, then add a slash.
477 */
478 if ((!rootflag) && (rr->u.SL.flags & 1) &&
479 !(oldslp->flags & 1)) {
480 if (rpnt >= plimit)
481 return NULL;
482 *rpnt++='/';
483 }
484 break;
485 }
486
487 /*
488 * If this component record isn't continued, then append a '/'.
489 */
490 if (!rootflag && !(oldslp->flags & 1)) {
491 if (rpnt >= plimit)
492 return NULL;
493 *rpnt++='/';
494 }
495 }
496 return rpnt;
497 }
498
parse_rock_ridge_inode(struct iso_directory_record * de,struct inode * inode)499 int parse_rock_ridge_inode(struct iso_directory_record * de,
500 struct inode * inode)
501 {
502 int result=parse_rock_ridge_inode_internal(de,inode,0);
503 /* if rockridge flag was reset and we didn't look for attributes
504 * behind eventual XA attributes, have a look there */
505 if ((inode->i_sb->u.isofs_sb.s_rock_offset==-1)
506 &&(inode->i_sb->u.isofs_sb.s_rock==2))
507 {
508 result=parse_rock_ridge_inode_internal(de,inode,14);
509 };
510 return result;
511 };
512
513 /* readpage() for symlinks: reads symlink contents into the page and either
514 makes it uptodate and returns 0 or returns error (-EIO) */
515
rock_ridge_symlink_readpage(struct file * file,struct page * page)516 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
517 {
518 struct inode *inode = page->mapping->host;
519 char *link = kmap(page);
520 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
521 unsigned char bufbits = ISOFS_BUFFER_BITS(inode);
522 struct buffer_head *bh;
523 char *rpnt = link;
524 unsigned char *pnt;
525 struct iso_directory_record *raw_inode;
526 CONTINUE_DECLS;
527 int block;
528 int sig;
529 int len;
530 unsigned char *chr;
531 struct rock_ridge *rr;
532
533 if (!inode->i_sb->u.isofs_sb.s_rock)
534 goto error;
535
536 block = inode->i_ino >> bufbits;
537 lock_kernel();
538 bh = sb_bread(inode->i_sb, block);
539 if (!bh)
540 goto out_noread;
541
542 pnt = (unsigned char *) bh->b_data + (inode->i_ino & (bufsize - 1));
543
544 raw_inode = (struct iso_directory_record *) pnt;
545
546 /*
547 * If we go past the end of the buffer, there is some sort of error.
548 */
549 if ((inode->i_ino & (bufsize - 1)) + *pnt > bufsize)
550 goto out_bad_span;
551
552 /* Now test for possible Rock Ridge extensions which will override
553 some of these numbers in the inode structure. */
554
555 SETUP_ROCK_RIDGE(raw_inode, chr, len);
556
557 repeat:
558 while (len > 2) { /* There may be one byte for padding somewhere */
559 rr = (struct rock_ridge *) chr;
560 if (rr->len < 3)
561 goto out; /* Something got screwed up here */
562 sig = isonum_721(chr);
563 chr += rr->len;
564 len -= rr->len;
565 if (len < 0)
566 goto out; /* corrupted isofs */
567
568 switch (sig) {
569 case SIG('R', 'R'):
570 if ((rr->u.RR.flags[0] & RR_SL) == 0)
571 goto out;
572 break;
573 case SIG('S', 'P'):
574 CHECK_SP(goto out);
575 break;
576 case SIG('S', 'L'):
577 rpnt = get_symlink_chunk(rpnt, rr,
578 link + (PAGE_SIZE - 1));
579 if (rpnt == NULL)
580 goto out;
581 break;
582 case SIG('C', 'E'):
583 /* This tells is if there is a continuation record */
584 CHECK_CE;
585 default:
586 break;
587 }
588 }
589 MAYBE_CONTINUE(repeat, inode);
590
591 if (rpnt == link)
592 goto fail;
593 brelse(bh);
594 *rpnt = '\0';
595 unlock_kernel();
596 SetPageUptodate(page);
597 kunmap(page);
598 UnlockPage(page);
599 return 0;
600
601 /* error exit from macro */
602 out:
603 if (buffer)
604 kfree(buffer);
605 goto fail;
606 out_noread:
607 printk("unable to read i-node block");
608 goto fail;
609 out_bad_span:
610 printk("symlink spans iso9660 blocks\n");
611 fail:
612 brelse(bh);
613 unlock_kernel();
614 error:
615 SetPageError(page);
616 kunmap(page);
617 UnlockPage(page);
618 return -EIO;
619 }
620
621 struct address_space_operations isofs_symlink_aops = {
622 readpage: rock_ridge_symlink_readpage
623 };
624