xfs_iget.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 (xfs_iflags_test(ip, 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 (xfs_iflags_test(ip, XFS_IRECLAIM)) {
  215. read_unlock(&ih->ih_lock);
  216. delay(1);
  217. XFS_STATS_INC(xs_ig_frecycle);
  218. goto again;
  219. }
  220. ASSERT(xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  221. /*
  222. * If lookup is racing with unlink, then we
  223. * should return an error immediately so we
  224. * don't remove it from the reclaim list and
  225. * potentially leak the inode.
  226. */
  227. if ((ip->i_d.di_mode == 0) &&
  228. !(flags & XFS_IGET_CREATE)) {
  229. read_unlock(&ih->ih_lock);
  230. return ENOENT;
  231. }
  232. /*
  233. * There may be transactions sitting in the
  234. * incore log buffers or being flushed to disk
  235. * at this time. We can't clear the
  236. * XFS_IRECLAIMABLE flag until these
  237. * transactions have hit the disk, otherwise we
  238. * will void the guarantee the flag provides
  239. * xfs_iunpin()
  240. */
  241. if (xfs_ipincount(ip)) {
  242. read_unlock(&ih->ih_lock);
  243. xfs_log_force(mp, 0,
  244. XFS_LOG_FORCE|XFS_LOG_SYNC);
  245. XFS_STATS_INC(xs_ig_frecycle);
  246. goto again;
  247. }
  248. vn_trace_exit(vp, "xfs_iget.alloc",
  249. (inst_t *)__return_address);
  250. XFS_STATS_INC(xs_ig_found);
  251. xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
  252. version = ih->ih_version;
  253. read_unlock(&ih->ih_lock);
  254. xfs_ihash_promote(ih, ip, version);
  255. XFS_MOUNT_ILOCK(mp);
  256. list_del_init(&ip->i_reclaim);
  257. XFS_MOUNT_IUNLOCK(mp);
  258. goto finish_inode;
  259. } else if (vp != inode_vp) {
  260. struct inode *inode = vn_to_inode(inode_vp);
  261. /* The inode is being torn down, pause and
  262. * try again.
  263. */
  264. if (inode->i_state & (I_FREEING | I_CLEAR)) {
  265. read_unlock(&ih->ih_lock);
  266. delay(1);
  267. XFS_STATS_INC(xs_ig_frecycle);
  268. goto again;
  269. }
  270. /* Chances are the other vnode (the one in the inode) is being torn
  271. * down right now, and we landed on top of it. Question is, what do
  272. * we do? Unhook the old inode and hook up the new one?
  273. */
  274. cmn_err(CE_PANIC,
  275. "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
  276. inode_vp, vp);
  277. }
  278. /*
  279. * Inode cache hit: if ip is not at the front of
  280. * its hash chain, move it there now.
  281. * Do this with the lock held for update, but
  282. * do statistics after releasing the lock.
  283. */
  284. version = ih->ih_version;
  285. read_unlock(&ih->ih_lock);
  286. xfs_ihash_promote(ih, ip, version);
  287. XFS_STATS_INC(xs_ig_found);
  288. finish_inode:
  289. if (ip->i_d.di_mode == 0) {
  290. if (!(flags & XFS_IGET_CREATE))
  291. return ENOENT;
  292. xfs_iocore_inode_reinit(ip);
  293. }
  294. if (lock_flags != 0)
  295. xfs_ilock(ip, lock_flags);
  296. xfs_iflags_clear(ip, XFS_ISTALE);
  297. vn_trace_exit(vp, "xfs_iget.found",
  298. (inst_t *)__return_address);
  299. goto return_ip;
  300. }
  301. }
  302. /*
  303. * Inode cache miss: save the hash chain version stamp and unlock
  304. * the chain, so we don't deadlock in vn_alloc.
  305. */
  306. XFS_STATS_INC(xs_ig_missed);
  307. version = ih->ih_version;
  308. read_unlock(&ih->ih_lock);
  309. /*
  310. * Read the disk inode attributes into a new inode structure and get
  311. * a new vnode for it. This should also initialize i_ino and i_mount.
  312. */
  313. error = xfs_iread(mp, tp, ino, &ip, bno,
  314. (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
  315. if (error)
  316. return error;
  317. vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
  318. xfs_inode_lock_init(ip, vp);
  319. xfs_iocore_inode_init(ip);
  320. if (lock_flags)
  321. xfs_ilock(ip, lock_flags);
  322. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  323. xfs_idestroy(ip);
  324. return ENOENT;
  325. }
  326. /*
  327. * Put ip on its hash chain, unless someone else hashed a duplicate
  328. * after we released the hash lock.
  329. */
  330. write_lock(&ih->ih_lock);
  331. if (ih->ih_version != version) {
  332. for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
  333. if (iq->i_ino == ino) {
  334. write_unlock(&ih->ih_lock);
  335. xfs_idestroy(ip);
  336. XFS_STATS_INC(xs_ig_dup);
  337. goto again;
  338. }
  339. }
  340. }
  341. /*
  342. * These values _must_ be set before releasing ihlock!
  343. */
  344. ip->i_hash = ih;
  345. if ((iq = ih->ih_next)) {
  346. iq->i_prevp = &ip->i_next;
  347. }
  348. ip->i_next = iq;
  349. ip->i_prevp = &ih->ih_next;
  350. ih->ih_next = ip;
  351. ip->i_udquot = ip->i_gdquot = NULL;
  352. ih->ih_version++;
  353. xfs_iflags_set(ip, XFS_INEW);
  354. write_unlock(&ih->ih_lock);
  355. /*
  356. * put ip on its cluster's hash chain
  357. */
  358. ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
  359. ip->i_cnext == NULL);
  360. chlnew = NULL;
  361. ch = XFS_CHASH(mp, ip->i_blkno);
  362. chlredo:
  363. s = mutex_spinlock(&ch->ch_lock);
  364. for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
  365. if (chl->chl_blkno == ip->i_blkno) {
  366. /* insert this inode into the doubly-linked list
  367. * where chl points */
  368. if ((iq = chl->chl_ip)) {
  369. ip->i_cprev = iq->i_cprev;
  370. iq->i_cprev->i_cnext = ip;
  371. iq->i_cprev = ip;
  372. ip->i_cnext = iq;
  373. } else {
  374. ip->i_cnext = ip;
  375. ip->i_cprev = ip;
  376. }
  377. chl->chl_ip = ip;
  378. ip->i_chash = chl;
  379. break;
  380. }
  381. }
  382. /* no hash list found for this block; add a new hash list */
  383. if (chl == NULL) {
  384. if (chlnew == NULL) {
  385. mutex_spinunlock(&ch->ch_lock, s);
  386. ASSERT(xfs_chashlist_zone != NULL);
  387. chlnew = (xfs_chashlist_t *)
  388. kmem_zone_alloc(xfs_chashlist_zone,
  389. KM_SLEEP);
  390. ASSERT(chlnew != NULL);
  391. goto chlredo;
  392. } else {
  393. ip->i_cnext = ip;
  394. ip->i_cprev = ip;
  395. ip->i_chash = chlnew;
  396. chlnew->chl_ip = ip;
  397. chlnew->chl_blkno = ip->i_blkno;
  398. if (ch->ch_list)
  399. ch->ch_list->chl_prev = chlnew;
  400. chlnew->chl_next = ch->ch_list;
  401. chlnew->chl_prev = NULL;
  402. ch->ch_list = chlnew;
  403. chlnew = NULL;
  404. }
  405. } else {
  406. if (chlnew != NULL) {
  407. kmem_zone_free(xfs_chashlist_zone, chlnew);
  408. }
  409. }
  410. mutex_spinunlock(&ch->ch_lock, s);
  411. /*
  412. * Link ip to its mount and thread it on the mount's inode list.
  413. */
  414. XFS_MOUNT_ILOCK(mp);
  415. if ((iq = mp->m_inodes)) {
  416. ASSERT(iq->i_mprev->i_mnext == iq);
  417. ip->i_mprev = iq->i_mprev;
  418. iq->i_mprev->i_mnext = ip;
  419. iq->i_mprev = ip;
  420. ip->i_mnext = iq;
  421. } else {
  422. ip->i_mnext = ip;
  423. ip->i_mprev = ip;
  424. }
  425. mp->m_inodes = ip;
  426. XFS_MOUNT_IUNLOCK(mp);
  427. return_ip:
  428. ASSERT(ip->i_df.if_ext_max ==
  429. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
  430. ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
  431. ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
  432. *ipp = ip;
  433. /*
  434. * If we have a real type for an on-disk inode, we can set ops(&unlock)
  435. * now. If it's a new inode being created, xfs_ialloc will handle it.
  436. */
  437. bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
  438. return 0;
  439. }
  440. /*
  441. * The 'normal' internal xfs_iget, if needed it will
  442. * 'allocate', or 'get', the vnode.
  443. */
  444. int
  445. xfs_iget(
  446. xfs_mount_t *mp,
  447. xfs_trans_t *tp,
  448. xfs_ino_t ino,
  449. uint flags,
  450. uint lock_flags,
  451. xfs_inode_t **ipp,
  452. xfs_daddr_t bno)
  453. {
  454. struct inode *inode;
  455. bhv_vnode_t *vp = NULL;
  456. int error;
  457. XFS_STATS_INC(xs_ig_attempts);
  458. retry:
  459. if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
  460. xfs_inode_t *ip;
  461. vp = vn_from_inode(inode);
  462. if (inode->i_state & I_NEW) {
  463. vn_initialize(inode);
  464. error = xfs_iget_core(vp, mp, tp, ino, flags,
  465. lock_flags, ipp, bno);
  466. if (error) {
  467. vn_mark_bad(vp);
  468. if (inode->i_state & I_NEW)
  469. unlock_new_inode(inode);
  470. iput(inode);
  471. }
  472. } else {
  473. /*
  474. * If the inode is not fully constructed due to
  475. * filehandle mismatches wait for the inode to go
  476. * away and try again.
  477. *
  478. * iget_locked will call __wait_on_freeing_inode
  479. * to wait for the inode to go away.
  480. */
  481. if (is_bad_inode(inode) ||
  482. ((ip = xfs_vtoi(vp)) == NULL)) {
  483. iput(inode);
  484. delay(1);
  485. goto retry;
  486. }
  487. if (lock_flags != 0)
  488. xfs_ilock(ip, lock_flags);
  489. XFS_STATS_INC(xs_ig_found);
  490. *ipp = ip;
  491. error = 0;
  492. }
  493. } else
  494. error = ENOMEM; /* If we got no inode we are out of memory */
  495. return error;
  496. }
  497. /*
  498. * Do the setup for the various locks within the incore inode.
  499. */
  500. void
  501. xfs_inode_lock_init(
  502. xfs_inode_t *ip,
  503. bhv_vnode_t *vp)
  504. {
  505. mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
  506. "xfsino", (long)vp->v_number);
  507. mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
  508. init_waitqueue_head(&ip->i_ipin_wait);
  509. atomic_set(&ip->i_pincount, 0);
  510. initnsema(&ip->i_flock, 1, "xfsfino");
  511. }
  512. /*
  513. * Look for the inode corresponding to the given ino in the hash table.
  514. * If it is there and its i_transp pointer matches tp, return it.
  515. * Otherwise, return NULL.
  516. */
  517. xfs_inode_t *
  518. xfs_inode_incore(xfs_mount_t *mp,
  519. xfs_ino_t ino,
  520. xfs_trans_t *tp)
  521. {
  522. xfs_ihash_t *ih;
  523. xfs_inode_t *ip;
  524. ulong version;
  525. ih = XFS_IHASH(mp, ino);
  526. read_lock(&ih->ih_lock);
  527. for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
  528. if (ip->i_ino == ino) {
  529. /*
  530. * If we find it and tp matches, return it.
  531. * Also move it to the front of the hash list
  532. * if we find it and it is not already there.
  533. * Otherwise break from the loop and return
  534. * NULL.
  535. */
  536. if (ip->i_transp == tp) {
  537. version = ih->ih_version;
  538. read_unlock(&ih->ih_lock);
  539. xfs_ihash_promote(ih, ip, version);
  540. return (ip);
  541. }
  542. break;
  543. }
  544. }
  545. read_unlock(&ih->ih_lock);
  546. return (NULL);
  547. }
  548. /*
  549. * Decrement reference count of an inode structure and unlock it.
  550. *
  551. * ip -- the inode being released
  552. * lock_flags -- this parameter indicates the inode's locks to be
  553. * to be released. See the comment on xfs_iunlock() for a list
  554. * of valid values.
  555. */
  556. void
  557. xfs_iput(xfs_inode_t *ip,
  558. uint lock_flags)
  559. {
  560. bhv_vnode_t *vp = XFS_ITOV(ip);
  561. vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
  562. xfs_iunlock(ip, lock_flags);
  563. VN_RELE(vp);
  564. }
  565. /*
  566. * Special iput for brand-new inodes that are still locked
  567. */
  568. void
  569. xfs_iput_new(xfs_inode_t *ip,
  570. uint lock_flags)
  571. {
  572. bhv_vnode_t *vp = XFS_ITOV(ip);
  573. struct inode *inode = vn_to_inode(vp);
  574. vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
  575. if ((ip->i_d.di_mode == 0)) {
  576. ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
  577. vn_mark_bad(vp);
  578. }
  579. if (inode->i_state & I_NEW)
  580. unlock_new_inode(inode);
  581. if (lock_flags)
  582. xfs_iunlock(ip, lock_flags);
  583. VN_RELE(vp);
  584. }
  585. /*
  586. * This routine embodies the part of the reclaim code that pulls
  587. * the inode from the inode hash table and the mount structure's
  588. * inode list.
  589. * This should only be called from xfs_reclaim().
  590. */
  591. void
  592. xfs_ireclaim(xfs_inode_t *ip)
  593. {
  594. bhv_vnode_t *vp;
  595. /*
  596. * Remove from old hash list and mount list.
  597. */
  598. XFS_STATS_INC(xs_ig_reclaims);
  599. xfs_iextract(ip);
  600. /*
  601. * Here we do a spurious inode lock in order to coordinate with
  602. * xfs_sync(). This is because xfs_sync() references the inodes
  603. * in the mount list without taking references on the corresponding
  604. * vnodes. We make that OK here by ensuring that we wait until
  605. * the inode is unlocked in xfs_sync() before we go ahead and
  606. * free it. We get both the regular lock and the io lock because
  607. * the xfs_sync() code may need to drop the regular one but will
  608. * still hold the io lock.
  609. */
  610. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  611. /*
  612. * Release dquots (and their references) if any. An inode may escape
  613. * xfs_inactive and get here via vn_alloc->vn_reclaim path.
  614. */
  615. XFS_QM_DQDETACH(ip->i_mount, ip);
  616. /*
  617. * Pull our behavior descriptor from the vnode chain.
  618. */
  619. vp = XFS_ITOV_NULL(ip);
  620. if (vp) {
  621. vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
  622. }
  623. /*
  624. * Free all memory associated with the inode.
  625. */
  626. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  627. xfs_idestroy(ip);
  628. }
  629. /*
  630. * This routine removes an about-to-be-destroyed inode from
  631. * all of the lists in which it is located with the exception
  632. * of the behavior chain.
  633. */
  634. void
  635. xfs_iextract(
  636. xfs_inode_t *ip)
  637. {
  638. xfs_ihash_t *ih;
  639. xfs_inode_t *iq;
  640. xfs_mount_t *mp;
  641. xfs_chash_t *ch;
  642. xfs_chashlist_t *chl, *chm;
  643. SPLDECL(s);
  644. ih = ip->i_hash;
  645. write_lock(&ih->ih_lock);
  646. if ((iq = ip->i_next)) {
  647. iq->i_prevp = ip->i_prevp;
  648. }
  649. *ip->i_prevp = iq;
  650. ih->ih_version++;
  651. write_unlock(&ih->ih_lock);
  652. /*
  653. * Remove from cluster hash list
  654. * 1) delete the chashlist if this is the last inode on the chashlist
  655. * 2) unchain from list of inodes
  656. * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
  657. */
  658. mp = ip->i_mount;
  659. ch = XFS_CHASH(mp, ip->i_blkno);
  660. s = mutex_spinlock(&ch->ch_lock);
  661. if (ip->i_cnext == ip) {
  662. /* Last inode on chashlist */
  663. ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
  664. ASSERT(ip->i_chash != NULL);
  665. chm=NULL;
  666. chl = ip->i_chash;
  667. if (chl->chl_prev)
  668. chl->chl_prev->chl_next = chl->chl_next;
  669. else
  670. ch->ch_list = chl->chl_next;
  671. if (chl->chl_next)
  672. chl->chl_next->chl_prev = chl->chl_prev;
  673. kmem_zone_free(xfs_chashlist_zone, chl);
  674. } else {
  675. /* delete one inode from a non-empty list */
  676. iq = ip->i_cnext;
  677. iq->i_cprev = ip->i_cprev;
  678. ip->i_cprev->i_cnext = iq;
  679. if (ip->i_chash->chl_ip == ip) {
  680. ip->i_chash->chl_ip = iq;
  681. }
  682. ip->i_chash = __return_address;
  683. ip->i_cprev = __return_address;
  684. ip->i_cnext = __return_address;
  685. }
  686. mutex_spinunlock(&ch->ch_lock, s);
  687. /*
  688. * Remove from mount's inode list.
  689. */
  690. XFS_MOUNT_ILOCK(mp);
  691. ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
  692. iq = ip->i_mnext;
  693. iq->i_mprev = ip->i_mprev;
  694. ip->i_mprev->i_mnext = iq;
  695. /*
  696. * Fix up the head pointer if it points to the inode being deleted.
  697. */
  698. if (mp->m_inodes == ip) {
  699. if (ip == iq) {
  700. mp->m_inodes = NULL;
  701. } else {
  702. mp->m_inodes = iq;
  703. }
  704. }
  705. /* Deal with the deleted inodes list */
  706. list_del_init(&ip->i_reclaim);
  707. mp->m_ireclaims++;
  708. XFS_MOUNT_IUNLOCK(mp);
  709. }
  710. /*
  711. * This is a wrapper routine around the xfs_ilock() routine
  712. * used to centralize some grungy code. It is used in places
  713. * that wish to lock the inode solely for reading the extents.
  714. * The reason these places can't just call xfs_ilock(SHARED)
  715. * is that the inode lock also guards to bringing in of the
  716. * extents from disk for a file in b-tree format. If the inode
  717. * is in b-tree format, then we need to lock the inode exclusively
  718. * until the extents are read in. Locking it exclusively all
  719. * the time would limit our parallelism unnecessarily, though.
  720. * What we do instead is check to see if the extents have been
  721. * read in yet, and only lock the inode exclusively if they
  722. * have not.
  723. *
  724. * The function returns a value which should be given to the
  725. * corresponding xfs_iunlock_map_shared(). This value is
  726. * the mode in which the lock was actually taken.
  727. */
  728. uint
  729. xfs_ilock_map_shared(
  730. xfs_inode_t *ip)
  731. {
  732. uint lock_mode;
  733. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  734. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  735. lock_mode = XFS_ILOCK_EXCL;
  736. } else {
  737. lock_mode = XFS_ILOCK_SHARED;
  738. }
  739. xfs_ilock(ip, lock_mode);
  740. return lock_mode;
  741. }
  742. /*
  743. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  744. * All it does is call xfs_iunlock() with the given lock_mode.
  745. */
  746. void
  747. xfs_iunlock_map_shared(
  748. xfs_inode_t *ip,
  749. unsigned int lock_mode)
  750. {
  751. xfs_iunlock(ip, lock_mode);
  752. }
  753. /*
  754. * The xfs inode contains 2 locks: a multi-reader lock called the
  755. * i_iolock and a multi-reader lock called the i_lock. This routine
  756. * allows either or both of the locks to be obtained.
  757. *
  758. * The 2 locks should always be ordered so that the IO lock is
  759. * obtained first in order to prevent deadlock.
  760. *
  761. * ip -- the inode being locked
  762. * lock_flags -- this parameter indicates the inode's locks
  763. * to be locked. It can be:
  764. * XFS_IOLOCK_SHARED,
  765. * XFS_IOLOCK_EXCL,
  766. * XFS_ILOCK_SHARED,
  767. * XFS_ILOCK_EXCL,
  768. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  769. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  770. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  771. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  772. */
  773. void
  774. xfs_ilock(xfs_inode_t *ip,
  775. uint lock_flags)
  776. {
  777. /*
  778. * You can't set both SHARED and EXCL for the same lock,
  779. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  780. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  781. */
  782. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  783. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  784. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  785. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  786. ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
  787. if (lock_flags & XFS_IOLOCK_EXCL) {
  788. mrupdate(&ip->i_iolock);
  789. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  790. mraccess(&ip->i_iolock);
  791. }
  792. if (lock_flags & XFS_ILOCK_EXCL) {
  793. mrupdate(&ip->i_lock);
  794. } else if (lock_flags & XFS_ILOCK_SHARED) {
  795. mraccess(&ip->i_lock);
  796. }
  797. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  798. }
  799. /*
  800. * This is just like xfs_ilock(), except that the caller
  801. * is guaranteed not to sleep. It returns 1 if it gets
  802. * the requested locks and 0 otherwise. If the IO lock is
  803. * obtained but the inode lock cannot be, then the IO lock
  804. * is dropped before returning.
  805. *
  806. * ip -- the inode being locked
  807. * lock_flags -- this parameter indicates the inode's locks to be
  808. * to be locked. See the comment for xfs_ilock() for a list
  809. * of valid values.
  810. *
  811. */
  812. int
  813. xfs_ilock_nowait(xfs_inode_t *ip,
  814. uint lock_flags)
  815. {
  816. int iolocked;
  817. int ilocked;
  818. /*
  819. * You can't set both SHARED and EXCL for the same lock,
  820. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  821. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  822. */
  823. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  824. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  825. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  826. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  827. ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
  828. iolocked = 0;
  829. if (lock_flags & XFS_IOLOCK_EXCL) {
  830. iolocked = mrtryupdate(&ip->i_iolock);
  831. if (!iolocked) {
  832. return 0;
  833. }
  834. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  835. iolocked = mrtryaccess(&ip->i_iolock);
  836. if (!iolocked) {
  837. return 0;
  838. }
  839. }
  840. if (lock_flags & XFS_ILOCK_EXCL) {
  841. ilocked = mrtryupdate(&ip->i_lock);
  842. if (!ilocked) {
  843. if (iolocked) {
  844. mrunlock(&ip->i_iolock);
  845. }
  846. return 0;
  847. }
  848. } else if (lock_flags & XFS_ILOCK_SHARED) {
  849. ilocked = mrtryaccess(&ip->i_lock);
  850. if (!ilocked) {
  851. if (iolocked) {
  852. mrunlock(&ip->i_iolock);
  853. }
  854. return 0;
  855. }
  856. }
  857. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  858. return 1;
  859. }
  860. /*
  861. * xfs_iunlock() is used to drop the inode locks acquired with
  862. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  863. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  864. * that we know which locks to drop.
  865. *
  866. * ip -- the inode being unlocked
  867. * lock_flags -- this parameter indicates the inode's locks to be
  868. * to be unlocked. See the comment for xfs_ilock() for a list
  869. * of valid values for this parameter.
  870. *
  871. */
  872. void
  873. xfs_iunlock(xfs_inode_t *ip,
  874. uint lock_flags)
  875. {
  876. /*
  877. * You can't set both SHARED and EXCL for the same lock,
  878. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  879. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  880. */
  881. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  882. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  883. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  884. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  885. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
  886. ASSERT(lock_flags != 0);
  887. if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
  888. ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
  889. (ismrlocked(&ip->i_iolock, MR_ACCESS)));
  890. ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
  891. (ismrlocked(&ip->i_iolock, MR_UPDATE)));
  892. mrunlock(&ip->i_iolock);
  893. }
  894. if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
  895. ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
  896. (ismrlocked(&ip->i_lock, MR_ACCESS)));
  897. ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
  898. (ismrlocked(&ip->i_lock, MR_UPDATE)));
  899. mrunlock(&ip->i_lock);
  900. /*
  901. * Let the AIL know that this item has been unlocked in case
  902. * it is in the AIL and anyone is waiting on it. Don't do
  903. * this if the caller has asked us not to.
  904. */
  905. if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
  906. ip->i_itemp != NULL) {
  907. xfs_trans_unlocked_item(ip->i_mount,
  908. (xfs_log_item_t*)(ip->i_itemp));
  909. }
  910. }
  911. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  912. }
  913. /*
  914. * give up write locks. the i/o lock cannot be held nested
  915. * if it is being demoted.
  916. */
  917. void
  918. xfs_ilock_demote(xfs_inode_t *ip,
  919. uint lock_flags)
  920. {
  921. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  922. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  923. if (lock_flags & XFS_ILOCK_EXCL) {
  924. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  925. mrdemote(&ip->i_lock);
  926. }
  927. if (lock_flags & XFS_IOLOCK_EXCL) {
  928. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  929. mrdemote(&ip->i_iolock);
  930. }
  931. }
  932. /*
  933. * The following three routines simply manage the i_flock
  934. * semaphore embedded in the inode. This semaphore synchronizes
  935. * processes attempting to flush the in-core inode back to disk.
  936. */
  937. void
  938. xfs_iflock(xfs_inode_t *ip)
  939. {
  940. psema(&(ip->i_flock), PINOD|PLTWAIT);
  941. }
  942. int
  943. xfs_iflock_nowait(xfs_inode_t *ip)
  944. {
  945. return (cpsema(&(ip->i_flock)));
  946. }
  947. void
  948. xfs_ifunlock(xfs_inode_t *ip)
  949. {
  950. ASSERT(issemalocked(&(ip->i_flock)));
  951. vsema(&(ip->i_flock));
  952. }