xfs_inode_item.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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_buf_item.h"
  31. #include "xfs_log.h"
  32. kmem_zone_t *xfs_ili_zone; /* inode log item zone */
  33. static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
  34. {
  35. return container_of(lip, struct xfs_inode_log_item, ili_item);
  36. }
  37. STATIC void
  38. xfs_inode_item_data_fork_size(
  39. struct xfs_inode_log_item *iip,
  40. int *nvecs,
  41. int *nbytes)
  42. {
  43. struct xfs_inode *ip = iip->ili_inode;
  44. switch (ip->i_d.di_format) {
  45. case XFS_DINODE_FMT_EXTENTS:
  46. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  47. ip->i_d.di_nextents > 0 &&
  48. ip->i_df.if_bytes > 0) {
  49. /* worst case, doesn't subtract delalloc extents */
  50. *nbytes += XFS_IFORK_DSIZE(ip);
  51. *nvecs += 1;
  52. }
  53. break;
  54. case XFS_DINODE_FMT_BTREE:
  55. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  56. ip->i_df.if_broot_bytes > 0) {
  57. *nbytes += ip->i_df.if_broot_bytes;
  58. *nvecs += 1;
  59. }
  60. break;
  61. case XFS_DINODE_FMT_LOCAL:
  62. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  63. ip->i_df.if_bytes > 0) {
  64. *nbytes += roundup(ip->i_df.if_bytes, 4);
  65. *nvecs += 1;
  66. }
  67. break;
  68. case XFS_DINODE_FMT_DEV:
  69. case XFS_DINODE_FMT_UUID:
  70. break;
  71. default:
  72. ASSERT(0);
  73. break;
  74. }
  75. }
  76. STATIC void
  77. xfs_inode_item_attr_fork_size(
  78. struct xfs_inode_log_item *iip,
  79. int *nvecs,
  80. int *nbytes)
  81. {
  82. struct xfs_inode *ip = iip->ili_inode;
  83. switch (ip->i_d.di_aformat) {
  84. case XFS_DINODE_FMT_EXTENTS:
  85. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  86. ip->i_d.di_anextents > 0 &&
  87. ip->i_afp->if_bytes > 0) {
  88. /* worst case, doesn't subtract unused space */
  89. *nbytes += XFS_IFORK_ASIZE(ip);
  90. *nvecs += 1;
  91. }
  92. break;
  93. case XFS_DINODE_FMT_BTREE:
  94. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  95. ip->i_afp->if_broot_bytes > 0) {
  96. *nbytes += ip->i_afp->if_broot_bytes;
  97. *nvecs += 1;
  98. }
  99. break;
  100. case XFS_DINODE_FMT_LOCAL:
  101. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  102. ip->i_afp->if_bytes > 0) {
  103. *nbytes += roundup(ip->i_afp->if_bytes, 4);
  104. *nvecs += 1;
  105. }
  106. break;
  107. default:
  108. ASSERT(0);
  109. break;
  110. }
  111. }
  112. /*
  113. * This returns the number of iovecs needed to log the given inode item.
  114. *
  115. * We need one iovec for the inode log format structure, one for the
  116. * inode core, and possibly one for the inode data/extents/b-tree root
  117. * and one for the inode attribute data/extents/b-tree root.
  118. */
  119. STATIC void
  120. xfs_inode_item_size(
  121. struct xfs_log_item *lip,
  122. int *nvecs,
  123. int *nbytes)
  124. {
  125. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  126. struct xfs_inode *ip = iip->ili_inode;
  127. *nvecs += 2;
  128. *nbytes += sizeof(struct xfs_inode_log_format) +
  129. xfs_log_dinode_size(ip->i_d.di_version);
  130. xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
  131. if (XFS_IFORK_Q(ip))
  132. xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
  133. }
  134. STATIC void
  135. xfs_inode_item_format_data_fork(
  136. struct xfs_inode_log_item *iip,
  137. struct xfs_inode_log_format *ilf,
  138. struct xfs_log_vec *lv,
  139. struct xfs_log_iovec **vecp)
  140. {
  141. struct xfs_inode *ip = iip->ili_inode;
  142. size_t data_bytes;
  143. switch (ip->i_d.di_format) {
  144. case XFS_DINODE_FMT_EXTENTS:
  145. iip->ili_fields &=
  146. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  147. XFS_ILOG_DEV | XFS_ILOG_UUID);
  148. if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  149. ip->i_d.di_nextents > 0 &&
  150. ip->i_df.if_bytes > 0) {
  151. struct xfs_bmbt_rec *p;
  152. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  153. ASSERT(xfs_iext_count(&ip->i_df) > 0);
  154. p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
  155. data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
  156. xlog_finish_iovec(lv, *vecp, data_bytes);
  157. ASSERT(data_bytes <= ip->i_df.if_bytes);
  158. ilf->ilf_dsize = data_bytes;
  159. ilf->ilf_size++;
  160. } else {
  161. iip->ili_fields &= ~XFS_ILOG_DEXT;
  162. }
  163. break;
  164. case XFS_DINODE_FMT_BTREE:
  165. iip->ili_fields &=
  166. ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  167. XFS_ILOG_DEV | XFS_ILOG_UUID);
  168. if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  169. ip->i_df.if_broot_bytes > 0) {
  170. ASSERT(ip->i_df.if_broot != NULL);
  171. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
  172. ip->i_df.if_broot,
  173. ip->i_df.if_broot_bytes);
  174. ilf->ilf_dsize = ip->i_df.if_broot_bytes;
  175. ilf->ilf_size++;
  176. } else {
  177. ASSERT(!(iip->ili_fields &
  178. XFS_ILOG_DBROOT));
  179. iip->ili_fields &= ~XFS_ILOG_DBROOT;
  180. }
  181. break;
  182. case XFS_DINODE_FMT_LOCAL:
  183. iip->ili_fields &=
  184. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
  185. XFS_ILOG_DEV | XFS_ILOG_UUID);
  186. if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  187. ip->i_df.if_bytes > 0) {
  188. /*
  189. * Round i_bytes up to a word boundary.
  190. * The underlying memory is guaranteed to
  191. * to be there by xfs_idata_realloc().
  192. */
  193. data_bytes = roundup(ip->i_df.if_bytes, 4);
  194. ASSERT(ip->i_df.if_real_bytes == 0 ||
  195. ip->i_df.if_real_bytes >= data_bytes);
  196. ASSERT(ip->i_df.if_u1.if_data != NULL);
  197. ASSERT(ip->i_d.di_size > 0);
  198. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
  199. ip->i_df.if_u1.if_data, data_bytes);
  200. ilf->ilf_dsize = (unsigned)data_bytes;
  201. ilf->ilf_size++;
  202. } else {
  203. iip->ili_fields &= ~XFS_ILOG_DDATA;
  204. }
  205. break;
  206. case XFS_DINODE_FMT_DEV:
  207. iip->ili_fields &=
  208. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  209. XFS_ILOG_DEXT | XFS_ILOG_UUID);
  210. if (iip->ili_fields & XFS_ILOG_DEV)
  211. ilf->ilf_u.ilfu_rdev = ip->i_df.if_u2.if_rdev;
  212. break;
  213. case XFS_DINODE_FMT_UUID:
  214. iip->ili_fields &=
  215. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  216. XFS_ILOG_DEXT | XFS_ILOG_DEV);
  217. if (iip->ili_fields & XFS_ILOG_UUID)
  218. ilf->ilf_u.ilfu_uuid = ip->i_df.if_u2.if_uuid;
  219. break;
  220. default:
  221. ASSERT(0);
  222. break;
  223. }
  224. }
  225. STATIC void
  226. xfs_inode_item_format_attr_fork(
  227. struct xfs_inode_log_item *iip,
  228. struct xfs_inode_log_format *ilf,
  229. struct xfs_log_vec *lv,
  230. struct xfs_log_iovec **vecp)
  231. {
  232. struct xfs_inode *ip = iip->ili_inode;
  233. size_t data_bytes;
  234. switch (ip->i_d.di_aformat) {
  235. case XFS_DINODE_FMT_EXTENTS:
  236. iip->ili_fields &=
  237. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  238. if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  239. ip->i_d.di_anextents > 0 &&
  240. ip->i_afp->if_bytes > 0) {
  241. struct xfs_bmbt_rec *p;
  242. ASSERT(xfs_iext_count(ip->i_afp) ==
  243. ip->i_d.di_anextents);
  244. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  245. p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
  246. data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
  247. xlog_finish_iovec(lv, *vecp, data_bytes);
  248. ilf->ilf_asize = data_bytes;
  249. ilf->ilf_size++;
  250. } else {
  251. iip->ili_fields &= ~XFS_ILOG_AEXT;
  252. }
  253. break;
  254. case XFS_DINODE_FMT_BTREE:
  255. iip->ili_fields &=
  256. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  257. if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
  258. ip->i_afp->if_broot_bytes > 0) {
  259. ASSERT(ip->i_afp->if_broot != NULL);
  260. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
  261. ip->i_afp->if_broot,
  262. ip->i_afp->if_broot_bytes);
  263. ilf->ilf_asize = ip->i_afp->if_broot_bytes;
  264. ilf->ilf_size++;
  265. } else {
  266. iip->ili_fields &= ~XFS_ILOG_ABROOT;
  267. }
  268. break;
  269. case XFS_DINODE_FMT_LOCAL:
  270. iip->ili_fields &=
  271. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  272. if ((iip->ili_fields & XFS_ILOG_ADATA) &&
  273. ip->i_afp->if_bytes > 0) {
  274. /*
  275. * Round i_bytes up to a word boundary.
  276. * The underlying memory is guaranteed to
  277. * to be there by xfs_idata_realloc().
  278. */
  279. data_bytes = roundup(ip->i_afp->if_bytes, 4);
  280. ASSERT(ip->i_afp->if_real_bytes == 0 ||
  281. ip->i_afp->if_real_bytes >= data_bytes);
  282. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  283. xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
  284. ip->i_afp->if_u1.if_data,
  285. data_bytes);
  286. ilf->ilf_asize = (unsigned)data_bytes;
  287. ilf->ilf_size++;
  288. } else {
  289. iip->ili_fields &= ~XFS_ILOG_ADATA;
  290. }
  291. break;
  292. default:
  293. ASSERT(0);
  294. break;
  295. }
  296. }
  297. static void
  298. xfs_inode_to_log_dinode(
  299. struct xfs_inode *ip,
  300. struct xfs_log_dinode *to,
  301. xfs_lsn_t lsn)
  302. {
  303. struct xfs_icdinode *from = &ip->i_d;
  304. struct inode *inode = VFS_I(ip);
  305. to->di_magic = XFS_DINODE_MAGIC;
  306. to->di_version = from->di_version;
  307. to->di_format = from->di_format;
  308. to->di_uid = from->di_uid;
  309. to->di_gid = from->di_gid;
  310. to->di_projid_lo = from->di_projid_lo;
  311. to->di_projid_hi = from->di_projid_hi;
  312. memset(to->di_pad, 0, sizeof(to->di_pad));
  313. memset(to->di_pad3, 0, sizeof(to->di_pad3));
  314. to->di_atime.t_sec = inode->i_atime.tv_sec;
  315. to->di_atime.t_nsec = inode->i_atime.tv_nsec;
  316. to->di_mtime.t_sec = inode->i_mtime.tv_sec;
  317. to->di_mtime.t_nsec = inode->i_mtime.tv_nsec;
  318. to->di_ctime.t_sec = inode->i_ctime.tv_sec;
  319. to->di_ctime.t_nsec = inode->i_ctime.tv_nsec;
  320. to->di_nlink = inode->i_nlink;
  321. to->di_gen = inode->i_generation;
  322. to->di_mode = inode->i_mode;
  323. to->di_size = from->di_size;
  324. to->di_nblocks = from->di_nblocks;
  325. to->di_extsize = from->di_extsize;
  326. to->di_nextents = from->di_nextents;
  327. to->di_anextents = from->di_anextents;
  328. to->di_forkoff = from->di_forkoff;
  329. to->di_aformat = from->di_aformat;
  330. to->di_dmevmask = from->di_dmevmask;
  331. to->di_dmstate = from->di_dmstate;
  332. to->di_flags = from->di_flags;
  333. /* log a dummy value to ensure log structure is fully initialised */
  334. to->di_next_unlinked = NULLAGINO;
  335. if (from->di_version == 3) {
  336. to->di_changecount = inode->i_version;
  337. to->di_crtime.t_sec = from->di_crtime.t_sec;
  338. to->di_crtime.t_nsec = from->di_crtime.t_nsec;
  339. to->di_flags2 = from->di_flags2;
  340. to->di_cowextsize = from->di_cowextsize;
  341. to->di_ino = ip->i_ino;
  342. to->di_lsn = lsn;
  343. memset(to->di_pad2, 0, sizeof(to->di_pad2));
  344. uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
  345. to->di_flushiter = 0;
  346. } else {
  347. to->di_flushiter = from->di_flushiter;
  348. }
  349. }
  350. /*
  351. * Format the inode core. Current timestamp data is only in the VFS inode
  352. * fields, so we need to grab them from there. Hence rather than just copying
  353. * the XFS inode core structure, format the fields directly into the iovec.
  354. */
  355. static void
  356. xfs_inode_item_format_core(
  357. struct xfs_inode *ip,
  358. struct xfs_log_vec *lv,
  359. struct xfs_log_iovec **vecp)
  360. {
  361. struct xfs_log_dinode *dic;
  362. dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
  363. xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
  364. xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_d.di_version));
  365. }
  366. /*
  367. * This is called to fill in the vector of log iovecs for the given inode
  368. * log item. It fills the first item with an inode log format structure,
  369. * the second with the on-disk inode structure, and a possible third and/or
  370. * fourth with the inode data/extents/b-tree root and inode attributes
  371. * data/extents/b-tree root.
  372. *
  373. * Note: Always use the 64 bit inode log format structure so we don't
  374. * leave an uninitialised hole in the format item on 64 bit systems. Log
  375. * recovery on 32 bit systems handles this just fine, so there's no reason
  376. * for not using an initialising the properly padded structure all the time.
  377. */
  378. STATIC void
  379. xfs_inode_item_format(
  380. struct xfs_log_item *lip,
  381. struct xfs_log_vec *lv)
  382. {
  383. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  384. struct xfs_inode *ip = iip->ili_inode;
  385. struct xfs_log_iovec *vecp = NULL;
  386. struct xfs_inode_log_format *ilf;
  387. ASSERT(ip->i_d.di_version > 1);
  388. ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
  389. ilf->ilf_type = XFS_LI_INODE;
  390. ilf->ilf_ino = ip->i_ino;
  391. ilf->ilf_blkno = ip->i_imap.im_blkno;
  392. ilf->ilf_len = ip->i_imap.im_len;
  393. ilf->ilf_boffset = ip->i_imap.im_boffset;
  394. ilf->ilf_fields = XFS_ILOG_CORE;
  395. ilf->ilf_size = 2; /* format + core */
  396. /*
  397. * make sure we don't leak uninitialised data into the log in the case
  398. * when we don't log every field in the inode.
  399. */
  400. ilf->ilf_dsize = 0;
  401. ilf->ilf_asize = 0;
  402. ilf->ilf_pad = 0;
  403. memset(&ilf->ilf_u.ilfu_uuid, 0, sizeof(ilf->ilf_u.ilfu_uuid));
  404. xlog_finish_iovec(lv, vecp, sizeof(*ilf));
  405. xfs_inode_item_format_core(ip, lv, &vecp);
  406. xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
  407. if (XFS_IFORK_Q(ip)) {
  408. xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
  409. } else {
  410. iip->ili_fields &=
  411. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  412. }
  413. /* update the format with the exact fields we actually logged */
  414. ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
  415. }
  416. /*
  417. * This is called to pin the inode associated with the inode log
  418. * item in memory so it cannot be written out.
  419. */
  420. STATIC void
  421. xfs_inode_item_pin(
  422. struct xfs_log_item *lip)
  423. {
  424. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  425. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  426. trace_xfs_inode_pin(ip, _RET_IP_);
  427. atomic_inc(&ip->i_pincount);
  428. }
  429. /*
  430. * This is called to unpin the inode associated with the inode log
  431. * item which was previously pinned with a call to xfs_inode_item_pin().
  432. *
  433. * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
  434. */
  435. STATIC void
  436. xfs_inode_item_unpin(
  437. struct xfs_log_item *lip,
  438. int remove)
  439. {
  440. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  441. trace_xfs_inode_unpin(ip, _RET_IP_);
  442. ASSERT(atomic_read(&ip->i_pincount) > 0);
  443. if (atomic_dec_and_test(&ip->i_pincount))
  444. wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
  445. }
  446. /*
  447. * Callback used to mark a buffer with XFS_LI_FAILED when items in the buffer
  448. * have been failed during writeback
  449. *
  450. * This informs the AIL that the inode is already flush locked on the next push,
  451. * and acquires a hold on the buffer to ensure that it isn't reclaimed before
  452. * dirty data makes it to disk.
  453. */
  454. STATIC void
  455. xfs_inode_item_error(
  456. struct xfs_log_item *lip,
  457. struct xfs_buf *bp)
  458. {
  459. ASSERT(xfs_isiflocked(INODE_ITEM(lip)->ili_inode));
  460. xfs_set_li_failed(lip, bp);
  461. }
  462. STATIC uint
  463. xfs_inode_item_push(
  464. struct xfs_log_item *lip,
  465. struct list_head *buffer_list)
  466. __releases(&lip->li_ailp->xa_lock)
  467. __acquires(&lip->li_ailp->xa_lock)
  468. {
  469. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  470. struct xfs_inode *ip = iip->ili_inode;
  471. struct xfs_buf *bp = lip->li_buf;
  472. uint rval = XFS_ITEM_SUCCESS;
  473. int error;
  474. if (xfs_ipincount(ip) > 0)
  475. return XFS_ITEM_PINNED;
  476. /*
  477. * The buffer containing this item failed to be written back
  478. * previously. Resubmit the buffer for IO.
  479. */
  480. if (lip->li_flags & XFS_LI_FAILED) {
  481. if (!xfs_buf_trylock(bp))
  482. return XFS_ITEM_LOCKED;
  483. if (!xfs_buf_resubmit_failed_buffers(bp, lip, buffer_list))
  484. rval = XFS_ITEM_FLUSHING;
  485. xfs_buf_unlock(bp);
  486. return rval;
  487. }
  488. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
  489. return XFS_ITEM_LOCKED;
  490. /*
  491. * Re-check the pincount now that we stabilized the value by
  492. * taking the ilock.
  493. */
  494. if (xfs_ipincount(ip) > 0) {
  495. rval = XFS_ITEM_PINNED;
  496. goto out_unlock;
  497. }
  498. /*
  499. * Stale inode items should force out the iclog.
  500. */
  501. if (ip->i_flags & XFS_ISTALE) {
  502. rval = XFS_ITEM_PINNED;
  503. goto out_unlock;
  504. }
  505. /*
  506. * Someone else is already flushing the inode. Nothing we can do
  507. * here but wait for the flush to finish and remove the item from
  508. * the AIL.
  509. */
  510. if (!xfs_iflock_nowait(ip)) {
  511. rval = XFS_ITEM_FLUSHING;
  512. goto out_unlock;
  513. }
  514. ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  515. ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  516. spin_unlock(&lip->li_ailp->xa_lock);
  517. error = xfs_iflush(ip, &bp);
  518. if (!error) {
  519. if (!xfs_buf_delwri_queue(bp, buffer_list))
  520. rval = XFS_ITEM_FLUSHING;
  521. xfs_buf_relse(bp);
  522. }
  523. spin_lock(&lip->li_ailp->xa_lock);
  524. out_unlock:
  525. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  526. return rval;
  527. }
  528. /*
  529. * Unlock the inode associated with the inode log item.
  530. * Clear the fields of the inode and inode log item that
  531. * are specific to the current transaction. If the
  532. * hold flags is set, do not unlock the inode.
  533. */
  534. STATIC void
  535. xfs_inode_item_unlock(
  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. /*
  584. * XXX rcc - this one really has to do something. Probably needs
  585. * to stamp in a new field in the incore inode.
  586. */
  587. STATIC void
  588. xfs_inode_item_committing(
  589. struct xfs_log_item *lip,
  590. xfs_lsn_t lsn)
  591. {
  592. INODE_ITEM(lip)->ili_last_lsn = lsn;
  593. }
  594. /*
  595. * This is the ops vector shared by all buf log items.
  596. */
  597. static const struct xfs_item_ops xfs_inode_item_ops = {
  598. .iop_size = xfs_inode_item_size,
  599. .iop_format = xfs_inode_item_format,
  600. .iop_pin = xfs_inode_item_pin,
  601. .iop_unpin = xfs_inode_item_unpin,
  602. .iop_unlock = xfs_inode_item_unlock,
  603. .iop_committed = xfs_inode_item_committed,
  604. .iop_push = xfs_inode_item_push,
  605. .iop_committing = xfs_inode_item_committing,
  606. .iop_error = xfs_inode_item_error
  607. };
  608. /*
  609. * Initialize the inode log item for a newly allocated (in-core) inode.
  610. */
  611. void
  612. xfs_inode_item_init(
  613. struct xfs_inode *ip,
  614. struct xfs_mount *mp)
  615. {
  616. struct xfs_inode_log_item *iip;
  617. ASSERT(ip->i_itemp == NULL);
  618. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  619. iip->ili_inode = ip;
  620. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  621. &xfs_inode_item_ops);
  622. }
  623. /*
  624. * Free the inode log item and any memory hanging off of it.
  625. */
  626. void
  627. xfs_inode_item_destroy(
  628. xfs_inode_t *ip)
  629. {
  630. kmem_free(ip->i_itemp->ili_item.li_lv_shadow);
  631. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  632. }
  633. /*
  634. * This is the inode flushing I/O completion routine. It is called
  635. * from interrupt level when the buffer containing the inode is
  636. * flushed to disk. It is responsible for removing the inode item
  637. * from the AIL if it has not been re-logged, and unlocking the inode's
  638. * flush lock.
  639. *
  640. * To reduce AIL lock traffic as much as possible, we scan the buffer log item
  641. * list for other inodes that will run this function. We remove them from the
  642. * buffer list so we can process all the inode IO completions in one AIL lock
  643. * traversal.
  644. */
  645. void
  646. xfs_iflush_done(
  647. struct xfs_buf *bp,
  648. struct xfs_log_item *lip)
  649. {
  650. struct xfs_inode_log_item *iip;
  651. struct xfs_log_item *blip;
  652. struct xfs_log_item *next;
  653. struct xfs_log_item *prev;
  654. struct xfs_ail *ailp = lip->li_ailp;
  655. int need_ail = 0;
  656. /*
  657. * Scan the buffer IO completions for other inodes being completed and
  658. * attach them to the current inode log item.
  659. */
  660. blip = bp->b_fspriv;
  661. prev = NULL;
  662. while (blip != NULL) {
  663. if (blip->li_cb != xfs_iflush_done) {
  664. prev = blip;
  665. blip = blip->li_bio_list;
  666. continue;
  667. }
  668. /* remove from list */
  669. next = blip->li_bio_list;
  670. if (!prev) {
  671. bp->b_fspriv = next;
  672. } else {
  673. prev->li_bio_list = next;
  674. }
  675. /* add to current list */
  676. blip->li_bio_list = lip->li_bio_list;
  677. lip->li_bio_list = blip;
  678. /*
  679. * while we have the item, do the unlocked check for needing
  680. * the AIL lock.
  681. */
  682. iip = INODE_ITEM(blip);
  683. if ((iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn) ||
  684. (blip->li_flags & XFS_LI_FAILED))
  685. need_ail++;
  686. blip = next;
  687. }
  688. /* make sure we capture the state of the initial inode. */
  689. iip = INODE_ITEM(lip);
  690. if ((iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn) ||
  691. lip->li_flags & XFS_LI_FAILED)
  692. need_ail++;
  693. /*
  694. * We only want to pull the item from the AIL if it is
  695. * actually there and its location in the log has not
  696. * changed since we started the flush. Thus, we only bother
  697. * if the ili_logged flag is set and the inode's lsn has not
  698. * changed. First we check the lsn outside
  699. * the lock since it's cheaper, and then we recheck while
  700. * holding the lock before removing the inode from the AIL.
  701. */
  702. if (need_ail) {
  703. bool mlip_changed = false;
  704. /* this is an opencoded batch version of xfs_trans_ail_delete */
  705. spin_lock(&ailp->xa_lock);
  706. for (blip = lip; blip; blip = blip->li_bio_list) {
  707. if (INODE_ITEM(blip)->ili_logged &&
  708. blip->li_lsn == INODE_ITEM(blip)->ili_flush_lsn)
  709. mlip_changed |= xfs_ail_delete_one(ailp, blip);
  710. else {
  711. xfs_clear_li_failed(blip);
  712. }
  713. }
  714. if (mlip_changed) {
  715. if (!XFS_FORCED_SHUTDOWN(ailp->xa_mount))
  716. xlog_assign_tail_lsn_locked(ailp->xa_mount);
  717. if (list_empty(&ailp->xa_ail))
  718. wake_up_all(&ailp->xa_empty);
  719. }
  720. spin_unlock(&ailp->xa_lock);
  721. if (mlip_changed)
  722. xfs_log_space_wake(ailp->xa_mount);
  723. }
  724. /*
  725. * clean up and unlock the flush lock now we are done. We can clear the
  726. * ili_last_fields bits now that we know that the data corresponding to
  727. * them is safely on disk.
  728. */
  729. for (blip = lip; blip; blip = next) {
  730. next = blip->li_bio_list;
  731. blip->li_bio_list = NULL;
  732. iip = INODE_ITEM(blip);
  733. iip->ili_logged = 0;
  734. iip->ili_last_fields = 0;
  735. xfs_ifunlock(iip->ili_inode);
  736. }
  737. }
  738. /*
  739. * This is the inode flushing abort routine. It is called from xfs_iflush when
  740. * the filesystem is shutting down to clean up the inode state. It is
  741. * responsible for removing the inode item from the AIL if it has not been
  742. * re-logged, and unlocking the inode's flush lock.
  743. */
  744. void
  745. xfs_iflush_abort(
  746. xfs_inode_t *ip,
  747. bool stale)
  748. {
  749. xfs_inode_log_item_t *iip = ip->i_itemp;
  750. if (iip) {
  751. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  752. xfs_trans_ail_remove(&iip->ili_item,
  753. stale ? SHUTDOWN_LOG_IO_ERROR :
  754. SHUTDOWN_CORRUPT_INCORE);
  755. }
  756. iip->ili_logged = 0;
  757. /*
  758. * Clear the ili_last_fields bits now that we know that the
  759. * data corresponding to them is safely on disk.
  760. */
  761. iip->ili_last_fields = 0;
  762. /*
  763. * Clear the inode logging fields so no more flushes are
  764. * attempted.
  765. */
  766. iip->ili_fields = 0;
  767. iip->ili_fsync_fields = 0;
  768. }
  769. /*
  770. * Release the inode's flush lock since we're done with it.
  771. */
  772. xfs_ifunlock(ip);
  773. }
  774. void
  775. xfs_istale_done(
  776. struct xfs_buf *bp,
  777. struct xfs_log_item *lip)
  778. {
  779. xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
  780. }
  781. /*
  782. * convert an xfs_inode_log_format struct from the old 32 bit version
  783. * (which can have different field alignments) to the native 64 bit version
  784. */
  785. int
  786. xfs_inode_item_format_convert(
  787. struct xfs_log_iovec *buf,
  788. struct xfs_inode_log_format *in_f)
  789. {
  790. struct xfs_inode_log_format_32 *in_f32 = buf->i_addr;
  791. if (buf->i_len != sizeof(*in_f32))
  792. return -EFSCORRUPTED;
  793. in_f->ilf_type = in_f32->ilf_type;
  794. in_f->ilf_size = in_f32->ilf_size;
  795. in_f->ilf_fields = in_f32->ilf_fields;
  796. in_f->ilf_asize = in_f32->ilf_asize;
  797. in_f->ilf_dsize = in_f32->ilf_dsize;
  798. in_f->ilf_ino = in_f32->ilf_ino;
  799. /* copy biggest field of ilf_u */
  800. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  801. in_f32->ilf_u.ilfu_uuid.__u_bits, sizeof(uuid_t));
  802. in_f->ilf_blkno = in_f32->ilf_blkno;
  803. in_f->ilf_len = in_f32->ilf_len;
  804. in_f->ilf_boffset = in_f32->ilf_boffset;
  805. return 0;
  806. }