xfs_iget.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_acl.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_inum.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_dir2.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_quota.h"
  41. #include "xfs_utils.h"
  42. #include "xfs_trans_priv.h"
  43. #include "xfs_inode_item.h"
  44. #include "xfs_bmap.h"
  45. #include "xfs_btree_trace.h"
  46. #include "xfs_dir2_trace.h"
  47. /*
  48. * Allocate and initialise an xfs_inode.
  49. */
  50. STATIC struct xfs_inode *
  51. xfs_inode_alloc(
  52. struct xfs_mount *mp,
  53. xfs_ino_t ino)
  54. {
  55. struct xfs_inode *ip;
  56. /*
  57. * if this didn't occur in transactions, we could use
  58. * KM_MAYFAIL and return NULL here on ENOMEM. Set the
  59. * code up to do this anyway.
  60. */
  61. ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
  62. if (!ip)
  63. return NULL;
  64. ASSERT(atomic_read(&ip->i_iocount) == 0);
  65. ASSERT(atomic_read(&ip->i_pincount) == 0);
  66. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  67. ASSERT(completion_done(&ip->i_flush));
  68. /* initialise the xfs inode */
  69. ip->i_ino = ino;
  70. ip->i_mount = mp;
  71. memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
  72. ip->i_afp = NULL;
  73. memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
  74. ip->i_flags = 0;
  75. ip->i_update_core = 0;
  76. ip->i_update_size = 0;
  77. ip->i_delayed_blks = 0;
  78. memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
  79. ip->i_size = 0;
  80. ip->i_new_size = 0;
  81. /*
  82. * Initialize inode's trace buffers.
  83. */
  84. #ifdef XFS_INODE_TRACE
  85. ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS);
  86. #endif
  87. #ifdef XFS_BMAP_TRACE
  88. ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS);
  89. #endif
  90. #ifdef XFS_BTREE_TRACE
  91. ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS);
  92. #endif
  93. #ifdef XFS_RW_TRACE
  94. ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS);
  95. #endif
  96. #ifdef XFS_ILOCK_TRACE
  97. ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS);
  98. #endif
  99. #ifdef XFS_DIR2_TRACE
  100. ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS);
  101. #endif
  102. /*
  103. * Now initialise the VFS inode. We do this after the xfs_inode
  104. * initialisation as internal failures will result in ->destroy_inode
  105. * being called and that will pass down through the reclaim path and
  106. * free the XFS inode. This path requires the XFS inode to already be
  107. * initialised. Hence if this call fails, the xfs_inode has already
  108. * been freed and we should not reference it at all in the error
  109. * handling.
  110. */
  111. if (!inode_init_always(mp->m_super, VFS_I(ip)))
  112. return NULL;
  113. /* prevent anyone from using this yet */
  114. VFS_I(ip)->i_state = I_NEW|I_LOCK;
  115. return ip;
  116. }
  117. /*
  118. * Check the validity of the inode we just found it the cache
  119. */
  120. static int
  121. xfs_iget_cache_hit(
  122. struct xfs_perag *pag,
  123. struct xfs_inode *ip,
  124. int flags,
  125. int lock_flags) __releases(pag->pag_ici_lock)
  126. {
  127. struct xfs_mount *mp = ip->i_mount;
  128. int error = EAGAIN;
  129. /*
  130. * If INEW is set this inode is being set up
  131. * If IRECLAIM is set this inode is being torn down
  132. * Pause and try again.
  133. */
  134. if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) {
  135. XFS_STATS_INC(xs_ig_frecycle);
  136. goto out_error;
  137. }
  138. /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
  139. if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
  140. /*
  141. * If lookup is racing with unlink, then we should return an
  142. * error immediately so we don't remove it from the reclaim
  143. * list and potentially leak the inode.
  144. */
  145. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  146. error = ENOENT;
  147. goto out_error;
  148. }
  149. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  150. /*
  151. * We need to re-initialise the VFS inode as it has been
  152. * 'freed' by the VFS. Do this here so we can deal with
  153. * errors cleanly, then tag it so it can be set up correctly
  154. * later.
  155. */
  156. if (!inode_init_always(mp->m_super, VFS_I(ip))) {
  157. error = ENOMEM;
  158. goto out_error;
  159. }
  160. /*
  161. * We must set the XFS_INEW flag before clearing the
  162. * XFS_IRECLAIMABLE flag so that if a racing lookup does
  163. * not find the XFS_IRECLAIMABLE above but has the igrab()
  164. * below succeed we can safely check XFS_INEW to detect
  165. * that this inode is still being initialised.
  166. */
  167. xfs_iflags_set(ip, XFS_INEW);
  168. xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
  169. /* clear the radix tree reclaim flag as well. */
  170. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  171. } else if (!igrab(VFS_I(ip))) {
  172. /* If the VFS inode is being torn down, pause and try again. */
  173. XFS_STATS_INC(xs_ig_frecycle);
  174. goto out_error;
  175. } else if (xfs_iflags_test(ip, XFS_INEW)) {
  176. /*
  177. * We are racing with another cache hit that is
  178. * currently recycling this inode out of the XFS_IRECLAIMABLE
  179. * state. Wait for the initialisation to complete before
  180. * continuing.
  181. */
  182. wait_on_inode(VFS_I(ip));
  183. }
  184. if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  185. error = ENOENT;
  186. iput(VFS_I(ip));
  187. goto out_error;
  188. }
  189. /* We've got a live one. */
  190. read_unlock(&pag->pag_ici_lock);
  191. if (lock_flags != 0)
  192. xfs_ilock(ip, lock_flags);
  193. xfs_iflags_clear(ip, XFS_ISTALE);
  194. xfs_itrace_exit_tag(ip, "xfs_iget.found");
  195. XFS_STATS_INC(xs_ig_found);
  196. return 0;
  197. out_error:
  198. read_unlock(&pag->pag_ici_lock);
  199. return error;
  200. }
  201. static int
  202. xfs_iget_cache_miss(
  203. struct xfs_mount *mp,
  204. struct xfs_perag *pag,
  205. xfs_trans_t *tp,
  206. xfs_ino_t ino,
  207. struct xfs_inode **ipp,
  208. xfs_daddr_t bno,
  209. int flags,
  210. int lock_flags) __releases(pag->pag_ici_lock)
  211. {
  212. struct xfs_inode *ip;
  213. int error;
  214. unsigned long first_index, mask;
  215. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
  216. ip = xfs_inode_alloc(mp, ino);
  217. if (!ip)
  218. return ENOMEM;
  219. error = xfs_iread(mp, tp, ip, bno, flags);
  220. if (error)
  221. goto out_destroy;
  222. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  223. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  224. error = ENOENT;
  225. goto out_destroy;
  226. }
  227. /*
  228. * Preload the radix tree so we can insert safely under the
  229. * write spinlock. Note that we cannot sleep inside the preload
  230. * region.
  231. */
  232. if (radix_tree_preload(GFP_KERNEL)) {
  233. error = EAGAIN;
  234. goto out_destroy;
  235. }
  236. /*
  237. * Because the inode hasn't been added to the radix-tree yet it can't
  238. * be found by another thread, so we can do the non-sleeping lock here.
  239. */
  240. if (lock_flags) {
  241. if (!xfs_ilock_nowait(ip, lock_flags))
  242. BUG();
  243. }
  244. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  245. first_index = agino & mask;
  246. write_lock(&pag->pag_ici_lock);
  247. /* insert the new inode */
  248. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  249. if (unlikely(error)) {
  250. WARN_ON(error != -EEXIST);
  251. XFS_STATS_INC(xs_ig_dup);
  252. error = EAGAIN;
  253. goto out_preload_end;
  254. }
  255. /* These values _must_ be set before releasing the radix tree lock! */
  256. ip->i_udquot = ip->i_gdquot = NULL;
  257. xfs_iflags_set(ip, XFS_INEW);
  258. write_unlock(&pag->pag_ici_lock);
  259. radix_tree_preload_end();
  260. *ipp = ip;
  261. return 0;
  262. out_preload_end:
  263. write_unlock(&pag->pag_ici_lock);
  264. radix_tree_preload_end();
  265. if (lock_flags)
  266. xfs_iunlock(ip, lock_flags);
  267. out_destroy:
  268. xfs_destroy_inode(ip);
  269. return error;
  270. }
  271. /*
  272. * Look up an inode by number in the given file system.
  273. * The inode is looked up in the cache held in each AG.
  274. * If the inode is found in the cache, initialise the vfs inode
  275. * if necessary.
  276. *
  277. * If it is not in core, read it in from the file system's device,
  278. * add it to the cache and initialise the vfs inode.
  279. *
  280. * The inode is locked according to the value of the lock_flags parameter.
  281. * This flag parameter indicates how and if the inode's IO lock and inode lock
  282. * should be taken.
  283. *
  284. * mp -- the mount point structure for the current file system. It points
  285. * to the inode hash table.
  286. * tp -- a pointer to the current transaction if there is one. This is
  287. * simply passed through to the xfs_iread() call.
  288. * ino -- the number of the inode desired. This is the unique identifier
  289. * within the file system for the inode being requested.
  290. * lock_flags -- flags indicating how to lock the inode. See the comment
  291. * for xfs_ilock() for a list of valid values.
  292. * bno -- the block number starting the buffer containing the inode,
  293. * if known (as by bulkstat), else 0.
  294. */
  295. int
  296. xfs_iget(
  297. xfs_mount_t *mp,
  298. xfs_trans_t *tp,
  299. xfs_ino_t ino,
  300. uint flags,
  301. uint lock_flags,
  302. xfs_inode_t **ipp,
  303. xfs_daddr_t bno)
  304. {
  305. xfs_inode_t *ip;
  306. int error;
  307. xfs_perag_t *pag;
  308. xfs_agino_t agino;
  309. /* the radix tree exists only in inode capable AGs */
  310. if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
  311. return EINVAL;
  312. /* get the perag structure and ensure that it's inode capable */
  313. pag = xfs_get_perag(mp, ino);
  314. if (!pag->pagi_inodeok)
  315. return EINVAL;
  316. ASSERT(pag->pag_ici_init);
  317. agino = XFS_INO_TO_AGINO(mp, ino);
  318. again:
  319. error = 0;
  320. read_lock(&pag->pag_ici_lock);
  321. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  322. if (ip) {
  323. error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
  324. if (error)
  325. goto out_error_or_again;
  326. } else {
  327. read_unlock(&pag->pag_ici_lock);
  328. XFS_STATS_INC(xs_ig_missed);
  329. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
  330. flags, lock_flags);
  331. if (error)
  332. goto out_error_or_again;
  333. }
  334. xfs_put_perag(mp, pag);
  335. *ipp = ip;
  336. ASSERT(ip->i_df.if_ext_max ==
  337. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  338. /*
  339. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  340. * now. If it's a new inode being created, xfs_ialloc will handle it.
  341. */
  342. if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
  343. xfs_setup_inode(ip);
  344. return 0;
  345. out_error_or_again:
  346. if (error == EAGAIN) {
  347. delay(1);
  348. goto again;
  349. }
  350. xfs_put_perag(mp, pag);
  351. return error;
  352. }
  353. /*
  354. * Look for the inode corresponding to the given ino in the hash table.
  355. * If it is there and its i_transp pointer matches tp, return it.
  356. * Otherwise, return NULL.
  357. */
  358. xfs_inode_t *
  359. xfs_inode_incore(xfs_mount_t *mp,
  360. xfs_ino_t ino,
  361. xfs_trans_t *tp)
  362. {
  363. xfs_inode_t *ip;
  364. xfs_perag_t *pag;
  365. pag = xfs_get_perag(mp, ino);
  366. read_lock(&pag->pag_ici_lock);
  367. ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
  368. read_unlock(&pag->pag_ici_lock);
  369. xfs_put_perag(mp, pag);
  370. /* the returned inode must match the transaction */
  371. if (ip && (ip->i_transp != tp))
  372. return NULL;
  373. return ip;
  374. }
  375. /*
  376. * Decrement reference count of an inode structure and unlock it.
  377. *
  378. * ip -- the inode being released
  379. * lock_flags -- this parameter indicates the inode's locks to be
  380. * to be released. See the comment on xfs_iunlock() for a list
  381. * of valid values.
  382. */
  383. void
  384. xfs_iput(xfs_inode_t *ip,
  385. uint lock_flags)
  386. {
  387. xfs_itrace_entry(ip);
  388. xfs_iunlock(ip, lock_flags);
  389. IRELE(ip);
  390. }
  391. /*
  392. * Special iput for brand-new inodes that are still locked
  393. */
  394. void
  395. xfs_iput_new(
  396. xfs_inode_t *ip,
  397. uint lock_flags)
  398. {
  399. struct inode *inode = VFS_I(ip);
  400. xfs_itrace_entry(ip);
  401. if ((ip->i_d.di_mode == 0)) {
  402. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  403. make_bad_inode(inode);
  404. }
  405. if (inode->i_state & I_NEW)
  406. unlock_new_inode(inode);
  407. if (lock_flags)
  408. xfs_iunlock(ip, lock_flags);
  409. IRELE(ip);
  410. }
  411. /*
  412. * This is called free all the memory associated with an inode.
  413. * It must free the inode itself and any buffers allocated for
  414. * if_extents/if_data and if_broot. It must also free the lock
  415. * associated with the inode.
  416. *
  417. * Note: because we don't initialise everything on reallocation out
  418. * of the zone, we must ensure we nullify everything correctly before
  419. * freeing the structure.
  420. */
  421. void
  422. xfs_ireclaim(
  423. struct xfs_inode *ip)
  424. {
  425. struct xfs_mount *mp = ip->i_mount;
  426. struct xfs_perag *pag;
  427. XFS_STATS_INC(xs_ig_reclaims);
  428. /*
  429. * Remove the inode from the per-AG radix tree. It doesn't matter
  430. * if it was never added to it because radix_tree_delete can deal
  431. * with that case just fine.
  432. */
  433. pag = xfs_get_perag(mp, ip->i_ino);
  434. write_lock(&pag->pag_ici_lock);
  435. radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
  436. write_unlock(&pag->pag_ici_lock);
  437. xfs_put_perag(mp, pag);
  438. /*
  439. * Here we do an (almost) spurious inode lock in order to coordinate
  440. * with inode cache radix tree lookups. This is because the lookup
  441. * can reference the inodes in the cache without taking references.
  442. *
  443. * We make that OK here by ensuring that we wait until the inode is
  444. * unlocked after the lookup before we go ahead and free it. We get
  445. * both the ilock and the iolock because the code may need to drop the
  446. * ilock one but will still hold the iolock.
  447. */
  448. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  449. xfs_qm_dqdetach(ip);
  450. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  451. switch (ip->i_d.di_mode & S_IFMT) {
  452. case S_IFREG:
  453. case S_IFDIR:
  454. case S_IFLNK:
  455. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  456. break;
  457. }
  458. if (ip->i_afp)
  459. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  460. #ifdef XFS_INODE_TRACE
  461. ktrace_free(ip->i_trace);
  462. #endif
  463. #ifdef XFS_BMAP_TRACE
  464. ktrace_free(ip->i_xtrace);
  465. #endif
  466. #ifdef XFS_BTREE_TRACE
  467. ktrace_free(ip->i_btrace);
  468. #endif
  469. #ifdef XFS_RW_TRACE
  470. ktrace_free(ip->i_rwtrace);
  471. #endif
  472. #ifdef XFS_ILOCK_TRACE
  473. ktrace_free(ip->i_lock_trace);
  474. #endif
  475. #ifdef XFS_DIR2_TRACE
  476. ktrace_free(ip->i_dir_trace);
  477. #endif
  478. if (ip->i_itemp) {
  479. /*
  480. * Only if we are shutting down the fs will we see an
  481. * inode still in the AIL. If it is there, we should remove
  482. * it to prevent a use-after-free from occurring.
  483. */
  484. xfs_log_item_t *lip = &ip->i_itemp->ili_item;
  485. struct xfs_ail *ailp = lip->li_ailp;
  486. ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
  487. XFS_FORCED_SHUTDOWN(ip->i_mount));
  488. if (lip->li_flags & XFS_LI_IN_AIL) {
  489. spin_lock(&ailp->xa_lock);
  490. if (lip->li_flags & XFS_LI_IN_AIL)
  491. xfs_trans_ail_delete(ailp, lip);
  492. else
  493. spin_unlock(&ailp->xa_lock);
  494. }
  495. xfs_inode_item_destroy(ip);
  496. ip->i_itemp = NULL;
  497. }
  498. /* asserts to verify all state is correct here */
  499. ASSERT(atomic_read(&ip->i_iocount) == 0);
  500. ASSERT(atomic_read(&ip->i_pincount) == 0);
  501. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  502. ASSERT(completion_done(&ip->i_flush));
  503. kmem_zone_free(xfs_inode_zone, ip);
  504. }
  505. /*
  506. * This is a wrapper routine around the xfs_ilock() routine
  507. * used to centralize some grungy code. It is used in places
  508. * that wish to lock the inode solely for reading the extents.
  509. * The reason these places can't just call xfs_ilock(SHARED)
  510. * is that the inode lock also guards to bringing in of the
  511. * extents from disk for a file in b-tree format. If the inode
  512. * is in b-tree format, then we need to lock the inode exclusively
  513. * until the extents are read in. Locking it exclusively all
  514. * the time would limit our parallelism unnecessarily, though.
  515. * What we do instead is check to see if the extents have been
  516. * read in yet, and only lock the inode exclusively if they
  517. * have not.
  518. *
  519. * The function returns a value which should be given to the
  520. * corresponding xfs_iunlock_map_shared(). This value is
  521. * the mode in which the lock was actually taken.
  522. */
  523. uint
  524. xfs_ilock_map_shared(
  525. xfs_inode_t *ip)
  526. {
  527. uint lock_mode;
  528. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  529. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  530. lock_mode = XFS_ILOCK_EXCL;
  531. } else {
  532. lock_mode = XFS_ILOCK_SHARED;
  533. }
  534. xfs_ilock(ip, lock_mode);
  535. return lock_mode;
  536. }
  537. /*
  538. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  539. * All it does is call xfs_iunlock() with the given lock_mode.
  540. */
  541. void
  542. xfs_iunlock_map_shared(
  543. xfs_inode_t *ip,
  544. unsigned int lock_mode)
  545. {
  546. xfs_iunlock(ip, lock_mode);
  547. }
  548. /*
  549. * The xfs inode contains 2 locks: a multi-reader lock called the
  550. * i_iolock and a multi-reader lock called the i_lock. This routine
  551. * allows either or both of the locks to be obtained.
  552. *
  553. * The 2 locks should always be ordered so that the IO lock is
  554. * obtained first in order to prevent deadlock.
  555. *
  556. * ip -- the inode being locked
  557. * lock_flags -- this parameter indicates the inode's locks
  558. * to be locked. It can be:
  559. * XFS_IOLOCK_SHARED,
  560. * XFS_IOLOCK_EXCL,
  561. * XFS_ILOCK_SHARED,
  562. * XFS_ILOCK_EXCL,
  563. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  564. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  565. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  566. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  567. */
  568. void
  569. xfs_ilock(
  570. xfs_inode_t *ip,
  571. uint lock_flags)
  572. {
  573. /*
  574. * You can't set both SHARED and EXCL for the same lock,
  575. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  576. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  577. */
  578. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  579. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  580. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  581. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  582. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  583. if (lock_flags & XFS_IOLOCK_EXCL)
  584. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  585. else if (lock_flags & XFS_IOLOCK_SHARED)
  586. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  587. if (lock_flags & XFS_ILOCK_EXCL)
  588. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  589. else if (lock_flags & XFS_ILOCK_SHARED)
  590. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  591. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  592. }
  593. /*
  594. * This is just like xfs_ilock(), except that the caller
  595. * is guaranteed not to sleep. It returns 1 if it gets
  596. * the requested locks and 0 otherwise. If the IO lock is
  597. * obtained but the inode lock cannot be, then the IO lock
  598. * is dropped before returning.
  599. *
  600. * ip -- the inode being locked
  601. * lock_flags -- this parameter indicates the inode's locks to be
  602. * to be locked. See the comment for xfs_ilock() for a list
  603. * of valid values.
  604. */
  605. int
  606. xfs_ilock_nowait(
  607. xfs_inode_t *ip,
  608. uint lock_flags)
  609. {
  610. /*
  611. * You can't set both SHARED and EXCL for the same lock,
  612. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  613. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  614. */
  615. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  616. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  617. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  618. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  619. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  620. if (lock_flags & XFS_IOLOCK_EXCL) {
  621. if (!mrtryupdate(&ip->i_iolock))
  622. goto out;
  623. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  624. if (!mrtryaccess(&ip->i_iolock))
  625. goto out;
  626. }
  627. if (lock_flags & XFS_ILOCK_EXCL) {
  628. if (!mrtryupdate(&ip->i_lock))
  629. goto out_undo_iolock;
  630. } else if (lock_flags & XFS_ILOCK_SHARED) {
  631. if (!mrtryaccess(&ip->i_lock))
  632. goto out_undo_iolock;
  633. }
  634. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  635. return 1;
  636. out_undo_iolock:
  637. if (lock_flags & XFS_IOLOCK_EXCL)
  638. mrunlock_excl(&ip->i_iolock);
  639. else if (lock_flags & XFS_IOLOCK_SHARED)
  640. mrunlock_shared(&ip->i_iolock);
  641. out:
  642. return 0;
  643. }
  644. /*
  645. * xfs_iunlock() is used to drop the inode locks acquired with
  646. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  647. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  648. * that we know which locks to drop.
  649. *
  650. * ip -- the inode being unlocked
  651. * lock_flags -- this parameter indicates the inode's locks to be
  652. * to be unlocked. See the comment for xfs_ilock() for a list
  653. * of valid values for this parameter.
  654. *
  655. */
  656. void
  657. xfs_iunlock(
  658. xfs_inode_t *ip,
  659. uint lock_flags)
  660. {
  661. /*
  662. * You can't set both SHARED and EXCL for the same lock,
  663. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  664. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  665. */
  666. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  667. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  668. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  669. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  670. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
  671. XFS_LOCK_DEP_MASK)) == 0);
  672. ASSERT(lock_flags != 0);
  673. if (lock_flags & XFS_IOLOCK_EXCL)
  674. mrunlock_excl(&ip->i_iolock);
  675. else if (lock_flags & XFS_IOLOCK_SHARED)
  676. mrunlock_shared(&ip->i_iolock);
  677. if (lock_flags & XFS_ILOCK_EXCL)
  678. mrunlock_excl(&ip->i_lock);
  679. else if (lock_flags & XFS_ILOCK_SHARED)
  680. mrunlock_shared(&ip->i_lock);
  681. if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
  682. !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
  683. /*
  684. * Let the AIL know that this item has been unlocked in case
  685. * it is in the AIL and anyone is waiting on it. Don't do
  686. * this if the caller has asked us not to.
  687. */
  688. xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
  689. (xfs_log_item_t*)(ip->i_itemp));
  690. }
  691. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  692. }
  693. /*
  694. * give up write locks. the i/o lock cannot be held nested
  695. * if it is being demoted.
  696. */
  697. void
  698. xfs_ilock_demote(
  699. xfs_inode_t *ip,
  700. uint lock_flags)
  701. {
  702. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  703. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  704. if (lock_flags & XFS_ILOCK_EXCL)
  705. mrdemote(&ip->i_lock);
  706. if (lock_flags & XFS_IOLOCK_EXCL)
  707. mrdemote(&ip->i_iolock);
  708. }
  709. #ifdef DEBUG
  710. /*
  711. * Debug-only routine, without additional rw_semaphore APIs, we can
  712. * now only answer requests regarding whether we hold the lock for write
  713. * (reader state is outside our visibility, we only track writer state).
  714. *
  715. * Note: this means !xfs_isilocked would give false positives, so don't do that.
  716. */
  717. int
  718. xfs_isilocked(
  719. xfs_inode_t *ip,
  720. uint lock_flags)
  721. {
  722. if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
  723. XFS_ILOCK_EXCL) {
  724. if (!ip->i_lock.mr_writer)
  725. return 0;
  726. }
  727. if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
  728. XFS_IOLOCK_EXCL) {
  729. if (!ip->i_iolock.mr_writer)
  730. return 0;
  731. }
  732. return 1;
  733. }
  734. #endif
  735. #ifdef XFS_INODE_TRACE
  736. #define KTRACE_ENTER(ip, vk, s, line, ra) \
  737. ktrace_enter((ip)->i_trace, \
  738. /* 0 */ (void *)(__psint_t)(vk), \
  739. /* 1 */ (void *)(s), \
  740. /* 2 */ (void *)(__psint_t) line, \
  741. /* 3 */ (void *)(__psint_t)atomic_read(&VFS_I(ip)->i_count), \
  742. /* 4 */ (void *)(ra), \
  743. /* 5 */ NULL, \
  744. /* 6 */ (void *)(__psint_t)current_cpu(), \
  745. /* 7 */ (void *)(__psint_t)current_pid(), \
  746. /* 8 */ (void *)__return_address, \
  747. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  748. /*
  749. * Vnode tracing code.
  750. */
  751. void
  752. _xfs_itrace_entry(xfs_inode_t *ip, const char *func, inst_t *ra)
  753. {
  754. KTRACE_ENTER(ip, INODE_KTRACE_ENTRY, func, 0, ra);
  755. }
  756. void
  757. _xfs_itrace_exit(xfs_inode_t *ip, const char *func, inst_t *ra)
  758. {
  759. KTRACE_ENTER(ip, INODE_KTRACE_EXIT, func, 0, ra);
  760. }
  761. void
  762. xfs_itrace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  763. {
  764. KTRACE_ENTER(ip, INODE_KTRACE_HOLD, file, line, ra);
  765. }
  766. void
  767. _xfs_itrace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  768. {
  769. KTRACE_ENTER(ip, INODE_KTRACE_REF, file, line, ra);
  770. }
  771. void
  772. xfs_itrace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  773. {
  774. KTRACE_ENTER(ip, INODE_KTRACE_RELE, file, line, ra);
  775. }
  776. #endif /* XFS_INODE_TRACE */