extent_io.c 92 KB

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