file.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  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/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/slab.h>
  32. #include <linux/btrfs.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 "compat.h"
  41. #include "volumes.h"
  42. static struct kmem_cache *btrfs_inode_defrag_cachep;
  43. /*
  44. * when auto defrag is enabled we
  45. * queue up these defrag structs to remember which
  46. * inodes need defragging passes
  47. */
  48. struct inode_defrag {
  49. struct rb_node rb_node;
  50. /* objectid */
  51. u64 ino;
  52. /*
  53. * transid where the defrag was added, we search for
  54. * extents newer than this
  55. */
  56. u64 transid;
  57. /* root objectid */
  58. u64 root;
  59. /* last offset we were able to defrag */
  60. u64 last_offset;
  61. /* if we've wrapped around back to zero once already */
  62. int cycled;
  63. };
  64. static int __compare_inode_defrag(struct inode_defrag *defrag1,
  65. struct inode_defrag *defrag2)
  66. {
  67. if (defrag1->root > defrag2->root)
  68. return 1;
  69. else if (defrag1->root < defrag2->root)
  70. return -1;
  71. else if (defrag1->ino > defrag2->ino)
  72. return 1;
  73. else if (defrag1->ino < defrag2->ino)
  74. return -1;
  75. else
  76. return 0;
  77. }
  78. /* pop a record for an inode into the defrag tree. The lock
  79. * must be held already
  80. *
  81. * If you're inserting a record for an older transid than an
  82. * existing record, the transid already in the tree is lowered
  83. *
  84. * If an existing record is found the defrag item you
  85. * pass in is freed
  86. */
  87. static int __btrfs_add_inode_defrag(struct inode *inode,
  88. struct inode_defrag *defrag)
  89. {
  90. struct btrfs_root *root = BTRFS_I(inode)->root;
  91. struct inode_defrag *entry;
  92. struct rb_node **p;
  93. struct rb_node *parent = NULL;
  94. int ret;
  95. p = &root->fs_info->defrag_inodes.rb_node;
  96. while (*p) {
  97. parent = *p;
  98. entry = rb_entry(parent, struct inode_defrag, rb_node);
  99. ret = __compare_inode_defrag(defrag, entry);
  100. if (ret < 0)
  101. p = &parent->rb_left;
  102. else if (ret > 0)
  103. p = &parent->rb_right;
  104. else {
  105. /* if we're reinserting an entry for
  106. * an old defrag run, make sure to
  107. * lower the transid of our existing record
  108. */
  109. if (defrag->transid < entry->transid)
  110. entry->transid = defrag->transid;
  111. if (defrag->last_offset > entry->last_offset)
  112. entry->last_offset = defrag->last_offset;
  113. return -EEXIST;
  114. }
  115. }
  116. set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
  117. rb_link_node(&defrag->rb_node, parent, p);
  118. rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
  119. return 0;
  120. }
  121. static inline int __need_auto_defrag(struct btrfs_root *root)
  122. {
  123. if (!btrfs_test_opt(root, AUTO_DEFRAG))
  124. return 0;
  125. if (btrfs_fs_closing(root->fs_info))
  126. return 0;
  127. return 1;
  128. }
  129. /*
  130. * insert a defrag record for this inode if auto defrag is
  131. * enabled
  132. */
  133. int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
  134. struct inode *inode)
  135. {
  136. struct btrfs_root *root = BTRFS_I(inode)->root;
  137. struct inode_defrag *defrag;
  138. u64 transid;
  139. int ret;
  140. if (!__need_auto_defrag(root))
  141. return 0;
  142. if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
  143. return 0;
  144. if (trans)
  145. transid = trans->transid;
  146. else
  147. transid = BTRFS_I(inode)->root->last_trans;
  148. defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
  149. if (!defrag)
  150. return -ENOMEM;
  151. defrag->ino = btrfs_ino(inode);
  152. defrag->transid = transid;
  153. defrag->root = root->root_key.objectid;
  154. spin_lock(&root->fs_info->defrag_inodes_lock);
  155. if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
  156. /*
  157. * If we set IN_DEFRAG flag and evict the inode from memory,
  158. * and then re-read this inode, this new inode doesn't have
  159. * IN_DEFRAG flag. At the case, we may find the existed defrag.
  160. */
  161. ret = __btrfs_add_inode_defrag(inode, defrag);
  162. if (ret)
  163. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  164. } else {
  165. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  166. }
  167. spin_unlock(&root->fs_info->defrag_inodes_lock);
  168. return 0;
  169. }
  170. /*
  171. * Requeue the defrag object. If there is a defrag object that points to
  172. * the same inode in the tree, we will merge them together (by
  173. * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
  174. */
  175. void btrfs_requeue_inode_defrag(struct inode *inode,
  176. struct inode_defrag *defrag)
  177. {
  178. struct btrfs_root *root = BTRFS_I(inode)->root;
  179. int ret;
  180. if (!__need_auto_defrag(root))
  181. goto out;
  182. /*
  183. * Here we don't check the IN_DEFRAG flag, because we need merge
  184. * them together.
  185. */
  186. spin_lock(&root->fs_info->defrag_inodes_lock);
  187. ret = __btrfs_add_inode_defrag(inode, defrag);
  188. spin_unlock(&root->fs_info->defrag_inodes_lock);
  189. if (ret)
  190. goto out;
  191. return;
  192. out:
  193. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  194. }
  195. /*
  196. * pick the defragable inode that we want, if it doesn't exist, we will get
  197. * the next one.
  198. */
  199. static struct inode_defrag *
  200. btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
  201. {
  202. struct inode_defrag *entry = NULL;
  203. struct inode_defrag tmp;
  204. struct rb_node *p;
  205. struct rb_node *parent = NULL;
  206. int ret;
  207. tmp.ino = ino;
  208. tmp.root = root;
  209. spin_lock(&fs_info->defrag_inodes_lock);
  210. p = fs_info->defrag_inodes.rb_node;
  211. while (p) {
  212. parent = p;
  213. entry = rb_entry(parent, struct inode_defrag, rb_node);
  214. ret = __compare_inode_defrag(&tmp, entry);
  215. if (ret < 0)
  216. p = parent->rb_left;
  217. else if (ret > 0)
  218. p = parent->rb_right;
  219. else
  220. goto out;
  221. }
  222. if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
  223. parent = rb_next(parent);
  224. if (parent)
  225. entry = rb_entry(parent, struct inode_defrag, rb_node);
  226. else
  227. entry = NULL;
  228. }
  229. out:
  230. if (entry)
  231. rb_erase(parent, &fs_info->defrag_inodes);
  232. spin_unlock(&fs_info->defrag_inodes_lock);
  233. return entry;
  234. }
  235. void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
  236. {
  237. struct inode_defrag *defrag;
  238. struct rb_node *node;
  239. spin_lock(&fs_info->defrag_inodes_lock);
  240. node = rb_first(&fs_info->defrag_inodes);
  241. while (node) {
  242. rb_erase(node, &fs_info->defrag_inodes);
  243. defrag = rb_entry(node, struct inode_defrag, rb_node);
  244. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  245. if (need_resched()) {
  246. spin_unlock(&fs_info->defrag_inodes_lock);
  247. cond_resched();
  248. spin_lock(&fs_info->defrag_inodes_lock);
  249. }
  250. node = rb_first(&fs_info->defrag_inodes);
  251. }
  252. spin_unlock(&fs_info->defrag_inodes_lock);
  253. }
  254. #define BTRFS_DEFRAG_BATCH 1024
  255. static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
  256. struct inode_defrag *defrag)
  257. {
  258. struct btrfs_root *inode_root;
  259. struct inode *inode;
  260. struct btrfs_key key;
  261. struct btrfs_ioctl_defrag_range_args range;
  262. int num_defrag;
  263. int index;
  264. int ret;
  265. /* get the inode */
  266. key.objectid = defrag->root;
  267. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  268. key.offset = (u64)-1;
  269. index = srcu_read_lock(&fs_info->subvol_srcu);
  270. inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
  271. if (IS_ERR(inode_root)) {
  272. ret = PTR_ERR(inode_root);
  273. goto cleanup;
  274. }
  275. if (btrfs_root_refs(&inode_root->root_item) == 0) {
  276. ret = -ENOENT;
  277. goto cleanup;
  278. }
  279. key.objectid = defrag->ino;
  280. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  281. key.offset = 0;
  282. inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
  283. if (IS_ERR(inode)) {
  284. ret = PTR_ERR(inode);
  285. goto cleanup;
  286. }
  287. srcu_read_unlock(&fs_info->subvol_srcu, index);
  288. /* do a chunk of defrag */
  289. clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
  290. memset(&range, 0, sizeof(range));
  291. range.len = (u64)-1;
  292. range.start = defrag->last_offset;
  293. sb_start_write(fs_info->sb);
  294. num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
  295. BTRFS_DEFRAG_BATCH);
  296. sb_end_write(fs_info->sb);
  297. /*
  298. * if we filled the whole defrag batch, there
  299. * must be more work to do. Queue this defrag
  300. * again
  301. */
  302. if (num_defrag == BTRFS_DEFRAG_BATCH) {
  303. defrag->last_offset = range.start;
  304. btrfs_requeue_inode_defrag(inode, defrag);
  305. } else if (defrag->last_offset && !defrag->cycled) {
  306. /*
  307. * we didn't fill our defrag batch, but
  308. * we didn't start at zero. Make sure we loop
  309. * around to the start of the file.
  310. */
  311. defrag->last_offset = 0;
  312. defrag->cycled = 1;
  313. btrfs_requeue_inode_defrag(inode, defrag);
  314. } else {
  315. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  316. }
  317. iput(inode);
  318. return 0;
  319. cleanup:
  320. srcu_read_unlock(&fs_info->subvol_srcu, index);
  321. kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
  322. return ret;
  323. }
  324. /*
  325. * run through the list of inodes in the FS that need
  326. * defragging
  327. */
  328. int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
  329. {
  330. struct inode_defrag *defrag;
  331. u64 first_ino = 0;
  332. u64 root_objectid = 0;
  333. atomic_inc(&fs_info->defrag_running);
  334. while(1) {
  335. /* Pause the auto defragger. */
  336. if (test_bit(BTRFS_FS_STATE_REMOUNTING,
  337. &fs_info->fs_state))
  338. break;
  339. if (!__need_auto_defrag(fs_info->tree_root))
  340. break;
  341. /* find an inode to defrag */
  342. defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
  343. first_ino);
  344. if (!defrag) {
  345. if (root_objectid || first_ino) {
  346. root_objectid = 0;
  347. first_ino = 0;
  348. continue;
  349. } else {
  350. break;
  351. }
  352. }
  353. first_ino = defrag->ino + 1;
  354. root_objectid = defrag->root;
  355. __btrfs_run_defrag_inode(fs_info, defrag);
  356. }
  357. atomic_dec(&fs_info->defrag_running);
  358. /*
  359. * during unmount, we use the transaction_wait queue to
  360. * wait for the defragger to stop
  361. */
  362. wake_up(&fs_info->transaction_wait);
  363. return 0;
  364. }
  365. /* simple helper to fault in pages and copy. This should go away
  366. * and be replaced with calls into generic code.
  367. */
  368. static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
  369. size_t write_bytes,
  370. struct page **prepared_pages,
  371. struct iov_iter *i)
  372. {
  373. size_t copied = 0;
  374. size_t total_copied = 0;
  375. int pg = 0;
  376. int offset = pos & (PAGE_CACHE_SIZE - 1);
  377. while (write_bytes > 0) {
  378. size_t count = min_t(size_t,
  379. PAGE_CACHE_SIZE - offset, write_bytes);
  380. struct page *page = prepared_pages[pg];
  381. /*
  382. * Copy data from userspace to the current page
  383. *
  384. * Disable pagefault to avoid recursive lock since
  385. * the pages are already locked
  386. */
  387. pagefault_disable();
  388. copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
  389. pagefault_enable();
  390. /* Flush processor's dcache for this page */
  391. flush_dcache_page(page);
  392. /*
  393. * if we get a partial write, we can end up with
  394. * partially up to date pages. These add
  395. * a lot of complexity, so make sure they don't
  396. * happen by forcing this copy to be retried.
  397. *
  398. * The rest of the btrfs_file_write code will fall
  399. * back to page at a time copies after we return 0.
  400. */
  401. if (!PageUptodate(page) && copied < count)
  402. copied = 0;
  403. iov_iter_advance(i, copied);
  404. write_bytes -= copied;
  405. total_copied += copied;
  406. /* Return to btrfs_file_aio_write to fault page */
  407. if (unlikely(copied == 0))
  408. break;
  409. if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
  410. offset += copied;
  411. } else {
  412. pg++;
  413. offset = 0;
  414. }
  415. }
  416. return total_copied;
  417. }
  418. /*
  419. * unlocks pages after btrfs_file_write is done with them
  420. */
  421. void btrfs_drop_pages(struct page **pages, size_t num_pages)
  422. {
  423. size_t i;
  424. for (i = 0; i < num_pages; i++) {
  425. /* page checked is some magic around finding pages that
  426. * have been modified without going through btrfs_set_page_dirty
  427. * clear it here
  428. */
  429. ClearPageChecked(pages[i]);
  430. unlock_page(pages[i]);
  431. mark_page_accessed(pages[i]);
  432. page_cache_release(pages[i]);
  433. }
  434. }
  435. /*
  436. * after copy_from_user, pages need to be dirtied and we need to make
  437. * sure holes are created between the current EOF and the start of
  438. * any next extents (if required).
  439. *
  440. * this also makes the decision about creating an inline extent vs
  441. * doing real data extents, marking pages dirty and delalloc as required.
  442. */
  443. int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
  444. struct page **pages, size_t num_pages,
  445. loff_t pos, size_t write_bytes,
  446. struct extent_state **cached)
  447. {
  448. int err = 0;
  449. int i;
  450. u64 num_bytes;
  451. u64 start_pos;
  452. u64 end_of_last_block;
  453. u64 end_pos = pos + write_bytes;
  454. loff_t isize = i_size_read(inode);
  455. start_pos = pos & ~((u64)root->sectorsize - 1);
  456. num_bytes = ALIGN(write_bytes + pos - start_pos, root->sectorsize);
  457. end_of_last_block = start_pos + num_bytes - 1;
  458. err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
  459. cached);
  460. if (err)
  461. return err;
  462. for (i = 0; i < num_pages; i++) {
  463. struct page *p = pages[i];
  464. SetPageUptodate(p);
  465. ClearPageChecked(p);
  466. set_page_dirty(p);
  467. }
  468. /*
  469. * we've only changed i_size in ram, and we haven't updated
  470. * the disk i_size. There is no need to log the inode
  471. * at this time.
  472. */
  473. if (end_pos > isize)
  474. i_size_write(inode, end_pos);
  475. return 0;
  476. }
  477. /*
  478. * this drops all the extents in the cache that intersect the range
  479. * [start, end]. Existing extents are split as required.
  480. */
  481. void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  482. int skip_pinned)
  483. {
  484. struct extent_map *em;
  485. struct extent_map *split = NULL;
  486. struct extent_map *split2 = NULL;
  487. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  488. u64 len = end - start + 1;
  489. u64 gen;
  490. int ret;
  491. int testend = 1;
  492. unsigned long flags;
  493. int compressed = 0;
  494. bool modified;
  495. WARN_ON(end < start);
  496. if (end == (u64)-1) {
  497. len = (u64)-1;
  498. testend = 0;
  499. }
  500. while (1) {
  501. int no_splits = 0;
  502. modified = false;
  503. if (!split)
  504. split = alloc_extent_map();
  505. if (!split2)
  506. split2 = alloc_extent_map();
  507. if (!split || !split2)
  508. no_splits = 1;
  509. write_lock(&em_tree->lock);
  510. em = lookup_extent_mapping(em_tree, start, len);
  511. if (!em) {
  512. write_unlock(&em_tree->lock);
  513. break;
  514. }
  515. flags = em->flags;
  516. gen = em->generation;
  517. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  518. if (testend && em->start + em->len >= start + len) {
  519. free_extent_map(em);
  520. write_unlock(&em_tree->lock);
  521. break;
  522. }
  523. start = em->start + em->len;
  524. if (testend)
  525. len = start + len - (em->start + em->len);
  526. free_extent_map(em);
  527. write_unlock(&em_tree->lock);
  528. continue;
  529. }
  530. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  531. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  532. clear_bit(EXTENT_FLAG_LOGGING, &flags);
  533. modified = !list_empty(&em->list);
  534. remove_extent_mapping(em_tree, em);
  535. if (no_splits)
  536. goto next;
  537. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  538. em->start < start) {
  539. split->start = em->start;
  540. split->len = start - em->start;
  541. split->orig_start = em->orig_start;
  542. split->block_start = em->block_start;
  543. if (compressed)
  544. split->block_len = em->block_len;
  545. else
  546. split->block_len = split->len;
  547. split->ram_bytes = em->ram_bytes;
  548. split->orig_block_len = max(split->block_len,
  549. em->orig_block_len);
  550. split->generation = gen;
  551. split->bdev = em->bdev;
  552. split->flags = flags;
  553. split->compress_type = em->compress_type;
  554. ret = add_extent_mapping(em_tree, split, modified);
  555. BUG_ON(ret); /* Logic error */
  556. free_extent_map(split);
  557. split = split2;
  558. split2 = NULL;
  559. }
  560. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  561. testend && em->start + em->len > start + len) {
  562. u64 diff = start + len - em->start;
  563. split->start = start + len;
  564. split->len = em->start + em->len - (start + len);
  565. split->bdev = em->bdev;
  566. split->flags = flags;
  567. split->compress_type = em->compress_type;
  568. split->generation = gen;
  569. split->orig_block_len = max(em->block_len,
  570. em->orig_block_len);
  571. split->ram_bytes = em->ram_bytes;
  572. if (compressed) {
  573. split->block_len = em->block_len;
  574. split->block_start = em->block_start;
  575. split->orig_start = em->orig_start;
  576. } else {
  577. split->block_len = split->len;
  578. split->block_start = em->block_start + diff;
  579. split->orig_start = em->orig_start;
  580. }
  581. ret = add_extent_mapping(em_tree, split, modified);
  582. BUG_ON(ret); /* Logic error */
  583. free_extent_map(split);
  584. split = NULL;
  585. }
  586. next:
  587. write_unlock(&em_tree->lock);
  588. /* once for us */
  589. free_extent_map(em);
  590. /* once for the tree*/
  591. free_extent_map(em);
  592. }
  593. if (split)
  594. free_extent_map(split);
  595. if (split2)
  596. free_extent_map(split2);
  597. }
  598. /*
  599. * this is very complex, but the basic idea is to drop all extents
  600. * in the range start - end. hint_block is filled in with a block number
  601. * that would be a good hint to the block allocator for this file.
  602. *
  603. * If an extent intersects the range but is not entirely inside the range
  604. * it is either truncated or split. Anything entirely inside the range
  605. * is deleted from the tree.
  606. */
  607. int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
  608. struct btrfs_root *root, struct inode *inode,
  609. struct btrfs_path *path, u64 start, u64 end,
  610. u64 *drop_end, int drop_cache)
  611. {
  612. struct extent_buffer *leaf;
  613. struct btrfs_file_extent_item *fi;
  614. struct btrfs_key key;
  615. struct btrfs_key new_key;
  616. u64 ino = btrfs_ino(inode);
  617. u64 search_start = start;
  618. u64 disk_bytenr = 0;
  619. u64 num_bytes = 0;
  620. u64 extent_offset = 0;
  621. u64 extent_end = 0;
  622. int del_nr = 0;
  623. int del_slot = 0;
  624. int extent_type;
  625. int recow;
  626. int ret;
  627. int modify_tree = -1;
  628. int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
  629. int found = 0;
  630. if (drop_cache)
  631. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  632. if (start >= BTRFS_I(inode)->disk_i_size)
  633. modify_tree = 0;
  634. while (1) {
  635. recow = 0;
  636. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  637. search_start, modify_tree);
  638. if (ret < 0)
  639. break;
  640. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  641. leaf = path->nodes[0];
  642. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  643. if (key.objectid == ino &&
  644. key.type == BTRFS_EXTENT_DATA_KEY)
  645. path->slots[0]--;
  646. }
  647. ret = 0;
  648. next_slot:
  649. leaf = path->nodes[0];
  650. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  651. BUG_ON(del_nr > 0);
  652. ret = btrfs_next_leaf(root, path);
  653. if (ret < 0)
  654. break;
  655. if (ret > 0) {
  656. ret = 0;
  657. break;
  658. }
  659. leaf = path->nodes[0];
  660. recow = 1;
  661. }
  662. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  663. if (key.objectid > ino ||
  664. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  665. break;
  666. fi = btrfs_item_ptr(leaf, path->slots[0],
  667. struct btrfs_file_extent_item);
  668. extent_type = btrfs_file_extent_type(leaf, fi);
  669. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  670. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  671. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  672. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  673. extent_offset = btrfs_file_extent_offset(leaf, fi);
  674. extent_end = key.offset +
  675. btrfs_file_extent_num_bytes(leaf, fi);
  676. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  677. extent_end = key.offset +
  678. btrfs_file_extent_inline_len(leaf, fi);
  679. } else {
  680. WARN_ON(1);
  681. extent_end = search_start;
  682. }
  683. if (extent_end <= search_start) {
  684. path->slots[0]++;
  685. goto next_slot;
  686. }
  687. found = 1;
  688. search_start = max(key.offset, start);
  689. if (recow || !modify_tree) {
  690. modify_tree = -1;
  691. btrfs_release_path(path);
  692. continue;
  693. }
  694. /*
  695. * | - range to drop - |
  696. * | -------- extent -------- |
  697. */
  698. if (start > key.offset && end < extent_end) {
  699. BUG_ON(del_nr > 0);
  700. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  701. memcpy(&new_key, &key, sizeof(new_key));
  702. new_key.offset = start;
  703. ret = btrfs_duplicate_item(trans, root, path,
  704. &new_key);
  705. if (ret == -EAGAIN) {
  706. btrfs_release_path(path);
  707. continue;
  708. }
  709. if (ret < 0)
  710. break;
  711. leaf = path->nodes[0];
  712. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  713. struct btrfs_file_extent_item);
  714. btrfs_set_file_extent_num_bytes(leaf, fi,
  715. start - key.offset);
  716. fi = btrfs_item_ptr(leaf, path->slots[0],
  717. struct btrfs_file_extent_item);
  718. extent_offset += start - key.offset;
  719. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  720. btrfs_set_file_extent_num_bytes(leaf, fi,
  721. extent_end - start);
  722. btrfs_mark_buffer_dirty(leaf);
  723. if (update_refs && disk_bytenr > 0) {
  724. ret = btrfs_inc_extent_ref(trans, root,
  725. disk_bytenr, num_bytes, 0,
  726. root->root_key.objectid,
  727. new_key.objectid,
  728. start - extent_offset, 0);
  729. BUG_ON(ret); /* -ENOMEM */
  730. }
  731. key.offset = start;
  732. }
  733. /*
  734. * | ---- range to drop ----- |
  735. * | -------- extent -------- |
  736. */
  737. if (start <= key.offset && end < extent_end) {
  738. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  739. memcpy(&new_key, &key, sizeof(new_key));
  740. new_key.offset = end;
  741. btrfs_set_item_key_safe(root, path, &new_key);
  742. extent_offset += end - key.offset;
  743. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  744. btrfs_set_file_extent_num_bytes(leaf, fi,
  745. extent_end - end);
  746. btrfs_mark_buffer_dirty(leaf);
  747. if (update_refs && disk_bytenr > 0)
  748. inode_sub_bytes(inode, end - key.offset);
  749. break;
  750. }
  751. search_start = extent_end;
  752. /*
  753. * | ---- range to drop ----- |
  754. * | -------- extent -------- |
  755. */
  756. if (start > key.offset && end >= extent_end) {
  757. BUG_ON(del_nr > 0);
  758. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  759. btrfs_set_file_extent_num_bytes(leaf, fi,
  760. start - key.offset);
  761. btrfs_mark_buffer_dirty(leaf);
  762. if (update_refs && disk_bytenr > 0)
  763. inode_sub_bytes(inode, extent_end - start);
  764. if (end == extent_end)
  765. break;
  766. path->slots[0]++;
  767. goto next_slot;
  768. }
  769. /*
  770. * | ---- range to drop ----- |
  771. * | ------ extent ------ |
  772. */
  773. if (start <= key.offset && end >= extent_end) {
  774. if (del_nr == 0) {
  775. del_slot = path->slots[0];
  776. del_nr = 1;
  777. } else {
  778. BUG_ON(del_slot + del_nr != path->slots[0]);
  779. del_nr++;
  780. }
  781. if (update_refs &&
  782. extent_type == BTRFS_FILE_EXTENT_INLINE) {
  783. inode_sub_bytes(inode,
  784. extent_end - key.offset);
  785. extent_end = ALIGN(extent_end,
  786. root->sectorsize);
  787. } else if (update_refs && disk_bytenr > 0) {
  788. ret = btrfs_free_extent(trans, root,
  789. disk_bytenr, num_bytes, 0,
  790. root->root_key.objectid,
  791. key.objectid, key.offset -
  792. extent_offset, 0);
  793. BUG_ON(ret); /* -ENOMEM */
  794. inode_sub_bytes(inode,
  795. extent_end - key.offset);
  796. }
  797. if (end == extent_end)
  798. break;
  799. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  800. path->slots[0]++;
  801. goto next_slot;
  802. }
  803. ret = btrfs_del_items(trans, root, path, del_slot,
  804. del_nr);
  805. if (ret) {
  806. btrfs_abort_transaction(trans, root, ret);
  807. break;
  808. }
  809. del_nr = 0;
  810. del_slot = 0;
  811. btrfs_release_path(path);
  812. continue;
  813. }
  814. BUG_ON(1);
  815. }
  816. if (!ret && del_nr > 0) {
  817. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  818. if (ret)
  819. btrfs_abort_transaction(trans, root, ret);
  820. }
  821. if (drop_end)
  822. *drop_end = found ? min(end, extent_end) : end;
  823. btrfs_release_path(path);
  824. return ret;
  825. }
  826. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  827. struct btrfs_root *root, struct inode *inode, u64 start,
  828. u64 end, int drop_cache)
  829. {
  830. struct btrfs_path *path;
  831. int ret;
  832. path = btrfs_alloc_path();
  833. if (!path)
  834. return -ENOMEM;
  835. ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
  836. drop_cache);
  837. btrfs_free_path(path);
  838. return ret;
  839. }
  840. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  841. u64 objectid, u64 bytenr, u64 orig_offset,
  842. u64 *start, u64 *end)
  843. {
  844. struct btrfs_file_extent_item *fi;
  845. struct btrfs_key key;
  846. u64 extent_end;
  847. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  848. return 0;
  849. btrfs_item_key_to_cpu(leaf, &key, slot);
  850. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  851. return 0;
  852. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  853. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  854. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  855. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  856. btrfs_file_extent_compression(leaf, fi) ||
  857. btrfs_file_extent_encryption(leaf, fi) ||
  858. btrfs_file_extent_other_encoding(leaf, fi))
  859. return 0;
  860. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  861. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  862. return 0;
  863. *start = key.offset;
  864. *end = extent_end;
  865. return 1;
  866. }
  867. /*
  868. * Mark extent in the range start - end as written.
  869. *
  870. * This changes extent type from 'pre-allocated' to 'regular'. If only
  871. * part of extent is marked as written, the extent will be split into
  872. * two or three.
  873. */
  874. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  875. struct inode *inode, u64 start, u64 end)
  876. {
  877. struct btrfs_root *root = BTRFS_I(inode)->root;
  878. struct extent_buffer *leaf;
  879. struct btrfs_path *path;
  880. struct btrfs_file_extent_item *fi;
  881. struct btrfs_key key;
  882. struct btrfs_key new_key;
  883. u64 bytenr;
  884. u64 num_bytes;
  885. u64 extent_end;
  886. u64 orig_offset;
  887. u64 other_start;
  888. u64 other_end;
  889. u64 split;
  890. int del_nr = 0;
  891. int del_slot = 0;
  892. int recow;
  893. int ret;
  894. u64 ino = btrfs_ino(inode);
  895. path = btrfs_alloc_path();
  896. if (!path)
  897. return -ENOMEM;
  898. again:
  899. recow = 0;
  900. split = start;
  901. key.objectid = ino;
  902. key.type = BTRFS_EXTENT_DATA_KEY;
  903. key.offset = split;
  904. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  905. if (ret < 0)
  906. goto out;
  907. if (ret > 0 && path->slots[0] > 0)
  908. path->slots[0]--;
  909. leaf = path->nodes[0];
  910. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  911. BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
  912. fi = btrfs_item_ptr(leaf, path->slots[0],
  913. struct btrfs_file_extent_item);
  914. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  915. BTRFS_FILE_EXTENT_PREALLOC);
  916. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  917. BUG_ON(key.offset > start || extent_end < end);
  918. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  919. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  920. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  921. memcpy(&new_key, &key, sizeof(new_key));
  922. if (start == key.offset && end < extent_end) {
  923. other_start = 0;
  924. other_end = start;
  925. if (extent_mergeable(leaf, path->slots[0] - 1,
  926. ino, bytenr, orig_offset,
  927. &other_start, &other_end)) {
  928. new_key.offset = end;
  929. btrfs_set_item_key_safe(root, path, &new_key);
  930. fi = btrfs_item_ptr(leaf, path->slots[0],
  931. struct btrfs_file_extent_item);
  932. btrfs_set_file_extent_generation(leaf, fi,
  933. trans->transid);
  934. btrfs_set_file_extent_num_bytes(leaf, fi,
  935. extent_end - end);
  936. btrfs_set_file_extent_offset(leaf, fi,
  937. end - orig_offset);
  938. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  939. struct btrfs_file_extent_item);
  940. btrfs_set_file_extent_generation(leaf, fi,
  941. trans->transid);
  942. btrfs_set_file_extent_num_bytes(leaf, fi,
  943. end - other_start);
  944. btrfs_mark_buffer_dirty(leaf);
  945. goto out;
  946. }
  947. }
  948. if (start > key.offset && end == extent_end) {
  949. other_start = end;
  950. other_end = 0;
  951. if (extent_mergeable(leaf, path->slots[0] + 1,
  952. ino, bytenr, orig_offset,
  953. &other_start, &other_end)) {
  954. fi = btrfs_item_ptr(leaf, path->slots[0],
  955. struct btrfs_file_extent_item);
  956. btrfs_set_file_extent_num_bytes(leaf, fi,
  957. start - key.offset);
  958. btrfs_set_file_extent_generation(leaf, fi,
  959. trans->transid);
  960. path->slots[0]++;
  961. new_key.offset = start;
  962. btrfs_set_item_key_safe(root, path, &new_key);
  963. fi = btrfs_item_ptr(leaf, path->slots[0],
  964. struct btrfs_file_extent_item);
  965. btrfs_set_file_extent_generation(leaf, fi,
  966. trans->transid);
  967. btrfs_set_file_extent_num_bytes(leaf, fi,
  968. other_end - start);
  969. btrfs_set_file_extent_offset(leaf, fi,
  970. start - orig_offset);
  971. btrfs_mark_buffer_dirty(leaf);
  972. goto out;
  973. }
  974. }
  975. while (start > key.offset || end < extent_end) {
  976. if (key.offset == start)
  977. split = end;
  978. new_key.offset = split;
  979. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  980. if (ret == -EAGAIN) {
  981. btrfs_release_path(path);
  982. goto again;
  983. }
  984. if (ret < 0) {
  985. btrfs_abort_transaction(trans, root, ret);
  986. goto out;
  987. }
  988. leaf = path->nodes[0];
  989. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  990. struct btrfs_file_extent_item);
  991. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  992. btrfs_set_file_extent_num_bytes(leaf, fi,
  993. split - key.offset);
  994. fi = btrfs_item_ptr(leaf, path->slots[0],
  995. struct btrfs_file_extent_item);
  996. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  997. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  998. btrfs_set_file_extent_num_bytes(leaf, fi,
  999. extent_end - split);
  1000. btrfs_mark_buffer_dirty(leaf);
  1001. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  1002. root->root_key.objectid,
  1003. ino, orig_offset, 0);
  1004. BUG_ON(ret); /* -ENOMEM */
  1005. if (split == start) {
  1006. key.offset = start;
  1007. } else {
  1008. BUG_ON(start != key.offset);
  1009. path->slots[0]--;
  1010. extent_end = end;
  1011. }
  1012. recow = 1;
  1013. }
  1014. other_start = end;
  1015. other_end = 0;
  1016. if (extent_mergeable(leaf, path->slots[0] + 1,
  1017. ino, bytenr, orig_offset,
  1018. &other_start, &other_end)) {
  1019. if (recow) {
  1020. btrfs_release_path(path);
  1021. goto again;
  1022. }
  1023. extent_end = other_end;
  1024. del_slot = path->slots[0] + 1;
  1025. del_nr++;
  1026. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  1027. 0, root->root_key.objectid,
  1028. ino, orig_offset, 0);
  1029. BUG_ON(ret); /* -ENOMEM */
  1030. }
  1031. other_start = 0;
  1032. other_end = start;
  1033. if (extent_mergeable(leaf, path->slots[0] - 1,
  1034. ino, bytenr, orig_offset,
  1035. &other_start, &other_end)) {
  1036. if (recow) {
  1037. btrfs_release_path(path);
  1038. goto again;
  1039. }
  1040. key.offset = other_start;
  1041. del_slot = path->slots[0];
  1042. del_nr++;
  1043. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  1044. 0, root->root_key.objectid,
  1045. ino, orig_offset, 0);
  1046. BUG_ON(ret); /* -ENOMEM */
  1047. }
  1048. if (del_nr == 0) {
  1049. fi = btrfs_item_ptr(leaf, path->slots[0],
  1050. struct btrfs_file_extent_item);
  1051. btrfs_set_file_extent_type(leaf, fi,
  1052. BTRFS_FILE_EXTENT_REG);
  1053. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1054. btrfs_mark_buffer_dirty(leaf);
  1055. } else {
  1056. fi = btrfs_item_ptr(leaf, del_slot - 1,
  1057. struct btrfs_file_extent_item);
  1058. btrfs_set_file_extent_type(leaf, fi,
  1059. BTRFS_FILE_EXTENT_REG);
  1060. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1061. btrfs_set_file_extent_num_bytes(leaf, fi,
  1062. extent_end - key.offset);
  1063. btrfs_mark_buffer_dirty(leaf);
  1064. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  1065. if (ret < 0) {
  1066. btrfs_abort_transaction(trans, root, ret);
  1067. goto out;
  1068. }
  1069. }
  1070. out:
  1071. btrfs_free_path(path);
  1072. return 0;
  1073. }
  1074. /*
  1075. * on error we return an unlocked page and the error value
  1076. * on success we return a locked page and 0
  1077. */
  1078. static int prepare_uptodate_page(struct page *page, u64 pos,
  1079. bool force_uptodate)
  1080. {
  1081. int ret = 0;
  1082. if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
  1083. !PageUptodate(page)) {
  1084. ret = btrfs_readpage(NULL, page);
  1085. if (ret)
  1086. return ret;
  1087. lock_page(page);
  1088. if (!PageUptodate(page)) {
  1089. unlock_page(page);
  1090. return -EIO;
  1091. }
  1092. }
  1093. return 0;
  1094. }
  1095. /*
  1096. * this gets pages into the page cache and locks them down, it also properly
  1097. * waits for data=ordered extents to finish before allowing the pages to be
  1098. * modified.
  1099. */
  1100. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  1101. struct page **pages, size_t num_pages,
  1102. loff_t pos, unsigned long first_index,
  1103. size_t write_bytes, bool force_uptodate)
  1104. {
  1105. struct extent_state *cached_state = NULL;
  1106. int i;
  1107. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1108. struct inode *inode = file_inode(file);
  1109. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1110. int err = 0;
  1111. int faili = 0;
  1112. u64 start_pos;
  1113. u64 last_pos;
  1114. start_pos = pos & ~((u64)root->sectorsize - 1);
  1115. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  1116. again:
  1117. for (i = 0; i < num_pages; i++) {
  1118. pages[i] = find_or_create_page(inode->i_mapping, index + i,
  1119. mask | __GFP_WRITE);
  1120. if (!pages[i]) {
  1121. faili = i - 1;
  1122. err = -ENOMEM;
  1123. goto fail;
  1124. }
  1125. if (i == 0)
  1126. err = prepare_uptodate_page(pages[i], pos,
  1127. force_uptodate);
  1128. if (i == num_pages - 1)
  1129. err = prepare_uptodate_page(pages[i],
  1130. pos + write_bytes, false);
  1131. if (err) {
  1132. page_cache_release(pages[i]);
  1133. faili = i - 1;
  1134. goto fail;
  1135. }
  1136. wait_on_page_writeback(pages[i]);
  1137. }
  1138. err = 0;
  1139. if (start_pos < inode->i_size) {
  1140. struct btrfs_ordered_extent *ordered;
  1141. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1142. start_pos, last_pos - 1, 0, &cached_state);
  1143. ordered = btrfs_lookup_first_ordered_extent(inode,
  1144. last_pos - 1);
  1145. if (ordered &&
  1146. ordered->file_offset + ordered->len > start_pos &&
  1147. ordered->file_offset < last_pos) {
  1148. btrfs_put_ordered_extent(ordered);
  1149. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1150. start_pos, last_pos - 1,
  1151. &cached_state, GFP_NOFS);
  1152. for (i = 0; i < num_pages; i++) {
  1153. unlock_page(pages[i]);
  1154. page_cache_release(pages[i]);
  1155. }
  1156. btrfs_wait_ordered_range(inode, start_pos,
  1157. last_pos - start_pos);
  1158. goto again;
  1159. }
  1160. if (ordered)
  1161. btrfs_put_ordered_extent(ordered);
  1162. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  1163. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1164. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  1165. 0, 0, &cached_state, GFP_NOFS);
  1166. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1167. start_pos, last_pos - 1, &cached_state,
  1168. GFP_NOFS);
  1169. }
  1170. for (i = 0; i < num_pages; i++) {
  1171. if (clear_page_dirty_for_io(pages[i]))
  1172. account_page_redirty(pages[i]);
  1173. set_page_extent_mapped(pages[i]);
  1174. WARN_ON(!PageLocked(pages[i]));
  1175. }
  1176. return 0;
  1177. fail:
  1178. while (faili >= 0) {
  1179. unlock_page(pages[faili]);
  1180. page_cache_release(pages[faili]);
  1181. faili--;
  1182. }
  1183. return err;
  1184. }
  1185. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  1186. struct iov_iter *i,
  1187. loff_t pos)
  1188. {
  1189. struct inode *inode = file_inode(file);
  1190. struct btrfs_root *root = BTRFS_I(inode)->root;
  1191. struct page **pages = NULL;
  1192. unsigned long first_index;
  1193. size_t num_written = 0;
  1194. int nrptrs;
  1195. int ret = 0;
  1196. bool force_page_uptodate = false;
  1197. nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
  1198. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  1199. (sizeof(struct page *)));
  1200. nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
  1201. nrptrs = max(nrptrs, 8);
  1202. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  1203. if (!pages)
  1204. return -ENOMEM;
  1205. first_index = pos >> PAGE_CACHE_SHIFT;
  1206. while (iov_iter_count(i) > 0) {
  1207. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1208. size_t write_bytes = min(iov_iter_count(i),
  1209. nrptrs * (size_t)PAGE_CACHE_SIZE -
  1210. offset);
  1211. size_t num_pages = (write_bytes + offset +
  1212. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1213. size_t dirty_pages;
  1214. size_t copied;
  1215. WARN_ON(num_pages > nrptrs);
  1216. /*
  1217. * Fault pages before locking them in prepare_pages
  1218. * to avoid recursive lock
  1219. */
  1220. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  1221. ret = -EFAULT;
  1222. break;
  1223. }
  1224. ret = btrfs_delalloc_reserve_space(inode,
  1225. num_pages << PAGE_CACHE_SHIFT);
  1226. if (ret)
  1227. break;
  1228. /*
  1229. * This is going to setup the pages array with the number of
  1230. * pages we want, so we don't really need to worry about the
  1231. * contents of pages from loop to loop
  1232. */
  1233. ret = prepare_pages(root, file, pages, num_pages,
  1234. pos, first_index, write_bytes,
  1235. force_page_uptodate);
  1236. if (ret) {
  1237. btrfs_delalloc_release_space(inode,
  1238. num_pages << PAGE_CACHE_SHIFT);
  1239. break;
  1240. }
  1241. copied = btrfs_copy_from_user(pos, num_pages,
  1242. write_bytes, pages, i);
  1243. /*
  1244. * if we have trouble faulting in the pages, fall
  1245. * back to one page at a time
  1246. */
  1247. if (copied < write_bytes)
  1248. nrptrs = 1;
  1249. if (copied == 0) {
  1250. force_page_uptodate = true;
  1251. dirty_pages = 0;
  1252. } else {
  1253. force_page_uptodate = false;
  1254. dirty_pages = (copied + offset +
  1255. PAGE_CACHE_SIZE - 1) >>
  1256. PAGE_CACHE_SHIFT;
  1257. }
  1258. /*
  1259. * If we had a short copy we need to release the excess delaloc
  1260. * bytes we reserved. We need to increment outstanding_extents
  1261. * because btrfs_delalloc_release_space will decrement it, but
  1262. * we still have an outstanding extent for the chunk we actually
  1263. * managed to copy.
  1264. */
  1265. if (num_pages > dirty_pages) {
  1266. if (copied > 0) {
  1267. spin_lock(&BTRFS_I(inode)->lock);
  1268. BTRFS_I(inode)->outstanding_extents++;
  1269. spin_unlock(&BTRFS_I(inode)->lock);
  1270. }
  1271. btrfs_delalloc_release_space(inode,
  1272. (num_pages - dirty_pages) <<
  1273. PAGE_CACHE_SHIFT);
  1274. }
  1275. if (copied > 0) {
  1276. ret = btrfs_dirty_pages(root, inode, pages,
  1277. dirty_pages, pos, copied,
  1278. NULL);
  1279. if (ret) {
  1280. btrfs_delalloc_release_space(inode,
  1281. dirty_pages << PAGE_CACHE_SHIFT);
  1282. btrfs_drop_pages(pages, num_pages);
  1283. break;
  1284. }
  1285. }
  1286. btrfs_drop_pages(pages, num_pages);
  1287. cond_resched();
  1288. balance_dirty_pages_ratelimited(inode->i_mapping);
  1289. if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1290. btrfs_btree_balance_dirty(root);
  1291. pos += copied;
  1292. num_written += copied;
  1293. }
  1294. kfree(pages);
  1295. return num_written ? num_written : ret;
  1296. }
  1297. static ssize_t __btrfs_direct_write(struct kiocb *iocb,
  1298. const struct iovec *iov,
  1299. unsigned long nr_segs, loff_t pos,
  1300. loff_t *ppos, size_t count, size_t ocount)
  1301. {
  1302. struct file *file = iocb->ki_filp;
  1303. struct iov_iter i;
  1304. ssize_t written;
  1305. ssize_t written_buffered;
  1306. loff_t endbyte;
  1307. int err;
  1308. written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
  1309. count, ocount);
  1310. if (written < 0 || written == count)
  1311. return written;
  1312. pos += written;
  1313. count -= written;
  1314. iov_iter_init(&i, iov, nr_segs, count, written);
  1315. written_buffered = __btrfs_buffered_write(file, &i, pos);
  1316. if (written_buffered < 0) {
  1317. err = written_buffered;
  1318. goto out;
  1319. }
  1320. endbyte = pos + written_buffered - 1;
  1321. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  1322. if (err)
  1323. goto out;
  1324. written += written_buffered;
  1325. *ppos = pos + written_buffered;
  1326. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
  1327. endbyte >> PAGE_CACHE_SHIFT);
  1328. out:
  1329. return written ? written : err;
  1330. }
  1331. static void update_time_for_write(struct inode *inode)
  1332. {
  1333. struct timespec now;
  1334. if (IS_NOCMTIME(inode))
  1335. return;
  1336. now = current_fs_time(inode->i_sb);
  1337. if (!timespec_equal(&inode->i_mtime, &now))
  1338. inode->i_mtime = now;
  1339. if (!timespec_equal(&inode->i_ctime, &now))
  1340. inode->i_ctime = now;
  1341. if (IS_I_VERSION(inode))
  1342. inode_inc_iversion(inode);
  1343. }
  1344. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  1345. const struct iovec *iov,
  1346. unsigned long nr_segs, loff_t pos)
  1347. {
  1348. struct file *file = iocb->ki_filp;
  1349. struct inode *inode = file_inode(file);
  1350. struct btrfs_root *root = BTRFS_I(inode)->root;
  1351. loff_t *ppos = &iocb->ki_pos;
  1352. u64 start_pos;
  1353. ssize_t num_written = 0;
  1354. ssize_t err = 0;
  1355. size_t count, ocount;
  1356. bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
  1357. sb_start_write(inode->i_sb);
  1358. mutex_lock(&inode->i_mutex);
  1359. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1360. if (err) {
  1361. mutex_unlock(&inode->i_mutex);
  1362. goto out;
  1363. }
  1364. count = ocount;
  1365. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1366. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1367. if (err) {
  1368. mutex_unlock(&inode->i_mutex);
  1369. goto out;
  1370. }
  1371. if (count == 0) {
  1372. mutex_unlock(&inode->i_mutex);
  1373. goto out;
  1374. }
  1375. err = file_remove_suid(file);
  1376. if (err) {
  1377. mutex_unlock(&inode->i_mutex);
  1378. goto out;
  1379. }
  1380. /*
  1381. * If BTRFS flips readonly due to some impossible error
  1382. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1383. * although we have opened a file as writable, we have
  1384. * to stop this write operation to ensure FS consistency.
  1385. */
  1386. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  1387. mutex_unlock(&inode->i_mutex);
  1388. err = -EROFS;
  1389. goto out;
  1390. }
  1391. /*
  1392. * We reserve space for updating the inode when we reserve space for the
  1393. * extent we are going to write, so we will enospc out there. We don't
  1394. * need to start yet another transaction to update the inode as we will
  1395. * update the inode when we finish writing whatever data we write.
  1396. */
  1397. update_time_for_write(inode);
  1398. start_pos = round_down(pos, root->sectorsize);
  1399. if (start_pos > i_size_read(inode)) {
  1400. err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
  1401. if (err) {
  1402. mutex_unlock(&inode->i_mutex);
  1403. goto out;
  1404. }
  1405. }
  1406. if (sync)
  1407. atomic_inc(&BTRFS_I(inode)->sync_writers);
  1408. if (unlikely(file->f_flags & O_DIRECT)) {
  1409. num_written = __btrfs_direct_write(iocb, iov, nr_segs,
  1410. pos, ppos, count, ocount);
  1411. } else {
  1412. struct iov_iter i;
  1413. iov_iter_init(&i, iov, nr_segs, count, num_written);
  1414. num_written = __btrfs_buffered_write(file, &i, pos);
  1415. if (num_written > 0)
  1416. *ppos = pos + num_written;
  1417. }
  1418. mutex_unlock(&inode->i_mutex);
  1419. /*
  1420. * we want to make sure fsync finds this change
  1421. * but we haven't joined a transaction running right now.
  1422. *
  1423. * Later on, someone is sure to update the inode and get the
  1424. * real transid recorded.
  1425. *
  1426. * We set last_trans now to the fs_info generation + 1,
  1427. * this will either be one more than the running transaction
  1428. * or the generation used for the next transaction if there isn't
  1429. * one running right now.
  1430. *
  1431. * We also have to set last_sub_trans to the current log transid,
  1432. * otherwise subsequent syncs to a file that's been synced in this
  1433. * transaction will appear to have already occured.
  1434. */
  1435. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  1436. BTRFS_I(inode)->last_sub_trans = root->log_transid;
  1437. if (num_written > 0 || num_written == -EIOCBQUEUED) {
  1438. err = generic_write_sync(file, pos, num_written);
  1439. if (err < 0 && num_written > 0)
  1440. num_written = err;
  1441. }
  1442. if (sync)
  1443. atomic_dec(&BTRFS_I(inode)->sync_writers);
  1444. out:
  1445. sb_end_write(inode->i_sb);
  1446. current->backing_dev_info = NULL;
  1447. return num_written ? num_written : err;
  1448. }
  1449. int btrfs_release_file(struct inode *inode, struct file *filp)
  1450. {
  1451. /*
  1452. * ordered_data_close is set by settattr when we are about to truncate
  1453. * a file from a non-zero size to a zero size. This tries to
  1454. * flush down new bytes that may have been written if the
  1455. * application were using truncate to replace a file in place.
  1456. */
  1457. if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  1458. &BTRFS_I(inode)->runtime_flags)) {
  1459. struct btrfs_trans_handle *trans;
  1460. struct btrfs_root *root = BTRFS_I(inode)->root;
  1461. /*
  1462. * We need to block on a committing transaction to keep us from
  1463. * throwing a ordered operation on to the list and causing
  1464. * something like sync to deadlock trying to flush out this
  1465. * inode.
  1466. */
  1467. trans = btrfs_start_transaction(root, 0);
  1468. if (IS_ERR(trans))
  1469. return PTR_ERR(trans);
  1470. btrfs_add_ordered_operation(trans, BTRFS_I(inode)->root, inode);
  1471. btrfs_end_transaction(trans, root);
  1472. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1473. filemap_flush(inode->i_mapping);
  1474. }
  1475. if (filp->private_data)
  1476. btrfs_ioctl_trans_end(filp);
  1477. return 0;
  1478. }
  1479. /*
  1480. * fsync call for both files and directories. This logs the inode into
  1481. * the tree log instead of forcing full commits whenever possible.
  1482. *
  1483. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1484. * in the metadata btree are up to date for copying to the log.
  1485. *
  1486. * It drops the inode mutex before doing the tree log commit. This is an
  1487. * important optimization for directories because holding the mutex prevents
  1488. * new operations on the dir while we write to disk.
  1489. */
  1490. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  1491. {
  1492. struct dentry *dentry = file->f_path.dentry;
  1493. struct inode *inode = dentry->d_inode;
  1494. struct btrfs_root *root = BTRFS_I(inode)->root;
  1495. int ret = 0;
  1496. struct btrfs_trans_handle *trans;
  1497. bool full_sync = 0;
  1498. trace_btrfs_sync_file(file, datasync);
  1499. /*
  1500. * We write the dirty pages in the range and wait until they complete
  1501. * out of the ->i_mutex. If so, we can flush the dirty pages by
  1502. * multi-task, and make the performance up. See
  1503. * btrfs_wait_ordered_range for an explanation of the ASYNC check.
  1504. */
  1505. atomic_inc(&BTRFS_I(inode)->sync_writers);
  1506. ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
  1507. if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1508. &BTRFS_I(inode)->runtime_flags))
  1509. ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
  1510. atomic_dec(&BTRFS_I(inode)->sync_writers);
  1511. if (ret)
  1512. return ret;
  1513. mutex_lock(&inode->i_mutex);
  1514. /*
  1515. * We flush the dirty pages again to avoid some dirty pages in the
  1516. * range being left.
  1517. */
  1518. atomic_inc(&root->log_batch);
  1519. full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1520. &BTRFS_I(inode)->runtime_flags);
  1521. if (full_sync)
  1522. btrfs_wait_ordered_range(inode, start, end - start + 1);
  1523. atomic_inc(&root->log_batch);
  1524. /*
  1525. * check the transaction that last modified this inode
  1526. * and see if its already been committed
  1527. */
  1528. if (!BTRFS_I(inode)->last_trans) {
  1529. mutex_unlock(&inode->i_mutex);
  1530. goto out;
  1531. }
  1532. /*
  1533. * if the last transaction that changed this file was before
  1534. * the current transaction, we can bail out now without any
  1535. * syncing
  1536. */
  1537. smp_mb();
  1538. if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
  1539. BTRFS_I(inode)->last_trans <=
  1540. root->fs_info->last_trans_committed) {
  1541. BTRFS_I(inode)->last_trans = 0;
  1542. /*
  1543. * We'v had everything committed since the last time we were
  1544. * modified so clear this flag in case it was set for whatever
  1545. * reason, it's no longer relevant.
  1546. */
  1547. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1548. &BTRFS_I(inode)->runtime_flags);
  1549. mutex_unlock(&inode->i_mutex);
  1550. goto out;
  1551. }
  1552. /*
  1553. * ok we haven't committed the transaction yet, lets do a commit
  1554. */
  1555. if (file->private_data)
  1556. btrfs_ioctl_trans_end(file);
  1557. trans = btrfs_start_transaction(root, 0);
  1558. if (IS_ERR(trans)) {
  1559. ret = PTR_ERR(trans);
  1560. mutex_unlock(&inode->i_mutex);
  1561. goto out;
  1562. }
  1563. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1564. if (ret < 0) {
  1565. mutex_unlock(&inode->i_mutex);
  1566. goto out;
  1567. }
  1568. /* we've logged all the items and now have a consistent
  1569. * version of the file in the log. It is possible that
  1570. * someone will come in and modify the file, but that's
  1571. * fine because the log is consistent on disk, and we
  1572. * have references to all of the file's extents
  1573. *
  1574. * It is possible that someone will come in and log the
  1575. * file again, but that will end up using the synchronization
  1576. * inside btrfs_sync_log to keep things safe.
  1577. */
  1578. mutex_unlock(&inode->i_mutex);
  1579. if (ret != BTRFS_NO_LOG_SYNC) {
  1580. if (ret > 0) {
  1581. /*
  1582. * If we didn't already wait for ordered extents we need
  1583. * to do that now.
  1584. */
  1585. if (!full_sync)
  1586. btrfs_wait_ordered_range(inode, start,
  1587. end - start + 1);
  1588. ret = btrfs_commit_transaction(trans, root);
  1589. } else {
  1590. ret = btrfs_sync_log(trans, root);
  1591. if (ret == 0) {
  1592. ret = btrfs_end_transaction(trans, root);
  1593. } else {
  1594. if (!full_sync)
  1595. btrfs_wait_ordered_range(inode, start,
  1596. end -
  1597. start + 1);
  1598. ret = btrfs_commit_transaction(trans, root);
  1599. }
  1600. }
  1601. } else {
  1602. ret = btrfs_end_transaction(trans, root);
  1603. }
  1604. out:
  1605. return ret > 0 ? -EIO : ret;
  1606. }
  1607. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1608. .fault = filemap_fault,
  1609. .page_mkwrite = btrfs_page_mkwrite,
  1610. .remap_pages = generic_file_remap_pages,
  1611. };
  1612. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1613. {
  1614. struct address_space *mapping = filp->f_mapping;
  1615. if (!mapping->a_ops->readpage)
  1616. return -ENOEXEC;
  1617. file_accessed(filp);
  1618. vma->vm_ops = &btrfs_file_vm_ops;
  1619. return 0;
  1620. }
  1621. static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
  1622. int slot, u64 start, u64 end)
  1623. {
  1624. struct btrfs_file_extent_item *fi;
  1625. struct btrfs_key key;
  1626. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  1627. return 0;
  1628. btrfs_item_key_to_cpu(leaf, &key, slot);
  1629. if (key.objectid != btrfs_ino(inode) ||
  1630. key.type != BTRFS_EXTENT_DATA_KEY)
  1631. return 0;
  1632. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  1633. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
  1634. return 0;
  1635. if (btrfs_file_extent_disk_bytenr(leaf, fi))
  1636. return 0;
  1637. if (key.offset == end)
  1638. return 1;
  1639. if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
  1640. return 1;
  1641. return 0;
  1642. }
  1643. static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
  1644. struct btrfs_path *path, u64 offset, u64 end)
  1645. {
  1646. struct btrfs_root *root = BTRFS_I(inode)->root;
  1647. struct extent_buffer *leaf;
  1648. struct btrfs_file_extent_item *fi;
  1649. struct extent_map *hole_em;
  1650. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1651. struct btrfs_key key;
  1652. int ret;
  1653. key.objectid = btrfs_ino(inode);
  1654. key.type = BTRFS_EXTENT_DATA_KEY;
  1655. key.offset = offset;
  1656. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1657. if (ret < 0)
  1658. return ret;
  1659. BUG_ON(!ret);
  1660. leaf = path->nodes[0];
  1661. if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
  1662. u64 num_bytes;
  1663. path->slots[0]--;
  1664. fi = btrfs_item_ptr(leaf, path->slots[0],
  1665. struct btrfs_file_extent_item);
  1666. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
  1667. end - offset;
  1668. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1669. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  1670. btrfs_set_file_extent_offset(leaf, fi, 0);
  1671. btrfs_mark_buffer_dirty(leaf);
  1672. goto out;
  1673. }
  1674. if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
  1675. u64 num_bytes;
  1676. path->slots[0]++;
  1677. key.offset = offset;
  1678. btrfs_set_item_key_safe(root, path, &key);
  1679. fi = btrfs_item_ptr(leaf, path->slots[0],
  1680. struct btrfs_file_extent_item);
  1681. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
  1682. offset;
  1683. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1684. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  1685. btrfs_set_file_extent_offset(leaf, fi, 0);
  1686. btrfs_mark_buffer_dirty(leaf);
  1687. goto out;
  1688. }
  1689. btrfs_release_path(path);
  1690. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  1691. 0, 0, end - offset, 0, end - offset,
  1692. 0, 0, 0);
  1693. if (ret)
  1694. return ret;
  1695. out:
  1696. btrfs_release_path(path);
  1697. hole_em = alloc_extent_map();
  1698. if (!hole_em) {
  1699. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  1700. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1701. &BTRFS_I(inode)->runtime_flags);
  1702. } else {
  1703. hole_em->start = offset;
  1704. hole_em->len = end - offset;
  1705. hole_em->ram_bytes = hole_em->len;
  1706. hole_em->orig_start = offset;
  1707. hole_em->block_start = EXTENT_MAP_HOLE;
  1708. hole_em->block_len = 0;
  1709. hole_em->orig_block_len = 0;
  1710. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  1711. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  1712. hole_em->generation = trans->transid;
  1713. do {
  1714. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  1715. write_lock(&em_tree->lock);
  1716. ret = add_extent_mapping(em_tree, hole_em, 1);
  1717. write_unlock(&em_tree->lock);
  1718. } while (ret == -EEXIST);
  1719. free_extent_map(hole_em);
  1720. if (ret)
  1721. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1722. &BTRFS_I(inode)->runtime_flags);
  1723. }
  1724. return 0;
  1725. }
  1726. static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  1727. {
  1728. struct btrfs_root *root = BTRFS_I(inode)->root;
  1729. struct extent_state *cached_state = NULL;
  1730. struct btrfs_path *path;
  1731. struct btrfs_block_rsv *rsv;
  1732. struct btrfs_trans_handle *trans;
  1733. u64 lockstart = round_up(offset, BTRFS_I(inode)->root->sectorsize);
  1734. u64 lockend = round_down(offset + len,
  1735. BTRFS_I(inode)->root->sectorsize) - 1;
  1736. u64 cur_offset = lockstart;
  1737. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  1738. u64 drop_end;
  1739. int ret = 0;
  1740. int err = 0;
  1741. bool same_page = ((offset >> PAGE_CACHE_SHIFT) ==
  1742. ((offset + len - 1) >> PAGE_CACHE_SHIFT));
  1743. btrfs_wait_ordered_range(inode, offset, len);
  1744. mutex_lock(&inode->i_mutex);
  1745. /*
  1746. * We needn't truncate any page which is beyond the end of the file
  1747. * because we are sure there is no data there.
  1748. */
  1749. /*
  1750. * Only do this if we are in the same page and we aren't doing the
  1751. * entire page.
  1752. */
  1753. if (same_page && len < PAGE_CACHE_SIZE) {
  1754. if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE))
  1755. ret = btrfs_truncate_page(inode, offset, len, 0);
  1756. mutex_unlock(&inode->i_mutex);
  1757. return ret;
  1758. }
  1759. /* zero back part of the first page */
  1760. if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
  1761. ret = btrfs_truncate_page(inode, offset, 0, 0);
  1762. if (ret) {
  1763. mutex_unlock(&inode->i_mutex);
  1764. return ret;
  1765. }
  1766. }
  1767. /* zero the front end of the last page */
  1768. if (offset + len < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
  1769. ret = btrfs_truncate_page(inode, offset + len, 0, 1);
  1770. if (ret) {
  1771. mutex_unlock(&inode->i_mutex);
  1772. return ret;
  1773. }
  1774. }
  1775. if (lockend < lockstart) {
  1776. mutex_unlock(&inode->i_mutex);
  1777. return 0;
  1778. }
  1779. while (1) {
  1780. struct btrfs_ordered_extent *ordered;
  1781. truncate_pagecache_range(inode, lockstart, lockend);
  1782. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1783. 0, &cached_state);
  1784. ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
  1785. /*
  1786. * We need to make sure we have no ordered extents in this range
  1787. * and nobody raced in and read a page in this range, if we did
  1788. * we need to try again.
  1789. */
  1790. if ((!ordered ||
  1791. (ordered->file_offset + ordered->len < lockstart ||
  1792. ordered->file_offset > lockend)) &&
  1793. !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
  1794. lockend, EXTENT_UPTODATE, 0,
  1795. cached_state)) {
  1796. if (ordered)
  1797. btrfs_put_ordered_extent(ordered);
  1798. break;
  1799. }
  1800. if (ordered)
  1801. btrfs_put_ordered_extent(ordered);
  1802. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
  1803. lockend, &cached_state, GFP_NOFS);
  1804. btrfs_wait_ordered_range(inode, lockstart,
  1805. lockend - lockstart + 1);
  1806. }
  1807. path = btrfs_alloc_path();
  1808. if (!path) {
  1809. ret = -ENOMEM;
  1810. goto out;
  1811. }
  1812. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  1813. if (!rsv) {
  1814. ret = -ENOMEM;
  1815. goto out_free;
  1816. }
  1817. rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
  1818. rsv->failfast = 1;
  1819. /*
  1820. * 1 - update the inode
  1821. * 1 - removing the extents in the range
  1822. * 1 - adding the hole extent
  1823. */
  1824. trans = btrfs_start_transaction(root, 3);
  1825. if (IS_ERR(trans)) {
  1826. err = PTR_ERR(trans);
  1827. goto out_free;
  1828. }
  1829. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  1830. min_size);
  1831. BUG_ON(ret);
  1832. trans->block_rsv = rsv;
  1833. while (cur_offset < lockend) {
  1834. ret = __btrfs_drop_extents(trans, root, inode, path,
  1835. cur_offset, lockend + 1,
  1836. &drop_end, 1);
  1837. if (ret != -ENOSPC)
  1838. break;
  1839. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1840. ret = fill_holes(trans, inode, path, cur_offset, drop_end);
  1841. if (ret) {
  1842. err = ret;
  1843. break;
  1844. }
  1845. cur_offset = drop_end;
  1846. ret = btrfs_update_inode(trans, root, inode);
  1847. if (ret) {
  1848. err = ret;
  1849. break;
  1850. }
  1851. btrfs_end_transaction(trans, root);
  1852. btrfs_btree_balance_dirty(root);
  1853. trans = btrfs_start_transaction(root, 3);
  1854. if (IS_ERR(trans)) {
  1855. ret = PTR_ERR(trans);
  1856. trans = NULL;
  1857. break;
  1858. }
  1859. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  1860. rsv, min_size);
  1861. BUG_ON(ret); /* shouldn't happen */
  1862. trans->block_rsv = rsv;
  1863. }
  1864. if (ret) {
  1865. err = ret;
  1866. goto out_trans;
  1867. }
  1868. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1869. ret = fill_holes(trans, inode, path, cur_offset, drop_end);
  1870. if (ret) {
  1871. err = ret;
  1872. goto out_trans;
  1873. }
  1874. out_trans:
  1875. if (!trans)
  1876. goto out_free;
  1877. inode_inc_iversion(inode);
  1878. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1879. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1880. ret = btrfs_update_inode(trans, root, inode);
  1881. btrfs_end_transaction(trans, root);
  1882. btrfs_btree_balance_dirty(root);
  1883. out_free:
  1884. btrfs_free_path(path);
  1885. btrfs_free_block_rsv(root, rsv);
  1886. out:
  1887. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1888. &cached_state, GFP_NOFS);
  1889. mutex_unlock(&inode->i_mutex);
  1890. if (ret && !err)
  1891. err = ret;
  1892. return err;
  1893. }
  1894. static long btrfs_fallocate(struct file *file, int mode,
  1895. loff_t offset, loff_t len)
  1896. {
  1897. struct inode *inode = file_inode(file);
  1898. struct extent_state *cached_state = NULL;
  1899. struct btrfs_root *root = BTRFS_I(inode)->root;
  1900. u64 cur_offset;
  1901. u64 last_byte;
  1902. u64 alloc_start;
  1903. u64 alloc_end;
  1904. u64 alloc_hint = 0;
  1905. u64 locked_end;
  1906. struct extent_map *em;
  1907. int blocksize = BTRFS_I(inode)->root->sectorsize;
  1908. int ret;
  1909. alloc_start = round_down(offset, blocksize);
  1910. alloc_end = round_up(offset + len, blocksize);
  1911. /* Make sure we aren't being give some crap mode */
  1912. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1913. return -EOPNOTSUPP;
  1914. if (mode & FALLOC_FL_PUNCH_HOLE)
  1915. return btrfs_punch_hole(inode, offset, len);
  1916. /*
  1917. * Make sure we have enough space before we do the
  1918. * allocation.
  1919. */
  1920. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
  1921. if (ret)
  1922. return ret;
  1923. if (root->fs_info->quota_enabled) {
  1924. ret = btrfs_qgroup_reserve(root, alloc_end - alloc_start);
  1925. if (ret)
  1926. goto out_reserve_fail;
  1927. }
  1928. /*
  1929. * wait for ordered IO before we have any locks. We'll loop again
  1930. * below with the locks held.
  1931. */
  1932. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1933. mutex_lock(&inode->i_mutex);
  1934. ret = inode_newsize_ok(inode, alloc_end);
  1935. if (ret)
  1936. goto out;
  1937. if (alloc_start > inode->i_size) {
  1938. ret = btrfs_cont_expand(inode, i_size_read(inode),
  1939. alloc_start);
  1940. if (ret)
  1941. goto out;
  1942. }
  1943. locked_end = alloc_end - 1;
  1944. while (1) {
  1945. struct btrfs_ordered_extent *ordered;
  1946. /* the extent lock is ordered inside the running
  1947. * transaction
  1948. */
  1949. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1950. locked_end, 0, &cached_state);
  1951. ordered = btrfs_lookup_first_ordered_extent(inode,
  1952. alloc_end - 1);
  1953. if (ordered &&
  1954. ordered->file_offset + ordered->len > alloc_start &&
  1955. ordered->file_offset < alloc_end) {
  1956. btrfs_put_ordered_extent(ordered);
  1957. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1958. alloc_start, locked_end,
  1959. &cached_state, GFP_NOFS);
  1960. /*
  1961. * we can't wait on the range with the transaction
  1962. * running or with the extent lock held
  1963. */
  1964. btrfs_wait_ordered_range(inode, alloc_start,
  1965. alloc_end - alloc_start);
  1966. } else {
  1967. if (ordered)
  1968. btrfs_put_ordered_extent(ordered);
  1969. break;
  1970. }
  1971. }
  1972. cur_offset = alloc_start;
  1973. while (1) {
  1974. u64 actual_end;
  1975. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1976. alloc_end - cur_offset, 0);
  1977. if (IS_ERR_OR_NULL(em)) {
  1978. if (!em)
  1979. ret = -ENOMEM;
  1980. else
  1981. ret = PTR_ERR(em);
  1982. break;
  1983. }
  1984. last_byte = min(extent_map_end(em), alloc_end);
  1985. actual_end = min_t(u64, extent_map_end(em), offset + len);
  1986. last_byte = ALIGN(last_byte, blocksize);
  1987. if (em->block_start == EXTENT_MAP_HOLE ||
  1988. (cur_offset >= inode->i_size &&
  1989. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1990. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1991. last_byte - cur_offset,
  1992. 1 << inode->i_blkbits,
  1993. offset + len,
  1994. &alloc_hint);
  1995. if (ret < 0) {
  1996. free_extent_map(em);
  1997. break;
  1998. }
  1999. } else if (actual_end > inode->i_size &&
  2000. !(mode & FALLOC_FL_KEEP_SIZE)) {
  2001. /*
  2002. * We didn't need to allocate any more space, but we
  2003. * still extended the size of the file so we need to
  2004. * update i_size.
  2005. */
  2006. inode->i_ctime = CURRENT_TIME;
  2007. i_size_write(inode, actual_end);
  2008. btrfs_ordered_update_i_size(inode, actual_end, NULL);
  2009. }
  2010. free_extent_map(em);
  2011. cur_offset = last_byte;
  2012. if (cur_offset >= alloc_end) {
  2013. ret = 0;
  2014. break;
  2015. }
  2016. }
  2017. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  2018. &cached_state, GFP_NOFS);
  2019. out:
  2020. mutex_unlock(&inode->i_mutex);
  2021. if (root->fs_info->quota_enabled)
  2022. btrfs_qgroup_free(root, alloc_end - alloc_start);
  2023. out_reserve_fail:
  2024. /* Let go of our reservation. */
  2025. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
  2026. return ret;
  2027. }
  2028. static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
  2029. {
  2030. struct btrfs_root *root = BTRFS_I(inode)->root;
  2031. struct extent_map *em;
  2032. struct extent_state *cached_state = NULL;
  2033. u64 lockstart = *offset;
  2034. u64 lockend = i_size_read(inode);
  2035. u64 start = *offset;
  2036. u64 orig_start = *offset;
  2037. u64 len = i_size_read(inode);
  2038. u64 last_end = 0;
  2039. int ret = 0;
  2040. lockend = max_t(u64, root->sectorsize, lockend);
  2041. if (lockend <= lockstart)
  2042. lockend = lockstart + root->sectorsize;
  2043. lockend--;
  2044. len = lockend - lockstart + 1;
  2045. len = max_t(u64, len, root->sectorsize);
  2046. if (inode->i_size == 0)
  2047. return -ENXIO;
  2048. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
  2049. &cached_state);
  2050. /*
  2051. * Delalloc is such a pain. If we have a hole and we have pending
  2052. * delalloc for a portion of the hole we will get back a hole that
  2053. * exists for the entire range since it hasn't been actually written
  2054. * yet. So to take care of this case we need to look for an extent just
  2055. * before the position we want in case there is outstanding delalloc
  2056. * going on here.
  2057. */
  2058. if (whence == SEEK_HOLE && start != 0) {
  2059. if (start <= root->sectorsize)
  2060. em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
  2061. root->sectorsize, 0);
  2062. else
  2063. em = btrfs_get_extent_fiemap(inode, NULL, 0,
  2064. start - root->sectorsize,
  2065. root->sectorsize, 0);
  2066. if (IS_ERR(em)) {
  2067. ret = PTR_ERR(em);
  2068. goto out;
  2069. }
  2070. last_end = em->start + em->len;
  2071. if (em->block_start == EXTENT_MAP_DELALLOC)
  2072. last_end = min_t(u64, last_end, inode->i_size);
  2073. free_extent_map(em);
  2074. }
  2075. while (1) {
  2076. em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
  2077. if (IS_ERR(em)) {
  2078. ret = PTR_ERR(em);
  2079. break;
  2080. }
  2081. if (em->block_start == EXTENT_MAP_HOLE) {
  2082. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2083. if (last_end <= orig_start) {
  2084. free_extent_map(em);
  2085. ret = -ENXIO;
  2086. break;
  2087. }
  2088. }
  2089. if (whence == SEEK_HOLE) {
  2090. *offset = start;
  2091. free_extent_map(em);
  2092. break;
  2093. }
  2094. } else {
  2095. if (whence == SEEK_DATA) {
  2096. if (em->block_start == EXTENT_MAP_DELALLOC) {
  2097. if (start >= inode->i_size) {
  2098. free_extent_map(em);
  2099. ret = -ENXIO;
  2100. break;
  2101. }
  2102. }
  2103. if (!test_bit(EXTENT_FLAG_PREALLOC,
  2104. &em->flags)) {
  2105. *offset = start;
  2106. free_extent_map(em);
  2107. break;
  2108. }
  2109. }
  2110. }
  2111. start = em->start + em->len;
  2112. last_end = em->start + em->len;
  2113. if (em->block_start == EXTENT_MAP_DELALLOC)
  2114. last_end = min_t(u64, last_end, inode->i_size);
  2115. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2116. free_extent_map(em);
  2117. ret = -ENXIO;
  2118. break;
  2119. }
  2120. free_extent_map(em);
  2121. cond_resched();
  2122. }
  2123. if (!ret)
  2124. *offset = min(*offset, inode->i_size);
  2125. out:
  2126. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  2127. &cached_state, GFP_NOFS);
  2128. return ret;
  2129. }
  2130. static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
  2131. {
  2132. struct inode *inode = file->f_mapping->host;
  2133. int ret;
  2134. mutex_lock(&inode->i_mutex);
  2135. switch (whence) {
  2136. case SEEK_END:
  2137. case SEEK_CUR:
  2138. offset = generic_file_llseek(file, offset, whence);
  2139. goto out;
  2140. case SEEK_DATA:
  2141. case SEEK_HOLE:
  2142. if (offset >= i_size_read(inode)) {
  2143. mutex_unlock(&inode->i_mutex);
  2144. return -ENXIO;
  2145. }
  2146. ret = find_desired_extent(inode, &offset, whence);
  2147. if (ret) {
  2148. mutex_unlock(&inode->i_mutex);
  2149. return ret;
  2150. }
  2151. }
  2152. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
  2153. offset = -EINVAL;
  2154. goto out;
  2155. }
  2156. if (offset > inode->i_sb->s_maxbytes) {
  2157. offset = -EINVAL;
  2158. goto out;
  2159. }
  2160. /* Special lock needed here? */
  2161. if (offset != file->f_pos) {
  2162. file->f_pos = offset;
  2163. file->f_version = 0;
  2164. }
  2165. out:
  2166. mutex_unlock(&inode->i_mutex);
  2167. return offset;
  2168. }
  2169. const struct file_operations btrfs_file_operations = {
  2170. .llseek = btrfs_file_llseek,
  2171. .read = do_sync_read,
  2172. .write = do_sync_write,
  2173. .aio_read = generic_file_aio_read,
  2174. .splice_read = generic_file_splice_read,
  2175. .aio_write = btrfs_file_aio_write,
  2176. .mmap = btrfs_file_mmap,
  2177. .open = generic_file_open,
  2178. .release = btrfs_release_file,
  2179. .fsync = btrfs_sync_file,
  2180. .fallocate = btrfs_fallocate,
  2181. .unlocked_ioctl = btrfs_ioctl,
  2182. #ifdef CONFIG_COMPAT
  2183. .compat_ioctl = btrfs_ioctl,
  2184. #endif
  2185. };
  2186. void btrfs_auto_defrag_exit(void)
  2187. {
  2188. if (btrfs_inode_defrag_cachep)
  2189. kmem_cache_destroy(btrfs_inode_defrag_cachep);
  2190. }
  2191. int btrfs_auto_defrag_init(void)
  2192. {
  2193. btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
  2194. sizeof(struct inode_defrag), 0,
  2195. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  2196. NULL);
  2197. if (!btrfs_inode_defrag_cachep)
  2198. return -ENOMEM;
  2199. return 0;
  2200. }