fs.c 19.9 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
 *
 * Created by David Woodhouse <dwmw2@infradead.org>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
 */

13 14
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

15
#include <linux/capability.h>
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/mtd/mtd.h>
#include <linux/pagemap.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/vfs.h>
#include <linux/crc32.h>
#include "nodelist.h"

static int jffs2_flash_setup(struct jffs2_sb_info *c);

30
int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
L
Linus Torvalds 已提交
31 32 33 34 35
{
	struct jffs2_full_dnode *old_metadata, *new_metadata;
	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
	struct jffs2_raw_inode *ri;
36
	union jffs2_device_node dev;
L
Linus Torvalds 已提交
37 38 39
	unsigned char *mdata = NULL;
	int mdatalen = 0;
	unsigned int ivalid;
40
	uint32_t alloclen;
L
Linus Torvalds 已提交
41
	int ret;
42
	int alloc_type = ALLOC_NORMAL;
43

44
	jffs2_dbg(1, "%s(): ino #%lu\n", __func__, inode->i_ino);
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52

	/* Special cases - we don't want more than one data node
	   for these types on the medium at any time. So setattr
	   must read the original data associated with the node
	   (i.e. the device numbers or the target name) and write
	   it out again with the appropriate data attached */
	if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
		/* For these, we don't actually need to read the old node */
53
		mdatalen = jffs2_encode_dev(&dev, inode->i_rdev);
L
Linus Torvalds 已提交
54
		mdata = (char *)&dev;
55 56
		jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
			  __func__, mdatalen);
L
Linus Torvalds 已提交
57
	} else if (S_ISLNK(inode->i_mode)) {
58
		mutex_lock(&f->sem);
L
Linus Torvalds 已提交
59 60
		mdatalen = f->metadata->size;
		mdata = kmalloc(f->metadata->size, GFP_USER);
61
		if (!mdata) {
62
			mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
63
			return -ENOMEM;
64
		}
L
Linus Torvalds 已提交
65 66
		ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
		if (ret) {
67
			mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
68 69 70
			kfree(mdata);
			return ret;
		}
71
		mutex_unlock(&f->sem);
72 73
		jffs2_dbg(1, "%s(): Writing %d bytes of symlink target\n",
			  __func__, mdatalen);
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81
	}

	ri = jffs2_alloc_raw_inode();
	if (!ri) {
		if (S_ISLNK(inode->i_mode))
			kfree(mdata);
		return -ENOMEM;
	}
82

83 84
	ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &alloclen,
				  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
L
Linus Torvalds 已提交
85 86
	if (ret) {
		jffs2_free_raw_inode(ri);
87
		if (S_ISLNK(inode->i_mode))
L
Linus Torvalds 已提交
88 89 90
			 kfree(mdata);
		return ret;
	}
91
	mutex_lock(&f->sem);
L
Linus Torvalds 已提交
92
	ivalid = iattr->ia_valid;
93

L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101
	ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
	ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
	ri->totlen = cpu_to_je32(sizeof(*ri) + mdatalen);
	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));

	ri->ino = cpu_to_je32(inode->i_ino);
	ri->version = cpu_to_je32(++f->highest_version);

102 103 104 105
	ri->uid = cpu_to_je16((ivalid & ATTR_UID)?
		from_kuid(&init_user_ns, iattr->ia_uid):i_uid_read(inode));
	ri->gid = cpu_to_je16((ivalid & ATTR_GID)?
		from_kgid(&init_user_ns, iattr->ia_gid):i_gid_read(inode));
L
Linus Torvalds 已提交
106 107

	if (ivalid & ATTR_MODE)
108
		ri->mode = cpu_to_jemode(iattr->ia_mode);
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	else
		ri->mode = cpu_to_jemode(inode->i_mode);


	ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
	ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
	ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
	ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));

	ri->offset = cpu_to_je32(0);
	ri->csize = ri->dsize = cpu_to_je32(mdatalen);
	ri->compr = JFFS2_COMPR_NONE;
	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
		/* It's an extension. Make it a hole node */
		ri->compr = JFFS2_COMPR_ZERO;
		ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
		ri->offset = cpu_to_je32(inode->i_size);
126 127 128 129
	} else if (ivalid & ATTR_SIZE && !iattr->ia_size) {
		/* For truncate-to-zero, treat it as deletion because
		   it'll always be obsoleting all previous nodes */
		alloc_type = ALLOC_DELETION;
L
Linus Torvalds 已提交
130 131 132 133 134 135 136
	}
	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
	if (mdatalen)
		ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
	else
		ri->data_crc = cpu_to_je32(0);

137
	new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, alloc_type);
L
Linus Torvalds 已提交
138 139
	if (S_ISLNK(inode->i_mode))
		kfree(mdata);
140

L
Linus Torvalds 已提交
141 142 143
	if (IS_ERR(new_metadata)) {
		jffs2_complete_reservation(c);
		jffs2_free_raw_inode(ri);
144
		mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151
		return PTR_ERR(new_metadata);
	}
	/* It worked. Update the inode */
	inode->i_atime = ITIME(je32_to_cpu(ri->atime));
	inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
	inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
	inode->i_mode = jemode_to_cpu(ri->mode);
152 153
	i_uid_write(inode, je16_to_cpu(ri->uid));
	i_gid_write(inode, je16_to_cpu(ri->gid));
L
Linus Torvalds 已提交
154 155 156 157 158


	old_metadata = f->metadata;

	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
A
Artem B. Bityutskiy 已提交
159
		jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
L
Linus Torvalds 已提交
160 161 162 163

	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
		jffs2_add_full_dnode_to_inode(c, f, new_metadata);
		inode->i_size = iattr->ia_size;
164
		inode->i_blocks = (inode->i_size + 511) >> 9;
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174
		f->metadata = NULL;
	} else {
		f->metadata = new_metadata;
	}
	if (old_metadata) {
		jffs2_mark_node_obsolete(c, old_metadata->raw);
		jffs2_free_full_dnode(old_metadata);
	}
	jffs2_free_raw_inode(ri);

175
	mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
176 177
	jffs2_complete_reservation(c);

178
	/* We have to do the truncate_setsize() without f->sem held, since
179
	   some pages may be locked and waiting for it in readpage().
L
Linus Torvalds 已提交
180 181 182
	   We are protected from a simultaneous write() extending i_size
	   back past iattr->ia_size, because do_truncate() holds the
	   generic inode semaphore. */
183
	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size) {
184
		truncate_setsize(inode, iattr->ia_size);
185 186
		inode->i_blocks = (inode->i_size + 511) >> 9;
	}	
L
Linus Torvalds 已提交
187 188 189 190 191 192

	return 0;
}

int jffs2_setattr(struct dentry *dentry, struct iattr *iattr)
{
193
	struct inode *inode = d_inode(dentry);
194 195
	int rc;

196
	rc = inode_change_ok(inode, iattr);
197 198 199
	if (rc)
		return rc;

200
	rc = jffs2_do_setattr(inode, iattr);
201
	if (!rc && (iattr->ia_valid & ATTR_MODE))
202
		rc = posix_acl_chmod(inode, inode->i_mode);
203

204
	return rc;
L
Linus Torvalds 已提交
205 206
}

207
int jffs2_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
208
{
209
	struct jffs2_sb_info *c = JFFS2_SB_INFO(dentry->d_sb);
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217
	unsigned long avail;

	buf->f_type = JFFS2_SUPER_MAGIC;
	buf->f_bsize = 1 << PAGE_SHIFT;
	buf->f_blocks = c->flash_size >> PAGE_SHIFT;
	buf->f_files = 0;
	buf->f_ffree = 0;
	buf->f_namelen = JFFS2_MAX_NAME_LEN;
218 219
	buf->f_fsid.val[0] = JFFS2_SUPER_MAGIC;
	buf->f_fsid.val[1] = c->mtd->index;
L
Linus Torvalds 已提交
220 221 222 223 224 225 226

	spin_lock(&c->erase_completion_lock);
	avail = c->dirty_size + c->free_size;
	if (avail > c->sector_size * c->resv_blocks_write)
		avail -= c->sector_size * c->resv_blocks_write;
	else
		avail = 0;
227
	spin_unlock(&c->erase_completion_lock);
L
Linus Torvalds 已提交
228 229 230 231 232 233 234

	buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;

	return 0;
}


235
void jffs2_evict_inode (struct inode *inode)
L
Linus Torvalds 已提交
236
{
237
	/* We can forget about this inode for now - drop all
L
Linus Torvalds 已提交
238 239 240 241
	 *  the nodelists associated with it, etc.
	 */
	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
242

243 244
	jffs2_dbg(1, "%s(): ino #%lu mode %o\n",
		  __func__, inode->i_ino, inode->i_mode);
245
	truncate_inode_pages_final(&inode->i_data);
246
	clear_inode(inode);
L
Linus Torvalds 已提交
247 248 249
	jffs2_do_clear_inode(c, f);
}

250
struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
L
Linus Torvalds 已提交
251 252 253 254
{
	struct jffs2_inode_info *f;
	struct jffs2_sb_info *c;
	struct jffs2_raw_inode latest_node;
255
	union jffs2_device_node jdev;
256
	struct inode *inode;
257
	dev_t rdev = 0;
L
Linus Torvalds 已提交
258 259
	int ret;

260
	jffs2_dbg(1, "%s(): ino == %lu\n", __func__, ino);
261 262 263 264 265 266

	inode = iget_locked(sb, ino);
	if (!inode)
		return ERR_PTR(-ENOMEM);
	if (!(inode->i_state & I_NEW))
		return inode;
L
Linus Torvalds 已提交
267 268 269 270 271

	f = JFFS2_INODE_INFO(inode);
	c = JFFS2_SB_INFO(inode->i_sb);

	jffs2_init_inode_info(f);
272
	mutex_lock(&f->sem);
273

L
Linus Torvalds 已提交
274
	ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
B
Brian Norris 已提交
275 276
	if (ret)
		goto error;
L
Linus Torvalds 已提交
277 278

	inode->i_mode = jemode_to_cpu(latest_node.mode);
279 280
	i_uid_write(inode, je16_to_cpu(latest_node.uid));
	i_gid_write(inode, je16_to_cpu(latest_node.gid));
L
Linus Torvalds 已提交
281 282 283 284 285
	inode->i_size = je32_to_cpu(latest_node.isize);
	inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
	inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
	inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));

M
Miklos Szeredi 已提交
286
	set_nlink(inode, f->inocache->pino_nlink);
L
Linus Torvalds 已提交
287 288

	inode->i_blocks = (inode->i_size + 511) >> 9;
289

L
Linus Torvalds 已提交
290 291 292 293
	switch (inode->i_mode & S_IFMT) {

	case S_IFLNK:
		inode->i_op = &jffs2_symlink_inode_operations;
A
Al Viro 已提交
294
		inode->i_link = f->target;
L
Linus Torvalds 已提交
295
		break;
296

L
Linus Torvalds 已提交
297 298 299
	case S_IFDIR:
	{
		struct jffs2_full_dirent *fd;
M
Miklos Szeredi 已提交
300
		set_nlink(inode, 2); /* parent and '.' */
L
Linus Torvalds 已提交
301 302 303

		for (fd=f->dents; fd; fd = fd->next) {
			if (fd->type == DT_DIR && fd->ino)
304
				inc_nlink(inode);
L
Linus Torvalds 已提交
305 306 307
		}
		/* Root dir gets i_nlink 3 for some reason */
		if (inode->i_ino == 1)
308
			inc_nlink(inode);
L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

		inode->i_op = &jffs2_dir_inode_operations;
		inode->i_fop = &jffs2_dir_operations;
		break;
	}
	case S_IFREG:
		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;
		break;

	case S_IFBLK:
	case S_IFCHR:
		/* Read the device numbers from the media */
324 325
		if (f->metadata->size != sizeof(jdev.old_id) &&
		    f->metadata->size != sizeof(jdev.new_id)) {
326 327
			pr_notice("Device node has strange size %d\n",
				  f->metadata->size);
328
			goto error_io;
329
		}
330
		jffs2_dbg(1, "Reading device numbers from flash\n");
331 332
		ret = jffs2_read_dnode(c, f, f->metadata, (char *)&jdev, 0, f->metadata->size);
		if (ret < 0) {
L
Linus Torvalds 已提交
333
			/* Eep */
334 335
			pr_notice("Read device numbers for inode %lu failed\n",
				  (unsigned long)inode->i_ino);
336
			goto error;
337
		}
338 339
		if (f->metadata->size == sizeof(jdev.old_id))
			rdev = old_decode_dev(je16_to_cpu(jdev.old_id));
340
		else
341
			rdev = new_decode_dev(je32_to_cpu(jdev.new_id));
L
Linus Torvalds 已提交
342 343 344 345

	case S_IFSOCK:
	case S_IFIFO:
		inode->i_op = &jffs2_file_inode_operations;
346
		init_special_inode(inode, inode->i_mode, rdev);
L
Linus Torvalds 已提交
347 348 349
		break;

	default:
350 351
		pr_warn("%s(): Bogus i_mode %o for ino %lu\n",
			__func__, inode->i_mode, (unsigned long)inode->i_ino);
L
Linus Torvalds 已提交
352 353
	}

354
	mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
355

356
	jffs2_dbg(1, "jffs2_read_inode() returning\n");
357 358 359 360 361 362
	unlock_new_inode(inode);
	return inode;

error_io:
	ret = -EIO;
error:
363
	mutex_unlock(&f->sem);
364 365 366
	jffs2_do_clear_inode(c, f);
	iget_failed(inode);
	return ERR_PTR(ret);
L
Linus Torvalds 已提交
367 368
}

369
void jffs2_dirty_inode(struct inode *inode, int flags)
L
Linus Torvalds 已提交
370 371 372 373
{
	struct iattr iattr;

	if (!(inode->i_state & I_DIRTY_DATASYNC)) {
374 375
		jffs2_dbg(2, "%s(): not calling setattr() for ino #%lu\n",
			  __func__, inode->i_ino);
L
Linus Torvalds 已提交
376 377 378
		return;
	}

379 380
	jffs2_dbg(1, "%s(): calling setattr() for ino #%lu\n",
		  __func__, inode->i_ino);
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390 391 392

	iattr.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_MTIME|ATTR_CTIME;
	iattr.ia_mode = inode->i_mode;
	iattr.ia_uid = inode->i_uid;
	iattr.ia_gid = inode->i_gid;
	iattr.ia_atime = inode->i_atime;
	iattr.ia_mtime = inode->i_mtime;
	iattr.ia_ctime = inode->i_ctime;

	jffs2_do_setattr(inode, &iattr);
}

393
int jffs2_do_remount_fs(struct super_block *sb, int *flags, char *data)
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405
{
	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);

	if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY))
		return -EROFS;

	/* We stop if it was running, then restart if it needs to.
	   This also catches the case where it was stopped and this
	   is just a remount to restart it.
	   Flush the writebuffer, if neccecary, else we loose it */
	if (!(sb->s_flags & MS_RDONLY)) {
		jffs2_stop_garbage_collect_thread(c);
406
		mutex_lock(&c->alloc_sem);
L
Linus Torvalds 已提交
407
		jffs2_flush_wbuf_pad(c);
408
		mutex_unlock(&c->alloc_sem);
409
	}
L
Linus Torvalds 已提交
410 411 412

	if (!(*flags & MS_RDONLY))
		jffs2_start_garbage_collect_thread(c);
413

L
Linus Torvalds 已提交
414 415 416 417 418 419
	*flags |= MS_NOATIME;
	return 0;
}

/* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
   fill in the raw_inode while you're at it. */
A
Al Viro 已提交
420
struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode, struct jffs2_raw_inode *ri)
L
Linus Torvalds 已提交
421 422 423 424 425 426 427
{
	struct inode *inode;
	struct super_block *sb = dir_i->i_sb;
	struct jffs2_sb_info *c;
	struct jffs2_inode_info *f;
	int ret;

428 429
	jffs2_dbg(1, "%s(): dir_i %ld, mode 0x%x\n",
		  __func__, dir_i->i_ino, mode);
L
Linus Torvalds 已提交
430 431

	c = JFFS2_SB_INFO(sb);
432

L
Linus Torvalds 已提交
433
	inode = new_inode(sb);
434

L
Linus Torvalds 已提交
435 436 437 438 439
	if (!inode)
		return ERR_PTR(-ENOMEM);

	f = JFFS2_INODE_INFO(inode);
	jffs2_init_inode_info(f);
440
	mutex_lock(&f->sem);
L
Linus Torvalds 已提交
441 442 443

	memset(ri, 0, sizeof(*ri));
	/* Set OS-specific defaults for new inodes */
444
	ri->uid = cpu_to_je16(from_kuid(&init_user_ns, current_fsuid()));
L
Linus Torvalds 已提交
445 446

	if (dir_i->i_mode & S_ISGID) {
447
		ri->gid = cpu_to_je16(i_gid_read(dir_i));
L
Linus Torvalds 已提交
448 449 450
		if (S_ISDIR(mode))
			mode |= S_ISGID;
	} else {
451
		ri->gid = cpu_to_je16(from_kgid(&init_user_ns, current_fsgid()));
L
Linus Torvalds 已提交
452
	}
453 454 455

	/* POSIX ACLs have to be processed now, at least partly.
	   The umask is only applied if there's no default ACL */
456 457
	ret = jffs2_init_acl_pre(dir_i, inode, &mode);
	if (ret) {
458 459 460 461
		mutex_unlock(&f->sem);
		make_bad_inode(inode);
		iput(inode);
		return ERR_PTR(ret);
462
	}
L
Linus Torvalds 已提交
463 464
	ret = jffs2_do_new_inode (c, f, mode, ri);
	if (ret) {
465
		mutex_unlock(&f->sem);
L
Linus Torvalds 已提交
466 467 468 469
		make_bad_inode(inode);
		iput(inode);
		return ERR_PTR(ret);
	}
M
Miklos Szeredi 已提交
470
	set_nlink(inode, 1);
L
Linus Torvalds 已提交
471 472
	inode->i_ino = je32_to_cpu(ri->ino);
	inode->i_mode = jemode_to_cpu(ri->mode);
473 474
	i_gid_write(inode, je16_to_cpu(ri->gid));
	i_uid_write(inode, je16_to_cpu(ri->uid));
L
Linus Torvalds 已提交
475 476 477 478 479 480
	inode->i_atime = inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
	ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));

	inode->i_blocks = 0;
	inode->i_size = 0;

481
	if (insert_inode_locked(inode) < 0) {
482
		mutex_unlock(&f->sem);
483 484 485 486
		make_bad_inode(inode);
		iput(inode);
		return ERR_PTR(-EINVAL);
	}
L
Linus Torvalds 已提交
487 488 489 490

	return inode;
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
static int calculate_inocache_hashsize(uint32_t flash_size)
{
	/*
	 * Pick a inocache hash size based on the size of the medium.
	 * Count how many megabytes we're dealing with, apply a hashsize twice
	 * that size, but rounding down to the usual big powers of 2. And keep
	 * to sensible bounds.
	 */

	int size_mb = flash_size / 1024 / 1024;
	int hashsize = (size_mb * 2) & ~0x3f;

	if (hashsize < INOCACHE_HASHSIZE_MIN)
		return INOCACHE_HASHSIZE_MIN;
	if (hashsize > INOCACHE_HASHSIZE_MAX)
		return INOCACHE_HASHSIZE_MAX;

	return hashsize;
}
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519

int jffs2_do_fill_super(struct super_block *sb, void *data, int silent)
{
	struct jffs2_sb_info *c;
	struct inode *root_i;
	int ret;
	size_t blocks;

	c = JFFS2_SB_INFO(sb);

H
Huang Shijie 已提交
520 521 522 523
	/* Do not support the MLC nand */
	if (c->mtd->type == MTD_MLCNANDFLASH)
		return -EINVAL;

524
#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
L
Linus Torvalds 已提交
525
	if (c->mtd->type == MTD_NANDFLASH) {
526
		pr_err("Cannot operate on NAND flash unless jffs2 NAND support is compiled in\n");
L
Linus Torvalds 已提交
527 528
		return -EINVAL;
	}
529
	if (c->mtd->type == MTD_DATAFLASH) {
530
		pr_err("Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in\n");
531 532 533
		return -EINVAL;
	}
#endif
L
Linus Torvalds 已提交
534 535

	c->flash_size = c->mtd->size;
536
	c->sector_size = c->mtd->erasesize;
L
Linus Torvalds 已提交
537 538 539 540 541 542
	blocks = c->flash_size / c->sector_size;

	/*
	 * Size alignment check
	 */
	if ((c->sector_size * blocks) != c->flash_size) {
543
		c->flash_size = c->sector_size * blocks;
544
		pr_info("Flash size not aligned to erasesize, reducing to %dKiB\n",
L
Linus Torvalds 已提交
545 546 547 548
			c->flash_size / 1024);
	}

	if (c->flash_size < 5*c->sector_size) {
549
		pr_err("Too few erase blocks (%d)\n",
550
		       c->flash_size / c->sector_size);
L
Linus Torvalds 已提交
551 552 553 554 555 556 557 558 559 560
		return -EINVAL;
	}

	c->cleanmarker_size = sizeof(struct jffs2_unknown_node);

	/* NAND (or other bizarre) flash... do setup accordingly */
	ret = jffs2_flash_setup(c);
	if (ret)
		return ret;

561 562
	c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
	c->inocache_list = kcalloc(c->inocache_hashsize, sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
L
Linus Torvalds 已提交
563 564 565 566 567
	if (!c->inocache_list) {
		ret = -ENOMEM;
		goto out_wbuf;
	}

568 569
	jffs2_init_xattr_subsystem(c);

L
Linus Torvalds 已提交
570 571 572
	if ((ret = jffs2_do_mount_fs(c)))
		goto out_inohash;

573
	jffs2_dbg(1, "%s(): Getting root inode\n", __func__);
574 575
	root_i = jffs2_iget(sb, 1);
	if (IS_ERR(root_i)) {
576
		jffs2_dbg(1, "get root inode failed\n");
577 578
		ret = PTR_ERR(root_i);
		goto out_root;
L
Linus Torvalds 已提交
579 580
	}

581 582
	ret = -ENOMEM;

583
	jffs2_dbg(1, "%s(): d_make_root()\n", __func__);
584
	sb->s_root = d_make_root(root_i);
L
Linus Torvalds 已提交
585
	if (!sb->s_root)
586
		goto out_root;
L
Linus Torvalds 已提交
587 588 589 590 591 592 593 594 595

	sb->s_maxbytes = 0xFFFFFFFF;
	sb->s_blocksize = PAGE_CACHE_SIZE;
	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
	sb->s_magic = JFFS2_SUPER_MAGIC;
	if (!(sb->s_flags & MS_RDONLY))
		jffs2_start_garbage_collect_thread(c);
	return 0;

596
out_root:
L
Linus Torvalds 已提交
597 598
	jffs2_free_ino_caches(c);
	jffs2_free_raw_node_refs(c);
599
	if (jffs2_blocks_use_vmalloc(c))
L
Linus Torvalds 已提交
600 601 602 603
		vfree(c->blocks);
	else
		kfree(c->blocks);
 out_inohash:
604
	jffs2_clear_xattr_subsystem(c);
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618
	kfree(c->inocache_list);
 out_wbuf:
	jffs2_flash_cleanup(c);

	return ret;
}

void jffs2_gc_release_inode(struct jffs2_sb_info *c,
				   struct jffs2_inode_info *f)
{
	iput(OFNI_EDONI_2SFFJ(f));
}

struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
619
					      int inum, int unlinked)
L
Linus Torvalds 已提交
620 621 622
{
	struct inode *inode;
	struct jffs2_inode_cache *ic;
623 624

	if (unlinked) {
L
Linus Torvalds 已提交
625
		/* The inode has zero nlink but its nodes weren't yet marked
626
		   obsolete. This has to be because we're still waiting for
L
Linus Torvalds 已提交
627 628
		   the final (close() and) iput() to happen.

629
		   There's a possibility that the final iput() could have
L
Linus Torvalds 已提交
630 631 632 633 634
		   happened while we were contemplating. In order to ensure
		   that we don't cause a new read_inode() (which would fail)
		   for the inode in question, we use ilookup() in this case
		   instead of iget().

635
		   The nlink can't _become_ zero at this point because we're
L
Linus Torvalds 已提交
636 637 638 639 640
		   holding the alloc_sem, and jffs2_do_unlink() would also
		   need that while decrementing nlink on any inode.
		*/
		inode = ilookup(OFNI_BS_2SFFJ(c), inum);
		if (!inode) {
641 642
			jffs2_dbg(1, "ilookup() failed for ino #%u; inode is probably deleted.\n",
				  inum);
L
Linus Torvalds 已提交
643 644 645 646

			spin_lock(&c->inocache_lock);
			ic = jffs2_get_ino_cache(c, inum);
			if (!ic) {
647 648
				jffs2_dbg(1, "Inode cache for ino #%u is gone\n",
					  inum);
L
Linus Torvalds 已提交
649 650 651 652 653
				spin_unlock(&c->inocache_lock);
				return NULL;
			}
			if (ic->state != INO_STATE_CHECKEDABSENT) {
				/* Wait for progress. Don't just loop */
654 655
				jffs2_dbg(1, "Waiting for ino #%u in state %d\n",
					  ic->ino, ic->state);
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667
				sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
			} else {
				spin_unlock(&c->inocache_lock);
			}

			return NULL;
		}
	} else {
		/* Inode has links to it still; they're not going away because
		   jffs2_do_unlink() would need the alloc_sem and we have it.
		   Just iget() it, and if read_inode() is necessary that's OK.
		*/
668 669 670
		inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
		if (IS_ERR(inode))
			return ERR_CAST(inode);
L
Linus Torvalds 已提交
671 672
	}
	if (is_bad_inode(inode)) {
673 674
		pr_notice("Eep. read_inode() failed for ino #%u. unlinked %d\n",
			  inum, unlinked);
L
Linus Torvalds 已提交
675 676 677 678 679 680 681 682
		/* NB. This will happen again. We need to do something appropriate here. */
		iput(inode);
		return ERR_PTR(-EIO);
	}

	return JFFS2_INODE_INFO(inode);
}

683 684
unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c,
				   struct jffs2_inode_info *f,
L
Linus Torvalds 已提交
685 686 687 688 689 690
				   unsigned long offset,
				   unsigned long *priv)
{
	struct inode *inode = OFNI_EDONI_2SFFJ(f);
	struct page *pg;

S
Sasha Levin 已提交
691
	pg = read_cache_page(inode->i_mapping, offset >> PAGE_CACHE_SHIFT,
L
Linus Torvalds 已提交
692 693 694
			     (void *)jffs2_do_readpage_unlock, inode);
	if (IS_ERR(pg))
		return (void *)pg;
695

L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	*priv = (unsigned long)pg;
	return kmap(pg);
}

void jffs2_gc_release_page(struct jffs2_sb_info *c,
			   unsigned char *ptr,
			   unsigned long *priv)
{
	struct page *pg = (void *)*priv;

	kunmap(pg);
	page_cache_release(pg);
}

static int jffs2_flash_setup(struct jffs2_sb_info *c) {
	int ret = 0;
712

L
Linus Torvalds 已提交
713 714 715 716 717 718 719
	if (jffs2_cleanmarker_oob(c)) {
		/* NAND flash... do setup accordingly */
		ret = jffs2_nand_flash_setup(c);
		if (ret)
			return ret;
	}

720 721 722 723 724 725
	/* and Dataflash */
	if (jffs2_dataflash(c)) {
		ret = jffs2_dataflash_setup(c);
		if (ret)
			return ret;
	}
726 727 728 729 730 731 732 733

	/* and Intel "Sibley" flash */
	if (jffs2_nor_wbuf_flash(c)) {
		ret = jffs2_nor_wbuf_flash_setup(c);
		if (ret)
			return ret;
	}

A
Artem Bityutskiy 已提交
734 735 736 737 738 739 740
	/* and an UBI volume */
	if (jffs2_ubivol(c)) {
		ret = jffs2_ubivol_setup(c);
		if (ret)
			return ret;
	}

L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749
	return ret;
}

void jffs2_flash_cleanup(struct jffs2_sb_info *c) {

	if (jffs2_cleanmarker_oob(c)) {
		jffs2_nand_flash_cleanup(c);
	}

750 751 752 753
	/* and DataFlash */
	if (jffs2_dataflash(c)) {
		jffs2_dataflash_cleanup(c);
	}
754 755 756 757 758

	/* and Intel "Sibley" flash */
	if (jffs2_nor_wbuf_flash(c)) {
		jffs2_nor_wbuf_flash_cleanup(c);
	}
A
Artem Bityutskiy 已提交
759 760 761 762 763

	/* and an UBI volume */
	if (jffs2_ubivol(c)) {
		jffs2_ubivol_cleanup(c);
	}
L
Linus Torvalds 已提交
764
}