xfs_inode_item.c 30 KB

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