xfs_iget.c 18 KB

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