xfs_iget.c 23 KB

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