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