xfs_iget.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. * Initialize the inode hash table for the newly mounted file system.
  43. * Choose an initial table size based on user specified value, else
  44. * use a simple algorithm using the maximum number of inodes as an
  45. * indicator for table size, and clamp it between one and some large
  46. * number of pages.
  47. */
  48. void
  49. xfs_ihash_init(xfs_mount_t *mp)
  50. {
  51. __uint64_t icount;
  52. uint i;
  53. if (!mp->m_ihsize) {
  54. icount = mp->m_maxicount ? mp->m_maxicount :
  55. (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog);
  56. mp->m_ihsize = 1 << max_t(uint, 8,
  57. (xfs_highbit64(icount) + 1) / 2);
  58. mp->m_ihsize = min_t(uint, mp->m_ihsize,
  59. (64 * NBPP) / sizeof(xfs_ihash_t));
  60. }
  61. mp->m_ihash = kmem_zalloc_greedy(&mp->m_ihsize,
  62. NBPC * sizeof(xfs_ihash_t),
  63. mp->m_ihsize * sizeof(xfs_ihash_t),
  64. KM_SLEEP | KM_MAYFAIL | KM_LARGE);
  65. mp->m_ihsize /= sizeof(xfs_ihash_t);
  66. for (i = 0; i < mp->m_ihsize; i++)
  67. rwlock_init(&(mp->m_ihash[i].ih_lock));
  68. }
  69. /*
  70. * Free up structures allocated by xfs_ihash_init, at unmount time.
  71. */
  72. void
  73. xfs_ihash_free(xfs_mount_t *mp)
  74. {
  75. kmem_free(mp->m_ihash, mp->m_ihsize * sizeof(xfs_ihash_t));
  76. mp->m_ihash = NULL;
  77. }
  78. /*
  79. * Initialize the inode cluster hash table for the newly mounted file system.
  80. * Its size is derived from the ihash table size.
  81. */
  82. void
  83. xfs_chash_init(xfs_mount_t *mp)
  84. {
  85. uint i;
  86. mp->m_chsize = max_t(uint, 1, mp->m_ihsize /
  87. (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog));
  88. mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize);
  89. mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize
  90. * sizeof(xfs_chash_t),
  91. KM_SLEEP | KM_LARGE);
  92. for (i = 0; i < mp->m_chsize; i++) {
  93. spinlock_init(&mp->m_chash[i].ch_lock,"xfshash");
  94. }
  95. }
  96. /*
  97. * Free up structures allocated by xfs_chash_init, at unmount time.
  98. */
  99. void
  100. xfs_chash_free(xfs_mount_t *mp)
  101. {
  102. int i;
  103. for (i = 0; i < mp->m_chsize; i++) {
  104. spinlock_destroy(&mp->m_chash[i].ch_lock);
  105. }
  106. kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
  107. mp->m_chash = NULL;
  108. }
  109. /*
  110. * Try to move an inode to the front of its hash list if possible
  111. * (and if its not there already). Called right after obtaining
  112. * the list version number and then dropping the read_lock on the
  113. * hash list in question (which is done right after looking up the
  114. * inode in question...).
  115. */
  116. STATIC void
  117. xfs_ihash_promote(
  118. xfs_ihash_t *ih,
  119. xfs_inode_t *ip,
  120. ulong version)
  121. {
  122. xfs_inode_t *iq;
  123. if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) {
  124. if (likely(version == ih->ih_version)) {
  125. /* remove from list */
  126. if ((iq = ip->i_next)) {
  127. iq->i_prevp = ip->i_prevp;
  128. }
  129. *ip->i_prevp = iq;
  130. /* insert at list head */
  131. iq = ih->ih_next;
  132. iq->i_prevp = &ip->i_next;
  133. ip->i_next = iq;
  134. ip->i_prevp = &ih->ih_next;
  135. ih->ih_next = ip;
  136. }
  137. write_unlock(&ih->ih_lock);
  138. }
  139. }
  140. /*
  141. * Look up an inode by number in the given file system.
  142. * The inode is looked up in the hash table for the file system
  143. * represented by the mount point parameter mp. Each bucket of
  144. * the hash table is guarded by an individual semaphore.
  145. *
  146. * If the inode is found in the hash table, its corresponding vnode
  147. * is obtained with a call to vn_get(). This call takes care of
  148. * coordination with the reclamation of the inode and vnode. Note
  149. * that the vmap structure is filled in while holding the hash lock.
  150. * This gives us the state of the inode/vnode when we found it and
  151. * is used for coordination in vn_get().
  152. *
  153. * If it is not in core, read it in from the file system's device and
  154. * add the inode into the hash table.
  155. *
  156. * The inode is locked according to the value of the lock_flags parameter.
  157. * This flag parameter indicates how and if the inode's IO lock and inode lock
  158. * should be taken.
  159. *
  160. * mp -- the mount point structure for the current file system. It points
  161. * to the inode hash table.
  162. * tp -- a pointer to the current transaction if there is one. This is
  163. * simply passed through to the xfs_iread() call.
  164. * ino -- the number of the inode desired. This is the unique identifier
  165. * within the file system for the inode being requested.
  166. * lock_flags -- flags indicating how to lock the inode. See the comment
  167. * for xfs_ilock() for a list of valid values.
  168. * bno -- the block number starting the buffer containing the inode,
  169. * if known (as by bulkstat), else 0.
  170. */
  171. STATIC int
  172. xfs_iget_core(
  173. bhv_vnode_t *vp,
  174. xfs_mount_t *mp,
  175. xfs_trans_t *tp,
  176. xfs_ino_t ino,
  177. uint flags,
  178. uint lock_flags,
  179. xfs_inode_t **ipp,
  180. xfs_daddr_t bno)
  181. {
  182. xfs_ihash_t *ih;
  183. xfs_inode_t *ip;
  184. xfs_inode_t *iq;
  185. bhv_vnode_t *inode_vp;
  186. ulong version;
  187. int error;
  188. /* REFERENCED */
  189. xfs_chash_t *ch;
  190. xfs_chashlist_t *chl, *chlnew;
  191. SPLDECL(s);
  192. ih = XFS_IHASH(mp, ino);
  193. again:
  194. read_lock(&ih->ih_lock);
  195. for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
  196. if (ip->i_ino == ino) {
  197. /*
  198. * If INEW is set this inode is being set up
  199. * we need to pause and try again.
  200. */
  201. if (ip->i_flags & XFS_INEW) {
  202. read_unlock(&ih->ih_lock);
  203. delay(1);
  204. XFS_STATS_INC(xs_ig_frecycle);
  205. goto again;
  206. }
  207. inode_vp = XFS_ITOV_NULL(ip);
  208. if (inode_vp == NULL) {
  209. /*
  210. * If IRECLAIM is set this inode is
  211. * on its way out of the system,
  212. * we need to pause and try again.
  213. */
  214. if (ip->i_flags & XFS_IRECLAIM) {
  215. read_unlock(&ih->ih_lock);
  216. delay(1);
  217. XFS_STATS_INC(xs_ig_frecycle);
  218. goto again;
  219. }
  220. vn_trace_exit(vp, "xfs_iget.alloc",
  221. (inst_t *)__return_address);
  222. XFS_STATS_INC(xs_ig_found);
  223. spin_lock(&ip->i_flags_lock);
  224. ip->i_flags &= ~XFS_IRECLAIMABLE;
  225. spin_unlock(&ip->i_flags_lock);
  226. version = ih->ih_version;
  227. read_unlock(&ih->ih_lock);
  228. xfs_ihash_promote(ih, ip, version);
  229. XFS_MOUNT_ILOCK(mp);
  230. list_del_init(&ip->i_reclaim);
  231. XFS_MOUNT_IUNLOCK(mp);
  232. goto finish_inode;
  233. } else if (vp != inode_vp) {
  234. struct inode *inode = vn_to_inode(inode_vp);
  235. /* The inode is being torn down, pause and
  236. * try again.
  237. */
  238. if (inode->i_state & (I_FREEING | I_CLEAR)) {
  239. read_unlock(&ih->ih_lock);
  240. delay(1);
  241. XFS_STATS_INC(xs_ig_frecycle);
  242. goto again;
  243. }
  244. /* Chances are the other vnode (the one in the inode) is being torn
  245. * down right now, and we landed on top of it. Question is, what do
  246. * we do? Unhook the old inode and hook up the new one?
  247. */
  248. cmn_err(CE_PANIC,
  249. "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
  250. inode_vp, vp);
  251. }
  252. /*
  253. * Inode cache hit: if ip is not at the front of
  254. * its hash chain, move it there now.
  255. * Do this with the lock held for update, but
  256. * do statistics after releasing the lock.
  257. */
  258. version = ih->ih_version;
  259. read_unlock(&ih->ih_lock);
  260. xfs_ihash_promote(ih, ip, version);
  261. XFS_STATS_INC(xs_ig_found);
  262. finish_inode:
  263. if (ip->i_d.di_mode == 0) {
  264. if (!(flags & XFS_IGET_CREATE))
  265. return ENOENT;
  266. xfs_iocore_inode_reinit(ip);
  267. }
  268. if (lock_flags != 0)
  269. xfs_ilock(ip, lock_flags);
  270. spin_lock(&ip->i_flags_lock);
  271. ip->i_flags &= ~XFS_ISTALE;
  272. spin_unlock(&ip->i_flags_lock);
  273. vn_trace_exit(vp, "xfs_iget.found",
  274. (inst_t *)__return_address);
  275. goto return_ip;
  276. }
  277. }
  278. /*
  279. * Inode cache miss: save the hash chain version stamp and unlock
  280. * the chain, so we don't deadlock in vn_alloc.
  281. */
  282. XFS_STATS_INC(xs_ig_missed);
  283. version = ih->ih_version;
  284. read_unlock(&ih->ih_lock);
  285. /*
  286. * Read the disk inode attributes into a new inode structure and get
  287. * a new vnode for it. This should also initialize i_ino and i_mount.
  288. */
  289. error = xfs_iread(mp, tp, ino, &ip, bno,
  290. (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
  291. if (error)
  292. return error;
  293. vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
  294. xfs_inode_lock_init(ip, vp);
  295. xfs_iocore_inode_init(ip);
  296. if (lock_flags)
  297. xfs_ilock(ip, lock_flags);
  298. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  299. xfs_idestroy(ip);
  300. return ENOENT;
  301. }
  302. /*
  303. * Put ip on its hash chain, unless someone else hashed a duplicate
  304. * after we released the hash lock.
  305. */
  306. write_lock(&ih->ih_lock);
  307. if (ih->ih_version != version) {
  308. for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
  309. if (iq->i_ino == ino) {
  310. write_unlock(&ih->ih_lock);
  311. xfs_idestroy(ip);
  312. XFS_STATS_INC(xs_ig_dup);
  313. goto again;
  314. }
  315. }
  316. }
  317. /*
  318. * These values _must_ be set before releasing ihlock!
  319. */
  320. ip->i_hash = ih;
  321. if ((iq = ih->ih_next)) {
  322. iq->i_prevp = &ip->i_next;
  323. }
  324. ip->i_next = iq;
  325. ip->i_prevp = &ih->ih_next;
  326. ih->ih_next = ip;
  327. ip->i_udquot = ip->i_gdquot = NULL;
  328. ih->ih_version++;
  329. spin_lock(&ip->i_flags_lock);
  330. ip->i_flags |= XFS_INEW;
  331. spin_unlock(&ip->i_flags_lock);
  332. write_unlock(&ih->ih_lock);
  333. /*
  334. * put ip on its cluster's hash chain
  335. */
  336. ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
  337. ip->i_cnext == NULL);
  338. chlnew = NULL;
  339. ch = XFS_CHASH(mp, ip->i_blkno);
  340. chlredo:
  341. s = mutex_spinlock(&ch->ch_lock);
  342. for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
  343. if (chl->chl_blkno == ip->i_blkno) {
  344. /* insert this inode into the doubly-linked list
  345. * where chl points */
  346. if ((iq = chl->chl_ip)) {
  347. ip->i_cprev = iq->i_cprev;
  348. iq->i_cprev->i_cnext = ip;
  349. iq->i_cprev = ip;
  350. ip->i_cnext = iq;
  351. } else {
  352. ip->i_cnext = ip;
  353. ip->i_cprev = ip;
  354. }
  355. chl->chl_ip = ip;
  356. ip->i_chash = chl;
  357. break;
  358. }
  359. }
  360. /* no hash list found for this block; add a new hash list */
  361. if (chl == NULL) {
  362. if (chlnew == NULL) {
  363. mutex_spinunlock(&ch->ch_lock, s);
  364. ASSERT(xfs_chashlist_zone != NULL);
  365. chlnew = (xfs_chashlist_t *)
  366. kmem_zone_alloc(xfs_chashlist_zone,
  367. KM_SLEEP);
  368. ASSERT(chlnew != NULL);
  369. goto chlredo;
  370. } else {
  371. ip->i_cnext = ip;
  372. ip->i_cprev = ip;
  373. ip->i_chash = chlnew;
  374. chlnew->chl_ip = ip;
  375. chlnew->chl_blkno = ip->i_blkno;
  376. if (ch->ch_list)
  377. ch->ch_list->chl_prev = chlnew;
  378. chlnew->chl_next = ch->ch_list;
  379. chlnew->chl_prev = NULL;
  380. ch->ch_list = chlnew;
  381. chlnew = NULL;
  382. }
  383. } else {
  384. if (chlnew != NULL) {
  385. kmem_zone_free(xfs_chashlist_zone, chlnew);
  386. }
  387. }
  388. mutex_spinunlock(&ch->ch_lock, s);
  389. /*
  390. * Link ip to its mount and thread it on the mount's inode list.
  391. */
  392. XFS_MOUNT_ILOCK(mp);
  393. if ((iq = mp->m_inodes)) {
  394. ASSERT(iq->i_mprev->i_mnext == iq);
  395. ip->i_mprev = iq->i_mprev;
  396. iq->i_mprev->i_mnext = ip;
  397. iq->i_mprev = ip;
  398. ip->i_mnext = iq;
  399. } else {
  400. ip->i_mnext = ip;
  401. ip->i_mprev = ip;
  402. }
  403. mp->m_inodes = ip;
  404. XFS_MOUNT_IUNLOCK(mp);
  405. return_ip:
  406. ASSERT(ip->i_df.if_ext_max ==
  407. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  408. ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
  409. ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
  410. *ipp = ip;
  411. /*
  412. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  413. * now. If it's a new inode being created, xfs_ialloc will handle it.
  414. */
  415. bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
  416. return 0;
  417. }
  418. /*
  419. * The 'normal' internal xfs_iget, if needed it will
  420. * 'allocate', or 'get', the vnode.
  421. */
  422. int
  423. xfs_iget(
  424. xfs_mount_t *mp,
  425. xfs_trans_t *tp,
  426. xfs_ino_t ino,
  427. uint flags,
  428. uint lock_flags,
  429. xfs_inode_t **ipp,
  430. xfs_daddr_t bno)
  431. {
  432. struct inode *inode;
  433. bhv_vnode_t *vp = NULL;
  434. int error;
  435. XFS_STATS_INC(xs_ig_attempts);
  436. retry:
  437. if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
  438. xfs_inode_t *ip;
  439. vp = vn_from_inode(inode);
  440. if (inode->i_state & I_NEW) {
  441. vn_initialize(inode);
  442. error = xfs_iget_core(vp, mp, tp, ino, flags,
  443. lock_flags, ipp, bno);
  444. if (error) {
  445. vn_mark_bad(vp);
  446. if (inode->i_state & I_NEW)
  447. unlock_new_inode(inode);
  448. iput(inode);
  449. }
  450. } else {
  451. /*
  452. * If the inode is not fully constructed due to
  453. * filehandle mismatches wait for the inode to go
  454. * away and try again.
  455. *
  456. * iget_locked will call __wait_on_freeing_inode
  457. * to wait for the inode to go away.
  458. */
  459. if (is_bad_inode(inode) ||
  460. ((ip = xfs_vtoi(vp)) == NULL)) {
  461. iput(inode);
  462. delay(1);
  463. goto retry;
  464. }
  465. if (lock_flags != 0)
  466. xfs_ilock(ip, lock_flags);
  467. XFS_STATS_INC(xs_ig_found);
  468. *ipp = ip;
  469. error = 0;
  470. }
  471. } else
  472. error = ENOMEM; /* If we got no inode we are out of memory */
  473. return error;
  474. }
  475. /*
  476. * Do the setup for the various locks within the incore inode.
  477. */
  478. void
  479. xfs_inode_lock_init(
  480. xfs_inode_t *ip,
  481. bhv_vnode_t *vp)
  482. {
  483. mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
  484. "xfsino", (long)vp->v_number);
  485. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
  486. init_waitqueue_head(&ip->i_ipin_wait);
  487. atomic_set(&ip->i_pincount, 0);
  488. initnsema(&ip->i_flock, 1, "xfsfino");
  489. }
  490. /*
  491. * Look for the inode corresponding to the given ino in the hash table.
  492. * If it is there and its i_transp pointer matches tp, return it.
  493. * Otherwise, return NULL.
  494. */
  495. xfs_inode_t *
  496. xfs_inode_incore(xfs_mount_t *mp,
  497. xfs_ino_t ino,
  498. xfs_trans_t *tp)
  499. {
  500. xfs_ihash_t *ih;
  501. xfs_inode_t *ip;
  502. ulong version;
  503. ih = XFS_IHASH(mp, ino);
  504. read_lock(&ih->ih_lock);
  505. for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
  506. if (ip->i_ino == ino) {
  507. /*
  508. * If we find it and tp matches, return it.
  509. * Also move it to the front of the hash list
  510. * if we find it and it is not already there.
  511. * Otherwise break from the loop and return
  512. * NULL.
  513. */
  514. if (ip->i_transp == tp) {
  515. version = ih->ih_version;
  516. read_unlock(&ih->ih_lock);
  517. xfs_ihash_promote(ih, ip, version);
  518. return (ip);
  519. }
  520. break;
  521. }
  522. }
  523. read_unlock(&ih->ih_lock);
  524. return (NULL);
  525. }
  526. /*
  527. * Decrement reference count of an inode structure and unlock it.
  528. *
  529. * ip -- the inode being released
  530. * lock_flags -- this parameter indicates the inode's locks to be
  531. * to be released. See the comment on xfs_iunlock() for a list
  532. * of valid values.
  533. */
  534. void
  535. xfs_iput(xfs_inode_t *ip,
  536. uint lock_flags)
  537. {
  538. bhv_vnode_t *vp = XFS_ITOV(ip);
  539. vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
  540. xfs_iunlock(ip, lock_flags);
  541. VN_RELE(vp);
  542. }
  543. /*
  544. * Special iput for brand-new inodes that are still locked
  545. */
  546. void
  547. xfs_iput_new(xfs_inode_t *ip,
  548. uint lock_flags)
  549. {
  550. bhv_vnode_t *vp = XFS_ITOV(ip);
  551. struct inode *inode = vn_to_inode(vp);
  552. vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
  553. if ((ip->i_d.di_mode == 0)) {
  554. ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
  555. vn_mark_bad(vp);
  556. }
  557. if (inode->i_state & I_NEW)
  558. unlock_new_inode(inode);
  559. if (lock_flags)
  560. xfs_iunlock(ip, lock_flags);
  561. VN_RELE(vp);
  562. }
  563. /*
  564. * This routine embodies the part of the reclaim code that pulls
  565. * the inode from the inode hash table and the mount structure's
  566. * inode list.
  567. * This should only be called from xfs_reclaim().
  568. */
  569. void
  570. xfs_ireclaim(xfs_inode_t *ip)
  571. {
  572. bhv_vnode_t *vp;
  573. /*
  574. * Remove from old hash list and mount list.
  575. */
  576. XFS_STATS_INC(xs_ig_reclaims);
  577. xfs_iextract(ip);
  578. /*
  579. * Here we do a spurious inode lock in order to coordinate with
  580. * xfs_sync(). This is because xfs_sync() references the inodes
  581. * in the mount list without taking references on the corresponding
  582. * vnodes. We make that OK here by ensuring that we wait until
  583. * the inode is unlocked in xfs_sync() before we go ahead and
  584. * free it. We get both the regular lock and the io lock because
  585. * the xfs_sync() code may need to drop the regular one but will
  586. * still hold the io lock.
  587. */
  588. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  589. /*
  590. * Release dquots (and their references) if any. An inode may escape
  591. * xfs_inactive and get here via vn_alloc->vn_reclaim path.
  592. */
  593. XFS_QM_DQDETACH(ip->i_mount, ip);
  594. /*
  595. * Pull our behavior descriptor from the vnode chain.
  596. */
  597. vp = XFS_ITOV_NULL(ip);
  598. if (vp) {
  599. vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
  600. }
  601. /*
  602. * Free all memory associated with the inode.
  603. */
  604. xfs_idestroy(ip);
  605. }
  606. /*
  607. * This routine removes an about-to-be-destroyed inode from
  608. * all of the lists in which it is located with the exception
  609. * of the behavior chain.
  610. */
  611. void
  612. xfs_iextract(
  613. xfs_inode_t *ip)
  614. {
  615. xfs_ihash_t *ih;
  616. xfs_inode_t *iq;
  617. xfs_mount_t *mp;
  618. xfs_chash_t *ch;
  619. xfs_chashlist_t *chl, *chm;
  620. SPLDECL(s);
  621. ih = ip->i_hash;
  622. write_lock(&ih->ih_lock);
  623. if ((iq = ip->i_next)) {
  624. iq->i_prevp = ip->i_prevp;
  625. }
  626. *ip->i_prevp = iq;
  627. ih->ih_version++;
  628. write_unlock(&ih->ih_lock);
  629. /*
  630. * Remove from cluster hash list
  631. * 1) delete the chashlist if this is the last inode on the chashlist
  632. * 2) unchain from list of inodes
  633. * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
  634. */
  635. mp = ip->i_mount;
  636. ch = XFS_CHASH(mp, ip->i_blkno);
  637. s = mutex_spinlock(&ch->ch_lock);
  638. if (ip->i_cnext == ip) {
  639. /* Last inode on chashlist */
  640. ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
  641. ASSERT(ip->i_chash != NULL);
  642. chm=NULL;
  643. chl = ip->i_chash;
  644. if (chl->chl_prev)
  645. chl->chl_prev->chl_next = chl->chl_next;
  646. else
  647. ch->ch_list = chl->chl_next;
  648. if (chl->chl_next)
  649. chl->chl_next->chl_prev = chl->chl_prev;
  650. kmem_zone_free(xfs_chashlist_zone, chl);
  651. } else {
  652. /* delete one inode from a non-empty list */
  653. iq = ip->i_cnext;
  654. iq->i_cprev = ip->i_cprev;
  655. ip->i_cprev->i_cnext = iq;
  656. if (ip->i_chash->chl_ip == ip) {
  657. ip->i_chash->chl_ip = iq;
  658. }
  659. ip->i_chash = __return_address;
  660. ip->i_cprev = __return_address;
  661. ip->i_cnext = __return_address;
  662. }
  663. mutex_spinunlock(&ch->ch_lock, s);
  664. /*
  665. * Remove from mount's inode list.
  666. */
  667. XFS_MOUNT_ILOCK(mp);
  668. ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
  669. iq = ip->i_mnext;
  670. iq->i_mprev = ip->i_mprev;
  671. ip->i_mprev->i_mnext = iq;
  672. /*
  673. * Fix up the head pointer if it points to the inode being deleted.
  674. */
  675. if (mp->m_inodes == ip) {
  676. if (ip == iq) {
  677. mp->m_inodes = NULL;
  678. } else {
  679. mp->m_inodes = iq;
  680. }
  681. }
  682. /* Deal with the deleted inodes list */
  683. list_del_init(&ip->i_reclaim);
  684. mp->m_ireclaims++;
  685. XFS_MOUNT_IUNLOCK(mp);
  686. }
  687. /*
  688. * This is a wrapper routine around the xfs_ilock() routine
  689. * used to centralize some grungy code. It is used in places
  690. * that wish to lock the inode solely for reading the extents.
  691. * The reason these places can't just call xfs_ilock(SHARED)
  692. * is that the inode lock also guards to bringing in of the
  693. * extents from disk for a file in b-tree format. If the inode
  694. * is in b-tree format, then we need to lock the inode exclusively
  695. * until the extents are read in. Locking it exclusively all
  696. * the time would limit our parallelism unnecessarily, though.
  697. * What we do instead is check to see if the extents have been
  698. * read in yet, and only lock the inode exclusively if they
  699. * have not.
  700. *
  701. * The function returns a value which should be given to the
  702. * corresponding xfs_iunlock_map_shared(). This value is
  703. * the mode in which the lock was actually taken.
  704. */
  705. uint
  706. xfs_ilock_map_shared(
  707. xfs_inode_t *ip)
  708. {
  709. uint lock_mode;
  710. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  711. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  712. lock_mode = XFS_ILOCK_EXCL;
  713. } else {
  714. lock_mode = XFS_ILOCK_SHARED;
  715. }
  716. xfs_ilock(ip, lock_mode);
  717. return lock_mode;
  718. }
  719. /*
  720. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  721. * All it does is call xfs_iunlock() with the given lock_mode.
  722. */
  723. void
  724. xfs_iunlock_map_shared(
  725. xfs_inode_t *ip,
  726. unsigned int lock_mode)
  727. {
  728. xfs_iunlock(ip, lock_mode);
  729. }
  730. /*
  731. * The xfs inode contains 2 locks: a multi-reader lock called the
  732. * i_iolock and a multi-reader lock called the i_lock. This routine
  733. * allows either or both of the locks to be obtained.
  734. *
  735. * The 2 locks should always be ordered so that the IO lock is
  736. * obtained first in order to prevent deadlock.
  737. *
  738. * ip -- the inode being locked
  739. * lock_flags -- this parameter indicates the inode's locks
  740. * to be locked. It can be:
  741. * XFS_IOLOCK_SHARED,
  742. * XFS_IOLOCK_EXCL,
  743. * XFS_ILOCK_SHARED,
  744. * XFS_ILOCK_EXCL,
  745. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  746. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  747. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  748. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  749. */
  750. void
  751. xfs_ilock(xfs_inode_t *ip,
  752. uint lock_flags)
  753. {
  754. /*
  755. * You can't set both SHARED and EXCL for the same lock,
  756. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  757. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  758. */
  759. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  760. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  761. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  762. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  763. ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
  764. if (lock_flags & XFS_IOLOCK_EXCL) {
  765. mrupdate(&ip->i_iolock);
  766. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  767. mraccess(&ip->i_iolock);
  768. }
  769. if (lock_flags & XFS_ILOCK_EXCL) {
  770. mrupdate(&ip->i_lock);
  771. } else if (lock_flags & XFS_ILOCK_SHARED) {
  772. mraccess(&ip->i_lock);
  773. }
  774. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  775. }
  776. /*
  777. * This is just like xfs_ilock(), except that the caller
  778. * is guaranteed not to sleep. It returns 1 if it gets
  779. * the requested locks and 0 otherwise. If the IO lock is
  780. * obtained but the inode lock cannot be, then the IO lock
  781. * is dropped before returning.
  782. *
  783. * ip -- the inode being locked
  784. * lock_flags -- this parameter indicates the inode's locks to be
  785. * to be locked. See the comment for xfs_ilock() for a list
  786. * of valid values.
  787. *
  788. */
  789. int
  790. xfs_ilock_nowait(xfs_inode_t *ip,
  791. uint lock_flags)
  792. {
  793. int iolocked;
  794. int ilocked;
  795. /*
  796. * You can't set both SHARED and EXCL for the same lock,
  797. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  798. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  799. */
  800. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  801. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  802. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  803. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  804. ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
  805. iolocked = 0;
  806. if (lock_flags & XFS_IOLOCK_EXCL) {
  807. iolocked = mrtryupdate(&ip->i_iolock);
  808. if (!iolocked) {
  809. return 0;
  810. }
  811. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  812. iolocked = mrtryaccess(&ip->i_iolock);
  813. if (!iolocked) {
  814. return 0;
  815. }
  816. }
  817. if (lock_flags & XFS_ILOCK_EXCL) {
  818. ilocked = mrtryupdate(&ip->i_lock);
  819. if (!ilocked) {
  820. if (iolocked) {
  821. mrunlock(&ip->i_iolock);
  822. }
  823. return 0;
  824. }
  825. } else if (lock_flags & XFS_ILOCK_SHARED) {
  826. ilocked = mrtryaccess(&ip->i_lock);
  827. if (!ilocked) {
  828. if (iolocked) {
  829. mrunlock(&ip->i_iolock);
  830. }
  831. return 0;
  832. }
  833. }
  834. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  835. return 1;
  836. }
  837. /*
  838. * xfs_iunlock() is used to drop the inode locks acquired with
  839. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  840. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  841. * that we know which locks to drop.
  842. *
  843. * ip -- the inode being unlocked
  844. * lock_flags -- this parameter indicates the inode's locks to be
  845. * to be unlocked. See the comment for xfs_ilock() for a list
  846. * of valid values for this parameter.
  847. *
  848. */
  849. void
  850. xfs_iunlock(xfs_inode_t *ip,
  851. uint lock_flags)
  852. {
  853. /*
  854. * You can't set both SHARED and EXCL for the same lock,
  855. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  856. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  857. */
  858. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  859. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  860. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  861. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  862. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
  863. ASSERT(lock_flags != 0);
  864. if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
  865. ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
  866. (ismrlocked(&ip->i_iolock, MR_ACCESS)));
  867. ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
  868. (ismrlocked(&ip->i_iolock, MR_UPDATE)));
  869. mrunlock(&ip->i_iolock);
  870. }
  871. if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
  872. ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
  873. (ismrlocked(&ip->i_lock, MR_ACCESS)));
  874. ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
  875. (ismrlocked(&ip->i_lock, MR_UPDATE)));
  876. mrunlock(&ip->i_lock);
  877. /*
  878. * Let the AIL know that this item has been unlocked in case
  879. * it is in the AIL and anyone is waiting on it. Don't do
  880. * this if the caller has asked us not to.
  881. */
  882. if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
  883. ip->i_itemp != NULL) {
  884. xfs_trans_unlocked_item(ip->i_mount,
  885. (xfs_log_item_t*)(ip->i_itemp));
  886. }
  887. }
  888. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  889. }
  890. /*
  891. * give up write locks. the i/o lock cannot be held nested
  892. * if it is being demoted.
  893. */
  894. void
  895. xfs_ilock_demote(xfs_inode_t *ip,
  896. uint lock_flags)
  897. {
  898. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  899. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  900. if (lock_flags & XFS_ILOCK_EXCL) {
  901. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  902. mrdemote(&ip->i_lock);
  903. }
  904. if (lock_flags & XFS_IOLOCK_EXCL) {
  905. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  906. mrdemote(&ip->i_iolock);
  907. }
  908. }
  909. /*
  910. * The following three routines simply manage the i_flock
  911. * semaphore embedded in the inode. This semaphore synchronizes
  912. * processes attempting to flush the in-core inode back to disk.
  913. */
  914. void
  915. xfs_iflock(xfs_inode_t *ip)
  916. {
  917. psema(&(ip->i_flock), PINOD|PLTWAIT);
  918. }
  919. int
  920. xfs_iflock_nowait(xfs_inode_t *ip)
  921. {
  922. return (cpsema(&(ip->i_flock)));
  923. }
  924. void
  925. xfs_ifunlock(xfs_inode_t *ip)
  926. {
  927. ASSERT(issemalocked(&(ip->i_flock)));
  928. vsema(&(ip->i_flock));
  929. }