segment.c 71 KB

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