segment.c 72 KB

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