xfs_inode_item.c 21 KB

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