extent_io.c 92 KB

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