extent_io.c 93 KB

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