file.c 46 KB

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