file.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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. int found = 0;
  551. if (drop_cache)
  552. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  553. if (start >= BTRFS_I(inode)->disk_i_size)
  554. modify_tree = 0;
  555. while (1) {
  556. recow = 0;
  557. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  558. search_start, modify_tree);
  559. if (ret < 0)
  560. break;
  561. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  562. leaf = path->nodes[0];
  563. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  564. if (key.objectid == ino &&
  565. key.type == BTRFS_EXTENT_DATA_KEY)
  566. path->slots[0]--;
  567. }
  568. ret = 0;
  569. next_slot:
  570. leaf = path->nodes[0];
  571. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  572. BUG_ON(del_nr > 0);
  573. ret = btrfs_next_leaf(root, path);
  574. if (ret < 0)
  575. break;
  576. if (ret > 0) {
  577. ret = 0;
  578. break;
  579. }
  580. leaf = path->nodes[0];
  581. recow = 1;
  582. }
  583. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  584. if (key.objectid > ino ||
  585. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  586. break;
  587. fi = btrfs_item_ptr(leaf, path->slots[0],
  588. struct btrfs_file_extent_item);
  589. extent_type = btrfs_file_extent_type(leaf, fi);
  590. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  591. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  592. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  593. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  594. extent_offset = btrfs_file_extent_offset(leaf, fi);
  595. extent_end = key.offset +
  596. btrfs_file_extent_num_bytes(leaf, fi);
  597. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  598. extent_end = key.offset +
  599. btrfs_file_extent_inline_len(leaf, fi);
  600. } else {
  601. WARN_ON(1);
  602. extent_end = search_start;
  603. }
  604. if (extent_end <= search_start) {
  605. path->slots[0]++;
  606. goto next_slot;
  607. }
  608. found = 1;
  609. search_start = max(key.offset, start);
  610. if (recow || !modify_tree) {
  611. modify_tree = -1;
  612. btrfs_release_path(path);
  613. continue;
  614. }
  615. /*
  616. * | - range to drop - |
  617. * | -------- extent -------- |
  618. */
  619. if (start > key.offset && end < extent_end) {
  620. BUG_ON(del_nr > 0);
  621. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  622. memcpy(&new_key, &key, sizeof(new_key));
  623. new_key.offset = start;
  624. ret = btrfs_duplicate_item(trans, root, path,
  625. &new_key);
  626. if (ret == -EAGAIN) {
  627. btrfs_release_path(path);
  628. continue;
  629. }
  630. if (ret < 0)
  631. break;
  632. leaf = path->nodes[0];
  633. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  634. struct btrfs_file_extent_item);
  635. btrfs_set_file_extent_num_bytes(leaf, fi,
  636. start - key.offset);
  637. fi = btrfs_item_ptr(leaf, path->slots[0],
  638. struct btrfs_file_extent_item);
  639. extent_offset += start - key.offset;
  640. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  641. btrfs_set_file_extent_num_bytes(leaf, fi,
  642. extent_end - start);
  643. btrfs_mark_buffer_dirty(leaf);
  644. if (update_refs && disk_bytenr > 0) {
  645. ret = btrfs_inc_extent_ref(trans, root,
  646. disk_bytenr, num_bytes, 0,
  647. root->root_key.objectid,
  648. new_key.objectid,
  649. start - extent_offset, 0);
  650. BUG_ON(ret); /* -ENOMEM */
  651. }
  652. key.offset = start;
  653. }
  654. /*
  655. * | ---- range to drop ----- |
  656. * | -------- extent -------- |
  657. */
  658. if (start <= key.offset && end < extent_end) {
  659. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  660. memcpy(&new_key, &key, sizeof(new_key));
  661. new_key.offset = end;
  662. btrfs_set_item_key_safe(trans, root, path, &new_key);
  663. extent_offset += end - key.offset;
  664. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  665. btrfs_set_file_extent_num_bytes(leaf, fi,
  666. extent_end - end);
  667. btrfs_mark_buffer_dirty(leaf);
  668. if (update_refs && disk_bytenr > 0)
  669. inode_sub_bytes(inode, end - key.offset);
  670. break;
  671. }
  672. search_start = extent_end;
  673. /*
  674. * | ---- range to drop ----- |
  675. * | -------- extent -------- |
  676. */
  677. if (start > key.offset && end >= extent_end) {
  678. BUG_ON(del_nr > 0);
  679. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  680. btrfs_set_file_extent_num_bytes(leaf, fi,
  681. start - key.offset);
  682. btrfs_mark_buffer_dirty(leaf);
  683. if (update_refs && disk_bytenr > 0)
  684. inode_sub_bytes(inode, extent_end - start);
  685. if (end == extent_end)
  686. break;
  687. path->slots[0]++;
  688. goto next_slot;
  689. }
  690. /*
  691. * | ---- range to drop ----- |
  692. * | ------ extent ------ |
  693. */
  694. if (start <= key.offset && end >= extent_end) {
  695. if (del_nr == 0) {
  696. del_slot = path->slots[0];
  697. del_nr = 1;
  698. } else {
  699. BUG_ON(del_slot + del_nr != path->slots[0]);
  700. del_nr++;
  701. }
  702. if (update_refs &&
  703. extent_type == BTRFS_FILE_EXTENT_INLINE) {
  704. inode_sub_bytes(inode,
  705. extent_end - key.offset);
  706. extent_end = ALIGN(extent_end,
  707. root->sectorsize);
  708. } else if (update_refs && disk_bytenr > 0) {
  709. ret = btrfs_free_extent(trans, root,
  710. disk_bytenr, num_bytes, 0,
  711. root->root_key.objectid,
  712. key.objectid, key.offset -
  713. extent_offset, 0);
  714. BUG_ON(ret); /* -ENOMEM */
  715. inode_sub_bytes(inode,
  716. extent_end - key.offset);
  717. }
  718. if (end == extent_end)
  719. break;
  720. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  721. path->slots[0]++;
  722. goto next_slot;
  723. }
  724. ret = btrfs_del_items(trans, root, path, del_slot,
  725. del_nr);
  726. if (ret) {
  727. btrfs_abort_transaction(trans, root, ret);
  728. break;
  729. }
  730. del_nr = 0;
  731. del_slot = 0;
  732. btrfs_release_path(path);
  733. continue;
  734. }
  735. BUG_ON(1);
  736. }
  737. if (!ret && del_nr > 0) {
  738. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  739. if (ret)
  740. btrfs_abort_transaction(trans, root, ret);
  741. }
  742. if (drop_end)
  743. *drop_end = found ? min(end, extent_end) : end;
  744. btrfs_release_path(path);
  745. return ret;
  746. }
  747. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  748. struct btrfs_root *root, struct inode *inode, u64 start,
  749. u64 end, int drop_cache)
  750. {
  751. struct btrfs_path *path;
  752. int ret;
  753. path = btrfs_alloc_path();
  754. if (!path)
  755. return -ENOMEM;
  756. ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
  757. drop_cache);
  758. btrfs_free_path(path);
  759. return ret;
  760. }
  761. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  762. u64 objectid, u64 bytenr, u64 orig_offset,
  763. u64 *start, u64 *end)
  764. {
  765. struct btrfs_file_extent_item *fi;
  766. struct btrfs_key key;
  767. u64 extent_end;
  768. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  769. return 0;
  770. btrfs_item_key_to_cpu(leaf, &key, slot);
  771. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  772. return 0;
  773. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  774. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  775. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  776. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  777. btrfs_file_extent_compression(leaf, fi) ||
  778. btrfs_file_extent_encryption(leaf, fi) ||
  779. btrfs_file_extent_other_encoding(leaf, fi))
  780. return 0;
  781. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  782. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  783. return 0;
  784. *start = key.offset;
  785. *end = extent_end;
  786. return 1;
  787. }
  788. /*
  789. * Mark extent in the range start - end as written.
  790. *
  791. * This changes extent type from 'pre-allocated' to 'regular'. If only
  792. * part of extent is marked as written, the extent will be split into
  793. * two or three.
  794. */
  795. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  796. struct inode *inode, u64 start, u64 end)
  797. {
  798. struct btrfs_root *root = BTRFS_I(inode)->root;
  799. struct extent_buffer *leaf;
  800. struct btrfs_path *path;
  801. struct btrfs_file_extent_item *fi;
  802. struct btrfs_key key;
  803. struct btrfs_key new_key;
  804. u64 bytenr;
  805. u64 num_bytes;
  806. u64 extent_end;
  807. u64 orig_offset;
  808. u64 other_start;
  809. u64 other_end;
  810. u64 split;
  811. int del_nr = 0;
  812. int del_slot = 0;
  813. int recow;
  814. int ret;
  815. u64 ino = btrfs_ino(inode);
  816. path = btrfs_alloc_path();
  817. if (!path)
  818. return -ENOMEM;
  819. again:
  820. recow = 0;
  821. split = start;
  822. key.objectid = ino;
  823. key.type = BTRFS_EXTENT_DATA_KEY;
  824. key.offset = split;
  825. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  826. if (ret < 0)
  827. goto out;
  828. if (ret > 0 && path->slots[0] > 0)
  829. path->slots[0]--;
  830. leaf = path->nodes[0];
  831. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  832. BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
  833. fi = btrfs_item_ptr(leaf, path->slots[0],
  834. struct btrfs_file_extent_item);
  835. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  836. BTRFS_FILE_EXTENT_PREALLOC);
  837. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  838. BUG_ON(key.offset > start || extent_end < end);
  839. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  840. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  841. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  842. memcpy(&new_key, &key, sizeof(new_key));
  843. if (start == key.offset && end < extent_end) {
  844. other_start = 0;
  845. other_end = start;
  846. if (extent_mergeable(leaf, path->slots[0] - 1,
  847. ino, bytenr, orig_offset,
  848. &other_start, &other_end)) {
  849. new_key.offset = end;
  850. btrfs_set_item_key_safe(trans, root, path, &new_key);
  851. fi = btrfs_item_ptr(leaf, path->slots[0],
  852. struct btrfs_file_extent_item);
  853. btrfs_set_file_extent_generation(leaf, fi,
  854. trans->transid);
  855. btrfs_set_file_extent_num_bytes(leaf, fi,
  856. extent_end - end);
  857. btrfs_set_file_extent_offset(leaf, fi,
  858. end - orig_offset);
  859. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  860. struct btrfs_file_extent_item);
  861. btrfs_set_file_extent_generation(leaf, fi,
  862. trans->transid);
  863. btrfs_set_file_extent_num_bytes(leaf, fi,
  864. end - other_start);
  865. btrfs_mark_buffer_dirty(leaf);
  866. goto out;
  867. }
  868. }
  869. if (start > key.offset && end == extent_end) {
  870. other_start = end;
  871. other_end = 0;
  872. if (extent_mergeable(leaf, path->slots[0] + 1,
  873. ino, bytenr, orig_offset,
  874. &other_start, &other_end)) {
  875. fi = btrfs_item_ptr(leaf, path->slots[0],
  876. struct btrfs_file_extent_item);
  877. btrfs_set_file_extent_num_bytes(leaf, fi,
  878. start - key.offset);
  879. btrfs_set_file_extent_generation(leaf, fi,
  880. trans->transid);
  881. path->slots[0]++;
  882. new_key.offset = start;
  883. btrfs_set_item_key_safe(trans, root, path, &new_key);
  884. fi = btrfs_item_ptr(leaf, path->slots[0],
  885. struct btrfs_file_extent_item);
  886. btrfs_set_file_extent_generation(leaf, fi,
  887. trans->transid);
  888. btrfs_set_file_extent_num_bytes(leaf, fi,
  889. other_end - start);
  890. btrfs_set_file_extent_offset(leaf, fi,
  891. start - orig_offset);
  892. btrfs_mark_buffer_dirty(leaf);
  893. goto out;
  894. }
  895. }
  896. while (start > key.offset || end < extent_end) {
  897. if (key.offset == start)
  898. split = end;
  899. new_key.offset = split;
  900. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  901. if (ret == -EAGAIN) {
  902. btrfs_release_path(path);
  903. goto again;
  904. }
  905. if (ret < 0) {
  906. btrfs_abort_transaction(trans, root, ret);
  907. goto out;
  908. }
  909. leaf = path->nodes[0];
  910. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  911. struct btrfs_file_extent_item);
  912. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  913. btrfs_set_file_extent_num_bytes(leaf, fi,
  914. split - key.offset);
  915. fi = btrfs_item_ptr(leaf, path->slots[0],
  916. struct btrfs_file_extent_item);
  917. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  918. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  919. btrfs_set_file_extent_num_bytes(leaf, fi,
  920. extent_end - split);
  921. btrfs_mark_buffer_dirty(leaf);
  922. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  923. root->root_key.objectid,
  924. ino, orig_offset, 0);
  925. BUG_ON(ret); /* -ENOMEM */
  926. if (split == start) {
  927. key.offset = start;
  928. } else {
  929. BUG_ON(start != key.offset);
  930. path->slots[0]--;
  931. extent_end = end;
  932. }
  933. recow = 1;
  934. }
  935. other_start = end;
  936. other_end = 0;
  937. if (extent_mergeable(leaf, path->slots[0] + 1,
  938. ino, bytenr, orig_offset,
  939. &other_start, &other_end)) {
  940. if (recow) {
  941. btrfs_release_path(path);
  942. goto again;
  943. }
  944. extent_end = other_end;
  945. del_slot = path->slots[0] + 1;
  946. del_nr++;
  947. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  948. 0, root->root_key.objectid,
  949. ino, orig_offset, 0);
  950. BUG_ON(ret); /* -ENOMEM */
  951. }
  952. other_start = 0;
  953. other_end = start;
  954. if (extent_mergeable(leaf, path->slots[0] - 1,
  955. ino, bytenr, orig_offset,
  956. &other_start, &other_end)) {
  957. if (recow) {
  958. btrfs_release_path(path);
  959. goto again;
  960. }
  961. key.offset = other_start;
  962. del_slot = path->slots[0];
  963. del_nr++;
  964. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  965. 0, root->root_key.objectid,
  966. ino, orig_offset, 0);
  967. BUG_ON(ret); /* -ENOMEM */
  968. }
  969. if (del_nr == 0) {
  970. fi = btrfs_item_ptr(leaf, path->slots[0],
  971. struct btrfs_file_extent_item);
  972. btrfs_set_file_extent_type(leaf, fi,
  973. BTRFS_FILE_EXTENT_REG);
  974. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  975. btrfs_mark_buffer_dirty(leaf);
  976. } else {
  977. fi = btrfs_item_ptr(leaf, del_slot - 1,
  978. struct btrfs_file_extent_item);
  979. btrfs_set_file_extent_type(leaf, fi,
  980. BTRFS_FILE_EXTENT_REG);
  981. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  982. btrfs_set_file_extent_num_bytes(leaf, fi,
  983. extent_end - key.offset);
  984. btrfs_mark_buffer_dirty(leaf);
  985. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  986. if (ret < 0) {
  987. btrfs_abort_transaction(trans, root, ret);
  988. goto out;
  989. }
  990. }
  991. out:
  992. btrfs_free_path(path);
  993. return 0;
  994. }
  995. /*
  996. * on error we return an unlocked page and the error value
  997. * on success we return a locked page and 0
  998. */
  999. static int prepare_uptodate_page(struct page *page, u64 pos,
  1000. bool force_uptodate)
  1001. {
  1002. int ret = 0;
  1003. if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
  1004. !PageUptodate(page)) {
  1005. ret = btrfs_readpage(NULL, page);
  1006. if (ret)
  1007. return ret;
  1008. lock_page(page);
  1009. if (!PageUptodate(page)) {
  1010. unlock_page(page);
  1011. return -EIO;
  1012. }
  1013. }
  1014. return 0;
  1015. }
  1016. /*
  1017. * this gets pages into the page cache and locks them down, it also properly
  1018. * waits for data=ordered extents to finish before allowing the pages to be
  1019. * modified.
  1020. */
  1021. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  1022. struct page **pages, size_t num_pages,
  1023. loff_t pos, unsigned long first_index,
  1024. size_t write_bytes, bool force_uptodate)
  1025. {
  1026. struct extent_state *cached_state = NULL;
  1027. int i;
  1028. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1029. struct inode *inode = fdentry(file)->d_inode;
  1030. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1031. int err = 0;
  1032. int faili = 0;
  1033. u64 start_pos;
  1034. u64 last_pos;
  1035. start_pos = pos & ~((u64)root->sectorsize - 1);
  1036. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  1037. again:
  1038. for (i = 0; i < num_pages; i++) {
  1039. pages[i] = find_or_create_page(inode->i_mapping, index + i,
  1040. mask | __GFP_WRITE);
  1041. if (!pages[i]) {
  1042. faili = i - 1;
  1043. err = -ENOMEM;
  1044. goto fail;
  1045. }
  1046. if (i == 0)
  1047. err = prepare_uptodate_page(pages[i], pos,
  1048. force_uptodate);
  1049. if (i == num_pages - 1)
  1050. err = prepare_uptodate_page(pages[i],
  1051. pos + write_bytes, false);
  1052. if (err) {
  1053. page_cache_release(pages[i]);
  1054. faili = i - 1;
  1055. goto fail;
  1056. }
  1057. wait_on_page_writeback(pages[i]);
  1058. }
  1059. err = 0;
  1060. if (start_pos < inode->i_size) {
  1061. struct btrfs_ordered_extent *ordered;
  1062. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1063. start_pos, last_pos - 1, 0, &cached_state);
  1064. ordered = btrfs_lookup_first_ordered_extent(inode,
  1065. last_pos - 1);
  1066. if (ordered &&
  1067. ordered->file_offset + ordered->len > start_pos &&
  1068. ordered->file_offset < last_pos) {
  1069. btrfs_put_ordered_extent(ordered);
  1070. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1071. start_pos, last_pos - 1,
  1072. &cached_state, GFP_NOFS);
  1073. for (i = 0; i < num_pages; i++) {
  1074. unlock_page(pages[i]);
  1075. page_cache_release(pages[i]);
  1076. }
  1077. btrfs_wait_ordered_range(inode, start_pos,
  1078. last_pos - start_pos);
  1079. goto again;
  1080. }
  1081. if (ordered)
  1082. btrfs_put_ordered_extent(ordered);
  1083. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  1084. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1085. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  1086. 0, 0, &cached_state, GFP_NOFS);
  1087. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1088. start_pos, last_pos - 1, &cached_state,
  1089. GFP_NOFS);
  1090. }
  1091. for (i = 0; i < num_pages; i++) {
  1092. if (clear_page_dirty_for_io(pages[i]))
  1093. account_page_redirty(pages[i]);
  1094. set_page_extent_mapped(pages[i]);
  1095. WARN_ON(!PageLocked(pages[i]));
  1096. }
  1097. return 0;
  1098. fail:
  1099. while (faili >= 0) {
  1100. unlock_page(pages[faili]);
  1101. page_cache_release(pages[faili]);
  1102. faili--;
  1103. }
  1104. return err;
  1105. }
  1106. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  1107. struct iov_iter *i,
  1108. loff_t pos)
  1109. {
  1110. struct inode *inode = fdentry(file)->d_inode;
  1111. struct btrfs_root *root = BTRFS_I(inode)->root;
  1112. struct page **pages = NULL;
  1113. unsigned long first_index;
  1114. size_t num_written = 0;
  1115. int nrptrs;
  1116. int ret = 0;
  1117. bool force_page_uptodate = false;
  1118. nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
  1119. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  1120. (sizeof(struct page *)));
  1121. nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
  1122. nrptrs = max(nrptrs, 8);
  1123. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  1124. if (!pages)
  1125. return -ENOMEM;
  1126. first_index = pos >> PAGE_CACHE_SHIFT;
  1127. while (iov_iter_count(i) > 0) {
  1128. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1129. size_t write_bytes = min(iov_iter_count(i),
  1130. nrptrs * (size_t)PAGE_CACHE_SIZE -
  1131. offset);
  1132. size_t num_pages = (write_bytes + offset +
  1133. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1134. size_t dirty_pages;
  1135. size_t copied;
  1136. WARN_ON(num_pages > nrptrs);
  1137. /*
  1138. * Fault pages before locking them in prepare_pages
  1139. * to avoid recursive lock
  1140. */
  1141. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  1142. ret = -EFAULT;
  1143. break;
  1144. }
  1145. ret = btrfs_delalloc_reserve_space(inode,
  1146. num_pages << PAGE_CACHE_SHIFT);
  1147. if (ret)
  1148. break;
  1149. /*
  1150. * This is going to setup the pages array with the number of
  1151. * pages we want, so we don't really need to worry about the
  1152. * contents of pages from loop to loop
  1153. */
  1154. ret = prepare_pages(root, file, pages, num_pages,
  1155. pos, first_index, write_bytes,
  1156. force_page_uptodate);
  1157. if (ret) {
  1158. btrfs_delalloc_release_space(inode,
  1159. num_pages << PAGE_CACHE_SHIFT);
  1160. break;
  1161. }
  1162. copied = btrfs_copy_from_user(pos, num_pages,
  1163. write_bytes, pages, i);
  1164. /*
  1165. * if we have trouble faulting in the pages, fall
  1166. * back to one page at a time
  1167. */
  1168. if (copied < write_bytes)
  1169. nrptrs = 1;
  1170. if (copied == 0) {
  1171. force_page_uptodate = true;
  1172. dirty_pages = 0;
  1173. } else {
  1174. force_page_uptodate = false;
  1175. dirty_pages = (copied + offset +
  1176. PAGE_CACHE_SIZE - 1) >>
  1177. PAGE_CACHE_SHIFT;
  1178. }
  1179. /*
  1180. * If we had a short copy we need to release the excess delaloc
  1181. * bytes we reserved. We need to increment outstanding_extents
  1182. * because btrfs_delalloc_release_space will decrement it, but
  1183. * we still have an outstanding extent for the chunk we actually
  1184. * managed to copy.
  1185. */
  1186. if (num_pages > dirty_pages) {
  1187. if (copied > 0) {
  1188. spin_lock(&BTRFS_I(inode)->lock);
  1189. BTRFS_I(inode)->outstanding_extents++;
  1190. spin_unlock(&BTRFS_I(inode)->lock);
  1191. }
  1192. btrfs_delalloc_release_space(inode,
  1193. (num_pages - dirty_pages) <<
  1194. PAGE_CACHE_SHIFT);
  1195. }
  1196. if (copied > 0) {
  1197. ret = btrfs_dirty_pages(root, inode, pages,
  1198. dirty_pages, pos, copied,
  1199. NULL);
  1200. if (ret) {
  1201. btrfs_delalloc_release_space(inode,
  1202. dirty_pages << PAGE_CACHE_SHIFT);
  1203. btrfs_drop_pages(pages, num_pages);
  1204. break;
  1205. }
  1206. }
  1207. btrfs_drop_pages(pages, num_pages);
  1208. cond_resched();
  1209. balance_dirty_pages_ratelimited(inode->i_mapping);
  1210. if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1211. btrfs_btree_balance_dirty(root, 1);
  1212. pos += copied;
  1213. num_written += copied;
  1214. }
  1215. kfree(pages);
  1216. return num_written ? num_written : ret;
  1217. }
  1218. static ssize_t __btrfs_direct_write(struct kiocb *iocb,
  1219. const struct iovec *iov,
  1220. unsigned long nr_segs, loff_t pos,
  1221. loff_t *ppos, size_t count, size_t ocount)
  1222. {
  1223. struct file *file = iocb->ki_filp;
  1224. struct iov_iter i;
  1225. ssize_t written;
  1226. ssize_t written_buffered;
  1227. loff_t endbyte;
  1228. int err;
  1229. written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
  1230. count, ocount);
  1231. if (written < 0 || written == count)
  1232. return written;
  1233. pos += written;
  1234. count -= written;
  1235. iov_iter_init(&i, iov, nr_segs, count, written);
  1236. written_buffered = __btrfs_buffered_write(file, &i, pos);
  1237. if (written_buffered < 0) {
  1238. err = written_buffered;
  1239. goto out;
  1240. }
  1241. endbyte = pos + written_buffered - 1;
  1242. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  1243. if (err)
  1244. goto out;
  1245. written += written_buffered;
  1246. *ppos = pos + written_buffered;
  1247. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
  1248. endbyte >> PAGE_CACHE_SHIFT);
  1249. out:
  1250. return written ? written : err;
  1251. }
  1252. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  1253. const struct iovec *iov,
  1254. unsigned long nr_segs, loff_t pos)
  1255. {
  1256. struct file *file = iocb->ki_filp;
  1257. struct inode *inode = fdentry(file)->d_inode;
  1258. struct btrfs_root *root = BTRFS_I(inode)->root;
  1259. loff_t *ppos = &iocb->ki_pos;
  1260. u64 start_pos;
  1261. ssize_t num_written = 0;
  1262. ssize_t err = 0;
  1263. size_t count, ocount;
  1264. sb_start_write(inode->i_sb);
  1265. mutex_lock(&inode->i_mutex);
  1266. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1267. if (err) {
  1268. mutex_unlock(&inode->i_mutex);
  1269. goto out;
  1270. }
  1271. count = ocount;
  1272. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1273. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1274. if (err) {
  1275. mutex_unlock(&inode->i_mutex);
  1276. goto out;
  1277. }
  1278. if (count == 0) {
  1279. mutex_unlock(&inode->i_mutex);
  1280. goto out;
  1281. }
  1282. err = file_remove_suid(file);
  1283. if (err) {
  1284. mutex_unlock(&inode->i_mutex);
  1285. goto out;
  1286. }
  1287. /*
  1288. * If BTRFS flips readonly due to some impossible error
  1289. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1290. * although we have opened a file as writable, we have
  1291. * to stop this write operation to ensure FS consistency.
  1292. */
  1293. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  1294. mutex_unlock(&inode->i_mutex);
  1295. err = -EROFS;
  1296. goto out;
  1297. }
  1298. err = file_update_time(file);
  1299. if (err) {
  1300. mutex_unlock(&inode->i_mutex);
  1301. goto out;
  1302. }
  1303. start_pos = round_down(pos, root->sectorsize);
  1304. if (start_pos > i_size_read(inode)) {
  1305. err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
  1306. if (err) {
  1307. mutex_unlock(&inode->i_mutex);
  1308. goto out;
  1309. }
  1310. }
  1311. if (unlikely(file->f_flags & O_DIRECT)) {
  1312. num_written = __btrfs_direct_write(iocb, iov, nr_segs,
  1313. pos, ppos, count, ocount);
  1314. } else {
  1315. struct iov_iter i;
  1316. iov_iter_init(&i, iov, nr_segs, count, num_written);
  1317. num_written = __btrfs_buffered_write(file, &i, pos);
  1318. if (num_written > 0)
  1319. *ppos = pos + num_written;
  1320. }
  1321. mutex_unlock(&inode->i_mutex);
  1322. /*
  1323. * we want to make sure fsync finds this change
  1324. * but we haven't joined a transaction running right now.
  1325. *
  1326. * Later on, someone is sure to update the inode and get the
  1327. * real transid recorded.
  1328. *
  1329. * We set last_trans now to the fs_info generation + 1,
  1330. * this will either be one more than the running transaction
  1331. * or the generation used for the next transaction if there isn't
  1332. * one running right now.
  1333. */
  1334. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  1335. if (num_written > 0 || num_written == -EIOCBQUEUED) {
  1336. err = generic_write_sync(file, pos, num_written);
  1337. if (err < 0 && num_written > 0)
  1338. num_written = err;
  1339. }
  1340. out:
  1341. sb_end_write(inode->i_sb);
  1342. current->backing_dev_info = NULL;
  1343. return num_written ? num_written : err;
  1344. }
  1345. int btrfs_release_file(struct inode *inode, struct file *filp)
  1346. {
  1347. /*
  1348. * ordered_data_close is set by settattr when we are about to truncate
  1349. * a file from a non-zero size to a zero size. This tries to
  1350. * flush down new bytes that may have been written if the
  1351. * application were using truncate to replace a file in place.
  1352. */
  1353. if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  1354. &BTRFS_I(inode)->runtime_flags)) {
  1355. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1356. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1357. filemap_flush(inode->i_mapping);
  1358. }
  1359. if (filp->private_data)
  1360. btrfs_ioctl_trans_end(filp);
  1361. return 0;
  1362. }
  1363. /*
  1364. * fsync call for both files and directories. This logs the inode into
  1365. * the tree log instead of forcing full commits whenever possible.
  1366. *
  1367. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1368. * in the metadata btree are up to date for copying to the log.
  1369. *
  1370. * It drops the inode mutex before doing the tree log commit. This is an
  1371. * important optimization for directories because holding the mutex prevents
  1372. * new operations on the dir while we write to disk.
  1373. */
  1374. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  1375. {
  1376. struct dentry *dentry = file->f_path.dentry;
  1377. struct inode *inode = dentry->d_inode;
  1378. struct btrfs_root *root = BTRFS_I(inode)->root;
  1379. int ret = 0;
  1380. struct btrfs_trans_handle *trans;
  1381. trace_btrfs_sync_file(file, datasync);
  1382. /*
  1383. * We write the dirty pages in the range and wait until they complete
  1384. * out of the ->i_mutex. If so, we can flush the dirty pages by
  1385. * multi-task, and make the performance up.
  1386. */
  1387. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1388. if (ret)
  1389. return ret;
  1390. mutex_lock(&inode->i_mutex);
  1391. /*
  1392. * We flush the dirty pages again to avoid some dirty pages in the
  1393. * range being left.
  1394. */
  1395. atomic_inc(&root->log_batch);
  1396. btrfs_wait_ordered_range(inode, start, end);
  1397. atomic_inc(&root->log_batch);
  1398. /*
  1399. * check the transaction that last modified this inode
  1400. * and see if its already been committed
  1401. */
  1402. if (!BTRFS_I(inode)->last_trans) {
  1403. mutex_unlock(&inode->i_mutex);
  1404. goto out;
  1405. }
  1406. /*
  1407. * if the last transaction that changed this file was before
  1408. * the current transaction, we can bail out now without any
  1409. * syncing
  1410. */
  1411. smp_mb();
  1412. if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
  1413. BTRFS_I(inode)->last_trans <=
  1414. root->fs_info->last_trans_committed) {
  1415. BTRFS_I(inode)->last_trans = 0;
  1416. /*
  1417. * We'v had everything committed since the last time we were
  1418. * modified so clear this flag in case it was set for whatever
  1419. * reason, it's no longer relevant.
  1420. */
  1421. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1422. &BTRFS_I(inode)->runtime_flags);
  1423. mutex_unlock(&inode->i_mutex);
  1424. goto out;
  1425. }
  1426. /*
  1427. * ok we haven't committed the transaction yet, lets do a commit
  1428. */
  1429. if (file->private_data)
  1430. btrfs_ioctl_trans_end(file);
  1431. trans = btrfs_start_transaction(root, 0);
  1432. if (IS_ERR(trans)) {
  1433. ret = PTR_ERR(trans);
  1434. mutex_unlock(&inode->i_mutex);
  1435. goto out;
  1436. }
  1437. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1438. if (ret < 0) {
  1439. mutex_unlock(&inode->i_mutex);
  1440. goto out;
  1441. }
  1442. /* we've logged all the items and now have a consistent
  1443. * version of the file in the log. It is possible that
  1444. * someone will come in and modify the file, but that's
  1445. * fine because the log is consistent on disk, and we
  1446. * have references to all of the file's extents
  1447. *
  1448. * It is possible that someone will come in and log the
  1449. * file again, but that will end up using the synchronization
  1450. * inside btrfs_sync_log to keep things safe.
  1451. */
  1452. mutex_unlock(&inode->i_mutex);
  1453. if (ret != BTRFS_NO_LOG_SYNC) {
  1454. if (ret > 0) {
  1455. ret = btrfs_commit_transaction(trans, root);
  1456. } else {
  1457. ret = btrfs_sync_log(trans, root);
  1458. if (ret == 0)
  1459. ret = btrfs_end_transaction(trans, root);
  1460. else
  1461. ret = btrfs_commit_transaction(trans, root);
  1462. }
  1463. } else {
  1464. ret = btrfs_end_transaction(trans, root);
  1465. }
  1466. out:
  1467. return ret > 0 ? -EIO : ret;
  1468. }
  1469. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1470. .fault = filemap_fault,
  1471. .page_mkwrite = btrfs_page_mkwrite,
  1472. .remap_pages = generic_file_remap_pages,
  1473. };
  1474. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1475. {
  1476. struct address_space *mapping = filp->f_mapping;
  1477. if (!mapping->a_ops->readpage)
  1478. return -ENOEXEC;
  1479. file_accessed(filp);
  1480. vma->vm_ops = &btrfs_file_vm_ops;
  1481. return 0;
  1482. }
  1483. static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
  1484. int slot, u64 start, u64 end)
  1485. {
  1486. struct btrfs_file_extent_item *fi;
  1487. struct btrfs_key key;
  1488. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  1489. return 0;
  1490. btrfs_item_key_to_cpu(leaf, &key, slot);
  1491. if (key.objectid != btrfs_ino(inode) ||
  1492. key.type != BTRFS_EXTENT_DATA_KEY)
  1493. return 0;
  1494. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  1495. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
  1496. return 0;
  1497. if (btrfs_file_extent_disk_bytenr(leaf, fi))
  1498. return 0;
  1499. if (key.offset == end)
  1500. return 1;
  1501. if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
  1502. return 1;
  1503. return 0;
  1504. }
  1505. static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
  1506. struct btrfs_path *path, u64 offset, u64 end)
  1507. {
  1508. struct btrfs_root *root = BTRFS_I(inode)->root;
  1509. struct extent_buffer *leaf;
  1510. struct btrfs_file_extent_item *fi;
  1511. struct extent_map *hole_em;
  1512. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1513. struct btrfs_key key;
  1514. int ret;
  1515. key.objectid = btrfs_ino(inode);
  1516. key.type = BTRFS_EXTENT_DATA_KEY;
  1517. key.offset = offset;
  1518. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1519. if (ret < 0)
  1520. return ret;
  1521. BUG_ON(!ret);
  1522. leaf = path->nodes[0];
  1523. if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
  1524. u64 num_bytes;
  1525. path->slots[0]--;
  1526. fi = btrfs_item_ptr(leaf, path->slots[0],
  1527. struct btrfs_file_extent_item);
  1528. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
  1529. end - offset;
  1530. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1531. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  1532. btrfs_set_file_extent_offset(leaf, fi, 0);
  1533. btrfs_mark_buffer_dirty(leaf);
  1534. goto out;
  1535. }
  1536. if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
  1537. u64 num_bytes;
  1538. path->slots[0]++;
  1539. key.offset = offset;
  1540. btrfs_set_item_key_safe(trans, root, path, &key);
  1541. fi = btrfs_item_ptr(leaf, path->slots[0],
  1542. struct btrfs_file_extent_item);
  1543. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
  1544. offset;
  1545. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1546. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  1547. btrfs_set_file_extent_offset(leaf, fi, 0);
  1548. btrfs_mark_buffer_dirty(leaf);
  1549. goto out;
  1550. }
  1551. btrfs_release_path(path);
  1552. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  1553. 0, 0, end - offset, 0, end - offset,
  1554. 0, 0, 0);
  1555. if (ret)
  1556. return ret;
  1557. out:
  1558. btrfs_release_path(path);
  1559. hole_em = alloc_extent_map();
  1560. if (!hole_em) {
  1561. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  1562. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1563. &BTRFS_I(inode)->runtime_flags);
  1564. } else {
  1565. hole_em->start = offset;
  1566. hole_em->len = end - offset;
  1567. hole_em->orig_start = offset;
  1568. hole_em->block_start = EXTENT_MAP_HOLE;
  1569. hole_em->block_len = 0;
  1570. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  1571. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  1572. hole_em->generation = trans->transid;
  1573. do {
  1574. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  1575. write_lock(&em_tree->lock);
  1576. ret = add_extent_mapping(em_tree, hole_em);
  1577. if (!ret)
  1578. list_move(&hole_em->list,
  1579. &em_tree->modified_extents);
  1580. write_unlock(&em_tree->lock);
  1581. } while (ret == -EEXIST);
  1582. free_extent_map(hole_em);
  1583. if (ret)
  1584. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1585. &BTRFS_I(inode)->runtime_flags);
  1586. }
  1587. return 0;
  1588. }
  1589. static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  1590. {
  1591. struct btrfs_root *root = BTRFS_I(inode)->root;
  1592. struct extent_state *cached_state = NULL;
  1593. struct btrfs_path *path;
  1594. struct btrfs_block_rsv *rsv;
  1595. struct btrfs_trans_handle *trans;
  1596. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1597. u64 lockstart = (offset + mask) & ~mask;
  1598. u64 lockend = ((offset + len) & ~mask) - 1;
  1599. u64 cur_offset = lockstart;
  1600. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  1601. u64 drop_end;
  1602. unsigned long nr;
  1603. int ret = 0;
  1604. int err = 0;
  1605. bool same_page = (offset >> PAGE_CACHE_SHIFT) ==
  1606. ((offset + len) >> PAGE_CACHE_SHIFT);
  1607. btrfs_wait_ordered_range(inode, offset, len);
  1608. mutex_lock(&inode->i_mutex);
  1609. if (offset >= inode->i_size) {
  1610. mutex_unlock(&inode->i_mutex);
  1611. return 0;
  1612. }
  1613. /*
  1614. * Only do this if we are in the same page and we aren't doing the
  1615. * entire page.
  1616. */
  1617. if (same_page && len < PAGE_CACHE_SIZE) {
  1618. ret = btrfs_truncate_page(inode, offset, len, 0);
  1619. mutex_unlock(&inode->i_mutex);
  1620. return ret;
  1621. }
  1622. /* zero back part of the first page */
  1623. ret = btrfs_truncate_page(inode, offset, 0, 0);
  1624. if (ret) {
  1625. mutex_unlock(&inode->i_mutex);
  1626. return ret;
  1627. }
  1628. /* zero the front end of the last page */
  1629. ret = btrfs_truncate_page(inode, offset + len, 0, 1);
  1630. if (ret) {
  1631. mutex_unlock(&inode->i_mutex);
  1632. return ret;
  1633. }
  1634. if (lockend < lockstart) {
  1635. mutex_unlock(&inode->i_mutex);
  1636. return 0;
  1637. }
  1638. while (1) {
  1639. struct btrfs_ordered_extent *ordered;
  1640. truncate_pagecache_range(inode, lockstart, lockend);
  1641. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1642. 0, &cached_state);
  1643. ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
  1644. /*
  1645. * We need to make sure we have no ordered extents in this range
  1646. * and nobody raced in and read a page in this range, if we did
  1647. * we need to try again.
  1648. */
  1649. if ((!ordered ||
  1650. (ordered->file_offset + ordered->len < lockstart ||
  1651. ordered->file_offset > lockend)) &&
  1652. !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
  1653. lockend, EXTENT_UPTODATE, 0,
  1654. cached_state)) {
  1655. if (ordered)
  1656. btrfs_put_ordered_extent(ordered);
  1657. break;
  1658. }
  1659. if (ordered)
  1660. btrfs_put_ordered_extent(ordered);
  1661. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
  1662. lockend, &cached_state, GFP_NOFS);
  1663. btrfs_wait_ordered_range(inode, lockstart,
  1664. lockend - lockstart + 1);
  1665. }
  1666. path = btrfs_alloc_path();
  1667. if (!path) {
  1668. ret = -ENOMEM;
  1669. goto out;
  1670. }
  1671. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  1672. if (!rsv) {
  1673. ret = -ENOMEM;
  1674. goto out_free;
  1675. }
  1676. rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
  1677. rsv->failfast = 1;
  1678. /*
  1679. * 1 - update the inode
  1680. * 1 - removing the extents in the range
  1681. * 1 - adding the hole extent
  1682. */
  1683. trans = btrfs_start_transaction(root, 3);
  1684. if (IS_ERR(trans)) {
  1685. err = PTR_ERR(trans);
  1686. goto out_free;
  1687. }
  1688. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  1689. min_size);
  1690. BUG_ON(ret);
  1691. trans->block_rsv = rsv;
  1692. while (cur_offset < lockend) {
  1693. ret = __btrfs_drop_extents(trans, root, inode, path,
  1694. cur_offset, lockend + 1,
  1695. &drop_end, 1);
  1696. if (ret != -ENOSPC)
  1697. break;
  1698. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1699. ret = fill_holes(trans, inode, path, cur_offset, drop_end);
  1700. if (ret) {
  1701. err = ret;
  1702. break;
  1703. }
  1704. cur_offset = drop_end;
  1705. ret = btrfs_update_inode(trans, root, inode);
  1706. if (ret) {
  1707. err = ret;
  1708. break;
  1709. }
  1710. nr = trans->blocks_used;
  1711. btrfs_end_transaction(trans, root);
  1712. btrfs_btree_balance_dirty(root, nr);
  1713. trans = btrfs_start_transaction(root, 3);
  1714. if (IS_ERR(trans)) {
  1715. ret = PTR_ERR(trans);
  1716. trans = NULL;
  1717. break;
  1718. }
  1719. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  1720. rsv, min_size);
  1721. BUG_ON(ret); /* shouldn't happen */
  1722. trans->block_rsv = rsv;
  1723. }
  1724. if (ret) {
  1725. err = ret;
  1726. goto out_trans;
  1727. }
  1728. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1729. ret = fill_holes(trans, inode, path, cur_offset, drop_end);
  1730. if (ret) {
  1731. err = ret;
  1732. goto out_trans;
  1733. }
  1734. out_trans:
  1735. if (!trans)
  1736. goto out_free;
  1737. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1738. ret = btrfs_update_inode(trans, root, inode);
  1739. nr = trans->blocks_used;
  1740. btrfs_end_transaction(trans, root);
  1741. btrfs_btree_balance_dirty(root, nr);
  1742. out_free:
  1743. btrfs_free_path(path);
  1744. btrfs_free_block_rsv(root, rsv);
  1745. out:
  1746. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1747. &cached_state, GFP_NOFS);
  1748. mutex_unlock(&inode->i_mutex);
  1749. if (ret && !err)
  1750. err = ret;
  1751. return err;
  1752. }
  1753. static long btrfs_fallocate(struct file *file, int mode,
  1754. loff_t offset, loff_t len)
  1755. {
  1756. struct inode *inode = file->f_path.dentry->d_inode;
  1757. struct extent_state *cached_state = NULL;
  1758. u64 cur_offset;
  1759. u64 last_byte;
  1760. u64 alloc_start;
  1761. u64 alloc_end;
  1762. u64 alloc_hint = 0;
  1763. u64 locked_end;
  1764. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1765. struct extent_map *em;
  1766. int ret;
  1767. alloc_start = offset & ~mask;
  1768. alloc_end = (offset + len + mask) & ~mask;
  1769. /* Make sure we aren't being give some crap mode */
  1770. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1771. return -EOPNOTSUPP;
  1772. if (mode & FALLOC_FL_PUNCH_HOLE)
  1773. return btrfs_punch_hole(inode, offset, len);
  1774. /*
  1775. * Make sure we have enough space before we do the
  1776. * allocation.
  1777. */
  1778. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start + 1);
  1779. if (ret)
  1780. return ret;
  1781. /*
  1782. * wait for ordered IO before we have any locks. We'll loop again
  1783. * below with the locks held.
  1784. */
  1785. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1786. mutex_lock(&inode->i_mutex);
  1787. ret = inode_newsize_ok(inode, alloc_end);
  1788. if (ret)
  1789. goto out;
  1790. if (alloc_start > inode->i_size) {
  1791. ret = btrfs_cont_expand(inode, i_size_read(inode),
  1792. alloc_start);
  1793. if (ret)
  1794. goto out;
  1795. }
  1796. locked_end = alloc_end - 1;
  1797. while (1) {
  1798. struct btrfs_ordered_extent *ordered;
  1799. /* the extent lock is ordered inside the running
  1800. * transaction
  1801. */
  1802. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1803. locked_end, 0, &cached_state);
  1804. ordered = btrfs_lookup_first_ordered_extent(inode,
  1805. alloc_end - 1);
  1806. if (ordered &&
  1807. ordered->file_offset + ordered->len > alloc_start &&
  1808. ordered->file_offset < alloc_end) {
  1809. btrfs_put_ordered_extent(ordered);
  1810. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1811. alloc_start, locked_end,
  1812. &cached_state, GFP_NOFS);
  1813. /*
  1814. * we can't wait on the range with the transaction
  1815. * running or with the extent lock held
  1816. */
  1817. btrfs_wait_ordered_range(inode, alloc_start,
  1818. alloc_end - alloc_start);
  1819. } else {
  1820. if (ordered)
  1821. btrfs_put_ordered_extent(ordered);
  1822. break;
  1823. }
  1824. }
  1825. cur_offset = alloc_start;
  1826. while (1) {
  1827. u64 actual_end;
  1828. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1829. alloc_end - cur_offset, 0);
  1830. if (IS_ERR_OR_NULL(em)) {
  1831. if (!em)
  1832. ret = -ENOMEM;
  1833. else
  1834. ret = PTR_ERR(em);
  1835. break;
  1836. }
  1837. last_byte = min(extent_map_end(em), alloc_end);
  1838. actual_end = min_t(u64, extent_map_end(em), offset + len);
  1839. last_byte = (last_byte + mask) & ~mask;
  1840. if (em->block_start == EXTENT_MAP_HOLE ||
  1841. (cur_offset >= inode->i_size &&
  1842. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1843. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1844. last_byte - cur_offset,
  1845. 1 << inode->i_blkbits,
  1846. offset + len,
  1847. &alloc_hint);
  1848. if (ret < 0) {
  1849. free_extent_map(em);
  1850. break;
  1851. }
  1852. } else if (actual_end > inode->i_size &&
  1853. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1854. /*
  1855. * We didn't need to allocate any more space, but we
  1856. * still extended the size of the file so we need to
  1857. * update i_size.
  1858. */
  1859. inode->i_ctime = CURRENT_TIME;
  1860. i_size_write(inode, actual_end);
  1861. btrfs_ordered_update_i_size(inode, actual_end, NULL);
  1862. }
  1863. free_extent_map(em);
  1864. cur_offset = last_byte;
  1865. if (cur_offset >= alloc_end) {
  1866. ret = 0;
  1867. break;
  1868. }
  1869. }
  1870. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  1871. &cached_state, GFP_NOFS);
  1872. out:
  1873. mutex_unlock(&inode->i_mutex);
  1874. /* Let go of our reservation. */
  1875. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start + 1);
  1876. return ret;
  1877. }
  1878. static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
  1879. {
  1880. struct btrfs_root *root = BTRFS_I(inode)->root;
  1881. struct extent_map *em;
  1882. struct extent_state *cached_state = NULL;
  1883. u64 lockstart = *offset;
  1884. u64 lockend = i_size_read(inode);
  1885. u64 start = *offset;
  1886. u64 orig_start = *offset;
  1887. u64 len = i_size_read(inode);
  1888. u64 last_end = 0;
  1889. int ret = 0;
  1890. lockend = max_t(u64, root->sectorsize, lockend);
  1891. if (lockend <= lockstart)
  1892. lockend = lockstart + root->sectorsize;
  1893. len = lockend - lockstart + 1;
  1894. len = max_t(u64, len, root->sectorsize);
  1895. if (inode->i_size == 0)
  1896. return -ENXIO;
  1897. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
  1898. &cached_state);
  1899. /*
  1900. * Delalloc is such a pain. If we have a hole and we have pending
  1901. * delalloc for a portion of the hole we will get back a hole that
  1902. * exists for the entire range since it hasn't been actually written
  1903. * yet. So to take care of this case we need to look for an extent just
  1904. * before the position we want in case there is outstanding delalloc
  1905. * going on here.
  1906. */
  1907. if (whence == SEEK_HOLE && start != 0) {
  1908. if (start <= root->sectorsize)
  1909. em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
  1910. root->sectorsize, 0);
  1911. else
  1912. em = btrfs_get_extent_fiemap(inode, NULL, 0,
  1913. start - root->sectorsize,
  1914. root->sectorsize, 0);
  1915. if (IS_ERR(em)) {
  1916. ret = PTR_ERR(em);
  1917. goto out;
  1918. }
  1919. last_end = em->start + em->len;
  1920. if (em->block_start == EXTENT_MAP_DELALLOC)
  1921. last_end = min_t(u64, last_end, inode->i_size);
  1922. free_extent_map(em);
  1923. }
  1924. while (1) {
  1925. em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
  1926. if (IS_ERR(em)) {
  1927. ret = PTR_ERR(em);
  1928. break;
  1929. }
  1930. if (em->block_start == EXTENT_MAP_HOLE) {
  1931. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1932. if (last_end <= orig_start) {
  1933. free_extent_map(em);
  1934. ret = -ENXIO;
  1935. break;
  1936. }
  1937. }
  1938. if (whence == SEEK_HOLE) {
  1939. *offset = start;
  1940. free_extent_map(em);
  1941. break;
  1942. }
  1943. } else {
  1944. if (whence == SEEK_DATA) {
  1945. if (em->block_start == EXTENT_MAP_DELALLOC) {
  1946. if (start >= inode->i_size) {
  1947. free_extent_map(em);
  1948. ret = -ENXIO;
  1949. break;
  1950. }
  1951. }
  1952. *offset = start;
  1953. free_extent_map(em);
  1954. break;
  1955. }
  1956. }
  1957. start = em->start + em->len;
  1958. last_end = em->start + em->len;
  1959. if (em->block_start == EXTENT_MAP_DELALLOC)
  1960. last_end = min_t(u64, last_end, inode->i_size);
  1961. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1962. free_extent_map(em);
  1963. ret = -ENXIO;
  1964. break;
  1965. }
  1966. free_extent_map(em);
  1967. cond_resched();
  1968. }
  1969. if (!ret)
  1970. *offset = min(*offset, inode->i_size);
  1971. out:
  1972. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1973. &cached_state, GFP_NOFS);
  1974. return ret;
  1975. }
  1976. static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
  1977. {
  1978. struct inode *inode = file->f_mapping->host;
  1979. int ret;
  1980. mutex_lock(&inode->i_mutex);
  1981. switch (whence) {
  1982. case SEEK_END:
  1983. case SEEK_CUR:
  1984. offset = generic_file_llseek(file, offset, whence);
  1985. goto out;
  1986. case SEEK_DATA:
  1987. case SEEK_HOLE:
  1988. if (offset >= i_size_read(inode)) {
  1989. mutex_unlock(&inode->i_mutex);
  1990. return -ENXIO;
  1991. }
  1992. ret = find_desired_extent(inode, &offset, whence);
  1993. if (ret) {
  1994. mutex_unlock(&inode->i_mutex);
  1995. return ret;
  1996. }
  1997. }
  1998. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
  1999. offset = -EINVAL;
  2000. goto out;
  2001. }
  2002. if (offset > inode->i_sb->s_maxbytes) {
  2003. offset = -EINVAL;
  2004. goto out;
  2005. }
  2006. /* Special lock needed here? */
  2007. if (offset != file->f_pos) {
  2008. file->f_pos = offset;
  2009. file->f_version = 0;
  2010. }
  2011. out:
  2012. mutex_unlock(&inode->i_mutex);
  2013. return offset;
  2014. }
  2015. const struct file_operations btrfs_file_operations = {
  2016. .llseek = btrfs_file_llseek,
  2017. .read = do_sync_read,
  2018. .write = do_sync_write,
  2019. .aio_read = generic_file_aio_read,
  2020. .splice_read = generic_file_splice_read,
  2021. .aio_write = btrfs_file_aio_write,
  2022. .mmap = btrfs_file_mmap,
  2023. .open = generic_file_open,
  2024. .release = btrfs_release_file,
  2025. .fsync = btrfs_sync_file,
  2026. .fallocate = btrfs_fallocate,
  2027. .unlocked_ioctl = btrfs_ioctl,
  2028. #ifdef CONFIG_COMPAT
  2029. .compat_ioctl = btrfs_ioctl,
  2030. #endif
  2031. };