file.c 25 KB

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