xfs_iget.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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_bit.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_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_utils.h"
  41. /*
  42. * Check the validity of the inode we just found it the cache
  43. */
  44. static int
  45. xfs_iget_cache_hit(
  46. struct xfs_perag *pag,
  47. struct xfs_inode *ip,
  48. int flags,
  49. int lock_flags) __releases(pag->pag_ici_lock)
  50. {
  51. struct xfs_mount *mp = ip->i_mount;
  52. int error = 0;
  53. /*
  54. * If INEW is set this inode is being set up
  55. * If IRECLAIM is set this inode is being torn down
  56. * Pause and try again.
  57. */
  58. if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) {
  59. error = EAGAIN;
  60. XFS_STATS_INC(xs_ig_frecycle);
  61. goto out_error;
  62. }
  63. /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
  64. if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
  65. /*
  66. * If lookup is racing with unlink, then we should return an
  67. * error immediately so we don't remove it from the reclaim
  68. * list and potentially leak the inode.
  69. */
  70. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  71. error = ENOENT;
  72. goto out_error;
  73. }
  74. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  75. /*
  76. * We need to re-initialise the VFS inode as it has been
  77. * 'freed' by the VFS. Do this here so we can deal with
  78. * errors cleanly, then tag it so it can be set up correctly
  79. * later.
  80. */
  81. if (!inode_init_always(mp->m_super, VFS_I(ip))) {
  82. error = ENOMEM;
  83. goto out_error;
  84. }
  85. xfs_iflags_set(ip, XFS_INEW);
  86. xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
  87. /* clear the radix tree reclaim flag as well. */
  88. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  89. read_unlock(&pag->pag_ici_lock);
  90. } else if (!igrab(VFS_I(ip))) {
  91. /* If the VFS inode is being torn down, pause and try again. */
  92. error = EAGAIN;
  93. XFS_STATS_INC(xs_ig_frecycle);
  94. goto out_error;
  95. } else {
  96. /* we've got a live one */
  97. read_unlock(&pag->pag_ici_lock);
  98. }
  99. if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  100. error = ENOENT;
  101. goto out;
  102. }
  103. if (lock_flags != 0)
  104. xfs_ilock(ip, lock_flags);
  105. xfs_iflags_clear(ip, XFS_ISTALE);
  106. xfs_itrace_exit_tag(ip, "xfs_iget.found");
  107. XFS_STATS_INC(xs_ig_found);
  108. return 0;
  109. out_error:
  110. read_unlock(&pag->pag_ici_lock);
  111. out:
  112. return error;
  113. }
  114. static int
  115. xfs_iget_cache_miss(
  116. struct xfs_mount *mp,
  117. struct xfs_perag *pag,
  118. xfs_trans_t *tp,
  119. xfs_ino_t ino,
  120. struct xfs_inode **ipp,
  121. xfs_daddr_t bno,
  122. int flags,
  123. int lock_flags) __releases(pag->pag_ici_lock)
  124. {
  125. struct xfs_inode *ip;
  126. int error;
  127. unsigned long first_index, mask;
  128. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
  129. /*
  130. * Read the disk inode attributes into a new inode structure and get
  131. * a new vnode for it. This should also initialize i_ino and i_mount.
  132. */
  133. error = xfs_iread(mp, tp, ino, &ip, bno,
  134. (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
  135. if (error)
  136. return error;
  137. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  138. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  139. error = ENOENT;
  140. goto out_destroy;
  141. }
  142. /*
  143. * Preload the radix tree so we can insert safely under the
  144. * write spinlock.
  145. */
  146. if (radix_tree_preload(GFP_KERNEL)) {
  147. error = EAGAIN;
  148. goto out_destroy;
  149. }
  150. if (lock_flags)
  151. xfs_ilock(ip, lock_flags);
  152. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  153. first_index = agino & mask;
  154. write_lock(&pag->pag_ici_lock);
  155. /* insert the new inode */
  156. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  157. if (unlikely(error)) {
  158. WARN_ON(error != -EEXIST);
  159. XFS_STATS_INC(xs_ig_dup);
  160. error = EAGAIN;
  161. goto out_unlock;
  162. }
  163. /* These values _must_ be set before releasing the radix tree lock! */
  164. ip->i_udquot = ip->i_gdquot = NULL;
  165. xfs_iflags_set(ip, XFS_INEW);
  166. write_unlock(&pag->pag_ici_lock);
  167. radix_tree_preload_end();
  168. *ipp = ip;
  169. return 0;
  170. out_unlock:
  171. write_unlock(&pag->pag_ici_lock);
  172. radix_tree_preload_end();
  173. out_destroy:
  174. xfs_idestroy(ip);
  175. return error;
  176. }
  177. /*
  178. * Look up an inode by number in the given file system.
  179. * The inode is looked up in the cache held in each AG.
  180. * If the inode is found in the cache, initialise the vfs inode
  181. * if necessary.
  182. *
  183. * If it is not in core, read it in from the file system's device,
  184. * add it to the cache and initialise the vfs inode.
  185. *
  186. * The inode is locked according to the value of the lock_flags parameter.
  187. * This flag parameter indicates how and if the inode's IO lock and inode lock
  188. * should be taken.
  189. *
  190. * mp -- the mount point structure for the current file system. It points
  191. * to the inode hash table.
  192. * tp -- a pointer to the current transaction if there is one. This is
  193. * simply passed through to the xfs_iread() call.
  194. * ino -- the number of the inode desired. This is the unique identifier
  195. * within the file system for the inode being requested.
  196. * lock_flags -- flags indicating how to lock the inode. See the comment
  197. * for xfs_ilock() for a list of valid values.
  198. * bno -- the block number starting the buffer containing the inode,
  199. * if known (as by bulkstat), else 0.
  200. */
  201. int
  202. xfs_iget(
  203. xfs_mount_t *mp,
  204. xfs_trans_t *tp,
  205. xfs_ino_t ino,
  206. uint flags,
  207. uint lock_flags,
  208. xfs_inode_t **ipp,
  209. xfs_daddr_t bno)
  210. {
  211. xfs_inode_t *ip;
  212. int error;
  213. xfs_perag_t *pag;
  214. xfs_agino_t agino;
  215. /* the radix tree exists only in inode capable AGs */
  216. if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
  217. return EINVAL;
  218. /* get the perag structure and ensure that it's inode capable */
  219. pag = xfs_get_perag(mp, ino);
  220. if (!pag->pagi_inodeok)
  221. return EINVAL;
  222. ASSERT(pag->pag_ici_init);
  223. agino = XFS_INO_TO_AGINO(mp, ino);
  224. again:
  225. error = 0;
  226. read_lock(&pag->pag_ici_lock);
  227. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  228. if (ip) {
  229. error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
  230. if (error)
  231. goto out_error_or_again;
  232. } else {
  233. read_unlock(&pag->pag_ici_lock);
  234. XFS_STATS_INC(xs_ig_missed);
  235. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
  236. flags, lock_flags);
  237. if (error)
  238. goto out_error_or_again;
  239. }
  240. xfs_put_perag(mp, pag);
  241. xfs_iflags_set(ip, XFS_IMODIFIED);
  242. *ipp = ip;
  243. ASSERT(ip->i_df.if_ext_max ==
  244. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  245. /*
  246. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  247. * now. If it's a new inode being created, xfs_ialloc will handle it.
  248. */
  249. if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
  250. xfs_setup_inode(ip);
  251. return 0;
  252. out_error_or_again:
  253. if (error == EAGAIN) {
  254. delay(1);
  255. goto again;
  256. }
  257. xfs_put_perag(mp, pag);
  258. return error;
  259. }
  260. /*
  261. * Look for the inode corresponding to the given ino in the hash table.
  262. * If it is there and its i_transp pointer matches tp, return it.
  263. * Otherwise, return NULL.
  264. */
  265. xfs_inode_t *
  266. xfs_inode_incore(xfs_mount_t *mp,
  267. xfs_ino_t ino,
  268. xfs_trans_t *tp)
  269. {
  270. xfs_inode_t *ip;
  271. xfs_perag_t *pag;
  272. pag = xfs_get_perag(mp, ino);
  273. read_lock(&pag->pag_ici_lock);
  274. ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
  275. read_unlock(&pag->pag_ici_lock);
  276. xfs_put_perag(mp, pag);
  277. /* the returned inode must match the transaction */
  278. if (ip && (ip->i_transp != tp))
  279. return NULL;
  280. return ip;
  281. }
  282. /*
  283. * Decrement reference count of an inode structure and unlock it.
  284. *
  285. * ip -- the inode being released
  286. * lock_flags -- this parameter indicates the inode's locks to be
  287. * to be released. See the comment on xfs_iunlock() for a list
  288. * of valid values.
  289. */
  290. void
  291. xfs_iput(xfs_inode_t *ip,
  292. uint lock_flags)
  293. {
  294. xfs_itrace_entry(ip);
  295. xfs_iunlock(ip, lock_flags);
  296. IRELE(ip);
  297. }
  298. /*
  299. * Special iput for brand-new inodes that are still locked
  300. */
  301. void
  302. xfs_iput_new(
  303. xfs_inode_t *ip,
  304. uint lock_flags)
  305. {
  306. struct inode *inode = VFS_I(ip);
  307. xfs_itrace_entry(ip);
  308. if ((ip->i_d.di_mode == 0)) {
  309. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  310. make_bad_inode(inode);
  311. }
  312. if (inode->i_state & I_NEW)
  313. unlock_new_inode(inode);
  314. if (lock_flags)
  315. xfs_iunlock(ip, lock_flags);
  316. IRELE(ip);
  317. }
  318. /*
  319. * This routine embodies the part of the reclaim code that pulls
  320. * the inode from the inode hash table and the mount structure's
  321. * inode list.
  322. * This should only be called from xfs_reclaim().
  323. */
  324. void
  325. xfs_ireclaim(xfs_inode_t *ip)
  326. {
  327. /*
  328. * Remove from old hash list and mount list.
  329. */
  330. XFS_STATS_INC(xs_ig_reclaims);
  331. xfs_iextract(ip);
  332. /*
  333. * Here we do a spurious inode lock in order to coordinate with inode
  334. * cache radix tree lookups. This is because the lookup can reference
  335. * the inodes in the cache without taking references. We make that OK
  336. * here by ensuring that we wait until the inode is unlocked after the
  337. * lookup before we go ahead and free it. We get both the ilock and
  338. * the iolock because the code may need to drop the ilock one but will
  339. * still hold the iolock.
  340. */
  341. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  342. /*
  343. * Release dquots (and their references) if any. An inode may escape
  344. * xfs_inactive and get here via vn_alloc->vn_reclaim path.
  345. */
  346. XFS_QM_DQDETACH(ip->i_mount, ip);
  347. /*
  348. * Free all memory associated with the inode.
  349. */
  350. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  351. xfs_idestroy(ip);
  352. }
  353. /*
  354. * This routine removes an about-to-be-destroyed inode from
  355. * all of the lists in which it is located with the exception
  356. * of the behavior chain.
  357. */
  358. void
  359. xfs_iextract(
  360. xfs_inode_t *ip)
  361. {
  362. xfs_mount_t *mp = ip->i_mount;
  363. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  364. write_lock(&pag->pag_ici_lock);
  365. radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
  366. write_unlock(&pag->pag_ici_lock);
  367. xfs_put_perag(mp, pag);
  368. mp->m_ireclaims++;
  369. }
  370. /*
  371. * This is a wrapper routine around the xfs_ilock() routine
  372. * used to centralize some grungy code. It is used in places
  373. * that wish to lock the inode solely for reading the extents.
  374. * The reason these places can't just call xfs_ilock(SHARED)
  375. * is that the inode lock also guards to bringing in of the
  376. * extents from disk for a file in b-tree format. If the inode
  377. * is in b-tree format, then we need to lock the inode exclusively
  378. * until the extents are read in. Locking it exclusively all
  379. * the time would limit our parallelism unnecessarily, though.
  380. * What we do instead is check to see if the extents have been
  381. * read in yet, and only lock the inode exclusively if they
  382. * have not.
  383. *
  384. * The function returns a value which should be given to the
  385. * corresponding xfs_iunlock_map_shared(). This value is
  386. * the mode in which the lock was actually taken.
  387. */
  388. uint
  389. xfs_ilock_map_shared(
  390. xfs_inode_t *ip)
  391. {
  392. uint lock_mode;
  393. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  394. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  395. lock_mode = XFS_ILOCK_EXCL;
  396. } else {
  397. lock_mode = XFS_ILOCK_SHARED;
  398. }
  399. xfs_ilock(ip, lock_mode);
  400. return lock_mode;
  401. }
  402. /*
  403. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  404. * All it does is call xfs_iunlock() with the given lock_mode.
  405. */
  406. void
  407. xfs_iunlock_map_shared(
  408. xfs_inode_t *ip,
  409. unsigned int lock_mode)
  410. {
  411. xfs_iunlock(ip, lock_mode);
  412. }
  413. /*
  414. * The xfs inode contains 2 locks: a multi-reader lock called the
  415. * i_iolock and a multi-reader lock called the i_lock. This routine
  416. * allows either or both of the locks to be obtained.
  417. *
  418. * The 2 locks should always be ordered so that the IO lock is
  419. * obtained first in order to prevent deadlock.
  420. *
  421. * ip -- the inode being locked
  422. * lock_flags -- this parameter indicates the inode's locks
  423. * to be locked. It can be:
  424. * XFS_IOLOCK_SHARED,
  425. * XFS_IOLOCK_EXCL,
  426. * XFS_ILOCK_SHARED,
  427. * XFS_ILOCK_EXCL,
  428. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  429. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  430. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  431. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  432. */
  433. void
  434. xfs_ilock(
  435. xfs_inode_t *ip,
  436. uint lock_flags)
  437. {
  438. /*
  439. * You can't set both SHARED and EXCL for the same lock,
  440. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  441. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  442. */
  443. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  444. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  445. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  446. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  447. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  448. if (lock_flags & XFS_IOLOCK_EXCL)
  449. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  450. else if (lock_flags & XFS_IOLOCK_SHARED)
  451. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  452. if (lock_flags & XFS_ILOCK_EXCL)
  453. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  454. else if (lock_flags & XFS_ILOCK_SHARED)
  455. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  456. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  457. }
  458. /*
  459. * This is just like xfs_ilock(), except that the caller
  460. * is guaranteed not to sleep. It returns 1 if it gets
  461. * the requested locks and 0 otherwise. If the IO lock is
  462. * obtained but the inode lock cannot be, then the IO lock
  463. * is dropped before returning.
  464. *
  465. * ip -- the inode being locked
  466. * lock_flags -- this parameter indicates the inode's locks to be
  467. * to be locked. See the comment for xfs_ilock() for a list
  468. * of valid values.
  469. */
  470. int
  471. xfs_ilock_nowait(
  472. xfs_inode_t *ip,
  473. uint lock_flags)
  474. {
  475. /*
  476. * You can't set both SHARED and EXCL for the same lock,
  477. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  478. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  479. */
  480. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  481. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  482. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  483. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  484. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  485. if (lock_flags & XFS_IOLOCK_EXCL) {
  486. if (!mrtryupdate(&ip->i_iolock))
  487. goto out;
  488. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  489. if (!mrtryaccess(&ip->i_iolock))
  490. goto out;
  491. }
  492. if (lock_flags & XFS_ILOCK_EXCL) {
  493. if (!mrtryupdate(&ip->i_lock))
  494. goto out_undo_iolock;
  495. } else if (lock_flags & XFS_ILOCK_SHARED) {
  496. if (!mrtryaccess(&ip->i_lock))
  497. goto out_undo_iolock;
  498. }
  499. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  500. return 1;
  501. out_undo_iolock:
  502. if (lock_flags & XFS_IOLOCK_EXCL)
  503. mrunlock_excl(&ip->i_iolock);
  504. else if (lock_flags & XFS_IOLOCK_SHARED)
  505. mrunlock_shared(&ip->i_iolock);
  506. out:
  507. return 0;
  508. }
  509. /*
  510. * xfs_iunlock() is used to drop the inode locks acquired with
  511. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  512. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  513. * that we know which locks to drop.
  514. *
  515. * ip -- the inode being unlocked
  516. * lock_flags -- this parameter indicates the inode's locks to be
  517. * to be unlocked. See the comment for xfs_ilock() for a list
  518. * of valid values for this parameter.
  519. *
  520. */
  521. void
  522. xfs_iunlock(
  523. xfs_inode_t *ip,
  524. uint lock_flags)
  525. {
  526. /*
  527. * You can't set both SHARED and EXCL for the same lock,
  528. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  529. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  530. */
  531. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  532. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  533. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  534. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  535. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
  536. XFS_LOCK_DEP_MASK)) == 0);
  537. ASSERT(lock_flags != 0);
  538. if (lock_flags & XFS_IOLOCK_EXCL)
  539. mrunlock_excl(&ip->i_iolock);
  540. else if (lock_flags & XFS_IOLOCK_SHARED)
  541. mrunlock_shared(&ip->i_iolock);
  542. if (lock_flags & XFS_ILOCK_EXCL)
  543. mrunlock_excl(&ip->i_lock);
  544. else if (lock_flags & XFS_ILOCK_SHARED)
  545. mrunlock_shared(&ip->i_lock);
  546. if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
  547. !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
  548. /*
  549. * Let the AIL know that this item has been unlocked in case
  550. * it is in the AIL and anyone is waiting on it. Don't do
  551. * this if the caller has asked us not to.
  552. */
  553. xfs_trans_unlocked_item(ip->i_mount,
  554. (xfs_log_item_t*)(ip->i_itemp));
  555. }
  556. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  557. }
  558. /*
  559. * give up write locks. the i/o lock cannot be held nested
  560. * if it is being demoted.
  561. */
  562. void
  563. xfs_ilock_demote(
  564. xfs_inode_t *ip,
  565. uint lock_flags)
  566. {
  567. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  568. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  569. if (lock_flags & XFS_ILOCK_EXCL)
  570. mrdemote(&ip->i_lock);
  571. if (lock_flags & XFS_IOLOCK_EXCL)
  572. mrdemote(&ip->i_iolock);
  573. }
  574. #ifdef DEBUG
  575. /*
  576. * Debug-only routine, without additional rw_semaphore APIs, we can
  577. * now only answer requests regarding whether we hold the lock for write
  578. * (reader state is outside our visibility, we only track writer state).
  579. *
  580. * Note: this means !xfs_isilocked would give false positives, so don't do that.
  581. */
  582. int
  583. xfs_isilocked(
  584. xfs_inode_t *ip,
  585. uint lock_flags)
  586. {
  587. if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
  588. XFS_ILOCK_EXCL) {
  589. if (!ip->i_lock.mr_writer)
  590. return 0;
  591. }
  592. if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
  593. XFS_IOLOCK_EXCL) {
  594. if (!ip->i_iolock.mr_writer)
  595. return 0;
  596. }
  597. return 1;
  598. }
  599. #endif