segment.c 71 KB

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