xfs_inode_item.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_inode_item.h"
  16. #include "xfs_trace.h"
  17. #include "xfs_trans_priv.h"
  18. #include "xfs_buf_item.h"
  19. #include "xfs_log.h"
  20. #include "xfs_log_priv.h"
  21. #include "xfs_error.h"
  22. #include <linux/iversion.h>
  23. struct kmem_cache *xfs_ili_cache; /* inode log item */
  24. static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
  25. {
  26. return container_of(lip, struct xfs_inode_log_item, ili_item);
  27. }
  28. /*
  29. * The logged size of an inode fork is always the current size of the inode
  30. * fork. This means that when an inode fork is relogged, the size of the logged
  31. * region is determined by the current state, not the combination of the
  32. * previously logged state + the current state. This is different relogging
  33. * behaviour to most other log items which will retain the size of the
  34. * previously logged changes when smaller regions are relogged.
  35. *
  36. * Hence operations that remove data from the inode fork (e.g. shortform
  37. * dir/attr remove, extent form extent removal, etc), the size of the relogged
  38. * inode gets -smaller- rather than stays the same size as the previously logged
  39. * size and this can result in the committing transaction reducing the amount of
  40. * space being consumed by the CIL.
  41. */
  42. STATIC void
  43. xfs_inode_item_data_fork_size(
  44. struct xfs_inode_log_item *iip,
  45. int *nvecs,
  46. int *nbytes)
  47. {
  48. struct xfs_inode *ip = iip->ili_inode;
  49. switch (ip->i_df.if_format) {
  50. case XFS_DINODE_FMT_EXTENTS:
  51. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  52. ip->i_df.if_nextents > 0 &&
  53. ip->i_df.if_bytes > 0) {
  54. /* worst case, doesn't subtract delalloc extents */
  55. *nbytes += xfs_inode_data_fork_size(ip);
  56. *nvecs += 1;
  57. }
  58. break;
  59. case XFS_DINODE_FMT_BTREE:
  60. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  61. ip->i_df.if_broot_bytes > 0) {
  62. *nbytes += ip->i_df.if_broot_bytes;
  63. *nvecs += 1;
  64. }
  65. break;
  66. case XFS_DINODE_FMT_LOCAL:
  67. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  68. ip->i_df.if_bytes > 0) {
  69. *nbytes += xlog_calc_iovec_len(ip->i_df.if_bytes);
  70. *nvecs += 1;
  71. }
  72. break;
  73. case XFS_DINODE_FMT_DEV:
  74. break;
  75. default:
  76. ASSERT(0);
  77. break;
  78. }
  79. }
  80. STATIC void
  81. xfs_inode_item_attr_fork_size(
  82. struct xfs_inode_log_item *iip,
  83. int *nvecs,
  84. int *nbytes)
  85. {
  86. struct xfs_inode *ip = iip->ili_inode;
  87. switch (ip->i_af.if_format) {
  88. case XFS_DINODE_FMT_EXTENTS:
  89. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  90. ip->i_af.if_nextents > 0 &&
  91. ip->i_af.if_bytes > 0) {
  92. /* worst case, doesn't subtract unused space */
  93. *nbytes += xfs_inode_attr_fork_size(ip);
  94. *nvecs += 1;
  95. }
  96. break;
  97. case XFS_DINODE_FMT_BTREE:
  98. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  99. ip->i_af.if_broot_bytes > 0) {
  100. *nbytes += ip->i_af.if_broot_bytes;
  101. *nvecs += 1;
  102. }
  103. break;
  104. case XFS_DINODE_FMT_LOCAL:
  105. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  106. ip->i_af.if_bytes > 0) {
  107. *nbytes += xlog_calc_iovec_len(ip->i_af.if_bytes);
  108. *nvecs += 1;
  109. }
  110. break;
  111. default:
  112. ASSERT(0);
  113. break;
  114. }
  115. }
  116. /*
  117. * This returns the number of iovecs needed to log the given inode item.
  118. *
  119. * We need one iovec for the inode log format structure, one for the
  120. * inode core, and possibly one for the inode data/extents/b-tree root
  121. * and one for the inode attribute data/extents/b-tree root.
  122. */
  123. STATIC void
  124. xfs_inode_item_size(
  125. struct xfs_log_item *lip,
  126. int *nvecs,
  127. int *nbytes)
  128. {
  129. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  130. struct xfs_inode *ip = iip->ili_inode;
  131. *nvecs += 2;
  132. *nbytes += sizeof(struct xfs_inode_log_format) +
  133. xfs_log_dinode_size(ip->i_mount);
  134. xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
  135. if (xfs_inode_has_attr_fork(ip))
  136. xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
  137. }
  138. STATIC void
  139. xfs_inode_item_format_data_fork(
  140. struct xfs_inode_log_item *iip,
  141. struct xfs_inode_log_format *ilf,
  142. struct xfs_log_vec *lv,
  143. struct xfs_log_iovec **vecp)
  144. {
  145. struct xfs_inode *ip = iip->ili_inode;
  146. size_t data_bytes;
  147. switch (ip->i_df.if_format) {
  148. case XFS_DINODE_FMT_EXTENTS:
  149. iip->ili_fields &=
  150. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
  151. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  152. ip->i_df.if_nextents > 0 &&
  153. ip->i_df.if_bytes > 0) {
  154. struct xfs_bmbt_rec *p;
  155. ASSERT(xfs_iext_count(&ip->i_df) > 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 | XFS_ILOG_DEV);
  169. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  170. ip->i_df.if_broot_bytes > 0) {
  171. ASSERT(ip->i_df.if_broot != NULL);
  172. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
  173. ip->i_df.if_broot,
  174. ip->i_df.if_broot_bytes);
  175. ilf->ilf_dsize = ip->i_df.if_broot_bytes;
  176. ilf->ilf_size++;
  177. } else {
  178. ASSERT(!(iip->ili_fields &
  179. XFS_ILOG_DBROOT));
  180. iip->ili_fields &= ~XFS_ILOG_DBROOT;
  181. }
  182. break;
  183. case XFS_DINODE_FMT_LOCAL:
  184. iip->ili_fields &=
  185. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
  186. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  187. ip->i_df.if_bytes > 0) {
  188. ASSERT(ip->i_df.if_u1.if_data != NULL);
  189. ASSERT(ip->i_disk_size > 0);
  190. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
  191. ip->i_df.if_u1.if_data,
  192. ip->i_df.if_bytes);
  193. ilf->ilf_dsize = (unsigned)ip->i_df.if_bytes;
  194. ilf->ilf_size++;
  195. } else {
  196. iip->ili_fields &= ~XFS_ILOG_DDATA;
  197. }
  198. break;
  199. case XFS_DINODE_FMT_DEV:
  200. iip->ili_fields &=
  201. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
  202. if (iip->ili_fields & XFS_ILOG_DEV)
  203. ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev);
  204. break;
  205. default:
  206. ASSERT(0);
  207. break;
  208. }
  209. }
  210. STATIC void
  211. xfs_inode_item_format_attr_fork(
  212. struct xfs_inode_log_item *iip,
  213. struct xfs_inode_log_format *ilf,
  214. struct xfs_log_vec *lv,
  215. struct xfs_log_iovec **vecp)
  216. {
  217. struct xfs_inode *ip = iip->ili_inode;
  218. size_t data_bytes;
  219. switch (ip->i_af.if_format) {
  220. case XFS_DINODE_FMT_EXTENTS:
  221. iip->ili_fields &=
  222. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  223. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  224. ip->i_af.if_nextents > 0 &&
  225. ip->i_af.if_bytes > 0) {
  226. struct xfs_bmbt_rec *p;
  227. ASSERT(xfs_iext_count(&ip->i_af) ==
  228. ip->i_af.if_nextents);
  229. p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
  230. data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
  231. xlog_finish_iovec(lv, *vecp, data_bytes);
  232. ilf->ilf_asize = data_bytes;
  233. ilf->ilf_size++;
  234. } else {
  235. iip->ili_fields &= ~XFS_ILOG_AEXT;
  236. }
  237. break;
  238. case XFS_DINODE_FMT_BTREE:
  239. iip->ili_fields &=
  240. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  241. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  242. ip->i_af.if_broot_bytes > 0) {
  243. ASSERT(ip->i_af.if_broot != NULL);
  244. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
  245. ip->i_af.if_broot,
  246. ip->i_af.if_broot_bytes);
  247. ilf->ilf_asize = ip->i_af.if_broot_bytes;
  248. ilf->ilf_size++;
  249. } else {
  250. iip->ili_fields &= ~XFS_ILOG_ABROOT;
  251. }
  252. break;
  253. case XFS_DINODE_FMT_LOCAL:
  254. iip->ili_fields &=
  255. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  256. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  257. ip->i_af.if_bytes > 0) {
  258. ASSERT(ip->i_af.if_u1.if_data != NULL);
  259. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
  260. ip->i_af.if_u1.if_data,
  261. ip->i_af.if_bytes);
  262. ilf->ilf_asize = (unsigned)ip->i_af.if_bytes;
  263. ilf->ilf_size++;
  264. } else {
  265. iip->ili_fields &= ~XFS_ILOG_ADATA;
  266. }
  267. break;
  268. default:
  269. ASSERT(0);
  270. break;
  271. }
  272. }
  273. /*
  274. * Convert an incore timestamp to a log timestamp. Note that the log format
  275. * specifies host endian format!
  276. */
  277. static inline xfs_log_timestamp_t
  278. xfs_inode_to_log_dinode_ts(
  279. struct xfs_inode *ip,
  280. const struct timespec64 tv)
  281. {
  282. struct xfs_log_legacy_timestamp *lits;
  283. xfs_log_timestamp_t its;
  284. if (xfs_inode_has_bigtime(ip))
  285. return xfs_inode_encode_bigtime(tv);
  286. lits = (struct xfs_log_legacy_timestamp *)&its;
  287. lits->t_sec = tv.tv_sec;
  288. lits->t_nsec = tv.tv_nsec;
  289. return its;
  290. }
  291. /*
  292. * The legacy DMAPI fields are only present in the on-disk and in-log inodes,
  293. * but not in the in-memory one. But we are guaranteed to have an inode buffer
  294. * in memory when logging an inode, so we can just copy it from the on-disk
  295. * inode to the in-log inode here so that recovery of file system with these
  296. * fields set to non-zero values doesn't lose them. For all other cases we zero
  297. * the fields.
  298. */
  299. static void
  300. xfs_copy_dm_fields_to_log_dinode(
  301. struct xfs_inode *ip,
  302. struct xfs_log_dinode *to)
  303. {
  304. struct xfs_dinode *dip;
  305. dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf,
  306. ip->i_imap.im_boffset);
  307. if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) {
  308. to->di_dmevmask = be32_to_cpu(dip->di_dmevmask);
  309. to->di_dmstate = be16_to_cpu(dip->di_dmstate);
  310. } else {
  311. to->di_dmevmask = 0;
  312. to->di_dmstate = 0;
  313. }
  314. }
  315. static inline void
  316. xfs_inode_to_log_dinode_iext_counters(
  317. struct xfs_inode *ip,
  318. struct xfs_log_dinode *to)
  319. {
  320. if (xfs_inode_has_large_extent_counts(ip)) {
  321. to->di_big_nextents = xfs_ifork_nextents(&ip->i_df);
  322. to->di_big_anextents = xfs_ifork_nextents(&ip->i_af);
  323. to->di_nrext64_pad = 0;
  324. } else {
  325. to->di_nextents = xfs_ifork_nextents(&ip->i_df);
  326. to->di_anextents = xfs_ifork_nextents(&ip->i_af);
  327. }
  328. }
  329. static void
  330. xfs_inode_to_log_dinode(
  331. struct xfs_inode *ip,
  332. struct xfs_log_dinode *to,
  333. xfs_lsn_t lsn)
  334. {
  335. struct inode *inode = VFS_I(ip);
  336. to->di_magic = XFS_DINODE_MAGIC;
  337. to->di_format = xfs_ifork_format(&ip->i_df);
  338. to->di_uid = i_uid_read(inode);
  339. to->di_gid = i_gid_read(inode);
  340. to->di_projid_lo = ip->i_projid & 0xffff;
  341. to->di_projid_hi = ip->i_projid >> 16;
  342. memset(to->di_pad3, 0, sizeof(to->di_pad3));
  343. to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode->i_atime);
  344. to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode->i_mtime);
  345. to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode->i_ctime);
  346. to->di_nlink = inode->i_nlink;
  347. to->di_gen = inode->i_generation;
  348. to->di_mode = inode->i_mode;
  349. to->di_size = ip->i_disk_size;
  350. to->di_nblocks = ip->i_nblocks;
  351. to->di_extsize = ip->i_extsize;
  352. to->di_forkoff = ip->i_forkoff;
  353. to->di_aformat = xfs_ifork_format(&ip->i_af);
  354. to->di_flags = ip->i_diflags;
  355. xfs_copy_dm_fields_to_log_dinode(ip, to);
  356. /* log a dummy value to ensure log structure is fully initialised */
  357. to->di_next_unlinked = NULLAGINO;
  358. if (xfs_has_v3inodes(ip->i_mount)) {
  359. to->di_version = 3;
  360. to->di_changecount = inode_peek_iversion(inode);
  361. to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime);
  362. to->di_flags2 = ip->i_diflags2;
  363. to->di_cowextsize = ip->i_cowextsize;
  364. to->di_ino = ip->i_ino;
  365. to->di_lsn = lsn;
  366. memset(to->di_pad2, 0, sizeof(to->di_pad2));
  367. uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
  368. to->di_v3_pad = 0;
  369. } else {
  370. to->di_version = 2;
  371. to->di_flushiter = ip->i_flushiter;
  372. memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
  373. }
  374. xfs_inode_to_log_dinode_iext_counters(ip, to);
  375. }
  376. /*
  377. * Format the inode core. Current timestamp data is only in the VFS inode
  378. * fields, so we need to grab them from there. Hence rather than just copying
  379. * the XFS inode core structure, format the fields directly into the iovec.
  380. */
  381. static void
  382. xfs_inode_item_format_core(
  383. struct xfs_inode *ip,
  384. struct xfs_log_vec *lv,
  385. struct xfs_log_iovec **vecp)
  386. {
  387. struct xfs_log_dinode *dic;
  388. dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
  389. xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
  390. xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_mount));
  391. }
  392. /*
  393. * This is called to fill in the vector of log iovecs for the given inode
  394. * log item. It fills the first item with an inode log format structure,
  395. * the second with the on-disk inode structure, and a possible third and/or
  396. * fourth with the inode data/extents/b-tree root and inode attributes
  397. * data/extents/b-tree root.
  398. *
  399. * Note: Always use the 64 bit inode log format structure so we don't
  400. * leave an uninitialised hole in the format item on 64 bit systems. Log
  401. * recovery on 32 bit systems handles this just fine, so there's no reason
  402. * for not using an initialising the properly padded structure all the time.
  403. */
  404. STATIC void
  405. xfs_inode_item_format(
  406. struct xfs_log_item *lip,
  407. struct xfs_log_vec *lv)
  408. {
  409. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  410. struct xfs_inode *ip = iip->ili_inode;
  411. struct xfs_log_iovec *vecp = NULL;
  412. struct xfs_inode_log_format *ilf;
  413. ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
  414. ilf->ilf_type = XFS_LI_INODE;
  415. ilf->ilf_ino = ip->i_ino;
  416. ilf->ilf_blkno = ip->i_imap.im_blkno;
  417. ilf->ilf_len = ip->i_imap.im_len;
  418. ilf->ilf_boffset = ip->i_imap.im_boffset;
  419. ilf->ilf_fields = XFS_ILOG_CORE;
  420. ilf->ilf_size = 2; /* format + core */
  421. /*
  422. * make sure we don't leak uninitialised data into the log in the case
  423. * when we don't log every field in the inode.
  424. */
  425. ilf->ilf_dsize = 0;
  426. ilf->ilf_asize = 0;
  427. ilf->ilf_pad = 0;
  428. memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
  429. xlog_finish_iovec(lv, vecp, sizeof(*ilf));
  430. xfs_inode_item_format_core(ip, lv, &vecp);
  431. xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
  432. if (xfs_inode_has_attr_fork(ip)) {
  433. xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
  434. } else {
  435. iip->ili_fields &=
  436. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  437. }
  438. /* update the format with the exact fields we actually logged */
  439. ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
  440. }
  441. /*
  442. * This is called to pin the inode associated with the inode log
  443. * item in memory so it cannot be written out.
  444. */
  445. STATIC void
  446. xfs_inode_item_pin(
  447. struct xfs_log_item *lip)
  448. {
  449. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  450. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  451. ASSERT(lip->li_buf);
  452. trace_xfs_inode_pin(ip, _RET_IP_);
  453. atomic_inc(&ip->i_pincount);
  454. }
  455. /*
  456. * This is called to unpin the inode associated with the inode log
  457. * item which was previously pinned with a call to xfs_inode_item_pin().
  458. *
  459. * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
  460. *
  461. * Note that unpin can race with inode cluster buffer freeing marking the buffer
  462. * stale. In that case, flush completions are run from the buffer unpin call,
  463. * which may happen before the inode is unpinned. If we lose the race, there
  464. * will be no buffer attached to the log item, but the inode will be marked
  465. * XFS_ISTALE.
  466. */
  467. STATIC void
  468. xfs_inode_item_unpin(
  469. struct xfs_log_item *lip,
  470. int remove)
  471. {
  472. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  473. trace_xfs_inode_unpin(ip, _RET_IP_);
  474. ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
  475. ASSERT(atomic_read(&ip->i_pincount) > 0);
  476. if (atomic_dec_and_test(&ip->i_pincount))
  477. wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
  478. }
  479. STATIC uint
  480. xfs_inode_item_push(
  481. struct xfs_log_item *lip,
  482. struct list_head *buffer_list)
  483. __releases(&lip->li_ailp->ail_lock)
  484. __acquires(&lip->li_ailp->ail_lock)
  485. {
  486. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  487. struct xfs_inode *ip = iip->ili_inode;
  488. struct xfs_buf *bp = lip->li_buf;
  489. uint rval = XFS_ITEM_SUCCESS;
  490. int error;
  491. if (!bp || (ip->i_flags & XFS_ISTALE)) {
  492. /*
  493. * Inode item/buffer is being aborted due to cluster
  494. * buffer deletion. Trigger a log force to have that operation
  495. * completed and items removed from the AIL before the next push
  496. * attempt.
  497. */
  498. return XFS_ITEM_PINNED;
  499. }
  500. if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp))
  501. return XFS_ITEM_PINNED;
  502. if (xfs_iflags_test(ip, XFS_IFLUSHING))
  503. return XFS_ITEM_FLUSHING;
  504. if (!xfs_buf_trylock(bp))
  505. return XFS_ITEM_LOCKED;
  506. spin_unlock(&lip->li_ailp->ail_lock);
  507. /*
  508. * We need to hold a reference for flushing the cluster buffer as it may
  509. * fail the buffer without IO submission. In which case, we better get a
  510. * reference for that completion because otherwise we don't get a
  511. * reference for IO until we queue the buffer for delwri submission.
  512. */
  513. xfs_buf_hold(bp);
  514. error = xfs_iflush_cluster(bp);
  515. if (!error) {
  516. if (!xfs_buf_delwri_queue(bp, buffer_list))
  517. rval = XFS_ITEM_FLUSHING;
  518. xfs_buf_relse(bp);
  519. } else {
  520. /*
  521. * Release the buffer if we were unable to flush anything. On
  522. * any other error, the buffer has already been released.
  523. */
  524. if (error == -EAGAIN)
  525. xfs_buf_relse(bp);
  526. rval = XFS_ITEM_LOCKED;
  527. }
  528. spin_lock(&lip->li_ailp->ail_lock);
  529. return rval;
  530. }
  531. /*
  532. * Unlock the inode associated with the inode log item.
  533. */
  534. STATIC void
  535. xfs_inode_item_release(
  536. struct xfs_log_item *lip)
  537. {
  538. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  539. struct xfs_inode *ip = iip->ili_inode;
  540. unsigned short lock_flags;
  541. ASSERT(ip->i_itemp != NULL);
  542. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  543. lock_flags = iip->ili_lock_flags;
  544. iip->ili_lock_flags = 0;
  545. if (lock_flags)
  546. xfs_iunlock(ip, lock_flags);
  547. }
  548. /*
  549. * This is called to find out where the oldest active copy of the inode log
  550. * item in the on disk log resides now that the last log write of it completed
  551. * at the given lsn. Since we always re-log all dirty data in an inode, the
  552. * latest copy in the on disk log is the only one that matters. Therefore,
  553. * simply return the given lsn.
  554. *
  555. * If the inode has been marked stale because the cluster is being freed, we
  556. * don't want to (re-)insert this inode into the AIL. There is a race condition
  557. * where the cluster buffer may be unpinned before the inode is inserted into
  558. * the AIL during transaction committed processing. If the buffer is unpinned
  559. * before the inode item has been committed and inserted, then it is possible
  560. * for the buffer to be written and IO completes before the inode is inserted
  561. * into the AIL. In that case, we'd be inserting a clean, stale inode into the
  562. * AIL which will never get removed. It will, however, get reclaimed which
  563. * triggers an assert in xfs_inode_free() complaining about freein an inode
  564. * still in the AIL.
  565. *
  566. * To avoid this, just unpin the inode directly and return a LSN of -1 so the
  567. * transaction committed code knows that it does not need to do any further
  568. * processing on the item.
  569. */
  570. STATIC xfs_lsn_t
  571. xfs_inode_item_committed(
  572. struct xfs_log_item *lip,
  573. xfs_lsn_t lsn)
  574. {
  575. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  576. struct xfs_inode *ip = iip->ili_inode;
  577. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  578. xfs_inode_item_unpin(lip, 0);
  579. return -1;
  580. }
  581. return lsn;
  582. }
  583. STATIC void
  584. xfs_inode_item_committing(
  585. struct xfs_log_item *lip,
  586. xfs_csn_t seq)
  587. {
  588. INODE_ITEM(lip)->ili_commit_seq = seq;
  589. return xfs_inode_item_release(lip);
  590. }
  591. static const struct xfs_item_ops xfs_inode_item_ops = {
  592. .iop_size = xfs_inode_item_size,
  593. .iop_format = xfs_inode_item_format,
  594. .iop_pin = xfs_inode_item_pin,
  595. .iop_unpin = xfs_inode_item_unpin,
  596. .iop_release = xfs_inode_item_release,
  597. .iop_committed = xfs_inode_item_committed,
  598. .iop_push = xfs_inode_item_push,
  599. .iop_committing = xfs_inode_item_committing,
  600. };
  601. /*
  602. * Initialize the inode log item for a newly allocated (in-core) inode.
  603. */
  604. void
  605. xfs_inode_item_init(
  606. struct xfs_inode *ip,
  607. struct xfs_mount *mp)
  608. {
  609. struct xfs_inode_log_item *iip;
  610. ASSERT(ip->i_itemp == NULL);
  611. iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
  612. GFP_KERNEL | __GFP_NOFAIL);
  613. iip->ili_inode = ip;
  614. spin_lock_init(&iip->ili_lock);
  615. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  616. &xfs_inode_item_ops);
  617. }
  618. /*
  619. * Free the inode log item and any memory hanging off of it.
  620. */
  621. void
  622. xfs_inode_item_destroy(
  623. struct xfs_inode *ip)
  624. {
  625. struct xfs_inode_log_item *iip = ip->i_itemp;
  626. ASSERT(iip->ili_item.li_buf == NULL);
  627. ip->i_itemp = NULL;
  628. kmem_free(iip->ili_item.li_lv_shadow);
  629. kmem_cache_free(xfs_ili_cache, iip);
  630. }
  631. /*
  632. * We only want to pull the item from the AIL if it is actually there
  633. * and its location in the log has not changed since we started the
  634. * flush. Thus, we only bother if the inode's lsn has not changed.
  635. */
  636. static void
  637. xfs_iflush_ail_updates(
  638. struct xfs_ail *ailp,
  639. struct list_head *list)
  640. {
  641. struct xfs_log_item *lip;
  642. xfs_lsn_t tail_lsn = 0;
  643. /* this is an opencoded batch version of xfs_trans_ail_delete */
  644. spin_lock(&ailp->ail_lock);
  645. list_for_each_entry(lip, list, li_bio_list) {
  646. xfs_lsn_t lsn;
  647. clear_bit(XFS_LI_FAILED, &lip->li_flags);
  648. if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
  649. continue;
  650. /*
  651. * dgc: Not sure how this happens, but it happens very
  652. * occassionaly via generic/388. xfs_iflush_abort() also
  653. * silently handles this same "under writeback but not in AIL at
  654. * shutdown" condition via xfs_trans_ail_delete().
  655. */
  656. if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
  657. ASSERT(xlog_is_shutdown(lip->li_log));
  658. continue;
  659. }
  660. lsn = xfs_ail_delete_one(ailp, lip);
  661. if (!tail_lsn && lsn)
  662. tail_lsn = lsn;
  663. }
  664. xfs_ail_update_finish(ailp, tail_lsn);
  665. }
  666. /*
  667. * Walk the list of inodes that have completed their IOs. If they are clean
  668. * remove them from the list and dissociate them from the buffer. Buffers that
  669. * are still dirty remain linked to the buffer and on the list. Caller must
  670. * handle them appropriately.
  671. */
  672. static void
  673. xfs_iflush_finish(
  674. struct xfs_buf *bp,
  675. struct list_head *list)
  676. {
  677. struct xfs_log_item *lip, *n;
  678. list_for_each_entry_safe(lip, n, list, li_bio_list) {
  679. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  680. bool drop_buffer = false;
  681. spin_lock(&iip->ili_lock);
  682. /*
  683. * Remove the reference to the cluster buffer if the inode is
  684. * clean in memory and drop the buffer reference once we've
  685. * dropped the locks we hold.
  686. */
  687. ASSERT(iip->ili_item.li_buf == bp);
  688. if (!iip->ili_fields) {
  689. iip->ili_item.li_buf = NULL;
  690. list_del_init(&lip->li_bio_list);
  691. drop_buffer = true;
  692. }
  693. iip->ili_last_fields = 0;
  694. iip->ili_flush_lsn = 0;
  695. spin_unlock(&iip->ili_lock);
  696. xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
  697. if (drop_buffer)
  698. xfs_buf_rele(bp);
  699. }
  700. }
  701. /*
  702. * Inode buffer IO completion routine. It is responsible for removing inodes
  703. * attached to the buffer from the AIL if they have not been re-logged and
  704. * completing the inode flush.
  705. */
  706. void
  707. xfs_buf_inode_iodone(
  708. struct xfs_buf *bp)
  709. {
  710. struct xfs_log_item *lip, *n;
  711. LIST_HEAD(flushed_inodes);
  712. LIST_HEAD(ail_updates);
  713. /*
  714. * Pull the attached inodes from the buffer one at a time and take the
  715. * appropriate action on them.
  716. */
  717. list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
  718. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  719. if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
  720. xfs_iflush_abort(iip->ili_inode);
  721. continue;
  722. }
  723. if (!iip->ili_last_fields)
  724. continue;
  725. /* Do an unlocked check for needing the AIL lock. */
  726. if (iip->ili_flush_lsn == lip->li_lsn ||
  727. test_bit(XFS_LI_FAILED, &lip->li_flags))
  728. list_move_tail(&lip->li_bio_list, &ail_updates);
  729. else
  730. list_move_tail(&lip->li_bio_list, &flushed_inodes);
  731. }
  732. if (!list_empty(&ail_updates)) {
  733. xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
  734. list_splice_tail(&ail_updates, &flushed_inodes);
  735. }
  736. xfs_iflush_finish(bp, &flushed_inodes);
  737. if (!list_empty(&flushed_inodes))
  738. list_splice_tail(&flushed_inodes, &bp->b_li_list);
  739. }
  740. void
  741. xfs_buf_inode_io_fail(
  742. struct xfs_buf *bp)
  743. {
  744. struct xfs_log_item *lip;
  745. list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
  746. set_bit(XFS_LI_FAILED, &lip->li_flags);
  747. }
  748. /*
  749. * Clear the inode logging fields so no more flushes are attempted. If we are
  750. * on a buffer list, it is now safe to remove it because the buffer is
  751. * guaranteed to be locked. The caller will drop the reference to the buffer
  752. * the log item held.
  753. */
  754. static void
  755. xfs_iflush_abort_clean(
  756. struct xfs_inode_log_item *iip)
  757. {
  758. iip->ili_last_fields = 0;
  759. iip->ili_fields = 0;
  760. iip->ili_fsync_fields = 0;
  761. iip->ili_flush_lsn = 0;
  762. iip->ili_item.li_buf = NULL;
  763. list_del_init(&iip->ili_item.li_bio_list);
  764. }
  765. /*
  766. * Abort flushing the inode from a context holding the cluster buffer locked.
  767. *
  768. * This is the normal runtime method of aborting writeback of an inode that is
  769. * attached to a cluster buffer. It occurs when the inode and the backing
  770. * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster
  771. * flushing or buffer IO completion encounters a log shutdown situation.
  772. *
  773. * If we need to abort inode writeback and we don't already hold the buffer
  774. * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be
  775. * necessary in a shutdown situation.
  776. */
  777. void
  778. xfs_iflush_abort(
  779. struct xfs_inode *ip)
  780. {
  781. struct xfs_inode_log_item *iip = ip->i_itemp;
  782. struct xfs_buf *bp;
  783. if (!iip) {
  784. /* clean inode, nothing to do */
  785. xfs_iflags_clear(ip, XFS_IFLUSHING);
  786. return;
  787. }
  788. /*
  789. * Remove the inode item from the AIL before we clear its internal
  790. * state. Whilst the inode is in the AIL, it should have a valid buffer
  791. * pointer for push operations to access - it is only safe to remove the
  792. * inode from the buffer once it has been removed from the AIL.
  793. *
  794. * We also clear the failed bit before removing the item from the AIL
  795. * as xfs_trans_ail_delete()->xfs_clear_li_failed() will release buffer
  796. * references the inode item owns and needs to hold until we've fully
  797. * aborted the inode log item and detached it from the buffer.
  798. */
  799. clear_bit(XFS_LI_FAILED, &iip->ili_item.li_flags);
  800. xfs_trans_ail_delete(&iip->ili_item, 0);
  801. /*
  802. * Grab the inode buffer so can we release the reference the inode log
  803. * item holds on it.
  804. */
  805. spin_lock(&iip->ili_lock);
  806. bp = iip->ili_item.li_buf;
  807. xfs_iflush_abort_clean(iip);
  808. spin_unlock(&iip->ili_lock);
  809. xfs_iflags_clear(ip, XFS_IFLUSHING);
  810. if (bp)
  811. xfs_buf_rele(bp);
  812. }
  813. /*
  814. * Abort an inode flush in the case of a shutdown filesystem. This can be called
  815. * from anywhere with just an inode reference and does not require holding the
  816. * inode cluster buffer locked. If the inode is attached to a cluster buffer,
  817. * it will grab and lock it safely, then abort the inode flush.
  818. */
  819. void
  820. xfs_iflush_shutdown_abort(
  821. struct xfs_inode *ip)
  822. {
  823. struct xfs_inode_log_item *iip = ip->i_itemp;
  824. struct xfs_buf *bp;
  825. if (!iip) {
  826. /* clean inode, nothing to do */
  827. xfs_iflags_clear(ip, XFS_IFLUSHING);
  828. return;
  829. }
  830. spin_lock(&iip->ili_lock);
  831. bp = iip->ili_item.li_buf;
  832. if (!bp) {
  833. spin_unlock(&iip->ili_lock);
  834. xfs_iflush_abort(ip);
  835. return;
  836. }
  837. /*
  838. * We have to take a reference to the buffer so that it doesn't get
  839. * freed when we drop the ili_lock and then wait to lock the buffer.
  840. * We'll clean up the extra reference after we pick up the ili_lock
  841. * again.
  842. */
  843. xfs_buf_hold(bp);
  844. spin_unlock(&iip->ili_lock);
  845. xfs_buf_lock(bp);
  846. spin_lock(&iip->ili_lock);
  847. if (!iip->ili_item.li_buf) {
  848. /*
  849. * Raced with another removal, hold the only reference
  850. * to bp now. Inode should not be in the AIL now, so just clean
  851. * up and return;
  852. */
  853. ASSERT(list_empty(&iip->ili_item.li_bio_list));
  854. ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags));
  855. xfs_iflush_abort_clean(iip);
  856. spin_unlock(&iip->ili_lock);
  857. xfs_iflags_clear(ip, XFS_IFLUSHING);
  858. xfs_buf_relse(bp);
  859. return;
  860. }
  861. /*
  862. * Got two references to bp. The first will get dropped by
  863. * xfs_iflush_abort() when the item is removed from the buffer list, but
  864. * we can't drop our reference until _abort() returns because we have to
  865. * unlock the buffer as well. Hence we abort and then unlock and release
  866. * our reference to the buffer.
  867. */
  868. ASSERT(iip->ili_item.li_buf == bp);
  869. spin_unlock(&iip->ili_lock);
  870. xfs_iflush_abort(ip);
  871. xfs_buf_relse(bp);
  872. }
  873. /*
  874. * convert an xfs_inode_log_format struct from the old 32 bit version
  875. * (which can have different field alignments) to the native 64 bit version
  876. */
  877. int
  878. xfs_inode_item_format_convert(
  879. struct xfs_log_iovec *buf,
  880. struct xfs_inode_log_format *in_f)
  881. {
  882. struct xfs_inode_log_format_32 *in_f32 = buf->i_addr;
  883. if (buf->i_len != sizeof(*in_f32)) {
  884. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
  885. return -EFSCORRUPTED;
  886. }
  887. in_f->ilf_type = in_f32->ilf_type;
  888. in_f->ilf_size = in_f32->ilf_size;
  889. in_f->ilf_fields = in_f32->ilf_fields;
  890. in_f->ilf_asize = in_f32->ilf_asize;
  891. in_f->ilf_dsize = in_f32->ilf_dsize;
  892. in_f->ilf_ino = in_f32->ilf_ino;
  893. memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
  894. in_f->ilf_blkno = in_f32->ilf_blkno;
  895. in_f->ilf_len = in_f32->ilf_len;
  896. in_f->ilf_boffset = in_f32->ilf_boffset;
  897. return 0;
  898. }