xfs_iget.c 18 KB

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