4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * directory VFS functions
9 #include <linux/smp_lock.h>
10 #include <linux/slab.h>
13 static 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 static 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_path
.dentry
->d_inode
;
30 struct hpfs_inode_info
*hpfs_inode
= hpfs_i(i
);
31 struct super_block
*s
= i
->i_sb
;
35 /*printk("dir lseek\n");*/
36 if (new_off
== 0 || new_off
== 1 || new_off
== 11 || new_off
== 12 || new_off
== 13) goto ok
;
37 mutex_lock(&i
->i_mutex
);
38 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
);
42 if (pos
== 12) goto fail
;
44 mutex_unlock(&i
->i_mutex
);
47 return filp
->f_pos
= new_off
;
49 mutex_unlock(&i
->i_mutex
);
50 /*printk("illegal lseek: %016llx\n", new_off);*/
55 static int hpfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
57 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
58 struct hpfs_inode_info
*hpfs_inode
= hpfs_i(inode
);
59 struct quad_buffer_head qbh
;
60 struct hpfs_dirent
*de
;
63 unsigned char *tempname
;
69 if (hpfs_sb(inode
->i_sb
)->sb_chk
) {
70 if (hpfs_chk_sectors(inode
->i_sb
, inode
->i_ino
, 1, "dir_fnode")) {
74 if (hpfs_chk_sectors(inode
->i_sb
, hpfs_inode
->i_dno
, 4, "dir_dnode")) {
79 if (hpfs_sb(inode
->i_sb
)->sb_chk
>= 2) {
80 struct buffer_head
*bh
;
83 if (!(fno
= hpfs_map_fnode(inode
->i_sb
, inode
->i_ino
, &bh
))) {
89 hpfs_error(inode
->i_sb
, "not a directory, fnode %08lx",
90 (unsigned long)inode
->i_ino
);
92 if (hpfs_inode
->i_dno
!= fno
->u
.external
[0].disk_secno
) {
94 hpfs_error(inode
->i_sb
, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode
->i_dno
, fno
->u
.external
[0].disk_secno
);
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) */
107 if (filp
->f_pos
== 13) {
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")) {
122 if (filp
->f_pos
== 12)
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
);
128 if (filp
->f_pos
== 0) {
129 if (filldir(dirent
, ".", 1, filp
->f_pos
, inode
->i_ino
, DT_DIR
) < 0)
133 if (filp
->f_pos
== 11) {
134 if (filldir(dirent
, "..", 2, filp
->f_pos
, hpfs_inode
->i_parent_dir
, DT_DIR
) < 0)
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
;
143 old_pos
= filp
->f_pos
;
144 if (!(de
= map_pos_dirent(inode
, &filp
->f_pos
, &qbh
))) {
148 if (de
->first
|| de
->last
) {
149 if (hpfs_sb(inode
->i_sb
)->sb_chk
) {
150 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
);
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
;
162 if (tempname
!= de
->name
) kfree(tempname
);
166 if (tempname
!= de
->name
) kfree(tempname
);
175 * lookup. Search the specified directory for the specified name, set
176 * *result to the corresponding inode.
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
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.
189 struct dentry
*hpfs_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
191 const unsigned char *name
= dentry
->d_name
.name
;
192 unsigned len
= dentry
->d_name
.len
;
193 struct quad_buffer_head qbh
;
194 struct hpfs_dirent
*de
;
197 struct inode
*result
= NULL
;
198 struct hpfs_inode_info
*hpfs_result
;
201 if ((err
= hpfs_chk_name(name
, &len
))) {
202 if (err
== -ENAMETOOLONG
) {
204 return ERR_PTR(-ENAMETOOLONG
);
210 * '.' and '..' will never be passed here.
213 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, NULL
, &qbh
);
216 * This is not really a bailout, just means file not found.
222 * Get inode number, what we're after.
228 * Go find or make an inode.
231 result
= iget_locked(dir
->i_sb
, ino
);
233 hpfs_error(dir
->i_sb
, "hpfs_lookup: can't get inode");
236 if (result
->i_state
& I_NEW
) {
237 hpfs_init_inode(result
);
239 hpfs_read_inode(result
);
240 else if (de
->ea_size
&& hpfs_sb(dir
->i_sb
)->sb_eas
)
241 hpfs_read_inode(result
);
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
;
249 unlock_new_inode(result
);
251 hpfs_result
= hpfs_i(result
);
252 if (!de
->directory
) hpfs_result
->i_parent_dir
= dir
->i_ino
;
254 hpfs_decide_conv(result
, name
, len
);
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");
262 * Fill in the info from the directory if this is a newly created
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
;
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.
288 result
->i_blocks
= 1 + ((result
->i_size
+ 511) >> 9);
302 d_add(dentry
, result
);
315 return ERR_PTR(-ENOENT
);
318 const struct file_operations hpfs_dir_ops
=
320 .llseek
= hpfs_dir_lseek
,
321 .read
= generic_read_dir
,
322 .readdir
= hpfs_readdir
,
323 .release
= hpfs_dir_release
,
324 .fsync
= hpfs_file_fsync
,