xfs_inode_item.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. * Someone else is already flushing the inode. Nothing we can do
  452. * here but wait for the flush to finish and remove the item from
  453. * the AIL.
  454. */
  455. if (!xfs_iflock_nowait(ip)) {
  456. rval = XFS_ITEM_FLUSHING;
  457. goto out_unlock;
  458. }
  459. /*
  460. * Stale inode items should force out the iclog.
  461. */
  462. if (ip->i_flags & XFS_ISTALE) {
  463. xfs_ifunlock(ip);
  464. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  465. return XFS_ITEM_PINNED;
  466. }
  467. ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  468. ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  469. spin_unlock(&lip->li_ailp->xa_lock);
  470. error = xfs_iflush(ip, &bp);
  471. if (!error) {
  472. if (!xfs_buf_delwri_queue(bp, buffer_list))
  473. rval = XFS_ITEM_FLUSHING;
  474. xfs_buf_relse(bp);
  475. }
  476. spin_lock(&lip->li_ailp->xa_lock);
  477. out_unlock:
  478. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  479. return rval;
  480. }
  481. /*
  482. * Unlock the inode associated with the inode log item.
  483. * Clear the fields of the inode and inode log item that
  484. * are specific to the current transaction. If the
  485. * hold flags is set, do not unlock the inode.
  486. */
  487. STATIC void
  488. xfs_inode_item_unlock(
  489. struct xfs_log_item *lip)
  490. {
  491. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  492. struct xfs_inode *ip = iip->ili_inode;
  493. unsigned short lock_flags;
  494. ASSERT(ip->i_itemp != NULL);
  495. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  496. /*
  497. * If the inode needed a separate buffer with which to log
  498. * its extents, then free it now.
  499. */
  500. if (iip->ili_extents_buf != NULL) {
  501. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
  502. ASSERT(ip->i_d.di_nextents > 0);
  503. ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
  504. ASSERT(ip->i_df.if_bytes > 0);
  505. kmem_free(iip->ili_extents_buf);
  506. iip->ili_extents_buf = NULL;
  507. }
  508. if (iip->ili_aextents_buf != NULL) {
  509. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
  510. ASSERT(ip->i_d.di_anextents > 0);
  511. ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
  512. ASSERT(ip->i_afp->if_bytes > 0);
  513. kmem_free(iip->ili_aextents_buf);
  514. iip->ili_aextents_buf = NULL;
  515. }
  516. lock_flags = iip->ili_lock_flags;
  517. iip->ili_lock_flags = 0;
  518. if (lock_flags)
  519. xfs_iunlock(ip, lock_flags);
  520. }
  521. /*
  522. * This is called to find out where the oldest active copy of the inode log
  523. * item in the on disk log resides now that the last log write of it completed
  524. * at the given lsn. Since we always re-log all dirty data in an inode, the
  525. * latest copy in the on disk log is the only one that matters. Therefore,
  526. * simply return the given lsn.
  527. *
  528. * If the inode has been marked stale because the cluster is being freed, we
  529. * don't want to (re-)insert this inode into the AIL. There is a race condition
  530. * where the cluster buffer may be unpinned before the inode is inserted into
  531. * the AIL during transaction committed processing. If the buffer is unpinned
  532. * before the inode item has been committed and inserted, then it is possible
  533. * for the buffer to be written and IO completes before the inode is inserted
  534. * into the AIL. In that case, we'd be inserting a clean, stale inode into the
  535. * AIL which will never get removed. It will, however, get reclaimed which
  536. * triggers an assert in xfs_inode_free() complaining about freein an inode
  537. * still in the AIL.
  538. *
  539. * To avoid this, just unpin the inode directly and return a LSN of -1 so the
  540. * transaction committed code knows that it does not need to do any further
  541. * processing on the item.
  542. */
  543. STATIC xfs_lsn_t
  544. xfs_inode_item_committed(
  545. struct xfs_log_item *lip,
  546. xfs_lsn_t lsn)
  547. {
  548. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  549. struct xfs_inode *ip = iip->ili_inode;
  550. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  551. xfs_inode_item_unpin(lip, 0);
  552. return -1;
  553. }
  554. return lsn;
  555. }
  556. /*
  557. * XXX rcc - this one really has to do something. Probably needs
  558. * to stamp in a new field in the incore inode.
  559. */
  560. STATIC void
  561. xfs_inode_item_committing(
  562. struct xfs_log_item *lip,
  563. xfs_lsn_t lsn)
  564. {
  565. INODE_ITEM(lip)->ili_last_lsn = lsn;
  566. }
  567. /*
  568. * This is the ops vector shared by all buf log items.
  569. */
  570. static const struct xfs_item_ops xfs_inode_item_ops = {
  571. .iop_size = xfs_inode_item_size,
  572. .iop_format = xfs_inode_item_format,
  573. .iop_pin = xfs_inode_item_pin,
  574. .iop_unpin = xfs_inode_item_unpin,
  575. .iop_unlock = xfs_inode_item_unlock,
  576. .iop_committed = xfs_inode_item_committed,
  577. .iop_push = xfs_inode_item_push,
  578. .iop_committing = xfs_inode_item_committing
  579. };
  580. /*
  581. * Initialize the inode log item for a newly allocated (in-core) inode.
  582. */
  583. void
  584. xfs_inode_item_init(
  585. struct xfs_inode *ip,
  586. struct xfs_mount *mp)
  587. {
  588. struct xfs_inode_log_item *iip;
  589. ASSERT(ip->i_itemp == NULL);
  590. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  591. iip->ili_inode = ip;
  592. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  593. &xfs_inode_item_ops);
  594. iip->ili_format.ilf_type = XFS_LI_INODE;
  595. iip->ili_format.ilf_ino = ip->i_ino;
  596. iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
  597. iip->ili_format.ilf_len = ip->i_imap.im_len;
  598. iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
  599. }
  600. /*
  601. * Free the inode log item and any memory hanging off of it.
  602. */
  603. void
  604. xfs_inode_item_destroy(
  605. xfs_inode_t *ip)
  606. {
  607. #ifdef XFS_TRANS_DEBUG
  608. if (ip->i_itemp->ili_root_size != 0) {
  609. kmem_free(ip->i_itemp->ili_orig_root);
  610. }
  611. #endif
  612. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  613. }
  614. /*
  615. * This is the inode flushing I/O completion routine. It is called
  616. * from interrupt level when the buffer containing the inode is
  617. * flushed to disk. It is responsible for removing the inode item
  618. * from the AIL if it has not been re-logged, and unlocking the inode's
  619. * flush lock.
  620. *
  621. * To reduce AIL lock traffic as much as possible, we scan the buffer log item
  622. * list for other inodes that will run this function. We remove them from the
  623. * buffer list so we can process all the inode IO completions in one AIL lock
  624. * traversal.
  625. */
  626. void
  627. xfs_iflush_done(
  628. struct xfs_buf *bp,
  629. struct xfs_log_item *lip)
  630. {
  631. struct xfs_inode_log_item *iip;
  632. struct xfs_log_item *blip;
  633. struct xfs_log_item *next;
  634. struct xfs_log_item *prev;
  635. struct xfs_ail *ailp = lip->li_ailp;
  636. int need_ail = 0;
  637. /*
  638. * Scan the buffer IO completions for other inodes being completed and
  639. * attach them to the current inode log item.
  640. */
  641. blip = bp->b_fspriv;
  642. prev = NULL;
  643. while (blip != NULL) {
  644. if (lip->li_cb != xfs_iflush_done) {
  645. prev = blip;
  646. blip = blip->li_bio_list;
  647. continue;
  648. }
  649. /* remove from list */
  650. next = blip->li_bio_list;
  651. if (!prev) {
  652. bp->b_fspriv = next;
  653. } else {
  654. prev->li_bio_list = next;
  655. }
  656. /* add to current list */
  657. blip->li_bio_list = lip->li_bio_list;
  658. lip->li_bio_list = blip;
  659. /*
  660. * while we have the item, do the unlocked check for needing
  661. * the AIL lock.
  662. */
  663. iip = INODE_ITEM(blip);
  664. if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
  665. need_ail++;
  666. blip = next;
  667. }
  668. /* make sure we capture the state of the initial inode. */
  669. iip = INODE_ITEM(lip);
  670. if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
  671. need_ail++;
  672. /*
  673. * We only want to pull the item from the AIL if it is
  674. * actually there and its location in the log has not
  675. * changed since we started the flush. Thus, we only bother
  676. * if the ili_logged flag is set and the inode's lsn has not
  677. * changed. First we check the lsn outside
  678. * the lock since it's cheaper, and then we recheck while
  679. * holding the lock before removing the inode from the AIL.
  680. */
  681. if (need_ail) {
  682. struct xfs_log_item *log_items[need_ail];
  683. int i = 0;
  684. spin_lock(&ailp->xa_lock);
  685. for (blip = lip; blip; blip = blip->li_bio_list) {
  686. iip = INODE_ITEM(blip);
  687. if (iip->ili_logged &&
  688. blip->li_lsn == iip->ili_flush_lsn) {
  689. log_items[i++] = blip;
  690. }
  691. ASSERT(i <= need_ail);
  692. }
  693. /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
  694. xfs_trans_ail_delete_bulk(ailp, log_items, i,
  695. SHUTDOWN_CORRUPT_INCORE);
  696. }
  697. /*
  698. * clean up and unlock the flush lock now we are done. We can clear the
  699. * ili_last_fields bits now that we know that the data corresponding to
  700. * them is safely on disk.
  701. */
  702. for (blip = lip; blip; blip = next) {
  703. next = blip->li_bio_list;
  704. blip->li_bio_list = NULL;
  705. iip = INODE_ITEM(blip);
  706. iip->ili_logged = 0;
  707. iip->ili_last_fields = 0;
  708. xfs_ifunlock(iip->ili_inode);
  709. }
  710. }
  711. /*
  712. * This is the inode flushing abort routine. It is called from xfs_iflush when
  713. * the filesystem is shutting down to clean up the inode state. It is
  714. * responsible for removing the inode item from the AIL if it has not been
  715. * re-logged, and unlocking the inode's flush lock.
  716. */
  717. void
  718. xfs_iflush_abort(
  719. xfs_inode_t *ip,
  720. bool stale)
  721. {
  722. xfs_inode_log_item_t *iip = ip->i_itemp;
  723. if (iip) {
  724. struct xfs_ail *ailp = iip->ili_item.li_ailp;
  725. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  726. spin_lock(&ailp->xa_lock);
  727. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  728. /* xfs_trans_ail_delete() drops the AIL lock. */
  729. xfs_trans_ail_delete(ailp, &iip->ili_item,
  730. stale ?
  731. SHUTDOWN_LOG_IO_ERROR :
  732. SHUTDOWN_CORRUPT_INCORE);
  733. } else
  734. spin_unlock(&ailp->xa_lock);
  735. }
  736. iip->ili_logged = 0;
  737. /*
  738. * Clear the ili_last_fields bits now that we know that the
  739. * data corresponding to them is safely on disk.
  740. */
  741. iip->ili_last_fields = 0;
  742. /*
  743. * Clear the inode logging fields so no more flushes are
  744. * attempted.
  745. */
  746. iip->ili_fields = 0;
  747. }
  748. /*
  749. * Release the inode's flush lock since we're done with it.
  750. */
  751. xfs_ifunlock(ip);
  752. }
  753. void
  754. xfs_istale_done(
  755. struct xfs_buf *bp,
  756. struct xfs_log_item *lip)
  757. {
  758. xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
  759. }
  760. /*
  761. * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
  762. * (which can have different field alignments) to the native version
  763. */
  764. int
  765. xfs_inode_item_format_convert(
  766. xfs_log_iovec_t *buf,
  767. xfs_inode_log_format_t *in_f)
  768. {
  769. if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
  770. xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
  771. in_f->ilf_type = in_f32->ilf_type;
  772. in_f->ilf_size = in_f32->ilf_size;
  773. in_f->ilf_fields = in_f32->ilf_fields;
  774. in_f->ilf_asize = in_f32->ilf_asize;
  775. in_f->ilf_dsize = in_f32->ilf_dsize;
  776. in_f->ilf_ino = in_f32->ilf_ino;
  777. /* copy biggest field of ilf_u */
  778. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  779. in_f32->ilf_u.ilfu_uuid.__u_bits,
  780. sizeof(uuid_t));
  781. in_f->ilf_blkno = in_f32->ilf_blkno;
  782. in_f->ilf_len = in_f32->ilf_len;
  783. in_f->ilf_boffset = in_f32->ilf_boffset;
  784. return 0;
  785. } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
  786. xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
  787. in_f->ilf_type = in_f64->ilf_type;
  788. in_f->ilf_size = in_f64->ilf_size;
  789. in_f->ilf_fields = in_f64->ilf_fields;
  790. in_f->ilf_asize = in_f64->ilf_asize;
  791. in_f->ilf_dsize = in_f64->ilf_dsize;
  792. in_f->ilf_ino = in_f64->ilf_ino;
  793. /* copy biggest field of ilf_u */
  794. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  795. in_f64->ilf_u.ilfu_uuid.__u_bits,
  796. sizeof(uuid_t));
  797. in_f->ilf_blkno = in_f64->ilf_blkno;
  798. in_f->ilf_len = in_f64->ilf_len;
  799. in_f->ilf_boffset = in_f64->ilf_boffset;
  800. return 0;
  801. }
  802. return EFSCORRUPTED;
  803. }