xfs_inode_item.c 29 KB

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