buffer.c 87 KB

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