file.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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);
  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_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  328. u64 len = end - start + 1;
  329. if (end == (u64)-1)
  330. len = (u64)-1;
  331. while(1) {
  332. spin_lock(&em_tree->lock);
  333. em = lookup_extent_mapping(em_tree, start, len);
  334. if (!em) {
  335. spin_unlock(&em_tree->lock);
  336. break;
  337. }
  338. remove_extent_mapping(em_tree, em);
  339. spin_unlock(&em_tree->lock);
  340. /* once for us */
  341. free_extent_map(em);
  342. /* once for the tree*/
  343. free_extent_map(em);
  344. }
  345. return 0;
  346. }
  347. int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
  348. {
  349. return 0;
  350. #if 0
  351. struct btrfs_path *path;
  352. struct btrfs_key found_key;
  353. struct extent_buffer *leaf;
  354. struct btrfs_file_extent_item *extent;
  355. u64 last_offset = 0;
  356. int nritems;
  357. int slot;
  358. int found_type;
  359. int ret;
  360. int err = 0;
  361. u64 extent_end = 0;
  362. path = btrfs_alloc_path();
  363. ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
  364. last_offset, 0);
  365. while(1) {
  366. nritems = btrfs_header_nritems(path->nodes[0]);
  367. if (path->slots[0] >= nritems) {
  368. ret = btrfs_next_leaf(root, path);
  369. if (ret)
  370. goto out;
  371. nritems = btrfs_header_nritems(path->nodes[0]);
  372. }
  373. slot = path->slots[0];
  374. leaf = path->nodes[0];
  375. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  376. if (found_key.objectid != inode->i_ino)
  377. break;
  378. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  379. goto out;
  380. if (found_key.offset < last_offset) {
  381. WARN_ON(1);
  382. btrfs_print_leaf(root, leaf);
  383. printk("inode %lu found offset %Lu expected %Lu\n",
  384. inode->i_ino, found_key.offset, last_offset);
  385. err = 1;
  386. goto out;
  387. }
  388. extent = btrfs_item_ptr(leaf, slot,
  389. struct btrfs_file_extent_item);
  390. found_type = btrfs_file_extent_type(leaf, extent);
  391. if (found_type == BTRFS_FILE_EXTENT_REG) {
  392. extent_end = found_key.offset +
  393. btrfs_file_extent_num_bytes(leaf, extent);
  394. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  395. struct btrfs_item *item;
  396. item = btrfs_item_nr(leaf, slot);
  397. extent_end = found_key.offset +
  398. btrfs_file_extent_inline_len(leaf, item);
  399. extent_end = (extent_end + root->sectorsize - 1) &
  400. ~((u64)root->sectorsize -1 );
  401. }
  402. last_offset = extent_end;
  403. path->slots[0]++;
  404. }
  405. if (0 && last_offset < inode->i_size) {
  406. WARN_ON(1);
  407. btrfs_print_leaf(root, leaf);
  408. printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
  409. last_offset, inode->i_size);
  410. err = 1;
  411. }
  412. out:
  413. btrfs_free_path(path);
  414. return err;
  415. #endif
  416. }
  417. /*
  418. * this is very complex, but the basic idea is to drop all extents
  419. * in the range start - end. hint_block is filled in with a block number
  420. * that would be a good hint to the block allocator for this file.
  421. *
  422. * If an extent intersects the range but is not entirely inside the range
  423. * it is either truncated or split. Anything entirely inside the range
  424. * is deleted from the tree.
  425. */
  426. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  427. struct btrfs_root *root, struct inode *inode,
  428. u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
  429. {
  430. u64 extent_end = 0;
  431. u64 search_start = start;
  432. struct extent_buffer *leaf;
  433. struct btrfs_file_extent_item *extent;
  434. struct btrfs_path *path;
  435. struct btrfs_key key;
  436. struct btrfs_file_extent_item old;
  437. int keep;
  438. int slot;
  439. int bookend;
  440. int found_type;
  441. int found_extent;
  442. int found_inline;
  443. int recow;
  444. int ret;
  445. btrfs_drop_extent_cache(inode, start, end - 1);
  446. path = btrfs_alloc_path();
  447. if (!path)
  448. return -ENOMEM;
  449. while(1) {
  450. recow = 0;
  451. btrfs_release_path(root, path);
  452. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  453. search_start, -1);
  454. if (ret < 0)
  455. goto out;
  456. if (ret > 0) {
  457. if (path->slots[0] == 0) {
  458. ret = 0;
  459. goto out;
  460. }
  461. path->slots[0]--;
  462. }
  463. next_slot:
  464. keep = 0;
  465. bookend = 0;
  466. found_extent = 0;
  467. found_inline = 0;
  468. extent = NULL;
  469. leaf = path->nodes[0];
  470. slot = path->slots[0];
  471. ret = 0;
  472. btrfs_item_key_to_cpu(leaf, &key, slot);
  473. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
  474. key.offset >= end) {
  475. goto out;
  476. }
  477. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  478. key.objectid != inode->i_ino) {
  479. goto out;
  480. }
  481. if (recow) {
  482. search_start = key.offset;
  483. continue;
  484. }
  485. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  486. extent = btrfs_item_ptr(leaf, slot,
  487. struct btrfs_file_extent_item);
  488. found_type = btrfs_file_extent_type(leaf, extent);
  489. if (found_type == BTRFS_FILE_EXTENT_REG) {
  490. extent_end =
  491. btrfs_file_extent_disk_bytenr(leaf,
  492. extent);
  493. if (extent_end)
  494. *hint_byte = extent_end;
  495. extent_end = key.offset +
  496. btrfs_file_extent_num_bytes(leaf, extent);
  497. found_extent = 1;
  498. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  499. struct btrfs_item *item;
  500. item = btrfs_item_nr(leaf, slot);
  501. found_inline = 1;
  502. extent_end = key.offset +
  503. btrfs_file_extent_inline_len(leaf, item);
  504. }
  505. } else {
  506. extent_end = search_start;
  507. }
  508. /* we found nothing we can drop */
  509. if ((!found_extent && !found_inline) ||
  510. search_start >= extent_end) {
  511. int nextret;
  512. u32 nritems;
  513. nritems = btrfs_header_nritems(leaf);
  514. if (slot >= nritems - 1) {
  515. nextret = btrfs_next_leaf(root, path);
  516. if (nextret)
  517. goto out;
  518. recow = 1;
  519. } else {
  520. path->slots[0]++;
  521. }
  522. goto next_slot;
  523. }
  524. if (found_inline) {
  525. u64 mask = root->sectorsize - 1;
  526. search_start = (extent_end + mask) & ~mask;
  527. } else
  528. search_start = extent_end;
  529. if (end <= extent_end && start >= key.offset && found_inline) {
  530. *hint_byte = EXTENT_MAP_INLINE;
  531. continue;
  532. }
  533. if (end < extent_end && end >= key.offset) {
  534. if (found_extent) {
  535. u64 disk_bytenr =
  536. btrfs_file_extent_disk_bytenr(leaf, extent);
  537. u64 disk_num_bytes =
  538. btrfs_file_extent_disk_num_bytes(leaf,
  539. extent);
  540. read_extent_buffer(leaf, &old,
  541. (unsigned long)extent,
  542. sizeof(old));
  543. if (disk_bytenr != 0) {
  544. ret = btrfs_inc_extent_ref(trans, root,
  545. disk_bytenr, disk_num_bytes,
  546. root->root_key.objectid,
  547. trans->transid,
  548. key.objectid, end);
  549. BUG_ON(ret);
  550. }
  551. }
  552. bookend = 1;
  553. if (found_inline && start <= key.offset)
  554. keep = 1;
  555. }
  556. /* truncate existing extent */
  557. if (start > key.offset) {
  558. u64 new_num;
  559. u64 old_num;
  560. keep = 1;
  561. WARN_ON(start & (root->sectorsize - 1));
  562. if (found_extent) {
  563. new_num = start - key.offset;
  564. old_num = btrfs_file_extent_num_bytes(leaf,
  565. extent);
  566. *hint_byte =
  567. btrfs_file_extent_disk_bytenr(leaf,
  568. extent);
  569. if (btrfs_file_extent_disk_bytenr(leaf,
  570. extent)) {
  571. dec_i_blocks(inode, old_num - new_num);
  572. }
  573. btrfs_set_file_extent_num_bytes(leaf, extent,
  574. new_num);
  575. btrfs_mark_buffer_dirty(leaf);
  576. } else if (key.offset < inline_limit &&
  577. (end > extent_end) &&
  578. (inline_limit < extent_end)) {
  579. u32 new_size;
  580. new_size = btrfs_file_extent_calc_inline_size(
  581. inline_limit - key.offset);
  582. dec_i_blocks(inode, (extent_end - key.offset) -
  583. (inline_limit - key.offset));
  584. btrfs_truncate_item(trans, root, path,
  585. new_size, 1);
  586. }
  587. }
  588. /* delete the entire extent */
  589. if (!keep) {
  590. u64 disk_bytenr = 0;
  591. u64 disk_num_bytes = 0;
  592. u64 extent_num_bytes = 0;
  593. u64 root_gen;
  594. u64 root_owner;
  595. root_gen = btrfs_header_generation(leaf);
  596. root_owner = btrfs_header_owner(leaf);
  597. if (found_extent) {
  598. disk_bytenr =
  599. btrfs_file_extent_disk_bytenr(leaf,
  600. extent);
  601. disk_num_bytes =
  602. btrfs_file_extent_disk_num_bytes(leaf,
  603. extent);
  604. extent_num_bytes =
  605. btrfs_file_extent_num_bytes(leaf, extent);
  606. *hint_byte =
  607. btrfs_file_extent_disk_bytenr(leaf,
  608. extent);
  609. }
  610. ret = btrfs_del_item(trans, root, path);
  611. /* TODO update progress marker and return */
  612. BUG_ON(ret);
  613. btrfs_release_path(root, path);
  614. extent = NULL;
  615. if (found_extent && disk_bytenr != 0) {
  616. dec_i_blocks(inode, extent_num_bytes);
  617. ret = btrfs_free_extent(trans, root,
  618. disk_bytenr,
  619. disk_num_bytes,
  620. root_owner,
  621. root_gen, inode->i_ino,
  622. key.offset, 0);
  623. }
  624. BUG_ON(ret);
  625. if (!bookend && search_start >= end) {
  626. ret = 0;
  627. goto out;
  628. }
  629. if (!bookend)
  630. continue;
  631. }
  632. if (bookend && found_inline && start <= key.offset) {
  633. u32 new_size;
  634. new_size = btrfs_file_extent_calc_inline_size(
  635. extent_end - end);
  636. dec_i_blocks(inode, (extent_end - key.offset) -
  637. (extent_end - end));
  638. btrfs_truncate_item(trans, root, path, new_size, 0);
  639. }
  640. /* create bookend, splitting the extent in two */
  641. if (bookend && found_extent) {
  642. struct btrfs_key ins;
  643. ins.objectid = inode->i_ino;
  644. ins.offset = end;
  645. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  646. btrfs_release_path(root, path);
  647. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  648. sizeof(*extent));
  649. leaf = path->nodes[0];
  650. if (ret) {
  651. btrfs_print_leaf(root, leaf);
  652. 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);
  653. }
  654. BUG_ON(ret);
  655. extent = btrfs_item_ptr(leaf, path->slots[0],
  656. struct btrfs_file_extent_item);
  657. write_extent_buffer(leaf, &old,
  658. (unsigned long)extent, sizeof(old));
  659. btrfs_set_file_extent_offset(leaf, extent,
  660. le64_to_cpu(old.offset) + end - key.offset);
  661. WARN_ON(le64_to_cpu(old.num_bytes) <
  662. (extent_end - end));
  663. btrfs_set_file_extent_num_bytes(leaf, extent,
  664. extent_end - end);
  665. btrfs_set_file_extent_type(leaf, extent,
  666. BTRFS_FILE_EXTENT_REG);
  667. btrfs_mark_buffer_dirty(path->nodes[0]);
  668. if (le64_to_cpu(old.disk_bytenr) != 0) {
  669. inode->i_blocks +=
  670. btrfs_file_extent_num_bytes(leaf,
  671. extent) >> 9;
  672. }
  673. ret = 0;
  674. goto out;
  675. }
  676. }
  677. out:
  678. btrfs_free_path(path);
  679. btrfs_check_file(root, inode);
  680. return ret;
  681. }
  682. /*
  683. * this gets pages into the page cache and locks them down
  684. */
  685. static int prepare_pages(struct btrfs_root *root, struct file *file,
  686. struct page **pages, size_t num_pages,
  687. loff_t pos, unsigned long first_index,
  688. unsigned long last_index, size_t write_bytes)
  689. {
  690. int i;
  691. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  692. struct inode *inode = fdentry(file)->d_inode;
  693. int err = 0;
  694. u64 start_pos;
  695. start_pos = pos & ~((u64)root->sectorsize - 1);
  696. memset(pages, 0, num_pages * sizeof(struct page *));
  697. for (i = 0; i < num_pages; i++) {
  698. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  699. if (!pages[i]) {
  700. err = -ENOMEM;
  701. BUG_ON(1);
  702. }
  703. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  704. ClearPageDirty(pages[i]);
  705. #else
  706. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  707. #endif
  708. wait_on_page_writeback(pages[i]);
  709. set_page_extent_mapped(pages[i]);
  710. WARN_ON(!PageLocked(pages[i]));
  711. }
  712. if (start_pos < inode->i_size) {
  713. u64 last_pos;
  714. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  715. lock_extent(&BTRFS_I(inode)->io_tree,
  716. start_pos, last_pos - 1, GFP_NOFS);
  717. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  718. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
  719. GFP_NOFS);
  720. unlock_extent(&BTRFS_I(inode)->io_tree,
  721. start_pos, last_pos - 1, GFP_NOFS);
  722. }
  723. return 0;
  724. }
  725. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  726. size_t count, loff_t *ppos)
  727. {
  728. loff_t pos;
  729. loff_t start_pos;
  730. ssize_t num_written = 0;
  731. ssize_t err = 0;
  732. int ret = 0;
  733. struct inode *inode = fdentry(file)->d_inode;
  734. struct btrfs_root *root = BTRFS_I(inode)->root;
  735. struct page **pages = NULL;
  736. int nrptrs;
  737. struct page *pinned[2];
  738. unsigned long first_index;
  739. unsigned long last_index;
  740. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  741. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  742. pinned[0] = NULL;
  743. pinned[1] = NULL;
  744. pos = *ppos;
  745. start_pos = pos;
  746. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  747. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  748. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  749. if (err)
  750. goto out_nolock;
  751. if (count == 0)
  752. goto out_nolock;
  753. err = remove_suid(fdentry(file));
  754. if (err)
  755. goto out_nolock;
  756. file_update_time(file);
  757. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  758. mutex_lock(&inode->i_mutex);
  759. first_index = pos >> PAGE_CACHE_SHIFT;
  760. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  761. /*
  762. * there are lots of better ways to do this, but this code
  763. * makes sure the first and last page in the file range are
  764. * up to date and ready for cow
  765. */
  766. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  767. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  768. if (!PageUptodate(pinned[0])) {
  769. ret = btrfs_readpage(NULL, pinned[0]);
  770. BUG_ON(ret);
  771. wait_on_page_locked(pinned[0]);
  772. } else {
  773. unlock_page(pinned[0]);
  774. }
  775. }
  776. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  777. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  778. if (!PageUptodate(pinned[1])) {
  779. ret = btrfs_readpage(NULL, pinned[1]);
  780. BUG_ON(ret);
  781. wait_on_page_locked(pinned[1]);
  782. } else {
  783. unlock_page(pinned[1]);
  784. }
  785. }
  786. while(count > 0) {
  787. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  788. size_t write_bytes = min(count, nrptrs *
  789. (size_t)PAGE_CACHE_SIZE -
  790. offset);
  791. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  792. PAGE_CACHE_SHIFT;
  793. WARN_ON(num_pages > nrptrs);
  794. memset(pages, 0, sizeof(pages));
  795. mutex_lock(&root->fs_info->fs_mutex);
  796. ret = btrfs_check_free_space(root, write_bytes, 0);
  797. mutex_unlock(&root->fs_info->fs_mutex);
  798. if (ret)
  799. goto out;
  800. ret = prepare_pages(root, file, pages, num_pages,
  801. pos, first_index, last_index,
  802. write_bytes);
  803. if (ret)
  804. goto out;
  805. ret = btrfs_copy_from_user(pos, num_pages,
  806. write_bytes, pages, buf);
  807. if (ret) {
  808. btrfs_drop_pages(pages, num_pages);
  809. goto out;
  810. }
  811. ret = dirty_and_release_pages(NULL, root, file, pages,
  812. num_pages, pos, write_bytes);
  813. btrfs_drop_pages(pages, num_pages);
  814. if (ret)
  815. goto out;
  816. buf += write_bytes;
  817. count -= write_bytes;
  818. pos += write_bytes;
  819. num_written += write_bytes;
  820. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  821. if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  822. btrfs_btree_balance_dirty(root, 1);
  823. btrfs_throttle(root);
  824. cond_resched();
  825. }
  826. out:
  827. mutex_unlock(&inode->i_mutex);
  828. out_nolock:
  829. kfree(pages);
  830. if (pinned[0])
  831. page_cache_release(pinned[0]);
  832. if (pinned[1])
  833. page_cache_release(pinned[1]);
  834. *ppos = pos;
  835. if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  836. err = sync_page_range(inode, inode->i_mapping,
  837. start_pos, num_written);
  838. if (err < 0)
  839. num_written = err;
  840. } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
  841. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  842. do_sync_file_range(file, start_pos,
  843. start_pos + num_written - 1,
  844. SYNC_FILE_RANGE_WRITE |
  845. SYNC_FILE_RANGE_WAIT_AFTER);
  846. #else
  847. do_sync_mapping_range(inode->i_mapping, start_pos,
  848. start_pos + num_written - 1,
  849. SYNC_FILE_RANGE_WRITE |
  850. SYNC_FILE_RANGE_WAIT_AFTER);
  851. #endif
  852. invalidate_mapping_pages(inode->i_mapping,
  853. start_pos >> PAGE_CACHE_SHIFT,
  854. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  855. }
  856. current->backing_dev_info = NULL;
  857. return num_written ? num_written : err;
  858. }
  859. static int btrfs_sync_file(struct file *file,
  860. struct dentry *dentry, int datasync)
  861. {
  862. struct inode *inode = dentry->d_inode;
  863. struct btrfs_root *root = BTRFS_I(inode)->root;
  864. int ret = 0;
  865. struct btrfs_trans_handle *trans;
  866. /*
  867. * check the transaction that last modified this inode
  868. * and see if its already been committed
  869. */
  870. mutex_lock(&root->fs_info->fs_mutex);
  871. if (!BTRFS_I(inode)->last_trans)
  872. goto out;
  873. mutex_lock(&root->fs_info->trans_mutex);
  874. if (BTRFS_I(inode)->last_trans <=
  875. root->fs_info->last_trans_committed) {
  876. BTRFS_I(inode)->last_trans = 0;
  877. mutex_unlock(&root->fs_info->trans_mutex);
  878. goto out;
  879. }
  880. mutex_unlock(&root->fs_info->trans_mutex);
  881. /*
  882. * ok we haven't committed the transaction yet, lets do a commit
  883. */
  884. trans = btrfs_start_transaction(root, 1);
  885. if (!trans) {
  886. ret = -ENOMEM;
  887. goto out;
  888. }
  889. ret = btrfs_commit_transaction(trans, root);
  890. out:
  891. mutex_unlock(&root->fs_info->fs_mutex);
  892. return ret > 0 ? EIO : ret;
  893. }
  894. static struct vm_operations_struct btrfs_file_vm_ops = {
  895. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  896. .nopage = filemap_nopage,
  897. .populate = filemap_populate,
  898. #else
  899. .fault = filemap_fault,
  900. #endif
  901. .page_mkwrite = btrfs_page_mkwrite,
  902. };
  903. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  904. {
  905. vma->vm_ops = &btrfs_file_vm_ops;
  906. file_accessed(filp);
  907. return 0;
  908. }
  909. struct file_operations btrfs_file_operations = {
  910. .llseek = generic_file_llseek,
  911. .read = do_sync_read,
  912. .aio_read = generic_file_aio_read,
  913. .splice_read = generic_file_splice_read,
  914. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  915. .sendfile = generic_file_sendfile,
  916. #endif
  917. .write = btrfs_file_write,
  918. .mmap = btrfs_file_mmap,
  919. .open = generic_file_open,
  920. .fsync = btrfs_sync_file,
  921. .unlocked_ioctl = btrfs_ioctl,
  922. #ifdef CONFIG_COMPAT
  923. .compat_ioctl = btrfs_ioctl,
  924. #endif
  925. };