dir.c 8.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5
/*
 *	fs/bfs/dir.c
 *	BFS directory operations.
 *	Copyright (C) 1999,2000  Tigran Aivazian <tigran@veritas.com>
6
 *      Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 */

#include <linux/time.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/buffer_head.h>
#include <linux/sched.h>
#include "bfs.h"

#undef DEBUG

#ifdef DEBUG
#define dprintf(x...)	printf(x)
#else
#define dprintf(x...)
#endif

24
static int bfs_add_entry(struct inode *dir, const struct qstr *child, int ino);
25
static struct buffer_head *bfs_find_entry(struct inode *dir,
26
				const struct qstr *child,
27
				struct bfs_dirent **res_dir);
L
Linus Torvalds 已提交
28

A
Al Viro 已提交
29
static int bfs_readdir(struct file *f, struct dir_context *ctx)
L
Linus Torvalds 已提交
30
{
A
Al Viro 已提交
31
	struct inode *dir = file_inode(f);
32 33
	struct buffer_head *bh;
	struct bfs_dirent *de;
L
Linus Torvalds 已提交
34 35 36
	unsigned int offset;
	int block;

A
Al Viro 已提交
37
	if (ctx->pos & (BFS_DIRENT_SIZE - 1)) {
38
		printf("Bad f_pos=%08lx for %s:%08lx\n",
A
Al Viro 已提交
39
					(unsigned long)ctx->pos,
40
					dir->i_sb->s_id, dir->i_ino);
A
Al Viro 已提交
41
		return -EINVAL;
L
Linus Torvalds 已提交
42 43
	}

A
Al Viro 已提交
44 45 46
	while (ctx->pos < dir->i_size) {
		offset = ctx->pos & (BFS_BSIZE - 1);
		block = BFS_I(dir)->i_sblock + (ctx->pos >> BFS_BSIZE_BITS);
L
Linus Torvalds 已提交
47 48
		bh = sb_bread(dir->i_sb, block);
		if (!bh) {
A
Al Viro 已提交
49
			ctx->pos += BFS_BSIZE - offset;
L
Linus Torvalds 已提交
50 51 52 53 54 55
			continue;
		}
		do {
			de = (struct bfs_dirent *)(bh->b_data + offset);
			if (de->ino) {
				int size = strnlen(de->name, BFS_NAMELEN);
A
Al Viro 已提交
56
				if (!dir_emit(ctx, de->name, size,
57
						le16_to_cpu(de->ino),
A
Al Viro 已提交
58
						DT_UNKNOWN)) {
L
Linus Torvalds 已提交
59 60 61 62 63
					brelse(bh);
					return 0;
				}
			}
			offset += BFS_DIRENT_SIZE;
A
Al Viro 已提交
64 65
			ctx->pos += BFS_DIRENT_SIZE;
		} while ((offset < BFS_BSIZE) && (ctx->pos < dir->i_size));
L
Linus Torvalds 已提交
66 67
		brelse(bh);
	}
A
Al Viro 已提交
68
	return 0;
L
Linus Torvalds 已提交
69 70
}

71
const struct file_operations bfs_dir_operations = {
L
Linus Torvalds 已提交
72
	.read		= generic_read_dir,
73
	.iterate_shared	= bfs_readdir,
74
	.fsync		= generic_file_fsync,
75
	.llseek		= generic_file_llseek,
L
Linus Torvalds 已提交
76 77
};

A
Al Viro 已提交
78
static int bfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
79
						bool excl)
L
Linus Torvalds 已提交
80 81
{
	int err;
82 83 84
	struct inode *inode;
	struct super_block *s = dir->i_sb;
	struct bfs_sb_info *info = BFS_SB(s);
L
Linus Torvalds 已提交
85 86 87 88
	unsigned long ino;

	inode = new_inode(s);
	if (!inode)
S
Sanidhya Kashyap 已提交
89
		return -ENOMEM;
D
Dmitri Vorobiev 已提交
90
	mutex_lock(&info->bfs_lock);
91
	ino = find_first_zero_bit(info->si_imap, info->si_lasti + 1);
L
Linus Torvalds 已提交
92
	if (ino > info->si_lasti) {
D
Dmitri Vorobiev 已提交
93
		mutex_unlock(&info->bfs_lock);
L
Linus Torvalds 已提交
94 95 96
		iput(inode);
		return -ENOSPC;
	}
97
	set_bit(ino, info->si_imap);
L
Linus Torvalds 已提交
98
	info->si_freei--;
99
	inode_init_owner(inode, dir, mode);
100
	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
101
	inode->i_blocks = 0;
L
Linus Torvalds 已提交
102 103 104 105
	inode->i_op = &bfs_file_inops;
	inode->i_fop = &bfs_file_operations;
	inode->i_mapping->a_ops = &bfs_aops;
	inode->i_ino = ino;
106
	BFS_I(inode)->i_dsk_ino = ino;
L
Linus Torvalds 已提交
107 108 109 110
	BFS_I(inode)->i_sblock = 0;
	BFS_I(inode)->i_eblock = 0;
	insert_inode_hash(inode);
        mark_inode_dirty(inode);
111
	bfs_dump_imap("create", s);
L
Linus Torvalds 已提交
112

113
	err = bfs_add_entry(dir, &dentry->d_name, inode->i_ino);
L
Linus Torvalds 已提交
114
	if (err) {
115
		inode_dec_link_count(inode);
D
Dmitri Vorobiev 已提交
116
		mutex_unlock(&info->bfs_lock);
E
Eric Sesterhenn 已提交
117
		iput(inode);
L
Linus Torvalds 已提交
118 119
		return err;
	}
D
Dmitri Vorobiev 已提交
120
	mutex_unlock(&info->bfs_lock);
L
Linus Torvalds 已提交
121 122 123 124
	d_instantiate(dentry, inode);
	return 0;
}

125
static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
126
						unsigned int flags)
L
Linus Torvalds 已提交
127
{
128 129 130
	struct inode *inode = NULL;
	struct buffer_head *bh;
	struct bfs_dirent *de;
D
Dmitri Vorobiev 已提交
131
	struct bfs_sb_info *info = BFS_SB(dir->i_sb);
L
Linus Torvalds 已提交
132 133 134 135

	if (dentry->d_name.len > BFS_NAMELEN)
		return ERR_PTR(-ENAMETOOLONG);

D
Dmitri Vorobiev 已提交
136
	mutex_lock(&info->bfs_lock);
137
	bh = bfs_find_entry(dir, &dentry->d_name, &de);
L
Linus Torvalds 已提交
138
	if (bh) {
139
		unsigned long ino = (unsigned long)le16_to_cpu(de->ino);
L
Linus Torvalds 已提交
140
		brelse(bh);
141
		inode = bfs_iget(dir->i_sb, ino);
L
Linus Torvalds 已提交
142
	}
D
Dmitri Vorobiev 已提交
143
	mutex_unlock(&info->bfs_lock);
A
Al Viro 已提交
144
	return d_splice_alias(inode, dentry);
L
Linus Torvalds 已提交
145 146
}

147 148
static int bfs_link(struct dentry *old, struct inode *dir,
						struct dentry *new)
L
Linus Torvalds 已提交
149
{
150
	struct inode *inode = d_inode(old);
D
Dmitri Vorobiev 已提交
151
	struct bfs_sb_info *info = BFS_SB(inode->i_sb);
L
Linus Torvalds 已提交
152 153
	int err;

D
Dmitri Vorobiev 已提交
154
	mutex_lock(&info->bfs_lock);
155
	err = bfs_add_entry(dir, &new->d_name, inode->i_ino);
L
Linus Torvalds 已提交
156
	if (err) {
D
Dmitri Vorobiev 已提交
157
		mutex_unlock(&info->bfs_lock);
L
Linus Torvalds 已提交
158 159
		return err;
	}
160
	inc_nlink(inode);
161
	inode->i_ctime = current_time(inode);
L
Linus Torvalds 已提交
162
	mark_inode_dirty(inode);
A
Al Viro 已提交
163
	ihold(inode);
L
Linus Torvalds 已提交
164
	d_instantiate(new, inode);
D
Dmitri Vorobiev 已提交
165
	mutex_unlock(&info->bfs_lock);
L
Linus Torvalds 已提交
166 167 168
	return 0;
}

169
static int bfs_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
170 171
{
	int error = -ENOENT;
172
	struct inode *inode = d_inode(dentry);
173 174
	struct buffer_head *bh;
	struct bfs_dirent *de;
D
Dmitri Vorobiev 已提交
175
	struct bfs_sb_info *info = BFS_SB(inode->i_sb);
L
Linus Torvalds 已提交
176

D
Dmitri Vorobiev 已提交
177
	mutex_lock(&info->bfs_lock);
178
	bh = bfs_find_entry(dir, &dentry->d_name, &de);
179
	if (!bh || (le16_to_cpu(de->ino) != inode->i_ino))
L
Linus Torvalds 已提交
180 181 182
		goto out_brelse;

	if (!inode->i_nlink) {
183 184 185
		printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
					inode->i_sb->s_id, inode->i_ino,
					inode->i_nlink);
M
Miklos Szeredi 已提交
186
		set_nlink(inode, 1);
L
Linus Torvalds 已提交
187 188
	}
	de->ino = 0;
189
	mark_buffer_dirty_inode(bh, dir);
190
	dir->i_ctime = dir->i_mtime = current_time(dir);
L
Linus Torvalds 已提交
191 192
	mark_inode_dirty(dir);
	inode->i_ctime = dir->i_ctime;
193
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
194 195 196 197
	error = 0;

out_brelse:
	brelse(bh);
D
Dmitri Vorobiev 已提交
198
	mutex_unlock(&info->bfs_lock);
L
Linus Torvalds 已提交
199 200 201
	return error;
}

202
static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry,
203 204
		      struct inode *new_dir, struct dentry *new_dentry,
		      unsigned int flags)
L
Linus Torvalds 已提交
205
{
206 207 208
	struct inode *old_inode, *new_inode;
	struct buffer_head *old_bh, *new_bh;
	struct bfs_dirent *old_de, *new_de;
D
Dmitri Vorobiev 已提交
209
	struct bfs_sb_info *info;
L
Linus Torvalds 已提交
210 211
	int error = -ENOENT;

212 213 214
	if (flags & ~RENAME_NOREPLACE)
		return -EINVAL;

L
Linus Torvalds 已提交
215
	old_bh = new_bh = NULL;
216
	old_inode = d_inode(old_dentry);
L
Linus Torvalds 已提交
217 218 219
	if (S_ISDIR(old_inode->i_mode))
		return -EINVAL;

D
Dmitri Vorobiev 已提交
220 221 222
	info = BFS_SB(old_inode->i_sb);

	mutex_lock(&info->bfs_lock);
223
	old_bh = bfs_find_entry(old_dir, &old_dentry->d_name, &old_de);
L
Linus Torvalds 已提交
224

225
	if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
L
Linus Torvalds 已提交
226 227 228
		goto end_rename;

	error = -EPERM;
229
	new_inode = d_inode(new_dentry);
230
	new_bh = bfs_find_entry(new_dir, &new_dentry->d_name, &new_de);
L
Linus Torvalds 已提交
231 232 233 234 235 236

	if (new_bh && !new_inode) {
		brelse(new_bh);
		new_bh = NULL;
	}
	if (!new_bh) {
237
		error = bfs_add_entry(new_dir, &new_dentry->d_name,
238
					old_inode->i_ino);
L
Linus Torvalds 已提交
239 240 241 242
		if (error)
			goto end_rename;
	}
	old_de->ino = 0;
243
	old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
L
Linus Torvalds 已提交
244 245
	mark_inode_dirty(old_dir);
	if (new_inode) {
246
		new_inode->i_ctime = current_time(new_inode);
247
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
248
	}
249
	mark_buffer_dirty_inode(old_bh, old_dir);
L
Linus Torvalds 已提交
250 251 252
	error = 0;

end_rename:
D
Dmitri Vorobiev 已提交
253
	mutex_unlock(&info->bfs_lock);
L
Linus Torvalds 已提交
254 255 256 257 258
	brelse(old_bh);
	brelse(new_bh);
	return error;
}

259
const struct inode_operations bfs_dir_inops = {
L
Linus Torvalds 已提交
260 261 262 263 264 265 266
	.create			= bfs_create,
	.lookup			= bfs_lookup,
	.link			= bfs_link,
	.unlink			= bfs_unlink,
	.rename			= bfs_rename,
};

267
static int bfs_add_entry(struct inode *dir, const struct qstr *child, int ino)
L
Linus Torvalds 已提交
268
{
269 270
	const unsigned char *name = child->name;
	int namelen = child->len;
271 272 273
	struct buffer_head *bh;
	struct bfs_dirent *de;
	int block, sblock, eblock, off, pos;
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284
	int i;

	dprintf("name=%s, namelen=%d\n", name, namelen);

	if (!namelen)
		return -ENOENT;
	if (namelen > BFS_NAMELEN)
		return -ENAMETOOLONG;

	sblock = BFS_I(dir)->i_sblock;
	eblock = BFS_I(dir)->i_eblock;
285
	for (block = sblock; block <= eblock; block++) {
L
Linus Torvalds 已提交
286
		bh = sb_bread(dir->i_sb, block);
287
		if (!bh)
S
Sanidhya Kashyap 已提交
288
			return -EIO;
289
		for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) {
L
Linus Torvalds 已提交
290 291
			de = (struct bfs_dirent *)(bh->b_data + off);
			if (!de->ino) {
292 293
				pos = (block - sblock) * BFS_BSIZE + off;
				if (pos >= dir->i_size) {
L
Linus Torvalds 已提交
294
					dir->i_size += BFS_DIRENT_SIZE;
295
					dir->i_ctime = current_time(dir);
L
Linus Torvalds 已提交
296
				}
297
				dir->i_mtime = current_time(dir);
L
Linus Torvalds 已提交
298
				mark_inode_dirty(dir);
299
				de->ino = cpu_to_le16((u16)ino);
300 301 302
				for (i = 0; i < BFS_NAMELEN; i++)
					de->name[i] =
						(i < namelen) ? name[i] : 0;
303
				mark_buffer_dirty_inode(bh, dir);
L
Linus Torvalds 已提交
304 305 306 307 308 309 310 311 312
				brelse(bh);
				return 0;
			}
		}
		brelse(bh);
	}
	return -ENOSPC;
}

313 314
static inline int bfs_namecmp(int len, const unsigned char *name,
							const char *buffer)
L
Linus Torvalds 已提交
315
{
316
	if ((len < BFS_NAMELEN) && buffer[len])
L
Linus Torvalds 已提交
317 318 319 320
		return 0;
	return !memcmp(name, buffer, len);
}

321
static struct buffer_head *bfs_find_entry(struct inode *dir,
322
			const struct qstr *child,
323
			struct bfs_dirent **res_dir)
L
Linus Torvalds 已提交
324
{
325 326 327
	unsigned long block = 0, offset = 0;
	struct buffer_head *bh = NULL;
	struct bfs_dirent *de;
328 329
	const unsigned char *name = child->name;
	int namelen = child->len;
L
Linus Torvalds 已提交
330 331 332 333

	*res_dir = NULL;
	if (namelen > BFS_NAMELEN)
		return NULL;
334

L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343 344
	while (block * BFS_BSIZE + offset < dir->i_size) {
		if (!bh) {
			bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
			if (!bh) {
				block++;
				continue;
			}
		}
		de = (struct bfs_dirent *)(bh->b_data + offset);
		offset += BFS_DIRENT_SIZE;
345 346
		if (le16_to_cpu(de->ino) &&
				bfs_namecmp(namelen, name, de->name)) {
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359
			*res_dir = de;
			return bh;
		}
		if (offset < bh->b_size)
			continue;
		brelse(bh);
		bh = NULL;
		offset = 0;
		block++;
	}
	brelse(bh);
	return NULL;
}