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/smp_lock.h>
12 #include <linux/buffer_head.h>
13 #include <linux/sched.h>
19 #define dprintf(x...) printf(x)
24 static int bfs_add_entry(struct inode
* dir
, const unsigned char * name
, int namelen
, int ino
);
25 static struct buffer_head
* bfs_find_entry(struct inode
* dir
,
26 const unsigned char * name
, int namelen
, struct bfs_dirent
** res_dir
);
28 static int bfs_readdir(struct file
* f
, void * dirent
, filldir_t filldir
)
30 struct inode
* dir
= f
->f_path
.dentry
->d_inode
;
31 struct buffer_head
* bh
;
32 struct bfs_dirent
* de
;
38 if (f
->f_pos
& (BFS_DIRENT_SIZE
-1)) {
39 printf("Bad f_pos=%08lx for %s:%08lx\n", (unsigned long)f
->f_pos
,
40 dir
->i_sb
->s_id
, dir
->i_ino
);
45 while (f
->f_pos
< dir
->i_size
) {
46 offset
= f
->f_pos
& (BFS_BSIZE
-1);
47 block
= BFS_I(dir
)->i_sblock
+ (f
->f_pos
>> BFS_BSIZE_BITS
);
48 bh
= sb_bread(dir
->i_sb
, block
);
50 f
->f_pos
+= BFS_BSIZE
- offset
;
54 de
= (struct bfs_dirent
*)(bh
->b_data
+ offset
);
56 int size
= strnlen(de
->name
, BFS_NAMELEN
);
57 if (filldir(dirent
, de
->name
, size
, f
->f_pos
, le16_to_cpu(de
->ino
), DT_UNKNOWN
) < 0) {
63 offset
+= BFS_DIRENT_SIZE
;
64 f
->f_pos
+= BFS_DIRENT_SIZE
;
65 } while (offset
< BFS_BSIZE
&& f
->f_pos
< dir
->i_size
);
73 const struct file_operations bfs_dir_operations
= {
74 .read
= generic_read_dir
,
75 .readdir
= bfs_readdir
,
79 extern void dump_imap(const char *, struct super_block
*);
81 static int bfs_create(struct inode
* dir
, struct dentry
* dentry
, int mode
,
86 struct super_block
* s
= dir
->i_sb
;
87 struct bfs_sb_info
* info
= BFS_SB(s
);
94 ino
= find_first_zero_bit(info
->si_imap
, info
->si_lasti
);
95 if (ino
> info
->si_lasti
) {
100 set_bit(ino
, info
->si_imap
);
102 inode
->i_uid
= current
->fsuid
;
103 inode
->i_gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
104 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
106 inode
->i_op
= &bfs_file_inops
;
107 inode
->i_fop
= &bfs_file_operations
;
108 inode
->i_mapping
->a_ops
= &bfs_aops
;
109 inode
->i_mode
= mode
;
111 BFS_I(inode
)->i_dsk_ino
= ino
;
112 BFS_I(inode
)->i_sblock
= 0;
113 BFS_I(inode
)->i_eblock
= 0;
114 insert_inode_hash(inode
);
115 mark_inode_dirty(inode
);
116 dump_imap("create",s
);
118 err
= bfs_add_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, inode
->i_ino
);
120 inode_dec_link_count(inode
);
126 d_instantiate(dentry
, inode
);
130 static struct dentry
* bfs_lookup(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
132 struct inode
* inode
= NULL
;
133 struct buffer_head
* bh
;
134 struct bfs_dirent
* de
;
136 if (dentry
->d_name
.len
> BFS_NAMELEN
)
137 return ERR_PTR(-ENAMETOOLONG
);
140 bh
= bfs_find_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
142 unsigned long ino
= (unsigned long)le16_to_cpu(de
->ino
);
144 inode
= iget(dir
->i_sb
, ino
);
147 return ERR_PTR(-EACCES
);
151 d_add(dentry
, inode
);
155 static int bfs_link(struct dentry
* old
, struct inode
* dir
, struct dentry
* new)
157 struct inode
* inode
= old
->d_inode
;
161 err
= bfs_add_entry(dir
, new->d_name
.name
, new->d_name
.len
, inode
->i_ino
);
167 inode
->i_ctime
= CURRENT_TIME_SEC
;
168 mark_inode_dirty(inode
);
169 atomic_inc(&inode
->i_count
);
170 d_instantiate(new, inode
);
176 static int bfs_unlink(struct inode
* dir
, struct dentry
* dentry
)
179 struct inode
* inode
;
180 struct buffer_head
* bh
;
181 struct bfs_dirent
* de
;
183 inode
= dentry
->d_inode
;
185 bh
= bfs_find_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
186 if (!bh
|| le16_to_cpu(de
->ino
) != inode
->i_ino
)
189 if (!inode
->i_nlink
) {
190 printf("unlinking non-existent file %s:%lu (nlink=%d)\n", inode
->i_sb
->s_id
,
191 inode
->i_ino
, inode
->i_nlink
);
195 mark_buffer_dirty(bh
);
196 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME_SEC
;
197 mark_inode_dirty(dir
);
198 inode
->i_ctime
= dir
->i_ctime
;
199 inode_dec_link_count(inode
);
208 static int bfs_rename(struct inode
* old_dir
, struct dentry
* old_dentry
,
209 struct inode
* new_dir
, struct dentry
* new_dentry
)
211 struct inode
* old_inode
, * new_inode
;
212 struct buffer_head
* old_bh
, * new_bh
;
213 struct bfs_dirent
* old_de
, * new_de
;
216 old_bh
= new_bh
= NULL
;
217 old_inode
= old_dentry
->d_inode
;
218 if (S_ISDIR(old_inode
->i_mode
))
222 old_bh
= bfs_find_entry(old_dir
,
223 old_dentry
->d_name
.name
,
224 old_dentry
->d_name
.len
, &old_de
);
226 if (!old_bh
|| le16_to_cpu(old_de
->ino
) != old_inode
->i_ino
)
230 new_inode
= new_dentry
->d_inode
;
231 new_bh
= bfs_find_entry(new_dir
,
232 new_dentry
->d_name
.name
,
233 new_dentry
->d_name
.len
, &new_de
);
235 if (new_bh
&& !new_inode
) {
240 error
= bfs_add_entry(new_dir
,
241 new_dentry
->d_name
.name
,
242 new_dentry
->d_name
.len
, old_inode
->i_ino
);
247 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME_SEC
;
248 mark_inode_dirty(old_dir
);
250 new_inode
->i_ctime
= CURRENT_TIME_SEC
;
251 inode_dec_link_count(new_inode
);
253 mark_buffer_dirty(old_bh
);
263 const struct inode_operations bfs_dir_inops
= {
264 .create
= bfs_create
,
265 .lookup
= bfs_lookup
,
267 .unlink
= bfs_unlink
,
268 .rename
= bfs_rename
,
271 static int bfs_add_entry(struct inode
* dir
, const unsigned char * name
, int namelen
, int ino
)
273 struct buffer_head
* bh
;
274 struct bfs_dirent
* de
;
275 int block
, sblock
, eblock
, off
, eoff
;
278 dprintf("name=%s, namelen=%d\n", name
, namelen
);
282 if (namelen
> BFS_NAMELEN
)
283 return -ENAMETOOLONG
;
285 sblock
= BFS_I(dir
)->i_sblock
;
286 eblock
= BFS_I(dir
)->i_eblock
;
287 eoff
= dir
->i_size
% BFS_BSIZE
;
288 for (block
=sblock
; block
<=eblock
; block
++) {
289 bh
= sb_bread(dir
->i_sb
, block
);
292 for (off
=0; off
<BFS_BSIZE
; off
+=BFS_DIRENT_SIZE
) {
293 de
= (struct bfs_dirent
*)(bh
->b_data
+ off
);
294 if (block
==eblock
&& off
>=eoff
) {
295 /* Do not read/interpret the garbage in the end of eblock. */
299 if ((block
-sblock
)*BFS_BSIZE
+ off
>= dir
->i_size
) {
300 dir
->i_size
+= BFS_DIRENT_SIZE
;
301 dir
->i_ctime
= CURRENT_TIME_SEC
;
303 dir
->i_mtime
= CURRENT_TIME_SEC
;
304 mark_inode_dirty(dir
);
305 de
->ino
= cpu_to_le16((u16
)ino
);
306 for (i
=0; i
<BFS_NAMELEN
; i
++)
307 de
->name
[i
] = (i
< namelen
) ? name
[i
] : 0;
308 mark_buffer_dirty(bh
);
318 static inline int bfs_namecmp(int len
, const unsigned char * name
, const char * buffer
)
320 if (len
< BFS_NAMELEN
&& buffer
[len
])
322 return !memcmp(name
, buffer
, len
);
325 static struct buffer_head
* bfs_find_entry(struct inode
* dir
,
326 const unsigned char * name
, int namelen
, struct bfs_dirent
** res_dir
)
328 unsigned long block
, offset
;
329 struct buffer_head
* bh
;
330 struct bfs_dirent
* de
;
333 if (namelen
> BFS_NAMELEN
)
337 while (block
* BFS_BSIZE
+ offset
< dir
->i_size
) {
339 bh
= sb_bread(dir
->i_sb
, BFS_I(dir
)->i_sblock
+ block
);
345 de
= (struct bfs_dirent
*)(bh
->b_data
+ offset
);
346 offset
+= BFS_DIRENT_SIZE
;
347 if (le16_to_cpu(de
->ino
) && bfs_namecmp(namelen
, name
, de
->name
)) {
351 if (offset
< bh
->b_size
)