file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 "ioctl.h"
  37. #include "print-tree.h"
  38. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  39. struct page **prepared_pages,
  40. const char __user * buf)
  41. {
  42. long page_fault = 0;
  43. int i;
  44. int offset = pos & (PAGE_CACHE_SIZE - 1);
  45. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  46. size_t count = min_t(size_t,
  47. PAGE_CACHE_SIZE - offset, write_bytes);
  48. struct page *page = prepared_pages[i];
  49. fault_in_pages_readable(buf, count);
  50. /* Copy data from userspace to the current page */
  51. kmap(page);
  52. page_fault = __copy_from_user(page_address(page) + offset,
  53. buf, count);
  54. /* Flush processor's dcache for this page */
  55. flush_dcache_page(page);
  56. kunmap(page);
  57. buf += count;
  58. write_bytes -= count;
  59. if (page_fault)
  60. break;
  61. }
  62. return page_fault ? -EFAULT : 0;
  63. }
  64. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  65. {
  66. size_t i;
  67. for (i = 0; i < num_pages; i++) {
  68. if (!pages[i])
  69. break;
  70. unlock_page(pages[i]);
  71. mark_page_accessed(pages[i]);
  72. page_cache_release(pages[i]);
  73. }
  74. }
  75. static int insert_inline_extent(struct btrfs_trans_handle *trans,
  76. struct btrfs_root *root, struct inode *inode,
  77. u64 offset, size_t size,
  78. struct page **pages, size_t page_offset,
  79. int num_pages)
  80. {
  81. struct btrfs_key key;
  82. struct btrfs_path *path;
  83. struct extent_buffer *leaf;
  84. char *kaddr;
  85. unsigned long ptr;
  86. struct btrfs_file_extent_item *ei;
  87. struct page *page;
  88. u32 datasize;
  89. int err = 0;
  90. int ret;
  91. int i;
  92. ssize_t cur_size;
  93. path = btrfs_alloc_path();
  94. if (!path)
  95. return -ENOMEM;
  96. btrfs_set_trans_block_group(trans, inode);
  97. key.objectid = inode->i_ino;
  98. key.offset = offset;
  99. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  100. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  101. if (ret < 0) {
  102. err = ret;
  103. goto fail;
  104. }
  105. if (ret == 1) {
  106. struct btrfs_key found_key;
  107. if (path->slots[0] == 0)
  108. goto insert;
  109. path->slots[0]--;
  110. leaf = path->nodes[0];
  111. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  112. if (found_key.objectid != inode->i_ino)
  113. goto insert;
  114. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  115. goto insert;
  116. ei = btrfs_item_ptr(leaf, path->slots[0],
  117. struct btrfs_file_extent_item);
  118. if (btrfs_file_extent_type(leaf, ei) !=
  119. BTRFS_FILE_EXTENT_INLINE) {
  120. goto insert;
  121. }
  122. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  123. ret = 0;
  124. }
  125. if (ret == 0) {
  126. u32 found_size;
  127. u64 found_end;
  128. leaf = path->nodes[0];
  129. ei = btrfs_item_ptr(leaf, path->slots[0],
  130. struct btrfs_file_extent_item);
  131. if (btrfs_file_extent_type(leaf, ei) !=
  132. BTRFS_FILE_EXTENT_INLINE) {
  133. err = ret;
  134. btrfs_print_leaf(root, leaf);
  135. printk("found wasn't inline offset %Lu inode %lu\n",
  136. offset, inode->i_ino);
  137. goto fail;
  138. }
  139. found_size = btrfs_file_extent_inline_len(leaf,
  140. btrfs_item_nr(leaf, path->slots[0]));
  141. found_end = key.offset + found_size;
  142. if (found_end < offset + size) {
  143. btrfs_release_path(root, path);
  144. ret = btrfs_search_slot(trans, root, &key, path,
  145. offset + size - found_end, 1);
  146. BUG_ON(ret != 0);
  147. ret = btrfs_extend_item(trans, root, path,
  148. offset + size - found_end);
  149. if (ret) {
  150. err = ret;
  151. goto fail;
  152. }
  153. leaf = path->nodes[0];
  154. ei = btrfs_item_ptr(leaf, path->slots[0],
  155. struct btrfs_file_extent_item);
  156. }
  157. if (found_end < offset) {
  158. ptr = btrfs_file_extent_inline_start(ei) + found_size;
  159. memset_extent_buffer(leaf, 0, ptr, offset - found_end);
  160. }
  161. } else {
  162. insert:
  163. btrfs_release_path(root, path);
  164. datasize = offset + size - key.offset;
  165. datasize = btrfs_file_extent_calc_inline_size(datasize);
  166. ret = btrfs_insert_empty_item(trans, root, path, &key,
  167. datasize);
  168. if (ret) {
  169. err = ret;
  170. printk("got bad ret %d\n", ret);
  171. goto fail;
  172. }
  173. leaf = path->nodes[0];
  174. ei = btrfs_item_ptr(leaf, path->slots[0],
  175. struct btrfs_file_extent_item);
  176. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  177. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  178. }
  179. ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
  180. cur_size = size;
  181. i = 0;
  182. while (size > 0) {
  183. page = pages[i];
  184. kaddr = kmap_atomic(page, KM_USER0);
  185. cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
  186. write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
  187. kunmap_atomic(kaddr, KM_USER0);
  188. page_offset = 0;
  189. ptr += cur_size;
  190. size -= cur_size;
  191. if (i >= num_pages) {
  192. printk("i %d num_pages %d\n", i, num_pages);
  193. }
  194. i++;
  195. }
  196. btrfs_mark_buffer_dirty(leaf);
  197. fail:
  198. btrfs_free_path(path);
  199. return err;
  200. }
  201. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  202. struct btrfs_root *root,
  203. struct file *file,
  204. struct page **pages,
  205. size_t num_pages,
  206. loff_t pos,
  207. size_t write_bytes)
  208. {
  209. int err = 0;
  210. int i;
  211. struct inode *inode = file->f_path.dentry->d_inode;
  212. struct extent_map *em;
  213. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_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. em = alloc_extent_map(GFP_NOFS);
  222. if (!em)
  223. return -ENOMEM;
  224. em->bdev = inode->i_sb->s_bdev;
  225. start_pos = pos & ~((u64)root->sectorsize - 1);
  226. num_bytes = (write_bytes + pos - start_pos +
  227. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  228. down_read(&BTRFS_I(inode)->root->snap_sem);
  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 > 32768 ||
  275. inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  276. u64 last_end;
  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. set_extent_delalloc(em_tree, start_pos, end_of_last_block,
  286. GFP_NOFS);
  287. } else {
  288. u64 aligned_end;
  289. /* step one, delete the existing extents in this range */
  290. aligned_end = (pos + write_bytes + root->sectorsize - 1) &
  291. ~((u64)root->sectorsize - 1);
  292. err = btrfs_drop_extents(trans, root, inode, start_pos,
  293. aligned_end, aligned_end, &hint_byte);
  294. if (err)
  295. goto failed;
  296. if (isize > inline_size)
  297. inline_size = min_t(u64, isize, aligned_end);
  298. inline_size -= start_pos;
  299. err = insert_inline_extent(trans, root, inode, start_pos,
  300. inline_size, pages, 0, num_pages);
  301. BUG_ON(err);
  302. }
  303. if (end_pos > isize) {
  304. i_size_write(inode, end_pos);
  305. btrfs_update_inode(trans, root, inode);
  306. }
  307. failed:
  308. err = btrfs_end_transaction(trans, root);
  309. out_unlock:
  310. mutex_unlock(&root->fs_info->fs_mutex);
  311. unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  312. free_extent_map(em);
  313. up_read(&BTRFS_I(inode)->root->snap_sem);
  314. return err;
  315. }
  316. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
  317. {
  318. struct extent_map *em;
  319. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  320. while(1) {
  321. em = lookup_extent_mapping(em_tree, start, end);
  322. if (!em)
  323. break;
  324. remove_extent_mapping(em_tree, em);
  325. /* once for us */
  326. free_extent_map(em);
  327. /* once for the tree*/
  328. free_extent_map(em);
  329. }
  330. return 0;
  331. }
  332. /*
  333. * this is very complex, but the basic idea is to drop all extents
  334. * in the range start - end. hint_block is filled in with a block number
  335. * that would be a good hint to the block allocator for this file.
  336. *
  337. * If an extent intersects the range but is not entirely inside the range
  338. * it is either truncated or split. Anything entirely inside the range
  339. * is deleted from the tree.
  340. */
  341. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  342. struct btrfs_root *root, struct inode *inode,
  343. u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
  344. {
  345. u64 extent_end = 0;
  346. u64 search_start = start;
  347. struct extent_buffer *leaf;
  348. struct btrfs_file_extent_item *extent;
  349. struct btrfs_path *path;
  350. struct btrfs_key key;
  351. struct btrfs_file_extent_item old;
  352. int keep;
  353. int slot;
  354. int bookend;
  355. int found_type;
  356. int found_extent;
  357. int found_inline;
  358. int recow;
  359. int ret;
  360. btrfs_drop_extent_cache(inode, start, end - 1);
  361. path = btrfs_alloc_path();
  362. if (!path)
  363. return -ENOMEM;
  364. while(1) {
  365. recow = 0;
  366. btrfs_release_path(root, path);
  367. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  368. search_start, -1);
  369. if (ret < 0)
  370. goto out;
  371. if (ret > 0) {
  372. if (path->slots[0] == 0) {
  373. ret = 0;
  374. goto out;
  375. }
  376. path->slots[0]--;
  377. }
  378. next_slot:
  379. keep = 0;
  380. bookend = 0;
  381. found_extent = 0;
  382. found_inline = 0;
  383. extent = NULL;
  384. leaf = path->nodes[0];
  385. slot = path->slots[0];
  386. ret = 0;
  387. btrfs_item_key_to_cpu(leaf, &key, slot);
  388. if (key.offset >= end || key.objectid != inode->i_ino) {
  389. goto out;
  390. }
  391. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
  392. goto out;
  393. }
  394. if (recow) {
  395. search_start = key.offset;
  396. continue;
  397. }
  398. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  399. extent = btrfs_item_ptr(leaf, slot,
  400. struct btrfs_file_extent_item);
  401. found_type = btrfs_file_extent_type(leaf, extent);
  402. if (found_type == BTRFS_FILE_EXTENT_REG) {
  403. extent_end =
  404. btrfs_file_extent_disk_bytenr(leaf,
  405. extent);
  406. if (extent_end)
  407. *hint_byte = extent_end;
  408. extent_end = key.offset +
  409. btrfs_file_extent_num_bytes(leaf, extent);
  410. found_extent = 1;
  411. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  412. struct btrfs_item *item;
  413. item = btrfs_item_nr(leaf, slot);
  414. found_inline = 1;
  415. extent_end = key.offset +
  416. btrfs_file_extent_inline_len(leaf, item);
  417. }
  418. } else {
  419. extent_end = search_start;
  420. }
  421. /* we found nothing we can drop */
  422. if ((!found_extent && !found_inline) ||
  423. search_start >= extent_end) {
  424. int nextret;
  425. u32 nritems;
  426. nritems = btrfs_header_nritems(leaf);
  427. if (slot >= nritems - 1) {
  428. nextret = btrfs_next_leaf(root, path);
  429. if (nextret)
  430. goto out;
  431. recow = 1;
  432. } else {
  433. path->slots[0]++;
  434. }
  435. goto next_slot;
  436. }
  437. if (found_inline) {
  438. u64 mask = root->sectorsize - 1;
  439. search_start = (extent_end + mask) & ~mask;
  440. } else
  441. search_start = extent_end;
  442. if (end <= extent_end && start >= key.offset && found_inline) {
  443. *hint_byte = EXTENT_MAP_INLINE;
  444. continue;
  445. }
  446. if (end < extent_end && end >= key.offset) {
  447. if (found_extent) {
  448. u64 disk_bytenr =
  449. btrfs_file_extent_disk_bytenr(leaf, extent);
  450. u64 disk_num_bytes =
  451. btrfs_file_extent_disk_num_bytes(leaf,
  452. extent);
  453. read_extent_buffer(leaf, &old,
  454. (unsigned long)extent,
  455. sizeof(old));
  456. if (disk_bytenr != 0) {
  457. ret = btrfs_inc_extent_ref(trans, root,
  458. disk_bytenr, disk_num_bytes,
  459. root->root_key.objectid,
  460. trans->transid,
  461. key.objectid, end);
  462. BUG_ON(ret);
  463. }
  464. }
  465. bookend = 1;
  466. if (found_inline && start <= key.offset &&
  467. inline_limit < extent_end)
  468. keep = 1;
  469. }
  470. /* truncate existing extent */
  471. if (start > key.offset) {
  472. u64 new_num;
  473. u64 old_num;
  474. keep = 1;
  475. WARN_ON(start & (root->sectorsize - 1));
  476. if (found_extent) {
  477. new_num = start - key.offset;
  478. old_num = btrfs_file_extent_num_bytes(leaf,
  479. extent);
  480. *hint_byte =
  481. btrfs_file_extent_disk_bytenr(leaf,
  482. extent);
  483. if (btrfs_file_extent_disk_bytenr(leaf,
  484. extent)) {
  485. inode->i_blocks -=
  486. (old_num - new_num) >> 9;
  487. }
  488. btrfs_set_file_extent_num_bytes(leaf, extent,
  489. new_num);
  490. btrfs_mark_buffer_dirty(leaf);
  491. } else if (key.offset < inline_limit &&
  492. (end > extent_end) &&
  493. (inline_limit < extent_end)) {
  494. u32 new_size;
  495. new_size = btrfs_file_extent_calc_inline_size(
  496. inline_limit - key.offset);
  497. btrfs_truncate_item(trans, root, path,
  498. new_size, 1);
  499. }
  500. }
  501. /* delete the entire extent */
  502. if (!keep) {
  503. u64 disk_bytenr = 0;
  504. u64 disk_num_bytes = 0;
  505. u64 extent_num_bytes = 0;
  506. u64 root_gen;
  507. u64 root_owner;
  508. root_gen = btrfs_header_generation(leaf);
  509. root_owner = btrfs_header_owner(leaf);
  510. if (found_extent) {
  511. disk_bytenr =
  512. btrfs_file_extent_disk_bytenr(leaf,
  513. extent);
  514. disk_num_bytes =
  515. btrfs_file_extent_disk_num_bytes(leaf,
  516. extent);
  517. extent_num_bytes =
  518. btrfs_file_extent_num_bytes(leaf, extent);
  519. *hint_byte =
  520. btrfs_file_extent_disk_bytenr(leaf,
  521. extent);
  522. }
  523. ret = btrfs_del_item(trans, root, path);
  524. /* TODO update progress marker and return */
  525. BUG_ON(ret);
  526. btrfs_release_path(root, path);
  527. extent = NULL;
  528. if (found_extent && disk_bytenr != 0) {
  529. inode->i_blocks -= extent_num_bytes >> 9;
  530. ret = btrfs_free_extent(trans, root,
  531. disk_bytenr,
  532. disk_num_bytes,
  533. root_owner,
  534. root_gen, inode->i_ino,
  535. key.offset, 0);
  536. }
  537. BUG_ON(ret);
  538. if (!bookend && search_start >= end) {
  539. ret = 0;
  540. goto out;
  541. }
  542. if (!bookend)
  543. continue;
  544. }
  545. if (bookend && found_inline && start <= key.offset &&
  546. inline_limit < extent_end && key.offset <= inline_limit) {
  547. u32 new_size;
  548. new_size = btrfs_file_extent_calc_inline_size(
  549. extent_end - inline_limit);
  550. btrfs_truncate_item(trans, root, path, new_size, 0);
  551. }
  552. /* create bookend, splitting the extent in two */
  553. if (bookend && found_extent) {
  554. struct btrfs_key ins;
  555. ins.objectid = inode->i_ino;
  556. ins.offset = end;
  557. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  558. btrfs_release_path(root, path);
  559. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  560. sizeof(*extent));
  561. leaf = path->nodes[0];
  562. if (ret) {
  563. btrfs_print_leaf(root, leaf);
  564. 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);
  565. }
  566. BUG_ON(ret);
  567. extent = btrfs_item_ptr(leaf, path->slots[0],
  568. struct btrfs_file_extent_item);
  569. write_extent_buffer(leaf, &old,
  570. (unsigned long)extent, sizeof(old));
  571. btrfs_set_file_extent_offset(leaf, extent,
  572. le64_to_cpu(old.offset) + end - key.offset);
  573. WARN_ON(le64_to_cpu(old.num_bytes) <
  574. (extent_end - end));
  575. btrfs_set_file_extent_num_bytes(leaf, extent,
  576. extent_end - end);
  577. btrfs_set_file_extent_type(leaf, extent,
  578. BTRFS_FILE_EXTENT_REG);
  579. btrfs_mark_buffer_dirty(path->nodes[0]);
  580. if (le64_to_cpu(old.disk_bytenr) != 0) {
  581. inode->i_blocks +=
  582. btrfs_file_extent_num_bytes(leaf,
  583. extent) >> 9;
  584. }
  585. ret = 0;
  586. goto out;
  587. }
  588. }
  589. out:
  590. btrfs_free_path(path);
  591. return ret;
  592. }
  593. /*
  594. * this gets pages into the page cache and locks them down
  595. */
  596. static int prepare_pages(struct btrfs_root *root,
  597. struct file *file,
  598. struct page **pages,
  599. size_t num_pages,
  600. loff_t pos,
  601. unsigned long first_index,
  602. unsigned long last_index,
  603. size_t write_bytes)
  604. {
  605. int i;
  606. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  607. struct inode *inode = file->f_path.dentry->d_inode;
  608. int err = 0;
  609. u64 start_pos;
  610. start_pos = pos & ~((u64)root->sectorsize - 1);
  611. memset(pages, 0, num_pages * sizeof(struct page *));
  612. for (i = 0; i < num_pages; i++) {
  613. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  614. if (!pages[i]) {
  615. err = -ENOMEM;
  616. BUG_ON(1);
  617. }
  618. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  619. wait_on_page_writeback(pages[i]);
  620. set_page_extent_mapped(pages[i]);
  621. WARN_ON(!PageLocked(pages[i]));
  622. }
  623. return 0;
  624. }
  625. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  626. size_t count, loff_t *ppos)
  627. {
  628. loff_t pos;
  629. loff_t start_pos;
  630. ssize_t num_written = 0;
  631. ssize_t err = 0;
  632. int ret = 0;
  633. struct inode *inode = file->f_path.dentry->d_inode;
  634. struct btrfs_root *root = BTRFS_I(inode)->root;
  635. struct page **pages = NULL;
  636. int nrptrs;
  637. struct page *pinned[2];
  638. unsigned long first_index;
  639. unsigned long last_index;
  640. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  641. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  642. pinned[0] = NULL;
  643. pinned[1] = NULL;
  644. if (file->f_flags & O_DIRECT)
  645. return -EINVAL;
  646. pos = *ppos;
  647. start_pos = pos;
  648. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  649. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  650. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  651. if (err)
  652. goto out;
  653. if (count == 0)
  654. goto out;
  655. err = remove_suid(file->f_path.dentry);
  656. if (err)
  657. goto out;
  658. file_update_time(file);
  659. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  660. mutex_lock(&inode->i_mutex);
  661. first_index = pos >> PAGE_CACHE_SHIFT;
  662. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  663. /*
  664. * there are lots of better ways to do this, but this code
  665. * makes sure the first and last page in the file range are
  666. * up to date and ready for cow
  667. */
  668. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  669. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  670. if (!PageUptodate(pinned[0])) {
  671. ret = btrfs_readpage(NULL, pinned[0]);
  672. BUG_ON(ret);
  673. wait_on_page_locked(pinned[0]);
  674. } else {
  675. unlock_page(pinned[0]);
  676. }
  677. }
  678. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  679. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  680. if (!PageUptodate(pinned[1])) {
  681. ret = btrfs_readpage(NULL, pinned[1]);
  682. BUG_ON(ret);
  683. wait_on_page_locked(pinned[1]);
  684. } else {
  685. unlock_page(pinned[1]);
  686. }
  687. }
  688. while(count > 0) {
  689. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  690. size_t write_bytes = min(count, nrptrs *
  691. (size_t)PAGE_CACHE_SIZE -
  692. offset);
  693. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  694. PAGE_CACHE_SHIFT;
  695. WARN_ON(num_pages > nrptrs);
  696. memset(pages, 0, sizeof(pages));
  697. ret = prepare_pages(root, file, pages, num_pages,
  698. pos, first_index, last_index,
  699. write_bytes);
  700. if (ret)
  701. goto out;
  702. ret = btrfs_copy_from_user(pos, num_pages,
  703. write_bytes, pages, buf);
  704. if (ret) {
  705. btrfs_drop_pages(pages, num_pages);
  706. goto out;
  707. }
  708. ret = dirty_and_release_pages(NULL, root, file, pages,
  709. num_pages, pos, write_bytes);
  710. btrfs_drop_pages(pages, num_pages);
  711. if (ret)
  712. goto out;
  713. buf += write_bytes;
  714. count -= write_bytes;
  715. pos += write_bytes;
  716. num_written += write_bytes;
  717. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  718. if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  719. btrfs_btree_balance_dirty(root, 1);
  720. cond_resched();
  721. }
  722. mutex_unlock(&inode->i_mutex);
  723. out:
  724. kfree(pages);
  725. if (pinned[0])
  726. page_cache_release(pinned[0]);
  727. if (pinned[1])
  728. page_cache_release(pinned[1]);
  729. *ppos = pos;
  730. if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  731. err = sync_page_range(inode, inode->i_mapping,
  732. start_pos, num_written);
  733. if (err < 0)
  734. num_written = err;
  735. }
  736. current->backing_dev_info = NULL;
  737. return num_written ? num_written : err;
  738. }
  739. static int btrfs_sync_file(struct file *file,
  740. struct dentry *dentry, int datasync)
  741. {
  742. struct inode *inode = dentry->d_inode;
  743. struct btrfs_root *root = BTRFS_I(inode)->root;
  744. int ret = 0;
  745. struct btrfs_trans_handle *trans;
  746. /*
  747. * check the transaction that last modified this inode
  748. * and see if its already been committed
  749. */
  750. mutex_lock(&root->fs_info->fs_mutex);
  751. if (!BTRFS_I(inode)->last_trans)
  752. goto out;
  753. mutex_lock(&root->fs_info->trans_mutex);
  754. if (BTRFS_I(inode)->last_trans <=
  755. root->fs_info->last_trans_committed) {
  756. BTRFS_I(inode)->last_trans = 0;
  757. mutex_unlock(&root->fs_info->trans_mutex);
  758. goto out;
  759. }
  760. mutex_unlock(&root->fs_info->trans_mutex);
  761. /*
  762. * ok we haven't committed the transaction yet, lets do a commit
  763. */
  764. trans = btrfs_start_transaction(root, 1);
  765. if (!trans) {
  766. ret = -ENOMEM;
  767. goto out;
  768. }
  769. ret = btrfs_commit_transaction(trans, root);
  770. out:
  771. mutex_unlock(&root->fs_info->fs_mutex);
  772. return ret > 0 ? EIO : ret;
  773. }
  774. static struct vm_operations_struct btrfs_file_vm_ops = {
  775. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  776. .nopage = filemap_nopage,
  777. .populate = filemap_populate,
  778. #else
  779. .fault = filemap_fault,
  780. #endif
  781. .page_mkwrite = btrfs_page_mkwrite,
  782. };
  783. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  784. {
  785. vma->vm_ops = &btrfs_file_vm_ops;
  786. file_accessed(filp);
  787. return 0;
  788. }
  789. struct file_operations btrfs_file_operations = {
  790. .llseek = generic_file_llseek,
  791. .read = do_sync_read,
  792. .aio_read = generic_file_aio_read,
  793. .splice_read = generic_file_splice_read,
  794. .write = btrfs_file_write,
  795. .mmap = btrfs_file_mmap,
  796. .open = generic_file_open,
  797. .fsync = btrfs_sync_file,
  798. .unlocked_ioctl = btrfs_ioctl,
  799. #ifdef CONFIG_COMPAT
  800. .compat_ioctl = btrfs_ioctl,
  801. #endif
  802. };