namei.c 70.5 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:
 *
 * namei.c
 *
 * Create and rename file, directory, symlinks
 *
 * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
 *
 *  Portions of this code from linux/fs/ext3/dir.c
 *
 *  Copyright (C) 1992, 1993, 1994, 1995
 *  Remy Card (card@masi.ibp.fr)
 *  Laboratoire MASI - Institut Blaise pascal
 *  Universite Pierre et Marie Curie (Paris VI)
 *
 *   from
 *
 *   linux/fs/minix/dir.c
 *
 *   Copyright (C) 1991, 1992 Linux Torvalds
 *
 * 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>
43
#include <linux/quotaops.h>
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

#include <cluster/masklog.h>

#include "ocfs2.h"

#include "alloc.h"
#include "dcache.h"
#include "dir.h"
#include "dlmglue.h"
#include "extent_map.h"
#include "file.h"
#include "inode.h"
#include "journal.h"
#include "namei.h"
#include "suballoc.h"
59
#include "super.h"
60 61 62
#include "symlink.h"
#include "sysfile.h"
#include "uptodate.h"
63
#include "xattr.h"
64
#include "acl.h"
Tao Ma's avatar
Tao Ma committed
65
#include "ocfs2_trace.h"
66 67 68 69 70

#include "buffer_head_io.h"

static int ocfs2_mknod_locked(struct ocfs2_super *osb,
			      struct inode *dir,
71
			      struct inode *inode,
72 73 74
			      dev_t dev,
			      struct buffer_head **new_fe_bh,
			      struct buffer_head *parent_fe_bh,
75
			      handle_t *handle,
76 77 78
			      struct ocfs2_alloc_context *inode_ac);

static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
79
				    struct inode **ret_orphan_dir,
80
				    u64 blkno,
81
				    char *name,
82 83
				    struct ocfs2_dir_lookup_result *lookup,
				    bool dio);
84 85

static int ocfs2_orphan_add(struct ocfs2_super *osb,
86
			    handle_t *handle,
87
			    struct inode *inode,
88
			    struct buffer_head *fe_bh,
89
			    char *name,
90
			    struct ocfs2_dir_lookup_result *lookup,
91 92
			    struct inode *orphan_dir_inode,
			    bool dio);
93 94

static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
95
				     handle_t *handle,
96 97 98
				     struct inode *inode,
				     const char *symname);

99 100 101 102 103 104 105 106
static int ocfs2_double_lock(struct ocfs2_super *osb,
			     struct buffer_head **bh1,
			     struct inode *inode1,
			     struct buffer_head **bh2,
			     struct inode *inode2,
			     int rename);

static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2);
107 108 109 110
/* An orphan dir name is an 8 byte value, printed as a hex string */
#define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))

static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
Al Viro's avatar
Al Viro committed
111
				   unsigned int flags)
112 113 114 115 116 117 118
{
	int status;
	u64 blkno;
	struct inode *inode = NULL;
	struct dentry *ret;
	struct ocfs2_inode_info *oi;

Tao Ma's avatar
Tao Ma committed
119 120 121
	trace_ocfs2_lookup(dir, dentry, dentry->d_name.len,
			   dentry->d_name.name,
			   (unsigned long long)OCFS2_I(dir)->ip_blkno, 0);
122 123 124 125 126 127

	if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) {
		ret = ERR_PTR(-ENAMETOOLONG);
		goto bail;
	}

Jan Kara's avatar
Jan Kara committed
128
	status = ocfs2_inode_lock_nested(dir, NULL, 0, OI_LS_PARENT);
129 130 131 132 133 134 135
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		ret = ERR_PTR(status);
		goto bail;
	}

136 137
	status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name,
					    dentry->d_name.len, &blkno);
138 139 140
	if (status < 0)
		goto bail_add;

141
	inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	if (IS_ERR(inode)) {
		ret = ERR_PTR(-EACCES);
		goto bail_unlock;
	}

	oi = OCFS2_I(inode);
	/* Clear any orphaned state... If we were able to look up the
	 * inode from a directory, it certainly can't be orphaned. We
	 * might have the bad state from a node which intended to
	 * orphan this inode but crashed before it could commit the
	 * unlink. */
	spin_lock(&oi->ip_lock);
	oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED;
	spin_unlock(&oi->ip_lock);

bail_add:
	ret = d_splice_alias(inode, dentry);

160 161 162 163 164 165 166 167 168 169 170
	if (inode) {
		/*
		 * If d_splice_alias() finds a DCACHE_DISCONNECTED
		 * dentry, it will d_move() it on top of ourse. The
		 * return value will indicate this however, so in
		 * those cases, we switch them around for the locking
		 * code.
		 *
		 * NOTE: This dentry already has ->d_op set from
		 * ocfs2_get_parent() and ocfs2_get_dentry()
		 */
171
		if (!IS_ERR_OR_NULL(ret))
172 173 174
			dentry = ret;

		status = ocfs2_dentry_attach_lock(dentry, inode,
175
						  OCFS2_I(dir)->ip_blkno);
176 177 178 179 180
		if (status) {
			mlog_errno(status);
			ret = ERR_PTR(status);
			goto bail_unlock;
		}
Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
181 182
	} else
		ocfs2_dentry_attach_gen(dentry);
183

184 185 186 187
bail_unlock:
	/* Don't drop the cluster lock until *after* the d_add --
	 * unlink on another node will message us to remove that
	 * dentry under this lock so otherwise we can race this with
188
	 * the downconvert thread and have a stale dentry. */
189
	ocfs2_inode_unlock(dir, 0);
190 191 192

bail:

Tao Ma's avatar
Tao Ma committed
193
	trace_ocfs2_lookup_ret(ret);
194 195 196 197

	return ret;
}

Al Viro's avatar
Al Viro committed
198
static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
199 200
{
	struct inode *inode;
201
	int status;
202 203 204 205

	inode = new_inode(dir->i_sb);
	if (!inode) {
		mlog(ML_ERROR, "new_inode failed!\n");
206
		return ERR_PTR(-ENOMEM);
207 208 209 210 211 212
	}

	/* populate as many fields early on as possible - many of
	 * these are used by the support functions here and in
	 * callers. */
	if (S_ISDIR(mode))
Miklos Szeredi's avatar
Miklos Szeredi committed
213
		set_nlink(inode, 2);
214
	inode_init_owner(inode, dir, mode);
215 216 217 218
	status = dquot_initialize(inode);
	if (status)
		return ERR_PTR(status);

219 220 221
	return inode;
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static void ocfs2_cleanup_add_entry_failure(struct ocfs2_super *osb,
		struct dentry *dentry, struct inode *inode)
{
	struct ocfs2_dentry_lock *dl = dentry->d_fsdata;

	ocfs2_simple_drop_lockres(osb, &dl->dl_lockres);
	ocfs2_lock_res_free(&dl->dl_lockres);
	BUG_ON(dl->dl_count != 1);
	spin_lock(&dentry_attach_lock);
	dentry->d_fsdata = NULL;
	spin_unlock(&dentry_attach_lock);
	kfree(dl);
	iput(inode);
}

237 238
static int ocfs2_mknod(struct inode *dir,
		       struct dentry *dentry,
Al Viro's avatar
Al Viro committed
239
		       umode_t mode,
240 241 242 243
		       dev_t dev)
{
	int status = 0;
	struct buffer_head *parent_fe_bh = NULL;
244
	handle_t *handle = NULL;
245 246 247 248 249 250
	struct ocfs2_super *osb;
	struct ocfs2_dinode *dirfe;
	struct buffer_head *new_fe_bh = NULL;
	struct inode *inode = NULL;
	struct ocfs2_alloc_context *inode_ac = NULL;
	struct ocfs2_alloc_context *data_ac = NULL;
251
	struct ocfs2_alloc_context *meta_ac = NULL;
252
	int want_clusters = 0;
253
	int want_meta = 0;
254 255 256 257
	int xattr_credits = 0;
	struct ocfs2_security_xattr_info si = {
		.enable = 1,
	};
258
	int did_quota_inode = 0;
259
	struct ocfs2_dir_lookup_result lookup = { NULL, };
260 261
	sigset_t oldset;
	int did_block_signals = 0;
262
	struct posix_acl *default_acl = NULL, *acl = NULL;
263
	struct ocfs2_dentry_lock *dl = NULL;
264

Tao Ma's avatar
Tao Ma committed
265 266 267
	trace_ocfs2_mknod(dir, dentry, dentry->d_name.len, dentry->d_name.name,
			  (unsigned long long)OCFS2_I(dir)->ip_blkno,
			  (unsigned long)dev, mode);
268

269 270 271 272 273
	status = dquot_initialize(dir);
	if (status) {
		mlog_errno(status);
		return status;
	}
274

275 276 277
	/* get our super block */
	osb = OCFS2_SB(dir->i_sb);

278
	status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
279 280 281 282 283 284
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		return status;
	}

Mark Fasheh's avatar
Mark Fasheh committed
285
	if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
286 287 288 289
		status = -EMLINK;
		goto leave;
	}

290
	dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Mark Fasheh's avatar
Mark Fasheh committed
291
	if (!ocfs2_read_links_count(dirfe)) {
292 293 294 295 296 297 298 299 300 301 302 303 304
		/* can't make a file in a deleted directory. */
		status = -ENOENT;
		goto leave;
	}

	status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
					   dentry->d_name.len);
	if (status)
		goto leave;

	/* get a spot inside the dir. */
	status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
					      dentry->d_name.name,
305
					      dentry->d_name.len, &lookup);
306 307 308 309 310 311
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

	/* reserve an inode spot */
312
	status = ocfs2_reserve_new_inode(osb, &inode_ac);
313 314 315 316 317 318
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto leave;
	}

319
	inode = ocfs2_get_init_inode(dir, mode);
320 321 322
	if (IS_ERR(inode)) {
		status = PTR_ERR(inode);
		inode = NULL;
323 324 325 326
		mlog_errno(status);
		goto leave;
	}

327
	/* get security xattr */
328
	status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
329 330 331 332 333 334 335 336 337
	if (status) {
		if (status == -EOPNOTSUPP)
			si.enable = 0;
		else {
			mlog_errno(status);
			goto leave;
		}
	}

338 339
	/* calculate meta data/clusters for setting security and acl xattr */
	status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
340 341
				       &si, &want_clusters,
				       &xattr_credits, &want_meta);
342 343 344
	if (status < 0) {
		mlog_errno(status);
		goto leave;
345 346
	}

347
	/* Reserve a cluster if creating an extent based directory. */
348
	if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
349 350
		want_clusters += 1;

351
		/* Dir indexing requires extra space as well */
352
		if (ocfs2_supports_indexed_dirs(osb))
353 354 355 356 357 358 359 360 361 362
			want_meta++;
	}

	status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac);
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto leave;
	}

363 364 365 366 367 368 369
	status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto leave;
	}

370
	status = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
371 372 373 374 375
	if (status) {
		mlog_errno(status);
		goto leave;
	}

376 377 378
	handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
							    S_ISDIR(mode),
							    xattr_credits));
379 380 381 382 383 384 385
	if (IS_ERR(handle)) {
		status = PTR_ERR(handle);
		handle = NULL;
		mlog_errno(status);
		goto leave;
	}

386 387 388 389
	/* Starting to change things, restart is no longer possible. */
	ocfs2_block_signals(&oldset);
	did_block_signals = 1;

390 391
	status = dquot_alloc_inode(inode);
	if (status)
392 393 394
		goto leave;
	did_quota_inode = 1;

395
	/* do the real work now. */
396
	status = ocfs2_mknod_locked(osb, dir, inode, dev,
397
				    &new_fe_bh, parent_fe_bh, handle,
398
				    inode_ac);
399 400 401 402 403 404 405
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

	if (S_ISDIR(mode)) {
		status = ocfs2_fill_new_dir(osb, handle, dir, inode,
406
					    new_fe_bh, data_ac, meta_ac);
407 408 409 410 411
		if (status < 0) {
			mlog_errno(status);
			goto leave;
		}

412 413
		status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
						 parent_fe_bh,
414
						 OCFS2_JOURNAL_ACCESS_WRITE);
415 416 417 418
		if (status < 0) {
			mlog_errno(status);
			goto leave;
		}
Mark Fasheh's avatar
Mark Fasheh committed
419
		ocfs2_add_links_count(dirfe, 1);
420
		ocfs2_journal_dirty(handle, parent_fe_bh);
421
		inc_nlink(dir);
422 423
	}

424 425 426 427 428 429 430 431 432 433 434
	if (default_acl) {
		status = ocfs2_set_acl(handle, inode, new_fe_bh,
				       ACL_TYPE_DEFAULT, default_acl,
				       meta_ac, data_ac);
	}
	if (!status && acl) {
		status = ocfs2_set_acl(handle, inode, new_fe_bh,
				       ACL_TYPE_ACCESS, acl,
				       meta_ac, data_ac);
	}

435 436 437 438 439
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

440 441
	if (si.enable) {
		status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
442
						 meta_ac, data_ac);
443 444 445 446 447 448
		if (status < 0) {
			mlog_errno(status);
			goto leave;
		}
	}

449 450 451 452 453 454 455 456
	/*
	 * Do this before adding the entry to the directory. We add
	 * also set d_op after success so that ->d_iput() will cleanup
	 * the dentry lock even if ocfs2_add_entry() fails below.
	 */
	status = ocfs2_dentry_attach_lock(dentry, inode,
					  OCFS2_I(dir)->ip_blkno);
	if (status) {
457 458 459 460
		mlog_errno(status);
		goto leave;
	}

461 462
	dl = dentry->d_fsdata;

463 464 465 466
	status = ocfs2_add_entry(handle, dentry, inode,
				 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
				 &lookup);
	if (status < 0) {
467 468 469 470
		mlog_errno(status);
		goto leave;
	}

471 472 473 474
	insert_inode_hash(inode);
	d_instantiate(dentry, inode);
	status = 0;
leave:
475 476 477 478
	if (default_acl)
		posix_acl_release(default_acl);
	if (acl)
		posix_acl_release(acl);
479
	if (status < 0 && did_quota_inode)
480
		dquot_free_inode(inode);
481
	if (handle)
482
		ocfs2_commit_trans(osb, handle);
483

484
	ocfs2_inode_unlock(dir, 1);
485 486
	if (did_block_signals)
		ocfs2_unblock_signals(&oldset);
487

488 489
	brelse(new_fe_bh);
	brelse(parent_fe_bh);
490
	kfree(si.value);
491

492 493
	ocfs2_free_dir_lookup_result(&lookup);

494 495 496 497 498 499
	if (inode_ac)
		ocfs2_free_alloc_context(inode_ac);

	if (data_ac)
		ocfs2_free_alloc_context(data_ac);

500 501
	if (meta_ac)
		ocfs2_free_alloc_context(meta_ac);
502

503 504 505 506 507 508
	/*
	 * We should call iput after the i_mutex of the bitmap been
	 * unlocked in ocfs2_free_alloc_context, or the
	 * ocfs2_delete_inode will mutex_lock again.
	 */
	if ((status < 0) && inode) {
509 510 511
		if (dl)
			ocfs2_cleanup_add_entry_failure(osb, dentry, inode);

512 513 514 515 516
		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
		clear_nlink(inode);
		iput(inode);
	}

Tao Ma's avatar
Tao Ma committed
517 518
	if (status)
		mlog_errno(status);
519 520 521 522

	return status;
}

523 524 525 526 527 528 529 530
static int __ocfs2_mknod_locked(struct inode *dir,
				struct inode *inode,
				dev_t dev,
				struct buffer_head **new_fe_bh,
				struct buffer_head *parent_fe_bh,
				handle_t *handle,
				struct ocfs2_alloc_context *inode_ac,
				u64 fe_blkno, u64 suballoc_loc, u16 suballoc_bit)
531 532
{
	int status = 0;
533
	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
534 535
	struct ocfs2_dinode *fe = NULL;
	struct ocfs2_extent_list *fel;
536
	u16 feat;
537
	struct ocfs2_inode_info *oi = OCFS2_I(inode);
538 539 540 541 542 543 544 545 546 547 548 549 550 551

	*new_fe_bh = NULL;

	/* populate as many fields early on as possible - many of
	 * these are used by the support functions here and in
	 * callers. */
	inode->i_ino = ino_from_blkno(osb->sb, fe_blkno);
	OCFS2_I(inode)->ip_blkno = fe_blkno;
	spin_lock(&osb->osb_lock);
	inode->i_generation = osb->s_next_generation++;
	spin_unlock(&osb->osb_lock);

	*new_fe_bh = sb_getblk(osb->sb, fe_blkno);
	if (!*new_fe_bh) {
552
		status = -ENOMEM;
553 554 555
		mlog_errno(status);
		goto leave;
	}
556
	ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
557

558 559
	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
					 *new_fe_bh,
560
					 OCFS2_JOURNAL_ACCESS_CREATE);
561 562 563 564 565 566 567 568 569 570 571
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

	fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data;
	memset(fe, 0, osb->sb->s_blocksize);

	fe->i_generation = cpu_to_le32(inode->i_generation);
	fe->i_fs_generation = cpu_to_le32(osb->fs_generation);
	fe->i_blkno = cpu_to_le64(fe_blkno);
572
	fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
573
	fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
574
	fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
575 576
	fe->i_uid = cpu_to_le32(i_uid_read(inode));
	fe->i_gid = cpu_to_le32(i_gid_read(inode));
577 578
	fe->i_mode = cpu_to_le16(inode->i_mode);
	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
579
		fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
Mark Fasheh's avatar
Mark Fasheh committed
580 581

	ocfs2_set_links_count(fe, inode->i_nlink);
582 583 584

	fe->i_last_eb_blk = 0;
	strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
585
	fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL);
586 587 588 589 590 591
	fe->i_atime = fe->i_ctime = fe->i_mtime =
		cpu_to_le64(CURRENT_TIME.tv_sec);
	fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec =
		cpu_to_le32(CURRENT_TIME.tv_nsec);
	fe->i_dtime = 0;

592
	/*
593 594
	 * If supported, directories start with inline data. If inline
	 * isn't supported, but indexing is, we start them as indexed.
595
	 */
596
	feat = le16_to_cpu(fe->i_dyn_features);
597
	if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
598 599
		fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);

600 601
		fe->id2.i_data.id_count = cpu_to_le16(
				ocfs2_max_inline_data_with_xattr(osb->sb, fe));
602 603 604 605 606 607
	} else {
		fel = &fe->id2.i_list;
		fel->l_tree_depth = 0;
		fel->l_next_free_rec = 0;
		fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb));
	}
608

609
	ocfs2_journal_dirty(handle, *new_fe_bh);
610

611
	ocfs2_populate_inode(inode, fe, 1);
612
	ocfs2_ci_set_new(osb, INODE_CACHE(inode));
Sunil Mushran's avatar
Sunil Mushran committed
613 614 615 616 617
	if (!ocfs2_mount_local(osb)) {
		status = ocfs2_create_new_inode_locks(inode);
		if (status < 0)
			mlog_errno(status);
	}
618

619 620 621
	oi->i_sync_tid = handle->h_transaction->t_tid;
	oi->i_datasync_tid = handle->h_transaction->t_tid;

622 623 624 625 626 627 628 629
leave:
	if (status < 0) {
		if (*new_fe_bh) {
			brelse(*new_fe_bh);
			*new_fe_bh = NULL;
		}
	}

Tao Ma's avatar
Tao Ma committed
630 631
	if (status)
		mlog_errno(status);
632 633 634
	return status;
}

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
static int ocfs2_mknod_locked(struct ocfs2_super *osb,
			      struct inode *dir,
			      struct inode *inode,
			      dev_t dev,
			      struct buffer_head **new_fe_bh,
			      struct buffer_head *parent_fe_bh,
			      handle_t *handle,
			      struct ocfs2_alloc_context *inode_ac)
{
	int status = 0;
	u64 suballoc_loc, fe_blkno = 0;
	u16 suballoc_bit;

	*new_fe_bh = NULL;

	status = ocfs2_claim_new_inode(handle, dir, parent_fe_bh,
				       inode_ac, &suballoc_loc,
				       &suballoc_bit, &fe_blkno);
	if (status < 0) {
		mlog_errno(status);
		return status;
	}

658
	status = __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
659 660
				    parent_fe_bh, handle, inode_ac,
				    fe_blkno, suballoc_loc, suballoc_bit);
661 662 663 664 665 666 667 668 669
	if (status < 0) {
		u64 bg_blkno = ocfs2_which_suballoc_group(fe_blkno, suballoc_bit);
		int tmp = ocfs2_free_suballoc_bits(handle, inode_ac->ac_inode,
				inode_ac->ac_bh, suballoc_bit, bg_blkno, 1);
		if (tmp)
			mlog_errno(tmp);
	}

	return status;
670 671
}

672 673
static int ocfs2_mkdir(struct inode *dir,
		       struct dentry *dentry,
674
		       umode_t mode)
675 676 677
{
	int ret;

Tao Ma's avatar
Tao Ma committed
678 679
	trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name,
			  OCFS2_I(dir)->ip_blkno, mode);
680
	ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
Tao Ma's avatar
Tao Ma committed
681 682
	if (ret)
		mlog_errno(ret);
683 684 685 686 687 688

	return ret;
}

static int ocfs2_create(struct inode *dir,
			struct dentry *dentry,
Al Viro's avatar
Al Viro committed
689
			umode_t mode,
Al Viro's avatar
Al Viro committed
690
			bool excl)
691 692 693
{
	int ret;

Tao Ma's avatar
Tao Ma committed
694 695
	trace_ocfs2_create(dir, dentry, dentry->d_name.len, dentry->d_name.name,
			   (unsigned long long)OCFS2_I(dir)->ip_blkno, mode);
696
	ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0);
Tao Ma's avatar
Tao Ma committed
697 698
	if (ret)
		mlog_errno(ret);
699 700 701 702 703 704 705 706

	return ret;
}

static int ocfs2_link(struct dentry *old_dentry,
		      struct inode *dir,
		      struct dentry *dentry)
{
707
	handle_t *handle;
708 709
	struct inode *inode = d_inode(old_dentry);
	struct inode *old_dir = d_inode(old_dentry->d_parent);
710 711
	int err;
	struct buffer_head *fe_bh = NULL;
712
	struct buffer_head *old_dir_bh = NULL;
713 714 715
	struct buffer_head *parent_fe_bh = NULL;
	struct ocfs2_dinode *fe = NULL;
	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
716
	struct ocfs2_dir_lookup_result lookup = { NULL, };
717
	sigset_t oldset;
718
	u64 old_de_ino;
719

Tao Ma's avatar
Tao Ma committed
720 721 722
	trace_ocfs2_link((unsigned long long)OCFS2_I(inode)->ip_blkno,
			 old_dentry->d_name.len, old_dentry->d_name.name,
			 dentry->d_name.len, dentry->d_name.name);
723

724 725
	if (S_ISDIR(inode->i_mode))
		return -EPERM;
726

727 728 729 730 731
	err = dquot_initialize(dir);
	if (err) {
		mlog_errno(err);
		return err;
	}
732

733 734
	err = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
			&parent_fe_bh, dir, 0);
735 736 737
	if (err < 0) {
		if (err != -ENOENT)
			mlog_errno(err);
738
		return err;
739 740
	}

741 742 743 744 745 746 747 748 749 750 751 752 753
	/* make sure both dirs have bhs
	 * get an extra ref on old_dir_bh if old==new */
	if (!parent_fe_bh) {
		if (old_dir_bh) {
			parent_fe_bh = old_dir_bh;
			get_bh(parent_fe_bh);
		} else {
			mlog(ML_ERROR, "%s: no old_dir_bh!\n", osb->uuid_str);
			err = -EIO;
			goto out;
		}
	}

754 755
	if (!dir->i_nlink) {
		err = -ENOENT;
756
		goto out;
757 758
	}

759
	err = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
			old_dentry->d_name.len, &old_de_ino);
	if (err) {
		err = -ENOENT;
		goto out;
	}

	/*
	 * Check whether another node removed the source inode while we
	 * were in the vfs.
	 */
	if (old_de_ino != OCFS2_I(inode)->ip_blkno) {
		err = -ENOENT;
		goto out;
	}

775 776 777
	err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
					dentry->d_name.len);
	if (err)
778
		goto out;
779 780 781

	err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
					   dentry->d_name.name,
782
					   dentry->d_name.len, &lookup);
783 784
	if (err < 0) {
		mlog_errno(err);
785
		goto out;
786 787
	}

788
	err = ocfs2_inode_lock(inode, &fe_bh, 1);
789 790 791
	if (err < 0) {
		if (err != -ENOENT)
			mlog_errno(err);
792
		goto out;
793 794 795
	}

	fe = (struct ocfs2_dinode *) fe_bh->b_data;
Mark Fasheh's avatar
Mark Fasheh committed
796
	if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
797
		err = -EMLINK;
798
		goto out_unlock_inode;
799 800
	}

801
	handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
802 803 804 805
	if (IS_ERR(handle)) {
		err = PTR_ERR(handle);
		handle = NULL;
		mlog_errno(err);
806
		goto out_unlock_inode;
807 808
	}

809 810 811
	/* Starting to change things, restart is no longer possible. */
	ocfs2_block_signals(&oldset);

812
	err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
813
				      OCFS2_JOURNAL_ACCESS_WRITE);
814 815
	if (err < 0) {
		mlog_errno(err);
816
		goto out_commit;
817 818
	}

819
	inc_nlink(inode);
820
	inode->i_ctime = CURRENT_TIME;
Mark Fasheh's avatar
Mark Fasheh committed
821
	ocfs2_set_links_count(fe, inode->i_nlink);
822 823
	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
824
	ocfs2_journal_dirty(handle, fe_bh);
825 826 827

	err = ocfs2_add_entry(handle, dentry, inode,
			      OCFS2_I(inode)->ip_blkno,
828
			      parent_fe_bh, &lookup);
829
	if (err) {
Mark Fasheh's avatar
Mark Fasheh committed
830
		ocfs2_add_links_count(fe, -1);
831
		drop_nlink(inode);
832
		mlog_errno(err);
833
		goto out_commit;
834 835
	}

836
	err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
837 838
	if (err) {
		mlog_errno(err);
839
		goto out_commit;
840 841
	}

Al Viro's avatar
Al Viro committed
842
	ihold(inode);
843
	d_instantiate(dentry, inode);
844 845

out_commit:
846
	ocfs2_commit_trans(osb, handle);
847
	ocfs2_unblock_signals(&oldset);
848
out_unlock_inode:
849
	ocfs2_inode_unlock(inode, 1);
850 851

out:
852
	ocfs2_double_unlock(old_dir, dir);
853

854 855
	brelse(fe_bh);
	brelse(parent_fe_bh);
856
	brelse(old_dir_bh);
857

858 859
	ocfs2_free_dir_lookup_result(&lookup);

Tao Ma's avatar
Tao Ma committed
860 861
	if (err)
		mlog_errno(err);
862 863 864 865

	return err;
}

866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
/*
 * Takes and drops an exclusive lock on the given dentry. This will
 * force other nodes to drop it.
 */
static int ocfs2_remote_dentry_delete(struct dentry *dentry)
{
	int ret;

	ret = ocfs2_dentry_lock(dentry, 1);
	if (ret)
		mlog_errno(ret);
	else
		ocfs2_dentry_unlock(dentry, 1);

	return ret;
}

883
static inline int ocfs2_inode_is_unlinkable(struct inode *inode)
884 885 886 887 888 889 890 891 892 893 894 895
{
	if (S_ISDIR(inode->i_mode)) {
		if (inode->i_nlink == 2)
			return 1;
		return 0;
	}

	if (inode->i_nlink == 1)
		return 1;
	return 0;
}

896 897 898 899
static int ocfs2_unlink(struct inode *dir,
			struct dentry *dentry)
{
	int status;
900
	int child_locked = 0;
901
	bool is_unlinkable = false;
902
	struct inode *inode = d_inode(dentry);
903
	struct inode *orphan_dir = NULL;
904 905 906 907 908
	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
	u64 blkno;
	struct ocfs2_dinode *fe = NULL;
	struct buffer_head *fe_bh = NULL;
	struct buffer_head *parent_node_bh = NULL;
909
	handle_t *handle = NULL;
910
	char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
911 912
	struct ocfs2_dir_lookup_result lookup = { NULL, };
	struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
913

Tao Ma's avatar
Tao Ma committed
914 915 916 917
	trace_ocfs2_unlink(dir, dentry, dentry->d_name.len,
			   dentry->d_name.name,
			   (unsigned long long)OCFS2_I(dir)->ip_blkno,
			   (unsigned long long)OCFS2_I(inode)->ip_blkno);
918

919 920 921 922 923
	status = dquot_initialize(dir);
	if (status) {
		mlog_errno(status);
		return status;
	}
924

925
	BUG_ON(d_inode(dentry->d_parent) != dir);
926

Tao Ma's avatar
Tao Ma committed
927
	if (inode == osb->root_inode)
928
		return -EPERM;
929

Jan Kara's avatar
Jan Kara committed
930 931
	status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
					 OI_LS_PARENT);
932 933 934
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
935
		return status;
936 937 938
	}

	status = ocfs2_find_files_on_disk(dentry->d_name.name,
939 940
					  dentry->d_name.len, &blkno, dir,
					  &lookup);
941 942 943 944 945 946 947 948 949
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		goto leave;
	}

	if (OCFS2_I(inode)->ip_blkno != blkno) {
		status = -ENOENT;

Tao Ma's avatar
Tao Ma committed
950 951 952 953
		trace_ocfs2_unlink_noent(
				(unsigned long long)OCFS2_I(inode)->ip_blkno,
				(unsigned long long)blkno,
				OCFS2_I(inode)->ip_flags);
954 955 956
		goto leave;
	}

957
	status = ocfs2_inode_lock(inode, &fe_bh, 1);
958 959 960 961 962
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		goto leave;
	}
963
	child_locked = 1;
964 965

	if (S_ISDIR(inode->i_mode)) {
966
		if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
967 968 969 970 971
			status = -ENOTEMPTY;
			goto leave;
		}
	}

972
	status = ocfs2_remote_dentry_delete(dentry);
973
	if (status < 0) {
974
		/* This remote delete should succeed under all normal
975 976 977 978 979
		 * circumstances. */
		mlog_errno(status);
		goto leave;
	}

980
	if (ocfs2_inode_is_unlinkable(inode)) {
981 982
		status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
						  OCFS2_I(inode)->ip_blkno,
983 984
						  orphan_name, &orphan_insert,
						  false);
Mark Fasheh's avatar