xfs_iget.c 20 KB

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