xfs_iget.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_utils.h"
  41. /*
  42. * Initialize the inode hash table for the newly mounted file system.
  43. * Choose an initial table size based on user specified value, else
  44. * use a simple algorithm using the maximum number of inodes as an
  45. * indicator for table size, and clamp it between one and some large
  46. * number of pages.
  47. */
  48. void
  49. xfs_ihash_init(xfs_mount_t *mp)
  50. {
  51. __uint64_t icount;
  52. uint i, flags = KM_SLEEP | KM_MAYFAIL;
  53. if (!mp->m_ihsize) {
  54. icount = mp->m_maxicount ? mp->m_maxicount :
  55. (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog);
  56. mp->m_ihsize = 1 << max_t(uint, 8,
  57. (xfs_highbit64(icount) + 1) / 2);
  58. mp->m_ihsize = min_t(uint, mp->m_ihsize,
  59. (64 * NBPP) / sizeof(xfs_ihash_t));
  60. }
  61. while (!(mp->m_ihash = (xfs_ihash_t *)kmem_zalloc(mp->m_ihsize *
  62. sizeof(xfs_ihash_t), flags))) {
  63. if ((mp->m_ihsize >>= 1) <= NBPP)
  64. flags = KM_SLEEP;
  65. }
  66. for (i = 0; i < mp->m_ihsize; i++) {
  67. rwlock_init(&(mp->m_ihash[i].ih_lock));
  68. }
  69. }
  70. /*
  71. * Free up structures allocated by xfs_ihash_init, at unmount time.
  72. */
  73. void
  74. xfs_ihash_free(xfs_mount_t *mp)
  75. {
  76. kmem_free(mp->m_ihash, mp->m_ihsize*sizeof(xfs_ihash_t));
  77. mp->m_ihash = NULL;
  78. }
  79. /*
  80. * Initialize the inode cluster hash table for the newly mounted file system.
  81. * Its size is derived from the ihash table size.
  82. */
  83. void
  84. xfs_chash_init(xfs_mount_t *mp)
  85. {
  86. uint i;
  87. mp->m_chsize = max_t(uint, 1, mp->m_ihsize /
  88. (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog));
  89. mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize);
  90. mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize
  91. * sizeof(xfs_chash_t),
  92. KM_SLEEP);
  93. for (i = 0; i < mp->m_chsize; i++) {
  94. spinlock_init(&mp->m_chash[i].ch_lock,"xfshash");
  95. }
  96. }
  97. /*
  98. * Free up structures allocated by xfs_chash_init, at unmount time.
  99. */
  100. void
  101. xfs_chash_free(xfs_mount_t *mp)
  102. {
  103. int i;
  104. for (i = 0; i < mp->m_chsize; i++) {
  105. spinlock_destroy(&mp->m_chash[i].ch_lock);
  106. }
  107. kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
  108. mp->m_chash = NULL;
  109. }
  110. /*
  111. * Try to move an inode to the front of its hash list if possible
  112. * (and if its not there already). Called right after obtaining
  113. * the list version number and then dropping the read_lock on the
  114. * hash list in question (which is done right after looking up the
  115. * inode in question...).
  116. */
  117. STATIC void
  118. xfs_ihash_promote(
  119. xfs_ihash_t *ih,
  120. xfs_inode_t *ip,
  121. ulong version)
  122. {
  123. xfs_inode_t *iq;
  124. if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) {
  125. if (likely(version == ih->ih_version)) {
  126. /* remove from list */
  127. if ((iq = ip->i_next)) {
  128. iq->i_prevp = ip->i_prevp;
  129. }
  130. *ip->i_prevp = iq;
  131. /* insert at list head */
  132. iq = ih->ih_next;
  133. iq->i_prevp = &ip->i_next;
  134. ip->i_next = iq;
  135. ip->i_prevp = &ih->ih_next;
  136. ih->ih_next = ip;
  137. }
  138. write_unlock(&ih->ih_lock);
  139. }
  140. }
  141. /*
  142. * Look up an inode by number in the given file system.
  143. * The inode is looked up in the hash table for the file system
  144. * represented by the mount point parameter mp. Each bucket of
  145. * the hash table is guarded by an individual semaphore.
  146. *
  147. * If the inode is found in the hash table, its corresponding vnode
  148. * is obtained with a call to vn_get(). This call takes care of
  149. * coordination with the reclamation of the inode and vnode. Note
  150. * that the vmap structure is filled in while holding the hash lock.
  151. * This gives us the state of the inode/vnode when we found it and
  152. * is used for coordination in vn_get().
  153. *
  154. * If it is not in core, read it in from the file system's device and
  155. * add the inode into the hash table.
  156. *
  157. * The inode is locked according to the value of the lock_flags parameter.
  158. * This flag parameter indicates how and if the inode's IO lock and inode lock
  159. * should be taken.
  160. *
  161. * mp -- the mount point structure for the current file system. It points
  162. * to the inode hash table.
  163. * tp -- a pointer to the current transaction if there is one. This is
  164. * simply passed through to the xfs_iread() call.
  165. * ino -- the number of the inode desired. This is the unique identifier
  166. * within the file system for the inode being requested.
  167. * lock_flags -- flags indicating how to lock the inode. See the comment
  168. * for xfs_ilock() for a list of valid values.
  169. * bno -- the block number starting the buffer containing the inode,
  170. * if known (as by bulkstat), else 0.
  171. */
  172. STATIC int
  173. xfs_iget_core(
  174. bhv_vnode_t *vp,
  175. xfs_mount_t *mp,
  176. xfs_trans_t *tp,
  177. xfs_ino_t ino,
  178. uint flags,
  179. uint lock_flags,
  180. xfs_inode_t **ipp,
  181. xfs_daddr_t bno)
  182. {
  183. xfs_ihash_t *ih;
  184. xfs_inode_t *ip;
  185. xfs_inode_t *iq;
  186. bhv_vnode_t *inode_vp;
  187. ulong version;
  188. int error;
  189. /* REFERENCED */
  190. xfs_chash_t *ch;
  191. xfs_chashlist_t *chl, *chlnew;
  192. SPLDECL(s);
  193. ih = XFS_IHASH(mp, ino);
  194. again:
  195. read_lock(&ih->ih_lock);
  196. for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
  197. if (ip->i_ino == ino) {
  198. /*
  199. * If INEW is set this inode is being set up
  200. * we need to pause and try again.
  201. */
  202. if (ip->i_flags & XFS_INEW) {
  203. read_unlock(&ih->ih_lock);
  204. delay(1);
  205. XFS_STATS_INC(xs_ig_frecycle);
  206. goto again;
  207. }
  208. inode_vp = XFS_ITOV_NULL(ip);
  209. if (inode_vp == NULL) {
  210. /*
  211. * If IRECLAIM is set this inode is
  212. * on its way out of the system,
  213. * we need to pause and try again.
  214. */
  215. if (ip->i_flags & XFS_IRECLAIM) {
  216. read_unlock(&ih->ih_lock);
  217. delay(1);
  218. XFS_STATS_INC(xs_ig_frecycle);
  219. goto again;
  220. }
  221. vn_trace_exit(vp, "xfs_iget.alloc",
  222. (inst_t *)__return_address);
  223. XFS_STATS_INC(xs_ig_found);
  224. ip->i_flags &= ~XFS_IRECLAIMABLE;
  225. version = ih->ih_version;
  226. read_unlock(&ih->ih_lock);
  227. xfs_ihash_promote(ih, ip, version);
  228. XFS_MOUNT_ILOCK(mp);
  229. list_del_init(&ip->i_reclaim);
  230. XFS_MOUNT_IUNLOCK(mp);
  231. goto finish_inode;
  232. } else if (vp != inode_vp) {
  233. struct inode *inode = vn_to_inode(inode_vp);
  234. /* The inode is being torn down, pause and
  235. * try again.
  236. */
  237. if (inode->i_state & (I_FREEING | I_CLEAR)) {
  238. read_unlock(&ih->ih_lock);
  239. delay(1);
  240. XFS_STATS_INC(xs_ig_frecycle);
  241. goto again;
  242. }
  243. /* Chances are the other vnode (the one in the inode) is being torn
  244. * down right now, and we landed on top of it. Question is, what do
  245. * we do? Unhook the old inode and hook up the new one?
  246. */
  247. cmn_err(CE_PANIC,
  248. "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
  249. inode_vp, vp);
  250. }
  251. /*
  252. * Inode cache hit: if ip is not at the front of
  253. * its hash chain, move it there now.
  254. * Do this with the lock held for update, but
  255. * do statistics after releasing the lock.
  256. */
  257. version = ih->ih_version;
  258. read_unlock(&ih->ih_lock);
  259. xfs_ihash_promote(ih, ip, version);
  260. XFS_STATS_INC(xs_ig_found);
  261. finish_inode:
  262. if (ip->i_d.di_mode == 0) {
  263. if (!(flags & XFS_IGET_CREATE))
  264. return ENOENT;
  265. xfs_iocore_inode_reinit(ip);
  266. }
  267. if (lock_flags != 0)
  268. xfs_ilock(ip, lock_flags);
  269. ip->i_flags &= ~XFS_ISTALE;
  270. vn_trace_exit(vp, "xfs_iget.found",
  271. (inst_t *)__return_address);
  272. goto return_ip;
  273. }
  274. }
  275. /*
  276. * Inode cache miss: save the hash chain version stamp and unlock
  277. * the chain, so we don't deadlock in vn_alloc.
  278. */
  279. XFS_STATS_INC(xs_ig_missed);
  280. version = ih->ih_version;
  281. read_unlock(&ih->ih_lock);
  282. /*
  283. * Read the disk inode attributes into a new inode structure and get
  284. * a new vnode for it. This should also initialize i_ino and i_mount.
  285. */
  286. error = xfs_iread(mp, tp, ino, &ip, bno,
  287. (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
  288. if (error)
  289. return error;
  290. vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
  291. xfs_inode_lock_init(ip, vp);
  292. xfs_iocore_inode_init(ip);
  293. if (lock_flags)
  294. xfs_ilock(ip, lock_flags);
  295. if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
  296. xfs_idestroy(ip);
  297. return ENOENT;
  298. }
  299. /*
  300. * Put ip on its hash chain, unless someone else hashed a duplicate
  301. * after we released the hash lock.
  302. */
  303. write_lock(&ih->ih_lock);
  304. if (ih->ih_version != version) {
  305. for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
  306. if (iq->i_ino == ino) {
  307. write_unlock(&ih->ih_lock);
  308. xfs_idestroy(ip);
  309. XFS_STATS_INC(xs_ig_dup);
  310. goto again;
  311. }
  312. }
  313. }
  314. /*
  315. * These values _must_ be set before releasing ihlock!
  316. */
  317. ip->i_hash = ih;
  318. if ((iq = ih->ih_next)) {
  319. iq->i_prevp = &ip->i_next;
  320. }
  321. ip->i_next = iq;
  322. ip->i_prevp = &ih->ih_next;
  323. ih->ih_next = ip;
  324. ip->i_udquot = ip->i_gdquot = NULL;
  325. ih->ih_version++;
  326. ip->i_flags |= XFS_INEW;
  327. write_unlock(&ih->ih_lock);
  328. /*
  329. * put ip on its cluster's hash chain
  330. */
  331. ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
  332. ip->i_cnext == NULL);
  333. chlnew = NULL;
  334. ch = XFS_CHASH(mp, ip->i_blkno);
  335. chlredo:
  336. s = mutex_spinlock(&ch->ch_lock);
  337. for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
  338. if (chl->chl_blkno == ip->i_blkno) {
  339. /* insert this inode into the doubly-linked list
  340. * where chl points */
  341. if ((iq = chl->chl_ip)) {
  342. ip->i_cprev = iq->i_cprev;
  343. iq->i_cprev->i_cnext = ip;
  344. iq->i_cprev = ip;
  345. ip->i_cnext = iq;
  346. } else {
  347. ip->i_cnext = ip;
  348. ip->i_cprev = ip;
  349. }
  350. chl->chl_ip = ip;
  351. ip->i_chash = chl;
  352. break;
  353. }
  354. }
  355. /* no hash list found for this block; add a new hash list */
  356. if (chl == NULL) {
  357. if (chlnew == NULL) {
  358. mutex_spinunlock(&ch->ch_lock, s);
  359. ASSERT(xfs_chashlist_zone != NULL);
  360. chlnew = (xfs_chashlist_t *)
  361. kmem_zone_alloc(xfs_chashlist_zone,
  362. KM_SLEEP);
  363. ASSERT(chlnew != NULL);
  364. goto chlredo;
  365. } else {
  366. ip->i_cnext = ip;
  367. ip->i_cprev = ip;
  368. ip->i_chash = chlnew;
  369. chlnew->chl_ip = ip;
  370. chlnew->chl_blkno = ip->i_blkno;
  371. if (ch->ch_list)
  372. ch->ch_list->chl_prev = chlnew;
  373. chlnew->chl_next = ch->ch_list;
  374. chlnew->chl_prev = NULL;
  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. bhv_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. bhv_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 = vn_from_inode(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 mismatches 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. bhv_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. bhv_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. bhv_vnode_t *vp = XFS_ITOV(ip);
  546. struct inode *inode = vn_to_inode(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. bhv_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. chl = ip->i_chash;
  639. if (chl->chl_prev)
  640. chl->chl_prev->chl_next = chl->chl_next;
  641. else
  642. ch->ch_list = chl->chl_next;
  643. if (chl->chl_next)
  644. chl->chl_next->chl_prev = chl->chl_prev;
  645. kmem_zone_free(xfs_chashlist_zone, chl);
  646. } else {
  647. /* delete one inode from a non-empty list */
  648. iq = ip->i_cnext;
  649. iq->i_cprev = ip->i_cprev;
  650. ip->i_cprev->i_cnext = iq;
  651. if (ip->i_chash->chl_ip == ip) {
  652. ip->i_chash->chl_ip = iq;
  653. }
  654. ip->i_chash = __return_address;
  655. ip->i_cprev = __return_address;
  656. ip->i_cnext = __return_address;
  657. }
  658. mutex_spinunlock(&ch->ch_lock, s);
  659. /*
  660. * Remove from mount's inode list.
  661. */
  662. XFS_MOUNT_ILOCK(mp);
  663. ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
  664. iq = ip->i_mnext;
  665. iq->i_mprev = ip->i_mprev;
  666. ip->i_mprev->i_mnext = iq;
  667. /*
  668. * Fix up the head pointer if it points to the inode being deleted.
  669. */
  670. if (mp->m_inodes == ip) {
  671. if (ip == iq) {
  672. mp->m_inodes = NULL;
  673. } else {
  674. mp->m_inodes = iq;
  675. }
  676. }
  677. /* Deal with the deleted inodes list */
  678. list_del_init(&ip->i_reclaim);
  679. mp->m_ireclaims++;
  680. XFS_MOUNT_IUNLOCK(mp);
  681. }
  682. /*
  683. * This is a wrapper routine around the xfs_ilock() routine
  684. * used to centralize some grungy code. It is used in places
  685. * that wish to lock the inode solely for reading the extents.
  686. * The reason these places can't just call xfs_ilock(SHARED)
  687. * is that the inode lock also guards to bringing in of the
  688. * extents from disk for a file in b-tree format. If the inode
  689. * is in b-tree format, then we need to lock the inode exclusively
  690. * until the extents are read in. Locking it exclusively all
  691. * the time would limit our parallelism unnecessarily, though.
  692. * What we do instead is check to see if the extents have been
  693. * read in yet, and only lock the inode exclusively if they
  694. * have not.
  695. *
  696. * The function returns a value which should be given to the
  697. * corresponding xfs_iunlock_map_shared(). This value is
  698. * the mode in which the lock was actually taken.
  699. */
  700. uint
  701. xfs_ilock_map_shared(
  702. xfs_inode_t *ip)
  703. {
  704. uint lock_mode;
  705. if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
  706. ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
  707. lock_mode = XFS_ILOCK_EXCL;
  708. } else {
  709. lock_mode = XFS_ILOCK_SHARED;
  710. }
  711. xfs_ilock(ip, lock_mode);
  712. return lock_mode;
  713. }
  714. /*
  715. * This is simply the unlock routine to go with xfs_ilock_map_shared().
  716. * All it does is call xfs_iunlock() with the given lock_mode.
  717. */
  718. void
  719. xfs_iunlock_map_shared(
  720. xfs_inode_t *ip,
  721. unsigned int lock_mode)
  722. {
  723. xfs_iunlock(ip, lock_mode);
  724. }
  725. /*
  726. * The xfs inode contains 2 locks: a multi-reader lock called the
  727. * i_iolock and a multi-reader lock called the i_lock. This routine
  728. * allows either or both of the locks to be obtained.
  729. *
  730. * The 2 locks should always be ordered so that the IO lock is
  731. * obtained first in order to prevent deadlock.
  732. *
  733. * ip -- the inode being locked
  734. * lock_flags -- this parameter indicates the inode's locks
  735. * to be locked. It can be:
  736. * XFS_IOLOCK_SHARED,
  737. * XFS_IOLOCK_EXCL,
  738. * XFS_ILOCK_SHARED,
  739. * XFS_ILOCK_EXCL,
  740. * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
  741. * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
  742. * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
  743. * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
  744. */
  745. void
  746. xfs_ilock(xfs_inode_t *ip,
  747. uint lock_flags)
  748. {
  749. /*
  750. * You can't set both SHARED and EXCL for the same lock,
  751. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  752. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  753. */
  754. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  755. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  756. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  757. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  758. ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
  759. if (lock_flags & XFS_IOLOCK_EXCL) {
  760. mrupdate(&ip->i_iolock);
  761. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  762. mraccess(&ip->i_iolock);
  763. }
  764. if (lock_flags & XFS_ILOCK_EXCL) {
  765. mrupdate(&ip->i_lock);
  766. } else if (lock_flags & XFS_ILOCK_SHARED) {
  767. mraccess(&ip->i_lock);
  768. }
  769. xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
  770. }
  771. /*
  772. * This is just like xfs_ilock(), except that the caller
  773. * is guaranteed not to sleep. It returns 1 if it gets
  774. * the requested locks and 0 otherwise. If the IO lock is
  775. * obtained but the inode lock cannot be, then the IO lock
  776. * is dropped before returning.
  777. *
  778. * ip -- the inode being locked
  779. * lock_flags -- this parameter indicates the inode's locks to be
  780. * to be locked. See the comment for xfs_ilock() for a list
  781. * of valid values.
  782. *
  783. */
  784. int
  785. xfs_ilock_nowait(xfs_inode_t *ip,
  786. uint lock_flags)
  787. {
  788. int iolocked;
  789. int ilocked;
  790. /*
  791. * You can't set both SHARED and EXCL for the same lock,
  792. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  793. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  794. */
  795. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  796. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  797. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  798. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  799. ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
  800. iolocked = 0;
  801. if (lock_flags & XFS_IOLOCK_EXCL) {
  802. iolocked = mrtryupdate(&ip->i_iolock);
  803. if (!iolocked) {
  804. return 0;
  805. }
  806. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  807. iolocked = mrtryaccess(&ip->i_iolock);
  808. if (!iolocked) {
  809. return 0;
  810. }
  811. }
  812. if (lock_flags & XFS_ILOCK_EXCL) {
  813. ilocked = mrtryupdate(&ip->i_lock);
  814. if (!ilocked) {
  815. if (iolocked) {
  816. mrunlock(&ip->i_iolock);
  817. }
  818. return 0;
  819. }
  820. } else if (lock_flags & XFS_ILOCK_SHARED) {
  821. ilocked = mrtryaccess(&ip->i_lock);
  822. if (!ilocked) {
  823. if (iolocked) {
  824. mrunlock(&ip->i_iolock);
  825. }
  826. return 0;
  827. }
  828. }
  829. xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
  830. return 1;
  831. }
  832. /*
  833. * xfs_iunlock() is used to drop the inode locks acquired with
  834. * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
  835. * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
  836. * that we know which locks to drop.
  837. *
  838. * ip -- the inode being unlocked
  839. * lock_flags -- this parameter indicates the inode's locks to be
  840. * to be unlocked. See the comment for xfs_ilock() for a list
  841. * of valid values for this parameter.
  842. *
  843. */
  844. void
  845. xfs_iunlock(xfs_inode_t *ip,
  846. uint lock_flags)
  847. {
  848. /*
  849. * You can't set both SHARED and EXCL for the same lock,
  850. * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
  851. * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
  852. */
  853. ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
  854. (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
  855. ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
  856. (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  857. ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
  858. ASSERT(lock_flags != 0);
  859. if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
  860. ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
  861. (ismrlocked(&ip->i_iolock, MR_ACCESS)));
  862. ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
  863. (ismrlocked(&ip->i_iolock, MR_UPDATE)));
  864. mrunlock(&ip->i_iolock);
  865. }
  866. if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
  867. ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
  868. (ismrlocked(&ip->i_lock, MR_ACCESS)));
  869. ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
  870. (ismrlocked(&ip->i_lock, MR_UPDATE)));
  871. mrunlock(&ip->i_lock);
  872. /*
  873. * Let the AIL know that this item has been unlocked in case
  874. * it is in the AIL and anyone is waiting on it. Don't do
  875. * this if the caller has asked us not to.
  876. */
  877. if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
  878. ip->i_itemp != NULL) {
  879. xfs_trans_unlocked_item(ip->i_mount,
  880. (xfs_log_item_t*)(ip->i_itemp));
  881. }
  882. }
  883. xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
  884. }
  885. /*
  886. * give up write locks. the i/o lock cannot be held nested
  887. * if it is being demoted.
  888. */
  889. void
  890. xfs_ilock_demote(xfs_inode_t *ip,
  891. uint lock_flags)
  892. {
  893. ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
  894. ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
  895. if (lock_flags & XFS_ILOCK_EXCL) {
  896. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  897. mrdemote(&ip->i_lock);
  898. }
  899. if (lock_flags & XFS_IOLOCK_EXCL) {
  900. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  901. mrdemote(&ip->i_iolock);
  902. }
  903. }
  904. /*
  905. * The following three routines simply manage the i_flock
  906. * semaphore embedded in the inode. This semaphore synchronizes
  907. * processes attempting to flush the in-core inode back to disk.
  908. */
  909. void
  910. xfs_iflock(xfs_inode_t *ip)
  911. {
  912. psema(&(ip->i_flock), PINOD|PLTWAIT);
  913. }
  914. int
  915. xfs_iflock_nowait(xfs_inode_t *ip)
  916. {
  917. return (cpsema(&(ip->i_flock)));
  918. }
  919. void
  920. xfs_ifunlock(xfs_inode_t *ip)
  921. {
  922. ASSERT(issemalocked(&(ip->i_flock)));
  923. vsema(&(ip->i_flock));
  924. }