segment.c 72 KB

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