/kern_oII/fs/btrfs/file.c

http://omnia2droid.googlecode.com/ · C · 1232 lines · 988 code · 106 blank · 138 comment · 206 complexity · b696f8550f43542a15b518a4e4efc2f8 MD5 · raw file

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