xfs_iget.c 23 KB

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