dcache.c 13 KB

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