xfs_iget.c 18 KB

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