xfs_inode_item.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. /*
  37. * This returns the number of iovecs needed to log the given inode item.
  38. *
  39. * We need one iovec for the inode log format structure, one for the
  40. * inode core, and possibly one for the inode data/extents/b-tree root
  41. * and one for the inode attribute data/extents/b-tree root.
  42. */
  43. STATIC uint
  44. xfs_inode_item_size(
  45. xfs_inode_log_item_t *iip)
  46. {
  47. uint nvecs;
  48. xfs_inode_t *ip;
  49. ip = iip->ili_inode;
  50. nvecs = 2;
  51. /*
  52. * Only log the data/extents/b-tree root if there is something
  53. * left to log.
  54. */
  55. iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
  56. switch (ip->i_d.di_format) {
  57. case XFS_DINODE_FMT_EXTENTS:
  58. iip->ili_format.ilf_fields &=
  59. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  60. XFS_ILOG_DEV | XFS_ILOG_UUID);
  61. if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) &&
  62. (ip->i_d.di_nextents > 0) &&
  63. (ip->i_df.if_bytes > 0)) {
  64. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  65. nvecs++;
  66. } else {
  67. iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT;
  68. }
  69. break;
  70. case XFS_DINODE_FMT_BTREE:
  71. ASSERT(ip->i_df.if_ext_max ==
  72. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  73. iip->ili_format.ilf_fields &=
  74. ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  75. XFS_ILOG_DEV | XFS_ILOG_UUID);
  76. if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) &&
  77. (ip->i_df.if_broot_bytes > 0)) {
  78. ASSERT(ip->i_df.if_broot != NULL);
  79. nvecs++;
  80. } else {
  81. ASSERT(!(iip->ili_format.ilf_fields &
  82. XFS_ILOG_DBROOT));
  83. #ifdef XFS_TRANS_DEBUG
  84. if (iip->ili_root_size > 0) {
  85. ASSERT(iip->ili_root_size ==
  86. ip->i_df.if_broot_bytes);
  87. ASSERT(memcmp(iip->ili_orig_root,
  88. ip->i_df.if_broot,
  89. iip->ili_root_size) == 0);
  90. } else {
  91. ASSERT(ip->i_df.if_broot_bytes == 0);
  92. }
  93. #endif
  94. iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT;
  95. }
  96. break;
  97. case XFS_DINODE_FMT_LOCAL:
  98. iip->ili_format.ilf_fields &=
  99. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
  100. XFS_ILOG_DEV | XFS_ILOG_UUID);
  101. if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) &&
  102. (ip->i_df.if_bytes > 0)) {
  103. ASSERT(ip->i_df.if_u1.if_data != NULL);
  104. ASSERT(ip->i_d.di_size > 0);
  105. nvecs++;
  106. } else {
  107. iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA;
  108. }
  109. break;
  110. case XFS_DINODE_FMT_DEV:
  111. iip->ili_format.ilf_fields &=
  112. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  113. XFS_ILOG_DEXT | XFS_ILOG_UUID);
  114. break;
  115. case XFS_DINODE_FMT_UUID:
  116. iip->ili_format.ilf_fields &=
  117. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  118. XFS_ILOG_DEXT | XFS_ILOG_DEV);
  119. break;
  120. default:
  121. ASSERT(0);
  122. break;
  123. }
  124. /*
  125. * If there are no attributes associated with this file,
  126. * then there cannot be anything more to log.
  127. * Clear all attribute-related log flags.
  128. */
  129. if (!XFS_IFORK_Q(ip)) {
  130. iip->ili_format.ilf_fields &=
  131. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  132. return nvecs;
  133. }
  134. /*
  135. * Log any necessary attribute data.
  136. */
  137. switch (ip->i_d.di_aformat) {
  138. case XFS_DINODE_FMT_EXTENTS:
  139. iip->ili_format.ilf_fields &=
  140. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  141. if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) &&
  142. (ip->i_d.di_anextents > 0) &&
  143. (ip->i_afp->if_bytes > 0)) {
  144. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  145. nvecs++;
  146. } else {
  147. iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT;
  148. }
  149. break;
  150. case XFS_DINODE_FMT_BTREE:
  151. iip->ili_format.ilf_fields &=
  152. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  153. if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) &&
  154. (ip->i_afp->if_broot_bytes > 0)) {
  155. ASSERT(ip->i_afp->if_broot != NULL);
  156. nvecs++;
  157. } else {
  158. iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT;
  159. }
  160. break;
  161. case XFS_DINODE_FMT_LOCAL:
  162. iip->ili_format.ilf_fields &=
  163. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  164. if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) &&
  165. (ip->i_afp->if_bytes > 0)) {
  166. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  167. nvecs++;
  168. } else {
  169. iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA;
  170. }
  171. break;
  172. default:
  173. ASSERT(0);
  174. break;
  175. }
  176. return nvecs;
  177. }
  178. /*
  179. * This is called to fill in the vector of log iovecs for the
  180. * given inode log item. It fills the first item with an inode
  181. * log format structure, the second with the on-disk inode structure,
  182. * and a possible third and/or fourth with the inode data/extents/b-tree
  183. * root and inode attributes data/extents/b-tree root.
  184. */
  185. STATIC void
  186. xfs_inode_item_format(
  187. xfs_inode_log_item_t *iip,
  188. xfs_log_iovec_t *log_vector)
  189. {
  190. uint nvecs;
  191. xfs_log_iovec_t *vecp;
  192. xfs_inode_t *ip;
  193. size_t data_bytes;
  194. xfs_bmbt_rec_t *ext_buffer;
  195. int nrecs;
  196. xfs_mount_t *mp;
  197. ip = iip->ili_inode;
  198. vecp = log_vector;
  199. vecp->i_addr = (xfs_caddr_t)&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 = (xfs_caddr_t)&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. nrecs = ip->i_df.if_bytes /
  291. (uint)sizeof(xfs_bmbt_rec_t);
  292. ASSERT(nrecs > 0);
  293. #ifdef XFS_NATIVE_HOST
  294. if (nrecs == ip->i_d.di_nextents) {
  295. /*
  296. * There are no delayed allocation
  297. * extents, so just point to the
  298. * real extents array.
  299. */
  300. vecp->i_addr =
  301. (char *)(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 = (xfs_caddr_t)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 = (xfs_caddr_t)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 = (xfs_caddr_t)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 == iip->ili_item.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 = (char *)(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 = (xfs_caddr_t)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 = (xfs_caddr_t)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 = (xfs_caddr_t)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 == iip->ili_item.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. xfs_inode_log_item_t *iip)
  492. {
  493. ASSERT(xfs_isilocked(iip->ili_inode, XFS_ILOCK_EXCL));
  494. trace_xfs_inode_pin(iip->ili_inode, _RET_IP_);
  495. atomic_inc(&iip->ili_inode->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. xfs_inode_log_item_t *iip,
  506. int remove)
  507. {
  508. struct xfs_inode *ip = iip->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. xfs_inode_log_item_t *iip)
  530. {
  531. register xfs_inode_t *ip;
  532. ip = iip->ili_inode;
  533. if (xfs_ipincount(ip) > 0) {
  534. return XFS_ITEM_PINNED;
  535. }
  536. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  537. return XFS_ITEM_LOCKED;
  538. }
  539. if (!xfs_iflock_nowait(ip)) {
  540. /*
  541. * inode has already been flushed to the backing buffer,
  542. * leave it locked in shared mode, pushbuf routine will
  543. * unlock it.
  544. */
  545. return XFS_ITEM_PUSHBUF;
  546. }
  547. /* Stale items should force out the iclog */
  548. if (ip->i_flags & XFS_ISTALE) {
  549. xfs_ifunlock(ip);
  550. /*
  551. * we hold the AIL lock - notify the unlock routine of this
  552. * so it doesn't try to get the lock again.
  553. */
  554. xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
  555. return XFS_ITEM_PINNED;
  556. }
  557. #ifdef DEBUG
  558. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  559. ASSERT(iip->ili_format.ilf_fields != 0);
  560. ASSERT(iip->ili_logged == 0);
  561. ASSERT(iip->ili_item.li_flags & XFS_LI_IN_AIL);
  562. }
  563. #endif
  564. return XFS_ITEM_SUCCESS;
  565. }
  566. /*
  567. * Unlock the inode associated with the inode log item.
  568. * Clear the fields of the inode and inode log item that
  569. * are specific to the current transaction. If the
  570. * hold flags is set, do not unlock the inode.
  571. */
  572. STATIC void
  573. xfs_inode_item_unlock(
  574. xfs_inode_log_item_t *iip)
  575. {
  576. uint hold;
  577. uint iolocked;
  578. uint lock_flags;
  579. xfs_inode_t *ip;
  580. ASSERT(iip != NULL);
  581. ASSERT(iip->ili_inode->i_itemp != NULL);
  582. ASSERT(xfs_isilocked(iip->ili_inode, XFS_ILOCK_EXCL));
  583. ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
  584. XFS_ILI_IOLOCKED_EXCL)) ||
  585. xfs_isilocked(iip->ili_inode, XFS_IOLOCK_EXCL));
  586. ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
  587. XFS_ILI_IOLOCKED_SHARED)) ||
  588. xfs_isilocked(iip->ili_inode, XFS_IOLOCK_SHARED));
  589. /*
  590. * Clear the transaction pointer in the inode.
  591. */
  592. ip = iip->ili_inode;
  593. ip->i_transp = NULL;
  594. /*
  595. * If the inode needed a separate buffer with which to log
  596. * its extents, then free it now.
  597. */
  598. if (iip->ili_extents_buf != NULL) {
  599. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
  600. ASSERT(ip->i_d.di_nextents > 0);
  601. ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
  602. ASSERT(ip->i_df.if_bytes > 0);
  603. kmem_free(iip->ili_extents_buf);
  604. iip->ili_extents_buf = NULL;
  605. }
  606. if (iip->ili_aextents_buf != NULL) {
  607. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
  608. ASSERT(ip->i_d.di_anextents > 0);
  609. ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
  610. ASSERT(ip->i_afp->if_bytes > 0);
  611. kmem_free(iip->ili_aextents_buf);
  612. iip->ili_aextents_buf = NULL;
  613. }
  614. /*
  615. * Figure out if we should unlock the inode or not.
  616. */
  617. hold = iip->ili_flags & XFS_ILI_HOLD;
  618. /*
  619. * Before clearing out the flags, remember whether we
  620. * are holding the inode's IO lock.
  621. */
  622. iolocked = iip->ili_flags & XFS_ILI_IOLOCKED_ANY;
  623. /*
  624. * Clear out the fields of the inode log item particular
  625. * to the current transaction.
  626. */
  627. iip->ili_flags = 0;
  628. /*
  629. * Unlock the inode if XFS_ILI_HOLD was not set.
  630. */
  631. if (!hold) {
  632. lock_flags = XFS_ILOCK_EXCL;
  633. if (iolocked & XFS_ILI_IOLOCKED_EXCL) {
  634. lock_flags |= XFS_IOLOCK_EXCL;
  635. } else if (iolocked & XFS_ILI_IOLOCKED_SHARED) {
  636. lock_flags |= XFS_IOLOCK_SHARED;
  637. }
  638. xfs_iput(iip->ili_inode, lock_flags);
  639. }
  640. }
  641. /*
  642. * This is called to find out where the oldest active copy of the
  643. * inode log item in the on disk log resides now that the last log
  644. * write of it completed at the given lsn. Since we always re-log
  645. * all dirty data in an inode, the latest copy in the on disk log
  646. * is the only one that matters. Therefore, simply return the
  647. * given lsn.
  648. */
  649. /*ARGSUSED*/
  650. STATIC xfs_lsn_t
  651. xfs_inode_item_committed(
  652. xfs_inode_log_item_t *iip,
  653. xfs_lsn_t lsn)
  654. {
  655. return (lsn);
  656. }
  657. /*
  658. * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
  659. * failed to get the inode flush lock but did get the inode locked SHARED.
  660. * Here we're trying to see if the inode buffer is incore, and if so whether it's
  661. * marked delayed write. If that's the case, we'll promote it and that will
  662. * allow the caller to write the buffer by triggering the xfsbufd to run.
  663. */
  664. STATIC void
  665. xfs_inode_item_pushbuf(
  666. xfs_inode_log_item_t *iip)
  667. {
  668. xfs_inode_t *ip;
  669. xfs_mount_t *mp;
  670. xfs_buf_t *bp;
  671. ip = iip->ili_inode;
  672. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
  673. /*
  674. * If a flush is not in progress anymore, chances are that the
  675. * inode was taken off the AIL. So, just get out.
  676. */
  677. if (completion_done(&ip->i_flush) ||
  678. ((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0)) {
  679. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  680. return;
  681. }
  682. mp = ip->i_mount;
  683. bp = xfs_incore(mp->m_ddev_targp, iip->ili_format.ilf_blkno,
  684. iip->ili_format.ilf_len, XBF_TRYLOCK);
  685. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  686. if (!bp)
  687. return;
  688. if (XFS_BUF_ISDELAYWRITE(bp))
  689. xfs_buf_delwri_promote(bp);
  690. xfs_buf_relse(bp);
  691. return;
  692. }
  693. /*
  694. * This is called to asynchronously write the inode associated with this
  695. * inode log item out to disk. The inode will already have been locked by
  696. * a successful call to xfs_inode_item_trylock().
  697. */
  698. STATIC void
  699. xfs_inode_item_push(
  700. xfs_inode_log_item_t *iip)
  701. {
  702. xfs_inode_t *ip;
  703. ip = iip->ili_inode;
  704. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
  705. ASSERT(!completion_done(&ip->i_flush));
  706. /*
  707. * Since we were able to lock the inode's flush lock and
  708. * we found it on the AIL, the inode must be dirty. This
  709. * is because the inode is removed from the AIL while still
  710. * holding the flush lock in xfs_iflush_done(). Thus, if
  711. * we found it in the AIL and were able to obtain the flush
  712. * lock without sleeping, then there must not have been
  713. * anyone in the process of flushing the inode.
  714. */
  715. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
  716. iip->ili_format.ilf_fields != 0);
  717. /*
  718. * Push the inode to it's backing buffer. This will not remove the
  719. * inode from the AIL - a further push will be required to trigger a
  720. * buffer push. However, this allows all the dirty inodes to be pushed
  721. * to the buffer before it is pushed to disk. THe buffer IO completion
  722. * will pull th einode from the AIL, mark it clean and unlock the flush
  723. * lock.
  724. */
  725. (void) xfs_iflush(ip, 0);
  726. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  727. return;
  728. }
  729. /*
  730. * XXX rcc - this one really has to do something. Probably needs
  731. * to stamp in a new field in the incore inode.
  732. */
  733. /* ARGSUSED */
  734. STATIC void
  735. xfs_inode_item_committing(
  736. xfs_inode_log_item_t *iip,
  737. xfs_lsn_t lsn)
  738. {
  739. iip->ili_last_lsn = lsn;
  740. return;
  741. }
  742. /*
  743. * This is the ops vector shared by all buf log items.
  744. */
  745. static struct xfs_item_ops xfs_inode_item_ops = {
  746. .iop_size = (uint(*)(xfs_log_item_t*))xfs_inode_item_size,
  747. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  748. xfs_inode_item_format,
  749. .iop_pin = (void(*)(xfs_log_item_t*))xfs_inode_item_pin,
  750. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_inode_item_unpin,
  751. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_inode_item_trylock,
  752. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_inode_item_unlock,
  753. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  754. xfs_inode_item_committed,
  755. .iop_push = (void(*)(xfs_log_item_t*))xfs_inode_item_push,
  756. .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_inode_item_pushbuf,
  757. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  758. xfs_inode_item_committing
  759. };
  760. /*
  761. * Initialize the inode log item for a newly allocated (in-core) inode.
  762. */
  763. void
  764. xfs_inode_item_init(
  765. xfs_inode_t *ip,
  766. xfs_mount_t *mp)
  767. {
  768. xfs_inode_log_item_t *iip;
  769. ASSERT(ip->i_itemp == NULL);
  770. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  771. iip->ili_inode = ip;
  772. xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
  773. &xfs_inode_item_ops);
  774. iip->ili_format.ilf_type = XFS_LI_INODE;
  775. iip->ili_format.ilf_ino = ip->i_ino;
  776. iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
  777. iip->ili_format.ilf_len = ip->i_imap.im_len;
  778. iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
  779. }
  780. /*
  781. * Free the inode log item and any memory hanging off of it.
  782. */
  783. void
  784. xfs_inode_item_destroy(
  785. xfs_inode_t *ip)
  786. {
  787. #ifdef XFS_TRANS_DEBUG
  788. if (ip->i_itemp->ili_root_size != 0) {
  789. kmem_free(ip->i_itemp->ili_orig_root);
  790. }
  791. #endif
  792. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  793. }
  794. /*
  795. * This is the inode flushing I/O completion routine. It is called
  796. * from interrupt level when the buffer containing the inode is
  797. * flushed to disk. It is responsible for removing the inode item
  798. * from the AIL if it has not been re-logged, and unlocking the inode's
  799. * flush lock.
  800. */
  801. /*ARGSUSED*/
  802. void
  803. xfs_iflush_done(
  804. xfs_buf_t *bp,
  805. xfs_inode_log_item_t *iip)
  806. {
  807. xfs_inode_t *ip = iip->ili_inode;
  808. struct xfs_ail *ailp = iip->ili_item.li_ailp;
  809. /*
  810. * We only want to pull the item from the AIL if it is
  811. * actually there and its location in the log has not
  812. * changed since we started the flush. Thus, we only bother
  813. * if the ili_logged flag is set and the inode's lsn has not
  814. * changed. First we check the lsn outside
  815. * the lock since it's cheaper, and then we recheck while
  816. * holding the lock before removing the inode from the AIL.
  817. */
  818. if (iip->ili_logged &&
  819. (iip->ili_item.li_lsn == iip->ili_flush_lsn)) {
  820. spin_lock(&ailp->xa_lock);
  821. if (iip->ili_item.li_lsn == iip->ili_flush_lsn) {
  822. /* xfs_trans_ail_delete() drops the AIL lock. */
  823. xfs_trans_ail_delete(ailp, (xfs_log_item_t*)iip);
  824. } else {
  825. spin_unlock(&ailp->xa_lock);
  826. }
  827. }
  828. iip->ili_logged = 0;
  829. /*
  830. * Clear the ili_last_fields bits now that we know that the
  831. * data corresponding to them is safely on disk.
  832. */
  833. iip->ili_last_fields = 0;
  834. /*
  835. * Release the inode's flush lock since we're done with it.
  836. */
  837. xfs_ifunlock(ip);
  838. return;
  839. }
  840. /*
  841. * This is the inode flushing abort routine. It is called
  842. * from xfs_iflush when the filesystem is shutting down to clean
  843. * up the inode state.
  844. * It is responsible for removing the inode item
  845. * from the AIL if it has not been re-logged, and unlocking the inode's
  846. * flush lock.
  847. */
  848. void
  849. xfs_iflush_abort(
  850. xfs_inode_t *ip)
  851. {
  852. xfs_inode_log_item_t *iip = ip->i_itemp;
  853. xfs_mount_t *mp;
  854. iip = ip->i_itemp;
  855. mp = ip->i_mount;
  856. if (iip) {
  857. struct xfs_ail *ailp = iip->ili_item.li_ailp;
  858. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  859. spin_lock(&ailp->xa_lock);
  860. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  861. /* xfs_trans_ail_delete() drops the AIL lock. */
  862. xfs_trans_ail_delete(ailp, (xfs_log_item_t *)iip);
  863. } else
  864. spin_unlock(&ailp->xa_lock);
  865. }
  866. iip->ili_logged = 0;
  867. /*
  868. * Clear the ili_last_fields bits now that we know that the
  869. * data corresponding to them is safely on disk.
  870. */
  871. iip->ili_last_fields = 0;
  872. /*
  873. * Clear the inode logging fields so no more flushes are
  874. * attempted.
  875. */
  876. iip->ili_format.ilf_fields = 0;
  877. }
  878. /*
  879. * Release the inode's flush lock since we're done with it.
  880. */
  881. xfs_ifunlock(ip);
  882. }
  883. void
  884. xfs_istale_done(
  885. xfs_buf_t *bp,
  886. xfs_inode_log_item_t *iip)
  887. {
  888. xfs_iflush_abort(iip->ili_inode);
  889. }
  890. /*
  891. * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
  892. * (which can have different field alignments) to the native version
  893. */
  894. int
  895. xfs_inode_item_format_convert(
  896. xfs_log_iovec_t *buf,
  897. xfs_inode_log_format_t *in_f)
  898. {
  899. if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
  900. xfs_inode_log_format_32_t *in_f32;
  901. in_f32 = (xfs_inode_log_format_32_t *)buf->i_addr;
  902. in_f->ilf_type = in_f32->ilf_type;
  903. in_f->ilf_size = in_f32->ilf_size;
  904. in_f->ilf_fields = in_f32->ilf_fields;
  905. in_f->ilf_asize = in_f32->ilf_asize;
  906. in_f->ilf_dsize = in_f32->ilf_dsize;
  907. in_f->ilf_ino = in_f32->ilf_ino;
  908. /* copy biggest field of ilf_u */
  909. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  910. in_f32->ilf_u.ilfu_uuid.__u_bits,
  911. sizeof(uuid_t));
  912. in_f->ilf_blkno = in_f32->ilf_blkno;
  913. in_f->ilf_len = in_f32->ilf_len;
  914. in_f->ilf_boffset = in_f32->ilf_boffset;
  915. return 0;
  916. } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
  917. xfs_inode_log_format_64_t *in_f64;
  918. in_f64 = (xfs_inode_log_format_64_t *)buf->i_addr;
  919. in_f->ilf_type = in_f64->ilf_type;
  920. in_f->ilf_size = in_f64->ilf_size;
  921. in_f->ilf_fields = in_f64->ilf_fields;
  922. in_f->ilf_asize = in_f64->ilf_asize;
  923. in_f->ilf_dsize = in_f64->ilf_dsize;
  924. in_f->ilf_ino = in_f64->ilf_ino;
  925. /* copy biggest field of ilf_u */
  926. memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
  927. in_f64->ilf_u.ilfu_uuid.__u_bits,
  928. sizeof(uuid_t));
  929. in_f->ilf_blkno = in_f64->ilf_blkno;
  930. in_f->ilf_len = in_f64->ilf_len;
  931. in_f->ilf_boffset = in_f64->ilf_boffset;
  932. return 0;
  933. }
  934. return EFSCORRUPTED;
  935. }