segment.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NILFS segment constructor.
  4. *
  5. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Ryusuke Konishi.
  8. *
  9. */
  10. #include <linux/pagemap.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/writeback.h>
  13. #include <linux/bitops.h>
  14. #include <linux/bio.h>
  15. #include <linux/completion.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/backing-dev.h>
  18. #include <linux/freezer.h>
  19. #include <linux/kthread.h>
  20. #include <linux/crc32.h>
  21. #include <linux/pagevec.h>
  22. #include <linux/slab.h>
  23. #include <linux/sched/signal.h>
  24. #include "nilfs.h"
  25. #include "btnode.h"
  26. #include "page.h"
  27. #include "segment.h"
  28. #include "sufile.h"
  29. #include "cpfile.h"
  30. #include "ifile.h"
  31. #include "segbuf.h"
  32. /*
  33. * Segment constructor
  34. */
  35. #define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */
  36. #define SC_MAX_SEGDELTA 64 /*
  37. * Upper limit of the number of segments
  38. * appended in collection retry loop
  39. */
  40. /* Construction mode */
  41. enum {
  42. SC_LSEG_SR = 1, /* Make a logical segment having a super root */
  43. SC_LSEG_DSYNC, /*
  44. * Flush data blocks of a given file and make
  45. * a logical segment without a super root.
  46. */
  47. SC_FLUSH_FILE, /*
  48. * Flush data files, leads to segment writes without
  49. * creating a checkpoint.
  50. */
  51. SC_FLUSH_DAT, /*
  52. * Flush DAT file. This also creates segments
  53. * without a checkpoint.
  54. */
  55. };
  56. /* Stage numbers of dirty block collection */
  57. enum {
  58. NILFS_ST_INIT = 0,
  59. NILFS_ST_GC, /* Collecting dirty blocks for GC */
  60. NILFS_ST_FILE,
  61. NILFS_ST_IFILE,
  62. NILFS_ST_CPFILE,
  63. NILFS_ST_SUFILE,
  64. NILFS_ST_DAT,
  65. NILFS_ST_SR, /* Super root */
  66. NILFS_ST_DSYNC, /* Data sync blocks */
  67. NILFS_ST_DONE,
  68. };
  69. #define CREATE_TRACE_POINTS
  70. #include <trace/events/nilfs2.h>
  71. /*
  72. * nilfs_sc_cstage_inc(), nilfs_sc_cstage_set(), nilfs_sc_cstage_get() are
  73. * wrapper functions of stage count (nilfs_sc_info->sc_stage.scnt). Users of
  74. * the variable must use them because transition of stage count must involve
  75. * trace events (trace_nilfs2_collection_stage_transition).
  76. *
  77. * nilfs_sc_cstage_get() isn't required for the above purpose because it doesn't
  78. * produce tracepoint events. It is provided just for making the intention
  79. * clear.
  80. */
  81. static inline void nilfs_sc_cstage_inc(struct nilfs_sc_info *sci)
  82. {
  83. sci->sc_stage.scnt++;
  84. trace_nilfs2_collection_stage_transition(sci);
  85. }
  86. static inline void nilfs_sc_cstage_set(struct nilfs_sc_info *sci, int next_scnt)
  87. {
  88. sci->sc_stage.scnt = next_scnt;
  89. trace_nilfs2_collection_stage_transition(sci);
  90. }
  91. static inline int nilfs_sc_cstage_get(struct nilfs_sc_info *sci)
  92. {
  93. return sci->sc_stage.scnt;
  94. }
  95. /* State flags of collection */
  96. #define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
  97. #define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
  98. #define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
  99. #define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
  100. /* Operations depending on the construction mode and file type */
  101. struct nilfs_sc_operations {
  102. int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *,
  103. struct inode *);
  104. int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *,
  105. struct inode *);
  106. int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *,
  107. struct inode *);
  108. void (*write_data_binfo)(struct nilfs_sc_info *,
  109. struct nilfs_segsum_pointer *,
  110. union nilfs_binfo *);
  111. void (*write_node_binfo)(struct nilfs_sc_info *,
  112. struct nilfs_segsum_pointer *,
  113. union nilfs_binfo *);
  114. };
  115. /*
  116. * Other definitions
  117. */
  118. static void nilfs_segctor_start_timer(struct nilfs_sc_info *);
  119. static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int);
  120. static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
  121. static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
  122. #define nilfs_cnt32_ge(a, b) \
  123. (typecheck(__u32, a) && typecheck(__u32, b) && \
  124. ((__s32)((a) - (b)) >= 0))
  125. static int nilfs_prepare_segment_lock(struct super_block *sb,
  126. struct nilfs_transaction_info *ti)
  127. {
  128. struct nilfs_transaction_info *cur_ti = current->journal_info;
  129. void *save = NULL;
  130. if (cur_ti) {
  131. if (cur_ti->ti_magic == NILFS_TI_MAGIC)
  132. return ++cur_ti->ti_count;
  133. /*
  134. * If journal_info field is occupied by other FS,
  135. * it is saved and will be restored on
  136. * nilfs_transaction_commit().
  137. */
  138. nilfs_warn(sb, "journal info from a different FS");
  139. save = current->journal_info;
  140. }
  141. if (!ti) {
  142. ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
  143. if (!ti)
  144. return -ENOMEM;
  145. ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
  146. } else {
  147. ti->ti_flags = 0;
  148. }
  149. ti->ti_count = 0;
  150. ti->ti_save = save;
  151. ti->ti_magic = NILFS_TI_MAGIC;
  152. current->journal_info = ti;
  153. return 0;
  154. }
  155. /**
  156. * nilfs_transaction_begin - start indivisible file operations.
  157. * @sb: super block
  158. * @ti: nilfs_transaction_info
  159. * @vacancy_check: flags for vacancy rate checks
  160. *
  161. * nilfs_transaction_begin() acquires a reader/writer semaphore, called
  162. * the segment semaphore, to make a segment construction and write tasks
  163. * exclusive. The function is used with nilfs_transaction_commit() in pairs.
  164. * The region enclosed by these two functions can be nested. To avoid a
  165. * deadlock, the semaphore is only acquired or released in the outermost call.
  166. *
  167. * This function allocates a nilfs_transaction_info struct to keep context
  168. * information on it. It is initialized and hooked onto the current task in
  169. * the outermost call. If a pre-allocated struct is given to @ti, it is used
  170. * instead; otherwise a new struct is assigned from a slab.
  171. *
  172. * When @vacancy_check flag is set, this function will check the amount of
  173. * free space, and will wait for the GC to reclaim disk space if low capacity.
  174. *
  175. * Return: 0 on success, or one of the following negative error codes on
  176. * failure:
  177. * * %-ENOMEM - Insufficient memory available.
  178. * * %-ENOSPC - No space left on device (if checking free space).
  179. */
  180. int nilfs_transaction_begin(struct super_block *sb,
  181. struct nilfs_transaction_info *ti,
  182. int vacancy_check)
  183. {
  184. struct the_nilfs *nilfs;
  185. int ret = nilfs_prepare_segment_lock(sb, ti);
  186. struct nilfs_transaction_info *trace_ti;
  187. if (unlikely(ret < 0))
  188. return ret;
  189. if (ret > 0) {
  190. trace_ti = current->journal_info;
  191. trace_nilfs2_transaction_transition(sb, trace_ti,
  192. trace_ti->ti_count, trace_ti->ti_flags,
  193. TRACE_NILFS2_TRANSACTION_BEGIN);
  194. return 0;
  195. }
  196. sb_start_intwrite(sb);
  197. nilfs = sb->s_fs_info;
  198. down_read(&nilfs->ns_segctor_sem);
  199. if (vacancy_check && nilfs_near_disk_full(nilfs)) {
  200. up_read(&nilfs->ns_segctor_sem);
  201. ret = -ENOSPC;
  202. goto failed;
  203. }
  204. trace_ti = current->journal_info;
  205. trace_nilfs2_transaction_transition(sb, trace_ti, trace_ti->ti_count,
  206. trace_ti->ti_flags,
  207. TRACE_NILFS2_TRANSACTION_BEGIN);
  208. return 0;
  209. failed:
  210. ti = current->journal_info;
  211. current->journal_info = ti->ti_save;
  212. if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
  213. kmem_cache_free(nilfs_transaction_cachep, ti);
  214. sb_end_intwrite(sb);
  215. return ret;
  216. }
  217. /**
  218. * nilfs_transaction_commit - commit indivisible file operations.
  219. * @sb: super block
  220. *
  221. * nilfs_transaction_commit() releases the read semaphore which is
  222. * acquired by nilfs_transaction_begin(). This is only performed
  223. * in outermost call of this function. If a commit flag is set,
  224. * nilfs_transaction_commit() sets a timer to start the segment
  225. * constructor. If a sync flag is set, it starts construction
  226. * directly.
  227. *
  228. * Return: 0 on success, or a negative error code on failure.
  229. */
  230. int nilfs_transaction_commit(struct super_block *sb)
  231. {
  232. struct nilfs_transaction_info *ti = current->journal_info;
  233. struct the_nilfs *nilfs = sb->s_fs_info;
  234. int err = 0;
  235. BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
  236. ti->ti_flags |= NILFS_TI_COMMIT;
  237. if (ti->ti_count > 0) {
  238. ti->ti_count--;
  239. trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
  240. ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
  241. return 0;
  242. }
  243. if (nilfs->ns_writer) {
  244. struct nilfs_sc_info *sci = nilfs->ns_writer;
  245. if (ti->ti_flags & NILFS_TI_COMMIT)
  246. nilfs_segctor_start_timer(sci);
  247. if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
  248. nilfs_segctor_do_flush(sci, 0);
  249. }
  250. up_read(&nilfs->ns_segctor_sem);
  251. trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
  252. ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
  253. current->journal_info = ti->ti_save;
  254. if (ti->ti_flags & NILFS_TI_SYNC)
  255. err = nilfs_construct_segment(sb);
  256. if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
  257. kmem_cache_free(nilfs_transaction_cachep, ti);
  258. sb_end_intwrite(sb);
  259. return err;
  260. }
  261. void nilfs_transaction_abort(struct super_block *sb)
  262. {
  263. struct nilfs_transaction_info *ti = current->journal_info;
  264. struct the_nilfs *nilfs = sb->s_fs_info;
  265. BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
  266. if (ti->ti_count > 0) {
  267. ti->ti_count--;
  268. trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
  269. ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
  270. return;
  271. }
  272. up_read(&nilfs->ns_segctor_sem);
  273. trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
  274. ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
  275. current->journal_info = ti->ti_save;
  276. if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
  277. kmem_cache_free(nilfs_transaction_cachep, ti);
  278. sb_end_intwrite(sb);
  279. }
  280. void nilfs_relax_pressure_in_lock(struct super_block *sb)
  281. {
  282. struct the_nilfs *nilfs = sb->s_fs_info;
  283. struct nilfs_sc_info *sci = nilfs->ns_writer;
  284. if (sb_rdonly(sb) || unlikely(!sci) || !sci->sc_flush_request)
  285. return;
  286. set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
  287. up_read(&nilfs->ns_segctor_sem);
  288. down_write(&nilfs->ns_segctor_sem);
  289. if (sci->sc_flush_request &&
  290. test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
  291. struct nilfs_transaction_info *ti = current->journal_info;
  292. ti->ti_flags |= NILFS_TI_WRITER;
  293. nilfs_segctor_do_immediate_flush(sci);
  294. ti->ti_flags &= ~NILFS_TI_WRITER;
  295. }
  296. downgrade_write(&nilfs->ns_segctor_sem);
  297. }
  298. static void nilfs_transaction_lock(struct super_block *sb,
  299. struct nilfs_transaction_info *ti,
  300. int gcflag)
  301. {
  302. struct nilfs_transaction_info *cur_ti = current->journal_info;
  303. struct the_nilfs *nilfs = sb->s_fs_info;
  304. struct nilfs_sc_info *sci = nilfs->ns_writer;
  305. WARN_ON(cur_ti);
  306. ti->ti_flags = NILFS_TI_WRITER;
  307. ti->ti_count = 0;
  308. ti->ti_save = cur_ti;
  309. ti->ti_magic = NILFS_TI_MAGIC;
  310. current->journal_info = ti;
  311. for (;;) {
  312. trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
  313. ti->ti_flags, TRACE_NILFS2_TRANSACTION_TRYLOCK);
  314. down_write(&nilfs->ns_segctor_sem);
  315. if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
  316. break;
  317. nilfs_segctor_do_immediate_flush(sci);
  318. up_write(&nilfs->ns_segctor_sem);
  319. cond_resched();
  320. }
  321. if (gcflag)
  322. ti->ti_flags |= NILFS_TI_GC;
  323. trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
  324. ti->ti_flags, TRACE_NILFS2_TRANSACTION_LOCK);
  325. }
  326. static void nilfs_transaction_unlock(struct super_block *sb)
  327. {
  328. struct nilfs_transaction_info *ti = current->journal_info;
  329. struct the_nilfs *nilfs = sb->s_fs_info;
  330. BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
  331. BUG_ON(ti->ti_count > 0);
  332. up_write(&nilfs->ns_segctor_sem);
  333. current->journal_info = ti->ti_save;
  334. trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
  335. ti->ti_flags, TRACE_NILFS2_TRANSACTION_UNLOCK);
  336. }
  337. static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
  338. struct nilfs_segsum_pointer *ssp,
  339. unsigned int bytes)
  340. {
  341. struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
  342. unsigned int blocksize = sci->sc_super->s_blocksize;
  343. void *p;
  344. if (unlikely(ssp->offset + bytes > blocksize)) {
  345. ssp->offset = 0;
  346. BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
  347. &segbuf->sb_segsum_buffers));
  348. ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
  349. }
  350. p = ssp->bh->b_data + ssp->offset;
  351. ssp->offset += bytes;
  352. return p;
  353. }
  354. /**
  355. * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
  356. * @sci: nilfs_sc_info
  357. *
  358. * Return: 0 on success, or a negative error code on failure.
  359. */
  360. static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
  361. {
  362. struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
  363. struct buffer_head *sumbh;
  364. unsigned int sumbytes;
  365. unsigned int flags = 0;
  366. int err;
  367. if (nilfs_doing_gc())
  368. flags = NILFS_SS_GC;
  369. err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
  370. if (unlikely(err))
  371. return err;
  372. sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
  373. sumbytes = segbuf->sb_sum.sumbytes;
  374. sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
  375. sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
  376. sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
  377. return 0;
  378. }
  379. /**
  380. * nilfs_segctor_zeropad_segsum - zero pad the rest of the segment summary area
  381. * @sci: segment constructor object
  382. *
  383. * nilfs_segctor_zeropad_segsum() zero-fills unallocated space at the end of
  384. * the current segment summary block.
  385. */
  386. static void nilfs_segctor_zeropad_segsum(struct nilfs_sc_info *sci)
  387. {
  388. struct nilfs_segsum_pointer *ssp;
  389. ssp = sci->sc_blk_cnt > 0 ? &sci->sc_binfo_ptr : &sci->sc_finfo_ptr;
  390. if (ssp->offset < ssp->bh->b_size)
  391. memset(ssp->bh->b_data + ssp->offset, 0,
  392. ssp->bh->b_size - ssp->offset);
  393. }
  394. static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
  395. {
  396. sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
  397. if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
  398. return -E2BIG; /*
  399. * The current segment is filled up
  400. * (internal code)
  401. */
  402. nilfs_segctor_zeropad_segsum(sci);
  403. sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
  404. return nilfs_segctor_reset_segment_buffer(sci);
  405. }
  406. static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
  407. {
  408. struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
  409. int err;
  410. if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
  411. err = nilfs_segctor_feed_segment(sci);
  412. if (err)
  413. return err;
  414. segbuf = sci->sc_curseg;
  415. }
  416. err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
  417. if (likely(!err))
  418. segbuf->sb_sum.flags |= NILFS_SS_SR;
  419. return err;
  420. }
  421. /*
  422. * Functions for making segment summary and payloads
  423. */
  424. static int nilfs_segctor_segsum_block_required(
  425. struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
  426. unsigned int binfo_size)
  427. {
  428. unsigned int blocksize = sci->sc_super->s_blocksize;
  429. /* Size of finfo and binfo is enough small against blocksize */
  430. return ssp->offset + binfo_size +
  431. (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
  432. blocksize;
  433. }
  434. static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
  435. struct inode *inode)
  436. {
  437. sci->sc_curseg->sb_sum.nfinfo++;
  438. sci->sc_binfo_ptr = sci->sc_finfo_ptr;
  439. nilfs_segctor_map_segsum_entry(
  440. sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
  441. if (NILFS_I(inode)->i_root &&
  442. !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
  443. set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
  444. /* skip finfo */
  445. }
  446. static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
  447. struct inode *inode)
  448. {
  449. struct nilfs_finfo *finfo;
  450. struct nilfs_inode_info *ii;
  451. struct nilfs_segment_buffer *segbuf;
  452. __u64 cno;
  453. if (sci->sc_blk_cnt == 0)
  454. return;
  455. ii = NILFS_I(inode);
  456. if (ii->i_type & NILFS_I_TYPE_GC)
  457. cno = ii->i_cno;
  458. else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
  459. cno = 0;
  460. else
  461. cno = sci->sc_cno;
  462. finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
  463. sizeof(*finfo));
  464. finfo->fi_ino = cpu_to_le64(inode->i_ino);
  465. finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
  466. finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
  467. finfo->fi_cno = cpu_to_le64(cno);
  468. segbuf = sci->sc_curseg;
  469. segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
  470. sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
  471. sci->sc_finfo_ptr = sci->sc_binfo_ptr;
  472. sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
  473. }
  474. static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
  475. struct buffer_head *bh,
  476. struct inode *inode,
  477. unsigned int binfo_size)
  478. {
  479. struct nilfs_segment_buffer *segbuf;
  480. int required, err = 0;
  481. retry:
  482. segbuf = sci->sc_curseg;
  483. required = nilfs_segctor_segsum_block_required(
  484. sci, &sci->sc_binfo_ptr, binfo_size);
  485. if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
  486. nilfs_segctor_end_finfo(sci, inode);
  487. err = nilfs_segctor_feed_segment(sci);
  488. if (err)
  489. return err;
  490. goto retry;
  491. }
  492. if (unlikely(required)) {
  493. nilfs_segctor_zeropad_segsum(sci);
  494. err = nilfs_segbuf_extend_segsum(segbuf);
  495. if (unlikely(err))
  496. goto failed;
  497. }
  498. if (sci->sc_blk_cnt == 0)
  499. nilfs_segctor_begin_finfo(sci, inode);
  500. nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
  501. /* Substitution to vblocknr is delayed until update_blocknr() */
  502. nilfs_segbuf_add_file_buffer(segbuf, bh);
  503. sci->sc_blk_cnt++;
  504. failed:
  505. return err;
  506. }
  507. /*
  508. * Callback functions that enumerate, mark, and collect dirty blocks
  509. */
  510. static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
  511. struct buffer_head *bh, struct inode *inode)
  512. {
  513. int err;
  514. err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
  515. if (err < 0)
  516. return err;
  517. err = nilfs_segctor_add_file_block(sci, bh, inode,
  518. sizeof(struct nilfs_binfo_v));
  519. if (!err)
  520. sci->sc_datablk_cnt++;
  521. return err;
  522. }
  523. static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
  524. struct buffer_head *bh,
  525. struct inode *inode)
  526. {
  527. return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
  528. }
  529. static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
  530. struct buffer_head *bh,
  531. struct inode *inode)
  532. {
  533. WARN_ON(!buffer_dirty(bh));
  534. return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
  535. }
  536. static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
  537. struct nilfs_segsum_pointer *ssp,
  538. union nilfs_binfo *binfo)
  539. {
  540. struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
  541. sci, ssp, sizeof(*binfo_v));
  542. *binfo_v = binfo->bi_v;
  543. }
  544. static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
  545. struct nilfs_segsum_pointer *ssp,
  546. union nilfs_binfo *binfo)
  547. {
  548. __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
  549. sci, ssp, sizeof(*vblocknr));
  550. *vblocknr = binfo->bi_v.bi_vblocknr;
  551. }
  552. static const struct nilfs_sc_operations nilfs_sc_file_ops = {
  553. .collect_data = nilfs_collect_file_data,
  554. .collect_node = nilfs_collect_file_node,
  555. .collect_bmap = nilfs_collect_file_bmap,
  556. .write_data_binfo = nilfs_write_file_data_binfo,
  557. .write_node_binfo = nilfs_write_file_node_binfo,
  558. };
  559. static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
  560. struct buffer_head *bh, struct inode *inode)
  561. {
  562. int err;
  563. err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
  564. if (err < 0)
  565. return err;
  566. err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
  567. if (!err)
  568. sci->sc_datablk_cnt++;
  569. return err;
  570. }
  571. static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
  572. struct buffer_head *bh, struct inode *inode)
  573. {
  574. WARN_ON(!buffer_dirty(bh));
  575. return nilfs_segctor_add_file_block(sci, bh, inode,
  576. sizeof(struct nilfs_binfo_dat));
  577. }
  578. static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
  579. struct nilfs_segsum_pointer *ssp,
  580. union nilfs_binfo *binfo)
  581. {
  582. __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
  583. sizeof(*blkoff));
  584. *blkoff = binfo->bi_dat.bi_blkoff;
  585. }
  586. static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
  587. struct nilfs_segsum_pointer *ssp,
  588. union nilfs_binfo *binfo)
  589. {
  590. struct nilfs_binfo_dat *binfo_dat =
  591. nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
  592. *binfo_dat = binfo->bi_dat;
  593. }
  594. static const struct nilfs_sc_operations nilfs_sc_dat_ops = {
  595. .collect_data = nilfs_collect_dat_data,
  596. .collect_node = nilfs_collect_file_node,
  597. .collect_bmap = nilfs_collect_dat_bmap,
  598. .write_data_binfo = nilfs_write_dat_data_binfo,
  599. .write_node_binfo = nilfs_write_dat_node_binfo,
  600. };
  601. static const struct nilfs_sc_operations nilfs_sc_dsync_ops = {
  602. .collect_data = nilfs_collect_file_data,
  603. .collect_node = NULL,
  604. .collect_bmap = NULL,
  605. .write_data_binfo = nilfs_write_file_data_binfo,
  606. .write_node_binfo = NULL,
  607. };
  608. static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
  609. struct list_head *listp,
  610. size_t nlimit,
  611. loff_t start, loff_t end)
  612. {
  613. struct address_space *mapping = inode->i_mapping;
  614. struct folio_batch fbatch;
  615. pgoff_t index = 0, last = ULONG_MAX;
  616. size_t ndirties = 0;
  617. int i;
  618. if (unlikely(start != 0 || end != LLONG_MAX)) {
  619. /*
  620. * A valid range is given for sync-ing data pages. The
  621. * range is rounded to per-page; extra dirty buffers
  622. * may be included if blocksize < pagesize.
  623. */
  624. index = start >> PAGE_SHIFT;
  625. last = end >> PAGE_SHIFT;
  626. }
  627. folio_batch_init(&fbatch);
  628. repeat:
  629. if (unlikely(index > last) ||
  630. !filemap_get_folios_tag(mapping, &index, last,
  631. PAGECACHE_TAG_DIRTY, &fbatch))
  632. return ndirties;
  633. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  634. struct buffer_head *bh, *head;
  635. struct folio *folio = fbatch.folios[i];
  636. folio_lock(folio);
  637. if (unlikely(folio->mapping != mapping)) {
  638. /* Exclude folios removed from the address space */
  639. folio_unlock(folio);
  640. continue;
  641. }
  642. head = folio_buffers(folio);
  643. if (!head)
  644. head = create_empty_buffers(folio,
  645. i_blocksize(inode), 0);
  646. bh = head;
  647. do {
  648. if (!buffer_dirty(bh) || buffer_async_write(bh))
  649. continue;
  650. get_bh(bh);
  651. list_add_tail(&bh->b_assoc_buffers, listp);
  652. ndirties++;
  653. if (unlikely(ndirties >= nlimit)) {
  654. folio_unlock(folio);
  655. folio_batch_release(&fbatch);
  656. cond_resched();
  657. return ndirties;
  658. }
  659. } while (bh = bh->b_this_page, bh != head);
  660. folio_unlock(folio);
  661. }
  662. folio_batch_release(&fbatch);
  663. cond_resched();
  664. goto repeat;
  665. }
  666. static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
  667. struct list_head *listp)
  668. {
  669. struct nilfs_inode_info *ii = NILFS_I(inode);
  670. struct inode *btnc_inode = ii->i_assoc_inode;
  671. struct folio_batch fbatch;
  672. struct buffer_head *bh, *head;
  673. unsigned int i;
  674. pgoff_t index = 0;
  675. if (!btnc_inode)
  676. return;
  677. folio_batch_init(&fbatch);
  678. while (filemap_get_folios_tag(btnc_inode->i_mapping, &index,
  679. (pgoff_t)-1, PAGECACHE_TAG_DIRTY, &fbatch)) {
  680. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  681. bh = head = folio_buffers(fbatch.folios[i]);
  682. do {
  683. if (buffer_dirty(bh) &&
  684. !buffer_async_write(bh)) {
  685. get_bh(bh);
  686. list_add_tail(&bh->b_assoc_buffers,
  687. listp);
  688. }
  689. bh = bh->b_this_page;
  690. } while (bh != head);
  691. }
  692. folio_batch_release(&fbatch);
  693. cond_resched();
  694. }
  695. }
  696. static void nilfs_dispose_list(struct the_nilfs *nilfs,
  697. struct list_head *head, int force)
  698. {
  699. struct nilfs_inode_info *ii, *n;
  700. struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
  701. unsigned int nv = 0;
  702. while (!list_empty(head)) {
  703. spin_lock(&nilfs->ns_inode_lock);
  704. list_for_each_entry_safe(ii, n, head, i_dirty) {
  705. list_del_init(&ii->i_dirty);
  706. if (force) {
  707. if (unlikely(ii->i_bh)) {
  708. brelse(ii->i_bh);
  709. ii->i_bh = NULL;
  710. }
  711. } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
  712. set_bit(NILFS_I_QUEUED, &ii->i_state);
  713. list_add_tail(&ii->i_dirty,
  714. &nilfs->ns_dirty_files);
  715. continue;
  716. }
  717. ivec[nv++] = ii;
  718. if (nv == SC_N_INODEVEC)
  719. break;
  720. }
  721. spin_unlock(&nilfs->ns_inode_lock);
  722. for (pii = ivec; nv > 0; pii++, nv--)
  723. iput(&(*pii)->vfs_inode);
  724. }
  725. }
  726. static void nilfs_iput_work_func(struct work_struct *work)
  727. {
  728. struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info,
  729. sc_iput_work);
  730. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  731. nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0);
  732. }
  733. static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
  734. struct nilfs_root *root)
  735. {
  736. int ret = 0;
  737. if (nilfs_mdt_fetch_dirty(root->ifile))
  738. ret++;
  739. if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
  740. ret++;
  741. if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
  742. ret++;
  743. if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
  744. ret++;
  745. return ret;
  746. }
  747. static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
  748. {
  749. return list_empty(&sci->sc_dirty_files) &&
  750. !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
  751. sci->sc_nfreesegs == 0 &&
  752. (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
  753. }
  754. static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
  755. {
  756. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  757. int ret = 0;
  758. if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
  759. set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
  760. spin_lock(&nilfs->ns_inode_lock);
  761. if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
  762. ret++;
  763. spin_unlock(&nilfs->ns_inode_lock);
  764. return ret;
  765. }
  766. static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
  767. {
  768. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  769. nilfs_mdt_clear_dirty(sci->sc_root->ifile);
  770. nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
  771. nilfs_mdt_clear_dirty(nilfs->ns_sufile);
  772. nilfs_mdt_clear_dirty(nilfs->ns_dat);
  773. }
  774. static void nilfs_fill_in_file_bmap(struct inode *ifile,
  775. struct nilfs_inode_info *ii)
  776. {
  777. struct buffer_head *ibh;
  778. struct nilfs_inode *raw_inode;
  779. if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
  780. ibh = ii->i_bh;
  781. BUG_ON(!ibh);
  782. raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
  783. ibh);
  784. nilfs_bmap_write(ii->i_bmap, raw_inode);
  785. nilfs_ifile_unmap_inode(raw_inode);
  786. }
  787. }
  788. static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
  789. {
  790. struct nilfs_inode_info *ii;
  791. list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
  792. nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
  793. set_bit(NILFS_I_COLLECTED, &ii->i_state);
  794. }
  795. }
  796. /**
  797. * nilfs_write_root_mdt_inode - export root metadata inode information to
  798. * the on-disk inode
  799. * @inode: inode object of the root metadata file
  800. * @raw_inode: on-disk inode
  801. *
  802. * nilfs_write_root_mdt_inode() writes inode information and bmap data of
  803. * @inode to the inode area of the metadata file allocated on the super root
  804. * block created to finalize the log. Since super root blocks are configured
  805. * each time, this function zero-fills the unused area of @raw_inode.
  806. */
  807. static void nilfs_write_root_mdt_inode(struct inode *inode,
  808. struct nilfs_inode *raw_inode)
  809. {
  810. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  811. nilfs_write_inode_common(inode, raw_inode);
  812. /* zero-fill unused portion of raw_inode */
  813. raw_inode->i_xattr = 0;
  814. raw_inode->i_pad = 0;
  815. memset((void *)raw_inode + sizeof(*raw_inode), 0,
  816. nilfs->ns_inode_size - sizeof(*raw_inode));
  817. nilfs_bmap_write(NILFS_I(inode)->i_bmap, raw_inode);
  818. }
  819. static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
  820. struct the_nilfs *nilfs)
  821. {
  822. struct buffer_head *bh_sr;
  823. struct nilfs_super_root *raw_sr;
  824. unsigned int isz, srsz;
  825. bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
  826. lock_buffer(bh_sr);
  827. raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
  828. isz = nilfs->ns_inode_size;
  829. srsz = NILFS_SR_BYTES(isz);
  830. raw_sr->sr_sum = 0; /* Ensure initialization within this update */
  831. raw_sr->sr_bytes = cpu_to_le16(srsz);
  832. raw_sr->sr_nongc_ctime
  833. = cpu_to_le64(nilfs_doing_gc() ?
  834. nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
  835. raw_sr->sr_flags = 0;
  836. nilfs_write_root_mdt_inode(nilfs->ns_dat, (void *)raw_sr +
  837. NILFS_SR_DAT_OFFSET(isz));
  838. nilfs_write_root_mdt_inode(nilfs->ns_cpfile, (void *)raw_sr +
  839. NILFS_SR_CPFILE_OFFSET(isz));
  840. nilfs_write_root_mdt_inode(nilfs->ns_sufile, (void *)raw_sr +
  841. NILFS_SR_SUFILE_OFFSET(isz));
  842. memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz);
  843. set_buffer_uptodate(bh_sr);
  844. unlock_buffer(bh_sr);
  845. }
  846. static void nilfs_redirty_inodes(struct list_head *head)
  847. {
  848. struct nilfs_inode_info *ii;
  849. list_for_each_entry(ii, head, i_dirty) {
  850. if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
  851. clear_bit(NILFS_I_COLLECTED, &ii->i_state);
  852. }
  853. }
  854. static void nilfs_drop_collected_inodes(struct list_head *head)
  855. {
  856. struct nilfs_inode_info *ii;
  857. list_for_each_entry(ii, head, i_dirty) {
  858. if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
  859. continue;
  860. clear_bit(NILFS_I_INODE_SYNC, &ii->i_state);
  861. set_bit(NILFS_I_UPDATED, &ii->i_state);
  862. }
  863. }
  864. static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
  865. struct inode *inode,
  866. struct list_head *listp,
  867. int (*collect)(struct nilfs_sc_info *,
  868. struct buffer_head *,
  869. struct inode *))
  870. {
  871. struct buffer_head *bh, *n;
  872. int err = 0;
  873. if (collect) {
  874. list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
  875. list_del_init(&bh->b_assoc_buffers);
  876. err = collect(sci, bh, inode);
  877. brelse(bh);
  878. if (unlikely(err))
  879. goto dispose_buffers;
  880. }
  881. return 0;
  882. }
  883. dispose_buffers:
  884. while (!list_empty(listp)) {
  885. bh = list_first_entry(listp, struct buffer_head,
  886. b_assoc_buffers);
  887. list_del_init(&bh->b_assoc_buffers);
  888. brelse(bh);
  889. }
  890. return err;
  891. }
  892. static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
  893. {
  894. /* Remaining number of blocks within segment buffer */
  895. return sci->sc_segbuf_nblocks -
  896. (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
  897. }
  898. static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
  899. struct inode *inode,
  900. const struct nilfs_sc_operations *sc_ops)
  901. {
  902. LIST_HEAD(data_buffers);
  903. LIST_HEAD(node_buffers);
  904. int err;
  905. if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
  906. size_t n, rest = nilfs_segctor_buffer_rest(sci);
  907. n = nilfs_lookup_dirty_data_buffers(
  908. inode, &data_buffers, rest + 1, 0, LLONG_MAX);
  909. if (n > rest) {
  910. err = nilfs_segctor_apply_buffers(
  911. sci, inode, &data_buffers,
  912. sc_ops->collect_data);
  913. BUG_ON(!err); /* always receive -E2BIG or true error */
  914. goto break_or_fail;
  915. }
  916. }
  917. nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
  918. if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
  919. err = nilfs_segctor_apply_buffers(
  920. sci, inode, &data_buffers, sc_ops->collect_data);
  921. if (unlikely(err)) {
  922. /* dispose node list */
  923. nilfs_segctor_apply_buffers(
  924. sci, inode, &node_buffers, NULL);
  925. goto break_or_fail;
  926. }
  927. sci->sc_stage.flags |= NILFS_CF_NODE;
  928. }
  929. /* Collect node */
  930. err = nilfs_segctor_apply_buffers(
  931. sci, inode, &node_buffers, sc_ops->collect_node);
  932. if (unlikely(err))
  933. goto break_or_fail;
  934. nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
  935. err = nilfs_segctor_apply_buffers(
  936. sci, inode, &node_buffers, sc_ops->collect_bmap);
  937. if (unlikely(err))
  938. goto break_or_fail;
  939. nilfs_segctor_end_finfo(sci, inode);
  940. sci->sc_stage.flags &= ~NILFS_CF_NODE;
  941. break_or_fail:
  942. return err;
  943. }
  944. static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
  945. struct inode *inode)
  946. {
  947. LIST_HEAD(data_buffers);
  948. size_t n, rest = nilfs_segctor_buffer_rest(sci);
  949. int err;
  950. n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
  951. sci->sc_dsync_start,
  952. sci->sc_dsync_end);
  953. err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
  954. nilfs_collect_file_data);
  955. if (!err) {
  956. nilfs_segctor_end_finfo(sci, inode);
  957. BUG_ON(n > rest);
  958. /* always receive -E2BIG or true error if n > rest */
  959. }
  960. return err;
  961. }
  962. /**
  963. * nilfs_free_segments - free the segments given by an array of segment numbers
  964. * @nilfs: nilfs object
  965. * @segnumv: array of segment numbers to be freed
  966. * @nsegs: number of segments to be freed in @segnumv
  967. *
  968. * nilfs_free_segments() wraps nilfs_sufile_freev() and
  969. * nilfs_sufile_cancel_freev(), and edits the segment usage metadata file
  970. * (sufile) to free all segments given by @segnumv and @nsegs at once. If
  971. * it fails midway, it cancels the changes so that none of the segments are
  972. * freed. If @nsegs is 0, this function does nothing.
  973. *
  974. * The freeing of segments is not finalized until the writing of a log with
  975. * a super root block containing this sufile change is complete, and it can
  976. * be canceled with nilfs_sufile_cancel_freev() until then.
  977. *
  978. * Return: 0 on success, or one of the following negative error codes on
  979. * failure:
  980. * * %-EINVAL - Invalid segment number.
  981. * * %-EIO - I/O error (including metadata corruption).
  982. * * %-ENOMEM - Insufficient memory available.
  983. */
  984. static int nilfs_free_segments(struct the_nilfs *nilfs, __u64 *segnumv,
  985. size_t nsegs)
  986. {
  987. size_t ndone;
  988. int ret;
  989. if (!nsegs)
  990. return 0;
  991. ret = nilfs_sufile_freev(nilfs->ns_sufile, segnumv, nsegs, &ndone);
  992. if (unlikely(ret)) {
  993. nilfs_sufile_cancel_freev(nilfs->ns_sufile, segnumv, ndone,
  994. NULL);
  995. /*
  996. * If a segment usage of the segments to be freed is in a
  997. * hole block, nilfs_sufile_freev() will return -ENOENT.
  998. * In this case, -EINVAL should be returned to the caller
  999. * since there is something wrong with the given segment
  1000. * number array. This error can only occur during GC, so
  1001. * there is no need to worry about it propagating to other
  1002. * callers (such as fsync).
  1003. */
  1004. if (ret == -ENOENT) {
  1005. nilfs_err(nilfs->ns_sb,
  1006. "The segment usage entry %llu to be freed is invalid (in a hole)",
  1007. (unsigned long long)segnumv[ndone]);
  1008. ret = -EINVAL;
  1009. }
  1010. }
  1011. return ret;
  1012. }
  1013. static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
  1014. {
  1015. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  1016. struct list_head *head;
  1017. struct nilfs_inode_info *ii;
  1018. int err = 0;
  1019. switch (nilfs_sc_cstage_get(sci)) {
  1020. case NILFS_ST_INIT:
  1021. /* Pre-processes */
  1022. sci->sc_stage.flags = 0;
  1023. if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
  1024. sci->sc_nblk_inc = 0;
  1025. sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
  1026. if (mode == SC_LSEG_DSYNC) {
  1027. nilfs_sc_cstage_set(sci, NILFS_ST_DSYNC);
  1028. goto dsync_mode;
  1029. }
  1030. }
  1031. sci->sc_stage.dirty_file_ptr = NULL;
  1032. sci->sc_stage.gc_inode_ptr = NULL;
  1033. if (mode == SC_FLUSH_DAT) {
  1034. nilfs_sc_cstage_set(sci, NILFS_ST_DAT);
  1035. goto dat_stage;
  1036. }
  1037. nilfs_sc_cstage_inc(sci);
  1038. fallthrough;
  1039. case NILFS_ST_GC:
  1040. if (nilfs_doing_gc()) {
  1041. head = &sci->sc_gc_inodes;
  1042. ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
  1043. head, i_dirty);
  1044. list_for_each_entry_continue(ii, head, i_dirty) {
  1045. err = nilfs_segctor_scan_file(
  1046. sci, &ii->vfs_inode,
  1047. &nilfs_sc_file_ops);
  1048. if (unlikely(err)) {
  1049. sci->sc_stage.gc_inode_ptr = list_entry(
  1050. ii->i_dirty.prev,
  1051. struct nilfs_inode_info,
  1052. i_dirty);
  1053. goto break_or_fail;
  1054. }
  1055. set_bit(NILFS_I_COLLECTED, &ii->i_state);
  1056. }
  1057. sci->sc_stage.gc_inode_ptr = NULL;
  1058. }
  1059. nilfs_sc_cstage_inc(sci);
  1060. fallthrough;
  1061. case NILFS_ST_FILE:
  1062. head = &sci->sc_dirty_files;
  1063. ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
  1064. i_dirty);
  1065. list_for_each_entry_continue(ii, head, i_dirty) {
  1066. clear_bit(NILFS_I_DIRTY, &ii->i_state);
  1067. err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
  1068. &nilfs_sc_file_ops);
  1069. if (unlikely(err)) {
  1070. sci->sc_stage.dirty_file_ptr =
  1071. list_entry(ii->i_dirty.prev,
  1072. struct nilfs_inode_info,
  1073. i_dirty);
  1074. goto break_or_fail;
  1075. }
  1076. /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
  1077. /* XXX: required ? */
  1078. }
  1079. sci->sc_stage.dirty_file_ptr = NULL;
  1080. if (mode == SC_FLUSH_FILE) {
  1081. nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
  1082. return 0;
  1083. }
  1084. nilfs_sc_cstage_inc(sci);
  1085. sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
  1086. fallthrough;
  1087. case NILFS_ST_IFILE:
  1088. err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
  1089. &nilfs_sc_file_ops);
  1090. if (unlikely(err))
  1091. break;
  1092. nilfs_sc_cstage_inc(sci);
  1093. /* Creating a checkpoint */
  1094. err = nilfs_cpfile_create_checkpoint(nilfs->ns_cpfile,
  1095. nilfs->ns_cno);
  1096. if (unlikely(err))
  1097. break;
  1098. fallthrough;
  1099. case NILFS_ST_CPFILE:
  1100. err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
  1101. &nilfs_sc_file_ops);
  1102. if (unlikely(err))
  1103. break;
  1104. nilfs_sc_cstage_inc(sci);
  1105. fallthrough;
  1106. case NILFS_ST_SUFILE:
  1107. err = nilfs_free_segments(nilfs, sci->sc_freesegs,
  1108. sci->sc_nfreesegs);
  1109. if (unlikely(err))
  1110. break;
  1111. sci->sc_stage.flags |= NILFS_CF_SUFREED;
  1112. err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
  1113. &nilfs_sc_file_ops);
  1114. if (unlikely(err))
  1115. break;
  1116. nilfs_sc_cstage_inc(sci);
  1117. fallthrough;
  1118. case NILFS_ST_DAT:
  1119. dat_stage:
  1120. err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
  1121. &nilfs_sc_dat_ops);
  1122. if (unlikely(err))
  1123. break;
  1124. if (mode == SC_FLUSH_DAT) {
  1125. nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
  1126. return 0;
  1127. }
  1128. nilfs_sc_cstage_inc(sci);
  1129. fallthrough;
  1130. case NILFS_ST_SR:
  1131. if (mode == SC_LSEG_SR) {
  1132. /* Appending a super root */
  1133. err = nilfs_segctor_add_super_root(sci);
  1134. if (unlikely(err))
  1135. break;
  1136. }
  1137. /* End of a logical segment */
  1138. sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
  1139. nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
  1140. return 0;
  1141. case NILFS_ST_DSYNC:
  1142. dsync_mode:
  1143. sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
  1144. ii = sci->sc_dsync_inode;
  1145. if (!test_bit(NILFS_I_BUSY, &ii->i_state))
  1146. break;
  1147. err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
  1148. if (unlikely(err))
  1149. break;
  1150. sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
  1151. nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
  1152. return 0;
  1153. case NILFS_ST_DONE:
  1154. return 0;
  1155. default:
  1156. BUG();
  1157. }
  1158. break_or_fail:
  1159. return err;
  1160. }
  1161. /**
  1162. * nilfs_segctor_begin_construction - setup segment buffer to make a new log
  1163. * @sci: nilfs_sc_info
  1164. * @nilfs: nilfs object
  1165. *
  1166. * Return: 0 on success, or a negative error code on failure.
  1167. */
  1168. static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
  1169. struct the_nilfs *nilfs)
  1170. {
  1171. struct nilfs_segment_buffer *segbuf, *prev;
  1172. __u64 nextnum;
  1173. int err, alloc = 0;
  1174. segbuf = nilfs_segbuf_new(sci->sc_super);
  1175. if (unlikely(!segbuf))
  1176. return -ENOMEM;
  1177. if (list_empty(&sci->sc_write_logs)) {
  1178. nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
  1179. nilfs->ns_pseg_offset, nilfs);
  1180. if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
  1181. nilfs_shift_to_next_segment(nilfs);
  1182. nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
  1183. }
  1184. segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
  1185. nextnum = nilfs->ns_nextnum;
  1186. if (nilfs->ns_segnum == nilfs->ns_nextnum)
  1187. /* Start from the head of a new full segment */
  1188. alloc++;
  1189. } else {
  1190. /* Continue logs */
  1191. prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
  1192. nilfs_segbuf_map_cont(segbuf, prev);
  1193. segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
  1194. nextnum = prev->sb_nextnum;
  1195. if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
  1196. nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
  1197. segbuf->sb_sum.seg_seq++;
  1198. alloc++;
  1199. }
  1200. }
  1201. err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
  1202. if (err)
  1203. goto failed;
  1204. if (alloc) {
  1205. err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
  1206. if (err)
  1207. goto failed;
  1208. }
  1209. nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
  1210. BUG_ON(!list_empty(&sci->sc_segbufs));
  1211. list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
  1212. sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
  1213. return 0;
  1214. failed:
  1215. nilfs_segbuf_free(segbuf);
  1216. return err;
  1217. }
  1218. static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
  1219. struct the_nilfs *nilfs, int nadd)
  1220. {
  1221. struct nilfs_segment_buffer *segbuf, *prev;
  1222. struct inode *sufile = nilfs->ns_sufile;
  1223. __u64 nextnextnum;
  1224. LIST_HEAD(list);
  1225. int err, ret, i;
  1226. prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
  1227. /*
  1228. * Since the segment specified with nextnum might be allocated during
  1229. * the previous construction, the buffer including its segusage may
  1230. * not be dirty. The following call ensures that the buffer is dirty
  1231. * and will pin the buffer on memory until the sufile is written.
  1232. */
  1233. err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
  1234. if (unlikely(err))
  1235. return err;
  1236. for (i = 0; i < nadd; i++) {
  1237. /* extend segment info */
  1238. err = -ENOMEM;
  1239. segbuf = nilfs_segbuf_new(sci->sc_super);
  1240. if (unlikely(!segbuf))
  1241. goto failed;
  1242. /* map this buffer to region of segment on-disk */
  1243. nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
  1244. sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
  1245. /* allocate the next next full segment */
  1246. err = nilfs_sufile_alloc(sufile, &nextnextnum);
  1247. if (unlikely(err))
  1248. goto failed_segbuf;
  1249. segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
  1250. nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
  1251. list_add_tail(&segbuf->sb_list, &list);
  1252. prev = segbuf;
  1253. }
  1254. list_splice_tail(&list, &sci->sc_segbufs);
  1255. return 0;
  1256. failed_segbuf:
  1257. nilfs_segbuf_free(segbuf);
  1258. failed:
  1259. list_for_each_entry(segbuf, &list, sb_list) {
  1260. ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
  1261. WARN_ON(ret); /* never fails */
  1262. }
  1263. nilfs_destroy_logs(&list);
  1264. return err;
  1265. }
  1266. static void nilfs_free_incomplete_logs(struct list_head *logs,
  1267. struct the_nilfs *nilfs)
  1268. {
  1269. struct nilfs_segment_buffer *segbuf, *prev;
  1270. struct inode *sufile = nilfs->ns_sufile;
  1271. int ret;
  1272. segbuf = NILFS_FIRST_SEGBUF(logs);
  1273. if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
  1274. ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
  1275. WARN_ON(ret); /* never fails */
  1276. }
  1277. if (atomic_read(&segbuf->sb_err)) {
  1278. /* Case 1: The first segment failed */
  1279. if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
  1280. /*
  1281. * Case 1a: Partial segment appended into an existing
  1282. * segment
  1283. */
  1284. nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
  1285. segbuf->sb_fseg_end);
  1286. else /* Case 1b: New full segment */
  1287. set_nilfs_discontinued(nilfs);
  1288. }
  1289. prev = segbuf;
  1290. list_for_each_entry_continue(segbuf, logs, sb_list) {
  1291. if (prev->sb_nextnum != segbuf->sb_nextnum) {
  1292. ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
  1293. WARN_ON(ret); /* never fails */
  1294. }
  1295. if (atomic_read(&segbuf->sb_err) &&
  1296. segbuf->sb_segnum != nilfs->ns_nextnum)
  1297. /* Case 2: extended segment (!= next) failed */
  1298. nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
  1299. prev = segbuf;
  1300. }
  1301. }
  1302. static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
  1303. struct inode *sufile)
  1304. {
  1305. struct nilfs_segment_buffer *segbuf;
  1306. unsigned long live_blocks;
  1307. int ret;
  1308. list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
  1309. live_blocks = segbuf->sb_sum.nblocks +
  1310. (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
  1311. ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
  1312. live_blocks,
  1313. sci->sc_seg_ctime);
  1314. WARN_ON(ret); /* always succeed because the segusage is dirty */
  1315. }
  1316. }
  1317. static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
  1318. {
  1319. struct nilfs_segment_buffer *segbuf;
  1320. int ret;
  1321. segbuf = NILFS_FIRST_SEGBUF(logs);
  1322. ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
  1323. segbuf->sb_pseg_start -
  1324. segbuf->sb_fseg_start, 0);
  1325. WARN_ON(ret); /* always succeed because the segusage is dirty */
  1326. list_for_each_entry_continue(segbuf, logs, sb_list) {
  1327. ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
  1328. 0, 0);
  1329. WARN_ON(ret); /* always succeed */
  1330. }
  1331. }
  1332. static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
  1333. struct nilfs_segment_buffer *last,
  1334. struct inode *sufile)
  1335. {
  1336. struct nilfs_segment_buffer *segbuf = last;
  1337. int ret;
  1338. list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
  1339. sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
  1340. ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
  1341. WARN_ON(ret);
  1342. }
  1343. nilfs_truncate_logs(&sci->sc_segbufs, last);
  1344. }
  1345. static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
  1346. struct the_nilfs *nilfs, int mode)
  1347. {
  1348. struct nilfs_cstage prev_stage = sci->sc_stage;
  1349. int err, nadd = 1;
  1350. /* Collection retry loop */
  1351. for (;;) {
  1352. sci->sc_nblk_this_inc = 0;
  1353. sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
  1354. err = nilfs_segctor_reset_segment_buffer(sci);
  1355. if (unlikely(err))
  1356. goto failed;
  1357. err = nilfs_segctor_collect_blocks(sci, mode);
  1358. sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
  1359. if (!err)
  1360. break;
  1361. if (unlikely(err != -E2BIG))
  1362. goto failed;
  1363. /* The current segment is filled up */
  1364. if (mode != SC_LSEG_SR ||
  1365. nilfs_sc_cstage_get(sci) < NILFS_ST_CPFILE)
  1366. break;
  1367. nilfs_clear_logs(&sci->sc_segbufs);
  1368. if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
  1369. err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
  1370. sci->sc_freesegs,
  1371. sci->sc_nfreesegs,
  1372. NULL);
  1373. WARN_ON(err); /* do not happen */
  1374. sci->sc_stage.flags &= ~NILFS_CF_SUFREED;
  1375. }
  1376. err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
  1377. if (unlikely(err))
  1378. return err;
  1379. nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
  1380. sci->sc_stage = prev_stage;
  1381. }
  1382. nilfs_segctor_zeropad_segsum(sci);
  1383. nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
  1384. return 0;
  1385. failed:
  1386. return err;
  1387. }
  1388. static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
  1389. struct buffer_head *new_bh)
  1390. {
  1391. BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
  1392. list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
  1393. /* The caller must release old_bh */
  1394. }
  1395. static int
  1396. nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
  1397. struct nilfs_segment_buffer *segbuf,
  1398. int mode)
  1399. {
  1400. struct inode *inode = NULL;
  1401. sector_t blocknr;
  1402. unsigned long nfinfo = segbuf->sb_sum.nfinfo;
  1403. unsigned long nblocks = 0, ndatablk = 0;
  1404. const struct nilfs_sc_operations *sc_op = NULL;
  1405. struct nilfs_segsum_pointer ssp;
  1406. struct nilfs_finfo *finfo = NULL;
  1407. union nilfs_binfo binfo;
  1408. struct buffer_head *bh, *bh_org;
  1409. ino_t ino = 0;
  1410. int err = 0;
  1411. if (!nfinfo)
  1412. goto out;
  1413. blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
  1414. ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
  1415. ssp.offset = sizeof(struct nilfs_segment_summary);
  1416. list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
  1417. if (bh == segbuf->sb_super_root)
  1418. break;
  1419. if (!finfo) {
  1420. finfo = nilfs_segctor_map_segsum_entry(
  1421. sci, &ssp, sizeof(*finfo));
  1422. ino = le64_to_cpu(finfo->fi_ino);
  1423. nblocks = le32_to_cpu(finfo->fi_nblocks);
  1424. ndatablk = le32_to_cpu(finfo->fi_ndatablk);
  1425. inode = bh->b_folio->mapping->host;
  1426. if (mode == SC_LSEG_DSYNC)
  1427. sc_op = &nilfs_sc_dsync_ops;
  1428. else if (ino == NILFS_DAT_INO)
  1429. sc_op = &nilfs_sc_dat_ops;
  1430. else /* file blocks */
  1431. sc_op = &nilfs_sc_file_ops;
  1432. }
  1433. bh_org = bh;
  1434. get_bh(bh_org);
  1435. err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
  1436. &binfo);
  1437. if (bh != bh_org)
  1438. nilfs_list_replace_buffer(bh_org, bh);
  1439. brelse(bh_org);
  1440. if (unlikely(err))
  1441. goto failed_bmap;
  1442. if (ndatablk > 0)
  1443. sc_op->write_data_binfo(sci, &ssp, &binfo);
  1444. else
  1445. sc_op->write_node_binfo(sci, &ssp, &binfo);
  1446. blocknr++;
  1447. if (--nblocks == 0) {
  1448. finfo = NULL;
  1449. if (--nfinfo == 0)
  1450. break;
  1451. } else if (ndatablk > 0)
  1452. ndatablk--;
  1453. }
  1454. out:
  1455. return 0;
  1456. failed_bmap:
  1457. return err;
  1458. }
  1459. static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
  1460. {
  1461. struct nilfs_segment_buffer *segbuf;
  1462. int err;
  1463. list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
  1464. err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
  1465. if (unlikely(err))
  1466. return err;
  1467. nilfs_segbuf_fill_in_segsum(segbuf);
  1468. }
  1469. return 0;
  1470. }
  1471. static void nilfs_begin_folio_io(struct folio *folio)
  1472. {
  1473. if (!folio || folio_test_writeback(folio))
  1474. /*
  1475. * For split b-tree node pages, this function may be called
  1476. * twice. We ignore the 2nd or later calls by this check.
  1477. */
  1478. return;
  1479. folio_lock(folio);
  1480. folio_clear_dirty_for_io(folio);
  1481. folio_start_writeback(folio);
  1482. folio_unlock(folio);
  1483. }
  1484. /**
  1485. * nilfs_prepare_write_logs - prepare to write logs
  1486. * @logs: logs to prepare for writing
  1487. * @seed: checksum seed value
  1488. *
  1489. * nilfs_prepare_write_logs() adds checksums and prepares the block
  1490. * buffers/folios for writing logs. In order to stabilize folios of
  1491. * memory-mapped file blocks by putting them in writeback state before
  1492. * calculating the checksums, first prepare to write payload blocks other
  1493. * than segment summary and super root blocks in which the checksums will
  1494. * be embedded.
  1495. */
  1496. static void nilfs_prepare_write_logs(struct list_head *logs, u32 seed)
  1497. {
  1498. struct nilfs_segment_buffer *segbuf;
  1499. struct folio *bd_folio = NULL, *fs_folio = NULL;
  1500. struct buffer_head *bh;
  1501. /* Prepare to write payload blocks */
  1502. list_for_each_entry(segbuf, logs, sb_list) {
  1503. list_for_each_entry(bh, &segbuf->sb_payload_buffers,
  1504. b_assoc_buffers) {
  1505. if (bh == segbuf->sb_super_root)
  1506. break;
  1507. set_buffer_async_write(bh);
  1508. if (bh->b_folio != fs_folio) {
  1509. nilfs_begin_folio_io(fs_folio);
  1510. fs_folio = bh->b_folio;
  1511. }
  1512. }
  1513. }
  1514. nilfs_begin_folio_io(fs_folio);
  1515. nilfs_add_checksums_on_logs(logs, seed);
  1516. /* Prepare to write segment summary blocks */
  1517. list_for_each_entry(segbuf, logs, sb_list) {
  1518. list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
  1519. b_assoc_buffers) {
  1520. mark_buffer_dirty(bh);
  1521. if (bh->b_folio == bd_folio)
  1522. continue;
  1523. if (bd_folio) {
  1524. folio_lock(bd_folio);
  1525. folio_wait_writeback(bd_folio);
  1526. folio_clear_dirty_for_io(bd_folio);
  1527. folio_start_writeback(bd_folio);
  1528. folio_unlock(bd_folio);
  1529. }
  1530. bd_folio = bh->b_folio;
  1531. }
  1532. }
  1533. /* Prepare to write super root block */
  1534. bh = NILFS_LAST_SEGBUF(logs)->sb_super_root;
  1535. if (bh) {
  1536. mark_buffer_dirty(bh);
  1537. if (bh->b_folio != bd_folio) {
  1538. folio_lock(bd_folio);
  1539. folio_wait_writeback(bd_folio);
  1540. folio_clear_dirty_for_io(bd_folio);
  1541. folio_start_writeback(bd_folio);
  1542. folio_unlock(bd_folio);
  1543. bd_folio = bh->b_folio;
  1544. }
  1545. }
  1546. if (bd_folio) {
  1547. folio_lock(bd_folio);
  1548. folio_wait_writeback(bd_folio);
  1549. folio_clear_dirty_for_io(bd_folio);
  1550. folio_start_writeback(bd_folio);
  1551. folio_unlock(bd_folio);
  1552. }
  1553. }
  1554. static int nilfs_segctor_write(struct nilfs_sc_info *sci,
  1555. struct the_nilfs *nilfs)
  1556. {
  1557. int ret;
  1558. ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
  1559. list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
  1560. return ret;
  1561. }
  1562. static void nilfs_end_folio_io(struct folio *folio, int err)
  1563. {
  1564. if (!folio)
  1565. return;
  1566. if (buffer_nilfs_node(folio_buffers(folio)) &&
  1567. !folio_test_writeback(folio)) {
  1568. /*
  1569. * For b-tree node pages, this function may be called twice
  1570. * or more because they might be split in a segment.
  1571. */
  1572. if (folio_test_dirty(folio)) {
  1573. /*
  1574. * For pages holding split b-tree node buffers, dirty
  1575. * flag on the buffers may be cleared discretely.
  1576. * In that case, the page is once redirtied for
  1577. * remaining buffers, and it must be cancelled if
  1578. * all the buffers get cleaned later.
  1579. */
  1580. folio_lock(folio);
  1581. if (nilfs_folio_buffers_clean(folio))
  1582. __nilfs_clear_folio_dirty(folio);
  1583. folio_unlock(folio);
  1584. }
  1585. return;
  1586. }
  1587. if (err || !nilfs_folio_buffers_clean(folio))
  1588. filemap_dirty_folio(folio->mapping, folio);
  1589. folio_end_writeback(folio);
  1590. }
  1591. static void nilfs_abort_logs(struct list_head *logs, int err)
  1592. {
  1593. struct nilfs_segment_buffer *segbuf;
  1594. struct folio *bd_folio = NULL, *fs_folio = NULL;
  1595. struct buffer_head *bh;
  1596. if (list_empty(logs))
  1597. return;
  1598. list_for_each_entry(segbuf, logs, sb_list) {
  1599. list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
  1600. b_assoc_buffers) {
  1601. clear_buffer_uptodate(bh);
  1602. if (bh->b_folio != bd_folio) {
  1603. if (bd_folio)
  1604. folio_end_writeback(bd_folio);
  1605. bd_folio = bh->b_folio;
  1606. }
  1607. }
  1608. list_for_each_entry(bh, &segbuf->sb_payload_buffers,
  1609. b_assoc_buffers) {
  1610. if (bh == segbuf->sb_super_root) {
  1611. clear_buffer_uptodate(bh);
  1612. if (bh->b_folio != bd_folio) {
  1613. folio_end_writeback(bd_folio);
  1614. bd_folio = bh->b_folio;
  1615. }
  1616. break;
  1617. }
  1618. clear_buffer_async_write(bh);
  1619. if (bh->b_folio != fs_folio) {
  1620. nilfs_end_folio_io(fs_folio, err);
  1621. fs_folio = bh->b_folio;
  1622. }
  1623. }
  1624. }
  1625. if (bd_folio)
  1626. folio_end_writeback(bd_folio);
  1627. nilfs_end_folio_io(fs_folio, err);
  1628. }
  1629. static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
  1630. struct the_nilfs *nilfs, int err)
  1631. {
  1632. LIST_HEAD(logs);
  1633. int ret;
  1634. list_splice_tail_init(&sci->sc_write_logs, &logs);
  1635. ret = nilfs_wait_on_logs(&logs);
  1636. nilfs_abort_logs(&logs, ret ? : err);
  1637. list_splice_tail_init(&sci->sc_segbufs, &logs);
  1638. if (list_empty(&logs))
  1639. return; /* if the first segment buffer preparation failed */
  1640. nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
  1641. nilfs_free_incomplete_logs(&logs, nilfs);
  1642. if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
  1643. ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
  1644. sci->sc_freesegs,
  1645. sci->sc_nfreesegs,
  1646. NULL);
  1647. WARN_ON(ret); /* do not happen */
  1648. }
  1649. nilfs_destroy_logs(&logs);
  1650. }
  1651. static void nilfs_set_next_segment(struct the_nilfs *nilfs,
  1652. struct nilfs_segment_buffer *segbuf)
  1653. {
  1654. nilfs->ns_segnum = segbuf->sb_segnum;
  1655. nilfs->ns_nextnum = segbuf->sb_nextnum;
  1656. nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
  1657. + segbuf->sb_sum.nblocks;
  1658. nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
  1659. nilfs->ns_ctime = segbuf->sb_sum.ctime;
  1660. }
  1661. static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
  1662. {
  1663. struct nilfs_segment_buffer *segbuf;
  1664. struct folio *bd_folio = NULL, *fs_folio = NULL;
  1665. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  1666. int update_sr = false;
  1667. list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
  1668. struct buffer_head *bh;
  1669. list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
  1670. b_assoc_buffers) {
  1671. set_buffer_uptodate(bh);
  1672. clear_buffer_dirty(bh);
  1673. if (bh->b_folio != bd_folio) {
  1674. if (bd_folio)
  1675. folio_end_writeback(bd_folio);
  1676. bd_folio = bh->b_folio;
  1677. }
  1678. }
  1679. /*
  1680. * We assume that the buffers which belong to the same folio
  1681. * continue over the buffer list.
  1682. * Under this assumption, the last BHs of folios is
  1683. * identifiable by the discontinuity of bh->b_folio
  1684. * (folio != fs_folio).
  1685. *
  1686. * For B-tree node blocks, however, this assumption is not
  1687. * guaranteed. The cleanup code of B-tree node folios needs
  1688. * special care.
  1689. */
  1690. list_for_each_entry(bh, &segbuf->sb_payload_buffers,
  1691. b_assoc_buffers) {
  1692. const unsigned long set_bits = BIT(BH_Uptodate);
  1693. const unsigned long clear_bits =
  1694. (BIT(BH_Dirty) | BIT(BH_Async_Write) |
  1695. BIT(BH_Delay) | BIT(BH_NILFS_Volatile) |
  1696. BIT(BH_NILFS_Redirected));
  1697. if (bh == segbuf->sb_super_root) {
  1698. set_buffer_uptodate(bh);
  1699. clear_buffer_dirty(bh);
  1700. if (bh->b_folio != bd_folio) {
  1701. folio_end_writeback(bd_folio);
  1702. bd_folio = bh->b_folio;
  1703. }
  1704. update_sr = true;
  1705. break;
  1706. }
  1707. set_mask_bits(&bh->b_state, clear_bits, set_bits);
  1708. if (bh->b_folio != fs_folio) {
  1709. nilfs_end_folio_io(fs_folio, 0);
  1710. fs_folio = bh->b_folio;
  1711. }
  1712. }
  1713. if (!nilfs_segbuf_simplex(segbuf)) {
  1714. if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
  1715. set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
  1716. sci->sc_lseg_stime = jiffies;
  1717. }
  1718. if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
  1719. clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
  1720. }
  1721. }
  1722. /*
  1723. * Since folios may continue over multiple segment buffers,
  1724. * end of the last folio must be checked outside of the loop.
  1725. */
  1726. if (bd_folio)
  1727. folio_end_writeback(bd_folio);
  1728. nilfs_end_folio_io(fs_folio, 0);
  1729. nilfs_drop_collected_inodes(&sci->sc_dirty_files);
  1730. if (nilfs_doing_gc())
  1731. nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
  1732. else
  1733. nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
  1734. sci->sc_nblk_inc += sci->sc_nblk_this_inc;
  1735. segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
  1736. nilfs_set_next_segment(nilfs, segbuf);
  1737. if (update_sr) {
  1738. nilfs->ns_flushed_device = 0;
  1739. nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
  1740. segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
  1741. clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
  1742. clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
  1743. set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
  1744. nilfs_segctor_clear_metadata_dirty(sci);
  1745. } else
  1746. clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
  1747. }
  1748. static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
  1749. {
  1750. int ret;
  1751. ret = nilfs_wait_on_logs(&sci->sc_write_logs);
  1752. if (!ret) {
  1753. nilfs_segctor_complete_write(sci);
  1754. nilfs_destroy_logs(&sci->sc_write_logs);
  1755. }
  1756. return ret;
  1757. }
  1758. static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
  1759. struct the_nilfs *nilfs)
  1760. {
  1761. struct nilfs_inode_info *ii, *n;
  1762. struct inode *ifile = sci->sc_root->ifile;
  1763. spin_lock(&nilfs->ns_inode_lock);
  1764. retry:
  1765. list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
  1766. if (!ii->i_bh) {
  1767. struct buffer_head *ibh;
  1768. int err;
  1769. spin_unlock(&nilfs->ns_inode_lock);
  1770. err = nilfs_ifile_get_inode_block(
  1771. ifile, ii->vfs_inode.i_ino, &ibh);
  1772. if (unlikely(err)) {
  1773. nilfs_warn(sci->sc_super,
  1774. "log writer: error %d getting inode block (ino=%lu)",
  1775. err, ii->vfs_inode.i_ino);
  1776. return err;
  1777. }
  1778. spin_lock(&nilfs->ns_inode_lock);
  1779. if (likely(!ii->i_bh))
  1780. ii->i_bh = ibh;
  1781. else
  1782. brelse(ibh);
  1783. goto retry;
  1784. }
  1785. // Always redirty the buffer to avoid race condition
  1786. mark_buffer_dirty(ii->i_bh);
  1787. nilfs_mdt_mark_dirty(ifile);
  1788. clear_bit(NILFS_I_QUEUED, &ii->i_state);
  1789. set_bit(NILFS_I_BUSY, &ii->i_state);
  1790. list_move_tail(&ii->i_dirty, &sci->sc_dirty_files);
  1791. }
  1792. spin_unlock(&nilfs->ns_inode_lock);
  1793. return 0;
  1794. }
  1795. static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
  1796. struct the_nilfs *nilfs)
  1797. {
  1798. struct nilfs_inode_info *ii, *n;
  1799. int during_mount = !(sci->sc_super->s_flags & SB_ACTIVE);
  1800. int defer_iput = false;
  1801. spin_lock(&nilfs->ns_inode_lock);
  1802. list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
  1803. if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
  1804. test_bit(NILFS_I_DIRTY, &ii->i_state))
  1805. continue;
  1806. clear_bit(NILFS_I_BUSY, &ii->i_state);
  1807. brelse(ii->i_bh);
  1808. ii->i_bh = NULL;
  1809. list_del_init(&ii->i_dirty);
  1810. if (!ii->vfs_inode.i_nlink || during_mount) {
  1811. /*
  1812. * Defer calling iput() to avoid deadlocks if
  1813. * i_nlink == 0 or mount is not yet finished.
  1814. */
  1815. list_add_tail(&ii->i_dirty, &sci->sc_iput_queue);
  1816. defer_iput = true;
  1817. } else {
  1818. spin_unlock(&nilfs->ns_inode_lock);
  1819. iput(&ii->vfs_inode);
  1820. spin_lock(&nilfs->ns_inode_lock);
  1821. }
  1822. }
  1823. spin_unlock(&nilfs->ns_inode_lock);
  1824. if (defer_iput)
  1825. schedule_work(&sci->sc_iput_work);
  1826. }
  1827. /*
  1828. * Main procedure of segment constructor
  1829. */
  1830. static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
  1831. {
  1832. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  1833. int err;
  1834. if (sb_rdonly(sci->sc_super))
  1835. return -EROFS;
  1836. nilfs_sc_cstage_set(sci, NILFS_ST_INIT);
  1837. sci->sc_cno = nilfs->ns_cno;
  1838. err = nilfs_segctor_collect_dirty_files(sci, nilfs);
  1839. if (unlikely(err))
  1840. goto out;
  1841. if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
  1842. set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
  1843. if (nilfs_segctor_clean(sci))
  1844. goto out;
  1845. do {
  1846. sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
  1847. err = nilfs_segctor_begin_construction(sci, nilfs);
  1848. if (unlikely(err))
  1849. goto failed;
  1850. /* Update time stamp */
  1851. sci->sc_seg_ctime = ktime_get_real_seconds();
  1852. err = nilfs_segctor_collect(sci, nilfs, mode);
  1853. if (unlikely(err))
  1854. goto failed;
  1855. /* Avoid empty segment */
  1856. if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE &&
  1857. nilfs_segbuf_empty(sci->sc_curseg)) {
  1858. nilfs_segctor_abort_construction(sci, nilfs, 1);
  1859. goto out;
  1860. }
  1861. err = nilfs_segctor_assign(sci, mode);
  1862. if (unlikely(err))
  1863. goto failed;
  1864. if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
  1865. nilfs_segctor_fill_in_file_bmap(sci);
  1866. if (mode == SC_LSEG_SR &&
  1867. nilfs_sc_cstage_get(sci) >= NILFS_ST_CPFILE) {
  1868. err = nilfs_cpfile_finalize_checkpoint(
  1869. nilfs->ns_cpfile, nilfs->ns_cno, sci->sc_root,
  1870. sci->sc_nblk_inc + sci->sc_nblk_this_inc,
  1871. sci->sc_seg_ctime,
  1872. !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags));
  1873. if (unlikely(err))
  1874. goto failed_to_write;
  1875. nilfs_segctor_fill_in_super_root(sci, nilfs);
  1876. }
  1877. nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
  1878. /* Write partial segments */
  1879. nilfs_prepare_write_logs(&sci->sc_segbufs, nilfs->ns_crc_seed);
  1880. err = nilfs_segctor_write(sci, nilfs);
  1881. if (unlikely(err))
  1882. goto failed_to_write;
  1883. if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE ||
  1884. nilfs->ns_blocksize_bits != PAGE_SHIFT) {
  1885. /*
  1886. * At this point, we avoid double buffering
  1887. * for blocksize < pagesize because page dirty
  1888. * flag is turned off during write and dirty
  1889. * buffers are not properly collected for
  1890. * pages crossing over segments.
  1891. */
  1892. err = nilfs_segctor_wait(sci);
  1893. if (err)
  1894. goto failed_to_write;
  1895. }
  1896. } while (nilfs_sc_cstage_get(sci) != NILFS_ST_DONE);
  1897. out:
  1898. nilfs_segctor_drop_written_files(sci, nilfs);
  1899. return err;
  1900. failed_to_write:
  1901. failed:
  1902. if (mode == SC_LSEG_SR && nilfs_sc_cstage_get(sci) >= NILFS_ST_IFILE)
  1903. nilfs_redirty_inodes(&sci->sc_dirty_files);
  1904. if (nilfs_doing_gc())
  1905. nilfs_redirty_inodes(&sci->sc_gc_inodes);
  1906. nilfs_segctor_abort_construction(sci, nilfs, err);
  1907. goto out;
  1908. }
  1909. /**
  1910. * nilfs_segctor_start_timer - set timer of background write
  1911. * @sci: nilfs_sc_info
  1912. *
  1913. * If the timer has already been set, it ignores the new request.
  1914. * This function MUST be called within a section locking the segment
  1915. * semaphore.
  1916. */
  1917. static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
  1918. {
  1919. spin_lock(&sci->sc_state_lock);
  1920. if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
  1921. if (sci->sc_task) {
  1922. sci->sc_timer.expires = jiffies + sci->sc_interval;
  1923. add_timer(&sci->sc_timer);
  1924. }
  1925. sci->sc_state |= NILFS_SEGCTOR_COMMIT;
  1926. }
  1927. spin_unlock(&sci->sc_state_lock);
  1928. }
  1929. static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
  1930. {
  1931. spin_lock(&sci->sc_state_lock);
  1932. if (!(sci->sc_flush_request & BIT(bn))) {
  1933. unsigned long prev_req = sci->sc_flush_request;
  1934. sci->sc_flush_request |= BIT(bn);
  1935. if (!prev_req)
  1936. wake_up(&sci->sc_wait_daemon);
  1937. }
  1938. spin_unlock(&sci->sc_state_lock);
  1939. }
  1940. struct nilfs_segctor_wait_request {
  1941. wait_queue_entry_t wq;
  1942. __u32 seq;
  1943. int err;
  1944. atomic_t done;
  1945. };
  1946. static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
  1947. {
  1948. struct nilfs_segctor_wait_request wait_req;
  1949. int err = 0;
  1950. init_wait(&wait_req.wq);
  1951. wait_req.err = 0;
  1952. atomic_set(&wait_req.done, 0);
  1953. init_waitqueue_entry(&wait_req.wq, current);
  1954. /*
  1955. * To prevent a race issue where completion notifications from the
  1956. * log writer thread are missed, increment the request sequence count
  1957. * "sc_seq_request" and insert a wait queue entry using the current
  1958. * sequence number into the "sc_wait_request" queue at the same time
  1959. * within the lock section of "sc_state_lock".
  1960. */
  1961. spin_lock(&sci->sc_state_lock);
  1962. wait_req.seq = ++sci->sc_seq_request;
  1963. add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
  1964. spin_unlock(&sci->sc_state_lock);
  1965. wake_up(&sci->sc_wait_daemon);
  1966. for (;;) {
  1967. set_current_state(TASK_INTERRUPTIBLE);
  1968. /*
  1969. * Synchronize only while the log writer thread is alive.
  1970. * Leave flushing out after the log writer thread exits to
  1971. * the cleanup work in nilfs_segctor_destroy().
  1972. */
  1973. if (!sci->sc_task)
  1974. break;
  1975. if (atomic_read(&wait_req.done)) {
  1976. err = wait_req.err;
  1977. break;
  1978. }
  1979. if (!signal_pending(current)) {
  1980. schedule();
  1981. continue;
  1982. }
  1983. err = -ERESTARTSYS;
  1984. break;
  1985. }
  1986. finish_wait(&sci->sc_wait_request, &wait_req.wq);
  1987. return err;
  1988. }
  1989. static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err, bool force)
  1990. {
  1991. struct nilfs_segctor_wait_request *wrq, *n;
  1992. unsigned long flags;
  1993. spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
  1994. list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.head, wq.entry) {
  1995. if (!atomic_read(&wrq->done) &&
  1996. (force || nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq))) {
  1997. wrq->err = err;
  1998. atomic_set(&wrq->done, 1);
  1999. }
  2000. if (atomic_read(&wrq->done)) {
  2001. wrq->wq.func(&wrq->wq,
  2002. TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  2003. 0, NULL);
  2004. }
  2005. }
  2006. spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
  2007. }
  2008. /**
  2009. * nilfs_construct_segment - construct a logical segment
  2010. * @sb: super block
  2011. *
  2012. * Return: 0 on success, or one of the following negative error codes on
  2013. * failure:
  2014. * * %-EIO - I/O error (including metadata corruption).
  2015. * * %-ENOMEM - Insufficient memory available.
  2016. * * %-ENOSPC - No space left on device (only in a panic state).
  2017. * * %-ERESTARTSYS - Interrupted.
  2018. * * %-EROFS - Read only filesystem.
  2019. */
  2020. int nilfs_construct_segment(struct super_block *sb)
  2021. {
  2022. struct the_nilfs *nilfs = sb->s_fs_info;
  2023. struct nilfs_sc_info *sci = nilfs->ns_writer;
  2024. struct nilfs_transaction_info *ti;
  2025. if (sb_rdonly(sb) || unlikely(!sci))
  2026. return -EROFS;
  2027. /* A call inside transactions causes a deadlock. */
  2028. BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
  2029. return nilfs_segctor_sync(sci);
  2030. }
  2031. /**
  2032. * nilfs_construct_dsync_segment - construct a data-only logical segment
  2033. * @sb: super block
  2034. * @inode: inode whose data blocks should be written out
  2035. * @start: start byte offset
  2036. * @end: end byte offset (inclusive)
  2037. *
  2038. * Return: 0 on success, or one of the following negative error codes on
  2039. * failure:
  2040. * * %-EIO - I/O error (including metadata corruption).
  2041. * * %-ENOMEM - Insufficient memory available.
  2042. * * %-ENOSPC - No space left on device (only in a panic state).
  2043. * * %-ERESTARTSYS - Interrupted.
  2044. * * %-EROFS - Read only filesystem.
  2045. */
  2046. int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
  2047. loff_t start, loff_t end)
  2048. {
  2049. struct the_nilfs *nilfs = sb->s_fs_info;
  2050. struct nilfs_sc_info *sci = nilfs->ns_writer;
  2051. struct nilfs_inode_info *ii;
  2052. struct nilfs_transaction_info ti;
  2053. int err = 0;
  2054. if (sb_rdonly(sb) || unlikely(!sci))
  2055. return -EROFS;
  2056. nilfs_transaction_lock(sb, &ti, 0);
  2057. ii = NILFS_I(inode);
  2058. if (test_bit(NILFS_I_INODE_SYNC, &ii->i_state) ||
  2059. nilfs_test_opt(nilfs, STRICT_ORDER) ||
  2060. test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
  2061. nilfs_discontinued(nilfs)) {
  2062. nilfs_transaction_unlock(sb);
  2063. err = nilfs_segctor_sync(sci);
  2064. return err;
  2065. }
  2066. spin_lock(&nilfs->ns_inode_lock);
  2067. if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
  2068. !test_bit(NILFS_I_BUSY, &ii->i_state)) {
  2069. spin_unlock(&nilfs->ns_inode_lock);
  2070. nilfs_transaction_unlock(sb);
  2071. return 0;
  2072. }
  2073. spin_unlock(&nilfs->ns_inode_lock);
  2074. sci->sc_dsync_inode = ii;
  2075. sci->sc_dsync_start = start;
  2076. sci->sc_dsync_end = end;
  2077. err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
  2078. if (!err)
  2079. nilfs->ns_flushed_device = 0;
  2080. nilfs_transaction_unlock(sb);
  2081. return err;
  2082. }
  2083. #define FLUSH_FILE_BIT (0x1) /* data file only */
  2084. #define FLUSH_DAT_BIT BIT(NILFS_DAT_INO) /* DAT only */
  2085. /**
  2086. * nilfs_segctor_accept - record accepted sequence count of log-write requests
  2087. * @sci: segment constructor object
  2088. */
  2089. static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
  2090. {
  2091. bool thread_is_alive;
  2092. spin_lock(&sci->sc_state_lock);
  2093. sci->sc_seq_accepted = sci->sc_seq_request;
  2094. thread_is_alive = (bool)sci->sc_task;
  2095. spin_unlock(&sci->sc_state_lock);
  2096. /*
  2097. * This function does not race with the log writer thread's
  2098. * termination. Therefore, deleting sc_timer, which should not be
  2099. * done after the log writer thread exits, can be done safely outside
  2100. * the area protected by sc_state_lock.
  2101. */
  2102. if (thread_is_alive)
  2103. timer_delete_sync(&sci->sc_timer);
  2104. }
  2105. /**
  2106. * nilfs_segctor_notify - notify the result of request to caller threads
  2107. * @sci: segment constructor object
  2108. * @mode: mode of log forming
  2109. * @err: error code to be notified
  2110. */
  2111. static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
  2112. {
  2113. /* Clear requests (even when the construction failed) */
  2114. spin_lock(&sci->sc_state_lock);
  2115. if (mode == SC_LSEG_SR) {
  2116. sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
  2117. sci->sc_seq_done = sci->sc_seq_accepted;
  2118. nilfs_segctor_wakeup(sci, err, false);
  2119. sci->sc_flush_request = 0;
  2120. } else {
  2121. if (mode == SC_FLUSH_FILE)
  2122. sci->sc_flush_request &= ~FLUSH_FILE_BIT;
  2123. else if (mode == SC_FLUSH_DAT)
  2124. sci->sc_flush_request &= ~FLUSH_DAT_BIT;
  2125. /* re-enable timer if checkpoint creation was not done */
  2126. if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && sci->sc_task &&
  2127. time_before(jiffies, sci->sc_timer.expires))
  2128. add_timer(&sci->sc_timer);
  2129. }
  2130. spin_unlock(&sci->sc_state_lock);
  2131. }
  2132. /**
  2133. * nilfs_segctor_construct - form logs and write them to disk
  2134. * @sci: segment constructor object
  2135. * @mode: mode of log forming
  2136. *
  2137. * Return: 0 on success, or a negative error code on failure.
  2138. */
  2139. static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
  2140. {
  2141. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  2142. struct nilfs_super_block **sbp;
  2143. int err = 0;
  2144. nilfs_segctor_accept(sci);
  2145. if (nilfs_discontinued(nilfs))
  2146. mode = SC_LSEG_SR;
  2147. if (!nilfs_segctor_confirm(sci))
  2148. err = nilfs_segctor_do_construct(sci, mode);
  2149. if (likely(!err)) {
  2150. if (mode != SC_FLUSH_DAT)
  2151. atomic_set(&nilfs->ns_ndirtyblks, 0);
  2152. if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
  2153. nilfs_discontinued(nilfs)) {
  2154. down_write(&nilfs->ns_sem);
  2155. err = -EIO;
  2156. sbp = nilfs_prepare_super(sci->sc_super,
  2157. nilfs_sb_will_flip(nilfs));
  2158. if (likely(sbp)) {
  2159. nilfs_set_log_cursor(sbp[0], nilfs);
  2160. err = nilfs_commit_super(sci->sc_super,
  2161. NILFS_SB_COMMIT);
  2162. }
  2163. up_write(&nilfs->ns_sem);
  2164. }
  2165. }
  2166. nilfs_segctor_notify(sci, mode, err);
  2167. return err;
  2168. }
  2169. static void nilfs_construction_timeout(struct timer_list *t)
  2170. {
  2171. struct nilfs_sc_info *sci = timer_container_of(sci, t, sc_timer);
  2172. wake_up_process(sci->sc_task);
  2173. }
  2174. static void
  2175. nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
  2176. {
  2177. struct nilfs_inode_info *ii, *n;
  2178. list_for_each_entry_safe(ii, n, head, i_dirty) {
  2179. if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
  2180. continue;
  2181. list_del_init(&ii->i_dirty);
  2182. truncate_inode_pages(&ii->vfs_inode.i_data, 0);
  2183. nilfs_btnode_cache_clear(ii->i_assoc_inode->i_mapping);
  2184. iput(&ii->vfs_inode);
  2185. }
  2186. }
  2187. int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
  2188. void **kbufs)
  2189. {
  2190. struct the_nilfs *nilfs = sb->s_fs_info;
  2191. struct nilfs_sc_info *sci = nilfs->ns_writer;
  2192. struct nilfs_transaction_info ti;
  2193. int err;
  2194. if (unlikely(!sci))
  2195. return -EROFS;
  2196. nilfs_transaction_lock(sb, &ti, 1);
  2197. err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
  2198. if (unlikely(err))
  2199. goto out_unlock;
  2200. err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
  2201. if (unlikely(err)) {
  2202. nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
  2203. goto out_unlock;
  2204. }
  2205. sci->sc_freesegs = kbufs[4];
  2206. sci->sc_nfreesegs = argv[4].v_nmembs;
  2207. list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
  2208. for (;;) {
  2209. err = nilfs_segctor_construct(sci, SC_LSEG_SR);
  2210. nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
  2211. if (likely(!err))
  2212. break;
  2213. nilfs_warn(sb, "error %d cleaning segments", err);
  2214. set_current_state(TASK_INTERRUPTIBLE);
  2215. schedule_timeout(sci->sc_interval);
  2216. }
  2217. if (nilfs_test_opt(nilfs, DISCARD)) {
  2218. int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
  2219. sci->sc_nfreesegs);
  2220. if (ret) {
  2221. nilfs_warn(sb,
  2222. "error %d on discard request, turning discards off for the device",
  2223. ret);
  2224. nilfs_clear_opt(nilfs, DISCARD);
  2225. }
  2226. }
  2227. out_unlock:
  2228. sci->sc_freesegs = NULL;
  2229. sci->sc_nfreesegs = 0;
  2230. nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
  2231. nilfs_transaction_unlock(sb);
  2232. return err;
  2233. }
  2234. static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
  2235. {
  2236. struct nilfs_transaction_info ti;
  2237. nilfs_transaction_lock(sci->sc_super, &ti, 0);
  2238. nilfs_segctor_construct(sci, mode);
  2239. /*
  2240. * Unclosed segment should be retried. We do this using sc_timer.
  2241. * Timeout of sc_timer will invoke complete construction which leads
  2242. * to close the current logical segment.
  2243. */
  2244. if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
  2245. nilfs_segctor_start_timer(sci);
  2246. nilfs_transaction_unlock(sci->sc_super);
  2247. }
  2248. static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
  2249. {
  2250. int mode = 0;
  2251. spin_lock(&sci->sc_state_lock);
  2252. mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
  2253. SC_FLUSH_DAT : SC_FLUSH_FILE;
  2254. spin_unlock(&sci->sc_state_lock);
  2255. if (mode) {
  2256. nilfs_segctor_do_construct(sci, mode);
  2257. spin_lock(&sci->sc_state_lock);
  2258. sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
  2259. ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
  2260. spin_unlock(&sci->sc_state_lock);
  2261. }
  2262. clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
  2263. }
  2264. static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
  2265. {
  2266. if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
  2267. time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
  2268. if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
  2269. return SC_FLUSH_FILE;
  2270. else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
  2271. return SC_FLUSH_DAT;
  2272. }
  2273. return SC_LSEG_SR;
  2274. }
  2275. /**
  2276. * nilfs_log_write_required - determine whether log writing is required
  2277. * @sci: nilfs_sc_info struct
  2278. * @modep: location for storing log writing mode
  2279. *
  2280. * Return: true if log writing is required, false otherwise. If log writing
  2281. * is required, the mode is stored in the location pointed to by @modep.
  2282. */
  2283. static bool nilfs_log_write_required(struct nilfs_sc_info *sci, int *modep)
  2284. {
  2285. bool timedout, ret = true;
  2286. spin_lock(&sci->sc_state_lock);
  2287. timedout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
  2288. time_after_eq(jiffies, sci->sc_timer.expires));
  2289. if (timedout || sci->sc_seq_request != sci->sc_seq_done)
  2290. *modep = SC_LSEG_SR;
  2291. else if (sci->sc_flush_request)
  2292. *modep = nilfs_segctor_flush_mode(sci);
  2293. else
  2294. ret = false;
  2295. spin_unlock(&sci->sc_state_lock);
  2296. return ret;
  2297. }
  2298. /**
  2299. * nilfs_segctor_thread - main loop of the log writer thread
  2300. * @arg: pointer to a struct nilfs_sc_info.
  2301. *
  2302. * nilfs_segctor_thread() is the main loop function of the log writer kernel
  2303. * thread, which determines whether log writing is necessary, and if so,
  2304. * performs the log write in the background, or waits if not. It is also
  2305. * used to decide the background writeback of the superblock.
  2306. *
  2307. * Return: Always 0.
  2308. */
  2309. static int nilfs_segctor_thread(void *arg)
  2310. {
  2311. struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
  2312. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  2313. nilfs_info(sci->sc_super,
  2314. "segctord starting. Construction interval = %lu seconds, CP frequency < %lu seconds",
  2315. sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
  2316. set_freezable();
  2317. while (!kthread_should_stop()) {
  2318. DEFINE_WAIT(wait);
  2319. bool should_write;
  2320. int mode;
  2321. if (freezing(current)) {
  2322. try_to_freeze();
  2323. continue;
  2324. }
  2325. prepare_to_wait(&sci->sc_wait_daemon, &wait,
  2326. TASK_INTERRUPTIBLE);
  2327. should_write = nilfs_log_write_required(sci, &mode);
  2328. if (!should_write)
  2329. schedule();
  2330. finish_wait(&sci->sc_wait_daemon, &wait);
  2331. if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
  2332. set_nilfs_discontinued(nilfs);
  2333. if (should_write)
  2334. nilfs_segctor_thread_construct(sci, mode);
  2335. }
  2336. /* end sync. */
  2337. spin_lock(&sci->sc_state_lock);
  2338. sci->sc_task = NULL;
  2339. timer_shutdown_sync(&sci->sc_timer);
  2340. spin_unlock(&sci->sc_state_lock);
  2341. return 0;
  2342. }
  2343. /*
  2344. * Setup & clean-up functions
  2345. */
  2346. static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
  2347. struct nilfs_root *root)
  2348. {
  2349. struct the_nilfs *nilfs = sb->s_fs_info;
  2350. struct nilfs_sc_info *sci;
  2351. sci = kzalloc_obj(*sci);
  2352. if (!sci)
  2353. return NULL;
  2354. sci->sc_super = sb;
  2355. nilfs_get_root(root);
  2356. sci->sc_root = root;
  2357. init_waitqueue_head(&sci->sc_wait_request);
  2358. init_waitqueue_head(&sci->sc_wait_daemon);
  2359. spin_lock_init(&sci->sc_state_lock);
  2360. INIT_LIST_HEAD(&sci->sc_dirty_files);
  2361. INIT_LIST_HEAD(&sci->sc_segbufs);
  2362. INIT_LIST_HEAD(&sci->sc_write_logs);
  2363. INIT_LIST_HEAD(&sci->sc_gc_inodes);
  2364. INIT_LIST_HEAD(&sci->sc_iput_queue);
  2365. INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
  2366. sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
  2367. sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
  2368. sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
  2369. if (nilfs->ns_interval)
  2370. sci->sc_interval = HZ * nilfs->ns_interval;
  2371. if (nilfs->ns_watermark)
  2372. sci->sc_watermark = nilfs->ns_watermark;
  2373. return sci;
  2374. }
  2375. static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
  2376. {
  2377. int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
  2378. /*
  2379. * The segctord thread was stopped and its timer was removed.
  2380. * But some tasks remain.
  2381. */
  2382. do {
  2383. struct nilfs_transaction_info ti;
  2384. nilfs_transaction_lock(sci->sc_super, &ti, 0);
  2385. ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
  2386. nilfs_transaction_unlock(sci->sc_super);
  2387. flush_work(&sci->sc_iput_work);
  2388. } while (ret && ret != -EROFS && retrycount-- > 0);
  2389. }
  2390. /**
  2391. * nilfs_segctor_destroy - destroy the segment constructor.
  2392. * @sci: nilfs_sc_info
  2393. *
  2394. * nilfs_segctor_destroy() kills the segctord thread and frees
  2395. * the nilfs_sc_info struct.
  2396. * Caller must hold the segment semaphore.
  2397. */
  2398. static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
  2399. {
  2400. struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
  2401. int flag;
  2402. up_write(&nilfs->ns_segctor_sem);
  2403. if (sci->sc_task) {
  2404. wake_up(&sci->sc_wait_daemon);
  2405. if (kthread_stop(sci->sc_task)) {
  2406. spin_lock(&sci->sc_state_lock);
  2407. sci->sc_task = NULL;
  2408. timer_shutdown_sync(&sci->sc_timer);
  2409. spin_unlock(&sci->sc_state_lock);
  2410. }
  2411. }
  2412. spin_lock(&sci->sc_state_lock);
  2413. flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
  2414. || sci->sc_seq_request != sci->sc_seq_done);
  2415. spin_unlock(&sci->sc_state_lock);
  2416. /*
  2417. * Forcibly wake up tasks waiting in nilfs_segctor_sync(), which can
  2418. * be called from delayed iput() via nilfs_evict_inode() and can race
  2419. * with the above log writer thread termination.
  2420. */
  2421. nilfs_segctor_wakeup(sci, 0, true);
  2422. if (flush_work(&sci->sc_iput_work))
  2423. flag = true;
  2424. if (flag || !nilfs_segctor_confirm(sci))
  2425. nilfs_segctor_write_out(sci);
  2426. if (!list_empty(&sci->sc_dirty_files)) {
  2427. nilfs_warn(sci->sc_super,
  2428. "disposed unprocessed dirty file(s) when stopping log writer");
  2429. nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
  2430. }
  2431. if (!list_empty(&sci->sc_iput_queue)) {
  2432. nilfs_warn(sci->sc_super,
  2433. "disposed unprocessed inode(s) in iput queue when stopping log writer");
  2434. nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1);
  2435. }
  2436. WARN_ON(!list_empty(&sci->sc_segbufs));
  2437. WARN_ON(!list_empty(&sci->sc_write_logs));
  2438. nilfs_put_root(sci->sc_root);
  2439. down_write(&nilfs->ns_segctor_sem);
  2440. kfree(sci);
  2441. }
  2442. /**
  2443. * nilfs_attach_log_writer - attach log writer
  2444. * @sb: super block instance
  2445. * @root: root object of the current filesystem tree
  2446. *
  2447. * This allocates a log writer object, initializes it, and starts the
  2448. * log writer.
  2449. *
  2450. * Return: 0 on success, or one of the following negative error codes on
  2451. * failure:
  2452. * * %-EINTR - Log writer thread creation failed due to interruption.
  2453. * * %-ENOMEM - Insufficient memory available.
  2454. */
  2455. int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)
  2456. {
  2457. struct the_nilfs *nilfs = sb->s_fs_info;
  2458. struct nilfs_sc_info *sci;
  2459. struct task_struct *t;
  2460. int err;
  2461. if (nilfs->ns_writer) {
  2462. /*
  2463. * This happens if the filesystem is made read-only by
  2464. * __nilfs_error or nilfs_remount and then remounted
  2465. * read/write. In these cases, reuse the existing
  2466. * writer.
  2467. */
  2468. return 0;
  2469. }
  2470. sci = nilfs_segctor_new(sb, root);
  2471. if (unlikely(!sci))
  2472. return -ENOMEM;
  2473. nilfs->ns_writer = sci;
  2474. t = kthread_create(nilfs_segctor_thread, sci, "segctord");
  2475. if (IS_ERR(t)) {
  2476. err = PTR_ERR(t);
  2477. nilfs_err(sb, "error %d creating segctord thread", err);
  2478. nilfs_detach_log_writer(sb);
  2479. return err;
  2480. }
  2481. sci->sc_task = t;
  2482. timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0);
  2483. wake_up_process(sci->sc_task);
  2484. return 0;
  2485. }
  2486. /**
  2487. * nilfs_detach_log_writer - destroy log writer
  2488. * @sb: super block instance
  2489. *
  2490. * This kills log writer daemon, frees the log writer object, and
  2491. * destroys list of dirty files.
  2492. */
  2493. void nilfs_detach_log_writer(struct super_block *sb)
  2494. {
  2495. struct the_nilfs *nilfs = sb->s_fs_info;
  2496. LIST_HEAD(garbage_list);
  2497. down_write(&nilfs->ns_segctor_sem);
  2498. if (nilfs->ns_writer) {
  2499. nilfs_segctor_destroy(nilfs->ns_writer);
  2500. nilfs->ns_writer = NULL;
  2501. }
  2502. set_nilfs_purging(nilfs);
  2503. /* Force to free the list of dirty files */
  2504. spin_lock(&nilfs->ns_inode_lock);
  2505. if (!list_empty(&nilfs->ns_dirty_files)) {
  2506. list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
  2507. nilfs_warn(sb,
  2508. "disposed unprocessed dirty file(s) when detaching log writer");
  2509. }
  2510. spin_unlock(&nilfs->ns_inode_lock);
  2511. up_write(&nilfs->ns_segctor_sem);
  2512. nilfs_dispose_list(nilfs, &garbage_list, 1);
  2513. clear_nilfs_purging(nilfs);
  2514. }