xfs_iget.c 22 KB

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