dcache.c 12 KB

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