extent_io.c 109 KB

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