xfs_iget.c 19 KB

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