1 // SPDX-License-Identifier: GPL-2.0
5 * (C) 1992, 1993, 1994 Eric Youngdale Modified for ISO 9660 filesystem.
7 * (C) 1991 Linus Torvalds - minix filesystem
9 * Steve Beynon : Missing last directory entries fixed
10 * (stephen@askone.demon.co.uk) : 21st June 1996
12 * isofs directory handling functions
14 #include <linux/gfp.h>
17 int isofs_name_translate(struct iso_directory_record
*de
, char *new, struct inode
*inode
)
19 char * old
= de
->name
;
20 int len
= de
->name_len
[0];
23 for (i
= 0; i
< len
; i
++) {
24 unsigned char c
= old
[i
];
28 if (c
>= 'A' && c
<= 'Z')
29 c
|= 0x20; /* lower case */
31 /* Drop trailing '.;1' (ISO 9660:1988 7.5.1 requires period) */
32 if (c
== '.' && i
== len
- 3 && old
[i
+ 1] == ';' && old
[i
+ 2] == '1')
35 /* Drop trailing ';1' */
36 if (c
== ';' && i
== len
- 2 && old
[i
+ 1] == '1')
39 /* Convert remaining ';' to '.' */
40 /* Also '/' to '.' (broken Acorn-generated ISO9660 images) */
41 if (c
== ';' || c
== '/')
49 /* Acorn extensions written by Matthew Wilcox <willy@infradead.org> 1998 */
50 int get_acorn_filename(struct iso_directory_record
*de
,
51 char *retname
, struct inode
*inode
)
55 int retnamlen
= isofs_name_translate(de
, retname
, inode
);
59 std
= sizeof(struct iso_directory_record
) + de
->name_len
[0];
62 if (de
->length
[0] - std
!= 32)
64 chr
= ((unsigned char *) de
) + std
;
65 if (strncmp(chr
, "ARCHIMEDES", 10))
67 if ((*retname
== '_') && ((chr
[19] & 1) == 1))
69 if (((de
->flags
[0] & 2) == 0) && (chr
[13] == 0xff)
70 && ((chr
[12] & 0xf0) == 0xf0)) {
71 retname
[retnamlen
] = ',';
72 sprintf(retname
+retnamlen
+1, "%3.3x",
73 ((chr
[12] & 0xf) << 8) | chr
[11]);
80 * This should _really_ be cleaned up some day..
82 static int do_isofs_readdir(struct inode
*inode
, struct file
*file
,
83 struct dir_context
*ctx
,
84 char *tmpname
, struct iso_directory_record
*tmpde
)
86 unsigned long bufsize
= ISOFS_BUFFER_SIZE(inode
);
87 unsigned char bufbits
= ISOFS_BUFFER_BITS(inode
);
88 unsigned long block
, offset
, block_saved
, offset_saved
;
89 unsigned long inode_number
= 0; /* Quiet GCC */
90 struct buffer_head
*bh
= NULL
;
94 char *p
= NULL
; /* Quiet GCC */
95 struct iso_directory_record
*de
;
96 struct isofs_sb_info
*sbi
= ISOFS_SB(inode
->i_sb
);
98 offset
= ctx
->pos
& (bufsize
- 1);
99 block
= ctx
->pos
>> bufbits
;
101 while (ctx
->pos
< inode
->i_size
) {
105 bh
= isofs_bread(inode
, block
);
110 de
= (struct iso_directory_record
*) (bh
->b_data
+ offset
);
112 de_len
= *(unsigned char *)de
;
115 * If the length byte is zero, we should move on to the next
116 * CDROM sector. If we are at the end of the directory, we
117 * kick out of the while loop.
123 ctx
->pos
= (ctx
->pos
+ ISOFS_BLOCK_SIZE
) & ~(ISOFS_BLOCK_SIZE
- 1);
124 block
= ctx
->pos
>> bufbits
;
130 offset_saved
= offset
;
133 /* Make sure we have a full directory entry */
134 if (offset
>= bufsize
) {
135 int slop
= bufsize
- offset
+ de_len
;
136 memcpy(tmpde
, de
, slop
);
137 offset
&= bufsize
- 1;
142 bh
= isofs_bread(inode
, block
);
145 memcpy((void *) tmpde
+ slop
, bh
->b_data
, offset
);
149 /* Basic sanity check, whether name doesn't exceed dir entry */
150 if (de_len
< de
->name_len
[0] +
151 sizeof(struct iso_directory_record
)) {
152 printk(KERN_NOTICE
"iso9660: Corrupted directory entry"
153 " in block %lu of inode %lu\n", block
,
159 isofs_normalize_block_and_offset(de
,
162 inode_number
= isofs_get_ino(block_saved
,
163 offset_saved
, bufbits
);
166 if (de
->flags
[-sbi
->s_high_sierra
] & 0x80) {
173 /* Handle the case of the '.' directory */
174 if (de
->name_len
[0] == 1 && de
->name
[0] == 0) {
175 if (!dir_emit_dot(file
, ctx
))
183 /* Handle the case of the '..' directory */
184 if (de
->name_len
[0] == 1 && de
->name
[0] == 1) {
185 if (!dir_emit_dotdot(file
, ctx
))
191 /* Handle everything else. Do name translation if there
192 is no Rock Ridge NM field. */
195 * Do not report hidden files if so instructed, or associated
196 * files unless instructed to do so
198 if ((sbi
->s_hide
&& (de
->flags
[-sbi
->s_high_sierra
] & 1)) ||
199 (!sbi
->s_showassoc
&&
200 (de
->flags
[-sbi
->s_high_sierra
] & 4))) {
207 len
= get_rock_ridge_filename(de
, tmpname
, inode
);
208 if (len
!= 0) { /* may be -1 */
215 if (sbi
->s_joliet_level
) {
216 len
= get_joliet_filename(de
, tmpname
, inode
);
220 if (sbi
->s_mapping
== 'a') {
221 len
= get_acorn_filename(de
, tmpname
, inode
);
224 if (sbi
->s_mapping
== 'n') {
225 len
= isofs_name_translate(de
, tmpname
, inode
);
229 len
= de
->name_len
[0];
233 if (!dir_emit(ctx
, p
, len
, inode_number
, DT_UNKNOWN
))
246 * Handle allocation of temporary space for name translation and
247 * handling split directory entries.. The real work is done by
248 * "do_isofs_readdir()".
250 static int isofs_readdir(struct file
*file
, struct dir_context
*ctx
)
254 struct iso_directory_record
*tmpde
;
255 struct inode
*inode
= file_inode(file
);
257 tmpname
= (char *)__get_free_page(GFP_KERNEL
);
261 tmpde
= (struct iso_directory_record
*) (tmpname
+1024);
263 result
= do_isofs_readdir(inode
, file
, ctx
, tmpname
, tmpde
);
265 free_page((unsigned long) tmpname
);
269 const struct file_operations isofs_dir_operations
=
271 .llseek
= generic_file_llseek
,
272 .read
= generic_read_dir
,
273 .iterate_shared
= isofs_readdir
,
277 * directories can handle most operations...
279 const struct inode_operations isofs_dir_inode_operations
=
281 .lookup
= isofs_lookup
,