file.c 25 KB

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