xfs_iget.c 26 KB

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