blob: e0aca9a0ac68b9f5a75764a570df2c3d0bd81a72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/isofs/dir.c
3 *
4 * (C) 1992, 1993, 1994 Eric Youngdale Modified for ISO 9660 filesystem.
5 *
6 * (C) 1991 Linus Torvalds - minix filesystem
7 *
8 * Steve Beynon : Missing last directory entries fixed
9 * (stephen@askone.demon.co.uk) : 21st June 1996
Dave Jonesc3ed85a2007-07-15 23:40:03 -070010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * isofs directory handling functions
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/smp_lock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Al Viro94f2f712005-04-25 18:32:12 -070015#include "isofs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017int isofs_name_translate(struct iso_directory_record *de, char *new, struct inode *inode)
18{
19 char * old = de->name;
20 int len = de->name_len[0];
21 int i;
Dave Jonesc3ed85a2007-07-15 23:40:03 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 for (i = 0; i < len; i++) {
24 unsigned char c = old[i];
25 if (!c)
26 break;
27
28 if (c >= 'A' && c <= 'Z')
29 c |= 0x20; /* lower case */
30
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')
33 break;
34
35 /* Drop trailing ';1' */
36 if (c == ';' && i == len - 2 && old[i + 1] == '1')
37 break;
38
39 /* Convert remaining ';' to '.' */
40 /* Also '/' to '.' (broken Acorn-generated ISO9660 images) */
41 if (c == ';' || c == '/')
42 c = '.';
43
44 new[i] = c;
45 }
46 return i;
47}
48
49/* Acorn extensions written by Matthew Wilcox <willy@bofh.ai> 1998 */
Dave Jonesc3ed85a2007-07-15 23:40:03 -070050int get_acorn_filename(struct iso_directory_record *de,
51 char *retname, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 int std;
Dave Jonesc3ed85a2007-07-15 23:40:03 -070054 unsigned char *chr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int retnamlen = isofs_name_translate(de, retname, inode);
Dave Jonesc3ed85a2007-07-15 23:40:03 -070056
57 if (retnamlen == 0)
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 std = sizeof(struct iso_directory_record) + de->name_len[0];
Dave Jonesc3ed85a2007-07-15 23:40:03 -070060 if (std & 1)
61 std++;
62 if ((*((unsigned char *) de) - std) != 32)
63 return retnamlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 chr = ((unsigned char *) de) + std;
Dave Jonesc3ed85a2007-07-15 23:40:03 -070065 if (strncmp(chr, "ARCHIMEDES", 10))
66 return retnamlen;
67 if ((*retname == '_') && ((chr[19] & 1) == 1))
68 *retname = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (((de->flags[0] & 2) == 0) && (chr[13] == 0xff)
Dave Jonesc3ed85a2007-07-15 23:40:03 -070070 && ((chr[12] & 0xf0) == 0xf0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 retname[retnamlen] = ',';
72 sprintf(retname+retnamlen+1, "%3.3x",
73 ((chr[12] & 0xf) << 8) | chr[11]);
74 retnamlen += 4;
75 }
76 return retnamlen;
77}
78
79/*
80 * This should _really_ be cleaned up some day..
81 */
82static int do_isofs_readdir(struct inode *inode, struct file *filp,
83 void *dirent, filldir_t filldir,
Dave Jonesc3ed85a2007-07-15 23:40:03 -070084 char *tmpname, struct iso_directory_record *tmpde)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
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;
91 int len;
92 int map;
93 int first_de = 1;
94 char *p = NULL; /* Quiet GCC */
95 struct iso_directory_record *de;
96 struct isofs_sb_info *sbi = ISOFS_SB(inode->i_sb);
97
98 offset = filp->f_pos & (bufsize - 1);
99 block = filp->f_pos >> bufbits;
100
101 while (filp->f_pos < inode->i_size) {
102 int de_len;
103
104 if (!bh) {
105 bh = isofs_bread(inode, block);
106 if (!bh)
107 return 0;
108 }
109
110 de = (struct iso_directory_record *) (bh->b_data + offset);
111
112 de_len = *(unsigned char *) de;
113
Dave Jonesc3ed85a2007-07-15 23:40:03 -0700114 /*
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.
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (de_len == 0) {
121 brelse(bh);
122 bh = NULL;
123 filp->f_pos = (filp->f_pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1);
124 block = filp->f_pos >> bufbits;
125 offset = 0;
126 continue;
127 }
128
129 block_saved = block;
130 offset_saved = offset;
131 offset += de_len;
132
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;
138 block++;
139 brelse(bh);
140 bh = NULL;
141 if (offset) {
142 bh = isofs_bread(inode, block);
143 if (!bh)
144 return 0;
145 memcpy((void *) tmpde + slop, bh->b_data, offset);
146 }
147 de = tmpde;
148 }
Jan Kara2deb1ac2008-04-30 00:52:33 -0700149 /* 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,
154 inode->i_ino);
155 return -EIO;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (first_de) {
159 isofs_normalize_block_and_offset(de,
Dave Jonesc3ed85a2007-07-15 23:40:03 -0700160 &block_saved,
161 &offset_saved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 inode_number = isofs_get_ino(block_saved,
Dave Jonesc3ed85a2007-07-15 23:40:03 -0700163 offset_saved, bufbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
166 if (de->flags[-sbi->s_high_sierra] & 0x80) {
167 first_de = 0;
168 filp->f_pos += de_len;
169 continue;
170 }
171 first_de = 1;
172
173 /* Handle the case of the '.' directory */
174 if (de->name_len[0] == 1 && de->name[0] == 0) {
175 if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0)
176 break;
177 filp->f_pos += de_len;
178 continue;
179 }
180
181 len = 0;
182
183 /* Handle the case of the '..' directory */
184 if (de->name_len[0] == 1 && de->name[0] == 1) {
Josef "Jeff" Sipek24858222006-12-08 02:36:40 -0800185 inode_number = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (filldir(dirent, "..", 2, filp->f_pos, inode_number, DT_DIR) < 0)
187 break;
188 filp->f_pos += de_len;
189 continue;
190 }
191
192 /* Handle everything else. Do name translation if there
193 is no Rock Ridge NM field. */
Jeremy White9769f4e2005-06-21 17:16:53 -0700194
195 /*
196 * Do not report hidden files if so instructed, or associated
197 * files unless instructed to do so
198 */
Jan Kara5404ac82009-06-17 16:26:27 -0700199 if ((sbi->s_hide && (de->flags[-sbi->s_high_sierra] & 1)) ||
200 (!sbi->s_showassoc &&
Jeremy White9769f4e2005-06-21 17:16:53 -0700201 (de->flags[-sbi->s_high_sierra] & 4))) {
202 filp->f_pos += de_len;
203 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 map = 1;
207 if (sbi->s_rock) {
208 len = get_rock_ridge_filename(de, tmpname, inode);
209 if (len != 0) { /* may be -1 */
210 p = tmpname;
211 map = 0;
212 }
213 }
214 if (map) {
215#ifdef CONFIG_JOLIET
216 if (sbi->s_joliet_level) {
217 len = get_joliet_filename(de, tmpname, inode);
218 p = tmpname;
219 } else
220#endif
221 if (sbi->s_mapping == 'a') {
222 len = get_acorn_filename(de, tmpname, inode);
223 p = tmpname;
224 } else
225 if (sbi->s_mapping == 'n') {
226 len = isofs_name_translate(de, tmpname, inode);
227 p = tmpname;
228 } else {
229 p = de->name;
230 len = de->name_len[0];
231 }
232 }
233 if (len > 0) {
234 if (filldir(dirent, p, len, filp->f_pos, inode_number, DT_UNKNOWN) < 0)
235 break;
236 }
237 filp->f_pos += de_len;
238
239 continue;
240 }
Dave Jonesc3ed85a2007-07-15 23:40:03 -0700241 if (bh)
242 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 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 */
251static int isofs_readdir(struct file *filp,
252 void *dirent, filldir_t filldir)
253{
254 int result;
Dave Jonesc3ed85a2007-07-15 23:40:03 -0700255 char *tmpname;
256 struct iso_directory_record *tmpde;
Josef "Jeff" Sipek24858222006-12-08 02:36:40 -0800257 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 tmpname = (char *)__get_free_page(GFP_KERNEL);
260 if (tmpname == NULL)
261 return -ENOMEM;
262
263 lock_kernel();
264 tmpde = (struct iso_directory_record *) (tmpname+1024);
265
266 result = do_isofs_readdir(inode, filp, dirent, filldir, tmpname, tmpde);
267
268 free_page((unsigned long) tmpname);
269 unlock_kernel();
270 return result;
271}
Dave Jonesc3ed85a2007-07-15 23:40:03 -0700272
273const struct file_operations isofs_dir_operations =
274{
jan Blunckca572722010-05-26 14:44:53 -0700275 .llseek = generic_file_llseek,
Dave Jonesc3ed85a2007-07-15 23:40:03 -0700276 .read = generic_read_dir,
277 .readdir = isofs_readdir,
278};
279
280/*
281 * directories can handle most operations...
282 */
283const struct inode_operations isofs_dir_inode_operations =
284{
285 .lookup = isofs_lookup,
286};
287
288