segment.c 75 KB

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