file.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include <linux/time.h>
  22. #include <linux/init.h>
  23. #include <linux/string.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/mpage.h>
  26. #include <linux/falloc.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/compat.h>
  30. #include <linux/slab.h>
  31. #include <linux/btrfs.h>
  32. #include <linux/uio.h>
  33. #include "ctree.h"
  34. #include "disk-io.h"
  35. #include "transaction.h"
  36. #include "btrfs_inode.h"
  37. #include "print-tree.h"
  38. #include "tree-log.h"
  39. #include "locking.h"
  40. #include "volumes.h"
  41. #include "qgroup.h"
  42. #include "compression.h"
  43. static struct kmem_cache *btrfs_inode_defrag_cachep;
  44. /*
  45. * when auto defrag is enabled we
  46. * queue up these defrag structs to remember which
  47. * inodes need defragging passes
  48. */
  49. struct inode_defrag {
  50. struct rb_node rb_node;
  51. /* objectid */
  52. u64 ino;
  53. /*
  54. * transid where the defrag was added, we search for
  55. * extents newer than this
  56. */
  57. u64 transid;
  58. /* root objectid */
  59. u64 root;
  60. /* last offset we were able to defrag */
  61. u64 last_offset;
  62. /* if we've wrapped around back to zero once already */
  63. int cycled;
  64. };
  65. static int __compare_inode_defrag(struct inode_defrag *defrag1,
  66. struct inode_defrag *defrag2)
  67. {
  68. if (defrag1->root > defrag2->root)
  69. return 1;
  70. else if (defrag1->root < defrag2->root)
  71. return -1;
  72. else if (defrag1->ino > defrag2->ino)
  73. return 1;
  74. else if (defrag1->ino < defrag2->ino)
  75. return -1;
  76. else
  77. return 0;
  78. }
  79. /* pop a record for an inode into the defrag tree. The lock
  80. * must be held already
  81. *
  82. * If you're inserting a record for an older transid than an
  83. * existing record, the transid already in the tree is lowered
  84. *
  85. * If an existing record is found the defrag item you
  86. * pass in is freed
  87. */
  88. static int __btrfs_add_inode_defrag(struct inode *inode,
  89. struct inode_defrag *defrag)
  90. {
  91. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  92. struct inode_defrag *entry;
  93. struct rb_node **p;
  94. struct rb_node *parent = NULL;
  95. int ret;
  96. p = &fs_info->defrag_inodes.rb_node;
  97. while (*p) {
  98. parent = *p;
  99. entry = rb_entry(parent, struct inode_defrag, rb_node);
  100. ret = __compare_inode_defrag(defrag, entry);
  101. if (ret < 0)
  102. p = &parent->rb_left;
  103. else if (ret > 0)
  104. p = &parent->rb_right;
  105. else {
  106. /* if we're reinserting an entry for
  107. * an old defrag run, make sure to
  108. * lower the transid of our existing record
  109. */
  110. if (defrag->transid < entry->transid)
  111. entry->transid = defrag->transid;
  112. if (defrag->last_offset > entry->last_offset)
  113. entry->last_offset = defrag->last_offset;
  114. return -EEXIST;
  115. }
  116. }
  117. set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
  118. rb_link_node(&defrag->rb_node, parent, p);
  119. rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes);
  120. return 0;
  121. }
  122. static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info)
  123. {
  124. if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
  125. return 0;
  126. if (btrfs_fs_closing(fs_info))
  127. return 0;
  128. return 1;
  129. }
  130. /*
  131. * insert a defrag record for this inode if auto defrag is
  132. * enabled
  133. */
  134. int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
  135. struct inode *inode)
  136. {
  137. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  138. struct btrfs_root *root = BTRFS_I(inode)->root;
  139. struct inode_defrag *defrag;
  140. u64 transid;
  141. int ret;
  142. if (!__need_auto_defrag(fs_info))
  143. return 0;
  144. if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
  145. return 0;
  146. if (trans)
  147. transid = trans->transid;
  148. else
  149. transid = BTRFS_I(inode)->root->last_trans;
  150. defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
  151. if (!defrag)
  152. return -ENOMEM;
  153. defrag->ino = btrfs_ino(inode);
  154. defrag->transid = transid;
  155. defrag->root = root->root_key.objectid;
  156. spin_lock(&fs_info->defrag_inodes_lock);
  157. if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
  158. /*
  159. * If we set IN_DEFRAG flag and evict the inode from memory,
  160. * and then re-read this inode, this new inode doesn't have
  161. * IN_DEFRAG flag. At the case, we may find the existed defrag.
  162. */
  163. ret = __btrfs_add_inode_defrag(inode, defrag);
  164. if (ret)
  165. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  166. } else {
  167. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  168. }
  169. spin_unlock(&fs_info->defrag_inodes_lock);
  170. return 0;
  171. }
  172. /*
  173. * Requeue the defrag object. If there is a defrag object that points to
  174. * the same inode in the tree, we will merge them together (by
  175. * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
  176. */
  177. static void btrfs_requeue_inode_defrag(struct inode *inode,
  178. struct inode_defrag *defrag)
  179. {
  180. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  181. int ret;
  182. if (!__need_auto_defrag(fs_info))
  183. goto out;
  184. /*
  185. * Here we don't check the IN_DEFRAG flag, because we need merge
  186. * them together.
  187. */
  188. spin_lock(&fs_info->defrag_inodes_lock);
  189. ret = __btrfs_add_inode_defrag(inode, defrag);
  190. spin_unlock(&fs_info->defrag_inodes_lock);
  191. if (ret)
  192. goto out;
  193. return;
  194. out:
  195. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  196. }
  197. /*
  198. * pick the defragable inode that we want, if it doesn't exist, we will get
  199. * the next one.
  200. */
  201. static struct inode_defrag *
  202. btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
  203. {
  204. struct inode_defrag *entry = NULL;
  205. struct inode_defrag tmp;
  206. struct rb_node *p;
  207. struct rb_node *parent = NULL;
  208. int ret;
  209. tmp.ino = ino;
  210. tmp.root = root;
  211. spin_lock(&fs_info->defrag_inodes_lock);
  212. p = fs_info->defrag_inodes.rb_node;
  213. while (p) {
  214. parent = p;
  215. entry = rb_entry(parent, struct inode_defrag, rb_node);
  216. ret = __compare_inode_defrag(&tmp, entry);
  217. if (ret < 0)
  218. p = parent->rb_left;
  219. else if (ret > 0)
  220. p = parent->rb_right;
  221. else
  222. goto out;
  223. }
  224. if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
  225. parent = rb_next(parent);
  226. if (parent)
  227. entry = rb_entry(parent, struct inode_defrag, rb_node);
  228. else
  229. entry = NULL;
  230. }
  231. out:
  232. if (entry)
  233. rb_erase(parent, &fs_info->defrag_inodes);
  234. spin_unlock(&fs_info->defrag_inodes_lock);
  235. return entry;
  236. }
  237. void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
  238. {
  239. struct inode_defrag *defrag;
  240. struct rb_node *node;
  241. spin_lock(&fs_info->defrag_inodes_lock);
  242. node = rb_first(&fs_info->defrag_inodes);
  243. while (node) {
  244. rb_erase(node, &fs_info->defrag_inodes);
  245. defrag = rb_entry(node, struct inode_defrag, rb_node);
  246. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  247. cond_resched_lock(&fs_info->defrag_inodes_lock);
  248. node = rb_first(&fs_info->defrag_inodes);
  249. }
  250. spin_unlock(&fs_info->defrag_inodes_lock);
  251. }
  252. #define BTRFS_DEFRAG_BATCH 1024
  253. static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
  254. struct inode_defrag *defrag)
  255. {
  256. struct btrfs_root *inode_root;
  257. struct inode *inode;
  258. struct btrfs_key key;
  259. struct btrfs_ioctl_defrag_range_args range;
  260. int num_defrag;
  261. int index;
  262. int ret;
  263. /* get the inode */
  264. key.objectid = defrag->root;
  265. key.type = BTRFS_ROOT_ITEM_KEY;
  266. key.offset = (u64)-1;
  267. index = srcu_read_lock(&fs_info->subvol_srcu);
  268. inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
  269. if (IS_ERR(inode_root)) {
  270. ret = PTR_ERR(inode_root);
  271. goto cleanup;
  272. }
  273. key.objectid = defrag->ino;
  274. key.type = BTRFS_INODE_ITEM_KEY;
  275. key.offset = 0;
  276. inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
  277. if (IS_ERR(inode)) {
  278. ret = PTR_ERR(inode);
  279. goto cleanup;
  280. }
  281. srcu_read_unlock(&fs_info->subvol_srcu, index);
  282. /* do a chunk of defrag */
  283. clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
  284. memset(&range, 0, sizeof(range));
  285. range.len = (u64)-1;
  286. range.start = defrag->last_offset;
  287. sb_start_write(fs_info->sb);
  288. num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
  289. BTRFS_DEFRAG_BATCH);
  290. sb_end_write(fs_info->sb);
  291. /*
  292. * if we filled the whole defrag batch, there
  293. * must be more work to do. Queue this defrag
  294. * again
  295. */
  296. if (num_defrag == BTRFS_DEFRAG_BATCH) {
  297. defrag->last_offset = range.start;
  298. btrfs_requeue_inode_defrag(inode, defrag);
  299. } else if (defrag->last_offset && !defrag->cycled) {
  300. /*
  301. * we didn't fill our defrag batch, but
  302. * we didn't start at zero. Make sure we loop
  303. * around to the start of the file.
  304. */
  305. defrag->last_offset = 0;
  306. defrag->cycled = 1;
  307. btrfs_requeue_inode_defrag(inode, defrag);
  308. } else {
  309. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  310. }
  311. iput(inode);
  312. return 0;
  313. cleanup:
  314. srcu_read_unlock(&fs_info->subvol_srcu, index);
  315. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  316. return ret;
  317. }
  318. /*
  319. * run through the list of inodes in the FS that need
  320. * defragging
  321. */
  322. int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
  323. {
  324. struct inode_defrag *defrag;
  325. u64 first_ino = 0;
  326. u64 root_objectid = 0;
  327. atomic_inc(&fs_info->defrag_running);
  328. while (1) {
  329. /* Pause the auto defragger. */
  330. if (test_bit(BTRFS_FS_STATE_REMOUNTING,
  331. &fs_info->fs_state))
  332. break;
  333. if (!__need_auto_defrag(fs_info))
  334. break;
  335. /* find an inode to defrag */
  336. defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
  337. first_ino);
  338. if (!defrag) {
  339. if (root_objectid || first_ino) {
  340. root_objectid = 0;
  341. first_ino = 0;
  342. continue;
  343. } else {
  344. break;
  345. }
  346. }
  347. first_ino = defrag->ino + 1;
  348. root_objectid = defrag->root;
  349. __btrfs_run_defrag_inode(fs_info, defrag);
  350. }
  351. atomic_dec(&fs_info->defrag_running);
  352. /*
  353. * during unmount, we use the transaction_wait queue to
  354. * wait for the defragger to stop
  355. */
  356. wake_up(&fs_info->transaction_wait);
  357. return 0;
  358. }
  359. /* simple helper to fault in pages and copy. This should go away
  360. * and be replaced with calls into generic code.
  361. */
  362. static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
  363. struct page **prepared_pages,
  364. struct iov_iter *i)
  365. {
  366. size_t copied = 0;
  367. size_t total_copied = 0;
  368. int pg = 0;
  369. int offset = pos & (PAGE_SIZE - 1);
  370. while (write_bytes > 0) {
  371. size_t count = min_t(size_t,
  372. PAGE_SIZE - offset, write_bytes);
  373. struct page *page = prepared_pages[pg];
  374. /*
  375. * Copy data from userspace to the current page
  376. */
  377. copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
  378. /* Flush processor's dcache for this page */
  379. flush_dcache_page(page);
  380. /*
  381. * if we get a partial write, we can end up with
  382. * partially up to date pages. These add
  383. * a lot of complexity, so make sure they don't
  384. * happen by forcing this copy to be retried.
  385. *
  386. * The rest of the btrfs_file_write code will fall
  387. * back to page at a time copies after we return 0.
  388. */
  389. if (!PageUptodate(page) && copied < count)
  390. copied = 0;
  391. iov_iter_advance(i, copied);
  392. write_bytes -= copied;
  393. total_copied += copied;
  394. /* Return to btrfs_file_write_iter to fault page */
  395. if (unlikely(copied == 0))
  396. break;
  397. if (copied < PAGE_SIZE - offset) {
  398. offset += copied;
  399. } else {
  400. pg++;
  401. offset = 0;
  402. }
  403. }
  404. return total_copied;
  405. }
  406. /*
  407. * unlocks pages after btrfs_file_write is done with them
  408. */
  409. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  410. {
  411. size_t i;
  412. for (i = 0; i < num_pages; i++) {
  413. /* page checked is some magic around finding pages that
  414. * have been modified without going through btrfs_set_page_dirty
  415. * clear it here. There should be no need to mark the pages
  416. * accessed as prepare_pages should have marked them accessed
  417. * in prepare_pages via find_or_create_page()
  418. */
  419. ClearPageChecked(pages[i]);
  420. unlock_page(pages[i]);
  421. put_page(pages[i]);
  422. }
  423. }
  424. /*
  425. * after copy_from_user, pages need to be dirtied and we need to make
  426. * sure holes are created between the current EOF and the start of
  427. * any next extents (if required).
  428. *
  429. * this also makes the decision about creating an inline extent vs
  430. * doing real data extents, marking pages dirty and delalloc as required.
  431. */
  432. int btrfs_dirty_pages(struct inode *inode, struct page **pages,
  433. size_t num_pages, loff_t pos, size_t write_bytes,
  434. struct extent_state **cached)
  435. {
  436. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  437. int err = 0;
  438. int i;
  439. u64 num_bytes;
  440. u64 start_pos;
  441. u64 end_of_last_block;
  442. u64 end_pos = pos + write_bytes;
  443. loff_t isize = i_size_read(inode);
  444. start_pos = pos & ~((u64) fs_info->sectorsize - 1);
  445. num_bytes = round_up(write_bytes + pos - start_pos,
  446. fs_info->sectorsize);
  447. end_of_last_block = start_pos + num_bytes - 1;
  448. err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
  449. cached, 0);
  450. if (err)
  451. return err;
  452. for (i = 0; i < num_pages; i++) {
  453. struct page *p = pages[i];
  454. SetPageUptodate(p);
  455. ClearPageChecked(p);
  456. set_page_dirty(p);
  457. }
  458. /*
  459. * we've only changed i_size in ram, and we haven't updated
  460. * the disk i_size. There is no need to log the inode
  461. * at this time.
  462. */
  463. if (end_pos > isize)
  464. i_size_write(inode, end_pos);
  465. return 0;
  466. }
  467. /*
  468. * this drops all the extents in the cache that intersect the range
  469. * [start, end]. Existing extents are split as required.
  470. */
  471. void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  472. int skip_pinned)
  473. {
  474. struct extent_map *em;
  475. struct extent_map *split = NULL;
  476. struct extent_map *split2 = NULL;
  477. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  478. u64 len = end - start + 1;
  479. u64 gen;
  480. int ret;
  481. int testend = 1;
  482. unsigned long flags;
  483. int compressed = 0;
  484. bool modified;
  485. WARN_ON(end < start);
  486. if (end == (u64)-1) {
  487. len = (u64)-1;
  488. testend = 0;
  489. }
  490. while (1) {
  491. int no_splits = 0;
  492. modified = false;
  493. if (!split)
  494. split = alloc_extent_map();
  495. if (!split2)
  496. split2 = alloc_extent_map();
  497. if (!split || !split2)
  498. no_splits = 1;
  499. write_lock(&em_tree->lock);
  500. em = lookup_extent_mapping(em_tree, start, len);
  501. if (!em) {
  502. write_unlock(&em_tree->lock);
  503. break;
  504. }
  505. flags = em->flags;
  506. gen = em->generation;
  507. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  508. if (testend && em->start + em->len >= start + len) {
  509. free_extent_map(em);
  510. write_unlock(&em_tree->lock);
  511. break;
  512. }
  513. start = em->start + em->len;
  514. if (testend)
  515. len = start + len - (em->start + em->len);
  516. free_extent_map(em);
  517. write_unlock(&em_tree->lock);
  518. continue;
  519. }
  520. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  521. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  522. clear_bit(EXTENT_FLAG_LOGGING, &flags);
  523. modified = !list_empty(&em->list);
  524. if (no_splits)
  525. goto next;
  526. if (em->start < start) {
  527. split->start = em->start;
  528. split->len = start - em->start;
  529. if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  530. split->orig_start = em->orig_start;
  531. split->block_start = em->block_start;
  532. if (compressed)
  533. split->block_len = em->block_len;
  534. else
  535. split->block_len = split->len;
  536. split->orig_block_len = max(split->block_len,
  537. em->orig_block_len);
  538. split->ram_bytes = em->ram_bytes;
  539. } else {
  540. split->orig_start = split->start;
  541. split->block_len = 0;
  542. split->block_start = em->block_start;
  543. split->orig_block_len = 0;
  544. split->ram_bytes = split->len;
  545. }
  546. split->generation = gen;
  547. split->bdev = em->bdev;
  548. split->flags = flags;
  549. split->compress_type = em->compress_type;
  550. replace_extent_mapping(em_tree, em, split, modified);
  551. free_extent_map(split);
  552. split = split2;
  553. split2 = NULL;
  554. }
  555. if (testend && em->start + em->len > start + len) {
  556. u64 diff = start + len - em->start;
  557. split->start = start + len;
  558. split->len = em->start + em->len - (start + len);
  559. split->bdev = em->bdev;
  560. split->flags = flags;
  561. split->compress_type = em->compress_type;
  562. split->generation = gen;
  563. if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  564. split->orig_block_len = max(em->block_len,
  565. em->orig_block_len);
  566. split->ram_bytes = em->ram_bytes;
  567. if (compressed) {
  568. split->block_len = em->block_len;
  569. split->block_start = em->block_start;
  570. split->orig_start = em->orig_start;
  571. } else {
  572. split->block_len = split->len;
  573. split->block_start = em->block_start
  574. + diff;
  575. split->orig_start = em->orig_start;
  576. }
  577. } else {
  578. split->ram_bytes = split->len;
  579. split->orig_start = split->start;
  580. split->block_len = 0;
  581. split->block_start = em->block_start;
  582. split->orig_block_len = 0;
  583. }
  584. if (extent_map_in_tree(em)) {
  585. replace_extent_mapping(em_tree, em, split,
  586. modified);
  587. } else {
  588. ret = add_extent_mapping(em_tree, split,
  589. modified);
  590. ASSERT(ret == 0); /* Logic error */
  591. }
  592. free_extent_map(split);
  593. split = NULL;
  594. }
  595. next:
  596. if (extent_map_in_tree(em))
  597. remove_extent_mapping(em_tree, em);
  598. write_unlock(&em_tree->lock);
  599. /* once for us */
  600. free_extent_map(em);
  601. /* once for the tree*/
  602. free_extent_map(em);
  603. }
  604. if (split)
  605. free_extent_map(split);
  606. if (split2)
  607. free_extent_map(split2);
  608. }
  609. /*
  610. * this is very complex, but the basic idea is to drop all extents
  611. * in the range start - end. hint_block is filled in with a block number
  612. * that would be a good hint to the block allocator for this file.
  613. *
  614. * If an extent intersects the range but is not entirely inside the range
  615. * it is either truncated or split. Anything entirely inside the range
  616. * is deleted from the tree.
  617. */
  618. int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
  619. struct btrfs_root *root, struct inode *inode,
  620. struct btrfs_path *path, u64 start, u64 end,
  621. u64 *drop_end, int drop_cache,
  622. int replace_extent,
  623. u32 extent_item_size,
  624. int *key_inserted)
  625. {
  626. struct btrfs_fs_info *fs_info = root->fs_info;
  627. struct extent_buffer *leaf;
  628. struct btrfs_file_extent_item *fi;
  629. struct btrfs_key key;
  630. struct btrfs_key new_key;
  631. u64 ino = btrfs_ino(inode);
  632. u64 search_start = start;
  633. u64 disk_bytenr = 0;
  634. u64 num_bytes = 0;
  635. u64 extent_offset = 0;
  636. u64 extent_end = 0;
  637. u64 last_end = start;
  638. int del_nr = 0;
  639. int del_slot = 0;
  640. int extent_type;
  641. int recow;
  642. int ret;
  643. int modify_tree = -1;
  644. int update_refs;
  645. int found = 0;
  646. int leafs_visited = 0;
  647. if (drop_cache)
  648. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  649. if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)
  650. modify_tree = 0;
  651. update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  652. root == fs_info->tree_root);
  653. while (1) {
  654. recow = 0;
  655. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  656. search_start, modify_tree);
  657. if (ret < 0)
  658. break;
  659. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  660. leaf = path->nodes[0];
  661. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  662. if (key.objectid == ino &&
  663. key.type == BTRFS_EXTENT_DATA_KEY)
  664. path->slots[0]--;
  665. }
  666. ret = 0;
  667. leafs_visited++;
  668. next_slot:
  669. leaf = path->nodes[0];
  670. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  671. BUG_ON(del_nr > 0);
  672. ret = btrfs_next_leaf(root, path);
  673. if (ret < 0)
  674. break;
  675. if (ret > 0) {
  676. ret = 0;
  677. break;
  678. }
  679. leafs_visited++;
  680. leaf = path->nodes[0];
  681. recow = 1;
  682. }
  683. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  684. if (key.objectid > ino)
  685. break;
  686. if (WARN_ON_ONCE(key.objectid < ino) ||
  687. key.type < BTRFS_EXTENT_DATA_KEY) {
  688. ASSERT(del_nr == 0);
  689. path->slots[0]++;
  690. goto next_slot;
  691. }
  692. if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  693. break;
  694. fi = btrfs_item_ptr(leaf, path->slots[0],
  695. struct btrfs_file_extent_item);
  696. extent_type = btrfs_file_extent_type(leaf, fi);
  697. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  698. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  699. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  700. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  701. extent_offset = btrfs_file_extent_offset(leaf, fi);
  702. extent_end = key.offset +
  703. btrfs_file_extent_num_bytes(leaf, fi);
  704. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  705. extent_end = key.offset +
  706. btrfs_file_extent_inline_len(leaf,
  707. path->slots[0], fi);
  708. } else {
  709. /* can't happen */
  710. BUG();
  711. }
  712. /*
  713. * Don't skip extent items representing 0 byte lengths. They
  714. * used to be created (bug) if while punching holes we hit
  715. * -ENOSPC condition. So if we find one here, just ensure we
  716. * delete it, otherwise we would insert a new file extent item
  717. * with the same key (offset) as that 0 bytes length file
  718. * extent item in the call to setup_items_for_insert() later
  719. * in this function.
  720. */
  721. if (extent_end == key.offset && extent_end >= search_start) {
  722. last_end = extent_end;
  723. goto delete_extent_item;
  724. }
  725. if (extent_end <= search_start) {
  726. path->slots[0]++;
  727. goto next_slot;
  728. }
  729. found = 1;
  730. search_start = max(key.offset, start);
  731. if (recow || !modify_tree) {
  732. modify_tree = -1;
  733. btrfs_release_path(path);
  734. continue;
  735. }
  736. /*
  737. * | - range to drop - |
  738. * | -------- extent -------- |
  739. */
  740. if (start > key.offset && end < extent_end) {
  741. BUG_ON(del_nr > 0);
  742. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  743. ret = -EOPNOTSUPP;
  744. break;
  745. }
  746. memcpy(&new_key, &key, sizeof(new_key));
  747. new_key.offset = start;
  748. ret = btrfs_duplicate_item(trans, root, path,
  749. &new_key);
  750. if (ret == -EAGAIN) {
  751. btrfs_release_path(path);
  752. continue;
  753. }
  754. if (ret < 0)
  755. break;
  756. leaf = path->nodes[0];
  757. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  758. struct btrfs_file_extent_item);
  759. btrfs_set_file_extent_num_bytes(leaf, fi,
  760. start - key.offset);
  761. fi = btrfs_item_ptr(leaf, path->slots[0],
  762. struct btrfs_file_extent_item);
  763. extent_offset += start - key.offset;
  764. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  765. btrfs_set_file_extent_num_bytes(leaf, fi,
  766. extent_end - start);
  767. btrfs_mark_buffer_dirty(leaf);
  768. if (update_refs && disk_bytenr > 0) {
  769. ret = btrfs_inc_extent_ref(trans, fs_info,
  770. disk_bytenr, num_bytes, 0,
  771. root->root_key.objectid,
  772. new_key.objectid,
  773. start - extent_offset);
  774. BUG_ON(ret); /* -ENOMEM */
  775. }
  776. key.offset = start;
  777. }
  778. /*
  779. * From here on out we will have actually dropped something, so
  780. * last_end can be updated.
  781. */
  782. last_end = extent_end;
  783. /*
  784. * | ---- range to drop ----- |
  785. * | -------- extent -------- |
  786. */
  787. if (start <= key.offset && end < extent_end) {
  788. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  789. ret = -EOPNOTSUPP;
  790. break;
  791. }
  792. memcpy(&new_key, &key, sizeof(new_key));
  793. new_key.offset = end;
  794. btrfs_set_item_key_safe(fs_info, path, &new_key);
  795. extent_offset += end - key.offset;
  796. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  797. btrfs_set_file_extent_num_bytes(leaf, fi,
  798. extent_end - end);
  799. btrfs_mark_buffer_dirty(leaf);
  800. if (update_refs && disk_bytenr > 0)
  801. inode_sub_bytes(inode, end - key.offset);
  802. break;
  803. }
  804. search_start = extent_end;
  805. /*
  806. * | ---- range to drop ----- |
  807. * | -------- extent -------- |
  808. */
  809. if (start > key.offset && end >= extent_end) {
  810. BUG_ON(del_nr > 0);
  811. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  812. ret = -EOPNOTSUPP;
  813. break;
  814. }
  815. btrfs_set_file_extent_num_bytes(leaf, fi,
  816. start - key.offset);
  817. btrfs_mark_buffer_dirty(leaf);
  818. if (update_refs && disk_bytenr > 0)
  819. inode_sub_bytes(inode, extent_end - start);
  820. if (end == extent_end)
  821. break;
  822. path->slots[0]++;
  823. goto next_slot;
  824. }
  825. /*
  826. * | ---- range to drop ----- |
  827. * | ------ extent ------ |
  828. */
  829. if (start <= key.offset && end >= extent_end) {
  830. delete_extent_item:
  831. if (del_nr == 0) {
  832. del_slot = path->slots[0];
  833. del_nr = 1;
  834. } else {
  835. BUG_ON(del_slot + del_nr != path->slots[0]);
  836. del_nr++;
  837. }
  838. if (update_refs &&
  839. extent_type == BTRFS_FILE_EXTENT_INLINE) {
  840. inode_sub_bytes(inode,
  841. extent_end - key.offset);
  842. extent_end = ALIGN(extent_end,
  843. fs_info->sectorsize);
  844. } else if (update_refs && disk_bytenr > 0) {
  845. ret = btrfs_free_extent(trans, fs_info,
  846. disk_bytenr, num_bytes, 0,
  847. root->root_key.objectid,
  848. key.objectid, key.offset -
  849. extent_offset);
  850. BUG_ON(ret); /* -ENOMEM */
  851. inode_sub_bytes(inode,
  852. extent_end - key.offset);
  853. }
  854. if (end == extent_end)
  855. break;
  856. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  857. path->slots[0]++;
  858. goto next_slot;
  859. }
  860. ret = btrfs_del_items(trans, root, path, del_slot,
  861. del_nr);
  862. if (ret) {
  863. btrfs_abort_transaction(trans, ret);
  864. break;
  865. }
  866. del_nr = 0;
  867. del_slot = 0;
  868. btrfs_release_path(path);
  869. continue;
  870. }
  871. BUG_ON(1);
  872. }
  873. if (!ret && del_nr > 0) {
  874. /*
  875. * Set path->slots[0] to first slot, so that after the delete
  876. * if items are move off from our leaf to its immediate left or
  877. * right neighbor leafs, we end up with a correct and adjusted
  878. * path->slots[0] for our insertion (if replace_extent != 0).
  879. */
  880. path->slots[0] = del_slot;
  881. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  882. if (ret)
  883. btrfs_abort_transaction(trans, ret);
  884. }
  885. leaf = path->nodes[0];
  886. /*
  887. * If btrfs_del_items() was called, it might have deleted a leaf, in
  888. * which case it unlocked our path, so check path->locks[0] matches a
  889. * write lock.
  890. */
  891. if (!ret && replace_extent && leafs_visited == 1 &&
  892. (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
  893. path->locks[0] == BTRFS_WRITE_LOCK) &&
  894. btrfs_leaf_free_space(fs_info, leaf) >=
  895. sizeof(struct btrfs_item) + extent_item_size) {
  896. key.objectid = ino;
  897. key.type = BTRFS_EXTENT_DATA_KEY;
  898. key.offset = start;
  899. if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
  900. struct btrfs_key slot_key;
  901. btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
  902. if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
  903. path->slots[0]++;
  904. }
  905. setup_items_for_insert(root, path, &key,
  906. &extent_item_size,
  907. extent_item_size,
  908. sizeof(struct btrfs_item) +
  909. extent_item_size, 1);
  910. *key_inserted = 1;
  911. }
  912. if (!replace_extent || !(*key_inserted))
  913. btrfs_release_path(path);
  914. if (drop_end)
  915. *drop_end = found ? min(end, last_end) : end;
  916. return ret;
  917. }
  918. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  919. struct btrfs_root *root, struct inode *inode, u64 start,
  920. u64 end, int drop_cache)
  921. {
  922. struct btrfs_path *path;
  923. int ret;
  924. path = btrfs_alloc_path();
  925. if (!path)
  926. return -ENOMEM;
  927. ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
  928. drop_cache, 0, 0, NULL);
  929. btrfs_free_path(path);
  930. return ret;
  931. }
  932. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  933. u64 objectid, u64 bytenr, u64 orig_offset,
  934. u64 *start, u64 *end)
  935. {
  936. struct btrfs_file_extent_item *fi;
  937. struct btrfs_key key;
  938. u64 extent_end;
  939. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  940. return 0;
  941. btrfs_item_key_to_cpu(leaf, &key, slot);
  942. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  943. return 0;
  944. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  945. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  946. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  947. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  948. btrfs_file_extent_compression(leaf, fi) ||
  949. btrfs_file_extent_encryption(leaf, fi) ||
  950. btrfs_file_extent_other_encoding(leaf, fi))
  951. return 0;
  952. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  953. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  954. return 0;
  955. *start = key.offset;
  956. *end = extent_end;
  957. return 1;
  958. }
  959. /*
  960. * Mark extent in the range start - end as written.
  961. *
  962. * This changes extent type from 'pre-allocated' to 'regular'. If only
  963. * part of extent is marked as written, the extent will be split into
  964. * two or three.
  965. */
  966. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  967. struct inode *inode, u64 start, u64 end)
  968. {
  969. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  970. struct btrfs_root *root = BTRFS_I(inode)->root;
  971. struct extent_buffer *leaf;
  972. struct btrfs_path *path;
  973. struct btrfs_file_extent_item *fi;
  974. struct btrfs_key key;
  975. struct btrfs_key new_key;
  976. u64 bytenr;
  977. u64 num_bytes;
  978. u64 extent_end;
  979. u64 orig_offset;
  980. u64 other_start;
  981. u64 other_end;
  982. u64 split;
  983. int del_nr = 0;
  984. int del_slot = 0;
  985. int recow;
  986. int ret;
  987. u64 ino = btrfs_ino(inode);
  988. path = btrfs_alloc_path();
  989. if (!path)
  990. return -ENOMEM;
  991. again:
  992. recow = 0;
  993. split = start;
  994. key.objectid = ino;
  995. key.type = BTRFS_EXTENT_DATA_KEY;
  996. key.offset = split;
  997. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  998. if (ret < 0)
  999. goto out;
  1000. if (ret > 0 && path->slots[0] > 0)
  1001. path->slots[0]--;
  1002. leaf = path->nodes[0];
  1003. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1004. if (key.objectid != ino ||
  1005. key.type != BTRFS_EXTENT_DATA_KEY) {
  1006. ret = -EINVAL;
  1007. btrfs_abort_transaction(trans, ret);
  1008. goto out;
  1009. }
  1010. fi = btrfs_item_ptr(leaf, path->slots[0],
  1011. struct btrfs_file_extent_item);
  1012. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
  1013. ret = -EINVAL;
  1014. btrfs_abort_transaction(trans, ret);
  1015. goto out;
  1016. }
  1017. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  1018. if (key.offset > start || extent_end < end) {
  1019. ret = -EINVAL;
  1020. btrfs_abort_transaction(trans, ret);
  1021. goto out;
  1022. }
  1023. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1024. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  1025. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  1026. memcpy(&new_key, &key, sizeof(new_key));
  1027. if (start == key.offset && end < extent_end) {
  1028. other_start = 0;
  1029. other_end = start;
  1030. if (extent_mergeable(leaf, path->slots[0] - 1,
  1031. ino, bytenr, orig_offset,
  1032. &other_start, &other_end)) {
  1033. new_key.offset = end;
  1034. btrfs_set_item_key_safe(fs_info, path, &new_key);
  1035. fi = btrfs_item_ptr(leaf, path->slots[0],
  1036. struct btrfs_file_extent_item);
  1037. btrfs_set_file_extent_generation(leaf, fi,
  1038. trans->transid);
  1039. btrfs_set_file_extent_num_bytes(leaf, fi,
  1040. extent_end - end);
  1041. btrfs_set_file_extent_offset(leaf, fi,
  1042. end - orig_offset);
  1043. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  1044. struct btrfs_file_extent_item);
  1045. btrfs_set_file_extent_generation(leaf, fi,
  1046. trans->transid);
  1047. btrfs_set_file_extent_num_bytes(leaf, fi,
  1048. end - other_start);
  1049. btrfs_mark_buffer_dirty(leaf);
  1050. goto out;
  1051. }
  1052. }
  1053. if (start > key.offset && end == extent_end) {
  1054. other_start = end;
  1055. other_end = 0;
  1056. if (extent_mergeable(leaf, path->slots[0] + 1,
  1057. ino, bytenr, orig_offset,
  1058. &other_start, &other_end)) {
  1059. fi = btrfs_item_ptr(leaf, path->slots[0],
  1060. struct btrfs_file_extent_item);
  1061. btrfs_set_file_extent_num_bytes(leaf, fi,
  1062. start - key.offset);
  1063. btrfs_set_file_extent_generation(leaf, fi,
  1064. trans->transid);
  1065. path->slots[0]++;
  1066. new_key.offset = start;
  1067. btrfs_set_item_key_safe(fs_info, path, &new_key);
  1068. fi = btrfs_item_ptr(leaf, path->slots[0],
  1069. struct btrfs_file_extent_item);
  1070. btrfs_set_file_extent_generation(leaf, fi,
  1071. trans->transid);
  1072. btrfs_set_file_extent_num_bytes(leaf, fi,
  1073. other_end - start);
  1074. btrfs_set_file_extent_offset(leaf, fi,
  1075. start - orig_offset);
  1076. btrfs_mark_buffer_dirty(leaf);
  1077. goto out;
  1078. }
  1079. }
  1080. while (start > key.offset || end < extent_end) {
  1081. if (key.offset == start)
  1082. split = end;
  1083. new_key.offset = split;
  1084. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  1085. if (ret == -EAGAIN) {
  1086. btrfs_release_path(path);
  1087. goto again;
  1088. }
  1089. if (ret < 0) {
  1090. btrfs_abort_transaction(trans, ret);
  1091. goto out;
  1092. }
  1093. leaf = path->nodes[0];
  1094. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  1095. struct btrfs_file_extent_item);
  1096. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1097. btrfs_set_file_extent_num_bytes(leaf, fi,
  1098. split - key.offset);
  1099. fi = btrfs_item_ptr(leaf, path->slots[0],
  1100. struct btrfs_file_extent_item);
  1101. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1102. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  1103. btrfs_set_file_extent_num_bytes(leaf, fi,
  1104. extent_end - split);
  1105. btrfs_mark_buffer_dirty(leaf);
  1106. ret = btrfs_inc_extent_ref(trans, fs_info, bytenr, num_bytes,
  1107. 0, root->root_key.objectid,
  1108. ino, orig_offset);
  1109. if (ret) {
  1110. btrfs_abort_transaction(trans, ret);
  1111. goto out;
  1112. }
  1113. if (split == start) {
  1114. key.offset = start;
  1115. } else {
  1116. if (start != key.offset) {
  1117. ret = -EINVAL;
  1118. btrfs_abort_transaction(trans, ret);
  1119. goto out;
  1120. }
  1121. path->slots[0]--;
  1122. extent_end = end;
  1123. }
  1124. recow = 1;
  1125. }
  1126. other_start = end;
  1127. other_end = 0;
  1128. if (extent_mergeable(leaf, path->slots[0] + 1,
  1129. ino, bytenr, orig_offset,
  1130. &other_start, &other_end)) {
  1131. if (recow) {
  1132. btrfs_release_path(path);
  1133. goto again;
  1134. }
  1135. extent_end = other_end;
  1136. del_slot = path->slots[0] + 1;
  1137. del_nr++;
  1138. ret = btrfs_free_extent(trans, fs_info, bytenr, num_bytes,
  1139. 0, root->root_key.objectid,
  1140. ino, orig_offset);
  1141. if (ret) {
  1142. btrfs_abort_transaction(trans, ret);
  1143. goto out;
  1144. }
  1145. }
  1146. other_start = 0;
  1147. other_end = start;
  1148. if (extent_mergeable(leaf, path->slots[0] - 1,
  1149. ino, bytenr, orig_offset,
  1150. &other_start, &other_end)) {
  1151. if (recow) {
  1152. btrfs_release_path(path);
  1153. goto again;
  1154. }
  1155. key.offset = other_start;
  1156. del_slot = path->slots[0];
  1157. del_nr++;
  1158. ret = btrfs_free_extent(trans, fs_info, bytenr, num_bytes,
  1159. 0, root->root_key.objectid,
  1160. ino, orig_offset);
  1161. if (ret) {
  1162. btrfs_abort_transaction(trans, ret);
  1163. goto out;
  1164. }
  1165. }
  1166. if (del_nr == 0) {
  1167. fi = btrfs_item_ptr(leaf, path->slots[0],
  1168. struct btrfs_file_extent_item);
  1169. btrfs_set_file_extent_type(leaf, fi,
  1170. BTRFS_FILE_EXTENT_REG);
  1171. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1172. btrfs_mark_buffer_dirty(leaf);
  1173. } else {
  1174. fi = btrfs_item_ptr(leaf, del_slot - 1,
  1175. struct btrfs_file_extent_item);
  1176. btrfs_set_file_extent_type(leaf, fi,
  1177. BTRFS_FILE_EXTENT_REG);
  1178. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1179. btrfs_set_file_extent_num_bytes(leaf, fi,
  1180. extent_end - key.offset);
  1181. btrfs_mark_buffer_dirty(leaf);
  1182. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  1183. if (ret < 0) {
  1184. btrfs_abort_transaction(trans, ret);
  1185. goto out;
  1186. }
  1187. }
  1188. out:
  1189. btrfs_free_path(path);
  1190. return 0;
  1191. }
  1192. /*
  1193. * on error we return an unlocked page and the error value
  1194. * on success we return a locked page and 0
  1195. */
  1196. static int prepare_uptodate_page(struct inode *inode,
  1197. struct page *page, u64 pos,
  1198. bool force_uptodate)
  1199. {
  1200. int ret = 0;
  1201. if (((pos & (PAGE_SIZE - 1)) || force_uptodate) &&
  1202. !PageUptodate(page)) {
  1203. ret = btrfs_readpage(NULL, page);
  1204. if (ret)
  1205. return ret;
  1206. lock_page(page);
  1207. if (!PageUptodate(page)) {
  1208. unlock_page(page);
  1209. return -EIO;
  1210. }
  1211. if (page->mapping != inode->i_mapping) {
  1212. unlock_page(page);
  1213. return -EAGAIN;
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. /*
  1219. * this just gets pages into the page cache and locks them down.
  1220. */
  1221. static noinline int prepare_pages(struct inode *inode, struct page **pages,
  1222. size_t num_pages, loff_t pos,
  1223. size_t write_bytes, bool force_uptodate)
  1224. {
  1225. int i;
  1226. unsigned long index = pos >> PAGE_SHIFT;
  1227. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1228. int err = 0;
  1229. int faili;
  1230. for (i = 0; i < num_pages; i++) {
  1231. again:
  1232. pages[i] = find_or_create_page(inode->i_mapping, index + i,
  1233. mask | __GFP_WRITE);
  1234. if (!pages[i]) {
  1235. faili = i - 1;
  1236. err = -ENOMEM;
  1237. goto fail;
  1238. }
  1239. if (i == 0)
  1240. err = prepare_uptodate_page(inode, pages[i], pos,
  1241. force_uptodate);
  1242. if (!err && i == num_pages - 1)
  1243. err = prepare_uptodate_page(inode, pages[i],
  1244. pos + write_bytes, false);
  1245. if (err) {
  1246. put_page(pages[i]);
  1247. if (err == -EAGAIN) {
  1248. err = 0;
  1249. goto again;
  1250. }
  1251. faili = i - 1;
  1252. goto fail;
  1253. }
  1254. wait_on_page_writeback(pages[i]);
  1255. }
  1256. return 0;
  1257. fail:
  1258. while (faili >= 0) {
  1259. unlock_page(pages[faili]);
  1260. put_page(pages[faili]);
  1261. faili--;
  1262. }
  1263. return err;
  1264. }
  1265. /*
  1266. * This function locks the extent and properly waits for data=ordered extents
  1267. * to finish before allowing the pages to be modified if need.
  1268. *
  1269. * The return value:
  1270. * 1 - the extent is locked
  1271. * 0 - the extent is not locked, and everything is OK
  1272. * -EAGAIN - need re-prepare the pages
  1273. * the other < 0 number - Something wrong happens
  1274. */
  1275. static noinline int
  1276. lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages,
  1277. size_t num_pages, loff_t pos,
  1278. size_t write_bytes,
  1279. u64 *lockstart, u64 *lockend,
  1280. struct extent_state **cached_state)
  1281. {
  1282. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1283. u64 start_pos;
  1284. u64 last_pos;
  1285. int i;
  1286. int ret = 0;
  1287. start_pos = round_down(pos, fs_info->sectorsize);
  1288. last_pos = start_pos
  1289. + round_up(pos + write_bytes - start_pos,
  1290. fs_info->sectorsize) - 1;
  1291. if (start_pos < inode->i_size) {
  1292. struct btrfs_ordered_extent *ordered;
  1293. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1294. start_pos, last_pos, cached_state);
  1295. ordered = btrfs_lookup_ordered_range(inode, start_pos,
  1296. last_pos - start_pos + 1);
  1297. if (ordered &&
  1298. ordered->file_offset + ordered->len > start_pos &&
  1299. ordered->file_offset <= last_pos) {
  1300. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1301. start_pos, last_pos,
  1302. cached_state, GFP_NOFS);
  1303. for (i = 0; i < num_pages; i++) {
  1304. unlock_page(pages[i]);
  1305. put_page(pages[i]);
  1306. }
  1307. btrfs_start_ordered_extent(inode, ordered, 1);
  1308. btrfs_put_ordered_extent(ordered);
  1309. return -EAGAIN;
  1310. }
  1311. if (ordered)
  1312. btrfs_put_ordered_extent(ordered);
  1313. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  1314. last_pos, EXTENT_DIRTY | EXTENT_DELALLOC |
  1315. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  1316. 0, 0, cached_state, GFP_NOFS);
  1317. *lockstart = start_pos;
  1318. *lockend = last_pos;
  1319. ret = 1;
  1320. }
  1321. for (i = 0; i < num_pages; i++) {
  1322. if (clear_page_dirty_for_io(pages[i]))
  1323. account_page_redirty(pages[i]);
  1324. set_page_extent_mapped(pages[i]);
  1325. WARN_ON(!PageLocked(pages[i]));
  1326. }
  1327. return ret;
  1328. }
  1329. static noinline int check_can_nocow(struct inode *inode, loff_t pos,
  1330. size_t *write_bytes)
  1331. {
  1332. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1333. struct btrfs_root *root = BTRFS_I(inode)->root;
  1334. struct btrfs_ordered_extent *ordered;
  1335. u64 lockstart, lockend;
  1336. u64 num_bytes;
  1337. int ret;
  1338. ret = btrfs_start_write_no_snapshoting(root);
  1339. if (!ret)
  1340. return -ENOSPC;
  1341. lockstart = round_down(pos, fs_info->sectorsize);
  1342. lockend = round_up(pos + *write_bytes,
  1343. fs_info->sectorsize) - 1;
  1344. while (1) {
  1345. lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
  1346. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  1347. lockend - lockstart + 1);
  1348. if (!ordered) {
  1349. break;
  1350. }
  1351. unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
  1352. btrfs_start_ordered_extent(inode, ordered, 1);
  1353. btrfs_put_ordered_extent(ordered);
  1354. }
  1355. num_bytes = lockend - lockstart + 1;
  1356. ret = can_nocow_extent(inode, lockstart, &num_bytes, NULL, NULL, NULL);
  1357. if (ret <= 0) {
  1358. ret = 0;
  1359. btrfs_end_write_no_snapshoting(root);
  1360. } else {
  1361. *write_bytes = min_t(size_t, *write_bytes ,
  1362. num_bytes - pos + lockstart);
  1363. }
  1364. unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
  1365. return ret;
  1366. }
  1367. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  1368. struct iov_iter *i,
  1369. loff_t pos)
  1370. {
  1371. struct inode *inode = file_inode(file);
  1372. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1373. struct btrfs_root *root = BTRFS_I(inode)->root;
  1374. struct page **pages = NULL;
  1375. struct extent_state *cached_state = NULL;
  1376. u64 release_bytes = 0;
  1377. u64 lockstart;
  1378. u64 lockend;
  1379. size_t num_written = 0;
  1380. int nrptrs;
  1381. int ret = 0;
  1382. bool only_release_metadata = false;
  1383. bool force_page_uptodate = false;
  1384. bool need_unlock;
  1385. nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE),
  1386. PAGE_SIZE / (sizeof(struct page *)));
  1387. nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
  1388. nrptrs = max(nrptrs, 8);
  1389. pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL);
  1390. if (!pages)
  1391. return -ENOMEM;
  1392. while (iov_iter_count(i) > 0) {
  1393. size_t offset = pos & (PAGE_SIZE - 1);
  1394. size_t sector_offset;
  1395. size_t write_bytes = min(iov_iter_count(i),
  1396. nrptrs * (size_t)PAGE_SIZE -
  1397. offset);
  1398. size_t num_pages = DIV_ROUND_UP(write_bytes + offset,
  1399. PAGE_SIZE);
  1400. size_t reserve_bytes;
  1401. size_t dirty_pages;
  1402. size_t copied;
  1403. size_t dirty_sectors;
  1404. size_t num_sectors;
  1405. WARN_ON(num_pages > nrptrs);
  1406. /*
  1407. * Fault pages before locking them in prepare_pages
  1408. * to avoid recursive lock
  1409. */
  1410. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  1411. ret = -EFAULT;
  1412. break;
  1413. }
  1414. sector_offset = pos & (fs_info->sectorsize - 1);
  1415. reserve_bytes = round_up(write_bytes + sector_offset,
  1416. fs_info->sectorsize);
  1417. ret = btrfs_check_data_free_space(inode, pos, write_bytes);
  1418. if (ret < 0) {
  1419. if ((BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
  1420. BTRFS_INODE_PREALLOC)) &&
  1421. check_can_nocow(inode, pos, &write_bytes) > 0) {
  1422. /*
  1423. * For nodata cow case, no need to reserve
  1424. * data space.
  1425. */
  1426. only_release_metadata = true;
  1427. /*
  1428. * our prealloc extent may be smaller than
  1429. * write_bytes, so scale down.
  1430. */
  1431. num_pages = DIV_ROUND_UP(write_bytes + offset,
  1432. PAGE_SIZE);
  1433. reserve_bytes = round_up(write_bytes +
  1434. sector_offset,
  1435. fs_info->sectorsize);
  1436. } else {
  1437. break;
  1438. }
  1439. }
  1440. ret = btrfs_delalloc_reserve_metadata(inode, reserve_bytes);
  1441. if (ret) {
  1442. if (!only_release_metadata)
  1443. btrfs_free_reserved_data_space(inode, pos,
  1444. write_bytes);
  1445. else
  1446. btrfs_end_write_no_snapshoting(root);
  1447. break;
  1448. }
  1449. release_bytes = reserve_bytes;
  1450. need_unlock = false;
  1451. again:
  1452. /*
  1453. * This is going to setup the pages array with the number of
  1454. * pages we want, so we don't really need to worry about the
  1455. * contents of pages from loop to loop
  1456. */
  1457. ret = prepare_pages(inode, pages, num_pages,
  1458. pos, write_bytes,
  1459. force_page_uptodate);
  1460. if (ret)
  1461. break;
  1462. ret = lock_and_cleanup_extent_if_need(inode, pages, num_pages,
  1463. pos, write_bytes, &lockstart,
  1464. &lockend, &cached_state);
  1465. if (ret < 0) {
  1466. if (ret == -EAGAIN)
  1467. goto again;
  1468. break;
  1469. } else if (ret > 0) {
  1470. need_unlock = true;
  1471. ret = 0;
  1472. }
  1473. copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
  1474. num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
  1475. dirty_sectors = round_up(copied + sector_offset,
  1476. fs_info->sectorsize);
  1477. dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
  1478. /*
  1479. * if we have trouble faulting in the pages, fall
  1480. * back to one page at a time
  1481. */
  1482. if (copied < write_bytes)
  1483. nrptrs = 1;
  1484. if (copied == 0) {
  1485. force_page_uptodate = true;
  1486. dirty_sectors = 0;
  1487. dirty_pages = 0;
  1488. } else {
  1489. force_page_uptodate = false;
  1490. dirty_pages = DIV_ROUND_UP(copied + offset,
  1491. PAGE_SIZE);
  1492. }
  1493. /*
  1494. * If we had a short copy we need to release the excess delaloc
  1495. * bytes we reserved. We need to increment outstanding_extents
  1496. * because btrfs_delalloc_release_space and
  1497. * btrfs_delalloc_release_metadata will decrement it, but
  1498. * we still have an outstanding extent for the chunk we actually
  1499. * managed to copy.
  1500. */
  1501. if (num_sectors > dirty_sectors) {
  1502. /* release everything except the sectors we dirtied */
  1503. release_bytes -= dirty_sectors <<
  1504. fs_info->sb->s_blocksize_bits;
  1505. if (copied > 0) {
  1506. spin_lock(&BTRFS_I(inode)->lock);
  1507. BTRFS_I(inode)->outstanding_extents++;
  1508. spin_unlock(&BTRFS_I(inode)->lock);
  1509. }
  1510. if (only_release_metadata) {
  1511. btrfs_delalloc_release_metadata(inode,
  1512. release_bytes);
  1513. } else {
  1514. u64 __pos;
  1515. __pos = round_down(pos,
  1516. fs_info->sectorsize) +
  1517. (dirty_pages << PAGE_SHIFT);
  1518. btrfs_delalloc_release_space(inode, __pos,
  1519. release_bytes);
  1520. }
  1521. }
  1522. release_bytes = round_up(copied + sector_offset,
  1523. fs_info->sectorsize);
  1524. if (copied > 0)
  1525. ret = btrfs_dirty_pages(inode, pages, dirty_pages,
  1526. pos, copied, NULL);
  1527. if (need_unlock)
  1528. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1529. lockstart, lockend, &cached_state,
  1530. GFP_NOFS);
  1531. if (ret) {
  1532. btrfs_drop_pages(pages, num_pages);
  1533. break;
  1534. }
  1535. release_bytes = 0;
  1536. if (only_release_metadata)
  1537. btrfs_end_write_no_snapshoting(root);
  1538. if (only_release_metadata && copied > 0) {
  1539. lockstart = round_down(pos,
  1540. fs_info->sectorsize);
  1541. lockend = round_up(pos + copied,
  1542. fs_info->sectorsize) - 1;
  1543. set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  1544. lockend, EXTENT_NORESERVE, NULL,
  1545. NULL, GFP_NOFS);
  1546. only_release_metadata = false;
  1547. }
  1548. btrfs_drop_pages(pages, num_pages);
  1549. cond_resched();
  1550. balance_dirty_pages_ratelimited(inode->i_mapping);
  1551. if (dirty_pages < (fs_info->nodesize >> PAGE_SHIFT) + 1)
  1552. btrfs_btree_balance_dirty(fs_info);
  1553. pos += copied;
  1554. num_written += copied;
  1555. }
  1556. kfree(pages);
  1557. if (release_bytes) {
  1558. if (only_release_metadata) {
  1559. btrfs_end_write_no_snapshoting(root);
  1560. btrfs_delalloc_release_metadata(inode, release_bytes);
  1561. } else {
  1562. btrfs_delalloc_release_space(inode,
  1563. round_down(pos, fs_info->sectorsize),
  1564. release_bytes);
  1565. }
  1566. }
  1567. return num_written ? num_written : ret;
  1568. }
  1569. static ssize_t __btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
  1570. {
  1571. struct file *file = iocb->ki_filp;
  1572. struct inode *inode = file_inode(file);
  1573. loff_t pos = iocb->ki_pos;
  1574. ssize_t written;
  1575. ssize_t written_buffered;
  1576. loff_t endbyte;
  1577. int err;
  1578. written = generic_file_direct_write(iocb, from);
  1579. if (written < 0 || !iov_iter_count(from))
  1580. return written;
  1581. pos += written;
  1582. written_buffered = __btrfs_buffered_write(file, from, pos);
  1583. if (written_buffered < 0) {
  1584. err = written_buffered;
  1585. goto out;
  1586. }
  1587. /*
  1588. * Ensure all data is persisted. We want the next direct IO read to be
  1589. * able to read what was just written.
  1590. */
  1591. endbyte = pos + written_buffered - 1;
  1592. err = btrfs_fdatawrite_range(inode, pos, endbyte);
  1593. if (err)
  1594. goto out;
  1595. err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte);
  1596. if (err)
  1597. goto out;
  1598. written += written_buffered;
  1599. iocb->ki_pos = pos + written_buffered;
  1600. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT,
  1601. endbyte >> PAGE_SHIFT);
  1602. out:
  1603. return written ? written : err;
  1604. }
  1605. static void update_time_for_write(struct inode *inode)
  1606. {
  1607. struct timespec now;
  1608. if (IS_NOCMTIME(inode))
  1609. return;
  1610. now = current_time(inode);
  1611. if (!timespec_equal(&inode->i_mtime, &now))
  1612. inode->i_mtime = now;
  1613. if (!timespec_equal(&inode->i_ctime, &now))
  1614. inode->i_ctime = now;
  1615. if (IS_I_VERSION(inode))
  1616. inode_inc_iversion(inode);
  1617. }
  1618. static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
  1619. struct iov_iter *from)
  1620. {
  1621. struct file *file = iocb->ki_filp;
  1622. struct inode *inode = file_inode(file);
  1623. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1624. struct btrfs_root *root = BTRFS_I(inode)->root;
  1625. u64 start_pos;
  1626. u64 end_pos;
  1627. ssize_t num_written = 0;
  1628. bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
  1629. ssize_t err;
  1630. loff_t pos;
  1631. size_t count;
  1632. loff_t oldsize;
  1633. int clean_page = 0;
  1634. inode_lock(inode);
  1635. err = generic_write_checks(iocb, from);
  1636. if (err <= 0) {
  1637. inode_unlock(inode);
  1638. return err;
  1639. }
  1640. current->backing_dev_info = inode_to_bdi(inode);
  1641. err = file_remove_privs(file);
  1642. if (err) {
  1643. inode_unlock(inode);
  1644. goto out;
  1645. }
  1646. /*
  1647. * If BTRFS flips readonly due to some impossible error
  1648. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1649. * although we have opened a file as writable, we have
  1650. * to stop this write operation to ensure FS consistency.
  1651. */
  1652. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  1653. inode_unlock(inode);
  1654. err = -EROFS;
  1655. goto out;
  1656. }
  1657. /*
  1658. * We reserve space for updating the inode when we reserve space for the
  1659. * extent we are going to write, so we will enospc out there. We don't
  1660. * need to start yet another transaction to update the inode as we will
  1661. * update the inode when we finish writing whatever data we write.
  1662. */
  1663. update_time_for_write(inode);
  1664. pos = iocb->ki_pos;
  1665. count = iov_iter_count(from);
  1666. start_pos = round_down(pos, fs_info->sectorsize);
  1667. oldsize = i_size_read(inode);
  1668. if (start_pos > oldsize) {
  1669. /* Expand hole size to cover write data, preventing empty gap */
  1670. end_pos = round_up(pos + count,
  1671. fs_info->sectorsize);
  1672. err = btrfs_cont_expand(inode, oldsize, end_pos);
  1673. if (err) {
  1674. inode_unlock(inode);
  1675. goto out;
  1676. }
  1677. if (start_pos > round_up(oldsize, fs_info->sectorsize))
  1678. clean_page = 1;
  1679. }
  1680. if (sync)
  1681. atomic_inc(&BTRFS_I(inode)->sync_writers);
  1682. if (iocb->ki_flags & IOCB_DIRECT) {
  1683. num_written = __btrfs_direct_write(iocb, from);
  1684. } else {
  1685. num_written = __btrfs_buffered_write(file, from, pos);
  1686. if (num_written > 0)
  1687. iocb->ki_pos = pos + num_written;
  1688. if (clean_page)
  1689. pagecache_isize_extended(inode, oldsize,
  1690. i_size_read(inode));
  1691. }
  1692. inode_unlock(inode);
  1693. /*
  1694. * We also have to set last_sub_trans to the current log transid,
  1695. * otherwise subsequent syncs to a file that's been synced in this
  1696. * transaction will appear to have already occurred.
  1697. */
  1698. spin_lock(&BTRFS_I(inode)->lock);
  1699. BTRFS_I(inode)->last_sub_trans = root->log_transid;
  1700. spin_unlock(&BTRFS_I(inode)->lock);
  1701. if (num_written > 0)
  1702. num_written = generic_write_sync(iocb, num_written);
  1703. if (sync)
  1704. atomic_dec(&BTRFS_I(inode)->sync_writers);
  1705. out:
  1706. current->backing_dev_info = NULL;
  1707. return num_written ? num_written : err;
  1708. }
  1709. int btrfs_release_file(struct inode *inode, struct file *filp)
  1710. {
  1711. if (filp->private_data)
  1712. btrfs_ioctl_trans_end(filp);
  1713. /*
  1714. * ordered_data_close is set by settattr when we are about to truncate
  1715. * a file from a non-zero size to a zero size. This tries to
  1716. * flush down new bytes that may have been written if the
  1717. * application were using truncate to replace a file in place.
  1718. */
  1719. if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  1720. &BTRFS_I(inode)->runtime_flags))
  1721. filemap_flush(inode->i_mapping);
  1722. return 0;
  1723. }
  1724. static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end)
  1725. {
  1726. int ret;
  1727. atomic_inc(&BTRFS_I(inode)->sync_writers);
  1728. ret = btrfs_fdatawrite_range(inode, start, end);
  1729. atomic_dec(&BTRFS_I(inode)->sync_writers);
  1730. return ret;
  1731. }
  1732. /*
  1733. * fsync call for both files and directories. This logs the inode into
  1734. * the tree log instead of forcing full commits whenever possible.
  1735. *
  1736. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1737. * in the metadata btree are up to date for copying to the log.
  1738. *
  1739. * It drops the inode mutex before doing the tree log commit. This is an
  1740. * important optimization for directories because holding the mutex prevents
  1741. * new operations on the dir while we write to disk.
  1742. */
  1743. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  1744. {
  1745. struct dentry *dentry = file_dentry(file);
  1746. struct inode *inode = d_inode(dentry);
  1747. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1748. struct btrfs_root *root = BTRFS_I(inode)->root;
  1749. struct btrfs_trans_handle *trans;
  1750. struct btrfs_log_ctx ctx;
  1751. int ret = 0;
  1752. bool full_sync = 0;
  1753. u64 len;
  1754. /*
  1755. * The range length can be represented by u64, we have to do the typecasts
  1756. * to avoid signed overflow if it's [0, LLONG_MAX] eg. from fsync()
  1757. */
  1758. len = (u64)end - (u64)start + 1;
  1759. trace_btrfs_sync_file(file, datasync);
  1760. /*
  1761. * We write the dirty pages in the range and wait until they complete
  1762. * out of the ->i_mutex. If so, we can flush the dirty pages by
  1763. * multi-task, and make the performance up. See
  1764. * btrfs_wait_ordered_range for an explanation of the ASYNC check.
  1765. */
  1766. ret = start_ordered_ops(inode, start, end);
  1767. if (ret)
  1768. return ret;
  1769. inode_lock(inode);
  1770. atomic_inc(&root->log_batch);
  1771. full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1772. &BTRFS_I(inode)->runtime_flags);
  1773. /*
  1774. * We might have have had more pages made dirty after calling
  1775. * start_ordered_ops and before acquiring the inode's i_mutex.
  1776. */
  1777. if (full_sync) {
  1778. /*
  1779. * For a full sync, we need to make sure any ordered operations
  1780. * start and finish before we start logging the inode, so that
  1781. * all extents are persisted and the respective file extent
  1782. * items are in the fs/subvol btree.
  1783. */
  1784. ret = btrfs_wait_ordered_range(inode, start, len);
  1785. } else {
  1786. /*
  1787. * Start any new ordered operations before starting to log the
  1788. * inode. We will wait for them to finish in btrfs_sync_log().
  1789. *
  1790. * Right before acquiring the inode's mutex, we might have new
  1791. * writes dirtying pages, which won't immediately start the
  1792. * respective ordered operations - that is done through the
  1793. * fill_delalloc callbacks invoked from the writepage and
  1794. * writepages address space operations. So make sure we start
  1795. * all ordered operations before starting to log our inode. Not
  1796. * doing this means that while logging the inode, writeback
  1797. * could start and invoke writepage/writepages, which would call
  1798. * the fill_delalloc callbacks (cow_file_range,
  1799. * submit_compressed_extents). These callbacks add first an
  1800. * extent map to the modified list of extents and then create
  1801. * the respective ordered operation, which means in
  1802. * tree-log.c:btrfs_log_inode() we might capture all existing
  1803. * ordered operations (with btrfs_get_logged_extents()) before
  1804. * the fill_delalloc callback adds its ordered operation, and by
  1805. * the time we visit the modified list of extent maps (with
  1806. * btrfs_log_changed_extents()), we see and process the extent
  1807. * map they created. We then use the extent map to construct a
  1808. * file extent item for logging without waiting for the
  1809. * respective ordered operation to finish - this file extent
  1810. * item points to a disk location that might not have yet been
  1811. * written to, containing random data - so after a crash a log
  1812. * replay will make our inode have file extent items that point
  1813. * to disk locations containing invalid data, as we returned
  1814. * success to userspace without waiting for the respective
  1815. * ordered operation to finish, because it wasn't captured by
  1816. * btrfs_get_logged_extents().
  1817. */
  1818. ret = start_ordered_ops(inode, start, end);
  1819. }
  1820. if (ret) {
  1821. inode_unlock(inode);
  1822. goto out;
  1823. }
  1824. atomic_inc(&root->log_batch);
  1825. /*
  1826. * If the last transaction that changed this file was before the current
  1827. * transaction and we have the full sync flag set in our inode, we can
  1828. * bail out now without any syncing.
  1829. *
  1830. * Note that we can't bail out if the full sync flag isn't set. This is
  1831. * because when the full sync flag is set we start all ordered extents
  1832. * and wait for them to fully complete - when they complete they update
  1833. * the inode's last_trans field through:
  1834. *
  1835. * btrfs_finish_ordered_io() ->
  1836. * btrfs_update_inode_fallback() ->
  1837. * btrfs_update_inode() ->
  1838. * btrfs_set_inode_last_trans()
  1839. *
  1840. * So we are sure that last_trans is up to date and can do this check to
  1841. * bail out safely. For the fast path, when the full sync flag is not
  1842. * set in our inode, we can not do it because we start only our ordered
  1843. * extents and don't wait for them to complete (that is when
  1844. * btrfs_finish_ordered_io runs), so here at this point their last_trans
  1845. * value might be less than or equals to fs_info->last_trans_committed,
  1846. * and setting a speculative last_trans for an inode when a buffered
  1847. * write is made (such as fs_info->generation + 1 for example) would not
  1848. * be reliable since after setting the value and before fsync is called
  1849. * any number of transactions can start and commit (transaction kthread
  1850. * commits the current transaction periodically), and a transaction
  1851. * commit does not start nor waits for ordered extents to complete.
  1852. */
  1853. smp_mb();
  1854. if (btrfs_inode_in_log(inode, fs_info->generation) ||
  1855. (full_sync && BTRFS_I(inode)->last_trans <=
  1856. fs_info->last_trans_committed) ||
  1857. (!btrfs_have_ordered_extents_in_range(inode, start, len) &&
  1858. BTRFS_I(inode)->last_trans
  1859. <= fs_info->last_trans_committed)) {
  1860. /*
  1861. * We've had everything committed since the last time we were
  1862. * modified so clear this flag in case it was set for whatever
  1863. * reason, it's no longer relevant.
  1864. */
  1865. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1866. &BTRFS_I(inode)->runtime_flags);
  1867. /*
  1868. * An ordered extent might have started before and completed
  1869. * already with io errors, in which case the inode was not
  1870. * updated and we end up here. So check the inode's mapping
  1871. * flags for any errors that might have happened while doing
  1872. * writeback of file data.
  1873. */
  1874. ret = filemap_check_errors(inode->i_mapping);
  1875. inode_unlock(inode);
  1876. goto out;
  1877. }
  1878. /*
  1879. * ok we haven't committed the transaction yet, lets do a commit
  1880. */
  1881. if (file->private_data)
  1882. btrfs_ioctl_trans_end(file);
  1883. /*
  1884. * We use start here because we will need to wait on the IO to complete
  1885. * in btrfs_sync_log, which could require joining a transaction (for
  1886. * example checking cross references in the nocow path). If we use join
  1887. * here we could get into a situation where we're waiting on IO to
  1888. * happen that is blocked on a transaction trying to commit. With start
  1889. * we inc the extwriter counter, so we wait for all extwriters to exit
  1890. * before we start blocking join'ers. This comment is to keep somebody
  1891. * from thinking they are super smart and changing this to
  1892. * btrfs_join_transaction *cough*Josef*cough*.
  1893. */
  1894. trans = btrfs_start_transaction(root, 0);
  1895. if (IS_ERR(trans)) {
  1896. ret = PTR_ERR(trans);
  1897. inode_unlock(inode);
  1898. goto out;
  1899. }
  1900. trans->sync = true;
  1901. btrfs_init_log_ctx(&ctx, inode);
  1902. ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
  1903. if (ret < 0) {
  1904. /* Fallthrough and commit/free transaction. */
  1905. ret = 1;
  1906. }
  1907. /* we've logged all the items and now have a consistent
  1908. * version of the file in the log. It is possible that
  1909. * someone will come in and modify the file, but that's
  1910. * fine because the log is consistent on disk, and we
  1911. * have references to all of the file's extents
  1912. *
  1913. * It is possible that someone will come in and log the
  1914. * file again, but that will end up using the synchronization
  1915. * inside btrfs_sync_log to keep things safe.
  1916. */
  1917. inode_unlock(inode);
  1918. /*
  1919. * If any of the ordered extents had an error, just return it to user
  1920. * space, so that the application knows some writes didn't succeed and
  1921. * can take proper action (retry for e.g.). Blindly committing the
  1922. * transaction in this case, would fool userspace that everything was
  1923. * successful. And we also want to make sure our log doesn't contain
  1924. * file extent items pointing to extents that weren't fully written to -
  1925. * just like in the non fast fsync path, where we check for the ordered
  1926. * operation's error flag before writing to the log tree and return -EIO
  1927. * if any of them had this flag set (btrfs_wait_ordered_range) -
  1928. * therefore we need to check for errors in the ordered operations,
  1929. * which are indicated by ctx.io_err.
  1930. */
  1931. if (ctx.io_err) {
  1932. btrfs_end_transaction(trans);
  1933. ret = ctx.io_err;
  1934. goto out;
  1935. }
  1936. if (ret != BTRFS_NO_LOG_SYNC) {
  1937. if (!ret) {
  1938. ret = btrfs_sync_log(trans, root, &ctx);
  1939. if (!ret) {
  1940. ret = btrfs_end_transaction(trans);
  1941. goto out;
  1942. }
  1943. }
  1944. if (!full_sync) {
  1945. ret = btrfs_wait_ordered_range(inode, start, len);
  1946. if (ret) {
  1947. btrfs_end_transaction(trans);
  1948. goto out;
  1949. }
  1950. }
  1951. ret = btrfs_commit_transaction(trans);
  1952. } else {
  1953. ret = btrfs_end_transaction(trans);
  1954. }
  1955. out:
  1956. return ret > 0 ? -EIO : ret;
  1957. }
  1958. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1959. .fault = filemap_fault,
  1960. .map_pages = filemap_map_pages,
  1961. .page_mkwrite = btrfs_page_mkwrite,
  1962. };
  1963. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1964. {
  1965. struct address_space *mapping = filp->f_mapping;
  1966. if (!mapping->a_ops->readpage)
  1967. return -ENOEXEC;
  1968. file_accessed(filp);
  1969. vma->vm_ops = &btrfs_file_vm_ops;
  1970. return 0;
  1971. }
  1972. static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
  1973. int slot, u64 start, u64 end)
  1974. {
  1975. struct btrfs_file_extent_item *fi;
  1976. struct btrfs_key key;
  1977. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  1978. return 0;
  1979. btrfs_item_key_to_cpu(leaf, &key, slot);
  1980. if (key.objectid != btrfs_ino(inode) ||
  1981. key.type != BTRFS_EXTENT_DATA_KEY)
  1982. return 0;
  1983. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  1984. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
  1985. return 0;
  1986. if (btrfs_file_extent_disk_bytenr(leaf, fi))
  1987. return 0;
  1988. if (key.offset == end)
  1989. return 1;
  1990. if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
  1991. return 1;
  1992. return 0;
  1993. }
  1994. static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
  1995. struct btrfs_path *path, u64 offset, u64 end)
  1996. {
  1997. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1998. struct btrfs_root *root = BTRFS_I(inode)->root;
  1999. struct extent_buffer *leaf;
  2000. struct btrfs_file_extent_item *fi;
  2001. struct extent_map *hole_em;
  2002. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2003. struct btrfs_key key;
  2004. int ret;
  2005. if (btrfs_fs_incompat(fs_info, NO_HOLES))
  2006. goto out;
  2007. key.objectid = btrfs_ino(inode);
  2008. key.type = BTRFS_EXTENT_DATA_KEY;
  2009. key.offset = offset;
  2010. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  2011. if (ret <= 0) {
  2012. /*
  2013. * We should have dropped this offset, so if we find it then
  2014. * something has gone horribly wrong.
  2015. */
  2016. if (ret == 0)
  2017. ret = -EINVAL;
  2018. return ret;
  2019. }
  2020. leaf = path->nodes[0];
  2021. if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
  2022. u64 num_bytes;
  2023. path->slots[0]--;
  2024. fi = btrfs_item_ptr(leaf, path->slots[0],
  2025. struct btrfs_file_extent_item);
  2026. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
  2027. end - offset;
  2028. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  2029. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  2030. btrfs_set_file_extent_offset(leaf, fi, 0);
  2031. btrfs_mark_buffer_dirty(leaf);
  2032. goto out;
  2033. }
  2034. if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
  2035. u64 num_bytes;
  2036. key.offset = offset;
  2037. btrfs_set_item_key_safe(fs_info, path, &key);
  2038. fi = btrfs_item_ptr(leaf, path->slots[0],
  2039. struct btrfs_file_extent_item);
  2040. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
  2041. offset;
  2042. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  2043. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  2044. btrfs_set_file_extent_offset(leaf, fi, 0);
  2045. btrfs_mark_buffer_dirty(leaf);
  2046. goto out;
  2047. }
  2048. btrfs_release_path(path);
  2049. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  2050. 0, 0, end - offset, 0, end - offset,
  2051. 0, 0, 0);
  2052. if (ret)
  2053. return ret;
  2054. out:
  2055. btrfs_release_path(path);
  2056. hole_em = alloc_extent_map();
  2057. if (!hole_em) {
  2058. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  2059. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2060. &BTRFS_I(inode)->runtime_flags);
  2061. } else {
  2062. hole_em->start = offset;
  2063. hole_em->len = end - offset;
  2064. hole_em->ram_bytes = hole_em->len;
  2065. hole_em->orig_start = offset;
  2066. hole_em->block_start = EXTENT_MAP_HOLE;
  2067. hole_em->block_len = 0;
  2068. hole_em->orig_block_len = 0;
  2069. hole_em->bdev = fs_info->fs_devices->latest_bdev;
  2070. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  2071. hole_em->generation = trans->transid;
  2072. do {
  2073. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  2074. write_lock(&em_tree->lock);
  2075. ret = add_extent_mapping(em_tree, hole_em, 1);
  2076. write_unlock(&em_tree->lock);
  2077. } while (ret == -EEXIST);
  2078. free_extent_map(hole_em);
  2079. if (ret)
  2080. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2081. &BTRFS_I(inode)->runtime_flags);
  2082. }
  2083. return 0;
  2084. }
  2085. /*
  2086. * Find a hole extent on given inode and change start/len to the end of hole
  2087. * extent.(hole/vacuum extent whose em->start <= start &&
  2088. * em->start + em->len > start)
  2089. * When a hole extent is found, return 1 and modify start/len.
  2090. */
  2091. static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len)
  2092. {
  2093. struct extent_map *em;
  2094. int ret = 0;
  2095. em = btrfs_get_extent(inode, NULL, 0, *start, *len, 0);
  2096. if (IS_ERR_OR_NULL(em)) {
  2097. if (!em)
  2098. ret = -ENOMEM;
  2099. else
  2100. ret = PTR_ERR(em);
  2101. return ret;
  2102. }
  2103. /* Hole or vacuum extent(only exists in no-hole mode) */
  2104. if (em->block_start == EXTENT_MAP_HOLE) {
  2105. ret = 1;
  2106. *len = em->start + em->len > *start + *len ?
  2107. 0 : *start + *len - em->start - em->len;
  2108. *start = em->start + em->len;
  2109. }
  2110. free_extent_map(em);
  2111. return ret;
  2112. }
  2113. static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  2114. {
  2115. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2116. struct btrfs_root *root = BTRFS_I(inode)->root;
  2117. struct extent_state *cached_state = NULL;
  2118. struct btrfs_path *path;
  2119. struct btrfs_block_rsv *rsv;
  2120. struct btrfs_trans_handle *trans;
  2121. u64 lockstart;
  2122. u64 lockend;
  2123. u64 tail_start;
  2124. u64 tail_len;
  2125. u64 orig_start = offset;
  2126. u64 cur_offset;
  2127. u64 min_size = btrfs_calc_trans_metadata_size(fs_info, 1);
  2128. u64 drop_end;
  2129. int ret = 0;
  2130. int err = 0;
  2131. unsigned int rsv_count;
  2132. bool same_block;
  2133. bool no_holes = btrfs_fs_incompat(fs_info, NO_HOLES);
  2134. u64 ino_size;
  2135. bool truncated_block = false;
  2136. bool updated_inode = false;
  2137. ret = btrfs_wait_ordered_range(inode, offset, len);
  2138. if (ret)
  2139. return ret;
  2140. inode_lock(inode);
  2141. ino_size = round_up(inode->i_size, fs_info->sectorsize);
  2142. ret = find_first_non_hole(inode, &offset, &len);
  2143. if (ret < 0)
  2144. goto out_only_mutex;
  2145. if (ret && !len) {
  2146. /* Already in a large hole */
  2147. ret = 0;
  2148. goto out_only_mutex;
  2149. }
  2150. lockstart = round_up(offset, btrfs_inode_sectorsize(inode));
  2151. lockend = round_down(offset + len,
  2152. btrfs_inode_sectorsize(inode)) - 1;
  2153. same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
  2154. == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
  2155. /*
  2156. * We needn't truncate any block which is beyond the end of the file
  2157. * because we are sure there is no data there.
  2158. */
  2159. /*
  2160. * Only do this if we are in the same block and we aren't doing the
  2161. * entire block.
  2162. */
  2163. if (same_block && len < fs_info->sectorsize) {
  2164. if (offset < ino_size) {
  2165. truncated_block = true;
  2166. ret = btrfs_truncate_block(inode, offset, len, 0);
  2167. } else {
  2168. ret = 0;
  2169. }
  2170. goto out_only_mutex;
  2171. }
  2172. /* zero back part of the first block */
  2173. if (offset < ino_size) {
  2174. truncated_block = true;
  2175. ret = btrfs_truncate_block(inode, offset, 0, 0);
  2176. if (ret) {
  2177. inode_unlock(inode);
  2178. return ret;
  2179. }
  2180. }
  2181. /* Check the aligned pages after the first unaligned page,
  2182. * if offset != orig_start, which means the first unaligned page
  2183. * including several following pages are already in holes,
  2184. * the extra check can be skipped */
  2185. if (offset == orig_start) {
  2186. /* after truncate page, check hole again */
  2187. len = offset + len - lockstart;
  2188. offset = lockstart;
  2189. ret = find_first_non_hole(inode, &offset, &len);
  2190. if (ret < 0)
  2191. goto out_only_mutex;
  2192. if (ret && !len) {
  2193. ret = 0;
  2194. goto out_only_mutex;
  2195. }
  2196. lockstart = offset;
  2197. }
  2198. /* Check the tail unaligned part is in a hole */
  2199. tail_start = lockend + 1;
  2200. tail_len = offset + len - tail_start;
  2201. if (tail_len) {
  2202. ret = find_first_non_hole(inode, &tail_start, &tail_len);
  2203. if (unlikely(ret < 0))
  2204. goto out_only_mutex;
  2205. if (!ret) {
  2206. /* zero the front end of the last page */
  2207. if (tail_start + tail_len < ino_size) {
  2208. truncated_block = true;
  2209. ret = btrfs_truncate_block(inode,
  2210. tail_start + tail_len,
  2211. 0, 1);
  2212. if (ret)
  2213. goto out_only_mutex;
  2214. }
  2215. }
  2216. }
  2217. if (lockend < lockstart) {
  2218. ret = 0;
  2219. goto out_only_mutex;
  2220. }
  2221. while (1) {
  2222. struct btrfs_ordered_extent *ordered;
  2223. truncate_pagecache_range(inode, lockstart, lockend);
  2224. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  2225. &cached_state);
  2226. ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
  2227. /*
  2228. * We need to make sure we have no ordered extents in this range
  2229. * and nobody raced in and read a page in this range, if we did
  2230. * we need to try again.
  2231. */
  2232. if ((!ordered ||
  2233. (ordered->file_offset + ordered->len <= lockstart ||
  2234. ordered->file_offset > lockend)) &&
  2235. !btrfs_page_exists_in_range(inode, lockstart, lockend)) {
  2236. if (ordered)
  2237. btrfs_put_ordered_extent(ordered);
  2238. break;
  2239. }
  2240. if (ordered)
  2241. btrfs_put_ordered_extent(ordered);
  2242. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
  2243. lockend, &cached_state, GFP_NOFS);
  2244. ret = btrfs_wait_ordered_range(inode, lockstart,
  2245. lockend - lockstart + 1);
  2246. if (ret) {
  2247. inode_unlock(inode);
  2248. return ret;
  2249. }
  2250. }
  2251. path = btrfs_alloc_path();
  2252. if (!path) {
  2253. ret = -ENOMEM;
  2254. goto out;
  2255. }
  2256. rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
  2257. if (!rsv) {
  2258. ret = -ENOMEM;
  2259. goto out_free;
  2260. }
  2261. rsv->size = btrfs_calc_trans_metadata_size(fs_info, 1);
  2262. rsv->failfast = 1;
  2263. /*
  2264. * 1 - update the inode
  2265. * 1 - removing the extents in the range
  2266. * 1 - adding the hole extent if no_holes isn't set
  2267. */
  2268. rsv_count = no_holes ? 2 : 3;
  2269. trans = btrfs_start_transaction(root, rsv_count);
  2270. if (IS_ERR(trans)) {
  2271. err = PTR_ERR(trans);
  2272. goto out_free;
  2273. }
  2274. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
  2275. min_size, 0);
  2276. BUG_ON(ret);
  2277. trans->block_rsv = rsv;
  2278. cur_offset = lockstart;
  2279. len = lockend - cur_offset;
  2280. while (cur_offset < lockend) {
  2281. ret = __btrfs_drop_extents(trans, root, inode, path,
  2282. cur_offset, lockend + 1,
  2283. &drop_end, 1, 0, 0, NULL);
  2284. if (ret != -ENOSPC)
  2285. break;
  2286. trans->block_rsv = &fs_info->trans_block_rsv;
  2287. if (cur_offset < drop_end && cur_offset < ino_size) {
  2288. ret = fill_holes(trans, inode, path, cur_offset,
  2289. drop_end);
  2290. if (ret) {
  2291. /*
  2292. * If we failed then we didn't insert our hole
  2293. * entries for the area we dropped, so now the
  2294. * fs is corrupted, so we must abort the
  2295. * transaction.
  2296. */
  2297. btrfs_abort_transaction(trans, ret);
  2298. err = ret;
  2299. break;
  2300. }
  2301. }
  2302. cur_offset = drop_end;
  2303. ret = btrfs_update_inode(trans, root, inode);
  2304. if (ret) {
  2305. err = ret;
  2306. break;
  2307. }
  2308. btrfs_end_transaction(trans);
  2309. btrfs_btree_balance_dirty(fs_info);
  2310. trans = btrfs_start_transaction(root, rsv_count);
  2311. if (IS_ERR(trans)) {
  2312. ret = PTR_ERR(trans);
  2313. trans = NULL;
  2314. break;
  2315. }
  2316. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
  2317. rsv, min_size, 0);
  2318. BUG_ON(ret); /* shouldn't happen */
  2319. trans->block_rsv = rsv;
  2320. ret = find_first_non_hole(inode, &cur_offset, &len);
  2321. if (unlikely(ret < 0))
  2322. break;
  2323. if (ret && !len) {
  2324. ret = 0;
  2325. break;
  2326. }
  2327. }
  2328. if (ret) {
  2329. err = ret;
  2330. goto out_trans;
  2331. }
  2332. trans->block_rsv = &fs_info->trans_block_rsv;
  2333. /*
  2334. * If we are using the NO_HOLES feature we might have had already an
  2335. * hole that overlaps a part of the region [lockstart, lockend] and
  2336. * ends at (or beyond) lockend. Since we have no file extent items to
  2337. * represent holes, drop_end can be less than lockend and so we must
  2338. * make sure we have an extent map representing the existing hole (the
  2339. * call to __btrfs_drop_extents() might have dropped the existing extent
  2340. * map representing the existing hole), otherwise the fast fsync path
  2341. * will not record the existence of the hole region
  2342. * [existing_hole_start, lockend].
  2343. */
  2344. if (drop_end <= lockend)
  2345. drop_end = lockend + 1;
  2346. /*
  2347. * Don't insert file hole extent item if it's for a range beyond eof
  2348. * (because it's useless) or if it represents a 0 bytes range (when
  2349. * cur_offset == drop_end).
  2350. */
  2351. if (cur_offset < ino_size && cur_offset < drop_end) {
  2352. ret = fill_holes(trans, inode, path, cur_offset, drop_end);
  2353. if (ret) {
  2354. /* Same comment as above. */
  2355. btrfs_abort_transaction(trans, ret);
  2356. err = ret;
  2357. goto out_trans;
  2358. }
  2359. }
  2360. out_trans:
  2361. if (!trans)
  2362. goto out_free;
  2363. inode_inc_iversion(inode);
  2364. inode->i_mtime = inode->i_ctime = current_time(inode);
  2365. trans->block_rsv = &fs_info->trans_block_rsv;
  2366. ret = btrfs_update_inode(trans, root, inode);
  2367. updated_inode = true;
  2368. btrfs_end_transaction(trans);
  2369. btrfs_btree_balance_dirty(fs_info);
  2370. out_free:
  2371. btrfs_free_path(path);
  2372. btrfs_free_block_rsv(fs_info, rsv);
  2373. out:
  2374. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  2375. &cached_state, GFP_NOFS);
  2376. out_only_mutex:
  2377. if (!updated_inode && truncated_block && !ret && !err) {
  2378. /*
  2379. * If we only end up zeroing part of a page, we still need to
  2380. * update the inode item, so that all the time fields are
  2381. * updated as well as the necessary btrfs inode in memory fields
  2382. * for detecting, at fsync time, if the inode isn't yet in the
  2383. * log tree or it's there but not up to date.
  2384. */
  2385. trans = btrfs_start_transaction(root, 1);
  2386. if (IS_ERR(trans)) {
  2387. err = PTR_ERR(trans);
  2388. } else {
  2389. err = btrfs_update_inode(trans, root, inode);
  2390. ret = btrfs_end_transaction(trans);
  2391. }
  2392. }
  2393. inode_unlock(inode);
  2394. if (ret && !err)
  2395. err = ret;
  2396. return err;
  2397. }
  2398. /* Helper structure to record which range is already reserved */
  2399. struct falloc_range {
  2400. struct list_head list;
  2401. u64 start;
  2402. u64 len;
  2403. };
  2404. /*
  2405. * Helper function to add falloc range
  2406. *
  2407. * Caller should have locked the larger range of extent containing
  2408. * [start, len)
  2409. */
  2410. static int add_falloc_range(struct list_head *head, u64 start, u64 len)
  2411. {
  2412. struct falloc_range *prev = NULL;
  2413. struct falloc_range *range = NULL;
  2414. if (list_empty(head))
  2415. goto insert;
  2416. /*
  2417. * As fallocate iterate by bytenr order, we only need to check
  2418. * the last range.
  2419. */
  2420. prev = list_entry(head->prev, struct falloc_range, list);
  2421. if (prev->start + prev->len == start) {
  2422. prev->len += len;
  2423. return 0;
  2424. }
  2425. insert:
  2426. range = kmalloc(sizeof(*range), GFP_KERNEL);
  2427. if (!range)
  2428. return -ENOMEM;
  2429. range->start = start;
  2430. range->len = len;
  2431. list_add_tail(&range->list, head);
  2432. return 0;
  2433. }
  2434. static long btrfs_fallocate(struct file *file, int mode,
  2435. loff_t offset, loff_t len)
  2436. {
  2437. struct inode *inode = file_inode(file);
  2438. struct extent_state *cached_state = NULL;
  2439. struct falloc_range *range;
  2440. struct falloc_range *tmp;
  2441. struct list_head reserve_list;
  2442. u64 cur_offset;
  2443. u64 last_byte;
  2444. u64 alloc_start;
  2445. u64 alloc_end;
  2446. u64 alloc_hint = 0;
  2447. u64 locked_end;
  2448. u64 actual_end = 0;
  2449. struct extent_map *em;
  2450. int blocksize = btrfs_inode_sectorsize(inode);
  2451. int ret;
  2452. alloc_start = round_down(offset, blocksize);
  2453. alloc_end = round_up(offset + len, blocksize);
  2454. cur_offset = alloc_start;
  2455. /* Make sure we aren't being give some crap mode */
  2456. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  2457. return -EOPNOTSUPP;
  2458. if (mode & FALLOC_FL_PUNCH_HOLE)
  2459. return btrfs_punch_hole(inode, offset, len);
  2460. /*
  2461. * Only trigger disk allocation, don't trigger qgroup reserve
  2462. *
  2463. * For qgroup space, it will be checked later.
  2464. */
  2465. ret = btrfs_alloc_data_chunk_ondemand(inode, alloc_end - alloc_start);
  2466. if (ret < 0)
  2467. return ret;
  2468. inode_lock(inode);
  2469. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
  2470. ret = inode_newsize_ok(inode, offset + len);
  2471. if (ret)
  2472. goto out;
  2473. }
  2474. /*
  2475. * TODO: Move these two operations after we have checked
  2476. * accurate reserved space, or fallocate can still fail but
  2477. * with page truncated or size expanded.
  2478. *
  2479. * But that's a minor problem and won't do much harm BTW.
  2480. */
  2481. if (alloc_start > inode->i_size) {
  2482. ret = btrfs_cont_expand(inode, i_size_read(inode),
  2483. alloc_start);
  2484. if (ret)
  2485. goto out;
  2486. } else if (offset + len > inode->i_size) {
  2487. /*
  2488. * If we are fallocating from the end of the file onward we
  2489. * need to zero out the end of the block if i_size lands in the
  2490. * middle of a block.
  2491. */
  2492. ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
  2493. if (ret)
  2494. goto out;
  2495. }
  2496. /*
  2497. * wait for ordered IO before we have any locks. We'll loop again
  2498. * below with the locks held.
  2499. */
  2500. ret = btrfs_wait_ordered_range(inode, alloc_start,
  2501. alloc_end - alloc_start);
  2502. if (ret)
  2503. goto out;
  2504. locked_end = alloc_end - 1;
  2505. while (1) {
  2506. struct btrfs_ordered_extent *ordered;
  2507. /* the extent lock is ordered inside the running
  2508. * transaction
  2509. */
  2510. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  2511. locked_end, &cached_state);
  2512. ordered = btrfs_lookup_first_ordered_extent(inode,
  2513. alloc_end - 1);
  2514. if (ordered &&
  2515. ordered->file_offset + ordered->len > alloc_start &&
  2516. ordered->file_offset < alloc_end) {
  2517. btrfs_put_ordered_extent(ordered);
  2518. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  2519. alloc_start, locked_end,
  2520. &cached_state, GFP_KERNEL);
  2521. /*
  2522. * we can't wait on the range with the transaction
  2523. * running or with the extent lock held
  2524. */
  2525. ret = btrfs_wait_ordered_range(inode, alloc_start,
  2526. alloc_end - alloc_start);
  2527. if (ret)
  2528. goto out;
  2529. } else {
  2530. if (ordered)
  2531. btrfs_put_ordered_extent(ordered);
  2532. break;
  2533. }
  2534. }
  2535. /* First, check if we exceed the qgroup limit */
  2536. INIT_LIST_HEAD(&reserve_list);
  2537. while (1) {
  2538. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2539. alloc_end - cur_offset, 0);
  2540. if (IS_ERR_OR_NULL(em)) {
  2541. if (!em)
  2542. ret = -ENOMEM;
  2543. else
  2544. ret = PTR_ERR(em);
  2545. break;
  2546. }
  2547. last_byte = min(extent_map_end(em), alloc_end);
  2548. actual_end = min_t(u64, extent_map_end(em), offset + len);
  2549. last_byte = ALIGN(last_byte, blocksize);
  2550. if (em->block_start == EXTENT_MAP_HOLE ||
  2551. (cur_offset >= inode->i_size &&
  2552. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  2553. ret = add_falloc_range(&reserve_list, cur_offset,
  2554. last_byte - cur_offset);
  2555. if (ret < 0) {
  2556. free_extent_map(em);
  2557. break;
  2558. }
  2559. ret = btrfs_qgroup_reserve_data(inode, cur_offset,
  2560. last_byte - cur_offset);
  2561. if (ret < 0)
  2562. break;
  2563. } else {
  2564. /*
  2565. * Do not need to reserve unwritten extent for this
  2566. * range, free reserved data space first, otherwise
  2567. * it'll result in false ENOSPC error.
  2568. */
  2569. btrfs_free_reserved_data_space(inode, cur_offset,
  2570. last_byte - cur_offset);
  2571. }
  2572. free_extent_map(em);
  2573. cur_offset = last_byte;
  2574. if (cur_offset >= alloc_end)
  2575. break;
  2576. }
  2577. /*
  2578. * If ret is still 0, means we're OK to fallocate.
  2579. * Or just cleanup the list and exit.
  2580. */
  2581. list_for_each_entry_safe(range, tmp, &reserve_list, list) {
  2582. if (!ret)
  2583. ret = btrfs_prealloc_file_range(inode, mode,
  2584. range->start,
  2585. range->len, 1 << inode->i_blkbits,
  2586. offset + len, &alloc_hint);
  2587. else
  2588. btrfs_free_reserved_data_space(inode, range->start,
  2589. range->len);
  2590. list_del(&range->list);
  2591. kfree(range);
  2592. }
  2593. if (ret < 0)
  2594. goto out_unlock;
  2595. if (actual_end > inode->i_size &&
  2596. !(mode & FALLOC_FL_KEEP_SIZE)) {
  2597. struct btrfs_trans_handle *trans;
  2598. struct btrfs_root *root = BTRFS_I(inode)->root;
  2599. /*
  2600. * We didn't need to allocate any more space, but we
  2601. * still extended the size of the file so we need to
  2602. * update i_size and the inode item.
  2603. */
  2604. trans = btrfs_start_transaction(root, 1);
  2605. if (IS_ERR(trans)) {
  2606. ret = PTR_ERR(trans);
  2607. } else {
  2608. inode->i_ctime = current_time(inode);
  2609. i_size_write(inode, actual_end);
  2610. btrfs_ordered_update_i_size(inode, actual_end, NULL);
  2611. ret = btrfs_update_inode(trans, root, inode);
  2612. if (ret)
  2613. btrfs_end_transaction(trans);
  2614. else
  2615. ret = btrfs_end_transaction(trans);
  2616. }
  2617. }
  2618. out_unlock:
  2619. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  2620. &cached_state, GFP_KERNEL);
  2621. out:
  2622. inode_unlock(inode);
  2623. /* Let go of our reservation. */
  2624. if (ret != 0)
  2625. btrfs_free_reserved_data_space(inode, alloc_start,
  2626. alloc_end - cur_offset);
  2627. return ret;
  2628. }
  2629. static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
  2630. {
  2631. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2632. struct extent_map *em = NULL;
  2633. struct extent_state *cached_state = NULL;
  2634. u64 lockstart;
  2635. u64 lockend;
  2636. u64 start;
  2637. u64 len;
  2638. int ret = 0;
  2639. if (inode->i_size == 0)
  2640. return -ENXIO;
  2641. /*
  2642. * *offset can be negative, in this case we start finding DATA/HOLE from
  2643. * the very start of the file.
  2644. */
  2645. start = max_t(loff_t, 0, *offset);
  2646. lockstart = round_down(start, fs_info->sectorsize);
  2647. lockend = round_up(i_size_read(inode),
  2648. fs_info->sectorsize);
  2649. if (lockend <= lockstart)
  2650. lockend = lockstart + fs_info->sectorsize;
  2651. lockend--;
  2652. len = lockend - lockstart + 1;
  2653. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  2654. &cached_state);
  2655. while (start < inode->i_size) {
  2656. em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
  2657. if (IS_ERR(em)) {
  2658. ret = PTR_ERR(em);
  2659. em = NULL;
  2660. break;
  2661. }
  2662. if (whence == SEEK_HOLE &&
  2663. (em->block_start == EXTENT_MAP_HOLE ||
  2664. test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
  2665. break;
  2666. else if (whence == SEEK_DATA &&
  2667. (em->block_start != EXTENT_MAP_HOLE &&
  2668. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
  2669. break;
  2670. start = em->start + em->len;
  2671. free_extent_map(em);
  2672. em = NULL;
  2673. cond_resched();
  2674. }
  2675. free_extent_map(em);
  2676. if (!ret) {
  2677. if (whence == SEEK_DATA && start >= inode->i_size)
  2678. ret = -ENXIO;
  2679. else
  2680. *offset = min_t(loff_t, start, inode->i_size);
  2681. }
  2682. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  2683. &cached_state, GFP_NOFS);
  2684. return ret;
  2685. }
  2686. static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
  2687. {
  2688. struct inode *inode = file->f_mapping->host;
  2689. int ret;
  2690. inode_lock(inode);
  2691. switch (whence) {
  2692. case SEEK_END:
  2693. case SEEK_CUR:
  2694. offset = generic_file_llseek(file, offset, whence);
  2695. goto out;
  2696. case SEEK_DATA:
  2697. case SEEK_HOLE:
  2698. if (offset >= i_size_read(inode)) {
  2699. inode_unlock(inode);
  2700. return -ENXIO;
  2701. }
  2702. ret = find_desired_extent(inode, &offset, whence);
  2703. if (ret) {
  2704. inode_unlock(inode);
  2705. return ret;
  2706. }
  2707. }
  2708. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  2709. out:
  2710. inode_unlock(inode);
  2711. return offset;
  2712. }
  2713. const struct file_operations btrfs_file_operations = {
  2714. .llseek = btrfs_file_llseek,
  2715. .read_iter = generic_file_read_iter,
  2716. .splice_read = generic_file_splice_read,
  2717. .write_iter = btrfs_file_write_iter,
  2718. .mmap = btrfs_file_mmap,
  2719. .open = generic_file_open,
  2720. .release = btrfs_release_file,
  2721. .fsync = btrfs_sync_file,
  2722. .fallocate = btrfs_fallocate,
  2723. .unlocked_ioctl = btrfs_ioctl,
  2724. #ifdef CONFIG_COMPAT
  2725. .compat_ioctl = btrfs_compat_ioctl,
  2726. #endif
  2727. .clone_file_range = btrfs_clone_file_range,
  2728. .dedupe_file_range = btrfs_dedupe_file_range,
  2729. };
  2730. void btrfs_auto_defrag_exit(void)
  2731. {
  2732. kmem_cache_destroy(btrfs_inode_defrag_cachep);
  2733. }
  2734. int btrfs_auto_defrag_init(void)
  2735. {
  2736. btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
  2737. sizeof(struct inode_defrag), 0,
  2738. SLAB_MEM_SPREAD,
  2739. NULL);
  2740. if (!btrfs_inode_defrag_cachep)
  2741. return -ENOMEM;
  2742. return 0;
  2743. }
  2744. int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end)
  2745. {
  2746. int ret;
  2747. /*
  2748. * So with compression we will find and lock a dirty page and clear the
  2749. * first one as dirty, setup an async extent, and immediately return
  2750. * with the entire range locked but with nobody actually marked with
  2751. * writeback. So we can't just filemap_write_and_wait_range() and
  2752. * expect it to work since it will just kick off a thread to do the
  2753. * actual work. So we need to call filemap_fdatawrite_range _again_
  2754. * since it will wait on the page lock, which won't be unlocked until
  2755. * after the pages have been marked as writeback and so we're good to go
  2756. * from there. We have to do this otherwise we'll miss the ordered
  2757. * extents and that results in badness. Please Josef, do not think you
  2758. * know better and pull this out at some point in the future, it is
  2759. * right and you are wrong.
  2760. */
  2761. ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
  2762. if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  2763. &BTRFS_I(inode)->runtime_flags))
  2764. ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
  2765. return ret;
  2766. }