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