4 * (C) 1992, 1993, 1994 Eric Youngdale Modified for ISO 9660 filesystem.
6 * (C) 1991 Linus Torvalds - minix filesystem
8 * Steve Beynon : Missing last directory entries fixed
9 * (stephen@askone.demon.co.uk) : 21st June 1996
11 * isofs directory handling functions
13 #include <linux/gfp.h>
16 int isofs_name_translate(struct iso_directory_record *de, char *new, struct inode *inode)
18 char * old = de->name;
19 int len = de->name_len[0];
22 for (i = 0; i < len; i++) {
23 unsigned char c = old[i];
27 if (c >= 'A' && c <= 'Z')
28 c |= 0x20; /* lower case */
30 /* Drop trailing '.;1' (ISO 9660:1988 7.5.1 requires period) */
31 if (c == '.' && i == len - 3 && old[i + 1] == ';' && old[i + 2] == '1')
34 /* Drop trailing ';1' */
35 if (c == ';' && i == len - 2 && old[i + 1] == '1')
38 /* Convert remaining ';' to '.' */
39 /* Also '/' to '.' (broken Acorn-generated ISO9660 images) */
40 if (c == ';' || c == '/')
48 /* Acorn extensions written by Matthew Wilcox <willy@bofh.ai> 1998 */
49 int get_acorn_filename(struct iso_directory_record *de,
50 char *retname, struct inode *inode)
54 int retnamlen = isofs_name_translate(de, retname, inode);
58 std = sizeof(struct iso_directory_record) + de->name_len[0];
61 if ((*((unsigned char *) de) - std) != 32)
63 chr = ((unsigned char *) de) + std;
64 if (strncmp(chr, "ARCHIMEDES", 10))
66 if ((*retname == '_') && ((chr[19] & 1) == 1))
68 if (((de->flags[0] & 2) == 0) && (chr[13] == 0xff)
69 && ((chr[12] & 0xf0) == 0xf0)) {
70 retname[retnamlen] = ',';
71 sprintf(retname+retnamlen+1, "%3.3x",
72 ((chr[12] & 0xf) << 8) | chr[11]);
79 * This should _really_ be cleaned up some day..
81 static int do_isofs_readdir(struct inode *inode, struct file *file,
82 struct dir_context *ctx,
83 char *tmpname, struct iso_directory_record *tmpde)
85 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
86 unsigned char bufbits = ISOFS_BUFFER_BITS(inode);
87 unsigned long block, offset, block_saved, offset_saved;
88 unsigned long inode_number = 0; /* Quiet GCC */
89 struct buffer_head *bh = NULL;
93 char *p = NULL; /* Quiet GCC */
94 struct iso_directory_record *de;
95 struct isofs_sb_info *sbi = ISOFS_SB(inode->i_sb);
97 offset = ctx->pos & (bufsize - 1);
98 block = ctx->pos >> bufbits;
100 while (ctx->pos < inode->i_size) {
104 bh = isofs_bread(inode, block);
109 de = (struct iso_directory_record *) (bh->b_data + offset);
111 de_len = *(unsigned char *)de;
114 * If the length byte is zero, we should move on to the next
115 * CDROM sector. If we are at the end of the directory, we
116 * kick out of the while loop.
122 ctx->pos = (ctx->pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1);
123 block = ctx->pos >> bufbits;
129 offset_saved = offset;
132 /* Make sure we have a full directory entry */
133 if (offset >= bufsize) {
134 int slop = bufsize - offset + de_len;
135 memcpy(tmpde, de, slop);
136 offset &= bufsize - 1;
141 bh = isofs_bread(inode, block);
144 memcpy((void *) tmpde + slop, bh->b_data, offset);
148 /* Basic sanity check, whether name doesn't exceed dir entry */
149 if (de_len < de->name_len[0] +
150 sizeof(struct iso_directory_record)) {
151 printk(KERN_NOTICE "iso9660: Corrupted directory entry"
152 " in block %lu of inode %lu\n", block,
158 isofs_normalize_block_and_offset(de,
161 inode_number = isofs_get_ino(block_saved,
162 offset_saved, bufbits);
165 if (de->flags[-sbi->s_high_sierra] & 0x80) {
172 /* Handle the case of the '.' directory */
173 if (de->name_len[0] == 1 && de->name[0] == 0) {
174 if (!dir_emit_dot(file, ctx))
182 /* Handle the case of the '..' directory */
183 if (de->name_len[0] == 1 && de->name[0] == 1) {
184 if (!dir_emit_dotdot(file, ctx))
190 /* Handle everything else. Do name translation if there
191 is no Rock Ridge NM field. */
194 * Do not report hidden files if so instructed, or associated
195 * files unless instructed to do so
197 if ((sbi->s_hide && (de->flags[-sbi->s_high_sierra] & 1)) ||
198 (!sbi->s_showassoc &&
199 (de->flags[-sbi->s_high_sierra] & 4))) {
206 len = get_rock_ridge_filename(de, tmpname, inode);
207 if (len != 0) { /* may be -1 */
214 if (sbi->s_joliet_level) {
215 len = get_joliet_filename(de, tmpname, inode);
219 if (sbi->s_mapping == 'a') {
220 len = get_acorn_filename(de, tmpname, inode);
223 if (sbi->s_mapping == 'n') {
224 len = isofs_name_translate(de, tmpname, inode);
228 len = de->name_len[0];
232 if (!dir_emit(ctx, p, len, inode_number, DT_UNKNOWN))
245 * Handle allocation of temporary space for name translation and
246 * handling split directory entries.. The real work is done by
247 * "do_isofs_readdir()".
249 static int isofs_readdir(struct file *file, struct dir_context *ctx)
253 struct iso_directory_record *tmpde;
254 struct inode *inode = file_inode(file);
256 tmpname = (char *)__get_free_page(GFP_KERNEL);
260 tmpde = (struct iso_directory_record *) (tmpname+1024);
262 result = do_isofs_readdir(inode, file, ctx, tmpname, tmpde);
264 free_page((unsigned long) tmpname);
268 const struct file_operations isofs_dir_operations =
270 .llseek = generic_file_llseek,
271 .read = generic_read_dir,
272 .iterate = isofs_readdir,
276 * directories can handle most operations...
278 const struct inode_operations isofs_dir_inode_operations =
280 .lookup = isofs_lookup,