import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / fs / hpfs / dir.c
1 /*
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
9 #include "hpfs_fn.h"
10 #include <linux/sched.h>
11 #include <linux/smp_lock.h>
12
13 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
24 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;
29         struct inode *i = filp->f_dentry->d_inode;
30         struct super_block *s = i->i_sb;
31         /*printk("dir lseek\n");*/
32         if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok;
33         hpfs_lock_inode(i);
34         pos = ((loff_t) hpfs_de_as_down_as_possible(s, i->i_hpfs_dno) << 4) + 1;
35         while (pos != new_off) {
36                 if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh);
37                 else goto fail;
38                 if (pos == 12) goto fail;
39         }
40         hpfs_unlock_inode(i);
41         ok:
42         return filp->f_pos = new_off;
43         fail:
44         hpfs_unlock_inode(i);
45         /*printk("illegal lseek: %016llx\n", new_off);*/
46         return -ESPIPE;
47 }
48
49 int hpfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
50 {
51         struct inode *inode = filp->f_dentry->d_inode;
52         struct quad_buffer_head qbh;
53         struct hpfs_dirent *de;
54         int lc;
55         long old_pos;
56         char *tempname;
57         int c1, c2 = 0;
58
59         if (inode->i_sb->s_hpfs_chk) {
60                 if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode"))
61                         return -EFSERROR;
62                 if (hpfs_chk_sectors(inode->i_sb, inode->i_hpfs_dno, 4, "dir_dnode"))
63                         return -EFSERROR;
64         }
65         if (inode->i_sb->s_hpfs_chk >= 2) {
66                 struct buffer_head *bh;
67                 struct fnode *fno;
68                 int e = 0;
69                 if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh)))
70                         return -EIOERROR;
71                 if (!fno->dirflag) {
72                         e = 1;
73                         hpfs_error(inode->i_sb, "not a directory, fnode %08x",inode->i_ino);
74                 }
75                 if (inode->i_hpfs_dno != fno->u.external[0].disk_secno) {
76                         e = 1;
77                         hpfs_error(inode->i_sb, "corrupted inode: i_hpfs_dno == %08x, fnode -> dnode == %08x", inode->i_hpfs_dno, fno->u.external[0].disk_secno);
78                 }
79                 brelse(bh);
80                 if (e) return -EFSERROR;
81         }
82         lc = inode->i_sb->s_hpfs_lowercase;
83         if (filp->f_pos == 12) { /* diff -r requires this (note, that diff -r */
84                 filp->f_pos = 13; /* also fails on msdos filesystem in 2.0) */
85                 return 0;
86         }
87         if (filp->f_pos == 13) return -ENOENT;
88         
89         hpfs_lock_inode(inode);
90         
91         while (1) {
92                 again:
93                 /* This won't work when cycle is longer than number of dirents
94                    accepted by filldir, but what can I do?
95                    maybe killall -9 ls helps */
96                 if (inode->i_sb->s_hpfs_chk)
97                         if (hpfs_stop_cycles(inode->i_sb, filp->f_pos, &c1, &c2, "hpfs_readdir")) {
98                                 hpfs_unlock_inode(inode);
99                                 return -EFSERROR;
100                         }
101                 if (filp->f_pos == 12) {
102                         hpfs_unlock_inode(inode);
103                         return 0;
104                 }
105                 if (filp->f_pos == 3 || filp->f_pos == 4 || filp->f_pos == 5) {
106                         printk("HPFS: warning: pos==%d\n",(int)filp->f_pos);
107                         hpfs_unlock_inode(inode);
108                         return 0;
109                 }
110                 if (filp->f_pos == 0) {
111                         if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0) {
112                                 hpfs_unlock_inode(inode);
113                                 return 0;
114                         }
115                         filp->f_pos = 11;
116                 }
117                 if (filp->f_pos == 11) {
118                         if (filldir(dirent, "..", 2, filp->f_pos, inode->i_hpfs_parent_dir, DT_DIR) < 0) {
119                                 hpfs_unlock_inode(inode);
120                                 return 0;
121                         }
122                         filp->f_pos = 1;
123                 }
124                 if (filp->f_pos == 1) {
125                         filp->f_pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, inode->i_hpfs_dno) << 4) + 1;
126                         hpfs_add_pos(inode, &filp->f_pos);
127                         filp->f_version = inode->i_version;
128                 }
129                         /*if (filp->f_version != inode->i_version) {
130                                 hpfs_unlock_inode(inode);
131                                 return -ENOENT;
132                         }*/     
133                         old_pos = filp->f_pos;
134                         if (!(de = map_pos_dirent(inode, &filp->f_pos, &qbh))) {
135                                 hpfs_unlock_inode(inode);
136                                 return -EIOERROR;
137                         }
138                         if (de->first || de->last) {
139                                 if (inode->i_sb->s_hpfs_chk) {
140                                         if (de->first && !de->last && (de->namelen != 2 || de ->name[0] != 1 || de->name[1] != 1)) hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08x", old_pos);
141                                         if (de->last && (de->namelen != 1 || de ->name[0] != 255)) hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08x", old_pos);
142                                 }
143                                 hpfs_brelse4(&qbh);
144                                 goto again;
145                         }
146                         tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3);
147                         if (filldir(dirent, tempname, de->namelen, old_pos, de->fnode, DT_UNKNOWN) < 0) {
148                                 filp->f_pos = old_pos;
149                                 if (tempname != (char *)de->name) kfree(tempname);
150                                 hpfs_brelse4(&qbh);
151                                 hpfs_unlock_inode(inode);
152                                 return 0;
153                         }
154                         if (tempname != (char *)de->name) kfree(tempname);
155                         hpfs_brelse4(&qbh);
156         }
157 }
158
159 /*
160  * lookup.  Search the specified directory for the specified name, set
161  * *result to the corresponding inode.
162  *
163  * lookup uses the inode number to tell read_inode whether it is reading
164  * the inode of a directory or a file -- file ino's are odd, directory
165  * ino's are even.  read_inode avoids i/o for file inodes; everything
166  * needed is up here in the directory.  (And file fnodes are out in
167  * the boondocks.)
168  *
169  *    - M.P.: this is over, sometimes we've got to read file's fnode for eas
170  *            inode numbers are just fnode sector numbers; iget lock is used
171  *            to tell read_inode to read fnode or not.
172  */
173
174 struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry)
175 {
176         const char *name = dentry->d_name.name;
177         unsigned len = dentry->d_name.len;
178         struct quad_buffer_head qbh;
179         struct hpfs_dirent *de;
180         ino_t ino;
181         int err;
182         struct inode *result = NULL;
183
184         if ((err = hpfs_chk_name((char *)name, &len))) {
185                 if (err == -ENAMETOOLONG) return ERR_PTR(-ENAMETOOLONG);
186                 goto end_add;
187         }
188
189         hpfs_lock_inode(dir);
190         /*
191          * '.' and '..' will never be passed here.
192          */
193
194         de = map_dirent(dir, dir->i_hpfs_dno, (char *) name, len, NULL, &qbh);
195
196         /*
197          * This is not really a bailout, just means file not found.
198          */
199
200         if (!de) goto end;
201
202         /*
203          * Get inode number, what we're after.
204          */
205
206         ino = de->fnode;
207
208         /*
209          * Go find or make an inode.
210          */
211
212         hpfs_lock_iget(dir->i_sb, de->directory || (de->ea_size && dir->i_sb->s_hpfs_eas) ? 1 : 2);
213         if (!(result = iget(dir->i_sb, ino))) {
214                 hpfs_unlock_iget(dir->i_sb);
215                 hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
216                 goto bail1;
217         }
218         if (!de->directory) result->i_hpfs_parent_dir = dir->i_ino;
219         hpfs_unlock_iget(dir->i_sb);
220
221         hpfs_decide_conv(result, (char *)name, len);
222
223         if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) {
224                 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");
225                 goto bail1;
226         }
227
228         /*
229          * Fill in the info from the directory if this is a newly created
230          * inode.
231          */
232
233         if (!result->i_ctime) {
234                 if (!(result->i_ctime = local_to_gmt(dir->i_sb, de->creation_date)))
235                         result->i_ctime = 1;
236                 result->i_mtime = local_to_gmt(dir->i_sb, de->write_date);
237                 result->i_atime = local_to_gmt(dir->i_sb, de->read_date);
238                 result->i_hpfs_ea_size = de->ea_size;
239                 if (!result->i_hpfs_ea_mode && de->read_only)
240                         result->i_mode &= ~0222;
241                 if (!de->directory) {
242                         if (result->i_size == -1) {
243                                 result->i_size = de->file_size;
244                                 result->i_data.a_ops = &hpfs_aops;
245                                 result->u.hpfs_i.mmu_private = result->i_size;
246                         /*
247                          * i_blocks should count the fnode and any anodes.
248                          * We count 1 for the fnode and don't bother about
249                          * anodes -- the disk heads are on the directory band
250                          * and we want them to stay there.
251                          */
252                                 result->i_blocks = 1 + ((result->i_size + 511) >> 9);
253                         }
254                 }
255         }
256
257         hpfs_brelse4(&qbh);
258
259         /*
260          * Made it.
261          */
262
263         end:
264         hpfs_unlock_inode(dir);
265         end_add:
266         hpfs_set_dentry_operations(dentry);
267         d_add(dentry, result);
268         return NULL;
269
270         /*
271          * Didn't.
272          */
273         bail1:
274         
275         hpfs_brelse4(&qbh);
276         
277         /*bail:*/
278
279         hpfs_unlock_inode(dir);
280         return ERR_PTR(-ENOENT);
281 }