segment.c 74 KB

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