xfs_inode_item.c 22 KB

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