inode.c 34.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * inode.c
 *
 * vfs' aops, fops, dops and iops
 *
 * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <linux/fs.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/highmem.h>
#include <linux/pagemap.h>

#include <asm/byteorder.h>

#define MLOG_MASK_PREFIX ML_INODE
#include <cluster/masklog.h>

#include "ocfs2.h"

#include "alloc.h"
#include "dlmglue.h"
#include "extent_map.h"
#include "file.h"
43
#include "heartbeat.h"
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "inode.h"
#include "journal.h"
#include "namei.h"
#include "suballoc.h"
#include "super.h"
#include "symlink.h"
#include "sysfile.h"
#include "uptodate.h"

#include "buffer_head_io.h"

struct ocfs2_find_inode_args
{
	u64		fi_blkno;
	unsigned long	fi_ino;
	unsigned int	fi_flags;
J
Jan Kara 已提交
60
	unsigned int	fi_sysfile_type;
61 62
};

J
Jan Kara 已提交
63 64
static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];

65 66 67 68 69 70 71 72
static int ocfs2_read_locked_inode(struct inode *inode,
				   struct ocfs2_find_inode_args *args);
static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
static int ocfs2_find_actor(struct inode *inode, void *opaque);
static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
				    struct inode *inode,
				    struct buffer_head *fe_bh);

H
Herbert Poetzl 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
void ocfs2_set_inode_flags(struct inode *inode)
{
	unsigned int flags = OCFS2_I(inode)->ip_attr;

	inode->i_flags &= ~(S_IMMUTABLE |
		S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);

	if (flags & OCFS2_IMMUTABLE_FL)
		inode->i_flags |= S_IMMUTABLE;

	if (flags & OCFS2_SYNC_FL)
		inode->i_flags |= S_SYNC;
	if (flags & OCFS2_APPEND_FL)
		inode->i_flags |= S_APPEND;
	if (flags & OCFS2_NOATIME_FL)
		inode->i_flags |= S_NOATIME;
	if (flags & OCFS2_DIRSYNC_FL)
		inode->i_flags |= S_DIRSYNC;
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
{
	unsigned int flags = oi->vfs_inode.i_flags;

	oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
			OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
	if (flags & S_SYNC)
		oi->ip_attr |= OCFS2_SYNC_FL;
	if (flags & S_APPEND)
		oi->ip_attr |= OCFS2_APPEND_FL;
	if (flags & S_IMMUTABLE)
		oi->ip_attr |= OCFS2_IMMUTABLE_FL;
	if (flags & S_NOATIME)
		oi->ip_attr |= OCFS2_NOATIME_FL;
	if (flags & S_DIRSYNC)
		oi->ip_attr |= OCFS2_DIRSYNC_FL;
}

J
Jan Kara 已提交
112 113
struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
			 int sysfile_type)
114 115 116 117 118
{
	struct inode *inode = NULL;
	struct super_block *sb = osb->sb;
	struct ocfs2_find_inode_args args;

119
	mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
120 121 122 123 124 125 126 127 128 129 130

	/* Ok. By now we've either got the offsets passed to us by the
	 * caller, or we just pulled them off the bh. Lets do some
	 * sanity checks to make sure they're OK. */
	if (blkno == 0) {
		inode = ERR_PTR(-EINVAL);
		mlog_errno(PTR_ERR(inode));
		goto bail;
	}

	args.fi_blkno = blkno;
131
	args.fi_flags = flags;
132
	args.fi_ino = ino_from_blkno(sb, blkno);
J
Jan Kara 已提交
133
	args.fi_sysfile_type = sysfile_type;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

	inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
			     ocfs2_init_locked_inode, &args);
	/* inode was *not* in the inode cache. 2.6.x requires
	 * us to do our own read_inode call and unlock it
	 * afterwards. */
	if (inode && inode->i_state & I_NEW) {
		mlog(0, "Inode was not in inode cache, reading it.\n");
		ocfs2_read_locked_inode(inode, &args);
		unlock_new_inode(inode);
	}
	if (inode == NULL) {
		inode = ERR_PTR(-ENOMEM);
		mlog_errno(PTR_ERR(inode));
		goto bail;
	}
	if (is_bad_inode(inode)) {
		iput(inode);
		inode = ERR_PTR(-ESTALE);
		goto bail;
	}

bail:
	if (!IS_ERR(inode)) {
158 159
		mlog(0, "returning inode with number %llu\n",
		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
160
		mlog_exit_ptr(inode);
M
Mark Fasheh 已提交
161
	}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	return inode;
}


/*
 * here's how inodes get read from disk:
 * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
 * found? : return the in-memory inode
 * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
 */

static int ocfs2_find_actor(struct inode *inode, void *opaque)
{
	struct ocfs2_find_inode_args *args = NULL;
	struct ocfs2_inode_info *oi = OCFS2_I(inode);
	int ret = 0;

	mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);

	args = opaque;

	mlog_bug_on_msg(!inode, "No inode in find actor!\n");

	if (oi->ip_blkno != args->fi_blkno)
		goto bail;

	ret = 1;
bail:
	mlog_exit(ret);
	return ret;
}

/*
 * initialize the new inode, but don't do anything that would cause
 * us to sleep.
 * return 0 on success, 1 on failure
 */
static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
{
	struct ocfs2_find_inode_args *args = opaque;

	mlog_entry("inode = %p, opaque = %p\n", inode, opaque);

	inode->i_ino = args->fi_ino;
	OCFS2_I(inode)->ip_blkno = args->fi_blkno;
J
Jan Kara 已提交
208 209 210
	if (args->fi_sysfile_type != 0)
		lockdep_set_class(&inode->i_mutex,
			&ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
211 212 213 214 215 216 217 218 219 220 221 222

	mlog_exit(0);
	return 0;
}

int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
		     	 int create_ino)
{
	struct super_block *sb;
	struct ocfs2_super *osb;
	int status = -EINVAL;

223
	mlog_entry("(0x%p, size:%llu)\n", inode,
224
		   (unsigned long long)le64_to_cpu(fe->i_size));
225 226 227 228 229 230 231 232

	sb = inode->i_sb;
	osb = OCFS2_SB(sb);

	/* this means that read_inode cannot create a superblock inode
	 * today.  change if needed. */
	if (!OCFS2_IS_VALID_DINODE(fe) ||
	    !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
M
Mark Fasheh 已提交
233
		mlog(0, "Invalid dinode: i_ino=%lu, i_blkno=%llu, "
234
		     "signature = %.*s, flags = 0x%x\n",
235 236
		     inode->i_ino,
		     (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
237 238 239 240 241 242 243 244 245 246 247 248
		     fe->i_signature, le32_to_cpu(fe->i_flags));
		goto bail;
	}

	if (le32_to_cpu(fe->i_fs_generation) != osb->fs_generation) {
		mlog(ML_ERROR, "file entry generation does not match "
		     "superblock! osb->fs_generation=%x, "
		     "fe->i_fs_generation=%x\n",
		     osb->fs_generation, le32_to_cpu(fe->i_fs_generation));
		goto bail;
	}

249 250
	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
251
	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
252

253 254 255 256 257 258 259 260 261 262 263
	inode->i_version = 1;
	inode->i_generation = le32_to_cpu(fe->i_generation);
	inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
	inode->i_mode = le16_to_cpu(fe->i_mode);
	inode->i_uid = le32_to_cpu(fe->i_uid);
	inode->i_gid = le32_to_cpu(fe->i_gid);

	/* Fast symlinks will have i_size but no allocated clusters. */
	if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
		inode->i_blocks = 0;
	else
264
		inode->i_blocks = ocfs2_inode_sector_count(inode);
265 266 267 268 269 270 271 272 273 274
	inode->i_mapping->a_ops = &ocfs2_aops;
	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);

	if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
		mlog(ML_ERROR,
275 276
		     "ip_blkno %llu != i_blkno %llu!\n",
		     (unsigned long long)OCFS2_I(inode)->ip_blkno,
277
		     (unsigned long long)le64_to_cpu(fe->i_blkno));
278 279 280

	inode->i_nlink = le16_to_cpu(fe->i_links_count);

281 282 283
	if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL))
		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
		mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
	} else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
	} else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
		mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
		/* we can't actually hit this as read_inode can't
		 * handle superblocks today ;-) */
		BUG();
	}

	switch (inode->i_mode & S_IFMT) {
	    case S_IFREG:
		    inode->i_fop = &ocfs2_fops;
		    inode->i_op = &ocfs2_file_iops;
		    i_size_write(inode, le64_to_cpu(fe->i_size));
		    break;
	    case S_IFDIR:
		    inode->i_op = &ocfs2_dir_iops;
		    inode->i_fop = &ocfs2_dops;
		    i_size_write(inode, le64_to_cpu(fe->i_size));
		    break;
	    case S_IFLNK:
		    if (ocfs2_inode_is_fast_symlink(inode))
			inode->i_op = &ocfs2_fast_symlink_inode_operations;
		    else
			inode->i_op = &ocfs2_symlink_inode_operations;
		    i_size_write(inode, le64_to_cpu(fe->i_size));
		    break;
	    default:
		    inode->i_op = &ocfs2_special_file_iops;
		    init_special_inode(inode, inode->i_mode,
				       inode->i_rdev);
		    break;
	}

321 322 323 324 325 326 327 328 329
	if (create_ino) {
		inode->i_ino = ino_from_blkno(inode->i_sb,
			       le64_to_cpu(fe->i_blkno));

		/*
		 * If we ever want to create system files from kernel,
		 * the generation argument to
		 * ocfs2_inode_lock_res_init() will have to change.
		 */
330
		BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
331

M
Mark Fasheh 已提交
332
		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
333
					  OCFS2_LOCK_TYPE_META, 0, inode);
T
Tiger Yang 已提交
334 335 336

		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
					  OCFS2_LOCK_TYPE_OPEN, 0, inode);
337 338
	}

339
	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
340 341 342
				  OCFS2_LOCK_TYPE_RW, inode->i_generation,
				  inode);

H
Herbert Poetzl 已提交
343 344
	ocfs2_set_inode_flags(inode);

345 346 347 348 349 350 351 352 353 354 355 356 357
	status = 0;
bail:
	mlog_exit(status);
	return status;
}

static int ocfs2_read_locked_inode(struct inode *inode,
				   struct ocfs2_find_inode_args *args)
{
	struct super_block *sb;
	struct ocfs2_super *osb;
	struct ocfs2_dinode *fe;
	struct buffer_head *bh = NULL;
358 359
	int status, can_lock;
	u32 generation = 0;
360 361 362 363 364 365

	mlog_entry("(0x%p, 0x%p)\n", inode, args);

	status = -EINVAL;
	if (inode == NULL || inode->i_sb == NULL) {
		mlog(ML_ERROR, "bad inode\n");
366
		return status;
367 368 369 370 371 372 373
	}
	sb = inode->i_sb;
	osb = OCFS2_SB(sb);

	if (!args) {
		mlog(ML_ERROR, "bad inode args\n");
		make_bad_inode(inode);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
		return status;
	}

	/*
	 * To improve performance of cold-cache inode stats, we take
	 * the cluster lock here if possible.
	 *
	 * Generally, OCFS2 never trusts the contents of an inode
	 * unless it's holding a cluster lock, so taking it here isn't
	 * a correctness issue as much as it is a performance
	 * improvement.
	 *
	 * There are three times when taking the lock is not a good idea:
	 *
	 * 1) During startup, before we have initialized the DLM.
	 *
	 * 2) If we are reading certain system files which never get
	 *    cluster locks (local alloc, truncate log).
	 *
	 * 3) If the process doing the iget() is responsible for
	 *    orphan dir recovery. We're holding the orphan dir lock and
	 *    can get into a deadlock with another process on another
	 *    node in ->delete_inode().
	 *
	 * #1 and #2 can be simply solved by never taking the lock
	 * here for system files (which are the only type we read
	 * during mount). It's a heavier approach, but our main
	 * concern is user-accesible files anyway.
	 *
	 * #3 works itself out because we'll eventually take the
	 * cluster lock before trusting anything anyway.
	 */
	can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
T
Tiger Yang 已提交
407
		&& !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
S
Sunil Mushran 已提交
408
		&& !ocfs2_mount_local(osb);
409 410 411 412 413 414 415 416 417 418 419

	/*
	 * To maintain backwards compatibility with older versions of
	 * ocfs2-tools, we still store the generation value for system
	 * files. The only ones that actually matter to userspace are
	 * the journals, but it's easier and inexpensive to just flag
	 * all system files similarly.
	 */
	if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
		generation = osb->fs_generation;

M
Mark Fasheh 已提交
420
	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
421 422 423
				  OCFS2_LOCK_TYPE_META,
				  generation, inode);

T
Tiger Yang 已提交
424 425 426 427
	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
				  OCFS2_LOCK_TYPE_OPEN,
				  0, inode);

428
	if (can_lock) {
T
Tiger Yang 已提交
429 430 431 432 433 434
		status = ocfs2_open_lock(inode);
		if (status) {
			make_bad_inode(inode);
			mlog_errno(status);
			return status;
		}
M
Mark Fasheh 已提交
435
		status = ocfs2_inode_lock(inode, NULL, 0);
436 437 438 439 440
		if (status) {
			make_bad_inode(inode);
			mlog_errno(status);
			return status;
		}
441 442
	}

T
Tiger Yang 已提交
443 444 445 446 447 448 449 450
	if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
		status = ocfs2_try_open_lock(inode, 0);
		if (status) {
			make_bad_inode(inode);	
			return status;
		}
	}

451 452
	status = ocfs2_read_block(osb, args->fi_blkno, &bh, 0,
				  can_lock ? inode : NULL);
453 454 455 456 457
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

458
	status = -EINVAL;
459 460
	fe = (struct ocfs2_dinode *) bh->b_data;
	if (!OCFS2_IS_VALID_DINODE(fe)) {
461 462
		mlog(0, "Invalid dinode #%llu: signature = %.*s\n",
		     (unsigned long long)args->fi_blkno, 7,
463
		     fe->i_signature);
464 465 466
		goto bail;
	}

467 468 469 470 471 472 473 474 475
	/*
	 * This is a code bug. Right now the caller needs to
	 * understand whether it is asking for a system file inode or
	 * not so the proper lock names can be built.
	 */
	mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
			!!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
			"Inode %llu: system file state is ambigous\n",
			(unsigned long long)args->fi_blkno);
476 477 478 479 480

	if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
	    S_ISBLK(le16_to_cpu(fe->i_mode)))
    		inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));

M
Mark Fasheh 已提交
481
	if (ocfs2_populate_inode(inode, fe, 0) < 0)
482 483 484 485 486 487 488
		goto bail;

	BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));

	status = 0;

bail:
489
	if (can_lock)
M
Mark Fasheh 已提交
490
		ocfs2_inode_unlock(inode, 0);
491 492 493 494

	if (status < 0)
		make_bad_inode(inode);

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	if (args && bh)
		brelse(bh);

	mlog_exit(status);
	return status;
}

void ocfs2_sync_blockdev(struct super_block *sb)
{
	sync_blockdev(sb->s_bdev);
}

static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
				     struct inode *inode,
				     struct buffer_head *fe_bh)
{
	int status = 0;
	struct ocfs2_truncate_context *tc = NULL;
	struct ocfs2_dinode *fe;
514
	handle_t *handle = NULL;
515 516 517 518 519

	mlog_entry_void();

	fe = (struct ocfs2_dinode *) fe_bh->b_data;

M
Mark Fasheh 已提交
520 521 522 523
	/*
	 * This check will also skip truncate of inodes with inline
	 * data and fast symlinks.
	 */
524
	if (fe->i_clusters) {
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
		if (IS_ERR(handle)) {
			status = PTR_ERR(handle);
			mlog_errno(status);
			goto out;
		}

		status = ocfs2_journal_access(handle, inode, fe_bh,
					      OCFS2_JOURNAL_ACCESS_WRITE);
		if (status < 0) {
			mlog_errno(status);
			goto out;
		}

		i_size_write(inode, 0);

		status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
		if (status < 0) {
			mlog_errno(status);
			goto out;
		}

		ocfs2_commit_trans(osb, handle);
		handle = NULL;

550 551 552 553 554
		status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
		if (status < 0) {
			mlog_errno(status);
			goto out;
		}
555

556 557 558 559 560
		status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
		if (status < 0) {
			mlog_errno(status);
			goto out;
		}
561 562
	}

563 564 565
out:
	if (handle)
		ocfs2_commit_trans(osb, handle);
566 567 568 569 570 571 572 573 574 575 576 577
	mlog_exit(status);
	return status;
}

static int ocfs2_remove_inode(struct inode *inode,
			      struct buffer_head *di_bh,
			      struct inode *orphan_dir_inode,
			      struct buffer_head *orphan_dir_bh)
{
	int status;
	struct inode *inode_alloc_inode = NULL;
	struct buffer_head *inode_alloc_bh = NULL;
578
	handle_t *handle;
579 580 581 582 583 584 585 586 587 588 589 590
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;

	inode_alloc_inode =
		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
					    le16_to_cpu(di->i_suballoc_slot));
	if (!inode_alloc_inode) {
		status = -EEXIST;
		mlog_errno(status);
		goto bail;
	}

591
	mutex_lock(&inode_alloc_inode->i_mutex);
M
Mark Fasheh 已提交
592
	status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
593
	if (status < 0) {
594
		mutex_unlock(&inode_alloc_inode->i_mutex);
595 596 597 598 599

		mlog_errno(status);
		goto bail;
	}

600
	handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
	if (IS_ERR(handle)) {
		status = PTR_ERR(handle);
		mlog_errno(status);
		goto bail_unlock;
	}

	status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
				  orphan_dir_bh);
	if (status < 0) {
		mlog_errno(status);
		goto bail_commit;
	}

	/* set the inodes dtime */
	status = ocfs2_journal_access(handle, inode, di_bh,
				      OCFS2_JOURNAL_ACCESS_WRITE);
	if (status < 0) {
		mlog_errno(status);
		goto bail_commit;
	}

	di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
	le32_and_cpu(&di->i_flags, ~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));

	status = ocfs2_journal_dirty(handle, di_bh);
	if (status < 0) {
		mlog_errno(status);
		goto bail_commit;
	}

	ocfs2_remove_from_cache(inode, di_bh);

	status = ocfs2_free_dinode(handle, inode_alloc_inode,
				   inode_alloc_bh, di);
	if (status < 0)
		mlog_errno(status);

bail_commit:
639
	ocfs2_commit_trans(osb, handle);
640
bail_unlock:
M
Mark Fasheh 已提交
641
	ocfs2_inode_unlock(inode_alloc_inode, 1);
642
	mutex_unlock(&inode_alloc_inode->i_mutex);
643 644 645 646 647 648 649
	brelse(inode_alloc_bh);
bail:
	iput(inode_alloc_inode);

	return status;
}

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
/* 
 * Serialize with orphan dir recovery. If the process doing
 * recovery on this orphan dir does an iget() with the dir
 * i_mutex held, we'll deadlock here. Instead we detect this
 * and exit early - recovery will wipe this inode for us.
 */
static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
					     int slot)
{
	int ret = 0;

	spin_lock(&osb->osb_lock);
	if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
		mlog(0, "Recovery is happening on orphan dir %d, will skip "
		     "this inode\n", slot);
		ret = -EDEADLK;
		goto out;
	}
	/* This signals to the orphan recovery process that it should
	 * wait for us to handle the wipe. */
	osb->osb_orphan_wipes[slot]++;
out:
	spin_unlock(&osb->osb_lock);
	return ret;
}

static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
					 int slot)
{
	spin_lock(&osb->osb_lock);
	osb->osb_orphan_wipes[slot]--;
	spin_unlock(&osb->osb_lock);

	wake_up(&osb->osb_wipe_event);
}

686 687 688 689 690 691 692
static int ocfs2_wipe_inode(struct inode *inode,
			    struct buffer_head *di_bh)
{
	int status, orphaned_slot;
	struct inode *orphan_dir_inode = NULL;
	struct buffer_head *orphan_dir_bh = NULL;
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
T
Tiger Yang 已提交
693
	struct ocfs2_dinode *di;
694

T
Tiger Yang 已提交
695 696
	di = (struct ocfs2_dinode *) di_bh->b_data;
	orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
697 698 699 700 701

	status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
	if (status)
		return status;

702 703 704 705 706 707 708 709 710 711 712 713
	orphan_dir_inode = ocfs2_get_system_file_inode(osb,
						       ORPHAN_DIR_SYSTEM_INODE,
						       orphaned_slot);
	if (!orphan_dir_inode) {
		status = -EEXIST;
		mlog_errno(status);
		goto bail;
	}

	/* Lock the orphan dir. The lock will be held for the entire
	 * delete_inode operation. We do this now to avoid races with
	 * recovery completion on other nodes. */
714
	mutex_lock(&orphan_dir_inode->i_mutex);
M
Mark Fasheh 已提交
715
	status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
716
	if (status < 0) {
717
		mutex_unlock(&orphan_dir_inode->i_mutex);
718 719 720 721 722 723

		mlog_errno(status);
		goto bail;
	}

	/* we do this while holding the orphan dir lock because we
M
Mark Fasheh 已提交
724 725
	 * don't want recovery being run from another node to try an
	 * inode delete underneath us -- this will result in two nodes
726 727 728 729 730 731 732 733 734 735 736 737 738
	 * truncating the same file! */
	status = ocfs2_truncate_for_delete(osb, inode, di_bh);
	if (status < 0) {
		mlog_errno(status);
		goto bail_unlock_dir;
	}

	status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
				    orphan_dir_bh);
	if (status < 0)
		mlog_errno(status);

bail_unlock_dir:
M
Mark Fasheh 已提交
739
	ocfs2_inode_unlock(orphan_dir_inode, 1);
740
	mutex_unlock(&orphan_dir_inode->i_mutex);
741 742 743
	brelse(orphan_dir_bh);
bail:
	iput(orphan_dir_inode);
744
	ocfs2_signal_wipe_completion(osb, orphaned_slot);
745 746 747 748 749

	return status;
}

/* There is a series of simple checks that should be done before a
M
Mark Fasheh 已提交
750
 * trylock is even considered. Encapsulate those in this function. */
751 752 753 754 755 756 757 758 759 760 761 762 763
static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
{
	int ret = 0;
	struct ocfs2_inode_info *oi = OCFS2_I(inode);
	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);

	/* We shouldn't be getting here for the root directory
	 * inode.. */
	if (inode == osb->root_inode) {
		mlog(ML_ERROR, "Skipping delete of root inode.\n");
		goto bail;
	}

M
Mark Fasheh 已提交
764
	/* If we're coming from downconvert_thread we can't go into our own
765 766 767 768
	 * voting [hello, deadlock city!], so unforuntately we just
	 * have to skip deleting this guy. That's OK though because
	 * the node who's doing the actual deleting should handle it
	 * anyway. */
M
Mark Fasheh 已提交
769
	if (current == osb->dc_task) {
770
		mlog(0, "Skipping delete of %lu because we're currently "
M
Mark Fasheh 已提交
771
		     "in downconvert\n", inode->i_ino);
772 773 774 775 776 777 778 779
		goto bail;
	}

	spin_lock(&oi->ip_lock);
	/* OCFS2 *never* deletes system files. This should technically
	 * never get here as system file inodes should always have a
	 * positive link count. */
	if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
780 781
		mlog(ML_ERROR, "Skipping delete of system file %llu\n",
		     (unsigned long long)oi->ip_blkno);
782 783 784
		goto bail_unlock;
	}

M
Mark Fasheh 已提交
785 786 787
	/* If we have allowd wipe of this inode for another node, it
	 * will be marked here so we can safely skip it. Recovery will
	 * cleanup any inodes we might inadvertantly skip here. */
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
	if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
		mlog(0, "Skipping delete of %lu because another node "
		     "has done this for us.\n", inode->i_ino);
		goto bail_unlock;
	}

	ret = 1;
bail_unlock:
	spin_unlock(&oi->ip_lock);
bail:
	return ret;
}

/* Query the cluster to determine whether we should wipe an inode from
 * disk or not.
 *
 * Requires the inode to have the cluster lock. */
static int ocfs2_query_inode_wipe(struct inode *inode,
				  struct buffer_head *di_bh,
				  int *wipe)
{
	int status = 0;
	struct ocfs2_inode_info *oi = OCFS2_I(inode);
	struct ocfs2_dinode *di;

	*wipe = 0;

	/* While we were waiting for the cluster lock in
	 * ocfs2_delete_inode, another node might have asked to delete
	 * the inode. Recheck our flags to catch this. */
	if (!ocfs2_inode_is_valid_to_delete(inode)) {
819 820
		mlog(0, "Skipping delete of %llu because flags changed\n",
		     (unsigned long long)oi->ip_blkno);
821 822 823 824 825 826
		goto bail;
	}

	/* Now that we have an up to date inode, we can double check
	 * the link count. */
	if (inode->i_nlink) {
827 828
		mlog(0, "Skipping delete of %llu because nlink = %u\n",
		     (unsigned long long)oi->ip_blkno, inode->i_nlink);
829 830 831 832 833 834 835 836 837
		goto bail;
	}

	/* Do some basic inode verification... */
	di = (struct ocfs2_dinode *) di_bh->b_data;
	if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
		/* for lack of a better error? */
		status = -EEXIST;
		mlog(ML_ERROR,
838
		     "Inode %llu (on-disk %llu) not orphaned! "
839
		     "Disk flags  0x%x, inode flags 0x%x\n",
840
		     (unsigned long long)oi->ip_blkno,
841 842
		     (unsigned long long)le64_to_cpu(di->i_blkno),
		     le32_to_cpu(di->i_flags), oi->ip_flags);
843 844 845 846 847 848 849 850 851 852
		goto bail;
	}

	/* has someone already deleted us?! baaad... */
	if (di->i_dtime) {
		status = -EEXIST;
		mlog_errno(status);
		goto bail;
	}

853 854 855 856 857 858 859 860 861 862 863 864 865
	/*
	 * This is how ocfs2 determines whether an inode is still live
	 * within the cluster. Every node takes a shared read lock on
	 * the inode open lock in ocfs2_read_locked_inode(). When we
	 * get to ->delete_inode(), each node tries to convert it's
	 * lock to an exclusive. Trylocks are serialized by the inode
	 * meta data lock. If the upconvert suceeds, we know the inode
	 * is no longer live and can be deleted.
	 *
	 * Though we call this with the meta data lock held, the
	 * trylock keeps us from ABBA deadlock.
	 */
	status = ocfs2_try_open_lock(inode, 1);
T
Tiger Yang 已提交
866
	if (status == -EAGAIN) {
867
		status = 0;
868
		mlog(0, "Skipping delete of %llu because it is in use on "
869
		     "other nodes\n", (unsigned long long)oi->ip_blkno);
870 871 872 873 874 875 876
		goto bail;
	}
	if (status < 0) {
		mlog_errno(status);
		goto bail;
	}

T
Tiger Yang 已提交
877 878 879 880
	*wipe = 1;
	mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
	     (unsigned long long)oi->ip_blkno,
	     le16_to_cpu(di->i_orphaned_slot));
881 882 883 884 885 886 887 888 889 890 891

bail:
	return status;
}

/* Support function for ocfs2_delete_inode. Will help us keep the
 * inode data in a consistent state for clear_inode. Always truncates
 * pages, optionally sync's them first. */
static void ocfs2_cleanup_delete_inode(struct inode *inode,
				       int sync_data)
{
892 893
	mlog(0, "Cleanup inode %llu, sync = %d\n",
	     (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
	if (sync_data)
		write_inode_now(inode, 1);
	truncate_inode_pages(&inode->i_data, 0);
}

void ocfs2_delete_inode(struct inode *inode)
{
	int wipe, status;
	sigset_t blocked, oldset;
	struct buffer_head *di_bh = NULL;

	mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);

	if (is_bad_inode(inode)) {
		mlog(0, "Skipping delete of bad inode\n");
		goto bail;
	}

	if (!ocfs2_inode_is_valid_to_delete(inode)) {
		/* It's probably not necessary to truncate_inode_pages
		 * here but we do it for safety anyway (it will most
		 * likely be a no-op anyway) */
		ocfs2_cleanup_delete_inode(inode, 0);
		goto bail;
	}

	/* We want to block signals in delete_inode as the lock and
	 * messaging paths may return us -ERESTARTSYS. Which would
	 * cause us to exit early, resulting in inodes being orphaned
	 * forever. */
	sigfillset(&blocked);
	status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
	if (status < 0) {
		mlog_errno(status);
		ocfs2_cleanup_delete_inode(inode, 1);
		goto bail;
	}

	/* Lock down the inode. This gives us an up to date view of
	 * it's metadata (for verification), and allows us to
M
Mark Fasheh 已提交
934
	 * serialize delete_inode on multiple nodes.
935 936 937 938 939
	 *
	 * Even though we might be doing a truncate, we don't take the
	 * allocation lock here as it won't be needed - nobody will
	 * have the file open.
	 */
M
Mark Fasheh 已提交
940
	status = ocfs2_inode_lock(inode, &di_bh, 1);
941 942 943 944 945 946 947 948 949 950 951
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		ocfs2_cleanup_delete_inode(inode, 0);
		goto bail_unblock;
	}

	/* Query the cluster. This will be the final decision made
	 * before we go ahead and wipe the inode. */
	status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
	if (!wipe || status < 0) {
M
Mark Fasheh 已提交
952
		/* Error and remote inode busy both mean we won't be
953 954 955 956 957
		 * removing the inode, so they take almost the same
		 * path. */
		if (status < 0)
			mlog_errno(status);

M
Mark Fasheh 已提交
958 959 960
		/* Someone in the cluster has disallowed a wipe of
		 * this inode, or it was never completely
		 * orphaned. Write out the pages and exit now. */
961 962 963 964 965 966 967 968
		ocfs2_cleanup_delete_inode(inode, 1);
		goto bail_unlock_inode;
	}

	ocfs2_cleanup_delete_inode(inode, 0);

	status = ocfs2_wipe_inode(inode, di_bh);
	if (status < 0) {
969 970
		if (status != -EDEADLK)
			mlog_errno(status);
971 972 973
		goto bail_unlock_inode;
	}

974 975 976 977 978 979 980 981 982
	/*
	 * Mark the inode as successfully deleted.
	 *
	 * This is important for ocfs2_clear_inode() as it will check
	 * this flag and skip any checkpointing work
	 *
	 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
	 * the LVB for other nodes.
	 */
983 984 985
	OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;

bail_unlock_inode:
M
Mark Fasheh 已提交
986
	ocfs2_inode_unlock(inode, 1);
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	brelse(di_bh);
bail_unblock:
	status = sigprocmask(SIG_SETMASK, &oldset, NULL);
	if (status < 0)
		mlog_errno(status);
bail:
	clear_inode(inode);
	mlog_exit_void();
}

void ocfs2_clear_inode(struct inode *inode)
{
	int status;
	struct ocfs2_inode_info *oi = OCFS2_I(inode);

	mlog_entry_void();

	if (!inode)
		goto bail;

1007 1008
	mlog(0, "Clearing inode: %llu, nlink = %u\n",
	     (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
1009 1010 1011 1012

	mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
			"Inode=%lu\n", inode->i_ino);

M
Mark Fasheh 已提交
1013 1014
	/* To preven remote deletes we hold open lock before, now it
	 * is time to unlock PR and EX open locks. */
T
Tiger Yang 已提交
1015 1016
	ocfs2_open_unlock(inode);

1017
	/* Do these before all the other work so that we don't bounce
M
Mark Fasheh 已提交
1018
	 * the downconvert thread while waiting to destroy the locks. */
1019
	ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
M
Mark Fasheh 已提交
1020
	ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
T
Tiger Yang 已提交
1021
	ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

	/* We very well may get a clear_inode before all an inodes
	 * metadata has hit disk. Of course, we can't drop any cluster
	 * locks until the journal has finished with it. The only
	 * exception here are successfully wiped inodes - their
	 * metadata can now be considered to be part of the system
	 * inodes from which it came. */
	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
		ocfs2_checkpoint_inode(inode);

	mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
1033 1034
			"Clear inode of %llu, inode has io markers\n",
			(unsigned long long)oi->ip_blkno);
1035

M
Mark Fasheh 已提交
1036 1037
	ocfs2_extent_map_trunc(inode, 0);

1038 1039 1040 1041 1042
	status = ocfs2_drop_inode_locks(inode);
	if (status < 0)
		mlog_errno(status);

	ocfs2_lock_res_free(&oi->ip_rw_lockres);
M
Mark Fasheh 已提交
1043
	ocfs2_lock_res_free(&oi->ip_inode_lockres);
T
Tiger Yang 已提交
1044
	ocfs2_lock_res_free(&oi->ip_open_lockres);
1045 1046 1047 1048

	ocfs2_metadata_cache_purge(inode);

	mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
1049 1050
			"Clear inode of %llu, inode has %u cache items\n",
			(unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
1051 1052

	mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
1053 1054
			"Clear inode of %llu, inode has a bad flag\n",
			(unsigned long long)oi->ip_blkno);
1055 1056

	mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1057 1058
			"Clear inode of %llu, inode is locked\n",
			(unsigned long long)oi->ip_blkno);
1059

M
Mark Fasheh 已提交
1060
	mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1061 1062
			"Clear inode of %llu, io_mutex is locked\n",
			(unsigned long long)oi->ip_blkno);
M
Mark Fasheh 已提交
1063
	mutex_unlock(&oi->ip_io_mutex);
1064 1065 1066 1067 1068 1069

	/*
	 * down_trylock() returns 0, down_write_trylock() returns 1
	 * kernel 1, world 0
	 */
	mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1070 1071
			"Clear inode of %llu, alloc_sem is locked\n",
			(unsigned long long)oi->ip_blkno);
1072 1073 1074
	up_write(&oi->ip_alloc_sem);

	mlog_bug_on_msg(oi->ip_open_count,
1075 1076
			"Clear inode of %llu has open count %d\n",
			(unsigned long long)oi->ip_blkno, oi->ip_open_count);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

	/* Clear all other flags. */
	oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
	oi->ip_created_trans = 0;
	oi->ip_last_trans = 0;
	oi->ip_dir_start_lookup = 0;
	oi->ip_blkno = 0ULL;

bail:
	mlog_exit_void();
}

/* Called under inode_lock, with no more references on the
 * struct inode, so it's safe here to check the flags field
 * and to manipulate i_nlink without any other locks. */
void ocfs2_drop_inode(struct inode *inode)
{
	struct ocfs2_inode_info *oi = OCFS2_I(inode);

	mlog_entry_void();

1098 1099
	mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
	     (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
1100

1101 1102 1103 1104
	if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
		generic_delete_inode(inode);
	else
		generic_drop_inode(inode);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

	mlog_exit_void();
}

/*
 * TODO: this should probably be merged into ocfs2_get_block
 *
 * However, you now need to pay attention to the cont_prepare_write()
 * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
 * expects never to extend).
 */
struct buffer_head *ocfs2_bread(struct inode *inode,
				int block, int *err, int reada)
{
	struct buffer_head *bh = NULL;
	int tmperr;
	u64 p_blkno;
	int readflags = OCFS2_BH_CACHED;

	if (reada)
		readflags |= OCFS2_BH_READAHEAD;

	if (((u64)block << inode->i_sb->s_blocksize_bits) >=
	    i_size_read(inode)) {
		BUG_ON(!reada);
		return NULL;
	}

1133
	down_read(&OCFS2_I(inode)->ip_alloc_sem);
1134 1135
	tmperr = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
					     NULL);
1136
	up_read(&OCFS2_I(inode)->ip_alloc_sem);
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	if (tmperr < 0) {
		mlog_errno(tmperr);
		goto fail;
	}

	tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
				  readflags, inode);
	if (tmperr < 0)
		goto fail;

	tmperr = 0;

	*err = 0;
	return bh;

fail:
	if (bh) {
		brelse(bh);
		bh = NULL;
	}
	*err = -EIO;
	return NULL;
}

/*
 * This is called from our getattr.
 */
int ocfs2_inode_revalidate(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	int status = 0;

1169 1170
	mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
		   inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

	if (!inode) {
		mlog(0, "eep, no inode!\n");
		status = -ENOENT;
		goto bail;
	}

	spin_lock(&OCFS2_I(inode)->ip_lock);
	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
		spin_unlock(&OCFS2_I(inode)->ip_lock);
		mlog(0, "inode deleted!\n");
		status = -ENOENT;
		goto bail;
	}
	spin_unlock(&OCFS2_I(inode)->ip_lock);

M
Mark Fasheh 已提交
1187
	/* Let ocfs2_inode_lock do the work of updating our struct
1188
	 * inode for us. */
M
Mark Fasheh 已提交
1189
	status = ocfs2_inode_lock(inode, NULL, 0);
1190 1191 1192 1193 1194
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		goto bail;
	}
M
Mark Fasheh 已提交
1195
	ocfs2_inode_unlock(inode, 0);
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
bail:
	mlog_exit(status);

	return status;
}

/*
 * Updates a disk inode from a
 * struct inode.
 * Only takes ip_lock.
 */
1207
int ocfs2_mark_inode_dirty(handle_t *handle,
1208 1209 1210 1211 1212 1213
			   struct inode *inode,
			   struct buffer_head *bh)
{
	int status;
	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;

1214 1215
	mlog_entry("(inode %llu)\n",
		   (unsigned long long)OCFS2_I(inode)->ip_blkno);
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

	status = ocfs2_journal_access(handle, inode, bh,
				      OCFS2_JOURNAL_ACCESS_WRITE);
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

	spin_lock(&OCFS2_I(inode)->ip_lock);
	fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1226
	ocfs2_get_inode_flags(OCFS2_I(inode));
H
Herbert Poetzl 已提交
1227
	fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
1228
	fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	spin_unlock(&OCFS2_I(inode)->ip_lock);

	fe->i_size = cpu_to_le64(i_size_read(inode));
	fe->i_links_count = cpu_to_le16(inode->i_nlink);
	fe->i_uid = cpu_to_le32(inode->i_uid);
	fe->i_gid = cpu_to_le32(inode->i_gid);
	fe->i_mode = cpu_to_le16(inode->i_mode);
	fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
	fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
	fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
	fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);

	status = ocfs2_journal_dirty(handle, bh);
	if (status < 0)
		mlog_errno(status);

	status = 0;
leave:

	mlog_exit(status);
	return status;
}

/*
 *
 * Updates a struct inode from a disk inode.
 * does no i/o, only takes ip_lock.
 */
void ocfs2_refresh_inode(struct inode *inode,
			 struct ocfs2_dinode *fe)
{
	spin_lock(&OCFS2_I(inode)->ip_lock);

	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
H
Herbert Poetzl 已提交
1265
	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
1266
	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
H
Herbert Poetzl 已提交
1267
	ocfs2_set_inode_flags(inode);
1268 1269 1270 1271 1272 1273 1274 1275
	i_size_write(inode, le64_to_cpu(fe->i_size));
	inode->i_nlink = le16_to_cpu(fe->i_links_count);
	inode->i_uid = le32_to_cpu(fe->i_uid);
	inode->i_gid = le32_to_cpu(fe->i_gid);
	inode->i_mode = le16_to_cpu(fe->i_mode);
	if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
		inode->i_blocks = 0;
	else
1276
		inode->i_blocks = ocfs2_inode_sector_count(inode);
1277 1278 1279 1280 1281 1282 1283 1284 1285
	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);

	spin_unlock(&OCFS2_I(inode)->ip_lock);
}