extent_io.c 108 KB

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