buffer.c 91 KB

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