xfs_iget.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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_acl.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_inum.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_dir2.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_quota.h"
  41. #include "xfs_utils.h"
  42. #include "xfs_trans_priv.h"
  43. #include "xfs_inode_item.h"
  44. #include "xfs_bmap.h"
  45. #include "xfs_btree_trace.h"
  46. #include "xfs_dir2_trace.h"
  47. /*
  48. * Allocate and initialise an xfs_inode.
  49. */
  50. STATIC struct xfs_inode *
  51. xfs_inode_alloc(
  52. struct xfs_mount *mp,
  53. xfs_ino_t ino)
  54. {
  55. struct xfs_inode *ip;
  56. /*
  57. * if this didn't occur in transactions, we could use
  58. * KM_MAYFAIL and return NULL here on ENOMEM. Set the
  59. * code up to do this anyway.
  60. */
  61. ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
  62. if (!ip)
  63. return NULL;
  64. if (inode_init_always(mp->m_super, VFS_I(ip))) {
  65. kmem_zone_free(xfs_inode_zone, ip);
  66. return NULL;
  67. }
  68. ASSERT(atomic_read(&ip->i_iocount) == 0);
  69. ASSERT(atomic_read(&ip->i_pincount) == 0);
  70. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  71. ASSERT(completion_done(&ip->i_flush));
  72. ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
  73. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
  74. /* initialise the xfs inode */
  75. ip->i_ino = ino;
  76. ip->i_mount = mp;
  77. memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
  78. ip->i_afp = NULL;
  79. memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
  80. ip->i_flags = 0;
  81. ip->i_update_core = 0;
  82. ip->i_delayed_blks = 0;
  83. memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
  84. ip->i_size = 0;
  85. ip->i_new_size = 0;
  86. /*
  87. * Initialize inode's trace buffers.
  88. */
  89. #ifdef XFS_INODE_TRACE
  90. ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS);
  91. #endif
  92. #ifdef XFS_BMAP_TRACE
  93. ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS);
  94. #endif
  95. #ifdef XFS_BTREE_TRACE
  96. ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS);
  97. #endif
  98. #ifdef XFS_RW_TRACE
  99. ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS);
  100. #endif
  101. #ifdef XFS_ILOCK_TRACE
  102. ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS);
  103. #endif
  104. #ifdef XFS_DIR2_TRACE
  105. ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS);
  106. #endif
  107. /* prevent anyone from using this yet */
  108. VFS_I(ip)->i_state = I_NEW|I_LOCK;
  109. return ip;
  110. }
  111. STATIC void
  112. xfs_inode_free(
  113. struct xfs_inode *ip)
  114. {
  115. switch (ip->i_d.di_mode & S_IFMT) {
  116. case S_IFREG:
  117. case S_IFDIR:
  118. case S_IFLNK:
  119. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  120. break;
  121. }
  122. if (ip->i_afp)
  123. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  124. #ifdef XFS_INODE_TRACE
  125. ktrace_free(ip->i_trace);
  126. #endif
  127. #ifdef XFS_BMAP_TRACE
  128. ktrace_free(ip->i_xtrace);
  129. #endif
  130. #ifdef XFS_BTREE_TRACE
  131. ktrace_free(ip->i_btrace);
  132. #endif
  133. #ifdef XFS_RW_TRACE
  134. ktrace_free(ip->i_rwtrace);
  135. #endif
  136. #ifdef XFS_ILOCK_TRACE
  137. ktrace_free(ip->i_lock_trace);
  138. #endif
  139. #ifdef XFS_DIR2_TRACE
  140. ktrace_free(ip->i_dir_trace);
  141. #endif
  142. if (ip->i_itemp) {
  143. /*
  144. * Only if we are shutting down the fs will we see an
  145. * inode still in the AIL. If it is there, we should remove
  146. * it to prevent a use-after-free from occurring.
  147. */
  148. xfs_log_item_t *lip = &ip->i_itemp->ili_item;
  149. struct xfs_ail *ailp = lip->li_ailp;
  150. ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
  151. XFS_FORCED_SHUTDOWN(ip->i_mount));
  152. if (lip->li_flags & XFS_LI_IN_AIL) {
  153. spin_lock(&ailp->xa_lock);
  154. if (lip->li_flags & XFS_LI_IN_AIL)
  155. xfs_trans_ail_delete(ailp, lip);
  156. else
  157. spin_unlock(&ailp->xa_lock);
  158. }
  159. xfs_inode_item_destroy(ip);
  160. ip->i_itemp = NULL;
  161. }
  162. /* asserts to verify all state is correct here */
  163. ASSERT(atomic_read(&ip->i_iocount) == 0);
  164. ASSERT(atomic_read(&ip->i_pincount) == 0);
  165. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  166. ASSERT(completion_done(&ip->i_flush));
  167. kmem_zone_free(xfs_inode_zone, ip);
  168. }
  169. /*
  170. * Check the validity of the inode we just found it the cache
  171. */
  172. static int
  173. xfs_iget_cache_hit(
  174. struct xfs_perag *pag,
  175. struct xfs_inode *ip,
  176. int flags,
  177. int lock_flags) __releases(pag->pag_ici_lock)
  178. {
  179. struct inode *inode = VFS_I(ip);
  180. struct xfs_mount *mp = ip->i_mount;
  181. int error;
  182. spin_lock(&ip->i_flags_lock);
  183. /*
  184. * If we are racing with another cache hit that is currently
  185. * instantiating this inode or currently recycling it out of
  186. * reclaimabe state, wait for the initialisation to complete
  187. * before continuing.
  188. *
  189. * XXX(hch): eventually we should do something equivalent to
  190. * wait_on_inode to wait for these flags to be cleared
  191. * instead of polling for it.
  192. */
  193. if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
  194. XFS_STATS_INC(xs_ig_frecycle);
  195. error = EAGAIN;
  196. goto out_error;
  197. }
  198. /*
  199. * If lookup is racing with unlink return an error immediately.
  200. */
  201. if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
  202. error = ENOENT;
  203. goto out_error;
  204. }
  205. /*
  206. * If IRECLAIMABLE is set, we've torn down the VFS inode already.
  207. * Need to carefully get it back into useable state.
  208. */
  209. if (ip->i_flags & XFS_IRECLAIMABLE) {
  210. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  211. /*
  212. * We need to set XFS_INEW atomically with clearing the
  213. * reclaimable tag so that we do have an indicator of the
  214. * inode still being initialized.
  215. */
  216. ip->i_flags |= XFS_INEW;
  217. ip->i_flags &= ~XFS_IRECLAIMABLE;
  218. __xfs_inode_clear_reclaim_tag(mp, pag, ip);
  219. spin_unlock(&ip->i_flags_lock);
  220. read_unlock(&pag->pag_ici_lock);
  221. error = -inode_init_always(mp->m_super, inode);
  222. if (error) {
  223. /*
  224. * Re-initializing the inode failed, and we are in deep
  225. * trouble. Try to re-add it to the reclaim list.
  226. */
  227. read_lock(&pag->pag_ici_lock);
  228. spin_lock(&ip->i_flags_lock);
  229. ip->i_flags &= ~XFS_INEW;
  230. ip->i_flags |= XFS_IRECLAIMABLE;
  231. __xfs_inode_set_reclaim_tag(pag, ip);
  232. goto out_error;
  233. }
  234. inode->i_state = I_LOCK|I_NEW;
  235. } else {
  236. /* If the VFS inode is being torn down, pause and try again. */
  237. if (!igrab(inode)) {
  238. error = EAGAIN;
  239. goto out_error;
  240. }
  241. /* We've got a live one. */
  242. spin_unlock(&ip->i_flags_lock);
  243. read_unlock(&pag->pag_ici_lock);
  244. }
  245. if (lock_flags != 0)
  246. xfs_ilock(ip, lock_flags);
  247. xfs_iflags_clear(ip, XFS_ISTALE);
  248. xfs_itrace_exit_tag(ip, "xfs_iget.found");
  249. XFS_STATS_INC(xs_ig_found);
  250. return 0;
  251. out_error:
  252. spin_unlock(&ip->i_flags_lock);
  253. read_unlock(&pag->pag_ici_lock);
  254. return error;
  255. }
  256. static int
  257. xfs_iget_cache_miss(
  258. struct xfs_mount *mp,
  259. struct xfs_perag *pag,
  260. xfs_trans_t *tp,
  261. xfs_ino_t ino,
  262. struct xfs_inode **ipp,
  263. xfs_daddr_t bno,
  264. int flags,
  265. int lock_flags)
  266. {
  267. struct xfs_inode *ip;
  268. int error;
  269. unsigned long first_index, mask;
  270. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
  271. ip = xfs_inode_alloc(mp, ino);
  272. if (!ip)
  273. return ENOMEM;
  274. error = xfs_iread(mp, tp, ip, bno, flags);
  275. if (error)
  276. goto out_destroy;
  277. xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
  278. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  279. error = ENOENT;
  280. goto out_destroy;
  281. }
  282. /*
  283. * Preload the radix tree so we can insert safely under the
  284. * write spinlock. Note that we cannot sleep inside the preload
  285. * region.
  286. */
  287. if (radix_tree_preload(GFP_KERNEL)) {
  288. error = EAGAIN;
  289. goto out_destroy;
  290. }
  291. /*
  292. * Because the inode hasn't been added to the radix-tree yet it can't
  293. * be found by another thread, so we can do the non-sleeping lock here.
  294. */
  295. if (lock_flags) {
  296. if (!xfs_ilock_nowait(ip, lock_flags))
  297. BUG();
  298. }
  299. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  300. first_index = agino & mask;
  301. write_lock(&pag->pag_ici_lock);
  302. /* insert the new inode */
  303. error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
  304. if (unlikely(error)) {
  305. WARN_ON(error != -EEXIST);
  306. XFS_STATS_INC(xs_ig_dup);
  307. error = EAGAIN;
  308. goto out_preload_end;
  309. }
  310. /* These values _must_ be set before releasing the radix tree lock! */
  311. ip->i_udquot = ip->i_gdquot = NULL;
  312. xfs_iflags_set(ip, XFS_INEW);
  313. write_unlock(&pag->pag_ici_lock);
  314. radix_tree_preload_end();
  315. *ipp = ip;
  316. return 0;
  317. out_preload_end:
  318. write_unlock(&pag->pag_ici_lock);
  319. radix_tree_preload_end();
  320. if (lock_flags)
  321. xfs_iunlock(ip, lock_flags);
  322. out_destroy:
  323. __destroy_inode(VFS_I(ip));
  324. xfs_inode_free(ip);
  325. return error;
  326. }
  327. /*
  328. * Look up an inode by number in the given file system.
  329. * The inode is looked up in the cache held in each AG.
  330. * If the inode is found in the cache, initialise the vfs inode
  331. * if necessary.
  332. *
  333. * If it is not in core, read it in from the file system's device,
  334. * add it to the cache and initialise the vfs inode.
  335. *
  336. * The inode is locked according to the value of the lock_flags parameter.
  337. * This flag parameter indicates how and if the inode's IO lock and inode lock
  338. * should be taken.
  339. *
  340. * mp -- the mount point structure for the current file system. It points
  341. * to the inode hash table.
  342. * tp -- a pointer to the current transaction if there is one. This is
  343. * simply passed through to the xfs_iread() call.
  344. * ino -- the number of the inode desired. This is the unique identifier
  345. * within the file system for the inode being requested.
  346. * lock_flags -- flags indicating how to lock the inode. See the comment
  347. * for xfs_ilock() for a list of valid values.
  348. * bno -- the block number starting the buffer containing the inode,
  349. * if known (as by bulkstat), else 0.
  350. */
  351. int
  352. xfs_iget(
  353. xfs_mount_t *mp,
  354. xfs_trans_t *tp,
  355. xfs_ino_t ino,
  356. uint flags,
  357. uint lock_flags,
  358. xfs_inode_t **ipp,
  359. xfs_daddr_t bno)
  360. {
  361. xfs_inode_t *ip;
  362. int error;
  363. xfs_perag_t *pag;
  364. xfs_agino_t agino;
  365. /* the radix tree exists only in inode capable AGs */
  366. if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
  367. return EINVAL;
  368. /* get the perag structure and ensure that it's inode capable */
  369. pag = xfs_get_perag(mp, ino);
  370. if (!pag->pagi_inodeok)
  371. return EINVAL;
  372. ASSERT(pag->pag_ici_init);
  373. agino = XFS_INO_TO_AGINO(mp, ino);
  374. again:
  375. error = 0;
  376. read_lock(&pag->pag_ici_lock);
  377. ip = radix_tree_lookup(&pag->pag_ici_root, agino);
  378. if (ip) {
  379. error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
  380. if (error)
  381. goto out_error_or_again;
  382. } else {
  383. read_unlock(&pag->pag_ici_lock);
  384. XFS_STATS_INC(xs_ig_missed);
  385. error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
  386. flags, lock_flags);
  387. if (error)
  388. goto out_error_or_again;
  389. }
  390. xfs_put_perag(mp, pag);
  391. *ipp = ip;
  392. ASSERT(ip->i_df.if_ext_max ==
  393. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  394. /*
  395. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  396. * now. If it's a new inode being created, xfs_ialloc will handle it.
  397. */
  398. if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
  399. xfs_setup_inode(ip);
  400. return 0;
  401. out_error_or_again:
  402. if (error == EAGAIN) {
  403. delay(1);
  404. goto again;
  405. }
  406. xfs_put_perag(mp, pag);
  407. return error;
  408. }
  409. /*
  410. * Decrement reference count of an inode structure and unlock it.
  411. *
  412. * ip -- the inode being released
  413. * lock_flags -- this parameter indicates the inode's locks to be
  414. * to be released. See the comment on xfs_iunlock() for a list
  415. * of valid values.
  416. */
  417. void
  418. xfs_iput(xfs_inode_t *ip,
  419. uint lock_flags)
  420. {
  421. xfs_itrace_entry(ip);
  422. xfs_iunlock(ip, lock_flags);
  423. IRELE(ip);
  424. }
  425. /*
  426. * Special iput for brand-new inodes that are still locked
  427. */
  428. void
  429. xfs_iput_new(
  430. xfs_inode_t *ip,
  431. uint lock_flags)
  432. {
  433. struct inode *inode = VFS_I(ip);
  434. xfs_itrace_entry(ip);
  435. if ((ip->i_d.di_mode == 0)) {
  436. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  437. make_bad_inode(inode);
  438. }
  439. if (inode->i_state & I_NEW)
  440. unlock_new_inode(inode);
  441. if (lock_flags)
  442. xfs_iunlock(ip, lock_flags);
  443. IRELE(ip);
  444. }
  445. /*
  446. * This is called free all the memory associated with an inode.
  447. * It must free the inode itself and any buffers allocated for
  448. * if_extents/if_data and if_broot. It must also free the lock
  449. * associated with the inode.
  450. *
  451. * Note: because we don't initialise everything on reallocation out
  452. * of the zone, we must ensure we nullify everything correctly before
  453. * freeing the structure.
  454. */
  455. void
  456. xfs_ireclaim(
  457. struct xfs_inode *ip)
  458. {
  459. struct xfs_mount *mp = ip->i_mount;
  460. struct xfs_perag *pag;
  461. XFS_STATS_INC(xs_ig_reclaims);
  462. /*
  463. * Remove the inode from the per-AG radix tree. It doesn't matter
  464. * if it was never added to it because radix_tree_delete can deal
  465. * with that case just fine.
  466. */
  467. pag = xfs_get_perag(mp, ip->i_ino);
  468. write_lock(&pag->pag_ici_lock);
  469. radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
  470. write_unlock(&pag->pag_ici_lock);
  471. xfs_put_perag(mp, pag);
  472. /*
  473. * Here we do an (almost) spurious inode lock in order to coordinate
  474. * with inode cache radix tree lookups. This is because the lookup
  475. * can reference the inodes in the cache without taking references.
  476. *
  477. * We make that OK here by ensuring that we wait until the inode is
  478. * unlocked after the lookup before we go ahead and free it. We get
  479. * both the ilock and the iolock because the code may need to drop the
  480. * ilock one but will still hold the iolock.
  481. */
  482. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  483. xfs_qm_dqdetach(ip);
  484. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  485. xfs_inode_free(ip);
  486. }
  487. /*
  488. * This is a wrapper routine around the xfs_ilock() routine
  489. * used to centralize some grungy code. It is used in places
  490. * that wish to lock the inode solely for reading the extents.
  491. * The reason these places can't just call xfs_ilock(SHARED)
  492. * is that the inode lock also guards to bringing in of the
  493. * extents from disk for a file in b-tree format. If the inode
  494. * is in b-tree format, then we need to lock the inode exclusively
  495. * until the extents are read in. Locking it exclusively all
  496. * the time would limit our parallelism unnecessarily, though.
  497. * What we do instead is check to see if the extents have been
  498. * read in yet, and only lock the inode exclusively if they
  499. * have not.
  500. *
  501. * The function returns a value which should be given to the
  502. * corresponding xfs_iunlock_map_shared(). This value is
  503. * the mode in which the lock was actually taken.
  504. */
  505. uint
  506. xfs_ilock_map_shared(
  507. xfs_inode_t *ip)
  508. {
  509. uint lock_mode;
  510. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  511. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  512. lock_mode = XFS_ILOCK_EXCL;
  513. } else {
  514. lock_mode = XFS_ILOCK_SHARED;
  515. }
  516. xfs_ilock(ip, lock_mode);
  517. return lock_mode;
  518. }
  519. /*
  520. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  521. * All it does is call xfs_iunlock() with the given lock_mode.
  522. */
  523. void
  524. xfs_iunlock_map_shared(
  525. xfs_inode_t *ip,
  526. unsigned int lock_mode)
  527. {
  528. xfs_iunlock(ip, lock_mode);
  529. }
  530. /*
  531. * The xfs inode contains 2 locks: a multi-reader lock called the
  532. * i_iolock and a multi-reader lock called the i_lock. This routine
  533. * allows either or both of the locks to be obtained.
  534. *
  535. * The 2 locks should always be ordered so that the IO lock is
  536. * obtained first in order to prevent deadlock.
  537. *
  538. * ip -- the inode being locked
  539. * lock_flags -- this parameter indicates the inode's locks
  540. * to be locked. It can be:
  541. * XFS_IOLOCK_SHARED,
  542. * XFS_IOLOCK_EXCL,
  543. * XFS_ILOCK_SHARED,
  544. * XFS_ILOCK_EXCL,
  545. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  546. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  547. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  548. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  549. */
  550. void
  551. xfs_ilock(
  552. xfs_inode_t *ip,
  553. uint lock_flags)
  554. {
  555. /*
  556. * You can't set both SHARED and EXCL for the same lock,
  557. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  558. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  559. */
  560. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  561. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  562. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  563. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  564. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  565. if (lock_flags & XFS_IOLOCK_EXCL)
  566. mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  567. else if (lock_flags & XFS_IOLOCK_SHARED)
  568. mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
  569. if (lock_flags & XFS_ILOCK_EXCL)
  570. mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  571. else if (lock_flags & XFS_ILOCK_SHARED)
  572. mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
  573. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  574. }
  575. /*
  576. * This is just like xfs_ilock(), except that the caller
  577. * is guaranteed not to sleep. It returns 1 if it gets
  578. * the requested locks and 0 otherwise. If the IO lock is
  579. * obtained but the inode lock cannot be, then the IO lock
  580. * is dropped before returning.
  581. *
  582. * ip -- the inode being locked
  583. * lock_flags -- this parameter indicates the inode's locks to be
  584. * to be locked. See the comment for xfs_ilock() for a list
  585. * of valid values.
  586. */
  587. int
  588. xfs_ilock_nowait(
  589. xfs_inode_t *ip,
  590. uint lock_flags)
  591. {
  592. /*
  593. * You can't set both SHARED and EXCL for the same lock,
  594. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  595. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  596. */
  597. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  598. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  599. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  600. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  601. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
  602. if (lock_flags & XFS_IOLOCK_EXCL) {
  603. if (!mrtryupdate(&ip->i_iolock))
  604. goto out;
  605. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  606. if (!mrtryaccess(&ip->i_iolock))
  607. goto out;
  608. }
  609. if (lock_flags & XFS_ILOCK_EXCL) {
  610. if (!mrtryupdate(&ip->i_lock))
  611. goto out_undo_iolock;
  612. } else if (lock_flags & XFS_ILOCK_SHARED) {
  613. if (!mrtryaccess(&ip->i_lock))
  614. goto out_undo_iolock;
  615. }
  616. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  617. return 1;
  618. out_undo_iolock:
  619. if (lock_flags & XFS_IOLOCK_EXCL)
  620. mrunlock_excl(&ip->i_iolock);
  621. else if (lock_flags & XFS_IOLOCK_SHARED)
  622. mrunlock_shared(&ip->i_iolock);
  623. out:
  624. return 0;
  625. }
  626. /*
  627. * xfs_iunlock() is used to drop the inode locks acquired with
  628. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  629. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  630. * that we know which locks to drop.
  631. *
  632. * ip -- the inode being unlocked
  633. * lock_flags -- this parameter indicates the inode's locks to be
  634. * to be unlocked. See the comment for xfs_ilock() for a list
  635. * of valid values for this parameter.
  636. *
  637. */
  638. void
  639. xfs_iunlock(
  640. xfs_inode_t *ip,
  641. uint lock_flags)
  642. {
  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_IUNLOCK_NONOTIFY |
  653. XFS_LOCK_DEP_MASK)) == 0);
  654. ASSERT(lock_flags != 0);
  655. if (lock_flags & XFS_IOLOCK_EXCL)
  656. mrunlock_excl(&ip->i_iolock);
  657. else if (lock_flags & XFS_IOLOCK_SHARED)
  658. mrunlock_shared(&ip->i_iolock);
  659. if (lock_flags & XFS_ILOCK_EXCL)
  660. mrunlock_excl(&ip->i_lock);
  661. else if (lock_flags & XFS_ILOCK_SHARED)
  662. mrunlock_shared(&ip->i_lock);
  663. if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
  664. !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
  665. /*
  666. * Let the AIL know that this item has been unlocked in case
  667. * it is in the AIL and anyone is waiting on it. Don't do
  668. * this if the caller has asked us not to.
  669. */
  670. xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
  671. (xfs_log_item_t*)(ip->i_itemp));
  672. }
  673. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  674. }
  675. /*
  676. * give up write locks. the i/o lock cannot be held nested
  677. * if it is being demoted.
  678. */
  679. void
  680. xfs_ilock_demote(
  681. xfs_inode_t *ip,
  682. uint lock_flags)
  683. {
  684. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  685. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  686. if (lock_flags & XFS_ILOCK_EXCL)
  687. mrdemote(&ip->i_lock);
  688. if (lock_flags & XFS_IOLOCK_EXCL)
  689. mrdemote(&ip->i_iolock);
  690. }
  691. #ifdef DEBUG
  692. /*
  693. * Debug-only routine, without additional rw_semaphore APIs, we can
  694. * now only answer requests regarding whether we hold the lock for write
  695. * (reader state is outside our visibility, we only track writer state).
  696. *
  697. * Note: this means !xfs_isilocked would give false positives, so don't do that.
  698. */
  699. int
  700. xfs_isilocked(
  701. xfs_inode_t *ip,
  702. uint lock_flags)
  703. {
  704. if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
  705. XFS_ILOCK_EXCL) {
  706. if (!ip->i_lock.mr_writer)
  707. return 0;
  708. }
  709. if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
  710. XFS_IOLOCK_EXCL) {
  711. if (!ip->i_iolock.mr_writer)
  712. return 0;
  713. }
  714. return 1;
  715. }
  716. #endif
  717. #ifdef XFS_INODE_TRACE
  718. #define KTRACE_ENTER(ip, vk, s, line, ra) \
  719. ktrace_enter((ip)->i_trace, \
  720. /* 0 */ (void *)(__psint_t)(vk), \
  721. /* 1 */ (void *)(s), \
  722. /* 2 */ (void *)(__psint_t) line, \
  723. /* 3 */ (void *)(__psint_t)atomic_read(&VFS_I(ip)->i_count), \
  724. /* 4 */ (void *)(ra), \
  725. /* 5 */ NULL, \
  726. /* 6 */ (void *)(__psint_t)current_cpu(), \
  727. /* 7 */ (void *)(__psint_t)current_pid(), \
  728. /* 8 */ (void *)__return_address, \
  729. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  730. /*
  731. * Vnode tracing code.
  732. */
  733. void
  734. _xfs_itrace_entry(xfs_inode_t *ip, const char *func, inst_t *ra)
  735. {
  736. KTRACE_ENTER(ip, INODE_KTRACE_ENTRY, func, 0, ra);
  737. }
  738. void
  739. _xfs_itrace_exit(xfs_inode_t *ip, const char *func, inst_t *ra)
  740. {
  741. KTRACE_ENTER(ip, INODE_KTRACE_EXIT, func, 0, ra);
  742. }
  743. void
  744. xfs_itrace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  745. {
  746. KTRACE_ENTER(ip, INODE_KTRACE_HOLD, file, line, ra);
  747. }
  748. void
  749. _xfs_itrace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  750. {
  751. KTRACE_ENTER(ip, INODE_KTRACE_REF, file, line, ra);
  752. }
  753. void
  754. xfs_itrace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  755. {
  756. KTRACE_ENTER(ip, INODE_KTRACE_RELE, file, line, ra);
  757. }
  758. #endif /* XFS_INODE_TRACE */