blob: d32f63a569f7f27f8958ea2b2f859e8322e337ee [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
Alexey Dobriyan405f5572009-07-11 22:08:37 +04009#include <linux/smp_lock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "hpfs_fn.h"
12
13static int hpfs_dir_release(struct inode *inode, struct file *filp)
14{
15 lock_kernel();
16 hpfs_del_pos(inode, &filp->f_pos);
17 /*hpfs_write_if_changed(inode);*/
18 unlock_kernel();
19 return 0;
20}
21
22/* This is slow, but it's not used often */
23
24static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence)
25{
26 loff_t new_off = off + (whence == 1 ? filp->f_pos : 0);
27 loff_t pos;
28 struct quad_buffer_head qbh;
Josef Sipek575800b2006-12-08 02:37:06 -080029 struct inode *i = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
31 struct super_block *s = i->i_sb;
32
33 lock_kernel();
34
35 /*printk("dir lseek\n");*/
36 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 -080037 mutex_lock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1;
39 while (pos != new_off) {
40 if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh);
41 else goto fail;
42 if (pos == 12) goto fail;
43 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080044 mutex_unlock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045ok:
46 unlock_kernel();
47 return filp->f_pos = new_off;
48fail:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080049 mutex_unlock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 /*printk("illegal lseek: %016llx\n", new_off);*/
51 unlock_kernel();
52 return -ESPIPE;
53}
54
55static int hpfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
56{
Josef Sipek575800b2006-12-08 02:37:06 -080057 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
59 struct quad_buffer_head qbh;
60 struct hpfs_dirent *de;
61 int lc;
62 long old_pos;
Al Viro7e7742e2010-01-31 17:09:29 -050063 unsigned char *tempname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int c1, c2 = 0;
65 int ret = 0;
66
67 lock_kernel();
68
69 if (hpfs_sb(inode->i_sb)->sb_chk) {
70 if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) {
71 ret = -EFSERROR;
72 goto out;
73 }
74 if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) {
75 ret = -EFSERROR;
76 goto out;
77 }
78 }
79 if (hpfs_sb(inode->i_sb)->sb_chk >= 2) {
80 struct buffer_head *bh;
81 struct fnode *fno;
82 int e = 0;
83 if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) {
84 ret = -EIOERROR;
85 goto out;
86 }
87 if (!fno->dirflag) {
88 e = 1;
Randy Dunlap18debbb2006-12-06 20:37:05 -080089 hpfs_error(inode->i_sb, "not a directory, fnode %08lx",
90 (unsigned long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 if (hpfs_inode->i_dno != fno->u.external[0].disk_secno) {
93 e = 1;
94 hpfs_error(inode->i_sb, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode->i_dno, fno->u.external[0].disk_secno);
95 }
96 brelse(bh);
97 if (e) {
98 ret = -EFSERROR;
99 goto out;
100 }
101 }
102 lc = hpfs_sb(inode->i_sb)->sb_lowercase;
103 if (filp->f_pos == 12) { /* diff -r requires this (note, that diff -r */
104 filp->f_pos = 13; /* also fails on msdos filesystem in 2.0) */
105 goto out;
106 }
107 if (filp->f_pos == 13) {
108 ret = -ENOENT;
109 goto out;
110 }
111
112 while (1) {
113 again:
114 /* This won't work when cycle is longer than number of dirents
115 accepted by filldir, but what can I do?
116 maybe killall -9 ls helps */
117 if (hpfs_sb(inode->i_sb)->sb_chk)
118 if (hpfs_stop_cycles(inode->i_sb, filp->f_pos, &c1, &c2, "hpfs_readdir")) {
119 ret = -EFSERROR;
120 goto out;
121 }
122 if (filp->f_pos == 12)
123 goto out;
124 if (filp->f_pos == 3 || filp->f_pos == 4 || filp->f_pos == 5) {
125 printk("HPFS: warning: pos==%d\n",(int)filp->f_pos);
126 goto out;
127 }
128 if (filp->f_pos == 0) {
129 if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0)
130 goto out;
131 filp->f_pos = 11;
132 }
133 if (filp->f_pos == 11) {
134 if (filldir(dirent, "..", 2, filp->f_pos, hpfs_inode->i_parent_dir, DT_DIR) < 0)
135 goto out;
136 filp->f_pos = 1;
137 }
138 if (filp->f_pos == 1) {
139 filp->f_pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1;
140 hpfs_add_pos(inode, &filp->f_pos);
141 filp->f_version = inode->i_version;
142 }
143 old_pos = filp->f_pos;
144 if (!(de = map_pos_dirent(inode, &filp->f_pos, &qbh))) {
145 ret = -EIOERROR;
146 goto out;
147 }
148 if (de->first || de->last) {
149 if (hpfs_sb(inode->i_sb)->sb_chk) {
Randy Dunlap18debbb2006-12-06 20:37:05 -0800150 if (de->first && !de->last && (de->namelen != 2
151 || de ->name[0] != 1 || de->name[1] != 1))
152 hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", old_pos);
153 if (de->last && (de->namelen != 1 || de ->name[0] != 255))
154 hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", old_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 hpfs_brelse4(&qbh);
157 goto again;
158 }
159 tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3);
160 if (filldir(dirent, tempname, de->namelen, old_pos, de->fnode, DT_UNKNOWN) < 0) {
161 filp->f_pos = old_pos;
Al Viro7e7742e2010-01-31 17:09:29 -0500162 if (tempname != de->name) kfree(tempname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 hpfs_brelse4(&qbh);
164 goto out;
165 }
Al Viro7e7742e2010-01-31 17:09:29 -0500166 if (tempname != de->name) kfree(tempname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 hpfs_brelse4(&qbh);
168 }
169out:
170 unlock_kernel();
171 return ret;
172}
173
174/*
175 * lookup. Search the specified directory for the specified name, set
176 * *result to the corresponding inode.
177 *
178 * lookup uses the inode number to tell read_inode whether it is reading
179 * the inode of a directory or a file -- file ino's are odd, directory
180 * ino's are even. read_inode avoids i/o for file inodes; everything
181 * needed is up here in the directory. (And file fnodes are out in
182 * the boondocks.)
183 *
184 * - M.P.: this is over, sometimes we've got to read file's fnode for eas
185 * inode numbers are just fnode sector numbers; iget lock is used
186 * to tell read_inode to read fnode or not.
187 */
188
189struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
190{
Al Viro7e7742e2010-01-31 17:09:29 -0500191 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned len = dentry->d_name.len;
193 struct quad_buffer_head qbh;
194 struct hpfs_dirent *de;
195 ino_t ino;
196 int err;
197 struct inode *result = NULL;
198 struct hpfs_inode_info *hpfs_result;
199
200 lock_kernel();
Al Viro7e7742e2010-01-31 17:09:29 -0500201 if ((err = hpfs_chk_name(name, &len))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (err == -ENAMETOOLONG) {
203 unlock_kernel();
204 return ERR_PTR(-ENAMETOOLONG);
205 }
206 goto end_add;
207 }
208
209 /*
210 * '.' and '..' will never be passed here.
211 */
212
Al Viro7e7742e2010-01-31 17:09:29 -0500213 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, NULL, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /*
216 * This is not really a bailout, just means file not found.
217 */
218
219 if (!de) goto end;
220
221 /*
222 * Get inode number, what we're after.
223 */
224
225 ino = de->fnode;
226
227 /*
228 * Go find or make an inode.
229 */
230
231 result = iget_locked(dir->i_sb, ino);
232 if (!result) {
233 hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
234 goto bail1;
235 }
236 if (result->i_state & I_NEW) {
237 hpfs_init_inode(result);
238 if (de->directory)
239 hpfs_read_inode(result);
240 else if (de->ea_size && hpfs_sb(dir->i_sb)->sb_eas)
241 hpfs_read_inode(result);
242 else {
243 result->i_mode |= S_IFREG;
244 result->i_mode &= ~0111;
245 result->i_op = &hpfs_file_iops;
246 result->i_fop = &hpfs_file_ops;
247 result->i_nlink = 1;
248 }
249 unlock_new_inode(result);
250 }
251 hpfs_result = hpfs_i(result);
252 if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino;
253
Al Viro7e7742e2010-01-31 17:09:29 -0500254 hpfs_decide_conv(result, name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) {
257 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");
258 goto bail1;
259 }
260
261 /*
262 * Fill in the info from the directory if this is a newly created
263 * inode.
264 */
265
266 if (!result->i_ctime.tv_sec) {
267 if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, de->creation_date)))
268 result->i_ctime.tv_sec = 1;
269 result->i_ctime.tv_nsec = 0;
270 result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, de->write_date);
271 result->i_mtime.tv_nsec = 0;
272 result->i_atime.tv_sec = local_to_gmt(dir->i_sb, de->read_date);
273 result->i_atime.tv_nsec = 0;
274 hpfs_result->i_ea_size = de->ea_size;
275 if (!hpfs_result->i_ea_mode && de->read_only)
276 result->i_mode &= ~0222;
277 if (!de->directory) {
278 if (result->i_size == -1) {
279 result->i_size = de->file_size;
280 result->i_data.a_ops = &hpfs_aops;
281 hpfs_i(result)->mmu_private = result->i_size;
282 /*
283 * i_blocks should count the fnode and any anodes.
284 * We count 1 for the fnode and don't bother about
285 * anodes -- the disk heads are on the directory band
286 * and we want them to stay there.
287 */
288 result->i_blocks = 1 + ((result->i_size + 511) >> 9);
289 }
290 }
291 }
292
293 hpfs_brelse4(&qbh);
294
295 /*
296 * Made it.
297 */
298
299 end:
300 end_add:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unlock_kernel();
302 d_add(dentry, result);
303 return NULL;
304
305 /*
306 * Didn't.
307 */
308 bail1:
309
310 hpfs_brelse4(&qbh);
311
312 /*bail:*/
313
314 unlock_kernel();
315 return ERR_PTR(-ENOENT);
316}
317
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800318const struct file_operations hpfs_dir_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 .llseek = hpfs_dir_lseek,
321 .read = generic_read_dir,
322 .readdir = hpfs_readdir,
323 .release = hpfs_dir_release,
324 .fsync = hpfs_file_fsync,
325};