xfs_iget.c 19 KB

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