extent_io.c 91 KB

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