1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * dcache.c
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * dentry cache handling code
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/namei.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <cluster/masklog.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include "ocfs2.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "alloc.h"
22*4882a593Smuzhiyun #include "dcache.h"
23*4882a593Smuzhiyun #include "dlmglue.h"
24*4882a593Smuzhiyun #include "file.h"
25*4882a593Smuzhiyun #include "inode.h"
26*4882a593Smuzhiyun #include "ocfs2_trace.h"
27*4882a593Smuzhiyun
ocfs2_dentry_attach_gen(struct dentry * dentry)28*4882a593Smuzhiyun void ocfs2_dentry_attach_gen(struct dentry *dentry)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun unsigned long gen =
31*4882a593Smuzhiyun OCFS2_I(d_inode(dentry->d_parent))->ip_dir_lock_gen;
32*4882a593Smuzhiyun BUG_ON(d_inode(dentry));
33*4882a593Smuzhiyun dentry->d_fsdata = (void *)gen;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun
ocfs2_dentry_revalidate(struct dentry * dentry,unsigned int flags)37*4882a593Smuzhiyun static int ocfs2_dentry_revalidate(struct dentry *dentry, unsigned int flags)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun struct inode *inode;
40*4882a593Smuzhiyun int ret = 0; /* if all else fails, just return false */
41*4882a593Smuzhiyun struct ocfs2_super *osb;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (flags & LOOKUP_RCU)
44*4882a593Smuzhiyun return -ECHILD;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun inode = d_inode(dentry);
47*4882a593Smuzhiyun osb = OCFS2_SB(dentry->d_sb);
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun trace_ocfs2_dentry_revalidate(dentry, dentry->d_name.len,
50*4882a593Smuzhiyun dentry->d_name.name);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* For a negative dentry -
53*4882a593Smuzhiyun * check the generation number of the parent and compare with the
54*4882a593Smuzhiyun * one stored in the inode.
55*4882a593Smuzhiyun */
56*4882a593Smuzhiyun if (inode == NULL) {
57*4882a593Smuzhiyun unsigned long gen = (unsigned long) dentry->d_fsdata;
58*4882a593Smuzhiyun unsigned long pgen;
59*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
60*4882a593Smuzhiyun pgen = OCFS2_I(d_inode(dentry->d_parent))->ip_dir_lock_gen;
61*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
62*4882a593Smuzhiyun trace_ocfs2_dentry_revalidate_negative(dentry->d_name.len,
63*4882a593Smuzhiyun dentry->d_name.name,
64*4882a593Smuzhiyun pgen, gen);
65*4882a593Smuzhiyun if (gen != pgen)
66*4882a593Smuzhiyun goto bail;
67*4882a593Smuzhiyun goto valid;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun BUG_ON(!osb);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (inode == osb->root_inode || is_bad_inode(inode))
73*4882a593Smuzhiyun goto bail;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun spin_lock(&OCFS2_I(inode)->ip_lock);
76*4882a593Smuzhiyun /* did we or someone else delete this inode? */
77*4882a593Smuzhiyun if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
78*4882a593Smuzhiyun spin_unlock(&OCFS2_I(inode)->ip_lock);
79*4882a593Smuzhiyun trace_ocfs2_dentry_revalidate_delete(
80*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno);
81*4882a593Smuzhiyun goto bail;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun spin_unlock(&OCFS2_I(inode)->ip_lock);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * We don't need a cluster lock to test this because once an
87*4882a593Smuzhiyun * inode nlink hits zero, it never goes back.
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun if (inode->i_nlink == 0) {
90*4882a593Smuzhiyun trace_ocfs2_dentry_revalidate_orphaned(
91*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno,
92*4882a593Smuzhiyun S_ISDIR(inode->i_mode));
93*4882a593Smuzhiyun goto bail;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * If the last lookup failed to create dentry lock, let us
98*4882a593Smuzhiyun * redo it.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun if (!dentry->d_fsdata) {
101*4882a593Smuzhiyun trace_ocfs2_dentry_revalidate_nofsdata(
102*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno);
103*4882a593Smuzhiyun goto bail;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun valid:
107*4882a593Smuzhiyun ret = 1;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun bail:
110*4882a593Smuzhiyun trace_ocfs2_dentry_revalidate_ret(ret);
111*4882a593Smuzhiyun return ret;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
ocfs2_match_dentry(struct dentry * dentry,u64 parent_blkno,int skip_unhashed)114*4882a593Smuzhiyun static int ocfs2_match_dentry(struct dentry *dentry,
115*4882a593Smuzhiyun u64 parent_blkno,
116*4882a593Smuzhiyun int skip_unhashed)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun struct inode *parent;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun * ocfs2_lookup() does a d_splice_alias() _before_ attaching
122*4882a593Smuzhiyun * to the lock data, so we skip those here, otherwise
123*4882a593Smuzhiyun * ocfs2_dentry_attach_lock() will get its original dentry
124*4882a593Smuzhiyun * back.
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun if (!dentry->d_fsdata)
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun if (!dentry->d_parent)
130*4882a593Smuzhiyun return 0;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun if (skip_unhashed && d_unhashed(dentry))
133*4882a593Smuzhiyun return 0;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun parent = d_inode(dentry->d_parent);
136*4882a593Smuzhiyun /* Negative parent dentry? */
137*4882a593Smuzhiyun if (!parent)
138*4882a593Smuzhiyun return 0;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /* Name is in a different directory. */
141*4882a593Smuzhiyun if (OCFS2_I(parent)->ip_blkno != parent_blkno)
142*4882a593Smuzhiyun return 0;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun return 1;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun * Walk the inode alias list, and find a dentry which has a given
149*4882a593Smuzhiyun * parent. ocfs2_dentry_attach_lock() wants to find _any_ alias as it
150*4882a593Smuzhiyun * is looking for a dentry_lock reference. The downconvert thread is
151*4882a593Smuzhiyun * looking to unhash aliases, so we allow it to skip any that already
152*4882a593Smuzhiyun * have that property.
153*4882a593Smuzhiyun */
ocfs2_find_local_alias(struct inode * inode,u64 parent_blkno,int skip_unhashed)154*4882a593Smuzhiyun struct dentry *ocfs2_find_local_alias(struct inode *inode,
155*4882a593Smuzhiyun u64 parent_blkno,
156*4882a593Smuzhiyun int skip_unhashed)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun struct dentry *dentry;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun spin_lock(&inode->i_lock);
161*4882a593Smuzhiyun hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
162*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
163*4882a593Smuzhiyun if (ocfs2_match_dentry(dentry, parent_blkno, skip_unhashed)) {
164*4882a593Smuzhiyun trace_ocfs2_find_local_alias(dentry->d_name.len,
165*4882a593Smuzhiyun dentry->d_name.name);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun dget_dlock(dentry);
168*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
169*4882a593Smuzhiyun spin_unlock(&inode->i_lock);
170*4882a593Smuzhiyun return dentry;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun spin_unlock(&inode->i_lock);
175*4882a593Smuzhiyun return NULL;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun DEFINE_SPINLOCK(dentry_attach_lock);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * Attach this dentry to a cluster lock.
182*4882a593Smuzhiyun *
183*4882a593Smuzhiyun * Dentry locks cover all links in a given directory to a particular
184*4882a593Smuzhiyun * inode. We do this so that ocfs2 can build a lock name which all
185*4882a593Smuzhiyun * nodes in the cluster can agree on at all times. Shoving full names
186*4882a593Smuzhiyun * in the cluster lock won't work due to size restrictions. Covering
187*4882a593Smuzhiyun * links inside of a directory is a good compromise because it still
188*4882a593Smuzhiyun * allows us to use the parent directory lock to synchronize
189*4882a593Smuzhiyun * operations.
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * Call this function with the parent dir semaphore and the parent dir
192*4882a593Smuzhiyun * cluster lock held.
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * The dir semaphore will protect us from having to worry about
195*4882a593Smuzhiyun * concurrent processes on our node trying to attach a lock at the
196*4882a593Smuzhiyun * same time.
197*4882a593Smuzhiyun *
198*4882a593Smuzhiyun * The dir cluster lock (held at either PR or EX mode) protects us
199*4882a593Smuzhiyun * from unlink and rename on other nodes.
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * A dput() can happen asynchronously due to pruning, so we cover
202*4882a593Smuzhiyun * attaching and detaching the dentry lock with a
203*4882a593Smuzhiyun * dentry_attach_lock.
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * A node which has done lookup on a name retains a protected read
206*4882a593Smuzhiyun * lock until final dput. If the user requests and unlink or rename,
207*4882a593Smuzhiyun * the protected read is upgraded to an exclusive lock. Other nodes
208*4882a593Smuzhiyun * who have seen the dentry will then be informed that they need to
209*4882a593Smuzhiyun * downgrade their lock, which will involve d_delete on the
210*4882a593Smuzhiyun * dentry. This happens in ocfs2_dentry_convert_worker().
211*4882a593Smuzhiyun */
ocfs2_dentry_attach_lock(struct dentry * dentry,struct inode * inode,u64 parent_blkno)212*4882a593Smuzhiyun int ocfs2_dentry_attach_lock(struct dentry *dentry,
213*4882a593Smuzhiyun struct inode *inode,
214*4882a593Smuzhiyun u64 parent_blkno)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun int ret;
217*4882a593Smuzhiyun struct dentry *alias;
218*4882a593Smuzhiyun struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun trace_ocfs2_dentry_attach_lock(dentry->d_name.len, dentry->d_name.name,
221*4882a593Smuzhiyun (unsigned long long)parent_blkno, dl);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /*
224*4882a593Smuzhiyun * Negative dentry. We ignore these for now.
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * XXX: Could we can improve ocfs2_dentry_revalidate() by
227*4882a593Smuzhiyun * tracking these?
228*4882a593Smuzhiyun */
229*4882a593Smuzhiyun if (!inode)
230*4882a593Smuzhiyun return 0;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun if (d_really_is_negative(dentry) && dentry->d_fsdata) {
233*4882a593Smuzhiyun /* Converting a negative dentry to positive
234*4882a593Smuzhiyun Clear dentry->d_fsdata */
235*4882a593Smuzhiyun dentry->d_fsdata = dl = NULL;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (dl) {
239*4882a593Smuzhiyun mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno,
240*4882a593Smuzhiyun " \"%pd\": old parent: %llu, new: %llu\n",
241*4882a593Smuzhiyun dentry,
242*4882a593Smuzhiyun (unsigned long long)parent_blkno,
243*4882a593Smuzhiyun (unsigned long long)dl->dl_parent_blkno);
244*4882a593Smuzhiyun return 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun alias = ocfs2_find_local_alias(inode, parent_blkno, 0);
248*4882a593Smuzhiyun if (alias) {
249*4882a593Smuzhiyun /*
250*4882a593Smuzhiyun * Great, an alias exists, which means we must have a
251*4882a593Smuzhiyun * dentry lock already. We can just grab the lock off
252*4882a593Smuzhiyun * the alias and add it to the list.
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * We're depending here on the fact that this dentry
255*4882a593Smuzhiyun * was found and exists in the dcache and so must have
256*4882a593Smuzhiyun * a reference to the dentry_lock because we can't
257*4882a593Smuzhiyun * race creates. Final dput() cannot happen on it
258*4882a593Smuzhiyun * since we have it pinned, so our reference is safe.
259*4882a593Smuzhiyun */
260*4882a593Smuzhiyun dl = alias->d_fsdata;
261*4882a593Smuzhiyun mlog_bug_on_msg(!dl, "parent %llu, ino %llu\n",
262*4882a593Smuzhiyun (unsigned long long)parent_blkno,
263*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno,
266*4882a593Smuzhiyun " \"%pd\": old parent: %llu, new: %llu\n",
267*4882a593Smuzhiyun dentry,
268*4882a593Smuzhiyun (unsigned long long)parent_blkno,
269*4882a593Smuzhiyun (unsigned long long)dl->dl_parent_blkno);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun trace_ocfs2_dentry_attach_lock_found(dl->dl_lockres.l_name,
272*4882a593Smuzhiyun (unsigned long long)parent_blkno,
273*4882a593Smuzhiyun (unsigned long long)OCFS2_I(inode)->ip_blkno);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun goto out_attach;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun * There are no other aliases
280*4882a593Smuzhiyun */
281*4882a593Smuzhiyun dl = kmalloc(sizeof(*dl), GFP_NOFS);
282*4882a593Smuzhiyun if (!dl) {
283*4882a593Smuzhiyun ret = -ENOMEM;
284*4882a593Smuzhiyun mlog_errno(ret);
285*4882a593Smuzhiyun return ret;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun dl->dl_count = 0;
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * Does this have to happen below, for all attaches, in case
291*4882a593Smuzhiyun * the struct inode gets blown away by the downconvert thread?
292*4882a593Smuzhiyun */
293*4882a593Smuzhiyun dl->dl_inode = igrab(inode);
294*4882a593Smuzhiyun dl->dl_parent_blkno = parent_blkno;
295*4882a593Smuzhiyun ocfs2_dentry_lock_res_init(dl, parent_blkno, inode);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun out_attach:
298*4882a593Smuzhiyun spin_lock(&dentry_attach_lock);
299*4882a593Smuzhiyun if (unlikely(dentry->d_fsdata && !alias)) {
300*4882a593Smuzhiyun /* d_fsdata is set by a racing thread which is doing
301*4882a593Smuzhiyun * the same thing as this thread is doing. Leave the racing
302*4882a593Smuzhiyun * thread going ahead and we return here.
303*4882a593Smuzhiyun */
304*4882a593Smuzhiyun spin_unlock(&dentry_attach_lock);
305*4882a593Smuzhiyun iput(dl->dl_inode);
306*4882a593Smuzhiyun ocfs2_lock_res_free(&dl->dl_lockres);
307*4882a593Smuzhiyun kfree(dl);
308*4882a593Smuzhiyun return 0;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun dentry->d_fsdata = dl;
312*4882a593Smuzhiyun dl->dl_count++;
313*4882a593Smuzhiyun spin_unlock(&dentry_attach_lock);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun * This actually gets us our PRMODE level lock. From now on,
317*4882a593Smuzhiyun * we'll have a notification if one of these names is
318*4882a593Smuzhiyun * destroyed on another node.
319*4882a593Smuzhiyun */
320*4882a593Smuzhiyun ret = ocfs2_dentry_lock(dentry, 0);
321*4882a593Smuzhiyun if (!ret)
322*4882a593Smuzhiyun ocfs2_dentry_unlock(dentry, 0);
323*4882a593Smuzhiyun else
324*4882a593Smuzhiyun mlog_errno(ret);
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun /*
327*4882a593Smuzhiyun * In case of error, manually free the allocation and do the iput().
328*4882a593Smuzhiyun * We need to do this because error here means no d_instantiate(),
329*4882a593Smuzhiyun * which means iput() will not be called during dput(dentry).
330*4882a593Smuzhiyun */
331*4882a593Smuzhiyun if (ret < 0 && !alias) {
332*4882a593Smuzhiyun ocfs2_lock_res_free(&dl->dl_lockres);
333*4882a593Smuzhiyun BUG_ON(dl->dl_count != 1);
334*4882a593Smuzhiyun spin_lock(&dentry_attach_lock);
335*4882a593Smuzhiyun dentry->d_fsdata = NULL;
336*4882a593Smuzhiyun spin_unlock(&dentry_attach_lock);
337*4882a593Smuzhiyun kfree(dl);
338*4882a593Smuzhiyun iput(inode);
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun dput(alias);
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun return ret;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /*
347*4882a593Smuzhiyun * ocfs2_dentry_iput() and friends.
348*4882a593Smuzhiyun *
349*4882a593Smuzhiyun * At this point, our particular dentry is detached from the inodes
350*4882a593Smuzhiyun * alias list, so there's no way that the locking code can find it.
351*4882a593Smuzhiyun *
352*4882a593Smuzhiyun * The interesting stuff happens when we determine that our lock needs
353*4882a593Smuzhiyun * to go away because this is the last subdir alias in the
354*4882a593Smuzhiyun * system. This function needs to handle a couple things:
355*4882a593Smuzhiyun *
356*4882a593Smuzhiyun * 1) Synchronizing lock shutdown with the downconvert threads. This
357*4882a593Smuzhiyun * is already handled for us via the lockres release drop function
358*4882a593Smuzhiyun * called in ocfs2_release_dentry_lock()
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * 2) A race may occur when we're doing our lock shutdown and
361*4882a593Smuzhiyun * another process wants to create a new dentry lock. Right now we
362*4882a593Smuzhiyun * let them race, which means that for a very short while, this
363*4882a593Smuzhiyun * node might have two locks on a lock resource. This should be a
364*4882a593Smuzhiyun * problem though because one of them is in the process of being
365*4882a593Smuzhiyun * thrown out.
366*4882a593Smuzhiyun */
ocfs2_drop_dentry_lock(struct ocfs2_super * osb,struct ocfs2_dentry_lock * dl)367*4882a593Smuzhiyun static void ocfs2_drop_dentry_lock(struct ocfs2_super *osb,
368*4882a593Smuzhiyun struct ocfs2_dentry_lock *dl)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun iput(dl->dl_inode);
371*4882a593Smuzhiyun ocfs2_simple_drop_lockres(osb, &dl->dl_lockres);
372*4882a593Smuzhiyun ocfs2_lock_res_free(&dl->dl_lockres);
373*4882a593Smuzhiyun kfree(dl);
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
ocfs2_dentry_lock_put(struct ocfs2_super * osb,struct ocfs2_dentry_lock * dl)376*4882a593Smuzhiyun void ocfs2_dentry_lock_put(struct ocfs2_super *osb,
377*4882a593Smuzhiyun struct ocfs2_dentry_lock *dl)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun int unlock = 0;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun BUG_ON(dl->dl_count == 0);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun spin_lock(&dentry_attach_lock);
384*4882a593Smuzhiyun dl->dl_count--;
385*4882a593Smuzhiyun unlock = !dl->dl_count;
386*4882a593Smuzhiyun spin_unlock(&dentry_attach_lock);
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun if (unlock)
389*4882a593Smuzhiyun ocfs2_drop_dentry_lock(osb, dl);
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
ocfs2_dentry_iput(struct dentry * dentry,struct inode * inode)392*4882a593Smuzhiyun static void ocfs2_dentry_iput(struct dentry *dentry, struct inode *inode)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun if (!dl) {
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun * No dentry lock is ok if we're disconnected or
399*4882a593Smuzhiyun * unhashed.
400*4882a593Smuzhiyun */
401*4882a593Smuzhiyun if (!(dentry->d_flags & DCACHE_DISCONNECTED) &&
402*4882a593Smuzhiyun !d_unhashed(dentry)) {
403*4882a593Smuzhiyun unsigned long long ino = 0ULL;
404*4882a593Smuzhiyun if (inode)
405*4882a593Smuzhiyun ino = (unsigned long long)OCFS2_I(inode)->ip_blkno;
406*4882a593Smuzhiyun mlog(ML_ERROR, "Dentry is missing cluster lock. "
407*4882a593Smuzhiyun "inode: %llu, d_flags: 0x%x, d_name: %pd\n",
408*4882a593Smuzhiyun ino, dentry->d_flags, dentry);
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun goto out;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun mlog_bug_on_msg(dl->dl_count == 0, "dentry: %pd, count: %u\n",
415*4882a593Smuzhiyun dentry, dl->dl_count);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun ocfs2_dentry_lock_put(OCFS2_SB(dentry->d_sb), dl);
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun out:
420*4882a593Smuzhiyun iput(inode);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /*
424*4882a593Smuzhiyun * d_move(), but keep the locks in sync.
425*4882a593Smuzhiyun *
426*4882a593Smuzhiyun * When we are done, "dentry" will have the parent dir and name of
427*4882a593Smuzhiyun * "target", which will be thrown away.
428*4882a593Smuzhiyun *
429*4882a593Smuzhiyun * We manually update the lock of "dentry" if need be.
430*4882a593Smuzhiyun *
431*4882a593Smuzhiyun * "target" doesn't have it's dentry lock touched - we allow the later
432*4882a593Smuzhiyun * dput() to handle this for us.
433*4882a593Smuzhiyun *
434*4882a593Smuzhiyun * This is called during ocfs2_rename(), while holding parent
435*4882a593Smuzhiyun * directory locks. The dentries have already been deleted on other
436*4882a593Smuzhiyun * nodes via ocfs2_remote_dentry_delete().
437*4882a593Smuzhiyun *
438*4882a593Smuzhiyun * Normally, the VFS handles the d_move() for the file system, after
439*4882a593Smuzhiyun * the ->rename() callback. OCFS2 wants to handle this internally, so
440*4882a593Smuzhiyun * the new lock can be created atomically with respect to the cluster.
441*4882a593Smuzhiyun */
ocfs2_dentry_move(struct dentry * dentry,struct dentry * target,struct inode * old_dir,struct inode * new_dir)442*4882a593Smuzhiyun void ocfs2_dentry_move(struct dentry *dentry, struct dentry *target,
443*4882a593Smuzhiyun struct inode *old_dir, struct inode *new_dir)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun int ret;
446*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(old_dir->i_sb);
447*4882a593Smuzhiyun struct inode *inode = d_inode(dentry);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /*
450*4882a593Smuzhiyun * Move within the same directory, so the actual lock info won't
451*4882a593Smuzhiyun * change.
452*4882a593Smuzhiyun *
453*4882a593Smuzhiyun * XXX: Is there any advantage to dropping the lock here?
454*4882a593Smuzhiyun */
455*4882a593Smuzhiyun if (old_dir == new_dir)
456*4882a593Smuzhiyun goto out_move;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun ocfs2_dentry_lock_put(osb, dentry->d_fsdata);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun dentry->d_fsdata = NULL;
461*4882a593Smuzhiyun ret = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(new_dir)->ip_blkno);
462*4882a593Smuzhiyun if (ret)
463*4882a593Smuzhiyun mlog_errno(ret);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun out_move:
466*4882a593Smuzhiyun d_move(dentry, target);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun const struct dentry_operations ocfs2_dentry_ops = {
470*4882a593Smuzhiyun .d_revalidate = ocfs2_dentry_revalidate,
471*4882a593Smuzhiyun .d_iput = ocfs2_dentry_iput,
472*4882a593Smuzhiyun };
473