xfs_iget.c 20 KB

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