dcache.c 12.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * dcache.c
 *
 * dentry cache handling code
 *
 * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
 */

#include <linux/fs.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/namei.h>

#include <cluster/masklog.h>

#include "ocfs2.h"

#include "alloc.h"
#include "dcache.h"
Mark Fasheh's avatar
Mark Fasheh committed
23
#include "dlmglue.h"
24 25
#include "file.h"
#include "inode.h"
Tao Ma's avatar
Tao Ma committed
26
#include "ocfs2_trace.h"
27

Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
28 29 30
void ocfs2_dentry_attach_gen(struct dentry *dentry)
{
	unsigned long gen =
31 32
		OCFS2_I(d_inode(dentry->d_parent))->ip_dir_lock_gen;
	BUG_ON(d_inode(dentry));
Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
33 34 35
	dentry->d_fsdata = (void *)gen;
}

Mark Fasheh's avatar
Mark Fasheh committed
36

37
static int ocfs2_dentry_revalidate(struct dentry *dentry, unsigned int flags)
38
{
39
	struct inode *inode;
40
	int ret = 0;    /* if all else fails, just return false */
41 42
	struct ocfs2_super *osb;

43
	if (flags & LOOKUP_RCU)
44 45
		return -ECHILD;

46
	inode = d_inode(dentry);
47
	osb = OCFS2_SB(dentry->d_sb);
48

Tao Ma's avatar
Tao Ma committed
49 50
	trace_ocfs2_dentry_revalidate(dentry, dentry->d_name.len,
				      dentry->d_name.name);
51

Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
52 53 54 55
	/* For a negative dentry -
	 * check the generation number of the parent and compare with the
	 * one stored in the inode.
	 */
56
	if (inode == NULL) {
Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
57
		unsigned long gen = (unsigned long) dentry->d_fsdata;
58 59
		unsigned long pgen;
		spin_lock(&dentry->d_lock);
60
		pgen = OCFS2_I(d_inode(dentry->d_parent))->ip_dir_lock_gen;
61
		spin_unlock(&dentry->d_lock);
Tao Ma's avatar
Tao Ma committed
62 63 64
		trace_ocfs2_dentry_revalidate_negative(dentry->d_name.len,
						       dentry->d_name.name,
						       pgen, gen);
Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
65 66 67
		if (gen != pgen)
			goto bail;
		goto valid;
68 69 70 71
	}

	BUG_ON(!osb);

Mark Fasheh's avatar
Mark Fasheh committed
72 73 74 75 76 77
	if (inode == osb->root_inode || is_bad_inode(inode))
		goto bail;

	spin_lock(&OCFS2_I(inode)->ip_lock);
	/* did we or someone else delete this inode? */
	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
78
		spin_unlock(&OCFS2_I(inode)->ip_lock);
Tao Ma's avatar
Tao Ma committed
79 80
		trace_ocfs2_dentry_revalidate_delete(
				(unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh's avatar
Mark Fasheh committed
81 82 83
		goto bail;
	}
	spin_unlock(&OCFS2_I(inode)->ip_lock);
84

Mark Fasheh's avatar
Mark Fasheh committed
85 86 87 88 89
	/*
	 * We don't need a cluster lock to test this because once an
	 * inode nlink hits zero, it never goes back.
	 */
	if (inode->i_nlink == 0) {
Tao Ma's avatar
Tao Ma committed
90 91 92
		trace_ocfs2_dentry_revalidate_orphaned(
			(unsigned long long)OCFS2_I(inode)->ip_blkno,
			S_ISDIR(inode->i_mode));
Mark Fasheh's avatar
Mark Fasheh committed
93
		goto bail;
94 95
	}

96 97 98 99 100
	/*
	 * If the last lookup failed to create dentry lock, let us
	 * redo it.
	 */
	if (!dentry->d_fsdata) {
Tao Ma's avatar
Tao Ma committed
101 102
		trace_ocfs2_dentry_revalidate_nofsdata(
				(unsigned long long)OCFS2_I(inode)->ip_blkno);
103 104 105
		goto bail;
	}

Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
106
valid:
107 108 109
	ret = 1;

bail:
Tao Ma's avatar
Tao Ma committed
110
	trace_ocfs2_dentry_revalidate_ret(ret);
111 112 113
	return ret;
}

Mark Fasheh's avatar
Mark Fasheh committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
static int ocfs2_match_dentry(struct dentry *dentry,
			      u64 parent_blkno,
			      int skip_unhashed)
{
	struct inode *parent;

	/*
	 * ocfs2_lookup() does a d_splice_alias() _before_ attaching
	 * to the lock data, so we skip those here, otherwise
	 * ocfs2_dentry_attach_lock() will get its original dentry
	 * back.
	 */
	if (!dentry->d_fsdata)
		return 0;

	if (!dentry->d_parent)
		return 0;

	if (skip_unhashed && d_unhashed(dentry))
		return 0;

135
	parent = d_inode(dentry->d_parent);
Mark Fasheh's avatar
Mark Fasheh committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149
	/* Negative parent dentry? */
	if (!parent)
		return 0;

	/* Name is in a different directory. */
	if (OCFS2_I(parent)->ip_blkno != parent_blkno)
		return 0;

	return 1;
}

/*
 * Walk the inode alias list, and find a dentry which has a given
 * parent. ocfs2_dentry_attach_lock() wants to find _any_ alias as it
150 151 152
 * is looking for a dentry_lock reference. The downconvert thread is
 * looking to unhash aliases, so we allow it to skip any that already
 * have that property.
Mark Fasheh's avatar
Mark Fasheh committed
153 154 155 156 157
 */
struct dentry *ocfs2_find_local_alias(struct inode *inode,
				      u64 parent_blkno,
				      int skip_unhashed)
{
158
	struct dentry *dentry;
Mark Fasheh's avatar
Mark Fasheh committed
159

160
	spin_lock(&inode->i_lock);
161
	hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Nick Piggin's avatar
Nick Piggin committed
162
		spin_lock(&dentry->d_lock);
Mark Fasheh's avatar
Mark Fasheh committed
163
		if (ocfs2_match_dentry(dentry, parent_blkno, skip_unhashed)) {
Tao Ma's avatar
Tao Ma committed
164 165
			trace_ocfs2_find_local_alias(dentry->d_name.len,
						     dentry->d_name.name);
Mark Fasheh's avatar
Mark Fasheh committed
166

167
			dget_dlock(dentry);
Nick Piggin's avatar
Nick Piggin committed
168
			spin_unlock(&dentry->d_lock);
169 170
			spin_unlock(&inode->i_lock);
			return dentry;
Mark Fasheh's avatar
Mark Fasheh committed
171
		}
Nick Piggin's avatar
Nick Piggin committed
172
		spin_unlock(&dentry->d_lock);
Mark Fasheh's avatar
Mark Fasheh committed
173
	}
174
	spin_unlock(&inode->i_lock);
175
	return NULL;
Mark Fasheh's avatar
Mark Fasheh committed
176 177
}

Mark Fasheh's avatar
Mark Fasheh committed
178 179
DEFINE_SPINLOCK(dentry_attach_lock);

Mark Fasheh's avatar
Mark Fasheh committed
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 208 209 210 211 212 213
/*
 * Attach this dentry to a cluster lock.
 *
 * Dentry locks cover all links in a given directory to a particular
 * inode. We do this so that ocfs2 can build a lock name which all
 * nodes in the cluster can agree on at all times. Shoving full names
 * in the cluster lock won't work due to size restrictions. Covering
 * links inside of a directory is a good compromise because it still
 * allows us to use the parent directory lock to synchronize
 * operations.
 *
 * Call this function with the parent dir semaphore and the parent dir
 * cluster lock held.
 *
 * The dir semaphore will protect us from having to worry about
 * concurrent processes on our node trying to attach a lock at the
 * same time.
 *
 * The dir cluster lock (held at either PR or EX mode) protects us
 * from unlink and rename on other nodes.
 *
 * A dput() can happen asynchronously due to pruning, so we cover
 * attaching and detaching the dentry lock with a
 * dentry_attach_lock.
 *
 * A node which has done lookup on a name retains a protected read
 * lock until final dput. If the user requests and unlink or rename,
 * the protected read is upgraded to an exclusive lock. Other nodes
 * who have seen the dentry will then be informed that they need to
 * downgrade their lock, which will involve d_delete on the
 * dentry. This happens in ocfs2_dentry_convert_worker().
 */
int ocfs2_dentry_attach_lock(struct dentry *dentry,
			     struct inode *inode,
214
			     u64 parent_blkno)
Mark Fasheh's avatar
Mark Fasheh committed
215 216 217 218 219
{
	int ret;
	struct dentry *alias;
	struct ocfs2_dentry_lock *dl = dentry->d_fsdata;

Tao Ma's avatar
Tao Ma committed
220 221
	trace_ocfs2_dentry_attach_lock(dentry->d_name.len, dentry->d_name.name,
				       (unsigned long long)parent_blkno, dl);
Mark Fasheh's avatar
Mark Fasheh committed
222 223 224 225 226 227 228 229 230 231

	/*
	 * Negative dentry. We ignore these for now.
	 *
	 * XXX: Could we can improve ocfs2_dentry_revalidate() by
	 * tracking these?
	 */
	if (!inode)
		return 0;

232
	if (d_really_is_negative(dentry) && dentry->d_fsdata) {
Goldwyn Rodrigues's avatar
Goldwyn Rodrigues committed
233 234 235 236 237
		/* Converting a negative dentry to positive
		   Clear dentry->d_fsdata */
		dentry->d_fsdata = dl = NULL;
	}

Mark Fasheh's avatar
Mark Fasheh committed
238 239
	if (dl) {
		mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno,
Al Viro's avatar
Al Viro committed
240 241
				" \"%pd\": old parent: %llu, new: %llu\n",
				dentry,
Mark Fasheh's avatar
Mark Fasheh committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
				(unsigned long long)parent_blkno,
				(unsigned long long)dl->dl_parent_blkno);
		return 0;
	}

	alias = ocfs2_find_local_alias(inode, parent_blkno, 0);
	if (alias) {
		/*
		 * Great, an alias exists, which means we must have a
		 * dentry lock already. We can just grab the lock off
		 * the alias and add it to the list.
		 *
		 * We're depending here on the fact that this dentry
		 * was found and exists in the dcache and so must have
		 * a reference to the dentry_lock because we can't
		 * race creates. Final dput() cannot happen on it
		 * since we have it pinned, so our reference is safe.
		 */
		dl = alias->d_fsdata;
261
		mlog_bug_on_msg(!dl, "parent %llu, ino %llu\n",
Mark Fasheh's avatar
Mark Fasheh committed
262
				(unsigned long long)parent_blkno,
263
				(unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh's avatar
Mark Fasheh committed
264 265

		mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno,
Al Viro's avatar
Al Viro committed
266 267
				" \"%pd\": old parent: %llu, new: %llu\n",
				dentry,
Mark Fasheh's avatar
Mark Fasheh committed
268 269 270
				(unsigned long long)parent_blkno,
				(unsigned long long)dl->dl_parent_blkno);

Tao Ma's avatar
Tao Ma committed
271 272 273
		trace_ocfs2_dentry_attach_lock_found(dl->dl_lockres.l_name,
				(unsigned long long)parent_blkno,
				(unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh's avatar
Mark Fasheh committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

		goto out_attach;
	}

	/*
	 * There are no other aliases
	 */
	dl = kmalloc(sizeof(*dl), GFP_NOFS);
	if (!dl) {
		ret = -ENOMEM;
		mlog_errno(ret);
		return ret;
	}

	dl->dl_count = 0;
	/*
	 * Does this have to happen below, for all attaches, in case
291
	 * the struct inode gets blown away by the downconvert thread?
Mark Fasheh's avatar
Mark Fasheh committed
292 293 294 295 296 297 298
	 */
	dl->dl_inode = igrab(inode);
	dl->dl_parent_blkno = parent_blkno;
	ocfs2_dentry_lock_res_init(dl, parent_blkno, inode);

out_attach:
	spin_lock(&dentry_attach_lock);
299 300 301 302 303 304 305 306 307 308 309 310
	if (unlikely(dentry->d_fsdata && !alias)) {
		/* d_fsdata is set by a racing thread which is doing
		 * the same thing as this thread is doing. Leave the racing
		 * thread going ahead and we return here.
		 */
		spin_unlock(&dentry_attach_lock);
		iput(dl->dl_inode);
		ocfs2_lock_res_free(&dl->dl_lockres);
		kfree(dl);
		return 0;
	}

Mark Fasheh's avatar
Mark Fasheh committed
311 312 313 314 315 316 317 318 319 320
	dentry->d_fsdata = dl;
	dl->dl_count++;
	spin_unlock(&dentry_attach_lock);

	/*
	 * This actually gets us our PRMODE level lock. From now on,
	 * we'll have a notification if one of these names is
	 * destroyed on another node.
	 */
	ret = ocfs2_dentry_lock(dentry, 0);
321 322 323
	if (!ret)
		ocfs2_dentry_unlock(dentry, 0);
	else
Mark Fasheh's avatar
Mark Fasheh committed
324 325
		mlog_errno(ret);

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	/*
	 * In case of error, manually free the allocation and do the iput().
	 * We need to do this because error here means no d_instantiate(),
	 * which means iput() will not be called during dput(dentry).
	 */
	if (ret < 0 && !alias) {
		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);
	}

Mark Fasheh's avatar
Mark Fasheh committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	dput(alias);

	return ret;
}

/*
 * ocfs2_dentry_iput() and friends.
 *
 * At this point, our particular dentry is detached from the inodes
 * alias list, so there's no way that the locking code can find it.
 *
 * The interesting stuff happens when we determine that our lock needs
 * to go away because this is the last subdir alias in the
 * system. This function needs to handle a couple things:
 *
 * 1) Synchronizing lock shutdown with the downconvert threads. This
 *    is already handled for us via the lockres release drop function
 *    called in ocfs2_release_dentry_lock()
 *
 * 2) A race may occur when we're doing our lock shutdown and
 *    another process wants to create a new dentry lock. Right now we
 *    let them race, which means that for a very short while, this
 *    node might have two locks on a lock resource. This should be a
 *    problem though because one of them is in the process of being
 *    thrown out.
 */
static void ocfs2_drop_dentry_lock(struct ocfs2_super *osb,
				   struct ocfs2_dentry_lock *dl)
{
370
	iput(dl->dl_inode);
Mark Fasheh's avatar
Mark Fasheh committed
371 372
	ocfs2_simple_drop_lockres(osb, &dl->dl_lockres);
	ocfs2_lock_res_free(&dl->dl_lockres);
373
	kfree(dl);
Mark Fasheh's avatar
Mark Fasheh committed
374 375 376 377 378
}

void ocfs2_dentry_lock_put(struct ocfs2_super *osb,
			   struct ocfs2_dentry_lock *dl)
{
379
	int unlock = 0;
Mark Fasheh's avatar
Mark Fasheh committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

	BUG_ON(dl->dl_count == 0);

	spin_lock(&dentry_attach_lock);
	dl->dl_count--;
	unlock = !dl->dl_count;
	spin_unlock(&dentry_attach_lock);

	if (unlock)
		ocfs2_drop_dentry_lock(osb, dl);
}

static void ocfs2_dentry_iput(struct dentry *dentry, struct inode *inode)
{
	struct ocfs2_dentry_lock *dl = dentry->d_fsdata;

396 397 398 399 400 401 402 403 404 405 406
	if (!dl) {
		/*
		 * No dentry lock is ok if we're disconnected or
		 * unhashed.
		 */
		if (!(dentry->d_flags & DCACHE_DISCONNECTED) &&
		    !d_unhashed(dentry)) {
			unsigned long long ino = 0ULL;
			if (inode)
				ino = (unsigned long long)OCFS2_I(inode)->ip_blkno;
			mlog(ML_ERROR, "Dentry is missing cluster lock. "
Al Viro's avatar
Al Viro committed
407 408
			     "inode: %llu, d_flags: 0x%x, d_name: %pd\n",
			     ino, dentry->d_flags, dentry);
409
		}
Mark Fasheh's avatar
Mark Fasheh committed
410 411

		goto out;
412
	}
Mark Fasheh's avatar
Mark Fasheh committed
413

Al Viro's avatar
Al Viro committed
414 415
	mlog_bug_on_msg(dl->dl_count == 0, "dentry: %pd, count: %u\n",
			dentry, dl->dl_count);
Mark Fasheh's avatar
Mark Fasheh committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

	ocfs2_dentry_lock_put(OCFS2_SB(dentry->d_sb), dl);

out:
	iput(inode);
}

/*
 * d_move(), but keep the locks in sync.
 *
 * When we are done, "dentry" will have the parent dir and name of
 * "target", which will be thrown away.
 *
 * We manually update the lock of "dentry" if need be.
 *
 * "target" doesn't have it's dentry lock touched - we allow the later
 * dput() to handle this for us.
 *
 * This is called during ocfs2_rename(), while holding parent
 * directory locks. The dentries have already been deleted on other
 * nodes via ocfs2_remote_dentry_delete().
 *
438
 * Normally, the VFS handles the d_move() for the file system, after
Mark Fasheh's avatar
Mark Fasheh committed
439 440 441 442 443 444 445 446
 * the ->rename() callback. OCFS2 wants to handle this internally, so
 * the new lock can be created atomically with respect to the cluster.
 */
void ocfs2_dentry_move(struct dentry *dentry, struct dentry *target,
		       struct inode *old_dir, struct inode *new_dir)
{
	int ret;
	struct ocfs2_super *osb = OCFS2_SB(old_dir->i_sb);
447
	struct inode *inode = d_inode(dentry);
Mark Fasheh's avatar
Mark Fasheh committed
448 449 450 451 452 453 454 455

	/*
	 * Move within the same directory, so the actual lock info won't
	 * change.
	 *
	 * XXX: Is there any advantage to dropping the lock here?
	 */
	if (old_dir == new_dir)
456
		goto out_move;
Mark Fasheh's avatar
Mark Fasheh committed
457 458 459 460

	ocfs2_dentry_lock_put(osb, dentry->d_fsdata);

	dentry->d_fsdata = NULL;
461
	ret = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(new_dir)->ip_blkno);
Mark Fasheh's avatar
Mark Fasheh committed
462 463
	if (ret)
		mlog_errno(ret);
464 465 466

out_move:
	d_move(dentry, target);
Mark Fasheh's avatar
Mark Fasheh committed
467 468
}

Al Viro's avatar
Al Viro committed
469
const struct dentry_operations ocfs2_dentry_ops = {
470
	.d_revalidate		= ocfs2_dentry_revalidate,
Mark Fasheh's avatar
Mark Fasheh committed
471
	.d_iput			= ocfs2_dentry_iput,
472
};