xfs_inode_item.c 30 KB

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