xfs_inode_item.c 21 KB

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