xfs_iget.c 27 KB

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