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_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_trans.h"
  28. #include "xfs_inode_item.h"
  29. #include "xfs_error.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_trans_priv.h"
  32. #include "xfs_dinode.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 void
  46. xfs_inode_item_size(
  47. struct xfs_log_item *lip,
  48. int *nvecs,
  49. int *nbytes)
  50. {
  51. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  52. struct xfs_inode *ip = iip->ili_inode;
  53. *nvecs += 2;
  54. *nbytes += sizeof(struct xfs_inode_log_format) +
  55. xfs_icdinode_size(ip->i_d.di_version);
  56. switch (ip->i_d.di_format) {
  57. case XFS_DINODE_FMT_EXTENTS:
  58. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  59. ip->i_d.di_nextents > 0 &&
  60. ip->i_df.if_bytes > 0) {
  61. /* worst case, doesn't subtract delalloc extents */
  62. *nbytes += XFS_IFORK_DSIZE(ip);
  63. *nvecs += 1;
  64. }
  65. break;
  66. case XFS_DINODE_FMT_BTREE:
  67. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  68. ip->i_df.if_broot_bytes > 0) {
  69. *nbytes += ip->i_df.if_broot_bytes;
  70. *nvecs += 1;
  71. }
  72. break;
  73. case XFS_DINODE_FMT_LOCAL:
  74. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  75. ip->i_df.if_bytes > 0) {
  76. *nbytes += roundup(ip->i_df.if_bytes, 4);
  77. *nvecs += 1;
  78. }
  79. break;
  80. case XFS_DINODE_FMT_DEV:
  81. case XFS_DINODE_FMT_UUID:
  82. break;
  83. default:
  84. ASSERT(0);
  85. break;
  86. }
  87. if (!XFS_IFORK_Q(ip))
  88. return;
  89. /*
  90. * Log any necessary attribute data.
  91. */
  92. switch (ip->i_d.di_aformat) {
  93. case XFS_DINODE_FMT_EXTENTS:
  94. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  95. ip->i_d.di_anextents > 0 &&
  96. ip->i_afp->if_bytes > 0) {
  97. /* worst case, doesn't subtract unused space */
  98. *nbytes += XFS_IFORK_ASIZE(ip);
  99. *nvecs += 1;
  100. }
  101. break;
  102. case XFS_DINODE_FMT_BTREE:
  103. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  104. ip->i_afp->if_broot_bytes > 0) {
  105. *nbytes += ip->i_afp->if_broot_bytes;
  106. *nvecs += 1;
  107. }
  108. break;
  109. case XFS_DINODE_FMT_LOCAL:
  110. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  111. ip->i_afp->if_bytes > 0) {
  112. *nbytes += roundup(ip->i_afp->if_bytes, 4);
  113. *nvecs += 1;
  114. }
  115. break;
  116. default:
  117. ASSERT(0);
  118. break;
  119. }
  120. }
  121. /*
  122. * xfs_inode_item_format_extents - convert in-core extents to on-disk form
  123. *
  124. * For either the data or attr fork in extent format, we need to endian convert
  125. * the in-core extent as we place them into the on-disk inode. In this case, we
  126. * need to do this conversion before we write the extents into the log. Because
  127. * we don't have the disk inode to write into here, we allocate a buffer and
  128. * format the extents into it via xfs_iextents_copy(). We free the buffer in
  129. * the unlock routine after the copy for the log has been made.
  130. *
  131. * In the case of the data fork, the in-core and on-disk fork sizes can be
  132. * different due to delayed allocation extents. We only log on-disk extents
  133. * here, so always use the physical fork size to determine the size of the
  134. * buffer we need to allocate.
  135. */
  136. STATIC void
  137. xfs_inode_item_format_extents(
  138. struct xfs_inode *ip,
  139. struct xfs_log_iovec *vecp,
  140. int whichfork,
  141. int type)
  142. {
  143. xfs_bmbt_rec_t *ext_buffer;
  144. ext_buffer = kmem_alloc(XFS_IFORK_SIZE(ip, whichfork), KM_SLEEP);
  145. if (whichfork == XFS_DATA_FORK)
  146. ip->i_itemp->ili_extents_buf = ext_buffer;
  147. else
  148. ip->i_itemp->ili_aextents_buf = ext_buffer;
  149. vecp->i_addr = ext_buffer;
  150. vecp->i_len = xfs_iextents_copy(ip, ext_buffer, whichfork);
  151. vecp->i_type = type;
  152. }
  153. /*
  154. * This is called to fill in the vector of log iovecs for the
  155. * given inode log item. It fills the first item with an inode
  156. * log format structure, the second with the on-disk inode structure,
  157. * and a possible third and/or fourth with the inode data/extents/b-tree
  158. * root and inode attributes data/extents/b-tree root.
  159. */
  160. STATIC void
  161. xfs_inode_item_format(
  162. struct xfs_log_item *lip,
  163. struct xfs_log_iovec *vecp)
  164. {
  165. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  166. struct xfs_inode *ip = iip->ili_inode;
  167. uint nvecs;
  168. size_t data_bytes;
  169. xfs_mount_t *mp;
  170. vecp->i_addr = &iip->ili_format;
  171. vecp->i_len = sizeof(xfs_inode_log_format_t);
  172. vecp->i_type = XLOG_REG_TYPE_IFORMAT;
  173. vecp++;
  174. nvecs = 1;
  175. vecp->i_addr = &ip->i_d;
  176. vecp->i_len = xfs_icdinode_size(ip->i_d.di_version);
  177. vecp->i_type = XLOG_REG_TYPE_ICORE;
  178. vecp++;
  179. nvecs++;
  180. /*
  181. * If this is really an old format inode, then we need to
  182. * log it as such. This means that we have to copy the link
  183. * count from the new field to the old. We don't have to worry
  184. * about the new fields, because nothing trusts them as long as
  185. * the old inode version number is there. If the superblock already
  186. * has a new version number, then we don't bother converting back.
  187. */
  188. mp = ip->i_mount;
  189. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  190. if (ip->i_d.di_version == 1) {
  191. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  192. /*
  193. * Convert it back.
  194. */
  195. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  196. ip->i_d.di_onlink = ip->i_d.di_nlink;
  197. } else {
  198. /*
  199. * The superblock version has already been bumped,
  200. * so just make the conversion to the new inode
  201. * format permanent.
  202. */
  203. ip->i_d.di_version = 2;
  204. ip->i_d.di_onlink = 0;
  205. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  206. }
  207. }
  208. switch (ip->i_d.di_format) {
  209. case XFS_DINODE_FMT_EXTENTS:
  210. iip->ili_fields &=
  211. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  212. XFS_ILOG_DEV | XFS_ILOG_UUID);
  213. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  214. ip->i_d.di_nextents > 0 &&
  215. ip->i_df.if_bytes > 0) {
  216. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  217. ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
  218. ASSERT(iip->ili_extents_buf == NULL);
  219. #ifdef XFS_NATIVE_HOST
  220. if (ip->i_d.di_nextents == ip->i_df.if_bytes /
  221. (uint)sizeof(xfs_bmbt_rec_t)) {
  222. /*
  223. * There are no delayed allocation
  224. * extents, so just point to the
  225. * real extents array.
  226. */
  227. vecp->i_addr = ip->i_df.if_u1.if_extents;
  228. vecp->i_len = ip->i_df.if_bytes;
  229. vecp->i_type = XLOG_REG_TYPE_IEXT;
  230. } else
  231. #endif
  232. {
  233. xfs_inode_item_format_extents(ip, vecp,
  234. XFS_DATA_FORK, XLOG_REG_TYPE_IEXT);
  235. }
  236. ASSERT(vecp->i_len <= ip->i_df.if_bytes);
  237. iip->ili_format.ilf_dsize = vecp->i_len;
  238. vecp++;
  239. nvecs++;
  240. } else {
  241. iip->ili_fields &= ~XFS_ILOG_DEXT;
  242. }
  243. break;
  244. case XFS_DINODE_FMT_BTREE:
  245. iip->ili_fields &=
  246. ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  247. XFS_ILOG_DEV | XFS_ILOG_UUID);
  248. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  249. ip->i_df.if_broot_bytes > 0) {
  250. ASSERT(ip->i_df.if_broot != NULL);
  251. vecp->i_addr = ip->i_df.if_broot;
  252. vecp->i_len = ip->i_df.if_broot_bytes;
  253. vecp->i_type = XLOG_REG_TYPE_IBROOT;
  254. vecp++;
  255. nvecs++;
  256. iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
  257. } else {
  258. ASSERT(!(iip->ili_fields &
  259. XFS_ILOG_DBROOT));
  260. iip->ili_fields &= ~XFS_ILOG_DBROOT;
  261. }
  262. break;
  263. case XFS_DINODE_FMT_LOCAL:
  264. iip->ili_fields &=
  265. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
  266. XFS_ILOG_DEV | XFS_ILOG_UUID);
  267. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  268. ip->i_df.if_bytes > 0) {
  269. ASSERT(ip->i_df.if_u1.if_data != NULL);
  270. ASSERT(ip->i_d.di_size > 0);
  271. vecp->i_addr = ip->i_df.if_u1.if_data;
  272. /*
  273. * Round i_bytes up to a word boundary.
  274. * The underlying memory is guaranteed to
  275. * to be there by xfs_idata_realloc().
  276. */
  277. data_bytes = roundup(ip->i_df.if_bytes, 4);
  278. ASSERT((ip->i_df.if_real_bytes == 0) ||
  279. (ip->i_df.if_real_bytes == data_bytes));
  280. vecp->i_len = (int)data_bytes;
  281. vecp->i_type = XLOG_REG_TYPE_ILOCAL;
  282. vecp++;
  283. nvecs++;
  284. iip->ili_format.ilf_dsize = (unsigned)data_bytes;
  285. } else {
  286. iip->ili_fields &= ~XFS_ILOG_DDATA;
  287. }
  288. break;
  289. case XFS_DINODE_FMT_DEV:
  290. iip->ili_fields &=
  291. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  292. XFS_ILOG_DEXT | XFS_ILOG_UUID);
  293. if (iip->ili_fields & XFS_ILOG_DEV) {
  294. iip->ili_format.ilf_u.ilfu_rdev =
  295. ip->i_df.if_u2.if_rdev;
  296. }
  297. break;
  298. case XFS_DINODE_FMT_UUID:
  299. iip->ili_fields &=
  300. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  301. XFS_ILOG_DEXT | XFS_ILOG_DEV);
  302. if (iip->ili_fields & XFS_ILOG_UUID) {
  303. iip->ili_format.ilf_u.ilfu_uuid =
  304. ip->i_df.if_u2.if_uuid;
  305. }
  306. break;
  307. default:
  308. ASSERT(0);
  309. break;
  310. }
  311. /*
  312. * If there are no attributes associated with the file, then we're done.
  313. */
  314. if (!XFS_IFORK_Q(ip)) {
  315. iip->ili_fields &=
  316. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  317. goto out;
  318. }
  319. switch (ip->i_d.di_aformat) {
  320. case XFS_DINODE_FMT_EXTENTS:
  321. iip->ili_fields &=
  322. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  323. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  324. ip->i_d.di_anextents > 0 &&
  325. ip->i_afp->if_bytes > 0) {
  326. ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
  327. ip->i_d.di_anextents);
  328. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  329. #ifdef XFS_NATIVE_HOST
  330. /*
  331. * There are not delayed allocation extents
  332. * for attributes, so just point at the array.
  333. */
  334. vecp->i_addr = ip->i_afp->if_u1.if_extents;
  335. vecp->i_len = ip->i_afp->if_bytes;
  336. vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
  337. #else
  338. ASSERT(iip->ili_aextents_buf == NULL);
  339. xfs_inode_item_format_extents(ip, vecp,
  340. XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
  341. #endif
  342. iip->ili_format.ilf_asize = vecp->i_len;
  343. vecp++;
  344. nvecs++;
  345. } else {
  346. iip->ili_fields &= ~XFS_ILOG_AEXT;
  347. }
  348. break;
  349. case XFS_DINODE_FMT_BTREE:
  350. iip->ili_fields &=
  351. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  352. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  353. ip->i_afp->if_broot_bytes > 0) {
  354. ASSERT(ip->i_afp->if_broot != NULL);
  355. vecp->i_addr = ip->i_afp->if_broot;
  356. vecp->i_len = ip->i_afp->if_broot_bytes;
  357. vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
  358. vecp++;
  359. nvecs++;
  360. iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
  361. } else {
  362. iip->ili_fields &= ~XFS_ILOG_ABROOT;
  363. }
  364. break;
  365. case XFS_DINODE_FMT_LOCAL:
  366. iip->ili_fields &=
  367. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  368. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  369. ip->i_afp->if_bytes > 0) {
  370. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  371. vecp->i_addr = ip->i_afp->if_u1.if_data;
  372. /*
  373. * Round i_bytes up to a word boundary.
  374. * The underlying memory is guaranteed to
  375. * to be there by xfs_idata_realloc().
  376. */
  377. data_bytes = roundup(ip->i_afp->if_bytes, 4);
  378. ASSERT((ip->i_afp->if_real_bytes == 0) ||
  379. (ip->i_afp->if_real_bytes == data_bytes));
  380. vecp->i_len = (int)data_bytes;
  381. vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
  382. vecp++;
  383. nvecs++;
  384. iip->ili_format.ilf_asize = (unsigned)data_bytes;
  385. } else {
  386. iip->ili_fields &= ~XFS_ILOG_ADATA;
  387. }
  388. break;
  389. default:
  390. ASSERT(0);
  391. break;
  392. }
  393. out:
  394. /*
  395. * Now update the log format that goes out to disk from the in-core
  396. * values. We always write the inode core to make the arithmetic
  397. * games in recovery easier, which isn't a big deal as just about any
  398. * transaction would dirty it anyway.
  399. */
  400. iip->ili_format.ilf_fields = XFS_ILOG_CORE |
  401. (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
  402. iip->ili_format.ilf_size = nvecs;
  403. }
  404. /*
  405. * This is called to pin the inode associated with the inode log
  406. * item in memory so it cannot be written out.
  407. */
  408. STATIC void
  409. xfs_inode_item_pin(
  410. struct xfs_log_item *lip)
  411. {
  412. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  413. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  414. trace_xfs_inode_pin(ip, _RET_IP_);
  415. atomic_inc(&ip->i_pincount);
  416. }
  417. /*
  418. * This is called to unpin the inode associated with the inode log
  419. * item which was previously pinned with a call to xfs_inode_item_pin().
  420. *
  421. * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
  422. */
  423. STATIC void
  424. xfs_inode_item_unpin(
  425. struct xfs_log_item *lip,
  426. int remove)
  427. {
  428. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  429. trace_xfs_inode_unpin(ip, _RET_IP_);
  430. ASSERT(atomic_read(&ip->i_pincount) > 0);
  431. if (atomic_dec_and_test(&ip->i_pincount))
  432. wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
  433. }
  434. STATIC uint
  435. xfs_inode_item_push(
  436. struct xfs_log_item *lip,
  437. struct list_head *buffer_list)
  438. {
  439. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  440. struct xfs_inode *ip = iip->ili_inode;
  441. struct xfs_buf *bp = NULL;
  442. uint rval = XFS_ITEM_SUCCESS;
  443. int error;
  444. if (xfs_ipincount(ip) > 0)
  445. return XFS_ITEM_PINNED;
  446. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
  447. return XFS_ITEM_LOCKED;
  448. /*
  449. * Re-check the pincount now that we stabilized the value by
  450. * taking the ilock.
  451. */
  452. if (xfs_ipincount(ip) > 0) {
  453. rval = XFS_ITEM_PINNED;
  454. goto out_unlock;
  455. }
  456. /*
  457. * Stale inode items should force out the iclog.
  458. */
  459. if (ip->i_flags & XFS_ISTALE) {
  460. rval = XFS_ITEM_PINNED;
  461. goto out_unlock;
  462. }
  463. /*
  464. * Someone else is already flushing the inode. Nothing we can do
  465. * here but wait for the flush to finish and remove the item from
  466. * the AIL.
  467. */
  468. if (!xfs_iflock_nowait(ip)) {
  469. rval = XFS_ITEM_FLUSHING;
  470. goto out_unlock;
  471. }
  472. ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  473. ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  474. spin_unlock(&lip->li_ailp->xa_lock);
  475. error = xfs_iflush(ip, &bp);
  476. if (!error) {
  477. if (!xfs_buf_delwri_queue(bp, buffer_list))
  478. rval = XFS_ITEM_FLUSHING;
  479. xfs_buf_relse(bp);
  480. }
  481. spin_lock(&lip->li_ailp->xa_lock);
  482. out_unlock:
  483. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  484. return rval;
  485. }
  486. /*
  487. * Unlock the inode associated with the inode log item.
  488. * Clear the fields of the inode and inode log item that
  489. * are specific to the current transaction. If the
  490. * hold flags is set, do not unlock the inode.
  491. */
  492. STATIC void
  493. xfs_inode_item_unlock(
  494. struct xfs_log_item *lip)
  495. {
  496. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  497. struct xfs_inode *ip = iip->ili_inode;
  498. unsigned short lock_flags;
  499. ASSERT(ip->i_itemp != NULL);
  500. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  501. /*
  502. * If the inode needed a separate buffer with which to log
  503. * its extents, then free it now.
  504. */
  505. if (iip->ili_extents_buf != NULL) {
  506. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
  507. ASSERT(ip->i_d.di_nextents > 0);
  508. ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
  509. ASSERT(ip->i_df.if_bytes > 0);
  510. kmem_free(iip->ili_extents_buf);
  511. iip->ili_extents_buf = NULL;
  512. }
  513. if (iip->ili_aextents_buf != NULL) {
  514. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
  515. ASSERT(ip->i_d.di_anextents > 0);
  516. ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
  517. ASSERT(ip->i_afp->if_bytes > 0);
  518. kmem_free(iip->ili_aextents_buf);
  519. iip->ili_aextents_buf = NULL;
  520. }
  521. lock_flags = iip->ili_lock_flags;
  522. iip->ili_lock_flags = 0;
  523. if (lock_flags)
  524. xfs_iunlock(ip, lock_flags);
  525. }
  526. /*
  527. * This is called to find out where the oldest active copy of the inode log
  528. * item in the on disk log resides now that the last log write of it completed
  529. * at the given lsn. Since we always re-log all dirty data in an inode, the
  530. * latest copy in the on disk log is the only one that matters. Therefore,
  531. * simply return the given lsn.
  532. *
  533. * If the inode has been marked stale because the cluster is being freed, we
  534. * don't want to (re-)insert this inode into the AIL. There is a race condition
  535. * where the cluster buffer may be unpinned before the inode is inserted into
  536. * the AIL during transaction committed processing. If the buffer is unpinned
  537. * before the inode item has been committed and inserted, then it is possible
  538. * for the buffer to be written and IO completes before the inode is inserted
  539. * into the AIL. In that case, we'd be inserting a clean, stale inode into the
  540. * AIL which will never get removed. It will, however, get reclaimed which
  541. * triggers an assert in xfs_inode_free() complaining about freein an inode
  542. * still in the AIL.
  543. *
  544. * To avoid this, just unpin the inode directly and return a LSN of -1 so the
  545. * transaction committed code knows that it does not need to do any further
  546. * processing on the item.
  547. */
  548. STATIC xfs_lsn_t
  549. xfs_inode_item_committed(
  550. struct xfs_log_item *lip,
  551. xfs_lsn_t lsn)
  552. {
  553. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  554. struct xfs_inode *ip = iip->ili_inode;
  555. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  556. xfs_inode_item_unpin(lip, 0);
  557. return -1;
  558. }
  559. return lsn;
  560. }
  561. /*
  562. * XXX rcc - this one really has to do something. Probably needs
  563. * to stamp in a new field in the incore inode.
  564. */
  565. STATIC void
  566. xfs_inode_item_committing(
  567. struct xfs_log_item *lip,
  568. xfs_lsn_t lsn)
  569. {
  570. INODE_ITEM(lip)->ili_last_lsn = lsn;
  571. }
  572. /*
  573. * This is the ops vector shared by all buf log items.
  574. */
  575. static const struct xfs_item_ops xfs_inode_item_ops = {
  576. .iop_size = xfs_inode_item_size,
  577. .iop_format = xfs_inode_item_format,
  578. .iop_pin = xfs_inode_item_pin,
  579. .iop_unpin = xfs_inode_item_unpin,
  580. .iop_unlock = xfs_inode_item_unlock,
  581. .iop_committed = xfs_inode_item_committed,
  582. .iop_push = xfs_inode_item_push,
  583. .iop_committing = xfs_inode_item_committing
  584. };
  585. /*
  586. * Initialize the inode log item for a newly allocated (in-core) inode.
  587. */
  588. void
  589. xfs_inode_item_init(
  590. struct xfs_inode *ip,
  591. struct xfs_mount *mp)
  592. {
  593. struct xfs_inode_log_item *iip;
  594. ASSERT(ip->i_itemp == NULL);
  595. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  596. iip->ili_inode = ip;
  597. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  598. &xfs_inode_item_ops);
  599. iip->ili_format.ilf_type = XFS_LI_INODE;
  600. iip->ili_format.ilf_ino = ip->i_ino;
  601. iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
  602. iip->ili_format.ilf_len = ip->i_imap.im_len;
  603. iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
  604. }
  605. /*
  606. * Free the inode log item and any memory hanging off of it.
  607. */
  608. void
  609. xfs_inode_item_destroy(
  610. xfs_inode_t *ip)
  611. {
  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. }