xfs_iget.c 18 KB

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