buffer.c 87 KB

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