dir.c 22.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
4
 * Copyright © 2001-2007 Red Hat, Inc.
5
 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Created by David Woodhouse <dwmw2@infradead.org>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/crc32.h>
#include <linux/jffs2.h>
18 19
#include "jffs2_fs_i.h"
#include "jffs2_fs_sb.h"
L
Linus Torvalds 已提交
20 21 22 23 24
#include <linux/time.h>
#include "nodelist.h"

static int jffs2_readdir (struct file *, void *, filldir_t);

A
Al Viro 已提交
25
static int jffs2_create (struct inode *,struct dentry *,umode_t,
L
Linus Torvalds 已提交
26 27 28 29 30 31
			 struct nameidata *);
static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
				    struct nameidata *);
static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
static int jffs2_unlink (struct inode *,struct dentry *);
static int jffs2_symlink (struct inode *,struct dentry *,const char *);
32
static int jffs2_mkdir (struct inode *,struct dentry *,umode_t);
L
Linus Torvalds 已提交
33
static int jffs2_rmdir (struct inode *,struct dentry *);
A
Al Viro 已提交
34
static int jffs2_mknod (struct inode *,struct dentry *,umode_t,dev_t);
L
Linus Torvalds 已提交
35
static int jffs2_rename (struct inode *, struct dentry *,
D
David Woodhouse 已提交
36
			 struct inode *, struct dentry *);
L
Linus Torvalds 已提交
37

38
const struct file_operations jffs2_dir_operations =
L
Linus Torvalds 已提交
39 40 41
{
	.read =		generic_read_dir,
	.readdir =	jffs2_readdir,
S
Stoyan Gaydarov 已提交
42
	.unlocked_ioctl=jffs2_ioctl,
43 44
	.fsync =	jffs2_fsync,
	.llseek =	generic_file_llseek,
L
Linus Torvalds 已提交
45 46 47
};


48
const struct inode_operations jffs2_dir_inode_operations =
L
Linus Torvalds 已提交
49
{
50 51
	.create =	jffs2_create,
	.lookup =	jffs2_lookup,
L
Linus Torvalds 已提交
52 53 54 55 56 57 58
	.link =		jffs2_link,
	.unlink =	jffs2_unlink,
	.symlink =	jffs2_symlink,
	.mkdir =	jffs2_mkdir,
	.rmdir =	jffs2_rmdir,
	.mknod =	jffs2_mknod,
	.rename =	jffs2_rename,
59
	.get_acl =	jffs2_get_acl,
L
Linus Torvalds 已提交
60
	.setattr =	jffs2_setattr,
61 62 63 64
	.setxattr =	jffs2_setxattr,
	.getxattr =	jffs2_getxattr,
	.listxattr =	jffs2_listxattr,
	.removexattr =	jffs2_removexattr
L
Linus Torvalds 已提交
65 66 67 68 69 70
};

/***********************************************************************/


/* We keep the dirent list sorted in increasing order of name hash,
71
   and we use the same hash function as the dentries. Makes this
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81
   nice and simple
*/
static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
				   struct nameidata *nd)
{
	struct jffs2_inode_info *dir_f;
	struct jffs2_full_dirent *fd = NULL, *fd_list;
	uint32_t ino = 0;
	struct inode *inode = NULL;

82
	jffs2_dbg(1, "jffs2_lookup()\n");
L
Linus Torvalds 已提交
83

84 85 86
	if (target->d_name.len > JFFS2_MAX_NAME_LEN)
		return ERR_PTR(-ENAMETOOLONG);

L
Linus Torvalds 已提交
87 88
	dir_f = JFFS2_INODE_INFO(dir_i);

89
	mutex_lock(&dir_f->sem);
L
Linus Torvalds 已提交
90 91 92

	/* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
	for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
93
		if (fd_list->nhash == target->d_name.hash &&
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101
		    (!fd || fd_list->version > fd->version) &&
		    strlen(fd_list->name) == target->d_name.len &&
		    !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
			fd = fd_list;
		}
	}
	if (fd)
		ino = fd->ino;
102
	mutex_unlock(&dir_f->sem);
L
Linus Torvalds 已提交
103
	if (ino) {
104
		inode = jffs2_iget(dir_i->i_sb, ino);
105
		if (IS_ERR(inode))
106
			pr_warn("iget() failed for ino #%u\n", ino);
L
Linus Torvalds 已提交
107 108
	}

109
	return d_splice_alias(inode, target);
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117
}

/***********************************************************************/


static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
	struct jffs2_inode_info *f;
J
Josef Sipek 已提交
118
	struct inode *inode = filp->f_path.dentry->d_inode;
L
Linus Torvalds 已提交
119 120 121
	struct jffs2_full_dirent *fd;
	unsigned long offset, curofs;

122 123
	jffs2_dbg(1, "jffs2_readdir() for dir_i #%lu\n",
		  filp->f_path.dentry->d_inode->i_ino);
L
Linus Torvalds 已提交
124 125 126 127 128 129

	f = JFFS2_INODE_INFO(inode);

	offset = filp->f_pos;

	if (offset == 0) {
130
		jffs2_dbg(1, "Dirent 0: \".\", ino #%lu\n", inode->i_ino);
L
Linus Torvalds 已提交
131 132 133 134 135
		if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
			goto out;
		offset++;
	}
	if (offset == 1) {
J
Josef Sipek 已提交
136
		unsigned long pino = parent_ino(filp->f_path.dentry);
137
		jffs2_dbg(1, "Dirent 1: \"..\", ino #%lu\n", pino);
L
Linus Torvalds 已提交
138 139 140 141 142 143
		if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
			goto out;
		offset++;
	}

	curofs=1;
144
	mutex_lock(&f->sem);
L
Linus Torvalds 已提交
145 146 147 148 149
	for (fd = f->dents; fd; fd = fd->next) {

		curofs++;
		/* First loop: curofs = 2; offset = 2 */
		if (curofs < offset) {
150 151
			jffs2_dbg(2, "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
				  fd->name, fd->ino, fd->type, curofs, offset);
L
Linus Torvalds 已提交
152 153 154
			continue;
		}
		if (!fd->ino) {
155 156
			jffs2_dbg(2, "Skipping deletion dirent \"%s\"\n",
				  fd->name);
L
Linus Torvalds 已提交
157 158 159
			offset++;
			continue;
		}
160 161
		jffs2_dbg(2, "Dirent %ld: \"%s\", ino #%u, type %d\n",
			  offset, fd->name, fd->ino, fd->type);
L
Linus Torvalds 已提交
162 163 164 165
		if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
			break;
		offset++;
	}
166
	mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174
 out:
	filp->f_pos = offset;
	return 0;
}

/***********************************************************************/


A
Al Viro 已提交
175 176
static int jffs2_create(struct inode *dir_i, struct dentry *dentry,
			umode_t mode, struct nameidata *nd)
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186
{
	struct jffs2_raw_inode *ri;
	struct jffs2_inode_info *f, *dir_f;
	struct jffs2_sb_info *c;
	struct inode *inode;
	int ret;

	ri = jffs2_alloc_raw_inode();
	if (!ri)
		return -ENOMEM;
187

L
Linus Torvalds 已提交
188 189
	c = JFFS2_SB_INFO(dir_i->i_sb);

190
	jffs2_dbg(1, "%s()\n", __func__);
L
Linus Torvalds 已提交
191

192
	inode = jffs2_new_inode(dir_i, mode, ri);
L
Linus Torvalds 已提交
193 194

	if (IS_ERR(inode)) {
195
		jffs2_dbg(1, "jffs2_new_inode() failed\n");
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207
		jffs2_free_raw_inode(ri);
		return PTR_ERR(inode);
	}

	inode->i_op = &jffs2_file_inode_operations;
	inode->i_fop = &jffs2_file_operations;
	inode->i_mapping->a_ops = &jffs2_file_address_operations;
	inode->i_mapping->nrpages = 0;

	f = JFFS2_INODE_INFO(inode);
	dir_f = JFFS2_INODE_INFO(dir_i);

208 209 210 211 212 213 214
	/* jffs2_do_create() will want to lock it, _after_ reserving
	   space and taking c-alloc_sem. If we keep it locked here,
	   lockdep gets unhappy (although it's a false positive;
	   nothing else will be looking at this inode yet so there's
	   no chance of AB-BA deadlock involving its f->sem). */
	mutex_unlock(&f->sem);

215
	ret = jffs2_do_create(c, dir_f, f, ri, &dentry->d_name);
216 217
	if (ret)
		goto fail;
L
Linus Torvalds 已提交
218 219 220 221 222

	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));

	jffs2_free_raw_inode(ri);

223 224 225
	jffs2_dbg(1, "%s(): Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
		  __func__, inode->i_ino, inode->i_mode, inode->i_nlink,
		  f->inocache->pino_nlink, inode->i_mapping->nrpages);
226 227 228

	d_instantiate(dentry, inode);
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
229
	return 0;
230 231

 fail:
A
Al Viro 已提交
232
	iget_failed(inode);
233 234
	jffs2_free_raw_inode(ri);
	return ret;
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245
}

/***********************************************************************/


static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
{
	struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
	struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
	int ret;
246
	uint32_t now = get_seconds();
L
Linus Torvalds 已提交
247

248
	ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
249
			      dentry->d_name.len, dead_f, now);
L
Linus Torvalds 已提交
250
	if (dead_f->inocache)
M
Miklos Szeredi 已提交
251
		set_nlink(dentry->d_inode, dead_f->inocache->pino_nlink);
252 253
	if (!ret)
		dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264 265
	return ret;
}
/***********************************************************************/


static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
{
	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
	struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
	int ret;
	uint8_t type;
266
	uint32_t now;
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278

	/* Don't let people make hard links to bad inodes. */
	if (!f->inocache)
		return -EIO;

	if (S_ISDIR(old_dentry->d_inode->i_mode))
		return -EPERM;

	/* XXX: This is ugly */
	type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
	if (!type) type = DT_REG;

279 280
	now = get_seconds();
	ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
L
Linus Torvalds 已提交
281 282

	if (!ret) {
283
		mutex_lock(&f->sem);
M
Miklos Szeredi 已提交
284
		set_nlink(old_dentry->d_inode, ++f->inocache->pino_nlink);
285
		mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
286
		d_instantiate(dentry, old_dentry->d_inode);
287
		dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
A
Al Viro 已提交
288
		ihold(old_dentry->d_inode);
L
Linus Torvalds 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	}
	return ret;
}

/***********************************************************************/

static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
{
	struct jffs2_inode_info *f, *dir_f;
	struct jffs2_sb_info *c;
	struct inode *inode;
	struct jffs2_raw_inode *ri;
	struct jffs2_raw_dirent *rd;
	struct jffs2_full_dnode *fn;
	struct jffs2_full_dirent *fd;
	int namelen;
305
	uint32_t alloclen;
306
	int ret, targetlen = strlen(target);
L
Linus Torvalds 已提交
307 308 309

	/* FIXME: If you care. We'd need to use frags for the target
	   if it grows much more than this */
310
	if (targetlen > 254)
311
		return -ENAMETOOLONG;
L
Linus Torvalds 已提交
312 313 314 315 316

	ri = jffs2_alloc_raw_inode();

	if (!ri)
		return -ENOMEM;
317

L
Linus Torvalds 已提交
318
	c = JFFS2_SB_INFO(dir_i->i_sb);
319 320 321

	/* Try to reserve enough space for both node and dirent.
	 * Just the node will do for now, though
L
Linus Torvalds 已提交
322 323
	 */
	namelen = dentry->d_name.len;
324 325
	ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
				  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
L
Linus Torvalds 已提交
326 327 328 329 330 331

	if (ret) {
		jffs2_free_raw_inode(ri);
		return ret;
	}

332
	inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342 343

	if (IS_ERR(inode)) {
		jffs2_free_raw_inode(ri);
		jffs2_complete_reservation(c);
		return PTR_ERR(inode);
	}

	inode->i_op = &jffs2_symlink_inode_operations;

	f = JFFS2_INODE_INFO(inode);

344
	inode->i_size = targetlen;
L
Linus Torvalds 已提交
345 346 347 348 349
	ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
	ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));

	ri->compr = JFFS2_COMPR_NONE;
350
	ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
L
Linus Torvalds 已提交
351
	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
352

353
	fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
L
Linus Torvalds 已提交
354 355 356 357 358

	jffs2_free_raw_inode(ri);

	if (IS_ERR(fn)) {
		/* Eeek. Wave bye bye */
359
		mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
360
		jffs2_complete_reservation(c);
361 362
		ret = PTR_ERR(fn);
		goto fail;
L
Linus Torvalds 已提交
363
	}
364

365
	/* We use f->target field to store the target path. */
J
Julia Lawall 已提交
366
	f->target = kmemdup(target, targetlen + 1, GFP_KERNEL);
367
	if (!f->target) {
368
		pr_warn("Can't allocate %d bytes of memory\n", targetlen + 1);
369
		mutex_unlock(&f->sem);
370
		jffs2_complete_reservation(c);
371 372
		ret = -ENOMEM;
		goto fail;
373 374
	}

375 376
	jffs2_dbg(1, "%s(): symlink's target '%s' cached\n",
		  __func__, (char *)f->target);
377

378
	/* No data here. Only a metadata node, which will be
L
Linus Torvalds 已提交
379 380 381
	   obsoleted by the first data write
	*/
	f->metadata = fn;
382
	mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
383 384

	jffs2_complete_reservation(c);
385

386
	ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
387 388 389
	if (ret)
		goto fail;

390
	ret = jffs2_init_acl_post(inode);
391 392
	if (ret)
		goto fail;
393

394 395
	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
				  ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
396 397
	if (ret)
		goto fail;
L
Linus Torvalds 已提交
398 399 400 401 402

	rd = jffs2_alloc_raw_dirent();
	if (!rd) {
		/* Argh. Now we treat it like a normal delete */
		jffs2_complete_reservation(c);
403 404
		ret = -ENOMEM;
		goto fail;
L
Linus Torvalds 已提交
405 406 407
	}

	dir_f = JFFS2_INODE_INFO(dir_i);
408
	mutex_lock(&dir_f->sem);
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));

	rd->pino = cpu_to_je32(dir_i->i_ino);
	rd->version = cpu_to_je32(++dir_f->highest_version);
	rd->ino = cpu_to_je32(inode->i_ino);
	rd->mctime = cpu_to_je32(get_seconds());
	rd->nsize = namelen;
	rd->type = DT_LNK;
	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));

424
	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
L
Linus Torvalds 已提交
425 426

	if (IS_ERR(fd)) {
427
		/* dirent failed to write. Delete the inode normally
L
Linus Torvalds 已提交
428 429 430
		   as if it were the final unlink() */
		jffs2_complete_reservation(c);
		jffs2_free_raw_dirent(rd);
431
		mutex_unlock(&dir_f->sem);
432 433
		ret = PTR_ERR(fd);
		goto fail;
L
Linus Torvalds 已提交
434 435 436 437 438 439 440 441 442 443
	}

	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));

	jffs2_free_raw_dirent(rd);

	/* Link the fd into the inode's list, obsoleting an old
	   one if necessary. */
	jffs2_add_fd_to_list(c, fd, &dir_f->dents);

444
	mutex_unlock(&dir_f->sem);
L
Linus Torvalds 已提交
445 446 447
	jffs2_complete_reservation(c);

	d_instantiate(dentry, inode);
448
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
449
	return 0;
450 451

 fail:
A
Al Viro 已提交
452
	iget_failed(inode);
453
	return ret;
L
Linus Torvalds 已提交
454 455 456
}


457
static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466
{
	struct jffs2_inode_info *f, *dir_f;
	struct jffs2_sb_info *c;
	struct inode *inode;
	struct jffs2_raw_inode *ri;
	struct jffs2_raw_dirent *rd;
	struct jffs2_full_dnode *fn;
	struct jffs2_full_dirent *fd;
	int namelen;
467
	uint32_t alloclen;
L
Linus Torvalds 已提交
468 469 470 471 472 473 474
	int ret;

	mode |= S_IFDIR;

	ri = jffs2_alloc_raw_inode();
	if (!ri)
		return -ENOMEM;
475

L
Linus Torvalds 已提交
476 477
	c = JFFS2_SB_INFO(dir_i->i_sb);

478 479
	/* Try to reserve enough space for both node and dirent.
	 * Just the node will do for now, though
L
Linus Torvalds 已提交
480 481
	 */
	namelen = dentry->d_name.len;
482 483
	ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
				  JFFS2_SUMMARY_INODE_SIZE);
L
Linus Torvalds 已提交
484 485 486 487 488 489

	if (ret) {
		jffs2_free_raw_inode(ri);
		return ret;
	}

490
	inode = jffs2_new_inode(dir_i, mode, ri);
L
Linus Torvalds 已提交
491 492 493 494 495 496 497 498 499 500 501 502

	if (IS_ERR(inode)) {
		jffs2_free_raw_inode(ri);
		jffs2_complete_reservation(c);
		return PTR_ERR(inode);
	}

	inode->i_op = &jffs2_dir_inode_operations;
	inode->i_fop = &jffs2_dir_operations;

	f = JFFS2_INODE_INFO(inode);

503
	/* Directories get nlink 2 at start */
M
Miklos Szeredi 已提交
504
	set_nlink(inode, 2);
505 506 507
	/* but ic->pino_nlink is the parent ino# */
	f->inocache->pino_nlink = dir_i->i_ino;

L
Linus Torvalds 已提交
508 509
	ri->data_crc = cpu_to_je32(0);
	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
510

511
	fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
L
Linus Torvalds 已提交
512 513 514 515 516

	jffs2_free_raw_inode(ri);

	if (IS_ERR(fn)) {
		/* Eeek. Wave bye bye */
517
		mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
518
		jffs2_complete_reservation(c);
519 520
		ret = PTR_ERR(fn);
		goto fail;
L
Linus Torvalds 已提交
521
	}
522
	/* No data here. Only a metadata node, which will be
L
Linus Torvalds 已提交
523 524 525
	   obsoleted by the first data write
	*/
	f->metadata = fn;
526
	mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
527 528

	jffs2_complete_reservation(c);
529

530
	ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
531 532 533
	if (ret)
		goto fail;

534
	ret = jffs2_init_acl_post(inode);
535 536
	if (ret)
		goto fail;
537

538 539
	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
				  ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
540 541
	if (ret)
		goto fail;
542

L
Linus Torvalds 已提交
543 544 545 546
	rd = jffs2_alloc_raw_dirent();
	if (!rd) {
		/* Argh. Now we treat it like a normal delete */
		jffs2_complete_reservation(c);
547 548
		ret = -ENOMEM;
		goto fail;
L
Linus Torvalds 已提交
549 550 551
	}

	dir_f = JFFS2_INODE_INFO(dir_i);
552
	mutex_lock(&dir_f->sem);
L
Linus Torvalds 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));

	rd->pino = cpu_to_je32(dir_i->i_ino);
	rd->version = cpu_to_je32(++dir_f->highest_version);
	rd->ino = cpu_to_je32(inode->i_ino);
	rd->mctime = cpu_to_je32(get_seconds());
	rd->nsize = namelen;
	rd->type = DT_DIR;
	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));

568
	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
569

L
Linus Torvalds 已提交
570
	if (IS_ERR(fd)) {
571
		/* dirent failed to write. Delete the inode normally
L
Linus Torvalds 已提交
572 573 574
		   as if it were the final unlink() */
		jffs2_complete_reservation(c);
		jffs2_free_raw_dirent(rd);
575
		mutex_unlock(&dir_f->sem);
576 577
		ret = PTR_ERR(fd);
		goto fail;
L
Linus Torvalds 已提交
578 579 580
	}

	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
581
	inc_nlink(dir_i);
L
Linus Torvalds 已提交
582 583 584 585 586 587 588

	jffs2_free_raw_dirent(rd);

	/* Link the fd into the inode's list, obsoleting an old
	   one if necessary. */
	jffs2_add_fd_to_list(c, fd, &dir_f->dents);

589
	mutex_unlock(&dir_f->sem);
L
Linus Torvalds 已提交
590 591 592
	jffs2_complete_reservation(c);

	d_instantiate(dentry, inode);
593
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
594
	return 0;
595 596

 fail:
A
Al Viro 已提交
597
	iget_failed(inode);
598
	return ret;
L
Linus Torvalds 已提交
599 600 601 602
}

static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
{
603 604
	struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
L
Linus Torvalds 已提交
605 606 607
	struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
	struct jffs2_full_dirent *fd;
	int ret;
608
	uint32_t now = get_seconds();
L
Linus Torvalds 已提交
609 610 611 612 613

	for (fd = f->dents ; fd; fd = fd->next) {
		if (fd->ino)
			return -ENOTEMPTY;
	}
614 615 616 617 618 619

	ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
			      dentry->d_name.len, f, now);
	if (!ret) {
		dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
		clear_nlink(dentry->d_inode);
620
		drop_nlink(dir_i);
621
	}
L
Linus Torvalds 已提交
622 623 624
	return ret;
}

A
Al Viro 已提交
625
static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, umode_t mode, dev_t rdev)
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634
{
	struct jffs2_inode_info *f, *dir_f;
	struct jffs2_sb_info *c;
	struct inode *inode;
	struct jffs2_raw_inode *ri;
	struct jffs2_raw_dirent *rd;
	struct jffs2_full_dnode *fn;
	struct jffs2_full_dirent *fd;
	int namelen;
635
	union jffs2_device_node dev;
L
Linus Torvalds 已提交
636
	int devlen = 0;
637
	uint32_t alloclen;
L
Linus Torvalds 已提交
638 639
	int ret;

640
	if (!new_valid_dev(rdev))
L
Linus Torvalds 已提交
641 642 643 644 645
		return -EINVAL;

	ri = jffs2_alloc_raw_inode();
	if (!ri)
		return -ENOMEM;
646

L
Linus Torvalds 已提交
647
	c = JFFS2_SB_INFO(dir_i->i_sb);
648

649 650
	if (S_ISBLK(mode) || S_ISCHR(mode))
		devlen = jffs2_encode_dev(&dev, rdev);
651 652 653

	/* Try to reserve enough space for both node and dirent.
	 * Just the node will do for now, though
L
Linus Torvalds 已提交
654 655
	 */
	namelen = dentry->d_name.len;
656
	ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
657
				  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
L
Linus Torvalds 已提交
658 659 660 661 662 663

	if (ret) {
		jffs2_free_raw_inode(ri);
		return ret;
	}

664
	inode = jffs2_new_inode(dir_i, mode, ri);
L
Linus Torvalds 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

	if (IS_ERR(inode)) {
		jffs2_free_raw_inode(ri);
		jffs2_complete_reservation(c);
		return PTR_ERR(inode);
	}
	inode->i_op = &jffs2_file_inode_operations;
	init_special_inode(inode, inode->i_mode, rdev);

	f = JFFS2_INODE_INFO(inode);

	ri->dsize = ri->csize = cpu_to_je32(devlen);
	ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));

	ri->compr = JFFS2_COMPR_NONE;
	ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
683

684
	fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
L
Linus Torvalds 已提交
685 686 687 688 689

	jffs2_free_raw_inode(ri);

	if (IS_ERR(fn)) {
		/* Eeek. Wave bye bye */
690
		mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
691
		jffs2_complete_reservation(c);
692 693
		ret = PTR_ERR(fn);
		goto fail;
L
Linus Torvalds 已提交
694
	}
695
	/* No data here. Only a metadata node, which will be
L
Linus Torvalds 已提交
696 697 698
	   obsoleted by the first data write
	*/
	f->metadata = fn;
699
	mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
700 701

	jffs2_complete_reservation(c);
702

703
	ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
704 705 706
	if (ret)
		goto fail;

707
	ret = jffs2_init_acl_post(inode);
708 709
	if (ret)
		goto fail;
710

711 712
	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
				  ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
713 714
	if (ret)
		goto fail;
L
Linus Torvalds 已提交
715 716 717 718 719

	rd = jffs2_alloc_raw_dirent();
	if (!rd) {
		/* Argh. Now we treat it like a normal delete */
		jffs2_complete_reservation(c);
720 721
		ret = -ENOMEM;
		goto fail;
L
Linus Torvalds 已提交
722 723 724
	}

	dir_f = JFFS2_INODE_INFO(dir_i);
725
	mutex_lock(&dir_f->sem);
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));

	rd->pino = cpu_to_je32(dir_i->i_ino);
	rd->version = cpu_to_je32(++dir_f->highest_version);
	rd->ino = cpu_to_je32(inode->i_ino);
	rd->mctime = cpu_to_je32(get_seconds());
	rd->nsize = namelen;

	/* XXX: This is ugly. */
	rd->type = (mode & S_IFMT) >> 12;

	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));

744
	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
745

L
Linus Torvalds 已提交
746
	if (IS_ERR(fd)) {
747
		/* dirent failed to write. Delete the inode normally
L
Linus Torvalds 已提交
748 749 750
		   as if it were the final unlink() */
		jffs2_complete_reservation(c);
		jffs2_free_raw_dirent(rd);
751
		mutex_unlock(&dir_f->sem);
752 753
		ret = PTR_ERR(fd);
		goto fail;
L
Linus Torvalds 已提交
754 755 756 757 758 759 760 761 762 763
	}

	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));

	jffs2_free_raw_dirent(rd);

	/* Link the fd into the inode's list, obsoleting an old
	   one if necessary. */
	jffs2_add_fd_to_list(c, fd, &dir_f->dents);

764
	mutex_unlock(&dir_f->sem);
L
Linus Torvalds 已提交
765 766 767
	jffs2_complete_reservation(c);

	d_instantiate(dentry, inode);
768
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
769
	return 0;
770 771

 fail:
A
Al Viro 已提交
772
	iget_failed(inode);
773
	return ret;
L
Linus Torvalds 已提交
774 775 776
}

static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
D
David Woodhouse 已提交
777
			 struct inode *new_dir_i, struct dentry *new_dentry)
L
Linus Torvalds 已提交
778 779 780 781 782
{
	int ret;
	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
	struct jffs2_inode_info *victim_f = NULL;
	uint8_t type;
783
	uint32_t now;
L
Linus Torvalds 已提交
784

785
	/* The VFS will check for us and prevent trying to rename a
L
Linus Torvalds 已提交
786 787 788 789 790 791 792 793 794
	 * file over a directory and vice versa, but if it's a directory,
	 * the VFS can't check whether the victim is empty. The filesystem
	 * needs to do that for itself.
	 */
	if (new_dentry->d_inode) {
		victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
		if (S_ISDIR(new_dentry->d_inode->i_mode)) {
			struct jffs2_full_dirent *fd;

795
			mutex_lock(&victim_f->sem);
L
Linus Torvalds 已提交
796 797
			for (fd = victim_f->dents; fd; fd = fd->next) {
				if (fd->ino) {
798
					mutex_unlock(&victim_f->sem);
L
Linus Torvalds 已提交
799 800 801
					return -ENOTEMPTY;
				}
			}
802
			mutex_unlock(&victim_f->sem);
L
Linus Torvalds 已提交
803 804 805 806
		}
	}

	/* XXX: We probably ought to alloc enough space for
807
	   both nodes at the same time. Writing the new link,
L
Linus Torvalds 已提交
808 809 810 811
	   then getting -ENOSPC, is quite bad :)
	*/

	/* Make a hard link */
812

L
Linus Torvalds 已提交
813 814 815 816
	/* XXX: This is ugly */
	type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
	if (!type) type = DT_REG;

817
	now = get_seconds();
818
	ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
L
Linus Torvalds 已提交
819
			    old_dentry->d_inode->i_ino, type,
820
			    new_dentry->d_name.name, new_dentry->d_name.len, now);
L
Linus Torvalds 已提交
821 822 823 824 825 826

	if (ret)
		return ret;

	if (victim_f) {
		/* There was a victim. Kill it off nicely */
827 828 829 830
		if (S_ISDIR(new_dentry->d_inode->i_mode))
			clear_nlink(new_dentry->d_inode);
		else
			drop_nlink(new_dentry->d_inode);
L
Linus Torvalds 已提交
831 832 833
		/* Don't oops if the victim was a dirent pointing to an
		   inode which didn't exist. */
		if (victim_f->inocache) {
834
			mutex_lock(&victim_f->sem);
835 836 837 838
			if (S_ISDIR(new_dentry->d_inode->i_mode))
				victim_f->inocache->pino_nlink = 0;
			else
				victim_f->inocache->pino_nlink--;
839
			mutex_unlock(&victim_f->sem);
L
Linus Torvalds 已提交
840 841 842
		}
	}

843
	/* If it was a directory we moved, and there was no victim,
L
Linus Torvalds 已提交
844 845
	   increase i_nlink on its new parent */
	if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
846
		inc_nlink(new_dir_i);
L
Linus Torvalds 已提交
847 848

	/* Unlink the original */
849
	ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
850
			      old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
L
Linus Torvalds 已提交
851 852 853 854 855 856

	/* We don't touch inode->i_nlink */

	if (ret) {
		/* Oh shit. We really ought to make a single node which can do both atomically */
		struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
857
		mutex_lock(&f->sem);
858
		inc_nlink(old_dentry->d_inode);
859 860
		if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode))
			f->inocache->pino_nlink++;
861
		mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
862

863 864
		pr_notice("%s(): Link succeeded, unlink failed (err %d). You now have a hard link\n",
			  __func__, ret);
L
Linus Torvalds 已提交
865 866
		/* Might as well let the VFS know */
		d_instantiate(new_dentry, old_dentry->d_inode);
A
Al Viro 已提交
867
		ihold(old_dentry->d_inode);
868
		new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
L
Linus Torvalds 已提交
869 870 871 872
		return ret;
	}

	if (S_ISDIR(old_dentry->d_inode->i_mode))
873
		drop_nlink(old_dir_i);
L
Linus Torvalds 已提交
874

875 876
	new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);

L
Linus Torvalds 已提交
877 878 879
	return 0;
}