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
isofs_name_translate(struct iso_directory_record * de,char * new,struct inode * inode)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 */
get_acorn_filename(struct iso_directory_record * de,char * retname,struct inode * inode)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 */
do_isofs_readdir(struct inode * inode,struct file * file,struct dir_context * ctx,char * tmpname)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 de_len = *(unsigned char *)de;
116
117 /*
118 * If the length byte is zero, we should move on to the next
119 * CDROM sector. If we are at the end of the directory, we
120 * kick out of the while loop.
121 */
122
123 if (de_len == 0) {
124 brelse(bh);
125 bh = NULL;
126 ctx->pos = (ctx->pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1);
127 block = ctx->pos >> bufbits;
128 offset = 0;
129 continue;
130 }
131
132 block_saved = block;
133 offset_saved = offset;
134 offset += de_len;
135
136 if (!isofs_dir_record_valid(de, offset_saved, bufsize)) {
137 printk(KERN_NOTICE "iso9660: Corrupted directory entry"
138 " in block %lu of inode %llu\n", block,
139 inode->i_ino);
140 brelse(bh);
141 return -EIO;
142 }
143
144 if (first_de) {
145 isofs_normalize_block_and_offset(de,
146 &block_saved,
147 &offset_saved);
148 inode_number = isofs_get_ino(block_saved,
149 offset_saved, bufbits);
150 }
151
152 if (de->flags[-sbi->s_high_sierra] & 0x80) {
153 first_de = 0;
154 ctx->pos += de_len;
155 continue;
156 }
157 first_de = 1;
158
159 /* Handle the case of the '.' directory */
160 if (de->name_len[0] == 1 && de->name[0] == 0) {
161 if (!dir_emit_dot(file, ctx))
162 break;
163 ctx->pos += de_len;
164 continue;
165 }
166
167 len = 0;
168
169 /* Handle the case of the '..' directory */
170 if (de->name_len[0] == 1 && de->name[0] == 1) {
171 if (!dir_emit_dotdot(file, ctx))
172 break;
173 ctx->pos += de_len;
174 continue;
175 }
176
177 /* Handle everything else. Do name translation if there
178 is no Rock Ridge NM field. */
179
180 /*
181 * Do not report hidden files if so instructed, or associated
182 * files unless instructed to do so
183 */
184 if ((sbi->s_hide && (de->flags[-sbi->s_high_sierra] & 1)) ||
185 (!sbi->s_showassoc &&
186 (de->flags[-sbi->s_high_sierra] & 4))) {
187 ctx->pos += de_len;
188 continue;
189 }
190
191 map = 1;
192 if (sbi->s_rock) {
193 len = get_rock_ridge_filename(de, tmpname, inode);
194 if (len != 0) { /* may be -1 */
195 p = tmpname;
196 map = 0;
197 }
198 }
199 if (map) {
200 #ifdef CONFIG_JOLIET
201 if (sbi->s_joliet_level) {
202 len = get_joliet_filename(de, tmpname, inode);
203 p = tmpname;
204 } else
205 #endif
206 if (sbi->s_mapping == 'a') {
207 len = get_acorn_filename(de, tmpname, inode);
208 p = tmpname;
209 } else
210 if (sbi->s_mapping == 'n') {
211 len = isofs_name_translate(de, tmpname, inode);
212 p = tmpname;
213 } else {
214 p = de->name;
215 len = de->name_len[0];
216 }
217 }
218 if (len > 0) {
219 if (!dir_emit(ctx, p, len, inode_number, DT_UNKNOWN))
220 break;
221 }
222 ctx->pos += de_len;
223 }
224 if (bh)
225 brelse(bh);
226 return 0;
227 }
228
229 /*
230 * Handle allocation of temporary space for name translation and
231 * handling split directory entries.. The real work is done by
232 * "do_isofs_readdir()".
233 */
isofs_readdir(struct file * file,struct dir_context * ctx)234 static int isofs_readdir(struct file *file, struct dir_context *ctx)
235 {
236 int result;
237 char *tmpname;
238 struct inode *inode = file_inode(file);
239
240 tmpname = kmalloc(1024, GFP_KERNEL);
241 if (tmpname == NULL)
242 return -ENOMEM;
243
244 result = do_isofs_readdir(inode, file, ctx, tmpname);
245
246 kfree(tmpname);
247 return result;
248 }
249
isofs_fileattr_get(struct dentry * dentry,struct file_kattr * fa)250 int isofs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
251 {
252 struct isofs_sb_info *sbi = ISOFS_SB(dentry->d_sb);
253
254 if (sbi->s_check == 'r') {
255 fa->fsx_xflags |= FS_XFLAG_CASEFOLD;
256 fa->flags |= FS_CASEFOLD_FL;
257 }
258 if (!sbi->s_joliet_level && !sbi->s_rock &&
259 (sbi->s_mapping == 'n' || sbi->s_mapping == 'a'))
260 fa->fsx_xflags |= FS_XFLAG_CASENONPRESERVING;
261 return 0;
262 }
263
264 const struct file_operations isofs_dir_operations =
265 {
266 .llseek = generic_file_llseek,
267 .read = generic_read_dir,
268 .iterate_shared = isofs_readdir,
269 .setlease = generic_setlease,
270 };
271
272 /*
273 * directories can handle most operations...
274 */
275 const struct inode_operations isofs_dir_inode_operations =
276 {
277 .lookup = isofs_lookup,
278 .fileattr_get = isofs_fileattr_get,
279 };
280
281