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. bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, ip, 1);
  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. if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
  329. xfs_inode_t *ip;
  330. vp = vn_from_inode(inode);
  331. if (inode->i_state & I_NEW) {
  332. vn_initialize(inode);
  333. error = xfs_iget_core(vp, mp, tp, ino, flags,
  334. lock_flags, ipp, bno);
  335. if (error) {
  336. vn_mark_bad(vp);
  337. if (inode->i_state & I_NEW)
  338. unlock_new_inode(inode);
  339. iput(inode);
  340. }
  341. } else {
  342. /*
  343. * If the inode is not fully constructed due to
  344. * filehandle mismatches wait for the inode to go
  345. * away and try again.
  346. *
  347. * iget_locked will call __wait_on_freeing_inode
  348. * to wait for the inode to go away.
  349. */
  350. if (is_bad_inode(inode) ||
  351. ((ip = xfs_vtoi(vp)) == NULL)) {
  352. iput(inode);
  353. delay(1);
  354. goto retry;
  355. }
  356. if (lock_flags != 0)
  357. xfs_ilock(ip, lock_flags);
  358. XFS_STATS_INC(xs_ig_found);
  359. *ipp = ip;
  360. error = 0;
  361. }
  362. } else
  363. error = ENOMEM; /* If we got no inode we are out of memory */
  364. return error;
  365. }
  366. /*
  367. * Do the setup for the various locks within the incore inode.
  368. */
  369. void
  370. xfs_inode_lock_init(
  371. xfs_inode_t *ip,
  372. bhv_vnode_t *vp)
  373. {
  374. mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
  375. "xfsino", (long)vp->v_number);
  376. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
  377. init_waitqueue_head(&ip->i_ipin_wait);
  378. atomic_set(&ip->i_pincount, 0);
  379. initnsema(&ip->i_flock, 1, "xfsfino");
  380. }
  381. /*
  382. * Look for the inode corresponding to the given ino in the hash table.
  383. * If it is there and its i_transp pointer matches tp, return it.
  384. * Otherwise, return NULL.
  385. */
  386. xfs_inode_t *
  387. xfs_inode_incore(xfs_mount_t *mp,
  388. xfs_ino_t ino,
  389. xfs_trans_t *tp)
  390. {
  391. xfs_inode_t *ip;
  392. xfs_perag_t *pag;
  393. pag = xfs_get_perag(mp, ino);
  394. read_lock(&pag->pag_ici_lock);
  395. ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
  396. read_unlock(&pag->pag_ici_lock);
  397. xfs_put_perag(mp, pag);
  398. /* the returned inode must match the transaction */
  399. if (ip && (ip->i_transp != tp))
  400. return NULL;
  401. return ip;
  402. }
  403. /*
  404. * Decrement reference count of an inode structure and unlock it.
  405. *
  406. * ip -- the inode being released
  407. * lock_flags -- this parameter indicates the inode's locks to be
  408. * to be released. See the comment on xfs_iunlock() for a list
  409. * of valid values.
  410. */
  411. void
  412. xfs_iput(xfs_inode_t *ip,
  413. uint lock_flags)
  414. {
  415. bhv_vnode_t *vp = XFS_ITOV(ip);
  416. vn_trace_entry(ip, "xfs_iput", (inst_t *)__return_address);
  417. xfs_iunlock(ip, lock_flags);
  418. VN_RELE(vp);
  419. }
  420. /*
  421. * Special iput for brand-new inodes that are still locked
  422. */
  423. void
  424. xfs_iput_new(xfs_inode_t *ip,
  425. uint lock_flags)
  426. {
  427. bhv_vnode_t *vp = XFS_ITOV(ip);
  428. struct inode *inode = vn_to_inode(vp);
  429. vn_trace_entry(ip, "xfs_iput_new", (inst_t *)__return_address);
  430. if ((ip->i_d.di_mode == 0)) {
  431. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  432. vn_mark_bad(vp);
  433. }
  434. if (inode->i_state & I_NEW)
  435. unlock_new_inode(inode);
  436. if (lock_flags)
  437. xfs_iunlock(ip, lock_flags);
  438. VN_RELE(vp);
  439. }
  440. /*
  441. * This routine embodies the part of the reclaim code that pulls
  442. * the inode from the inode hash table and the mount structure's
  443. * inode list.
  444. * This should only be called from xfs_reclaim().
  445. */
  446. void
  447. xfs_ireclaim(xfs_inode_t *ip)
  448. {
  449. bhv_vnode_t *vp;
  450. /*
  451. * Remove from old hash list and mount list.
  452. */
  453. XFS_STATS_INC(xs_ig_reclaims);
  454. xfs_iextract(ip);
  455. /*
  456. * Here we do a spurious inode lock in order to coordinate with
  457. * xfs_sync(). This is because xfs_sync() references the inodes
  458. * in the mount list without taking references on the corresponding
  459. * vnodes. We make that OK here by ensuring that we wait until
  460. * the inode is unlocked in xfs_sync() before we go ahead and
  461. * free it. We get both the regular lock and the io lock because
  462. * the xfs_sync() code may need to drop the regular one but will
  463. * still hold the io lock.
  464. */
  465. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  466. /*
  467. * Release dquots (and their references) if any. An inode may escape
  468. * xfs_inactive and get here via vn_alloc->vn_reclaim path.
  469. */
  470. XFS_QM_DQDETACH(ip->i_mount, ip);
  471. /*
  472. * Pull our behavior descriptor from the vnode chain.
  473. */
  474. vp = XFS_ITOV_NULL(ip);
  475. if (vp) {
  476. vn_to_inode(vp)->i_private = NULL;
  477. ip->i_vnode = NULL;
  478. }
  479. /*
  480. * Free all memory associated with the inode.
  481. */
  482. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  483. xfs_idestroy(ip);
  484. }
  485. /*
  486. * This routine removes an about-to-be-destroyed inode from
  487. * all of the lists in which it is located with the exception
  488. * of the behavior chain.
  489. */
  490. void
  491. xfs_iextract(
  492. xfs_inode_t *ip)
  493. {
  494. xfs_mount_t *mp = ip->i_mount;
  495. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  496. xfs_inode_t *iq;
  497. write_lock(&pag->pag_ici_lock);
  498. radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
  499. write_unlock(&pag->pag_ici_lock);
  500. xfs_put_perag(mp, pag);
  501. /*
  502. * Remove from cluster list
  503. */
  504. mp = ip->i_mount;
  505. spin_lock(&ip->i_cluster->icl_lock);
  506. hlist_del(&ip->i_cnode);
  507. spin_unlock(&ip->i_cluster->icl_lock);
  508. /* was last inode in cluster? */
  509. if (hlist_empty(&ip->i_cluster->icl_inodes))
  510. kmem_zone_free(xfs_icluster_zone, ip->i_cluster);
  511. /*
  512. * Remove from mount's inode list.
  513. */
  514. XFS_MOUNT_ILOCK(mp);
  515. ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
  516. iq = ip->i_mnext;
  517. iq->i_mprev = ip->i_mprev;
  518. ip->i_mprev->i_mnext = iq;
  519. /*
  520. * Fix up the head pointer if it points to the inode being deleted.
  521. */
  522. if (mp->m_inodes == ip) {
  523. if (ip == iq) {
  524. mp->m_inodes = NULL;
  525. } else {
  526. mp->m_inodes = iq;
  527. }
  528. }
  529. /* Deal with the deleted inodes list */
  530. list_del_init(&ip->i_reclaim);
  531. mp->m_ireclaims++;
  532. XFS_MOUNT_IUNLOCK(mp);
  533. }
  534. /*
  535. * This is a wrapper routine around the xfs_ilock() routine
  536. * used to centralize some grungy code. It is used in places
  537. * that wish to lock the inode solely for reading the extents.
  538. * The reason these places can't just call xfs_ilock(SHARED)
  539. * is that the inode lock also guards to bringing in of the
  540. * extents from disk for a file in b-tree format. If the inode
  541. * is in b-tree format, then we need to lock the inode exclusively
  542. * until the extents are read in. Locking it exclusively all
  543. * the time would limit our parallelism unnecessarily, though.
  544. * What we do instead is check to see if the extents have been
  545. * read in yet, and only lock the inode exclusively if they
  546. * have not.
  547. *
  548. * The function returns a value which should be given to the
  549. * corresponding xfs_iunlock_map_shared(). This value is
  550. * the mode in which the lock was actually taken.
  551. */
  552. uint
  553. xfs_ilock_map_shared(
  554. xfs_inode_t *ip)
  555. {
  556. uint lock_mode;
  557. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  558. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  559. lock_mode = XFS_ILOCK_EXCL;
  560. } else {
  561. lock_mode = XFS_ILOCK_SHARED;
  562. }
  563. xfs_ilock(ip, lock_mode);
  564. return lock_mode;
  565. }
  566. /*
  567. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  568. * All it does is call xfs_iunlock() with the given lock_mode.
  569. */
  570. void
  571. xfs_iunlock_map_shared(
  572. xfs_inode_t *ip,
  573. unsigned int lock_mode)
  574. {
  575. xfs_iunlock(ip, lock_mode);
  576. }
  577. /*
  578. * The xfs inode contains 2 locks: a multi-reader lock called the
  579. * i_iolock and a multi-reader lock called the i_lock. This routine
  580. * allows either or both of the locks to be obtained.
  581. *
  582. * The 2 locks should always be ordered so that the IO lock is
  583. * obtained first in order to prevent deadlock.
  584. *
  585. * ip -- the inode being locked
  586. * lock_flags -- this parameter indicates the inode's locks
  587. * to be locked. It can be:
  588. * XFS_IOLOCK_SHARED,
  589. * XFS_IOLOCK_EXCL,
  590. * XFS_ILOCK_SHARED,
  591. * XFS_ILOCK_EXCL,
  592. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  593. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  594. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  595. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  596. */
  597. void
  598. xfs_ilock(xfs_inode_t *ip,
  599. uint lock_flags)
  600. {
  601. /*
  602. * You can't set both SHARED and EXCL for the same lock,
  603. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  604. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  605. */
  606. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  607. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  608. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  609. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  610. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  611. if (lock_flags & XFS_IOLOCK_EXCL) {
  612. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  613. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  614. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  615. }
  616. if (lock_flags & XFS_ILOCK_EXCL) {
  617. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  618. } else if (lock_flags & XFS_ILOCK_SHARED) {
  619. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  620. }
  621. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  622. }
  623. /*
  624. * This is just like xfs_ilock(), except that the caller
  625. * is guaranteed not to sleep. It returns 1 if it gets
  626. * the requested locks and 0 otherwise. If the IO lock is
  627. * obtained but the inode lock cannot be, then the IO lock
  628. * is dropped before returning.
  629. *
  630. * ip -- the inode being locked
  631. * lock_flags -- this parameter indicates the inode's locks to be
  632. * to be locked. See the comment for xfs_ilock() for a list
  633. * of valid values.
  634. *
  635. */
  636. int
  637. xfs_ilock_nowait(xfs_inode_t *ip,
  638. uint lock_flags)
  639. {
  640. int iolocked;
  641. int ilocked;
  642. /*
  643. * You can't set both SHARED and EXCL for the same lock,
  644. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  645. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  646. */
  647. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  648. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  649. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  650. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  651. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  652. iolocked = 0;
  653. if (lock_flags & XFS_IOLOCK_EXCL) {
  654. iolocked = mrtryupdate(&ip->i_iolock);
  655. if (!iolocked) {
  656. return 0;
  657. }
  658. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  659. iolocked = mrtryaccess(&ip->i_iolock);
  660. if (!iolocked) {
  661. return 0;
  662. }
  663. }
  664. if (lock_flags & XFS_ILOCK_EXCL) {
  665. ilocked = mrtryupdate(&ip->i_lock);
  666. if (!ilocked) {
  667. if (iolocked) {
  668. mrunlock(&ip->i_iolock);
  669. }
  670. return 0;
  671. }
  672. } else if (lock_flags & XFS_ILOCK_SHARED) {
  673. ilocked = mrtryaccess(&ip->i_lock);
  674. if (!ilocked) {
  675. if (iolocked) {
  676. mrunlock(&ip->i_iolock);
  677. }
  678. return 0;
  679. }
  680. }
  681. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  682. return 1;
  683. }
  684. /*
  685. * xfs_iunlock() is used to drop the inode locks acquired with
  686. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  687. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  688. * that we know which locks to drop.
  689. *
  690. * ip -- the inode being unlocked
  691. * lock_flags -- this parameter indicates the inode's locks to be
  692. * to be unlocked. See the comment for xfs_ilock() for a list
  693. * of valid values for this parameter.
  694. *
  695. */
  696. void
  697. xfs_iunlock(xfs_inode_t *ip,
  698. uint lock_flags)
  699. {
  700. /*
  701. * You can't set both SHARED and EXCL for the same lock,
  702. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  703. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  704. */
  705. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  706. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  707. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  708. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  709. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
  710. XFS_LOCK_DEP_MASK)) == 0);
  711. ASSERT(lock_flags != 0);
  712. if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
  713. ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
  714. (ismrlocked(&ip->i_iolock, MR_ACCESS)));
  715. ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
  716. (ismrlocked(&ip->i_iolock, MR_UPDATE)));
  717. mrunlock(&ip->i_iolock);
  718. }
  719. if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
  720. ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
  721. (ismrlocked(&ip->i_lock, MR_ACCESS)));
  722. ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
  723. (ismrlocked(&ip->i_lock, MR_UPDATE)));
  724. mrunlock(&ip->i_lock);
  725. /*
  726. * Let the AIL know that this item has been unlocked in case
  727. * it is in the AIL and anyone is waiting on it. Don't do
  728. * this if the caller has asked us not to.
  729. */
  730. if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
  731. ip->i_itemp != NULL) {
  732. xfs_trans_unlocked_item(ip->i_mount,
  733. (xfs_log_item_t*)(ip->i_itemp));
  734. }
  735. }
  736. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  737. }
  738. /*
  739. * give up write locks. the i/o lock cannot be held nested
  740. * if it is being demoted.
  741. */
  742. void
  743. xfs_ilock_demote(xfs_inode_t *ip,
  744. uint lock_flags)
  745. {
  746. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  747. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  748. if (lock_flags & XFS_ILOCK_EXCL) {
  749. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  750. mrdemote(&ip->i_lock);
  751. }
  752. if (lock_flags & XFS_IOLOCK_EXCL) {
  753. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  754. mrdemote(&ip->i_iolock);
  755. }
  756. }
  757. /*
  758. * The following three routines simply manage the i_flock
  759. * semaphore embedded in the inode. This semaphore synchronizes
  760. * processes attempting to flush the in-core inode back to disk.
  761. */
  762. void
  763. xfs_iflock(xfs_inode_t *ip)
  764. {
  765. psema(&(ip->i_flock), PINOD|PLTWAIT);
  766. }
  767. int
  768. xfs_iflock_nowait(xfs_inode_t *ip)
  769. {
  770. return (cpsema(&(ip->i_flock)));
  771. }
  772. void
  773. xfs_ifunlock(xfs_inode_t *ip)
  774. {
  775. ASSERT(issemalocked(&(ip->i_flock)));
  776. vsema(&(ip->i_flock));
  777. }