4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * directory VFS functions
10 #include <linux/sched.h>
11 #include <linux/smp_lock.h>
13 int hpfs_dir_release(struct inode *inode, struct file *filp)
16 hpfs_del_pos(inode, &filp->f_pos);
17 /*hpfs_write_if_changed(inode);*/
22 /* This is slow, but it's not used often */
24 loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence)
26 loff_t new_off = off + (whence == 1 ? filp->f_pos : 0);
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;
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);
38 if (pos == 12) goto fail;
42 return filp->f_pos = new_off;
45 /*printk("illegal lseek: %016llx\n", new_off);*/
49 int hpfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
51 struct inode *inode = filp->f_dentry->d_inode;
52 struct quad_buffer_head qbh;
53 struct hpfs_dirent *de;
59 if (inode->i_sb->s_hpfs_chk) {
60 if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode"))
62 if (hpfs_chk_sectors(inode->i_sb, inode->i_hpfs_dno, 4, "dir_dnode"))
65 if (inode->i_sb->s_hpfs_chk >= 2) {
66 struct buffer_head *bh;
69 if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh)))
73 hpfs_error(inode->i_sb, "not a directory, fnode %08x",inode->i_ino);
75 if (inode->i_hpfs_dno != fno->u.external[0].disk_secno) {
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);
80 if (e) return -EFSERROR;
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) */
87 if (filp->f_pos == 13) return -ENOENT;
89 hpfs_lock_inode(inode);
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);
101 if (filp->f_pos == 12) {
102 hpfs_unlock_inode(inode);
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);
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);
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);
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;
129 /*if (filp->f_version != inode->i_version) {
130 hpfs_unlock_inode(inode);
133 old_pos = filp->f_pos;
134 if (!(de = map_pos_dirent(inode, &filp->f_pos, &qbh))) {
135 hpfs_unlock_inode(inode);
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);
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);
151 hpfs_unlock_inode(inode);
154 if (tempname != (char *)de->name) kfree(tempname);
160 * lookup. Search the specified directory for the specified name, set
161 * *result to the corresponding inode.
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
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.
174 struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry)
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;
182 struct inode *result = NULL;
184 if ((err = hpfs_chk_name((char *)name, &len))) {
185 if (err == -ENAMETOOLONG) return ERR_PTR(-ENAMETOOLONG);
189 hpfs_lock_inode(dir);
191 * '.' and '..' will never be passed here.
194 de = map_dirent(dir, dir->i_hpfs_dno, (char *) name, len, NULL, &qbh);
197 * This is not really a bailout, just means file not found.
203 * Get inode number, what we're after.
209 * Go find or make an inode.
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");
218 if (!de->directory) result->i_hpfs_parent_dir = dir->i_ino;
219 hpfs_unlock_iget(dir->i_sb);
221 hpfs_decide_conv(result, (char *)name, len);
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");
229 * Fill in the info from the directory if this is a newly created
233 if (!result->i_ctime) {
234 if (!(result->i_ctime = local_to_gmt(dir->i_sb, de->creation_date)))
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;
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.
252 result->i_blocks = 1 + ((result->i_size + 511) >> 9);
264 hpfs_unlock_inode(dir);
266 hpfs_set_dentry_operations(dentry);
267 d_add(dentry, result);
279 hpfs_unlock_inode(dir);
280 return ERR_PTR(-ENOENT);