xfs_inode_item.c 30 KB

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