xfs_inode_item.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. * Copyright (c) 2000-2002,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_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_trans_priv.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_dinode.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_inode_item.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. kmem_zone_t *xfs_ili_zone; /* inode log item zone */
  34. static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
  35. {
  36. return container_of(lip, struct xfs_inode_log_item, ili_item);
  37. }
  38. /*
  39. * This returns the number of iovecs needed to log the given inode item.
  40. *
  41. * We need one iovec for the inode log format structure, one for the
  42. * inode core, and possibly one for the inode data/extents/b-tree root
  43. * and one for the inode attribute data/extents/b-tree root.
  44. */
  45. STATIC uint
  46. xfs_inode_item_size(
  47. struct xfs_log_item *lip)
  48. {
  49. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  50. struct xfs_inode *ip = iip->ili_inode;
  51. uint nvecs = 2;
  52. switch (ip->i_d.di_format) {
  53. case XFS_DINODE_FMT_EXTENTS:
  54. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  55. ip->i_d.di_nextents > 0 &&
  56. ip->i_df.if_bytes > 0)
  57. nvecs++;
  58. break;
  59. case XFS_DINODE_FMT_BTREE:
  60. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  61. ip->i_df.if_broot_bytes > 0)
  62. nvecs++;
  63. break;
  64. case XFS_DINODE_FMT_LOCAL:
  65. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  66. ip->i_df.if_bytes > 0)
  67. nvecs++;
  68. break;
  69. case XFS_DINODE_FMT_DEV:
  70. case XFS_DINODE_FMT_UUID:
  71. break;
  72. default:
  73. ASSERT(0);
  74. break;
  75. }
  76. if (!XFS_IFORK_Q(ip))
  77. return nvecs;
  78. /*
  79. * Log any necessary attribute data.
  80. */
  81. switch (ip->i_d.di_aformat) {
  82. case XFS_DINODE_FMT_EXTENTS:
  83. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  84. ip->i_d.di_anextents > 0 &&
  85. ip->i_afp->if_bytes > 0)
  86. nvecs++;
  87. break;
  88. case XFS_DINODE_FMT_BTREE:
  89. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  90. ip->i_afp->if_broot_bytes > 0)
  91. nvecs++;
  92. break;
  93. case XFS_DINODE_FMT_LOCAL:
  94. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  95. ip->i_afp->if_bytes > 0)
  96. nvecs++;
  97. break;
  98. default:
  99. ASSERT(0);
  100. break;
  101. }
  102. return nvecs;
  103. }
  104. /*
  105. * xfs_inode_item_format_extents - convert in-core extents to on-disk form
  106. *
  107. * For either the data or attr fork in extent format, we need to endian convert
  108. * the in-core extent as we place them into the on-disk inode. In this case, we
  109. * need to do this conversion before we write the extents into the log. Because
  110. * we don't have the disk inode to write into here, we allocate a buffer and
  111. * format the extents into it via xfs_iextents_copy(). We free the buffer in
  112. * the unlock routine after the copy for the log has been made.
  113. *
  114. * In the case of the data fork, the in-core and on-disk fork sizes can be
  115. * different due to delayed allocation extents. We only log on-disk extents
  116. * here, so always use the physical fork size to determine the size of the
  117. * buffer we need to allocate.
  118. */
  119. STATIC void
  120. xfs_inode_item_format_extents(
  121. struct xfs_inode *ip,
  122. struct xfs_log_iovec *vecp,
  123. int whichfork,
  124. int type)
  125. {
  126. xfs_bmbt_rec_t *ext_buffer;
  127. ext_buffer = kmem_alloc(XFS_IFORK_SIZE(ip, whichfork), KM_SLEEP);
  128. if (whichfork == XFS_DATA_FORK)
  129. ip->i_itemp->ili_extents_buf = ext_buffer;
  130. else
  131. ip->i_itemp->ili_aextents_buf = ext_buffer;
  132. vecp->i_addr = ext_buffer;
  133. vecp->i_len = xfs_iextents_copy(ip, ext_buffer, whichfork);
  134. vecp->i_type = type;
  135. }
  136. /*
  137. * This is called to fill in the vector of log iovecs for the
  138. * given inode log item. It fills the first item with an inode
  139. * log format structure, the second with the on-disk inode structure,
  140. * and a possible third and/or fourth with the inode data/extents/b-tree
  141. * root and inode attributes data/extents/b-tree root.
  142. */
  143. STATIC void
  144. xfs_inode_item_format(
  145. struct xfs_log_item *lip,
  146. struct xfs_log_iovec *vecp)
  147. {
  148. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  149. struct xfs_inode *ip = iip->ili_inode;
  150. uint nvecs;
  151. size_t data_bytes;
  152. xfs_mount_t *mp;
  153. vecp->i_addr = &iip->ili_format;
  154. vecp->i_len = sizeof(xfs_inode_log_format_t);
  155. vecp->i_type = XLOG_REG_TYPE_IFORMAT;
  156. vecp++;
  157. nvecs = 1;
  158. vecp->i_addr = &ip->i_d;
  159. vecp->i_len = sizeof(struct xfs_icdinode);
  160. vecp->i_type = XLOG_REG_TYPE_ICORE;
  161. vecp++;
  162. nvecs++;
  163. /*
  164. * If this is really an old format inode, then we need to
  165. * log it as such. This means that we have to copy the link
  166. * count from the new field to the old. We don't have to worry
  167. * about the new fields, because nothing trusts them as long as
  168. * the old inode version number is there. If the superblock already
  169. * has a new version number, then we don't bother converting back.
  170. */
  171. mp = ip->i_mount;
  172. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  173. if (ip->i_d.di_version == 1) {
  174. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  175. /*
  176. * Convert it back.
  177. */
  178. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  179. ip->i_d.di_onlink = ip->i_d.di_nlink;
  180. } else {
  181. /*
  182. * The superblock version has already been bumped,
  183. * so just make the conversion to the new inode
  184. * format permanent.
  185. */
  186. ip->i_d.di_version = 2;
  187. ip->i_d.di_onlink = 0;
  188. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  189. }
  190. }
  191. switch (ip->i_d.di_format) {
  192. case XFS_DINODE_FMT_EXTENTS:
  193. iip->ili_fields &=
  194. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  195. XFS_ILOG_DEV | XFS_ILOG_UUID);
  196. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  197. ip->i_d.di_nextents > 0 &&
  198. ip->i_df.if_bytes > 0) {
  199. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  200. ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
  201. ASSERT(iip->ili_extents_buf == NULL);
  202. #ifdef XFS_NATIVE_HOST
  203. if (ip->i_d.di_nextents == ip->i_df.if_bytes /
  204. (uint)sizeof(xfs_bmbt_rec_t)) {
  205. /*
  206. * There are no delayed allocation
  207. * extents, so just point to the
  208. * real extents array.
  209. */
  210. vecp->i_addr = ip->i_df.if_u1.if_extents;
  211. vecp->i_len = ip->i_df.if_bytes;
  212. vecp->i_type = XLOG_REG_TYPE_IEXT;
  213. } else
  214. #endif
  215. {
  216. xfs_inode_item_format_extents(ip, vecp,
  217. XFS_DATA_FORK, XLOG_REG_TYPE_IEXT);
  218. }
  219. ASSERT(vecp->i_len <= ip->i_df.if_bytes);
  220. iip->ili_format.ilf_dsize = vecp->i_len;
  221. vecp++;
  222. nvecs++;
  223. } else {
  224. iip->ili_fields &= ~XFS_ILOG_DEXT;
  225. }
  226. break;
  227. case XFS_DINODE_FMT_BTREE:
  228. iip->ili_fields &=
  229. ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  230. XFS_ILOG_DEV | XFS_ILOG_UUID);
  231. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  232. ip->i_df.if_broot_bytes > 0) {
  233. ASSERT(ip->i_df.if_broot != NULL);
  234. vecp->i_addr = ip->i_df.if_broot;
  235. vecp->i_len = ip->i_df.if_broot_bytes;
  236. vecp->i_type = XLOG_REG_TYPE_IBROOT;
  237. vecp++;
  238. nvecs++;
  239. iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
  240. } else {
  241. ASSERT(!(iip->ili_fields &
  242. XFS_ILOG_DBROOT));
  243. #ifdef XFS_TRANS_DEBUG
  244. if (iip->ili_root_size > 0) {
  245. ASSERT(iip->ili_root_size ==
  246. ip->i_df.if_broot_bytes);
  247. ASSERT(memcmp(iip->ili_orig_root,
  248. ip->i_df.if_broot,
  249. iip->ili_root_size) == 0);
  250. } else {
  251. ASSERT(ip->i_df.if_broot_bytes == 0);
  252. }
  253. #endif
  254. iip->ili_fields &= ~XFS_ILOG_DBROOT;
  255. }
  256. break;
  257. case XFS_DINODE_FMT_LOCAL:
  258. iip->ili_fields &=
  259. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
  260. XFS_ILOG_DEV | XFS_ILOG_UUID);
  261. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  262. ip->i_df.if_bytes > 0) {
  263. ASSERT(ip->i_df.if_u1.if_data != NULL);
  264. ASSERT(ip->i_d.di_size > 0);
  265. vecp->i_addr = ip->i_df.if_u1.if_data;
  266. /*
  267. * Round i_bytes up to a word boundary.
  268. * The underlying memory is guaranteed to
  269. * to be there by xfs_idata_realloc().
  270. */
  271. data_bytes = roundup(ip->i_df.if_bytes, 4);
  272. ASSERT((ip->i_df.if_real_bytes == 0) ||
  273. (ip->i_df.if_real_bytes == data_bytes));
  274. vecp->i_len = (int)data_bytes;
  275. vecp->i_type = XLOG_REG_TYPE_ILOCAL;
  276. vecp++;
  277. nvecs++;
  278. iip->ili_format.ilf_dsize = (unsigned)data_bytes;
  279. } else {
  280. iip->ili_fields &= ~XFS_ILOG_DDATA;
  281. }
  282. break;
  283. case XFS_DINODE_FMT_DEV:
  284. iip->ili_fields &=
  285. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  286. XFS_ILOG_DEXT | XFS_ILOG_UUID);
  287. if (iip->ili_fields & XFS_ILOG_DEV) {
  288. iip->ili_format.ilf_u.ilfu_rdev =
  289. ip->i_df.if_u2.if_rdev;
  290. }
  291. break;
  292. case XFS_DINODE_FMT_UUID:
  293. iip->ili_fields &=
  294. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  295. XFS_ILOG_DEXT | XFS_ILOG_DEV);
  296. if (iip->ili_fields & XFS_ILOG_UUID) {
  297. iip->ili_format.ilf_u.ilfu_uuid =
  298. ip->i_df.if_u2.if_uuid;
  299. }
  300. break;
  301. default:
  302. ASSERT(0);
  303. break;
  304. }
  305. /*
  306. * If there are no attributes associated with the file, then we're done.
  307. */
  308. if (!XFS_IFORK_Q(ip)) {
  309. iip->ili_fields &=
  310. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  311. goto out;
  312. }
  313. switch (ip->i_d.di_aformat) {
  314. case XFS_DINODE_FMT_EXTENTS:
  315. iip->ili_fields &=
  316. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  317. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  318. ip->i_d.di_anextents > 0 &&
  319. ip->i_afp->if_bytes > 0) {
  320. ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
  321. ip->i_d.di_anextents);
  322. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  323. #ifdef XFS_NATIVE_HOST
  324. /*
  325. * There are not delayed allocation extents
  326. * for attributes, so just point at the array.
  327. */
  328. vecp->i_addr = ip->i_afp->if_u1.if_extents;
  329. vecp->i_len = ip->i_afp->if_bytes;
  330. vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
  331. #else
  332. ASSERT(iip->ili_aextents_buf == NULL);
  333. xfs_inode_item_format_extents(ip, vecp,
  334. XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
  335. #endif
  336. iip->ili_format.ilf_asize = vecp->i_len;
  337. vecp++;
  338. nvecs++;
  339. } else {
  340. iip->ili_fields &= ~XFS_ILOG_AEXT;
  341. }
  342. break;
  343. case XFS_DINODE_FMT_BTREE:
  344. iip->ili_fields &=
  345. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  346. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  347. ip->i_afp->if_broot_bytes > 0) {
  348. ASSERT(ip->i_afp->if_broot != NULL);
  349. vecp->i_addr = ip->i_afp->if_broot;
  350. vecp->i_len = ip->i_afp->if_broot_bytes;
  351. vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
  352. vecp++;
  353. nvecs++;
  354. iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
  355. } else {
  356. iip->ili_fields &= ~XFS_ILOG_ABROOT;
  357. }
  358. break;
  359. case XFS_DINODE_FMT_LOCAL:
  360. iip->ili_fields &=
  361. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  362. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  363. ip->i_afp->if_bytes > 0) {
  364. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  365. vecp->i_addr = ip->i_afp->if_u1.if_data;
  366. /*
  367. * Round i_bytes up to a word boundary.
  368. * The underlying memory is guaranteed to
  369. * to be there by xfs_idata_realloc().
  370. */
  371. data_bytes = roundup(ip->i_afp->if_bytes, 4);
  372. ASSERT((ip->i_afp->if_real_bytes == 0) ||
  373. (ip->i_afp->if_real_bytes == data_bytes));
  374. vecp->i_len = (int)data_bytes;
  375. vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
  376. vecp++;
  377. nvecs++;
  378. iip->ili_format.ilf_asize = (unsigned)data_bytes;
  379. } else {
  380. iip->ili_fields &= ~XFS_ILOG_ADATA;
  381. }
  382. break;
  383. default:
  384. ASSERT(0);
  385. break;
  386. }
  387. out:
  388. /*
  389. * Now update the log format that goes out to disk from the in-core
  390. * values. We always write the inode core to make the arithmetic
  391. * games in recovery easier, which isn't a big deal as just about any
  392. * transaction would dirty it anyway.
  393. */
  394. iip->ili_format.ilf_fields = XFS_ILOG_CORE |
  395. (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
  396. iip->ili_format.ilf_size = nvecs;
  397. }
  398. /*
  399. * This is called to pin the inode associated with the inode log
  400. * item in memory so it cannot be written out.
  401. */
  402. STATIC void
  403. xfs_inode_item_pin(
  404. struct xfs_log_item *lip)
  405. {
  406. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  407. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  408. trace_xfs_inode_pin(ip, _RET_IP_);
  409. atomic_inc(&ip->i_pincount);
  410. }
  411. /*
  412. * This is called to unpin the inode associated with the inode log
  413. * item which was previously pinned with a call to xfs_inode_item_pin().
  414. *
  415. * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
  416. */
  417. STATIC void
  418. xfs_inode_item_unpin(
  419. struct xfs_log_item *lip,
  420. int remove)
  421. {
  422. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  423. trace_xfs_inode_unpin(ip, _RET_IP_);
  424. ASSERT(atomic_read(&ip->i_pincount) > 0);
  425. if (atomic_dec_and_test(&ip->i_pincount))
  426. wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
  427. }
  428. STATIC uint
  429. xfs_inode_item_push(
  430. struct xfs_log_item *lip,
  431. struct list_head *buffer_list)
  432. {
  433. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  434. struct xfs_inode *ip = iip->ili_inode;
  435. struct xfs_buf *bp = NULL;
  436. uint rval = XFS_ITEM_SUCCESS;
  437. int error;
  438. if (xfs_ipincount(ip) > 0)
  439. return XFS_ITEM_PINNED;
  440. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
  441. return XFS_ITEM_LOCKED;
  442. /*
  443. * Re-check the pincount now that we stabilized the value by
  444. * taking the ilock.
  445. */
  446. if (xfs_ipincount(ip) > 0) {
  447. rval = XFS_ITEM_PINNED;
  448. goto out_unlock;
  449. }
  450. /*
  451. * Stale inode items should force out the iclog.
  452. */
  453. if (ip->i_flags & XFS_ISTALE) {
  454. rval = XFS_ITEM_PINNED;
  455. goto out_unlock;
  456. }
  457. /*
  458. * Someone else is already flushing the inode. Nothing we can do
  459. * here but wait for the flush to finish and remove the item from
  460. * the AIL.
  461. */
  462. if (!xfs_iflock_nowait(ip)) {
  463. rval = XFS_ITEM_FLUSHING;
  464. goto out_unlock;
  465. }
  466. ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  467. ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  468. spin_unlock(&lip->li_ailp->xa_lock);
  469. error = xfs_iflush(ip, &bp);
  470. if (!error) {
  471. if (!xfs_buf_delwri_queue(bp, buffer_list))
  472. rval = XFS_ITEM_FLUSHING;
  473. xfs_buf_relse(bp);
  474. }
  475. spin_lock(&lip->li_ailp->xa_lock);
  476. out_unlock:
  477. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  478. return rval;
  479. }
  480. /*
  481. * Unlock the inode associated with the inode log item.
  482. * Clear the fields of the inode and inode log item that
  483. * are specific to the current transaction. If the
  484. * hold flags is set, do not unlock the inode.
  485. */
  486. STATIC void
  487. xfs_inode_item_unlock(
  488. struct xfs_log_item *lip)
  489. {
  490. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  491. struct xfs_inode *ip = iip->ili_inode;
  492. unsigned short lock_flags;
  493. ASSERT(ip->i_itemp != NULL);
  494. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  495. /*
  496. * If the inode needed a separate buffer with which to log
  497. * its extents, then free it now.
  498. */
  499. if (iip->ili_extents_buf != NULL) {
  500. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
  501. ASSERT(ip->i_d.di_nextents > 0);
  502. ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
  503. ASSERT(ip->i_df.if_bytes > 0);
  504. kmem_free(iip->ili_extents_buf);
  505. iip->ili_extents_buf = NULL;
  506. }
  507. if (iip->ili_aextents_buf != NULL) {
  508. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
  509. ASSERT(ip->i_d.di_anextents > 0);
  510. ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
  511. ASSERT(ip->i_afp->if_bytes > 0);
  512. kmem_free(iip->ili_aextents_buf);
  513. iip->ili_aextents_buf = NULL;
  514. }
  515. lock_flags = iip->ili_lock_flags;
  516. iip->ili_lock_flags = 0;
  517. if (lock_flags)
  518. xfs_iunlock(ip, lock_flags);
  519. }
  520. /*
  521. * This is called to find out where the oldest active copy of the inode log
  522. * item in the on disk log resides now that the last log write of it completed
  523. * at the given lsn. Since we always re-log all dirty data in an inode, the
  524. * latest copy in the on disk log is the only one that matters. Therefore,
  525. * simply return the given lsn.
  526. *
  527. * If the inode has been marked stale because the cluster is being freed, we
  528. * don't want to (re-)insert this inode into the AIL. There is a race condition
  529. * where the cluster buffer may be unpinned before the inode is inserted into
  530. * the AIL during transaction committed processing. If the buffer is unpinned
  531. * before the inode item has been committed and inserted, then it is possible
  532. * for the buffer to be written and IO completes before the inode is inserted
  533. * into the AIL. In that case, we'd be inserting a clean, stale inode into the
  534. * AIL which will never get removed. It will, however, get reclaimed which
  535. * triggers an assert in xfs_inode_free() complaining about freein an inode
  536. * still in the AIL.
  537. *
  538. * To avoid this, just unpin the inode directly and return a LSN of -1 so the
  539. * transaction committed code knows that it does not need to do any further
  540. * processing on the item.
  541. */
  542. STATIC xfs_lsn_t
  543. xfs_inode_item_committed(
  544. struct xfs_log_item *lip,
  545. xfs_lsn_t lsn)
  546. {
  547. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  548. struct xfs_inode *ip = iip->ili_inode;
  549. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  550. xfs_inode_item_unpin(lip, 0);
  551. return -1;
  552. }
  553. return lsn;
  554. }
  555. /*
  556. * XXX rcc - this one really has to do something. Probably needs
  557. * to stamp in a new field in the incore inode.
  558. */
  559. STATIC void
  560. xfs_inode_item_committing(
  561. struct xfs_log_item *lip,
  562. xfs_lsn_t lsn)
  563. {
  564. INODE_ITEM(lip)->ili_last_lsn = lsn;
  565. }
  566. /*
  567. * This is the ops vector shared by all buf log items.
  568. */
  569. static const struct xfs_item_ops xfs_inode_item_ops = {
  570. .iop_size = xfs_inode_item_size,
  571. .iop_format = xfs_inode_item_format,
  572. .iop_pin = xfs_inode_item_pin,
  573. .iop_unpin = xfs_inode_item_unpin,
  574. .iop_unlock = xfs_inode_item_unlock,
  575. .iop_committed = xfs_inode_item_committed,
  576. .iop_push = xfs_inode_item_push,
  577. .iop_committing = xfs_inode_item_committing
  578. };
  579. /*
  580. * Initialize the inode log item for a newly allocated (in-core) inode.
  581. */
  582. void
  583. xfs_inode_item_init(
  584. struct xfs_inode *ip,
  585. struct xfs_mount *mp)
  586. {
  587. struct xfs_inode_log_item *iip;
  588. ASSERT(ip->i_itemp == NULL);
  589. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  590. iip->ili_inode = ip;
  591. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  592. &xfs_inode_item_ops);
  593. iip->ili_format.ilf_type = XFS_LI_INODE;
  594. iip->ili_format.ilf_ino = ip->i_ino;
  595. iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
  596. iip->ili_format.ilf_len = ip->i_imap.im_len;
  597. iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
  598. }
  599. /*
  600. * Free the inode log item and any memory hanging off of it.
  601. */
  602. void
  603. xfs_inode_item_destroy(
  604. xfs_inode_t *ip)
  605. {
  606. #ifdef XFS_TRANS_DEBUG
  607. if (ip->i_itemp->ili_root_size != 0) {
  608. kmem_free(ip->i_itemp->ili_orig_root);
  609. }
  610. #endif
  611. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  612. }
  613. /*
  614. * This is the inode flushing I/O completion routine. It is called
  615. * from interrupt level when the buffer containing the inode is
  616. * flushed to disk. It is responsible for removing the inode item
  617. * from the AIL if it has not been re-logged, and unlocking the inode's
  618. * flush lock.
  619. *
  620. * To reduce AIL lock traffic as much as possible, we scan the buffer log item
  621. * list for other inodes that will run this function. We remove them from the
  622. * buffer list so we can process all the inode IO completions in one AIL lock
  623. * traversal.
  624. */
  625. void
  626. xfs_iflush_done(
  627. struct xfs_buf *bp,
  628. struct xfs_log_item *lip)
  629. {
  630. struct xfs_inode_log_item *iip;
  631. struct xfs_log_item *blip;
  632. struct xfs_log_item *next;
  633. struct xfs_log_item *prev;
  634. struct xfs_ail *ailp = lip->li_ailp;
  635. int need_ail = 0;
  636. /*
  637. * Scan the buffer IO completions for other inodes being completed and
  638. * attach them to the current inode log item.
  639. */
  640. blip = bp->b_fspriv;
  641. prev = NULL;
  642. while (blip != NULL) {
  643. if (lip->li_cb != xfs_iflush_done) {
  644. prev = blip;
  645. blip = blip->li_bio_list;
  646. continue;
  647. }
  648. /* remove from list */
  649. next = blip->li_bio_list;
  650. if (!prev) {
  651. bp->b_fspriv = next;
  652. } else {
  653. prev->li_bio_list = next;
  654. }
  655. /* add to current list */
  656. blip->li_bio_list = lip->li_bio_list;
  657. lip->li_bio_list = blip;
  658. /*
  659. * while we have the item, do the unlocked check for needing
  660. * the AIL lock.
  661. */
  662. iip = INODE_ITEM(blip);
  663. if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
  664. need_ail++;
  665. blip = next;
  666. }
  667. /* make sure we capture the state of the initial inode. */
  668. iip = INODE_ITEM(lip);
  669. if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
  670. need_ail++;
  671. /*
  672. * We only want to pull the item from the AIL if it is
  673. * actually there and its location in the log has not
  674. * changed since we started the flush. Thus, we only bother
  675. * if the ili_logged flag is set and the inode's lsn has not
  676. * changed. First we check the lsn outside
  677. * the lock since it's cheaper, and then we recheck while
  678. * holding the lock before removing the inode from the AIL.
  679. */
  680. if (need_ail) {
  681. struct xfs_log_item *log_items[need_ail];
  682. int i = 0;
  683. spin_lock(&ailp->xa_lock);
  684. for (blip = lip; blip; blip = blip->li_bio_list) {
  685. iip = INODE_ITEM(blip);
  686. if (iip->ili_logged &&
  687. blip->li_lsn == iip->ili_flush_lsn) {
  688. log_items[i++] = blip;
  689. }
  690. ASSERT(i <= need_ail);
  691. }
  692. /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
  693. xfs_trans_ail_delete_bulk(ailp, log_items, i,
  694. SHUTDOWN_CORRUPT_INCORE);
  695. }
  696. /*
  697. * clean up and unlock the flush lock now we are done. We can clear the
  698. * ili_last_fields bits now that we know that the data corresponding to
  699. * them is safely on disk.
  700. */
  701. for (blip = lip; blip; blip = next) {
  702. next = blip->li_bio_list;
  703. blip->li_bio_list = NULL;
  704. iip = INODE_ITEM(blip);
  705. iip->ili_logged = 0;
  706. iip->ili_last_fields = 0;
  707. xfs_ifunlock(iip->ili_inode);
  708. }
  709. }
  710. /*
  711. * This is the inode flushing abort routine. It is called from xfs_iflush when
  712. * the filesystem is shutting down to clean up the inode state. It is
  713. * responsible for removing the inode item from the AIL if it has not been
  714. * re-logged, and unlocking the inode's flush lock.
  715. */
  716. void
  717. xfs_iflush_abort(
  718. xfs_inode_t *ip,
  719. bool stale)
  720. {
  721. xfs_inode_log_item_t *iip = ip->i_itemp;
  722. if (iip) {
  723. struct xfs_ail *ailp = iip->ili_item.li_ailp;
  724. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  725. spin_lock(&ailp->xa_lock);
  726. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  727. /* xfs_trans_ail_delete() drops the AIL lock. */
  728. xfs_trans_ail_delete(ailp, &iip->ili_item,
  729. stale ?
  730. SHUTDOWN_LOG_IO_ERROR :
  731. SHUTDOWN_CORRUPT_INCORE);
  732. } else
  733. spin_unlock(&ailp->xa_lock);
  734. }
  735. iip->ili_logged = 0;
  736. /*
  737. * Clear the ili_last_fields bits now that we know that the
  738. * data corresponding to them is safely on disk.
  739. */
  740. iip->ili_last_fields = 0;
  741. /*
  742. * Clear the inode logging fields so no more flushes are
  743. * attempted.
  744. */
  745. iip->ili_fields = 0;
  746. }
  747. /*
  748. * Release the inode's flush lock since we're done with it.
  749. */
  750. xfs_ifunlock(ip);
  751. }
  752. void
  753. xfs_istale_done(
  754. struct xfs_buf *bp,
  755. struct xfs_log_item *lip)
  756. {
  757. xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
  758. }
  759. /*
  760. * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
  761. * (which can have different field alignments) to the native version
  762. */
  763. int
  764. xfs_inode_item_format_convert(
  765. xfs_log_iovec_t *buf,
  766. xfs_inode_log_format_t *in_f)
  767. {
  768. if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
  769. xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
  770. in_f->ilf_type = in_f32->ilf_type;
  771. in_f->ilf_size = in_f32->ilf_size;
  772. in_f->ilf_fields = in_f32->ilf_fields;
  773. in_f->ilf_asize = in_f32->ilf_asize;
  774. in_f->ilf_dsize = in_f32->ilf_dsize;
  775. in_f->ilf_ino = in_f32->ilf_ino;
  776. /* copy biggest field of ilf_u */
  777. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  778. in_f32->ilf_u.ilfu_uuid.__u_bits,
  779. sizeof(uuid_t));
  780. in_f->ilf_blkno = in_f32->ilf_blkno;
  781. in_f->ilf_len = in_f32->ilf_len;
  782. in_f->ilf_boffset = in_f32->ilf_boffset;
  783. return 0;
  784. } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
  785. xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
  786. in_f->ilf_type = in_f64->ilf_type;
  787. in_f->ilf_size = in_f64->ilf_size;
  788. in_f->ilf_fields = in_f64->ilf_fields;
  789. in_f->ilf_asize = in_f64->ilf_asize;
  790. in_f->ilf_dsize = in_f64->ilf_dsize;
  791. in_f->ilf_ino = in_f64->ilf_ino;
  792. /* copy biggest field of ilf_u */
  793. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  794. in_f64->ilf_u.ilfu_uuid.__u_bits,
  795. sizeof(uuid_t));
  796. in_f->ilf_blkno = in_f64->ilf_blkno;
  797. in_f->ilf_len = in_f64->ilf_len;
  798. in_f->ilf_boffset = in_f64->ilf_boffset;
  799. return 0;
  800. }
  801. return EFSCORRUPTED;
  802. }