blob: 2fa0089a02a8ec2934cda55cbbae18e50c34a4ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hpfs/dir.c
3 *
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5 *
6 * directory VFS functions
7 */
8
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "hpfs_fn.h"
11
12static int hpfs_dir_release(struct inode *inode, struct file *filp)
13{
Arnd Bergmann9a311b92011-01-22 20:26:12 +010014 hpfs_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 hpfs_del_pos(inode, &filp->f_pos);
16 /*hpfs_write_if_changed(inode);*/
Arnd Bergmann9a311b92011-01-22 20:26:12 +010017 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 return 0;
19}
20
21/* This is slow, but it's not used often */
22
23static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence)
24{
25 loff_t new_off = off + (whence == 1 ? filp->f_pos : 0);
26 loff_t pos;
27 struct quad_buffer_head qbh;
Josef Sipek575800b2006-12-08 02:37:06 -080028 struct inode *i = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
30 struct super_block *s = i->i_sb;
31
Josef Bacik06222e42011-07-18 13:21:38 -040032 /* Somebody else will have to figure out what to do here */
33 if (whence == SEEK_DATA || whence == SEEK_HOLE)
34 return -EINVAL;
35
Arnd Bergmann9a311b92011-01-22 20:26:12 +010036 hpfs_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 /*printk("dir lseek\n");*/
39 if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080040 mutex_lock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1;
42 while (pos != new_off) {
43 if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh);
44 else goto fail;
45 if (pos == 12) goto fail;
46 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080047 mutex_unlock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048ok:
Arnd Bergmann9a311b92011-01-22 20:26:12 +010049 hpfs_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return filp->f_pos = new_off;
51fail:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080052 mutex_unlock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /*printk("illegal lseek: %016llx\n", new_off);*/
Arnd Bergmann9a311b92011-01-22 20:26:12 +010054 hpfs_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return -ESPIPE;
56}
57
58static int hpfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
59{
Josef Sipek575800b2006-12-08 02:37:06 -080060 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
62 struct quad_buffer_head qbh;
63 struct hpfs_dirent *de;
64 int lc;
65 long old_pos;
Al Viro7e7742e2010-01-31 17:09:29 -050066 unsigned char *tempname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int c1, c2 = 0;
68 int ret = 0;
69
Arnd Bergmann9a311b92011-01-22 20:26:12 +010070 hpfs_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if (hpfs_sb(inode->i_sb)->sb_chk) {
73 if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) {
74 ret = -EFSERROR;
75 goto out;
76 }
77 if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) {
78 ret = -EFSERROR;
79 goto out;
80 }
81 }
82 if (hpfs_sb(inode->i_sb)->sb_chk >= 2) {
83 struct buffer_head *bh;
84 struct fnode *fno;
85 int e = 0;
86 if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) {
87 ret = -EIOERROR;
88 goto out;
89 }
90 if (!fno->dirflag) {
91 e = 1;
Randy Dunlap18debbb2006-12-06 20:37:05 -080092 hpfs_error(inode->i_sb, "not a directory, fnode %08lx",
93 (unsigned long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Mikulas Patocka0b697602011-05-08 20:44:26 +020095 if (hpfs_inode->i_dno != le32_to_cpu(fno->u.external[0].disk_secno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 e = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020097 hpfs_error(inode->i_sb, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode->i_dno, le32_to_cpu(fno->u.external[0].disk_secno));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 brelse(bh);
100 if (e) {
101 ret = -EFSERROR;
102 goto out;
103 }
104 }
105 lc = hpfs_sb(inode->i_sb)->sb_lowercase;
106 if (filp->f_pos == 12) { /* diff -r requires this (note, that diff -r */
107 filp->f_pos = 13; /* also fails on msdos filesystem in 2.0) */
108 goto out;
109 }
110 if (filp->f_pos == 13) {
111 ret = -ENOENT;
112 goto out;
113 }
114
115 while (1) {
116 again:
117 /* This won't work when cycle is longer than number of dirents
118 accepted by filldir, but what can I do?
119 maybe killall -9 ls helps */
120 if (hpfs_sb(inode->i_sb)->sb_chk)
121 if (hpfs_stop_cycles(inode->i_sb, filp->f_pos, &c1, &c2, "hpfs_readdir")) {
122 ret = -EFSERROR;
123 goto out;
124 }
125 if (filp->f_pos == 12)
126 goto out;
127 if (filp->f_pos == 3 || filp->f_pos == 4 || filp->f_pos == 5) {
128 printk("HPFS: warning: pos==%d\n",(int)filp->f_pos);
129 goto out;
130 }
131 if (filp->f_pos == 0) {
132 if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0)
133 goto out;
134 filp->f_pos = 11;
135 }
136 if (filp->f_pos == 11) {
137 if (filldir(dirent, "..", 2, filp->f_pos, hpfs_inode->i_parent_dir, DT_DIR) < 0)
138 goto out;
139 filp->f_pos = 1;
140 }
141 if (filp->f_pos == 1) {
142 filp->f_pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1;
143 hpfs_add_pos(inode, &filp->f_pos);
144 filp->f_version = inode->i_version;
145 }
146 old_pos = filp->f_pos;
147 if (!(de = map_pos_dirent(inode, &filp->f_pos, &qbh))) {
148 ret = -EIOERROR;
149 goto out;
150 }
151 if (de->first || de->last) {
152 if (hpfs_sb(inode->i_sb)->sb_chk) {
Randy Dunlap18debbb2006-12-06 20:37:05 -0800153 if (de->first && !de->last && (de->namelen != 2
154 || de ->name[0] != 1 || de->name[1] != 1))
155 hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", old_pos);
156 if (de->last && (de->namelen != 1 || de ->name[0] != 255))
157 hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", old_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 hpfs_brelse4(&qbh);
160 goto again;
161 }
162 tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200163 if (filldir(dirent, tempname, de->namelen, old_pos, le32_to_cpu(de->fnode), DT_UNKNOWN) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 filp->f_pos = old_pos;
Al Viro7e7742e2010-01-31 17:09:29 -0500165 if (tempname != de->name) kfree(tempname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 hpfs_brelse4(&qbh);
167 goto out;
168 }
Al Viro7e7742e2010-01-31 17:09:29 -0500169 if (tempname != de->name) kfree(tempname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 hpfs_brelse4(&qbh);
171 }
172out:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100173 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return ret;
175}
176
177/*
178 * lookup. Search the specified directory for the specified name, set
179 * *result to the corresponding inode.
180 *
181 * lookup uses the inode number to tell read_inode whether it is reading
182 * the inode of a directory or a file -- file ino's are odd, directory
183 * ino's are even. read_inode avoids i/o for file inodes; everything
184 * needed is up here in the directory. (And file fnodes are out in
185 * the boondocks.)
186 *
187 * - M.P.: this is over, sometimes we've got to read file's fnode for eas
188 * inode numbers are just fnode sector numbers; iget lock is used
189 * to tell read_inode to read fnode or not.
190 */
191
192struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
193{
Al Viro7e7742e2010-01-31 17:09:29 -0500194 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned len = dentry->d_name.len;
196 struct quad_buffer_head qbh;
197 struct hpfs_dirent *de;
198 ino_t ino;
199 int err;
200 struct inode *result = NULL;
201 struct hpfs_inode_info *hpfs_result;
202
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100203 hpfs_lock(dir->i_sb);
Al Viro7e7742e2010-01-31 17:09:29 -0500204 if ((err = hpfs_chk_name(name, &len))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (err == -ENAMETOOLONG) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100206 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ERR_PTR(-ENAMETOOLONG);
208 }
209 goto end_add;
210 }
211
212 /*
213 * '.' and '..' will never be passed here.
214 */
215
Al Viro7e7742e2010-01-31 17:09:29 -0500216 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, NULL, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /*
219 * This is not really a bailout, just means file not found.
220 */
221
222 if (!de) goto end;
223
224 /*
225 * Get inode number, what we're after.
226 */
227
Mikulas Patocka0b697602011-05-08 20:44:26 +0200228 ino = le32_to_cpu(de->fnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /*
231 * Go find or make an inode.
232 */
233
234 result = iget_locked(dir->i_sb, ino);
235 if (!result) {
236 hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
237 goto bail1;
238 }
239 if (result->i_state & I_NEW) {
240 hpfs_init_inode(result);
241 if (de->directory)
242 hpfs_read_inode(result);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200243 else if (le32_to_cpu(de->ea_size) && hpfs_sb(dir->i_sb)->sb_eas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 hpfs_read_inode(result);
245 else {
246 result->i_mode |= S_IFREG;
247 result->i_mode &= ~0111;
248 result->i_op = &hpfs_file_iops;
249 result->i_fop = &hpfs_file_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200250 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 unlock_new_inode(result);
253 }
254 hpfs_result = hpfs_i(result);
255 if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) {
258 hpfs_error(result->i_sb, "ACLs or XPERM found. This is probably HPFS386. This driver doesn't support it now. Send me some info on these structures");
259 goto bail1;
260 }
261
262 /*
263 * Fill in the info from the directory if this is a newly created
264 * inode.
265 */
266
267 if (!result->i_ctime.tv_sec) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200268 if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->creation_date))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 result->i_ctime.tv_sec = 1;
270 result->i_ctime.tv_nsec = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200271 result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->write_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 result->i_mtime.tv_nsec = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200273 result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->read_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 result->i_atime.tv_nsec = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200275 hpfs_result->i_ea_size = le32_to_cpu(de->ea_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!hpfs_result->i_ea_mode && de->read_only)
277 result->i_mode &= ~0222;
278 if (!de->directory) {
279 if (result->i_size == -1) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200280 result->i_size = le32_to_cpu(de->file_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 result->i_data.a_ops = &hpfs_aops;
282 hpfs_i(result)->mmu_private = result->i_size;
283 /*
284 * i_blocks should count the fnode and any anodes.
285 * We count 1 for the fnode and don't bother about
286 * anodes -- the disk heads are on the directory band
287 * and we want them to stay there.
288 */
289 result->i_blocks = 1 + ((result->i_size + 511) >> 9);
290 }
291 }
292 }
293
294 hpfs_brelse4(&qbh);
295
296 /*
297 * Made it.
298 */
299
300 end:
301 end_add:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100302 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 d_add(dentry, result);
304 return NULL;
305
306 /*
307 * Didn't.
308 */
309 bail1:
310
311 hpfs_brelse4(&qbh);
312
313 /*bail:*/
314
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100315 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return ERR_PTR(-ENOENT);
317}
318
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800319const struct file_operations hpfs_dir_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 .llseek = hpfs_dir_lseek,
322 .read = generic_read_dir,
323 .readdir = hpfs_readdir,
324 .release = hpfs_dir_release,
325 .fsync = hpfs_file_fsync,
326};