file.c 58 KB

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