buffer.c 85 KB

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