xfs_inode_item.c 29 KB

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