dcache.c 14 KB

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