extent_io.c 103 KB

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