file.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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/smp_lock.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mpage.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/version.h>
  32. #include "ctree.h"
  33. #include "disk-io.h"
  34. #include "transaction.h"
  35. #include "btrfs_inode.h"
  36. #include "ordered-data.h"
  37. #include "ioctl.h"
  38. #include "print-tree.h"
  39. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  40. struct page **prepared_pages,
  41. const char __user * buf)
  42. {
  43. long page_fault = 0;
  44. int i;
  45. int offset = pos & (PAGE_CACHE_SIZE - 1);
  46. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  47. size_t count = min_t(size_t,
  48. PAGE_CACHE_SIZE - offset, write_bytes);
  49. struct page *page = prepared_pages[i];
  50. fault_in_pages_readable(buf, count);
  51. /* Copy data from userspace to the current page */
  52. kmap(page);
  53. page_fault = __copy_from_user(page_address(page) + offset,
  54. buf, count);
  55. /* Flush processor's dcache for this page */
  56. flush_dcache_page(page);
  57. kunmap(page);
  58. buf += count;
  59. write_bytes -= count;
  60. if (page_fault)
  61. break;
  62. }
  63. return page_fault ? -EFAULT : 0;
  64. }
  65. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  66. {
  67. size_t i;
  68. for (i = 0; i < num_pages; i++) {
  69. if (!pages[i])
  70. break;
  71. unlock_page(pages[i]);
  72. mark_page_accessed(pages[i]);
  73. page_cache_release(pages[i]);
  74. }
  75. }
  76. static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
  77. struct btrfs_root *root, struct inode *inode,
  78. u64 offset, size_t size,
  79. struct page **pages, size_t page_offset,
  80. int num_pages)
  81. {
  82. struct btrfs_key key;
  83. struct btrfs_path *path;
  84. struct extent_buffer *leaf;
  85. char *kaddr;
  86. unsigned long ptr;
  87. struct btrfs_file_extent_item *ei;
  88. struct page *page;
  89. u32 datasize;
  90. int err = 0;
  91. int ret;
  92. int i;
  93. ssize_t cur_size;
  94. path = btrfs_alloc_path();
  95. if (!path)
  96. return -ENOMEM;
  97. btrfs_set_trans_block_group(trans, inode);
  98. key.objectid = inode->i_ino;
  99. key.offset = offset;
  100. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  101. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  102. if (ret < 0) {
  103. err = ret;
  104. goto fail;
  105. }
  106. if (ret == 1) {
  107. struct btrfs_key found_key;
  108. if (path->slots[0] == 0)
  109. goto insert;
  110. path->slots[0]--;
  111. leaf = path->nodes[0];
  112. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  113. if (found_key.objectid != inode->i_ino)
  114. goto insert;
  115. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  116. goto insert;
  117. ei = btrfs_item_ptr(leaf, path->slots[0],
  118. struct btrfs_file_extent_item);
  119. if (btrfs_file_extent_type(leaf, ei) !=
  120. BTRFS_FILE_EXTENT_INLINE) {
  121. goto insert;
  122. }
  123. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  124. ret = 0;
  125. }
  126. if (ret == 0) {
  127. u32 found_size;
  128. u64 found_end;
  129. leaf = path->nodes[0];
  130. ei = btrfs_item_ptr(leaf, path->slots[0],
  131. struct btrfs_file_extent_item);
  132. if (btrfs_file_extent_type(leaf, ei) !=
  133. BTRFS_FILE_EXTENT_INLINE) {
  134. err = ret;
  135. btrfs_print_leaf(root, leaf);
  136. printk("found wasn't inline offset %Lu inode %lu\n",
  137. offset, inode->i_ino);
  138. goto fail;
  139. }
  140. found_size = btrfs_file_extent_inline_len(leaf,
  141. btrfs_item_nr(leaf, path->slots[0]));
  142. found_end = key.offset + found_size;
  143. if (found_end < offset + size) {
  144. btrfs_release_path(root, path);
  145. ret = btrfs_search_slot(trans, root, &key, path,
  146. offset + size - found_end, 1);
  147. BUG_ON(ret != 0);
  148. ret = btrfs_extend_item(trans, root, path,
  149. offset + size - found_end);
  150. if (ret) {
  151. err = ret;
  152. goto fail;
  153. }
  154. leaf = path->nodes[0];
  155. ei = btrfs_item_ptr(leaf, path->slots[0],
  156. struct btrfs_file_extent_item);
  157. inode->i_blocks += (offset + size - found_end) >> 9;
  158. }
  159. if (found_end < offset) {
  160. ptr = btrfs_file_extent_inline_start(ei) + found_size;
  161. memset_extent_buffer(leaf, 0, ptr, offset - found_end);
  162. }
  163. } else {
  164. insert:
  165. btrfs_release_path(root, path);
  166. datasize = offset + size - key.offset;
  167. inode->i_blocks += datasize >> 9;
  168. datasize = btrfs_file_extent_calc_inline_size(datasize);
  169. ret = btrfs_insert_empty_item(trans, root, path, &key,
  170. datasize);
  171. if (ret) {
  172. err = ret;
  173. printk("got bad ret %d\n", ret);
  174. goto fail;
  175. }
  176. leaf = path->nodes[0];
  177. ei = btrfs_item_ptr(leaf, path->slots[0],
  178. struct btrfs_file_extent_item);
  179. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  180. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  181. }
  182. ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
  183. cur_size = size;
  184. i = 0;
  185. while (size > 0) {
  186. page = pages[i];
  187. kaddr = kmap_atomic(page, KM_USER0);
  188. cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
  189. write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
  190. kunmap_atomic(kaddr, KM_USER0);
  191. page_offset = 0;
  192. ptr += cur_size;
  193. size -= cur_size;
  194. if (i >= num_pages) {
  195. printk("i %d num_pages %d\n", i, num_pages);
  196. }
  197. i++;
  198. }
  199. btrfs_mark_buffer_dirty(leaf);
  200. fail:
  201. btrfs_free_path(path);
  202. return err;
  203. }
  204. static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
  205. struct btrfs_root *root,
  206. struct file *file,
  207. struct page **pages,
  208. size_t num_pages,
  209. loff_t pos,
  210. size_t write_bytes)
  211. {
  212. int err = 0;
  213. int i;
  214. struct inode *inode = fdentry(file)->d_inode;
  215. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  216. u64 hint_byte;
  217. u64 num_bytes;
  218. u64 start_pos;
  219. u64 end_of_last_block;
  220. u64 end_pos = pos + write_bytes;
  221. u64 inline_size;
  222. loff_t isize = i_size_read(inode);
  223. start_pos = pos & ~((u64)root->sectorsize - 1);
  224. num_bytes = (write_bytes + pos - start_pos +
  225. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  226. end_of_last_block = start_pos + num_bytes - 1;
  227. lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  228. mutex_lock(&root->fs_info->fs_mutex);
  229. trans = btrfs_start_transaction(root, 1);
  230. if (!trans) {
  231. err = -ENOMEM;
  232. goto out_unlock;
  233. }
  234. btrfs_set_trans_block_group(trans, inode);
  235. hint_byte = 0;
  236. if ((end_of_last_block & 4095) == 0) {
  237. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  238. }
  239. set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  240. /* FIXME...EIEIO, ENOSPC and more */
  241. /* insert any holes we need to create */
  242. if (isize < end_pos) {
  243. u64 last_pos_in_file;
  244. u64 hole_size;
  245. u64 mask = root->sectorsize - 1;
  246. last_pos_in_file = (isize + mask) & ~mask;
  247. hole_size = (end_pos - last_pos_in_file + mask) & ~mask;
  248. if (last_pos_in_file < end_pos) {
  249. err = btrfs_drop_extents(trans, root, inode,
  250. last_pos_in_file,
  251. last_pos_in_file + hole_size,
  252. last_pos_in_file,
  253. &hint_byte);
  254. if (err)
  255. goto failed;
  256. err = btrfs_insert_file_extent(trans, root,
  257. inode->i_ino,
  258. last_pos_in_file,
  259. 0, 0, hole_size, 0);
  260. btrfs_drop_extent_cache(inode, last_pos_in_file,
  261. last_pos_in_file + hole_size -1);
  262. btrfs_check_file(root, inode);
  263. }
  264. if (err)
  265. goto failed;
  266. }
  267. /*
  268. * either allocate an extent for the new bytes or setup the key
  269. * to show we are doing inline data in the extent
  270. */
  271. inline_size = end_pos;
  272. if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  273. inline_size > root->fs_info->max_inline ||
  274. (inline_size & (root->sectorsize -1)) == 0 ||
  275. inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  276. u64 last_end;
  277. u64 existing_delalloc = 0;
  278. for (i = 0; i < num_pages; i++) {
  279. struct page *p = pages[i];
  280. SetPageUptodate(p);
  281. set_page_dirty(p);
  282. }
  283. last_end = (u64)(pages[num_pages -1]->index) <<
  284. PAGE_CACHE_SHIFT;
  285. last_end += PAGE_CACHE_SIZE - 1;
  286. if (start_pos < isize) {
  287. u64 delalloc_start = start_pos;
  288. existing_delalloc = count_range_bits(io_tree,
  289. &delalloc_start,
  290. end_of_last_block, (u64)-1,
  291. EXTENT_DELALLOC);
  292. }
  293. set_extent_delalloc(io_tree, start_pos, end_of_last_block,
  294. GFP_NOFS);
  295. btrfs_add_ordered_inode(inode);
  296. } else {
  297. u64 aligned_end;
  298. /* step one, delete the existing extents in this range */
  299. aligned_end = (pos + write_bytes + root->sectorsize - 1) &
  300. ~((u64)root->sectorsize - 1);
  301. err = btrfs_drop_extents(trans, root, inode, start_pos,
  302. aligned_end, aligned_end, &hint_byte);
  303. if (err)
  304. goto failed;
  305. if (isize > inline_size)
  306. inline_size = min_t(u64, isize, aligned_end);
  307. inline_size -= start_pos;
  308. err = insert_inline_extent(trans, root, inode, start_pos,
  309. inline_size, pages, 0, num_pages);
  310. btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1);
  311. BUG_ON(err);
  312. }
  313. if (end_pos > isize) {
  314. i_size_write(inode, end_pos);
  315. btrfs_update_inode(trans, root, inode);
  316. }
  317. failed:
  318. err = btrfs_end_transaction(trans, root);
  319. out_unlock:
  320. mutex_unlock(&root->fs_info->fs_mutex);
  321. unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  322. return err;
  323. }
  324. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
  325. {
  326. struct extent_map *em;
  327. struct extent_map *split = NULL;
  328. struct extent_map *split2 = NULL;
  329. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  330. u64 len = end - start + 1;
  331. int ret;
  332. int testend = 1;
  333. if (end == (u64)-1) {
  334. len = (u64)-1;
  335. testend = 0;
  336. }
  337. while(1) {
  338. if (!split)
  339. split = alloc_extent_map(GFP_NOFS);
  340. if (!split2)
  341. split2 = alloc_extent_map(GFP_NOFS);
  342. spin_lock(&em_tree->lock);
  343. em = lookup_extent_mapping(em_tree, start, len);
  344. if (!em) {
  345. spin_unlock(&em_tree->lock);
  346. break;
  347. }
  348. remove_extent_mapping(em_tree, em);
  349. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  350. em->start < start) {
  351. split->start = em->start;
  352. split->len = start - em->start;
  353. split->block_start = em->block_start;
  354. split->bdev = em->bdev;
  355. split->flags = em->flags;
  356. ret = add_extent_mapping(em_tree, split);
  357. BUG_ON(ret);
  358. free_extent_map(split);
  359. split = split2;
  360. split2 = NULL;
  361. }
  362. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  363. testend && em->start + em->len > start + len) {
  364. u64 diff = start + len - em->start;
  365. split->start = start + len;
  366. split->len = em->start + em->len - (start + len);
  367. split->bdev = em->bdev;
  368. split->flags = em->flags;
  369. split->block_start = em->block_start + diff;
  370. ret = add_extent_mapping(em_tree, split);
  371. BUG_ON(ret);
  372. free_extent_map(split);
  373. split = NULL;
  374. }
  375. spin_unlock(&em_tree->lock);
  376. /* once for us */
  377. free_extent_map(em);
  378. /* once for the tree*/
  379. free_extent_map(em);
  380. }
  381. if (split)
  382. free_extent_map(split);
  383. if (split2)
  384. free_extent_map(split2);
  385. return 0;
  386. }
  387. int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
  388. {
  389. return 0;
  390. #if 0
  391. struct btrfs_path *path;
  392. struct btrfs_key found_key;
  393. struct extent_buffer *leaf;
  394. struct btrfs_file_extent_item *extent;
  395. u64 last_offset = 0;
  396. int nritems;
  397. int slot;
  398. int found_type;
  399. int ret;
  400. int err = 0;
  401. u64 extent_end = 0;
  402. path = btrfs_alloc_path();
  403. ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
  404. last_offset, 0);
  405. while(1) {
  406. nritems = btrfs_header_nritems(path->nodes[0]);
  407. if (path->slots[0] >= nritems) {
  408. ret = btrfs_next_leaf(root, path);
  409. if (ret)
  410. goto out;
  411. nritems = btrfs_header_nritems(path->nodes[0]);
  412. }
  413. slot = path->slots[0];
  414. leaf = path->nodes[0];
  415. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  416. if (found_key.objectid != inode->i_ino)
  417. break;
  418. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  419. goto out;
  420. if (found_key.offset < last_offset) {
  421. WARN_ON(1);
  422. btrfs_print_leaf(root, leaf);
  423. printk("inode %lu found offset %Lu expected %Lu\n",
  424. inode->i_ino, found_key.offset, last_offset);
  425. err = 1;
  426. goto out;
  427. }
  428. extent = btrfs_item_ptr(leaf, slot,
  429. struct btrfs_file_extent_item);
  430. found_type = btrfs_file_extent_type(leaf, extent);
  431. if (found_type == BTRFS_FILE_EXTENT_REG) {
  432. extent_end = found_key.offset +
  433. btrfs_file_extent_num_bytes(leaf, extent);
  434. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  435. struct btrfs_item *item;
  436. item = btrfs_item_nr(leaf, slot);
  437. extent_end = found_key.offset +
  438. btrfs_file_extent_inline_len(leaf, item);
  439. extent_end = (extent_end + root->sectorsize - 1) &
  440. ~((u64)root->sectorsize -1 );
  441. }
  442. last_offset = extent_end;
  443. path->slots[0]++;
  444. }
  445. if (0 && last_offset < inode->i_size) {
  446. WARN_ON(1);
  447. btrfs_print_leaf(root, leaf);
  448. printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
  449. last_offset, inode->i_size);
  450. err = 1;
  451. }
  452. out:
  453. btrfs_free_path(path);
  454. return err;
  455. #endif
  456. }
  457. /*
  458. * this is very complex, but the basic idea is to drop all extents
  459. * in the range start - end. hint_block is filled in with a block number
  460. * that would be a good hint to the block allocator for this file.
  461. *
  462. * If an extent intersects the range but is not entirely inside the range
  463. * it is either truncated or split. Anything entirely inside the range
  464. * is deleted from the tree.
  465. */
  466. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  467. struct btrfs_root *root, struct inode *inode,
  468. u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
  469. {
  470. u64 extent_end = 0;
  471. u64 search_start = start;
  472. struct extent_buffer *leaf;
  473. struct btrfs_file_extent_item *extent;
  474. struct btrfs_path *path;
  475. struct btrfs_key key;
  476. struct btrfs_file_extent_item old;
  477. int keep;
  478. int slot;
  479. int bookend;
  480. int found_type;
  481. int found_extent;
  482. int found_inline;
  483. int recow;
  484. int ret;
  485. btrfs_drop_extent_cache(inode, start, end - 1);
  486. path = btrfs_alloc_path();
  487. if (!path)
  488. return -ENOMEM;
  489. while(1) {
  490. recow = 0;
  491. btrfs_release_path(root, path);
  492. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  493. search_start, -1);
  494. if (ret < 0)
  495. goto out;
  496. if (ret > 0) {
  497. if (path->slots[0] == 0) {
  498. ret = 0;
  499. goto out;
  500. }
  501. path->slots[0]--;
  502. }
  503. next_slot:
  504. keep = 0;
  505. bookend = 0;
  506. found_extent = 0;
  507. found_inline = 0;
  508. extent = NULL;
  509. leaf = path->nodes[0];
  510. slot = path->slots[0];
  511. ret = 0;
  512. btrfs_item_key_to_cpu(leaf, &key, slot);
  513. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
  514. key.offset >= end) {
  515. goto out;
  516. }
  517. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  518. key.objectid != inode->i_ino) {
  519. goto out;
  520. }
  521. if (recow) {
  522. search_start = key.offset;
  523. continue;
  524. }
  525. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  526. extent = btrfs_item_ptr(leaf, slot,
  527. struct btrfs_file_extent_item);
  528. found_type = btrfs_file_extent_type(leaf, extent);
  529. if (found_type == BTRFS_FILE_EXTENT_REG) {
  530. extent_end =
  531. btrfs_file_extent_disk_bytenr(leaf,
  532. extent);
  533. if (extent_end)
  534. *hint_byte = extent_end;
  535. extent_end = key.offset +
  536. btrfs_file_extent_num_bytes(leaf, extent);
  537. found_extent = 1;
  538. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  539. struct btrfs_item *item;
  540. item = btrfs_item_nr(leaf, slot);
  541. found_inline = 1;
  542. extent_end = key.offset +
  543. btrfs_file_extent_inline_len(leaf, item);
  544. }
  545. } else {
  546. extent_end = search_start;
  547. }
  548. /* we found nothing we can drop */
  549. if ((!found_extent && !found_inline) ||
  550. search_start >= extent_end) {
  551. int nextret;
  552. u32 nritems;
  553. nritems = btrfs_header_nritems(leaf);
  554. if (slot >= nritems - 1) {
  555. nextret = btrfs_next_leaf(root, path);
  556. if (nextret)
  557. goto out;
  558. recow = 1;
  559. } else {
  560. path->slots[0]++;
  561. }
  562. goto next_slot;
  563. }
  564. if (found_inline) {
  565. u64 mask = root->sectorsize - 1;
  566. search_start = (extent_end + mask) & ~mask;
  567. } else
  568. search_start = extent_end;
  569. if (end <= extent_end && start >= key.offset && found_inline) {
  570. *hint_byte = EXTENT_MAP_INLINE;
  571. continue;
  572. }
  573. if (end < extent_end && end >= key.offset) {
  574. if (found_extent) {
  575. u64 disk_bytenr =
  576. btrfs_file_extent_disk_bytenr(leaf, extent);
  577. u64 disk_num_bytes =
  578. btrfs_file_extent_disk_num_bytes(leaf,
  579. extent);
  580. read_extent_buffer(leaf, &old,
  581. (unsigned long)extent,
  582. sizeof(old));
  583. if (disk_bytenr != 0) {
  584. ret = btrfs_inc_extent_ref(trans, root,
  585. disk_bytenr, disk_num_bytes,
  586. root->root_key.objectid,
  587. trans->transid,
  588. key.objectid, end);
  589. BUG_ON(ret);
  590. }
  591. }
  592. bookend = 1;
  593. if (found_inline && start <= key.offset)
  594. keep = 1;
  595. }
  596. /* truncate existing extent */
  597. if (start > key.offset) {
  598. u64 new_num;
  599. u64 old_num;
  600. keep = 1;
  601. WARN_ON(start & (root->sectorsize - 1));
  602. if (found_extent) {
  603. new_num = start - key.offset;
  604. old_num = btrfs_file_extent_num_bytes(leaf,
  605. extent);
  606. *hint_byte =
  607. btrfs_file_extent_disk_bytenr(leaf,
  608. extent);
  609. if (btrfs_file_extent_disk_bytenr(leaf,
  610. extent)) {
  611. dec_i_blocks(inode, old_num - new_num);
  612. }
  613. btrfs_set_file_extent_num_bytes(leaf, extent,
  614. new_num);
  615. btrfs_mark_buffer_dirty(leaf);
  616. } else if (key.offset < inline_limit &&
  617. (end > extent_end) &&
  618. (inline_limit < extent_end)) {
  619. u32 new_size;
  620. new_size = btrfs_file_extent_calc_inline_size(
  621. inline_limit - key.offset);
  622. dec_i_blocks(inode, (extent_end - key.offset) -
  623. (inline_limit - key.offset));
  624. btrfs_truncate_item(trans, root, path,
  625. new_size, 1);
  626. }
  627. }
  628. /* delete the entire extent */
  629. if (!keep) {
  630. u64 disk_bytenr = 0;
  631. u64 disk_num_bytes = 0;
  632. u64 extent_num_bytes = 0;
  633. u64 root_gen;
  634. u64 root_owner;
  635. root_gen = btrfs_header_generation(leaf);
  636. root_owner = btrfs_header_owner(leaf);
  637. if (found_extent) {
  638. disk_bytenr =
  639. btrfs_file_extent_disk_bytenr(leaf,
  640. extent);
  641. disk_num_bytes =
  642. btrfs_file_extent_disk_num_bytes(leaf,
  643. extent);
  644. extent_num_bytes =
  645. btrfs_file_extent_num_bytes(leaf, extent);
  646. *hint_byte =
  647. btrfs_file_extent_disk_bytenr(leaf,
  648. extent);
  649. }
  650. ret = btrfs_del_item(trans, root, path);
  651. /* TODO update progress marker and return */
  652. BUG_ON(ret);
  653. btrfs_release_path(root, path);
  654. extent = NULL;
  655. if (found_extent && disk_bytenr != 0) {
  656. dec_i_blocks(inode, extent_num_bytes);
  657. ret = btrfs_free_extent(trans, root,
  658. disk_bytenr,
  659. disk_num_bytes,
  660. root_owner,
  661. root_gen, inode->i_ino,
  662. key.offset, 0);
  663. }
  664. BUG_ON(ret);
  665. if (!bookend && search_start >= end) {
  666. ret = 0;
  667. goto out;
  668. }
  669. if (!bookend)
  670. continue;
  671. }
  672. if (bookend && found_inline && start <= key.offset) {
  673. u32 new_size;
  674. new_size = btrfs_file_extent_calc_inline_size(
  675. extent_end - end);
  676. dec_i_blocks(inode, (extent_end - key.offset) -
  677. (extent_end - end));
  678. btrfs_truncate_item(trans, root, path, new_size, 0);
  679. }
  680. /* create bookend, splitting the extent in two */
  681. if (bookend && found_extent) {
  682. struct btrfs_key ins;
  683. ins.objectid = inode->i_ino;
  684. ins.offset = end;
  685. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  686. btrfs_release_path(root, path);
  687. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  688. sizeof(*extent));
  689. leaf = path->nodes[0];
  690. if (ret) {
  691. btrfs_print_leaf(root, leaf);
  692. printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep);
  693. }
  694. BUG_ON(ret);
  695. extent = btrfs_item_ptr(leaf, path->slots[0],
  696. struct btrfs_file_extent_item);
  697. write_extent_buffer(leaf, &old,
  698. (unsigned long)extent, sizeof(old));
  699. btrfs_set_file_extent_offset(leaf, extent,
  700. le64_to_cpu(old.offset) + end - key.offset);
  701. WARN_ON(le64_to_cpu(old.num_bytes) <
  702. (extent_end - end));
  703. btrfs_set_file_extent_num_bytes(leaf, extent,
  704. extent_end - end);
  705. btrfs_set_file_extent_type(leaf, extent,
  706. BTRFS_FILE_EXTENT_REG);
  707. btrfs_mark_buffer_dirty(path->nodes[0]);
  708. if (le64_to_cpu(old.disk_bytenr) != 0) {
  709. inode->i_blocks +=
  710. btrfs_file_extent_num_bytes(leaf,
  711. extent) >> 9;
  712. }
  713. ret = 0;
  714. goto out;
  715. }
  716. }
  717. out:
  718. btrfs_free_path(path);
  719. btrfs_check_file(root, inode);
  720. return ret;
  721. }
  722. /*
  723. * this gets pages into the page cache and locks them down
  724. */
  725. static int prepare_pages(struct btrfs_root *root, struct file *file,
  726. struct page **pages, size_t num_pages,
  727. loff_t pos, unsigned long first_index,
  728. unsigned long last_index, size_t write_bytes)
  729. {
  730. int i;
  731. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  732. struct inode *inode = fdentry(file)->d_inode;
  733. int err = 0;
  734. u64 start_pos;
  735. start_pos = pos & ~((u64)root->sectorsize - 1);
  736. memset(pages, 0, num_pages * sizeof(struct page *));
  737. for (i = 0; i < num_pages; i++) {
  738. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  739. if (!pages[i]) {
  740. err = -ENOMEM;
  741. BUG_ON(1);
  742. }
  743. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  744. ClearPageDirty(pages[i]);
  745. #else
  746. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  747. #endif
  748. wait_on_page_writeback(pages[i]);
  749. set_page_extent_mapped(pages[i]);
  750. WARN_ON(!PageLocked(pages[i]));
  751. }
  752. if (start_pos < inode->i_size) {
  753. u64 last_pos;
  754. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  755. lock_extent(&BTRFS_I(inode)->io_tree,
  756. start_pos, last_pos - 1, GFP_NOFS);
  757. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  758. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
  759. GFP_NOFS);
  760. unlock_extent(&BTRFS_I(inode)->io_tree,
  761. start_pos, last_pos - 1, GFP_NOFS);
  762. }
  763. return 0;
  764. }
  765. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  766. size_t count, loff_t *ppos)
  767. {
  768. loff_t pos;
  769. loff_t start_pos;
  770. ssize_t num_written = 0;
  771. ssize_t err = 0;
  772. int ret = 0;
  773. struct inode *inode = fdentry(file)->d_inode;
  774. struct btrfs_root *root = BTRFS_I(inode)->root;
  775. struct page **pages = NULL;
  776. int nrptrs;
  777. struct page *pinned[2];
  778. unsigned long first_index;
  779. unsigned long last_index;
  780. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  781. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  782. pinned[0] = NULL;
  783. pinned[1] = NULL;
  784. pos = *ppos;
  785. start_pos = pos;
  786. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  787. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  788. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  789. if (err)
  790. goto out_nolock;
  791. if (count == 0)
  792. goto out_nolock;
  793. err = remove_suid(fdentry(file));
  794. if (err)
  795. goto out_nolock;
  796. file_update_time(file);
  797. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  798. mutex_lock(&inode->i_mutex);
  799. first_index = pos >> PAGE_CACHE_SHIFT;
  800. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  801. /*
  802. * if this is a nodatasum mount, force summing off for the inode
  803. * all the time. That way a later mount with summing on won't
  804. * get confused
  805. */
  806. if (btrfs_test_opt(root, NODATASUM))
  807. btrfs_set_flag(inode, NODATASUM);
  808. /*
  809. * there are lots of better ways to do this, but this code
  810. * makes sure the first and last page in the file range are
  811. * up to date and ready for cow
  812. */
  813. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  814. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  815. if (!PageUptodate(pinned[0])) {
  816. ret = btrfs_readpage(NULL, pinned[0]);
  817. BUG_ON(ret);
  818. wait_on_page_locked(pinned[0]);
  819. } else {
  820. unlock_page(pinned[0]);
  821. }
  822. }
  823. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  824. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  825. if (!PageUptodate(pinned[1])) {
  826. ret = btrfs_readpage(NULL, pinned[1]);
  827. BUG_ON(ret);
  828. wait_on_page_locked(pinned[1]);
  829. } else {
  830. unlock_page(pinned[1]);
  831. }
  832. }
  833. while(count > 0) {
  834. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  835. size_t write_bytes = min(count, nrptrs *
  836. (size_t)PAGE_CACHE_SIZE -
  837. offset);
  838. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  839. PAGE_CACHE_SHIFT;
  840. WARN_ON(num_pages > nrptrs);
  841. memset(pages, 0, sizeof(pages));
  842. mutex_lock(&root->fs_info->fs_mutex);
  843. ret = btrfs_check_free_space(root, write_bytes, 0);
  844. mutex_unlock(&root->fs_info->fs_mutex);
  845. if (ret)
  846. goto out;
  847. ret = prepare_pages(root, file, pages, num_pages,
  848. pos, first_index, last_index,
  849. write_bytes);
  850. if (ret)
  851. goto out;
  852. ret = btrfs_copy_from_user(pos, num_pages,
  853. write_bytes, pages, buf);
  854. if (ret) {
  855. btrfs_drop_pages(pages, num_pages);
  856. goto out;
  857. }
  858. ret = dirty_and_release_pages(NULL, root, file, pages,
  859. num_pages, pos, write_bytes);
  860. btrfs_drop_pages(pages, num_pages);
  861. if (ret)
  862. goto out;
  863. buf += write_bytes;
  864. count -= write_bytes;
  865. pos += write_bytes;
  866. num_written += write_bytes;
  867. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  868. if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  869. btrfs_btree_balance_dirty(root, 1);
  870. btrfs_throttle(root);
  871. cond_resched();
  872. }
  873. out:
  874. mutex_unlock(&inode->i_mutex);
  875. out_nolock:
  876. kfree(pages);
  877. if (pinned[0])
  878. page_cache_release(pinned[0]);
  879. if (pinned[1])
  880. page_cache_release(pinned[1]);
  881. *ppos = pos;
  882. if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  883. err = sync_page_range(inode, inode->i_mapping,
  884. start_pos, num_written);
  885. if (err < 0)
  886. num_written = err;
  887. } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
  888. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  889. do_sync_file_range(file, start_pos,
  890. start_pos + num_written - 1,
  891. SYNC_FILE_RANGE_WRITE |
  892. SYNC_FILE_RANGE_WAIT_AFTER);
  893. #else
  894. do_sync_mapping_range(inode->i_mapping, start_pos,
  895. start_pos + num_written - 1,
  896. SYNC_FILE_RANGE_WRITE |
  897. SYNC_FILE_RANGE_WAIT_AFTER);
  898. #endif
  899. invalidate_mapping_pages(inode->i_mapping,
  900. start_pos >> PAGE_CACHE_SHIFT,
  901. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  902. }
  903. current->backing_dev_info = NULL;
  904. btrfs_ordered_throttle(root, inode);
  905. return num_written ? num_written : err;
  906. }
  907. static int btrfs_sync_file(struct file *file,
  908. struct dentry *dentry, int datasync)
  909. {
  910. struct inode *inode = dentry->d_inode;
  911. struct btrfs_root *root = BTRFS_I(inode)->root;
  912. int ret = 0;
  913. struct btrfs_trans_handle *trans;
  914. /*
  915. * check the transaction that last modified this inode
  916. * and see if its already been committed
  917. */
  918. mutex_lock(&root->fs_info->fs_mutex);
  919. if (!BTRFS_I(inode)->last_trans)
  920. goto out;
  921. mutex_lock(&root->fs_info->trans_mutex);
  922. if (BTRFS_I(inode)->last_trans <=
  923. root->fs_info->last_trans_committed) {
  924. BTRFS_I(inode)->last_trans = 0;
  925. mutex_unlock(&root->fs_info->trans_mutex);
  926. goto out;
  927. }
  928. mutex_unlock(&root->fs_info->trans_mutex);
  929. /*
  930. * ok we haven't committed the transaction yet, lets do a commit
  931. */
  932. trans = btrfs_start_transaction(root, 1);
  933. if (!trans) {
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. ret = btrfs_commit_transaction(trans, root);
  938. out:
  939. mutex_unlock(&root->fs_info->fs_mutex);
  940. return ret > 0 ? EIO : ret;
  941. }
  942. static struct vm_operations_struct btrfs_file_vm_ops = {
  943. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  944. .nopage = filemap_nopage,
  945. .populate = filemap_populate,
  946. #else
  947. .fault = filemap_fault,
  948. #endif
  949. .page_mkwrite = btrfs_page_mkwrite,
  950. };
  951. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  952. {
  953. vma->vm_ops = &btrfs_file_vm_ops;
  954. file_accessed(filp);
  955. return 0;
  956. }
  957. struct file_operations btrfs_file_operations = {
  958. .llseek = generic_file_llseek,
  959. .read = do_sync_read,
  960. .aio_read = generic_file_aio_read,
  961. .splice_read = generic_file_splice_read,
  962. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  963. .sendfile = generic_file_sendfile,
  964. #endif
  965. .write = btrfs_file_write,
  966. .mmap = btrfs_file_mmap,
  967. .open = generic_file_open,
  968. .fsync = btrfs_sync_file,
  969. .unlocked_ioctl = btrfs_ioctl,
  970. #ifdef CONFIG_COMPAT
  971. .compat_ioctl = btrfs_ioctl,
  972. #endif
  973. };