extent_io.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454
  1. #include <linux/bitops.h>
  2. #include <linux/slab.h>
  3. #include <linux/bio.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/page-flags.h>
  7. #include <linux/module.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/swap.h>
  11. #include <linux/writeback.h>
  12. #include <linux/pagevec.h>
  13. #include <linux/prefetch.h>
  14. #include <linux/cleancache.h>
  15. #include "extent_io.h"
  16. #include "extent_map.h"
  17. #include "compat.h"
  18. #include "ctree.h"
  19. #include "btrfs_inode.h"
  20. #include "volumes.h"
  21. #include "check-integrity.h"
  22. static struct kmem_cache *extent_state_cache;
  23. static struct kmem_cache *extent_buffer_cache;
  24. static LIST_HEAD(buffers);
  25. static LIST_HEAD(states);
  26. #define LEAK_DEBUG 0
  27. #if LEAK_DEBUG
  28. static DEFINE_SPINLOCK(leak_lock);
  29. #endif
  30. #define BUFFER_LRU_MAX 64
  31. struct tree_entry {
  32. u64 start;
  33. u64 end;
  34. struct rb_node rb_node;
  35. };
  36. struct extent_page_data {
  37. struct bio *bio;
  38. struct extent_io_tree *tree;
  39. get_extent_t *get_extent;
  40. /* tells writepage not to lock the state bits for this range
  41. * it still does the unlocking
  42. */
  43. unsigned int extent_locked:1;
  44. /* tells the submit_bio code to use a WRITE_SYNC */
  45. unsigned int sync_io:1;
  46. };
  47. int __init extent_io_init(void)
  48. {
  49. extent_state_cache = kmem_cache_create("extent_state",
  50. sizeof(struct extent_state), 0,
  51. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  52. if (!extent_state_cache)
  53. return -ENOMEM;
  54. extent_buffer_cache = kmem_cache_create("extent_buffers",
  55. sizeof(struct extent_buffer), 0,
  56. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  57. if (!extent_buffer_cache)
  58. goto free_state_cache;
  59. return 0;
  60. free_state_cache:
  61. kmem_cache_destroy(extent_state_cache);
  62. return -ENOMEM;
  63. }
  64. void extent_io_exit(void)
  65. {
  66. struct extent_state *state;
  67. struct extent_buffer *eb;
  68. while (!list_empty(&states)) {
  69. state = list_entry(states.next, struct extent_state, leak_list);
  70. printk(KERN_ERR "btrfs state leak: start %llu end %llu "
  71. "state %lu in tree %p refs %d\n",
  72. (unsigned long long)state->start,
  73. (unsigned long long)state->end,
  74. state->state, state->tree, atomic_read(&state->refs));
  75. list_del(&state->leak_list);
  76. kmem_cache_free(extent_state_cache, state);
  77. }
  78. while (!list_empty(&buffers)) {
  79. eb = list_entry(buffers.next, struct extent_buffer, leak_list);
  80. printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
  81. "refs %d\n", (unsigned long long)eb->start,
  82. eb->len, atomic_read(&eb->refs));
  83. list_del(&eb->leak_list);
  84. kmem_cache_free(extent_buffer_cache, eb);
  85. }
  86. if (extent_state_cache)
  87. kmem_cache_destroy(extent_state_cache);
  88. if (extent_buffer_cache)
  89. kmem_cache_destroy(extent_buffer_cache);
  90. }
  91. void extent_io_tree_init(struct extent_io_tree *tree,
  92. struct address_space *mapping)
  93. {
  94. tree->state = RB_ROOT;
  95. INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
  96. tree->ops = NULL;
  97. tree->dirty_bytes = 0;
  98. spin_lock_init(&tree->lock);
  99. spin_lock_init(&tree->buffer_lock);
  100. tree->mapping = mapping;
  101. }
  102. static struct extent_state *alloc_extent_state(gfp_t mask)
  103. {
  104. struct extent_state *state;
  105. #if LEAK_DEBUG
  106. unsigned long flags;
  107. #endif
  108. state = kmem_cache_alloc(extent_state_cache, mask);
  109. if (!state)
  110. return state;
  111. state->state = 0;
  112. state->private = 0;
  113. state->tree = NULL;
  114. #if LEAK_DEBUG
  115. spin_lock_irqsave(&leak_lock, flags);
  116. list_add(&state->leak_list, &states);
  117. spin_unlock_irqrestore(&leak_lock, flags);
  118. #endif
  119. atomic_set(&state->refs, 1);
  120. init_waitqueue_head(&state->wq);
  121. return state;
  122. }
  123. void free_extent_state(struct extent_state *state)
  124. {
  125. if (!state)
  126. return;
  127. if (atomic_dec_and_test(&state->refs)) {
  128. #if LEAK_DEBUG
  129. unsigned long flags;
  130. #endif
  131. WARN_ON(state->tree);
  132. #if LEAK_DEBUG
  133. spin_lock_irqsave(&leak_lock, flags);
  134. list_del(&state->leak_list);
  135. spin_unlock_irqrestore(&leak_lock, flags);
  136. #endif
  137. kmem_cache_free(extent_state_cache, state);
  138. }
  139. }
  140. static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
  141. struct rb_node *node)
  142. {
  143. struct rb_node **p = &root->rb_node;
  144. struct rb_node *parent = NULL;
  145. struct tree_entry *entry;
  146. while (*p) {
  147. parent = *p;
  148. entry = rb_entry(parent, struct tree_entry, rb_node);
  149. if (offset < entry->start)
  150. p = &(*p)->rb_left;
  151. else if (offset > entry->end)
  152. p = &(*p)->rb_right;
  153. else
  154. return parent;
  155. }
  156. entry = rb_entry(node, struct tree_entry, rb_node);
  157. rb_link_node(node, parent, p);
  158. rb_insert_color(node, root);
  159. return NULL;
  160. }
  161. static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
  162. struct rb_node **prev_ret,
  163. struct rb_node **next_ret)
  164. {
  165. struct rb_root *root = &tree->state;
  166. struct rb_node *n = root->rb_node;
  167. struct rb_node *prev = NULL;
  168. struct rb_node *orig_prev = NULL;
  169. struct tree_entry *entry;
  170. struct tree_entry *prev_entry = NULL;
  171. while (n) {
  172. entry = rb_entry(n, struct tree_entry, rb_node);
  173. prev = n;
  174. prev_entry = entry;
  175. if (offset < entry->start)
  176. n = n->rb_left;
  177. else if (offset > entry->end)
  178. n = n->rb_right;
  179. else
  180. return n;
  181. }
  182. if (prev_ret) {
  183. orig_prev = prev;
  184. while (prev && offset > prev_entry->end) {
  185. prev = rb_next(prev);
  186. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  187. }
  188. *prev_ret = prev;
  189. prev = orig_prev;
  190. }
  191. if (next_ret) {
  192. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  193. while (prev && offset < prev_entry->start) {
  194. prev = rb_prev(prev);
  195. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  196. }
  197. *next_ret = prev;
  198. }
  199. return NULL;
  200. }
  201. static inline struct rb_node *tree_search(struct extent_io_tree *tree,
  202. u64 offset)
  203. {
  204. struct rb_node *prev = NULL;
  205. struct rb_node *ret;
  206. ret = __etree_search(tree, offset, &prev, NULL);
  207. if (!ret)
  208. return prev;
  209. return ret;
  210. }
  211. static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
  212. struct extent_state *other)
  213. {
  214. if (tree->ops && tree->ops->merge_extent_hook)
  215. tree->ops->merge_extent_hook(tree->mapping->host, new,
  216. other);
  217. }
  218. /*
  219. * utility function to look for merge candidates inside a given range.
  220. * Any extents with matching state are merged together into a single
  221. * extent in the tree. Extents with EXTENT_IO in their state field
  222. * are not merged because the end_io handlers need to be able to do
  223. * operations on them without sleeping (or doing allocations/splits).
  224. *
  225. * This should be called with the tree lock held.
  226. */
  227. static void merge_state(struct extent_io_tree *tree,
  228. struct extent_state *state)
  229. {
  230. struct extent_state *other;
  231. struct rb_node *other_node;
  232. if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  233. return;
  234. other_node = rb_prev(&state->rb_node);
  235. if (other_node) {
  236. other = rb_entry(other_node, struct extent_state, rb_node);
  237. if (other->end == state->start - 1 &&
  238. other->state == state->state) {
  239. merge_cb(tree, state, other);
  240. state->start = other->start;
  241. other->tree = NULL;
  242. rb_erase(&other->rb_node, &tree->state);
  243. free_extent_state(other);
  244. }
  245. }
  246. other_node = rb_next(&state->rb_node);
  247. if (other_node) {
  248. other = rb_entry(other_node, struct extent_state, rb_node);
  249. if (other->start == state->end + 1 &&
  250. other->state == state->state) {
  251. merge_cb(tree, state, other);
  252. state->end = other->end;
  253. other->tree = NULL;
  254. rb_erase(&other->rb_node, &tree->state);
  255. free_extent_state(other);
  256. }
  257. }
  258. }
  259. static void set_state_cb(struct extent_io_tree *tree,
  260. struct extent_state *state, int *bits)
  261. {
  262. if (tree->ops && tree->ops->set_bit_hook)
  263. tree->ops->set_bit_hook(tree->mapping->host, state, bits);
  264. }
  265. static void clear_state_cb(struct extent_io_tree *tree,
  266. struct extent_state *state, int *bits)
  267. {
  268. if (tree->ops && tree->ops->clear_bit_hook)
  269. tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
  270. }
  271. static void set_state_bits(struct extent_io_tree *tree,
  272. struct extent_state *state, int *bits);
  273. /*
  274. * insert an extent_state struct into the tree. 'bits' are set on the
  275. * struct before it is inserted.
  276. *
  277. * This may return -EEXIST if the extent is already there, in which case the
  278. * state struct is freed.
  279. *
  280. * The tree lock is not taken internally. This is a utility function and
  281. * probably isn't what you want to call (see set/clear_extent_bit).
  282. */
  283. static int insert_state(struct extent_io_tree *tree,
  284. struct extent_state *state, u64 start, u64 end,
  285. int *bits)
  286. {
  287. struct rb_node *node;
  288. if (end < start) {
  289. printk(KERN_ERR "btrfs end < start %llu %llu\n",
  290. (unsigned long long)end,
  291. (unsigned long long)start);
  292. WARN_ON(1);
  293. }
  294. state->start = start;
  295. state->end = end;
  296. set_state_bits(tree, state, bits);
  297. node = tree_insert(&tree->state, end, &state->rb_node);
  298. if (node) {
  299. struct extent_state *found;
  300. found = rb_entry(node, struct extent_state, rb_node);
  301. printk(KERN_ERR "btrfs found node %llu %llu on insert of "
  302. "%llu %llu\n", (unsigned long long)found->start,
  303. (unsigned long long)found->end,
  304. (unsigned long long)start, (unsigned long long)end);
  305. return -EEXIST;
  306. }
  307. state->tree = tree;
  308. merge_state(tree, state);
  309. return 0;
  310. }
  311. static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
  312. u64 split)
  313. {
  314. if (tree->ops && tree->ops->split_extent_hook)
  315. tree->ops->split_extent_hook(tree->mapping->host, orig, split);
  316. }
  317. /*
  318. * split a given extent state struct in two, inserting the preallocated
  319. * struct 'prealloc' as the newly created second half. 'split' indicates an
  320. * offset inside 'orig' where it should be split.
  321. *
  322. * Before calling,
  323. * the tree has 'orig' at [orig->start, orig->end]. After calling, there
  324. * are two extent state structs in the tree:
  325. * prealloc: [orig->start, split - 1]
  326. * orig: [ split, orig->end ]
  327. *
  328. * The tree locks are not taken by this function. They need to be held
  329. * by the caller.
  330. */
  331. static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
  332. struct extent_state *prealloc, u64 split)
  333. {
  334. struct rb_node *node;
  335. split_cb(tree, orig, split);
  336. prealloc->start = orig->start;
  337. prealloc->end = split - 1;
  338. prealloc->state = orig->state;
  339. orig->start = split;
  340. node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
  341. if (node) {
  342. free_extent_state(prealloc);
  343. return -EEXIST;
  344. }
  345. prealloc->tree = tree;
  346. return 0;
  347. }
  348. /*
  349. * utility function to clear some bits in an extent state struct.
  350. * it will optionally wake up any one waiting on this state (wake == 1), or
  351. * forcibly remove the state from the tree (delete == 1).
  352. *
  353. * If no bits are set on the state struct after clearing things, the
  354. * struct is freed and removed from the tree
  355. */
  356. static int clear_state_bit(struct extent_io_tree *tree,
  357. struct extent_state *state,
  358. int *bits, int wake)
  359. {
  360. int bits_to_clear = *bits & ~EXTENT_CTLBITS;
  361. int ret = state->state & bits_to_clear;
  362. if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
  363. u64 range = state->end - state->start + 1;
  364. WARN_ON(range > tree->dirty_bytes);
  365. tree->dirty_bytes -= range;
  366. }
  367. clear_state_cb(tree, state, bits);
  368. state->state &= ~bits_to_clear;
  369. if (wake)
  370. wake_up(&state->wq);
  371. if (state->state == 0) {
  372. if (state->tree) {
  373. rb_erase(&state->rb_node, &tree->state);
  374. state->tree = NULL;
  375. free_extent_state(state);
  376. } else {
  377. WARN_ON(1);
  378. }
  379. } else {
  380. merge_state(tree, state);
  381. }
  382. return ret;
  383. }
  384. static struct extent_state *
  385. alloc_extent_state_atomic(struct extent_state *prealloc)
  386. {
  387. if (!prealloc)
  388. prealloc = alloc_extent_state(GFP_ATOMIC);
  389. return prealloc;
  390. }
  391. /*
  392. * clear some bits on a range in the tree. This may require splitting
  393. * or inserting elements in the tree, so the gfp mask is used to
  394. * indicate which allocations or sleeping are allowed.
  395. *
  396. * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
  397. * the given range from the tree regardless of state (ie for truncate).
  398. *
  399. * the range [start, end] is inclusive.
  400. *
  401. * This takes the tree lock, and returns < 0 on error, > 0 if any of the
  402. * bits were already set, or zero if none of the bits were already set.
  403. */
  404. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  405. int bits, int wake, int delete,
  406. struct extent_state **cached_state,
  407. gfp_t mask)
  408. {
  409. struct extent_state *state;
  410. struct extent_state *cached;
  411. struct extent_state *prealloc = NULL;
  412. struct rb_node *next_node;
  413. struct rb_node *node;
  414. u64 last_end;
  415. int err;
  416. int set = 0;
  417. int clear = 0;
  418. if (delete)
  419. bits |= ~EXTENT_CTLBITS;
  420. bits |= EXTENT_FIRST_DELALLOC;
  421. if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  422. clear = 1;
  423. again:
  424. if (!prealloc && (mask & __GFP_WAIT)) {
  425. prealloc = alloc_extent_state(mask);
  426. if (!prealloc)
  427. return -ENOMEM;
  428. }
  429. spin_lock(&tree->lock);
  430. if (cached_state) {
  431. cached = *cached_state;
  432. if (clear) {
  433. *cached_state = NULL;
  434. cached_state = NULL;
  435. }
  436. if (cached && cached->tree && cached->start <= start &&
  437. cached->end > start) {
  438. if (clear)
  439. atomic_dec(&cached->refs);
  440. state = cached;
  441. goto hit_next;
  442. }
  443. if (clear)
  444. free_extent_state(cached);
  445. }
  446. /*
  447. * this search will find the extents that end after
  448. * our range starts
  449. */
  450. node = tree_search(tree, start);
  451. if (!node)
  452. goto out;
  453. state = rb_entry(node, struct extent_state, rb_node);
  454. hit_next:
  455. if (state->start > end)
  456. goto out;
  457. WARN_ON(state->end < start);
  458. last_end = state->end;
  459. if (state->end < end && !need_resched())
  460. next_node = rb_next(&state->rb_node);
  461. else
  462. next_node = NULL;
  463. /* the state doesn't have the wanted bits, go ahead */
  464. if (!(state->state & bits))
  465. goto next;
  466. /*
  467. * | ---- desired range ---- |
  468. * | state | or
  469. * | ------------- state -------------- |
  470. *
  471. * We need to split the extent we found, and may flip
  472. * bits on second half.
  473. *
  474. * If the extent we found extends past our range, we
  475. * just split and search again. It'll get split again
  476. * the next time though.
  477. *
  478. * If the extent we found is inside our range, we clear
  479. * the desired bit on it.
  480. */
  481. if (state->start < start) {
  482. prealloc = alloc_extent_state_atomic(prealloc);
  483. BUG_ON(!prealloc);
  484. err = split_state(tree, state, prealloc, start);
  485. BUG_ON(err == -EEXIST);
  486. prealloc = NULL;
  487. if (err)
  488. goto out;
  489. if (state->end <= end) {
  490. set |= clear_state_bit(tree, state, &bits, wake);
  491. if (last_end == (u64)-1)
  492. goto out;
  493. start = last_end + 1;
  494. }
  495. goto search_again;
  496. }
  497. /*
  498. * | ---- desired range ---- |
  499. * | state |
  500. * We need to split the extent, and clear the bit
  501. * on the first half
  502. */
  503. if (state->start <= end && state->end > end) {
  504. prealloc = alloc_extent_state_atomic(prealloc);
  505. BUG_ON(!prealloc);
  506. err = split_state(tree, state, prealloc, end + 1);
  507. BUG_ON(err == -EEXIST);
  508. if (wake)
  509. wake_up(&state->wq);
  510. set |= clear_state_bit(tree, prealloc, &bits, wake);
  511. prealloc = NULL;
  512. goto out;
  513. }
  514. set |= clear_state_bit(tree, state, &bits, wake);
  515. next:
  516. if (last_end == (u64)-1)
  517. goto out;
  518. start = last_end + 1;
  519. if (start <= end && next_node) {
  520. state = rb_entry(next_node, struct extent_state,
  521. rb_node);
  522. goto hit_next;
  523. }
  524. goto search_again;
  525. out:
  526. spin_unlock(&tree->lock);
  527. if (prealloc)
  528. free_extent_state(prealloc);
  529. return set;
  530. search_again:
  531. if (start > end)
  532. goto out;
  533. spin_unlock(&tree->lock);
  534. if (mask & __GFP_WAIT)
  535. cond_resched();
  536. goto again;
  537. }
  538. static int wait_on_state(struct extent_io_tree *tree,
  539. struct extent_state *state)
  540. __releases(tree->lock)
  541. __acquires(tree->lock)
  542. {
  543. DEFINE_WAIT(wait);
  544. prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
  545. spin_unlock(&tree->lock);
  546. schedule();
  547. spin_lock(&tree->lock);
  548. finish_wait(&state->wq, &wait);
  549. return 0;
  550. }
  551. /*
  552. * waits for one or more bits to clear on a range in the state tree.
  553. * The range [start, end] is inclusive.
  554. * The tree lock is taken by this function
  555. */
  556. int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
  557. {
  558. struct extent_state *state;
  559. struct rb_node *node;
  560. spin_lock(&tree->lock);
  561. again:
  562. while (1) {
  563. /*
  564. * this search will find all the extents that end after
  565. * our range starts
  566. */
  567. node = tree_search(tree, start);
  568. if (!node)
  569. break;
  570. state = rb_entry(node, struct extent_state, rb_node);
  571. if (state->start > end)
  572. goto out;
  573. if (state->state & bits) {
  574. start = state->start;
  575. atomic_inc(&state->refs);
  576. wait_on_state(tree, state);
  577. free_extent_state(state);
  578. goto again;
  579. }
  580. start = state->end + 1;
  581. if (start > end)
  582. break;
  583. cond_resched_lock(&tree->lock);
  584. }
  585. out:
  586. spin_unlock(&tree->lock);
  587. return 0;
  588. }
  589. static void set_state_bits(struct extent_io_tree *tree,
  590. struct extent_state *state,
  591. int *bits)
  592. {
  593. int bits_to_set = *bits & ~EXTENT_CTLBITS;
  594. set_state_cb(tree, state, bits);
  595. if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
  596. u64 range = state->end - state->start + 1;
  597. tree->dirty_bytes += range;
  598. }
  599. state->state |= bits_to_set;
  600. }
  601. static void cache_state(struct extent_state *state,
  602. struct extent_state **cached_ptr)
  603. {
  604. if (cached_ptr && !(*cached_ptr)) {
  605. if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
  606. *cached_ptr = state;
  607. atomic_inc(&state->refs);
  608. }
  609. }
  610. }
  611. static void uncache_state(struct extent_state **cached_ptr)
  612. {
  613. if (cached_ptr && (*cached_ptr)) {
  614. struct extent_state *state = *cached_ptr;
  615. *cached_ptr = NULL;
  616. free_extent_state(state);
  617. }
  618. }
  619. /*
  620. * set some bits on a range in the tree. This may require allocations or
  621. * sleeping, so the gfp mask is used to indicate what is allowed.
  622. *
  623. * If any of the exclusive bits are set, this will fail with -EEXIST if some
  624. * part of the range already has the desired bits set. The start of the
  625. * existing range is returned in failed_start in this case.
  626. *
  627. * [start, end] is inclusive This takes the tree lock.
  628. */
  629. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  630. int bits, int exclusive_bits, u64 *failed_start,
  631. struct extent_state **cached_state, gfp_t mask)
  632. {
  633. struct extent_state *state;
  634. struct extent_state *prealloc = NULL;
  635. struct rb_node *node;
  636. int err = 0;
  637. u64 last_start;
  638. u64 last_end;
  639. bits |= EXTENT_FIRST_DELALLOC;
  640. again:
  641. if (!prealloc && (mask & __GFP_WAIT)) {
  642. prealloc = alloc_extent_state(mask);
  643. BUG_ON(!prealloc);
  644. }
  645. spin_lock(&tree->lock);
  646. if (cached_state && *cached_state) {
  647. state = *cached_state;
  648. if (state->start <= start && state->end > start &&
  649. state->tree) {
  650. node = &state->rb_node;
  651. goto hit_next;
  652. }
  653. }
  654. /*
  655. * this search will find all the extents that end after
  656. * our range starts.
  657. */
  658. node = tree_search(tree, start);
  659. if (!node) {
  660. prealloc = alloc_extent_state_atomic(prealloc);
  661. BUG_ON(!prealloc);
  662. err = insert_state(tree, prealloc, start, end, &bits);
  663. prealloc = NULL;
  664. BUG_ON(err == -EEXIST);
  665. goto out;
  666. }
  667. state = rb_entry(node, struct extent_state, rb_node);
  668. hit_next:
  669. last_start = state->start;
  670. last_end = state->end;
  671. /*
  672. * | ---- desired range ---- |
  673. * | state |
  674. *
  675. * Just lock what we found and keep going
  676. */
  677. if (state->start == start && state->end <= end) {
  678. struct rb_node *next_node;
  679. if (state->state & exclusive_bits) {
  680. *failed_start = state->start;
  681. err = -EEXIST;
  682. goto out;
  683. }
  684. set_state_bits(tree, state, &bits);
  685. cache_state(state, cached_state);
  686. merge_state(tree, state);
  687. if (last_end == (u64)-1)
  688. goto out;
  689. start = last_end + 1;
  690. next_node = rb_next(&state->rb_node);
  691. if (next_node && start < end && prealloc && !need_resched()) {
  692. state = rb_entry(next_node, struct extent_state,
  693. rb_node);
  694. if (state->start == start)
  695. goto hit_next;
  696. }
  697. goto search_again;
  698. }
  699. /*
  700. * | ---- desired range ---- |
  701. * | state |
  702. * or
  703. * | ------------- state -------------- |
  704. *
  705. * We need to split the extent we found, and may flip bits on
  706. * second half.
  707. *
  708. * If the extent we found extends past our
  709. * range, we just split and search again. It'll get split
  710. * again the next time though.
  711. *
  712. * If the extent we found is inside our range, we set the
  713. * desired bit on it.
  714. */
  715. if (state->start < start) {
  716. if (state->state & exclusive_bits) {
  717. *failed_start = start;
  718. err = -EEXIST;
  719. goto out;
  720. }
  721. prealloc = alloc_extent_state_atomic(prealloc);
  722. BUG_ON(!prealloc);
  723. err = split_state(tree, state, prealloc, start);
  724. BUG_ON(err == -EEXIST);
  725. prealloc = NULL;
  726. if (err)
  727. goto out;
  728. if (state->end <= end) {
  729. set_state_bits(tree, state, &bits);
  730. cache_state(state, cached_state);
  731. merge_state(tree, state);
  732. if (last_end == (u64)-1)
  733. goto out;
  734. start = last_end + 1;
  735. }
  736. goto search_again;
  737. }
  738. /*
  739. * | ---- desired range ---- |
  740. * | state | or | state |
  741. *
  742. * There's a hole, we need to insert something in it and
  743. * ignore the extent we found.
  744. */
  745. if (state->start > start) {
  746. u64 this_end;
  747. if (end < last_start)
  748. this_end = end;
  749. else
  750. this_end = last_start - 1;
  751. prealloc = alloc_extent_state_atomic(prealloc);
  752. BUG_ON(!prealloc);
  753. /*
  754. * Avoid to free 'prealloc' if it can be merged with
  755. * the later extent.
  756. */
  757. err = insert_state(tree, prealloc, start, this_end,
  758. &bits);
  759. BUG_ON(err == -EEXIST);
  760. if (err) {
  761. free_extent_state(prealloc);
  762. prealloc = NULL;
  763. goto out;
  764. }
  765. cache_state(prealloc, cached_state);
  766. prealloc = NULL;
  767. start = this_end + 1;
  768. goto search_again;
  769. }
  770. /*
  771. * | ---- desired range ---- |
  772. * | state |
  773. * We need to split the extent, and set the bit
  774. * on the first half
  775. */
  776. if (state->start <= end && state->end > end) {
  777. if (state->state & exclusive_bits) {
  778. *failed_start = start;
  779. err = -EEXIST;
  780. goto out;
  781. }
  782. prealloc = alloc_extent_state_atomic(prealloc);
  783. BUG_ON(!prealloc);
  784. err = split_state(tree, state, prealloc, end + 1);
  785. BUG_ON(err == -EEXIST);
  786. set_state_bits(tree, prealloc, &bits);
  787. cache_state(prealloc, cached_state);
  788. merge_state(tree, prealloc);
  789. prealloc = NULL;
  790. goto out;
  791. }
  792. goto search_again;
  793. out:
  794. spin_unlock(&tree->lock);
  795. if (prealloc)
  796. free_extent_state(prealloc);
  797. return err;
  798. search_again:
  799. if (start > end)
  800. goto out;
  801. spin_unlock(&tree->lock);
  802. if (mask & __GFP_WAIT)
  803. cond_resched();
  804. goto again;
  805. }
  806. /**
  807. * convert_extent - convert all bits in a given range from one bit to another
  808. * @tree: the io tree to search
  809. * @start: the start offset in bytes
  810. * @end: the end offset in bytes (inclusive)
  811. * @bits: the bits to set in this range
  812. * @clear_bits: the bits to clear in this range
  813. * @mask: the allocation mask
  814. *
  815. * This will go through and set bits for the given range. If any states exist
  816. * already in this range they are set with the given bit and cleared of the
  817. * clear_bits. This is only meant to be used by things that are mergeable, ie
  818. * converting from say DELALLOC to DIRTY. This is not meant to be used with
  819. * boundary bits like LOCK.
  820. */
  821. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  822. int bits, int clear_bits, gfp_t mask)
  823. {
  824. struct extent_state *state;
  825. struct extent_state *prealloc = NULL;
  826. struct rb_node *node;
  827. int err = 0;
  828. u64 last_start;
  829. u64 last_end;
  830. again:
  831. if (!prealloc && (mask & __GFP_WAIT)) {
  832. prealloc = alloc_extent_state(mask);
  833. if (!prealloc)
  834. return -ENOMEM;
  835. }
  836. spin_lock(&tree->lock);
  837. /*
  838. * this search will find all the extents that end after
  839. * our range starts.
  840. */
  841. node = tree_search(tree, start);
  842. if (!node) {
  843. prealloc = alloc_extent_state_atomic(prealloc);
  844. if (!prealloc) {
  845. err = -ENOMEM;
  846. goto out;
  847. }
  848. err = insert_state(tree, prealloc, start, end, &bits);
  849. prealloc = NULL;
  850. BUG_ON(err == -EEXIST);
  851. goto out;
  852. }
  853. state = rb_entry(node, struct extent_state, rb_node);
  854. hit_next:
  855. last_start = state->start;
  856. last_end = state->end;
  857. /*
  858. * | ---- desired range ---- |
  859. * | state |
  860. *
  861. * Just lock what we found and keep going
  862. */
  863. if (state->start == start && state->end <= end) {
  864. struct rb_node *next_node;
  865. set_state_bits(tree, state, &bits);
  866. clear_state_bit(tree, state, &clear_bits, 0);
  867. if (last_end == (u64)-1)
  868. goto out;
  869. start = last_end + 1;
  870. next_node = rb_next(&state->rb_node);
  871. if (next_node && start < end && prealloc && !need_resched()) {
  872. state = rb_entry(next_node, struct extent_state,
  873. rb_node);
  874. if (state->start == start)
  875. goto hit_next;
  876. }
  877. goto search_again;
  878. }
  879. /*
  880. * | ---- desired range ---- |
  881. * | state |
  882. * or
  883. * | ------------- state -------------- |
  884. *
  885. * We need to split the extent we found, and may flip bits on
  886. * second half.
  887. *
  888. * If the extent we found extends past our
  889. * range, we just split and search again. It'll get split
  890. * again the next time though.
  891. *
  892. * If the extent we found is inside our range, we set the
  893. * desired bit on it.
  894. */
  895. if (state->start < start) {
  896. prealloc = alloc_extent_state_atomic(prealloc);
  897. if (!prealloc) {
  898. err = -ENOMEM;
  899. goto out;
  900. }
  901. err = split_state(tree, state, prealloc, start);
  902. BUG_ON(err == -EEXIST);
  903. prealloc = NULL;
  904. if (err)
  905. goto out;
  906. if (state->end <= end) {
  907. set_state_bits(tree, state, &bits);
  908. clear_state_bit(tree, state, &clear_bits, 0);
  909. if (last_end == (u64)-1)
  910. goto out;
  911. start = last_end + 1;
  912. }
  913. goto search_again;
  914. }
  915. /*
  916. * | ---- desired range ---- |
  917. * | state | or | state |
  918. *
  919. * There's a hole, we need to insert something in it and
  920. * ignore the extent we found.
  921. */
  922. if (state->start > start) {
  923. u64 this_end;
  924. if (end < last_start)
  925. this_end = end;
  926. else
  927. this_end = last_start - 1;
  928. prealloc = alloc_extent_state_atomic(prealloc);
  929. if (!prealloc) {
  930. err = -ENOMEM;
  931. goto out;
  932. }
  933. /*
  934. * Avoid to free 'prealloc' if it can be merged with
  935. * the later extent.
  936. */
  937. err = insert_state(tree, prealloc, start, this_end,
  938. &bits);
  939. BUG_ON(err == -EEXIST);
  940. if (err) {
  941. free_extent_state(prealloc);
  942. prealloc = NULL;
  943. goto out;
  944. }
  945. prealloc = NULL;
  946. start = this_end + 1;
  947. goto search_again;
  948. }
  949. /*
  950. * | ---- desired range ---- |
  951. * | state |
  952. * We need to split the extent, and set the bit
  953. * on the first half
  954. */
  955. if (state->start <= end && state->end > end) {
  956. prealloc = alloc_extent_state_atomic(prealloc);
  957. if (!prealloc) {
  958. err = -ENOMEM;
  959. goto out;
  960. }
  961. err = split_state(tree, state, prealloc, end + 1);
  962. BUG_ON(err == -EEXIST);
  963. set_state_bits(tree, prealloc, &bits);
  964. clear_state_bit(tree, prealloc, &clear_bits, 0);
  965. prealloc = NULL;
  966. goto out;
  967. }
  968. goto search_again;
  969. out:
  970. spin_unlock(&tree->lock);
  971. if (prealloc)
  972. free_extent_state(prealloc);
  973. return err;
  974. search_again:
  975. if (start > end)
  976. goto out;
  977. spin_unlock(&tree->lock);
  978. if (mask & __GFP_WAIT)
  979. cond_resched();
  980. goto again;
  981. }
  982. /* wrappers around set/clear extent bit */
  983. int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  984. gfp_t mask)
  985. {
  986. return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
  987. NULL, mask);
  988. }
  989. int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  990. int bits, gfp_t mask)
  991. {
  992. return set_extent_bit(tree, start, end, bits, 0, NULL,
  993. NULL, mask);
  994. }
  995. int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  996. int bits, gfp_t mask)
  997. {
  998. return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
  999. }
  1000. int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
  1001. struct extent_state **cached_state, gfp_t mask)
  1002. {
  1003. return set_extent_bit(tree, start, end,
  1004. EXTENT_DELALLOC | EXTENT_UPTODATE,
  1005. 0, NULL, cached_state, mask);
  1006. }
  1007. int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
  1008. gfp_t mask)
  1009. {
  1010. return clear_extent_bit(tree, start, end,
  1011. EXTENT_DIRTY | EXTENT_DELALLOC |
  1012. EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
  1013. }
  1014. int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
  1015. gfp_t mask)
  1016. {
  1017. return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
  1018. NULL, mask);
  1019. }
  1020. int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
  1021. struct extent_state **cached_state, gfp_t mask)
  1022. {
  1023. return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
  1024. NULL, cached_state, mask);
  1025. }
  1026. static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
  1027. u64 end, struct extent_state **cached_state,
  1028. gfp_t mask)
  1029. {
  1030. return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
  1031. cached_state, mask);
  1032. }
  1033. /*
  1034. * either insert or lock state struct between start and end use mask to tell
  1035. * us if waiting is desired.
  1036. */
  1037. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1038. int bits, struct extent_state **cached_state, gfp_t mask)
  1039. {
  1040. int err;
  1041. u64 failed_start;
  1042. while (1) {
  1043. err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
  1044. EXTENT_LOCKED, &failed_start,
  1045. cached_state, mask);
  1046. if (err == -EEXIST && (mask & __GFP_WAIT)) {
  1047. wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
  1048. start = failed_start;
  1049. } else {
  1050. break;
  1051. }
  1052. WARN_ON(start > end);
  1053. }
  1054. return err;
  1055. }
  1056. int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
  1057. {
  1058. return lock_extent_bits(tree, start, end, 0, NULL, mask);
  1059. }
  1060. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
  1061. gfp_t mask)
  1062. {
  1063. int err;
  1064. u64 failed_start;
  1065. err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
  1066. &failed_start, NULL, mask);
  1067. if (err == -EEXIST) {
  1068. if (failed_start > start)
  1069. clear_extent_bit(tree, start, failed_start - 1,
  1070. EXTENT_LOCKED, 1, 0, NULL, mask);
  1071. return 0;
  1072. }
  1073. return 1;
  1074. }
  1075. int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
  1076. struct extent_state **cached, gfp_t mask)
  1077. {
  1078. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
  1079. mask);
  1080. }
  1081. int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
  1082. {
  1083. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
  1084. mask);
  1085. }
  1086. /*
  1087. * helper function to set both pages and extents in the tree writeback
  1088. */
  1089. static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
  1090. {
  1091. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1092. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1093. struct page *page;
  1094. while (index <= end_index) {
  1095. page = find_get_page(tree->mapping, index);
  1096. BUG_ON(!page);
  1097. set_page_writeback(page);
  1098. page_cache_release(page);
  1099. index++;
  1100. }
  1101. return 0;
  1102. }
  1103. /* find the first state struct with 'bits' set after 'start', and
  1104. * return it. tree->lock must be held. NULL will returned if
  1105. * nothing was found after 'start'
  1106. */
  1107. struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
  1108. u64 start, int bits)
  1109. {
  1110. struct rb_node *node;
  1111. struct extent_state *state;
  1112. /*
  1113. * this search will find all the extents that end after
  1114. * our range starts.
  1115. */
  1116. node = tree_search(tree, start);
  1117. if (!node)
  1118. goto out;
  1119. while (1) {
  1120. state = rb_entry(node, struct extent_state, rb_node);
  1121. if (state->end >= start && (state->state & bits))
  1122. return state;
  1123. node = rb_next(node);
  1124. if (!node)
  1125. break;
  1126. }
  1127. out:
  1128. return NULL;
  1129. }
  1130. /*
  1131. * find the first offset in the io tree with 'bits' set. zero is
  1132. * returned if we find something, and *start_ret and *end_ret are
  1133. * set to reflect the state struct that was found.
  1134. *
  1135. * If nothing was found, 1 is returned, < 0 on error
  1136. */
  1137. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  1138. u64 *start_ret, u64 *end_ret, int bits)
  1139. {
  1140. struct extent_state *state;
  1141. int ret = 1;
  1142. spin_lock(&tree->lock);
  1143. state = find_first_extent_bit_state(tree, start, bits);
  1144. if (state) {
  1145. *start_ret = state->start;
  1146. *end_ret = state->end;
  1147. ret = 0;
  1148. }
  1149. spin_unlock(&tree->lock);
  1150. return ret;
  1151. }
  1152. /*
  1153. * find a contiguous range of bytes in the file marked as delalloc, not
  1154. * more than 'max_bytes'. start and end are used to return the range,
  1155. *
  1156. * 1 is returned if we find something, 0 if nothing was in the tree
  1157. */
  1158. static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
  1159. u64 *start, u64 *end, u64 max_bytes,
  1160. struct extent_state **cached_state)
  1161. {
  1162. struct rb_node *node;
  1163. struct extent_state *state;
  1164. u64 cur_start = *start;
  1165. u64 found = 0;
  1166. u64 total_bytes = 0;
  1167. spin_lock(&tree->lock);
  1168. /*
  1169. * this search will find all the extents that end after
  1170. * our range starts.
  1171. */
  1172. node = tree_search(tree, cur_start);
  1173. if (!node) {
  1174. if (!found)
  1175. *end = (u64)-1;
  1176. goto out;
  1177. }
  1178. while (1) {
  1179. state = rb_entry(node, struct extent_state, rb_node);
  1180. if (found && (state->start != cur_start ||
  1181. (state->state & EXTENT_BOUNDARY))) {
  1182. goto out;
  1183. }
  1184. if (!(state->state & EXTENT_DELALLOC)) {
  1185. if (!found)
  1186. *end = state->end;
  1187. goto out;
  1188. }
  1189. if (!found) {
  1190. *start = state->start;
  1191. *cached_state = state;
  1192. atomic_inc(&state->refs);
  1193. }
  1194. found++;
  1195. *end = state->end;
  1196. cur_start = state->end + 1;
  1197. node = rb_next(node);
  1198. if (!node)
  1199. break;
  1200. total_bytes += state->end - state->start + 1;
  1201. if (total_bytes >= max_bytes)
  1202. break;
  1203. }
  1204. out:
  1205. spin_unlock(&tree->lock);
  1206. return found;
  1207. }
  1208. static noinline int __unlock_for_delalloc(struct inode *inode,
  1209. struct page *locked_page,
  1210. u64 start, u64 end)
  1211. {
  1212. int ret;
  1213. struct page *pages[16];
  1214. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1215. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1216. unsigned long nr_pages = end_index - index + 1;
  1217. int i;
  1218. if (index == locked_page->index && end_index == index)
  1219. return 0;
  1220. while (nr_pages > 0) {
  1221. ret = find_get_pages_contig(inode->i_mapping, index,
  1222. min_t(unsigned long, nr_pages,
  1223. ARRAY_SIZE(pages)), pages);
  1224. for (i = 0; i < ret; i++) {
  1225. if (pages[i] != locked_page)
  1226. unlock_page(pages[i]);
  1227. page_cache_release(pages[i]);
  1228. }
  1229. nr_pages -= ret;
  1230. index += ret;
  1231. cond_resched();
  1232. }
  1233. return 0;
  1234. }
  1235. static noinline int lock_delalloc_pages(struct inode *inode,
  1236. struct page *locked_page,
  1237. u64 delalloc_start,
  1238. u64 delalloc_end)
  1239. {
  1240. unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
  1241. unsigned long start_index = index;
  1242. unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
  1243. unsigned long pages_locked = 0;
  1244. struct page *pages[16];
  1245. unsigned long nrpages;
  1246. int ret;
  1247. int i;
  1248. /* the caller is responsible for locking the start index */
  1249. if (index == locked_page->index && index == end_index)
  1250. return 0;
  1251. /* skip the page at the start index */
  1252. nrpages = end_index - index + 1;
  1253. while (nrpages > 0) {
  1254. ret = find_get_pages_contig(inode->i_mapping, index,
  1255. min_t(unsigned long,
  1256. nrpages, ARRAY_SIZE(pages)), pages);
  1257. if (ret == 0) {
  1258. ret = -EAGAIN;
  1259. goto done;
  1260. }
  1261. /* now we have an array of pages, lock them all */
  1262. for (i = 0; i < ret; i++) {
  1263. /*
  1264. * the caller is taking responsibility for
  1265. * locked_page
  1266. */
  1267. if (pages[i] != locked_page) {
  1268. lock_page(pages[i]);
  1269. if (!PageDirty(pages[i]) ||
  1270. pages[i]->mapping != inode->i_mapping) {
  1271. ret = -EAGAIN;
  1272. unlock_page(pages[i]);
  1273. page_cache_release(pages[i]);
  1274. goto done;
  1275. }
  1276. }
  1277. page_cache_release(pages[i]);
  1278. pages_locked++;
  1279. }
  1280. nrpages -= ret;
  1281. index += ret;
  1282. cond_resched();
  1283. }
  1284. ret = 0;
  1285. done:
  1286. if (ret && pages_locked) {
  1287. __unlock_for_delalloc(inode, locked_page,
  1288. delalloc_start,
  1289. ((u64)(start_index + pages_locked - 1)) <<
  1290. PAGE_CACHE_SHIFT);
  1291. }
  1292. return ret;
  1293. }
  1294. /*
  1295. * find a contiguous range of bytes in the file marked as delalloc, not
  1296. * more than 'max_bytes'. start and end are used to return the range,
  1297. *
  1298. * 1 is returned if we find something, 0 if nothing was in the tree
  1299. */
  1300. static noinline u64 find_lock_delalloc_range(struct inode *inode,
  1301. struct extent_io_tree *tree,
  1302. struct page *locked_page,
  1303. u64 *start, u64 *end,
  1304. u64 max_bytes)
  1305. {
  1306. u64 delalloc_start;
  1307. u64 delalloc_end;
  1308. u64 found;
  1309. struct extent_state *cached_state = NULL;
  1310. int ret;
  1311. int loops = 0;
  1312. again:
  1313. /* step one, find a bunch of delalloc bytes starting at start */
  1314. delalloc_start = *start;
  1315. delalloc_end = 0;
  1316. found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
  1317. max_bytes, &cached_state);
  1318. if (!found || delalloc_end <= *start) {
  1319. *start = delalloc_start;
  1320. *end = delalloc_end;
  1321. free_extent_state(cached_state);
  1322. return found;
  1323. }
  1324. /*
  1325. * start comes from the offset of locked_page. We have to lock
  1326. * pages in order, so we can't process delalloc bytes before
  1327. * locked_page
  1328. */
  1329. if (delalloc_start < *start)
  1330. delalloc_start = *start;
  1331. /*
  1332. * make sure to limit the number of pages we try to lock down
  1333. * if we're looping.
  1334. */
  1335. if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
  1336. delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
  1337. /* step two, lock all the pages after the page that has start */
  1338. ret = lock_delalloc_pages(inode, locked_page,
  1339. delalloc_start, delalloc_end);
  1340. if (ret == -EAGAIN) {
  1341. /* some of the pages are gone, lets avoid looping by
  1342. * shortening the size of the delalloc range we're searching
  1343. */
  1344. free_extent_state(cached_state);
  1345. if (!loops) {
  1346. unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
  1347. max_bytes = PAGE_CACHE_SIZE - offset;
  1348. loops = 1;
  1349. goto again;
  1350. } else {
  1351. found = 0;
  1352. goto out_failed;
  1353. }
  1354. }
  1355. BUG_ON(ret);
  1356. /* step three, lock the state bits for the whole range */
  1357. lock_extent_bits(tree, delalloc_start, delalloc_end,
  1358. 0, &cached_state, GFP_NOFS);
  1359. /* then test to make sure it is all still delalloc */
  1360. ret = test_range_bit(tree, delalloc_start, delalloc_end,
  1361. EXTENT_DELALLOC, 1, cached_state);
  1362. if (!ret) {
  1363. unlock_extent_cached(tree, delalloc_start, delalloc_end,
  1364. &cached_state, GFP_NOFS);
  1365. __unlock_for_delalloc(inode, locked_page,
  1366. delalloc_start, delalloc_end);
  1367. cond_resched();
  1368. goto again;
  1369. }
  1370. free_extent_state(cached_state);
  1371. *start = delalloc_start;
  1372. *end = delalloc_end;
  1373. out_failed:
  1374. return found;
  1375. }
  1376. int extent_clear_unlock_delalloc(struct inode *inode,
  1377. struct extent_io_tree *tree,
  1378. u64 start, u64 end, struct page *locked_page,
  1379. unsigned long op)
  1380. {
  1381. int ret;
  1382. struct page *pages[16];
  1383. unsigned long index = start >> PAGE_CACHE_SHIFT;
  1384. unsigned long end_index = end >> PAGE_CACHE_SHIFT;
  1385. unsigned long nr_pages = end_index - index + 1;
  1386. int i;
  1387. int clear_bits = 0;
  1388. if (op & EXTENT_CLEAR_UNLOCK)
  1389. clear_bits |= EXTENT_LOCKED;
  1390. if (op & EXTENT_CLEAR_DIRTY)
  1391. clear_bits |= EXTENT_DIRTY;
  1392. if (op & EXTENT_CLEAR_DELALLOC)
  1393. clear_bits |= EXTENT_DELALLOC;
  1394. clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
  1395. if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  1396. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
  1397. EXTENT_SET_PRIVATE2)))
  1398. return 0;
  1399. while (nr_pages > 0) {
  1400. ret = find_get_pages_contig(inode->i_mapping, index,
  1401. min_t(unsigned long,
  1402. nr_pages, ARRAY_SIZE(pages)), pages);
  1403. for (i = 0; i < ret; i++) {
  1404. if (op & EXTENT_SET_PRIVATE2)
  1405. SetPagePrivate2(pages[i]);
  1406. if (pages[i] == locked_page) {
  1407. page_cache_release(pages[i]);
  1408. continue;
  1409. }
  1410. if (op & EXTENT_CLEAR_DIRTY)
  1411. clear_page_dirty_for_io(pages[i]);
  1412. if (op & EXTENT_SET_WRITEBACK)
  1413. set_page_writeback(pages[i]);
  1414. if (op & EXTENT_END_WRITEBACK)
  1415. end_page_writeback(pages[i]);
  1416. if (op & EXTENT_CLEAR_UNLOCK_PAGE)
  1417. unlock_page(pages[i]);
  1418. page_cache_release(pages[i]);
  1419. }
  1420. nr_pages -= ret;
  1421. index += ret;
  1422. cond_resched();
  1423. }
  1424. return 0;
  1425. }
  1426. /*
  1427. * count the number of bytes in the tree that have a given bit(s)
  1428. * set. This can be fairly slow, except for EXTENT_DIRTY which is
  1429. * cached. The total number found is returned.
  1430. */
  1431. u64 count_range_bits(struct extent_io_tree *tree,
  1432. u64 *start, u64 search_end, u64 max_bytes,
  1433. unsigned long bits, int contig)
  1434. {
  1435. struct rb_node *node;
  1436. struct extent_state *state;
  1437. u64 cur_start = *start;
  1438. u64 total_bytes = 0;
  1439. u64 last = 0;
  1440. int found = 0;
  1441. if (search_end <= cur_start) {
  1442. WARN_ON(1);
  1443. return 0;
  1444. }
  1445. spin_lock(&tree->lock);
  1446. if (cur_start == 0 && bits == EXTENT_DIRTY) {
  1447. total_bytes = tree->dirty_bytes;
  1448. goto out;
  1449. }
  1450. /*
  1451. * this search will find all the extents that end after
  1452. * our range starts.
  1453. */
  1454. node = tree_search(tree, cur_start);
  1455. if (!node)
  1456. goto out;
  1457. while (1) {
  1458. state = rb_entry(node, struct extent_state, rb_node);
  1459. if (state->start > search_end)
  1460. break;
  1461. if (contig && found && state->start > last + 1)
  1462. break;
  1463. if (state->end >= cur_start && (state->state & bits) == bits) {
  1464. total_bytes += min(search_end, state->end) + 1 -
  1465. max(cur_start, state->start);
  1466. if (total_bytes >= max_bytes)
  1467. break;
  1468. if (!found) {
  1469. *start = max(cur_start, state->start);
  1470. found = 1;
  1471. }
  1472. last = state->end;
  1473. } else if (contig && found) {
  1474. break;
  1475. }
  1476. node = rb_next(node);
  1477. if (!node)
  1478. break;
  1479. }
  1480. out:
  1481. spin_unlock(&tree->lock);
  1482. return total_bytes;
  1483. }
  1484. /*
  1485. * set the private field for a given byte offset in the tree. If there isn't
  1486. * an extent_state there already, this does nothing.
  1487. */
  1488. int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
  1489. {
  1490. struct rb_node *node;
  1491. struct extent_state *state;
  1492. int ret = 0;
  1493. spin_lock(&tree->lock);
  1494. /*
  1495. * this search will find all the extents that end after
  1496. * our range starts.
  1497. */
  1498. node = tree_search(tree, start);
  1499. if (!node) {
  1500. ret = -ENOENT;
  1501. goto out;
  1502. }
  1503. state = rb_entry(node, struct extent_state, rb_node);
  1504. if (state->start != start) {
  1505. ret = -ENOENT;
  1506. goto out;
  1507. }
  1508. state->private = private;
  1509. out:
  1510. spin_unlock(&tree->lock);
  1511. return ret;
  1512. }
  1513. int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
  1514. {
  1515. struct rb_node *node;
  1516. struct extent_state *state;
  1517. int ret = 0;
  1518. spin_lock(&tree->lock);
  1519. /*
  1520. * this search will find all the extents that end after
  1521. * our range starts.
  1522. */
  1523. node = tree_search(tree, start);
  1524. if (!node) {
  1525. ret = -ENOENT;
  1526. goto out;
  1527. }
  1528. state = rb_entry(node, struct extent_state, rb_node);
  1529. if (state->start != start) {
  1530. ret = -ENOENT;
  1531. goto out;
  1532. }
  1533. *private = state->private;
  1534. out:
  1535. spin_unlock(&tree->lock);
  1536. return ret;
  1537. }
  1538. /*
  1539. * searches a range in the state tree for a given mask.
  1540. * If 'filled' == 1, this returns 1 only if every extent in the tree
  1541. * has the bits set. Otherwise, 1 is returned if any bit in the
  1542. * range is found set.
  1543. */
  1544. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1545. int bits, int filled, struct extent_state *cached)
  1546. {
  1547. struct extent_state *state = NULL;
  1548. struct rb_node *node;
  1549. int bitset = 0;
  1550. spin_lock(&tree->lock);
  1551. if (cached && cached->tree && cached->start <= start &&
  1552. cached->end > start)
  1553. node = &cached->rb_node;
  1554. else
  1555. node = tree_search(tree, start);
  1556. while (node && start <= end) {
  1557. state = rb_entry(node, struct extent_state, rb_node);
  1558. if (filled && state->start > start) {
  1559. bitset = 0;
  1560. break;
  1561. }
  1562. if (state->start > end)
  1563. break;
  1564. if (state->state & bits) {
  1565. bitset = 1;
  1566. if (!filled)
  1567. break;
  1568. } else if (filled) {
  1569. bitset = 0;
  1570. break;
  1571. }
  1572. if (state->end == (u64)-1)
  1573. break;
  1574. start = state->end + 1;
  1575. if (start > end)
  1576. break;
  1577. node = rb_next(node);
  1578. if (!node) {
  1579. if (filled)
  1580. bitset = 0;
  1581. break;
  1582. }
  1583. }
  1584. spin_unlock(&tree->lock);
  1585. return bitset;
  1586. }
  1587. /*
  1588. * helper function to set a given page up to date if all the
  1589. * extents in the tree for that page are up to date
  1590. */
  1591. static int check_page_uptodate(struct extent_io_tree *tree,
  1592. struct page *page)
  1593. {
  1594. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  1595. u64 end = start + PAGE_CACHE_SIZE - 1;
  1596. if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
  1597. SetPageUptodate(page);
  1598. return 0;
  1599. }
  1600. /*
  1601. * helper function to unlock a page if all the extents in the tree
  1602. * for that page are unlocked
  1603. */
  1604. static int check_page_locked(struct extent_io_tree *tree,
  1605. struct page *page)
  1606. {
  1607. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  1608. u64 end = start + PAGE_CACHE_SIZE - 1;
  1609. if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
  1610. unlock_page(page);
  1611. return 0;
  1612. }
  1613. /*
  1614. * helper function to end page writeback if all the extents
  1615. * in the tree for that page are done with writeback
  1616. */
  1617. static int check_page_writeback(struct extent_io_tree *tree,
  1618. struct page *page)
  1619. {
  1620. end_page_writeback(page);
  1621. return 0;
  1622. }
  1623. /*
  1624. * When IO fails, either with EIO or csum verification fails, we
  1625. * try other mirrors that might have a good copy of the data. This
  1626. * io_failure_record is used to record state as we go through all the
  1627. * mirrors. If another mirror has good data, the page is set up to date
  1628. * and things continue. If a good mirror can't be found, the original
  1629. * bio end_io callback is called to indicate things have failed.
  1630. */
  1631. struct io_failure_record {
  1632. struct page *page;
  1633. u64 start;
  1634. u64 len;
  1635. u64 logical;
  1636. unsigned long bio_flags;
  1637. int this_mirror;
  1638. int failed_mirror;
  1639. int in_validation;
  1640. };
  1641. static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
  1642. int did_repair)
  1643. {
  1644. int ret;
  1645. int err = 0;
  1646. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1647. set_state_private(failure_tree, rec->start, 0);
  1648. ret = clear_extent_bits(failure_tree, rec->start,
  1649. rec->start + rec->len - 1,
  1650. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1651. if (ret)
  1652. err = ret;
  1653. if (did_repair) {
  1654. ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
  1655. rec->start + rec->len - 1,
  1656. EXTENT_DAMAGED, GFP_NOFS);
  1657. if (ret && !err)
  1658. err = ret;
  1659. }
  1660. kfree(rec);
  1661. return err;
  1662. }
  1663. static void repair_io_failure_callback(struct bio *bio, int err)
  1664. {
  1665. complete(bio->bi_private);
  1666. }
  1667. /*
  1668. * this bypasses the standard btrfs submit functions deliberately, as
  1669. * the standard behavior is to write all copies in a raid setup. here we only
  1670. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1671. * submit_bio directly.
  1672. * to avoid any synchonization issues, wait for the data after writing, which
  1673. * actually prevents the read that triggered the error from finishing.
  1674. * currently, there can be no more than two copies of every data bit. thus,
  1675. * exactly one rewrite is required.
  1676. */
  1677. int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
  1678. u64 length, u64 logical, struct page *page,
  1679. int mirror_num)
  1680. {
  1681. struct bio *bio;
  1682. struct btrfs_device *dev;
  1683. DECLARE_COMPLETION_ONSTACK(compl);
  1684. u64 map_length = 0;
  1685. u64 sector;
  1686. struct btrfs_bio *bbio = NULL;
  1687. int ret;
  1688. BUG_ON(!mirror_num);
  1689. bio = bio_alloc(GFP_NOFS, 1);
  1690. if (!bio)
  1691. return -EIO;
  1692. bio->bi_private = &compl;
  1693. bio->bi_end_io = repair_io_failure_callback;
  1694. bio->bi_size = 0;
  1695. map_length = length;
  1696. ret = btrfs_map_block(map_tree, WRITE, logical,
  1697. &map_length, &bbio, mirror_num);
  1698. if (ret) {
  1699. bio_put(bio);
  1700. return -EIO;
  1701. }
  1702. BUG_ON(mirror_num != bbio->mirror_num);
  1703. sector = bbio->stripes[mirror_num-1].physical >> 9;
  1704. bio->bi_sector = sector;
  1705. dev = bbio->stripes[mirror_num-1].dev;
  1706. kfree(bbio);
  1707. if (!dev || !dev->bdev || !dev->writeable) {
  1708. bio_put(bio);
  1709. return -EIO;
  1710. }
  1711. bio->bi_bdev = dev->bdev;
  1712. bio_add_page(bio, page, length, start-page_offset(page));
  1713. btrfsic_submit_bio(WRITE_SYNC, bio);
  1714. wait_for_completion(&compl);
  1715. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  1716. /* try to remap that extent elsewhere? */
  1717. bio_put(bio);
  1718. return -EIO;
  1719. }
  1720. printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
  1721. "sector %llu)\n", page->mapping->host->i_ino, start,
  1722. dev->name, sector);
  1723. bio_put(bio);
  1724. return 0;
  1725. }
  1726. /*
  1727. * each time an IO finishes, we do a fast check in the IO failure tree
  1728. * to see if we need to process or clean up an io_failure_record
  1729. */
  1730. static int clean_io_failure(u64 start, struct page *page)
  1731. {
  1732. u64 private;
  1733. u64 private_failure;
  1734. struct io_failure_record *failrec;
  1735. struct btrfs_mapping_tree *map_tree;
  1736. struct extent_state *state;
  1737. int num_copies;
  1738. int did_repair = 0;
  1739. int ret;
  1740. struct inode *inode = page->mapping->host;
  1741. private = 0;
  1742. ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1743. (u64)-1, 1, EXTENT_DIRTY, 0);
  1744. if (!ret)
  1745. return 0;
  1746. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
  1747. &private_failure);
  1748. if (ret)
  1749. return 0;
  1750. failrec = (struct io_failure_record *)(unsigned long) private_failure;
  1751. BUG_ON(!failrec->this_mirror);
  1752. if (failrec->in_validation) {
  1753. /* there was no real error, just free the record */
  1754. pr_debug("clean_io_failure: freeing dummy error at %llu\n",
  1755. failrec->start);
  1756. did_repair = 1;
  1757. goto out;
  1758. }
  1759. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1760. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1761. failrec->start,
  1762. EXTENT_LOCKED);
  1763. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1764. if (state && state->start == failrec->start) {
  1765. map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
  1766. num_copies = btrfs_num_copies(map_tree, failrec->logical,
  1767. failrec->len);
  1768. if (num_copies > 1) {
  1769. ret = repair_io_failure(map_tree, start, failrec->len,
  1770. failrec->logical, page,
  1771. failrec->failed_mirror);
  1772. did_repair = !ret;
  1773. }
  1774. }
  1775. out:
  1776. if (!ret)
  1777. ret = free_io_failure(inode, failrec, did_repair);
  1778. return ret;
  1779. }
  1780. /*
  1781. * this is a generic handler for readpage errors (default
  1782. * readpage_io_failed_hook). if other copies exist, read those and write back
  1783. * good data to the failed position. does not investigate in remapping the
  1784. * failed extent elsewhere, hoping the device will be smart enough to do this as
  1785. * needed
  1786. */
  1787. static int bio_readpage_error(struct bio *failed_bio, struct page *page,
  1788. u64 start, u64 end, int failed_mirror,
  1789. struct extent_state *state)
  1790. {
  1791. struct io_failure_record *failrec = NULL;
  1792. u64 private;
  1793. struct extent_map *em;
  1794. struct inode *inode = page->mapping->host;
  1795. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1796. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1797. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1798. struct bio *bio;
  1799. int num_copies;
  1800. int ret;
  1801. int read_mode;
  1802. u64 logical;
  1803. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  1804. ret = get_state_private(failure_tree, start, &private);
  1805. if (ret) {
  1806. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1807. if (!failrec)
  1808. return -ENOMEM;
  1809. failrec->start = start;
  1810. failrec->len = end - start + 1;
  1811. failrec->this_mirror = 0;
  1812. failrec->bio_flags = 0;
  1813. failrec->in_validation = 0;
  1814. read_lock(&em_tree->lock);
  1815. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1816. if (!em) {
  1817. read_unlock(&em_tree->lock);
  1818. kfree(failrec);
  1819. return -EIO;
  1820. }
  1821. if (em->start > start || em->start + em->len < start) {
  1822. free_extent_map(em);
  1823. em = NULL;
  1824. }
  1825. read_unlock(&em_tree->lock);
  1826. if (!em || IS_ERR(em)) {
  1827. kfree(failrec);
  1828. return -EIO;
  1829. }
  1830. logical = start - em->start;
  1831. logical = em->block_start + logical;
  1832. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1833. logical = em->block_start;
  1834. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1835. extent_set_compress_type(&failrec->bio_flags,
  1836. em->compress_type);
  1837. }
  1838. pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
  1839. "len=%llu\n", logical, start, failrec->len);
  1840. failrec->logical = logical;
  1841. free_extent_map(em);
  1842. /* set the bits in the private failure tree */
  1843. ret = set_extent_bits(failure_tree, start, end,
  1844. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1845. if (ret >= 0)
  1846. ret = set_state_private(failure_tree, start,
  1847. (u64)(unsigned long)failrec);
  1848. /* set the bits in the inode's tree */
  1849. if (ret >= 0)
  1850. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
  1851. GFP_NOFS);
  1852. if (ret < 0) {
  1853. kfree(failrec);
  1854. return ret;
  1855. }
  1856. } else {
  1857. failrec = (struct io_failure_record *)(unsigned long)private;
  1858. pr_debug("bio_readpage_error: (found) logical=%llu, "
  1859. "start=%llu, len=%llu, validation=%d\n",
  1860. failrec->logical, failrec->start, failrec->len,
  1861. failrec->in_validation);
  1862. /*
  1863. * when data can be on disk more than twice, add to failrec here
  1864. * (e.g. with a list for failed_mirror) to make
  1865. * clean_io_failure() clean all those errors at once.
  1866. */
  1867. }
  1868. num_copies = btrfs_num_copies(
  1869. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1870. failrec->logical, failrec->len);
  1871. if (num_copies == 1) {
  1872. /*
  1873. * we only have a single copy of the data, so don't bother with
  1874. * all the retry and error correction code that follows. no
  1875. * matter what the error is, it is very likely to persist.
  1876. */
  1877. pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
  1878. "state=%p, num_copies=%d, next_mirror %d, "
  1879. "failed_mirror %d\n", state, num_copies,
  1880. failrec->this_mirror, failed_mirror);
  1881. free_io_failure(inode, failrec, 0);
  1882. return -EIO;
  1883. }
  1884. if (!state) {
  1885. spin_lock(&tree->lock);
  1886. state = find_first_extent_bit_state(tree, failrec->start,
  1887. EXTENT_LOCKED);
  1888. if (state && state->start != failrec->start)
  1889. state = NULL;
  1890. spin_unlock(&tree->lock);
  1891. }
  1892. /*
  1893. * there are two premises:
  1894. * a) deliver good data to the caller
  1895. * b) correct the bad sectors on disk
  1896. */
  1897. if (failed_bio->bi_vcnt > 1) {
  1898. /*
  1899. * to fulfill b), we need to know the exact failing sectors, as
  1900. * we don't want to rewrite any more than the failed ones. thus,
  1901. * we need separate read requests for the failed bio
  1902. *
  1903. * if the following BUG_ON triggers, our validation request got
  1904. * merged. we need separate requests for our algorithm to work.
  1905. */
  1906. BUG_ON(failrec->in_validation);
  1907. failrec->in_validation = 1;
  1908. failrec->this_mirror = failed_mirror;
  1909. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  1910. } else {
  1911. /*
  1912. * we're ready to fulfill a) and b) alongside. get a good copy
  1913. * of the failed sector and if we succeed, we have setup
  1914. * everything for repair_io_failure to do the rest for us.
  1915. */
  1916. if (failrec->in_validation) {
  1917. BUG_ON(failrec->this_mirror != failed_mirror);
  1918. failrec->in_validation = 0;
  1919. failrec->this_mirror = 0;
  1920. }
  1921. failrec->failed_mirror = failed_mirror;
  1922. failrec->this_mirror++;
  1923. if (failrec->this_mirror == failed_mirror)
  1924. failrec->this_mirror++;
  1925. read_mode = READ_SYNC;
  1926. }
  1927. if (!state || failrec->this_mirror > num_copies) {
  1928. pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
  1929. "next_mirror %d, failed_mirror %d\n", state,
  1930. num_copies, failrec->this_mirror, failed_mirror);
  1931. free_io_failure(inode, failrec, 0);
  1932. return -EIO;
  1933. }
  1934. bio = bio_alloc(GFP_NOFS, 1);
  1935. bio->bi_private = state;
  1936. bio->bi_end_io = failed_bio->bi_end_io;
  1937. bio->bi_sector = failrec->logical >> 9;
  1938. bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  1939. bio->bi_size = 0;
  1940. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1941. pr_debug("bio_readpage_error: submitting new read[%#x] to "
  1942. "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
  1943. failrec->this_mirror, num_copies, failrec->in_validation);
  1944. ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
  1945. failrec->this_mirror,
  1946. failrec->bio_flags, 0);
  1947. return ret;
  1948. }
  1949. /* lots and lots of room for performance fixes in the end_bio funcs */
  1950. int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  1951. {
  1952. int uptodate = (err == 0);
  1953. struct extent_io_tree *tree;
  1954. int ret;
  1955. tree = &BTRFS_I(page->mapping->host)->io_tree;
  1956. if (tree->ops && tree->ops->writepage_end_io_hook) {
  1957. ret = tree->ops->writepage_end_io_hook(page, start,
  1958. end, NULL, uptodate);
  1959. if (ret)
  1960. uptodate = 0;
  1961. }
  1962. if (!uptodate && tree->ops &&
  1963. tree->ops->writepage_io_failed_hook) {
  1964. ret = tree->ops->writepage_io_failed_hook(NULL, page,
  1965. start, end, NULL);
  1966. /* Writeback already completed */
  1967. if (ret == 0)
  1968. return 1;
  1969. }
  1970. if (!uptodate) {
  1971. clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
  1972. ClearPageUptodate(page);
  1973. SetPageError(page);
  1974. }
  1975. return 0;
  1976. }
  1977. /*
  1978. * after a writepage IO is done, we need to:
  1979. * clear the uptodate bits on error
  1980. * clear the writeback bits in the extent tree for this IO
  1981. * end_page_writeback if the page has no more pending IO
  1982. *
  1983. * Scheduling is not allowed, so the extent state tree is expected
  1984. * to have one and only one object corresponding to this IO.
  1985. */
  1986. static void end_bio_extent_writepage(struct bio *bio, int err)
  1987. {
  1988. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1989. struct extent_io_tree *tree;
  1990. u64 start;
  1991. u64 end;
  1992. int whole_page;
  1993. do {
  1994. struct page *page = bvec->bv_page;
  1995. tree = &BTRFS_I(page->mapping->host)->io_tree;
  1996. start = ((u64)page->index << PAGE_CACHE_SHIFT) +
  1997. bvec->bv_offset;
  1998. end = start + bvec->bv_len - 1;
  1999. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  2000. whole_page = 1;
  2001. else
  2002. whole_page = 0;
  2003. if (--bvec >= bio->bi_io_vec)
  2004. prefetchw(&bvec->bv_page->flags);
  2005. if (end_extent_writepage(page, err, start, end))
  2006. continue;
  2007. if (whole_page)
  2008. end_page_writeback(page);
  2009. else
  2010. check_page_writeback(tree, page);
  2011. } while (bvec >= bio->bi_io_vec);
  2012. bio_put(bio);
  2013. }
  2014. /*
  2015. * after a readpage IO is done, we need to:
  2016. * clear the uptodate bits on error
  2017. * set the uptodate bits if things worked
  2018. * set the page up to date if all extents in the tree are uptodate
  2019. * clear the lock bit in the extent tree
  2020. * unlock the page if there are no other extents locked for it
  2021. *
  2022. * Scheduling is not allowed, so the extent state tree is expected
  2023. * to have one and only one object corresponding to this IO.
  2024. */
  2025. static void end_bio_extent_readpage(struct bio *bio, int err)
  2026. {
  2027. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  2028. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  2029. struct bio_vec *bvec = bio->bi_io_vec;
  2030. struct extent_io_tree *tree;
  2031. u64 start;
  2032. u64 end;
  2033. int whole_page;
  2034. int ret;
  2035. if (err)
  2036. uptodate = 0;
  2037. do {
  2038. struct page *page = bvec->bv_page;
  2039. struct extent_state *cached = NULL;
  2040. struct extent_state *state;
  2041. pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
  2042. "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
  2043. (long int)bio->bi_bdev);
  2044. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2045. start = ((u64)page->index << PAGE_CACHE_SHIFT) +
  2046. bvec->bv_offset;
  2047. end = start + bvec->bv_len - 1;
  2048. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  2049. whole_page = 1;
  2050. else
  2051. whole_page = 0;
  2052. if (++bvec <= bvec_end)
  2053. prefetchw(&bvec->bv_page->flags);
  2054. spin_lock(&tree->lock);
  2055. state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
  2056. if (state && state->start == start) {
  2057. /*
  2058. * take a reference on the state, unlock will drop
  2059. * the ref
  2060. */
  2061. cache_state(state, &cached);
  2062. }
  2063. spin_unlock(&tree->lock);
  2064. if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
  2065. ret = tree->ops->readpage_end_io_hook(page, start, end,
  2066. state);
  2067. if (ret)
  2068. uptodate = 0;
  2069. else
  2070. clean_io_failure(start, page);
  2071. }
  2072. if (!uptodate) {
  2073. int failed_mirror;
  2074. failed_mirror = (int)(unsigned long)bio->bi_bdev;
  2075. /*
  2076. * The generic bio_readpage_error handles errors the
  2077. * following way: If possible, new read requests are
  2078. * created and submitted and will end up in
  2079. * end_bio_extent_readpage as well (if we're lucky, not
  2080. * in the !uptodate case). In that case it returns 0 and
  2081. * we just go on with the next page in our bio. If it
  2082. * can't handle the error it will return -EIO and we
  2083. * remain responsible for that page.
  2084. */
  2085. ret = bio_readpage_error(bio, page, start, end,
  2086. failed_mirror, NULL);
  2087. if (ret == 0) {
  2088. error_handled:
  2089. uptodate =
  2090. test_bit(BIO_UPTODATE, &bio->bi_flags);
  2091. if (err)
  2092. uptodate = 0;
  2093. uncache_state(&cached);
  2094. continue;
  2095. }
  2096. if (tree->ops && tree->ops->readpage_io_failed_hook) {
  2097. ret = tree->ops->readpage_io_failed_hook(
  2098. bio, page, start, end,
  2099. failed_mirror, state);
  2100. if (ret == 0)
  2101. goto error_handled;
  2102. }
  2103. }
  2104. if (uptodate) {
  2105. set_extent_uptodate(tree, start, end, &cached,
  2106. GFP_ATOMIC);
  2107. }
  2108. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2109. if (whole_page) {
  2110. if (uptodate) {
  2111. SetPageUptodate(page);
  2112. } else {
  2113. ClearPageUptodate(page);
  2114. SetPageError(page);
  2115. }
  2116. unlock_page(page);
  2117. } else {
  2118. if (uptodate) {
  2119. check_page_uptodate(tree, page);
  2120. } else {
  2121. ClearPageUptodate(page);
  2122. SetPageError(page);
  2123. }
  2124. check_page_locked(tree, page);
  2125. }
  2126. } while (bvec <= bvec_end);
  2127. bio_put(bio);
  2128. }
  2129. struct bio *
  2130. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2131. gfp_t gfp_flags)
  2132. {
  2133. struct bio *bio;
  2134. bio = bio_alloc(gfp_flags, nr_vecs);
  2135. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2136. while (!bio && (nr_vecs /= 2))
  2137. bio = bio_alloc(gfp_flags, nr_vecs);
  2138. }
  2139. if (bio) {
  2140. bio->bi_size = 0;
  2141. bio->bi_bdev = bdev;
  2142. bio->bi_sector = first_sector;
  2143. }
  2144. return bio;
  2145. }
  2146. static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
  2147. unsigned long bio_flags)
  2148. {
  2149. int ret = 0;
  2150. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2151. struct page *page = bvec->bv_page;
  2152. struct extent_io_tree *tree = bio->bi_private;
  2153. u64 start;
  2154. start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
  2155. bio->bi_private = NULL;
  2156. bio_get(bio);
  2157. if (tree->ops && tree->ops->submit_bio_hook)
  2158. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2159. mirror_num, bio_flags, start);
  2160. else
  2161. btrfsic_submit_bio(rw, bio);
  2162. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  2163. ret = -EOPNOTSUPP;
  2164. bio_put(bio);
  2165. return ret;
  2166. }
  2167. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  2168. struct page *page, sector_t sector,
  2169. size_t size, unsigned long offset,
  2170. struct block_device *bdev,
  2171. struct bio **bio_ret,
  2172. unsigned long max_pages,
  2173. bio_end_io_t end_io_func,
  2174. int mirror_num,
  2175. unsigned long prev_bio_flags,
  2176. unsigned long bio_flags)
  2177. {
  2178. int ret = 0;
  2179. struct bio *bio;
  2180. int nr;
  2181. int contig = 0;
  2182. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  2183. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2184. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  2185. if (bio_ret && *bio_ret) {
  2186. bio = *bio_ret;
  2187. if (old_compressed)
  2188. contig = bio->bi_sector == sector;
  2189. else
  2190. contig = bio->bi_sector + (bio->bi_size >> 9) ==
  2191. sector;
  2192. if (prev_bio_flags != bio_flags || !contig ||
  2193. (tree->ops && tree->ops->merge_bio_hook &&
  2194. tree->ops->merge_bio_hook(page, offset, page_size, bio,
  2195. bio_flags)) ||
  2196. bio_add_page(bio, page, page_size, offset) < page_size) {
  2197. ret = submit_one_bio(rw, bio, mirror_num,
  2198. prev_bio_flags);
  2199. bio = NULL;
  2200. } else {
  2201. return 0;
  2202. }
  2203. }
  2204. if (this_compressed)
  2205. nr = BIO_MAX_PAGES;
  2206. else
  2207. nr = bio_get_nr_vecs(bdev);
  2208. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  2209. if (!bio)
  2210. return -ENOMEM;
  2211. bio_add_page(bio, page, page_size, offset);
  2212. bio->bi_end_io = end_io_func;
  2213. bio->bi_private = tree;
  2214. if (bio_ret)
  2215. *bio_ret = bio;
  2216. else
  2217. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2218. return ret;
  2219. }
  2220. void set_page_extent_mapped(struct page *page)
  2221. {
  2222. if (!PagePrivate(page)) {
  2223. SetPagePrivate(page);
  2224. page_cache_get(page);
  2225. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2226. }
  2227. }
  2228. static void set_page_extent_head(struct page *page, unsigned long len)
  2229. {
  2230. WARN_ON(!PagePrivate(page));
  2231. set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
  2232. }
  2233. /*
  2234. * basic readpage implementation. Locked extent state structs are inserted
  2235. * into the tree that are removed when the IO is done (by the end_io
  2236. * handlers)
  2237. */
  2238. static int __extent_read_full_page(struct extent_io_tree *tree,
  2239. struct page *page,
  2240. get_extent_t *get_extent,
  2241. struct bio **bio, int mirror_num,
  2242. unsigned long *bio_flags)
  2243. {
  2244. struct inode *inode = page->mapping->host;
  2245. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2246. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2247. u64 end;
  2248. u64 cur = start;
  2249. u64 extent_offset;
  2250. u64 last_byte = i_size_read(inode);
  2251. u64 block_start;
  2252. u64 cur_end;
  2253. sector_t sector;
  2254. struct extent_map *em;
  2255. struct block_device *bdev;
  2256. struct btrfs_ordered_extent *ordered;
  2257. int ret;
  2258. int nr = 0;
  2259. size_t pg_offset = 0;
  2260. size_t iosize;
  2261. size_t disk_io_size;
  2262. size_t blocksize = inode->i_sb->s_blocksize;
  2263. unsigned long this_bio_flag = 0;
  2264. set_page_extent_mapped(page);
  2265. if (!PageUptodate(page)) {
  2266. if (cleancache_get_page(page) == 0) {
  2267. BUG_ON(blocksize != PAGE_SIZE);
  2268. goto out;
  2269. }
  2270. }
  2271. end = page_end;
  2272. while (1) {
  2273. lock_extent(tree, start, end, GFP_NOFS);
  2274. ordered = btrfs_lookup_ordered_extent(inode, start);
  2275. if (!ordered)
  2276. break;
  2277. unlock_extent(tree, start, end, GFP_NOFS);
  2278. btrfs_start_ordered_extent(inode, ordered, 1);
  2279. btrfs_put_ordered_extent(ordered);
  2280. }
  2281. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  2282. char *userpage;
  2283. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  2284. if (zero_offset) {
  2285. iosize = PAGE_CACHE_SIZE - zero_offset;
  2286. userpage = kmap_atomic(page);
  2287. memset(userpage + zero_offset, 0, iosize);
  2288. flush_dcache_page(page);
  2289. kunmap_atomic(userpage);
  2290. }
  2291. }
  2292. while (cur <= end) {
  2293. if (cur >= last_byte) {
  2294. char *userpage;
  2295. struct extent_state *cached = NULL;
  2296. iosize = PAGE_CACHE_SIZE - pg_offset;
  2297. userpage = kmap_atomic(page);
  2298. memset(userpage + pg_offset, 0, iosize);
  2299. flush_dcache_page(page);
  2300. kunmap_atomic(userpage);
  2301. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2302. &cached, GFP_NOFS);
  2303. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2304. &cached, GFP_NOFS);
  2305. break;
  2306. }
  2307. em = get_extent(inode, page, pg_offset, cur,
  2308. end - cur + 1, 0);
  2309. if (IS_ERR_OR_NULL(em)) {
  2310. SetPageError(page);
  2311. unlock_extent(tree, cur, end, GFP_NOFS);
  2312. break;
  2313. }
  2314. extent_offset = cur - em->start;
  2315. BUG_ON(extent_map_end(em) <= cur);
  2316. BUG_ON(end < cur);
  2317. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2318. this_bio_flag = EXTENT_BIO_COMPRESSED;
  2319. extent_set_compress_type(&this_bio_flag,
  2320. em->compress_type);
  2321. }
  2322. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2323. cur_end = min(extent_map_end(em) - 1, end);
  2324. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  2325. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2326. disk_io_size = em->block_len;
  2327. sector = em->block_start >> 9;
  2328. } else {
  2329. sector = (em->block_start + extent_offset) >> 9;
  2330. disk_io_size = iosize;
  2331. }
  2332. bdev = em->bdev;
  2333. block_start = em->block_start;
  2334. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2335. block_start = EXTENT_MAP_HOLE;
  2336. free_extent_map(em);
  2337. em = NULL;
  2338. /* we've found a hole, just zero and go on */
  2339. if (block_start == EXTENT_MAP_HOLE) {
  2340. char *userpage;
  2341. struct extent_state *cached = NULL;
  2342. userpage = kmap_atomic(page);
  2343. memset(userpage + pg_offset, 0, iosize);
  2344. flush_dcache_page(page);
  2345. kunmap_atomic(userpage);
  2346. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2347. &cached, GFP_NOFS);
  2348. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2349. &cached, GFP_NOFS);
  2350. cur = cur + iosize;
  2351. pg_offset += iosize;
  2352. continue;
  2353. }
  2354. /* the get_extent function already copied into the page */
  2355. if (test_range_bit(tree, cur, cur_end,
  2356. EXTENT_UPTODATE, 1, NULL)) {
  2357. check_page_uptodate(tree, page);
  2358. unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
  2359. cur = cur + iosize;
  2360. pg_offset += iosize;
  2361. continue;
  2362. }
  2363. /* we have an inline extent but it didn't get marked up
  2364. * to date. Error out
  2365. */
  2366. if (block_start == EXTENT_MAP_INLINE) {
  2367. SetPageError(page);
  2368. unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
  2369. cur = cur + iosize;
  2370. pg_offset += iosize;
  2371. continue;
  2372. }
  2373. ret = 0;
  2374. if (tree->ops && tree->ops->readpage_io_hook) {
  2375. ret = tree->ops->readpage_io_hook(page, cur,
  2376. cur + iosize - 1);
  2377. }
  2378. if (!ret) {
  2379. unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
  2380. pnr -= page->index;
  2381. ret = submit_extent_page(READ, tree, page,
  2382. sector, disk_io_size, pg_offset,
  2383. bdev, bio, pnr,
  2384. end_bio_extent_readpage, mirror_num,
  2385. *bio_flags,
  2386. this_bio_flag);
  2387. nr++;
  2388. *bio_flags = this_bio_flag;
  2389. }
  2390. if (ret)
  2391. SetPageError(page);
  2392. cur = cur + iosize;
  2393. pg_offset += iosize;
  2394. }
  2395. out:
  2396. if (!nr) {
  2397. if (!PageError(page))
  2398. SetPageUptodate(page);
  2399. unlock_page(page);
  2400. }
  2401. return 0;
  2402. }
  2403. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2404. get_extent_t *get_extent, int mirror_num)
  2405. {
  2406. struct bio *bio = NULL;
  2407. unsigned long bio_flags = 0;
  2408. int ret;
  2409. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2410. &bio_flags);
  2411. if (bio)
  2412. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2413. return ret;
  2414. }
  2415. static noinline void update_nr_written(struct page *page,
  2416. struct writeback_control *wbc,
  2417. unsigned long nr_written)
  2418. {
  2419. wbc->nr_to_write -= nr_written;
  2420. if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
  2421. wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
  2422. page->mapping->writeback_index = page->index + nr_written;
  2423. }
  2424. /*
  2425. * the writepage semantics are similar to regular writepage. extent
  2426. * records are inserted to lock ranges in the tree, and as dirty areas
  2427. * are found, they are marked writeback. Then the lock bits are removed
  2428. * and the end_io handler clears the writeback ranges
  2429. */
  2430. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  2431. void *data)
  2432. {
  2433. struct inode *inode = page->mapping->host;
  2434. struct extent_page_data *epd = data;
  2435. struct extent_io_tree *tree = epd->tree;
  2436. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2437. u64 delalloc_start;
  2438. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2439. u64 end;
  2440. u64 cur = start;
  2441. u64 extent_offset;
  2442. u64 last_byte = i_size_read(inode);
  2443. u64 block_start;
  2444. u64 iosize;
  2445. sector_t sector;
  2446. struct extent_state *cached_state = NULL;
  2447. struct extent_map *em;
  2448. struct block_device *bdev;
  2449. int ret;
  2450. int nr = 0;
  2451. size_t pg_offset = 0;
  2452. size_t blocksize;
  2453. loff_t i_size = i_size_read(inode);
  2454. unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
  2455. u64 nr_delalloc;
  2456. u64 delalloc_end;
  2457. int page_started;
  2458. int compressed;
  2459. int write_flags;
  2460. unsigned long nr_written = 0;
  2461. bool fill_delalloc = true;
  2462. if (wbc->sync_mode == WB_SYNC_ALL)
  2463. write_flags = WRITE_SYNC;
  2464. else
  2465. write_flags = WRITE;
  2466. trace___extent_writepage(page, inode, wbc);
  2467. WARN_ON(!PageLocked(page));
  2468. ClearPageError(page);
  2469. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  2470. if (page->index > end_index ||
  2471. (page->index == end_index && !pg_offset)) {
  2472. page->mapping->a_ops->invalidatepage(page, 0);
  2473. unlock_page(page);
  2474. return 0;
  2475. }
  2476. if (page->index == end_index) {
  2477. char *userpage;
  2478. userpage = kmap_atomic(page);
  2479. memset(userpage + pg_offset, 0,
  2480. PAGE_CACHE_SIZE - pg_offset);
  2481. kunmap_atomic(userpage);
  2482. flush_dcache_page(page);
  2483. }
  2484. pg_offset = 0;
  2485. set_page_extent_mapped(page);
  2486. if (!tree->ops || !tree->ops->fill_delalloc)
  2487. fill_delalloc = false;
  2488. delalloc_start = start;
  2489. delalloc_end = 0;
  2490. page_started = 0;
  2491. if (!epd->extent_locked && fill_delalloc) {
  2492. u64 delalloc_to_write = 0;
  2493. /*
  2494. * make sure the wbc mapping index is at least updated
  2495. * to this page.
  2496. */
  2497. update_nr_written(page, wbc, 0);
  2498. while (delalloc_end < page_end) {
  2499. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2500. page,
  2501. &delalloc_start,
  2502. &delalloc_end,
  2503. 128 * 1024 * 1024);
  2504. if (nr_delalloc == 0) {
  2505. delalloc_start = delalloc_end + 1;
  2506. continue;
  2507. }
  2508. ret = tree->ops->fill_delalloc(inode, page,
  2509. delalloc_start,
  2510. delalloc_end,
  2511. &page_started,
  2512. &nr_written);
  2513. BUG_ON(ret);
  2514. /*
  2515. * delalloc_end is already one less than the total
  2516. * length, so we don't subtract one from
  2517. * PAGE_CACHE_SIZE
  2518. */
  2519. delalloc_to_write += (delalloc_end - delalloc_start +
  2520. PAGE_CACHE_SIZE) >>
  2521. PAGE_CACHE_SHIFT;
  2522. delalloc_start = delalloc_end + 1;
  2523. }
  2524. if (wbc->nr_to_write < delalloc_to_write) {
  2525. int thresh = 8192;
  2526. if (delalloc_to_write < thresh * 2)
  2527. thresh = delalloc_to_write;
  2528. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2529. thresh);
  2530. }
  2531. /* did the fill delalloc function already unlock and start
  2532. * the IO?
  2533. */
  2534. if (page_started) {
  2535. ret = 0;
  2536. /*
  2537. * we've unlocked the page, so we can't update
  2538. * the mapping's writeback index, just update
  2539. * nr_to_write.
  2540. */
  2541. wbc->nr_to_write -= nr_written;
  2542. goto done_unlocked;
  2543. }
  2544. }
  2545. if (tree->ops && tree->ops->writepage_start_hook) {
  2546. ret = tree->ops->writepage_start_hook(page, start,
  2547. page_end);
  2548. if (ret) {
  2549. /* Fixup worker will requeue */
  2550. if (ret == -EBUSY)
  2551. wbc->pages_skipped++;
  2552. else
  2553. redirty_page_for_writepage(wbc, page);
  2554. update_nr_written(page, wbc, nr_written);
  2555. unlock_page(page);
  2556. ret = 0;
  2557. goto done_unlocked;
  2558. }
  2559. }
  2560. /*
  2561. * we don't want to touch the inode after unlocking the page,
  2562. * so we update the mapping writeback index now
  2563. */
  2564. update_nr_written(page, wbc, nr_written + 1);
  2565. end = page_end;
  2566. if (last_byte <= start) {
  2567. if (tree->ops && tree->ops->writepage_end_io_hook)
  2568. tree->ops->writepage_end_io_hook(page, start,
  2569. page_end, NULL, 1);
  2570. goto done;
  2571. }
  2572. blocksize = inode->i_sb->s_blocksize;
  2573. while (cur <= end) {
  2574. if (cur >= last_byte) {
  2575. if (tree->ops && tree->ops->writepage_end_io_hook)
  2576. tree->ops->writepage_end_io_hook(page, cur,
  2577. page_end, NULL, 1);
  2578. break;
  2579. }
  2580. em = epd->get_extent(inode, page, pg_offset, cur,
  2581. end - cur + 1, 1);
  2582. if (IS_ERR_OR_NULL(em)) {
  2583. SetPageError(page);
  2584. break;
  2585. }
  2586. extent_offset = cur - em->start;
  2587. BUG_ON(extent_map_end(em) <= cur);
  2588. BUG_ON(end < cur);
  2589. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2590. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  2591. sector = (em->block_start + extent_offset) >> 9;
  2592. bdev = em->bdev;
  2593. block_start = em->block_start;
  2594. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  2595. free_extent_map(em);
  2596. em = NULL;
  2597. /*
  2598. * compressed and inline extents are written through other
  2599. * paths in the FS
  2600. */
  2601. if (compressed || block_start == EXTENT_MAP_HOLE ||
  2602. block_start == EXTENT_MAP_INLINE) {
  2603. /*
  2604. * end_io notification does not happen here for
  2605. * compressed extents
  2606. */
  2607. if (!compressed && tree->ops &&
  2608. tree->ops->writepage_end_io_hook)
  2609. tree->ops->writepage_end_io_hook(page, cur,
  2610. cur + iosize - 1,
  2611. NULL, 1);
  2612. else if (compressed) {
  2613. /* we don't want to end_page_writeback on
  2614. * a compressed extent. this happens
  2615. * elsewhere
  2616. */
  2617. nr++;
  2618. }
  2619. cur += iosize;
  2620. pg_offset += iosize;
  2621. continue;
  2622. }
  2623. /* leave this out until we have a page_mkwrite call */
  2624. if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
  2625. EXTENT_DIRTY, 0, NULL)) {
  2626. cur = cur + iosize;
  2627. pg_offset += iosize;
  2628. continue;
  2629. }
  2630. if (tree->ops && tree->ops->writepage_io_hook) {
  2631. ret = tree->ops->writepage_io_hook(page, cur,
  2632. cur + iosize - 1);
  2633. } else {
  2634. ret = 0;
  2635. }
  2636. if (ret) {
  2637. SetPageError(page);
  2638. } else {
  2639. unsigned long max_nr = end_index + 1;
  2640. set_range_writeback(tree, cur, cur + iosize - 1);
  2641. if (!PageWriteback(page)) {
  2642. printk(KERN_ERR "btrfs warning page %lu not "
  2643. "writeback, cur %llu end %llu\n",
  2644. page->index, (unsigned long long)cur,
  2645. (unsigned long long)end);
  2646. }
  2647. ret = submit_extent_page(write_flags, tree, page,
  2648. sector, iosize, pg_offset,
  2649. bdev, &epd->bio, max_nr,
  2650. end_bio_extent_writepage,
  2651. 0, 0, 0);
  2652. if (ret)
  2653. SetPageError(page);
  2654. }
  2655. cur = cur + iosize;
  2656. pg_offset += iosize;
  2657. nr++;
  2658. }
  2659. done:
  2660. if (nr == 0) {
  2661. /* make sure the mapping tag for page dirty gets cleared */
  2662. set_page_writeback(page);
  2663. end_page_writeback(page);
  2664. }
  2665. unlock_page(page);
  2666. done_unlocked:
  2667. /* drop our reference on any cached states */
  2668. free_extent_state(cached_state);
  2669. return 0;
  2670. }
  2671. /**
  2672. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  2673. * @mapping: address space structure to write
  2674. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  2675. * @writepage: function called for each page
  2676. * @data: data passed to writepage function
  2677. *
  2678. * If a page is already under I/O, write_cache_pages() skips it, even
  2679. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  2680. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  2681. * and msync() need to guarantee that all the data which was dirty at the time
  2682. * the call was made get new I/O started against them. If wbc->sync_mode is
  2683. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  2684. * existing IO to complete.
  2685. */
  2686. static int extent_write_cache_pages(struct extent_io_tree *tree,
  2687. struct address_space *mapping,
  2688. struct writeback_control *wbc,
  2689. writepage_t writepage, void *data,
  2690. void (*flush_fn)(void *))
  2691. {
  2692. int ret = 0;
  2693. int done = 0;
  2694. int nr_to_write_done = 0;
  2695. struct pagevec pvec;
  2696. int nr_pages;
  2697. pgoff_t index;
  2698. pgoff_t end; /* Inclusive */
  2699. int scanned = 0;
  2700. int tag;
  2701. pagevec_init(&pvec, 0);
  2702. if (wbc->range_cyclic) {
  2703. index = mapping->writeback_index; /* Start from prev offset */
  2704. end = -1;
  2705. } else {
  2706. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2707. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2708. scanned = 1;
  2709. }
  2710. if (wbc->sync_mode == WB_SYNC_ALL)
  2711. tag = PAGECACHE_TAG_TOWRITE;
  2712. else
  2713. tag = PAGECACHE_TAG_DIRTY;
  2714. retry:
  2715. if (wbc->sync_mode == WB_SYNC_ALL)
  2716. tag_pages_for_writeback(mapping, index, end);
  2717. while (!done && !nr_to_write_done && (index <= end) &&
  2718. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  2719. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  2720. unsigned i;
  2721. scanned = 1;
  2722. for (i = 0; i < nr_pages; i++) {
  2723. struct page *page = pvec.pages[i];
  2724. /*
  2725. * At this point we hold neither mapping->tree_lock nor
  2726. * lock on the page itself: the page may be truncated or
  2727. * invalidated (changing page->mapping to NULL), or even
  2728. * swizzled back from swapper_space to tmpfs file
  2729. * mapping
  2730. */
  2731. if (tree->ops &&
  2732. tree->ops->write_cache_pages_lock_hook) {
  2733. tree->ops->write_cache_pages_lock_hook(page,
  2734. data, flush_fn);
  2735. } else {
  2736. if (!trylock_page(page)) {
  2737. flush_fn(data);
  2738. lock_page(page);
  2739. }
  2740. }
  2741. if (unlikely(page->mapping != mapping)) {
  2742. unlock_page(page);
  2743. continue;
  2744. }
  2745. if (!wbc->range_cyclic && page->index > end) {
  2746. done = 1;
  2747. unlock_page(page);
  2748. continue;
  2749. }
  2750. if (wbc->sync_mode != WB_SYNC_NONE) {
  2751. if (PageWriteback(page))
  2752. flush_fn(data);
  2753. wait_on_page_writeback(page);
  2754. }
  2755. if (PageWriteback(page) ||
  2756. !clear_page_dirty_for_io(page)) {
  2757. unlock_page(page);
  2758. continue;
  2759. }
  2760. ret = (*writepage)(page, wbc, data);
  2761. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  2762. unlock_page(page);
  2763. ret = 0;
  2764. }
  2765. if (ret)
  2766. done = 1;
  2767. /*
  2768. * the filesystem may choose to bump up nr_to_write.
  2769. * We have to make sure to honor the new nr_to_write
  2770. * at any time
  2771. */
  2772. nr_to_write_done = wbc->nr_to_write <= 0;
  2773. }
  2774. pagevec_release(&pvec);
  2775. cond_resched();
  2776. }
  2777. if (!scanned && !done) {
  2778. /*
  2779. * We hit the last page and there is more work to be done: wrap
  2780. * back to the start of the file
  2781. */
  2782. scanned = 1;
  2783. index = 0;
  2784. goto retry;
  2785. }
  2786. return ret;
  2787. }
  2788. static void flush_epd_write_bio(struct extent_page_data *epd)
  2789. {
  2790. if (epd->bio) {
  2791. if (epd->sync_io)
  2792. submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
  2793. else
  2794. submit_one_bio(WRITE, epd->bio, 0, 0);
  2795. epd->bio = NULL;
  2796. }
  2797. }
  2798. static noinline void flush_write_bio(void *data)
  2799. {
  2800. struct extent_page_data *epd = data;
  2801. flush_epd_write_bio(epd);
  2802. }
  2803. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  2804. get_extent_t *get_extent,
  2805. struct writeback_control *wbc)
  2806. {
  2807. int ret;
  2808. struct extent_page_data epd = {
  2809. .bio = NULL,
  2810. .tree = tree,
  2811. .get_extent = get_extent,
  2812. .extent_locked = 0,
  2813. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2814. };
  2815. ret = __extent_writepage(page, wbc, &epd);
  2816. flush_epd_write_bio(&epd);
  2817. return ret;
  2818. }
  2819. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  2820. u64 start, u64 end, get_extent_t *get_extent,
  2821. int mode)
  2822. {
  2823. int ret = 0;
  2824. struct address_space *mapping = inode->i_mapping;
  2825. struct page *page;
  2826. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  2827. PAGE_CACHE_SHIFT;
  2828. struct extent_page_data epd = {
  2829. .bio = NULL,
  2830. .tree = tree,
  2831. .get_extent = get_extent,
  2832. .extent_locked = 1,
  2833. .sync_io = mode == WB_SYNC_ALL,
  2834. };
  2835. struct writeback_control wbc_writepages = {
  2836. .sync_mode = mode,
  2837. .nr_to_write = nr_pages * 2,
  2838. .range_start = start,
  2839. .range_end = end + 1,
  2840. };
  2841. while (start <= end) {
  2842. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  2843. if (clear_page_dirty_for_io(page))
  2844. ret = __extent_writepage(page, &wbc_writepages, &epd);
  2845. else {
  2846. if (tree->ops && tree->ops->writepage_end_io_hook)
  2847. tree->ops->writepage_end_io_hook(page, start,
  2848. start + PAGE_CACHE_SIZE - 1,
  2849. NULL, 1);
  2850. unlock_page(page);
  2851. }
  2852. page_cache_release(page);
  2853. start += PAGE_CACHE_SIZE;
  2854. }
  2855. flush_epd_write_bio(&epd);
  2856. return ret;
  2857. }
  2858. int extent_writepages(struct extent_io_tree *tree,
  2859. struct address_space *mapping,
  2860. get_extent_t *get_extent,
  2861. struct writeback_control *wbc)
  2862. {
  2863. int ret = 0;
  2864. struct extent_page_data epd = {
  2865. .bio = NULL,
  2866. .tree = tree,
  2867. .get_extent = get_extent,
  2868. .extent_locked = 0,
  2869. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2870. };
  2871. ret = extent_write_cache_pages(tree, mapping, wbc,
  2872. __extent_writepage, &epd,
  2873. flush_write_bio);
  2874. flush_epd_write_bio(&epd);
  2875. return ret;
  2876. }
  2877. int extent_readpages(struct extent_io_tree *tree,
  2878. struct address_space *mapping,
  2879. struct list_head *pages, unsigned nr_pages,
  2880. get_extent_t get_extent)
  2881. {
  2882. struct bio *bio = NULL;
  2883. unsigned page_idx;
  2884. unsigned long bio_flags = 0;
  2885. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  2886. struct page *page = list_entry(pages->prev, struct page, lru);
  2887. prefetchw(&page->flags);
  2888. list_del(&page->lru);
  2889. if (!add_to_page_cache_lru(page, mapping,
  2890. page->index, GFP_NOFS)) {
  2891. __extent_read_full_page(tree, page, get_extent,
  2892. &bio, 0, &bio_flags);
  2893. }
  2894. page_cache_release(page);
  2895. }
  2896. BUG_ON(!list_empty(pages));
  2897. if (bio)
  2898. submit_one_bio(READ, bio, 0, bio_flags);
  2899. return 0;
  2900. }
  2901. /*
  2902. * basic invalidatepage code, this waits on any locked or writeback
  2903. * ranges corresponding to the page, and then deletes any extent state
  2904. * records from the tree
  2905. */
  2906. int extent_invalidatepage(struct extent_io_tree *tree,
  2907. struct page *page, unsigned long offset)
  2908. {
  2909. struct extent_state *cached_state = NULL;
  2910. u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
  2911. u64 end = start + PAGE_CACHE_SIZE - 1;
  2912. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  2913. start += (offset + blocksize - 1) & ~(blocksize - 1);
  2914. if (start > end)
  2915. return 0;
  2916. lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
  2917. wait_on_page_writeback(page);
  2918. clear_extent_bit(tree, start, end,
  2919. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  2920. EXTENT_DO_ACCOUNTING,
  2921. 1, 1, &cached_state, GFP_NOFS);
  2922. return 0;
  2923. }
  2924. /*
  2925. * a helper for releasepage, this tests for areas of the page that
  2926. * are locked or under IO and drops the related state bits if it is safe
  2927. * to drop the page.
  2928. */
  2929. int try_release_extent_state(struct extent_map_tree *map,
  2930. struct extent_io_tree *tree, struct page *page,
  2931. gfp_t mask)
  2932. {
  2933. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2934. u64 end = start + PAGE_CACHE_SIZE - 1;
  2935. int ret = 1;
  2936. if (test_range_bit(tree, start, end,
  2937. EXTENT_IOBITS, 0, NULL))
  2938. ret = 0;
  2939. else {
  2940. if ((mask & GFP_NOFS) == GFP_NOFS)
  2941. mask = GFP_NOFS;
  2942. /*
  2943. * at this point we can safely clear everything except the
  2944. * locked bit and the nodatasum bit
  2945. */
  2946. ret = clear_extent_bit(tree, start, end,
  2947. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  2948. 0, 0, NULL, mask);
  2949. /* if clear_extent_bit failed for enomem reasons,
  2950. * we can't allow the release to continue.
  2951. */
  2952. if (ret < 0)
  2953. ret = 0;
  2954. else
  2955. ret = 1;
  2956. }
  2957. return ret;
  2958. }
  2959. /*
  2960. * a helper for releasepage. As long as there are no locked extents
  2961. * in the range corresponding to the page, both state records and extent
  2962. * map records are removed
  2963. */
  2964. int try_release_extent_mapping(struct extent_map_tree *map,
  2965. struct extent_io_tree *tree, struct page *page,
  2966. gfp_t mask)
  2967. {
  2968. struct extent_map *em;
  2969. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2970. u64 end = start + PAGE_CACHE_SIZE - 1;
  2971. if ((mask & __GFP_WAIT) &&
  2972. page->mapping->host->i_size > 16 * 1024 * 1024) {
  2973. u64 len;
  2974. while (start <= end) {
  2975. len = end - start + 1;
  2976. write_lock(&map->lock);
  2977. em = lookup_extent_mapping(map, start, len);
  2978. if (!em) {
  2979. write_unlock(&map->lock);
  2980. break;
  2981. }
  2982. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  2983. em->start != start) {
  2984. write_unlock(&map->lock);
  2985. free_extent_map(em);
  2986. break;
  2987. }
  2988. if (!test_range_bit(tree, em->start,
  2989. extent_map_end(em) - 1,
  2990. EXTENT_LOCKED | EXTENT_WRITEBACK,
  2991. 0, NULL)) {
  2992. remove_extent_mapping(map, em);
  2993. /* once for the rb tree */
  2994. free_extent_map(em);
  2995. }
  2996. start = extent_map_end(em);
  2997. write_unlock(&map->lock);
  2998. /* once for us */
  2999. free_extent_map(em);
  3000. }
  3001. }
  3002. return try_release_extent_state(map, tree, page, mask);
  3003. }
  3004. /*
  3005. * helper function for fiemap, which doesn't want to see any holes.
  3006. * This maps until we find something past 'last'
  3007. */
  3008. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3009. u64 offset,
  3010. u64 last,
  3011. get_extent_t *get_extent)
  3012. {
  3013. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3014. struct extent_map *em;
  3015. u64 len;
  3016. if (offset >= last)
  3017. return NULL;
  3018. while(1) {
  3019. len = last - offset;
  3020. if (len == 0)
  3021. break;
  3022. len = (len + sectorsize - 1) & ~(sectorsize - 1);
  3023. em = get_extent(inode, NULL, 0, offset, len, 0);
  3024. if (IS_ERR_OR_NULL(em))
  3025. return em;
  3026. /* if this isn't a hole return it */
  3027. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3028. em->block_start != EXTENT_MAP_HOLE) {
  3029. return em;
  3030. }
  3031. /* this is a hole, advance to the next extent */
  3032. offset = extent_map_end(em);
  3033. free_extent_map(em);
  3034. if (offset >= last)
  3035. break;
  3036. }
  3037. return NULL;
  3038. }
  3039. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3040. __u64 start, __u64 len, get_extent_t *get_extent)
  3041. {
  3042. int ret = 0;
  3043. u64 off = start;
  3044. u64 max = start + len;
  3045. u32 flags = 0;
  3046. u32 found_type;
  3047. u64 last;
  3048. u64 last_for_get_extent = 0;
  3049. u64 disko = 0;
  3050. u64 isize = i_size_read(inode);
  3051. struct btrfs_key found_key;
  3052. struct extent_map *em = NULL;
  3053. struct extent_state *cached_state = NULL;
  3054. struct btrfs_path *path;
  3055. struct btrfs_file_extent_item *item;
  3056. int end = 0;
  3057. u64 em_start = 0;
  3058. u64 em_len = 0;
  3059. u64 em_end = 0;
  3060. unsigned long emflags;
  3061. if (len == 0)
  3062. return -EINVAL;
  3063. path = btrfs_alloc_path();
  3064. if (!path)
  3065. return -ENOMEM;
  3066. path->leave_spinning = 1;
  3067. start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
  3068. len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
  3069. /*
  3070. * lookup the last file extent. We're not using i_size here
  3071. * because there might be preallocation past i_size
  3072. */
  3073. ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
  3074. path, btrfs_ino(inode), -1, 0);
  3075. if (ret < 0) {
  3076. btrfs_free_path(path);
  3077. return ret;
  3078. }
  3079. WARN_ON(!ret);
  3080. path->slots[0]--;
  3081. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3082. struct btrfs_file_extent_item);
  3083. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3084. found_type = btrfs_key_type(&found_key);
  3085. /* No extents, but there might be delalloc bits */
  3086. if (found_key.objectid != btrfs_ino(inode) ||
  3087. found_type != BTRFS_EXTENT_DATA_KEY) {
  3088. /* have to trust i_size as the end */
  3089. last = (u64)-1;
  3090. last_for_get_extent = isize;
  3091. } else {
  3092. /*
  3093. * remember the start of the last extent. There are a
  3094. * bunch of different factors that go into the length of the
  3095. * extent, so its much less complex to remember where it started
  3096. */
  3097. last = found_key.offset;
  3098. last_for_get_extent = last + 1;
  3099. }
  3100. btrfs_free_path(path);
  3101. /*
  3102. * we might have some extents allocated but more delalloc past those
  3103. * extents. so, we trust isize unless the start of the last extent is
  3104. * beyond isize
  3105. */
  3106. if (last < isize) {
  3107. last = (u64)-1;
  3108. last_for_get_extent = isize;
  3109. }
  3110. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
  3111. &cached_state, GFP_NOFS);
  3112. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3113. get_extent);
  3114. if (!em)
  3115. goto out;
  3116. if (IS_ERR(em)) {
  3117. ret = PTR_ERR(em);
  3118. goto out;
  3119. }
  3120. while (!end) {
  3121. u64 offset_in_extent;
  3122. /* break if the extent we found is outside the range */
  3123. if (em->start >= max || extent_map_end(em) < off)
  3124. break;
  3125. /*
  3126. * get_extent may return an extent that starts before our
  3127. * requested range. We have to make sure the ranges
  3128. * we return to fiemap always move forward and don't
  3129. * overlap, so adjust the offsets here
  3130. */
  3131. em_start = max(em->start, off);
  3132. /*
  3133. * record the offset from the start of the extent
  3134. * for adjusting the disk offset below
  3135. */
  3136. offset_in_extent = em_start - em->start;
  3137. em_end = extent_map_end(em);
  3138. em_len = em_end - em_start;
  3139. emflags = em->flags;
  3140. disko = 0;
  3141. flags = 0;
  3142. /*
  3143. * bump off for our next call to get_extent
  3144. */
  3145. off = extent_map_end(em);
  3146. if (off >= max)
  3147. end = 1;
  3148. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3149. end = 1;
  3150. flags |= FIEMAP_EXTENT_LAST;
  3151. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3152. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  3153. FIEMAP_EXTENT_NOT_ALIGNED);
  3154. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  3155. flags |= (FIEMAP_EXTENT_DELALLOC |
  3156. FIEMAP_EXTENT_UNKNOWN);
  3157. } else {
  3158. disko = em->block_start + offset_in_extent;
  3159. }
  3160. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3161. flags |= FIEMAP_EXTENT_ENCODED;
  3162. free_extent_map(em);
  3163. em = NULL;
  3164. if ((em_start >= last) || em_len == (u64)-1 ||
  3165. (last == (u64)-1 && isize <= em_end)) {
  3166. flags |= FIEMAP_EXTENT_LAST;
  3167. end = 1;
  3168. }
  3169. /* now scan forward to see if this is really the last extent. */
  3170. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  3171. get_extent);
  3172. if (IS_ERR(em)) {
  3173. ret = PTR_ERR(em);
  3174. goto out;
  3175. }
  3176. if (!em) {
  3177. flags |= FIEMAP_EXTENT_LAST;
  3178. end = 1;
  3179. }
  3180. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  3181. em_len, flags);
  3182. if (ret)
  3183. goto out_free;
  3184. }
  3185. out_free:
  3186. free_extent_map(em);
  3187. out:
  3188. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
  3189. &cached_state, GFP_NOFS);
  3190. return ret;
  3191. }
  3192. inline struct page *extent_buffer_page(struct extent_buffer *eb,
  3193. unsigned long i)
  3194. {
  3195. struct page *p;
  3196. struct address_space *mapping;
  3197. if (i == 0)
  3198. return eb->first_page;
  3199. i += eb->start >> PAGE_CACHE_SHIFT;
  3200. mapping = eb->first_page->mapping;
  3201. if (!mapping)
  3202. return NULL;
  3203. /*
  3204. * extent_buffer_page is only called after pinning the page
  3205. * by increasing the reference count. So we know the page must
  3206. * be in the radix tree.
  3207. */
  3208. rcu_read_lock();
  3209. p = radix_tree_lookup(&mapping->page_tree, i);
  3210. rcu_read_unlock();
  3211. return p;
  3212. }
  3213. inline unsigned long num_extent_pages(u64 start, u64 len)
  3214. {
  3215. return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
  3216. (start >> PAGE_CACHE_SHIFT);
  3217. }
  3218. static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
  3219. u64 start,
  3220. unsigned long len,
  3221. gfp_t mask)
  3222. {
  3223. struct extent_buffer *eb = NULL;
  3224. #if LEAK_DEBUG
  3225. unsigned long flags;
  3226. #endif
  3227. eb = kmem_cache_zalloc(extent_buffer_cache, mask);
  3228. if (eb == NULL)
  3229. return NULL;
  3230. eb->start = start;
  3231. eb->len = len;
  3232. rwlock_init(&eb->lock);
  3233. atomic_set(&eb->write_locks, 0);
  3234. atomic_set(&eb->read_locks, 0);
  3235. atomic_set(&eb->blocking_readers, 0);
  3236. atomic_set(&eb->blocking_writers, 0);
  3237. atomic_set(&eb->spinning_readers, 0);
  3238. atomic_set(&eb->spinning_writers, 0);
  3239. eb->lock_nested = 0;
  3240. init_waitqueue_head(&eb->write_lock_wq);
  3241. init_waitqueue_head(&eb->read_lock_wq);
  3242. #if LEAK_DEBUG
  3243. spin_lock_irqsave(&leak_lock, flags);
  3244. list_add(&eb->leak_list, &buffers);
  3245. spin_unlock_irqrestore(&leak_lock, flags);
  3246. #endif
  3247. atomic_set(&eb->refs, 1);
  3248. return eb;
  3249. }
  3250. static void __free_extent_buffer(struct extent_buffer *eb)
  3251. {
  3252. #if LEAK_DEBUG
  3253. unsigned long flags;
  3254. spin_lock_irqsave(&leak_lock, flags);
  3255. list_del(&eb->leak_list);
  3256. spin_unlock_irqrestore(&leak_lock, flags);
  3257. #endif
  3258. kmem_cache_free(extent_buffer_cache, eb);
  3259. }
  3260. /*
  3261. * Helper for releasing extent buffer page.
  3262. */
  3263. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
  3264. unsigned long start_idx)
  3265. {
  3266. unsigned long index;
  3267. struct page *page;
  3268. if (!eb->first_page)
  3269. return;
  3270. index = num_extent_pages(eb->start, eb->len);
  3271. if (start_idx >= index)
  3272. return;
  3273. do {
  3274. index--;
  3275. page = extent_buffer_page(eb, index);
  3276. if (page)
  3277. page_cache_release(page);
  3278. } while (index != start_idx);
  3279. }
  3280. /*
  3281. * Helper for releasing the extent buffer.
  3282. */
  3283. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  3284. {
  3285. btrfs_release_extent_buffer_page(eb, 0);
  3286. __free_extent_buffer(eb);
  3287. }
  3288. struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
  3289. u64 start, unsigned long len,
  3290. struct page *page0)
  3291. {
  3292. unsigned long num_pages = num_extent_pages(start, len);
  3293. unsigned long i;
  3294. unsigned long index = start >> PAGE_CACHE_SHIFT;
  3295. struct extent_buffer *eb;
  3296. struct extent_buffer *exists = NULL;
  3297. struct page *p;
  3298. struct address_space *mapping = tree->mapping;
  3299. int uptodate = 1;
  3300. int ret;
  3301. rcu_read_lock();
  3302. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3303. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3304. rcu_read_unlock();
  3305. mark_page_accessed(eb->first_page);
  3306. return eb;
  3307. }
  3308. rcu_read_unlock();
  3309. eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
  3310. if (!eb)
  3311. return NULL;
  3312. if (page0) {
  3313. eb->first_page = page0;
  3314. i = 1;
  3315. index++;
  3316. page_cache_get(page0);
  3317. mark_page_accessed(page0);
  3318. set_page_extent_mapped(page0);
  3319. set_page_extent_head(page0, len);
  3320. uptodate = PageUptodate(page0);
  3321. } else {
  3322. i = 0;
  3323. }
  3324. for (; i < num_pages; i++, index++) {
  3325. p = find_or_create_page(mapping, index, GFP_NOFS);
  3326. if (!p) {
  3327. WARN_ON(1);
  3328. goto free_eb;
  3329. }
  3330. set_page_extent_mapped(p);
  3331. mark_page_accessed(p);
  3332. if (i == 0) {
  3333. eb->first_page = p;
  3334. set_page_extent_head(p, len);
  3335. } else {
  3336. set_page_private(p, EXTENT_PAGE_PRIVATE);
  3337. }
  3338. if (!PageUptodate(p))
  3339. uptodate = 0;
  3340. /*
  3341. * see below about how we avoid a nasty race with release page
  3342. * and why we unlock later
  3343. */
  3344. if (i != 0)
  3345. unlock_page(p);
  3346. }
  3347. if (uptodate)
  3348. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3349. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  3350. if (ret)
  3351. goto free_eb;
  3352. spin_lock(&tree->buffer_lock);
  3353. ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
  3354. if (ret == -EEXIST) {
  3355. exists = radix_tree_lookup(&tree->buffer,
  3356. start >> PAGE_CACHE_SHIFT);
  3357. /* add one reference for the caller */
  3358. atomic_inc(&exists->refs);
  3359. spin_unlock(&tree->buffer_lock);
  3360. radix_tree_preload_end();
  3361. goto free_eb;
  3362. }
  3363. /* add one reference for the tree */
  3364. atomic_inc(&eb->refs);
  3365. spin_unlock(&tree->buffer_lock);
  3366. radix_tree_preload_end();
  3367. /*
  3368. * there is a race where release page may have
  3369. * tried to find this extent buffer in the radix
  3370. * but failed. It will tell the VM it is safe to
  3371. * reclaim the, and it will clear the page private bit.
  3372. * We must make sure to set the page private bit properly
  3373. * after the extent buffer is in the radix tree so
  3374. * it doesn't get lost
  3375. */
  3376. set_page_extent_mapped(eb->first_page);
  3377. set_page_extent_head(eb->first_page, eb->len);
  3378. if (!page0)
  3379. unlock_page(eb->first_page);
  3380. return eb;
  3381. free_eb:
  3382. if (eb->first_page && !page0)
  3383. unlock_page(eb->first_page);
  3384. if (!atomic_dec_and_test(&eb->refs))
  3385. return exists;
  3386. btrfs_release_extent_buffer(eb);
  3387. return exists;
  3388. }
  3389. struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
  3390. u64 start, unsigned long len)
  3391. {
  3392. struct extent_buffer *eb;
  3393. rcu_read_lock();
  3394. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3395. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3396. rcu_read_unlock();
  3397. mark_page_accessed(eb->first_page);
  3398. return eb;
  3399. }
  3400. rcu_read_unlock();
  3401. return NULL;
  3402. }
  3403. void free_extent_buffer(struct extent_buffer *eb)
  3404. {
  3405. if (!eb)
  3406. return;
  3407. if (!atomic_dec_and_test(&eb->refs))
  3408. return;
  3409. WARN_ON(1);
  3410. }
  3411. int clear_extent_buffer_dirty(struct extent_io_tree *tree,
  3412. struct extent_buffer *eb)
  3413. {
  3414. unsigned long i;
  3415. unsigned long num_pages;
  3416. struct page *page;
  3417. num_pages = num_extent_pages(eb->start, eb->len);
  3418. for (i = 0; i < num_pages; i++) {
  3419. page = extent_buffer_page(eb, i);
  3420. if (!PageDirty(page))
  3421. continue;
  3422. lock_page(page);
  3423. WARN_ON(!PagePrivate(page));
  3424. set_page_extent_mapped(page);
  3425. if (i == 0)
  3426. set_page_extent_head(page, eb->len);
  3427. clear_page_dirty_for_io(page);
  3428. spin_lock_irq(&page->mapping->tree_lock);
  3429. if (!PageDirty(page)) {
  3430. radix_tree_tag_clear(&page->mapping->page_tree,
  3431. page_index(page),
  3432. PAGECACHE_TAG_DIRTY);
  3433. }
  3434. spin_unlock_irq(&page->mapping->tree_lock);
  3435. ClearPageError(page);
  3436. unlock_page(page);
  3437. }
  3438. return 0;
  3439. }
  3440. int set_extent_buffer_dirty(struct extent_io_tree *tree,
  3441. struct extent_buffer *eb)
  3442. {
  3443. unsigned long i;
  3444. unsigned long num_pages;
  3445. int was_dirty = 0;
  3446. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  3447. num_pages = num_extent_pages(eb->start, eb->len);
  3448. for (i = 0; i < num_pages; i++)
  3449. __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
  3450. return was_dirty;
  3451. }
  3452. static int __eb_straddles_pages(u64 start, u64 len)
  3453. {
  3454. if (len < PAGE_CACHE_SIZE)
  3455. return 1;
  3456. if (start & (PAGE_CACHE_SIZE - 1))
  3457. return 1;
  3458. if ((start + len) & (PAGE_CACHE_SIZE - 1))
  3459. return 1;
  3460. return 0;
  3461. }
  3462. static int eb_straddles_pages(struct extent_buffer *eb)
  3463. {
  3464. return __eb_straddles_pages(eb->start, eb->len);
  3465. }
  3466. int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
  3467. struct extent_buffer *eb,
  3468. struct extent_state **cached_state)
  3469. {
  3470. unsigned long i;
  3471. struct page *page;
  3472. unsigned long num_pages;
  3473. num_pages = num_extent_pages(eb->start, eb->len);
  3474. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3475. clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
  3476. cached_state, GFP_NOFS);
  3477. for (i = 0; i < num_pages; i++) {
  3478. page = extent_buffer_page(eb, i);
  3479. if (page)
  3480. ClearPageUptodate(page);
  3481. }
  3482. return 0;
  3483. }
  3484. int set_extent_buffer_uptodate(struct extent_io_tree *tree,
  3485. struct extent_buffer *eb)
  3486. {
  3487. unsigned long i;
  3488. struct page *page;
  3489. unsigned long num_pages;
  3490. num_pages = num_extent_pages(eb->start, eb->len);
  3491. if (eb_straddles_pages(eb)) {
  3492. set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
  3493. NULL, GFP_NOFS);
  3494. }
  3495. for (i = 0; i < num_pages; i++) {
  3496. page = extent_buffer_page(eb, i);
  3497. if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
  3498. ((i == num_pages - 1) &&
  3499. ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
  3500. check_page_uptodate(tree, page);
  3501. continue;
  3502. }
  3503. SetPageUptodate(page);
  3504. }
  3505. return 0;
  3506. }
  3507. int extent_range_uptodate(struct extent_io_tree *tree,
  3508. u64 start, u64 end)
  3509. {
  3510. struct page *page;
  3511. int ret;
  3512. int pg_uptodate = 1;
  3513. int uptodate;
  3514. unsigned long index;
  3515. if (__eb_straddles_pages(start, end - start + 1)) {
  3516. ret = test_range_bit(tree, start, end,
  3517. EXTENT_UPTODATE, 1, NULL);
  3518. if (ret)
  3519. return 1;
  3520. }
  3521. while (start <= end) {
  3522. index = start >> PAGE_CACHE_SHIFT;
  3523. page = find_get_page(tree->mapping, index);
  3524. if (!page)
  3525. return 1;
  3526. uptodate = PageUptodate(page);
  3527. page_cache_release(page);
  3528. if (!uptodate) {
  3529. pg_uptodate = 0;
  3530. break;
  3531. }
  3532. start += PAGE_CACHE_SIZE;
  3533. }
  3534. return pg_uptodate;
  3535. }
  3536. int extent_buffer_uptodate(struct extent_io_tree *tree,
  3537. struct extent_buffer *eb,
  3538. struct extent_state *cached_state)
  3539. {
  3540. int ret = 0;
  3541. unsigned long num_pages;
  3542. unsigned long i;
  3543. struct page *page;
  3544. int pg_uptodate = 1;
  3545. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  3546. return 1;
  3547. if (eb_straddles_pages(eb)) {
  3548. ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
  3549. EXTENT_UPTODATE, 1, cached_state);
  3550. if (ret)
  3551. return ret;
  3552. }
  3553. num_pages = num_extent_pages(eb->start, eb->len);
  3554. for (i = 0; i < num_pages; i++) {
  3555. page = extent_buffer_page(eb, i);
  3556. if (!PageUptodate(page)) {
  3557. pg_uptodate = 0;
  3558. break;
  3559. }
  3560. }
  3561. return pg_uptodate;
  3562. }
  3563. int read_extent_buffer_pages(struct extent_io_tree *tree,
  3564. struct extent_buffer *eb, u64 start, int wait,
  3565. get_extent_t *get_extent, int mirror_num)
  3566. {
  3567. unsigned long i;
  3568. unsigned long start_i;
  3569. struct page *page;
  3570. int err;
  3571. int ret = 0;
  3572. int locked_pages = 0;
  3573. int all_uptodate = 1;
  3574. int inc_all_pages = 0;
  3575. unsigned long num_pages;
  3576. struct bio *bio = NULL;
  3577. unsigned long bio_flags = 0;
  3578. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  3579. return 0;
  3580. if (eb_straddles_pages(eb)) {
  3581. if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
  3582. EXTENT_UPTODATE, 1, NULL)) {
  3583. return 0;
  3584. }
  3585. }
  3586. if (start) {
  3587. WARN_ON(start < eb->start);
  3588. start_i = (start >> PAGE_CACHE_SHIFT) -
  3589. (eb->start >> PAGE_CACHE_SHIFT);
  3590. } else {
  3591. start_i = 0;
  3592. }
  3593. num_pages = num_extent_pages(eb->start, eb->len);
  3594. for (i = start_i; i < num_pages; i++) {
  3595. page = extent_buffer_page(eb, i);
  3596. if (wait == WAIT_NONE) {
  3597. if (!trylock_page(page))
  3598. goto unlock_exit;
  3599. } else {
  3600. lock_page(page);
  3601. }
  3602. locked_pages++;
  3603. if (!PageUptodate(page))
  3604. all_uptodate = 0;
  3605. }
  3606. if (all_uptodate) {
  3607. if (start_i == 0)
  3608. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3609. goto unlock_exit;
  3610. }
  3611. for (i = start_i; i < num_pages; i++) {
  3612. page = extent_buffer_page(eb, i);
  3613. WARN_ON(!PagePrivate(page));
  3614. set_page_extent_mapped(page);
  3615. if (i == 0)
  3616. set_page_extent_head(page, eb->len);
  3617. if (inc_all_pages)
  3618. page_cache_get(page);
  3619. if (!PageUptodate(page)) {
  3620. if (start_i == 0)
  3621. inc_all_pages = 1;
  3622. ClearPageError(page);
  3623. err = __extent_read_full_page(tree, page,
  3624. get_extent, &bio,
  3625. mirror_num, &bio_flags);
  3626. if (err)
  3627. ret = err;
  3628. } else {
  3629. unlock_page(page);
  3630. }
  3631. }
  3632. if (bio)
  3633. submit_one_bio(READ, bio, mirror_num, bio_flags);
  3634. if (ret || wait != WAIT_COMPLETE)
  3635. return ret;
  3636. for (i = start_i; i < num_pages; i++) {
  3637. page = extent_buffer_page(eb, i);
  3638. wait_on_page_locked(page);
  3639. if (!PageUptodate(page))
  3640. ret = -EIO;
  3641. }
  3642. if (!ret)
  3643. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3644. return ret;
  3645. unlock_exit:
  3646. i = start_i;
  3647. while (locked_pages > 0) {
  3648. page = extent_buffer_page(eb, i);
  3649. i++;
  3650. unlock_page(page);
  3651. locked_pages--;
  3652. }
  3653. return ret;
  3654. }
  3655. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  3656. unsigned long start,
  3657. unsigned long len)
  3658. {
  3659. size_t cur;
  3660. size_t offset;
  3661. struct page *page;
  3662. char *kaddr;
  3663. char *dst = (char *)dstv;
  3664. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3665. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3666. WARN_ON(start > eb->len);
  3667. WARN_ON(start + len > eb->start + eb->len);
  3668. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3669. while (len > 0) {
  3670. page = extent_buffer_page(eb, i);
  3671. cur = min(len, (PAGE_CACHE_SIZE - offset));
  3672. kaddr = page_address(page);
  3673. memcpy(dst, kaddr + offset, cur);
  3674. dst += cur;
  3675. len -= cur;
  3676. offset = 0;
  3677. i++;
  3678. }
  3679. }
  3680. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  3681. unsigned long min_len, char **map,
  3682. unsigned long *map_start,
  3683. unsigned long *map_len)
  3684. {
  3685. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  3686. char *kaddr;
  3687. struct page *p;
  3688. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3689. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3690. unsigned long end_i = (start_offset + start + min_len - 1) >>
  3691. PAGE_CACHE_SHIFT;
  3692. if (i != end_i)
  3693. return -EINVAL;
  3694. if (i == 0) {
  3695. offset = start_offset;
  3696. *map_start = 0;
  3697. } else {
  3698. offset = 0;
  3699. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  3700. }
  3701. if (start + min_len > eb->len) {
  3702. printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  3703. "wanted %lu %lu\n", (unsigned long long)eb->start,
  3704. eb->len, start, min_len);
  3705. WARN_ON(1);
  3706. return -EINVAL;
  3707. }
  3708. p = extent_buffer_page(eb, i);
  3709. kaddr = page_address(p);
  3710. *map = kaddr + offset;
  3711. *map_len = PAGE_CACHE_SIZE - offset;
  3712. return 0;
  3713. }
  3714. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  3715. unsigned long start,
  3716. unsigned long len)
  3717. {
  3718. size_t cur;
  3719. size_t offset;
  3720. struct page *page;
  3721. char *kaddr;
  3722. char *ptr = (char *)ptrv;
  3723. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3724. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3725. int ret = 0;
  3726. WARN_ON(start > eb->len);
  3727. WARN_ON(start + len > eb->start + eb->len);
  3728. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3729. while (len > 0) {
  3730. page = extent_buffer_page(eb, i);
  3731. cur = min(len, (PAGE_CACHE_SIZE - offset));
  3732. kaddr = page_address(page);
  3733. ret = memcmp(ptr, kaddr + offset, cur);
  3734. if (ret)
  3735. break;
  3736. ptr += cur;
  3737. len -= cur;
  3738. offset = 0;
  3739. i++;
  3740. }
  3741. return ret;
  3742. }
  3743. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  3744. unsigned long start, unsigned long len)
  3745. {
  3746. size_t cur;
  3747. size_t offset;
  3748. struct page *page;
  3749. char *kaddr;
  3750. char *src = (char *)srcv;
  3751. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3752. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3753. WARN_ON(start > eb->len);
  3754. WARN_ON(start + len > eb->start + eb->len);
  3755. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3756. while (len > 0) {
  3757. page = extent_buffer_page(eb, i);
  3758. WARN_ON(!PageUptodate(page));
  3759. cur = min(len, PAGE_CACHE_SIZE - offset);
  3760. kaddr = page_address(page);
  3761. memcpy(kaddr + offset, src, cur);
  3762. src += cur;
  3763. len -= cur;
  3764. offset = 0;
  3765. i++;
  3766. }
  3767. }
  3768. void memset_extent_buffer(struct extent_buffer *eb, char c,
  3769. unsigned long start, unsigned long len)
  3770. {
  3771. size_t cur;
  3772. size_t offset;
  3773. struct page *page;
  3774. char *kaddr;
  3775. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3776. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3777. WARN_ON(start > eb->len);
  3778. WARN_ON(start + len > eb->start + eb->len);
  3779. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3780. while (len > 0) {
  3781. page = extent_buffer_page(eb, i);
  3782. WARN_ON(!PageUptodate(page));
  3783. cur = min(len, PAGE_CACHE_SIZE - offset);
  3784. kaddr = page_address(page);
  3785. memset(kaddr + offset, c, cur);
  3786. len -= cur;
  3787. offset = 0;
  3788. i++;
  3789. }
  3790. }
  3791. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  3792. unsigned long dst_offset, unsigned long src_offset,
  3793. unsigned long len)
  3794. {
  3795. u64 dst_len = dst->len;
  3796. size_t cur;
  3797. size_t offset;
  3798. struct page *page;
  3799. char *kaddr;
  3800. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3801. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  3802. WARN_ON(src->len != dst_len);
  3803. offset = (start_offset + dst_offset) &
  3804. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3805. while (len > 0) {
  3806. page = extent_buffer_page(dst, i);
  3807. WARN_ON(!PageUptodate(page));
  3808. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  3809. kaddr = page_address(page);
  3810. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  3811. src_offset += cur;
  3812. len -= cur;
  3813. offset = 0;
  3814. i++;
  3815. }
  3816. }
  3817. static void move_pages(struct page *dst_page, struct page *src_page,
  3818. unsigned long dst_off, unsigned long src_off,
  3819. unsigned long len)
  3820. {
  3821. char *dst_kaddr = page_address(dst_page);
  3822. if (dst_page == src_page) {
  3823. memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
  3824. } else {
  3825. char *src_kaddr = page_address(src_page);
  3826. char *p = dst_kaddr + dst_off + len;
  3827. char *s = src_kaddr + src_off + len;
  3828. while (len--)
  3829. *--p = *--s;
  3830. }
  3831. }
  3832. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  3833. {
  3834. unsigned long distance = (src > dst) ? src - dst : dst - src;
  3835. return distance < len;
  3836. }
  3837. static void copy_pages(struct page *dst_page, struct page *src_page,
  3838. unsigned long dst_off, unsigned long src_off,
  3839. unsigned long len)
  3840. {
  3841. char *dst_kaddr = page_address(dst_page);
  3842. char *src_kaddr;
  3843. if (dst_page != src_page) {
  3844. src_kaddr = page_address(src_page);
  3845. } else {
  3846. src_kaddr = dst_kaddr;
  3847. BUG_ON(areas_overlap(src_off, dst_off, len));
  3848. }
  3849. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  3850. }
  3851. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  3852. unsigned long src_offset, unsigned long len)
  3853. {
  3854. size_t cur;
  3855. size_t dst_off_in_page;
  3856. size_t src_off_in_page;
  3857. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3858. unsigned long dst_i;
  3859. unsigned long src_i;
  3860. if (src_offset + len > dst->len) {
  3861. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  3862. "len %lu dst len %lu\n", src_offset, len, dst->len);
  3863. BUG_ON(1);
  3864. }
  3865. if (dst_offset + len > dst->len) {
  3866. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  3867. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  3868. BUG_ON(1);
  3869. }
  3870. while (len > 0) {
  3871. dst_off_in_page = (start_offset + dst_offset) &
  3872. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3873. src_off_in_page = (start_offset + src_offset) &
  3874. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3875. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  3876. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  3877. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  3878. src_off_in_page));
  3879. cur = min_t(unsigned long, cur,
  3880. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  3881. copy_pages(extent_buffer_page(dst, dst_i),
  3882. extent_buffer_page(dst, src_i),
  3883. dst_off_in_page, src_off_in_page, cur);
  3884. src_offset += cur;
  3885. dst_offset += cur;
  3886. len -= cur;
  3887. }
  3888. }
  3889. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  3890. unsigned long src_offset, unsigned long len)
  3891. {
  3892. size_t cur;
  3893. size_t dst_off_in_page;
  3894. size_t src_off_in_page;
  3895. unsigned long dst_end = dst_offset + len - 1;
  3896. unsigned long src_end = src_offset + len - 1;
  3897. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3898. unsigned long dst_i;
  3899. unsigned long src_i;
  3900. if (src_offset + len > dst->len) {
  3901. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  3902. "len %lu len %lu\n", src_offset, len, dst->len);
  3903. BUG_ON(1);
  3904. }
  3905. if (dst_offset + len > dst->len) {
  3906. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  3907. "len %lu len %lu\n", dst_offset, len, dst->len);
  3908. BUG_ON(1);
  3909. }
  3910. if (!areas_overlap(src_offset, dst_offset, len)) {
  3911. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  3912. return;
  3913. }
  3914. while (len > 0) {
  3915. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  3916. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  3917. dst_off_in_page = (start_offset + dst_end) &
  3918. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3919. src_off_in_page = (start_offset + src_end) &
  3920. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3921. cur = min_t(unsigned long, len, src_off_in_page + 1);
  3922. cur = min(cur, dst_off_in_page + 1);
  3923. move_pages(extent_buffer_page(dst, dst_i),
  3924. extent_buffer_page(dst, src_i),
  3925. dst_off_in_page - cur + 1,
  3926. src_off_in_page - cur + 1, cur);
  3927. dst_end -= cur;
  3928. src_end -= cur;
  3929. len -= cur;
  3930. }
  3931. }
  3932. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  3933. {
  3934. struct extent_buffer *eb =
  3935. container_of(head, struct extent_buffer, rcu_head);
  3936. btrfs_release_extent_buffer(eb);
  3937. }
  3938. int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
  3939. {
  3940. u64 start = page_offset(page);
  3941. struct extent_buffer *eb;
  3942. int ret = 1;
  3943. spin_lock(&tree->buffer_lock);
  3944. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3945. if (!eb) {
  3946. spin_unlock(&tree->buffer_lock);
  3947. return ret;
  3948. }
  3949. if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3950. ret = 0;
  3951. goto out;
  3952. }
  3953. /*
  3954. * set @eb->refs to 0 if it is already 1, and then release the @eb.
  3955. * Or go back.
  3956. */
  3957. if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
  3958. ret = 0;
  3959. goto out;
  3960. }
  3961. radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3962. out:
  3963. spin_unlock(&tree->buffer_lock);
  3964. /* at this point we can safely release the extent buffer */
  3965. if (atomic_read(&eb->refs) == 0)
  3966. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  3967. return ret;
  3968. }