dcache.c 14 KB

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