extent_io.c 99 KB

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