blob: a399e6d9dc74d1a1d2fb225af24685886cd5ec59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/bfs/dir.c
3 * BFS directory operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
Andrew Stribblehillfac92be2005-09-09 13:02:04 -07005 * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/time.h>
9#include <linux/string.h>
10#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080023static int bfs_add_entry(struct inode *dir, const unsigned char *name,
24 int namelen, int ino);
25static struct buffer_head *bfs_find_entry(struct inode *dir,
26 const unsigned char *name, int namelen,
27 struct bfs_dirent **res_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Al Viro81b9f662013-05-16 13:41:48 -040029static int bfs_readdir(struct file *f, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Al Viro496ad9a2013-01-23 17:07:38 -050031 struct inode *dir = file_inode(f);
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080032 struct buffer_head *bh;
33 struct bfs_dirent *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 unsigned int offset;
35 int block;
36
Al Viro81b9f662013-05-16 13:41:48 -040037 if (ctx->pos & (BFS_DIRENT_SIZE - 1)) {
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080038 printf("Bad f_pos=%08lx for %s:%08lx\n",
Al Viro81b9f662013-05-16 13:41:48 -040039 (unsigned long)ctx->pos,
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080040 dir->i_sb->s_id, dir->i_ino);
Al Viro81b9f662013-05-16 13:41:48 -040041 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43
Al Viro81b9f662013-05-16 13:41:48 -040044 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 bh = sb_bread(dir->i_sb, block);
48 if (!bh) {
Al Viro81b9f662013-05-16 13:41:48 -040049 ctx->pos += BFS_BSIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 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);
Al Viro81b9f662013-05-16 13:41:48 -040056 if (!dir_emit(ctx, de->name, size,
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080057 le16_to_cpu(de->ino),
Al Viro81b9f662013-05-16 13:41:48 -040058 DT_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return 0;
61 }
62 }
63 offset += BFS_DIRENT_SIZE;
Al Viro81b9f662013-05-16 13:41:48 -040064 ctx->pos += BFS_DIRENT_SIZE;
65 } while ((offset < BFS_BSIZE) && (ctx->pos < dir->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 brelse(bh);
67 }
Al Viro81b9f662013-05-16 13:41:48 -040068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080071const struct file_operations bfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .read = generic_read_dir,
Al Viro81b9f662013-05-16 13:41:48 -040073 .iterate = bfs_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +020074 .fsync = generic_file_fsync,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +020075 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
78extern void dump_imap(const char *, struct super_block *);
79
Al Viro4acdaf22011-07-26 01:42:34 -040080static int bfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040081 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 int err;
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080084 struct inode *inode;
85 struct super_block *s = dir->i_sb;
86 struct bfs_sb_info *info = BFS_SB(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned long ino;
88
89 inode = new_inode(s);
90 if (!inode)
91 return -ENOSPC;
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -070092 mutex_lock(&info->bfs_lock);
Akinobu Mita69b195b2011-03-21 08:32:53 -040093 ino = find_first_zero_bit(info->si_imap, info->si_lasti + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (ino > info->si_lasti) {
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -070095 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 iput(inode);
97 return -ENOSPC;
98 }
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080099 set_bit(ino, info->si_imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 info->si_freei--;
Dmitry Monakhove6ecdc72010-03-04 17:31:46 +0300101 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700103 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 inode->i_op = &bfs_file_inops;
105 inode->i_fop = &bfs_file_operations;
106 inode->i_mapping->a_ops = &bfs_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 inode->i_ino = ino;
Alexey Dobriyance0fe7e2005-10-04 17:43:06 +0100108 BFS_I(inode)->i_dsk_ino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 BFS_I(inode)->i_sblock = 0;
110 BFS_I(inode)->i_eblock = 0;
111 insert_inode_hash(inode);
112 mark_inode_dirty(inode);
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800113 dump_imap("create", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800115 err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len,
116 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (err) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700118 inode_dec_link_count(inode);
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700119 mutex_unlock(&info->bfs_lock);
Eric Sesterhenn15581822008-09-13 02:33:12 -0700120 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return err;
122 }
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700123 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 d_instantiate(dentry, inode);
125 return 0;
126}
127
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800128static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400129 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800131 struct inode *inode = NULL;
132 struct buffer_head *bh;
133 struct bfs_dirent *de;
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700134 struct bfs_sb_info *info = BFS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if (dentry->d_name.len > BFS_NAMELEN)
137 return ERR_PTR(-ENAMETOOLONG);
138
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700139 mutex_lock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
141 if (bh) {
Andrew Stribblehillfac92be2005-09-09 13:02:04 -0700142 unsigned long ino = (unsigned long)le16_to_cpu(de->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 brelse(bh);
David Howellse33ab082008-02-07 00:15:32 -0800144 inode = bfs_iget(dir->i_sb, ino);
145 if (IS_ERR(inode)) {
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700146 mutex_unlock(&info->bfs_lock);
David Howellse33ab082008-02-07 00:15:32 -0800147 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 }
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700150 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 d_add(dentry, inode);
152 return NULL;
153}
154
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800155static int bfs_link(struct dentry *old, struct inode *dir,
156 struct dentry *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800158 struct inode *inode = old->d_inode;
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700159 struct bfs_sb_info *info = BFS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int err;
161
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700162 mutex_lock(&info->bfs_lock);
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800163 err = bfs_add_entry(dir, new->d_name.name, new->d_name.len,
164 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (err) {
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700166 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return err;
168 }
Dave Hansend8c76e62006-09-30 23:29:04 -0700169 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 inode->i_ctime = CURRENT_TIME_SEC;
171 mark_inode_dirty(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400172 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 d_instantiate(new, inode);
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700174 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800178static int bfs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int error = -ENOENT;
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700181 struct inode *inode = dentry->d_inode;
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800182 struct buffer_head *bh;
183 struct bfs_dirent *de;
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700184 struct bfs_sb_info *info = BFS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700186 mutex_lock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800188 if (!bh || (le16_to_cpu(de->ino) != inode->i_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto out_brelse;
190
191 if (!inode->i_nlink) {
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800192 printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
193 inode->i_sb->s_id, inode->i_ino,
194 inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200195 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 de->ino = 0;
Al Viro4427f0c2009-06-08 01:15:58 -0400198 mark_buffer_dirty_inode(bh, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
200 mark_inode_dirty(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 inode->i_ctime = dir->i_ctime;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700202 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 error = 0;
204
205out_brelse:
206 brelse(bh);
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700207 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return error;
209}
210
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800211static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry,
212 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800214 struct inode *old_inode, *new_inode;
215 struct buffer_head *old_bh, *new_bh;
216 struct bfs_dirent *old_de, *new_de;
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700217 struct bfs_sb_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 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
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700225 info = BFS_SB(old_inode->i_sb);
226
227 mutex_lock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 old_bh = bfs_find_entry(old_dir,
229 old_dentry->d_name.name,
230 old_dentry->d_name.len, &old_de);
231
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800232 if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 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,
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800248 new_dentry->d_name.len,
249 old_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 new_inode->i_ctime = CURRENT_TIME_SEC;
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700258 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Al Viro4427f0c2009-06-08 01:15:58 -0400260 mark_buffer_dirty_inode(old_bh, old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 error = 0;
262
263end_rename:
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700264 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 brelse(old_bh);
266 brelse(new_bh);
267 return error;
268}
269
Arjan van de Ven754661f2007-02-12 00:55:38 -0800270const struct inode_operations bfs_dir_inops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .create = bfs_create,
272 .lookup = bfs_lookup,
273 .link = bfs_link,
274 .unlink = bfs_unlink,
275 .rename = bfs_rename,
276};
277
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800278static int bfs_add_entry(struct inode *dir, const unsigned char *name,
279 int namelen, int ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800281 struct buffer_head *bh;
282 struct bfs_dirent *de;
283 int block, sblock, eblock, off, pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 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;
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800295 for (block = sblock; block <= eblock; block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 bh = sb_bread(dir->i_sb, block);
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800297 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -ENOSPC;
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800299 for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 de = (struct bfs_dirent *)(bh->b_data + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!de->ino) {
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800302 pos = (block - sblock) * BFS_BSIZE + off;
303 if (pos >= dir->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 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);
Andrew Stribblehillfac92be2005-09-09 13:02:04 -0700309 de->ino = cpu_to_le16((u16)ino);
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800310 for (i = 0; i < BFS_NAMELEN; i++)
311 de->name[i] =
312 (i < namelen) ? name[i] : 0;
Al Viro4427f0c2009-06-08 01:15:58 -0400313 mark_buffer_dirty_inode(bh, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 brelse(bh);
315 return 0;
316 }
317 }
318 brelse(bh);
319 }
320 return -ENOSPC;
321}
322
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800323static inline int bfs_namecmp(int len, const unsigned char *name,
324 const char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800326 if ((len < BFS_NAMELEN) && buffer[len])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328 return !memcmp(name, buffer, len);
329}
330
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800331static struct buffer_head *bfs_find_entry(struct inode *dir,
332 const unsigned char *name, int namelen,
333 struct bfs_dirent **res_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800335 unsigned long block = 0, offset = 0;
336 struct buffer_head *bh = NULL;
337 struct bfs_dirent *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 *res_dir = NULL;
340 if (namelen > BFS_NAMELEN)
341 return NULL;
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 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;
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800353 if (le16_to_cpu(de->ino) &&
354 bfs_namecmp(namelen, name, de->name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *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}