xfs_inode_item.c 27 KB

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