xfs_iget.c 25 KB

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