xfs_log_cil.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2010 Red Hat, Inc. All Rights Reserved.
  4. */
  5. #include "xfs.h"
  6. #include "xfs_fs.h"
  7. #include "xfs_format.h"
  8. #include "xfs_log_format.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_extent_busy.h"
  13. #include "xfs_trans.h"
  14. #include "xfs_trans_priv.h"
  15. #include "xfs_log.h"
  16. #include "xfs_log_priv.h"
  17. #include "xfs_trace.h"
  18. struct workqueue_struct *xfs_discard_wq;
  19. /*
  20. * Allocate a new ticket. Failing to get a new ticket makes it really hard to
  21. * recover, so we don't allow failure here. Also, we allocate in a context that
  22. * we don't want to be issuing transactions from, so we need to tell the
  23. * allocation code this as well.
  24. *
  25. * We don't reserve any space for the ticket - we are going to steal whatever
  26. * space we require from transactions as they commit. To ensure we reserve all
  27. * the space required, we need to set the current reservation of the ticket to
  28. * zero so that we know to steal the initial transaction overhead from the
  29. * first transaction commit.
  30. */
  31. static struct xlog_ticket *
  32. xlog_cil_ticket_alloc(
  33. struct xlog *log)
  34. {
  35. struct xlog_ticket *tic;
  36. tic = xlog_ticket_alloc(log, 0, 1, 0);
  37. /*
  38. * set the current reservation to zero so we know to steal the basic
  39. * transaction overhead reservation from the first transaction commit.
  40. */
  41. tic->t_curr_res = 0;
  42. tic->t_iclog_hdrs = 0;
  43. return tic;
  44. }
  45. static inline void
  46. xlog_cil_set_iclog_hdr_count(struct xfs_cil *cil)
  47. {
  48. struct xlog *log = cil->xc_log;
  49. atomic_set(&cil->xc_iclog_hdrs,
  50. (XLOG_CIL_BLOCKING_SPACE_LIMIT(log) /
  51. (log->l_iclog_size - log->l_iclog_hsize)));
  52. }
  53. /*
  54. * Check if the current log item was first committed in this sequence.
  55. * We can't rely on just the log item being in the CIL, we have to check
  56. * the recorded commit sequence number.
  57. *
  58. * Note: for this to be used in a non-racy manner, it has to be called with
  59. * CIL flushing locked out. As a result, it should only be used during the
  60. * transaction commit process when deciding what to format into the item.
  61. */
  62. static bool
  63. xlog_item_in_current_chkpt(
  64. struct xfs_cil *cil,
  65. struct xfs_log_item *lip)
  66. {
  67. if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
  68. return false;
  69. /*
  70. * li_seq is written on the first commit of a log item to record the
  71. * first checkpoint it is written to. Hence if it is different to the
  72. * current sequence, we're in a new checkpoint.
  73. */
  74. return lip->li_seq == READ_ONCE(cil->xc_current_sequence);
  75. }
  76. bool
  77. xfs_log_item_in_current_chkpt(
  78. struct xfs_log_item *lip)
  79. {
  80. return xlog_item_in_current_chkpt(lip->li_log->l_cilp, lip);
  81. }
  82. /*
  83. * Unavoidable forward declaration - xlog_cil_push_work() calls
  84. * xlog_cil_ctx_alloc() itself.
  85. */
  86. static void xlog_cil_push_work(struct work_struct *work);
  87. static struct xfs_cil_ctx *
  88. xlog_cil_ctx_alloc(void)
  89. {
  90. struct xfs_cil_ctx *ctx;
  91. ctx = kmem_zalloc(sizeof(*ctx), KM_NOFS);
  92. INIT_LIST_HEAD(&ctx->committing);
  93. INIT_LIST_HEAD(&ctx->busy_extents);
  94. INIT_LIST_HEAD(&ctx->log_items);
  95. INIT_LIST_HEAD(&ctx->lv_chain);
  96. INIT_WORK(&ctx->push_work, xlog_cil_push_work);
  97. return ctx;
  98. }
  99. /*
  100. * Aggregate the CIL per cpu structures into global counts, lists, etc and
  101. * clear the percpu state ready for the next context to use. This is called
  102. * from the push code with the context lock held exclusively, hence nothing else
  103. * will be accessing or modifying the per-cpu counters.
  104. */
  105. static void
  106. xlog_cil_push_pcp_aggregate(
  107. struct xfs_cil *cil,
  108. struct xfs_cil_ctx *ctx)
  109. {
  110. struct xlog_cil_pcp *cilpcp;
  111. int cpu;
  112. for_each_online_cpu(cpu) {
  113. cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
  114. ctx->ticket->t_curr_res += cilpcp->space_reserved;
  115. cilpcp->space_reserved = 0;
  116. if (!list_empty(&cilpcp->busy_extents)) {
  117. list_splice_init(&cilpcp->busy_extents,
  118. &ctx->busy_extents);
  119. }
  120. if (!list_empty(&cilpcp->log_items))
  121. list_splice_init(&cilpcp->log_items, &ctx->log_items);
  122. /*
  123. * We're in the middle of switching cil contexts. Reset the
  124. * counter we use to detect when the current context is nearing
  125. * full.
  126. */
  127. cilpcp->space_used = 0;
  128. }
  129. }
  130. /*
  131. * Aggregate the CIL per-cpu space used counters into the global atomic value.
  132. * This is called when the per-cpu counter aggregation will first pass the soft
  133. * limit threshold so we can switch to atomic counter aggregation for accurate
  134. * detection of hard limit traversal.
  135. */
  136. static void
  137. xlog_cil_insert_pcp_aggregate(
  138. struct xfs_cil *cil,
  139. struct xfs_cil_ctx *ctx)
  140. {
  141. struct xlog_cil_pcp *cilpcp;
  142. int cpu;
  143. int count = 0;
  144. /* Trigger atomic updates then aggregate only for the first caller */
  145. if (!test_and_clear_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags))
  146. return;
  147. for_each_online_cpu(cpu) {
  148. int old, prev;
  149. cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
  150. do {
  151. old = cilpcp->space_used;
  152. prev = cmpxchg(&cilpcp->space_used, old, 0);
  153. } while (old != prev);
  154. count += old;
  155. }
  156. atomic_add(count, &ctx->space_used);
  157. }
  158. static void
  159. xlog_cil_ctx_switch(
  160. struct xfs_cil *cil,
  161. struct xfs_cil_ctx *ctx)
  162. {
  163. xlog_cil_set_iclog_hdr_count(cil);
  164. set_bit(XLOG_CIL_EMPTY, &cil->xc_flags);
  165. set_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags);
  166. ctx->sequence = ++cil->xc_current_sequence;
  167. ctx->cil = cil;
  168. cil->xc_ctx = ctx;
  169. }
  170. /*
  171. * After the first stage of log recovery is done, we know where the head and
  172. * tail of the log are. We need this log initialisation done before we can
  173. * initialise the first CIL checkpoint context.
  174. *
  175. * Here we allocate a log ticket to track space usage during a CIL push. This
  176. * ticket is passed to xlog_write() directly so that we don't slowly leak log
  177. * space by failing to account for space used by log headers and additional
  178. * region headers for split regions.
  179. */
  180. void
  181. xlog_cil_init_post_recovery(
  182. struct xlog *log)
  183. {
  184. log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
  185. log->l_cilp->xc_ctx->sequence = 1;
  186. xlog_cil_set_iclog_hdr_count(log->l_cilp);
  187. }
  188. static inline int
  189. xlog_cil_iovec_space(
  190. uint niovecs)
  191. {
  192. return round_up((sizeof(struct xfs_log_vec) +
  193. niovecs * sizeof(struct xfs_log_iovec)),
  194. sizeof(uint64_t));
  195. }
  196. /*
  197. * Allocate or pin log vector buffers for CIL insertion.
  198. *
  199. * The CIL currently uses disposable buffers for copying a snapshot of the
  200. * modified items into the log during a push. The biggest problem with this is
  201. * the requirement to allocate the disposable buffer during the commit if:
  202. * a) does not exist; or
  203. * b) it is too small
  204. *
  205. * If we do this allocation within xlog_cil_insert_format_items(), it is done
  206. * under the xc_ctx_lock, which means that a CIL push cannot occur during
  207. * the memory allocation. This means that we have a potential deadlock situation
  208. * under low memory conditions when we have lots of dirty metadata pinned in
  209. * the CIL and we need a CIL commit to occur to free memory.
  210. *
  211. * To avoid this, we need to move the memory allocation outside the
  212. * xc_ctx_lock, but because the log vector buffers are disposable, that opens
  213. * up a TOCTOU race condition w.r.t. the CIL committing and removing the log
  214. * vector buffers between the check and the formatting of the item into the
  215. * log vector buffer within the xc_ctx_lock.
  216. *
  217. * Because the log vector buffer needs to be unchanged during the CIL push
  218. * process, we cannot share the buffer between the transaction commit (which
  219. * modifies the buffer) and the CIL push context that is writing the changes
  220. * into the log. This means skipping preallocation of buffer space is
  221. * unreliable, but we most definitely do not want to be allocating and freeing
  222. * buffers unnecessarily during commits when overwrites can be done safely.
  223. *
  224. * The simplest solution to this problem is to allocate a shadow buffer when a
  225. * log item is committed for the second time, and then to only use this buffer
  226. * if necessary. The buffer can remain attached to the log item until such time
  227. * it is needed, and this is the buffer that is reallocated to match the size of
  228. * the incoming modification. Then during the formatting of the item we can swap
  229. * the active buffer with the new one if we can't reuse the existing buffer. We
  230. * don't free the old buffer as it may be reused on the next modification if
  231. * it's size is right, otherwise we'll free and reallocate it at that point.
  232. *
  233. * This function builds a vector for the changes in each log item in the
  234. * transaction. It then works out the length of the buffer needed for each log
  235. * item, allocates them and attaches the vector to the log item in preparation
  236. * for the formatting step which occurs under the xc_ctx_lock.
  237. *
  238. * While this means the memory footprint goes up, it avoids the repeated
  239. * alloc/free pattern that repeated modifications of an item would otherwise
  240. * cause, and hence minimises the CPU overhead of such behaviour.
  241. */
  242. static void
  243. xlog_cil_alloc_shadow_bufs(
  244. struct xlog *log,
  245. struct xfs_trans *tp)
  246. {
  247. struct xfs_log_item *lip;
  248. list_for_each_entry(lip, &tp->t_items, li_trans) {
  249. struct xfs_log_vec *lv;
  250. int niovecs = 0;
  251. int nbytes = 0;
  252. int buf_size;
  253. bool ordered = false;
  254. /* Skip items which aren't dirty in this transaction. */
  255. if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
  256. continue;
  257. /* get number of vecs and size of data to be stored */
  258. lip->li_ops->iop_size(lip, &niovecs, &nbytes);
  259. /*
  260. * Ordered items need to be tracked but we do not wish to write
  261. * them. We need a logvec to track the object, but we do not
  262. * need an iovec or buffer to be allocated for copying data.
  263. */
  264. if (niovecs == XFS_LOG_VEC_ORDERED) {
  265. ordered = true;
  266. niovecs = 0;
  267. nbytes = 0;
  268. }
  269. /*
  270. * We 64-bit align the length of each iovec so that the start of
  271. * the next one is naturally aligned. We'll need to account for
  272. * that slack space here.
  273. *
  274. * We also add the xlog_op_header to each region when
  275. * formatting, but that's not accounted to the size of the item
  276. * at this point. Hence we'll need an addition number of bytes
  277. * for each vector to hold an opheader.
  278. *
  279. * Then round nbytes up to 64-bit alignment so that the initial
  280. * buffer alignment is easy to calculate and verify.
  281. */
  282. nbytes += niovecs *
  283. (sizeof(uint64_t) + sizeof(struct xlog_op_header));
  284. nbytes = round_up(nbytes, sizeof(uint64_t));
  285. /*
  286. * The data buffer needs to start 64-bit aligned, so round up
  287. * that space to ensure we can align it appropriately and not
  288. * overrun the buffer.
  289. */
  290. buf_size = nbytes + xlog_cil_iovec_space(niovecs);
  291. /*
  292. * if we have no shadow buffer, or it is too small, we need to
  293. * reallocate it.
  294. */
  295. if (!lip->li_lv_shadow ||
  296. buf_size > lip->li_lv_shadow->lv_size) {
  297. /*
  298. * We free and allocate here as a realloc would copy
  299. * unnecessary data. We don't use kvzalloc() for the
  300. * same reason - we don't need to zero the data area in
  301. * the buffer, only the log vector header and the iovec
  302. * storage.
  303. */
  304. kmem_free(lip->li_lv_shadow);
  305. lv = xlog_kvmalloc(buf_size);
  306. memset(lv, 0, xlog_cil_iovec_space(niovecs));
  307. INIT_LIST_HEAD(&lv->lv_list);
  308. lv->lv_item = lip;
  309. lv->lv_size = buf_size;
  310. if (ordered)
  311. lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
  312. else
  313. lv->lv_iovecp = (struct xfs_log_iovec *)&lv[1];
  314. lip->li_lv_shadow = lv;
  315. } else {
  316. /* same or smaller, optimise common overwrite case */
  317. lv = lip->li_lv_shadow;
  318. if (ordered)
  319. lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
  320. else
  321. lv->lv_buf_len = 0;
  322. lv->lv_bytes = 0;
  323. }
  324. /* Ensure the lv is set up according to ->iop_size */
  325. lv->lv_niovecs = niovecs;
  326. /* The allocated data region lies beyond the iovec region */
  327. lv->lv_buf = (char *)lv + xlog_cil_iovec_space(niovecs);
  328. }
  329. }
  330. /*
  331. * Prepare the log item for insertion into the CIL. Calculate the difference in
  332. * log space it will consume, and if it is a new item pin it as well.
  333. */
  334. STATIC void
  335. xfs_cil_prepare_item(
  336. struct xlog *log,
  337. struct xfs_log_vec *lv,
  338. struct xfs_log_vec *old_lv,
  339. int *diff_len)
  340. {
  341. /* Account for the new LV being passed in */
  342. if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
  343. *diff_len += lv->lv_bytes;
  344. /*
  345. * If there is no old LV, this is the first time we've seen the item in
  346. * this CIL context and so we need to pin it. If we are replacing the
  347. * old_lv, then remove the space it accounts for and make it the shadow
  348. * buffer for later freeing. In both cases we are now switching to the
  349. * shadow buffer, so update the pointer to it appropriately.
  350. */
  351. if (!old_lv) {
  352. if (lv->lv_item->li_ops->iop_pin)
  353. lv->lv_item->li_ops->iop_pin(lv->lv_item);
  354. lv->lv_item->li_lv_shadow = NULL;
  355. } else if (old_lv != lv) {
  356. ASSERT(lv->lv_buf_len != XFS_LOG_VEC_ORDERED);
  357. *diff_len -= old_lv->lv_bytes;
  358. lv->lv_item->li_lv_shadow = old_lv;
  359. }
  360. /* attach new log vector to log item */
  361. lv->lv_item->li_lv = lv;
  362. /*
  363. * If this is the first time the item is being committed to the
  364. * CIL, store the sequence number on the log item so we can
  365. * tell in future commits whether this is the first checkpoint
  366. * the item is being committed into.
  367. */
  368. if (!lv->lv_item->li_seq)
  369. lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
  370. }
  371. /*
  372. * Format log item into a flat buffers
  373. *
  374. * For delayed logging, we need to hold a formatted buffer containing all the
  375. * changes on the log item. This enables us to relog the item in memory and
  376. * write it out asynchronously without needing to relock the object that was
  377. * modified at the time it gets written into the iclog.
  378. *
  379. * This function takes the prepared log vectors attached to each log item, and
  380. * formats the changes into the log vector buffer. The buffer it uses is
  381. * dependent on the current state of the vector in the CIL - the shadow lv is
  382. * guaranteed to be large enough for the current modification, but we will only
  383. * use that if we can't reuse the existing lv. If we can't reuse the existing
  384. * lv, then simple swap it out for the shadow lv. We don't free it - that is
  385. * done lazily either by th enext modification or the freeing of the log item.
  386. *
  387. * We don't set up region headers during this process; we simply copy the
  388. * regions into the flat buffer. We can do this because we still have to do a
  389. * formatting step to write the regions into the iclog buffer. Writing the
  390. * ophdrs during the iclog write means that we can support splitting large
  391. * regions across iclog boundares without needing a change in the format of the
  392. * item/region encapsulation.
  393. *
  394. * Hence what we need to do now is change the rewrite the vector array to point
  395. * to the copied region inside the buffer we just allocated. This allows us to
  396. * format the regions into the iclog as though they are being formatted
  397. * directly out of the objects themselves.
  398. */
  399. static void
  400. xlog_cil_insert_format_items(
  401. struct xlog *log,
  402. struct xfs_trans *tp,
  403. int *diff_len)
  404. {
  405. struct xfs_log_item *lip;
  406. /* Bail out if we didn't find a log item. */
  407. if (list_empty(&tp->t_items)) {
  408. ASSERT(0);
  409. return;
  410. }
  411. list_for_each_entry(lip, &tp->t_items, li_trans) {
  412. struct xfs_log_vec *lv;
  413. struct xfs_log_vec *old_lv = NULL;
  414. struct xfs_log_vec *shadow;
  415. bool ordered = false;
  416. /* Skip items which aren't dirty in this transaction. */
  417. if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
  418. continue;
  419. /*
  420. * The formatting size information is already attached to
  421. * the shadow lv on the log item.
  422. */
  423. shadow = lip->li_lv_shadow;
  424. if (shadow->lv_buf_len == XFS_LOG_VEC_ORDERED)
  425. ordered = true;
  426. /* Skip items that do not have any vectors for writing */
  427. if (!shadow->lv_niovecs && !ordered)
  428. continue;
  429. /* compare to existing item size */
  430. old_lv = lip->li_lv;
  431. if (lip->li_lv && shadow->lv_size <= lip->li_lv->lv_size) {
  432. /* same or smaller, optimise common overwrite case */
  433. lv = lip->li_lv;
  434. if (ordered)
  435. goto insert;
  436. /*
  437. * set the item up as though it is a new insertion so
  438. * that the space reservation accounting is correct.
  439. */
  440. *diff_len -= lv->lv_bytes;
  441. /* Ensure the lv is set up according to ->iop_size */
  442. lv->lv_niovecs = shadow->lv_niovecs;
  443. /* reset the lv buffer information for new formatting */
  444. lv->lv_buf_len = 0;
  445. lv->lv_bytes = 0;
  446. lv->lv_buf = (char *)lv +
  447. xlog_cil_iovec_space(lv->lv_niovecs);
  448. } else {
  449. /* switch to shadow buffer! */
  450. lv = shadow;
  451. lv->lv_item = lip;
  452. if (ordered) {
  453. /* track as an ordered logvec */
  454. ASSERT(lip->li_lv == NULL);
  455. goto insert;
  456. }
  457. }
  458. ASSERT(IS_ALIGNED((unsigned long)lv->lv_buf, sizeof(uint64_t)));
  459. lip->li_ops->iop_format(lip, lv);
  460. insert:
  461. xfs_cil_prepare_item(log, lv, old_lv, diff_len);
  462. }
  463. }
  464. /*
  465. * The use of lockless waitqueue_active() requires that the caller has
  466. * serialised itself against the wakeup call in xlog_cil_push_work(). That
  467. * can be done by either holding the push lock or the context lock.
  468. */
  469. static inline bool
  470. xlog_cil_over_hard_limit(
  471. struct xlog *log,
  472. int32_t space_used)
  473. {
  474. if (waitqueue_active(&log->l_cilp->xc_push_wait))
  475. return true;
  476. if (space_used >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log))
  477. return true;
  478. return false;
  479. }
  480. /*
  481. * Insert the log items into the CIL and calculate the difference in space
  482. * consumed by the item. Add the space to the checkpoint ticket and calculate
  483. * if the change requires additional log metadata. If it does, take that space
  484. * as well. Remove the amount of space we added to the checkpoint ticket from
  485. * the current transaction ticket so that the accounting works out correctly.
  486. */
  487. static void
  488. xlog_cil_insert_items(
  489. struct xlog *log,
  490. struct xfs_trans *tp,
  491. uint32_t released_space)
  492. {
  493. struct xfs_cil *cil = log->l_cilp;
  494. struct xfs_cil_ctx *ctx = cil->xc_ctx;
  495. struct xfs_log_item *lip;
  496. int len = 0;
  497. int iovhdr_res = 0, split_res = 0, ctx_res = 0;
  498. int space_used;
  499. int order;
  500. struct xlog_cil_pcp *cilpcp;
  501. ASSERT(tp);
  502. /*
  503. * We can do this safely because the context can't checkpoint until we
  504. * are done so it doesn't matter exactly how we update the CIL.
  505. */
  506. xlog_cil_insert_format_items(log, tp, &len);
  507. /*
  508. * Subtract the space released by intent cancelation from the space we
  509. * consumed so that we remove it from the CIL space and add it back to
  510. * the current transaction reservation context.
  511. */
  512. len -= released_space;
  513. /*
  514. * Grab the per-cpu pointer for the CIL before we start any accounting.
  515. * That ensures that we are running with pre-emption disabled and so we
  516. * can't be scheduled away between split sample/update operations that
  517. * are done without outside locking to serialise them.
  518. */
  519. cilpcp = get_cpu_ptr(cil->xc_pcp);
  520. /*
  521. * We need to take the CIL checkpoint unit reservation on the first
  522. * commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't
  523. * unnecessarily do an atomic op in the fast path here. We can clear the
  524. * XLOG_CIL_EMPTY bit as we are under the xc_ctx_lock here and that
  525. * needs to be held exclusively to reset the XLOG_CIL_EMPTY bit.
  526. */
  527. if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags) &&
  528. test_and_clear_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
  529. ctx_res = ctx->ticket->t_unit_res;
  530. /*
  531. * Check if we need to steal iclog headers. atomic_read() is not a
  532. * locked atomic operation, so we can check the value before we do any
  533. * real atomic ops in the fast path. If we've already taken the CIL unit
  534. * reservation from this commit, we've already got one iclog header
  535. * space reserved so we have to account for that otherwise we risk
  536. * overrunning the reservation on this ticket.
  537. *
  538. * If the CIL is already at the hard limit, we might need more header
  539. * space that originally reserved. So steal more header space from every
  540. * commit that occurs once we are over the hard limit to ensure the CIL
  541. * push won't run out of reservation space.
  542. *
  543. * This can steal more than we need, but that's OK.
  544. *
  545. * The cil->xc_ctx_lock provides the serialisation necessary for safely
  546. * calling xlog_cil_over_hard_limit() in this context.
  547. */
  548. space_used = atomic_read(&ctx->space_used) + cilpcp->space_used + len;
  549. if (atomic_read(&cil->xc_iclog_hdrs) > 0 ||
  550. xlog_cil_over_hard_limit(log, space_used)) {
  551. split_res = log->l_iclog_hsize +
  552. sizeof(struct xlog_op_header);
  553. if (ctx_res)
  554. ctx_res += split_res * (tp->t_ticket->t_iclog_hdrs - 1);
  555. else
  556. ctx_res = split_res * tp->t_ticket->t_iclog_hdrs;
  557. atomic_sub(tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs);
  558. }
  559. cilpcp->space_reserved += ctx_res;
  560. /*
  561. * Accurately account when over the soft limit, otherwise fold the
  562. * percpu count into the global count if over the per-cpu threshold.
  563. */
  564. if (!test_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags)) {
  565. atomic_add(len, &ctx->space_used);
  566. } else if (cilpcp->space_used + len >
  567. (XLOG_CIL_SPACE_LIMIT(log) / num_online_cpus())) {
  568. space_used = atomic_add_return(cilpcp->space_used + len,
  569. &ctx->space_used);
  570. cilpcp->space_used = 0;
  571. /*
  572. * If we just transitioned over the soft limit, we need to
  573. * transition to the global atomic counter.
  574. */
  575. if (space_used >= XLOG_CIL_SPACE_LIMIT(log))
  576. xlog_cil_insert_pcp_aggregate(cil, ctx);
  577. } else {
  578. cilpcp->space_used += len;
  579. }
  580. /* attach the transaction to the CIL if it has any busy extents */
  581. if (!list_empty(&tp->t_busy))
  582. list_splice_init(&tp->t_busy, &cilpcp->busy_extents);
  583. /*
  584. * Now update the order of everything modified in the transaction
  585. * and insert items into the CIL if they aren't already there.
  586. * We do this here so we only need to take the CIL lock once during
  587. * the transaction commit.
  588. */
  589. order = atomic_inc_return(&ctx->order_id);
  590. list_for_each_entry(lip, &tp->t_items, li_trans) {
  591. /* Skip items which aren't dirty in this transaction. */
  592. if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
  593. continue;
  594. lip->li_order_id = order;
  595. if (!list_empty(&lip->li_cil))
  596. continue;
  597. list_add_tail(&lip->li_cil, &cilpcp->log_items);
  598. }
  599. put_cpu_ptr(cilpcp);
  600. /*
  601. * If we've overrun the reservation, dump the tx details before we move
  602. * the log items. Shutdown is imminent...
  603. */
  604. tp->t_ticket->t_curr_res -= ctx_res + len;
  605. if (WARN_ON(tp->t_ticket->t_curr_res < 0)) {
  606. xfs_warn(log->l_mp, "Transaction log reservation overrun:");
  607. xfs_warn(log->l_mp,
  608. " log items: %d bytes (iov hdrs: %d bytes)",
  609. len, iovhdr_res);
  610. xfs_warn(log->l_mp, " split region headers: %d bytes",
  611. split_res);
  612. xfs_warn(log->l_mp, " ctx ticket: %d bytes", ctx_res);
  613. xlog_print_trans(tp);
  614. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  615. }
  616. }
  617. static void
  618. xlog_cil_free_logvec(
  619. struct list_head *lv_chain)
  620. {
  621. struct xfs_log_vec *lv;
  622. while (!list_empty(lv_chain)) {
  623. lv = list_first_entry(lv_chain, struct xfs_log_vec, lv_list);
  624. list_del_init(&lv->lv_list);
  625. kmem_free(lv);
  626. }
  627. }
  628. static void
  629. xlog_discard_endio_work(
  630. struct work_struct *work)
  631. {
  632. struct xfs_cil_ctx *ctx =
  633. container_of(work, struct xfs_cil_ctx, discard_endio_work);
  634. struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
  635. xfs_extent_busy_clear(mp, &ctx->busy_extents, false);
  636. kmem_free(ctx);
  637. }
  638. /*
  639. * Queue up the actual completion to a thread to avoid IRQ-safe locking for
  640. * pagb_lock. Note that we need a unbounded workqueue, otherwise we might
  641. * get the execution delayed up to 30 seconds for weird reasons.
  642. */
  643. static void
  644. xlog_discard_endio(
  645. struct bio *bio)
  646. {
  647. struct xfs_cil_ctx *ctx = bio->bi_private;
  648. INIT_WORK(&ctx->discard_endio_work, xlog_discard_endio_work);
  649. queue_work(xfs_discard_wq, &ctx->discard_endio_work);
  650. bio_put(bio);
  651. }
  652. static void
  653. xlog_discard_busy_extents(
  654. struct xfs_mount *mp,
  655. struct xfs_cil_ctx *ctx)
  656. {
  657. struct list_head *list = &ctx->busy_extents;
  658. struct xfs_extent_busy *busyp;
  659. struct bio *bio = NULL;
  660. struct blk_plug plug;
  661. int error = 0;
  662. ASSERT(xfs_has_discard(mp));
  663. blk_start_plug(&plug);
  664. list_for_each_entry(busyp, list, list) {
  665. trace_xfs_discard_extent(mp, busyp->agno, busyp->bno,
  666. busyp->length);
  667. error = __blkdev_issue_discard(mp->m_ddev_targp->bt_bdev,
  668. XFS_AGB_TO_DADDR(mp, busyp->agno, busyp->bno),
  669. XFS_FSB_TO_BB(mp, busyp->length),
  670. GFP_NOFS, &bio);
  671. if (error && error != -EOPNOTSUPP) {
  672. xfs_info(mp,
  673. "discard failed for extent [0x%llx,%u], error %d",
  674. (unsigned long long)busyp->bno,
  675. busyp->length,
  676. error);
  677. break;
  678. }
  679. }
  680. if (bio) {
  681. bio->bi_private = ctx;
  682. bio->bi_end_io = xlog_discard_endio;
  683. submit_bio(bio);
  684. } else {
  685. xlog_discard_endio_work(&ctx->discard_endio_work);
  686. }
  687. blk_finish_plug(&plug);
  688. }
  689. /*
  690. * Mark all items committed and clear busy extents. We free the log vector
  691. * chains in a separate pass so that we unpin the log items as quickly as
  692. * possible.
  693. */
  694. static void
  695. xlog_cil_committed(
  696. struct xfs_cil_ctx *ctx)
  697. {
  698. struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
  699. bool abort = xlog_is_shutdown(ctx->cil->xc_log);
  700. /*
  701. * If the I/O failed, we're aborting the commit and already shutdown.
  702. * Wake any commit waiters before aborting the log items so we don't
  703. * block async log pushers on callbacks. Async log pushers explicitly do
  704. * not wait on log force completion because they may be holding locks
  705. * required to unpin items.
  706. */
  707. if (abort) {
  708. spin_lock(&ctx->cil->xc_push_lock);
  709. wake_up_all(&ctx->cil->xc_start_wait);
  710. wake_up_all(&ctx->cil->xc_commit_wait);
  711. spin_unlock(&ctx->cil->xc_push_lock);
  712. }
  713. xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, &ctx->lv_chain,
  714. ctx->start_lsn, abort);
  715. xfs_extent_busy_sort(&ctx->busy_extents);
  716. xfs_extent_busy_clear(mp, &ctx->busy_extents,
  717. xfs_has_discard(mp) && !abort);
  718. spin_lock(&ctx->cil->xc_push_lock);
  719. list_del(&ctx->committing);
  720. spin_unlock(&ctx->cil->xc_push_lock);
  721. xlog_cil_free_logvec(&ctx->lv_chain);
  722. if (!list_empty(&ctx->busy_extents))
  723. xlog_discard_busy_extents(mp, ctx);
  724. else
  725. kmem_free(ctx);
  726. }
  727. void
  728. xlog_cil_process_committed(
  729. struct list_head *list)
  730. {
  731. struct xfs_cil_ctx *ctx;
  732. while ((ctx = list_first_entry_or_null(list,
  733. struct xfs_cil_ctx, iclog_entry))) {
  734. list_del(&ctx->iclog_entry);
  735. xlog_cil_committed(ctx);
  736. }
  737. }
  738. /*
  739. * Record the LSN of the iclog we were just granted space to start writing into.
  740. * If the context doesn't have a start_lsn recorded, then this iclog will
  741. * contain the start record for the checkpoint. Otherwise this write contains
  742. * the commit record for the checkpoint.
  743. */
  744. void
  745. xlog_cil_set_ctx_write_state(
  746. struct xfs_cil_ctx *ctx,
  747. struct xlog_in_core *iclog)
  748. {
  749. struct xfs_cil *cil = ctx->cil;
  750. xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header.h_lsn);
  751. ASSERT(!ctx->commit_lsn);
  752. if (!ctx->start_lsn) {
  753. spin_lock(&cil->xc_push_lock);
  754. /*
  755. * The LSN we need to pass to the log items on transaction
  756. * commit is the LSN reported by the first log vector write, not
  757. * the commit lsn. If we use the commit record lsn then we can
  758. * move the grant write head beyond the tail LSN and overwrite
  759. * it.
  760. */
  761. ctx->start_lsn = lsn;
  762. wake_up_all(&cil->xc_start_wait);
  763. spin_unlock(&cil->xc_push_lock);
  764. /*
  765. * Make sure the metadata we are about to overwrite in the log
  766. * has been flushed to stable storage before this iclog is
  767. * issued.
  768. */
  769. spin_lock(&cil->xc_log->l_icloglock);
  770. iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
  771. spin_unlock(&cil->xc_log->l_icloglock);
  772. return;
  773. }
  774. /*
  775. * Take a reference to the iclog for the context so that we still hold
  776. * it when xlog_write is done and has released it. This means the
  777. * context controls when the iclog is released for IO.
  778. */
  779. atomic_inc(&iclog->ic_refcnt);
  780. /*
  781. * xlog_state_get_iclog_space() guarantees there is enough space in the
  782. * iclog for an entire commit record, so we can attach the context
  783. * callbacks now. This needs to be done before we make the commit_lsn
  784. * visible to waiters so that checkpoints with commit records in the
  785. * same iclog order their IO completion callbacks in the same order that
  786. * the commit records appear in the iclog.
  787. */
  788. spin_lock(&cil->xc_log->l_icloglock);
  789. list_add_tail(&ctx->iclog_entry, &iclog->ic_callbacks);
  790. spin_unlock(&cil->xc_log->l_icloglock);
  791. /*
  792. * Now we can record the commit LSN and wake anyone waiting for this
  793. * sequence to have the ordered commit record assigned to a physical
  794. * location in the log.
  795. */
  796. spin_lock(&cil->xc_push_lock);
  797. ctx->commit_iclog = iclog;
  798. ctx->commit_lsn = lsn;
  799. wake_up_all(&cil->xc_commit_wait);
  800. spin_unlock(&cil->xc_push_lock);
  801. }
  802. /*
  803. * Ensure that the order of log writes follows checkpoint sequence order. This
  804. * relies on the context LSN being zero until the log write has guaranteed the
  805. * LSN that the log write will start at via xlog_state_get_iclog_space().
  806. */
  807. enum _record_type {
  808. _START_RECORD,
  809. _COMMIT_RECORD,
  810. };
  811. static int
  812. xlog_cil_order_write(
  813. struct xfs_cil *cil,
  814. xfs_csn_t sequence,
  815. enum _record_type record)
  816. {
  817. struct xfs_cil_ctx *ctx;
  818. restart:
  819. spin_lock(&cil->xc_push_lock);
  820. list_for_each_entry(ctx, &cil->xc_committing, committing) {
  821. /*
  822. * Avoid getting stuck in this loop because we were woken by the
  823. * shutdown, but then went back to sleep once already in the
  824. * shutdown state.
  825. */
  826. if (xlog_is_shutdown(cil->xc_log)) {
  827. spin_unlock(&cil->xc_push_lock);
  828. return -EIO;
  829. }
  830. /*
  831. * Higher sequences will wait for this one so skip them.
  832. * Don't wait for our own sequence, either.
  833. */
  834. if (ctx->sequence >= sequence)
  835. continue;
  836. /* Wait until the LSN for the record has been recorded. */
  837. switch (record) {
  838. case _START_RECORD:
  839. if (!ctx->start_lsn) {
  840. xlog_wait(&cil->xc_start_wait, &cil->xc_push_lock);
  841. goto restart;
  842. }
  843. break;
  844. case _COMMIT_RECORD:
  845. if (!ctx->commit_lsn) {
  846. xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
  847. goto restart;
  848. }
  849. break;
  850. }
  851. }
  852. spin_unlock(&cil->xc_push_lock);
  853. return 0;
  854. }
  855. /*
  856. * Write out the log vector change now attached to the CIL context. This will
  857. * write a start record that needs to be strictly ordered in ascending CIL
  858. * sequence order so that log recovery will always use in-order start LSNs when
  859. * replaying checkpoints.
  860. */
  861. static int
  862. xlog_cil_write_chain(
  863. struct xfs_cil_ctx *ctx,
  864. uint32_t chain_len)
  865. {
  866. struct xlog *log = ctx->cil->xc_log;
  867. int error;
  868. error = xlog_cil_order_write(ctx->cil, ctx->sequence, _START_RECORD);
  869. if (error)
  870. return error;
  871. return xlog_write(log, ctx, &ctx->lv_chain, ctx->ticket, chain_len);
  872. }
  873. /*
  874. * Write out the commit record of a checkpoint transaction to close off a
  875. * running log write. These commit records are strictly ordered in ascending CIL
  876. * sequence order so that log recovery will always replay the checkpoints in the
  877. * correct order.
  878. */
  879. static int
  880. xlog_cil_write_commit_record(
  881. struct xfs_cil_ctx *ctx)
  882. {
  883. struct xlog *log = ctx->cil->xc_log;
  884. struct xlog_op_header ophdr = {
  885. .oh_clientid = XFS_TRANSACTION,
  886. .oh_tid = cpu_to_be32(ctx->ticket->t_tid),
  887. .oh_flags = XLOG_COMMIT_TRANS,
  888. };
  889. struct xfs_log_iovec reg = {
  890. .i_addr = &ophdr,
  891. .i_len = sizeof(struct xlog_op_header),
  892. .i_type = XLOG_REG_TYPE_COMMIT,
  893. };
  894. struct xfs_log_vec vec = {
  895. .lv_niovecs = 1,
  896. .lv_iovecp = &reg,
  897. };
  898. int error;
  899. LIST_HEAD(lv_chain);
  900. list_add(&vec.lv_list, &lv_chain);
  901. if (xlog_is_shutdown(log))
  902. return -EIO;
  903. error = xlog_cil_order_write(ctx->cil, ctx->sequence, _COMMIT_RECORD);
  904. if (error)
  905. return error;
  906. /* account for space used by record data */
  907. ctx->ticket->t_curr_res -= reg.i_len;
  908. error = xlog_write(log, ctx, &lv_chain, ctx->ticket, reg.i_len);
  909. if (error)
  910. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  911. return error;
  912. }
  913. struct xlog_cil_trans_hdr {
  914. struct xlog_op_header oph[2];
  915. struct xfs_trans_header thdr;
  916. struct xfs_log_iovec lhdr[2];
  917. };
  918. /*
  919. * Build a checkpoint transaction header to begin the journal transaction. We
  920. * need to account for the space used by the transaction header here as it is
  921. * not accounted for in xlog_write().
  922. *
  923. * This is the only place we write a transaction header, so we also build the
  924. * log opheaders that indicate the start of a log transaction and wrap the
  925. * transaction header. We keep the start record in it's own log vector rather
  926. * than compacting them into a single region as this ends up making the logic
  927. * in xlog_write() for handling empty opheaders for start, commit and unmount
  928. * records much simpler.
  929. */
  930. static void
  931. xlog_cil_build_trans_hdr(
  932. struct xfs_cil_ctx *ctx,
  933. struct xlog_cil_trans_hdr *hdr,
  934. struct xfs_log_vec *lvhdr,
  935. int num_iovecs)
  936. {
  937. struct xlog_ticket *tic = ctx->ticket;
  938. __be32 tid = cpu_to_be32(tic->t_tid);
  939. memset(hdr, 0, sizeof(*hdr));
  940. /* Log start record */
  941. hdr->oph[0].oh_tid = tid;
  942. hdr->oph[0].oh_clientid = XFS_TRANSACTION;
  943. hdr->oph[0].oh_flags = XLOG_START_TRANS;
  944. /* log iovec region pointer */
  945. hdr->lhdr[0].i_addr = &hdr->oph[0];
  946. hdr->lhdr[0].i_len = sizeof(struct xlog_op_header);
  947. hdr->lhdr[0].i_type = XLOG_REG_TYPE_LRHEADER;
  948. /* log opheader */
  949. hdr->oph[1].oh_tid = tid;
  950. hdr->oph[1].oh_clientid = XFS_TRANSACTION;
  951. hdr->oph[1].oh_len = cpu_to_be32(sizeof(struct xfs_trans_header));
  952. /* transaction header in host byte order format */
  953. hdr->thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
  954. hdr->thdr.th_type = XFS_TRANS_CHECKPOINT;
  955. hdr->thdr.th_tid = tic->t_tid;
  956. hdr->thdr.th_num_items = num_iovecs;
  957. /* log iovec region pointer */
  958. hdr->lhdr[1].i_addr = &hdr->oph[1];
  959. hdr->lhdr[1].i_len = sizeof(struct xlog_op_header) +
  960. sizeof(struct xfs_trans_header);
  961. hdr->lhdr[1].i_type = XLOG_REG_TYPE_TRANSHDR;
  962. lvhdr->lv_niovecs = 2;
  963. lvhdr->lv_iovecp = &hdr->lhdr[0];
  964. lvhdr->lv_bytes = hdr->lhdr[0].i_len + hdr->lhdr[1].i_len;
  965. tic->t_curr_res -= lvhdr->lv_bytes;
  966. }
  967. /*
  968. * CIL item reordering compare function. We want to order in ascending ID order,
  969. * but we want to leave items with the same ID in the order they were added to
  970. * the list. This is important for operations like reflink where we log 4 order
  971. * dependent intents in a single transaction when we overwrite an existing
  972. * shared extent with a new shared extent. i.e. BUI(unmap), CUI(drop),
  973. * CUI (inc), BUI(remap)...
  974. */
  975. static int
  976. xlog_cil_order_cmp(
  977. void *priv,
  978. const struct list_head *a,
  979. const struct list_head *b)
  980. {
  981. struct xfs_log_vec *l1 = container_of(a, struct xfs_log_vec, lv_list);
  982. struct xfs_log_vec *l2 = container_of(b, struct xfs_log_vec, lv_list);
  983. return l1->lv_order_id > l2->lv_order_id;
  984. }
  985. /*
  986. * Pull all the log vectors off the items in the CIL, and remove the items from
  987. * the CIL. We don't need the CIL lock here because it's only needed on the
  988. * transaction commit side which is currently locked out by the flush lock.
  989. *
  990. * If a log item is marked with a whiteout, we do not need to write it to the
  991. * journal and so we just move them to the whiteout list for the caller to
  992. * dispose of appropriately.
  993. */
  994. static void
  995. xlog_cil_build_lv_chain(
  996. struct xfs_cil_ctx *ctx,
  997. struct list_head *whiteouts,
  998. uint32_t *num_iovecs,
  999. uint32_t *num_bytes)
  1000. {
  1001. while (!list_empty(&ctx->log_items)) {
  1002. struct xfs_log_item *item;
  1003. struct xfs_log_vec *lv;
  1004. item = list_first_entry(&ctx->log_items,
  1005. struct xfs_log_item, li_cil);
  1006. if (test_bit(XFS_LI_WHITEOUT, &item->li_flags)) {
  1007. list_move(&item->li_cil, whiteouts);
  1008. trace_xfs_cil_whiteout_skip(item);
  1009. continue;
  1010. }
  1011. lv = item->li_lv;
  1012. lv->lv_order_id = item->li_order_id;
  1013. /* we don't write ordered log vectors */
  1014. if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
  1015. *num_bytes += lv->lv_bytes;
  1016. *num_iovecs += lv->lv_niovecs;
  1017. list_add_tail(&lv->lv_list, &ctx->lv_chain);
  1018. list_del_init(&item->li_cil);
  1019. item->li_order_id = 0;
  1020. item->li_lv = NULL;
  1021. }
  1022. }
  1023. static void
  1024. xlog_cil_cleanup_whiteouts(
  1025. struct list_head *whiteouts)
  1026. {
  1027. while (!list_empty(whiteouts)) {
  1028. struct xfs_log_item *item = list_first_entry(whiteouts,
  1029. struct xfs_log_item, li_cil);
  1030. list_del_init(&item->li_cil);
  1031. trace_xfs_cil_whiteout_unpin(item);
  1032. item->li_ops->iop_unpin(item, 1);
  1033. }
  1034. }
  1035. /*
  1036. * Push the Committed Item List to the log.
  1037. *
  1038. * If the current sequence is the same as xc_push_seq we need to do a flush. If
  1039. * xc_push_seq is less than the current sequence, then it has already been
  1040. * flushed and we don't need to do anything - the caller will wait for it to
  1041. * complete if necessary.
  1042. *
  1043. * xc_push_seq is checked unlocked against the sequence number for a match.
  1044. * Hence we can allow log forces to run racily and not issue pushes for the
  1045. * same sequence twice. If we get a race between multiple pushes for the same
  1046. * sequence they will block on the first one and then abort, hence avoiding
  1047. * needless pushes.
  1048. */
  1049. static void
  1050. xlog_cil_push_work(
  1051. struct work_struct *work)
  1052. {
  1053. struct xfs_cil_ctx *ctx =
  1054. container_of(work, struct xfs_cil_ctx, push_work);
  1055. struct xfs_cil *cil = ctx->cil;
  1056. struct xlog *log = cil->xc_log;
  1057. struct xfs_cil_ctx *new_ctx;
  1058. int num_iovecs = 0;
  1059. int num_bytes = 0;
  1060. int error = 0;
  1061. struct xlog_cil_trans_hdr thdr;
  1062. struct xfs_log_vec lvhdr = {};
  1063. xfs_csn_t push_seq;
  1064. bool push_commit_stable;
  1065. LIST_HEAD (whiteouts);
  1066. struct xlog_ticket *ticket;
  1067. new_ctx = xlog_cil_ctx_alloc();
  1068. new_ctx->ticket = xlog_cil_ticket_alloc(log);
  1069. down_write(&cil->xc_ctx_lock);
  1070. spin_lock(&cil->xc_push_lock);
  1071. push_seq = cil->xc_push_seq;
  1072. ASSERT(push_seq <= ctx->sequence);
  1073. push_commit_stable = cil->xc_push_commit_stable;
  1074. cil->xc_push_commit_stable = false;
  1075. /*
  1076. * As we are about to switch to a new, empty CIL context, we no longer
  1077. * need to throttle tasks on CIL space overruns. Wake any waiters that
  1078. * the hard push throttle may have caught so they can start committing
  1079. * to the new context. The ctx->xc_push_lock provides the serialisation
  1080. * necessary for safely using the lockless waitqueue_active() check in
  1081. * this context.
  1082. */
  1083. if (waitqueue_active(&cil->xc_push_wait))
  1084. wake_up_all(&cil->xc_push_wait);
  1085. xlog_cil_push_pcp_aggregate(cil, ctx);
  1086. /*
  1087. * Check if we've anything to push. If there is nothing, then we don't
  1088. * move on to a new sequence number and so we have to be able to push
  1089. * this sequence again later.
  1090. */
  1091. if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) {
  1092. cil->xc_push_seq = 0;
  1093. spin_unlock(&cil->xc_push_lock);
  1094. goto out_skip;
  1095. }
  1096. /* check for a previously pushed sequence */
  1097. if (push_seq < ctx->sequence) {
  1098. spin_unlock(&cil->xc_push_lock);
  1099. goto out_skip;
  1100. }
  1101. /*
  1102. * We are now going to push this context, so add it to the committing
  1103. * list before we do anything else. This ensures that anyone waiting on
  1104. * this push can easily detect the difference between a "push in
  1105. * progress" and "CIL is empty, nothing to do".
  1106. *
  1107. * IOWs, a wait loop can now check for:
  1108. * the current sequence not being found on the committing list;
  1109. * an empty CIL; and
  1110. * an unchanged sequence number
  1111. * to detect a push that had nothing to do and therefore does not need
  1112. * waiting on. If the CIL is not empty, we get put on the committing
  1113. * list before emptying the CIL and bumping the sequence number. Hence
  1114. * an empty CIL and an unchanged sequence number means we jumped out
  1115. * above after doing nothing.
  1116. *
  1117. * Hence the waiter will either find the commit sequence on the
  1118. * committing list or the sequence number will be unchanged and the CIL
  1119. * still dirty. In that latter case, the push has not yet started, and
  1120. * so the waiter will have to continue trying to check the CIL
  1121. * committing list until it is found. In extreme cases of delay, the
  1122. * sequence may fully commit between the attempts the wait makes to wait
  1123. * on the commit sequence.
  1124. */
  1125. list_add(&ctx->committing, &cil->xc_committing);
  1126. spin_unlock(&cil->xc_push_lock);
  1127. xlog_cil_build_lv_chain(ctx, &whiteouts, &num_iovecs, &num_bytes);
  1128. /*
  1129. * Switch the contexts so we can drop the context lock and move out
  1130. * of a shared context. We can't just go straight to the commit record,
  1131. * though - we need to synchronise with previous and future commits so
  1132. * that the commit records are correctly ordered in the log to ensure
  1133. * that we process items during log IO completion in the correct order.
  1134. *
  1135. * For example, if we get an EFI in one checkpoint and the EFD in the
  1136. * next (e.g. due to log forces), we do not want the checkpoint with
  1137. * the EFD to be committed before the checkpoint with the EFI. Hence
  1138. * we must strictly order the commit records of the checkpoints so
  1139. * that: a) the checkpoint callbacks are attached to the iclogs in the
  1140. * correct order; and b) the checkpoints are replayed in correct order
  1141. * in log recovery.
  1142. *
  1143. * Hence we need to add this context to the committing context list so
  1144. * that higher sequences will wait for us to write out a commit record
  1145. * before they do.
  1146. *
  1147. * xfs_log_force_seq requires us to mirror the new sequence into the cil
  1148. * structure atomically with the addition of this sequence to the
  1149. * committing list. This also ensures that we can do unlocked checks
  1150. * against the current sequence in log forces without risking
  1151. * deferencing a freed context pointer.
  1152. */
  1153. spin_lock(&cil->xc_push_lock);
  1154. xlog_cil_ctx_switch(cil, new_ctx);
  1155. spin_unlock(&cil->xc_push_lock);
  1156. up_write(&cil->xc_ctx_lock);
  1157. /*
  1158. * Sort the log vector chain before we add the transaction headers.
  1159. * This ensures we always have the transaction headers at the start
  1160. * of the chain.
  1161. */
  1162. list_sort(NULL, &ctx->lv_chain, xlog_cil_order_cmp);
  1163. /*
  1164. * Build a checkpoint transaction header and write it to the log to
  1165. * begin the transaction. We need to account for the space used by the
  1166. * transaction header here as it is not accounted for in xlog_write().
  1167. * Add the lvhdr to the head of the lv chain we pass to xlog_write() so
  1168. * it gets written into the iclog first.
  1169. */
  1170. xlog_cil_build_trans_hdr(ctx, &thdr, &lvhdr, num_iovecs);
  1171. num_bytes += lvhdr.lv_bytes;
  1172. list_add(&lvhdr.lv_list, &ctx->lv_chain);
  1173. /*
  1174. * Take the lvhdr back off the lv_chain immediately after calling
  1175. * xlog_cil_write_chain() as it should not be passed to log IO
  1176. * completion.
  1177. */
  1178. error = xlog_cil_write_chain(ctx, num_bytes);
  1179. list_del(&lvhdr.lv_list);
  1180. if (error)
  1181. goto out_abort_free_ticket;
  1182. error = xlog_cil_write_commit_record(ctx);
  1183. if (error)
  1184. goto out_abort_free_ticket;
  1185. /*
  1186. * Grab the ticket from the ctx so we can ungrant it after releasing the
  1187. * commit_iclog. The ctx may be freed by the time we return from
  1188. * releasing the commit_iclog (i.e. checkpoint has been completed and
  1189. * callback run) so we can't reference the ctx after the call to
  1190. * xlog_state_release_iclog().
  1191. */
  1192. ticket = ctx->ticket;
  1193. /*
  1194. * If the checkpoint spans multiple iclogs, wait for all previous iclogs
  1195. * to complete before we submit the commit_iclog. We can't use state
  1196. * checks for this - ACTIVE can be either a past completed iclog or a
  1197. * future iclog being filled, while WANT_SYNC through SYNC_DONE can be a
  1198. * past or future iclog awaiting IO or ordered IO completion to be run.
  1199. * In the latter case, if it's a future iclog and we wait on it, the we
  1200. * will hang because it won't get processed through to ic_force_wait
  1201. * wakeup until this commit_iclog is written to disk. Hence we use the
  1202. * iclog header lsn and compare it to the commit lsn to determine if we
  1203. * need to wait on iclogs or not.
  1204. */
  1205. spin_lock(&log->l_icloglock);
  1206. if (ctx->start_lsn != ctx->commit_lsn) {
  1207. xfs_lsn_t plsn;
  1208. plsn = be64_to_cpu(ctx->commit_iclog->ic_prev->ic_header.h_lsn);
  1209. if (plsn && XFS_LSN_CMP(plsn, ctx->commit_lsn) < 0) {
  1210. /*
  1211. * Waiting on ic_force_wait orders the completion of
  1212. * iclogs older than ic_prev. Hence we only need to wait
  1213. * on the most recent older iclog here.
  1214. */
  1215. xlog_wait_on_iclog(ctx->commit_iclog->ic_prev);
  1216. spin_lock(&log->l_icloglock);
  1217. }
  1218. /*
  1219. * We need to issue a pre-flush so that the ordering for this
  1220. * checkpoint is correctly preserved down to stable storage.
  1221. */
  1222. ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
  1223. }
  1224. /*
  1225. * The commit iclog must be written to stable storage to guarantee
  1226. * journal IO vs metadata writeback IO is correctly ordered on stable
  1227. * storage.
  1228. *
  1229. * If the push caller needs the commit to be immediately stable and the
  1230. * commit_iclog is not yet marked as XLOG_STATE_WANT_SYNC to indicate it
  1231. * will be written when released, switch it's state to WANT_SYNC right
  1232. * now.
  1233. */
  1234. ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FUA;
  1235. if (push_commit_stable &&
  1236. ctx->commit_iclog->ic_state == XLOG_STATE_ACTIVE)
  1237. xlog_state_switch_iclogs(log, ctx->commit_iclog, 0);
  1238. ticket = ctx->ticket;
  1239. xlog_state_release_iclog(log, ctx->commit_iclog, ticket);
  1240. /* Not safe to reference ctx now! */
  1241. spin_unlock(&log->l_icloglock);
  1242. xlog_cil_cleanup_whiteouts(&whiteouts);
  1243. xfs_log_ticket_ungrant(log, ticket);
  1244. return;
  1245. out_skip:
  1246. up_write(&cil->xc_ctx_lock);
  1247. xfs_log_ticket_put(new_ctx->ticket);
  1248. kmem_free(new_ctx);
  1249. return;
  1250. out_abort_free_ticket:
  1251. ASSERT(xlog_is_shutdown(log));
  1252. xlog_cil_cleanup_whiteouts(&whiteouts);
  1253. if (!ctx->commit_iclog) {
  1254. xfs_log_ticket_ungrant(log, ctx->ticket);
  1255. xlog_cil_committed(ctx);
  1256. return;
  1257. }
  1258. spin_lock(&log->l_icloglock);
  1259. ticket = ctx->ticket;
  1260. xlog_state_release_iclog(log, ctx->commit_iclog, ticket);
  1261. /* Not safe to reference ctx now! */
  1262. spin_unlock(&log->l_icloglock);
  1263. xfs_log_ticket_ungrant(log, ticket);
  1264. }
  1265. /*
  1266. * We need to push CIL every so often so we don't cache more than we can fit in
  1267. * the log. The limit really is that a checkpoint can't be more than half the
  1268. * log (the current checkpoint is not allowed to overwrite the previous
  1269. * checkpoint), but commit latency and memory usage limit this to a smaller
  1270. * size.
  1271. */
  1272. static void
  1273. xlog_cil_push_background(
  1274. struct xlog *log) __releases(cil->xc_ctx_lock)
  1275. {
  1276. struct xfs_cil *cil = log->l_cilp;
  1277. int space_used = atomic_read(&cil->xc_ctx->space_used);
  1278. /*
  1279. * The cil won't be empty because we are called while holding the
  1280. * context lock so whatever we added to the CIL will still be there.
  1281. */
  1282. ASSERT(!test_bit(XLOG_CIL_EMPTY, &cil->xc_flags));
  1283. /*
  1284. * We are done if:
  1285. * - we haven't used up all the space available yet; or
  1286. * - we've already queued up a push; and
  1287. * - we're not over the hard limit; and
  1288. * - nothing has been over the hard limit.
  1289. *
  1290. * If so, we don't need to take the push lock as there's nothing to do.
  1291. */
  1292. if (space_used < XLOG_CIL_SPACE_LIMIT(log) ||
  1293. (cil->xc_push_seq == cil->xc_current_sequence &&
  1294. space_used < XLOG_CIL_BLOCKING_SPACE_LIMIT(log) &&
  1295. !waitqueue_active(&cil->xc_push_wait))) {
  1296. up_read(&cil->xc_ctx_lock);
  1297. return;
  1298. }
  1299. spin_lock(&cil->xc_push_lock);
  1300. if (cil->xc_push_seq < cil->xc_current_sequence) {
  1301. cil->xc_push_seq = cil->xc_current_sequence;
  1302. queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
  1303. }
  1304. /*
  1305. * Drop the context lock now, we can't hold that if we need to sleep
  1306. * because we are over the blocking threshold. The push_lock is still
  1307. * held, so blocking threshold sleep/wakeup is still correctly
  1308. * serialised here.
  1309. */
  1310. up_read(&cil->xc_ctx_lock);
  1311. /*
  1312. * If we are well over the space limit, throttle the work that is being
  1313. * done until the push work on this context has begun. Enforce the hard
  1314. * throttle on all transaction commits once it has been activated, even
  1315. * if the committing transactions have resulted in the space usage
  1316. * dipping back down under the hard limit.
  1317. *
  1318. * The ctx->xc_push_lock provides the serialisation necessary for safely
  1319. * calling xlog_cil_over_hard_limit() in this context.
  1320. */
  1321. if (xlog_cil_over_hard_limit(log, space_used)) {
  1322. trace_xfs_log_cil_wait(log, cil->xc_ctx->ticket);
  1323. ASSERT(space_used < log->l_logsize);
  1324. xlog_wait(&cil->xc_push_wait, &cil->xc_push_lock);
  1325. return;
  1326. }
  1327. spin_unlock(&cil->xc_push_lock);
  1328. }
  1329. /*
  1330. * xlog_cil_push_now() is used to trigger an immediate CIL push to the sequence
  1331. * number that is passed. When it returns, the work will be queued for
  1332. * @push_seq, but it won't be completed.
  1333. *
  1334. * If the caller is performing a synchronous force, we will flush the workqueue
  1335. * to get previously queued work moving to minimise the wait time they will
  1336. * undergo waiting for all outstanding pushes to complete. The caller is
  1337. * expected to do the required waiting for push_seq to complete.
  1338. *
  1339. * If the caller is performing an async push, we need to ensure that the
  1340. * checkpoint is fully flushed out of the iclogs when we finish the push. If we
  1341. * don't do this, then the commit record may remain sitting in memory in an
  1342. * ACTIVE iclog. This then requires another full log force to push to disk,
  1343. * which defeats the purpose of having an async, non-blocking CIL force
  1344. * mechanism. Hence in this case we need to pass a flag to the push work to
  1345. * indicate it needs to flush the commit record itself.
  1346. */
  1347. static void
  1348. xlog_cil_push_now(
  1349. struct xlog *log,
  1350. xfs_lsn_t push_seq,
  1351. bool async)
  1352. {
  1353. struct xfs_cil *cil = log->l_cilp;
  1354. if (!cil)
  1355. return;
  1356. ASSERT(push_seq && push_seq <= cil->xc_current_sequence);
  1357. /* start on any pending background push to minimise wait time on it */
  1358. if (!async)
  1359. flush_workqueue(cil->xc_push_wq);
  1360. spin_lock(&cil->xc_push_lock);
  1361. /*
  1362. * If this is an async flush request, we always need to set the
  1363. * xc_push_commit_stable flag even if something else has already queued
  1364. * a push. The flush caller is asking for the CIL to be on stable
  1365. * storage when the next push completes, so regardless of who has queued
  1366. * the push, the flush requires stable semantics from it.
  1367. */
  1368. cil->xc_push_commit_stable = async;
  1369. /*
  1370. * If the CIL is empty or we've already pushed the sequence then
  1371. * there's no more work that we need to do.
  1372. */
  1373. if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags) ||
  1374. push_seq <= cil->xc_push_seq) {
  1375. spin_unlock(&cil->xc_push_lock);
  1376. return;
  1377. }
  1378. cil->xc_push_seq = push_seq;
  1379. queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
  1380. spin_unlock(&cil->xc_push_lock);
  1381. }
  1382. bool
  1383. xlog_cil_empty(
  1384. struct xlog *log)
  1385. {
  1386. struct xfs_cil *cil = log->l_cilp;
  1387. bool empty = false;
  1388. spin_lock(&cil->xc_push_lock);
  1389. if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
  1390. empty = true;
  1391. spin_unlock(&cil->xc_push_lock);
  1392. return empty;
  1393. }
  1394. /*
  1395. * If there are intent done items in this transaction and the related intent was
  1396. * committed in the current (same) CIL checkpoint, we don't need to write either
  1397. * the intent or intent done item to the journal as the change will be
  1398. * journalled atomically within this checkpoint. As we cannot remove items from
  1399. * the CIL here, mark the related intent with a whiteout so that the CIL push
  1400. * can remove it rather than writing it to the journal. Then remove the intent
  1401. * done item from the current transaction and release it so it doesn't get put
  1402. * into the CIL at all.
  1403. */
  1404. static uint32_t
  1405. xlog_cil_process_intents(
  1406. struct xfs_cil *cil,
  1407. struct xfs_trans *tp)
  1408. {
  1409. struct xfs_log_item *lip, *ilip, *next;
  1410. uint32_t len = 0;
  1411. list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
  1412. if (!(lip->li_ops->flags & XFS_ITEM_INTENT_DONE))
  1413. continue;
  1414. ilip = lip->li_ops->iop_intent(lip);
  1415. if (!ilip || !xlog_item_in_current_chkpt(cil, ilip))
  1416. continue;
  1417. set_bit(XFS_LI_WHITEOUT, &ilip->li_flags);
  1418. trace_xfs_cil_whiteout_mark(ilip);
  1419. len += ilip->li_lv->lv_bytes;
  1420. kmem_free(ilip->li_lv);
  1421. ilip->li_lv = NULL;
  1422. xfs_trans_del_item(lip);
  1423. lip->li_ops->iop_release(lip);
  1424. }
  1425. return len;
  1426. }
  1427. /*
  1428. * Commit a transaction with the given vector to the Committed Item List.
  1429. *
  1430. * To do this, we need to format the item, pin it in memory if required and
  1431. * account for the space used by the transaction. Once we have done that we
  1432. * need to release the unused reservation for the transaction, attach the
  1433. * transaction to the checkpoint context so we carry the busy extents through
  1434. * to checkpoint completion, and then unlock all the items in the transaction.
  1435. *
  1436. * Called with the context lock already held in read mode to lock out
  1437. * background commit, returns without it held once background commits are
  1438. * allowed again.
  1439. */
  1440. void
  1441. xlog_cil_commit(
  1442. struct xlog *log,
  1443. struct xfs_trans *tp,
  1444. xfs_csn_t *commit_seq,
  1445. bool regrant)
  1446. {
  1447. struct xfs_cil *cil = log->l_cilp;
  1448. struct xfs_log_item *lip, *next;
  1449. uint32_t released_space = 0;
  1450. /*
  1451. * Do all necessary memory allocation before we lock the CIL.
  1452. * This ensures the allocation does not deadlock with a CIL
  1453. * push in memory reclaim (e.g. from kswapd).
  1454. */
  1455. xlog_cil_alloc_shadow_bufs(log, tp);
  1456. /* lock out background commit */
  1457. down_read(&cil->xc_ctx_lock);
  1458. if (tp->t_flags & XFS_TRANS_HAS_INTENT_DONE)
  1459. released_space = xlog_cil_process_intents(cil, tp);
  1460. xlog_cil_insert_items(log, tp, released_space);
  1461. if (regrant && !xlog_is_shutdown(log))
  1462. xfs_log_ticket_regrant(log, tp->t_ticket);
  1463. else
  1464. xfs_log_ticket_ungrant(log, tp->t_ticket);
  1465. tp->t_ticket = NULL;
  1466. xfs_trans_unreserve_and_mod_sb(tp);
  1467. /*
  1468. * Once all the items of the transaction have been copied to the CIL,
  1469. * the items can be unlocked and possibly freed.
  1470. *
  1471. * This needs to be done before we drop the CIL context lock because we
  1472. * have to update state in the log items and unlock them before they go
  1473. * to disk. If we don't, then the CIL checkpoint can race with us and
  1474. * we can run checkpoint completion before we've updated and unlocked
  1475. * the log items. This affects (at least) processing of stale buffers,
  1476. * inodes and EFIs.
  1477. */
  1478. trace_xfs_trans_commit_items(tp, _RET_IP_);
  1479. list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
  1480. xfs_trans_del_item(lip);
  1481. if (lip->li_ops->iop_committing)
  1482. lip->li_ops->iop_committing(lip, cil->xc_ctx->sequence);
  1483. }
  1484. if (commit_seq)
  1485. *commit_seq = cil->xc_ctx->sequence;
  1486. /* xlog_cil_push_background() releases cil->xc_ctx_lock */
  1487. xlog_cil_push_background(log);
  1488. }
  1489. /*
  1490. * Flush the CIL to stable storage but don't wait for it to complete. This
  1491. * requires the CIL push to ensure the commit record for the push hits the disk,
  1492. * but otherwise is no different to a push done from a log force.
  1493. */
  1494. void
  1495. xlog_cil_flush(
  1496. struct xlog *log)
  1497. {
  1498. xfs_csn_t seq = log->l_cilp->xc_current_sequence;
  1499. trace_xfs_log_force(log->l_mp, seq, _RET_IP_);
  1500. xlog_cil_push_now(log, seq, true);
  1501. /*
  1502. * If the CIL is empty, make sure that any previous checkpoint that may
  1503. * still be in an active iclog is pushed to stable storage.
  1504. */
  1505. if (test_bit(XLOG_CIL_EMPTY, &log->l_cilp->xc_flags))
  1506. xfs_log_force(log->l_mp, 0);
  1507. }
  1508. /*
  1509. * Conditionally push the CIL based on the sequence passed in.
  1510. *
  1511. * We only need to push if we haven't already pushed the sequence number given.
  1512. * Hence the only time we will trigger a push here is if the push sequence is
  1513. * the same as the current context.
  1514. *
  1515. * We return the current commit lsn to allow the callers to determine if a
  1516. * iclog flush is necessary following this call.
  1517. */
  1518. xfs_lsn_t
  1519. xlog_cil_force_seq(
  1520. struct xlog *log,
  1521. xfs_csn_t sequence)
  1522. {
  1523. struct xfs_cil *cil = log->l_cilp;
  1524. struct xfs_cil_ctx *ctx;
  1525. xfs_lsn_t commit_lsn = NULLCOMMITLSN;
  1526. ASSERT(sequence <= cil->xc_current_sequence);
  1527. if (!sequence)
  1528. sequence = cil->xc_current_sequence;
  1529. trace_xfs_log_force(log->l_mp, sequence, _RET_IP_);
  1530. /*
  1531. * check to see if we need to force out the current context.
  1532. * xlog_cil_push() handles racing pushes for the same sequence,
  1533. * so no need to deal with it here.
  1534. */
  1535. restart:
  1536. xlog_cil_push_now(log, sequence, false);
  1537. /*
  1538. * See if we can find a previous sequence still committing.
  1539. * We need to wait for all previous sequence commits to complete
  1540. * before allowing the force of push_seq to go ahead. Hence block
  1541. * on commits for those as well.
  1542. */
  1543. spin_lock(&cil->xc_push_lock);
  1544. list_for_each_entry(ctx, &cil->xc_committing, committing) {
  1545. /*
  1546. * Avoid getting stuck in this loop because we were woken by the
  1547. * shutdown, but then went back to sleep once already in the
  1548. * shutdown state.
  1549. */
  1550. if (xlog_is_shutdown(log))
  1551. goto out_shutdown;
  1552. if (ctx->sequence > sequence)
  1553. continue;
  1554. if (!ctx->commit_lsn) {
  1555. /*
  1556. * It is still being pushed! Wait for the push to
  1557. * complete, then start again from the beginning.
  1558. */
  1559. XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
  1560. xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
  1561. goto restart;
  1562. }
  1563. if (ctx->sequence != sequence)
  1564. continue;
  1565. /* found it! */
  1566. commit_lsn = ctx->commit_lsn;
  1567. }
  1568. /*
  1569. * The call to xlog_cil_push_now() executes the push in the background.
  1570. * Hence by the time we have got here it our sequence may not have been
  1571. * pushed yet. This is true if the current sequence still matches the
  1572. * push sequence after the above wait loop and the CIL still contains
  1573. * dirty objects. This is guaranteed by the push code first adding the
  1574. * context to the committing list before emptying the CIL.
  1575. *
  1576. * Hence if we don't find the context in the committing list and the
  1577. * current sequence number is unchanged then the CIL contents are
  1578. * significant. If the CIL is empty, if means there was nothing to push
  1579. * and that means there is nothing to wait for. If the CIL is not empty,
  1580. * it means we haven't yet started the push, because if it had started
  1581. * we would have found the context on the committing list.
  1582. */
  1583. if (sequence == cil->xc_current_sequence &&
  1584. !test_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) {
  1585. spin_unlock(&cil->xc_push_lock);
  1586. goto restart;
  1587. }
  1588. spin_unlock(&cil->xc_push_lock);
  1589. return commit_lsn;
  1590. /*
  1591. * We detected a shutdown in progress. We need to trigger the log force
  1592. * to pass through it's iclog state machine error handling, even though
  1593. * we are already in a shutdown state. Hence we can't return
  1594. * NULLCOMMITLSN here as that has special meaning to log forces (i.e.
  1595. * LSN is already stable), so we return a zero LSN instead.
  1596. */
  1597. out_shutdown:
  1598. spin_unlock(&cil->xc_push_lock);
  1599. return 0;
  1600. }
  1601. /*
  1602. * Move dead percpu state to the relevant CIL context structures.
  1603. *
  1604. * We have to lock the CIL context here to ensure that nothing is modifying
  1605. * the percpu state, either addition or removal. Both of these are done under
  1606. * the CIL context lock, so grabbing that exclusively here will ensure we can
  1607. * safely drain the cilpcp for the CPU that is dying.
  1608. */
  1609. void
  1610. xlog_cil_pcp_dead(
  1611. struct xlog *log,
  1612. unsigned int cpu)
  1613. {
  1614. struct xfs_cil *cil = log->l_cilp;
  1615. struct xlog_cil_pcp *cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
  1616. struct xfs_cil_ctx *ctx;
  1617. down_write(&cil->xc_ctx_lock);
  1618. ctx = cil->xc_ctx;
  1619. if (ctx->ticket)
  1620. ctx->ticket->t_curr_res += cilpcp->space_reserved;
  1621. cilpcp->space_reserved = 0;
  1622. if (!list_empty(&cilpcp->log_items))
  1623. list_splice_init(&cilpcp->log_items, &ctx->log_items);
  1624. if (!list_empty(&cilpcp->busy_extents))
  1625. list_splice_init(&cilpcp->busy_extents, &ctx->busy_extents);
  1626. atomic_add(cilpcp->space_used, &ctx->space_used);
  1627. cilpcp->space_used = 0;
  1628. up_write(&cil->xc_ctx_lock);
  1629. }
  1630. /*
  1631. * Perform initial CIL structure initialisation.
  1632. */
  1633. int
  1634. xlog_cil_init(
  1635. struct xlog *log)
  1636. {
  1637. struct xfs_cil *cil;
  1638. struct xfs_cil_ctx *ctx;
  1639. struct xlog_cil_pcp *cilpcp;
  1640. int cpu;
  1641. cil = kmem_zalloc(sizeof(*cil), KM_MAYFAIL);
  1642. if (!cil)
  1643. return -ENOMEM;
  1644. /*
  1645. * Limit the CIL pipeline depth to 4 concurrent works to bound the
  1646. * concurrency the log spinlocks will be exposed to.
  1647. */
  1648. cil->xc_push_wq = alloc_workqueue("xfs-cil/%s",
  1649. XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_UNBOUND),
  1650. 4, log->l_mp->m_super->s_id);
  1651. if (!cil->xc_push_wq)
  1652. goto out_destroy_cil;
  1653. cil->xc_log = log;
  1654. cil->xc_pcp = alloc_percpu(struct xlog_cil_pcp);
  1655. if (!cil->xc_pcp)
  1656. goto out_destroy_wq;
  1657. for_each_possible_cpu(cpu) {
  1658. cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
  1659. INIT_LIST_HEAD(&cilpcp->busy_extents);
  1660. INIT_LIST_HEAD(&cilpcp->log_items);
  1661. }
  1662. INIT_LIST_HEAD(&cil->xc_committing);
  1663. spin_lock_init(&cil->xc_push_lock);
  1664. init_waitqueue_head(&cil->xc_push_wait);
  1665. init_rwsem(&cil->xc_ctx_lock);
  1666. init_waitqueue_head(&cil->xc_start_wait);
  1667. init_waitqueue_head(&cil->xc_commit_wait);
  1668. log->l_cilp = cil;
  1669. ctx = xlog_cil_ctx_alloc();
  1670. xlog_cil_ctx_switch(cil, ctx);
  1671. return 0;
  1672. out_destroy_wq:
  1673. destroy_workqueue(cil->xc_push_wq);
  1674. out_destroy_cil:
  1675. kmem_free(cil);
  1676. return -ENOMEM;
  1677. }
  1678. void
  1679. xlog_cil_destroy(
  1680. struct xlog *log)
  1681. {
  1682. struct xfs_cil *cil = log->l_cilp;
  1683. if (cil->xc_ctx) {
  1684. if (cil->xc_ctx->ticket)
  1685. xfs_log_ticket_put(cil->xc_ctx->ticket);
  1686. kmem_free(cil->xc_ctx);
  1687. }
  1688. ASSERT(test_bit(XLOG_CIL_EMPTY, &cil->xc_flags));
  1689. free_percpu(cil->xc_pcp);
  1690. destroy_workqueue(cil->xc_push_wq);
  1691. kmem_free(cil);
  1692. }