segment.c 78 KB

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