xfs_iget.c 20 KB

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