1 // SPDX-License-Identifier: GPL-2.0
4 * BFS directory operations.
5 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
6 * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
9 #include <linux/time.h>
10 #include <linux/string.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
,
25 int namelen
, int ino
);
26 static struct buffer_head
*bfs_find_entry(struct inode
*dir
,
27 const unsigned char *name
, int namelen
,
28 struct bfs_dirent
**res_dir
);
30 static int bfs_readdir(struct file
*f
, struct dir_context
*ctx
)
32 struct inode
*dir
= file_inode(f
);
33 struct buffer_head
*bh
;
34 struct bfs_dirent
*de
;
38 if (ctx
->pos
& (BFS_DIRENT_SIZE
- 1)) {
39 printf("Bad f_pos=%08lx for %s:%08lx\n",
40 (unsigned long)ctx
->pos
,
41 dir
->i_sb
->s_id
, dir
->i_ino
);
45 while (ctx
->pos
< dir
->i_size
) {
46 offset
= ctx
->pos
& (BFS_BSIZE
- 1);
47 block
= BFS_I(dir
)->i_sblock
+ (ctx
->pos
>> BFS_BSIZE_BITS
);
48 bh
= sb_bread(dir
->i_sb
, block
);
50 ctx
->pos
+= BFS_BSIZE
- offset
;
54 de
= (struct bfs_dirent
*)(bh
->b_data
+ offset
);
56 int size
= strnlen(de
->name
, BFS_NAMELEN
);
57 if (!dir_emit(ctx
, de
->name
, size
,
64 offset
+= BFS_DIRENT_SIZE
;
65 ctx
->pos
+= BFS_DIRENT_SIZE
;
66 } while ((offset
< BFS_BSIZE
) && (ctx
->pos
< dir
->i_size
));
72 const struct file_operations bfs_dir_operations
= {
73 .read
= generic_read_dir
,
74 .iterate_shared
= bfs_readdir
,
75 .fsync
= generic_file_fsync
,
76 .llseek
= generic_file_llseek
,
79 static int bfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
84 struct super_block
*s
= dir
->i_sb
;
85 struct bfs_sb_info
*info
= BFS_SB(s
);
91 mutex_lock(&info
->bfs_lock
);
92 ino
= find_first_zero_bit(info
->si_imap
, info
->si_lasti
+ 1);
93 if (ino
> info
->si_lasti
) {
94 mutex_unlock(&info
->bfs_lock
);
98 set_bit(ino
, info
->si_imap
);
100 inode_init_owner(inode
, dir
, mode
);
101 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
103 inode
->i_op
= &bfs_file_inops
;
104 inode
->i_fop
= &bfs_file_operations
;
105 inode
->i_mapping
->a_ops
= &bfs_aops
;
107 BFS_I(inode
)->i_dsk_ino
= ino
;
108 BFS_I(inode
)->i_sblock
= 0;
109 BFS_I(inode
)->i_eblock
= 0;
110 insert_inode_hash(inode
);
111 mark_inode_dirty(inode
);
112 bfs_dump_imap("create", s
);
114 err
= bfs_add_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
,
117 inode_dec_link_count(inode
);
118 mutex_unlock(&info
->bfs_lock
);
122 mutex_unlock(&info
->bfs_lock
);
123 d_instantiate(dentry
, inode
);
127 static struct dentry
*bfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
130 struct inode
*inode
= NULL
;
131 struct buffer_head
*bh
;
132 struct bfs_dirent
*de
;
133 struct bfs_sb_info
*info
= BFS_SB(dir
->i_sb
);
135 if (dentry
->d_name
.len
> BFS_NAMELEN
)
136 return ERR_PTR(-ENAMETOOLONG
);
138 mutex_lock(&info
->bfs_lock
);
139 bh
= bfs_find_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
141 unsigned long ino
= (unsigned long)le16_to_cpu(de
->ino
);
143 inode
= bfs_iget(dir
->i_sb
, ino
);
145 mutex_unlock(&info
->bfs_lock
);
146 return ERR_CAST(inode
);
149 mutex_unlock(&info
->bfs_lock
);
150 d_add(dentry
, inode
);
154 static int bfs_link(struct dentry
*old
, struct inode
*dir
,
157 struct inode
*inode
= d_inode(old
);
158 struct bfs_sb_info
*info
= BFS_SB(inode
->i_sb
);
161 mutex_lock(&info
->bfs_lock
);
162 err
= bfs_add_entry(dir
, new->d_name
.name
, new->d_name
.len
,
165 mutex_unlock(&info
->bfs_lock
);
169 inode
->i_ctime
= current_time(inode
);
170 mark_inode_dirty(inode
);
172 d_instantiate(new, inode
);
173 mutex_unlock(&info
->bfs_lock
);
177 static int bfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
180 struct inode
*inode
= d_inode(dentry
);
181 struct buffer_head
*bh
;
182 struct bfs_dirent
*de
;
183 struct bfs_sb_info
*info
= BFS_SB(inode
->i_sb
);
185 mutex_lock(&info
->bfs_lock
);
186 bh
= bfs_find_entry(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &de
);
187 if (!bh
|| (le16_to_cpu(de
->ino
) != inode
->i_ino
))
190 if (!inode
->i_nlink
) {
191 printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
192 inode
->i_sb
->s_id
, inode
->i_ino
,
197 mark_buffer_dirty_inode(bh
, dir
);
198 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
199 mark_inode_dirty(dir
);
200 inode
->i_ctime
= dir
->i_ctime
;
201 inode_dec_link_count(inode
);
206 mutex_unlock(&info
->bfs_lock
);
210 static int bfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
211 struct inode
*new_dir
, struct dentry
*new_dentry
,
214 struct inode
*old_inode
, *new_inode
;
215 struct buffer_head
*old_bh
, *new_bh
;
216 struct bfs_dirent
*old_de
, *new_de
;
217 struct bfs_sb_info
*info
;
220 if (flags
& ~RENAME_NOREPLACE
)
223 old_bh
= new_bh
= NULL
;
224 old_inode
= d_inode(old_dentry
);
225 if (S_ISDIR(old_inode
->i_mode
))
228 info
= BFS_SB(old_inode
->i_sb
);
230 mutex_lock(&info
->bfs_lock
);
231 old_bh
= bfs_find_entry(old_dir
,
232 old_dentry
->d_name
.name
,
233 old_dentry
->d_name
.len
, &old_de
);
235 if (!old_bh
|| (le16_to_cpu(old_de
->ino
) != old_inode
->i_ino
))
239 new_inode
= d_inode(new_dentry
);
240 new_bh
= bfs_find_entry(new_dir
,
241 new_dentry
->d_name
.name
,
242 new_dentry
->d_name
.len
, &new_de
);
244 if (new_bh
&& !new_inode
) {
249 error
= bfs_add_entry(new_dir
,
250 new_dentry
->d_name
.name
,
251 new_dentry
->d_name
.len
,
257 old_dir
->i_ctime
= old_dir
->i_mtime
= current_time(old_dir
);
258 mark_inode_dirty(old_dir
);
260 new_inode
->i_ctime
= current_time(new_inode
);
261 inode_dec_link_count(new_inode
);
263 mark_buffer_dirty_inode(old_bh
, old_dir
);
267 mutex_unlock(&info
->bfs_lock
);
273 const struct inode_operations bfs_dir_inops
= {
274 .create
= bfs_create
,
275 .lookup
= bfs_lookup
,
277 .unlink
= bfs_unlink
,
278 .rename
= bfs_rename
,
281 static int bfs_add_entry(struct inode
*dir
, const unsigned char *name
,
282 int namelen
, int ino
)
284 struct buffer_head
*bh
;
285 struct bfs_dirent
*de
;
286 int block
, sblock
, eblock
, off
, pos
;
289 dprintf("name=%s, namelen=%d\n", name
, namelen
);
293 if (namelen
> BFS_NAMELEN
)
294 return -ENAMETOOLONG
;
296 sblock
= BFS_I(dir
)->i_sblock
;
297 eblock
= BFS_I(dir
)->i_eblock
;
298 for (block
= sblock
; block
<= eblock
; block
++) {
299 bh
= sb_bread(dir
->i_sb
, block
);
302 for (off
= 0; off
< BFS_BSIZE
; off
+= BFS_DIRENT_SIZE
) {
303 de
= (struct bfs_dirent
*)(bh
->b_data
+ off
);
305 pos
= (block
- sblock
) * BFS_BSIZE
+ off
;
306 if (pos
>= dir
->i_size
) {
307 dir
->i_size
+= BFS_DIRENT_SIZE
;
308 dir
->i_ctime
= current_time(dir
);
310 dir
->i_mtime
= current_time(dir
);
311 mark_inode_dirty(dir
);
312 de
->ino
= cpu_to_le16((u16
)ino
);
313 for (i
= 0; i
< BFS_NAMELEN
; i
++)
315 (i
< namelen
) ? name
[i
] : 0;
316 mark_buffer_dirty_inode(bh
, dir
);
326 static inline int bfs_namecmp(int len
, const unsigned char *name
,
329 if ((len
< BFS_NAMELEN
) && buffer
[len
])
331 return !memcmp(name
, buffer
, len
);
334 static struct buffer_head
*bfs_find_entry(struct inode
*dir
,
335 const unsigned char *name
, int namelen
,
336 struct bfs_dirent
**res_dir
)
338 unsigned long block
= 0, offset
= 0;
339 struct buffer_head
*bh
= NULL
;
340 struct bfs_dirent
*de
;
343 if (namelen
> BFS_NAMELEN
)
346 while (block
* BFS_BSIZE
+ offset
< dir
->i_size
) {
348 bh
= sb_bread(dir
->i_sb
, BFS_I(dir
)->i_sblock
+ block
);
354 de
= (struct bfs_dirent
*)(bh
->b_data
+ offset
);
355 offset
+= BFS_DIRENT_SIZE
;
356 if (le16_to_cpu(de
->ino
) &&
357 bfs_namecmp(namelen
, name
, de
->name
)) {
361 if (offset
< bh
->b_size
)