root/fs/isofs/namei.c

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

DEFINITIONS

This source file includes following definitions.
  1. isofs_match
  2. isofs_find_entry
  3. isofs_lookup

   1 /*
   2  *  linux/fs/isofs/namei.c
   3  *
   4  *  (C) 1992  Eric Youngdale Modified for ISO9660 filesystem.
   5  *
   6  *  (C) 1991  Linus Torvalds - minix filesystem
   7  */
   8 
   9 #include <linux/sched.h>
  10 #include <linux/iso_fs.h>
  11 #include <linux/kernel.h>
  12 #include <linux/string.h>
  13 #include <linux/stat.h>
  14 #include <linux/fcntl.h>
  15 #include <asm/segment.h>
  16 #include <linux/malloc.h>
  17 
  18 #include <linux/errno.h>
  19 
  20 /*
  21  * ok, we cannot use strncmp, as the name is not in our data space.
  22  * Thus we'll have to use isofs_match. No big problem. Match also makes
  23  * some sanity tests.
  24  *
  25  * NOTE! unlike strncmp, isofs_match returns 1 for success, 0 for failure.
  26  */
  27 static int isofs_match(int len,const char * name, char * compare, int dlen)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29         if (!compare)
  30                 return 0;
  31 
  32         /* check special "." and ".." files */
  33         if (dlen == 1) {
  34                 /* "." */
  35                 if (compare[0] == 0) {
  36                         if (!len)
  37                                 return 1;
  38                         compare = ".";
  39                 } else if (compare[0] == 1) {
  40                         compare = "..";
  41                         dlen = 2;
  42                 }
  43         }
  44 #if 0
  45         if (len <= 2) printk("Match: %d %d %s %d %d \n",len,dlen,compare,de->name[0], dlen);
  46 #endif
  47         
  48         if (dlen != len)
  49                 return 0;
  50         return !memcmp(name, compare, len);
  51 }
  52 
  53 /*
  54  *      isofs_find_entry()
  55  *
  56  * finds an entry in the specified directory with the wanted name. It
  57  * returns the cache buffer in which the entry was found, and the entry
  58  * itself (as an inode number). It does NOT read the inode of the
  59  * entry - you'll have to do that yourself if you want to.
  60  */
  61 static struct buffer_head * isofs_find_entry(struct inode * dir,
     /* [previous][next][first][last][top][bottom][index][help] */
  62         const char * name, int namelen, unsigned long * ino, unsigned long * ino_back)
  63 {
  64         unsigned long bufsize = ISOFS_BUFFER_SIZE(dir);
  65         unsigned char bufbits = ISOFS_BUFFER_BITS(dir);
  66         unsigned int block, i, f_pos, offset, inode_number;
  67         struct buffer_head * bh;
  68         void * cpnt = NULL;
  69         unsigned int old_offset;
  70         unsigned int backlink;
  71         int dlen, rrflag, match;
  72         int high_sierra = 0;
  73         char * dpnt;
  74         struct iso_directory_record * de;
  75         char c;
  76 
  77         *ino = 0;
  78         if (!dir) return NULL;
  79         
  80         if (!(block = dir->u.isofs_i.i_first_extent)) return NULL;
  81   
  82         f_pos = 0;
  83 
  84         offset = f_pos & (bufsize - 1);
  85         block = isofs_bmap(dir,f_pos >> bufbits);
  86 
  87         if (!block || !(bh = bread(dir->i_dev,block,bufsize))) return NULL;
  88   
  89         while (f_pos < dir->i_size) {
  90                 de = (struct iso_directory_record *) (bh->b_data + offset);
  91                 backlink = dir->i_ino;
  92                 inode_number = (block << bufbits) + (offset & (bufsize - 1));
  93 
  94                 /* If byte is zero, this is the end of file, or time to move to
  95                    the next sector. Usually 2048 byte boundaries. */
  96                 
  97                 if (*((unsigned char *) de) == 0) {
  98                         brelse(bh);
  99                         offset = 0;
 100                         f_pos = ((f_pos & ~(ISOFS_BLOCK_SIZE - 1))
 101                                  + ISOFS_BLOCK_SIZE);
 102                         block = isofs_bmap(dir,f_pos>>bufbits);
 103                         if (!block || !(bh = bread(dir->i_dev,block,bufsize)))
 104                                 return 0;
 105                         continue; /* Will kick out if past end of directory */
 106                 }
 107 
 108                 old_offset = offset;
 109                 offset += *((unsigned char *) de);
 110                 f_pos += *((unsigned char *) de);
 111 
 112                 /* Handle case where the directory entry spans two blocks.
 113                    Usually 1024 byte boundaries */
 114                 if (offset >= bufsize) {
 115                         unsigned int frag1;
 116                         frag1 = bufsize - old_offset;
 117                         cpnt = kmalloc(*((unsigned char *) de),GFP_KERNEL);
 118                         if (!cpnt) return 0;
 119                         memcpy(cpnt, bh->b_data + old_offset, frag1);
 120 
 121                         de = (struct iso_directory_record *) cpnt;
 122                         brelse(bh);
 123                         offset = f_pos & (bufsize - 1);
 124                         block = isofs_bmap(dir,f_pos>>bufbits);
 125                         if (!block || !(bh = bread(dir->i_dev,block,bufsize))) {
 126                                 kfree(cpnt);
 127                                 return 0;
 128                         };
 129                         memcpy((char *)cpnt+frag1, bh->b_data, offset);
 130                 }
 131                 
 132                 /* Handle the '.' case */
 133                 
 134                 if (de->name[0]==0 && de->name_len[0]==1) {
 135                         inode_number = dir->i_ino;
 136                         backlink = 0;
 137                 }
 138                 
 139                 /* Handle the '..' case */
 140 
 141                 if (de->name[0]==1 && de->name_len[0]==1) {
 142 #if 0
 143                         printk("Doing .. (%d %d)",
 144                                dir->i_sb->s_firstdatazone,
 145                                dir->i_ino);
 146 #endif
 147                         if((dir->i_sb->u.isofs_sb.s_firstdatazone) != dir->i_ino)
 148                                 inode_number = dir->u.isofs_i.i_backlink;
 149                         else
 150                                 inode_number = dir->i_ino;
 151                         backlink = 0;
 152                 }
 153     
 154                 /* Do not report hidden or associated files */
 155                 high_sierra = dir->i_sb->u.isofs_sb.s_high_sierra;
 156                 if (de->flags[-high_sierra] & 5) {
 157                   if (cpnt) {
 158                     kfree(cpnt);
 159                     cpnt = NULL;
 160                   };
 161                   continue;
 162                 }
 163                 
 164                 dlen = de->name_len[0];
 165                 dpnt = de->name;
 166                 /* Now convert the filename in the buffer to lower case */
 167                 rrflag = get_rock_ridge_filename(de, &dpnt, &dlen, dir);
 168                 if (rrflag) {
 169                   if (rrflag == -1) goto out; /* Relocated deep directory */
 170                 } else {
 171                   if(dir->i_sb->u.isofs_sb.s_mapping == 'n') {
 172                     for (i = 0; i < dlen; i++) {
 173                       c = dpnt[i];
 174                       if (c >= 'A' && c <= 'Z') c |= 0x20;  /* lower case */
 175                       if (c == ';' && i == dlen-2 && dpnt[i+1] == '1') {
 176                         dlen -= 2;
 177                         break;
 178                       }
 179                       if (c == ';') c = '.';
 180                       de->name[i] = c;
 181                     }
 182                     /* This allows us to match with and without a trailing
 183                        period.  */
 184                     if(dpnt[dlen-1] == '.' && namelen == dlen-1)
 185                       dlen--;
 186                   }
 187                 }
 188                 match = isofs_match(namelen,name,dpnt,dlen);
 189                 if (cpnt) {
 190                         kfree(cpnt);
 191                         cpnt = NULL;
 192                 }
 193 
 194                 if(rrflag) kfree(dpnt);
 195                 if (match) {
 196                         if(inode_number == -1) {
 197                                 /* Should only happen for the '..' entry */
 198                                 inode_number = 
 199                                         isofs_lookup_grandparent(dir,
 200                                            find_rock_ridge_relocation(de,dir));
 201                                 if(inode_number == -1){
 202                                         /* Should never happen */
 203                                         printk("Backlink not properly set.\n");
 204                                         goto out;
 205                                 }
 206                         }
 207                         *ino = inode_number;
 208                         *ino_back = backlink;
 209                         return bh;
 210                 }
 211         }
 212  out:
 213         if (cpnt)
 214                 kfree(cpnt);
 215         brelse(bh);
 216         return NULL;
 217 }
 218 
 219 int isofs_lookup(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 220         struct inode ** result)
 221 {
 222         unsigned long ino, ino_back;
 223         struct buffer_head * bh;
 224 
 225 #ifdef DEBUG
 226         printk("lookup: %x %d\n",dir->i_ino, len);
 227 #endif
 228         *result = NULL;
 229         if (!dir)
 230                 return -ENOENT;
 231 
 232         if (!S_ISDIR(dir->i_mode)) {
 233                 iput(dir);
 234                 return -ENOENT;
 235         }
 236 
 237         ino = 0;
 238 
 239         if (dcache_lookup(dir, name, len, &ino)) ino_back = dir->i_ino;
 240 
 241         if (!ino) {
 242           if (!(bh = isofs_find_entry(dir,name,len, &ino, &ino_back))) {
 243             iput(dir);
 244             return -ENOENT;
 245           }
 246           if (ino_back == dir->i_ino)
 247                 dcache_add(dir, name, len, ino);
 248           brelse(bh);
 249         };
 250 
 251         if (!(*result = iget(dir->i_sb,ino))) {
 252                 iput(dir);
 253                 return -EACCES;
 254         }
 255 
 256         /* We need this backlink for the ".." entry unless the name that we
 257            are looking up traversed a mount point (in which case the inode
 258            may not even be on an iso9660 filesystem, and writing to
 259            u.isofs_i would only cause memory corruption).
 260         */
 261         
 262         if (ino_back && !(*result)->i_pipe && (*result)->i_sb == dir->i_sb) {
 263           (*result)->u.isofs_i.i_backlink = ino_back; 
 264         }
 265         
 266         iput(dir);
 267         return 0;
 268 }

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