dcache.c 12 KB

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