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