buffer.c 90 KB

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