xfs_iget.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. * Look up an inode by number in the given file system.
  43. * The inode is looked up in the cache held in each AG.
  44. * If the inode is found in the cache, attach it to the provided
  45. * vnode.
  46. *
  47. * If it is not in core, read it in from the file system's device,
  48. * add it to the cache and attach the provided vnode.
  49. *
  50. * The inode is locked according to the value of the lock_flags parameter.
  51. * This flag parameter indicates how and if the inode's IO lock and inode lock
  52. * should be taken.
  53. *
  54. * mp -- the mount point structure for the current file system. It points
  55. * to the inode hash table.
  56. * tp -- a pointer to the current transaction if there is one. This is
  57. * simply passed through to the xfs_iread() call.
  58. * ino -- the number of the inode desired. This is the unique identifier
  59. * within the file system for the inode being requested.
  60. * lock_flags -- flags indicating how to lock the inode. See the comment
  61. * for xfs_ilock() for a list of valid values.
  62. * bno -- the block number starting the buffer containing the inode,
  63. * if known (as by bulkstat), else 0.
  64. */
  65. STATIC int
  66. xfs_iget_core(
  67. struct inode *inode,
  68. xfs_mount_t *mp,
  69. xfs_trans_t *tp,
  70. xfs_ino_t ino,
  71. uint flags,
  72. uint lock_flags,
  73. xfs_inode_t **ipp,
  74. xfs_daddr_t bno)
  75. {
  76. struct inode *old_inode;
  77. xfs_inode_t *ip;
  78. xfs_inode_t *iq;
  79. int error;
  80. unsigned long first_index, mask;
  81. xfs_perag_t *pag;
  82. xfs_agino_t agino;
  83. /* the radix tree exists only in inode capable AGs */
  84. if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
  85. return EINVAL;
  86. /* get the perag structure and ensure that it's inode capable */
  87. pag = xfs_get_perag(mp, ino);
  88. if (!pag->pagi_inodeok)
  89. return EINVAL;
  90. ASSERT(pag->pag_ici_init);
  91. agino = XFS_INO_TO_AGINO(mp, ino);
  92. again:
  93. read_lock(&pag->pag_ici_lock);
  94. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  95. if (ip != NULL) {
  96. /*
  97. * If INEW is set this inode is being set up
  98. * we need to pause and try again.
  99. */
  100. if (xfs_iflags_test(ip, XFS_INEW)) {
  101. read_unlock(&pag->pag_ici_lock);
  102. delay(1);
  103. XFS_STATS_INC(xs_ig_frecycle);
  104. goto again;
  105. }
  106. old_inode = ip->i_vnode;
  107. if (old_inode == NULL) {
  108. /*
  109. * If IRECLAIM is set this inode is
  110. * on its way out of the system,
  111. * we need to pause and try again.
  112. */
  113. if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
  114. read_unlock(&pag->pag_ici_lock);
  115. delay(1);
  116. XFS_STATS_INC(xs_ig_frecycle);
  117. goto again;
  118. }
  119. ASSERT(xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  120. /*
  121. * If lookup is racing with unlink, then we
  122. * should return an error immediately so we
  123. * don't remove it from the reclaim list and
  124. * potentially leak the inode.
  125. */
  126. if ((ip->i_d.di_mode == 0) &&
  127. !(flags & XFS_IGET_CREATE)) {
  128. read_unlock(&pag->pag_ici_lock);
  129. xfs_put_perag(mp, pag);
  130. return ENOENT;
  131. }
  132. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  133. XFS_STATS_INC(xs_ig_found);
  134. xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
  135. read_unlock(&pag->pag_ici_lock);
  136. XFS_MOUNT_ILOCK(mp);
  137. list_del_init(&ip->i_reclaim);
  138. XFS_MOUNT_IUNLOCK(mp);
  139. goto finish_inode;
  140. } else if (inode != old_inode) {
  141. /* The inode is being torn down, pause and
  142. * try again.
  143. */
  144. if (old_inode->i_state & (I_FREEING | I_CLEAR)) {
  145. read_unlock(&pag->pag_ici_lock);
  146. delay(1);
  147. XFS_STATS_INC(xs_ig_frecycle);
  148. goto again;
  149. }
  150. /* Chances are the other vnode (the one in the inode) is being torn
  151. * down right now, and we landed on top of it. Question is, what do
  152. * we do? Unhook the old inode and hook up the new one?
  153. */
  154. cmn_err(CE_PANIC,
  155. "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
  156. old_inode, inode);
  157. }
  158. /*
  159. * Inode cache hit
  160. */
  161. read_unlock(&pag->pag_ici_lock);
  162. XFS_STATS_INC(xs_ig_found);
  163. finish_inode:
  164. if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  165. xfs_put_perag(mp, pag);
  166. return ENOENT;
  167. }
  168. if (lock_flags != 0)
  169. xfs_ilock(ip, lock_flags);
  170. xfs_iflags_clear(ip, XFS_ISTALE);
  171. xfs_itrace_exit_tag(ip, "xfs_iget.found");
  172. goto return_ip;
  173. }
  174. /*
  175. * Inode cache miss
  176. */
  177. read_unlock(&pag->pag_ici_lock);
  178. XFS_STATS_INC(xs_ig_missed);
  179. /*
  180. * Read the disk inode attributes into a new inode structure and get
  181. * a new vnode for it. This should also initialize i_ino and i_mount.
  182. */
  183. error = xfs_iread(mp, tp, ino, &ip, bno,
  184. (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
  185. if (error) {
  186. xfs_put_perag(mp, pag);
  187. return error;
  188. }
  189. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  190. mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
  191. "xfsino", ip->i_ino);
  192. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  193. init_waitqueue_head(&ip->i_ipin_wait);
  194. atomic_set(&ip->i_pincount, 0);
  195. /*
  196. * Because we want to use a counting completion, complete
  197. * the flush completion once to allow a single access to
  198. * the flush completion without blocking.
  199. */
  200. init_completion(&ip->i_flush);
  201. complete(&ip->i_flush);
  202. if (lock_flags)
  203. xfs_ilock(ip, lock_flags);
  204. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  205. xfs_idestroy(ip);
  206. xfs_put_perag(mp, pag);
  207. return ENOENT;
  208. }
  209. /*
  210. * Preload the radix tree so we can insert safely under the
  211. * write spinlock.
  212. */
  213. if (radix_tree_preload(GFP_KERNEL)) {
  214. xfs_idestroy(ip);
  215. delay(1);
  216. goto again;
  217. }
  218. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  219. first_index = agino & mask;
  220. write_lock(&pag->pag_ici_lock);
  221. /*
  222. * insert the new inode
  223. */
  224. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  225. if (unlikely(error)) {
  226. BUG_ON(error != -EEXIST);
  227. write_unlock(&pag->pag_ici_lock);
  228. radix_tree_preload_end();
  229. xfs_idestroy(ip);
  230. XFS_STATS_INC(xs_ig_dup);
  231. goto again;
  232. }
  233. /*
  234. * These values _must_ be set before releasing the radix tree lock!
  235. */
  236. ip->i_udquot = ip->i_gdquot = NULL;
  237. xfs_iflags_set(ip, XFS_INEW);
  238. write_unlock(&pag->pag_ici_lock);
  239. radix_tree_preload_end();
  240. /*
  241. * Link ip to its mount and thread it on the mount's inode list.
  242. */
  243. XFS_MOUNT_ILOCK(mp);
  244. if ((iq = mp->m_inodes)) {
  245. ASSERT(iq->i_mprev->i_mnext == iq);
  246. ip->i_mprev = iq->i_mprev;
  247. iq->i_mprev->i_mnext = ip;
  248. iq->i_mprev = ip;
  249. ip->i_mnext = iq;
  250. } else {
  251. ip->i_mnext = ip;
  252. ip->i_mprev = ip;
  253. }
  254. mp->m_inodes = ip;
  255. XFS_MOUNT_IUNLOCK(mp);
  256. xfs_put_perag(mp, pag);
  257. return_ip:
  258. ASSERT(ip->i_df.if_ext_max ==
  259. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  260. xfs_iflags_set(ip, XFS_IMODIFIED);
  261. *ipp = ip;
  262. /*
  263. * Set up the Linux with the Linux inode.
  264. */
  265. ip->i_vnode = inode;
  266. inode->i_private = ip;
  267. /*
  268. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  269. * now. If it's a new inode being created, xfs_ialloc will handle it.
  270. */
  271. if (ip->i_d.di_mode != 0)
  272. xfs_setup_inode(ip);
  273. return 0;
  274. }
  275. /*
  276. * The 'normal' internal xfs_iget, if needed it will
  277. * 'allocate', or 'get', the vnode.
  278. */
  279. int
  280. xfs_iget(
  281. xfs_mount_t *mp,
  282. xfs_trans_t *tp,
  283. xfs_ino_t ino,
  284. uint flags,
  285. uint lock_flags,
  286. xfs_inode_t **ipp,
  287. xfs_daddr_t bno)
  288. {
  289. struct inode *inode;
  290. xfs_inode_t *ip;
  291. int error;
  292. XFS_STATS_INC(xs_ig_attempts);
  293. retry:
  294. inode = iget_locked(mp->m_super, ino);
  295. if (!inode)
  296. /* If we got no inode we are out of memory */
  297. return ENOMEM;
  298. if (inode->i_state & I_NEW) {
  299. XFS_STATS_INC(vn_active);
  300. XFS_STATS_INC(vn_alloc);
  301. error = xfs_iget_core(inode, mp, tp, ino, flags,
  302. lock_flags, ipp, bno);
  303. if (error) {
  304. make_bad_inode(inode);
  305. if (inode->i_state & I_NEW)
  306. unlock_new_inode(inode);
  307. iput(inode);
  308. }
  309. return error;
  310. }
  311. /*
  312. * If the inode is not fully constructed due to
  313. * filehandle mismatches wait for the inode to go
  314. * away and try again.
  315. *
  316. * iget_locked will call __wait_on_freeing_inode
  317. * to wait for the inode to go away.
  318. */
  319. if (is_bad_inode(inode)) {
  320. iput(inode);
  321. delay(1);
  322. goto retry;
  323. }
  324. ip = XFS_I(inode);
  325. if (!ip) {
  326. iput(inode);
  327. delay(1);
  328. goto retry;
  329. }
  330. if (lock_flags != 0)
  331. xfs_ilock(ip, lock_flags);
  332. XFS_STATS_INC(xs_ig_found);
  333. *ipp = ip;
  334. return 0;
  335. }
  336. /*
  337. * Look for the inode corresponding to the given ino in the hash table.
  338. * If it is there and its i_transp pointer matches tp, return it.
  339. * Otherwise, return NULL.
  340. */
  341. xfs_inode_t *
  342. xfs_inode_incore(xfs_mount_t *mp,
  343. xfs_ino_t ino,
  344. xfs_trans_t *tp)
  345. {
  346. xfs_inode_t *ip;
  347. xfs_perag_t *pag;
  348. pag = xfs_get_perag(mp, ino);
  349. read_lock(&pag->pag_ici_lock);
  350. ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
  351. read_unlock(&pag->pag_ici_lock);
  352. xfs_put_perag(mp, pag);
  353. /* the returned inode must match the transaction */
  354. if (ip && (ip->i_transp != tp))
  355. return NULL;
  356. return ip;
  357. }
  358. /*
  359. * Decrement reference count of an inode structure and unlock it.
  360. *
  361. * ip -- the inode being released
  362. * lock_flags -- this parameter indicates the inode's locks to be
  363. * to be released. See the comment on xfs_iunlock() for a list
  364. * of valid values.
  365. */
  366. void
  367. xfs_iput(xfs_inode_t *ip,
  368. uint lock_flags)
  369. {
  370. xfs_itrace_entry(ip);
  371. xfs_iunlock(ip, lock_flags);
  372. IRELE(ip);
  373. }
  374. /*
  375. * Special iput for brand-new inodes that are still locked
  376. */
  377. void
  378. xfs_iput_new(
  379. xfs_inode_t *ip,
  380. uint lock_flags)
  381. {
  382. struct inode *inode = VFS_I(ip);
  383. xfs_itrace_entry(ip);
  384. if ((ip->i_d.di_mode == 0)) {
  385. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  386. make_bad_inode(inode);
  387. }
  388. if (inode->i_state & I_NEW)
  389. unlock_new_inode(inode);
  390. if (lock_flags)
  391. xfs_iunlock(ip, lock_flags);
  392. IRELE(ip);
  393. }
  394. /*
  395. * This routine embodies the part of the reclaim code that pulls
  396. * the inode from the inode hash table and the mount structure's
  397. * inode list.
  398. * This should only be called from xfs_reclaim().
  399. */
  400. void
  401. xfs_ireclaim(xfs_inode_t *ip)
  402. {
  403. /*
  404. * Remove from old hash list and mount list.
  405. */
  406. XFS_STATS_INC(xs_ig_reclaims);
  407. xfs_iextract(ip);
  408. /*
  409. * Here we do a spurious inode lock in order to coordinate with
  410. * xfs_sync(). This is because xfs_sync() references the inodes
  411. * in the mount list without taking references on the corresponding
  412. * vnodes. We make that OK here by ensuring that we wait until
  413. * the inode is unlocked in xfs_sync() before we go ahead and
  414. * free it. We get both the regular lock and the io lock because
  415. * the xfs_sync() code may need to drop the regular one but will
  416. * still hold the io lock.
  417. */
  418. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  419. /*
  420. * Release dquots (and their references) if any. An inode may escape
  421. * xfs_inactive and get here via vn_alloc->vn_reclaim path.
  422. */
  423. XFS_QM_DQDETACH(ip->i_mount, ip);
  424. /*
  425. * Pull our behavior descriptor from the vnode chain.
  426. */
  427. if (ip->i_vnode) {
  428. ip->i_vnode->i_private = NULL;
  429. ip->i_vnode = NULL;
  430. }
  431. /*
  432. * Free all memory associated with the inode.
  433. */
  434. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  435. xfs_idestroy(ip);
  436. }
  437. /*
  438. * This routine removes an about-to-be-destroyed inode from
  439. * all of the lists in which it is located with the exception
  440. * of the behavior chain.
  441. */
  442. void
  443. xfs_iextract(
  444. xfs_inode_t *ip)
  445. {
  446. xfs_mount_t *mp = ip->i_mount;
  447. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  448. xfs_inode_t *iq;
  449. write_lock(&pag->pag_ici_lock);
  450. radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
  451. write_unlock(&pag->pag_ici_lock);
  452. xfs_put_perag(mp, pag);
  453. /*
  454. * Remove from mount's inode list.
  455. */
  456. XFS_MOUNT_ILOCK(mp);
  457. ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
  458. iq = ip->i_mnext;
  459. iq->i_mprev = ip->i_mprev;
  460. ip->i_mprev->i_mnext = iq;
  461. /*
  462. * Fix up the head pointer if it points to the inode being deleted.
  463. */
  464. if (mp->m_inodes == ip) {
  465. if (ip == iq) {
  466. mp->m_inodes = NULL;
  467. } else {
  468. mp->m_inodes = iq;
  469. }
  470. }
  471. /* Deal with the deleted inodes list */
  472. list_del_init(&ip->i_reclaim);
  473. mp->m_ireclaims++;
  474. XFS_MOUNT_IUNLOCK(mp);
  475. }
  476. /*
  477. * This is a wrapper routine around the xfs_ilock() routine
  478. * used to centralize some grungy code. It is used in places
  479. * that wish to lock the inode solely for reading the extents.
  480. * The reason these places can't just call xfs_ilock(SHARED)
  481. * is that the inode lock also guards to bringing in of the
  482. * extents from disk for a file in b-tree format. If the inode
  483. * is in b-tree format, then we need to lock the inode exclusively
  484. * until the extents are read in. Locking it exclusively all
  485. * the time would limit our parallelism unnecessarily, though.
  486. * What we do instead is check to see if the extents have been
  487. * read in yet, and only lock the inode exclusively if they
  488. * have not.
  489. *
  490. * The function returns a value which should be given to the
  491. * corresponding xfs_iunlock_map_shared(). This value is
  492. * the mode in which the lock was actually taken.
  493. */
  494. uint
  495. xfs_ilock_map_shared(
  496. xfs_inode_t *ip)
  497. {
  498. uint lock_mode;
  499. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  500. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  501. lock_mode = XFS_ILOCK_EXCL;
  502. } else {
  503. lock_mode = XFS_ILOCK_SHARED;
  504. }
  505. xfs_ilock(ip, lock_mode);
  506. return lock_mode;
  507. }
  508. /*
  509. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  510. * All it does is call xfs_iunlock() with the given lock_mode.
  511. */
  512. void
  513. xfs_iunlock_map_shared(
  514. xfs_inode_t *ip,
  515. unsigned int lock_mode)
  516. {
  517. xfs_iunlock(ip, lock_mode);
  518. }
  519. /*
  520. * The xfs inode contains 2 locks: a multi-reader lock called the
  521. * i_iolock and a multi-reader lock called the i_lock. This routine
  522. * allows either or both of the locks to be obtained.
  523. *
  524. * The 2 locks should always be ordered so that the IO lock is
  525. * obtained first in order to prevent deadlock.
  526. *
  527. * ip -- the inode being locked
  528. * lock_flags -- this parameter indicates the inode's locks
  529. * to be locked. It can be:
  530. * XFS_IOLOCK_SHARED,
  531. * XFS_IOLOCK_EXCL,
  532. * XFS_ILOCK_SHARED,
  533. * XFS_ILOCK_EXCL,
  534. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  535. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  536. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  537. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  538. */
  539. void
  540. xfs_ilock(
  541. xfs_inode_t *ip,
  542. uint lock_flags)
  543. {
  544. /*
  545. * You can't set both SHARED and EXCL for the same lock,
  546. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  547. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  548. */
  549. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  550. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  551. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  552. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  553. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  554. if (lock_flags & XFS_IOLOCK_EXCL)
  555. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  556. else if (lock_flags & XFS_IOLOCK_SHARED)
  557. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  558. if (lock_flags & XFS_ILOCK_EXCL)
  559. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  560. else if (lock_flags & XFS_ILOCK_SHARED)
  561. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  562. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  563. }
  564. /*
  565. * This is just like xfs_ilock(), except that the caller
  566. * is guaranteed not to sleep. It returns 1 if it gets
  567. * the requested locks and 0 otherwise. If the IO lock is
  568. * obtained but the inode lock cannot be, then the IO lock
  569. * is dropped before returning.
  570. *
  571. * ip -- the inode being locked
  572. * lock_flags -- this parameter indicates the inode's locks to be
  573. * to be locked. See the comment for xfs_ilock() for a list
  574. * of valid values.
  575. */
  576. int
  577. xfs_ilock_nowait(
  578. xfs_inode_t *ip,
  579. uint lock_flags)
  580. {
  581. /*
  582. * You can't set both SHARED and EXCL for the same lock,
  583. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  584. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  585. */
  586. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  587. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  588. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  589. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  590. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  591. if (lock_flags & XFS_IOLOCK_EXCL) {
  592. if (!mrtryupdate(&ip->i_iolock))
  593. goto out;
  594. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  595. if (!mrtryaccess(&ip->i_iolock))
  596. goto out;
  597. }
  598. if (lock_flags & XFS_ILOCK_EXCL) {
  599. if (!mrtryupdate(&ip->i_lock))
  600. goto out_undo_iolock;
  601. } else if (lock_flags & XFS_ILOCK_SHARED) {
  602. if (!mrtryaccess(&ip->i_lock))
  603. goto out_undo_iolock;
  604. }
  605. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  606. return 1;
  607. out_undo_iolock:
  608. if (lock_flags & XFS_IOLOCK_EXCL)
  609. mrunlock_excl(&ip->i_iolock);
  610. else if (lock_flags & XFS_IOLOCK_SHARED)
  611. mrunlock_shared(&ip->i_iolock);
  612. out:
  613. return 0;
  614. }
  615. /*
  616. * xfs_iunlock() is used to drop the inode locks acquired with
  617. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  618. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  619. * that we know which locks to drop.
  620. *
  621. * ip -- the inode being unlocked
  622. * lock_flags -- this parameter indicates the inode's locks to be
  623. * to be unlocked. See the comment for xfs_ilock() for a list
  624. * of valid values for this parameter.
  625. *
  626. */
  627. void
  628. xfs_iunlock(
  629. xfs_inode_t *ip,
  630. uint lock_flags)
  631. {
  632. /*
  633. * You can't set both SHARED and EXCL for the same lock,
  634. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  635. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  636. */
  637. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  638. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  639. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  640. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  641. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
  642. XFS_LOCK_DEP_MASK)) == 0);
  643. ASSERT(lock_flags != 0);
  644. if (lock_flags & XFS_IOLOCK_EXCL)
  645. mrunlock_excl(&ip->i_iolock);
  646. else if (lock_flags & XFS_IOLOCK_SHARED)
  647. mrunlock_shared(&ip->i_iolock);
  648. if (lock_flags & XFS_ILOCK_EXCL)
  649. mrunlock_excl(&ip->i_lock);
  650. else if (lock_flags & XFS_ILOCK_SHARED)
  651. mrunlock_shared(&ip->i_lock);
  652. if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
  653. !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
  654. /*
  655. * Let the AIL know that this item has been unlocked in case
  656. * it is in the AIL and anyone is waiting on it. Don't do
  657. * this if the caller has asked us not to.
  658. */
  659. xfs_trans_unlocked_item(ip->i_mount,
  660. (xfs_log_item_t*)(ip->i_itemp));
  661. }
  662. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  663. }
  664. /*
  665. * give up write locks. the i/o lock cannot be held nested
  666. * if it is being demoted.
  667. */
  668. void
  669. xfs_ilock_demote(
  670. xfs_inode_t *ip,
  671. uint lock_flags)
  672. {
  673. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  674. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  675. if (lock_flags & XFS_ILOCK_EXCL)
  676. mrdemote(&ip->i_lock);
  677. if (lock_flags & XFS_IOLOCK_EXCL)
  678. mrdemote(&ip->i_iolock);
  679. }
  680. #ifdef DEBUG
  681. /*
  682. * Debug-only routine, without additional rw_semaphore APIs, we can
  683. * now only answer requests regarding whether we hold the lock for write
  684. * (reader state is outside our visibility, we only track writer state).
  685. *
  686. * Note: this means !xfs_isilocked would give false positives, so don't do that.
  687. */
  688. int
  689. xfs_isilocked(
  690. xfs_inode_t *ip,
  691. uint lock_flags)
  692. {
  693. if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
  694. XFS_ILOCK_EXCL) {
  695. if (!ip->i_lock.mr_writer)
  696. return 0;
  697. }
  698. if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
  699. XFS_IOLOCK_EXCL) {
  700. if (!ip->i_iolock.mr_writer)
  701. return 0;
  702. }
  703. return 1;
  704. }
  705. #endif