segment.c 75 KB

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