xfs_inode_item.c 30 KB

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