xfs_inode_item.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_trans_priv.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trace.h"
  35. kmem_zone_t *xfs_ili_zone; /* inode log item zone */
  36. static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
  37. {
  38. return container_of(lip, struct xfs_inode_log_item, ili_item);
  39. }
  40. /*
  41. * This returns the number of iovecs needed to log the given inode item.
  42. *
  43. * We need one iovec for the inode log format structure, one for the
  44. * inode core, and possibly one for the inode data/extents/b-tree root
  45. * and one for the inode attribute data/extents/b-tree root.
  46. */
  47. STATIC uint
  48. xfs_inode_item_size(
  49. struct xfs_log_item *lip)
  50. {
  51. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  52. struct xfs_inode *ip = iip->ili_inode;
  53. uint nvecs = 2;
  54. /*
  55. * Only log the data/extents/b-tree root if there is something
  56. * left to log.
  57. */
  58. iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
  59. switch (ip->i_d.di_format) {
  60. case XFS_DINODE_FMT_EXTENTS:
  61. iip->ili_format.ilf_fields &=
  62. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  63. XFS_ILOG_DEV | XFS_ILOG_UUID);
  64. if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) &&
  65. (ip->i_d.di_nextents > 0) &&
  66. (ip->i_df.if_bytes > 0)) {
  67. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  68. nvecs++;
  69. } else {
  70. iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT;
  71. }
  72. break;
  73. case XFS_DINODE_FMT_BTREE:
  74. ASSERT(ip->i_df.if_ext_max ==
  75. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  76. iip->ili_format.ilf_fields &=
  77. ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  78. XFS_ILOG_DEV | XFS_ILOG_UUID);
  79. if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) &&
  80. (ip->i_df.if_broot_bytes > 0)) {
  81. ASSERT(ip->i_df.if_broot != NULL);
  82. nvecs++;
  83. } else {
  84. ASSERT(!(iip->ili_format.ilf_fields &
  85. XFS_ILOG_DBROOT));
  86. #ifdef XFS_TRANS_DEBUG
  87. if (iip->ili_root_size > 0) {
  88. ASSERT(iip->ili_root_size ==
  89. ip->i_df.if_broot_bytes);
  90. ASSERT(memcmp(iip->ili_orig_root,
  91. ip->i_df.if_broot,
  92. iip->ili_root_size) == 0);
  93. } else {
  94. ASSERT(ip->i_df.if_broot_bytes == 0);
  95. }
  96. #endif
  97. iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT;
  98. }
  99. break;
  100. case XFS_DINODE_FMT_LOCAL:
  101. iip->ili_format.ilf_fields &=
  102. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
  103. XFS_ILOG_DEV | XFS_ILOG_UUID);
  104. if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) &&
  105. (ip->i_df.if_bytes > 0)) {
  106. ASSERT(ip->i_df.if_u1.if_data != NULL);
  107. ASSERT(ip->i_d.di_size > 0);
  108. nvecs++;
  109. } else {
  110. iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA;
  111. }
  112. break;
  113. case XFS_DINODE_FMT_DEV:
  114. iip->ili_format.ilf_fields &=
  115. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  116. XFS_ILOG_DEXT | XFS_ILOG_UUID);
  117. break;
  118. case XFS_DINODE_FMT_UUID:
  119. iip->ili_format.ilf_fields &=
  120. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  121. XFS_ILOG_DEXT | XFS_ILOG_DEV);
  122. break;
  123. default:
  124. ASSERT(0);
  125. break;
  126. }
  127. /*
  128. * If there are no attributes associated with this file,
  129. * then there cannot be anything more to log.
  130. * Clear all attribute-related log flags.
  131. */
  132. if (!XFS_IFORK_Q(ip)) {
  133. iip->ili_format.ilf_fields &=
  134. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  135. return nvecs;
  136. }
  137. /*
  138. * Log any necessary attribute data.
  139. */
  140. switch (ip->i_d.di_aformat) {
  141. case XFS_DINODE_FMT_EXTENTS:
  142. iip->ili_format.ilf_fields &=
  143. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  144. if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) &&
  145. (ip->i_d.di_anextents > 0) &&
  146. (ip->i_afp->if_bytes > 0)) {
  147. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  148. nvecs++;
  149. } else {
  150. iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT;
  151. }
  152. break;
  153. case XFS_DINODE_FMT_BTREE:
  154. iip->ili_format.ilf_fields &=
  155. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  156. if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) &&
  157. (ip->i_afp->if_broot_bytes > 0)) {
  158. ASSERT(ip->i_afp->if_broot != NULL);
  159. nvecs++;
  160. } else {
  161. iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT;
  162. }
  163. break;
  164. case XFS_DINODE_FMT_LOCAL:
  165. iip->ili_format.ilf_fields &=
  166. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  167. if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) &&
  168. (ip->i_afp->if_bytes > 0)) {
  169. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  170. nvecs++;
  171. } else {
  172. iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA;
  173. }
  174. break;
  175. default:
  176. ASSERT(0);
  177. break;
  178. }
  179. return nvecs;
  180. }
  181. /*
  182. * This is called to fill in the vector of log iovecs for the
  183. * given inode log item. It fills the first item with an inode
  184. * log format structure, the second with the on-disk inode structure,
  185. * and a possible third and/or fourth with the inode data/extents/b-tree
  186. * root and inode attributes data/extents/b-tree root.
  187. */
  188. STATIC void
  189. xfs_inode_item_format(
  190. struct xfs_log_item *lip,
  191. struct xfs_log_iovec *vecp)
  192. {
  193. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  194. struct xfs_inode *ip = iip->ili_inode;
  195. uint nvecs;
  196. size_t data_bytes;
  197. xfs_bmbt_rec_t *ext_buffer;
  198. int nrecs;
  199. xfs_mount_t *mp;
  200. vecp->i_addr = &iip->ili_format;
  201. vecp->i_len = sizeof(xfs_inode_log_format_t);
  202. vecp->i_type = XLOG_REG_TYPE_IFORMAT;
  203. vecp++;
  204. nvecs = 1;
  205. /*
  206. * Make sure the linux inode is dirty. We do this before
  207. * clearing i_update_core as the VFS will call back into
  208. * XFS here and set i_update_core, so we need to dirty the
  209. * inode first so that the ordering of i_update_core and
  210. * unlogged modifications still works as described below.
  211. */
  212. xfs_mark_inode_dirty_sync(ip);
  213. /*
  214. * Clear i_update_core if the timestamps (or any other
  215. * non-transactional modification) need flushing/logging
  216. * and we're about to log them with the rest of the core.
  217. *
  218. * This is the same logic as xfs_iflush() but this code can't
  219. * run at the same time as xfs_iflush because we're in commit
  220. * processing here and so we have the inode lock held in
  221. * exclusive mode. Although it doesn't really matter
  222. * for the timestamps if both routines were to grab the
  223. * timestamps or not. That would be ok.
  224. *
  225. * We clear i_update_core before copying out the data.
  226. * This is for coordination with our timestamp updates
  227. * that don't hold the inode lock. They will always
  228. * update the timestamps BEFORE setting i_update_core,
  229. * so if we clear i_update_core after they set it we
  230. * are guaranteed to see their updates to the timestamps
  231. * either here. Likewise, if they set it after we clear it
  232. * here, we'll see it either on the next commit of this
  233. * inode or the next time the inode gets flushed via
  234. * xfs_iflush(). This depends on strongly ordered memory
  235. * semantics, but we have that. We use the SYNCHRONIZE
  236. * macro to make sure that the compiler does not reorder
  237. * the i_update_core access below the data copy below.
  238. */
  239. if (ip->i_update_core) {
  240. ip->i_update_core = 0;
  241. SYNCHRONIZE();
  242. }
  243. /*
  244. * Make sure to get the latest timestamps from the Linux inode.
  245. */
  246. xfs_synchronize_times(ip);
  247. vecp->i_addr = &ip->i_d;
  248. vecp->i_len = sizeof(struct xfs_icdinode);
  249. vecp->i_type = XLOG_REG_TYPE_ICORE;
  250. vecp++;
  251. nvecs++;
  252. iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
  253. /*
  254. * If this is really an old format inode, then we need to
  255. * log it as such. This means that we have to copy the link
  256. * count from the new field to the old. We don't have to worry
  257. * about the new fields, because nothing trusts them as long as
  258. * the old inode version number is there. If the superblock already
  259. * has a new version number, then we don't bother converting back.
  260. */
  261. mp = ip->i_mount;
  262. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  263. if (ip->i_d.di_version == 1) {
  264. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  265. /*
  266. * Convert it back.
  267. */
  268. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  269. ip->i_d.di_onlink = ip->i_d.di_nlink;
  270. } else {
  271. /*
  272. * The superblock version has already been bumped,
  273. * so just make the conversion to the new inode
  274. * format permanent.
  275. */
  276. ip->i_d.di_version = 2;
  277. ip->i_d.di_onlink = 0;
  278. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  279. }
  280. }
  281. switch (ip->i_d.di_format) {
  282. case XFS_DINODE_FMT_EXTENTS:
  283. ASSERT(!(iip->ili_format.ilf_fields &
  284. (XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  285. XFS_ILOG_DEV | XFS_ILOG_UUID)));
  286. if (iip->ili_format.ilf_fields & XFS_ILOG_DEXT) {
  287. ASSERT(ip->i_df.if_bytes > 0);
  288. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  289. ASSERT(ip->i_d.di_nextents > 0);
  290. ASSERT(iip->ili_extents_buf == NULL);
  291. nrecs = ip->i_df.if_bytes /
  292. (uint)sizeof(xfs_bmbt_rec_t);
  293. ASSERT(nrecs > 0);
  294. #ifdef XFS_NATIVE_HOST
  295. if (nrecs == ip->i_d.di_nextents) {
  296. /*
  297. * There are no delayed allocation
  298. * extents, so just point to the
  299. * real extents array.
  300. */
  301. vecp->i_addr = ip->i_df.if_u1.if_extents;
  302. vecp->i_len = ip->i_df.if_bytes;
  303. vecp->i_type = XLOG_REG_TYPE_IEXT;
  304. } else
  305. #endif
  306. {
  307. /*
  308. * There are delayed allocation extents
  309. * in the inode, or we need to convert
  310. * the extents to on disk format.
  311. * Use xfs_iextents_copy()
  312. * to copy only the real extents into
  313. * a separate buffer. We'll free the
  314. * buffer in the unlock routine.
  315. */
  316. ext_buffer = kmem_alloc(ip->i_df.if_bytes,
  317. KM_SLEEP);
  318. iip->ili_extents_buf = ext_buffer;
  319. vecp->i_addr = ext_buffer;
  320. vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
  321. XFS_DATA_FORK);
  322. vecp->i_type = XLOG_REG_TYPE_IEXT;
  323. }
  324. ASSERT(vecp->i_len <= ip->i_df.if_bytes);
  325. iip->ili_format.ilf_dsize = vecp->i_len;
  326. vecp++;
  327. nvecs++;
  328. }
  329. break;
  330. case XFS_DINODE_FMT_BTREE:
  331. ASSERT(!(iip->ili_format.ilf_fields &
  332. (XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  333. XFS_ILOG_DEV | XFS_ILOG_UUID)));
  334. if (iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) {
  335. ASSERT(ip->i_df.if_broot_bytes > 0);
  336. ASSERT(ip->i_df.if_broot != NULL);
  337. vecp->i_addr = ip->i_df.if_broot;
  338. vecp->i_len = ip->i_df.if_broot_bytes;
  339. vecp->i_type = XLOG_REG_TYPE_IBROOT;
  340. vecp++;
  341. nvecs++;
  342. iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
  343. }
  344. break;
  345. case XFS_DINODE_FMT_LOCAL:
  346. ASSERT(!(iip->ili_format.ilf_fields &
  347. (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
  348. XFS_ILOG_DEV | XFS_ILOG_UUID)));
  349. if (iip->ili_format.ilf_fields & XFS_ILOG_DDATA) {
  350. ASSERT(ip->i_df.if_bytes > 0);
  351. ASSERT(ip->i_df.if_u1.if_data != NULL);
  352. ASSERT(ip->i_d.di_size > 0);
  353. vecp->i_addr = ip->i_df.if_u1.if_data;
  354. /*
  355. * Round i_bytes up to a word boundary.
  356. * The underlying memory is guaranteed to
  357. * to be there by xfs_idata_realloc().
  358. */
  359. data_bytes = roundup(ip->i_df.if_bytes, 4);
  360. ASSERT((ip->i_df.if_real_bytes == 0) ||
  361. (ip->i_df.if_real_bytes == data_bytes));
  362. vecp->i_len = (int)data_bytes;
  363. vecp->i_type = XLOG_REG_TYPE_ILOCAL;
  364. vecp++;
  365. nvecs++;
  366. iip->ili_format.ilf_dsize = (unsigned)data_bytes;
  367. }
  368. break;
  369. case XFS_DINODE_FMT_DEV:
  370. ASSERT(!(iip->ili_format.ilf_fields &
  371. (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
  372. XFS_ILOG_DDATA | XFS_ILOG_UUID)));
  373. if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
  374. iip->ili_format.ilf_u.ilfu_rdev =
  375. ip->i_df.if_u2.if_rdev;
  376. }
  377. break;
  378. case XFS_DINODE_FMT_UUID:
  379. ASSERT(!(iip->ili_format.ilf_fields &
  380. (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
  381. XFS_ILOG_DDATA | XFS_ILOG_DEV)));
  382. if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
  383. iip->ili_format.ilf_u.ilfu_uuid =
  384. ip->i_df.if_u2.if_uuid;
  385. }
  386. break;
  387. default:
  388. ASSERT(0);
  389. break;
  390. }
  391. /*
  392. * If there are no attributes associated with the file,
  393. * then we're done.
  394. * Assert that no attribute-related log flags are set.
  395. */
  396. if (!XFS_IFORK_Q(ip)) {
  397. ASSERT(nvecs == lip->li_desc->lid_size);
  398. iip->ili_format.ilf_size = nvecs;
  399. ASSERT(!(iip->ili_format.ilf_fields &
  400. (XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
  401. return;
  402. }
  403. switch (ip->i_d.di_aformat) {
  404. case XFS_DINODE_FMT_EXTENTS:
  405. ASSERT(!(iip->ili_format.ilf_fields &
  406. (XFS_ILOG_ADATA | XFS_ILOG_ABROOT)));
  407. if (iip->ili_format.ilf_fields & XFS_ILOG_AEXT) {
  408. ASSERT(ip->i_afp->if_bytes > 0);
  409. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  410. ASSERT(ip->i_d.di_anextents > 0);
  411. #ifdef DEBUG
  412. nrecs = ip->i_afp->if_bytes /
  413. (uint)sizeof(xfs_bmbt_rec_t);
  414. #endif
  415. ASSERT(nrecs > 0);
  416. ASSERT(nrecs == ip->i_d.di_anextents);
  417. #ifdef XFS_NATIVE_HOST
  418. /*
  419. * There are not delayed allocation extents
  420. * for attributes, so just point at the array.
  421. */
  422. vecp->i_addr = ip->i_afp->if_u1.if_extents;
  423. vecp->i_len = ip->i_afp->if_bytes;
  424. #else
  425. ASSERT(iip->ili_aextents_buf == NULL);
  426. /*
  427. * Need to endian flip before logging
  428. */
  429. ext_buffer = kmem_alloc(ip->i_afp->if_bytes,
  430. KM_SLEEP);
  431. iip->ili_aextents_buf = ext_buffer;
  432. vecp->i_addr = ext_buffer;
  433. vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
  434. XFS_ATTR_FORK);
  435. #endif
  436. vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
  437. iip->ili_format.ilf_asize = vecp->i_len;
  438. vecp++;
  439. nvecs++;
  440. }
  441. break;
  442. case XFS_DINODE_FMT_BTREE:
  443. ASSERT(!(iip->ili_format.ilf_fields &
  444. (XFS_ILOG_ADATA | XFS_ILOG_AEXT)));
  445. if (iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) {
  446. ASSERT(ip->i_afp->if_broot_bytes > 0);
  447. ASSERT(ip->i_afp->if_broot != NULL);
  448. vecp->i_addr = ip->i_afp->if_broot;
  449. vecp->i_len = ip->i_afp->if_broot_bytes;
  450. vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
  451. vecp++;
  452. nvecs++;
  453. iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
  454. }
  455. break;
  456. case XFS_DINODE_FMT_LOCAL:
  457. ASSERT(!(iip->ili_format.ilf_fields &
  458. (XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
  459. if (iip->ili_format.ilf_fields & XFS_ILOG_ADATA) {
  460. ASSERT(ip->i_afp->if_bytes > 0);
  461. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  462. vecp->i_addr = ip->i_afp->if_u1.if_data;
  463. /*
  464. * Round i_bytes up to a word boundary.
  465. * The underlying memory is guaranteed to
  466. * to be there by xfs_idata_realloc().
  467. */
  468. data_bytes = roundup(ip->i_afp->if_bytes, 4);
  469. ASSERT((ip->i_afp->if_real_bytes == 0) ||
  470. (ip->i_afp->if_real_bytes == data_bytes));
  471. vecp->i_len = (int)data_bytes;
  472. vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
  473. vecp++;
  474. nvecs++;
  475. iip->ili_format.ilf_asize = (unsigned)data_bytes;
  476. }
  477. break;
  478. default:
  479. ASSERT(0);
  480. break;
  481. }
  482. ASSERT(nvecs == lip->li_desc->lid_size);
  483. iip->ili_format.ilf_size = nvecs;
  484. }
  485. /*
  486. * This is called to pin the inode associated with the inode log
  487. * item in memory so it cannot be written out.
  488. */
  489. STATIC void
  490. xfs_inode_item_pin(
  491. struct xfs_log_item *lip)
  492. {
  493. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  494. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  495. trace_xfs_inode_pin(ip, _RET_IP_);
  496. atomic_inc(&ip->i_pincount);
  497. }
  498. /*
  499. * This is called to unpin the inode associated with the inode log
  500. * item which was previously pinned with a call to xfs_inode_item_pin().
  501. *
  502. * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
  503. */
  504. STATIC void
  505. xfs_inode_item_unpin(
  506. struct xfs_log_item *lip,
  507. int remove)
  508. {
  509. struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
  510. trace_xfs_inode_unpin(ip, _RET_IP_);
  511. ASSERT(atomic_read(&ip->i_pincount) > 0);
  512. if (atomic_dec_and_test(&ip->i_pincount))
  513. wake_up(&ip->i_ipin_wait);
  514. }
  515. /*
  516. * This is called to attempt to lock the inode associated with this
  517. * inode log item, in preparation for the push routine which does the actual
  518. * iflush. Don't sleep on the inode lock or the flush lock.
  519. *
  520. * If the flush lock is already held, indicating that the inode has
  521. * been or is in the process of being flushed, then (ideally) we'd like to
  522. * see if the inode's buffer is still incore, and if so give it a nudge.
  523. * We delay doing so until the pushbuf routine, though, to avoid holding
  524. * the AIL lock across a call to the blackhole which is the buffer cache.
  525. * Also we don't want to sleep in any device strategy routines, which can happen
  526. * if we do the subsequent bawrite in here.
  527. */
  528. STATIC uint
  529. xfs_inode_item_trylock(
  530. struct xfs_log_item *lip)
  531. {
  532. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  533. struct xfs_inode *ip = iip->ili_inode;
  534. if (xfs_ipincount(ip) > 0)
  535. return XFS_ITEM_PINNED;
  536. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
  537. return XFS_ITEM_LOCKED;
  538. if (!xfs_iflock_nowait(ip)) {
  539. /*
  540. * inode has already been flushed to the backing buffer,
  541. * leave it locked in shared mode, pushbuf routine will
  542. * unlock it.
  543. */
  544. return XFS_ITEM_PUSHBUF;
  545. }
  546. /* Stale items should force out the iclog */
  547. if (ip->i_flags & XFS_ISTALE) {
  548. xfs_ifunlock(ip);
  549. /*
  550. * we hold the AIL lock - notify the unlock routine of this
  551. * so it doesn't try to get the lock again.
  552. */
  553. xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
  554. return XFS_ITEM_PINNED;
  555. }
  556. #ifdef DEBUG
  557. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  558. ASSERT(iip->ili_format.ilf_fields != 0);
  559. ASSERT(iip->ili_logged == 0);
  560. ASSERT(lip->li_flags & XFS_LI_IN_AIL);
  561. }
  562. #endif
  563. return XFS_ITEM_SUCCESS;
  564. }
  565. /*
  566. * Unlock the inode associated with the inode log item.
  567. * Clear the fields of the inode and inode log item that
  568. * are specific to the current transaction. If the
  569. * hold flags is set, do not unlock the inode.
  570. */
  571. STATIC void
  572. xfs_inode_item_unlock(
  573. struct xfs_log_item *lip)
  574. {
  575. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  576. struct xfs_inode *ip = iip->ili_inode;
  577. unsigned short lock_flags;
  578. ASSERT(iip->ili_inode->i_itemp != NULL);
  579. ASSERT(xfs_isilocked(iip->ili_inode, XFS_ILOCK_EXCL));
  580. /*
  581. * Clear the transaction pointer in the inode.
  582. */
  583. ip->i_transp = NULL;
  584. /*
  585. * If the inode needed a separate buffer with which to log
  586. * its extents, then free it now.
  587. */
  588. if (iip->ili_extents_buf != NULL) {
  589. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
  590. ASSERT(ip->i_d.di_nextents > 0);
  591. ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
  592. ASSERT(ip->i_df.if_bytes > 0);
  593. kmem_free(iip->ili_extents_buf);
  594. iip->ili_extents_buf = NULL;
  595. }
  596. if (iip->ili_aextents_buf != NULL) {
  597. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
  598. ASSERT(ip->i_d.di_anextents > 0);
  599. ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
  600. ASSERT(ip->i_afp->if_bytes > 0);
  601. kmem_free(iip->ili_aextents_buf);
  602. iip->ili_aextents_buf = NULL;
  603. }
  604. lock_flags = iip->ili_lock_flags;
  605. iip->ili_lock_flags = 0;
  606. if (lock_flags) {
  607. xfs_iunlock(iip->ili_inode, lock_flags);
  608. IRELE(iip->ili_inode);
  609. }
  610. }
  611. /*
  612. * This is called to find out where the oldest active copy of the
  613. * inode log item in the on disk log resides now that the last log
  614. * write of it completed at the given lsn. Since we always re-log
  615. * all dirty data in an inode, the latest copy in the on disk log
  616. * is the only one that matters. Therefore, simply return the
  617. * given lsn.
  618. */
  619. STATIC xfs_lsn_t
  620. xfs_inode_item_committed(
  621. struct xfs_log_item *lip,
  622. xfs_lsn_t lsn)
  623. {
  624. return lsn;
  625. }
  626. /*
  627. * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
  628. * failed to get the inode flush lock but did get the inode locked SHARED.
  629. * Here we're trying to see if the inode buffer is incore, and if so whether it's
  630. * marked delayed write. If that's the case, we'll promote it and that will
  631. * allow the caller to write the buffer by triggering the xfsbufd to run.
  632. */
  633. STATIC void
  634. xfs_inode_item_pushbuf(
  635. struct xfs_log_item *lip)
  636. {
  637. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  638. struct xfs_inode *ip = iip->ili_inode;
  639. struct xfs_buf *bp;
  640. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
  641. /*
  642. * If a flush is not in progress anymore, chances are that the
  643. * inode was taken off the AIL. So, just get out.
  644. */
  645. if (completion_done(&ip->i_flush) ||
  646. !(lip->li_flags & XFS_LI_IN_AIL)) {
  647. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  648. return;
  649. }
  650. bp = xfs_incore(ip->i_mount->m_ddev_targp, iip->ili_format.ilf_blkno,
  651. iip->ili_format.ilf_len, XBF_TRYLOCK);
  652. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  653. if (!bp)
  654. return;
  655. if (XFS_BUF_ISDELAYWRITE(bp))
  656. xfs_buf_delwri_promote(bp);
  657. xfs_buf_relse(bp);
  658. }
  659. /*
  660. * This is called to asynchronously write the inode associated with this
  661. * inode log item out to disk. The inode will already have been locked by
  662. * a successful call to xfs_inode_item_trylock().
  663. */
  664. STATIC void
  665. xfs_inode_item_push(
  666. struct xfs_log_item *lip)
  667. {
  668. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  669. struct xfs_inode *ip = iip->ili_inode;
  670. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
  671. ASSERT(!completion_done(&ip->i_flush));
  672. /*
  673. * Since we were able to lock the inode's flush lock and
  674. * we found it on the AIL, the inode must be dirty. This
  675. * is because the inode is removed from the AIL while still
  676. * holding the flush lock in xfs_iflush_done(). Thus, if
  677. * we found it in the AIL and were able to obtain the flush
  678. * lock without sleeping, then there must not have been
  679. * anyone in the process of flushing the inode.
  680. */
  681. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
  682. iip->ili_format.ilf_fields != 0);
  683. /*
  684. * Push the inode to it's backing buffer. This will not remove the
  685. * inode from the AIL - a further push will be required to trigger a
  686. * buffer push. However, this allows all the dirty inodes to be pushed
  687. * to the buffer before it is pushed to disk. THe buffer IO completion
  688. * will pull th einode from the AIL, mark it clean and unlock the flush
  689. * lock.
  690. */
  691. (void) xfs_iflush(ip, 0);
  692. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  693. }
  694. /*
  695. * XXX rcc - this one really has to do something. Probably needs
  696. * to stamp in a new field in the incore inode.
  697. */
  698. STATIC void
  699. xfs_inode_item_committing(
  700. struct xfs_log_item *lip,
  701. xfs_lsn_t lsn)
  702. {
  703. INODE_ITEM(lip)->ili_last_lsn = lsn;
  704. }
  705. /*
  706. * This is the ops vector shared by all buf log items.
  707. */
  708. static struct xfs_item_ops xfs_inode_item_ops = {
  709. .iop_size = xfs_inode_item_size,
  710. .iop_format = xfs_inode_item_format,
  711. .iop_pin = xfs_inode_item_pin,
  712. .iop_unpin = xfs_inode_item_unpin,
  713. .iop_trylock = xfs_inode_item_trylock,
  714. .iop_unlock = xfs_inode_item_unlock,
  715. .iop_committed = xfs_inode_item_committed,
  716. .iop_push = xfs_inode_item_push,
  717. .iop_pushbuf = xfs_inode_item_pushbuf,
  718. .iop_committing = xfs_inode_item_committing
  719. };
  720. /*
  721. * Initialize the inode log item for a newly allocated (in-core) inode.
  722. */
  723. void
  724. xfs_inode_item_init(
  725. struct xfs_inode *ip,
  726. struct xfs_mount *mp)
  727. {
  728. struct xfs_inode_log_item *iip;
  729. ASSERT(ip->i_itemp == NULL);
  730. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  731. iip->ili_inode = ip;
  732. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  733. &xfs_inode_item_ops);
  734. iip->ili_format.ilf_type = XFS_LI_INODE;
  735. iip->ili_format.ilf_ino = ip->i_ino;
  736. iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
  737. iip->ili_format.ilf_len = ip->i_imap.im_len;
  738. iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
  739. }
  740. /*
  741. * Free the inode log item and any memory hanging off of it.
  742. */
  743. void
  744. xfs_inode_item_destroy(
  745. xfs_inode_t *ip)
  746. {
  747. #ifdef XFS_TRANS_DEBUG
  748. if (ip->i_itemp->ili_root_size != 0) {
  749. kmem_free(ip->i_itemp->ili_orig_root);
  750. }
  751. #endif
  752. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  753. }
  754. /*
  755. * This is the inode flushing I/O completion routine. It is called
  756. * from interrupt level when the buffer containing the inode is
  757. * flushed to disk. It is responsible for removing the inode item
  758. * from the AIL if it has not been re-logged, and unlocking the inode's
  759. * flush lock.
  760. */
  761. void
  762. xfs_iflush_done(
  763. struct xfs_buf *bp,
  764. struct xfs_log_item *lip)
  765. {
  766. struct xfs_inode_log_item *iip = INODE_ITEM(lip);
  767. xfs_inode_t *ip = iip->ili_inode;
  768. struct xfs_ail *ailp = lip->li_ailp;
  769. /*
  770. * We only want to pull the item from the AIL if it is
  771. * actually there and its location in the log has not
  772. * changed since we started the flush. Thus, we only bother
  773. * if the ili_logged flag is set and the inode's lsn has not
  774. * changed. First we check the lsn outside
  775. * the lock since it's cheaper, and then we recheck while
  776. * holding the lock before removing the inode from the AIL.
  777. */
  778. if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn) {
  779. spin_lock(&ailp->xa_lock);
  780. if (lip->li_lsn == iip->ili_flush_lsn) {
  781. /* xfs_trans_ail_delete() drops the AIL lock. */
  782. xfs_trans_ail_delete(ailp, lip);
  783. } else {
  784. spin_unlock(&ailp->xa_lock);
  785. }
  786. }
  787. iip->ili_logged = 0;
  788. /*
  789. * Clear the ili_last_fields bits now that we know that the
  790. * data corresponding to them is safely on disk.
  791. */
  792. iip->ili_last_fields = 0;
  793. /*
  794. * Release the inode's flush lock since we're done with it.
  795. */
  796. xfs_ifunlock(ip);
  797. }
  798. /*
  799. * This is the inode flushing abort routine. It is called
  800. * from xfs_iflush when the filesystem is shutting down to clean
  801. * up the inode state.
  802. * It is responsible for removing the inode item
  803. * from the AIL if it has not been re-logged, and unlocking the inode's
  804. * flush lock.
  805. */
  806. void
  807. xfs_iflush_abort(
  808. xfs_inode_t *ip)
  809. {
  810. xfs_inode_log_item_t *iip = ip->i_itemp;
  811. xfs_mount_t *mp;
  812. iip = ip->i_itemp;
  813. mp = ip->i_mount;
  814. if (iip) {
  815. struct xfs_ail *ailp = iip->ili_item.li_ailp;
  816. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  817. spin_lock(&ailp->xa_lock);
  818. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  819. /* xfs_trans_ail_delete() drops the AIL lock. */
  820. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)iip);
  821. } else
  822. spin_unlock(&ailp->xa_lock);
  823. }
  824. iip->ili_logged = 0;
  825. /*
  826. * Clear the ili_last_fields bits now that we know that the
  827. * data corresponding to them is safely on disk.
  828. */
  829. iip->ili_last_fields = 0;
  830. /*
  831. * Clear the inode logging fields so no more flushes are
  832. * attempted.
  833. */
  834. iip->ili_format.ilf_fields = 0;
  835. }
  836. /*
  837. * Release the inode's flush lock since we're done with it.
  838. */
  839. xfs_ifunlock(ip);
  840. }
  841. void
  842. xfs_istale_done(
  843. struct xfs_buf *bp,
  844. struct xfs_log_item *lip)
  845. {
  846. xfs_iflush_abort(INODE_ITEM(lip)->ili_inode);
  847. }
  848. /*
  849. * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
  850. * (which can have different field alignments) to the native version
  851. */
  852. int
  853. xfs_inode_item_format_convert(
  854. xfs_log_iovec_t *buf,
  855. xfs_inode_log_format_t *in_f)
  856. {
  857. if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
  858. xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
  859. in_f->ilf_type = in_f32->ilf_type;
  860. in_f->ilf_size = in_f32->ilf_size;
  861. in_f->ilf_fields = in_f32->ilf_fields;
  862. in_f->ilf_asize = in_f32->ilf_asize;
  863. in_f->ilf_dsize = in_f32->ilf_dsize;
  864. in_f->ilf_ino = in_f32->ilf_ino;
  865. /* copy biggest field of ilf_u */
  866. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  867. in_f32->ilf_u.ilfu_uuid.__u_bits,
  868. sizeof(uuid_t));
  869. in_f->ilf_blkno = in_f32->ilf_blkno;
  870. in_f->ilf_len = in_f32->ilf_len;
  871. in_f->ilf_boffset = in_f32->ilf_boffset;
  872. return 0;
  873. } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
  874. xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
  875. in_f->ilf_type = in_f64->ilf_type;
  876. in_f->ilf_size = in_f64->ilf_size;
  877. in_f->ilf_fields = in_f64->ilf_fields;
  878. in_f->ilf_asize = in_f64->ilf_asize;
  879. in_f->ilf_dsize = in_f64->ilf_dsize;
  880. in_f->ilf_ino = in_f64->ilf_ino;
  881. /* copy biggest field of ilf_u */
  882. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  883. in_f64->ilf_u.ilfu_uuid.__u_bits,
  884. sizeof(uuid_t));
  885. in_f->ilf_blkno = in_f64->ilf_blkno;
  886. in_f->ilf_len = in_f64->ilf_len;
  887. in_f->ilf_boffset = in_f64->ilf_boffset;
  888. return 0;
  889. }
  890. return EFSCORRUPTED;
  891. }