namei.c 69.9 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
/* An orphan dir name is an 8 byte value, printed as a hex string */
#define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64)))
109 110
#define OCFS2_DIO_ORPHAN_PREFIX "dio-"
#define OCFS2_DIO_ORPHAN_PREFIX_LEN 4
111 112

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

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

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

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

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

143
	inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	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);

162 163 164 165 166 167 168 169 170 171 172
	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()
		 */
173
		if (!IS_ERR_OR_NULL(ret))
174 175 176
			dentry = ret;

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

186 187 188 189
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
190
	 * the downconvert thread and have a stale dentry. */
191
	ocfs2_inode_unlock(dir, 0);
192 193 194

bail:

Tao Ma's avatar
Tao Ma committed
195
	trace_ocfs2_lookup_ret(ret);
196 197 198 199

	return ret;
}

Al Viro's avatar
Al Viro committed
200
static struct inode *ocfs2_get_init_inode(struct inode *dir, umode_t mode)
201 202 203 204 205 206 207 208 209 210 211 212 213
{
	struct inode *inode;

	inode = new_inode(dir->i_sb);
	if (!inode) {
		mlog(ML_ERROR, "new_inode failed!\n");
		return NULL;
	}

	/* 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
214
		set_nlink(inode, 2);
215
	inode_init_owner(inode, dir, mode);
216
	dquot_initialize(inode);
217 218 219
	return inode;
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
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);
}

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

Tao Ma's avatar
Tao Ma committed
263 264 265
	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);
266

267
	dquot_initialize(dir);
268

269 270 271
	/* get our super block */
	osb = OCFS2_SB(dir->i_sb);

272
	status = ocfs2_inode_lock(dir, &parent_fe_bh, 1);
273 274 275 276 277 278
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		return status;
	}

Mark Fasheh's avatar
Mark Fasheh committed
279
	if (S_ISDIR(mode) && (dir->i_nlink >= ocfs2_link_max(osb))) {
280 281 282 283
		status = -EMLINK;
		goto leave;
	}

284
	dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Mark Fasheh's avatar
Mark Fasheh committed
285
	if (!ocfs2_read_links_count(dirfe)) {
286 287 288 289 290 291 292 293 294 295 296 297 298
		/* 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,
299
					      dentry->d_name.len, &lookup);
300 301 302 303 304 305
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

	/* reserve an inode spot */
306
	status = ocfs2_reserve_new_inode(osb, &inode_ac);
307 308 309 310 311 312
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto leave;
	}

313 314 315 316 317 318 319
	inode = ocfs2_get_init_inode(dir, mode);
	if (!inode) {
		status = -ENOMEM;
		mlog_errno(status);
		goto leave;
	}

320
	/* get security xattr */
321
	status = ocfs2_init_security_get(inode, dir, &dentry->d_name, &si);
322 323 324 325 326 327 328 329 330
	if (status) {
		if (status == -EOPNOTSUPP)
			si.enable = 0;
		else {
			mlog_errno(status);
			goto leave;
		}
	}

331 332
	/* calculate meta data/clusters for setting security and acl xattr */
	status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode,
333 334
				       &si, &want_clusters,
				       &xattr_credits, &want_meta);
335 336 337
	if (status < 0) {
		mlog_errno(status);
		goto leave;
338 339
	}

340
	/* Reserve a cluster if creating an extent based directory. */
341
	if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) {
342 343
		want_clusters += 1;

344
		/* Dir indexing requires extra space as well */
345
		if (ocfs2_supports_indexed_dirs(osb))
346 347 348 349 350 351 352 353 354 355
			want_meta++;
	}

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

356 357 358 359 360 361 362
	status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac);
	if (status < 0) {
		if (status != -ENOSPC)
			mlog_errno(status);
		goto leave;
	}

363 364 365 366 367 368
	status = posix_acl_create(dir, &mode, &default_acl, &acl);
	if (status) {
		mlog_errno(status);
		goto leave;
	}

369 370 371
	handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb,
							    S_ISDIR(mode),
							    xattr_credits));
372 373 374 375 376 377 378
	if (IS_ERR(handle)) {
		status = PTR_ERR(handle);
		handle = NULL;
		mlog_errno(status);
		goto leave;
	}

379 380 381 382
	/* Starting to change things, restart is no longer possible. */
	ocfs2_block_signals(&oldset);
	did_block_signals = 1;

383 384
	status = dquot_alloc_inode(inode);
	if (status)
385 386 387
		goto leave;
	did_quota_inode = 1;

388
	/* do the real work now. */
389
	status = ocfs2_mknod_locked(osb, dir, inode, dev,
390
				    &new_fe_bh, parent_fe_bh, handle,
391
				    inode_ac);
392 393 394 395 396 397 398
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

	if (S_ISDIR(mode)) {
		status = ocfs2_fill_new_dir(osb, handle, dir, inode,
399
					    new_fe_bh, data_ac, meta_ac);
400 401 402 403 404
		if (status < 0) {
			mlog_errno(status);
			goto leave;
		}

405 406
		status = ocfs2_journal_access_di(handle, INODE_CACHE(dir),
						 parent_fe_bh,
407
						 OCFS2_JOURNAL_ACCESS_WRITE);
408 409 410 411
		if (status < 0) {
			mlog_errno(status);
			goto leave;
		}
Mark Fasheh's avatar
Mark Fasheh committed
412
		ocfs2_add_links_count(dirfe, 1);
413
		ocfs2_journal_dirty(handle, parent_fe_bh);
414
		inc_nlink(dir);
415 416
	}

417 418 419 420 421 422 423 424 425 426 427
	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);
	}

428 429 430 431 432
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

433 434
	if (si.enable) {
		status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si,
435
						 meta_ac, data_ac);
436 437 438 439 440 441
		if (status < 0) {
			mlog_errno(status);
			goto leave;
		}
	}

442 443 444 445 446 447 448 449
	/*
	 * 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) {
450 451 452 453
		mlog_errno(status);
		goto leave;
	}

454 455
	dl = dentry->d_fsdata;

456 457 458 459
	status = ocfs2_add_entry(handle, dentry, inode,
				 OCFS2_I(inode)->ip_blkno, parent_fe_bh,
				 &lookup);
	if (status < 0) {
460 461 462 463
		mlog_errno(status);
		goto leave;
	}

464 465 466 467
	insert_inode_hash(inode);
	d_instantiate(dentry, inode);
	status = 0;
leave:
468 469 470 471
	if (default_acl)
		posix_acl_release(default_acl);
	if (acl)
		posix_acl_release(acl);
472
	if (status < 0 && did_quota_inode)
473
		dquot_free_inode(inode);
474
	if (handle)
475
		ocfs2_commit_trans(osb, handle);
476

477
	ocfs2_inode_unlock(dir, 1);
478 479
	if (did_block_signals)
		ocfs2_unblock_signals(&oldset);
480

481 482
	brelse(new_fe_bh);
	brelse(parent_fe_bh);
483
	kfree(si.value);
484

485 486
	ocfs2_free_dir_lookup_result(&lookup);

487 488 489 490 491 492
	if (inode_ac)
		ocfs2_free_alloc_context(inode_ac);

	if (data_ac)
		ocfs2_free_alloc_context(data_ac);

493 494
	if (meta_ac)
		ocfs2_free_alloc_context(meta_ac);
495

496 497 498 499 500 501
	/*
	 * 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) {
502 503 504
		if (dl)
			ocfs2_cleanup_add_entry_failure(osb, dentry, inode);

505 506 507 508 509
		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SKIP_ORPHAN_DIR;
		clear_nlink(inode);
		iput(inode);
	}

Tao Ma's avatar
Tao Ma committed
510 511
	if (status)
		mlog_errno(status);
512 513 514 515

	return status;
}

516 517 518 519 520 521 522 523
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)
524 525
{
	int status = 0;
526
	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
527 528
	struct ocfs2_dinode *fe = NULL;
	struct ocfs2_extent_list *fel;
529
	u16 feat;
530
	struct ocfs2_inode_info *oi = OCFS2_I(inode);
531 532 533 534 535 536 537 538 539 540 541 542 543 544

	*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) {
545
		status = -ENOMEM;
546 547 548
		mlog_errno(status);
		goto leave;
	}
549
	ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), *new_fe_bh);
550

551 552
	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
					 *new_fe_bh,
553
					 OCFS2_JOURNAL_ACCESS_CREATE);
554 555 556 557 558 559 560 561 562 563 564
	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);
565
	fe->i_suballoc_loc = cpu_to_le64(suballoc_loc);
566
	fe->i_suballoc_bit = cpu_to_le16(suballoc_bit);
567
	fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot);
568 569
	fe->i_uid = cpu_to_le32(i_uid_read(inode));
	fe->i_gid = cpu_to_le32(i_gid_read(inode));
570 571
	fe->i_mode = cpu_to_le16(inode->i_mode);
	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
572
		fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev));
Mark Fasheh's avatar
Mark Fasheh committed
573 574

	ocfs2_set_links_count(fe, inode->i_nlink);
575 576 577

	fe->i_last_eb_blk = 0;
	strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE);
578
	fe->i_flags |= cpu_to_le32(OCFS2_VALID_FL);
579 580 581 582 583 584
	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;

585
	/*
586 587
	 * If supported, directories start with inline data. If inline
	 * isn't supported, but indexing is, we start them as indexed.
588
	 */
589
	feat = le16_to_cpu(fe->i_dyn_features);
590
	if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) {
591 592
		fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL);

593 594
		fe->id2.i_data.id_count = cpu_to_le16(
				ocfs2_max_inline_data_with_xattr(osb->sb, fe));
595 596 597 598 599 600
	} 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));
	}
601

602
	ocfs2_journal_dirty(handle, *new_fe_bh);
603

604
	ocfs2_populate_inode(inode, fe, 1);
605
	ocfs2_ci_set_new(osb, INODE_CACHE(inode));
Sunil Mushran's avatar
Sunil Mushran committed
606 607 608 609 610
	if (!ocfs2_mount_local(osb)) {
		status = ocfs2_create_new_inode_locks(inode);
		if (status < 0)
			mlog_errno(status);
	}
611

612 613 614
	oi->i_sync_tid = handle->h_transaction->t_tid;
	oi->i_datasync_tid = handle->h_transaction->t_tid;

615 616 617 618 619 620 621 622
leave:
	if (status < 0) {
		if (*new_fe_bh) {
			brelse(*new_fe_bh);
			*new_fe_bh = NULL;
		}
	}

Tao Ma's avatar
Tao Ma committed
623 624
	if (status)
		mlog_errno(status);
625 626 627
	return status;
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
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;
	}

	return __ocfs2_mknod_locked(dir, inode, dev, new_fe_bh,
				    parent_fe_bh, handle, inode_ac,
				    fe_blkno, suballoc_loc, suballoc_bit);
}

656 657
static int ocfs2_mkdir(struct inode *dir,
		       struct dentry *dentry,
658
		       umode_t mode)
659 660 661
{
	int ret;

Tao Ma's avatar
Tao Ma committed
662 663
	trace_ocfs2_mkdir(dir, dentry, dentry->d_name.len, dentry->d_name.name,
			  OCFS2_I(dir)->ip_blkno, mode);
664
	ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0);
Tao Ma's avatar
Tao Ma committed
665 666
	if (ret)
		mlog_errno(ret);
667 668 669 670 671 672

	return ret;
}

static int ocfs2_create(struct inode *dir,
			struct dentry *dentry,
Al Viro's avatar
Al Viro committed
673
			umode_t mode,
Al Viro's avatar
Al Viro committed
674
			bool excl)
675 676 677
{
	int ret;

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

	return ret;
}

static int ocfs2_link(struct dentry *old_dentry,
		      struct inode *dir,
		      struct dentry *dentry)
{
691
	handle_t *handle;
692
	struct inode *inode = old_dentry->d_inode;
693
	struct inode *old_dir = old_dentry->d_parent->d_inode;
694 695
	int err;
	struct buffer_head *fe_bh = NULL;
696
	struct buffer_head *old_dir_bh = NULL;
697 698 699
	struct buffer_head *parent_fe_bh = NULL;
	struct ocfs2_dinode *fe = NULL;
	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
700
	struct ocfs2_dir_lookup_result lookup = { NULL, };
701
	sigset_t oldset;
702
	u64 old_de_ino;
703

Tao Ma's avatar
Tao Ma committed
704 705 706
	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);
707

708 709
	if (S_ISDIR(inode->i_mode))
		return -EPERM;
710

711
	dquot_initialize(dir);
712

713 714
	err = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
			&parent_fe_bh, dir, 0);
715 716 717
	if (err < 0) {
		if (err != -ENOENT)
			mlog_errno(err);
718
		return err;
719 720
	}

721 722 723 724 725 726 727 728 729 730 731 732 733
	/* 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;
		}
	}

734 735
	if (!dir->i_nlink) {
		err = -ENOENT;
736
		goto out;
737 738
	}

739
	err = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name,
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
			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;
	}

755 756 757
	err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
					dentry->d_name.len);
	if (err)
758
		goto out;
759 760 761

	err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
					   dentry->d_name.name,
762
					   dentry->d_name.len, &lookup);
763 764
	if (err < 0) {
		mlog_errno(err);
765
		goto out;
766 767
	}

768
	err = ocfs2_inode_lock(inode, &fe_bh, 1);
769 770 771
	if (err < 0) {
		if (err != -ENOENT)
			mlog_errno(err);
772
		goto out;
773 774 775
	}

	fe = (struct ocfs2_dinode *) fe_bh->b_data;
Mark Fasheh's avatar
Mark Fasheh committed
776
	if (ocfs2_read_links_count(fe) >= ocfs2_link_max(osb)) {
777
		err = -EMLINK;
778
		goto out_unlock_inode;
779 780
	}

781
	handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb));
782 783 784 785
	if (IS_ERR(handle)) {
		err = PTR_ERR(handle);
		handle = NULL;
		mlog_errno(err);
786
		goto out_unlock_inode;
787 788
	}

789 790 791
	/* Starting to change things, restart is no longer possible. */
	ocfs2_block_signals(&oldset);

792
	err = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
793
				      OCFS2_JOURNAL_ACCESS_WRITE);
794 795
	if (err < 0) {
		mlog_errno(err);
796
		goto out_commit;
797 798
	}

799
	inc_nlink(inode);
800
	inode->i_ctime = CURRENT_TIME;
Mark Fasheh's avatar
Mark Fasheh committed
801
	ocfs2_set_links_count(fe, inode->i_nlink);
802 803
	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
804
	ocfs2_journal_dirty(handle, fe_bh);
805 806 807

	err = ocfs2_add_entry(handle, dentry, inode,
			      OCFS2_I(inode)->ip_blkno,
808
			      parent_fe_bh, &lookup);
809
	if (err) {
Mark Fasheh's avatar
Mark Fasheh committed
810
		ocfs2_add_links_count(fe, -1);
811
		drop_nlink(inode);
812
		mlog_errno(err);
813
		goto out_commit;
814 815
	}

816
	err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
817 818
	if (err) {
		mlog_errno(err);
819
		goto out_commit;
820 821
	}

Al Viro's avatar
Al Viro committed
822
	ihold(inode);
823
	d_instantiate(dentry, inode);
824 825

out_commit:
826
	ocfs2_commit_trans(osb, handle);
827
	ocfs2_unblock_signals(&oldset);
828
out_unlock_inode:
829
	ocfs2_inode_unlock(inode, 1);
830 831

out:
832
	ocfs2_double_unlock(old_dir, dir);
833

834 835
	brelse(fe_bh);
	brelse(parent_fe_bh);
836
	brelse(old_dir_bh);
837

838 839
	ocfs2_free_dir_lookup_result(&lookup);

Tao Ma's avatar
Tao Ma committed
840 841
	if (err)
		mlog_errno(err);
842 843 844 845

	return err;
}

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
/*
 * 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;
}

863
static inline int ocfs2_inode_is_unlinkable(struct inode *inode)
864 865 866 867 868 869 870 871 872 873 874 875
{
	if (S_ISDIR(inode->i_mode)) {
		if (inode->i_nlink == 2)
			return 1;
		return 0;
	}

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

876 877 878 879
static int ocfs2_unlink(struct inode *dir,
			struct dentry *dentry)
{
	int status;
880
	int child_locked = 0;
881
	bool is_unlinkable = false;
882
	struct inode *inode = dentry->d_inode;
883
	struct inode *orphan_dir = NULL;
884 885 886 887 888
	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;
889
	handle_t *handle = NULL;
890
	char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
891 892
	struct ocfs2_dir_lookup_result lookup = { NULL, };
	struct ocfs2_dir_lookup_result orphan_insert = { NULL, };
893

Tao Ma's avatar
Tao Ma committed
894 895 896 897
	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);
898

899
	dquot_initialize(dir);
900

901 902
	BUG_ON(dentry->d_parent->d_inode != dir);

Tao Ma's avatar
Tao Ma committed
903
	if (inode == osb->root_inode)
904
		return -EPERM;
905

Jan Kara's avatar
Jan Kara committed
906 907
	status = ocfs2_inode_lock_nested(dir, &parent_node_bh, 1,
					 OI_LS_PARENT);
908 909 910
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
911
		return status;
912 913 914
	}

	status = ocfs2_find_files_on_disk(dentry->d_name.name,
915 916
					  dentry->d_name.len, &blkno, dir,
					  &lookup);
917 918 919 920 921 922 923 924 925
	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
926 927 928 929
		trace_ocfs2_unlink_noent(
				(unsigned long long)OCFS2_I(inode)->ip_blkno,
				(unsigned long long)blkno,
				OCFS2_I(inode)->ip_flags);
930 931 932
		goto leave;
	}

933
	status = ocfs2_inode_lock(inode, &fe_bh, 1);
934 935 936 937 938
	if (status < 0) {
		if (status != -ENOENT)
			mlog_errno(status);
		goto leave;
	}
939
	child_locked = 1;
940 941

	if (S_ISDIR(inode->i_mode)) {
942
		if (inode->i_nlink != 2 || !ocfs2_empty_dir(inode)) {
943 944 945 946 947
			status = -ENOTEMPTY;
			goto leave;
		}
	}

948
	status = ocfs2_remote_dentry_delete(dentry);
949
	if (status < 0) {
950
		/* This remote delete should succeed under all normal
951 952 953 954 955
		 * circumstances. */
		mlog_errno(status);
		goto leave;
	}

956
	if (ocfs2_inode_is_unlinkable(inode)) {
957 958
		status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
						  OCFS2_I(inode)->ip_blkno,
959 960
						  orphan_name, &orphan_insert,
						  false);
961 962 963 964
		if (status < 0) {
			mlog_errno(status);
			goto leave;
		}
965
		is_unlinkable = true;
966 967
	}

968
	handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb));
969 970 971 972 973 974 975
	if (IS_ERR(handle)) {
		status = PTR_ERR(handle);
		handle = NULL;
		mlog_errno(status);
		goto leave;
	}

976
	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
977
					 OCFS2_JOURNAL_ACCESS_WRITE);
978 979 980 981 982 983 984 985
	if (status < 0) {
		mlog_errno(status);
		goto leave;
	}

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

	/* delete the name from the parent dir */
986
	status = ocfs2_delete_entry(handle, dir, &lookup);
987 988 989 990