]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/bfs/dir.c | |
3 | * BFS directory operations. | |
4 | * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com> | |
fac92bec | 5 | * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005 |
1da177e4 LT |
6 | */ |
7 | ||
8 | #include <linux/time.h> | |
9 | #include <linux/string.h> | |
10 | #include <linux/fs.h> | |
1da177e4 LT |
11 | #include <linux/buffer_head.h> |
12 | #include <linux/sched.h> | |
13 | #include "bfs.h" | |
14 | ||
15 | #undef DEBUG | |
16 | ||
17 | #ifdef DEBUG | |
18 | #define dprintf(x...) printf(x) | |
19 | #else | |
20 | #define dprintf(x...) | |
21 | #endif | |
22 | ||
f433dc56 DV |
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); | |
1da177e4 | 28 | |
f433dc56 | 29 | static int bfs_readdir(struct file *f, void *dirent, filldir_t filldir) |
1da177e4 | 30 | { |
f433dc56 DV |
31 | struct inode *dir = f->f_path.dentry->d_inode; |
32 | struct buffer_head *bh; | |
33 | struct bfs_dirent *de; | |
3f165e4c | 34 | struct bfs_sb_info *info = BFS_SB(dir->i_sb); |
1da177e4 LT |
35 | unsigned int offset; |
36 | int block; | |
37 | ||
3f165e4c | 38 | mutex_lock(&info->bfs_lock); |
1da177e4 | 39 | |
f433dc56 DV |
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); | |
3f165e4c | 44 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
45 | return -EBADF; |
46 | } | |
47 | ||
48 | while (f->f_pos < dir->i_size) { | |
f433dc56 | 49 | offset = f->f_pos & (BFS_BSIZE - 1); |
1da177e4 LT |
50 | block = BFS_I(dir)->i_sblock + (f->f_pos >> BFS_BSIZE_BITS); |
51 | bh = sb_bread(dir->i_sb, block); | |
52 | if (!bh) { | |
53 | f->f_pos += BFS_BSIZE - offset; | |
54 | continue; | |
55 | } | |
56 | do { | |
57 | de = (struct bfs_dirent *)(bh->b_data + offset); | |
58 | if (de->ino) { | |
59 | int size = strnlen(de->name, BFS_NAMELEN); | |
f433dc56 DV |
60 | if (filldir(dirent, de->name, size, f->f_pos, |
61 | le16_to_cpu(de->ino), | |
62 | DT_UNKNOWN) < 0) { | |
1da177e4 | 63 | brelse(bh); |
3f165e4c | 64 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
65 | return 0; |
66 | } | |
67 | } | |
68 | offset += BFS_DIRENT_SIZE; | |
69 | f->f_pos += BFS_DIRENT_SIZE; | |
f433dc56 | 70 | } while ((offset < BFS_BSIZE) && (f->f_pos < dir->i_size)); |
1da177e4 LT |
71 | brelse(bh); |
72 | } | |
73 | ||
3f165e4c | 74 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
75 | return 0; |
76 | } | |
77 | ||
4b6f5d20 | 78 | const struct file_operations bfs_dir_operations = { |
1da177e4 LT |
79 | .read = generic_read_dir, |
80 | .readdir = bfs_readdir, | |
1b061d92 | 81 | .fsync = generic_file_fsync, |
3222a3e5 | 82 | .llseek = generic_file_llseek, |
1da177e4 LT |
83 | }; |
84 | ||
85 | extern void dump_imap(const char *, struct super_block *); | |
86 | ||
f433dc56 DV |
87 | static int bfs_create(struct inode *dir, struct dentry *dentry, int mode, |
88 | struct nameidata *nd) | |
1da177e4 LT |
89 | { |
90 | int err; | |
f433dc56 DV |
91 | struct inode *inode; |
92 | struct super_block *s = dir->i_sb; | |
93 | struct bfs_sb_info *info = BFS_SB(s); | |
1da177e4 LT |
94 | unsigned long ino; |
95 | ||
96 | inode = new_inode(s); | |
97 | if (!inode) | |
98 | return -ENOSPC; | |
3f165e4c | 99 | mutex_lock(&info->bfs_lock); |
69b195be | 100 | ino = find_first_zero_bit(info->si_imap, info->si_lasti + 1); |
1da177e4 | 101 | if (ino > info->si_lasti) { |
3f165e4c | 102 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
103 | iput(inode); |
104 | return -ENOSPC; | |
105 | } | |
f433dc56 | 106 | set_bit(ino, info->si_imap); |
1da177e4 | 107 | info->si_freei--; |
e6ecdc70 | 108 | inode_init_owner(inode, dir, mode); |
1da177e4 | 109 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
ba52de12 | 110 | inode->i_blocks = 0; |
1da177e4 LT |
111 | inode->i_op = &bfs_file_inops; |
112 | inode->i_fop = &bfs_file_operations; | |
113 | inode->i_mapping->a_ops = &bfs_aops; | |
1da177e4 | 114 | inode->i_ino = ino; |
ce0fe7e7 | 115 | BFS_I(inode)->i_dsk_ino = ino; |
1da177e4 LT |
116 | BFS_I(inode)->i_sblock = 0; |
117 | BFS_I(inode)->i_eblock = 0; | |
118 | insert_inode_hash(inode); | |
119 | mark_inode_dirty(inode); | |
f433dc56 | 120 | dump_imap("create", s); |
1da177e4 | 121 | |
f433dc56 DV |
122 | err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, |
123 | inode->i_ino); | |
1da177e4 | 124 | if (err) { |
9a53c3a7 | 125 | inode_dec_link_count(inode); |
3f165e4c | 126 | mutex_unlock(&info->bfs_lock); |
1558182f | 127 | iput(inode); |
1da177e4 LT |
128 | return err; |
129 | } | |
3f165e4c | 130 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
131 | d_instantiate(dentry, inode); |
132 | return 0; | |
133 | } | |
134 | ||
f433dc56 DV |
135 | static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry, |
136 | struct nameidata *nd) | |
1da177e4 | 137 | { |
f433dc56 DV |
138 | struct inode *inode = NULL; |
139 | struct buffer_head *bh; | |
140 | struct bfs_dirent *de; | |
3f165e4c | 141 | struct bfs_sb_info *info = BFS_SB(dir->i_sb); |
1da177e4 LT |
142 | |
143 | if (dentry->d_name.len > BFS_NAMELEN) | |
144 | return ERR_PTR(-ENAMETOOLONG); | |
145 | ||
3f165e4c | 146 | mutex_lock(&info->bfs_lock); |
1da177e4 LT |
147 | bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de); |
148 | if (bh) { | |
fac92bec | 149 | unsigned long ino = (unsigned long)le16_to_cpu(de->ino); |
1da177e4 | 150 | brelse(bh); |
e33ab086 DH |
151 | inode = bfs_iget(dir->i_sb, ino); |
152 | if (IS_ERR(inode)) { | |
3f165e4c | 153 | mutex_unlock(&info->bfs_lock); |
e33ab086 | 154 | return ERR_CAST(inode); |
1da177e4 LT |
155 | } |
156 | } | |
3f165e4c | 157 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
158 | d_add(dentry, inode); |
159 | return NULL; | |
160 | } | |
161 | ||
f433dc56 DV |
162 | static int bfs_link(struct dentry *old, struct inode *dir, |
163 | struct dentry *new) | |
1da177e4 | 164 | { |
f433dc56 | 165 | struct inode *inode = old->d_inode; |
3f165e4c | 166 | struct bfs_sb_info *info = BFS_SB(inode->i_sb); |
1da177e4 LT |
167 | int err; |
168 | ||
3f165e4c | 169 | mutex_lock(&info->bfs_lock); |
f433dc56 DV |
170 | err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, |
171 | inode->i_ino); | |
1da177e4 | 172 | if (err) { |
3f165e4c | 173 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
174 | return err; |
175 | } | |
d8c76e6f | 176 | inc_nlink(inode); |
1da177e4 LT |
177 | inode->i_ctime = CURRENT_TIME_SEC; |
178 | mark_inode_dirty(inode); | |
7de9c6ee | 179 | ihold(inode); |
1da177e4 | 180 | d_instantiate(new, inode); |
3f165e4c | 181 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
182 | return 0; |
183 | } | |
184 | ||
f433dc56 | 185 | static int bfs_unlink(struct inode *dir, struct dentry *dentry) |
1da177e4 LT |
186 | { |
187 | int error = -ENOENT; | |
3f165e4c | 188 | struct inode *inode = dentry->d_inode; |
f433dc56 DV |
189 | struct buffer_head *bh; |
190 | struct bfs_dirent *de; | |
3f165e4c | 191 | struct bfs_sb_info *info = BFS_SB(inode->i_sb); |
1da177e4 | 192 | |
3f165e4c | 193 | mutex_lock(&info->bfs_lock); |
1da177e4 | 194 | bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de); |
f433dc56 | 195 | if (!bh || (le16_to_cpu(de->ino) != inode->i_ino)) |
1da177e4 LT |
196 | goto out_brelse; |
197 | ||
198 | if (!inode->i_nlink) { | |
f433dc56 DV |
199 | printf("unlinking non-existent file %s:%lu (nlink=%d)\n", |
200 | inode->i_sb->s_id, inode->i_ino, | |
201 | inode->i_nlink); | |
1da177e4 LT |
202 | inode->i_nlink = 1; |
203 | } | |
204 | de->ino = 0; | |
4427f0c3 | 205 | mark_buffer_dirty_inode(bh, dir); |
1da177e4 LT |
206 | dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC; |
207 | mark_inode_dirty(dir); | |
1da177e4 | 208 | inode->i_ctime = dir->i_ctime; |
9a53c3a7 | 209 | inode_dec_link_count(inode); |
1da177e4 LT |
210 | error = 0; |
211 | ||
212 | out_brelse: | |
213 | brelse(bh); | |
3f165e4c | 214 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
215 | return error; |
216 | } | |
217 | ||
f433dc56 DV |
218 | static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
219 | struct inode *new_dir, struct dentry *new_dentry) | |
1da177e4 | 220 | { |
f433dc56 DV |
221 | struct inode *old_inode, *new_inode; |
222 | struct buffer_head *old_bh, *new_bh; | |
223 | struct bfs_dirent *old_de, *new_de; | |
3f165e4c | 224 | struct bfs_sb_info *info; |
1da177e4 LT |
225 | int error = -ENOENT; |
226 | ||
e4eaac06 SW |
227 | if (new_dentry->d_inode && S_ISDIR(new_dentry->d_inode->i_mode)) |
228 | dentry_unhash(new_dentry); | |
229 | ||
1da177e4 LT |
230 | old_bh = new_bh = NULL; |
231 | old_inode = old_dentry->d_inode; | |
232 | if (S_ISDIR(old_inode->i_mode)) | |
233 | return -EINVAL; | |
234 | ||
3f165e4c DV |
235 | info = BFS_SB(old_inode->i_sb); |
236 | ||
237 | mutex_lock(&info->bfs_lock); | |
1da177e4 LT |
238 | old_bh = bfs_find_entry(old_dir, |
239 | old_dentry->d_name.name, | |
240 | old_dentry->d_name.len, &old_de); | |
241 | ||
f433dc56 | 242 | if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino)) |
1da177e4 LT |
243 | goto end_rename; |
244 | ||
245 | error = -EPERM; | |
246 | new_inode = new_dentry->d_inode; | |
247 | new_bh = bfs_find_entry(new_dir, | |
248 | new_dentry->d_name.name, | |
249 | new_dentry->d_name.len, &new_de); | |
250 | ||
251 | if (new_bh && !new_inode) { | |
252 | brelse(new_bh); | |
253 | new_bh = NULL; | |
254 | } | |
255 | if (!new_bh) { | |
256 | error = bfs_add_entry(new_dir, | |
257 | new_dentry->d_name.name, | |
f433dc56 DV |
258 | new_dentry->d_name.len, |
259 | old_inode->i_ino); | |
1da177e4 LT |
260 | if (error) |
261 | goto end_rename; | |
262 | } | |
263 | old_de->ino = 0; | |
264 | old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC; | |
265 | mark_inode_dirty(old_dir); | |
266 | if (new_inode) { | |
1da177e4 | 267 | new_inode->i_ctime = CURRENT_TIME_SEC; |
9a53c3a7 | 268 | inode_dec_link_count(new_inode); |
1da177e4 | 269 | } |
4427f0c3 | 270 | mark_buffer_dirty_inode(old_bh, old_dir); |
1da177e4 LT |
271 | error = 0; |
272 | ||
273 | end_rename: | |
3f165e4c | 274 | mutex_unlock(&info->bfs_lock); |
1da177e4 LT |
275 | brelse(old_bh); |
276 | brelse(new_bh); | |
277 | return error; | |
278 | } | |
279 | ||
754661f1 | 280 | const struct inode_operations bfs_dir_inops = { |
1da177e4 LT |
281 | .create = bfs_create, |
282 | .lookup = bfs_lookup, | |
283 | .link = bfs_link, | |
284 | .unlink = bfs_unlink, | |
285 | .rename = bfs_rename, | |
286 | }; | |
287 | ||
f433dc56 DV |
288 | static int bfs_add_entry(struct inode *dir, const unsigned char *name, |
289 | int namelen, int ino) | |
1da177e4 | 290 | { |
f433dc56 DV |
291 | struct buffer_head *bh; |
292 | struct bfs_dirent *de; | |
293 | int block, sblock, eblock, off, pos; | |
1da177e4 LT |
294 | int i; |
295 | ||
296 | dprintf("name=%s, namelen=%d\n", name, namelen); | |
297 | ||
298 | if (!namelen) | |
299 | return -ENOENT; | |
300 | if (namelen > BFS_NAMELEN) | |
301 | return -ENAMETOOLONG; | |
302 | ||
303 | sblock = BFS_I(dir)->i_sblock; | |
304 | eblock = BFS_I(dir)->i_eblock; | |
f433dc56 | 305 | for (block = sblock; block <= eblock; block++) { |
1da177e4 | 306 | bh = sb_bread(dir->i_sb, block); |
f433dc56 | 307 | if (!bh) |
1da177e4 | 308 | return -ENOSPC; |
f433dc56 | 309 | for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) { |
1da177e4 | 310 | de = (struct bfs_dirent *)(bh->b_data + off); |
1da177e4 | 311 | if (!de->ino) { |
f433dc56 DV |
312 | pos = (block - sblock) * BFS_BSIZE + off; |
313 | if (pos >= dir->i_size) { | |
1da177e4 LT |
314 | dir->i_size += BFS_DIRENT_SIZE; |
315 | dir->i_ctime = CURRENT_TIME_SEC; | |
316 | } | |
317 | dir->i_mtime = CURRENT_TIME_SEC; | |
318 | mark_inode_dirty(dir); | |
fac92bec | 319 | de->ino = cpu_to_le16((u16)ino); |
f433dc56 DV |
320 | for (i = 0; i < BFS_NAMELEN; i++) |
321 | de->name[i] = | |
322 | (i < namelen) ? name[i] : 0; | |
4427f0c3 | 323 | mark_buffer_dirty_inode(bh, dir); |
1da177e4 LT |
324 | brelse(bh); |
325 | return 0; | |
326 | } | |
327 | } | |
328 | brelse(bh); | |
329 | } | |
330 | return -ENOSPC; | |
331 | } | |
332 | ||
f433dc56 DV |
333 | static inline int bfs_namecmp(int len, const unsigned char *name, |
334 | const char *buffer) | |
1da177e4 | 335 | { |
f433dc56 | 336 | if ((len < BFS_NAMELEN) && buffer[len]) |
1da177e4 LT |
337 | return 0; |
338 | return !memcmp(name, buffer, len); | |
339 | } | |
340 | ||
f433dc56 DV |
341 | static struct buffer_head *bfs_find_entry(struct inode *dir, |
342 | const unsigned char *name, int namelen, | |
343 | struct bfs_dirent **res_dir) | |
1da177e4 | 344 | { |
f433dc56 DV |
345 | unsigned long block = 0, offset = 0; |
346 | struct buffer_head *bh = NULL; | |
347 | struct bfs_dirent *de; | |
1da177e4 LT |
348 | |
349 | *res_dir = NULL; | |
350 | if (namelen > BFS_NAMELEN) | |
351 | return NULL; | |
f433dc56 | 352 | |
1da177e4 LT |
353 | while (block * BFS_BSIZE + offset < dir->i_size) { |
354 | if (!bh) { | |
355 | bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block); | |
356 | if (!bh) { | |
357 | block++; | |
358 | continue; | |
359 | } | |
360 | } | |
361 | de = (struct bfs_dirent *)(bh->b_data + offset); | |
362 | offset += BFS_DIRENT_SIZE; | |
f433dc56 DV |
363 | if (le16_to_cpu(de->ino) && |
364 | bfs_namecmp(namelen, name, de->name)) { | |
1da177e4 LT |
365 | *res_dir = de; |
366 | return bh; | |
367 | } | |
368 | if (offset < bh->b_size) | |
369 | continue; | |
370 | brelse(bh); | |
371 | bh = NULL; | |
372 | offset = 0; | |
373 | block++; | |
374 | } | |
375 | brelse(bh); | |
376 | return NULL; | |
377 | } |