xfs_inode_item.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. /*
  33. * This file contains the implementation of the xfs_inode_log_item.
  34. * It contains the item operations used to manipulate the inode log
  35. * items as well as utility routines used by the inode specific
  36. * transaction routines.
  37. */
  38. #include "xfs.h"
  39. #include "xfs_macros.h"
  40. #include "xfs_types.h"
  41. #include "xfs_inum.h"
  42. #include "xfs_log.h"
  43. #include "xfs_trans.h"
  44. #include "xfs_buf_item.h"
  45. #include "xfs_sb.h"
  46. #include "xfs_dir.h"
  47. #include "xfs_dir2.h"
  48. #include "xfs_dmapi.h"
  49. #include "xfs_mount.h"
  50. #include "xfs_trans_priv.h"
  51. #include "xfs_ag.h"
  52. #include "xfs_alloc_btree.h"
  53. #include "xfs_bmap_btree.h"
  54. #include "xfs_ialloc_btree.h"
  55. #include "xfs_btree.h"
  56. #include "xfs_ialloc.h"
  57. #include "xfs_attr_sf.h"
  58. #include "xfs_dir_sf.h"
  59. #include "xfs_dir2_sf.h"
  60. #include "xfs_dinode.h"
  61. #include "xfs_inode_item.h"
  62. #include "xfs_inode.h"
  63. #include "xfs_rw.h"
  64. kmem_zone_t *xfs_ili_zone; /* inode log item zone */
  65. /*
  66. * This returns the number of iovecs needed to log the given inode item.
  67. *
  68. * We need one iovec for the inode log format structure, one for the
  69. * inode core, and possibly one for the inode data/extents/b-tree root
  70. * and one for the inode attribute data/extents/b-tree root.
  71. */
  72. STATIC uint
  73. xfs_inode_item_size(
  74. xfs_inode_log_item_t *iip)
  75. {
  76. uint nvecs;
  77. xfs_inode_t *ip;
  78. ip = iip->ili_inode;
  79. nvecs = 2;
  80. /*
  81. * Only log the data/extents/b-tree root if there is something
  82. * left to log.
  83. */
  84. iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
  85. switch (ip->i_d.di_format) {
  86. case XFS_DINODE_FMT_EXTENTS:
  87. iip->ili_format.ilf_fields &=
  88. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  89. XFS_ILOG_DEV | XFS_ILOG_UUID);
  90. if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) &&
  91. (ip->i_d.di_nextents > 0) &&
  92. (ip->i_df.if_bytes > 0)) {
  93. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  94. nvecs++;
  95. } else {
  96. iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT;
  97. }
  98. break;
  99. case XFS_DINODE_FMT_BTREE:
  100. ASSERT(ip->i_df.if_ext_max ==
  101. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  102. iip->ili_format.ilf_fields &=
  103. ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  104. XFS_ILOG_DEV | XFS_ILOG_UUID);
  105. if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) &&
  106. (ip->i_df.if_broot_bytes > 0)) {
  107. ASSERT(ip->i_df.if_broot != NULL);
  108. nvecs++;
  109. } else {
  110. ASSERT(!(iip->ili_format.ilf_fields &
  111. XFS_ILOG_DBROOT));
  112. #ifdef XFS_TRANS_DEBUG
  113. if (iip->ili_root_size > 0) {
  114. ASSERT(iip->ili_root_size ==
  115. ip->i_df.if_broot_bytes);
  116. ASSERT(memcmp(iip->ili_orig_root,
  117. ip->i_df.if_broot,
  118. iip->ili_root_size) == 0);
  119. } else {
  120. ASSERT(ip->i_df.if_broot_bytes == 0);
  121. }
  122. #endif
  123. iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT;
  124. }
  125. break;
  126. case XFS_DINODE_FMT_LOCAL:
  127. iip->ili_format.ilf_fields &=
  128. ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
  129. XFS_ILOG_DEV | XFS_ILOG_UUID);
  130. if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) &&
  131. (ip->i_df.if_bytes > 0)) {
  132. ASSERT(ip->i_df.if_u1.if_data != NULL);
  133. ASSERT(ip->i_d.di_size > 0);
  134. nvecs++;
  135. } else {
  136. iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA;
  137. }
  138. break;
  139. case XFS_DINODE_FMT_DEV:
  140. iip->ili_format.ilf_fields &=
  141. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  142. XFS_ILOG_DEXT | XFS_ILOG_UUID);
  143. break;
  144. case XFS_DINODE_FMT_UUID:
  145. iip->ili_format.ilf_fields &=
  146. ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  147. XFS_ILOG_DEXT | XFS_ILOG_DEV);
  148. break;
  149. default:
  150. ASSERT(0);
  151. break;
  152. }
  153. /*
  154. * If there are no attributes associated with this file,
  155. * then there cannot be anything more to log.
  156. * Clear all attribute-related log flags.
  157. */
  158. if (!XFS_IFORK_Q(ip)) {
  159. iip->ili_format.ilf_fields &=
  160. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
  161. return nvecs;
  162. }
  163. /*
  164. * Log any necessary attribute data.
  165. */
  166. switch (ip->i_d.di_aformat) {
  167. case XFS_DINODE_FMT_EXTENTS:
  168. iip->ili_format.ilf_fields &=
  169. ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
  170. if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) &&
  171. (ip->i_d.di_anextents > 0) &&
  172. (ip->i_afp->if_bytes > 0)) {
  173. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  174. nvecs++;
  175. } else {
  176. iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT;
  177. }
  178. break;
  179. case XFS_DINODE_FMT_BTREE:
  180. iip->ili_format.ilf_fields &=
  181. ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
  182. if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) &&
  183. (ip->i_afp->if_broot_bytes > 0)) {
  184. ASSERT(ip->i_afp->if_broot != NULL);
  185. nvecs++;
  186. } else {
  187. iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT;
  188. }
  189. break;
  190. case XFS_DINODE_FMT_LOCAL:
  191. iip->ili_format.ilf_fields &=
  192. ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
  193. if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) &&
  194. (ip->i_afp->if_bytes > 0)) {
  195. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  196. nvecs++;
  197. } else {
  198. iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA;
  199. }
  200. break;
  201. default:
  202. ASSERT(0);
  203. break;
  204. }
  205. return nvecs;
  206. }
  207. /*
  208. * This is called to fill in the vector of log iovecs for the
  209. * given inode log item. It fills the first item with an inode
  210. * log format structure, the second with the on-disk inode structure,
  211. * and a possible third and/or fourth with the inode data/extents/b-tree
  212. * root and inode attributes data/extents/b-tree root.
  213. */
  214. STATIC void
  215. xfs_inode_item_format(
  216. xfs_inode_log_item_t *iip,
  217. xfs_log_iovec_t *log_vector)
  218. {
  219. uint nvecs;
  220. xfs_log_iovec_t *vecp;
  221. xfs_inode_t *ip;
  222. size_t data_bytes;
  223. xfs_bmbt_rec_t *ext_buffer;
  224. int nrecs;
  225. xfs_mount_t *mp;
  226. ip = iip->ili_inode;
  227. vecp = log_vector;
  228. vecp->i_addr = (xfs_caddr_t)&iip->ili_format;
  229. vecp->i_len = sizeof(xfs_inode_log_format_t);
  230. XLOG_VEC_SET_TYPE(vecp, 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. * We don't have to worry about re-ordering here because
  265. * the update_size field is protected by the inode lock
  266. * and we have that held in exclusive mode.
  267. */
  268. if (ip->i_update_size)
  269. ip->i_update_size = 0;
  270. vecp->i_addr = (xfs_caddr_t)&ip->i_d;
  271. vecp->i_len = sizeof(xfs_dinode_core_t);
  272. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ICORE);
  273. vecp++;
  274. nvecs++;
  275. iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
  276. /*
  277. * If this is really an old format inode, then we need to
  278. * log it as such. This means that we have to copy the link
  279. * count from the new field to the old. We don't have to worry
  280. * about the new fields, because nothing trusts them as long as
  281. * the old inode version number is there. If the superblock already
  282. * has a new version number, then we don't bother converting back.
  283. */
  284. mp = ip->i_mount;
  285. ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
  286. XFS_SB_VERSION_HASNLINK(&mp->m_sb));
  287. if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
  288. if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
  289. /*
  290. * Convert it back.
  291. */
  292. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  293. ip->i_d.di_onlink = ip->i_d.di_nlink;
  294. } else {
  295. /*
  296. * The superblock version has already been bumped,
  297. * so just make the conversion to the new inode
  298. * format permanent.
  299. */
  300. ip->i_d.di_version = XFS_DINODE_VERSION_2;
  301. ip->i_d.di_onlink = 0;
  302. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  303. }
  304. }
  305. switch (ip->i_d.di_format) {
  306. case XFS_DINODE_FMT_EXTENTS:
  307. ASSERT(!(iip->ili_format.ilf_fields &
  308. (XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
  309. XFS_ILOG_DEV | XFS_ILOG_UUID)));
  310. if (iip->ili_format.ilf_fields & XFS_ILOG_DEXT) {
  311. ASSERT(ip->i_df.if_bytes > 0);
  312. ASSERT(ip->i_df.if_u1.if_extents != NULL);
  313. ASSERT(ip->i_d.di_nextents > 0);
  314. ASSERT(iip->ili_extents_buf == NULL);
  315. nrecs = ip->i_df.if_bytes /
  316. (uint)sizeof(xfs_bmbt_rec_t);
  317. ASSERT(nrecs > 0);
  318. #ifdef XFS_NATIVE_HOST
  319. if (nrecs == ip->i_d.di_nextents) {
  320. /*
  321. * There are no delayed allocation
  322. * extents, so just point to the
  323. * real extents array.
  324. */
  325. vecp->i_addr =
  326. (char *)(ip->i_df.if_u1.if_extents);
  327. vecp->i_len = ip->i_df.if_bytes;
  328. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT);
  329. } else
  330. #endif
  331. {
  332. /*
  333. * There are delayed allocation extents
  334. * in the inode, or we need to convert
  335. * the extents to on disk format.
  336. * Use xfs_iextents_copy()
  337. * to copy only the real extents into
  338. * a separate buffer. We'll free the
  339. * buffer in the unlock routine.
  340. */
  341. ext_buffer = kmem_alloc(ip->i_df.if_bytes,
  342. KM_SLEEP);
  343. iip->ili_extents_buf = ext_buffer;
  344. vecp->i_addr = (xfs_caddr_t)ext_buffer;
  345. vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
  346. XFS_DATA_FORK);
  347. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT);
  348. }
  349. ASSERT(vecp->i_len <= ip->i_df.if_bytes);
  350. iip->ili_format.ilf_dsize = vecp->i_len;
  351. vecp++;
  352. nvecs++;
  353. }
  354. break;
  355. case XFS_DINODE_FMT_BTREE:
  356. ASSERT(!(iip->ili_format.ilf_fields &
  357. (XFS_ILOG_DDATA | XFS_ILOG_DEXT |
  358. XFS_ILOG_DEV | XFS_ILOG_UUID)));
  359. if (iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) {
  360. ASSERT(ip->i_df.if_broot_bytes > 0);
  361. ASSERT(ip->i_df.if_broot != NULL);
  362. vecp->i_addr = (xfs_caddr_t)ip->i_df.if_broot;
  363. vecp->i_len = ip->i_df.if_broot_bytes;
  364. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IBROOT);
  365. vecp++;
  366. nvecs++;
  367. iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
  368. }
  369. break;
  370. case XFS_DINODE_FMT_LOCAL:
  371. ASSERT(!(iip->ili_format.ilf_fields &
  372. (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
  373. XFS_ILOG_DEV | XFS_ILOG_UUID)));
  374. if (iip->ili_format.ilf_fields & XFS_ILOG_DDATA) {
  375. ASSERT(ip->i_df.if_bytes > 0);
  376. ASSERT(ip->i_df.if_u1.if_data != NULL);
  377. ASSERT(ip->i_d.di_size > 0);
  378. vecp->i_addr = (xfs_caddr_t)ip->i_df.if_u1.if_data;
  379. /*
  380. * Round i_bytes up to a word boundary.
  381. * The underlying memory is guaranteed to
  382. * to be there by xfs_idata_realloc().
  383. */
  384. data_bytes = roundup(ip->i_df.if_bytes, 4);
  385. ASSERT((ip->i_df.if_real_bytes == 0) ||
  386. (ip->i_df.if_real_bytes == data_bytes));
  387. vecp->i_len = (int)data_bytes;
  388. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ILOCAL);
  389. vecp++;
  390. nvecs++;
  391. iip->ili_format.ilf_dsize = (unsigned)data_bytes;
  392. }
  393. break;
  394. case XFS_DINODE_FMT_DEV:
  395. ASSERT(!(iip->ili_format.ilf_fields &
  396. (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
  397. XFS_ILOG_DDATA | XFS_ILOG_UUID)));
  398. if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
  399. iip->ili_format.ilf_u.ilfu_rdev =
  400. ip->i_df.if_u2.if_rdev;
  401. }
  402. break;
  403. case XFS_DINODE_FMT_UUID:
  404. ASSERT(!(iip->ili_format.ilf_fields &
  405. (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
  406. XFS_ILOG_DDATA | XFS_ILOG_DEV)));
  407. if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
  408. iip->ili_format.ilf_u.ilfu_uuid =
  409. ip->i_df.if_u2.if_uuid;
  410. }
  411. break;
  412. default:
  413. ASSERT(0);
  414. break;
  415. }
  416. /*
  417. * If there are no attributes associated with the file,
  418. * then we're done.
  419. * Assert that no attribute-related log flags are set.
  420. */
  421. if (!XFS_IFORK_Q(ip)) {
  422. ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
  423. iip->ili_format.ilf_size = nvecs;
  424. ASSERT(!(iip->ili_format.ilf_fields &
  425. (XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
  426. return;
  427. }
  428. switch (ip->i_d.di_aformat) {
  429. case XFS_DINODE_FMT_EXTENTS:
  430. ASSERT(!(iip->ili_format.ilf_fields &
  431. (XFS_ILOG_ADATA | XFS_ILOG_ABROOT)));
  432. if (iip->ili_format.ilf_fields & XFS_ILOG_AEXT) {
  433. ASSERT(ip->i_afp->if_bytes > 0);
  434. ASSERT(ip->i_afp->if_u1.if_extents != NULL);
  435. ASSERT(ip->i_d.di_anextents > 0);
  436. #ifdef DEBUG
  437. nrecs = ip->i_afp->if_bytes /
  438. (uint)sizeof(xfs_bmbt_rec_t);
  439. #endif
  440. ASSERT(nrecs > 0);
  441. ASSERT(nrecs == ip->i_d.di_anextents);
  442. #ifdef XFS_NATIVE_HOST
  443. /*
  444. * There are not delayed allocation extents
  445. * for attributes, so just point at the array.
  446. */
  447. vecp->i_addr = (char *)(ip->i_afp->if_u1.if_extents);
  448. vecp->i_len = ip->i_afp->if_bytes;
  449. #else
  450. ASSERT(iip->ili_aextents_buf == NULL);
  451. /*
  452. * Need to endian flip before logging
  453. */
  454. ext_buffer = kmem_alloc(ip->i_afp->if_bytes,
  455. KM_SLEEP);
  456. iip->ili_aextents_buf = ext_buffer;
  457. vecp->i_addr = (xfs_caddr_t)ext_buffer;
  458. vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
  459. XFS_ATTR_FORK);
  460. #endif
  461. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_EXT);
  462. iip->ili_format.ilf_asize = vecp->i_len;
  463. vecp++;
  464. nvecs++;
  465. }
  466. break;
  467. case XFS_DINODE_FMT_BTREE:
  468. ASSERT(!(iip->ili_format.ilf_fields &
  469. (XFS_ILOG_ADATA | XFS_ILOG_AEXT)));
  470. if (iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) {
  471. ASSERT(ip->i_afp->if_broot_bytes > 0);
  472. ASSERT(ip->i_afp->if_broot != NULL);
  473. vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_broot;
  474. vecp->i_len = ip->i_afp->if_broot_bytes;
  475. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_BROOT);
  476. vecp++;
  477. nvecs++;
  478. iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
  479. }
  480. break;
  481. case XFS_DINODE_FMT_LOCAL:
  482. ASSERT(!(iip->ili_format.ilf_fields &
  483. (XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
  484. if (iip->ili_format.ilf_fields & XFS_ILOG_ADATA) {
  485. ASSERT(ip->i_afp->if_bytes > 0);
  486. ASSERT(ip->i_afp->if_u1.if_data != NULL);
  487. vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_u1.if_data;
  488. /*
  489. * Round i_bytes up to a word boundary.
  490. * The underlying memory is guaranteed to
  491. * to be there by xfs_idata_realloc().
  492. */
  493. data_bytes = roundup(ip->i_afp->if_bytes, 4);
  494. ASSERT((ip->i_afp->if_real_bytes == 0) ||
  495. (ip->i_afp->if_real_bytes == data_bytes));
  496. vecp->i_len = (int)data_bytes;
  497. XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_LOCAL);
  498. vecp++;
  499. nvecs++;
  500. iip->ili_format.ilf_asize = (unsigned)data_bytes;
  501. }
  502. break;
  503. default:
  504. ASSERT(0);
  505. break;
  506. }
  507. ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
  508. iip->ili_format.ilf_size = nvecs;
  509. }
  510. /*
  511. * This is called to pin the inode associated with the inode log
  512. * item in memory so it cannot be written out. Do this by calling
  513. * xfs_ipin() to bump the pin count in the inode while holding the
  514. * inode pin lock.
  515. */
  516. STATIC void
  517. xfs_inode_item_pin(
  518. xfs_inode_log_item_t *iip)
  519. {
  520. ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
  521. xfs_ipin(iip->ili_inode);
  522. }
  523. /*
  524. * This is called to unpin the inode associated with the inode log
  525. * item which was previously pinned with a call to xfs_inode_item_pin().
  526. * Just call xfs_iunpin() on the inode to do this.
  527. */
  528. /* ARGSUSED */
  529. STATIC void
  530. xfs_inode_item_unpin(
  531. xfs_inode_log_item_t *iip,
  532. int stale)
  533. {
  534. xfs_iunpin(iip->ili_inode);
  535. }
  536. /* ARGSUSED */
  537. STATIC void
  538. xfs_inode_item_unpin_remove(
  539. xfs_inode_log_item_t *iip,
  540. xfs_trans_t *tp)
  541. {
  542. xfs_iunpin(iip->ili_inode);
  543. }
  544. /*
  545. * This is called to attempt to lock the inode associated with this
  546. * inode log item, in preparation for the push routine which does the actual
  547. * iflush. Don't sleep on the inode lock or the flush lock.
  548. *
  549. * If the flush lock is already held, indicating that the inode has
  550. * been or is in the process of being flushed, then (ideally) we'd like to
  551. * see if the inode's buffer is still incore, and if so give it a nudge.
  552. * We delay doing so until the pushbuf routine, though, to avoid holding
  553. * the AIL lock across a call to the blackhole which is the buffercache.
  554. * Also we don't want to sleep in any device strategy routines, which can happen
  555. * if we do the subsequent bawrite in here.
  556. */
  557. STATIC uint
  558. xfs_inode_item_trylock(
  559. xfs_inode_log_item_t *iip)
  560. {
  561. register xfs_inode_t *ip;
  562. ip = iip->ili_inode;
  563. if (xfs_ipincount(ip) > 0) {
  564. return XFS_ITEM_PINNED;
  565. }
  566. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  567. return XFS_ITEM_LOCKED;
  568. }
  569. if (!xfs_iflock_nowait(ip)) {
  570. /*
  571. * If someone else isn't already trying to push the inode
  572. * buffer, we get to do it.
  573. */
  574. if (iip->ili_pushbuf_flag == 0) {
  575. iip->ili_pushbuf_flag = 1;
  576. #ifdef DEBUG
  577. iip->ili_push_owner = get_thread_id();
  578. #endif
  579. /*
  580. * Inode is left locked in shared mode.
  581. * Pushbuf routine gets to unlock it.
  582. */
  583. return XFS_ITEM_PUSHBUF;
  584. } else {
  585. /*
  586. * We hold the AIL_LOCK, so we must specify the
  587. * NONOTIFY flag so that we won't double trip.
  588. */
  589. xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
  590. return XFS_ITEM_FLUSHING;
  591. }
  592. /* NOTREACHED */
  593. }
  594. /* Stale items should force out the iclog */
  595. if (ip->i_flags & XFS_ISTALE) {
  596. xfs_ifunlock(ip);
  597. xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
  598. return XFS_ITEM_PINNED;
  599. }
  600. #ifdef DEBUG
  601. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  602. ASSERT(iip->ili_format.ilf_fields != 0);
  603. ASSERT(iip->ili_logged == 0);
  604. ASSERT(iip->ili_item.li_flags & XFS_LI_IN_AIL);
  605. }
  606. #endif
  607. return XFS_ITEM_SUCCESS;
  608. }
  609. /*
  610. * Unlock the inode associated with the inode log item.
  611. * Clear the fields of the inode and inode log item that
  612. * are specific to the current transaction. If the
  613. * hold flags is set, do not unlock the inode.
  614. */
  615. STATIC void
  616. xfs_inode_item_unlock(
  617. xfs_inode_log_item_t *iip)
  618. {
  619. uint hold;
  620. uint iolocked;
  621. uint lock_flags;
  622. xfs_inode_t *ip;
  623. ASSERT(iip != NULL);
  624. ASSERT(iip->ili_inode->i_itemp != NULL);
  625. ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
  626. ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
  627. XFS_ILI_IOLOCKED_EXCL)) ||
  628. ismrlocked(&(iip->ili_inode->i_iolock), MR_UPDATE));
  629. ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
  630. XFS_ILI_IOLOCKED_SHARED)) ||
  631. ismrlocked(&(iip->ili_inode->i_iolock), MR_ACCESS));
  632. /*
  633. * Clear the transaction pointer in the inode.
  634. */
  635. ip = iip->ili_inode;
  636. ip->i_transp = NULL;
  637. /*
  638. * If the inode needed a separate buffer with which to log
  639. * its extents, then free it now.
  640. */
  641. if (iip->ili_extents_buf != NULL) {
  642. ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
  643. ASSERT(ip->i_d.di_nextents > 0);
  644. ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
  645. ASSERT(ip->i_df.if_bytes > 0);
  646. kmem_free(iip->ili_extents_buf, ip->i_df.if_bytes);
  647. iip->ili_extents_buf = NULL;
  648. }
  649. if (iip->ili_aextents_buf != NULL) {
  650. ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
  651. ASSERT(ip->i_d.di_anextents > 0);
  652. ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
  653. ASSERT(ip->i_afp->if_bytes > 0);
  654. kmem_free(iip->ili_aextents_buf, ip->i_afp->if_bytes);
  655. iip->ili_aextents_buf = NULL;
  656. }
  657. /*
  658. * Figure out if we should unlock the inode or not.
  659. */
  660. hold = iip->ili_flags & XFS_ILI_HOLD;
  661. /*
  662. * Before clearing out the flags, remember whether we
  663. * are holding the inode's IO lock.
  664. */
  665. iolocked = iip->ili_flags & XFS_ILI_IOLOCKED_ANY;
  666. /*
  667. * Clear out the fields of the inode log item particular
  668. * to the current transaction.
  669. */
  670. iip->ili_ilock_recur = 0;
  671. iip->ili_iolock_recur = 0;
  672. iip->ili_flags = 0;
  673. /*
  674. * Unlock the inode if XFS_ILI_HOLD was not set.
  675. */
  676. if (!hold) {
  677. lock_flags = XFS_ILOCK_EXCL;
  678. if (iolocked & XFS_ILI_IOLOCKED_EXCL) {
  679. lock_flags |= XFS_IOLOCK_EXCL;
  680. } else if (iolocked & XFS_ILI_IOLOCKED_SHARED) {
  681. lock_flags |= XFS_IOLOCK_SHARED;
  682. }
  683. xfs_iput(iip->ili_inode, lock_flags);
  684. }
  685. }
  686. /*
  687. * This is called to find out where the oldest active copy of the
  688. * inode log item in the on disk log resides now that the last log
  689. * write of it completed at the given lsn. Since we always re-log
  690. * all dirty data in an inode, the latest copy in the on disk log
  691. * is the only one that matters. Therefore, simply return the
  692. * given lsn.
  693. */
  694. /*ARGSUSED*/
  695. STATIC xfs_lsn_t
  696. xfs_inode_item_committed(
  697. xfs_inode_log_item_t *iip,
  698. xfs_lsn_t lsn)
  699. {
  700. return (lsn);
  701. }
  702. /*
  703. * The transaction with the inode locked has aborted. The inode
  704. * must not be dirty within the transaction (unless we're forcibly
  705. * shutting down). We simply unlock just as if the transaction
  706. * had been cancelled.
  707. */
  708. STATIC void
  709. xfs_inode_item_abort(
  710. xfs_inode_log_item_t *iip)
  711. {
  712. xfs_inode_item_unlock(iip);
  713. return;
  714. }
  715. /*
  716. * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
  717. * failed to get the inode flush lock but did get the inode locked SHARED.
  718. * Here we're trying to see if the inode buffer is incore, and if so whether it's
  719. * marked delayed write. If that's the case, we'll initiate a bawrite on that
  720. * buffer to expedite the process.
  721. *
  722. * We aren't holding the AIL_LOCK (or the flush lock) when this gets called,
  723. * so it is inherently race-y.
  724. */
  725. STATIC void
  726. xfs_inode_item_pushbuf(
  727. xfs_inode_log_item_t *iip)
  728. {
  729. xfs_inode_t *ip;
  730. xfs_mount_t *mp;
  731. xfs_buf_t *bp;
  732. uint dopush;
  733. ip = iip->ili_inode;
  734. ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
  735. /*
  736. * The ili_pushbuf_flag keeps others from
  737. * trying to duplicate our effort.
  738. */
  739. ASSERT(iip->ili_pushbuf_flag != 0);
  740. ASSERT(iip->ili_push_owner == get_thread_id());
  741. /*
  742. * If flushlock isn't locked anymore, chances are that the
  743. * inode flush completed and the inode was taken off the AIL.
  744. * So, just get out.
  745. */
  746. if ((valusema(&(ip->i_flock)) > 0) ||
  747. ((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0)) {
  748. iip->ili_pushbuf_flag = 0;
  749. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  750. return;
  751. }
  752. mp = ip->i_mount;
  753. bp = xfs_incore(mp->m_ddev_targp, iip->ili_format.ilf_blkno,
  754. iip->ili_format.ilf_len, XFS_INCORE_TRYLOCK);
  755. if (bp != NULL) {
  756. if (XFS_BUF_ISDELAYWRITE(bp)) {
  757. /*
  758. * We were racing with iflush because we don't hold
  759. * the AIL_LOCK or the flush lock. However, at this point,
  760. * we have the buffer, and we know that it's dirty.
  761. * So, it's possible that iflush raced with us, and
  762. * this item is already taken off the AIL.
  763. * If not, we can flush it async.
  764. */
  765. dopush = ((iip->ili_item.li_flags & XFS_LI_IN_AIL) &&
  766. (valusema(&(ip->i_flock)) <= 0));
  767. iip->ili_pushbuf_flag = 0;
  768. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  769. xfs_buftrace("INODE ITEM PUSH", bp);
  770. if (XFS_BUF_ISPINNED(bp)) {
  771. xfs_log_force(mp, (xfs_lsn_t)0,
  772. XFS_LOG_FORCE);
  773. }
  774. if (dopush) {
  775. xfs_bawrite(mp, bp);
  776. } else {
  777. xfs_buf_relse(bp);
  778. }
  779. } else {
  780. iip->ili_pushbuf_flag = 0;
  781. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  782. xfs_buf_relse(bp);
  783. }
  784. return;
  785. }
  786. /*
  787. * We have to be careful about resetting pushbuf flag too early (above).
  788. * Even though in theory we can do it as soon as we have the buflock,
  789. * we don't want others to be doing work needlessly. They'll come to
  790. * this function thinking that pushing the buffer is their
  791. * responsibility only to find that the buffer is still locked by
  792. * another doing the same thing
  793. */
  794. iip->ili_pushbuf_flag = 0;
  795. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  796. return;
  797. }
  798. /*
  799. * This is called to asynchronously write the inode associated with this
  800. * inode log item out to disk. The inode will already have been locked by
  801. * a successful call to xfs_inode_item_trylock().
  802. */
  803. STATIC void
  804. xfs_inode_item_push(
  805. xfs_inode_log_item_t *iip)
  806. {
  807. xfs_inode_t *ip;
  808. ip = iip->ili_inode;
  809. ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
  810. ASSERT(valusema(&(ip->i_flock)) <= 0);
  811. /*
  812. * Since we were able to lock the inode's flush lock and
  813. * we found it on the AIL, the inode must be dirty. This
  814. * is because the inode is removed from the AIL while still
  815. * holding the flush lock in xfs_iflush_done(). Thus, if
  816. * we found it in the AIL and were able to obtain the flush
  817. * lock without sleeping, then there must not have been
  818. * anyone in the process of flushing the inode.
  819. */
  820. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
  821. iip->ili_format.ilf_fields != 0);
  822. /*
  823. * Write out the inode. The completion routine ('iflush_done') will
  824. * pull it from the AIL, mark it clean, unlock the flush lock.
  825. */
  826. (void) xfs_iflush(ip, XFS_IFLUSH_ASYNC);
  827. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  828. return;
  829. }
  830. /*
  831. * XXX rcc - this one really has to do something. Probably needs
  832. * to stamp in a new field in the incore inode.
  833. */
  834. /* ARGSUSED */
  835. STATIC void
  836. xfs_inode_item_committing(
  837. xfs_inode_log_item_t *iip,
  838. xfs_lsn_t lsn)
  839. {
  840. iip->ili_last_lsn = lsn;
  841. return;
  842. }
  843. /*
  844. * This is the ops vector shared by all buf log items.
  845. */
  846. STATIC struct xfs_item_ops xfs_inode_item_ops = {
  847. .iop_size = (uint(*)(xfs_log_item_t*))xfs_inode_item_size,
  848. .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
  849. xfs_inode_item_format,
  850. .iop_pin = (void(*)(xfs_log_item_t*))xfs_inode_item_pin,
  851. .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_inode_item_unpin,
  852. .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
  853. xfs_inode_item_unpin_remove,
  854. .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_inode_item_trylock,
  855. .iop_unlock = (void(*)(xfs_log_item_t*))xfs_inode_item_unlock,
  856. .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
  857. xfs_inode_item_committed,
  858. .iop_push = (void(*)(xfs_log_item_t*))xfs_inode_item_push,
  859. .iop_abort = (void(*)(xfs_log_item_t*))xfs_inode_item_abort,
  860. .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_inode_item_pushbuf,
  861. .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
  862. xfs_inode_item_committing
  863. };
  864. /*
  865. * Initialize the inode log item for a newly allocated (in-core) inode.
  866. */
  867. void
  868. xfs_inode_item_init(
  869. xfs_inode_t *ip,
  870. xfs_mount_t *mp)
  871. {
  872. xfs_inode_log_item_t *iip;
  873. ASSERT(ip->i_itemp == NULL);
  874. iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
  875. iip->ili_item.li_type = XFS_LI_INODE;
  876. iip->ili_item.li_ops = &xfs_inode_item_ops;
  877. iip->ili_item.li_mountp = mp;
  878. iip->ili_inode = ip;
  879. /*
  880. We have zeroed memory. No need ...
  881. iip->ili_extents_buf = NULL;
  882. iip->ili_pushbuf_flag = 0;
  883. */
  884. iip->ili_format.ilf_type = XFS_LI_INODE;
  885. iip->ili_format.ilf_ino = ip->i_ino;
  886. iip->ili_format.ilf_blkno = ip->i_blkno;
  887. iip->ili_format.ilf_len = ip->i_len;
  888. iip->ili_format.ilf_boffset = ip->i_boffset;
  889. }
  890. /*
  891. * Free the inode log item and any memory hanging off of it.
  892. */
  893. void
  894. xfs_inode_item_destroy(
  895. xfs_inode_t *ip)
  896. {
  897. #ifdef XFS_TRANS_DEBUG
  898. if (ip->i_itemp->ili_root_size != 0) {
  899. kmem_free(ip->i_itemp->ili_orig_root,
  900. ip->i_itemp->ili_root_size);
  901. }
  902. #endif
  903. kmem_zone_free(xfs_ili_zone, ip->i_itemp);
  904. }
  905. /*
  906. * This is the inode flushing I/O completion routine. It is called
  907. * from interrupt level when the buffer containing the inode is
  908. * flushed to disk. It is responsible for removing the inode item
  909. * from the AIL if it has not been re-logged, and unlocking the inode's
  910. * flush lock.
  911. */
  912. /*ARGSUSED*/
  913. void
  914. xfs_iflush_done(
  915. xfs_buf_t *bp,
  916. xfs_inode_log_item_t *iip)
  917. {
  918. xfs_inode_t *ip;
  919. SPLDECL(s);
  920. ip = iip->ili_inode;
  921. /*
  922. * We only want to pull the item from the AIL if it is
  923. * actually there and its location in the log has not
  924. * changed since we started the flush. Thus, we only bother
  925. * if the ili_logged flag is set and the inode's lsn has not
  926. * changed. First we check the lsn outside
  927. * the lock since it's cheaper, and then we recheck while
  928. * holding the lock before removing the inode from the AIL.
  929. */
  930. if (iip->ili_logged &&
  931. (iip->ili_item.li_lsn == iip->ili_flush_lsn)) {
  932. AIL_LOCK(ip->i_mount, s);
  933. if (iip->ili_item.li_lsn == iip->ili_flush_lsn) {
  934. /*
  935. * xfs_trans_delete_ail() drops the AIL lock.
  936. */
  937. xfs_trans_delete_ail(ip->i_mount,
  938. (xfs_log_item_t*)iip, s);
  939. } else {
  940. AIL_UNLOCK(ip->i_mount, s);
  941. }
  942. }
  943. iip->ili_logged = 0;
  944. /*
  945. * Clear the ili_last_fields bits now that we know that the
  946. * data corresponding to them is safely on disk.
  947. */
  948. iip->ili_last_fields = 0;
  949. /*
  950. * Release the inode's flush lock since we're done with it.
  951. */
  952. xfs_ifunlock(ip);
  953. return;
  954. }
  955. /*
  956. * This is the inode flushing abort routine. It is called
  957. * from xfs_iflush when the filesystem is shutting down to clean
  958. * up the inode state.
  959. * It is responsible for removing the inode item
  960. * from the AIL if it has not been re-logged, and unlocking the inode's
  961. * flush lock.
  962. */
  963. void
  964. xfs_iflush_abort(
  965. xfs_inode_t *ip)
  966. {
  967. xfs_inode_log_item_t *iip;
  968. xfs_mount_t *mp;
  969. SPLDECL(s);
  970. iip = ip->i_itemp;
  971. mp = ip->i_mount;
  972. if (iip) {
  973. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  974. AIL_LOCK(mp, s);
  975. if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
  976. /*
  977. * xfs_trans_delete_ail() drops the AIL lock.
  978. */
  979. xfs_trans_delete_ail(mp, (xfs_log_item_t *)iip,
  980. s);
  981. } else
  982. AIL_UNLOCK(mp, s);
  983. }
  984. iip->ili_logged = 0;
  985. /*
  986. * Clear the ili_last_fields bits now that we know that the
  987. * data corresponding to them is safely on disk.
  988. */
  989. iip->ili_last_fields = 0;
  990. /*
  991. * Clear the inode logging fields so no more flushes are
  992. * attempted.
  993. */
  994. iip->ili_format.ilf_fields = 0;
  995. }
  996. /*
  997. * Release the inode's flush lock since we're done with it.
  998. */
  999. xfs_ifunlock(ip);
  1000. }
  1001. void
  1002. xfs_istale_done(
  1003. xfs_buf_t *bp,
  1004. xfs_inode_log_item_t *iip)
  1005. {
  1006. xfs_iflush_abort(iip->ili_inode);
  1007. }