| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* linux/fs/isofs/dir.c |
| 4 |
* |
| 5 |
* (C) 1992, 1993, 1994 Eric Youngdale Modified for ISO 9660 filesystem. |
| 6 |
* |
| 7 |
* (C) 1991 Linus Torvalds - minix filesystem |
| 8 |
* |
| 9 |
* Steve Beynon : Missing last directory entries fixed |
| 10 |
* ([email protected]) : 21st June 1996 |
| 11 |
* |
| 12 |
* isofs directory handling functions |
| 13 |
*/ |
| 14 |
#include <linux/gfp.h> |
| 15 |
#include <linux/filelock.h> |
| 16 |
#include "isofs.h" |
| 17 |
|
| 18 |
int isofs_name_translate(struct iso_directory_record *de, char *new, struct inode *inode) |
| 19 |
{ |
| 20 |
char * old = de->name; |
| 21 |
int len = de->name_len[0]; |
| 22 |
int i; |
| 23 |
|
| 24 |
for (i = 0; i < len; i++) { |
| 25 |
unsigned char c = old[i]; |
| 26 |
if (!c) |
| 27 |
break; |
| 28 |
|
| 29 |
if (c >= 'A' && c <= 'Z') |
| 30 |
c |= 0x20; /* lower case */ |
| 31 |
|
| 32 |
/* Drop trailing '.;1' (ISO 9660:1988 7.5.1 requires period) */ |
| 33 |
if (c == '.' && i == len - 3 && old[i + 1] == ';' && old[i + 2] == '1') |
| 34 |
break; |
| 35 |
|
| 36 |
/* Drop trailing ';1' */ |
| 37 |
if (c == ';' && i == len - 2 && old[i + 1] == '1') |
| 38 |
break; |
| 39 |
|
| 40 |
/* Convert remaining ';' to '.' */ |
| 41 |
/* Also '/' to '.' (broken Acorn-generated ISO9660 images) */ |
| 42 |
if (c == ';' || c == '/') |
| 43 |
c = '.'; |
| 44 |
|
| 45 |
new[i] = c; |
| 46 |
} |
| 47 |
return i; |
| 48 |
} |
| 49 |
|
| 50 |
/* Acorn extensions written by Matthew Wilcox <[email protected]> 1998 */ |
| 51 |
int get_acorn_filename(struct iso_directory_record *de, |
| 52 |
char *retname, struct inode *inode) |
| 53 |
{ |
| 54 |
int std; |
| 55 |
unsigned char *chr; |
| 56 |
int retnamlen = isofs_name_translate(de, retname, inode); |
| 57 |
|
| 58 |
if (retnamlen == 0) |
| 59 |
return 0; |
| 60 |
std = sizeof(struct iso_directory_record) + de->name_len[0]; |
| 61 |
if (std & 1) |
| 62 |
std++; |
| 63 |
if (de->length[0] - std != 32) |
| 64 |
return retnamlen; |
| 65 |
chr = ((unsigned char *) de) + std; |
| 66 |
if (strncmp(chr, "ARCHIMEDES", 10)) |
| 67 |
return retnamlen; |
| 68 |
if ((*retname == '_') && ((chr[19] & 1) == 1)) |
| 69 |
*retname = '!'; |
| 70 |
if (((de->flags[0] & 2) == 0) && (chr[13] == 0xff) |
| 71 |
&& ((chr[12] & 0xf0) == 0xf0)) { |
| 72 |
retname[retnamlen] = ','; |
| 73 |
sprintf(retname+retnamlen+1, "%3.3x", |
| 74 |
((chr[12] & 0xf) << 8) | chr[11]); |
| 75 |
retnamlen += 4; |
| 76 |
} |
| 77 |
return retnamlen; |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* This should _really_ be cleaned up some day.. |
| 82 |
*/ |
| 83 |
static int do_isofs_readdir(struct inode *inode, struct file *file, |
| 84 |
struct dir_context *ctx, |
| 85 |
char *tmpname, struct iso_directory_record *tmpde) |
| 86 |
{ |
| 87 |
unsigned long bufsize = ISOFS_BUFFER_SIZE(inode); |
| 88 |
unsigned char bufbits = ISOFS_BUFFER_BITS(inode); |
| 89 |
unsigned long block, offset, block_saved, offset_saved; |
| 90 |
unsigned long inode_number = 0; /* Quiet GCC */ |
| 91 |
struct buffer_head *bh = NULL; |
| 92 |
int len; |
| 93 |
int map; |
| 94 |
int first_de = 1; |
| 95 |
char *p = NULL; /* Quiet GCC */ |
| 96 |
struct iso_directory_record *de; |
| 97 |
struct isofs_sb_info *sbi = ISOFS_SB(inode->i_sb); |
| 98 |
|
| 99 |
offset = ctx->pos & (bufsize - 1); |
| 100 |
block = ctx->pos >> bufbits; |
| 101 |
|
| 102 |
while (ctx->pos < inode->i_size) { |
| 103 |
int de_len; |
| 104 |
|
| 105 |
if (!bh) { |
| 106 |
bh = isofs_bread(inode, block); |
| 107 |
if (!bh) |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
de = (struct iso_directory_record *) (bh->b_data + offset); |
| 112 |
|
| 113 |
de_len = *(unsigned char *)de; |
| 114 |
|
| 115 |
/* |
| 116 |
* If the length byte is zero, we should move on to the next |
| 117 |
* CDROM sector. If we are at the end of the directory, we |
| 118 |
* kick out of the while loop. |
| 119 |
*/ |
| 120 |
|
| 121 |
if (de_len == 0) { |
| 122 |
brelse(bh); |
| 123 |
bh = NULL; |
| 124 |
ctx->pos = (ctx->pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1); |
| 125 |
block = ctx->pos >> bufbits; |
| 126 |
offset = 0; |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
block_saved = block; |
| 131 |
offset_saved = offset; |
| 132 |
offset += de_len; |
| 133 |
|
| 134 |
/* Make sure we have a full directory entry */ |
| 135 |
if (offset >= bufsize) { |
| 136 |
int slop = bufsize - offset + de_len; |
| 137 |
memcpy(tmpde, de, slop); |
| 138 |
offset &= bufsize - 1; |
| 139 |
block++; |
| 140 |
brelse(bh); |
| 141 |
bh = NULL; |
| 142 |
if (offset) { |
| 143 |
bh = isofs_bread(inode, block); |
| 144 |
if (!bh) |
| 145 |
return 0; |
| 146 |
memcpy((void *) tmpde + slop, bh->b_data, offset); |
| 147 |
} |
| 148 |
de = tmpde; |
| 149 |
} |
| 150 |
/* Basic sanity check, whether name doesn't exceed dir entry */ |
| 151 |
if (de_len < sizeof(struct iso_directory_record) || |
| 152 |
de_len < de->name_len[0] + |
| 153 |
sizeof(struct iso_directory_record)) { |
| 154 |
printk(KERN_NOTICE "iso9660: Corrupted directory entry" |
| 155 |
" in block %lu of inode %lu\n", block, |
| 156 |
inode->i_ino); |
| 157 |
brelse(bh); |
| 158 |
return -EIO; |
| 159 |
} |
| 160 |
|
| 161 |
if (first_de) { |
| 162 |
isofs_normalize_block_and_offset(de, |
| 163 |
&block_saved, |
| 164 |
&offset_saved); |
| 165 |
inode_number = isofs_get_ino(block_saved, |
| 166 |
offset_saved, bufbits); |
| 167 |
} |
| 168 |
|
| 169 |
if (de->flags[-sbi->s_high_sierra] & 0x80) { |
| 170 |
first_de = 0; |
| 171 |
ctx->pos += de_len; |
| 172 |
continue; |
| 173 |
} |
| 174 |
first_de = 1; |
| 175 |
|
| 176 |
/* Handle the case of the '.' directory */ |
| 177 |
if (de->name_len[0] == 1 && de->name[0] == 0) { |
| 178 |
if (!dir_emit_dot(file, ctx)) |
| 179 |
break; |
| 180 |
ctx->pos += de_len; |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
len = 0; |
| 185 |
|
| 186 |
/* Handle the case of the '..' directory */ |
| 187 |
if (de->name_len[0] == 1 && de->name[0] == 1) { |
| 188 |
if (!dir_emit_dotdot(file, ctx)) |
| 189 |
break; |
| 190 |
ctx->pos += de_len; |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
/* Handle everything else. Do name translation if there |
| 195 |
is no Rock Ridge NM field. */ |
| 196 |
|
| 197 |
/* |
| 198 |
* Do not report hidden files if so instructed, or associated |
| 199 |
* files unless instructed to do so |
| 200 |
*/ |
| 201 |
if ((sbi->s_hide && (de->flags[-sbi->s_high_sierra] & 1)) || |
| 202 |
(!sbi->s_showassoc && |
| 203 |
(de->flags[-sbi->s_high_sierra] & 4))) { |
| 204 |
ctx->pos += de_len; |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
map = 1; |
| 209 |
if (sbi->s_rock) { |
| 210 |
len = get_rock_ridge_filename(de, tmpname, inode); |
| 211 |
if (len != 0) { /* may be -1 */ |
| 212 |
p = tmpname; |
| 213 |
map = 0; |
| 214 |
} |
| 215 |
} |
| 216 |
if (map) { |
| 217 |
#ifdef CONFIG_JOLIET |
| 218 |
if (sbi->s_joliet_level) { |
| 219 |
len = get_joliet_filename(de, tmpname, inode); |
| 220 |
p = tmpname; |
| 221 |
} else |
| 222 |
#endif |
| 223 |
if (sbi->s_mapping == 'a') { |
| 224 |
len = get_acorn_filename(de, tmpname, inode); |
| 225 |
p = tmpname; |
| 226 |
} else |
| 227 |
if (sbi->s_mapping == 'n') { |
| 228 |
len = isofs_name_translate(de, tmpname, inode); |
| 229 |
p = tmpname; |
| 230 |
} else { |
| 231 |
p = de->name; |
| 232 |
len = de->name_len[0]; |
| 233 |
} |
| 234 |
} |
| 235 |
if (len > 0) { |
| 236 |
if (!dir_emit(ctx, p, len, inode_number, DT_UNKNOWN)) |
| 237 |
break; |
| 238 |
} |
| 239 |
ctx->pos += de_len; |
| 240 |
} |
| 241 |
if (bh) |
| 242 |
brelse(bh); |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
/* |
| 247 |
* Handle allocation of temporary space for name translation and |
| 248 |
* handling split directory entries.. The real work is done by |
| 249 |
* "do_isofs_readdir()". |
| 250 |
*/ |
| 251 |
static int isofs_readdir(struct file *file, struct dir_context *ctx) |
| 252 |
{ |
| 253 |
int result; |
| 254 |
char *tmpname; |
| 255 |
struct iso_directory_record *tmpde; |
| 256 |
struct inode *inode = file_inode(file); |
| 257 |
|
| 258 |
tmpname = (char *)__get_free_page(GFP_KERNEL); |
| 259 |
if (tmpname == NULL) |
| 260 |
return -ENOMEM; |
| 261 |
|
| 262 |
tmpde = (struct iso_directory_record *) (tmpname+1024); |
| 263 |
|
| 264 |
result = do_isofs_readdir(inode, file, ctx, tmpname, tmpde); |
| 265 |
|
| 266 |
free_page((unsigned long) tmpname); |
| 267 |
return result; |
| 268 |
} |
| 269 |
|
| 270 |
const struct file_operations isofs_dir_operations = |
| 271 |
{ |
| 272 |
.llseek = generic_file_llseek, |
| 273 |
.read = generic_read_dir, |
| 274 |
.iterate_shared = isofs_readdir, |
| 275 |
.setlease = generic_setlease, |
| 276 |
}; |
| 277 |
|
| 278 |
/* |
| 279 |
* directories can handle most operations... |
| 280 |
*/ |
| 281 |
const struct inode_operations isofs_dir_inode_operations = |
| 282 |
{ |
| 283 |
.lookup = isofs_lookup, |
| 284 |
}; |
| 285 |
|
| 286 |
|