xfs_iget.c 18 KB

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