buffer.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/buffer.c
  4. *
  5. * Copyright (C) 1991, 1992, 2002 Linus Torvalds
  6. */
  7. /*
  8. * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
  9. *
  10. * Removed a lot of unnecessary code and simplified things now that
  11. * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
  12. *
  13. * Speed up hash, lru, and free list operations. Use gfp() for allocating
  14. * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
  15. *
  16. * Added 32k buffer block sizes - these are required older ARM systems. - RMK
  17. *
  18. * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/fs.h>
  24. #include <linux/iomap.h>
  25. #include <linux/mm.h>
  26. #include <linux/percpu.h>
  27. #include <linux/slab.h>
  28. #include <linux/capability.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/file.h>
  31. #include <linux/quotaops.h>
  32. #include <linux/highmem.h>
  33. #include <linux/export.h>
  34. #include <linux/backing-dev.h>
  35. #include <linux/writeback.h>
  36. #include <linux/hash.h>
  37. #include <linux/suspend.h>
  38. #include <linux/buffer_head.h>
  39. #include <linux/task_io_accounting_ops.h>
  40. #include <linux/bio.h>
  41. #include <linux/cpu.h>
  42. #include <linux/bitops.h>
  43. #include <linux/mpage.h>
  44. #include <linux/bit_spinlock.h>
  45. #include <linux/pagevec.h>
  46. #include <linux/sched/mm.h>
  47. #include <trace/events/block.h>
  48. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
  49. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  50. enum rw_hint hint, struct writeback_control *wbc);
  51. #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
  52. inline void touch_buffer(struct buffer_head *bh)
  53. {
  54. trace_block_touch_buffer(bh);
  55. mark_page_accessed(bh->b_page);
  56. }
  57. EXPORT_SYMBOL(touch_buffer);
  58. void __lock_buffer(struct buffer_head *bh)
  59. {
  60. wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
  61. }
  62. EXPORT_SYMBOL(__lock_buffer);
  63. void unlock_buffer(struct buffer_head *bh)
  64. {
  65. clear_bit_unlock(BH_Lock, &bh->b_state);
  66. smp_mb__after_atomic();
  67. wake_up_bit(&bh->b_state, BH_Lock);
  68. }
  69. EXPORT_SYMBOL(unlock_buffer);
  70. /*
  71. * Returns if the page has dirty or writeback buffers. If all the buffers
  72. * are unlocked and clean then the PageDirty information is stale. If
  73. * any of the pages are locked, it is assumed they are locked for IO.
  74. */
  75. void buffer_check_dirty_writeback(struct page *page,
  76. bool *dirty, bool *writeback)
  77. {
  78. struct buffer_head *head, *bh;
  79. *dirty = false;
  80. *writeback = false;
  81. BUG_ON(!PageLocked(page));
  82. if (!page_has_buffers(page))
  83. return;
  84. if (PageWriteback(page))
  85. *writeback = true;
  86. head = page_buffers(page);
  87. bh = head;
  88. do {
  89. if (buffer_locked(bh))
  90. *writeback = true;
  91. if (buffer_dirty(bh))
  92. *dirty = true;
  93. bh = bh->b_this_page;
  94. } while (bh != head);
  95. }
  96. EXPORT_SYMBOL(buffer_check_dirty_writeback);
  97. /*
  98. * Block until a buffer comes unlocked. This doesn't stop it
  99. * from becoming locked again - you have to lock it yourself
  100. * if you want to preserve its state.
  101. */
  102. void __wait_on_buffer(struct buffer_head * bh)
  103. {
  104. wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
  105. }
  106. EXPORT_SYMBOL(__wait_on_buffer);
  107. static void
  108. __clear_page_buffers(struct page *page)
  109. {
  110. ClearPagePrivate(page);
  111. set_page_private(page, 0);
  112. put_page(page);
  113. }
  114. static void buffer_io_error(struct buffer_head *bh, char *msg)
  115. {
  116. if (!test_bit(BH_Quiet, &bh->b_state))
  117. printk_ratelimited(KERN_ERR
  118. "Buffer I/O error on dev %pg, logical block %llu%s\n",
  119. bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
  120. }
  121. /*
  122. * End-of-IO handler helper function which does not touch the bh after
  123. * unlocking it.
  124. * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
  125. * a race there is benign: unlock_buffer() only use the bh's address for
  126. * hashing after unlocking the buffer, so it doesn't actually touch the bh
  127. * itself.
  128. */
  129. static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
  130. {
  131. if (uptodate) {
  132. set_buffer_uptodate(bh);
  133. } else {
  134. /* This happens, due to failed read-ahead attempts. */
  135. clear_buffer_uptodate(bh);
  136. }
  137. unlock_buffer(bh);
  138. }
  139. /*
  140. * Default synchronous end-of-IO handler.. Just mark it up-to-date and
  141. * unlock the buffer. This is what ll_rw_block uses too.
  142. */
  143. void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
  144. {
  145. __end_buffer_read_notouch(bh, uptodate);
  146. put_bh(bh);
  147. }
  148. EXPORT_SYMBOL(end_buffer_read_sync);
  149. void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  150. {
  151. if (uptodate) {
  152. set_buffer_uptodate(bh);
  153. } else {
  154. buffer_io_error(bh, ", lost sync page write");
  155. mark_buffer_write_io_error(bh);
  156. clear_buffer_uptodate(bh);
  157. }
  158. unlock_buffer(bh);
  159. put_bh(bh);
  160. }
  161. EXPORT_SYMBOL(end_buffer_write_sync);
  162. /*
  163. * Various filesystems appear to want __find_get_block to be non-blocking.
  164. * But it's the page lock which protects the buffers. To get around this,
  165. * we get exclusion from try_to_free_buffers with the blockdev mapping's
  166. * private_lock.
  167. *
  168. * Hack idea: for the blockdev mapping, private_lock contention
  169. * may be quite high. This code could TryLock the page, and if that
  170. * succeeds, there is no need to take private_lock.
  171. */
  172. static struct buffer_head *
  173. __find_get_block_slow(struct block_device *bdev, sector_t block)
  174. {
  175. struct inode *bd_inode = bdev->bd_inode;
  176. struct address_space *bd_mapping = bd_inode->i_mapping;
  177. struct buffer_head *ret = NULL;
  178. pgoff_t index;
  179. struct buffer_head *bh;
  180. struct buffer_head *head;
  181. struct page *page;
  182. int all_mapped = 1;
  183. static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1);
  184. index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
  185. page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
  186. if (!page)
  187. goto out;
  188. spin_lock(&bd_mapping->private_lock);
  189. if (!page_has_buffers(page))
  190. goto out_unlock;
  191. head = page_buffers(page);
  192. bh = head;
  193. do {
  194. if (!buffer_mapped(bh))
  195. all_mapped = 0;
  196. else if (bh->b_blocknr == block) {
  197. ret = bh;
  198. get_bh(bh);
  199. goto out_unlock;
  200. }
  201. bh = bh->b_this_page;
  202. } while (bh != head);
  203. /* we might be here because some of the buffers on this page are
  204. * not mapped. This is due to various races between
  205. * file io on the block device and getblk. It gets dealt with
  206. * elsewhere, don't buffer_error if we had some unmapped buffers
  207. */
  208. ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE);
  209. if (all_mapped && __ratelimit(&last_warned)) {
  210. printk("__find_get_block_slow() failed. block=%llu, "
  211. "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, "
  212. "device %pg blocksize: %d\n",
  213. (unsigned long long)block,
  214. (unsigned long long)bh->b_blocknr,
  215. bh->b_state, bh->b_size, bdev,
  216. 1 << bd_inode->i_blkbits);
  217. }
  218. out_unlock:
  219. spin_unlock(&bd_mapping->private_lock);
  220. put_page(page);
  221. out:
  222. return ret;
  223. }
  224. /*
  225. * I/O completion handler for block_read_full_page() - pages
  226. * which come unlocked at the end of I/O.
  227. */
  228. static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  229. {
  230. unsigned long flags;
  231. struct buffer_head *first;
  232. struct buffer_head *tmp;
  233. struct page *page;
  234. int page_uptodate = 1;
  235. BUG_ON(!buffer_async_read(bh));
  236. page = bh->b_page;
  237. if (uptodate) {
  238. set_buffer_uptodate(bh);
  239. } else {
  240. clear_buffer_uptodate(bh);
  241. buffer_io_error(bh, ", async page read");
  242. SetPageError(page);
  243. }
  244. /*
  245. * Be _very_ careful from here on. Bad things can happen if
  246. * two buffer heads end IO at almost the same time and both
  247. * decide that the page is now completely done.
  248. */
  249. first = page_buffers(page);
  250. local_irq_save(flags);
  251. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  252. clear_buffer_async_read(bh);
  253. unlock_buffer(bh);
  254. tmp = bh;
  255. do {
  256. if (!buffer_uptodate(tmp))
  257. page_uptodate = 0;
  258. if (buffer_async_read(tmp)) {
  259. BUG_ON(!buffer_locked(tmp));
  260. goto still_busy;
  261. }
  262. tmp = tmp->b_this_page;
  263. } while (tmp != bh);
  264. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  265. local_irq_restore(flags);
  266. /*
  267. * If none of the buffers had errors and they are all
  268. * uptodate then we can set the page uptodate.
  269. */
  270. if (page_uptodate && !PageError(page))
  271. SetPageUptodate(page);
  272. unlock_page(page);
  273. return;
  274. still_busy:
  275. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  276. local_irq_restore(flags);
  277. return;
  278. }
  279. /*
  280. * Completion handler for block_write_full_page() - pages which are unlocked
  281. * during I/O, and which have PageWriteback cleared upon I/O completion.
  282. */
  283. void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  284. {
  285. unsigned long flags;
  286. struct buffer_head *first;
  287. struct buffer_head *tmp;
  288. struct page *page;
  289. BUG_ON(!buffer_async_write(bh));
  290. page = bh->b_page;
  291. if (uptodate) {
  292. set_buffer_uptodate(bh);
  293. } else {
  294. buffer_io_error(bh, ", lost async page write");
  295. mark_buffer_write_io_error(bh);
  296. clear_buffer_uptodate(bh);
  297. SetPageError(page);
  298. }
  299. first = page_buffers(page);
  300. local_irq_save(flags);
  301. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  302. clear_buffer_async_write(bh);
  303. unlock_buffer(bh);
  304. tmp = bh->b_this_page;
  305. while (tmp != bh) {
  306. if (buffer_async_write(tmp)) {
  307. BUG_ON(!buffer_locked(tmp));
  308. goto still_busy;
  309. }
  310. tmp = tmp->b_this_page;
  311. }
  312. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  313. local_irq_restore(flags);
  314. end_page_writeback(page);
  315. return;
  316. still_busy:
  317. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  318. local_irq_restore(flags);
  319. return;
  320. }
  321. EXPORT_SYMBOL(end_buffer_async_write);
  322. /*
  323. * If a page's buffers are under async readin (end_buffer_async_read
  324. * completion) then there is a possibility that another thread of
  325. * control could lock one of the buffers after it has completed
  326. * but while some of the other buffers have not completed. This
  327. * locked buffer would confuse end_buffer_async_read() into not unlocking
  328. * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
  329. * that this buffer is not under async I/O.
  330. *
  331. * The page comes unlocked when it has no locked buffer_async buffers
  332. * left.
  333. *
  334. * PageLocked prevents anyone starting new async I/O reads any of
  335. * the buffers.
  336. *
  337. * PageWriteback is used to prevent simultaneous writeout of the same
  338. * page.
  339. *
  340. * PageLocked prevents anyone from starting writeback of a page which is
  341. * under read I/O (PageWriteback is only ever set against a locked page).
  342. */
  343. static void mark_buffer_async_read(struct buffer_head *bh)
  344. {
  345. bh->b_end_io = end_buffer_async_read;
  346. set_buffer_async_read(bh);
  347. }
  348. static void mark_buffer_async_write_endio(struct buffer_head *bh,
  349. bh_end_io_t *handler)
  350. {
  351. bh->b_end_io = handler;
  352. set_buffer_async_write(bh);
  353. }
  354. void mark_buffer_async_write(struct buffer_head *bh)
  355. {
  356. mark_buffer_async_write_endio(bh, end_buffer_async_write);
  357. }
  358. EXPORT_SYMBOL(mark_buffer_async_write);
  359. /*
  360. * fs/buffer.c contains helper functions for buffer-backed address space's
  361. * fsync functions. A common requirement for buffer-based filesystems is
  362. * that certain data from the backing blockdev needs to be written out for
  363. * a successful fsync(). For example, ext2 indirect blocks need to be
  364. * written back and waited upon before fsync() returns.
  365. *
  366. * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
  367. * inode_has_buffers() and invalidate_inode_buffers() are provided for the
  368. * management of a list of dependent buffers at ->i_mapping->private_list.
  369. *
  370. * Locking is a little subtle: try_to_free_buffers() will remove buffers
  371. * from their controlling inode's queue when they are being freed. But
  372. * try_to_free_buffers() will be operating against the *blockdev* mapping
  373. * at the time, not against the S_ISREG file which depends on those buffers.
  374. * So the locking for private_list is via the private_lock in the address_space
  375. * which backs the buffers. Which is different from the address_space
  376. * against which the buffers are listed. So for a particular address_space,
  377. * mapping->private_lock does *not* protect mapping->private_list! In fact,
  378. * mapping->private_list will always be protected by the backing blockdev's
  379. * ->private_lock.
  380. *
  381. * Which introduces a requirement: all buffers on an address_space's
  382. * ->private_list must be from the same address_space: the blockdev's.
  383. *
  384. * address_spaces which do not place buffers at ->private_list via these
  385. * utility functions are free to use private_lock and private_list for
  386. * whatever they want. The only requirement is that list_empty(private_list)
  387. * be true at clear_inode() time.
  388. *
  389. * FIXME: clear_inode should not call invalidate_inode_buffers(). The
  390. * filesystems should do that. invalidate_inode_buffers() should just go
  391. * BUG_ON(!list_empty).
  392. *
  393. * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
  394. * take an address_space, not an inode. And it should be called
  395. * mark_buffer_dirty_fsync() to clearly define why those buffers are being
  396. * queued up.
  397. *
  398. * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
  399. * list if it is already on a list. Because if the buffer is on a list,
  400. * it *must* already be on the right one. If not, the filesystem is being
  401. * silly. This will save a ton of locking. But first we have to ensure
  402. * that buffers are taken *off* the old inode's list when they are freed
  403. * (presumably in truncate). That requires careful auditing of all
  404. * filesystems (do it inside bforget()). It could also be done by bringing
  405. * b_inode back.
  406. */
  407. /*
  408. * The buffer's backing address_space's private_lock must be held
  409. */
  410. static void __remove_assoc_queue(struct buffer_head *bh)
  411. {
  412. list_del_init(&bh->b_assoc_buffers);
  413. WARN_ON(!bh->b_assoc_map);
  414. bh->b_assoc_map = NULL;
  415. }
  416. int inode_has_buffers(struct inode *inode)
  417. {
  418. return !list_empty(&inode->i_data.private_list);
  419. }
  420. /*
  421. * osync is designed to support O_SYNC io. It waits synchronously for
  422. * all already-submitted IO to complete, but does not queue any new
  423. * writes to the disk.
  424. *
  425. * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
  426. * you dirty the buffers, and then use osync_inode_buffers to wait for
  427. * completion. Any other dirty buffers which are not yet queued for
  428. * write will not be flushed to disk by the osync.
  429. */
  430. static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
  431. {
  432. struct buffer_head *bh;
  433. struct list_head *p;
  434. int err = 0;
  435. spin_lock(lock);
  436. repeat:
  437. list_for_each_prev(p, list) {
  438. bh = BH_ENTRY(p);
  439. if (buffer_locked(bh)) {
  440. get_bh(bh);
  441. spin_unlock(lock);
  442. wait_on_buffer(bh);
  443. if (!buffer_uptodate(bh))
  444. err = -EIO;
  445. brelse(bh);
  446. spin_lock(lock);
  447. goto repeat;
  448. }
  449. }
  450. spin_unlock(lock);
  451. return err;
  452. }
  453. void emergency_thaw_bdev(struct super_block *sb)
  454. {
  455. while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
  456. printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev);
  457. }
  458. /**
  459. * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
  460. * @mapping: the mapping which wants those buffers written
  461. *
  462. * Starts I/O against the buffers at mapping->private_list, and waits upon
  463. * that I/O.
  464. *
  465. * Basically, this is a convenience function for fsync().
  466. * @mapping is a file or directory which needs those buffers to be written for
  467. * a successful fsync().
  468. */
  469. int sync_mapping_buffers(struct address_space *mapping)
  470. {
  471. struct address_space *buffer_mapping = mapping->private_data;
  472. if (buffer_mapping == NULL || list_empty(&mapping->private_list))
  473. return 0;
  474. return fsync_buffers_list(&buffer_mapping->private_lock,
  475. &mapping->private_list);
  476. }
  477. EXPORT_SYMBOL(sync_mapping_buffers);
  478. /*
  479. * Called when we've recently written block `bblock', and it is known that
  480. * `bblock' was for a buffer_boundary() buffer. This means that the block at
  481. * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
  482. * dirty, schedule it for IO. So that indirects merge nicely with their data.
  483. */
  484. void write_boundary_block(struct block_device *bdev,
  485. sector_t bblock, unsigned blocksize)
  486. {
  487. struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
  488. if (bh) {
  489. if (buffer_dirty(bh))
  490. ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
  491. put_bh(bh);
  492. }
  493. }
  494. void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
  495. {
  496. struct address_space *mapping = inode->i_mapping;
  497. struct address_space *buffer_mapping = bh->b_page->mapping;
  498. mark_buffer_dirty(bh);
  499. if (!mapping->private_data) {
  500. mapping->private_data = buffer_mapping;
  501. } else {
  502. BUG_ON(mapping->private_data != buffer_mapping);
  503. }
  504. if (!bh->b_assoc_map) {
  505. spin_lock(&buffer_mapping->private_lock);
  506. list_move_tail(&bh->b_assoc_buffers,
  507. &mapping->private_list);
  508. bh->b_assoc_map = mapping;
  509. spin_unlock(&buffer_mapping->private_lock);
  510. }
  511. }
  512. EXPORT_SYMBOL(mark_buffer_dirty_inode);
  513. /*
  514. * Mark the page dirty, and set it dirty in the page cache, and mark the inode
  515. * dirty.
  516. *
  517. * If warn is true, then emit a warning if the page is not uptodate and has
  518. * not been truncated.
  519. *
  520. * The caller must hold lock_page_memcg().
  521. */
  522. void __set_page_dirty(struct page *page, struct address_space *mapping,
  523. int warn)
  524. {
  525. unsigned long flags;
  526. xa_lock_irqsave(&mapping->i_pages, flags);
  527. if (page->mapping) { /* Race with truncate? */
  528. WARN_ON_ONCE(warn && !PageUptodate(page));
  529. account_page_dirtied(page, mapping);
  530. __xa_set_mark(&mapping->i_pages, page_index(page),
  531. PAGECACHE_TAG_DIRTY);
  532. }
  533. xa_unlock_irqrestore(&mapping->i_pages, flags);
  534. }
  535. EXPORT_SYMBOL_GPL(__set_page_dirty);
  536. /*
  537. * Add a page to the dirty page list.
  538. *
  539. * It is a sad fact of life that this function is called from several places
  540. * deeply under spinlocking. It may not sleep.
  541. *
  542. * If the page has buffers, the uptodate buffers are set dirty, to preserve
  543. * dirty-state coherency between the page and the buffers. It the page does
  544. * not have buffers then when they are later attached they will all be set
  545. * dirty.
  546. *
  547. * The buffers are dirtied before the page is dirtied. There's a small race
  548. * window in which a writepage caller may see the page cleanness but not the
  549. * buffer dirtiness. That's fine. If this code were to set the page dirty
  550. * before the buffers, a concurrent writepage caller could clear the page dirty
  551. * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
  552. * page on the dirty page list.
  553. *
  554. * We use private_lock to lock against try_to_free_buffers while using the
  555. * page's buffer list. Also use this to protect against clean buffers being
  556. * added to the page after it was set dirty.
  557. *
  558. * FIXME: may need to call ->reservepage here as well. That's rather up to the
  559. * address_space though.
  560. */
  561. int __set_page_dirty_buffers(struct page *page)
  562. {
  563. int newly_dirty;
  564. struct address_space *mapping = page_mapping(page);
  565. if (unlikely(!mapping))
  566. return !TestSetPageDirty(page);
  567. spin_lock(&mapping->private_lock);
  568. if (page_has_buffers(page)) {
  569. struct buffer_head *head = page_buffers(page);
  570. struct buffer_head *bh = head;
  571. do {
  572. set_buffer_dirty(bh);
  573. bh = bh->b_this_page;
  574. } while (bh != head);
  575. }
  576. /*
  577. * Lock out page->mem_cgroup migration to keep PageDirty
  578. * synchronized with per-memcg dirty page counters.
  579. */
  580. lock_page_memcg(page);
  581. newly_dirty = !TestSetPageDirty(page);
  582. spin_unlock(&mapping->private_lock);
  583. if (newly_dirty)
  584. __set_page_dirty(page, mapping, 1);
  585. unlock_page_memcg(page);
  586. if (newly_dirty)
  587. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  588. return newly_dirty;
  589. }
  590. EXPORT_SYMBOL(__set_page_dirty_buffers);
  591. /*
  592. * Write out and wait upon a list of buffers.
  593. *
  594. * We have conflicting pressures: we want to make sure that all
  595. * initially dirty buffers get waited on, but that any subsequently
  596. * dirtied buffers don't. After all, we don't want fsync to last
  597. * forever if somebody is actively writing to the file.
  598. *
  599. * Do this in two main stages: first we copy dirty buffers to a
  600. * temporary inode list, queueing the writes as we go. Then we clean
  601. * up, waiting for those writes to complete.
  602. *
  603. * During this second stage, any subsequent updates to the file may end
  604. * up refiling the buffer on the original inode's dirty list again, so
  605. * there is a chance we will end up with a buffer queued for write but
  606. * not yet completed on that list. So, as a final cleanup we go through
  607. * the osync code to catch these locked, dirty buffers without requeuing
  608. * any newly dirty buffers for write.
  609. */
  610. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
  611. {
  612. struct buffer_head *bh;
  613. struct list_head tmp;
  614. struct address_space *mapping;
  615. int err = 0, err2;
  616. struct blk_plug plug;
  617. INIT_LIST_HEAD(&tmp);
  618. blk_start_plug(&plug);
  619. spin_lock(lock);
  620. while (!list_empty(list)) {
  621. bh = BH_ENTRY(list->next);
  622. mapping = bh->b_assoc_map;
  623. __remove_assoc_queue(bh);
  624. /* Avoid race with mark_buffer_dirty_inode() which does
  625. * a lockless check and we rely on seeing the dirty bit */
  626. smp_mb();
  627. if (buffer_dirty(bh) || buffer_locked(bh)) {
  628. list_add(&bh->b_assoc_buffers, &tmp);
  629. bh->b_assoc_map = mapping;
  630. if (buffer_dirty(bh)) {
  631. get_bh(bh);
  632. spin_unlock(lock);
  633. /*
  634. * Ensure any pending I/O completes so that
  635. * write_dirty_buffer() actually writes the
  636. * current contents - it is a noop if I/O is
  637. * still in flight on potentially older
  638. * contents.
  639. */
  640. write_dirty_buffer(bh, REQ_SYNC);
  641. /*
  642. * Kick off IO for the previous mapping. Note
  643. * that we will not run the very last mapping,
  644. * wait_on_buffer() will do that for us
  645. * through sync_buffer().
  646. */
  647. brelse(bh);
  648. spin_lock(lock);
  649. }
  650. }
  651. }
  652. spin_unlock(lock);
  653. blk_finish_plug(&plug);
  654. spin_lock(lock);
  655. while (!list_empty(&tmp)) {
  656. bh = BH_ENTRY(tmp.prev);
  657. get_bh(bh);
  658. mapping = bh->b_assoc_map;
  659. __remove_assoc_queue(bh);
  660. /* Avoid race with mark_buffer_dirty_inode() which does
  661. * a lockless check and we rely on seeing the dirty bit */
  662. smp_mb();
  663. if (buffer_dirty(bh)) {
  664. list_add(&bh->b_assoc_buffers,
  665. &mapping->private_list);
  666. bh->b_assoc_map = mapping;
  667. }
  668. spin_unlock(lock);
  669. wait_on_buffer(bh);
  670. if (!buffer_uptodate(bh))
  671. err = -EIO;
  672. brelse(bh);
  673. spin_lock(lock);
  674. }
  675. spin_unlock(lock);
  676. err2 = osync_buffers_list(lock, list);
  677. if (err)
  678. return err;
  679. else
  680. return err2;
  681. }
  682. /*
  683. * Invalidate any and all dirty buffers on a given inode. We are
  684. * probably unmounting the fs, but that doesn't mean we have already
  685. * done a sync(). Just drop the buffers from the inode list.
  686. *
  687. * NOTE: we take the inode's blockdev's mapping's private_lock. Which
  688. * assumes that all the buffers are against the blockdev. Not true
  689. * for reiserfs.
  690. */
  691. void invalidate_inode_buffers(struct inode *inode)
  692. {
  693. if (inode_has_buffers(inode)) {
  694. struct address_space *mapping = &inode->i_data;
  695. struct list_head *list = &mapping->private_list;
  696. struct address_space *buffer_mapping = mapping->private_data;
  697. spin_lock(&buffer_mapping->private_lock);
  698. while (!list_empty(list))
  699. __remove_assoc_queue(BH_ENTRY(list->next));
  700. spin_unlock(&buffer_mapping->private_lock);
  701. }
  702. }
  703. EXPORT_SYMBOL(invalidate_inode_buffers);
  704. /*
  705. * Remove any clean buffers from the inode's buffer list. This is called
  706. * when we're trying to free the inode itself. Those buffers can pin it.
  707. *
  708. * Returns true if all buffers were removed.
  709. */
  710. int remove_inode_buffers(struct inode *inode)
  711. {
  712. int ret = 1;
  713. if (inode_has_buffers(inode)) {
  714. struct address_space *mapping = &inode->i_data;
  715. struct list_head *list = &mapping->private_list;
  716. struct address_space *buffer_mapping = mapping->private_data;
  717. spin_lock(&buffer_mapping->private_lock);
  718. while (!list_empty(list)) {
  719. struct buffer_head *bh = BH_ENTRY(list->next);
  720. if (buffer_dirty(bh)) {
  721. ret = 0;
  722. break;
  723. }
  724. __remove_assoc_queue(bh);
  725. }
  726. spin_unlock(&buffer_mapping->private_lock);
  727. }
  728. return ret;
  729. }
  730. /*
  731. * Create the appropriate buffers when given a page for data area and
  732. * the size of each buffer.. Use the bh->b_this_page linked list to
  733. * follow the buffers created. Return NULL if unable to create more
  734. * buffers.
  735. *
  736. * The retry flag is used to differentiate async IO (paging, swapping)
  737. * which may not fail from ordinary buffer allocations.
  738. */
  739. struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
  740. bool retry)
  741. {
  742. struct buffer_head *bh, *head;
  743. gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
  744. long offset;
  745. struct mem_cgroup *memcg;
  746. if (retry)
  747. gfp |= __GFP_NOFAIL;
  748. memcg = get_mem_cgroup_from_page(page);
  749. memalloc_use_memcg(memcg);
  750. head = NULL;
  751. offset = PAGE_SIZE;
  752. while ((offset -= size) >= 0) {
  753. bh = alloc_buffer_head(gfp);
  754. if (!bh)
  755. goto no_grow;
  756. bh->b_this_page = head;
  757. bh->b_blocknr = -1;
  758. head = bh;
  759. bh->b_size = size;
  760. /* Link the buffer to its page */
  761. set_bh_page(bh, page, offset);
  762. }
  763. out:
  764. memalloc_unuse_memcg();
  765. mem_cgroup_put(memcg);
  766. return head;
  767. /*
  768. * In case anything failed, we just free everything we got.
  769. */
  770. no_grow:
  771. if (head) {
  772. do {
  773. bh = head;
  774. head = head->b_this_page;
  775. free_buffer_head(bh);
  776. } while (head);
  777. }
  778. goto out;
  779. }
  780. EXPORT_SYMBOL_GPL(alloc_page_buffers);
  781. static inline void
  782. link_dev_buffers(struct page *page, struct buffer_head *head)
  783. {
  784. struct buffer_head *bh, *tail;
  785. bh = head;
  786. do {
  787. tail = bh;
  788. bh = bh->b_this_page;
  789. } while (bh);
  790. tail->b_this_page = head;
  791. attach_page_buffers(page, head);
  792. }
  793. static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
  794. {
  795. sector_t retval = ~((sector_t)0);
  796. loff_t sz = i_size_read(bdev->bd_inode);
  797. if (sz) {
  798. unsigned int sizebits = blksize_bits(size);
  799. retval = (sz >> sizebits);
  800. }
  801. return retval;
  802. }
  803. /*
  804. * Initialise the state of a blockdev page's buffers.
  805. */
  806. static sector_t
  807. init_page_buffers(struct page *page, struct block_device *bdev,
  808. sector_t block, int size)
  809. {
  810. struct buffer_head *head = page_buffers(page);
  811. struct buffer_head *bh = head;
  812. int uptodate = PageUptodate(page);
  813. sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
  814. do {
  815. if (!buffer_mapped(bh)) {
  816. bh->b_end_io = NULL;
  817. bh->b_private = NULL;
  818. bh->b_bdev = bdev;
  819. bh->b_blocknr = block;
  820. if (uptodate)
  821. set_buffer_uptodate(bh);
  822. if (block < end_block)
  823. set_buffer_mapped(bh);
  824. }
  825. block++;
  826. bh = bh->b_this_page;
  827. } while (bh != head);
  828. /*
  829. * Caller needs to validate requested block against end of device.
  830. */
  831. return end_block;
  832. }
  833. /*
  834. * Create the page-cache page that contains the requested block.
  835. *
  836. * This is used purely for blockdev mappings.
  837. */
  838. static int
  839. grow_dev_page(struct block_device *bdev, sector_t block,
  840. pgoff_t index, int size, int sizebits, gfp_t gfp)
  841. {
  842. struct inode *inode = bdev->bd_inode;
  843. struct page *page;
  844. struct buffer_head *bh;
  845. sector_t end_block;
  846. int ret = 0; /* Will call free_more_memory() */
  847. gfp_t gfp_mask;
  848. gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
  849. /*
  850. * XXX: __getblk_slow() can not really deal with failure and
  851. * will endlessly loop on improvised global reclaim. Prefer
  852. * looping in the allocator rather than here, at least that
  853. * code knows what it's doing.
  854. */
  855. gfp_mask |= __GFP_NOFAIL;
  856. page = find_or_create_page(inode->i_mapping, index, gfp_mask);
  857. BUG_ON(!PageLocked(page));
  858. if (page_has_buffers(page)) {
  859. bh = page_buffers(page);
  860. if (bh->b_size == size) {
  861. end_block = init_page_buffers(page, bdev,
  862. (sector_t)index << sizebits,
  863. size);
  864. goto done;
  865. }
  866. if (!try_to_free_buffers(page))
  867. goto failed;
  868. }
  869. /*
  870. * Allocate some buffers for this page
  871. */
  872. bh = alloc_page_buffers(page, size, true);
  873. /*
  874. * Link the page to the buffers and initialise them. Take the
  875. * lock to be atomic wrt __find_get_block(), which does not
  876. * run under the page lock.
  877. */
  878. spin_lock(&inode->i_mapping->private_lock);
  879. link_dev_buffers(page, bh);
  880. end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
  881. size);
  882. spin_unlock(&inode->i_mapping->private_lock);
  883. done:
  884. ret = (block < end_block) ? 1 : -ENXIO;
  885. failed:
  886. unlock_page(page);
  887. put_page(page);
  888. return ret;
  889. }
  890. /*
  891. * Create buffers for the specified block device block's page. If
  892. * that page was dirty, the buffers are set dirty also.
  893. */
  894. static int
  895. grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
  896. {
  897. pgoff_t index;
  898. int sizebits;
  899. sizebits = -1;
  900. do {
  901. sizebits++;
  902. } while ((size << sizebits) < PAGE_SIZE);
  903. index = block >> sizebits;
  904. /*
  905. * Check for a block which wants to lie outside our maximum possible
  906. * pagecache index. (this comparison is done using sector_t types).
  907. */
  908. if (unlikely(index != block >> sizebits)) {
  909. printk(KERN_ERR "%s: requested out-of-range block %llu for "
  910. "device %pg\n",
  911. __func__, (unsigned long long)block,
  912. bdev);
  913. return -EIO;
  914. }
  915. /* Create a page with the proper size buffers.. */
  916. return grow_dev_page(bdev, block, index, size, sizebits, gfp);
  917. }
  918. static struct buffer_head *
  919. __getblk_slow(struct block_device *bdev, sector_t block,
  920. unsigned size, gfp_t gfp)
  921. {
  922. /* Size must be multiple of hard sectorsize */
  923. if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
  924. (size < 512 || size > PAGE_SIZE))) {
  925. printk(KERN_ERR "getblk(): invalid block size %d requested\n",
  926. size);
  927. printk(KERN_ERR "logical block size: %d\n",
  928. bdev_logical_block_size(bdev));
  929. dump_stack();
  930. return NULL;
  931. }
  932. for (;;) {
  933. struct buffer_head *bh;
  934. int ret;
  935. bh = __find_get_block(bdev, block, size);
  936. if (bh)
  937. return bh;
  938. ret = grow_buffers(bdev, block, size, gfp);
  939. if (ret < 0)
  940. return NULL;
  941. }
  942. }
  943. /*
  944. * The relationship between dirty buffers and dirty pages:
  945. *
  946. * Whenever a page has any dirty buffers, the page's dirty bit is set, and
  947. * the page is tagged dirty in the page cache.
  948. *
  949. * At all times, the dirtiness of the buffers represents the dirtiness of
  950. * subsections of the page. If the page has buffers, the page dirty bit is
  951. * merely a hint about the true dirty state.
  952. *
  953. * When a page is set dirty in its entirety, all its buffers are marked dirty
  954. * (if the page has buffers).
  955. *
  956. * When a buffer is marked dirty, its page is dirtied, but the page's other
  957. * buffers are not.
  958. *
  959. * Also. When blockdev buffers are explicitly read with bread(), they
  960. * individually become uptodate. But their backing page remains not
  961. * uptodate - even if all of its buffers are uptodate. A subsequent
  962. * block_read_full_page() against that page will discover all the uptodate
  963. * buffers, will set the page uptodate and will perform no I/O.
  964. */
  965. /**
  966. * mark_buffer_dirty - mark a buffer_head as needing writeout
  967. * @bh: the buffer_head to mark dirty
  968. *
  969. * mark_buffer_dirty() will set the dirty bit against the buffer, then set
  970. * its backing page dirty, then tag the page as dirty in the page cache
  971. * and then attach the address_space's inode to its superblock's dirty
  972. * inode list.
  973. *
  974. * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
  975. * i_pages lock and mapping->host->i_lock.
  976. */
  977. void mark_buffer_dirty(struct buffer_head *bh)
  978. {
  979. WARN_ON_ONCE(!buffer_uptodate(bh));
  980. trace_block_dirty_buffer(bh);
  981. /*
  982. * Very *carefully* optimize the it-is-already-dirty case.
  983. *
  984. * Don't let the final "is it dirty" escape to before we
  985. * perhaps modified the buffer.
  986. */
  987. if (buffer_dirty(bh)) {
  988. smp_mb();
  989. if (buffer_dirty(bh))
  990. return;
  991. }
  992. if (!test_set_buffer_dirty(bh)) {
  993. struct page *page = bh->b_page;
  994. struct address_space *mapping = NULL;
  995. lock_page_memcg(page);
  996. if (!TestSetPageDirty(page)) {
  997. mapping = page_mapping(page);
  998. if (mapping)
  999. __set_page_dirty(page, mapping, 0);
  1000. }
  1001. unlock_page_memcg(page);
  1002. if (mapping)
  1003. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1004. }
  1005. }
  1006. EXPORT_SYMBOL(mark_buffer_dirty);
  1007. void mark_buffer_write_io_error(struct buffer_head *bh)
  1008. {
  1009. set_buffer_write_io_error(bh);
  1010. /* FIXME: do we need to set this in both places? */
  1011. if (bh->b_page && bh->b_page->mapping)
  1012. mapping_set_error(bh->b_page->mapping, -EIO);
  1013. if (bh->b_assoc_map)
  1014. mapping_set_error(bh->b_assoc_map, -EIO);
  1015. }
  1016. EXPORT_SYMBOL(mark_buffer_write_io_error);
  1017. /*
  1018. * Decrement a buffer_head's reference count. If all buffers against a page
  1019. * have zero reference count, are clean and unlocked, and if the page is clean
  1020. * and unlocked then try_to_free_buffers() may strip the buffers from the page
  1021. * in preparation for freeing it (sometimes, rarely, buffers are removed from
  1022. * a page but it ends up not being freed, and buffers may later be reattached).
  1023. */
  1024. void __brelse(struct buffer_head * buf)
  1025. {
  1026. if (atomic_read(&buf->b_count)) {
  1027. put_bh(buf);
  1028. return;
  1029. }
  1030. WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
  1031. }
  1032. EXPORT_SYMBOL(__brelse);
  1033. /*
  1034. * bforget() is like brelse(), except it discards any
  1035. * potentially dirty data.
  1036. */
  1037. void __bforget(struct buffer_head *bh)
  1038. {
  1039. clear_buffer_dirty(bh);
  1040. if (bh->b_assoc_map) {
  1041. struct address_space *buffer_mapping = bh->b_page->mapping;
  1042. spin_lock(&buffer_mapping->private_lock);
  1043. list_del_init(&bh->b_assoc_buffers);
  1044. bh->b_assoc_map = NULL;
  1045. spin_unlock(&buffer_mapping->private_lock);
  1046. }
  1047. __brelse(bh);
  1048. }
  1049. EXPORT_SYMBOL(__bforget);
  1050. static struct buffer_head *__bread_slow(struct buffer_head *bh)
  1051. {
  1052. lock_buffer(bh);
  1053. if (buffer_uptodate(bh)) {
  1054. unlock_buffer(bh);
  1055. return bh;
  1056. } else {
  1057. get_bh(bh);
  1058. bh->b_end_io = end_buffer_read_sync;
  1059. submit_bh(REQ_OP_READ, 0, bh);
  1060. wait_on_buffer(bh);
  1061. if (buffer_uptodate(bh))
  1062. return bh;
  1063. }
  1064. brelse(bh);
  1065. return NULL;
  1066. }
  1067. /*
  1068. * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
  1069. * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
  1070. * refcount elevated by one when they're in an LRU. A buffer can only appear
  1071. * once in a particular CPU's LRU. A single buffer can be present in multiple
  1072. * CPU's LRUs at the same time.
  1073. *
  1074. * This is a transparent caching front-end to sb_bread(), sb_getblk() and
  1075. * sb_find_get_block().
  1076. *
  1077. * The LRUs themselves only need locking against invalidate_bh_lrus. We use
  1078. * a local interrupt disable for that.
  1079. */
  1080. #define BH_LRU_SIZE 16
  1081. struct bh_lru {
  1082. struct buffer_head *bhs[BH_LRU_SIZE];
  1083. };
  1084. static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
  1085. #ifdef CONFIG_SMP
  1086. #define bh_lru_lock() local_irq_disable()
  1087. #define bh_lru_unlock() local_irq_enable()
  1088. #else
  1089. #define bh_lru_lock() preempt_disable()
  1090. #define bh_lru_unlock() preempt_enable()
  1091. #endif
  1092. static inline void check_irqs_on(void)
  1093. {
  1094. #ifdef irqs_disabled
  1095. BUG_ON(irqs_disabled());
  1096. #endif
  1097. }
  1098. /*
  1099. * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is
  1100. * inserted at the front, and the buffer_head at the back if any is evicted.
  1101. * Or, if already in the LRU it is moved to the front.
  1102. */
  1103. static void bh_lru_install(struct buffer_head *bh)
  1104. {
  1105. struct buffer_head *evictee = bh;
  1106. struct bh_lru *b;
  1107. int i;
  1108. check_irqs_on();
  1109. bh_lru_lock();
  1110. b = this_cpu_ptr(&bh_lrus);
  1111. for (i = 0; i < BH_LRU_SIZE; i++) {
  1112. swap(evictee, b->bhs[i]);
  1113. if (evictee == bh) {
  1114. bh_lru_unlock();
  1115. return;
  1116. }
  1117. }
  1118. get_bh(bh);
  1119. bh_lru_unlock();
  1120. brelse(evictee);
  1121. }
  1122. /*
  1123. * Look up the bh in this cpu's LRU. If it's there, move it to the head.
  1124. */
  1125. static struct buffer_head *
  1126. lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
  1127. {
  1128. struct buffer_head *ret = NULL;
  1129. unsigned int i;
  1130. check_irqs_on();
  1131. bh_lru_lock();
  1132. for (i = 0; i < BH_LRU_SIZE; i++) {
  1133. struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
  1134. if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
  1135. bh->b_size == size) {
  1136. if (i) {
  1137. while (i) {
  1138. __this_cpu_write(bh_lrus.bhs[i],
  1139. __this_cpu_read(bh_lrus.bhs[i - 1]));
  1140. i--;
  1141. }
  1142. __this_cpu_write(bh_lrus.bhs[0], bh);
  1143. }
  1144. get_bh(bh);
  1145. ret = bh;
  1146. break;
  1147. }
  1148. }
  1149. bh_lru_unlock();
  1150. return ret;
  1151. }
  1152. /*
  1153. * Perform a pagecache lookup for the matching buffer. If it's there, refresh
  1154. * it in the LRU and mark it as accessed. If it is not present then return
  1155. * NULL
  1156. */
  1157. struct buffer_head *
  1158. __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
  1159. {
  1160. struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
  1161. if (bh == NULL) {
  1162. /* __find_get_block_slow will mark the page accessed */
  1163. bh = __find_get_block_slow(bdev, block);
  1164. if (bh)
  1165. bh_lru_install(bh);
  1166. } else
  1167. touch_buffer(bh);
  1168. return bh;
  1169. }
  1170. EXPORT_SYMBOL(__find_get_block);
  1171. /*
  1172. * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
  1173. * which corresponds to the passed block_device, block and size. The
  1174. * returned buffer has its reference count incremented.
  1175. *
  1176. * __getblk_gfp() will lock up the machine if grow_dev_page's
  1177. * try_to_free_buffers() attempt is failing. FIXME, perhaps?
  1178. */
  1179. struct buffer_head *
  1180. __getblk_gfp(struct block_device *bdev, sector_t block,
  1181. unsigned size, gfp_t gfp)
  1182. {
  1183. struct buffer_head *bh = __find_get_block(bdev, block, size);
  1184. might_sleep();
  1185. if (bh == NULL)
  1186. bh = __getblk_slow(bdev, block, size, gfp);
  1187. return bh;
  1188. }
  1189. EXPORT_SYMBOL(__getblk_gfp);
  1190. /*
  1191. * Do async read-ahead on a buffer..
  1192. */
  1193. void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
  1194. {
  1195. struct buffer_head *bh = __getblk(bdev, block, size);
  1196. if (likely(bh)) {
  1197. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
  1198. brelse(bh);
  1199. }
  1200. }
  1201. EXPORT_SYMBOL(__breadahead);
  1202. void __breadahead_gfp(struct block_device *bdev, sector_t block, unsigned size,
  1203. gfp_t gfp)
  1204. {
  1205. struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
  1206. if (likely(bh)) {
  1207. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
  1208. brelse(bh);
  1209. }
  1210. }
  1211. EXPORT_SYMBOL(__breadahead_gfp);
  1212. /**
  1213. * __bread_gfp() - reads a specified block and returns the bh
  1214. * @bdev: the block_device to read from
  1215. * @block: number of block
  1216. * @size: size (in bytes) to read
  1217. * @gfp: page allocation flag
  1218. *
  1219. * Reads a specified block, and returns buffer head that contains it.
  1220. * The page cache can be allocated from non-movable area
  1221. * not to prevent page migration if you set gfp to zero.
  1222. * It returns NULL if the block was unreadable.
  1223. */
  1224. struct buffer_head *
  1225. __bread_gfp(struct block_device *bdev, sector_t block,
  1226. unsigned size, gfp_t gfp)
  1227. {
  1228. struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
  1229. if (likely(bh) && !buffer_uptodate(bh))
  1230. bh = __bread_slow(bh);
  1231. return bh;
  1232. }
  1233. EXPORT_SYMBOL(__bread_gfp);
  1234. /*
  1235. * invalidate_bh_lrus() is called rarely - but not only at unmount.
  1236. * This doesn't race because it runs in each cpu either in irq
  1237. * or with preempt disabled.
  1238. */
  1239. static void invalidate_bh_lru(void *arg)
  1240. {
  1241. struct bh_lru *b = &get_cpu_var(bh_lrus);
  1242. int i;
  1243. for (i = 0; i < BH_LRU_SIZE; i++) {
  1244. brelse(b->bhs[i]);
  1245. b->bhs[i] = NULL;
  1246. }
  1247. put_cpu_var(bh_lrus);
  1248. }
  1249. static bool has_bh_in_lru(int cpu, void *dummy)
  1250. {
  1251. struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
  1252. int i;
  1253. for (i = 0; i < BH_LRU_SIZE; i++) {
  1254. if (b->bhs[i])
  1255. return 1;
  1256. }
  1257. return 0;
  1258. }
  1259. void invalidate_bh_lrus(void)
  1260. {
  1261. on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
  1262. }
  1263. EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
  1264. void set_bh_page(struct buffer_head *bh,
  1265. struct page *page, unsigned long offset)
  1266. {
  1267. bh->b_page = page;
  1268. BUG_ON(offset >= PAGE_SIZE);
  1269. if (PageHighMem(page))
  1270. /*
  1271. * This catches illegal uses and preserves the offset:
  1272. */
  1273. bh->b_data = (char *)(0 + offset);
  1274. else
  1275. bh->b_data = page_address(page) + offset;
  1276. }
  1277. EXPORT_SYMBOL(set_bh_page);
  1278. /*
  1279. * Called when truncating a buffer on a page completely.
  1280. */
  1281. /* Bits that are cleared during an invalidate */
  1282. #define BUFFER_FLAGS_DISCARD \
  1283. (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
  1284. 1 << BH_Delay | 1 << BH_Unwritten)
  1285. static void discard_buffer(struct buffer_head * bh)
  1286. {
  1287. unsigned long b_state, b_state_old;
  1288. lock_buffer(bh);
  1289. clear_buffer_dirty(bh);
  1290. bh->b_bdev = NULL;
  1291. b_state = bh->b_state;
  1292. for (;;) {
  1293. b_state_old = cmpxchg(&bh->b_state, b_state,
  1294. (b_state & ~BUFFER_FLAGS_DISCARD));
  1295. if (b_state_old == b_state)
  1296. break;
  1297. b_state = b_state_old;
  1298. }
  1299. unlock_buffer(bh);
  1300. }
  1301. /**
  1302. * block_invalidatepage - invalidate part or all of a buffer-backed page
  1303. *
  1304. * @page: the page which is affected
  1305. * @offset: start of the range to invalidate
  1306. * @length: length of the range to invalidate
  1307. *
  1308. * block_invalidatepage() is called when all or part of the page has become
  1309. * invalidated by a truncate operation.
  1310. *
  1311. * block_invalidatepage() does not have to release all buffers, but it must
  1312. * ensure that no dirty buffer is left outside @offset and that no I/O
  1313. * is underway against any of the blocks which are outside the truncation
  1314. * point. Because the caller is about to free (and possibly reuse) those
  1315. * blocks on-disk.
  1316. */
  1317. void block_invalidatepage(struct page *page, unsigned int offset,
  1318. unsigned int length)
  1319. {
  1320. struct buffer_head *head, *bh, *next;
  1321. unsigned int curr_off = 0;
  1322. unsigned int stop = length + offset;
  1323. BUG_ON(!PageLocked(page));
  1324. if (!page_has_buffers(page))
  1325. goto out;
  1326. /*
  1327. * Check for overflow
  1328. */
  1329. BUG_ON(stop > PAGE_SIZE || stop < length);
  1330. head = page_buffers(page);
  1331. bh = head;
  1332. do {
  1333. unsigned int next_off = curr_off + bh->b_size;
  1334. next = bh->b_this_page;
  1335. /*
  1336. * Are we still fully in range ?
  1337. */
  1338. if (next_off > stop)
  1339. goto out;
  1340. /*
  1341. * is this block fully invalidated?
  1342. */
  1343. if (offset <= curr_off)
  1344. discard_buffer(bh);
  1345. curr_off = next_off;
  1346. bh = next;
  1347. } while (bh != head);
  1348. /*
  1349. * We release buffers only if the entire page is being invalidated.
  1350. * The get_block cached value has been unconditionally invalidated,
  1351. * so real IO is not possible anymore.
  1352. */
  1353. if (length == PAGE_SIZE)
  1354. try_to_release_page(page, 0);
  1355. out:
  1356. return;
  1357. }
  1358. EXPORT_SYMBOL(block_invalidatepage);
  1359. /*
  1360. * We attach and possibly dirty the buffers atomically wrt
  1361. * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
  1362. * is already excluded via the page lock.
  1363. */
  1364. void create_empty_buffers(struct page *page,
  1365. unsigned long blocksize, unsigned long b_state)
  1366. {
  1367. struct buffer_head *bh, *head, *tail;
  1368. head = alloc_page_buffers(page, blocksize, true);
  1369. bh = head;
  1370. do {
  1371. bh->b_state |= b_state;
  1372. tail = bh;
  1373. bh = bh->b_this_page;
  1374. } while (bh);
  1375. tail->b_this_page = head;
  1376. spin_lock(&page->mapping->private_lock);
  1377. if (PageUptodate(page) || PageDirty(page)) {
  1378. bh = head;
  1379. do {
  1380. if (PageDirty(page))
  1381. set_buffer_dirty(bh);
  1382. if (PageUptodate(page))
  1383. set_buffer_uptodate(bh);
  1384. bh = bh->b_this_page;
  1385. } while (bh != head);
  1386. }
  1387. attach_page_buffers(page, head);
  1388. spin_unlock(&page->mapping->private_lock);
  1389. }
  1390. EXPORT_SYMBOL(create_empty_buffers);
  1391. /**
  1392. * clean_bdev_aliases: clean a range of buffers in block device
  1393. * @bdev: Block device to clean buffers in
  1394. * @block: Start of a range of blocks to clean
  1395. * @len: Number of blocks to clean
  1396. *
  1397. * We are taking a range of blocks for data and we don't want writeback of any
  1398. * buffer-cache aliases starting from return from this function and until the
  1399. * moment when something will explicitly mark the buffer dirty (hopefully that
  1400. * will not happen until we will free that block ;-) We don't even need to mark
  1401. * it not-uptodate - nobody can expect anything from a newly allocated buffer
  1402. * anyway. We used to use unmap_buffer() for such invalidation, but that was
  1403. * wrong. We definitely don't want to mark the alias unmapped, for example - it
  1404. * would confuse anyone who might pick it with bread() afterwards...
  1405. *
  1406. * Also.. Note that bforget() doesn't lock the buffer. So there can be
  1407. * writeout I/O going on against recently-freed buffers. We don't wait on that
  1408. * I/O in bforget() - it's more efficient to wait on the I/O only if we really
  1409. * need to. That happens here.
  1410. */
  1411. void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
  1412. {
  1413. struct inode *bd_inode = bdev->bd_inode;
  1414. struct address_space *bd_mapping = bd_inode->i_mapping;
  1415. struct pagevec pvec;
  1416. pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1417. pgoff_t end;
  1418. int i, count;
  1419. struct buffer_head *bh;
  1420. struct buffer_head *head;
  1421. end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1422. pagevec_init(&pvec);
  1423. while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) {
  1424. count = pagevec_count(&pvec);
  1425. for (i = 0; i < count; i++) {
  1426. struct page *page = pvec.pages[i];
  1427. if (!page_has_buffers(page))
  1428. continue;
  1429. /*
  1430. * We use page lock instead of bd_mapping->private_lock
  1431. * to pin buffers here since we can afford to sleep and
  1432. * it scales better than a global spinlock lock.
  1433. */
  1434. lock_page(page);
  1435. /* Recheck when the page is locked which pins bhs */
  1436. if (!page_has_buffers(page))
  1437. goto unlock_page;
  1438. head = page_buffers(page);
  1439. bh = head;
  1440. do {
  1441. if (!buffer_mapped(bh) || (bh->b_blocknr < block))
  1442. goto next;
  1443. if (bh->b_blocknr >= block + len)
  1444. break;
  1445. clear_buffer_dirty(bh);
  1446. wait_on_buffer(bh);
  1447. clear_buffer_req(bh);
  1448. next:
  1449. bh = bh->b_this_page;
  1450. } while (bh != head);
  1451. unlock_page:
  1452. unlock_page(page);
  1453. }
  1454. pagevec_release(&pvec);
  1455. cond_resched();
  1456. /* End of range already reached? */
  1457. if (index > end || !index)
  1458. break;
  1459. }
  1460. }
  1461. EXPORT_SYMBOL(clean_bdev_aliases);
  1462. /*
  1463. * Size is a power-of-two in the range 512..PAGE_SIZE,
  1464. * and the case we care about most is PAGE_SIZE.
  1465. *
  1466. * So this *could* possibly be written with those
  1467. * constraints in mind (relevant mostly if some
  1468. * architecture has a slow bit-scan instruction)
  1469. */
  1470. static inline int block_size_bits(unsigned int blocksize)
  1471. {
  1472. return ilog2(blocksize);
  1473. }
  1474. static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
  1475. {
  1476. BUG_ON(!PageLocked(page));
  1477. if (!page_has_buffers(page))
  1478. create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits),
  1479. b_state);
  1480. return page_buffers(page);
  1481. }
  1482. /*
  1483. * NOTE! All mapped/uptodate combinations are valid:
  1484. *
  1485. * Mapped Uptodate Meaning
  1486. *
  1487. * No No "unknown" - must do get_block()
  1488. * No Yes "hole" - zero-filled
  1489. * Yes No "allocated" - allocated on disk, not read in
  1490. * Yes Yes "valid" - allocated and up-to-date in memory.
  1491. *
  1492. * "Dirty" is valid only with the last case (mapped+uptodate).
  1493. */
  1494. /*
  1495. * While block_write_full_page is writing back the dirty buffers under
  1496. * the page lock, whoever dirtied the buffers may decide to clean them
  1497. * again at any time. We handle that by only looking at the buffer
  1498. * state inside lock_buffer().
  1499. *
  1500. * If block_write_full_page() is called for regular writeback
  1501. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1502. * locked buffer. This only can happen if someone has written the buffer
  1503. * directly, with submit_bh(). At the address_space level PageWriteback
  1504. * prevents this contention from occurring.
  1505. *
  1506. * If block_write_full_page() is called with wbc->sync_mode ==
  1507. * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
  1508. * causes the writes to be flagged as synchronous writes.
  1509. */
  1510. int __block_write_full_page(struct inode *inode, struct page *page,
  1511. get_block_t *get_block, struct writeback_control *wbc,
  1512. bh_end_io_t *handler)
  1513. {
  1514. int err;
  1515. sector_t block;
  1516. sector_t last_block;
  1517. struct buffer_head *bh, *head;
  1518. unsigned int blocksize, bbits;
  1519. int nr_underway = 0;
  1520. int write_flags = wbc_to_write_flags(wbc);
  1521. head = create_page_buffers(page, inode,
  1522. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1523. /*
  1524. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1525. * here, and the (potentially unmapped) buffers may become dirty at
  1526. * any time. If a buffer becomes dirty here after we've inspected it
  1527. * then we just miss that fact, and the page stays dirty.
  1528. *
  1529. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1530. * handle that here by just cleaning them.
  1531. */
  1532. bh = head;
  1533. blocksize = bh->b_size;
  1534. bbits = block_size_bits(blocksize);
  1535. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1536. last_block = (i_size_read(inode) - 1) >> bbits;
  1537. /*
  1538. * Get all the dirty buffers mapped to disk addresses and
  1539. * handle any aliases from the underlying blockdev's mapping.
  1540. */
  1541. do {
  1542. if (block > last_block) {
  1543. /*
  1544. * mapped buffers outside i_size will occur, because
  1545. * this page can be outside i_size when there is a
  1546. * truncate in progress.
  1547. */
  1548. /*
  1549. * The buffer was zeroed by block_write_full_page()
  1550. */
  1551. clear_buffer_dirty(bh);
  1552. set_buffer_uptodate(bh);
  1553. } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
  1554. buffer_dirty(bh)) {
  1555. WARN_ON(bh->b_size != blocksize);
  1556. err = get_block(inode, block, bh, 1);
  1557. if (err)
  1558. goto recover;
  1559. clear_buffer_delay(bh);
  1560. if (buffer_new(bh)) {
  1561. /* blockdev mappings never come here */
  1562. clear_buffer_new(bh);
  1563. clean_bdev_bh_alias(bh);
  1564. }
  1565. }
  1566. bh = bh->b_this_page;
  1567. block++;
  1568. } while (bh != head);
  1569. do {
  1570. if (!buffer_mapped(bh))
  1571. continue;
  1572. /*
  1573. * If it's a fully non-blocking write attempt and we cannot
  1574. * lock the buffer then redirty the page. Note that this can
  1575. * potentially cause a busy-wait loop from writeback threads
  1576. * and kswapd activity, but those code paths have their own
  1577. * higher-level throttling.
  1578. */
  1579. if (wbc->sync_mode != WB_SYNC_NONE) {
  1580. lock_buffer(bh);
  1581. } else if (!trylock_buffer(bh)) {
  1582. redirty_page_for_writepage(wbc, page);
  1583. continue;
  1584. }
  1585. if (test_clear_buffer_dirty(bh)) {
  1586. mark_buffer_async_write_endio(bh, handler);
  1587. } else {
  1588. unlock_buffer(bh);
  1589. }
  1590. } while ((bh = bh->b_this_page) != head);
  1591. /*
  1592. * The page and its buffers are protected by PageWriteback(), so we can
  1593. * drop the bh refcounts early.
  1594. */
  1595. BUG_ON(PageWriteback(page));
  1596. set_page_writeback(page);
  1597. do {
  1598. struct buffer_head *next = bh->b_this_page;
  1599. if (buffer_async_write(bh)) {
  1600. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1601. inode->i_write_hint, wbc);
  1602. nr_underway++;
  1603. }
  1604. bh = next;
  1605. } while (bh != head);
  1606. unlock_page(page);
  1607. err = 0;
  1608. done:
  1609. if (nr_underway == 0) {
  1610. /*
  1611. * The page was marked dirty, but the buffers were
  1612. * clean. Someone wrote them back by hand with
  1613. * ll_rw_block/submit_bh. A rare case.
  1614. */
  1615. end_page_writeback(page);
  1616. /*
  1617. * The page and buffer_heads can be released at any time from
  1618. * here on.
  1619. */
  1620. }
  1621. return err;
  1622. recover:
  1623. /*
  1624. * ENOSPC, or some other error. We may already have added some
  1625. * blocks to the file, so we need to write these out to avoid
  1626. * exposing stale data.
  1627. * The page is currently locked and not marked for writeback
  1628. */
  1629. bh = head;
  1630. /* Recovery: lock and submit the mapped buffers */
  1631. do {
  1632. if (buffer_mapped(bh) && buffer_dirty(bh) &&
  1633. !buffer_delay(bh)) {
  1634. lock_buffer(bh);
  1635. mark_buffer_async_write_endio(bh, handler);
  1636. } else {
  1637. /*
  1638. * The buffer may have been set dirty during
  1639. * attachment to a dirty page.
  1640. */
  1641. clear_buffer_dirty(bh);
  1642. }
  1643. } while ((bh = bh->b_this_page) != head);
  1644. SetPageError(page);
  1645. BUG_ON(PageWriteback(page));
  1646. mapping_set_error(page->mapping, err);
  1647. set_page_writeback(page);
  1648. do {
  1649. struct buffer_head *next = bh->b_this_page;
  1650. if (buffer_async_write(bh)) {
  1651. clear_buffer_dirty(bh);
  1652. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1653. inode->i_write_hint, wbc);
  1654. nr_underway++;
  1655. }
  1656. bh = next;
  1657. } while (bh != head);
  1658. unlock_page(page);
  1659. goto done;
  1660. }
  1661. EXPORT_SYMBOL(__block_write_full_page);
  1662. /*
  1663. * If a page has any new buffers, zero them out here, and mark them uptodate
  1664. * and dirty so they'll be written out (in order to prevent uninitialised
  1665. * block data from leaking). And clear the new bit.
  1666. */
  1667. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1668. {
  1669. unsigned int block_start, block_end;
  1670. struct buffer_head *head, *bh;
  1671. BUG_ON(!PageLocked(page));
  1672. if (!page_has_buffers(page))
  1673. return;
  1674. bh = head = page_buffers(page);
  1675. block_start = 0;
  1676. do {
  1677. block_end = block_start + bh->b_size;
  1678. if (buffer_new(bh)) {
  1679. if (block_end > from && block_start < to) {
  1680. if (!PageUptodate(page)) {
  1681. unsigned start, size;
  1682. start = max(from, block_start);
  1683. size = min(to, block_end) - start;
  1684. zero_user(page, start, size);
  1685. set_buffer_uptodate(bh);
  1686. }
  1687. clear_buffer_new(bh);
  1688. mark_buffer_dirty(bh);
  1689. }
  1690. }
  1691. block_start = block_end;
  1692. bh = bh->b_this_page;
  1693. } while (bh != head);
  1694. }
  1695. EXPORT_SYMBOL(page_zero_new_buffers);
  1696. static void
  1697. iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
  1698. struct iomap *iomap)
  1699. {
  1700. loff_t offset = block << inode->i_blkbits;
  1701. bh->b_bdev = iomap->bdev;
  1702. /*
  1703. * Block points to offset in file we need to map, iomap contains
  1704. * the offset at which the map starts. If the map ends before the
  1705. * current block, then do not map the buffer and let the caller
  1706. * handle it.
  1707. */
  1708. BUG_ON(offset >= iomap->offset + iomap->length);
  1709. switch (iomap->type) {
  1710. case IOMAP_HOLE:
  1711. /*
  1712. * If the buffer is not up to date or beyond the current EOF,
  1713. * we need to mark it as new to ensure sub-block zeroing is
  1714. * executed if necessary.
  1715. */
  1716. if (!buffer_uptodate(bh) ||
  1717. (offset >= i_size_read(inode)))
  1718. set_buffer_new(bh);
  1719. break;
  1720. case IOMAP_DELALLOC:
  1721. if (!buffer_uptodate(bh) ||
  1722. (offset >= i_size_read(inode)))
  1723. set_buffer_new(bh);
  1724. set_buffer_uptodate(bh);
  1725. set_buffer_mapped(bh);
  1726. set_buffer_delay(bh);
  1727. break;
  1728. case IOMAP_UNWRITTEN:
  1729. /*
  1730. * For unwritten regions, we always need to ensure that regions
  1731. * in the block we are not writing to are zeroed. Mark the
  1732. * buffer as new to ensure this.
  1733. */
  1734. set_buffer_new(bh);
  1735. set_buffer_unwritten(bh);
  1736. /* FALLTHRU */
  1737. case IOMAP_MAPPED:
  1738. if ((iomap->flags & IOMAP_F_NEW) ||
  1739. offset >= i_size_read(inode))
  1740. set_buffer_new(bh);
  1741. bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
  1742. inode->i_blkbits;
  1743. set_buffer_mapped(bh);
  1744. break;
  1745. }
  1746. }
  1747. int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
  1748. get_block_t *get_block, struct iomap *iomap)
  1749. {
  1750. unsigned from = pos & (PAGE_SIZE - 1);
  1751. unsigned to = from + len;
  1752. struct inode *inode = page->mapping->host;
  1753. unsigned block_start, block_end;
  1754. sector_t block;
  1755. int err = 0;
  1756. unsigned blocksize, bbits;
  1757. struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
  1758. BUG_ON(!PageLocked(page));
  1759. BUG_ON(from > PAGE_SIZE);
  1760. BUG_ON(to > PAGE_SIZE);
  1761. BUG_ON(from > to);
  1762. head = create_page_buffers(page, inode, 0);
  1763. blocksize = head->b_size;
  1764. bbits = block_size_bits(blocksize);
  1765. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1766. for(bh = head, block_start = 0; bh != head || !block_start;
  1767. block++, block_start=block_end, bh = bh->b_this_page) {
  1768. block_end = block_start + blocksize;
  1769. if (block_end <= from || block_start >= to) {
  1770. if (PageUptodate(page)) {
  1771. if (!buffer_uptodate(bh))
  1772. set_buffer_uptodate(bh);
  1773. }
  1774. continue;
  1775. }
  1776. if (buffer_new(bh))
  1777. clear_buffer_new(bh);
  1778. if (!buffer_mapped(bh)) {
  1779. WARN_ON(bh->b_size != blocksize);
  1780. if (get_block) {
  1781. err = get_block(inode, block, bh, 1);
  1782. if (err)
  1783. break;
  1784. } else {
  1785. iomap_to_bh(inode, block, bh, iomap);
  1786. }
  1787. if (buffer_new(bh)) {
  1788. clean_bdev_bh_alias(bh);
  1789. if (PageUptodate(page)) {
  1790. clear_buffer_new(bh);
  1791. set_buffer_uptodate(bh);
  1792. mark_buffer_dirty(bh);
  1793. continue;
  1794. }
  1795. if (block_end > to || block_start < from)
  1796. zero_user_segments(page,
  1797. to, block_end,
  1798. block_start, from);
  1799. continue;
  1800. }
  1801. }
  1802. if (PageUptodate(page)) {
  1803. if (!buffer_uptodate(bh))
  1804. set_buffer_uptodate(bh);
  1805. continue;
  1806. }
  1807. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1808. !buffer_unwritten(bh) &&
  1809. (block_start < from || block_end > to)) {
  1810. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1811. *wait_bh++=bh;
  1812. }
  1813. }
  1814. /*
  1815. * If we issued read requests - let them complete.
  1816. */
  1817. while(wait_bh > wait) {
  1818. wait_on_buffer(*--wait_bh);
  1819. if (!buffer_uptodate(*wait_bh))
  1820. err = -EIO;
  1821. }
  1822. if (unlikely(err))
  1823. page_zero_new_buffers(page, from, to);
  1824. return err;
  1825. }
  1826. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  1827. get_block_t *get_block)
  1828. {
  1829. return __block_write_begin_int(page, pos, len, get_block, NULL);
  1830. }
  1831. EXPORT_SYMBOL(__block_write_begin);
  1832. static int __block_commit_write(struct inode *inode, struct page *page,
  1833. unsigned from, unsigned to)
  1834. {
  1835. unsigned block_start, block_end;
  1836. int partial = 0;
  1837. unsigned blocksize;
  1838. struct buffer_head *bh, *head;
  1839. bh = head = page_buffers(page);
  1840. blocksize = bh->b_size;
  1841. block_start = 0;
  1842. do {
  1843. block_end = block_start + blocksize;
  1844. if (block_end <= from || block_start >= to) {
  1845. if (!buffer_uptodate(bh))
  1846. partial = 1;
  1847. } else {
  1848. set_buffer_uptodate(bh);
  1849. mark_buffer_dirty(bh);
  1850. }
  1851. clear_buffer_new(bh);
  1852. block_start = block_end;
  1853. bh = bh->b_this_page;
  1854. } while (bh != head);
  1855. /*
  1856. * If this is a partial write which happened to make all buffers
  1857. * uptodate then we can optimize away a bogus readpage() for
  1858. * the next read(). Here we 'discover' whether the page went
  1859. * uptodate as a result of this (potentially partial) write.
  1860. */
  1861. if (!partial)
  1862. SetPageUptodate(page);
  1863. return 0;
  1864. }
  1865. /*
  1866. * block_write_begin takes care of the basic task of block allocation and
  1867. * bringing partial write blocks uptodate first.
  1868. *
  1869. * The filesystem needs to handle block truncation upon failure.
  1870. */
  1871. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  1872. unsigned flags, struct page **pagep, get_block_t *get_block)
  1873. {
  1874. pgoff_t index = pos >> PAGE_SHIFT;
  1875. struct page *page;
  1876. int status;
  1877. page = grab_cache_page_write_begin(mapping, index, flags);
  1878. if (!page)
  1879. return -ENOMEM;
  1880. status = __block_write_begin(page, pos, len, get_block);
  1881. if (unlikely(status)) {
  1882. unlock_page(page);
  1883. put_page(page);
  1884. page = NULL;
  1885. }
  1886. *pagep = page;
  1887. return status;
  1888. }
  1889. EXPORT_SYMBOL(block_write_begin);
  1890. int block_write_end(struct file *file, struct address_space *mapping,
  1891. loff_t pos, unsigned len, unsigned copied,
  1892. struct page *page, void *fsdata)
  1893. {
  1894. struct inode *inode = mapping->host;
  1895. unsigned start;
  1896. start = pos & (PAGE_SIZE - 1);
  1897. if (unlikely(copied < len)) {
  1898. /*
  1899. * The buffers that were written will now be uptodate, so we
  1900. * don't have to worry about a readpage reading them and
  1901. * overwriting a partial write. However if we have encountered
  1902. * a short write and only partially written into a buffer, it
  1903. * will not be marked uptodate, so a readpage might come in and
  1904. * destroy our partial write.
  1905. *
  1906. * Do the simplest thing, and just treat any short write to a
  1907. * non uptodate page as a zero-length write, and force the
  1908. * caller to redo the whole thing.
  1909. */
  1910. if (!PageUptodate(page))
  1911. copied = 0;
  1912. page_zero_new_buffers(page, start+copied, start+len);
  1913. }
  1914. flush_dcache_page(page);
  1915. /* This could be a short (even 0-length) commit */
  1916. __block_commit_write(inode, page, start, start+copied);
  1917. return copied;
  1918. }
  1919. EXPORT_SYMBOL(block_write_end);
  1920. int generic_write_end(struct file *file, struct address_space *mapping,
  1921. loff_t pos, unsigned len, unsigned copied,
  1922. struct page *page, void *fsdata)
  1923. {
  1924. struct inode *inode = mapping->host;
  1925. loff_t old_size = inode->i_size;
  1926. bool i_size_changed = false;
  1927. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1928. /*
  1929. * No need to use i_size_read() here, the i_size cannot change under us
  1930. * because we hold i_rwsem.
  1931. *
  1932. * But it's important to update i_size while still holding page lock:
  1933. * page writeout could otherwise come in and zero beyond i_size.
  1934. */
  1935. if (pos + copied > inode->i_size) {
  1936. i_size_write(inode, pos + copied);
  1937. i_size_changed = true;
  1938. }
  1939. unlock_page(page);
  1940. put_page(page);
  1941. if (old_size < pos)
  1942. pagecache_isize_extended(inode, old_size, pos);
  1943. /*
  1944. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1945. * makes the holding time of page lock longer. Second, it forces lock
  1946. * ordering of page lock and transaction start for journaling
  1947. * filesystems.
  1948. */
  1949. if (i_size_changed)
  1950. mark_inode_dirty(inode);
  1951. return copied;
  1952. }
  1953. EXPORT_SYMBOL(generic_write_end);
  1954. /*
  1955. * block_is_partially_uptodate checks whether buffers within a page are
  1956. * uptodate or not.
  1957. *
  1958. * Returns true if all buffers which correspond to a file portion
  1959. * we want to read are uptodate.
  1960. */
  1961. int block_is_partially_uptodate(struct page *page, unsigned long from,
  1962. unsigned long count)
  1963. {
  1964. unsigned block_start, block_end, blocksize;
  1965. unsigned to;
  1966. struct buffer_head *bh, *head;
  1967. int ret = 1;
  1968. if (!page_has_buffers(page))
  1969. return 0;
  1970. head = page_buffers(page);
  1971. blocksize = head->b_size;
  1972. to = min_t(unsigned, PAGE_SIZE - from, count);
  1973. to = from + to;
  1974. if (from < blocksize && to > PAGE_SIZE - blocksize)
  1975. return 0;
  1976. bh = head;
  1977. block_start = 0;
  1978. do {
  1979. block_end = block_start + blocksize;
  1980. if (block_end > from && block_start < to) {
  1981. if (!buffer_uptodate(bh)) {
  1982. ret = 0;
  1983. break;
  1984. }
  1985. if (block_end >= to)
  1986. break;
  1987. }
  1988. block_start = block_end;
  1989. bh = bh->b_this_page;
  1990. } while (bh != head);
  1991. return ret;
  1992. }
  1993. EXPORT_SYMBOL(block_is_partially_uptodate);
  1994. /*
  1995. * Generic "read page" function for block devices that have the normal
  1996. * get_block functionality. This is most of the block device filesystems.
  1997. * Reads the page asynchronously --- the unlock_buffer() and
  1998. * set/clear_buffer_uptodate() functions propagate buffer state into the
  1999. * page struct once IO has completed.
  2000. */
  2001. int block_read_full_page(struct page *page, get_block_t *get_block)
  2002. {
  2003. struct inode *inode = page->mapping->host;
  2004. sector_t iblock, lblock;
  2005. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  2006. unsigned int blocksize, bbits;
  2007. int nr, i;
  2008. int fully_mapped = 1;
  2009. head = create_page_buffers(page, inode, 0);
  2010. blocksize = head->b_size;
  2011. bbits = block_size_bits(blocksize);
  2012. iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
  2013. lblock = (i_size_read(inode)+blocksize-1) >> bbits;
  2014. bh = head;
  2015. nr = 0;
  2016. i = 0;
  2017. do {
  2018. if (buffer_uptodate(bh))
  2019. continue;
  2020. if (!buffer_mapped(bh)) {
  2021. int err = 0;
  2022. fully_mapped = 0;
  2023. if (iblock < lblock) {
  2024. WARN_ON(bh->b_size != blocksize);
  2025. err = get_block(inode, iblock, bh, 0);
  2026. if (err)
  2027. SetPageError(page);
  2028. }
  2029. if (!buffer_mapped(bh)) {
  2030. zero_user(page, i * blocksize, blocksize);
  2031. if (!err)
  2032. set_buffer_uptodate(bh);
  2033. continue;
  2034. }
  2035. /*
  2036. * get_block() might have updated the buffer
  2037. * synchronously
  2038. */
  2039. if (buffer_uptodate(bh))
  2040. continue;
  2041. }
  2042. arr[nr++] = bh;
  2043. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  2044. if (fully_mapped)
  2045. SetPageMappedToDisk(page);
  2046. if (!nr) {
  2047. /*
  2048. * All buffers are uptodate - we can set the page uptodate
  2049. * as well. But not if get_block() returned an error.
  2050. */
  2051. if (!PageError(page))
  2052. SetPageUptodate(page);
  2053. unlock_page(page);
  2054. return 0;
  2055. }
  2056. /* Stage two: lock the buffers */
  2057. for (i = 0; i < nr; i++) {
  2058. bh = arr[i];
  2059. lock_buffer(bh);
  2060. mark_buffer_async_read(bh);
  2061. }
  2062. /*
  2063. * Stage 3: start the IO. Check for uptodateness
  2064. * inside the buffer lock in case another process reading
  2065. * the underlying blockdev brought it uptodate (the sct fix).
  2066. */
  2067. for (i = 0; i < nr; i++) {
  2068. bh = arr[i];
  2069. if (buffer_uptodate(bh))
  2070. end_buffer_async_read(bh, 1);
  2071. else
  2072. submit_bh(REQ_OP_READ, 0, bh);
  2073. }
  2074. return 0;
  2075. }
  2076. EXPORT_SYMBOL(block_read_full_page);
  2077. /* utility function for filesystems that need to do work on expanding
  2078. * truncates. Uses filesystem pagecache writes to allow the filesystem to
  2079. * deal with the hole.
  2080. */
  2081. int generic_cont_expand_simple(struct inode *inode, loff_t size)
  2082. {
  2083. struct address_space *mapping = inode->i_mapping;
  2084. struct page *page;
  2085. void *fsdata;
  2086. int err;
  2087. err = inode_newsize_ok(inode, size);
  2088. if (err)
  2089. goto out;
  2090. err = pagecache_write_begin(NULL, mapping, size, 0,
  2091. AOP_FLAG_CONT_EXPAND, &page, &fsdata);
  2092. if (err)
  2093. goto out;
  2094. err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  2095. BUG_ON(err > 0);
  2096. out:
  2097. return err;
  2098. }
  2099. EXPORT_SYMBOL(generic_cont_expand_simple);
  2100. static int cont_expand_zero(struct file *file, struct address_space *mapping,
  2101. loff_t pos, loff_t *bytes)
  2102. {
  2103. struct inode *inode = mapping->host;
  2104. unsigned int blocksize = i_blocksize(inode);
  2105. struct page *page;
  2106. void *fsdata;
  2107. pgoff_t index, curidx;
  2108. loff_t curpos;
  2109. unsigned zerofrom, offset, len;
  2110. int err = 0;
  2111. index = pos >> PAGE_SHIFT;
  2112. offset = pos & ~PAGE_MASK;
  2113. while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
  2114. zerofrom = curpos & ~PAGE_MASK;
  2115. if (zerofrom & (blocksize-1)) {
  2116. *bytes |= (blocksize-1);
  2117. (*bytes)++;
  2118. }
  2119. len = PAGE_SIZE - zerofrom;
  2120. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2121. &page, &fsdata);
  2122. if (err)
  2123. goto out;
  2124. zero_user(page, zerofrom, len);
  2125. err = pagecache_write_end(file, mapping, curpos, len, len,
  2126. page, fsdata);
  2127. if (err < 0)
  2128. goto out;
  2129. BUG_ON(err != len);
  2130. err = 0;
  2131. balance_dirty_pages_ratelimited(mapping);
  2132. if (fatal_signal_pending(current)) {
  2133. err = -EINTR;
  2134. goto out;
  2135. }
  2136. }
  2137. /* page covers the boundary, find the boundary offset */
  2138. if (index == curidx) {
  2139. zerofrom = curpos & ~PAGE_MASK;
  2140. /* if we will expand the thing last block will be filled */
  2141. if (offset <= zerofrom) {
  2142. goto out;
  2143. }
  2144. if (zerofrom & (blocksize-1)) {
  2145. *bytes |= (blocksize-1);
  2146. (*bytes)++;
  2147. }
  2148. len = offset - zerofrom;
  2149. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2150. &page, &fsdata);
  2151. if (err)
  2152. goto out;
  2153. zero_user(page, zerofrom, len);
  2154. err = pagecache_write_end(file, mapping, curpos, len, len,
  2155. page, fsdata);
  2156. if (err < 0)
  2157. goto out;
  2158. BUG_ON(err != len);
  2159. err = 0;
  2160. }
  2161. out:
  2162. return err;
  2163. }
  2164. /*
  2165. * For moronic filesystems that do not allow holes in file.
  2166. * We may have to extend the file.
  2167. */
  2168. int cont_write_begin(struct file *file, struct address_space *mapping,
  2169. loff_t pos, unsigned len, unsigned flags,
  2170. struct page **pagep, void **fsdata,
  2171. get_block_t *get_block, loff_t *bytes)
  2172. {
  2173. struct inode *inode = mapping->host;
  2174. unsigned int blocksize = i_blocksize(inode);
  2175. unsigned int zerofrom;
  2176. int err;
  2177. err = cont_expand_zero(file, mapping, pos, bytes);
  2178. if (err)
  2179. return err;
  2180. zerofrom = *bytes & ~PAGE_MASK;
  2181. if (pos+len > *bytes && zerofrom & (blocksize-1)) {
  2182. *bytes |= (blocksize-1);
  2183. (*bytes)++;
  2184. }
  2185. return block_write_begin(mapping, pos, len, flags, pagep, get_block);
  2186. }
  2187. EXPORT_SYMBOL(cont_write_begin);
  2188. int block_commit_write(struct page *page, unsigned from, unsigned to)
  2189. {
  2190. struct inode *inode = page->mapping->host;
  2191. __block_commit_write(inode,page,from,to);
  2192. return 0;
  2193. }
  2194. EXPORT_SYMBOL(block_commit_write);
  2195. /*
  2196. * block_page_mkwrite() is not allowed to change the file size as it gets
  2197. * called from a page fault handler when a page is first dirtied. Hence we must
  2198. * be careful to check for EOF conditions here. We set the page up correctly
  2199. * for a written page which means we get ENOSPC checking when writing into
  2200. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2201. * support these features.
  2202. *
  2203. * We are not allowed to take the i_mutex here so we have to play games to
  2204. * protect against truncate races as the page could now be beyond EOF. Because
  2205. * truncate writes the inode size before removing pages, once we have the
  2206. * page lock we can determine safely if the page is beyond EOF. If it is not
  2207. * beyond EOF, then the page is guaranteed safe against truncation until we
  2208. * unlock the page.
  2209. *
  2210. * Direct callers of this function should protect against filesystem freezing
  2211. * using sb_start_pagefault() - sb_end_pagefault() functions.
  2212. */
  2213. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2214. get_block_t get_block)
  2215. {
  2216. struct page *page = vmf->page;
  2217. struct inode *inode = file_inode(vma->vm_file);
  2218. unsigned long end;
  2219. loff_t size;
  2220. int ret;
  2221. lock_page(page);
  2222. size = i_size_read(inode);
  2223. if ((page->mapping != inode->i_mapping) ||
  2224. (page_offset(page) > size)) {
  2225. /* We overload EFAULT to mean page got truncated */
  2226. ret = -EFAULT;
  2227. goto out_unlock;
  2228. }
  2229. /* page is wholly or partially inside EOF */
  2230. if (((page->index + 1) << PAGE_SHIFT) > size)
  2231. end = size & ~PAGE_MASK;
  2232. else
  2233. end = PAGE_SIZE;
  2234. ret = __block_write_begin(page, 0, end, get_block);
  2235. if (!ret)
  2236. ret = block_commit_write(page, 0, end);
  2237. if (unlikely(ret < 0))
  2238. goto out_unlock;
  2239. set_page_dirty(page);
  2240. wait_for_stable_page(page);
  2241. return 0;
  2242. out_unlock:
  2243. unlock_page(page);
  2244. return ret;
  2245. }
  2246. EXPORT_SYMBOL(block_page_mkwrite);
  2247. /*
  2248. * nobh_write_begin()'s prereads are special: the buffer_heads are freed
  2249. * immediately, while under the page lock. So it needs a special end_io
  2250. * handler which does not touch the bh after unlocking it.
  2251. */
  2252. static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
  2253. {
  2254. __end_buffer_read_notouch(bh, uptodate);
  2255. }
  2256. /*
  2257. * Attach the singly-linked list of buffers created by nobh_write_begin, to
  2258. * the page (converting it to circular linked list and taking care of page
  2259. * dirty races).
  2260. */
  2261. static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
  2262. {
  2263. struct buffer_head *bh;
  2264. BUG_ON(!PageLocked(page));
  2265. spin_lock(&page->mapping->private_lock);
  2266. bh = head;
  2267. do {
  2268. if (PageDirty(page))
  2269. set_buffer_dirty(bh);
  2270. if (!bh->b_this_page)
  2271. bh->b_this_page = head;
  2272. bh = bh->b_this_page;
  2273. } while (bh != head);
  2274. attach_page_buffers(page, head);
  2275. spin_unlock(&page->mapping->private_lock);
  2276. }
  2277. /*
  2278. * On entry, the page is fully not uptodate.
  2279. * On exit the page is fully uptodate in the areas outside (from,to)
  2280. * The filesystem needs to handle block truncation upon failure.
  2281. */
  2282. int nobh_write_begin(struct address_space *mapping,
  2283. loff_t pos, unsigned len, unsigned flags,
  2284. struct page **pagep, void **fsdata,
  2285. get_block_t *get_block)
  2286. {
  2287. struct inode *inode = mapping->host;
  2288. const unsigned blkbits = inode->i_blkbits;
  2289. const unsigned blocksize = 1 << blkbits;
  2290. struct buffer_head *head, *bh;
  2291. struct page *page;
  2292. pgoff_t index;
  2293. unsigned from, to;
  2294. unsigned block_in_page;
  2295. unsigned block_start, block_end;
  2296. sector_t block_in_file;
  2297. int nr_reads = 0;
  2298. int ret = 0;
  2299. int is_mapped_to_disk = 1;
  2300. index = pos >> PAGE_SHIFT;
  2301. from = pos & (PAGE_SIZE - 1);
  2302. to = from + len;
  2303. page = grab_cache_page_write_begin(mapping, index, flags);
  2304. if (!page)
  2305. return -ENOMEM;
  2306. *pagep = page;
  2307. *fsdata = NULL;
  2308. if (page_has_buffers(page)) {
  2309. ret = __block_write_begin(page, pos, len, get_block);
  2310. if (unlikely(ret))
  2311. goto out_release;
  2312. return ret;
  2313. }
  2314. if (PageMappedToDisk(page))
  2315. return 0;
  2316. /*
  2317. * Allocate buffers so that we can keep track of state, and potentially
  2318. * attach them to the page if an error occurs. In the common case of
  2319. * no error, they will just be freed again without ever being attached
  2320. * to the page (which is all OK, because we're under the page lock).
  2321. *
  2322. * Be careful: the buffer linked list is a NULL terminated one, rather
  2323. * than the circular one we're used to.
  2324. */
  2325. head = alloc_page_buffers(page, blocksize, false);
  2326. if (!head) {
  2327. ret = -ENOMEM;
  2328. goto out_release;
  2329. }
  2330. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  2331. /*
  2332. * We loop across all blocks in the page, whether or not they are
  2333. * part of the affected region. This is so we can discover if the
  2334. * page is fully mapped-to-disk.
  2335. */
  2336. for (block_start = 0, block_in_page = 0, bh = head;
  2337. block_start < PAGE_SIZE;
  2338. block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
  2339. int create;
  2340. block_end = block_start + blocksize;
  2341. bh->b_state = 0;
  2342. create = 1;
  2343. if (block_start >= to)
  2344. create = 0;
  2345. ret = get_block(inode, block_in_file + block_in_page,
  2346. bh, create);
  2347. if (ret)
  2348. goto failed;
  2349. if (!buffer_mapped(bh))
  2350. is_mapped_to_disk = 0;
  2351. if (buffer_new(bh))
  2352. clean_bdev_bh_alias(bh);
  2353. if (PageUptodate(page)) {
  2354. set_buffer_uptodate(bh);
  2355. continue;
  2356. }
  2357. if (buffer_new(bh) || !buffer_mapped(bh)) {
  2358. zero_user_segments(page, block_start, from,
  2359. to, block_end);
  2360. continue;
  2361. }
  2362. if (buffer_uptodate(bh))
  2363. continue; /* reiserfs does this */
  2364. if (block_start < from || block_end > to) {
  2365. lock_buffer(bh);
  2366. bh->b_end_io = end_buffer_read_nobh;
  2367. submit_bh(REQ_OP_READ, 0, bh);
  2368. nr_reads++;
  2369. }
  2370. }
  2371. if (nr_reads) {
  2372. /*
  2373. * The page is locked, so these buffers are protected from
  2374. * any VM or truncate activity. Hence we don't need to care
  2375. * for the buffer_head refcounts.
  2376. */
  2377. for (bh = head; bh; bh = bh->b_this_page) {
  2378. wait_on_buffer(bh);
  2379. if (!buffer_uptodate(bh))
  2380. ret = -EIO;
  2381. }
  2382. if (ret)
  2383. goto failed;
  2384. }
  2385. if (is_mapped_to_disk)
  2386. SetPageMappedToDisk(page);
  2387. *fsdata = head; /* to be released by nobh_write_end */
  2388. return 0;
  2389. failed:
  2390. BUG_ON(!ret);
  2391. /*
  2392. * Error recovery is a bit difficult. We need to zero out blocks that
  2393. * were newly allocated, and dirty them to ensure they get written out.
  2394. * Buffers need to be attached to the page at this point, otherwise
  2395. * the handling of potential IO errors during writeout would be hard
  2396. * (could try doing synchronous writeout, but what if that fails too?)
  2397. */
  2398. attach_nobh_buffers(page, head);
  2399. page_zero_new_buffers(page, from, to);
  2400. out_release:
  2401. unlock_page(page);
  2402. put_page(page);
  2403. *pagep = NULL;
  2404. return ret;
  2405. }
  2406. EXPORT_SYMBOL(nobh_write_begin);
  2407. int nobh_write_end(struct file *file, struct address_space *mapping,
  2408. loff_t pos, unsigned len, unsigned copied,
  2409. struct page *page, void *fsdata)
  2410. {
  2411. struct inode *inode = page->mapping->host;
  2412. struct buffer_head *head = fsdata;
  2413. struct buffer_head *bh;
  2414. BUG_ON(fsdata != NULL && page_has_buffers(page));
  2415. if (unlikely(copied < len) && head)
  2416. attach_nobh_buffers(page, head);
  2417. if (page_has_buffers(page))
  2418. return generic_write_end(file, mapping, pos, len,
  2419. copied, page, fsdata);
  2420. SetPageUptodate(page);
  2421. set_page_dirty(page);
  2422. if (pos+copied > inode->i_size) {
  2423. i_size_write(inode, pos+copied);
  2424. mark_inode_dirty(inode);
  2425. }
  2426. unlock_page(page);
  2427. put_page(page);
  2428. while (head) {
  2429. bh = head;
  2430. head = head->b_this_page;
  2431. free_buffer_head(bh);
  2432. }
  2433. return copied;
  2434. }
  2435. EXPORT_SYMBOL(nobh_write_end);
  2436. /*
  2437. * nobh_writepage() - based on block_full_write_page() except
  2438. * that it tries to operate without attaching bufferheads to
  2439. * the page.
  2440. */
  2441. int nobh_writepage(struct page *page, get_block_t *get_block,
  2442. struct writeback_control *wbc)
  2443. {
  2444. struct inode * const inode = page->mapping->host;
  2445. loff_t i_size = i_size_read(inode);
  2446. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2447. unsigned offset;
  2448. int ret;
  2449. /* Is the page fully inside i_size? */
  2450. if (page->index < end_index)
  2451. goto out;
  2452. /* Is the page fully outside i_size? (truncate in progress) */
  2453. offset = i_size & (PAGE_SIZE-1);
  2454. if (page->index >= end_index+1 || !offset) {
  2455. unlock_page(page);
  2456. return 0; /* don't care */
  2457. }
  2458. /*
  2459. * The page straddles i_size. It must be zeroed out on each and every
  2460. * writepage invocation because it may be mmapped. "A file is mapped
  2461. * in multiples of the page size. For a file that is not a multiple of
  2462. * the page size, the remaining memory is zeroed when mapped, and
  2463. * writes to that region are not written out to the file."
  2464. */
  2465. zero_user_segment(page, offset, PAGE_SIZE);
  2466. out:
  2467. ret = mpage_writepage(page, get_block, wbc);
  2468. if (ret == -EAGAIN)
  2469. ret = __block_write_full_page(inode, page, get_block, wbc,
  2470. end_buffer_async_write);
  2471. return ret;
  2472. }
  2473. EXPORT_SYMBOL(nobh_writepage);
  2474. int nobh_truncate_page(struct address_space *mapping,
  2475. loff_t from, get_block_t *get_block)
  2476. {
  2477. pgoff_t index = from >> PAGE_SHIFT;
  2478. unsigned offset = from & (PAGE_SIZE-1);
  2479. unsigned blocksize;
  2480. sector_t iblock;
  2481. unsigned length, pos;
  2482. struct inode *inode = mapping->host;
  2483. struct page *page;
  2484. struct buffer_head map_bh;
  2485. int err;
  2486. blocksize = i_blocksize(inode);
  2487. length = offset & (blocksize - 1);
  2488. /* Block boundary? Nothing to do */
  2489. if (!length)
  2490. return 0;
  2491. length = blocksize - length;
  2492. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2493. page = grab_cache_page(mapping, index);
  2494. err = -ENOMEM;
  2495. if (!page)
  2496. goto out;
  2497. if (page_has_buffers(page)) {
  2498. has_buffers:
  2499. unlock_page(page);
  2500. put_page(page);
  2501. return block_truncate_page(mapping, from, get_block);
  2502. }
  2503. /* Find the buffer that contains "offset" */
  2504. pos = blocksize;
  2505. while (offset >= pos) {
  2506. iblock++;
  2507. pos += blocksize;
  2508. }
  2509. map_bh.b_size = blocksize;
  2510. map_bh.b_state = 0;
  2511. err = get_block(inode, iblock, &map_bh, 0);
  2512. if (err)
  2513. goto unlock;
  2514. /* unmapped? It's a hole - nothing to do */
  2515. if (!buffer_mapped(&map_bh))
  2516. goto unlock;
  2517. /* Ok, it's mapped. Make sure it's up-to-date */
  2518. if (!PageUptodate(page)) {
  2519. err = mapping->a_ops->readpage(NULL, page);
  2520. if (err) {
  2521. put_page(page);
  2522. goto out;
  2523. }
  2524. lock_page(page);
  2525. if (!PageUptodate(page)) {
  2526. err = -EIO;
  2527. goto unlock;
  2528. }
  2529. if (page_has_buffers(page))
  2530. goto has_buffers;
  2531. }
  2532. zero_user(page, offset, length);
  2533. set_page_dirty(page);
  2534. err = 0;
  2535. unlock:
  2536. unlock_page(page);
  2537. put_page(page);
  2538. out:
  2539. return err;
  2540. }
  2541. EXPORT_SYMBOL(nobh_truncate_page);
  2542. int block_truncate_page(struct address_space *mapping,
  2543. loff_t from, get_block_t *get_block)
  2544. {
  2545. pgoff_t index = from >> PAGE_SHIFT;
  2546. unsigned offset = from & (PAGE_SIZE-1);
  2547. unsigned blocksize;
  2548. sector_t iblock;
  2549. unsigned length, pos;
  2550. struct inode *inode = mapping->host;
  2551. struct page *page;
  2552. struct buffer_head *bh;
  2553. int err;
  2554. blocksize = i_blocksize(inode);
  2555. length = offset & (blocksize - 1);
  2556. /* Block boundary? Nothing to do */
  2557. if (!length)
  2558. return 0;
  2559. length = blocksize - length;
  2560. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2561. page = grab_cache_page(mapping, index);
  2562. err = -ENOMEM;
  2563. if (!page)
  2564. goto out;
  2565. if (!page_has_buffers(page))
  2566. create_empty_buffers(page, blocksize, 0);
  2567. /* Find the buffer that contains "offset" */
  2568. bh = page_buffers(page);
  2569. pos = blocksize;
  2570. while (offset >= pos) {
  2571. bh = bh->b_this_page;
  2572. iblock++;
  2573. pos += blocksize;
  2574. }
  2575. err = 0;
  2576. if (!buffer_mapped(bh)) {
  2577. WARN_ON(bh->b_size != blocksize);
  2578. err = get_block(inode, iblock, bh, 0);
  2579. if (err)
  2580. goto unlock;
  2581. /* unmapped? It's a hole - nothing to do */
  2582. if (!buffer_mapped(bh))
  2583. goto unlock;
  2584. }
  2585. /* Ok, it's mapped. Make sure it's up-to-date */
  2586. if (PageUptodate(page))
  2587. set_buffer_uptodate(bh);
  2588. if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
  2589. err = -EIO;
  2590. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  2591. wait_on_buffer(bh);
  2592. /* Uhhuh. Read error. Complain and punt. */
  2593. if (!buffer_uptodate(bh))
  2594. goto unlock;
  2595. }
  2596. zero_user(page, offset, length);
  2597. mark_buffer_dirty(bh);
  2598. err = 0;
  2599. unlock:
  2600. unlock_page(page);
  2601. put_page(page);
  2602. out:
  2603. return err;
  2604. }
  2605. EXPORT_SYMBOL(block_truncate_page);
  2606. /*
  2607. * The generic ->writepage function for buffer-backed address_spaces
  2608. */
  2609. int block_write_full_page(struct page *page, get_block_t *get_block,
  2610. struct writeback_control *wbc)
  2611. {
  2612. struct inode * const inode = page->mapping->host;
  2613. loff_t i_size = i_size_read(inode);
  2614. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2615. unsigned offset;
  2616. /* Is the page fully inside i_size? */
  2617. if (page->index < end_index)
  2618. return __block_write_full_page(inode, page, get_block, wbc,
  2619. end_buffer_async_write);
  2620. /* Is the page fully outside i_size? (truncate in progress) */
  2621. offset = i_size & (PAGE_SIZE-1);
  2622. if (page->index >= end_index+1 || !offset) {
  2623. unlock_page(page);
  2624. return 0; /* don't care */
  2625. }
  2626. /*
  2627. * The page straddles i_size. It must be zeroed out on each and every
  2628. * writepage invocation because it may be mmapped. "A file is mapped
  2629. * in multiples of the page size. For a file that is not a multiple of
  2630. * the page size, the remaining memory is zeroed when mapped, and
  2631. * writes to that region are not written out to the file."
  2632. */
  2633. zero_user_segment(page, offset, PAGE_SIZE);
  2634. return __block_write_full_page(inode, page, get_block, wbc,
  2635. end_buffer_async_write);
  2636. }
  2637. EXPORT_SYMBOL(block_write_full_page);
  2638. sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
  2639. get_block_t *get_block)
  2640. {
  2641. struct inode *inode = mapping->host;
  2642. struct buffer_head tmp = {
  2643. .b_size = i_blocksize(inode),
  2644. };
  2645. get_block(inode, block, &tmp, 0);
  2646. return tmp.b_blocknr;
  2647. }
  2648. EXPORT_SYMBOL(generic_block_bmap);
  2649. static void end_bio_bh_io_sync(struct bio *bio)
  2650. {
  2651. struct buffer_head *bh = bio->bi_private;
  2652. if (unlikely(bio_flagged(bio, BIO_QUIET)))
  2653. set_bit(BH_Quiet, &bh->b_state);
  2654. bh->b_end_io(bh, !bio->bi_status);
  2655. bio_put(bio);
  2656. }
  2657. /*
  2658. * This allows us to do IO even on the odd last sectors
  2659. * of a device, even if the block size is some multiple
  2660. * of the physical sector size.
  2661. *
  2662. * We'll just truncate the bio to the size of the device,
  2663. * and clear the end of the buffer head manually.
  2664. *
  2665. * Truly out-of-range accesses will turn into actual IO
  2666. * errors, this only handles the "we need to be able to
  2667. * do IO at the final sector" case.
  2668. */
  2669. void guard_bio_eod(struct bio *bio)
  2670. {
  2671. sector_t maxsector;
  2672. struct hd_struct *part;
  2673. rcu_read_lock();
  2674. part = __disk_get_part(bio->bi_disk, bio->bi_partno);
  2675. if (part)
  2676. maxsector = part_nr_sects_read(part);
  2677. else
  2678. maxsector = get_capacity(bio->bi_disk);
  2679. rcu_read_unlock();
  2680. if (!maxsector)
  2681. return;
  2682. /*
  2683. * If the *whole* IO is past the end of the device,
  2684. * let it through, and the IO layer will turn it into
  2685. * an EIO.
  2686. */
  2687. if (unlikely(bio->bi_iter.bi_sector >= maxsector))
  2688. return;
  2689. maxsector -= bio->bi_iter.bi_sector;
  2690. if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
  2691. return;
  2692. bio_truncate(bio, maxsector << 9);
  2693. }
  2694. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  2695. enum rw_hint write_hint, struct writeback_control *wbc)
  2696. {
  2697. struct bio *bio;
  2698. BUG_ON(!buffer_locked(bh));
  2699. BUG_ON(!buffer_mapped(bh));
  2700. BUG_ON(!bh->b_end_io);
  2701. BUG_ON(buffer_delay(bh));
  2702. BUG_ON(buffer_unwritten(bh));
  2703. /*
  2704. * Only clear out a write error when rewriting
  2705. */
  2706. if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
  2707. clear_buffer_write_io_error(bh);
  2708. /*
  2709. * from here on down, it's all bio -- do the initial mapping,
  2710. * submit_bio -> generic_make_request may further map this bio around
  2711. */
  2712. bio = bio_alloc(GFP_NOIO, 1);
  2713. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  2714. bio_set_dev(bio, bh->b_bdev);
  2715. bio->bi_write_hint = write_hint;
  2716. bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  2717. BUG_ON(bio->bi_iter.bi_size != bh->b_size);
  2718. bio->bi_end_io = end_bio_bh_io_sync;
  2719. bio->bi_private = bh;
  2720. if (buffer_meta(bh))
  2721. op_flags |= REQ_META;
  2722. if (buffer_prio(bh))
  2723. op_flags |= REQ_PRIO;
  2724. bio_set_op_attrs(bio, op, op_flags);
  2725. /* Take care of bh's that straddle the end of the device */
  2726. guard_bio_eod(bio);
  2727. if (wbc) {
  2728. wbc_init_bio(wbc, bio);
  2729. wbc_account_cgroup_owner(wbc, bh->b_page, bh->b_size);
  2730. }
  2731. submit_bio(bio);
  2732. return 0;
  2733. }
  2734. int submit_bh(int op, int op_flags, struct buffer_head *bh)
  2735. {
  2736. return submit_bh_wbc(op, op_flags, bh, 0, NULL);
  2737. }
  2738. EXPORT_SYMBOL(submit_bh);
  2739. /**
  2740. * ll_rw_block: low-level access to block devices (DEPRECATED)
  2741. * @op: whether to %READ or %WRITE
  2742. * @op_flags: req_flag_bits
  2743. * @nr: number of &struct buffer_heads in the array
  2744. * @bhs: array of pointers to &struct buffer_head
  2745. *
  2746. * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
  2747. * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
  2748. * @op_flags contains flags modifying the detailed I/O behavior, most notably
  2749. * %REQ_RAHEAD.
  2750. *
  2751. * This function drops any buffer that it cannot get a lock on (with the
  2752. * BH_Lock state bit), any buffer that appears to be clean when doing a write
  2753. * request, and any buffer that appears to be up-to-date when doing read
  2754. * request. Further it marks as clean buffers that are processed for
  2755. * writing (the buffer cache won't assume that they are actually clean
  2756. * until the buffer gets unlocked).
  2757. *
  2758. * ll_rw_block sets b_end_io to simple completion handler that marks
  2759. * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
  2760. * any waiters.
  2761. *
  2762. * All of the buffers must be for the same device, and must also be a
  2763. * multiple of the current approved size for the device.
  2764. */
  2765. void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
  2766. {
  2767. int i;
  2768. for (i = 0; i < nr; i++) {
  2769. struct buffer_head *bh = bhs[i];
  2770. if (!trylock_buffer(bh))
  2771. continue;
  2772. if (op == WRITE) {
  2773. if (test_clear_buffer_dirty(bh)) {
  2774. bh->b_end_io = end_buffer_write_sync;
  2775. get_bh(bh);
  2776. submit_bh(op, op_flags, bh);
  2777. continue;
  2778. }
  2779. } else {
  2780. if (!buffer_uptodate(bh)) {
  2781. bh->b_end_io = end_buffer_read_sync;
  2782. get_bh(bh);
  2783. submit_bh(op, op_flags, bh);
  2784. continue;
  2785. }
  2786. }
  2787. unlock_buffer(bh);
  2788. }
  2789. }
  2790. EXPORT_SYMBOL(ll_rw_block);
  2791. void write_dirty_buffer(struct buffer_head *bh, int op_flags)
  2792. {
  2793. lock_buffer(bh);
  2794. if (!test_clear_buffer_dirty(bh)) {
  2795. unlock_buffer(bh);
  2796. return;
  2797. }
  2798. bh->b_end_io = end_buffer_write_sync;
  2799. get_bh(bh);
  2800. submit_bh(REQ_OP_WRITE, op_flags, bh);
  2801. }
  2802. EXPORT_SYMBOL(write_dirty_buffer);
  2803. /*
  2804. * For a data-integrity writeout, we need to wait upon any in-progress I/O
  2805. * and then start new I/O and then wait upon it. The caller must have a ref on
  2806. * the buffer_head.
  2807. */
  2808. int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
  2809. {
  2810. int ret = 0;
  2811. WARN_ON(atomic_read(&bh->b_count) < 1);
  2812. lock_buffer(bh);
  2813. if (test_clear_buffer_dirty(bh)) {
  2814. /*
  2815. * The bh should be mapped, but it might not be if the
  2816. * device was hot-removed. Not much we can do but fail the I/O.
  2817. */
  2818. if (!buffer_mapped(bh)) {
  2819. unlock_buffer(bh);
  2820. return -EIO;
  2821. }
  2822. get_bh(bh);
  2823. bh->b_end_io = end_buffer_write_sync;
  2824. ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
  2825. wait_on_buffer(bh);
  2826. if (!ret && !buffer_uptodate(bh))
  2827. ret = -EIO;
  2828. } else {
  2829. unlock_buffer(bh);
  2830. }
  2831. return ret;
  2832. }
  2833. EXPORT_SYMBOL(__sync_dirty_buffer);
  2834. int sync_dirty_buffer(struct buffer_head *bh)
  2835. {
  2836. return __sync_dirty_buffer(bh, REQ_SYNC);
  2837. }
  2838. EXPORT_SYMBOL(sync_dirty_buffer);
  2839. /*
  2840. * try_to_free_buffers() checks if all the buffers on this particular page
  2841. * are unused, and releases them if so.
  2842. *
  2843. * Exclusion against try_to_free_buffers may be obtained by either
  2844. * locking the page or by holding its mapping's private_lock.
  2845. *
  2846. * If the page is dirty but all the buffers are clean then we need to
  2847. * be sure to mark the page clean as well. This is because the page
  2848. * may be against a block device, and a later reattachment of buffers
  2849. * to a dirty page will set *all* buffers dirty. Which would corrupt
  2850. * filesystem data on the same device.
  2851. *
  2852. * The same applies to regular filesystem pages: if all the buffers are
  2853. * clean then we set the page clean and proceed. To do that, we require
  2854. * total exclusion from __set_page_dirty_buffers(). That is obtained with
  2855. * private_lock.
  2856. *
  2857. * try_to_free_buffers() is non-blocking.
  2858. */
  2859. static inline int buffer_busy(struct buffer_head *bh)
  2860. {
  2861. return atomic_read(&bh->b_count) |
  2862. (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
  2863. }
  2864. static int
  2865. drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
  2866. {
  2867. struct buffer_head *head = page_buffers(page);
  2868. struct buffer_head *bh;
  2869. bh = head;
  2870. do {
  2871. if (buffer_busy(bh))
  2872. goto failed;
  2873. bh = bh->b_this_page;
  2874. } while (bh != head);
  2875. do {
  2876. struct buffer_head *next = bh->b_this_page;
  2877. if (bh->b_assoc_map)
  2878. __remove_assoc_queue(bh);
  2879. bh = next;
  2880. } while (bh != head);
  2881. *buffers_to_free = head;
  2882. __clear_page_buffers(page);
  2883. return 1;
  2884. failed:
  2885. return 0;
  2886. }
  2887. int try_to_free_buffers(struct page *page)
  2888. {
  2889. struct address_space * const mapping = page->mapping;
  2890. struct buffer_head *buffers_to_free = NULL;
  2891. int ret = 0;
  2892. BUG_ON(!PageLocked(page));
  2893. if (PageWriteback(page))
  2894. return 0;
  2895. if (mapping == NULL) { /* can this still happen? */
  2896. ret = drop_buffers(page, &buffers_to_free);
  2897. goto out;
  2898. }
  2899. spin_lock(&mapping->private_lock);
  2900. ret = drop_buffers(page, &buffers_to_free);
  2901. /*
  2902. * If the filesystem writes its buffers by hand (eg ext3)
  2903. * then we can have clean buffers against a dirty page. We
  2904. * clean the page here; otherwise the VM will never notice
  2905. * that the filesystem did any IO at all.
  2906. *
  2907. * Also, during truncate, discard_buffer will have marked all
  2908. * the page's buffers clean. We discover that here and clean
  2909. * the page also.
  2910. *
  2911. * private_lock must be held over this entire operation in order
  2912. * to synchronise against __set_page_dirty_buffers and prevent the
  2913. * dirty bit from being lost.
  2914. */
  2915. if (ret)
  2916. cancel_dirty_page(page);
  2917. spin_unlock(&mapping->private_lock);
  2918. out:
  2919. if (buffers_to_free) {
  2920. struct buffer_head *bh = buffers_to_free;
  2921. do {
  2922. struct buffer_head *next = bh->b_this_page;
  2923. free_buffer_head(bh);
  2924. bh = next;
  2925. } while (bh != buffers_to_free);
  2926. }
  2927. return ret;
  2928. }
  2929. EXPORT_SYMBOL(try_to_free_buffers);
  2930. /*
  2931. * There are no bdflush tunables left. But distributions are
  2932. * still running obsolete flush daemons, so we terminate them here.
  2933. *
  2934. * Use of bdflush() is deprecated and will be removed in a future kernel.
  2935. * The `flush-X' kernel threads fully replace bdflush daemons and this call.
  2936. */
  2937. SYSCALL_DEFINE2(bdflush, int, func, long, data)
  2938. {
  2939. static int msg_count;
  2940. if (!capable(CAP_SYS_ADMIN))
  2941. return -EPERM;
  2942. if (msg_count < 5) {
  2943. msg_count++;
  2944. printk(KERN_INFO
  2945. "warning: process `%s' used the obsolete bdflush"
  2946. " system call\n", current->comm);
  2947. printk(KERN_INFO "Fix your initscripts?\n");
  2948. }
  2949. if (func == 1)
  2950. do_exit(0);
  2951. return 0;
  2952. }
  2953. /*
  2954. * Buffer-head allocation
  2955. */
  2956. static struct kmem_cache *bh_cachep __read_mostly;
  2957. /*
  2958. * Once the number of bh's in the machine exceeds this level, we start
  2959. * stripping them in writeback.
  2960. */
  2961. static unsigned long max_buffer_heads;
  2962. int buffer_heads_over_limit;
  2963. struct bh_accounting {
  2964. int nr; /* Number of live bh's */
  2965. int ratelimit; /* Limit cacheline bouncing */
  2966. };
  2967. static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
  2968. static void recalc_bh_state(void)
  2969. {
  2970. int i;
  2971. int tot = 0;
  2972. if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
  2973. return;
  2974. __this_cpu_write(bh_accounting.ratelimit, 0);
  2975. for_each_online_cpu(i)
  2976. tot += per_cpu(bh_accounting, i).nr;
  2977. buffer_heads_over_limit = (tot > max_buffer_heads);
  2978. }
  2979. struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  2980. {
  2981. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  2982. if (ret) {
  2983. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  2984. preempt_disable();
  2985. __this_cpu_inc(bh_accounting.nr);
  2986. recalc_bh_state();
  2987. preempt_enable();
  2988. }
  2989. return ret;
  2990. }
  2991. EXPORT_SYMBOL(alloc_buffer_head);
  2992. void free_buffer_head(struct buffer_head *bh)
  2993. {
  2994. BUG_ON(!list_empty(&bh->b_assoc_buffers));
  2995. kmem_cache_free(bh_cachep, bh);
  2996. preempt_disable();
  2997. __this_cpu_dec(bh_accounting.nr);
  2998. recalc_bh_state();
  2999. preempt_enable();
  3000. }
  3001. EXPORT_SYMBOL(free_buffer_head);
  3002. static int buffer_exit_cpu_dead(unsigned int cpu)
  3003. {
  3004. int i;
  3005. struct bh_lru *b = &per_cpu(bh_lrus, cpu);
  3006. for (i = 0; i < BH_LRU_SIZE; i++) {
  3007. brelse(b->bhs[i]);
  3008. b->bhs[i] = NULL;
  3009. }
  3010. this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
  3011. per_cpu(bh_accounting, cpu).nr = 0;
  3012. return 0;
  3013. }
  3014. /**
  3015. * bh_uptodate_or_lock - Test whether the buffer is uptodate
  3016. * @bh: struct buffer_head
  3017. *
  3018. * Return true if the buffer is up-to-date and false,
  3019. * with the buffer locked, if not.
  3020. */
  3021. int bh_uptodate_or_lock(struct buffer_head *bh)
  3022. {
  3023. if (!buffer_uptodate(bh)) {
  3024. lock_buffer(bh);
  3025. if (!buffer_uptodate(bh))
  3026. return 0;
  3027. unlock_buffer(bh);
  3028. }
  3029. return 1;
  3030. }
  3031. EXPORT_SYMBOL(bh_uptodate_or_lock);
  3032. /**
  3033. * bh_submit_read - Submit a locked buffer for reading
  3034. * @bh: struct buffer_head
  3035. *
  3036. * Returns zero on success and -EIO on error.
  3037. */
  3038. int bh_submit_read(struct buffer_head *bh)
  3039. {
  3040. BUG_ON(!buffer_locked(bh));
  3041. if (buffer_uptodate(bh)) {
  3042. unlock_buffer(bh);
  3043. return 0;
  3044. }
  3045. get_bh(bh);
  3046. bh->b_end_io = end_buffer_read_sync;
  3047. submit_bh(REQ_OP_READ, 0, bh);
  3048. wait_on_buffer(bh);
  3049. if (buffer_uptodate(bh))
  3050. return 0;
  3051. return -EIO;
  3052. }
  3053. EXPORT_SYMBOL(bh_submit_read);
  3054. void __init buffer_init(void)
  3055. {
  3056. unsigned long nrpages;
  3057. int ret;
  3058. bh_cachep = kmem_cache_create("buffer_head",
  3059. sizeof(struct buffer_head), 0,
  3060. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  3061. SLAB_MEM_SPREAD),
  3062. NULL);
  3063. /*
  3064. * Limit the bh occupancy to 10% of ZONE_NORMAL
  3065. */
  3066. nrpages = (nr_free_buffer_pages() * 10) / 100;
  3067. max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
  3068. ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
  3069. NULL, buffer_exit_cpu_dead);
  3070. WARN_ON(ret < 0);
  3071. }