xfs_inode_item.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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_log_dinode_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(xfs_iext_count(&ip->i_df) > 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(xfs_iext_count(ip->i_afp) ==
  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. static void
  297. xfs_inode_to_log_dinode(
  298. struct xfs_inode *ip,
  299. struct xfs_log_dinode *to,
  300. xfs_lsn_t lsn)
  301. {
  302. struct xfs_icdinode *from = &ip->i_d;
  303. struct inode *inode = VFS_I(ip);
  304. to->di_magic = XFS_DINODE_MAGIC;
  305. to->di_version = from->di_version;
  306. to->di_format = from->di_format;
  307. to->di_uid = from->di_uid;
  308. to->di_gid = from->di_gid;
  309. to->di_projid_lo = from->di_projid_lo;
  310. to->di_projid_hi = from->di_projid_hi;
  311. memset(to->di_pad, 0, sizeof(to->di_pad));
  312. memset(to->di_pad3, 0, sizeof(to->di_pad3));
  313. to->di_atime.t_sec = inode->i_atime.tv_sec;
  314. to->di_atime.t_nsec = inode->i_atime.tv_nsec;
  315. to->di_mtime.t_sec = inode->i_mtime.tv_sec;
  316. to->di_mtime.t_nsec = inode->i_mtime.tv_nsec;
  317. to->di_ctime.t_sec = inode->i_ctime.tv_sec;
  318. to->di_ctime.t_nsec = inode->i_ctime.tv_nsec;
  319. to->di_nlink = inode->i_nlink;
  320. to->di_gen = inode->i_generation;
  321. to->di_mode = inode->i_mode;
  322. to->di_size = from->di_size;
  323. to->di_nblocks = from->di_nblocks;
  324. to->di_extsize = from->di_extsize;
  325. to->di_nextents = from->di_nextents;
  326. to->di_anextents = from->di_anextents;
  327. to->di_forkoff = from->di_forkoff;
  328. to->di_aformat = from->di_aformat;
  329. to->di_dmevmask = from->di_dmevmask;
  330. to->di_dmstate = from->di_dmstate;
  331. to->di_flags = from->di_flags;
  332. if (from->di_version == 3) {
  333. to->di_changecount = inode->i_version;
  334. to->di_crtime.t_sec = from->di_crtime.t_sec;
  335. to->di_crtime.t_nsec = from->di_crtime.t_nsec;
  336. to->di_flags2 = from->di_flags2;
  337. to->di_cowextsize = from->di_cowextsize;
  338. to->di_ino = ip->i_ino;
  339. to->di_lsn = lsn;
  340. memset(to->di_pad2, 0, sizeof(to->di_pad2));
  341. uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
  342. to->di_flushiter = 0;
  343. } else {
  344. to->di_flushiter = from->di_flushiter;
  345. }
  346. }
  347. /*
  348. * Format the inode core. Current timestamp data is only in the VFS inode
  349. * fields, so we need to grab them from there. Hence rather than just copying
  350. * the XFS inode core structure, format the fields directly into the iovec.
  351. */
  352. static void
  353. xfs_inode_item_format_core(
  354. struct xfs_inode *ip,
  355. struct xfs_log_vec *lv,
  356. struct xfs_log_iovec **vecp)
  357. {
  358. struct xfs_log_dinode *dic;
  359. dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
  360. xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
  361. xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_d.di_version));
  362. }
  363. /*
  364. * This is called to fill in the vector of log iovecs for the given inode
  365. * log item. It fills the first item with an inode log format structure,
  366. * the second with the on-disk inode structure, and a possible third and/or
  367. * fourth with the inode data/extents/b-tree root and inode attributes
  368. * data/extents/b-tree root.
  369. */
  370. STATIC void
  371. xfs_inode_item_format(
  372. struct xfs_log_item *lip,
  373. struct xfs_log_vec *lv)
  374. {
  375. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  376. struct xfs_inode *ip = iip->ili_inode;
  377. struct xfs_inode_log_format *ilf;
  378. struct xfs_log_iovec *vecp = NULL;
  379. ASSERT(ip->i_d.di_version > 1);
  380. ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
  381. ilf->ilf_type = XFS_LI_INODE;
  382. ilf->ilf_ino = ip->i_ino;
  383. ilf->ilf_blkno = ip->i_imap.im_blkno;
  384. ilf->ilf_len = ip->i_imap.im_len;
  385. ilf->ilf_boffset = ip->i_imap.im_boffset;
  386. ilf->ilf_fields = XFS_ILOG_CORE;
  387. ilf->ilf_size = 2; /* format + core */
  388. xlog_finish_iovec(lv, vecp, sizeof(struct xfs_inode_log_format));
  389. xfs_inode_item_format_core(ip, lv, &vecp);
  390. xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
  391. if (XFS_IFORK_Q(ip)) {
  392. xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
  393. } else {
  394. iip->ili_fields &=
  395. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  396. }
  397. /* update the format with the exact fields we actually logged */
  398. ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
  399. }
  400. /*
  401. * This is called to pin the inode associated with the inode log
  402. * item in memory so it cannot be written out.
  403. */
  404. STATIC void
  405. xfs_inode_item_pin(
  406. struct xfs_log_item *lip)
  407. {
  408. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  409. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  410. trace_xfs_inode_pin(ip, _RET_IP_);
  411. atomic_inc(&ip->i_pincount);
  412. }
  413. /*
  414. * This is called to unpin the inode associated with the inode log
  415. * item which was previously pinned with a call to xfs_inode_item_pin().
  416. *
  417. * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
  418. */
  419. STATIC void
  420. xfs_inode_item_unpin(
  421. struct xfs_log_item *lip,
  422. int remove)
  423. {
  424. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  425. trace_xfs_inode_unpin(ip, _RET_IP_);
  426. ASSERT(atomic_read(&ip->i_pincount) > 0);
  427. if (atomic_dec_and_test(&ip->i_pincount))
  428. wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
  429. }
  430. STATIC uint
  431. xfs_inode_item_push(
  432. struct xfs_log_item *lip,
  433. struct list_head *buffer_list)
  434. __releases(&lip->li_ailp->xa_lock)
  435. __acquires(&lip->li_ailp->xa_lock)
  436. {
  437. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  438. struct xfs_inode *ip = iip->ili_inode;
  439. struct xfs_buf *bp = NULL;
  440. uint rval = XFS_ITEM_SUCCESS;
  441. int error;
  442. if (xfs_ipincount(ip) > 0)
  443. return XFS_ITEM_PINNED;
  444. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
  445. return XFS_ITEM_LOCKED;
  446. /*
  447. * Re-check the pincount now that we stabilized the value by
  448. * taking the ilock.
  449. */
  450. if (xfs_ipincount(ip) > 0) {
  451. rval = XFS_ITEM_PINNED;
  452. goto out_unlock;
  453. }
  454. /*
  455. * Stale inode items should force out the iclog.
  456. */
  457. if (ip->i_flags & XFS_ISTALE) {
  458. rval = XFS_ITEM_PINNED;
  459. goto out_unlock;
  460. }
  461. /*
  462. * Someone else is already flushing the inode. Nothing we can do
  463. * here but wait for the flush to finish and remove the item from
  464. * the AIL.
  465. */
  466. if (!xfs_iflock_nowait(ip)) {
  467. rval = XFS_ITEM_FLUSHING;
  468. goto out_unlock;
  469. }
  470. ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  471. ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
  472. spin_unlock(&lip->li_ailp->xa_lock);
  473. error = xfs_iflush(ip, &bp);
  474. if (!error) {
  475. if (!xfs_buf_delwri_queue(bp, buffer_list))
  476. rval = XFS_ITEM_FLUSHING;
  477. xfs_buf_relse(bp);
  478. }
  479. spin_lock(&lip->li_ailp->xa_lock);
  480. out_unlock:
  481. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  482. return rval;
  483. }
  484. /*
  485. * Unlock the inode associated with the inode log item.
  486. * Clear the fields of the inode and inode log item that
  487. * are specific to the current transaction. If the
  488. * hold flags is set, do not unlock the inode.
  489. */
  490. STATIC void
  491. xfs_inode_item_unlock(
  492. struct xfs_log_item *lip)
  493. {
  494. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  495. struct xfs_inode *ip = iip->ili_inode;
  496. unsigned short lock_flags;
  497. ASSERT(ip->i_itemp != NULL);
  498. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  499. lock_flags = iip->ili_lock_flags;
  500. iip->ili_lock_flags = 0;
  501. if (lock_flags)
  502. xfs_iunlock(ip, lock_flags);
  503. }
  504. /*
  505. * This is called to find out where the oldest active copy of the inode log
  506. * item in the on disk log resides now that the last log write of it completed
  507. * at the given lsn. Since we always re-log all dirty data in an inode, the
  508. * latest copy in the on disk log is the only one that matters. Therefore,
  509. * simply return the given lsn.
  510. *
  511. * If the inode has been marked stale because the cluster is being freed, we
  512. * don't want to (re-)insert this inode into the AIL. There is a race condition
  513. * where the cluster buffer may be unpinned before the inode is inserted into
  514. * the AIL during transaction committed processing. If the buffer is unpinned
  515. * before the inode item has been committed and inserted, then it is possible
  516. * for the buffer to be written and IO completes before the inode is inserted
  517. * into the AIL. In that case, we'd be inserting a clean, stale inode into the
  518. * AIL which will never get removed. It will, however, get reclaimed which
  519. * triggers an assert in xfs_inode_free() complaining about freein an inode
  520. * still in the AIL.
  521. *
  522. * To avoid this, just unpin the inode directly and return a LSN of -1 so the
  523. * transaction committed code knows that it does not need to do any further
  524. * processing on the item.
  525. */
  526. STATIC xfs_lsn_t
  527. xfs_inode_item_committed(
  528. struct xfs_log_item *lip,
  529. xfs_lsn_t lsn)
  530. {
  531. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  532. struct xfs_inode *ip = iip->ili_inode;
  533. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  534. xfs_inode_item_unpin(lip, 0);
  535. return -1;
  536. }
  537. return lsn;
  538. }
  539. /*
  540. * XXX rcc - this one really has to do something. Probably needs
  541. * to stamp in a new field in the incore inode.
  542. */
  543. STATIC void
  544. xfs_inode_item_committing(
  545. struct xfs_log_item *lip,
  546. xfs_lsn_t lsn)
  547. {
  548. INODE_ITEM(lip)->ili_last_lsn = lsn;
  549. }
  550. /*
  551. * This is the ops vector shared by all buf log items.
  552. */
  553. static const struct xfs_item_ops xfs_inode_item_ops = {
  554. .iop_size = xfs_inode_item_size,
  555. .iop_format = xfs_inode_item_format,
  556. .iop_pin = xfs_inode_item_pin,
  557. .iop_unpin = xfs_inode_item_unpin,
  558. .iop_unlock = xfs_inode_item_unlock,
  559. .iop_committed = xfs_inode_item_committed,
  560. .iop_push = xfs_inode_item_push,
  561. .iop_committing = xfs_inode_item_committing
  562. };
  563. /*
  564. * Initialize the inode log item for a newly allocated (in-core) inode.
  565. */
  566. void
  567. xfs_inode_item_init(
  568. struct xfs_inode *ip,
  569. struct xfs_mount *mp)
  570. {
  571. struct xfs_inode_log_item *iip;
  572. ASSERT(ip->i_itemp == NULL);
  573. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  574. iip->ili_inode = ip;
  575. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  576. &xfs_inode_item_ops);
  577. }
  578. /*
  579. * Free the inode log item and any memory hanging off of it.
  580. */
  581. void
  582. xfs_inode_item_destroy(
  583. xfs_inode_t *ip)
  584. {
  585. kmem_free(ip->i_itemp->ili_item.li_lv_shadow);
  586. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  587. }
  588. /*
  589. * This is the inode flushing I/O completion routine. It is called
  590. * from interrupt level when the buffer containing the inode is
  591. * flushed to disk. It is responsible for removing the inode item
  592. * from the AIL if it has not been re-logged, and unlocking the inode's
  593. * flush lock.
  594. *
  595. * To reduce AIL lock traffic as much as possible, we scan the buffer log item
  596. * list for other inodes that will run this function. We remove them from the
  597. * buffer list so we can process all the inode IO completions in one AIL lock
  598. * traversal.
  599. */
  600. void
  601. xfs_iflush_done(
  602. struct xfs_buf *bp,
  603. struct xfs_log_item *lip)
  604. {
  605. struct xfs_inode_log_item *iip;
  606. struct xfs_log_item *blip;
  607. struct xfs_log_item *next;
  608. struct xfs_log_item *prev;
  609. struct xfs_ail *ailp = lip->li_ailp;
  610. int need_ail = 0;
  611. /*
  612. * Scan the buffer IO completions for other inodes being completed and
  613. * attach them to the current inode log item.
  614. */
  615. blip = bp->b_fspriv;
  616. prev = NULL;
  617. while (blip != NULL) {
  618. if (blip->li_cb != xfs_iflush_done) {
  619. prev = blip;
  620. blip = blip->li_bio_list;
  621. continue;
  622. }
  623. /* remove from list */
  624. next = blip->li_bio_list;
  625. if (!prev) {
  626. bp->b_fspriv = next;
  627. } else {
  628. prev->li_bio_list = next;
  629. }
  630. /* add to current list */
  631. blip->li_bio_list = lip->li_bio_list;
  632. lip->li_bio_list = blip;
  633. /*
  634. * while we have the item, do the unlocked check for needing
  635. * the AIL lock.
  636. */
  637. iip = INODE_ITEM(blip);
  638. if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
  639. need_ail++;
  640. blip = next;
  641. }
  642. /* make sure we capture the state of the initial inode. */
  643. iip = INODE_ITEM(lip);
  644. if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
  645. need_ail++;
  646. /*
  647. * We only want to pull the item from the AIL if it is
  648. * actually there and its location in the log has not
  649. * changed since we started the flush. Thus, we only bother
  650. * if the ili_logged flag is set and the inode's lsn has not
  651. * changed. First we check the lsn outside
  652. * the lock since it's cheaper, and then we recheck while
  653. * holding the lock before removing the inode from the AIL.
  654. */
  655. if (need_ail) {
  656. struct xfs_log_item *log_items[need_ail];
  657. int i = 0;
  658. spin_lock(&ailp->xa_lock);
  659. for (blip = lip; blip; blip = blip->li_bio_list) {
  660. iip = INODE_ITEM(blip);
  661. if (iip->ili_logged &&
  662. blip->li_lsn == iip->ili_flush_lsn) {
  663. log_items[i++] = blip;
  664. }
  665. ASSERT(i <= need_ail);
  666. }
  667. /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
  668. xfs_trans_ail_delete_bulk(ailp, log_items, i,
  669. SHUTDOWN_CORRUPT_INCORE);
  670. }
  671. /*
  672. * clean up and unlock the flush lock now we are done. We can clear the
  673. * ili_last_fields bits now that we know that the data corresponding to
  674. * them is safely on disk.
  675. */
  676. for (blip = lip; blip; blip = next) {
  677. next = blip->li_bio_list;
  678. blip->li_bio_list = NULL;
  679. iip = INODE_ITEM(blip);
  680. iip->ili_logged = 0;
  681. iip->ili_last_fields = 0;
  682. xfs_ifunlock(iip->ili_inode);
  683. }
  684. }
  685. /*
  686. * This is the inode flushing abort routine. It is called from xfs_iflush when
  687. * the filesystem is shutting down to clean up the inode state. It is
  688. * responsible for removing the inode item from the AIL if it has not been
  689. * re-logged, and unlocking the inode's flush lock.
  690. */
  691. void
  692. xfs_iflush_abort(
  693. xfs_inode_t *ip,
  694. bool stale)
  695. {
  696. xfs_inode_log_item_t *iip = ip->i_itemp;
  697. if (iip) {
  698. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  699. xfs_trans_ail_remove(&iip->ili_item,
  700. stale ? SHUTDOWN_LOG_IO_ERROR :
  701. SHUTDOWN_CORRUPT_INCORE);
  702. }
  703. iip->ili_logged = 0;
  704. /*
  705. * Clear the ili_last_fields bits now that we know that the
  706. * data corresponding to them is safely on disk.
  707. */
  708. iip->ili_last_fields = 0;
  709. /*
  710. * Clear the inode logging fields so no more flushes are
  711. * attempted.
  712. */
  713. iip->ili_fields = 0;
  714. iip->ili_fsync_fields = 0;
  715. }
  716. /*
  717. * Release the inode's flush lock since we're done with it.
  718. */
  719. xfs_ifunlock(ip);
  720. }
  721. void
  722. xfs_istale_done(
  723. struct xfs_buf *bp,
  724. struct xfs_log_item *lip)
  725. {
  726. xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
  727. }
  728. /*
  729. * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
  730. * (which can have different field alignments) to the native version
  731. */
  732. int
  733. xfs_inode_item_format_convert(
  734. xfs_log_iovec_t *buf,
  735. xfs_inode_log_format_t *in_f)
  736. {
  737. if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
  738. xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
  739. in_f->ilf_type = in_f32->ilf_type;
  740. in_f->ilf_size = in_f32->ilf_size;
  741. in_f->ilf_fields = in_f32->ilf_fields;
  742. in_f->ilf_asize = in_f32->ilf_asize;
  743. in_f->ilf_dsize = in_f32->ilf_dsize;
  744. in_f->ilf_ino = in_f32->ilf_ino;
  745. /* copy biggest field of ilf_u */
  746. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  747. in_f32->ilf_u.ilfu_uuid.__u_bits,
  748. sizeof(uuid_t));
  749. in_f->ilf_blkno = in_f32->ilf_blkno;
  750. in_f->ilf_len = in_f32->ilf_len;
  751. in_f->ilf_boffset = in_f32->ilf_boffset;
  752. return 0;
  753. } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
  754. xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
  755. in_f->ilf_type = in_f64->ilf_type;
  756. in_f->ilf_size = in_f64->ilf_size;
  757. in_f->ilf_fields = in_f64->ilf_fields;
  758. in_f->ilf_asize = in_f64->ilf_asize;
  759. in_f->ilf_dsize = in_f64->ilf_dsize;
  760. in_f->ilf_ino = in_f64->ilf_ino;
  761. /* copy biggest field of ilf_u */
  762. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  763. in_f64->ilf_u.ilfu_uuid.__u_bits,
  764. sizeof(uuid_t));
  765. in_f->ilf_blkno = in_f64->ilf_blkno;
  766. in_f->ilf_len = in_f64->ilf_len;
  767. in_f->ilf_boffset = in_f64->ilf_boffset;
  768. return 0;
  769. }
  770. return -EFSCORRUPTED;
  771. }