root/fs/isofs/rock.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. find_rock_ridge_relocation
  2. get_rock_ridge_filename
  3. parse_rock_ridge_inode
  4. get_rock_ridge_symlink

   1 /*
   2  *  linux/fs/isofs/rock.c
   3  *
   4  *  (C) 1992, 1993  Eric Youngdale
   5  *
   6  *  Rock Ridge Extensions to iso9660
   7  */
   8 #include <linux/config.h>
   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/malloc.h>
  15 
  16 #include "rock.h"
  17 
  18 /* These functions are designed to read the system areas of a directory record
  19  * and extract relevant information.  There are different functions provided
  20  * depending upon what information we need at the time.  One function fills
  21  * out an inode structure, a second one extracts a filename, a third one
  22  * returns a symbolic link name, and a fourth one returns the extent number
  23  * for the file. */
  24 
  25 #define SIG(A,B) ((A << 8) | B)
  26 
  27 
  28 /* This is a way of ensuring that we have something in the system
  29    use fields that is compatible with Rock Ridge */
  30 #define CHECK_SP(FAIL)                          \
  31       if(rr->u.SP.magic[0] != 0xbe) FAIL;       \
  32       if(rr->u.SP.magic[1] != 0xef) FAIL;
  33 
  34 /* We define a series of macros because each function must do exactly the
  35    same thing in certain places.  We use the macros to ensure that everything
  36    is done correctly */
  37 
  38 #define CONTINUE_DECLS \
  39   int cont_extent = 0, cont_offset = 0, cont_size = 0;   \
  40   void * buffer = 0
  41 
  42 #define CHECK_CE                                \
  43       {cont_extent = isonum_733(rr->u.CE.extent); \
  44       cont_offset = isonum_733(rr->u.CE.offset); \
  45       cont_size = isonum_733(rr->u.CE.size);}
  46 
  47 #define SETUP_ROCK_RIDGE(DE,CHR,LEN)                            \
  48   {LEN= sizeof(struct iso_directory_record) + DE->name_len[0];  \
  49   if(LEN & 1) LEN++;                                            \
  50   CHR = ((unsigned char *) DE) + LEN;                           \
  51   LEN = *((unsigned char *) DE) - LEN;}
  52 
  53 #define MAYBE_CONTINUE(LABEL,DEV) \
  54   {if (buffer) kfree(buffer); \
  55   if (cont_extent){ \
  56     int block, offset, offset1; \
  57     struct buffer_head * bh; \
  58     buffer = kmalloc(cont_size,GFP_KERNEL); \
  59     if (!buffer) goto out; \
  60     block = cont_extent; \
  61     offset = cont_offset; \
  62     offset1 = 0; \
  63     if(ISOFS_BUFFER_SIZE(DEV) == 1024) {     \
  64       block <<= 1;    \
  65       if (offset >= 1024) block++; \
  66       offset &= 1023; \
  67       if(offset + cont_size >= 1024) { \
  68           bh = bread(DEV->i_dev, block++, ISOFS_BUFFER_SIZE(DEV)); \
  69           if(!bh) {printk("Unable to read continuation Rock Ridge record\n"); \
  70                      kfree(buffer); \
  71                      buffer = NULL; } else { \
  72             memcpy(buffer, bh->b_data + offset, 1024 - offset); \
  73             brelse(bh); \
  74             offset1 = 1024 - offset; \
  75             offset = 0;} \
  76       }  \
  77     };     \
  78     if(buffer) { \
  79       bh = bread(DEV->i_dev, block, ISOFS_BUFFER_SIZE(DEV)); \
  80       if(bh){       \
  81         memcpy(buffer + offset1, bh->b_data + offset, cont_size - offset1); \
  82         brelse(bh); \
  83         chr = (unsigned char *) buffer; \
  84         len = cont_size; \
  85         cont_extent = 0; \
  86         cont_size = 0; \
  87         cont_offset = 0; \
  88         goto LABEL; \
  89       };    \
  90     } \
  91     printk("Unable to read rock-ridge attributes\n");    \
  92   }}
  93 
  94 /* This is the inner layer of the get filename routine, and is called
  95    for each system area and continuation record related to the file */
  96 
  97 int find_rock_ridge_relocation(struct iso_directory_record * de, 
     /* [previous][next][first][last][top][bottom][index][help] */
  98                                struct inode * inode) {
  99   int flag;
 100   int len;
 101   int retval;
 102   unsigned char * chr;
 103   CONTINUE_DECLS;
 104   flag = 0;
 105   
 106   /* If this is a '..' then we are looking for the parent, otherwise we
 107      are looking for the child */
 108   
 109   if (de->name[0]==1 && de->name_len[0]==1) flag = 1;
 110   /* Return value if we do not find appropriate record. */
 111   retval = isonum_733 (de->extent);
 112   
 113   if (!inode->i_sb->u.isofs_sb.s_rock) return retval;
 114 
 115   SETUP_ROCK_RIDGE(de, chr, len);
 116  repeat:
 117   {
 118     int rrflag, sig;
 119     struct rock_ridge * rr;
 120     
 121     while (len > 1){ /* There may be one byte for padding somewhere */
 122       rr = (struct rock_ridge *) chr;
 123       if (rr->len == 0) goto out; /* Something got screwed up here */
 124       sig = (chr[0] << 8) + chr[1];
 125       chr += rr->len; 
 126       len -= rr->len;
 127 
 128       switch(sig){
 129       case SIG('R','R'):
 130         rrflag = rr->u.RR.flags[0];
 131         if (flag && !(rrflag & RR_PL)) goto out;
 132         if (!flag && !(rrflag & RR_CL)) goto out;
 133         break;
 134       case SIG('S','P'):
 135         CHECK_SP(goto out);
 136         break;
 137       case SIG('C','L'):
 138 #ifdef DEBUG
 139         printk("RR: CL\n");
 140 #endif
 141         if (flag == 0) {
 142           retval = isonum_733(rr->u.CL.location);
 143           goto out;
 144         };
 145         break;
 146       case SIG('P','L'):
 147 #ifdef DEBUG
 148         printk("RR: PL\n");
 149 #endif
 150         if (flag != 0) {
 151           retval = isonum_733(rr->u.PL.location);
 152           goto out;
 153         };
 154         break;
 155       case SIG('C','E'):
 156         CHECK_CE; /* This tells is if there is a continuation record */
 157         break;
 158       default:
 159         break;
 160       }
 161     };
 162   };
 163   MAYBE_CONTINUE(repeat, inode);
 164   return retval;
 165  out:
 166   if(buffer) kfree(buffer);
 167   return retval;
 168 }
 169 
 170 int get_rock_ridge_filename(struct iso_directory_record * de,
     /* [previous][next][first][last][top][bottom][index][help] */
 171                            char ** name, int * namlen, struct inode * inode)
 172 {
 173   int len;
 174   unsigned char * chr;
 175   CONTINUE_DECLS;
 176   char * retname = NULL;
 177   int retnamlen = 0, truncate=0;
 178  
 179   if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
 180 
 181   SETUP_ROCK_RIDGE(de, chr, len);
 182  repeat:
 183   {
 184     struct rock_ridge * rr;
 185     int sig;
 186     
 187     while (len > 1){ /* There may be one byte for padding somewhere */
 188       rr = (struct rock_ridge *) chr;
 189       if (rr->len == 0) goto out; /* Something got screwed up here */
 190       sig = (chr[0] << 8) + chr[1];
 191       chr += rr->len; 
 192       len -= rr->len;
 193 
 194       switch(sig){
 195       case SIG('R','R'):
 196         if((rr->u.RR.flags[0] & RR_NM) == 0) goto out;
 197         break;
 198       case SIG('S','P'):
 199         CHECK_SP(goto out);
 200         break;
 201       case SIG('C','E'):
 202         CHECK_CE;
 203         break;
 204       case SIG('N','M'):
 205         if (truncate) break;
 206         if (rr->u.NM.flags & ~1) {
 207           printk("Unsupported NM flag settings (%d)\n",rr->u.NM.flags);
 208           break;
 209         };
 210         if (!retname){
 211           retname = (char *) kmalloc (255,GFP_KERNEL);
 212           /* This may be a waste, but we only
 213              need this for a moment.  The layers
 214              that call this function should
 215              deallocate the mem fairly soon
 216              after control is returned */
 217 
 218           if (!retname) goto out;
 219           *retname = 0; /* Zero length string */
 220           retnamlen = 0;
 221         };
 222         if((strlen(retname) + rr->len - 5) >= 254) {
 223           truncate = 1;
 224           break;
 225         };
 226         strncat(retname, rr->u.NM.name, rr->len - 5);
 227         retnamlen += rr->len - 5;
 228         break;
 229       case SIG('R','E'):
 230 #ifdef DEBUG
 231         printk("RR: RE (%x)\n", inode->i_ino);
 232 #endif
 233         if (buffer) kfree(buffer);
 234         if (retname) kfree(retname);
 235         return -1;
 236       default:
 237         break;
 238       }
 239     };
 240   }
 241   MAYBE_CONTINUE(repeat,inode);
 242   if(retname){
 243     *name = retname;
 244     *namlen = retnamlen;
 245     return 1;
 246   };
 247   return 0;  /* This file did not have a NM field */
 248  out:
 249   if(buffer) kfree(buffer);
 250   if (retname) kfree(retname);
 251   return 0;
 252 }
 253 
 254 int parse_rock_ridge_inode(struct iso_directory_record * de,
     /* [previous][next][first][last][top][bottom][index][help] */
 255                            struct inode * inode){
 256   int len;
 257   unsigned char * chr;
 258   CONTINUE_DECLS;
 259 
 260   if (!inode->i_sb->u.isofs_sb.s_rock) return 0;
 261 
 262   SETUP_ROCK_RIDGE(de, chr, len);
 263  repeat:
 264   {
 265     int cnt, sig;
 266     struct inode * reloc;
 267     struct rock_ridge * rr;
 268     int rootflag;
 269     
 270     while (len > 1){ /* There may be one byte for padding somewhere */
 271       rr = (struct rock_ridge *) chr;
 272       if (rr->len == 0) goto out; /* Something got screwed up here */
 273       sig = (chr[0] << 8) + chr[1];
 274       chr += rr->len; 
 275       len -= rr->len;
 276       
 277       switch(sig){
 278       case SIG('R','R'):
 279         if((rr->u.RR.flags[0] & 
 280             (RR_PX | RR_TF | RR_SL | RR_CL)) == 0) goto out;
 281         break;
 282       case SIG('S','P'):
 283         CHECK_SP(goto out);
 284         break;
 285       case SIG('C','E'):
 286         CHECK_CE;
 287         break;
 288       case SIG('E','R'):
 289         printk("ISO9660 Extensions: ");
 290         { int p;
 291           for(p=0;p<rr->u.ER.len_id;p++) printk("%c",rr->u.ER.data[p]);
 292         };
 293           printk("\n");
 294         break;
 295       case SIG('P','X'):
 296         inode->i_mode  = isonum_733(rr->u.PX.mode);
 297         inode->i_nlink = isonum_733(rr->u.PX.n_links);
 298         inode->i_uid   = isonum_733(rr->u.PX.uid);
 299         inode->i_gid   = isonum_733(rr->u.PX.gid);
 300         break;
 301       case SIG('P','N'):
 302         { int high, low;
 303           high = isonum_733(rr->u.PN.dev_high);
 304           low = isonum_733(rr->u.PN.dev_low);
 305           inode->i_rdev = ((high << 8) | (low & 0xff)) & 0xffff;
 306         };
 307         break;
 308       case SIG('T','F'):
 309         /* Some RRIP writers incorrectly place ctime in the TF_CREATE field.
 310            Try and handle this correctly for either case. */
 311         cnt = 0; /* Rock ridge never appears on a High Sierra disk */
 312         if(rr->u.TF.flags & TF_CREATE) 
 313           inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
 314         if(rr->u.TF.flags & TF_MODIFY) 
 315           inode->i_mtime = iso_date(rr->u.TF.times[cnt++].time, 0);
 316         if(rr->u.TF.flags & TF_ACCESS) 
 317           inode->i_atime = iso_date(rr->u.TF.times[cnt++].time, 0);
 318         if(rr->u.TF.flags & TF_ATTRIBUTES) 
 319           inode->i_ctime = iso_date(rr->u.TF.times[cnt++].time, 0);
 320         break;
 321       case SIG('S','L'):
 322         {int slen;
 323          struct SL_component * slp;
 324          slen = rr->len - 5;
 325          slp = &rr->u.SL.link;
 326          inode->i_size = 0;
 327          while (slen > 1){
 328            rootflag = 0;
 329            switch(slp->flags &~1){
 330            case 0:
 331              inode->i_size += slp->len;
 332              break;
 333            case 2:
 334              inode->i_size += 1;
 335              break;
 336            case 4:
 337              inode->i_size += 2;
 338              break;
 339            case 8:
 340              rootflag = 1;
 341              inode->i_size += 1;
 342              break;
 343            default:
 344              printk("Symlink component flag not implemented\n");
 345            };
 346            slen -= slp->len + 2;
 347            slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
 348 
 349            if(slen < 2) break;
 350            if(!rootflag) inode->i_size += 1;
 351          };
 352        };
 353         break;
 354       case SIG('R','E'):
 355         printk("Attempt to read inode for relocated directory\n");
 356         goto out;
 357       case SIG('C','L'):
 358 #ifdef DEBUG
 359         printk("RR CL (%x)\n",inode->i_ino);
 360 #endif
 361         inode->u.isofs_i.i_first_extent = isonum_733(rr->u.CL.location) <<
 362                 inode -> i_sb -> u.isofs_sb.s_log_zone_size;
 363         reloc = iget(inode->i_sb, inode->u.isofs_i.i_first_extent);
 364         inode->i_mode = reloc->i_mode;
 365         inode->i_nlink = reloc->i_nlink;
 366         inode->i_uid = reloc->i_uid;
 367         inode->i_gid = reloc->i_gid;
 368         inode->i_rdev = reloc->i_rdev;
 369         inode->i_size = reloc->i_size;
 370         inode->i_atime = reloc->i_atime;
 371         inode->i_ctime = reloc->i_ctime;
 372         inode->i_mtime = reloc->i_mtime;
 373         iput(reloc);
 374         break;
 375       default:
 376         break;
 377       }
 378     };
 379   }
 380   MAYBE_CONTINUE(repeat,inode);
 381   return 0;
 382  out:
 383   if(buffer) kfree(buffer);
 384   return 0;
 385 }
 386 
 387 
 388 /* Returns the name of the file that this inode is symlinked to.  This is
 389    in malloc'd memory, so it needs to be freed, once we are through with it */
 390 
 391 char * get_rock_ridge_symlink(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 392 {
 393   unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
 394   unsigned char bufbits = ISOFS_BUFFER_BITS(inode);
 395   struct buffer_head * bh;
 396   unsigned char * pnt;
 397   void * cpnt = NULL;
 398   char * rpnt;
 399   struct iso_directory_record * raw_inode;
 400   CONTINUE_DECLS;
 401   int block;
 402   int sig;
 403   int rootflag;
 404   int len;
 405   unsigned char * chr;
 406   struct rock_ridge * rr;
 407   
 408   if (!inode->i_sb->u.isofs_sb.s_rock)
 409     panic("Cannot have symlink with high sierra variant of iso filesystem\n");
 410 
 411   rpnt = 0;
 412   
 413   block = inode->i_ino >> bufbits;
 414   if (!(bh=bread(inode->i_dev,block, bufsize))) {
 415     printk("unable to read i-node block");
 416     return NULL;
 417   };
 418   
 419   pnt = ((unsigned char *) bh->b_data) + (inode->i_ino & (bufsize - 1));
 420   
 421   raw_inode = ((struct iso_directory_record *) pnt);
 422   
 423   if ((inode->i_ino & (bufsize - 1)) + *pnt > bufsize){
 424     int frag1, offset;
 425     
 426     offset = (inode->i_ino & (bufsize - 1));
 427     frag1 = bufsize - offset;
 428     cpnt = kmalloc(*pnt,GFP_KERNEL);
 429     if(!cpnt) return NULL;
 430     memcpy(cpnt, bh->b_data + offset, frag1);
 431     brelse(bh);
 432     if (!(bh = bread(inode->i_dev,++block, bufsize))) {
 433       kfree(cpnt);
 434       printk("unable to read i-node block");
 435       return NULL;
 436     };
 437     offset += *pnt - bufsize;
 438     memcpy((char *)cpnt+frag1, bh->b_data, offset);
 439     pnt = ((unsigned char *) cpnt);
 440     raw_inode = ((struct iso_directory_record *) pnt);
 441   };
 442   
 443   /* Now test for possible Rock Ridge extensions which will override some of
 444      these numbers in the inode structure. */
 445   
 446   SETUP_ROCK_RIDGE(raw_inode, chr, len);
 447   
 448  repeat:
 449   while (len > 1){ /* There may be one byte for padding somewhere */
 450     if (rpnt) break;
 451     rr = (struct rock_ridge *) chr;
 452     if (rr->len == 0) goto out; /* Something got screwed up here */
 453     sig = (chr[0] << 8) + chr[1];
 454     chr += rr->len; 
 455     len -= rr->len;
 456     
 457     switch(sig){
 458     case SIG('R','R'):
 459       if((rr->u.RR.flags[0] & RR_SL) == 0) goto out;
 460       break;
 461     case SIG('S','P'):
 462       CHECK_SP(goto out);
 463       break;
 464     case SIG('S','L'):
 465       {int slen;
 466        struct SL_component * slp;
 467        slen = rr->len - 5;
 468        slp = &rr->u.SL.link;
 469        while (slen > 1){
 470          if (!rpnt){
 471            rpnt = (char *) kmalloc (inode->i_size +1, GFP_KERNEL);
 472            if (!rpnt) goto out;
 473            *rpnt = 0;
 474          };
 475          rootflag = 0;
 476          switch(slp->flags &~1){
 477          case 0:
 478            strncat(rpnt,slp->text, slp->len);
 479            break;
 480          case 2:
 481            strcat(rpnt,".");
 482            break;
 483          case 4:
 484            strcat(rpnt,"..");
 485            break;
 486          case 8:
 487            rootflag = 1;
 488            strcat(rpnt,"/");
 489            break;
 490          default:
 491            printk("Symlink component flag not implemented (%d)\n",slen);
 492          };
 493          slen -= slp->len + 2;
 494          slp = (struct SL_component *) (((char *) slp) + slp->len + 2);
 495 
 496          if(slen < 2) break;
 497          if(!rootflag) strcat(rpnt,"/");
 498        };
 499        break;
 500      default:
 501        break;
 502      }
 503     };
 504   };
 505   MAYBE_CONTINUE(repeat,inode);
 506   brelse(bh);
 507   
 508   if (cpnt) {
 509     kfree(cpnt);
 510     cpnt = NULL;
 511   };
 512 
 513   return rpnt;
 514  out:
 515   if(buffer) kfree(buffer);
 516   return 0;
 517 }
 518 
 519 
 520 
 521 
 522 
 523 

/* [previous][next][first][last][top][bottom][index][help] */