xfs_inode_item.c 30 KB

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