xfs_iget.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_dinode.h"
  33. #include "xfs_inode.h"
  34. #include "xfs_btree.h"
  35. #include "xfs_ialloc.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_utils.h"
  38. #include "xfs_trans_priv.h"
  39. #include "xfs_inode_item.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_trace.h"
  42. /*
  43. * Define xfs inode iolock lockdep classes. We need to ensure that all active
  44. * inodes are considered the same for lockdep purposes, including inodes that
  45. * are recycled through the XFS_IRECLAIMABLE state. This is the the only way to
  46. * guarantee the locks are considered the same when there are multiple lock
  47. * initialisation siteѕ. Also, define a reclaimable inode class so it is
  48. * obvious in lockdep reports which class the report is against.
  49. */
  50. static struct lock_class_key xfs_iolock_active;
  51. struct lock_class_key xfs_iolock_reclaimable;
  52. /*
  53. * Allocate and initialise an xfs_inode.
  54. */
  55. STATIC struct xfs_inode *
  56. xfs_inode_alloc(
  57. struct xfs_mount *mp,
  58. xfs_ino_t ino)
  59. {
  60. struct xfs_inode *ip;
  61. /*
  62. * if this didn't occur in transactions, we could use
  63. * KM_MAYFAIL and return NULL here on ENOMEM. Set the
  64. * code up to do this anyway.
  65. */
  66. ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
  67. if (!ip)
  68. return NULL;
  69. if (inode_init_always(mp->m_super, VFS_I(ip))) {
  70. kmem_zone_free(xfs_inode_zone, ip);
  71. return NULL;
  72. }
  73. ASSERT(atomic_read(&ip->i_pincount) == 0);
  74. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  75. ASSERT(completion_done(&ip->i_flush));
  76. ASSERT(ip->i_ino == 0);
  77. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  78. lockdep_set_class_and_name(&ip->i_iolock.mr_lock,
  79. &xfs_iolock_active, "xfs_iolock_active");
  80. /* initialise the xfs inode */
  81. ip->i_ino = ino;
  82. ip->i_mount = mp;
  83. memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
  84. ip->i_afp = NULL;
  85. memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
  86. ip->i_flags = 0;
  87. ip->i_update_core = 0;
  88. ip->i_delayed_blks = 0;
  89. memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
  90. ip->i_size = 0;
  91. ip->i_new_size = 0;
  92. return ip;
  93. }
  94. STATIC void
  95. xfs_inode_free_callback(
  96. struct rcu_head *head)
  97. {
  98. struct inode *inode = container_of(head, struct inode, i_rcu);
  99. struct xfs_inode *ip = XFS_I(inode);
  100. INIT_LIST_HEAD(&inode->i_dentry);
  101. kmem_zone_free(xfs_inode_zone, ip);
  102. }
  103. void
  104. xfs_inode_free(
  105. struct xfs_inode *ip)
  106. {
  107. switch (ip->i_d.di_mode & S_IFMT) {
  108. case S_IFREG:
  109. case S_IFDIR:
  110. case S_IFLNK:
  111. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  112. break;
  113. }
  114. if (ip->i_afp)
  115. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  116. if (ip->i_itemp) {
  117. /*
  118. * Only if we are shutting down the fs will we see an
  119. * inode still in the AIL. If it is there, we should remove
  120. * it to prevent a use-after-free from occurring.
  121. */
  122. xfs_log_item_t *lip = &ip->i_itemp->ili_item;
  123. struct xfs_ail *ailp = lip->li_ailp;
  124. ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
  125. XFS_FORCED_SHUTDOWN(ip->i_mount));
  126. if (lip->li_flags & XFS_LI_IN_AIL) {
  127. spin_lock(&ailp->xa_lock);
  128. if (lip->li_flags & XFS_LI_IN_AIL)
  129. xfs_trans_ail_delete(ailp, lip);
  130. else
  131. spin_unlock(&ailp->xa_lock);
  132. }
  133. xfs_inode_item_destroy(ip);
  134. ip->i_itemp = NULL;
  135. }
  136. /* asserts to verify all state is correct here */
  137. ASSERT(atomic_read(&ip->i_pincount) == 0);
  138. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  139. ASSERT(completion_done(&ip->i_flush));
  140. /*
  141. * Because we use RCU freeing we need to ensure the inode always
  142. * appears to be reclaimed with an invalid inode number when in the
  143. * free state. The ip->i_flags_lock provides the barrier against lookup
  144. * races.
  145. */
  146. spin_lock(&ip->i_flags_lock);
  147. ip->i_flags = XFS_IRECLAIM;
  148. ip->i_ino = 0;
  149. spin_unlock(&ip->i_flags_lock);
  150. call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
  151. }
  152. /*
  153. * Check the validity of the inode we just found it the cache
  154. */
  155. static int
  156. xfs_iget_cache_hit(
  157. struct xfs_perag *pag,
  158. struct xfs_inode *ip,
  159. xfs_ino_t ino,
  160. int flags,
  161. int lock_flags) __releases(RCU)
  162. {
  163. struct inode *inode = VFS_I(ip);
  164. struct xfs_mount *mp = ip->i_mount;
  165. int error;
  166. /*
  167. * check for re-use of an inode within an RCU grace period due to the
  168. * radix tree nodes not being updated yet. We monitor for this by
  169. * setting the inode number to zero before freeing the inode structure.
  170. * If the inode has been reallocated and set up, then the inode number
  171. * will not match, so check for that, too.
  172. */
  173. spin_lock(&ip->i_flags_lock);
  174. if (ip->i_ino != ino) {
  175. trace_xfs_iget_skip(ip);
  176. XFS_STATS_INC(xs_ig_frecycle);
  177. error = EAGAIN;
  178. goto out_error;
  179. }
  180. /*
  181. * If we are racing with another cache hit that is currently
  182. * instantiating this inode or currently recycling it out of
  183. * reclaimabe state, wait for the initialisation to complete
  184. * before continuing.
  185. *
  186. * XXX(hch): eventually we should do something equivalent to
  187. * wait_on_inode to wait for these flags to be cleared
  188. * instead of polling for it.
  189. */
  190. if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
  191. trace_xfs_iget_skip(ip);
  192. XFS_STATS_INC(xs_ig_frecycle);
  193. error = EAGAIN;
  194. goto out_error;
  195. }
  196. /*
  197. * If lookup is racing with unlink return an error immediately.
  198. */
  199. if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  200. error = ENOENT;
  201. goto out_error;
  202. }
  203. /*
  204. * If IRECLAIMABLE is set, we've torn down the VFS inode already.
  205. * Need to carefully get it back into useable state.
  206. */
  207. if (ip->i_flags & XFS_IRECLAIMABLE) {
  208. trace_xfs_iget_reclaim(ip);
  209. /*
  210. * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
  211. * from stomping over us while we recycle the inode. We can't
  212. * clear the radix tree reclaimable tag yet as it requires
  213. * pag_ici_lock to be held exclusive.
  214. */
  215. ip->i_flags |= XFS_IRECLAIM;
  216. spin_unlock(&ip->i_flags_lock);
  217. rcu_read_unlock();
  218. error = -inode_init_always(mp->m_super, inode);
  219. if (error) {
  220. /*
  221. * Re-initializing the inode failed, and we are in deep
  222. * trouble. Try to re-add it to the reclaim list.
  223. */
  224. rcu_read_lock();
  225. spin_lock(&ip->i_flags_lock);
  226. ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
  227. ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
  228. trace_xfs_iget_reclaim_fail(ip);
  229. goto out_error;
  230. }
  231. spin_lock(&pag->pag_ici_lock);
  232. spin_lock(&ip->i_flags_lock);
  233. /*
  234. * Clear the per-lifetime state in the inode as we are now
  235. * effectively a new inode and need to return to the initial
  236. * state before reuse occurs.
  237. */
  238. ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
  239. ip->i_flags |= XFS_INEW;
  240. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  241. inode->i_state = I_NEW;
  242. ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
  243. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  244. lockdep_set_class_and_name(&ip->i_iolock.mr_lock,
  245. &xfs_iolock_active, "xfs_iolock_active");
  246. spin_unlock(&ip->i_flags_lock);
  247. spin_unlock(&pag->pag_ici_lock);
  248. } else {
  249. /* If the VFS inode is being torn down, pause and try again. */
  250. if (!igrab(inode)) {
  251. trace_xfs_iget_skip(ip);
  252. error = EAGAIN;
  253. goto out_error;
  254. }
  255. /* We've got a live one. */
  256. spin_unlock(&ip->i_flags_lock);
  257. rcu_read_unlock();
  258. trace_xfs_iget_hit(ip);
  259. }
  260. if (lock_flags != 0)
  261. xfs_ilock(ip, lock_flags);
  262. xfs_iflags_clear(ip, XFS_ISTALE);
  263. XFS_STATS_INC(xs_ig_found);
  264. return 0;
  265. out_error:
  266. spin_unlock(&ip->i_flags_lock);
  267. rcu_read_unlock();
  268. return error;
  269. }
  270. static int
  271. xfs_iget_cache_miss(
  272. struct xfs_mount *mp,
  273. struct xfs_perag *pag,
  274. xfs_trans_t *tp,
  275. xfs_ino_t ino,
  276. struct xfs_inode **ipp,
  277. int flags,
  278. int lock_flags)
  279. {
  280. struct xfs_inode *ip;
  281. int error;
  282. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
  283. ip = xfs_inode_alloc(mp, ino);
  284. if (!ip)
  285. return ENOMEM;
  286. error = xfs_iread(mp, tp, ip, flags);
  287. if (error)
  288. goto out_destroy;
  289. trace_xfs_iget_miss(ip);
  290. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  291. error = ENOENT;
  292. goto out_destroy;
  293. }
  294. /*
  295. * Preload the radix tree so we can insert safely under the
  296. * write spinlock. Note that we cannot sleep inside the preload
  297. * region.
  298. */
  299. if (radix_tree_preload(GFP_KERNEL)) {
  300. error = EAGAIN;
  301. goto out_destroy;
  302. }
  303. /*
  304. * Because the inode hasn't been added to the radix-tree yet it can't
  305. * be found by another thread, so we can do the non-sleeping lock here.
  306. */
  307. if (lock_flags) {
  308. if (!xfs_ilock_nowait(ip, lock_flags))
  309. BUG();
  310. }
  311. spin_lock(&pag->pag_ici_lock);
  312. /* insert the new inode */
  313. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  314. if (unlikely(error)) {
  315. WARN_ON(error != -EEXIST);
  316. XFS_STATS_INC(xs_ig_dup);
  317. error = EAGAIN;
  318. goto out_preload_end;
  319. }
  320. /* These values _must_ be set before releasing the radix tree lock! */
  321. ip->i_udquot = ip->i_gdquot = NULL;
  322. xfs_iflags_set(ip, XFS_INEW);
  323. spin_unlock(&pag->pag_ici_lock);
  324. radix_tree_preload_end();
  325. *ipp = ip;
  326. return 0;
  327. out_preload_end:
  328. spin_unlock(&pag->pag_ici_lock);
  329. radix_tree_preload_end();
  330. if (lock_flags)
  331. xfs_iunlock(ip, lock_flags);
  332. out_destroy:
  333. __destroy_inode(VFS_I(ip));
  334. xfs_inode_free(ip);
  335. return error;
  336. }
  337. /*
  338. * Look up an inode by number in the given file system.
  339. * The inode is looked up in the cache held in each AG.
  340. * If the inode is found in the cache, initialise the vfs inode
  341. * if necessary.
  342. *
  343. * If it is not in core, read it in from the file system's device,
  344. * add it to the cache and initialise the vfs inode.
  345. *
  346. * The inode is locked according to the value of the lock_flags parameter.
  347. * This flag parameter indicates how and if the inode's IO lock and inode lock
  348. * should be taken.
  349. *
  350. * mp -- the mount point structure for the current file system. It points
  351. * to the inode hash table.
  352. * tp -- a pointer to the current transaction if there is one. This is
  353. * simply passed through to the xfs_iread() call.
  354. * ino -- the number of the inode desired. This is the unique identifier
  355. * within the file system for the inode being requested.
  356. * lock_flags -- flags indicating how to lock the inode. See the comment
  357. * for xfs_ilock() for a list of valid values.
  358. */
  359. int
  360. xfs_iget(
  361. xfs_mount_t *mp,
  362. xfs_trans_t *tp,
  363. xfs_ino_t ino,
  364. uint flags,
  365. uint lock_flags,
  366. xfs_inode_t **ipp)
  367. {
  368. xfs_inode_t *ip;
  369. int error;
  370. xfs_perag_t *pag;
  371. xfs_agino_t agino;
  372. /* reject inode numbers outside existing AGs */
  373. if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
  374. return EINVAL;
  375. /* get the perag structure and ensure that it's inode capable */
  376. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
  377. agino = XFS_INO_TO_AGINO(mp, ino);
  378. again:
  379. error = 0;
  380. rcu_read_lock();
  381. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  382. if (ip) {
  383. error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
  384. if (error)
  385. goto out_error_or_again;
  386. } else {
  387. rcu_read_unlock();
  388. XFS_STATS_INC(xs_ig_missed);
  389. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
  390. flags, lock_flags);
  391. if (error)
  392. goto out_error_or_again;
  393. }
  394. xfs_perag_put(pag);
  395. *ipp = ip;
  396. ASSERT(ip->i_df.if_ext_max ==
  397. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  398. /*
  399. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  400. * now. If it's a new inode being created, xfs_ialloc will handle it.
  401. */
  402. if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
  403. xfs_setup_inode(ip);
  404. return 0;
  405. out_error_or_again:
  406. if (error == EAGAIN) {
  407. delay(1);
  408. goto again;
  409. }
  410. xfs_perag_put(pag);
  411. return error;
  412. }
  413. /*
  414. * This is a wrapper routine around the xfs_ilock() routine
  415. * used to centralize some grungy code. It is used in places
  416. * that wish to lock the inode solely for reading the extents.
  417. * The reason these places can't just call xfs_ilock(SHARED)
  418. * is that the inode lock also guards to bringing in of the
  419. * extents from disk for a file in b-tree format. If the inode
  420. * is in b-tree format, then we need to lock the inode exclusively
  421. * until the extents are read in. Locking it exclusively all
  422. * the time would limit our parallelism unnecessarily, though.
  423. * What we do instead is check to see if the extents have been
  424. * read in yet, and only lock the inode exclusively if they
  425. * have not.
  426. *
  427. * The function returns a value which should be given to the
  428. * corresponding xfs_iunlock_map_shared(). This value is
  429. * the mode in which the lock was actually taken.
  430. */
  431. uint
  432. xfs_ilock_map_shared(
  433. xfs_inode_t *ip)
  434. {
  435. uint lock_mode;
  436. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  437. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  438. lock_mode = XFS_ILOCK_EXCL;
  439. } else {
  440. lock_mode = XFS_ILOCK_SHARED;
  441. }
  442. xfs_ilock(ip, lock_mode);
  443. return lock_mode;
  444. }
  445. /*
  446. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  447. * All it does is call xfs_iunlock() with the given lock_mode.
  448. */
  449. void
  450. xfs_iunlock_map_shared(
  451. xfs_inode_t *ip,
  452. unsigned int lock_mode)
  453. {
  454. xfs_iunlock(ip, lock_mode);
  455. }
  456. /*
  457. * The xfs inode contains 2 locks: a multi-reader lock called the
  458. * i_iolock and a multi-reader lock called the i_lock. This routine
  459. * allows either or both of the locks to be obtained.
  460. *
  461. * The 2 locks should always be ordered so that the IO lock is
  462. * obtained first in order to prevent deadlock.
  463. *
  464. * ip -- the inode being locked
  465. * lock_flags -- this parameter indicates the inode's locks
  466. * to be locked. It can be:
  467. * XFS_IOLOCK_SHARED,
  468. * XFS_IOLOCK_EXCL,
  469. * XFS_ILOCK_SHARED,
  470. * XFS_ILOCK_EXCL,
  471. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  472. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  473. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  474. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  475. */
  476. void
  477. xfs_ilock(
  478. xfs_inode_t *ip,
  479. uint lock_flags)
  480. {
  481. /*
  482. * You can't set both SHARED and EXCL for the same lock,
  483. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  484. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  485. */
  486. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  487. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  488. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  489. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  490. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  491. if (lock_flags & XFS_IOLOCK_EXCL)
  492. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  493. else if (lock_flags & XFS_IOLOCK_SHARED)
  494. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  495. if (lock_flags & XFS_ILOCK_EXCL)
  496. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  497. else if (lock_flags & XFS_ILOCK_SHARED)
  498. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  499. trace_xfs_ilock(ip, lock_flags, _RET_IP_);
  500. }
  501. /*
  502. * This is just like xfs_ilock(), except that the caller
  503. * is guaranteed not to sleep. It returns 1 if it gets
  504. * the requested locks and 0 otherwise. If the IO lock is
  505. * obtained but the inode lock cannot be, then the IO lock
  506. * is dropped before returning.
  507. *
  508. * ip -- the inode being locked
  509. * lock_flags -- this parameter indicates the inode's locks to be
  510. * to be locked. See the comment for xfs_ilock() for a list
  511. * of valid values.
  512. */
  513. int
  514. xfs_ilock_nowait(
  515. xfs_inode_t *ip,
  516. uint lock_flags)
  517. {
  518. /*
  519. * You can't set both SHARED and EXCL for the same lock,
  520. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  521. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  522. */
  523. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  524. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  525. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  526. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  527. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  528. if (lock_flags & XFS_IOLOCK_EXCL) {
  529. if (!mrtryupdate(&ip->i_iolock))
  530. goto out;
  531. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  532. if (!mrtryaccess(&ip->i_iolock))
  533. goto out;
  534. }
  535. if (lock_flags & XFS_ILOCK_EXCL) {
  536. if (!mrtryupdate(&ip->i_lock))
  537. goto out_undo_iolock;
  538. } else if (lock_flags & XFS_ILOCK_SHARED) {
  539. if (!mrtryaccess(&ip->i_lock))
  540. goto out_undo_iolock;
  541. }
  542. trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
  543. return 1;
  544. out_undo_iolock:
  545. if (lock_flags & XFS_IOLOCK_EXCL)
  546. mrunlock_excl(&ip->i_iolock);
  547. else if (lock_flags & XFS_IOLOCK_SHARED)
  548. mrunlock_shared(&ip->i_iolock);
  549. out:
  550. return 0;
  551. }
  552. /*
  553. * xfs_iunlock() is used to drop the inode locks acquired with
  554. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  555. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  556. * that we know which locks to drop.
  557. *
  558. * ip -- the inode being unlocked
  559. * lock_flags -- this parameter indicates the inode's locks to be
  560. * to be unlocked. See the comment for xfs_ilock() for a list
  561. * of valid values for this parameter.
  562. *
  563. */
  564. void
  565. xfs_iunlock(
  566. xfs_inode_t *ip,
  567. uint lock_flags)
  568. {
  569. /*
  570. * You can't set both SHARED and EXCL for the same lock,
  571. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  572. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  573. */
  574. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  575. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  576. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  577. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  578. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
  579. XFS_LOCK_DEP_MASK)) == 0);
  580. ASSERT(lock_flags != 0);
  581. if (lock_flags & XFS_IOLOCK_EXCL)
  582. mrunlock_excl(&ip->i_iolock);
  583. else if (lock_flags & XFS_IOLOCK_SHARED)
  584. mrunlock_shared(&ip->i_iolock);
  585. if (lock_flags & XFS_ILOCK_EXCL)
  586. mrunlock_excl(&ip->i_lock);
  587. else if (lock_flags & XFS_ILOCK_SHARED)
  588. mrunlock_shared(&ip->i_lock);
  589. if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
  590. !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
  591. /*
  592. * Let the AIL know that this item has been unlocked in case
  593. * it is in the AIL and anyone is waiting on it. Don't do
  594. * this if the caller has asked us not to.
  595. */
  596. xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
  597. (xfs_log_item_t*)(ip->i_itemp));
  598. }
  599. trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
  600. }
  601. /*
  602. * give up write locks. the i/o lock cannot be held nested
  603. * if it is being demoted.
  604. */
  605. void
  606. xfs_ilock_demote(
  607. xfs_inode_t *ip,
  608. uint lock_flags)
  609. {
  610. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  611. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  612. if (lock_flags & XFS_ILOCK_EXCL)
  613. mrdemote(&ip->i_lock);
  614. if (lock_flags & XFS_IOLOCK_EXCL)
  615. mrdemote(&ip->i_iolock);
  616. trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
  617. }
  618. #ifdef DEBUG
  619. int
  620. xfs_isilocked(
  621. xfs_inode_t *ip,
  622. uint lock_flags)
  623. {
  624. if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
  625. if (!(lock_flags & XFS_ILOCK_SHARED))
  626. return !!ip->i_lock.mr_writer;
  627. return rwsem_is_locked(&ip->i_lock.mr_lock);
  628. }
  629. if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
  630. if (!(lock_flags & XFS_IOLOCK_SHARED))
  631. return !!ip->i_iolock.mr_writer;
  632. return rwsem_is_locked(&ip->i_iolock.mr_lock);
  633. }
  634. ASSERT(0);
  635. return 0;
  636. }
  637. #endif