extent_io.c 95 KB

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