file.c 23 KB

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