buffer.c 89 KB

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