xfs_iget.c 23 KB

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