xfs_iget.c 20 KB

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