blob: 26e3964a4b8c19a7075d0ef623ab041392c78b53 [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>
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{
14 lock_kernel();
15 hpfs_del_pos(inode, &filp->f_pos);
16 /*hpfs_write_if_changed(inode);*/
17 unlock_kernel();
18 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
32 lock_kernel();
33
34 /*printk("dir lseek\n");*/
35 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 -080036 mutex_lock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1;
38 while (pos != new_off) {
39 if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh);
40 else goto fail;
41 if (pos == 12) goto fail;
42 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080043 mutex_unlock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044ok:
45 unlock_kernel();
46 return filp->f_pos = new_off;
47fail:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080048 mutex_unlock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /*printk("illegal lseek: %016llx\n", new_off);*/
50 unlock_kernel();
51 return -ESPIPE;
52}
53
54static int hpfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
55{
Josef Sipek575800b2006-12-08 02:37:06 -080056 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
58 struct quad_buffer_head qbh;
59 struct hpfs_dirent *de;
60 int lc;
61 long old_pos;
Al Viro7e7742e2010-01-31 17:09:29 -050062 unsigned char *tempname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int c1, c2 = 0;
64 int ret = 0;
65
66 lock_kernel();
67
68 if (hpfs_sb(inode->i_sb)->sb_chk) {
69 if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) {
70 ret = -EFSERROR;
71 goto out;
72 }
73 if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) {
74 ret = -EFSERROR;
75 goto out;
76 }
77 }
78 if (hpfs_sb(inode->i_sb)->sb_chk >= 2) {
79 struct buffer_head *bh;
80 struct fnode *fno;
81 int e = 0;
82 if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) {
83 ret = -EIOERROR;
84 goto out;
85 }
86 if (!fno->dirflag) {
87 e = 1;
Randy Dunlap18debbb2006-12-06 20:37:05 -080088 hpfs_error(inode->i_sb, "not a directory, fnode %08lx",
89 (unsigned long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91 if (hpfs_inode->i_dno != fno->u.external[0].disk_secno) {
92 e = 1;
93 hpfs_error(inode->i_sb, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode->i_dno, fno->u.external[0].disk_secno);
94 }
95 brelse(bh);
96 if (e) {
97 ret = -EFSERROR;
98 goto out;
99 }
100 }
101 lc = hpfs_sb(inode->i_sb)->sb_lowercase;
102 if (filp->f_pos == 12) { /* diff -r requires this (note, that diff -r */
103 filp->f_pos = 13; /* also fails on msdos filesystem in 2.0) */
104 goto out;
105 }
106 if (filp->f_pos == 13) {
107 ret = -ENOENT;
108 goto out;
109 }
110
111 while (1) {
112 again:
113 /* This won't work when cycle is longer than number of dirents
114 accepted by filldir, but what can I do?
115 maybe killall -9 ls helps */
116 if (hpfs_sb(inode->i_sb)->sb_chk)
117 if (hpfs_stop_cycles(inode->i_sb, filp->f_pos, &c1, &c2, "hpfs_readdir")) {
118 ret = -EFSERROR;
119 goto out;
120 }
121 if (filp->f_pos == 12)
122 goto out;
123 if (filp->f_pos == 3 || filp->f_pos == 4 || filp->f_pos == 5) {
124 printk("HPFS: warning: pos==%d\n",(int)filp->f_pos);
125 goto out;
126 }
127 if (filp->f_pos == 0) {
128 if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0)
129 goto out;
130 filp->f_pos = 11;
131 }
132 if (filp->f_pos == 11) {
133 if (filldir(dirent, "..", 2, filp->f_pos, hpfs_inode->i_parent_dir, DT_DIR) < 0)
134 goto out;
135 filp->f_pos = 1;
136 }
137 if (filp->f_pos == 1) {
138 filp->f_pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1;
139 hpfs_add_pos(inode, &filp->f_pos);
140 filp->f_version = inode->i_version;
141 }
142 old_pos = filp->f_pos;
143 if (!(de = map_pos_dirent(inode, &filp->f_pos, &qbh))) {
144 ret = -EIOERROR;
145 goto out;
146 }
147 if (de->first || de->last) {
148 if (hpfs_sb(inode->i_sb)->sb_chk) {
Randy Dunlap18debbb2006-12-06 20:37:05 -0800149 if (de->first && !de->last && (de->namelen != 2
150 || de ->name[0] != 1 || de->name[1] != 1))
151 hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", old_pos);
152 if (de->last && (de->namelen != 1 || de ->name[0] != 255))
153 hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", old_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 hpfs_brelse4(&qbh);
156 goto again;
157 }
158 tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3);
159 if (filldir(dirent, tempname, de->namelen, old_pos, de->fnode, DT_UNKNOWN) < 0) {
160 filp->f_pos = old_pos;
Al Viro7e7742e2010-01-31 17:09:29 -0500161 if (tempname != de->name) kfree(tempname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 hpfs_brelse4(&qbh);
163 goto out;
164 }
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 }
168out:
169 unlock_kernel();
170 return ret;
171}
172
173/*
174 * lookup. Search the specified directory for the specified name, set
175 * *result to the corresponding inode.
176 *
177 * lookup uses the inode number to tell read_inode whether it is reading
178 * the inode of a directory or a file -- file ino's are odd, directory
179 * ino's are even. read_inode avoids i/o for file inodes; everything
180 * needed is up here in the directory. (And file fnodes are out in
181 * the boondocks.)
182 *
183 * - M.P.: this is over, sometimes we've got to read file's fnode for eas
184 * inode numbers are just fnode sector numbers; iget lock is used
185 * to tell read_inode to read fnode or not.
186 */
187
188struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
189{
Al Viro7e7742e2010-01-31 17:09:29 -0500190 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned len = dentry->d_name.len;
192 struct quad_buffer_head qbh;
193 struct hpfs_dirent *de;
194 ino_t ino;
195 int err;
196 struct inode *result = NULL;
197 struct hpfs_inode_info *hpfs_result;
198
199 lock_kernel();
Al Viro7e7742e2010-01-31 17:09:29 -0500200 if ((err = hpfs_chk_name(name, &len))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (err == -ENAMETOOLONG) {
202 unlock_kernel();
203 return ERR_PTR(-ENAMETOOLONG);
204 }
205 goto end_add;
206 }
207
208 /*
209 * '.' and '..' will never be passed here.
210 */
211
Al Viro7e7742e2010-01-31 17:09:29 -0500212 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, NULL, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /*
215 * This is not really a bailout, just means file not found.
216 */
217
218 if (!de) goto end;
219
220 /*
221 * Get inode number, what we're after.
222 */
223
224 ino = de->fnode;
225
226 /*
227 * Go find or make an inode.
228 */
229
230 result = iget_locked(dir->i_sb, ino);
231 if (!result) {
232 hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
233 goto bail1;
234 }
235 if (result->i_state & I_NEW) {
236 hpfs_init_inode(result);
237 if (de->directory)
238 hpfs_read_inode(result);
239 else if (de->ea_size && hpfs_sb(dir->i_sb)->sb_eas)
240 hpfs_read_inode(result);
241 else {
242 result->i_mode |= S_IFREG;
243 result->i_mode &= ~0111;
244 result->i_op = &hpfs_file_iops;
245 result->i_fop = &hpfs_file_ops;
246 result->i_nlink = 1;
247 }
248 unlock_new_inode(result);
249 }
250 hpfs_result = hpfs_i(result);
251 if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino;
252
Al Viro7e7742e2010-01-31 17:09:29 -0500253 hpfs_decide_conv(result, name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) {
256 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");
257 goto bail1;
258 }
259
260 /*
261 * Fill in the info from the directory if this is a newly created
262 * inode.
263 */
264
265 if (!result->i_ctime.tv_sec) {
266 if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, de->creation_date)))
267 result->i_ctime.tv_sec = 1;
268 result->i_ctime.tv_nsec = 0;
269 result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, de->write_date);
270 result->i_mtime.tv_nsec = 0;
271 result->i_atime.tv_sec = local_to_gmt(dir->i_sb, de->read_date);
272 result->i_atime.tv_nsec = 0;
273 hpfs_result->i_ea_size = de->ea_size;
274 if (!hpfs_result->i_ea_mode && de->read_only)
275 result->i_mode &= ~0222;
276 if (!de->directory) {
277 if (result->i_size == -1) {
278 result->i_size = de->file_size;
279 result->i_data.a_ops = &hpfs_aops;
280 hpfs_i(result)->mmu_private = result->i_size;
281 /*
282 * i_blocks should count the fnode and any anodes.
283 * We count 1 for the fnode and don't bother about
284 * anodes -- the disk heads are on the directory band
285 * and we want them to stay there.
286 */
287 result->i_blocks = 1 + ((result->i_size + 511) >> 9);
288 }
289 }
290 }
291
292 hpfs_brelse4(&qbh);
293
294 /*
295 * Made it.
296 */
297
298 end:
299 end_add:
300 hpfs_set_dentry_operations(dentry);
301 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};