extent_io.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481
  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. }
  1979. if (!uptodate) {
  1980. clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
  1981. ClearPageUptodate(page);
  1982. SetPageError(page);
  1983. }
  1984. return 0;
  1985. }
  1986. /*
  1987. * after a writepage IO is done, we need to:
  1988. * clear the uptodate bits on error
  1989. * clear the writeback bits in the extent tree for this IO
  1990. * end_page_writeback if the page has no more pending IO
  1991. *
  1992. * Scheduling is not allowed, so the extent state tree is expected
  1993. * to have one and only one object corresponding to this IO.
  1994. */
  1995. static void end_bio_extent_writepage(struct bio *bio, int err)
  1996. {
  1997. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1998. struct extent_io_tree *tree;
  1999. u64 start;
  2000. u64 end;
  2001. int whole_page;
  2002. do {
  2003. struct page *page = bvec->bv_page;
  2004. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2005. start = ((u64)page->index << PAGE_CACHE_SHIFT) +
  2006. bvec->bv_offset;
  2007. end = start + bvec->bv_len - 1;
  2008. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  2009. whole_page = 1;
  2010. else
  2011. whole_page = 0;
  2012. if (--bvec >= bio->bi_io_vec)
  2013. prefetchw(&bvec->bv_page->flags);
  2014. if (end_extent_writepage(page, err, start, end))
  2015. continue;
  2016. if (whole_page)
  2017. end_page_writeback(page);
  2018. else
  2019. check_page_writeback(tree, page);
  2020. } while (bvec >= bio->bi_io_vec);
  2021. bio_put(bio);
  2022. }
  2023. /*
  2024. * after a readpage IO is done, we need to:
  2025. * clear the uptodate bits on error
  2026. * set the uptodate bits if things worked
  2027. * set the page up to date if all extents in the tree are uptodate
  2028. * clear the lock bit in the extent tree
  2029. * unlock the page if there are no other extents locked for it
  2030. *
  2031. * Scheduling is not allowed, so the extent state tree is expected
  2032. * to have one and only one object corresponding to this IO.
  2033. */
  2034. static void end_bio_extent_readpage(struct bio *bio, int err)
  2035. {
  2036. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  2037. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  2038. struct bio_vec *bvec = bio->bi_io_vec;
  2039. struct extent_io_tree *tree;
  2040. u64 start;
  2041. u64 end;
  2042. int whole_page;
  2043. int ret;
  2044. if (err)
  2045. uptodate = 0;
  2046. do {
  2047. struct page *page = bvec->bv_page;
  2048. struct extent_state *cached = NULL;
  2049. struct extent_state *state;
  2050. pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
  2051. "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
  2052. (long int)bio->bi_bdev);
  2053. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2054. start = ((u64)page->index << PAGE_CACHE_SHIFT) +
  2055. bvec->bv_offset;
  2056. end = start + bvec->bv_len - 1;
  2057. if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
  2058. whole_page = 1;
  2059. else
  2060. whole_page = 0;
  2061. if (++bvec <= bvec_end)
  2062. prefetchw(&bvec->bv_page->flags);
  2063. spin_lock(&tree->lock);
  2064. state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
  2065. if (state && state->start == start) {
  2066. /*
  2067. * take a reference on the state, unlock will drop
  2068. * the ref
  2069. */
  2070. cache_state(state, &cached);
  2071. }
  2072. spin_unlock(&tree->lock);
  2073. if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
  2074. ret = tree->ops->readpage_end_io_hook(page, start, end,
  2075. state);
  2076. if (ret)
  2077. uptodate = 0;
  2078. else
  2079. clean_io_failure(start, page);
  2080. }
  2081. if (!uptodate) {
  2082. int failed_mirror;
  2083. failed_mirror = (int)(unsigned long)bio->bi_bdev;
  2084. /*
  2085. * The generic bio_readpage_error handles errors the
  2086. * following way: If possible, new read requests are
  2087. * created and submitted and will end up in
  2088. * end_bio_extent_readpage as well (if we're lucky, not
  2089. * in the !uptodate case). In that case it returns 0 and
  2090. * we just go on with the next page in our bio. If it
  2091. * can't handle the error it will return -EIO and we
  2092. * remain responsible for that page.
  2093. */
  2094. ret = bio_readpage_error(bio, page, start, end,
  2095. failed_mirror, NULL);
  2096. if (ret == 0) {
  2097. error_handled:
  2098. uptodate =
  2099. test_bit(BIO_UPTODATE, &bio->bi_flags);
  2100. if (err)
  2101. uptodate = 0;
  2102. uncache_state(&cached);
  2103. continue;
  2104. }
  2105. if (tree->ops && tree->ops->readpage_io_failed_hook) {
  2106. ret = tree->ops->readpage_io_failed_hook(
  2107. bio, page, start, end,
  2108. failed_mirror, state);
  2109. if (ret == 0)
  2110. goto error_handled;
  2111. }
  2112. }
  2113. if (uptodate) {
  2114. set_extent_uptodate(tree, start, end, &cached,
  2115. GFP_ATOMIC);
  2116. }
  2117. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2118. if (whole_page) {
  2119. if (uptodate) {
  2120. SetPageUptodate(page);
  2121. } else {
  2122. ClearPageUptodate(page);
  2123. SetPageError(page);
  2124. }
  2125. unlock_page(page);
  2126. } else {
  2127. if (uptodate) {
  2128. check_page_uptodate(tree, page);
  2129. } else {
  2130. ClearPageUptodate(page);
  2131. SetPageError(page);
  2132. }
  2133. check_page_locked(tree, page);
  2134. }
  2135. } while (bvec <= bvec_end);
  2136. bio_put(bio);
  2137. }
  2138. struct bio *
  2139. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2140. gfp_t gfp_flags)
  2141. {
  2142. struct bio *bio;
  2143. bio = bio_alloc(gfp_flags, nr_vecs);
  2144. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2145. while (!bio && (nr_vecs /= 2))
  2146. bio = bio_alloc(gfp_flags, nr_vecs);
  2147. }
  2148. if (bio) {
  2149. bio->bi_size = 0;
  2150. bio->bi_bdev = bdev;
  2151. bio->bi_sector = first_sector;
  2152. }
  2153. return bio;
  2154. }
  2155. static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
  2156. unsigned long bio_flags)
  2157. {
  2158. int ret = 0;
  2159. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2160. struct page *page = bvec->bv_page;
  2161. struct extent_io_tree *tree = bio->bi_private;
  2162. u64 start;
  2163. start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
  2164. bio->bi_private = NULL;
  2165. bio_get(bio);
  2166. if (tree->ops && tree->ops->submit_bio_hook)
  2167. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2168. mirror_num, bio_flags, start);
  2169. else
  2170. btrfsic_submit_bio(rw, bio);
  2171. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  2172. ret = -EOPNOTSUPP;
  2173. bio_put(bio);
  2174. return ret;
  2175. }
  2176. static int merge_bio(struct extent_io_tree *tree, struct page *page,
  2177. unsigned long offset, size_t size, struct bio *bio,
  2178. unsigned long bio_flags)
  2179. {
  2180. int ret = 0;
  2181. if (tree->ops && tree->ops->merge_bio_hook)
  2182. ret = tree->ops->merge_bio_hook(page, offset, size, bio,
  2183. bio_flags);
  2184. BUG_ON(ret < 0);
  2185. return ret;
  2186. }
  2187. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  2188. struct page *page, sector_t sector,
  2189. size_t size, unsigned long offset,
  2190. struct block_device *bdev,
  2191. struct bio **bio_ret,
  2192. unsigned long max_pages,
  2193. bio_end_io_t end_io_func,
  2194. int mirror_num,
  2195. unsigned long prev_bio_flags,
  2196. unsigned long bio_flags)
  2197. {
  2198. int ret = 0;
  2199. struct bio *bio;
  2200. int nr;
  2201. int contig = 0;
  2202. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  2203. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2204. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  2205. if (bio_ret && *bio_ret) {
  2206. bio = *bio_ret;
  2207. if (old_compressed)
  2208. contig = bio->bi_sector == sector;
  2209. else
  2210. contig = bio->bi_sector + (bio->bi_size >> 9) ==
  2211. sector;
  2212. if (prev_bio_flags != bio_flags || !contig ||
  2213. merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
  2214. bio_add_page(bio, page, page_size, offset) < page_size) {
  2215. ret = submit_one_bio(rw, bio, mirror_num,
  2216. prev_bio_flags);
  2217. bio = NULL;
  2218. } else {
  2219. return 0;
  2220. }
  2221. }
  2222. if (this_compressed)
  2223. nr = BIO_MAX_PAGES;
  2224. else
  2225. nr = bio_get_nr_vecs(bdev);
  2226. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  2227. if (!bio)
  2228. return -ENOMEM;
  2229. bio_add_page(bio, page, page_size, offset);
  2230. bio->bi_end_io = end_io_func;
  2231. bio->bi_private = tree;
  2232. if (bio_ret)
  2233. *bio_ret = bio;
  2234. else
  2235. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2236. return ret;
  2237. }
  2238. void set_page_extent_mapped(struct page *page)
  2239. {
  2240. if (!PagePrivate(page)) {
  2241. SetPagePrivate(page);
  2242. page_cache_get(page);
  2243. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2244. }
  2245. }
  2246. static void set_page_extent_head(struct page *page, unsigned long len)
  2247. {
  2248. WARN_ON(!PagePrivate(page));
  2249. set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
  2250. }
  2251. /*
  2252. * basic readpage implementation. Locked extent state structs are inserted
  2253. * into the tree that are removed when the IO is done (by the end_io
  2254. * handlers)
  2255. */
  2256. static int __extent_read_full_page(struct extent_io_tree *tree,
  2257. struct page *page,
  2258. get_extent_t *get_extent,
  2259. struct bio **bio, int mirror_num,
  2260. unsigned long *bio_flags)
  2261. {
  2262. struct inode *inode = page->mapping->host;
  2263. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2264. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2265. u64 end;
  2266. u64 cur = start;
  2267. u64 extent_offset;
  2268. u64 last_byte = i_size_read(inode);
  2269. u64 block_start;
  2270. u64 cur_end;
  2271. sector_t sector;
  2272. struct extent_map *em;
  2273. struct block_device *bdev;
  2274. struct btrfs_ordered_extent *ordered;
  2275. int ret;
  2276. int nr = 0;
  2277. size_t pg_offset = 0;
  2278. size_t iosize;
  2279. size_t disk_io_size;
  2280. size_t blocksize = inode->i_sb->s_blocksize;
  2281. unsigned long this_bio_flag = 0;
  2282. set_page_extent_mapped(page);
  2283. if (!PageUptodate(page)) {
  2284. if (cleancache_get_page(page) == 0) {
  2285. BUG_ON(blocksize != PAGE_SIZE);
  2286. goto out;
  2287. }
  2288. }
  2289. end = page_end;
  2290. while (1) {
  2291. lock_extent(tree, start, end, GFP_NOFS);
  2292. ordered = btrfs_lookup_ordered_extent(inode, start);
  2293. if (!ordered)
  2294. break;
  2295. unlock_extent(tree, start, end, GFP_NOFS);
  2296. btrfs_start_ordered_extent(inode, ordered, 1);
  2297. btrfs_put_ordered_extent(ordered);
  2298. }
  2299. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  2300. char *userpage;
  2301. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  2302. if (zero_offset) {
  2303. iosize = PAGE_CACHE_SIZE - zero_offset;
  2304. userpage = kmap_atomic(page, KM_USER0);
  2305. memset(userpage + zero_offset, 0, iosize);
  2306. flush_dcache_page(page);
  2307. kunmap_atomic(userpage, KM_USER0);
  2308. }
  2309. }
  2310. while (cur <= end) {
  2311. if (cur >= last_byte) {
  2312. char *userpage;
  2313. struct extent_state *cached = NULL;
  2314. iosize = PAGE_CACHE_SIZE - pg_offset;
  2315. userpage = kmap_atomic(page, KM_USER0);
  2316. memset(userpage + pg_offset, 0, iosize);
  2317. flush_dcache_page(page);
  2318. kunmap_atomic(userpage, KM_USER0);
  2319. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2320. &cached, GFP_NOFS);
  2321. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2322. &cached, GFP_NOFS);
  2323. break;
  2324. }
  2325. em = get_extent(inode, page, pg_offset, cur,
  2326. end - cur + 1, 0);
  2327. if (IS_ERR_OR_NULL(em)) {
  2328. SetPageError(page);
  2329. unlock_extent(tree, cur, end, GFP_NOFS);
  2330. break;
  2331. }
  2332. extent_offset = cur - em->start;
  2333. BUG_ON(extent_map_end(em) <= cur);
  2334. BUG_ON(end < cur);
  2335. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2336. this_bio_flag = EXTENT_BIO_COMPRESSED;
  2337. extent_set_compress_type(&this_bio_flag,
  2338. em->compress_type);
  2339. }
  2340. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2341. cur_end = min(extent_map_end(em) - 1, end);
  2342. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  2343. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2344. disk_io_size = em->block_len;
  2345. sector = em->block_start >> 9;
  2346. } else {
  2347. sector = (em->block_start + extent_offset) >> 9;
  2348. disk_io_size = iosize;
  2349. }
  2350. bdev = em->bdev;
  2351. block_start = em->block_start;
  2352. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2353. block_start = EXTENT_MAP_HOLE;
  2354. free_extent_map(em);
  2355. em = NULL;
  2356. /* we've found a hole, just zero and go on */
  2357. if (block_start == EXTENT_MAP_HOLE) {
  2358. char *userpage;
  2359. struct extent_state *cached = NULL;
  2360. userpage = kmap_atomic(page, KM_USER0);
  2361. memset(userpage + pg_offset, 0, iosize);
  2362. flush_dcache_page(page);
  2363. kunmap_atomic(userpage, KM_USER0);
  2364. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2365. &cached, GFP_NOFS);
  2366. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2367. &cached, GFP_NOFS);
  2368. cur = cur + iosize;
  2369. pg_offset += iosize;
  2370. continue;
  2371. }
  2372. /* the get_extent function already copied into the page */
  2373. if (test_range_bit(tree, cur, cur_end,
  2374. EXTENT_UPTODATE, 1, NULL)) {
  2375. check_page_uptodate(tree, page);
  2376. unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
  2377. cur = cur + iosize;
  2378. pg_offset += iosize;
  2379. continue;
  2380. }
  2381. /* we have an inline extent but it didn't get marked up
  2382. * to date. Error out
  2383. */
  2384. if (block_start == EXTENT_MAP_INLINE) {
  2385. SetPageError(page);
  2386. unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
  2387. cur = cur + iosize;
  2388. pg_offset += iosize;
  2389. continue;
  2390. }
  2391. ret = 0;
  2392. if (tree->ops && tree->ops->readpage_io_hook) {
  2393. ret = tree->ops->readpage_io_hook(page, cur,
  2394. cur + iosize - 1);
  2395. }
  2396. if (!ret) {
  2397. unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
  2398. pnr -= page->index;
  2399. ret = submit_extent_page(READ, tree, page,
  2400. sector, disk_io_size, pg_offset,
  2401. bdev, bio, pnr,
  2402. end_bio_extent_readpage, mirror_num,
  2403. *bio_flags,
  2404. this_bio_flag);
  2405. nr++;
  2406. *bio_flags = this_bio_flag;
  2407. }
  2408. if (ret)
  2409. SetPageError(page);
  2410. cur = cur + iosize;
  2411. pg_offset += iosize;
  2412. }
  2413. out:
  2414. if (!nr) {
  2415. if (!PageError(page))
  2416. SetPageUptodate(page);
  2417. unlock_page(page);
  2418. }
  2419. return 0;
  2420. }
  2421. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2422. get_extent_t *get_extent, int mirror_num)
  2423. {
  2424. struct bio *bio = NULL;
  2425. unsigned long bio_flags = 0;
  2426. int ret;
  2427. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2428. &bio_flags);
  2429. if (bio)
  2430. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2431. return ret;
  2432. }
  2433. static noinline void update_nr_written(struct page *page,
  2434. struct writeback_control *wbc,
  2435. unsigned long nr_written)
  2436. {
  2437. wbc->nr_to_write -= nr_written;
  2438. if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
  2439. wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
  2440. page->mapping->writeback_index = page->index + nr_written;
  2441. }
  2442. /*
  2443. * the writepage semantics are similar to regular writepage. extent
  2444. * records are inserted to lock ranges in the tree, and as dirty areas
  2445. * are found, they are marked writeback. Then the lock bits are removed
  2446. * and the end_io handler clears the writeback ranges
  2447. */
  2448. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  2449. void *data)
  2450. {
  2451. struct inode *inode = page->mapping->host;
  2452. struct extent_page_data *epd = data;
  2453. struct extent_io_tree *tree = epd->tree;
  2454. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2455. u64 delalloc_start;
  2456. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2457. u64 end;
  2458. u64 cur = start;
  2459. u64 extent_offset;
  2460. u64 last_byte = i_size_read(inode);
  2461. u64 block_start;
  2462. u64 iosize;
  2463. sector_t sector;
  2464. struct extent_state *cached_state = NULL;
  2465. struct extent_map *em;
  2466. struct block_device *bdev;
  2467. int ret;
  2468. int nr = 0;
  2469. size_t pg_offset = 0;
  2470. size_t blocksize;
  2471. loff_t i_size = i_size_read(inode);
  2472. unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
  2473. u64 nr_delalloc;
  2474. u64 delalloc_end;
  2475. int page_started;
  2476. int compressed;
  2477. int write_flags;
  2478. unsigned long nr_written = 0;
  2479. bool fill_delalloc = true;
  2480. if (wbc->sync_mode == WB_SYNC_ALL)
  2481. write_flags = WRITE_SYNC;
  2482. else
  2483. write_flags = WRITE;
  2484. trace___extent_writepage(page, inode, wbc);
  2485. WARN_ON(!PageLocked(page));
  2486. ClearPageError(page);
  2487. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  2488. if (page->index > end_index ||
  2489. (page->index == end_index && !pg_offset)) {
  2490. page->mapping->a_ops->invalidatepage(page, 0);
  2491. unlock_page(page);
  2492. return 0;
  2493. }
  2494. if (page->index == end_index) {
  2495. char *userpage;
  2496. userpage = kmap_atomic(page, KM_USER0);
  2497. memset(userpage + pg_offset, 0,
  2498. PAGE_CACHE_SIZE - pg_offset);
  2499. kunmap_atomic(userpage, KM_USER0);
  2500. flush_dcache_page(page);
  2501. }
  2502. pg_offset = 0;
  2503. set_page_extent_mapped(page);
  2504. if (!tree->ops || !tree->ops->fill_delalloc)
  2505. fill_delalloc = false;
  2506. delalloc_start = start;
  2507. delalloc_end = 0;
  2508. page_started = 0;
  2509. if (!epd->extent_locked && fill_delalloc) {
  2510. u64 delalloc_to_write = 0;
  2511. /*
  2512. * make sure the wbc mapping index is at least updated
  2513. * to this page.
  2514. */
  2515. update_nr_written(page, wbc, 0);
  2516. while (delalloc_end < page_end) {
  2517. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2518. page,
  2519. &delalloc_start,
  2520. &delalloc_end,
  2521. 128 * 1024 * 1024);
  2522. if (nr_delalloc == 0) {
  2523. delalloc_start = delalloc_end + 1;
  2524. continue;
  2525. }
  2526. ret = tree->ops->fill_delalloc(inode, page,
  2527. delalloc_start,
  2528. delalloc_end,
  2529. &page_started,
  2530. &nr_written);
  2531. BUG_ON(ret);
  2532. /*
  2533. * delalloc_end is already one less than the total
  2534. * length, so we don't subtract one from
  2535. * PAGE_CACHE_SIZE
  2536. */
  2537. delalloc_to_write += (delalloc_end - delalloc_start +
  2538. PAGE_CACHE_SIZE) >>
  2539. PAGE_CACHE_SHIFT;
  2540. delalloc_start = delalloc_end + 1;
  2541. }
  2542. if (wbc->nr_to_write < delalloc_to_write) {
  2543. int thresh = 8192;
  2544. if (delalloc_to_write < thresh * 2)
  2545. thresh = delalloc_to_write;
  2546. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2547. thresh);
  2548. }
  2549. /* did the fill delalloc function already unlock and start
  2550. * the IO?
  2551. */
  2552. if (page_started) {
  2553. ret = 0;
  2554. /*
  2555. * we've unlocked the page, so we can't update
  2556. * the mapping's writeback index, just update
  2557. * nr_to_write.
  2558. */
  2559. wbc->nr_to_write -= nr_written;
  2560. goto done_unlocked;
  2561. }
  2562. }
  2563. if (tree->ops && tree->ops->writepage_start_hook) {
  2564. ret = tree->ops->writepage_start_hook(page, start,
  2565. page_end);
  2566. if (ret) {
  2567. /* Fixup worker will requeue */
  2568. if (ret == -EBUSY)
  2569. wbc->pages_skipped++;
  2570. else
  2571. redirty_page_for_writepage(wbc, page);
  2572. update_nr_written(page, wbc, nr_written);
  2573. unlock_page(page);
  2574. ret = 0;
  2575. goto done_unlocked;
  2576. }
  2577. }
  2578. /*
  2579. * we don't want to touch the inode after unlocking the page,
  2580. * so we update the mapping writeback index now
  2581. */
  2582. update_nr_written(page, wbc, nr_written + 1);
  2583. end = page_end;
  2584. if (last_byte <= start) {
  2585. if (tree->ops && tree->ops->writepage_end_io_hook)
  2586. tree->ops->writepage_end_io_hook(page, start,
  2587. page_end, NULL, 1);
  2588. goto done;
  2589. }
  2590. blocksize = inode->i_sb->s_blocksize;
  2591. while (cur <= end) {
  2592. if (cur >= last_byte) {
  2593. if (tree->ops && tree->ops->writepage_end_io_hook)
  2594. tree->ops->writepage_end_io_hook(page, cur,
  2595. page_end, NULL, 1);
  2596. break;
  2597. }
  2598. em = epd->get_extent(inode, page, pg_offset, cur,
  2599. end - cur + 1, 1);
  2600. if (IS_ERR_OR_NULL(em)) {
  2601. SetPageError(page);
  2602. break;
  2603. }
  2604. extent_offset = cur - em->start;
  2605. BUG_ON(extent_map_end(em) <= cur);
  2606. BUG_ON(end < cur);
  2607. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2608. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  2609. sector = (em->block_start + extent_offset) >> 9;
  2610. bdev = em->bdev;
  2611. block_start = em->block_start;
  2612. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  2613. free_extent_map(em);
  2614. em = NULL;
  2615. /*
  2616. * compressed and inline extents are written through other
  2617. * paths in the FS
  2618. */
  2619. if (compressed || block_start == EXTENT_MAP_HOLE ||
  2620. block_start == EXTENT_MAP_INLINE) {
  2621. /*
  2622. * end_io notification does not happen here for
  2623. * compressed extents
  2624. */
  2625. if (!compressed && tree->ops &&
  2626. tree->ops->writepage_end_io_hook)
  2627. tree->ops->writepage_end_io_hook(page, cur,
  2628. cur + iosize - 1,
  2629. NULL, 1);
  2630. else if (compressed) {
  2631. /* we don't want to end_page_writeback on
  2632. * a compressed extent. this happens
  2633. * elsewhere
  2634. */
  2635. nr++;
  2636. }
  2637. cur += iosize;
  2638. pg_offset += iosize;
  2639. continue;
  2640. }
  2641. /* leave this out until we have a page_mkwrite call */
  2642. if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
  2643. EXTENT_DIRTY, 0, NULL)) {
  2644. cur = cur + iosize;
  2645. pg_offset += iosize;
  2646. continue;
  2647. }
  2648. if (tree->ops && tree->ops->writepage_io_hook) {
  2649. ret = tree->ops->writepage_io_hook(page, cur,
  2650. cur + iosize - 1);
  2651. } else {
  2652. ret = 0;
  2653. }
  2654. if (ret) {
  2655. SetPageError(page);
  2656. } else {
  2657. unsigned long max_nr = end_index + 1;
  2658. set_range_writeback(tree, cur, cur + iosize - 1);
  2659. if (!PageWriteback(page)) {
  2660. printk(KERN_ERR "btrfs warning page %lu not "
  2661. "writeback, cur %llu end %llu\n",
  2662. page->index, (unsigned long long)cur,
  2663. (unsigned long long)end);
  2664. }
  2665. ret = submit_extent_page(write_flags, tree, page,
  2666. sector, iosize, pg_offset,
  2667. bdev, &epd->bio, max_nr,
  2668. end_bio_extent_writepage,
  2669. 0, 0, 0);
  2670. if (ret)
  2671. SetPageError(page);
  2672. }
  2673. cur = cur + iosize;
  2674. pg_offset += iosize;
  2675. nr++;
  2676. }
  2677. done:
  2678. if (nr == 0) {
  2679. /* make sure the mapping tag for page dirty gets cleared */
  2680. set_page_writeback(page);
  2681. end_page_writeback(page);
  2682. }
  2683. unlock_page(page);
  2684. done_unlocked:
  2685. /* drop our reference on any cached states */
  2686. free_extent_state(cached_state);
  2687. return 0;
  2688. }
  2689. /**
  2690. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  2691. * @mapping: address space structure to write
  2692. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  2693. * @writepage: function called for each page
  2694. * @data: data passed to writepage function
  2695. *
  2696. * If a page is already under I/O, write_cache_pages() skips it, even
  2697. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  2698. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  2699. * and msync() need to guarantee that all the data which was dirty at the time
  2700. * the call was made get new I/O started against them. If wbc->sync_mode is
  2701. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  2702. * existing IO to complete.
  2703. */
  2704. static int extent_write_cache_pages(struct extent_io_tree *tree,
  2705. struct address_space *mapping,
  2706. struct writeback_control *wbc,
  2707. writepage_t writepage, void *data,
  2708. void (*flush_fn)(void *))
  2709. {
  2710. int ret = 0;
  2711. int done = 0;
  2712. int nr_to_write_done = 0;
  2713. struct pagevec pvec;
  2714. int nr_pages;
  2715. pgoff_t index;
  2716. pgoff_t end; /* Inclusive */
  2717. int scanned = 0;
  2718. int tag;
  2719. pagevec_init(&pvec, 0);
  2720. if (wbc->range_cyclic) {
  2721. index = mapping->writeback_index; /* Start from prev offset */
  2722. end = -1;
  2723. } else {
  2724. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2725. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2726. scanned = 1;
  2727. }
  2728. if (wbc->sync_mode == WB_SYNC_ALL)
  2729. tag = PAGECACHE_TAG_TOWRITE;
  2730. else
  2731. tag = PAGECACHE_TAG_DIRTY;
  2732. retry:
  2733. if (wbc->sync_mode == WB_SYNC_ALL)
  2734. tag_pages_for_writeback(mapping, index, end);
  2735. while (!done && !nr_to_write_done && (index <= end) &&
  2736. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  2737. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  2738. unsigned i;
  2739. scanned = 1;
  2740. for (i = 0; i < nr_pages; i++) {
  2741. struct page *page = pvec.pages[i];
  2742. /*
  2743. * At this point we hold neither mapping->tree_lock nor
  2744. * lock on the page itself: the page may be truncated or
  2745. * invalidated (changing page->mapping to NULL), or even
  2746. * swizzled back from swapper_space to tmpfs file
  2747. * mapping
  2748. */
  2749. if (tree->ops &&
  2750. tree->ops->write_cache_pages_lock_hook) {
  2751. tree->ops->write_cache_pages_lock_hook(page,
  2752. data, flush_fn);
  2753. } else {
  2754. if (!trylock_page(page)) {
  2755. flush_fn(data);
  2756. lock_page(page);
  2757. }
  2758. }
  2759. if (unlikely(page->mapping != mapping)) {
  2760. unlock_page(page);
  2761. continue;
  2762. }
  2763. if (!wbc->range_cyclic && page->index > end) {
  2764. done = 1;
  2765. unlock_page(page);
  2766. continue;
  2767. }
  2768. if (wbc->sync_mode != WB_SYNC_NONE) {
  2769. if (PageWriteback(page))
  2770. flush_fn(data);
  2771. wait_on_page_writeback(page);
  2772. }
  2773. if (PageWriteback(page) ||
  2774. !clear_page_dirty_for_io(page)) {
  2775. unlock_page(page);
  2776. continue;
  2777. }
  2778. ret = (*writepage)(page, wbc, data);
  2779. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  2780. unlock_page(page);
  2781. ret = 0;
  2782. }
  2783. if (ret)
  2784. done = 1;
  2785. /*
  2786. * the filesystem may choose to bump up nr_to_write.
  2787. * We have to make sure to honor the new nr_to_write
  2788. * at any time
  2789. */
  2790. nr_to_write_done = wbc->nr_to_write <= 0;
  2791. }
  2792. pagevec_release(&pvec);
  2793. cond_resched();
  2794. }
  2795. if (!scanned && !done) {
  2796. /*
  2797. * We hit the last page and there is more work to be done: wrap
  2798. * back to the start of the file
  2799. */
  2800. scanned = 1;
  2801. index = 0;
  2802. goto retry;
  2803. }
  2804. return ret;
  2805. }
  2806. static void flush_epd_write_bio(struct extent_page_data *epd)
  2807. {
  2808. if (epd->bio) {
  2809. if (epd->sync_io)
  2810. submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
  2811. else
  2812. submit_one_bio(WRITE, epd->bio, 0, 0);
  2813. epd->bio = NULL;
  2814. }
  2815. }
  2816. static noinline void flush_write_bio(void *data)
  2817. {
  2818. struct extent_page_data *epd = data;
  2819. flush_epd_write_bio(epd);
  2820. }
  2821. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  2822. get_extent_t *get_extent,
  2823. struct writeback_control *wbc)
  2824. {
  2825. int ret;
  2826. struct extent_page_data epd = {
  2827. .bio = NULL,
  2828. .tree = tree,
  2829. .get_extent = get_extent,
  2830. .extent_locked = 0,
  2831. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2832. };
  2833. ret = __extent_writepage(page, wbc, &epd);
  2834. flush_epd_write_bio(&epd);
  2835. return ret;
  2836. }
  2837. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  2838. u64 start, u64 end, get_extent_t *get_extent,
  2839. int mode)
  2840. {
  2841. int ret = 0;
  2842. struct address_space *mapping = inode->i_mapping;
  2843. struct page *page;
  2844. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  2845. PAGE_CACHE_SHIFT;
  2846. struct extent_page_data epd = {
  2847. .bio = NULL,
  2848. .tree = tree,
  2849. .get_extent = get_extent,
  2850. .extent_locked = 1,
  2851. .sync_io = mode == WB_SYNC_ALL,
  2852. };
  2853. struct writeback_control wbc_writepages = {
  2854. .sync_mode = mode,
  2855. .nr_to_write = nr_pages * 2,
  2856. .range_start = start,
  2857. .range_end = end + 1,
  2858. };
  2859. while (start <= end) {
  2860. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  2861. if (clear_page_dirty_for_io(page))
  2862. ret = __extent_writepage(page, &wbc_writepages, &epd);
  2863. else {
  2864. if (tree->ops && tree->ops->writepage_end_io_hook)
  2865. tree->ops->writepage_end_io_hook(page, start,
  2866. start + PAGE_CACHE_SIZE - 1,
  2867. NULL, 1);
  2868. unlock_page(page);
  2869. }
  2870. page_cache_release(page);
  2871. start += PAGE_CACHE_SIZE;
  2872. }
  2873. flush_epd_write_bio(&epd);
  2874. return ret;
  2875. }
  2876. int extent_writepages(struct extent_io_tree *tree,
  2877. struct address_space *mapping,
  2878. get_extent_t *get_extent,
  2879. struct writeback_control *wbc)
  2880. {
  2881. int ret = 0;
  2882. struct extent_page_data epd = {
  2883. .bio = NULL,
  2884. .tree = tree,
  2885. .get_extent = get_extent,
  2886. .extent_locked = 0,
  2887. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2888. };
  2889. ret = extent_write_cache_pages(tree, mapping, wbc,
  2890. __extent_writepage, &epd,
  2891. flush_write_bio);
  2892. flush_epd_write_bio(&epd);
  2893. return ret;
  2894. }
  2895. int extent_readpages(struct extent_io_tree *tree,
  2896. struct address_space *mapping,
  2897. struct list_head *pages, unsigned nr_pages,
  2898. get_extent_t get_extent)
  2899. {
  2900. struct bio *bio = NULL;
  2901. unsigned page_idx;
  2902. unsigned long bio_flags = 0;
  2903. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  2904. struct page *page = list_entry(pages->prev, struct page, lru);
  2905. prefetchw(&page->flags);
  2906. list_del(&page->lru);
  2907. if (!add_to_page_cache_lru(page, mapping,
  2908. page->index, GFP_NOFS)) {
  2909. __extent_read_full_page(tree, page, get_extent,
  2910. &bio, 0, &bio_flags);
  2911. }
  2912. page_cache_release(page);
  2913. }
  2914. BUG_ON(!list_empty(pages));
  2915. if (bio)
  2916. submit_one_bio(READ, bio, 0, bio_flags);
  2917. return 0;
  2918. }
  2919. /*
  2920. * basic invalidatepage code, this waits on any locked or writeback
  2921. * ranges corresponding to the page, and then deletes any extent state
  2922. * records from the tree
  2923. */
  2924. int extent_invalidatepage(struct extent_io_tree *tree,
  2925. struct page *page, unsigned long offset)
  2926. {
  2927. struct extent_state *cached_state = NULL;
  2928. u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
  2929. u64 end = start + PAGE_CACHE_SIZE - 1;
  2930. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  2931. start += (offset + blocksize - 1) & ~(blocksize - 1);
  2932. if (start > end)
  2933. return 0;
  2934. lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
  2935. wait_on_page_writeback(page);
  2936. clear_extent_bit(tree, start, end,
  2937. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  2938. EXTENT_DO_ACCOUNTING,
  2939. 1, 1, &cached_state, GFP_NOFS);
  2940. return 0;
  2941. }
  2942. /*
  2943. * a helper for releasepage, this tests for areas of the page that
  2944. * are locked or under IO and drops the related state bits if it is safe
  2945. * to drop the page.
  2946. */
  2947. int try_release_extent_state(struct extent_map_tree *map,
  2948. struct extent_io_tree *tree, struct page *page,
  2949. gfp_t mask)
  2950. {
  2951. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2952. u64 end = start + PAGE_CACHE_SIZE - 1;
  2953. int ret = 1;
  2954. if (test_range_bit(tree, start, end,
  2955. EXTENT_IOBITS, 0, NULL))
  2956. ret = 0;
  2957. else {
  2958. if ((mask & GFP_NOFS) == GFP_NOFS)
  2959. mask = GFP_NOFS;
  2960. /*
  2961. * at this point we can safely clear everything except the
  2962. * locked bit and the nodatasum bit
  2963. */
  2964. ret = clear_extent_bit(tree, start, end,
  2965. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  2966. 0, 0, NULL, mask);
  2967. /* if clear_extent_bit failed for enomem reasons,
  2968. * we can't allow the release to continue.
  2969. */
  2970. if (ret < 0)
  2971. ret = 0;
  2972. else
  2973. ret = 1;
  2974. }
  2975. return ret;
  2976. }
  2977. /*
  2978. * a helper for releasepage. As long as there are no locked extents
  2979. * in the range corresponding to the page, both state records and extent
  2980. * map records are removed
  2981. */
  2982. int try_release_extent_mapping(struct extent_map_tree *map,
  2983. struct extent_io_tree *tree, struct page *page,
  2984. gfp_t mask)
  2985. {
  2986. struct extent_map *em;
  2987. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2988. u64 end = start + PAGE_CACHE_SIZE - 1;
  2989. if ((mask & __GFP_WAIT) &&
  2990. page->mapping->host->i_size > 16 * 1024 * 1024) {
  2991. u64 len;
  2992. while (start <= end) {
  2993. len = end - start + 1;
  2994. write_lock(&map->lock);
  2995. em = lookup_extent_mapping(map, start, len);
  2996. if (!em) {
  2997. write_unlock(&map->lock);
  2998. break;
  2999. }
  3000. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3001. em->start != start) {
  3002. write_unlock(&map->lock);
  3003. free_extent_map(em);
  3004. break;
  3005. }
  3006. if (!test_range_bit(tree, em->start,
  3007. extent_map_end(em) - 1,
  3008. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3009. 0, NULL)) {
  3010. remove_extent_mapping(map, em);
  3011. /* once for the rb tree */
  3012. free_extent_map(em);
  3013. }
  3014. start = extent_map_end(em);
  3015. write_unlock(&map->lock);
  3016. /* once for us */
  3017. free_extent_map(em);
  3018. }
  3019. }
  3020. return try_release_extent_state(map, tree, page, mask);
  3021. }
  3022. /*
  3023. * helper function for fiemap, which doesn't want to see any holes.
  3024. * This maps until we find something past 'last'
  3025. */
  3026. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3027. u64 offset,
  3028. u64 last,
  3029. get_extent_t *get_extent)
  3030. {
  3031. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3032. struct extent_map *em;
  3033. u64 len;
  3034. if (offset >= last)
  3035. return NULL;
  3036. while(1) {
  3037. len = last - offset;
  3038. if (len == 0)
  3039. break;
  3040. len = (len + sectorsize - 1) & ~(sectorsize - 1);
  3041. em = get_extent(inode, NULL, 0, offset, len, 0);
  3042. if (IS_ERR_OR_NULL(em))
  3043. return em;
  3044. /* if this isn't a hole return it */
  3045. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3046. em->block_start != EXTENT_MAP_HOLE) {
  3047. return em;
  3048. }
  3049. /* this is a hole, advance to the next extent */
  3050. offset = extent_map_end(em);
  3051. free_extent_map(em);
  3052. if (offset >= last)
  3053. break;
  3054. }
  3055. return NULL;
  3056. }
  3057. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3058. __u64 start, __u64 len, get_extent_t *get_extent)
  3059. {
  3060. int ret = 0;
  3061. u64 off = start;
  3062. u64 max = start + len;
  3063. u32 flags = 0;
  3064. u32 found_type;
  3065. u64 last;
  3066. u64 last_for_get_extent = 0;
  3067. u64 disko = 0;
  3068. u64 isize = i_size_read(inode);
  3069. struct btrfs_key found_key;
  3070. struct extent_map *em = NULL;
  3071. struct extent_state *cached_state = NULL;
  3072. struct btrfs_path *path;
  3073. struct btrfs_file_extent_item *item;
  3074. int end = 0;
  3075. u64 em_start = 0;
  3076. u64 em_len = 0;
  3077. u64 em_end = 0;
  3078. unsigned long emflags;
  3079. if (len == 0)
  3080. return -EINVAL;
  3081. path = btrfs_alloc_path();
  3082. if (!path)
  3083. return -ENOMEM;
  3084. path->leave_spinning = 1;
  3085. start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
  3086. len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
  3087. /*
  3088. * lookup the last file extent. We're not using i_size here
  3089. * because there might be preallocation past i_size
  3090. */
  3091. ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
  3092. path, btrfs_ino(inode), -1, 0);
  3093. if (ret < 0) {
  3094. btrfs_free_path(path);
  3095. return ret;
  3096. }
  3097. WARN_ON(!ret);
  3098. path->slots[0]--;
  3099. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3100. struct btrfs_file_extent_item);
  3101. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3102. found_type = btrfs_key_type(&found_key);
  3103. /* No extents, but there might be delalloc bits */
  3104. if (found_key.objectid != btrfs_ino(inode) ||
  3105. found_type != BTRFS_EXTENT_DATA_KEY) {
  3106. /* have to trust i_size as the end */
  3107. last = (u64)-1;
  3108. last_for_get_extent = isize;
  3109. } else {
  3110. /*
  3111. * remember the start of the last extent. There are a
  3112. * bunch of different factors that go into the length of the
  3113. * extent, so its much less complex to remember where it started
  3114. */
  3115. last = found_key.offset;
  3116. last_for_get_extent = last + 1;
  3117. }
  3118. btrfs_free_path(path);
  3119. /*
  3120. * we might have some extents allocated but more delalloc past those
  3121. * extents. so, we trust isize unless the start of the last extent is
  3122. * beyond isize
  3123. */
  3124. if (last < isize) {
  3125. last = (u64)-1;
  3126. last_for_get_extent = isize;
  3127. }
  3128. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
  3129. &cached_state, GFP_NOFS);
  3130. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3131. get_extent);
  3132. if (!em)
  3133. goto out;
  3134. if (IS_ERR(em)) {
  3135. ret = PTR_ERR(em);
  3136. goto out;
  3137. }
  3138. while (!end) {
  3139. u64 offset_in_extent;
  3140. /* break if the extent we found is outside the range */
  3141. if (em->start >= max || extent_map_end(em) < off)
  3142. break;
  3143. /*
  3144. * get_extent may return an extent that starts before our
  3145. * requested range. We have to make sure the ranges
  3146. * we return to fiemap always move forward and don't
  3147. * overlap, so adjust the offsets here
  3148. */
  3149. em_start = max(em->start, off);
  3150. /*
  3151. * record the offset from the start of the extent
  3152. * for adjusting the disk offset below
  3153. */
  3154. offset_in_extent = em_start - em->start;
  3155. em_end = extent_map_end(em);
  3156. em_len = em_end - em_start;
  3157. emflags = em->flags;
  3158. disko = 0;
  3159. flags = 0;
  3160. /*
  3161. * bump off for our next call to get_extent
  3162. */
  3163. off = extent_map_end(em);
  3164. if (off >= max)
  3165. end = 1;
  3166. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3167. end = 1;
  3168. flags |= FIEMAP_EXTENT_LAST;
  3169. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3170. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  3171. FIEMAP_EXTENT_NOT_ALIGNED);
  3172. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  3173. flags |= (FIEMAP_EXTENT_DELALLOC |
  3174. FIEMAP_EXTENT_UNKNOWN);
  3175. } else {
  3176. disko = em->block_start + offset_in_extent;
  3177. }
  3178. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3179. flags |= FIEMAP_EXTENT_ENCODED;
  3180. free_extent_map(em);
  3181. em = NULL;
  3182. if ((em_start >= last) || em_len == (u64)-1 ||
  3183. (last == (u64)-1 && isize <= em_end)) {
  3184. flags |= FIEMAP_EXTENT_LAST;
  3185. end = 1;
  3186. }
  3187. /* now scan forward to see if this is really the last extent. */
  3188. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  3189. get_extent);
  3190. if (IS_ERR(em)) {
  3191. ret = PTR_ERR(em);
  3192. goto out;
  3193. }
  3194. if (!em) {
  3195. flags |= FIEMAP_EXTENT_LAST;
  3196. end = 1;
  3197. }
  3198. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  3199. em_len, flags);
  3200. if (ret)
  3201. goto out_free;
  3202. }
  3203. out_free:
  3204. free_extent_map(em);
  3205. out:
  3206. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
  3207. &cached_state, GFP_NOFS);
  3208. return ret;
  3209. }
  3210. inline struct page *extent_buffer_page(struct extent_buffer *eb,
  3211. unsigned long i)
  3212. {
  3213. struct page *p;
  3214. struct address_space *mapping;
  3215. if (i == 0)
  3216. return eb->first_page;
  3217. i += eb->start >> PAGE_CACHE_SHIFT;
  3218. mapping = eb->first_page->mapping;
  3219. if (!mapping)
  3220. return NULL;
  3221. /*
  3222. * extent_buffer_page is only called after pinning the page
  3223. * by increasing the reference count. So we know the page must
  3224. * be in the radix tree.
  3225. */
  3226. rcu_read_lock();
  3227. p = radix_tree_lookup(&mapping->page_tree, i);
  3228. rcu_read_unlock();
  3229. return p;
  3230. }
  3231. inline unsigned long num_extent_pages(u64 start, u64 len)
  3232. {
  3233. return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
  3234. (start >> PAGE_CACHE_SHIFT);
  3235. }
  3236. static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
  3237. u64 start,
  3238. unsigned long len,
  3239. gfp_t mask)
  3240. {
  3241. struct extent_buffer *eb = NULL;
  3242. #if LEAK_DEBUG
  3243. unsigned long flags;
  3244. #endif
  3245. eb = kmem_cache_zalloc(extent_buffer_cache, mask);
  3246. if (eb == NULL)
  3247. return NULL;
  3248. eb->start = start;
  3249. eb->len = len;
  3250. rwlock_init(&eb->lock);
  3251. atomic_set(&eb->write_locks, 0);
  3252. atomic_set(&eb->read_locks, 0);
  3253. atomic_set(&eb->blocking_readers, 0);
  3254. atomic_set(&eb->blocking_writers, 0);
  3255. atomic_set(&eb->spinning_readers, 0);
  3256. atomic_set(&eb->spinning_writers, 0);
  3257. eb->lock_nested = 0;
  3258. init_waitqueue_head(&eb->write_lock_wq);
  3259. init_waitqueue_head(&eb->read_lock_wq);
  3260. #if LEAK_DEBUG
  3261. spin_lock_irqsave(&leak_lock, flags);
  3262. list_add(&eb->leak_list, &buffers);
  3263. spin_unlock_irqrestore(&leak_lock, flags);
  3264. #endif
  3265. atomic_set(&eb->refs, 1);
  3266. return eb;
  3267. }
  3268. static void __free_extent_buffer(struct extent_buffer *eb)
  3269. {
  3270. #if LEAK_DEBUG
  3271. unsigned long flags;
  3272. spin_lock_irqsave(&leak_lock, flags);
  3273. list_del(&eb->leak_list);
  3274. spin_unlock_irqrestore(&leak_lock, flags);
  3275. #endif
  3276. kmem_cache_free(extent_buffer_cache, eb);
  3277. }
  3278. /*
  3279. * Helper for releasing extent buffer page.
  3280. */
  3281. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
  3282. unsigned long start_idx)
  3283. {
  3284. unsigned long index;
  3285. struct page *page;
  3286. if (!eb->first_page)
  3287. return;
  3288. index = num_extent_pages(eb->start, eb->len);
  3289. if (start_idx >= index)
  3290. return;
  3291. do {
  3292. index--;
  3293. page = extent_buffer_page(eb, index);
  3294. if (page)
  3295. page_cache_release(page);
  3296. } while (index != start_idx);
  3297. }
  3298. /*
  3299. * Helper for releasing the extent buffer.
  3300. */
  3301. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  3302. {
  3303. btrfs_release_extent_buffer_page(eb, 0);
  3304. __free_extent_buffer(eb);
  3305. }
  3306. struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
  3307. u64 start, unsigned long len,
  3308. struct page *page0)
  3309. {
  3310. unsigned long num_pages = num_extent_pages(start, len);
  3311. unsigned long i;
  3312. unsigned long index = start >> PAGE_CACHE_SHIFT;
  3313. struct extent_buffer *eb;
  3314. struct extent_buffer *exists = NULL;
  3315. struct page *p;
  3316. struct address_space *mapping = tree->mapping;
  3317. int uptodate = 1;
  3318. int ret;
  3319. rcu_read_lock();
  3320. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3321. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3322. rcu_read_unlock();
  3323. mark_page_accessed(eb->first_page);
  3324. return eb;
  3325. }
  3326. rcu_read_unlock();
  3327. eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
  3328. if (!eb)
  3329. return NULL;
  3330. if (page0) {
  3331. eb->first_page = page0;
  3332. i = 1;
  3333. index++;
  3334. page_cache_get(page0);
  3335. mark_page_accessed(page0);
  3336. set_page_extent_mapped(page0);
  3337. set_page_extent_head(page0, len);
  3338. uptodate = PageUptodate(page0);
  3339. } else {
  3340. i = 0;
  3341. }
  3342. for (; i < num_pages; i++, index++) {
  3343. p = find_or_create_page(mapping, index, GFP_NOFS);
  3344. if (!p) {
  3345. WARN_ON(1);
  3346. goto free_eb;
  3347. }
  3348. set_page_extent_mapped(p);
  3349. mark_page_accessed(p);
  3350. if (i == 0) {
  3351. eb->first_page = p;
  3352. set_page_extent_head(p, len);
  3353. } else {
  3354. set_page_private(p, EXTENT_PAGE_PRIVATE);
  3355. }
  3356. if (!PageUptodate(p))
  3357. uptodate = 0;
  3358. /*
  3359. * see below about how we avoid a nasty race with release page
  3360. * and why we unlock later
  3361. */
  3362. if (i != 0)
  3363. unlock_page(p);
  3364. }
  3365. if (uptodate)
  3366. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3367. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  3368. if (ret)
  3369. goto free_eb;
  3370. spin_lock(&tree->buffer_lock);
  3371. ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
  3372. if (ret == -EEXIST) {
  3373. exists = radix_tree_lookup(&tree->buffer,
  3374. start >> PAGE_CACHE_SHIFT);
  3375. /* add one reference for the caller */
  3376. atomic_inc(&exists->refs);
  3377. spin_unlock(&tree->buffer_lock);
  3378. radix_tree_preload_end();
  3379. goto free_eb;
  3380. }
  3381. /* add one reference for the tree */
  3382. atomic_inc(&eb->refs);
  3383. spin_unlock(&tree->buffer_lock);
  3384. radix_tree_preload_end();
  3385. /*
  3386. * there is a race where release page may have
  3387. * tried to find this extent buffer in the radix
  3388. * but failed. It will tell the VM it is safe to
  3389. * reclaim the, and it will clear the page private bit.
  3390. * We must make sure to set the page private bit properly
  3391. * after the extent buffer is in the radix tree so
  3392. * it doesn't get lost
  3393. */
  3394. set_page_extent_mapped(eb->first_page);
  3395. set_page_extent_head(eb->first_page, eb->len);
  3396. if (!page0)
  3397. unlock_page(eb->first_page);
  3398. return eb;
  3399. free_eb:
  3400. if (eb->first_page && !page0)
  3401. unlock_page(eb->first_page);
  3402. if (!atomic_dec_and_test(&eb->refs))
  3403. return exists;
  3404. btrfs_release_extent_buffer(eb);
  3405. return exists;
  3406. }
  3407. struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
  3408. u64 start, unsigned long len)
  3409. {
  3410. struct extent_buffer *eb;
  3411. rcu_read_lock();
  3412. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3413. if (eb && atomic_inc_not_zero(&eb->refs)) {
  3414. rcu_read_unlock();
  3415. mark_page_accessed(eb->first_page);
  3416. return eb;
  3417. }
  3418. rcu_read_unlock();
  3419. return NULL;
  3420. }
  3421. void free_extent_buffer(struct extent_buffer *eb)
  3422. {
  3423. if (!eb)
  3424. return;
  3425. if (!atomic_dec_and_test(&eb->refs))
  3426. return;
  3427. WARN_ON(1);
  3428. }
  3429. int clear_extent_buffer_dirty(struct extent_io_tree *tree,
  3430. struct extent_buffer *eb)
  3431. {
  3432. unsigned long i;
  3433. unsigned long num_pages;
  3434. struct page *page;
  3435. num_pages = num_extent_pages(eb->start, eb->len);
  3436. for (i = 0; i < num_pages; i++) {
  3437. page = extent_buffer_page(eb, i);
  3438. if (!PageDirty(page))
  3439. continue;
  3440. lock_page(page);
  3441. WARN_ON(!PagePrivate(page));
  3442. set_page_extent_mapped(page);
  3443. if (i == 0)
  3444. set_page_extent_head(page, eb->len);
  3445. clear_page_dirty_for_io(page);
  3446. spin_lock_irq(&page->mapping->tree_lock);
  3447. if (!PageDirty(page)) {
  3448. radix_tree_tag_clear(&page->mapping->page_tree,
  3449. page_index(page),
  3450. PAGECACHE_TAG_DIRTY);
  3451. }
  3452. spin_unlock_irq(&page->mapping->tree_lock);
  3453. ClearPageError(page);
  3454. unlock_page(page);
  3455. }
  3456. return 0;
  3457. }
  3458. int set_extent_buffer_dirty(struct extent_io_tree *tree,
  3459. struct extent_buffer *eb)
  3460. {
  3461. unsigned long i;
  3462. unsigned long num_pages;
  3463. int was_dirty = 0;
  3464. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  3465. num_pages = num_extent_pages(eb->start, eb->len);
  3466. for (i = 0; i < num_pages; i++)
  3467. __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
  3468. return was_dirty;
  3469. }
  3470. static int __eb_straddles_pages(u64 start, u64 len)
  3471. {
  3472. if (len < PAGE_CACHE_SIZE)
  3473. return 1;
  3474. if (start & (PAGE_CACHE_SIZE - 1))
  3475. return 1;
  3476. if ((start + len) & (PAGE_CACHE_SIZE - 1))
  3477. return 1;
  3478. return 0;
  3479. }
  3480. static int eb_straddles_pages(struct extent_buffer *eb)
  3481. {
  3482. return __eb_straddles_pages(eb->start, eb->len);
  3483. }
  3484. int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
  3485. struct extent_buffer *eb,
  3486. struct extent_state **cached_state)
  3487. {
  3488. unsigned long i;
  3489. struct page *page;
  3490. unsigned long num_pages;
  3491. num_pages = num_extent_pages(eb->start, eb->len);
  3492. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3493. clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
  3494. cached_state, GFP_NOFS);
  3495. for (i = 0; i < num_pages; i++) {
  3496. page = extent_buffer_page(eb, i);
  3497. if (page)
  3498. ClearPageUptodate(page);
  3499. }
  3500. return 0;
  3501. }
  3502. int set_extent_buffer_uptodate(struct extent_io_tree *tree,
  3503. struct extent_buffer *eb)
  3504. {
  3505. unsigned long i;
  3506. struct page *page;
  3507. unsigned long num_pages;
  3508. num_pages = num_extent_pages(eb->start, eb->len);
  3509. if (eb_straddles_pages(eb)) {
  3510. set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
  3511. NULL, GFP_NOFS);
  3512. }
  3513. for (i = 0; i < num_pages; i++) {
  3514. page = extent_buffer_page(eb, i);
  3515. if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
  3516. ((i == num_pages - 1) &&
  3517. ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
  3518. check_page_uptodate(tree, page);
  3519. continue;
  3520. }
  3521. SetPageUptodate(page);
  3522. }
  3523. return 0;
  3524. }
  3525. int extent_range_uptodate(struct extent_io_tree *tree,
  3526. u64 start, u64 end)
  3527. {
  3528. struct page *page;
  3529. int ret;
  3530. int pg_uptodate = 1;
  3531. int uptodate;
  3532. unsigned long index;
  3533. if (__eb_straddles_pages(start, end - start + 1)) {
  3534. ret = test_range_bit(tree, start, end,
  3535. EXTENT_UPTODATE, 1, NULL);
  3536. if (ret)
  3537. return 1;
  3538. }
  3539. while (start <= end) {
  3540. index = start >> PAGE_CACHE_SHIFT;
  3541. page = find_get_page(tree->mapping, index);
  3542. if (!page)
  3543. return 1;
  3544. uptodate = PageUptodate(page);
  3545. page_cache_release(page);
  3546. if (!uptodate) {
  3547. pg_uptodate = 0;
  3548. break;
  3549. }
  3550. start += PAGE_CACHE_SIZE;
  3551. }
  3552. return pg_uptodate;
  3553. }
  3554. int extent_buffer_uptodate(struct extent_io_tree *tree,
  3555. struct extent_buffer *eb,
  3556. struct extent_state *cached_state)
  3557. {
  3558. int ret = 0;
  3559. unsigned long num_pages;
  3560. unsigned long i;
  3561. struct page *page;
  3562. int pg_uptodate = 1;
  3563. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  3564. return 1;
  3565. if (eb_straddles_pages(eb)) {
  3566. ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
  3567. EXTENT_UPTODATE, 1, cached_state);
  3568. if (ret)
  3569. return ret;
  3570. }
  3571. num_pages = num_extent_pages(eb->start, eb->len);
  3572. for (i = 0; i < num_pages; i++) {
  3573. page = extent_buffer_page(eb, i);
  3574. if (!PageUptodate(page)) {
  3575. pg_uptodate = 0;
  3576. break;
  3577. }
  3578. }
  3579. return pg_uptodate;
  3580. }
  3581. int read_extent_buffer_pages(struct extent_io_tree *tree,
  3582. struct extent_buffer *eb, u64 start, int wait,
  3583. get_extent_t *get_extent, int mirror_num)
  3584. {
  3585. unsigned long i;
  3586. unsigned long start_i;
  3587. struct page *page;
  3588. int err;
  3589. int ret = 0;
  3590. int locked_pages = 0;
  3591. int all_uptodate = 1;
  3592. int inc_all_pages = 0;
  3593. unsigned long num_pages;
  3594. struct bio *bio = NULL;
  3595. unsigned long bio_flags = 0;
  3596. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  3597. return 0;
  3598. if (eb_straddles_pages(eb)) {
  3599. if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
  3600. EXTENT_UPTODATE, 1, NULL)) {
  3601. return 0;
  3602. }
  3603. }
  3604. if (start) {
  3605. WARN_ON(start < eb->start);
  3606. start_i = (start >> PAGE_CACHE_SHIFT) -
  3607. (eb->start >> PAGE_CACHE_SHIFT);
  3608. } else {
  3609. start_i = 0;
  3610. }
  3611. num_pages = num_extent_pages(eb->start, eb->len);
  3612. for (i = start_i; i < num_pages; i++) {
  3613. page = extent_buffer_page(eb, i);
  3614. if (wait == WAIT_NONE) {
  3615. if (!trylock_page(page))
  3616. goto unlock_exit;
  3617. } else {
  3618. lock_page(page);
  3619. }
  3620. locked_pages++;
  3621. if (!PageUptodate(page))
  3622. all_uptodate = 0;
  3623. }
  3624. if (all_uptodate) {
  3625. if (start_i == 0)
  3626. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3627. goto unlock_exit;
  3628. }
  3629. for (i = start_i; i < num_pages; i++) {
  3630. page = extent_buffer_page(eb, i);
  3631. WARN_ON(!PagePrivate(page));
  3632. set_page_extent_mapped(page);
  3633. if (i == 0)
  3634. set_page_extent_head(page, eb->len);
  3635. if (inc_all_pages)
  3636. page_cache_get(page);
  3637. if (!PageUptodate(page)) {
  3638. if (start_i == 0)
  3639. inc_all_pages = 1;
  3640. ClearPageError(page);
  3641. err = __extent_read_full_page(tree, page,
  3642. get_extent, &bio,
  3643. mirror_num, &bio_flags);
  3644. if (err)
  3645. ret = err;
  3646. } else {
  3647. unlock_page(page);
  3648. }
  3649. }
  3650. if (bio)
  3651. submit_one_bio(READ, bio, mirror_num, bio_flags);
  3652. if (ret || wait != WAIT_COMPLETE)
  3653. return ret;
  3654. for (i = start_i; i < num_pages; i++) {
  3655. page = extent_buffer_page(eb, i);
  3656. wait_on_page_locked(page);
  3657. if (!PageUptodate(page))
  3658. ret = -EIO;
  3659. }
  3660. if (!ret)
  3661. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3662. return ret;
  3663. unlock_exit:
  3664. i = start_i;
  3665. while (locked_pages > 0) {
  3666. page = extent_buffer_page(eb, i);
  3667. i++;
  3668. unlock_page(page);
  3669. locked_pages--;
  3670. }
  3671. return ret;
  3672. }
  3673. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  3674. unsigned long start,
  3675. unsigned long len)
  3676. {
  3677. size_t cur;
  3678. size_t offset;
  3679. struct page *page;
  3680. char *kaddr;
  3681. char *dst = (char *)dstv;
  3682. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3683. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3684. WARN_ON(start > eb->len);
  3685. WARN_ON(start + len > eb->start + eb->len);
  3686. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3687. while (len > 0) {
  3688. page = extent_buffer_page(eb, i);
  3689. cur = min(len, (PAGE_CACHE_SIZE - offset));
  3690. kaddr = page_address(page);
  3691. memcpy(dst, kaddr + offset, cur);
  3692. dst += cur;
  3693. len -= cur;
  3694. offset = 0;
  3695. i++;
  3696. }
  3697. }
  3698. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  3699. unsigned long min_len, char **map,
  3700. unsigned long *map_start,
  3701. unsigned long *map_len)
  3702. {
  3703. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  3704. char *kaddr;
  3705. struct page *p;
  3706. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3707. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3708. unsigned long end_i = (start_offset + start + min_len - 1) >>
  3709. PAGE_CACHE_SHIFT;
  3710. if (i != end_i)
  3711. return -EINVAL;
  3712. if (i == 0) {
  3713. offset = start_offset;
  3714. *map_start = 0;
  3715. } else {
  3716. offset = 0;
  3717. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  3718. }
  3719. if (start + min_len > eb->len) {
  3720. printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  3721. "wanted %lu %lu\n", (unsigned long long)eb->start,
  3722. eb->len, start, min_len);
  3723. WARN_ON(1);
  3724. return -EINVAL;
  3725. }
  3726. p = extent_buffer_page(eb, i);
  3727. kaddr = page_address(p);
  3728. *map = kaddr + offset;
  3729. *map_len = PAGE_CACHE_SIZE - offset;
  3730. return 0;
  3731. }
  3732. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  3733. unsigned long start,
  3734. unsigned long len)
  3735. {
  3736. size_t cur;
  3737. size_t offset;
  3738. struct page *page;
  3739. char *kaddr;
  3740. char *ptr = (char *)ptrv;
  3741. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3742. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3743. int ret = 0;
  3744. WARN_ON(start > eb->len);
  3745. WARN_ON(start + len > eb->start + eb->len);
  3746. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3747. while (len > 0) {
  3748. page = extent_buffer_page(eb, i);
  3749. cur = min(len, (PAGE_CACHE_SIZE - offset));
  3750. kaddr = page_address(page);
  3751. ret = memcmp(ptr, kaddr + offset, cur);
  3752. if (ret)
  3753. break;
  3754. ptr += cur;
  3755. len -= cur;
  3756. offset = 0;
  3757. i++;
  3758. }
  3759. return ret;
  3760. }
  3761. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  3762. unsigned long start, unsigned long len)
  3763. {
  3764. size_t cur;
  3765. size_t offset;
  3766. struct page *page;
  3767. char *kaddr;
  3768. char *src = (char *)srcv;
  3769. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3770. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3771. WARN_ON(start > eb->len);
  3772. WARN_ON(start + len > eb->start + eb->len);
  3773. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3774. while (len > 0) {
  3775. page = extent_buffer_page(eb, i);
  3776. WARN_ON(!PageUptodate(page));
  3777. cur = min(len, PAGE_CACHE_SIZE - offset);
  3778. kaddr = page_address(page);
  3779. memcpy(kaddr + offset, src, cur);
  3780. src += cur;
  3781. len -= cur;
  3782. offset = 0;
  3783. i++;
  3784. }
  3785. }
  3786. void memset_extent_buffer(struct extent_buffer *eb, char c,
  3787. unsigned long start, unsigned long len)
  3788. {
  3789. size_t cur;
  3790. size_t offset;
  3791. struct page *page;
  3792. char *kaddr;
  3793. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3794. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3795. WARN_ON(start > eb->len);
  3796. WARN_ON(start + len > eb->start + eb->len);
  3797. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3798. while (len > 0) {
  3799. page = extent_buffer_page(eb, i);
  3800. WARN_ON(!PageUptodate(page));
  3801. cur = min(len, PAGE_CACHE_SIZE - offset);
  3802. kaddr = page_address(page);
  3803. memset(kaddr + offset, c, cur);
  3804. len -= cur;
  3805. offset = 0;
  3806. i++;
  3807. }
  3808. }
  3809. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  3810. unsigned long dst_offset, unsigned long src_offset,
  3811. unsigned long len)
  3812. {
  3813. u64 dst_len = dst->len;
  3814. size_t cur;
  3815. size_t offset;
  3816. struct page *page;
  3817. char *kaddr;
  3818. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3819. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  3820. WARN_ON(src->len != dst_len);
  3821. offset = (start_offset + dst_offset) &
  3822. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3823. while (len > 0) {
  3824. page = extent_buffer_page(dst, i);
  3825. WARN_ON(!PageUptodate(page));
  3826. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  3827. kaddr = page_address(page);
  3828. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  3829. src_offset += cur;
  3830. len -= cur;
  3831. offset = 0;
  3832. i++;
  3833. }
  3834. }
  3835. static void move_pages(struct page *dst_page, struct page *src_page,
  3836. unsigned long dst_off, unsigned long src_off,
  3837. unsigned long len)
  3838. {
  3839. char *dst_kaddr = page_address(dst_page);
  3840. if (dst_page == src_page) {
  3841. memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
  3842. } else {
  3843. char *src_kaddr = page_address(src_page);
  3844. char *p = dst_kaddr + dst_off + len;
  3845. char *s = src_kaddr + src_off + len;
  3846. while (len--)
  3847. *--p = *--s;
  3848. }
  3849. }
  3850. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  3851. {
  3852. unsigned long distance = (src > dst) ? src - dst : dst - src;
  3853. return distance < len;
  3854. }
  3855. static void copy_pages(struct page *dst_page, struct page *src_page,
  3856. unsigned long dst_off, unsigned long src_off,
  3857. unsigned long len)
  3858. {
  3859. char *dst_kaddr = page_address(dst_page);
  3860. char *src_kaddr;
  3861. if (dst_page != src_page) {
  3862. src_kaddr = page_address(src_page);
  3863. } else {
  3864. src_kaddr = dst_kaddr;
  3865. BUG_ON(areas_overlap(src_off, dst_off, len));
  3866. }
  3867. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  3868. }
  3869. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  3870. unsigned long src_offset, unsigned long len)
  3871. {
  3872. size_t cur;
  3873. size_t dst_off_in_page;
  3874. size_t src_off_in_page;
  3875. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3876. unsigned long dst_i;
  3877. unsigned long src_i;
  3878. if (src_offset + len > dst->len) {
  3879. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  3880. "len %lu dst len %lu\n", src_offset, len, dst->len);
  3881. BUG_ON(1);
  3882. }
  3883. if (dst_offset + len > dst->len) {
  3884. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  3885. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  3886. BUG_ON(1);
  3887. }
  3888. while (len > 0) {
  3889. dst_off_in_page = (start_offset + dst_offset) &
  3890. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3891. src_off_in_page = (start_offset + src_offset) &
  3892. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3893. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  3894. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  3895. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  3896. src_off_in_page));
  3897. cur = min_t(unsigned long, cur,
  3898. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  3899. copy_pages(extent_buffer_page(dst, dst_i),
  3900. extent_buffer_page(dst, src_i),
  3901. dst_off_in_page, src_off_in_page, cur);
  3902. src_offset += cur;
  3903. dst_offset += cur;
  3904. len -= cur;
  3905. }
  3906. }
  3907. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  3908. unsigned long src_offset, unsigned long len)
  3909. {
  3910. size_t cur;
  3911. size_t dst_off_in_page;
  3912. size_t src_off_in_page;
  3913. unsigned long dst_end = dst_offset + len - 1;
  3914. unsigned long src_end = src_offset + len - 1;
  3915. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3916. unsigned long dst_i;
  3917. unsigned long src_i;
  3918. if (src_offset + len > dst->len) {
  3919. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  3920. "len %lu len %lu\n", src_offset, len, dst->len);
  3921. BUG_ON(1);
  3922. }
  3923. if (dst_offset + len > dst->len) {
  3924. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  3925. "len %lu len %lu\n", dst_offset, len, dst->len);
  3926. BUG_ON(1);
  3927. }
  3928. if (!areas_overlap(src_offset, dst_offset, len)) {
  3929. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  3930. return;
  3931. }
  3932. while (len > 0) {
  3933. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  3934. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  3935. dst_off_in_page = (start_offset + dst_end) &
  3936. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3937. src_off_in_page = (start_offset + src_end) &
  3938. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3939. cur = min_t(unsigned long, len, src_off_in_page + 1);
  3940. cur = min(cur, dst_off_in_page + 1);
  3941. move_pages(extent_buffer_page(dst, dst_i),
  3942. extent_buffer_page(dst, src_i),
  3943. dst_off_in_page - cur + 1,
  3944. src_off_in_page - cur + 1, cur);
  3945. dst_end -= cur;
  3946. src_end -= cur;
  3947. len -= cur;
  3948. }
  3949. }
  3950. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  3951. {
  3952. struct extent_buffer *eb =
  3953. container_of(head, struct extent_buffer, rcu_head);
  3954. btrfs_release_extent_buffer(eb);
  3955. }
  3956. int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
  3957. {
  3958. u64 start = page_offset(page);
  3959. struct extent_buffer *eb;
  3960. int ret = 1;
  3961. spin_lock(&tree->buffer_lock);
  3962. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3963. if (!eb) {
  3964. spin_unlock(&tree->buffer_lock);
  3965. return ret;
  3966. }
  3967. if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3968. ret = 0;
  3969. goto out;
  3970. }
  3971. /*
  3972. * set @eb->refs to 0 if it is already 1, and then release the @eb.
  3973. * Or go back.
  3974. */
  3975. if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
  3976. ret = 0;
  3977. goto out;
  3978. }
  3979. radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3980. out:
  3981. spin_unlock(&tree->buffer_lock);
  3982. /* at this point we can safely release the extent buffer */
  3983. if (atomic_read(&eb->refs) == 0)
  3984. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  3985. return ret;
  3986. }