xfs_inode_item.c 29 KB

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