xfs_iget.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  191. xfs_idestroy(ip);
  192. xfs_put_perag(mp, pag);
  193. return ENOENT;
  194. }
  195. /*
  196. * Preload the radix tree so we can insert safely under the
  197. * write spinlock.
  198. */
  199. if (radix_tree_preload(GFP_KERNEL)) {
  200. xfs_idestroy(ip);
  201. delay(1);
  202. goto again;
  203. }
  204. if (lock_flags)
  205. xfs_ilock(ip, lock_flags);
  206. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  207. first_index = agino & mask;
  208. write_lock(&pag->pag_ici_lock);
  209. /*
  210. * insert the new inode
  211. */
  212. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  213. if (unlikely(error)) {
  214. BUG_ON(error != -EEXIST);
  215. write_unlock(&pag->pag_ici_lock);
  216. radix_tree_preload_end();
  217. if (lock_flags)
  218. xfs_iunlock(ip, lock_flags);
  219. xfs_idestroy(ip);
  220. XFS_STATS_INC(xs_ig_dup);
  221. goto again;
  222. }
  223. /*
  224. * These values _must_ be set before releasing the radix tree lock!
  225. */
  226. ip->i_udquot = ip->i_gdquot = NULL;
  227. xfs_iflags_set(ip, XFS_INEW);
  228. write_unlock(&pag->pag_ici_lock);
  229. radix_tree_preload_end();
  230. /*
  231. * Link ip to its mount and thread it on the mount's inode list.
  232. */
  233. XFS_MOUNT_ILOCK(mp);
  234. if ((iq = mp->m_inodes)) {
  235. ASSERT(iq->i_mprev->i_mnext == iq);
  236. ip->i_mprev = iq->i_mprev;
  237. iq->i_mprev->i_mnext = ip;
  238. iq->i_mprev = ip;
  239. ip->i_mnext = iq;
  240. } else {
  241. ip->i_mnext = ip;
  242. ip->i_mprev = ip;
  243. }
  244. mp->m_inodes = ip;
  245. XFS_MOUNT_IUNLOCK(mp);
  246. xfs_put_perag(mp, pag);
  247. return_ip:
  248. ASSERT(ip->i_df.if_ext_max ==
  249. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  250. xfs_iflags_set(ip, XFS_IMODIFIED);
  251. *ipp = ip;
  252. /*
  253. * Set up the Linux with the Linux inode.
  254. */
  255. ip->i_vnode = inode;
  256. inode->i_private = ip;
  257. /*
  258. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  259. * now. If it's a new inode being created, xfs_ialloc will handle it.
  260. */
  261. if (ip->i_d.di_mode != 0)
  262. xfs_setup_inode(ip);
  263. return 0;
  264. }
  265. /*
  266. * The 'normal' internal xfs_iget, if needed it will
  267. * 'allocate', or 'get', the vnode.
  268. */
  269. int
  270. xfs_iget(
  271. xfs_mount_t *mp,
  272. xfs_trans_t *tp,
  273. xfs_ino_t ino,
  274. uint flags,
  275. uint lock_flags,
  276. xfs_inode_t **ipp,
  277. xfs_daddr_t bno)
  278. {
  279. struct inode *inode;
  280. xfs_inode_t *ip;
  281. int error;
  282. XFS_STATS_INC(xs_ig_attempts);
  283. retry:
  284. inode = iget_locked(mp->m_super, ino);
  285. if (!inode)
  286. /* If we got no inode we are out of memory */
  287. return ENOMEM;
  288. if (inode->i_state & I_NEW) {
  289. XFS_STATS_INC(vn_active);
  290. XFS_STATS_INC(vn_alloc);
  291. error = xfs_iget_core(inode, mp, tp, ino, flags,
  292. lock_flags, ipp, bno);
  293. if (error) {
  294. make_bad_inode(inode);
  295. if (inode->i_state & I_NEW)
  296. unlock_new_inode(inode);
  297. iput(inode);
  298. }
  299. return error;
  300. }
  301. /*
  302. * If the inode is not fully constructed due to
  303. * filehandle mismatches wait for the inode to go
  304. * away and try again.
  305. *
  306. * iget_locked will call __wait_on_freeing_inode
  307. * to wait for the inode to go away.
  308. */
  309. if (is_bad_inode(inode)) {
  310. iput(inode);
  311. delay(1);
  312. goto retry;
  313. }
  314. ip = XFS_I(inode);
  315. if (!ip) {
  316. iput(inode);
  317. delay(1);
  318. goto retry;
  319. }
  320. if (lock_flags != 0)
  321. xfs_ilock(ip, lock_flags);
  322. XFS_STATS_INC(xs_ig_found);
  323. *ipp = ip;
  324. return 0;
  325. }
  326. /*
  327. * Look for the inode corresponding to the given ino in the hash table.
  328. * If it is there and its i_transp pointer matches tp, return it.
  329. * Otherwise, return NULL.
  330. */
  331. xfs_inode_t *
  332. xfs_inode_incore(xfs_mount_t *mp,
  333. xfs_ino_t ino,
  334. xfs_trans_t *tp)
  335. {
  336. xfs_inode_t *ip;
  337. xfs_perag_t *pag;
  338. pag = xfs_get_perag(mp, ino);
  339. read_lock(&pag->pag_ici_lock);
  340. ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
  341. read_unlock(&pag->pag_ici_lock);
  342. xfs_put_perag(mp, pag);
  343. /* the returned inode must match the transaction */
  344. if (ip && (ip->i_transp != tp))
  345. return NULL;
  346. return ip;
  347. }
  348. /*
  349. * Decrement reference count of an inode structure and unlock it.
  350. *
  351. * ip -- the inode being released
  352. * lock_flags -- this parameter indicates the inode's locks to be
  353. * to be released. See the comment on xfs_iunlock() for a list
  354. * of valid values.
  355. */
  356. void
  357. xfs_iput(xfs_inode_t *ip,
  358. uint lock_flags)
  359. {
  360. xfs_itrace_entry(ip);
  361. xfs_iunlock(ip, lock_flags);
  362. IRELE(ip);
  363. }
  364. /*
  365. * Special iput for brand-new inodes that are still locked
  366. */
  367. void
  368. xfs_iput_new(
  369. xfs_inode_t *ip,
  370. uint lock_flags)
  371. {
  372. struct inode *inode = VFS_I(ip);
  373. xfs_itrace_entry(ip);
  374. if ((ip->i_d.di_mode == 0)) {
  375. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  376. make_bad_inode(inode);
  377. }
  378. if (inode->i_state & I_NEW)
  379. unlock_new_inode(inode);
  380. if (lock_flags)
  381. xfs_iunlock(ip, lock_flags);
  382. IRELE(ip);
  383. }
  384. /*
  385. * This routine embodies the part of the reclaim code that pulls
  386. * the inode from the inode hash table and the mount structure's
  387. * inode list.
  388. * This should only be called from xfs_reclaim().
  389. */
  390. void
  391. xfs_ireclaim(xfs_inode_t *ip)
  392. {
  393. /*
  394. * Remove from old hash list and mount list.
  395. */
  396. XFS_STATS_INC(xs_ig_reclaims);
  397. xfs_iextract(ip);
  398. /*
  399. * Here we do a spurious inode lock in order to coordinate with
  400. * xfs_sync(). This is because xfs_sync() references the inodes
  401. * in the mount list without taking references on the corresponding
  402. * vnodes. We make that OK here by ensuring that we wait until
  403. * the inode is unlocked in xfs_sync() before we go ahead and
  404. * free it. We get both the regular lock and the io lock because
  405. * the xfs_sync() code may need to drop the regular one but will
  406. * still hold the io lock.
  407. */
  408. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  409. /*
  410. * Release dquots (and their references) if any. An inode may escape
  411. * xfs_inactive and get here via vn_alloc->vn_reclaim path.
  412. */
  413. XFS_QM_DQDETACH(ip->i_mount, ip);
  414. /*
  415. * Pull our behavior descriptor from the vnode chain.
  416. */
  417. if (ip->i_vnode) {
  418. ip->i_vnode->i_private = NULL;
  419. ip->i_vnode = NULL;
  420. }
  421. /*
  422. * Free all memory associated with the inode.
  423. */
  424. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  425. xfs_idestroy(ip);
  426. }
  427. /*
  428. * This routine removes an about-to-be-destroyed inode from
  429. * all of the lists in which it is located with the exception
  430. * of the behavior chain.
  431. */
  432. void
  433. xfs_iextract(
  434. xfs_inode_t *ip)
  435. {
  436. xfs_mount_t *mp = ip->i_mount;
  437. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  438. xfs_inode_t *iq;
  439. write_lock(&pag->pag_ici_lock);
  440. radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
  441. write_unlock(&pag->pag_ici_lock);
  442. xfs_put_perag(mp, pag);
  443. /*
  444. * Remove from mount's inode list.
  445. */
  446. XFS_MOUNT_ILOCK(mp);
  447. ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
  448. iq = ip->i_mnext;
  449. iq->i_mprev = ip->i_mprev;
  450. ip->i_mprev->i_mnext = iq;
  451. /*
  452. * Fix up the head pointer if it points to the inode being deleted.
  453. */
  454. if (mp->m_inodes == ip) {
  455. if (ip == iq) {
  456. mp->m_inodes = NULL;
  457. } else {
  458. mp->m_inodes = iq;
  459. }
  460. }
  461. /* Deal with the deleted inodes list */
  462. list_del_init(&ip->i_reclaim);
  463. mp->m_ireclaims++;
  464. XFS_MOUNT_IUNLOCK(mp);
  465. }
  466. /*
  467. * This is a wrapper routine around the xfs_ilock() routine
  468. * used to centralize some grungy code. It is used in places
  469. * that wish to lock the inode solely for reading the extents.
  470. * The reason these places can't just call xfs_ilock(SHARED)
  471. * is that the inode lock also guards to bringing in of the
  472. * extents from disk for a file in b-tree format. If the inode
  473. * is in b-tree format, then we need to lock the inode exclusively
  474. * until the extents are read in. Locking it exclusively all
  475. * the time would limit our parallelism unnecessarily, though.
  476. * What we do instead is check to see if the extents have been
  477. * read in yet, and only lock the inode exclusively if they
  478. * have not.
  479. *
  480. * The function returns a value which should be given to the
  481. * corresponding xfs_iunlock_map_shared(). This value is
  482. * the mode in which the lock was actually taken.
  483. */
  484. uint
  485. xfs_ilock_map_shared(
  486. xfs_inode_t *ip)
  487. {
  488. uint lock_mode;
  489. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  490. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  491. lock_mode = XFS_ILOCK_EXCL;
  492. } else {
  493. lock_mode = XFS_ILOCK_SHARED;
  494. }
  495. xfs_ilock(ip, lock_mode);
  496. return lock_mode;
  497. }
  498. /*
  499. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  500. * All it does is call xfs_iunlock() with the given lock_mode.
  501. */
  502. void
  503. xfs_iunlock_map_shared(
  504. xfs_inode_t *ip,
  505. unsigned int lock_mode)
  506. {
  507. xfs_iunlock(ip, lock_mode);
  508. }
  509. /*
  510. * The xfs inode contains 2 locks: a multi-reader lock called the
  511. * i_iolock and a multi-reader lock called the i_lock. This routine
  512. * allows either or both of the locks to be obtained.
  513. *
  514. * The 2 locks should always be ordered so that the IO lock is
  515. * obtained first in order to prevent deadlock.
  516. *
  517. * ip -- the inode being locked
  518. * lock_flags -- this parameter indicates the inode's locks
  519. * to be locked. It can be:
  520. * XFS_IOLOCK_SHARED,
  521. * XFS_IOLOCK_EXCL,
  522. * XFS_ILOCK_SHARED,
  523. * XFS_ILOCK_EXCL,
  524. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  525. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  526. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  527. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  528. */
  529. void
  530. xfs_ilock(
  531. xfs_inode_t *ip,
  532. uint lock_flags)
  533. {
  534. /*
  535. * You can't set both SHARED and EXCL for the same lock,
  536. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  537. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  538. */
  539. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  540. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  541. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  542. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  543. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  544. if (lock_flags & XFS_IOLOCK_EXCL)
  545. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  546. else if (lock_flags & XFS_IOLOCK_SHARED)
  547. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  548. if (lock_flags & XFS_ILOCK_EXCL)
  549. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  550. else if (lock_flags & XFS_ILOCK_SHARED)
  551. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  552. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  553. }
  554. /*
  555. * This is just like xfs_ilock(), except that the caller
  556. * is guaranteed not to sleep. It returns 1 if it gets
  557. * the requested locks and 0 otherwise. If the IO lock is
  558. * obtained but the inode lock cannot be, then the IO lock
  559. * is dropped before returning.
  560. *
  561. * ip -- the inode being locked
  562. * lock_flags -- this parameter indicates the inode's locks to be
  563. * to be locked. See the comment for xfs_ilock() for a list
  564. * of valid values.
  565. */
  566. int
  567. xfs_ilock_nowait(
  568. xfs_inode_t *ip,
  569. uint lock_flags)
  570. {
  571. /*
  572. * You can't set both SHARED and EXCL for the same lock,
  573. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  574. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  575. */
  576. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  577. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  578. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  579. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  580. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  581. if (lock_flags & XFS_IOLOCK_EXCL) {
  582. if (!mrtryupdate(&ip->i_iolock))
  583. goto out;
  584. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  585. if (!mrtryaccess(&ip->i_iolock))
  586. goto out;
  587. }
  588. if (lock_flags & XFS_ILOCK_EXCL) {
  589. if (!mrtryupdate(&ip->i_lock))
  590. goto out_undo_iolock;
  591. } else if (lock_flags & XFS_ILOCK_SHARED) {
  592. if (!mrtryaccess(&ip->i_lock))
  593. goto out_undo_iolock;
  594. }
  595. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  596. return 1;
  597. out_undo_iolock:
  598. if (lock_flags & XFS_IOLOCK_EXCL)
  599. mrunlock_excl(&ip->i_iolock);
  600. else if (lock_flags & XFS_IOLOCK_SHARED)
  601. mrunlock_shared(&ip->i_iolock);
  602. out:
  603. return 0;
  604. }
  605. /*
  606. * xfs_iunlock() is used to drop the inode locks acquired with
  607. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  608. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  609. * that we know which locks to drop.
  610. *
  611. * ip -- the inode being unlocked
  612. * lock_flags -- this parameter indicates the inode's locks to be
  613. * to be unlocked. See the comment for xfs_ilock() for a list
  614. * of valid values for this parameter.
  615. *
  616. */
  617. void
  618. xfs_iunlock(
  619. xfs_inode_t *ip,
  620. uint lock_flags)
  621. {
  622. /*
  623. * You can't set both SHARED and EXCL for the same lock,
  624. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  625. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  626. */
  627. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  628. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  629. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  630. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  631. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
  632. XFS_LOCK_DEP_MASK)) == 0);
  633. ASSERT(lock_flags != 0);
  634. if (lock_flags & XFS_IOLOCK_EXCL)
  635. mrunlock_excl(&ip->i_iolock);
  636. else if (lock_flags & XFS_IOLOCK_SHARED)
  637. mrunlock_shared(&ip->i_iolock);
  638. if (lock_flags & XFS_ILOCK_EXCL)
  639. mrunlock_excl(&ip->i_lock);
  640. else if (lock_flags & XFS_ILOCK_SHARED)
  641. mrunlock_shared(&ip->i_lock);
  642. if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
  643. !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
  644. /*
  645. * Let the AIL know that this item has been unlocked in case
  646. * it is in the AIL and anyone is waiting on it. Don't do
  647. * this if the caller has asked us not to.
  648. */
  649. xfs_trans_unlocked_item(ip->i_mount,
  650. (xfs_log_item_t*)(ip->i_itemp));
  651. }
  652. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  653. }
  654. /*
  655. * give up write locks. the i/o lock cannot be held nested
  656. * if it is being demoted.
  657. */
  658. void
  659. xfs_ilock_demote(
  660. xfs_inode_t *ip,
  661. uint lock_flags)
  662. {
  663. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  664. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  665. if (lock_flags & XFS_ILOCK_EXCL)
  666. mrdemote(&ip->i_lock);
  667. if (lock_flags & XFS_IOLOCK_EXCL)
  668. mrdemote(&ip->i_iolock);
  669. }
  670. #ifdef DEBUG
  671. /*
  672. * Debug-only routine, without additional rw_semaphore APIs, we can
  673. * now only answer requests regarding whether we hold the lock for write
  674. * (reader state is outside our visibility, we only track writer state).
  675. *
  676. * Note: this means !xfs_isilocked would give false positives, so don't do that.
  677. */
  678. int
  679. xfs_isilocked(
  680. xfs_inode_t *ip,
  681. uint lock_flags)
  682. {
  683. if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
  684. XFS_ILOCK_EXCL) {
  685. if (!ip->i_lock.mr_writer)
  686. return 0;
  687. }
  688. if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
  689. XFS_IOLOCK_EXCL) {
  690. if (!ip->i_iolock.mr_writer)
  691. return 0;
  692. }
  693. return 1;
  694. }
  695. #endif