extent_io.c 110 KB

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