extent_io.c 94 KB

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