xfs_iget.c 21 KB

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