file.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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/swap.h>
  27. #include <linux/writeback.h>
  28. #include <linux/statfs.h>
  29. #include <linux/compat.h>
  30. #include <linux/slab.h>
  31. #include "ctree.h"
  32. #include "disk-io.h"
  33. #include "transaction.h"
  34. #include "btrfs_inode.h"
  35. #include "ioctl.h"
  36. #include "print-tree.h"
  37. #include "tree-log.h"
  38. #include "locking.h"
  39. #include "compat.h"
  40. /* simple helper to fault in pages and copy. This should go away
  41. * and be replaced with calls into generic code.
  42. */
  43. static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
  44. int write_bytes,
  45. struct page **prepared_pages,
  46. struct iov_iter *i)
  47. {
  48. size_t copied = 0;
  49. int pg = 0;
  50. int offset = pos & (PAGE_CACHE_SIZE - 1);
  51. int total_copied = 0;
  52. while (write_bytes > 0) {
  53. size_t count = min_t(size_t,
  54. PAGE_CACHE_SIZE - offset, write_bytes);
  55. struct page *page = prepared_pages[pg];
  56. /*
  57. * Copy data from userspace to the current page
  58. *
  59. * Disable pagefault to avoid recursive lock since
  60. * the pages are already locked
  61. */
  62. pagefault_disable();
  63. copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
  64. pagefault_enable();
  65. /* Flush processor's dcache for this page */
  66. flush_dcache_page(page);
  67. iov_iter_advance(i, copied);
  68. write_bytes -= copied;
  69. total_copied += copied;
  70. /* Return to btrfs_file_aio_write to fault page */
  71. if (unlikely(copied == 0)) {
  72. break;
  73. }
  74. if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
  75. offset += copied;
  76. } else {
  77. pg++;
  78. offset = 0;
  79. }
  80. }
  81. return total_copied;
  82. }
  83. /*
  84. * unlocks pages after btrfs_file_write is done with them
  85. */
  86. static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
  87. {
  88. size_t i;
  89. for (i = 0; i < num_pages; i++) {
  90. if (!pages[i])
  91. break;
  92. /* page checked is some magic around finding pages that
  93. * have been modified without going through btrfs_set_page_dirty
  94. * clear it here
  95. */
  96. ClearPageChecked(pages[i]);
  97. unlock_page(pages[i]);
  98. mark_page_accessed(pages[i]);
  99. page_cache_release(pages[i]);
  100. }
  101. }
  102. /*
  103. * after copy_from_user, pages need to be dirtied and we need to make
  104. * sure holes are created between the current EOF and the start of
  105. * any next extents (if required).
  106. *
  107. * this also makes the decision about creating an inline extent vs
  108. * doing real data extents, marking pages dirty and delalloc as required.
  109. */
  110. static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  111. struct btrfs_root *root,
  112. struct file *file,
  113. struct page **pages,
  114. size_t num_pages,
  115. loff_t pos,
  116. size_t write_bytes)
  117. {
  118. int err = 0;
  119. int i;
  120. struct inode *inode = fdentry(file)->d_inode;
  121. u64 num_bytes;
  122. u64 start_pos;
  123. u64 end_of_last_block;
  124. u64 end_pos = pos + write_bytes;
  125. loff_t isize = i_size_read(inode);
  126. start_pos = pos & ~((u64)root->sectorsize - 1);
  127. num_bytes = (write_bytes + pos - start_pos +
  128. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  129. end_of_last_block = start_pos + num_bytes - 1;
  130. err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
  131. NULL);
  132. BUG_ON(err);
  133. for (i = 0; i < num_pages; i++) {
  134. struct page *p = pages[i];
  135. SetPageUptodate(p);
  136. ClearPageChecked(p);
  137. set_page_dirty(p);
  138. }
  139. if (end_pos > isize) {
  140. i_size_write(inode, end_pos);
  141. /* we've only changed i_size in ram, and we haven't updated
  142. * the disk i_size. There is no need to log the inode
  143. * at this time.
  144. */
  145. }
  146. return 0;
  147. }
  148. /*
  149. * this drops all the extents in the cache that intersect the range
  150. * [start, end]. Existing extents are split as required.
  151. */
  152. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  153. int skip_pinned)
  154. {
  155. struct extent_map *em;
  156. struct extent_map *split = NULL;
  157. struct extent_map *split2 = NULL;
  158. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  159. u64 len = end - start + 1;
  160. int ret;
  161. int testend = 1;
  162. unsigned long flags;
  163. int compressed = 0;
  164. WARN_ON(end < start);
  165. if (end == (u64)-1) {
  166. len = (u64)-1;
  167. testend = 0;
  168. }
  169. while (1) {
  170. if (!split)
  171. split = alloc_extent_map(GFP_NOFS);
  172. if (!split2)
  173. split2 = alloc_extent_map(GFP_NOFS);
  174. write_lock(&em_tree->lock);
  175. em = lookup_extent_mapping(em_tree, start, len);
  176. if (!em) {
  177. write_unlock(&em_tree->lock);
  178. break;
  179. }
  180. flags = em->flags;
  181. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  182. if (testend && em->start + em->len >= start + len) {
  183. free_extent_map(em);
  184. write_unlock(&em_tree->lock);
  185. break;
  186. }
  187. start = em->start + em->len;
  188. if (testend)
  189. len = start + len - (em->start + em->len);
  190. free_extent_map(em);
  191. write_unlock(&em_tree->lock);
  192. continue;
  193. }
  194. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  195. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  196. remove_extent_mapping(em_tree, em);
  197. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  198. em->start < start) {
  199. split->start = em->start;
  200. split->len = start - em->start;
  201. split->orig_start = em->orig_start;
  202. split->block_start = em->block_start;
  203. if (compressed)
  204. split->block_len = em->block_len;
  205. else
  206. split->block_len = split->len;
  207. split->bdev = em->bdev;
  208. split->flags = flags;
  209. split->compress_type = em->compress_type;
  210. ret = add_extent_mapping(em_tree, split);
  211. BUG_ON(ret);
  212. free_extent_map(split);
  213. split = split2;
  214. split2 = NULL;
  215. }
  216. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  217. testend && em->start + em->len > start + len) {
  218. u64 diff = start + len - em->start;
  219. split->start = start + len;
  220. split->len = em->start + em->len - (start + len);
  221. split->bdev = em->bdev;
  222. split->flags = flags;
  223. split->compress_type = em->compress_type;
  224. if (compressed) {
  225. split->block_len = em->block_len;
  226. split->block_start = em->block_start;
  227. split->orig_start = em->orig_start;
  228. } else {
  229. split->block_len = split->len;
  230. split->block_start = em->block_start + diff;
  231. split->orig_start = split->start;
  232. }
  233. ret = add_extent_mapping(em_tree, split);
  234. BUG_ON(ret);
  235. free_extent_map(split);
  236. split = NULL;
  237. }
  238. write_unlock(&em_tree->lock);
  239. /* once for us */
  240. free_extent_map(em);
  241. /* once for the tree*/
  242. free_extent_map(em);
  243. }
  244. if (split)
  245. free_extent_map(split);
  246. if (split2)
  247. free_extent_map(split2);
  248. return 0;
  249. }
  250. /*
  251. * this is very complex, but the basic idea is to drop all extents
  252. * in the range start - end. hint_block is filled in with a block number
  253. * that would be a good hint to the block allocator for this file.
  254. *
  255. * If an extent intersects the range but is not entirely inside the range
  256. * it is either truncated or split. Anything entirely inside the range
  257. * is deleted from the tree.
  258. */
  259. int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
  260. u64 start, u64 end, u64 *hint_byte, int drop_cache)
  261. {
  262. struct btrfs_root *root = BTRFS_I(inode)->root;
  263. struct extent_buffer *leaf;
  264. struct btrfs_file_extent_item *fi;
  265. struct btrfs_path *path;
  266. struct btrfs_key key;
  267. struct btrfs_key new_key;
  268. u64 search_start = start;
  269. u64 disk_bytenr = 0;
  270. u64 num_bytes = 0;
  271. u64 extent_offset = 0;
  272. u64 extent_end = 0;
  273. int del_nr = 0;
  274. int del_slot = 0;
  275. int extent_type;
  276. int recow;
  277. int ret;
  278. if (drop_cache)
  279. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  280. path = btrfs_alloc_path();
  281. if (!path)
  282. return -ENOMEM;
  283. while (1) {
  284. recow = 0;
  285. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  286. search_start, -1);
  287. if (ret < 0)
  288. break;
  289. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  290. leaf = path->nodes[0];
  291. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  292. if (key.objectid == inode->i_ino &&
  293. key.type == BTRFS_EXTENT_DATA_KEY)
  294. path->slots[0]--;
  295. }
  296. ret = 0;
  297. next_slot:
  298. leaf = path->nodes[0];
  299. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  300. BUG_ON(del_nr > 0);
  301. ret = btrfs_next_leaf(root, path);
  302. if (ret < 0)
  303. break;
  304. if (ret > 0) {
  305. ret = 0;
  306. break;
  307. }
  308. leaf = path->nodes[0];
  309. recow = 1;
  310. }
  311. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  312. if (key.objectid > inode->i_ino ||
  313. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  314. break;
  315. fi = btrfs_item_ptr(leaf, path->slots[0],
  316. struct btrfs_file_extent_item);
  317. extent_type = btrfs_file_extent_type(leaf, fi);
  318. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  319. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  320. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  321. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  322. extent_offset = btrfs_file_extent_offset(leaf, fi);
  323. extent_end = key.offset +
  324. btrfs_file_extent_num_bytes(leaf, fi);
  325. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  326. extent_end = key.offset +
  327. btrfs_file_extent_inline_len(leaf, fi);
  328. } else {
  329. WARN_ON(1);
  330. extent_end = search_start;
  331. }
  332. if (extent_end <= search_start) {
  333. path->slots[0]++;
  334. goto next_slot;
  335. }
  336. search_start = max(key.offset, start);
  337. if (recow) {
  338. btrfs_release_path(root, path);
  339. continue;
  340. }
  341. /*
  342. * | - range to drop - |
  343. * | -------- extent -------- |
  344. */
  345. if (start > key.offset && end < extent_end) {
  346. BUG_ON(del_nr > 0);
  347. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  348. memcpy(&new_key, &key, sizeof(new_key));
  349. new_key.offset = start;
  350. ret = btrfs_duplicate_item(trans, root, path,
  351. &new_key);
  352. if (ret == -EAGAIN) {
  353. btrfs_release_path(root, path);
  354. continue;
  355. }
  356. if (ret < 0)
  357. break;
  358. leaf = path->nodes[0];
  359. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  360. struct btrfs_file_extent_item);
  361. btrfs_set_file_extent_num_bytes(leaf, fi,
  362. start - key.offset);
  363. fi = btrfs_item_ptr(leaf, path->slots[0],
  364. struct btrfs_file_extent_item);
  365. extent_offset += start - key.offset;
  366. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  367. btrfs_set_file_extent_num_bytes(leaf, fi,
  368. extent_end - start);
  369. btrfs_mark_buffer_dirty(leaf);
  370. if (disk_bytenr > 0) {
  371. ret = btrfs_inc_extent_ref(trans, root,
  372. disk_bytenr, num_bytes, 0,
  373. root->root_key.objectid,
  374. new_key.objectid,
  375. start - extent_offset);
  376. BUG_ON(ret);
  377. *hint_byte = disk_bytenr;
  378. }
  379. key.offset = start;
  380. }
  381. /*
  382. * | ---- range to drop ----- |
  383. * | -------- extent -------- |
  384. */
  385. if (start <= key.offset && end < extent_end) {
  386. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  387. memcpy(&new_key, &key, sizeof(new_key));
  388. new_key.offset = end;
  389. btrfs_set_item_key_safe(trans, root, path, &new_key);
  390. extent_offset += end - key.offset;
  391. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  392. btrfs_set_file_extent_num_bytes(leaf, fi,
  393. extent_end - end);
  394. btrfs_mark_buffer_dirty(leaf);
  395. if (disk_bytenr > 0) {
  396. inode_sub_bytes(inode, end - key.offset);
  397. *hint_byte = disk_bytenr;
  398. }
  399. break;
  400. }
  401. search_start = extent_end;
  402. /*
  403. * | ---- range to drop ----- |
  404. * | -------- extent -------- |
  405. */
  406. if (start > key.offset && end >= extent_end) {
  407. BUG_ON(del_nr > 0);
  408. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  409. btrfs_set_file_extent_num_bytes(leaf, fi,
  410. start - key.offset);
  411. btrfs_mark_buffer_dirty(leaf);
  412. if (disk_bytenr > 0) {
  413. inode_sub_bytes(inode, extent_end - start);
  414. *hint_byte = disk_bytenr;
  415. }
  416. if (end == extent_end)
  417. break;
  418. path->slots[0]++;
  419. goto next_slot;
  420. }
  421. /*
  422. * | ---- range to drop ----- |
  423. * | ------ extent ------ |
  424. */
  425. if (start <= key.offset && end >= extent_end) {
  426. if (del_nr == 0) {
  427. del_slot = path->slots[0];
  428. del_nr = 1;
  429. } else {
  430. BUG_ON(del_slot + del_nr != path->slots[0]);
  431. del_nr++;
  432. }
  433. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  434. inode_sub_bytes(inode,
  435. extent_end - key.offset);
  436. extent_end = ALIGN(extent_end,
  437. root->sectorsize);
  438. } else if (disk_bytenr > 0) {
  439. ret = btrfs_free_extent(trans, root,
  440. disk_bytenr, num_bytes, 0,
  441. root->root_key.objectid,
  442. key.objectid, key.offset -
  443. extent_offset);
  444. BUG_ON(ret);
  445. inode_sub_bytes(inode,
  446. extent_end - key.offset);
  447. *hint_byte = disk_bytenr;
  448. }
  449. if (end == extent_end)
  450. break;
  451. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  452. path->slots[0]++;
  453. goto next_slot;
  454. }
  455. ret = btrfs_del_items(trans, root, path, del_slot,
  456. del_nr);
  457. BUG_ON(ret);
  458. del_nr = 0;
  459. del_slot = 0;
  460. btrfs_release_path(root, path);
  461. continue;
  462. }
  463. BUG_ON(1);
  464. }
  465. if (del_nr > 0) {
  466. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  467. BUG_ON(ret);
  468. }
  469. btrfs_free_path(path);
  470. return ret;
  471. }
  472. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  473. u64 objectid, u64 bytenr, u64 orig_offset,
  474. u64 *start, u64 *end)
  475. {
  476. struct btrfs_file_extent_item *fi;
  477. struct btrfs_key key;
  478. u64 extent_end;
  479. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  480. return 0;
  481. btrfs_item_key_to_cpu(leaf, &key, slot);
  482. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  483. return 0;
  484. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  485. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  486. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  487. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  488. btrfs_file_extent_compression(leaf, fi) ||
  489. btrfs_file_extent_encryption(leaf, fi) ||
  490. btrfs_file_extent_other_encoding(leaf, fi))
  491. return 0;
  492. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  493. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  494. return 0;
  495. *start = key.offset;
  496. *end = extent_end;
  497. return 1;
  498. }
  499. /*
  500. * Mark extent in the range start - end as written.
  501. *
  502. * This changes extent type from 'pre-allocated' to 'regular'. If only
  503. * part of extent is marked as written, the extent will be split into
  504. * two or three.
  505. */
  506. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  507. struct inode *inode, u64 start, u64 end)
  508. {
  509. struct btrfs_root *root = BTRFS_I(inode)->root;
  510. struct extent_buffer *leaf;
  511. struct btrfs_path *path;
  512. struct btrfs_file_extent_item *fi;
  513. struct btrfs_key key;
  514. struct btrfs_key new_key;
  515. u64 bytenr;
  516. u64 num_bytes;
  517. u64 extent_end;
  518. u64 orig_offset;
  519. u64 other_start;
  520. u64 other_end;
  521. u64 split;
  522. int del_nr = 0;
  523. int del_slot = 0;
  524. int recow;
  525. int ret;
  526. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  527. path = btrfs_alloc_path();
  528. BUG_ON(!path);
  529. again:
  530. recow = 0;
  531. split = start;
  532. key.objectid = inode->i_ino;
  533. key.type = BTRFS_EXTENT_DATA_KEY;
  534. key.offset = split;
  535. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  536. if (ret > 0 && path->slots[0] > 0)
  537. path->slots[0]--;
  538. leaf = path->nodes[0];
  539. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  540. BUG_ON(key.objectid != inode->i_ino ||
  541. key.type != BTRFS_EXTENT_DATA_KEY);
  542. fi = btrfs_item_ptr(leaf, path->slots[0],
  543. struct btrfs_file_extent_item);
  544. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  545. BTRFS_FILE_EXTENT_PREALLOC);
  546. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  547. BUG_ON(key.offset > start || extent_end < end);
  548. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  549. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  550. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  551. memcpy(&new_key, &key, sizeof(new_key));
  552. if (start == key.offset && end < extent_end) {
  553. other_start = 0;
  554. other_end = start;
  555. if (extent_mergeable(leaf, path->slots[0] - 1,
  556. inode->i_ino, bytenr, orig_offset,
  557. &other_start, &other_end)) {
  558. new_key.offset = end;
  559. btrfs_set_item_key_safe(trans, root, path, &new_key);
  560. fi = btrfs_item_ptr(leaf, path->slots[0],
  561. struct btrfs_file_extent_item);
  562. btrfs_set_file_extent_num_bytes(leaf, fi,
  563. extent_end - end);
  564. btrfs_set_file_extent_offset(leaf, fi,
  565. end - orig_offset);
  566. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  567. struct btrfs_file_extent_item);
  568. btrfs_set_file_extent_num_bytes(leaf, fi,
  569. end - other_start);
  570. btrfs_mark_buffer_dirty(leaf);
  571. goto out;
  572. }
  573. }
  574. if (start > key.offset && end == extent_end) {
  575. other_start = end;
  576. other_end = 0;
  577. if (extent_mergeable(leaf, path->slots[0] + 1,
  578. inode->i_ino, bytenr, orig_offset,
  579. &other_start, &other_end)) {
  580. fi = btrfs_item_ptr(leaf, path->slots[0],
  581. struct btrfs_file_extent_item);
  582. btrfs_set_file_extent_num_bytes(leaf, fi,
  583. start - key.offset);
  584. path->slots[0]++;
  585. new_key.offset = start;
  586. btrfs_set_item_key_safe(trans, root, path, &new_key);
  587. fi = btrfs_item_ptr(leaf, path->slots[0],
  588. struct btrfs_file_extent_item);
  589. btrfs_set_file_extent_num_bytes(leaf, fi,
  590. other_end - start);
  591. btrfs_set_file_extent_offset(leaf, fi,
  592. start - orig_offset);
  593. btrfs_mark_buffer_dirty(leaf);
  594. goto out;
  595. }
  596. }
  597. while (start > key.offset || end < extent_end) {
  598. if (key.offset == start)
  599. split = end;
  600. new_key.offset = split;
  601. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  602. if (ret == -EAGAIN) {
  603. btrfs_release_path(root, path);
  604. goto again;
  605. }
  606. BUG_ON(ret < 0);
  607. leaf = path->nodes[0];
  608. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  609. struct btrfs_file_extent_item);
  610. btrfs_set_file_extent_num_bytes(leaf, fi,
  611. split - key.offset);
  612. fi = btrfs_item_ptr(leaf, path->slots[0],
  613. struct btrfs_file_extent_item);
  614. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  615. btrfs_set_file_extent_num_bytes(leaf, fi,
  616. extent_end - split);
  617. btrfs_mark_buffer_dirty(leaf);
  618. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  619. root->root_key.objectid,
  620. inode->i_ino, orig_offset);
  621. BUG_ON(ret);
  622. if (split == start) {
  623. key.offset = start;
  624. } else {
  625. BUG_ON(start != key.offset);
  626. path->slots[0]--;
  627. extent_end = end;
  628. }
  629. recow = 1;
  630. }
  631. other_start = end;
  632. other_end = 0;
  633. if (extent_mergeable(leaf, path->slots[0] + 1,
  634. inode->i_ino, bytenr, orig_offset,
  635. &other_start, &other_end)) {
  636. if (recow) {
  637. btrfs_release_path(root, path);
  638. goto again;
  639. }
  640. extent_end = other_end;
  641. del_slot = path->slots[0] + 1;
  642. del_nr++;
  643. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  644. 0, root->root_key.objectid,
  645. inode->i_ino, orig_offset);
  646. BUG_ON(ret);
  647. }
  648. other_start = 0;
  649. other_end = start;
  650. if (extent_mergeable(leaf, path->slots[0] - 1,
  651. inode->i_ino, bytenr, orig_offset,
  652. &other_start, &other_end)) {
  653. if (recow) {
  654. btrfs_release_path(root, path);
  655. goto again;
  656. }
  657. key.offset = other_start;
  658. del_slot = path->slots[0];
  659. del_nr++;
  660. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  661. 0, root->root_key.objectid,
  662. inode->i_ino, orig_offset);
  663. BUG_ON(ret);
  664. }
  665. if (del_nr == 0) {
  666. fi = btrfs_item_ptr(leaf, path->slots[0],
  667. struct btrfs_file_extent_item);
  668. btrfs_set_file_extent_type(leaf, fi,
  669. BTRFS_FILE_EXTENT_REG);
  670. btrfs_mark_buffer_dirty(leaf);
  671. } else {
  672. fi = btrfs_item_ptr(leaf, del_slot - 1,
  673. struct btrfs_file_extent_item);
  674. btrfs_set_file_extent_type(leaf, fi,
  675. BTRFS_FILE_EXTENT_REG);
  676. btrfs_set_file_extent_num_bytes(leaf, fi,
  677. extent_end - key.offset);
  678. btrfs_mark_buffer_dirty(leaf);
  679. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  680. BUG_ON(ret);
  681. }
  682. out:
  683. btrfs_free_path(path);
  684. return 0;
  685. }
  686. /*
  687. * this gets pages into the page cache and locks them down, it also properly
  688. * waits for data=ordered extents to finish before allowing the pages to be
  689. * modified.
  690. */
  691. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  692. struct page **pages, size_t num_pages,
  693. loff_t pos, unsigned long first_index,
  694. unsigned long last_index, size_t write_bytes)
  695. {
  696. struct extent_state *cached_state = NULL;
  697. int i;
  698. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  699. struct inode *inode = fdentry(file)->d_inode;
  700. int err = 0;
  701. u64 start_pos;
  702. u64 last_pos;
  703. start_pos = pos & ~((u64)root->sectorsize - 1);
  704. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  705. if (start_pos > inode->i_size) {
  706. err = btrfs_cont_expand(inode, start_pos);
  707. if (err)
  708. return err;
  709. }
  710. memset(pages, 0, num_pages * sizeof(struct page *));
  711. again:
  712. for (i = 0; i < num_pages; i++) {
  713. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  714. if (!pages[i]) {
  715. err = -ENOMEM;
  716. BUG_ON(1);
  717. }
  718. wait_on_page_writeback(pages[i]);
  719. }
  720. if (start_pos < inode->i_size) {
  721. struct btrfs_ordered_extent *ordered;
  722. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  723. start_pos, last_pos - 1, 0, &cached_state,
  724. GFP_NOFS);
  725. ordered = btrfs_lookup_first_ordered_extent(inode,
  726. last_pos - 1);
  727. if (ordered &&
  728. ordered->file_offset + ordered->len > start_pos &&
  729. ordered->file_offset < last_pos) {
  730. btrfs_put_ordered_extent(ordered);
  731. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  732. start_pos, last_pos - 1,
  733. &cached_state, GFP_NOFS);
  734. for (i = 0; i < num_pages; i++) {
  735. unlock_page(pages[i]);
  736. page_cache_release(pages[i]);
  737. }
  738. btrfs_wait_ordered_range(inode, start_pos,
  739. last_pos - start_pos);
  740. goto again;
  741. }
  742. if (ordered)
  743. btrfs_put_ordered_extent(ordered);
  744. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  745. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  746. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  747. GFP_NOFS);
  748. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  749. start_pos, last_pos - 1, &cached_state,
  750. GFP_NOFS);
  751. }
  752. for (i = 0; i < num_pages; i++) {
  753. clear_page_dirty_for_io(pages[i]);
  754. set_page_extent_mapped(pages[i]);
  755. WARN_ON(!PageLocked(pages[i]));
  756. }
  757. return 0;
  758. }
  759. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  760. const struct iovec *iov,
  761. unsigned long nr_segs, loff_t pos)
  762. {
  763. struct file *file = iocb->ki_filp;
  764. struct inode *inode = fdentry(file)->d_inode;
  765. struct btrfs_root *root = BTRFS_I(inode)->root;
  766. struct page *pinned[2];
  767. struct page **pages = NULL;
  768. struct iov_iter i;
  769. loff_t *ppos = &iocb->ki_pos;
  770. loff_t start_pos;
  771. ssize_t num_written = 0;
  772. ssize_t err = 0;
  773. size_t count;
  774. size_t ocount;
  775. int ret = 0;
  776. int nrptrs;
  777. unsigned long first_index;
  778. unsigned long last_index;
  779. int will_write;
  780. int buffered = 0;
  781. int copied = 0;
  782. int dirty_pages = 0;
  783. will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
  784. (file->f_flags & O_DIRECT));
  785. pinned[0] = NULL;
  786. pinned[1] = NULL;
  787. start_pos = pos;
  788. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  789. mutex_lock(&inode->i_mutex);
  790. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  791. if (err)
  792. goto out;
  793. count = ocount;
  794. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  795. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  796. if (err)
  797. goto out;
  798. if (count == 0)
  799. goto out;
  800. err = file_remove_suid(file);
  801. if (err)
  802. goto out;
  803. file_update_time(file);
  804. BTRFS_I(inode)->sequence++;
  805. if (unlikely(file->f_flags & O_DIRECT)) {
  806. num_written = generic_file_direct_write(iocb, iov, &nr_segs,
  807. pos, ppos, count,
  808. ocount);
  809. /*
  810. * the generic O_DIRECT will update in-memory i_size after the
  811. * DIOs are done. But our endio handlers that update the on
  812. * disk i_size never update past the in memory i_size. So we
  813. * need one more update here to catch any additions to the
  814. * file
  815. */
  816. if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
  817. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  818. mark_inode_dirty(inode);
  819. }
  820. if (num_written < 0) {
  821. ret = num_written;
  822. num_written = 0;
  823. goto out;
  824. } else if (num_written == count) {
  825. /* pick up pos changes done by the generic code */
  826. pos = *ppos;
  827. goto out;
  828. }
  829. /*
  830. * We are going to do buffered for the rest of the range, so we
  831. * need to make sure to invalidate the buffered pages when we're
  832. * done.
  833. */
  834. buffered = 1;
  835. pos += num_written;
  836. }
  837. iov_iter_init(&i, iov, nr_segs, count, num_written);
  838. nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
  839. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  840. (sizeof(struct page *)));
  841. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  842. /* generic_write_checks can change our pos */
  843. start_pos = pos;
  844. first_index = pos >> PAGE_CACHE_SHIFT;
  845. last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
  846. /*
  847. * there are lots of better ways to do this, but this code
  848. * makes sure the first and last page in the file range are
  849. * up to date and ready for cow
  850. */
  851. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  852. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  853. if (!PageUptodate(pinned[0])) {
  854. ret = btrfs_readpage(NULL, pinned[0]);
  855. BUG_ON(ret);
  856. wait_on_page_locked(pinned[0]);
  857. } else {
  858. unlock_page(pinned[0]);
  859. }
  860. }
  861. if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
  862. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  863. if (!PageUptodate(pinned[1])) {
  864. ret = btrfs_readpage(NULL, pinned[1]);
  865. BUG_ON(ret);
  866. wait_on_page_locked(pinned[1]);
  867. } else {
  868. unlock_page(pinned[1]);
  869. }
  870. }
  871. while (iov_iter_count(&i) > 0) {
  872. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  873. size_t write_bytes = min(iov_iter_count(&i),
  874. nrptrs * (size_t)PAGE_CACHE_SIZE -
  875. offset);
  876. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  877. PAGE_CACHE_SHIFT;
  878. WARN_ON(num_pages > nrptrs);
  879. memset(pages, 0, sizeof(struct page *) * nrptrs);
  880. /*
  881. * Fault pages before locking them in prepare_pages
  882. * to avoid recursive lock
  883. */
  884. if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
  885. ret = -EFAULT;
  886. goto out;
  887. }
  888. ret = btrfs_delalloc_reserve_space(inode,
  889. num_pages << PAGE_CACHE_SHIFT);
  890. if (ret)
  891. goto out;
  892. ret = prepare_pages(root, file, pages, num_pages,
  893. pos, first_index, last_index,
  894. write_bytes);
  895. if (ret) {
  896. btrfs_delalloc_release_space(inode,
  897. num_pages << PAGE_CACHE_SHIFT);
  898. goto out;
  899. }
  900. copied = btrfs_copy_from_user(pos, num_pages,
  901. write_bytes, pages, &i);
  902. dirty_pages = (copied + PAGE_CACHE_SIZE - 1) >>
  903. PAGE_CACHE_SHIFT;
  904. if (num_pages > dirty_pages) {
  905. if (copied > 0)
  906. atomic_inc(
  907. &BTRFS_I(inode)->outstanding_extents);
  908. btrfs_delalloc_release_space(inode,
  909. (num_pages - dirty_pages) <<
  910. PAGE_CACHE_SHIFT);
  911. }
  912. if (copied > 0) {
  913. dirty_and_release_pages(NULL, root, file, pages,
  914. dirty_pages, pos, copied);
  915. }
  916. btrfs_drop_pages(pages, num_pages);
  917. if (copied > 0) {
  918. if (will_write) {
  919. filemap_fdatawrite_range(inode->i_mapping, pos,
  920. pos + copied - 1);
  921. } else {
  922. balance_dirty_pages_ratelimited_nr(
  923. inode->i_mapping,
  924. dirty_pages);
  925. if (dirty_pages <
  926. (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  927. btrfs_btree_balance_dirty(root, 1);
  928. btrfs_throttle(root);
  929. }
  930. }
  931. pos += copied;
  932. num_written += copied;
  933. cond_resched();
  934. }
  935. out:
  936. mutex_unlock(&inode->i_mutex);
  937. if (ret)
  938. err = ret;
  939. kfree(pages);
  940. if (pinned[0])
  941. page_cache_release(pinned[0]);
  942. if (pinned[1])
  943. page_cache_release(pinned[1]);
  944. *ppos = pos;
  945. /*
  946. * we want to make sure fsync finds this change
  947. * but we haven't joined a transaction running right now.
  948. *
  949. * Later on, someone is sure to update the inode and get the
  950. * real transid recorded.
  951. *
  952. * We set last_trans now to the fs_info generation + 1,
  953. * this will either be one more than the running transaction
  954. * or the generation used for the next transaction if there isn't
  955. * one running right now.
  956. */
  957. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  958. if (num_written > 0 && will_write) {
  959. struct btrfs_trans_handle *trans;
  960. err = btrfs_wait_ordered_range(inode, start_pos, num_written);
  961. if (err)
  962. num_written = err;
  963. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  964. trans = btrfs_start_transaction(root, 0);
  965. if (IS_ERR(trans)) {
  966. num_written = PTR_ERR(trans);
  967. goto done;
  968. }
  969. mutex_lock(&inode->i_mutex);
  970. ret = btrfs_log_dentry_safe(trans, root,
  971. file->f_dentry);
  972. mutex_unlock(&inode->i_mutex);
  973. if (ret == 0) {
  974. ret = btrfs_sync_log(trans, root);
  975. if (ret == 0)
  976. btrfs_end_transaction(trans, root);
  977. else
  978. btrfs_commit_transaction(trans, root);
  979. } else if (ret != BTRFS_NO_LOG_SYNC) {
  980. btrfs_commit_transaction(trans, root);
  981. } else {
  982. btrfs_end_transaction(trans, root);
  983. }
  984. }
  985. if (file->f_flags & O_DIRECT && buffered) {
  986. invalidate_mapping_pages(inode->i_mapping,
  987. start_pos >> PAGE_CACHE_SHIFT,
  988. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  989. }
  990. }
  991. done:
  992. current->backing_dev_info = NULL;
  993. return num_written ? num_written : err;
  994. }
  995. int btrfs_release_file(struct inode *inode, struct file *filp)
  996. {
  997. /*
  998. * ordered_data_close is set by settattr when we are about to truncate
  999. * a file from a non-zero size to a zero size. This tries to
  1000. * flush down new bytes that may have been written if the
  1001. * application were using truncate to replace a file in place.
  1002. */
  1003. if (BTRFS_I(inode)->ordered_data_close) {
  1004. BTRFS_I(inode)->ordered_data_close = 0;
  1005. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1006. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1007. filemap_flush(inode->i_mapping);
  1008. }
  1009. if (filp->private_data)
  1010. btrfs_ioctl_trans_end(filp);
  1011. return 0;
  1012. }
  1013. /*
  1014. * fsync call for both files and directories. This logs the inode into
  1015. * the tree log instead of forcing full commits whenever possible.
  1016. *
  1017. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1018. * in the metadata btree are up to date for copying to the log.
  1019. *
  1020. * It drops the inode mutex before doing the tree log commit. This is an
  1021. * important optimization for directories because holding the mutex prevents
  1022. * new operations on the dir while we write to disk.
  1023. */
  1024. int btrfs_sync_file(struct file *file, int datasync)
  1025. {
  1026. struct dentry *dentry = file->f_path.dentry;
  1027. struct inode *inode = dentry->d_inode;
  1028. struct btrfs_root *root = BTRFS_I(inode)->root;
  1029. int ret = 0;
  1030. struct btrfs_trans_handle *trans;
  1031. /* we wait first, since the writeback may change the inode */
  1032. root->log_batch++;
  1033. /* the VFS called filemap_fdatawrite for us */
  1034. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  1035. root->log_batch++;
  1036. /*
  1037. * check the transaction that last modified this inode
  1038. * and see if its already been committed
  1039. */
  1040. if (!BTRFS_I(inode)->last_trans)
  1041. goto out;
  1042. /*
  1043. * if the last transaction that changed this file was before
  1044. * the current transaction, we can bail out now without any
  1045. * syncing
  1046. */
  1047. mutex_lock(&root->fs_info->trans_mutex);
  1048. if (BTRFS_I(inode)->last_trans <=
  1049. root->fs_info->last_trans_committed) {
  1050. BTRFS_I(inode)->last_trans = 0;
  1051. mutex_unlock(&root->fs_info->trans_mutex);
  1052. goto out;
  1053. }
  1054. mutex_unlock(&root->fs_info->trans_mutex);
  1055. /*
  1056. * ok we haven't committed the transaction yet, lets do a commit
  1057. */
  1058. if (file->private_data)
  1059. btrfs_ioctl_trans_end(file);
  1060. trans = btrfs_start_transaction(root, 0);
  1061. if (IS_ERR(trans)) {
  1062. ret = PTR_ERR(trans);
  1063. goto out;
  1064. }
  1065. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1066. if (ret < 0)
  1067. goto out;
  1068. /* we've logged all the items and now have a consistent
  1069. * version of the file in the log. It is possible that
  1070. * someone will come in and modify the file, but that's
  1071. * fine because the log is consistent on disk, and we
  1072. * have references to all of the file's extents
  1073. *
  1074. * It is possible that someone will come in and log the
  1075. * file again, but that will end up using the synchronization
  1076. * inside btrfs_sync_log to keep things safe.
  1077. */
  1078. mutex_unlock(&dentry->d_inode->i_mutex);
  1079. if (ret != BTRFS_NO_LOG_SYNC) {
  1080. if (ret > 0) {
  1081. ret = btrfs_commit_transaction(trans, root);
  1082. } else {
  1083. ret = btrfs_sync_log(trans, root);
  1084. if (ret == 0)
  1085. ret = btrfs_end_transaction(trans, root);
  1086. else
  1087. ret = btrfs_commit_transaction(trans, root);
  1088. }
  1089. } else {
  1090. ret = btrfs_end_transaction(trans, root);
  1091. }
  1092. mutex_lock(&dentry->d_inode->i_mutex);
  1093. out:
  1094. return ret > 0 ? -EIO : ret;
  1095. }
  1096. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1097. .fault = filemap_fault,
  1098. .page_mkwrite = btrfs_page_mkwrite,
  1099. };
  1100. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1101. {
  1102. struct address_space *mapping = filp->f_mapping;
  1103. if (!mapping->a_ops->readpage)
  1104. return -ENOEXEC;
  1105. file_accessed(filp);
  1106. vma->vm_ops = &btrfs_file_vm_ops;
  1107. vma->vm_flags |= VM_CAN_NONLINEAR;
  1108. return 0;
  1109. }
  1110. const struct file_operations btrfs_file_operations = {
  1111. .llseek = generic_file_llseek,
  1112. .read = do_sync_read,
  1113. .write = do_sync_write,
  1114. .aio_read = generic_file_aio_read,
  1115. .splice_read = generic_file_splice_read,
  1116. .aio_write = btrfs_file_aio_write,
  1117. .mmap = btrfs_file_mmap,
  1118. .open = generic_file_open,
  1119. .release = btrfs_release_file,
  1120. .fsync = btrfs_sync_file,
  1121. .unlocked_ioctl = btrfs_ioctl,
  1122. #ifdef CONFIG_COMPAT
  1123. .compat_ioctl = btrfs_ioctl,
  1124. #endif
  1125. };