xfs_inode_item.c 29 KB

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