xfs_inode_item.c 30 KB

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