3 * BFS directory operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
5 * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
8 #include <linux/time.h>
9 #include <linux/string.h>
11 #include <linux/buffer_head.h>
12 #include <linux/sched.h>
18 #define dprintf(x...) printf(x)
23 static int bfs_add_entry(struct inode
*dir
, const unsigned char *name
,
24 int namelen
, int ino
);
25 static struct buffer_head
*bfs_find_entry(struct inode
*dir
,
26 const unsigned char *name
, int namelen
,
27 struct bfs_dirent
**res_dir
);
29 static int bfs_readdir(struct file
*f
, void *dirent
, filldir_t filldir
)
31 struct inode
*dir
= f
->f_path
.dentry
->d_inode
;
32 struct buffer_head
*bh
;
33 struct bfs_dirent
*de
;
34 struct bfs_sb_info
*info
= BFS_SB(dir
->i_sb
);
38 mutex_lock(&info
->bfs_lock
);
40 if (f
->f_pos
& (BFS_DIRENT_SIZE
- 1)) {
41 printf("Bad f_pos=%08lx for %s:%08lx\n",
42 (unsigned long)f
->f_pos
,
43 dir
->i_sb
->s_id
, dir
->i_ino
);
44 mutex_unlock(&info
->bfs_lock
);
48 while (f
->f_pos
< dir
->i_size
) {
49 offset
= f
->f_pos
& (BFS_BSIZE
- 1);
50 block
= BFS_I(dir
)->i_sblock
+ (f
->f_pos
>> BFS_BSIZE_BITS
);
51 bh
= sb_bread(dir
->i_sb
, block
);
53 f
->f_pos
+= BFS_BSIZE
- offset
;
57 de
= (struct bfs_dirent
*)(bh
->b_data
+ offset
);
59 int size
= strnlen(de
->name
, BFS_NAMELEN
);
60 if (filldir(dirent
, de
->name
, size
, f
->f_pos
,
64 mutex_unlock(&info
->bfs_lock
);
68 offset
+= BFS_DIRENT_SIZE
;
69 f
->f_pos
+= BFS_DIRENT_SIZE
;
70 } while ((offset
< BFS_BSIZE
) && (f
->f_pos
< dir
->i_size
));
74 mutex_unlock(&info
->bfs_lock
);
78 const struct file_operations bfs_dir_operations
= {
79 .read
= generic_read_dir
,
80 .readdir
= bfs_readdir
,
81 .fsync
= simple_fsync
,
82 .llseek
= generic_file_llseek
,
85 extern void dump_imap(const char *, struct super_block
*);
87 static int bfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
92 struct super_block
*s
= dir
->i_sb
;
93 struct bfs_sb_info
*info
= BFS_SB(s
);
99 mutex_lock(&info
->bfs_lock
);
100 ino
= find_first_zero_bit(info
->si_imap
, info
->si_lasti
);
101 if (ino
> info
->si_lasti
) {
102 mutex_unlock(&info
->bfs_lock
);
106 set_bit(ino
, info
->si_imap
);
108 inode
->i_uid
= current_fsuid();
109 inode
->i_gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current_fsgid();
110 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
112 inode
->i_op
= &bfs_file_inops
;
113 inode
->i_fop
= &bfs_file_operations
;
114 inode
->i_mapping
->a_ops
= &bfs_aops
;
115 inode
->i_mode
= mode
;
117 BFS_I(inode
)->i_dsk_ino
= ino
;
118 BFS_I(inode
)->i_sblock
= 0;
119 BFS_I(inode
)->i_eblock
= 0;
120 insert_inode_hash(inode
);
121 mark_inode_dirty(inode
);
122 dump_imap("create", s
);
124 err
= bfs_add_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
,
127 inode_dec_link_count(inode
);
128 mutex_unlock(&info
->bfs_lock
);
132 mutex_unlock(&info
->bfs_lock
);
133 d_instantiate(dentry
, inode
);
137 static struct dentry
*bfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
138 struct nameidata
*nd
)
140 struct inode
*inode
= NULL
;
141 struct buffer_head
*bh
;
142 struct bfs_dirent
*de
;
143 struct bfs_sb_info
*info
= BFS_SB(dir
->i_sb
);
145 if (dentry
->d_name
.len
> BFS_NAMELEN
)
146 return ERR_PTR(-ENAMETOOLONG
);
148 mutex_lock(&info
->bfs_lock
);
149 bh
= bfs_find_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
151 unsigned long ino
= (unsigned long)le16_to_cpu(de
->ino
);
153 inode
= bfs_iget(dir
->i_sb
, ino
);
155 mutex_unlock(&info
->bfs_lock
);
156 return ERR_CAST(inode
);
159 mutex_unlock(&info
->bfs_lock
);
160 d_add(dentry
, inode
);
164 static int bfs_link(struct dentry
*old
, struct inode
*dir
,
167 struct inode
*inode
= old
->d_inode
;
168 struct bfs_sb_info
*info
= BFS_SB(inode
->i_sb
);
171 mutex_lock(&info
->bfs_lock
);
172 err
= bfs_add_entry(dir
, new->d_name
.name
, new->d_name
.len
,
175 mutex_unlock(&info
->bfs_lock
);
179 inode
->i_ctime
= CURRENT_TIME_SEC
;
180 mark_inode_dirty(inode
);
181 atomic_inc(&inode
->i_count
);
182 d_instantiate(new, inode
);
183 mutex_unlock(&info
->bfs_lock
);
187 static int bfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
190 struct inode
*inode
= dentry
->d_inode
;
191 struct buffer_head
*bh
;
192 struct bfs_dirent
*de
;
193 struct bfs_sb_info
*info
= BFS_SB(inode
->i_sb
);
195 mutex_lock(&info
->bfs_lock
);
196 bh
= bfs_find_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
197 if (!bh
|| (le16_to_cpu(de
->ino
) != inode
->i_ino
))
200 if (!inode
->i_nlink
) {
201 printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
202 inode
->i_sb
->s_id
, inode
->i_ino
,
207 mark_buffer_dirty_inode(bh
, dir
);
208 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME_SEC
;
209 mark_inode_dirty(dir
);
210 inode
->i_ctime
= dir
->i_ctime
;
211 inode_dec_link_count(inode
);
216 mutex_unlock(&info
->bfs_lock
);
220 static int bfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
221 struct inode
*new_dir
, struct dentry
*new_dentry
)
223 struct inode
*old_inode
, *new_inode
;
224 struct buffer_head
*old_bh
, *new_bh
;
225 struct bfs_dirent
*old_de
, *new_de
;
226 struct bfs_sb_info
*info
;
229 old_bh
= new_bh
= NULL
;
230 old_inode
= old_dentry
->d_inode
;
231 if (S_ISDIR(old_inode
->i_mode
))
234 info
= BFS_SB(old_inode
->i_sb
);
236 mutex_lock(&info
->bfs_lock
);
237 old_bh
= bfs_find_entry(old_dir
,
238 old_dentry
->d_name
.name
,
239 old_dentry
->d_name
.len
, &old_de
);
241 if (!old_bh
|| (le16_to_cpu(old_de
->ino
) != old_inode
->i_ino
))
245 new_inode
= new_dentry
->d_inode
;
246 new_bh
= bfs_find_entry(new_dir
,
247 new_dentry
->d_name
.name
,
248 new_dentry
->d_name
.len
, &new_de
);
250 if (new_bh
&& !new_inode
) {
255 error
= bfs_add_entry(new_dir
,
256 new_dentry
->d_name
.name
,
257 new_dentry
->d_name
.len
,
263 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME_SEC
;
264 mark_inode_dirty(old_dir
);
266 new_inode
->i_ctime
= CURRENT_TIME_SEC
;
267 inode_dec_link_count(new_inode
);
269 mark_buffer_dirty_inode(old_bh
, old_dir
);
273 mutex_unlock(&info
->bfs_lock
);
279 const struct inode_operations bfs_dir_inops
= {
280 .create
= bfs_create
,
281 .lookup
= bfs_lookup
,
283 .unlink
= bfs_unlink
,
284 .rename
= bfs_rename
,
287 static int bfs_add_entry(struct inode
*dir
, const unsigned char *name
,
288 int namelen
, int ino
)
290 struct buffer_head
*bh
;
291 struct bfs_dirent
*de
;
292 int block
, sblock
, eblock
, off
, pos
;
295 dprintf("name=%s, namelen=%d\n", name
, namelen
);
299 if (namelen
> BFS_NAMELEN
)
300 return -ENAMETOOLONG
;
302 sblock
= BFS_I(dir
)->i_sblock
;
303 eblock
= BFS_I(dir
)->i_eblock
;
304 for (block
= sblock
; block
<= eblock
; block
++) {
305 bh
= sb_bread(dir
->i_sb
, block
);
308 for (off
= 0; off
< BFS_BSIZE
; off
+= BFS_DIRENT_SIZE
) {
309 de
= (struct bfs_dirent
*)(bh
->b_data
+ off
);
311 pos
= (block
- sblock
) * BFS_BSIZE
+ off
;
312 if (pos
>= dir
->i_size
) {
313 dir
->i_size
+= BFS_DIRENT_SIZE
;
314 dir
->i_ctime
= CURRENT_TIME_SEC
;
316 dir
->i_mtime
= CURRENT_TIME_SEC
;
317 mark_inode_dirty(dir
);
318 de
->ino
= cpu_to_le16((u16
)ino
);
319 for (i
= 0; i
< BFS_NAMELEN
; i
++)
321 (i
< namelen
) ? name
[i
] : 0;
322 mark_buffer_dirty_inode(bh
, dir
);
332 static inline int bfs_namecmp(int len
, const unsigned char *name
,
335 if ((len
< BFS_NAMELEN
) && buffer
[len
])
337 return !memcmp(name
, buffer
, len
);
340 static struct buffer_head
*bfs_find_entry(struct inode
*dir
,
341 const unsigned char *name
, int namelen
,
342 struct bfs_dirent
**res_dir
)
344 unsigned long block
= 0, offset
= 0;
345 struct buffer_head
*bh
= NULL
;
346 struct bfs_dirent
*de
;
349 if (namelen
> BFS_NAMELEN
)
352 while (block
* BFS_BSIZE
+ offset
< dir
->i_size
) {
354 bh
= sb_bread(dir
->i_sb
, BFS_I(dir
)->i_sblock
+ block
);
360 de
= (struct bfs_dirent
*)(bh
->b_data
+ offset
);
361 offset
+= BFS_DIRENT_SIZE
;
362 if (le16_to_cpu(de
->ino
) &&
363 bfs_namecmp(namelen
, name
, de
->name
)) {
367 if (offset
< bh
->b_size
)