xfs_iget.c 22 KB

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