xfs_iget.c 27 KB

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