xfs_iget.c 19 KB

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