extent_io.c 97 KB

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