xfs_iget.c 21 KB

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