buffer.c 94 KB

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