xfs_iget.c 18 KB

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