xfs_inode_item.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. iip->ili_fields &= ~XFS_ILOG_DBROOT;
  244. }
  245. break;
  246. case XFS_DINODE_FMT_LOCAL:
  247. iip->ili_fields &=
  248. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
  249. XFS_ILOG_DEV | XFS_ILOG_UUID);
  250. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  251. ip->i_df.if_bytes > 0) {
  252. ASSERT(ip->i_df.if_u1.if_data != NULL);
  253. ASSERT(ip->i_d.di_size > 0);
  254. vecp->i_addr = ip->i_df.if_u1.if_data;
  255. /*
  256. * Round i_bytes up to a word boundary.
  257. * The underlying memory is guaranteed to
  258. * to be there by xfs_idata_realloc().
  259. */
  260. data_bytes = roundup(ip->i_df.if_bytes, 4);
  261. ASSERT((ip->i_df.if_real_bytes == 0) ||
  262. (ip->i_df.if_real_bytes == data_bytes));
  263. vecp->i_len = (int)data_bytes;
  264. vecp->i_type = XLOG_REG_TYPE_ILOCAL;
  265. vecp++;
  266. nvecs++;
  267. iip->ili_format.ilf_dsize = (unsigned)data_bytes;
  268. } else {
  269. iip->ili_fields &= ~XFS_ILOG_DDATA;
  270. }
  271. break;
  272. case XFS_DINODE_FMT_DEV:
  273. iip->ili_fields &=
  274. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  275. XFS_ILOG_DEXT | XFS_ILOG_UUID);
  276. if (iip->ili_fields & XFS_ILOG_DEV) {
  277. iip->ili_format.ilf_u.ilfu_rdev =
  278. ip->i_df.if_u2.if_rdev;
  279. }
  280. break;
  281. case XFS_DINODE_FMT_UUID:
  282. iip->ili_fields &=
  283. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  284. XFS_ILOG_DEXT | XFS_ILOG_DEV);
  285. if (iip->ili_fields & XFS_ILOG_UUID) {
  286. iip->ili_format.ilf_u.ilfu_uuid =
  287. ip->i_df.if_u2.if_uuid;
  288. }
  289. break;
  290. default:
  291. ASSERT(0);
  292. break;
  293. }
  294. /*
  295. * If there are no attributes associated with the file, then we're done.
  296. */
  297. if (!XFS_IFORK_Q(ip)) {
  298. iip->ili_fields &=
  299. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  300. goto out;
  301. }
  302. switch (ip->i_d.di_aformat) {
  303. case XFS_DINODE_FMT_EXTENTS:
  304. iip->ili_fields &=
  305. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  306. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  307. ip->i_d.di_anextents > 0 &&
  308. ip->i_afp->if_bytes > 0) {
  309. ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
  310. ip->i_d.di_anextents);
  311. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  312. #ifdef XFS_NATIVE_HOST
  313. /*
  314. * There are not delayed allocation extents
  315. * for attributes, so just point at the array.
  316. */
  317. vecp->i_addr = ip->i_afp->if_u1.if_extents;
  318. vecp->i_len = ip->i_afp->if_bytes;
  319. vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
  320. #else
  321. ASSERT(iip->ili_aextents_buf == NULL);
  322. xfs_inode_item_format_extents(ip, vecp,
  323. XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
  324. #endif
  325. iip->ili_format.ilf_asize = vecp->i_len;
  326. vecp++;
  327. nvecs++;
  328. } else {
  329. iip->ili_fields &= ~XFS_ILOG_AEXT;
  330. }
  331. break;
  332. case XFS_DINODE_FMT_BTREE:
  333. iip->ili_fields &=
  334. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  335. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  336. ip->i_afp->if_broot_bytes > 0) {
  337. ASSERT(ip->i_afp->if_broot != NULL);
  338. vecp->i_addr = ip->i_afp->if_broot;
  339. vecp->i_len = ip->i_afp->if_broot_bytes;
  340. vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
  341. vecp++;
  342. nvecs++;
  343. iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
  344. } else {
  345. iip->ili_fields &= ~XFS_ILOG_ABROOT;
  346. }
  347. break;
  348. case XFS_DINODE_FMT_LOCAL:
  349. iip->ili_fields &=
  350. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  351. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  352. ip->i_afp->if_bytes > 0) {
  353. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  354. vecp->i_addr = ip->i_afp->if_u1.if_data;
  355. /*
  356. * Round i_bytes up to a word boundary.
  357. * The underlying memory is guaranteed to
  358. * to be there by xfs_idata_realloc().
  359. */
  360. data_bytes = roundup(ip->i_afp->if_bytes, 4);
  361. ASSERT((ip->i_afp->if_real_bytes == 0) ||
  362. (ip->i_afp->if_real_bytes == data_bytes));
  363. vecp->i_len = (int)data_bytes;
  364. vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
  365. vecp++;
  366. nvecs++;
  367. iip->ili_format.ilf_asize = (unsigned)data_bytes;
  368. } else {
  369. iip->ili_fields &= ~XFS_ILOG_ADATA;
  370. }
  371. break;
  372. default:
  373. ASSERT(0);
  374. break;
  375. }
  376. out:
  377. /*
  378. * Now update the log format that goes out to disk from the in-core
  379. * values. We always write the inode core to make the arithmetic
  380. * games in recovery easier, which isn't a big deal as just about any
  381. * transaction would dirty it anyway.
  382. */
  383. iip->ili_format.ilf_fields = XFS_ILOG_CORE |
  384. (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
  385. iip->ili_format.ilf_size = nvecs;
  386. }
  387. /*
  388. * This is called to pin the inode associated with the inode log
  389. * item in memory so it cannot be written out.
  390. */
  391. STATIC void
  392. xfs_inode_item_pin(
  393. struct xfs_log_item *lip)
  394. {
  395. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  396. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  397. trace_xfs_inode_pin(ip, _RET_IP_);
  398. atomic_inc(&ip->i_pincount);
  399. }
  400. /*
  401. * This is called to unpin the inode associated with the inode log
  402. * item which was previously pinned with a call to xfs_inode_item_pin().
  403. *
  404. * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
  405. */
  406. STATIC void
  407. xfs_inode_item_unpin(
  408. struct xfs_log_item *lip,
  409. int remove)
  410. {
  411. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  412. trace_xfs_inode_unpin(ip, _RET_IP_);
  413. ASSERT(atomic_read(&ip->i_pincount) > 0);
  414. if (atomic_dec_and_test(&ip->i_pincount))
  415. wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
  416. }
  417. STATIC uint
  418. xfs_inode_item_push(
  419. struct xfs_log_item *lip,
  420. struct list_head *buffer_list)
  421. {
  422. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  423. struct xfs_inode *ip = iip->ili_inode;
  424. struct xfs_buf *bp = NULL;
  425. uint rval = XFS_ITEM_SUCCESS;
  426. int error;
  427. if (xfs_ipincount(ip) > 0)
  428. return XFS_ITEM_PINNED;
  429. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
  430. return XFS_ITEM_LOCKED;
  431. /*
  432. * Re-check the pincount now that we stabilized the value by
  433. * taking the ilock.
  434. */
  435. if (xfs_ipincount(ip) > 0) {
  436. rval = XFS_ITEM_PINNED;
  437. goto out_unlock;
  438. }
  439. /*
  440. * Stale inode items should force out the iclog.
  441. */
  442. if (ip->i_flags & XFS_ISTALE) {
  443. rval = XFS_ITEM_PINNED;
  444. goto out_unlock;
  445. }
  446. /*
  447. * Someone else is already flushing the inode. Nothing we can do
  448. * here but wait for the flush to finish and remove the item from
  449. * the AIL.
  450. */
  451. if (!xfs_iflock_nowait(ip)) {
  452. rval = XFS_ITEM_FLUSHING;
  453. goto out_unlock;
  454. }
  455. ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  456. ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  457. spin_unlock(&lip->li_ailp->xa_lock);
  458. error = xfs_iflush(ip, &bp);
  459. if (!error) {
  460. if (!xfs_buf_delwri_queue(bp, buffer_list))
  461. rval = XFS_ITEM_FLUSHING;
  462. xfs_buf_relse(bp);
  463. }
  464. spin_lock(&lip->li_ailp->xa_lock);
  465. out_unlock:
  466. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  467. return rval;
  468. }
  469. /*
  470. * Unlock the inode associated with the inode log item.
  471. * Clear the fields of the inode and inode log item that
  472. * are specific to the current transaction. If the
  473. * hold flags is set, do not unlock the inode.
  474. */
  475. STATIC void
  476. xfs_inode_item_unlock(
  477. struct xfs_log_item *lip)
  478. {
  479. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  480. struct xfs_inode *ip = iip->ili_inode;
  481. unsigned short lock_flags;
  482. ASSERT(ip->i_itemp != NULL);
  483. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  484. /*
  485. * If the inode needed a separate buffer with which to log
  486. * its extents, then free it now.
  487. */
  488. if (iip->ili_extents_buf != NULL) {
  489. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
  490. ASSERT(ip->i_d.di_nextents > 0);
  491. ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
  492. ASSERT(ip->i_df.if_bytes > 0);
  493. kmem_free(iip->ili_extents_buf);
  494. iip->ili_extents_buf = NULL;
  495. }
  496. if (iip->ili_aextents_buf != NULL) {
  497. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
  498. ASSERT(ip->i_d.di_anextents > 0);
  499. ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
  500. ASSERT(ip->i_afp->if_bytes > 0);
  501. kmem_free(iip->ili_aextents_buf);
  502. iip->ili_aextents_buf = NULL;
  503. }
  504. lock_flags = iip->ili_lock_flags;
  505. iip->ili_lock_flags = 0;
  506. if (lock_flags)
  507. xfs_iunlock(ip, lock_flags);
  508. }
  509. /*
  510. * This is called to find out where the oldest active copy of the inode log
  511. * item in the on disk log resides now that the last log write of it completed
  512. * at the given lsn. Since we always re-log all dirty data in an inode, the
  513. * latest copy in the on disk log is the only one that matters. Therefore,
  514. * simply return the given lsn.
  515. *
  516. * If the inode has been marked stale because the cluster is being freed, we
  517. * don't want to (re-)insert this inode into the AIL. There is a race condition
  518. * where the cluster buffer may be unpinned before the inode is inserted into
  519. * the AIL during transaction committed processing. If the buffer is unpinned
  520. * before the inode item has been committed and inserted, then it is possible
  521. * for the buffer to be written and IO completes before the inode is inserted
  522. * into the AIL. In that case, we'd be inserting a clean, stale inode into the
  523. * AIL which will never get removed. It will, however, get reclaimed which
  524. * triggers an assert in xfs_inode_free() complaining about freein an inode
  525. * still in the AIL.
  526. *
  527. * To avoid this, just unpin the inode directly and return a LSN of -1 so the
  528. * transaction committed code knows that it does not need to do any further
  529. * processing on the item.
  530. */
  531. STATIC xfs_lsn_t
  532. xfs_inode_item_committed(
  533. struct xfs_log_item *lip,
  534. xfs_lsn_t lsn)
  535. {
  536. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  537. struct xfs_inode *ip = iip->ili_inode;
  538. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  539. xfs_inode_item_unpin(lip, 0);
  540. return -1;
  541. }
  542. return lsn;
  543. }
  544. /*
  545. * XXX rcc - this one really has to do something. Probably needs
  546. * to stamp in a new field in the incore inode.
  547. */
  548. STATIC void
  549. xfs_inode_item_committing(
  550. struct xfs_log_item *lip,
  551. xfs_lsn_t lsn)
  552. {
  553. INODE_ITEM(lip)->ili_last_lsn = lsn;
  554. }
  555. /*
  556. * This is the ops vector shared by all buf log items.
  557. */
  558. static const struct xfs_item_ops xfs_inode_item_ops = {
  559. .iop_size = xfs_inode_item_size,
  560. .iop_format = xfs_inode_item_format,
  561. .iop_pin = xfs_inode_item_pin,
  562. .iop_unpin = xfs_inode_item_unpin,
  563. .iop_unlock = xfs_inode_item_unlock,
  564. .iop_committed = xfs_inode_item_committed,
  565. .iop_push = xfs_inode_item_push,
  566. .iop_committing = xfs_inode_item_committing
  567. };
  568. /*
  569. * Initialize the inode log item for a newly allocated (in-core) inode.
  570. */
  571. void
  572. xfs_inode_item_init(
  573. struct xfs_inode *ip,
  574. struct xfs_mount *mp)
  575. {
  576. struct xfs_inode_log_item *iip;
  577. ASSERT(ip->i_itemp == NULL);
  578. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  579. iip->ili_inode = ip;
  580. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  581. &xfs_inode_item_ops);
  582. iip->ili_format.ilf_type = XFS_LI_INODE;
  583. iip->ili_format.ilf_ino = ip->i_ino;
  584. iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
  585. iip->ili_format.ilf_len = ip->i_imap.im_len;
  586. iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
  587. }
  588. /*
  589. * Free the inode log item and any memory hanging off of it.
  590. */
  591. void
  592. xfs_inode_item_destroy(
  593. xfs_inode_t *ip)
  594. {
  595. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  596. }
  597. /*
  598. * This is the inode flushing I/O completion routine. It is called
  599. * from interrupt level when the buffer containing the inode is
  600. * flushed to disk. It is responsible for removing the inode item
  601. * from the AIL if it has not been re-logged, and unlocking the inode's
  602. * flush lock.
  603. *
  604. * To reduce AIL lock traffic as much as possible, we scan the buffer log item
  605. * list for other inodes that will run this function. We remove them from the
  606. * buffer list so we can process all the inode IO completions in one AIL lock
  607. * traversal.
  608. */
  609. void
  610. xfs_iflush_done(
  611. struct xfs_buf *bp,
  612. struct xfs_log_item *lip)
  613. {
  614. struct xfs_inode_log_item *iip;
  615. struct xfs_log_item *blip;
  616. struct xfs_log_item *next;
  617. struct xfs_log_item *prev;
  618. struct xfs_ail *ailp = lip->li_ailp;
  619. int need_ail = 0;
  620. /*
  621. * Scan the buffer IO completions for other inodes being completed and
  622. * attach them to the current inode log item.
  623. */
  624. blip = bp->b_fspriv;
  625. prev = NULL;
  626. while (blip != NULL) {
  627. if (lip->li_cb != xfs_iflush_done) {
  628. prev = blip;
  629. blip = blip->li_bio_list;
  630. continue;
  631. }
  632. /* remove from list */
  633. next = blip->li_bio_list;
  634. if (!prev) {
  635. bp->b_fspriv = next;
  636. } else {
  637. prev->li_bio_list = next;
  638. }
  639. /* add to current list */
  640. blip->li_bio_list = lip->li_bio_list;
  641. lip->li_bio_list = blip;
  642. /*
  643. * while we have the item, do the unlocked check for needing
  644. * the AIL lock.
  645. */
  646. iip = INODE_ITEM(blip);
  647. if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
  648. need_ail++;
  649. blip = next;
  650. }
  651. /* make sure we capture the state of the initial inode. */
  652. iip = INODE_ITEM(lip);
  653. if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
  654. need_ail++;
  655. /*
  656. * We only want to pull the item from the AIL if it is
  657. * actually there and its location in the log has not
  658. * changed since we started the flush. Thus, we only bother
  659. * if the ili_logged flag is set and the inode's lsn has not
  660. * changed. First we check the lsn outside
  661. * the lock since it's cheaper, and then we recheck while
  662. * holding the lock before removing the inode from the AIL.
  663. */
  664. if (need_ail) {
  665. struct xfs_log_item *log_items[need_ail];
  666. int i = 0;
  667. spin_lock(&ailp->xa_lock);
  668. for (blip = lip; blip; blip = blip->li_bio_list) {
  669. iip = INODE_ITEM(blip);
  670. if (iip->ili_logged &&
  671. blip->li_lsn == iip->ili_flush_lsn) {
  672. log_items[i++] = blip;
  673. }
  674. ASSERT(i <= need_ail);
  675. }
  676. /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
  677. xfs_trans_ail_delete_bulk(ailp, log_items, i,
  678. SHUTDOWN_CORRUPT_INCORE);
  679. }
  680. /*
  681. * clean up and unlock the flush lock now we are done. We can clear the
  682. * ili_last_fields bits now that we know that the data corresponding to
  683. * them is safely on disk.
  684. */
  685. for (blip = lip; blip; blip = next) {
  686. next = blip->li_bio_list;
  687. blip->li_bio_list = NULL;
  688. iip = INODE_ITEM(blip);
  689. iip->ili_logged = 0;
  690. iip->ili_last_fields = 0;
  691. xfs_ifunlock(iip->ili_inode);
  692. }
  693. }
  694. /*
  695. * This is the inode flushing abort routine. It is called from xfs_iflush when
  696. * the filesystem is shutting down to clean up the inode state. It is
  697. * responsible for removing the inode item from the AIL if it has not been
  698. * re-logged, and unlocking the inode's flush lock.
  699. */
  700. void
  701. xfs_iflush_abort(
  702. xfs_inode_t *ip,
  703. bool stale)
  704. {
  705. xfs_inode_log_item_t *iip = ip->i_itemp;
  706. if (iip) {
  707. struct xfs_ail *ailp = iip->ili_item.li_ailp;
  708. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  709. spin_lock(&ailp->xa_lock);
  710. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  711. /* xfs_trans_ail_delete() drops the AIL lock. */
  712. xfs_trans_ail_delete(ailp, &iip->ili_item,
  713. stale ?
  714. SHUTDOWN_LOG_IO_ERROR :
  715. SHUTDOWN_CORRUPT_INCORE);
  716. } else
  717. spin_unlock(&ailp->xa_lock);
  718. }
  719. iip->ili_logged = 0;
  720. /*
  721. * Clear the ili_last_fields bits now that we know that the
  722. * data corresponding to them is safely on disk.
  723. */
  724. iip->ili_last_fields = 0;
  725. /*
  726. * Clear the inode logging fields so no more flushes are
  727. * attempted.
  728. */
  729. iip->ili_fields = 0;
  730. }
  731. /*
  732. * Release the inode's flush lock since we're done with it.
  733. */
  734. xfs_ifunlock(ip);
  735. }
  736. void
  737. xfs_istale_done(
  738. struct xfs_buf *bp,
  739. struct xfs_log_item *lip)
  740. {
  741. xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
  742. }
  743. /*
  744. * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
  745. * (which can have different field alignments) to the native version
  746. */
  747. int
  748. xfs_inode_item_format_convert(
  749. xfs_log_iovec_t *buf,
  750. xfs_inode_log_format_t *in_f)
  751. {
  752. if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
  753. xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
  754. in_f->ilf_type = in_f32->ilf_type;
  755. in_f->ilf_size = in_f32->ilf_size;
  756. in_f->ilf_fields = in_f32->ilf_fields;
  757. in_f->ilf_asize = in_f32->ilf_asize;
  758. in_f->ilf_dsize = in_f32->ilf_dsize;
  759. in_f->ilf_ino = in_f32->ilf_ino;
  760. /* copy biggest field of ilf_u */
  761. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  762. in_f32->ilf_u.ilfu_uuid.__u_bits,
  763. sizeof(uuid_t));
  764. in_f->ilf_blkno = in_f32->ilf_blkno;
  765. in_f->ilf_len = in_f32->ilf_len;
  766. in_f->ilf_boffset = in_f32->ilf_boffset;
  767. return 0;
  768. } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
  769. xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
  770. in_f->ilf_type = in_f64->ilf_type;
  771. in_f->ilf_size = in_f64->ilf_size;
  772. in_f->ilf_fields = in_f64->ilf_fields;
  773. in_f->ilf_asize = in_f64->ilf_asize;
  774. in_f->ilf_dsize = in_f64->ilf_dsize;
  775. in_f->ilf_ino = in_f64->ilf_ino;
  776. /* copy biggest field of ilf_u */
  777. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  778. in_f64->ilf_u.ilfu_uuid.__u_bits,
  779. sizeof(uuid_t));
  780. in_f->ilf_blkno = in_f64->ilf_blkno;
  781. in_f->ilf_len = in_f64->ilf_len;
  782. in_f->ilf_boffset = in_f64->ilf_boffset;
  783. return 0;
  784. }
  785. return EFSCORRUPTED;
  786. }