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