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