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