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