xfs_inode_item.c 30 KB

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