buffer.c 90 KB

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