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