dcache.c 12 KB

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