xfs_inode_item.c 27 KB

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