xfs_iget.c 23 KB

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