xfs_iget.c 20 KB

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