VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / fs / isofs / rock.c
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/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>
19
20 #include "rock.h"
21
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. */
28
29 #define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
30
31
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 */
41
42 #define CONTINUE_DECLS \
43   int cont_extent = 0, cont_offset = 0, cont_size = 0;   \
44   void *buffer = NULL
45
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);}
50
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   }                                                             \
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       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");    \
86   }}
87
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)
91 {
92   int len;
93   unsigned char * chr;
94   CONTINUE_DECLS;
95   int retnamlen = 0, truncate=0;
96  
97   if (!ISOFS_SB(inode->i_sb)->s_rock) return 0;
98   *retname = 0;
99
100   SETUP_ROCK_RIDGE(de, chr, len);
101  repeat:
102   {
103     struct rock_ridge * rr;
104     int sig;
105     
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;
112
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;
125         /*
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.
131          */
132         if (rr->u.NM.flags & 6) {
133           break;
134         }
135
136         if (rr->u.NM.flags & ~1) {
137           printk("Unsupported NM flag settings (%d)\n",rr->u.NM.flags);
138           break;
139         }
140         if((strlen(retname) + rr->len - 5) >= 254) {
141           truncate = 1;
142           break;
143         }
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;
152       }
153     }
154   }
155   MAYBE_CONTINUE(repeat,inode);
156   return retnamlen; /* If 0, this file did not have a NM field */
157  out:
158   if(buffer) kfree(buffer);
159   return 0;
160 }
161
162 int parse_rock_ridge_inode_internal(struct iso_directory_record * de,
163                                     struct inode * inode,int regard_xa){
164   int len;
165   unsigned char * chr;
166   int symlink_len = 0;
167   CONTINUE_DECLS;
168
169   if (!ISOFS_SB(inode->i_sb)->s_rock) return 0;
170
171   SETUP_ROCK_RIDGE(de, chr, len);
172   if (regard_xa)
173    {
174      chr+=14;
175      len-=14;
176      if (len<0) len=0;
177    };
178    
179  repeat:
180   {
181     int cnt, sig;
182     struct inode * reloc;
183     struct rock_ridge * rr;
184     int rootflag;
185     
186     while (len > 1){ /* There may be one byte for padding somewhere */
187       rr = (struct rock_ridge *) chr;
188       if (rr->len == 0) goto out; /* Something got screwed up here */
189       sig = isonum_721(chr);
190       chr += rr->len; 
191       len -= rr->len;
192       
193       switch(sig){
194 #ifndef CONFIG_ZISOFS           /* No flag for SF or ZF */
195       case SIG('R','R'):
196         if((rr->u.RR.flags[0] & 
197             (RR_PX | RR_TF | RR_SL | RR_CL)) == 0) goto out;
198         break;
199 #endif
200       case SIG('S','P'):
201         CHECK_SP(goto out);
202         break;
203       case SIG('C','E'):
204         CHECK_CE;
205         break;
206       case SIG('E','R'):
207         ISOFS_SB(inode->i_sb)->s_rock = 1;
208         printk(KERN_DEBUG "ISO 9660 Extensions: ");
209         { int p;
210           for(p=0;p<rr->u.ER.len_id;p++) printk("%c",rr->u.ER.data[p]);
211         }
212           printk("\n");
213         break;
214       case SIG('P','X'):
215         inode->i_mode  = isonum_733(rr->u.PX.mode);
216         inode->i_nlink = isonum_733(rr->u.PX.n_links);
217         inode->i_uid   = isonum_733(rr->u.PX.uid);
218         inode->i_gid   = isonum_733(rr->u.PX.gid);
219         break;
220       case SIG('P','N'):
221         { int high, low;
222           high = isonum_733(rr->u.PN.dev_high);
223           low = isonum_733(rr->u.PN.dev_low);
224           /*
225            * The Rock Ridge standard specifies that if sizeof(dev_t) <= 4,
226            * then the high field is unused, and the device number is completely
227            * stored in the low field.  Some writers may ignore this subtlety,
228            * and as a result we test to see if the entire device number is
229            * stored in the low field, and use that.
230            */
231           if((low & ~0xff) && high == 0) {
232             inode->i_rdev = MKDEV(low >> 8, low & 0xff);
233           } else {
234             inode->i_rdev = MKDEV(high, low);
235           }
236         }
237         break;
238       case SIG('T','F'):
239         /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
240            Try to handle this correctly for either case. */
241         cnt = 0; /* Rock ridge never appears on a High Sierra disk */
242         if(rr->u.TF.flags & TF_CREATE) { 
243           inode->i_ctime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
244           inode->i_ctime.tv_nsec = 0;
245         }
246         if(rr->u.TF.flags & TF_MODIFY) {
247           inode->i_mtime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
248           inode->i_mtime.tv_nsec = 0;
249         }
250         if(rr->u.TF.flags & TF_ACCESS) {
251           inode->i_atime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
252           inode->i_atime.tv_nsec = 0;
253         }
254         if(rr->u.TF.flags & TF_ATTRIBUTES) { 
255           inode->i_ctime.tv_sec = iso_date(rr->u.TF.times[cnt++].time, 0);
256           inode->i_ctime.tv_nsec = 0;
257         } 
258         break;
259       case SIG('S','L'):
260         {int slen;
261          struct SL_component * slp;
262          struct SL_component * oldslp;
263          slen = rr->len - 5;
264          slp = &rr->u.SL.link;
265          inode->i_size = symlink_len;
266          while (slen > 1){
267            rootflag = 0;
268            switch(slp->flags &~1){
269            case 0:
270              inode->i_size += slp->len;
271              break;
272            case 2:
273              inode->i_size += 1;
274              break;
275            case 4:
276              inode->i_size += 2;
277              break;
278            case 8:
279              rootflag = 1;
280              inode->i_size += 1;
281              break;
282            default:
283              printk("Symlink component flag not implemented\n");
284            }
285            slen -= slp->len + 2;
286            oldslp = slp;
287            slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
288
289            if(slen < 2) {
290              if(    ((rr->u.SL.flags & 1) != 0) 
291                     && ((oldslp->flags & 1) == 0) ) inode->i_size += 1;
292              break;
293            }
294
295            /*
296             * If this component record isn't continued, then append a '/'.
297             */
298            if (!rootflag && (oldslp->flags & 1) == 0)
299                    inode->i_size += 1;
300          }
301         }
302         symlink_len = inode->i_size;
303         break;
304       case SIG('R','E'):
305         printk(KERN_WARNING "Attempt to read inode for relocated directory\n");
306         goto out;
307       case SIG('C','L'):
308         ISOFS_I(inode)->i_first_extent = isonum_733(rr->u.CL.location);
309         reloc = isofs_iget(inode->i_sb, ISOFS_I(inode)->i_first_extent, 0);
310         if (!reloc)
311                 goto out;
312         inode->i_mode = reloc->i_mode;
313         inode->i_nlink = reloc->i_nlink;
314         inode->i_uid = reloc->i_uid;
315         inode->i_gid = reloc->i_gid;
316         inode->i_rdev = reloc->i_rdev;
317         inode->i_size = reloc->i_size;
318         inode->i_blocks = reloc->i_blocks;
319         inode->i_atime = reloc->i_atime;
320         inode->i_ctime = reloc->i_ctime;
321         inode->i_mtime = reloc->i_mtime;
322         iput(reloc);
323         break;
324 #ifdef CONFIG_ZISOFS
325       case SIG('Z','F'):
326               if ( !ISOFS_SB(inode->i_sb)->s_nocompress ) {
327                       int algo;
328                       algo = isonum_721(rr->u.ZF.algorithm);
329                       if ( algo == SIG('p','z') ) {
330                               int block_shift = isonum_711(&rr->u.ZF.parms[1]);
331                               if ( block_shift < PAGE_CACHE_SHIFT || block_shift > 17 ) {
332                                       printk(KERN_WARNING "isofs: Can't handle ZF block size of 2^%d\n", block_shift);
333                               } else {
334                                 /* Note: we don't change i_blocks here */
335                                       ISOFS_I(inode)->i_file_format = isofs_file_compressed;
336                                 /* Parameters to compression algorithm (header size, block size) */
337                                       ISOFS_I(inode)->i_format_parm[0] = isonum_711(&rr->u.ZF.parms[0]);
338                                       ISOFS_I(inode)->i_format_parm[1] = isonum_711(&rr->u.ZF.parms[1]);
339                                       inode->i_size = isonum_733(rr->u.ZF.real_size);
340                               }
341                       } else {
342                               printk(KERN_WARNING "isofs: Unknown ZF compression algorithm: %c%c\n",
343                                      rr->u.ZF.algorithm[0], rr->u.ZF.algorithm[1]);
344                       }
345               }
346               break;
347 #endif
348       default:
349         break;
350       }
351     }
352   }
353   MAYBE_CONTINUE(repeat,inode);
354   return 0;
355  out:
356   if(buffer) kfree(buffer);
357   return 0;
358 }
359
360 static char *get_symlink_chunk(char *rpnt, struct rock_ridge *rr, char *plimit)
361 {
362         int slen;
363         int rootflag;
364         struct SL_component *oldslp;
365         struct SL_component *slp;
366         slen = rr->len - 5;
367         slp = &rr->u.SL.link;
368         while (slen > 1) {
369                 rootflag = 0;
370                 switch (slp->flags & ~1) {
371                 case 0:
372                         if (slp->len > plimit - rpnt)
373                                 return NULL;
374                         memcpy(rpnt, slp->text, slp->len);
375                         rpnt+=slp->len;
376                         break;
377                 case 2:
378                         if (rpnt >= plimit)
379                                 return NULL;
380                         *rpnt++='.';
381                         break;
382                 case 4:
383                         if (2 > plimit - rpnt)
384                                 return NULL;
385                         *rpnt++='.';
386                         *rpnt++='.';
387                         break;
388                 case 8:
389                         if (rpnt >= plimit)
390                                 return NULL;
391                         rootflag = 1;
392                         *rpnt++='/';
393                         break;
394                 default:
395                         printk("Symlink component flag not implemented (%d)\n",
396                              slp->flags);
397                 }
398                 slen -= slp->len + 2;
399                 oldslp = slp;
400                 slp = (struct SL_component *) ((char *) slp + slp->len + 2);
401
402                 if (slen < 2) {
403                         /*
404                          * If there is another SL record, and this component
405                          * record isn't continued, then add a slash.
406                          */
407                         if ((!rootflag) && (rr->u.SL.flags & 1) &&
408                             !(oldslp->flags & 1)) {
409                                 if (rpnt >= plimit)
410                                         return NULL;
411                                 *rpnt++='/';
412                         }
413                         break;
414                 }
415
416                 /*
417                  * If this component record isn't continued, then append a '/'.
418                  */
419                 if (!rootflag && !(oldslp->flags & 1)) {
420                         if (rpnt >= plimit)
421                                 return NULL;
422                         *rpnt++='/';
423                 }
424         }
425         return rpnt;
426 }
427
428 int parse_rock_ridge_inode(struct iso_directory_record * de,
429                            struct inode * inode)
430 {
431    int result=parse_rock_ridge_inode_internal(de,inode,0);
432    /* if rockridge flag was reset and we didn't look for attributes
433     * behind eventual XA attributes, have a look there */
434    if ((ISOFS_SB(inode->i_sb)->s_rock_offset==-1)
435        &&(ISOFS_SB(inode->i_sb)->s_rock==2))
436      {
437         result=parse_rock_ridge_inode_internal(de,inode,14);
438      };
439    return result;
440 };
441
442 /* readpage() for symlinks: reads symlink contents into the page and either
443    makes it uptodate and returns 0 or returns error (-EIO) */
444
445 static int rock_ridge_symlink_readpage(struct file *file, struct page *page)
446 {
447         struct inode *inode = page->mapping->host;
448         struct iso_inode_info *ei = ISOFS_I(inode);
449         char *link = kmap(page);
450         unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
451         struct buffer_head *bh;
452         char *rpnt = link;
453         unsigned char *pnt;
454         struct iso_directory_record *raw_inode;
455         CONTINUE_DECLS;
456         unsigned long block, offset;
457         int sig;
458         int len;
459         unsigned char *chr;
460         struct rock_ridge *rr;
461
462         if (!ISOFS_SB(inode->i_sb)->s_rock)
463                 panic ("Cannot have symlink with high sierra variant of iso filesystem\n");
464
465         block = ei->i_iget5_block;
466         lock_kernel();
467         bh = sb_bread(inode->i_sb, block);
468         if (!bh)
469                 goto out_noread;
470
471         offset = ei->i_iget5_offset;
472         pnt = (unsigned char *) bh->b_data + offset;
473
474         raw_inode = (struct iso_directory_record *) pnt;
475
476         /*
477          * If we go past the end of the buffer, there is some sort of error.
478          */
479         if (offset + *pnt > bufsize)
480                 goto out_bad_span;
481
482         /* Now test for possible Rock Ridge extensions which will override
483            some of these numbers in the inode structure. */
484
485         SETUP_ROCK_RIDGE(raw_inode, chr, len);
486
487       repeat:
488         while (len > 1) { /* There may be one byte for padding somewhere */
489                 rr = (struct rock_ridge *) chr;
490                 if (rr->len == 0)
491                         goto out;       /* Something got screwed up here */
492                 sig = isonum_721(chr);
493                 chr += rr->len;
494                 len -= rr->len;
495
496                 switch (sig) {
497                 case SIG('R', 'R'):
498                         if ((rr->u.RR.flags[0] & RR_SL) == 0)
499                                 goto out;
500                         break;
501                 case SIG('S', 'P'):
502                         CHECK_SP(goto out);
503                         break;
504                 case SIG('S', 'L'):
505                         rpnt = get_symlink_chunk(rpnt, rr,
506                                                  link + (PAGE_SIZE - 1));
507                         if (rpnt == NULL)
508                                 goto out;
509                         break;
510                 case SIG('C', 'E'):
511                         /* This tells is if there is a continuation record */
512                         CHECK_CE;
513                 default:
514                         break;
515                 }
516         }
517         MAYBE_CONTINUE(repeat, inode);
518
519         if (rpnt == link)
520                 goto fail;
521         brelse(bh);
522         *rpnt = '\0';
523         unlock_kernel();
524         SetPageUptodate(page);
525         kunmap(page);
526         unlock_page(page);
527         return 0;
528
529         /* error exit from macro */
530       out:
531         if (buffer)
532                 kfree(buffer);
533         goto fail;
534       out_noread:
535         printk("unable to read i-node block");
536         goto fail;
537       out_bad_span:
538         printk("symlink spans iso9660 blocks\n");
539       fail:
540         brelse(bh);
541         unlock_kernel();
542         SetPageError(page);
543         kunmap(page);
544         unlock_page(page);
545         return -EIO;
546 }
547
548 struct address_space_operations isofs_symlink_aops = {
549         .readpage       = rock_ridge_symlink_readpage
550 };