xfs_inode_item.c 30 KB

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