file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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, ssize_t size,
  78. struct page *page, size_t page_offset)
  79. {
  80. struct btrfs_key key;
  81. struct btrfs_path *path;
  82. struct extent_buffer *leaf;
  83. char *kaddr;
  84. unsigned long ptr;
  85. struct btrfs_file_extent_item *ei;
  86. u32 datasize;
  87. int err = 0;
  88. int ret;
  89. path = btrfs_alloc_path();
  90. if (!path)
  91. return -ENOMEM;
  92. btrfs_set_trans_block_group(trans, inode);
  93. key.objectid = inode->i_ino;
  94. key.offset = offset;
  95. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  96. BUG_ON(size >= PAGE_CACHE_SIZE);
  97. datasize = btrfs_file_extent_calc_inline_size(size);
  98. ret = btrfs_insert_empty_item(trans, root, path, &key,
  99. datasize);
  100. if (ret) {
  101. err = ret;
  102. goto fail;
  103. }
  104. leaf = path->nodes[0];
  105. ei = btrfs_item_ptr(leaf, path->slots[0],
  106. struct btrfs_file_extent_item);
  107. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  108. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  109. ptr = btrfs_file_extent_inline_start(ei);
  110. kaddr = kmap_atomic(page, KM_USER1);
  111. write_extent_buffer(leaf, kaddr + page_offset, ptr, size);
  112. kunmap_atomic(kaddr, KM_USER1);
  113. btrfs_mark_buffer_dirty(leaf);
  114. fail:
  115. btrfs_free_path(path);
  116. return err;
  117. }
  118. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  119. struct btrfs_root *root,
  120. struct file *file,
  121. struct page **pages,
  122. size_t num_pages,
  123. loff_t pos,
  124. size_t write_bytes)
  125. {
  126. int err = 0;
  127. int i;
  128. struct inode *inode = file->f_path.dentry->d_inode;
  129. struct extent_map *em;
  130. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  131. u64 hint_byte;
  132. u64 num_bytes;
  133. u64 start_pos;
  134. u64 end_of_last_block;
  135. u64 end_pos = pos + write_bytes;
  136. u32 inline_size;
  137. loff_t isize = i_size_read(inode);
  138. em = alloc_extent_map(GFP_NOFS);
  139. if (!em)
  140. return -ENOMEM;
  141. em->bdev = inode->i_sb->s_bdev;
  142. start_pos = pos & ~((u64)root->sectorsize - 1);
  143. num_bytes = (write_bytes + pos - start_pos +
  144. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  145. down_read(&BTRFS_I(inode)->root->snap_sem);
  146. end_of_last_block = start_pos + num_bytes - 1;
  147. lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  148. mutex_lock(&root->fs_info->fs_mutex);
  149. trans = btrfs_start_transaction(root, 1);
  150. if (!trans) {
  151. err = -ENOMEM;
  152. goto out_unlock;
  153. }
  154. btrfs_set_trans_block_group(trans, inode);
  155. inode->i_blocks += num_bytes >> 9;
  156. hint_byte = 0;
  157. if ((end_of_last_block & 4095) == 0) {
  158. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  159. }
  160. set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  161. /* FIXME...EIEIO, ENOSPC and more */
  162. /* insert any holes we need to create */
  163. if (inode->i_size < start_pos) {
  164. u64 last_pos_in_file;
  165. u64 hole_size;
  166. u64 mask = root->sectorsize - 1;
  167. last_pos_in_file = (isize + mask) & ~mask;
  168. hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
  169. if (last_pos_in_file < start_pos) {
  170. err = btrfs_drop_extents(trans, root, inode,
  171. last_pos_in_file,
  172. last_pos_in_file + hole_size,
  173. &hint_byte);
  174. if (err)
  175. goto failed;
  176. err = btrfs_insert_file_extent(trans, root,
  177. inode->i_ino,
  178. last_pos_in_file,
  179. 0, 0, hole_size);
  180. }
  181. if (err)
  182. goto failed;
  183. }
  184. /*
  185. * either allocate an extent for the new bytes or setup the key
  186. * to show we are doing inline data in the extent
  187. */
  188. inline_size = end_pos - start_pos;
  189. if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
  190. inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  191. inline_size >= PAGE_CACHE_SIZE) {
  192. u64 last_end;
  193. for (i = 0; i < num_pages; i++) {
  194. struct page *p = pages[i];
  195. SetPageUptodate(p);
  196. set_page_dirty(p);
  197. }
  198. last_end = pages[num_pages -1]->index << PAGE_CACHE_SHIFT;
  199. last_end += PAGE_CACHE_SIZE - 1;
  200. set_extent_delalloc(em_tree, start_pos, end_of_last_block,
  201. GFP_NOFS);
  202. } else {
  203. struct page *p = pages[0];
  204. /* step one, delete the existing extents in this range */
  205. err = btrfs_drop_extents(trans, root, inode, start_pos,
  206. (pos + write_bytes + root->sectorsize -1) &
  207. ~((u64)root->sectorsize - 1), &hint_byte);
  208. if (err)
  209. goto failed;
  210. err = insert_inline_extent(trans, root, inode, start_pos,
  211. end_pos - start_pos, p, 0);
  212. BUG_ON(err);
  213. em->start = start_pos;
  214. em->end = end_pos - 1;
  215. em->block_start = EXTENT_MAP_INLINE;
  216. em->block_end = EXTENT_MAP_INLINE;
  217. add_extent_mapping(em_tree, em);
  218. }
  219. if (end_pos > isize) {
  220. i_size_write(inode, end_pos);
  221. btrfs_update_inode(trans, root, inode);
  222. }
  223. failed:
  224. err = btrfs_end_transaction(trans, root);
  225. out_unlock:
  226. mutex_unlock(&root->fs_info->fs_mutex);
  227. unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  228. free_extent_map(em);
  229. up_read(&BTRFS_I(inode)->root->snap_sem);
  230. return err;
  231. }
  232. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
  233. {
  234. struct extent_map *em;
  235. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  236. while(1) {
  237. em = lookup_extent_mapping(em_tree, start, end);
  238. if (!em)
  239. break;
  240. remove_extent_mapping(em_tree, em);
  241. /* once for us */
  242. free_extent_map(em);
  243. /* once for the tree*/
  244. free_extent_map(em);
  245. }
  246. return 0;
  247. }
  248. /*
  249. * this is very complex, but the basic idea is to drop all extents
  250. * in the range start - end. hint_block is filled in with a block number
  251. * that would be a good hint to the block allocator for this file.
  252. *
  253. * If an extent intersects the range but is not entirely inside the range
  254. * it is either truncated or split. Anything entirely inside the range
  255. * is deleted from the tree.
  256. */
  257. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  258. struct btrfs_root *root, struct inode *inode,
  259. u64 start, u64 end, u64 *hint_byte)
  260. {
  261. int ret;
  262. struct btrfs_key key;
  263. struct extent_buffer *leaf;
  264. int slot;
  265. struct btrfs_file_extent_item *extent;
  266. u64 extent_end = 0;
  267. int keep;
  268. struct btrfs_file_extent_item old;
  269. struct btrfs_path *path;
  270. u64 search_start = start;
  271. int bookend;
  272. int found_type;
  273. int found_extent;
  274. int found_inline;
  275. int recow;
  276. btrfs_drop_extent_cache(inode, start, end - 1);
  277. path = btrfs_alloc_path();
  278. if (!path)
  279. return -ENOMEM;
  280. while(1) {
  281. recow = 0;
  282. btrfs_release_path(root, path);
  283. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  284. search_start, -1);
  285. if (ret < 0)
  286. goto out;
  287. if (ret > 0) {
  288. if (path->slots[0] == 0) {
  289. ret = 0;
  290. goto out;
  291. }
  292. path->slots[0]--;
  293. }
  294. next_slot:
  295. keep = 0;
  296. bookend = 0;
  297. found_extent = 0;
  298. found_inline = 0;
  299. extent = NULL;
  300. leaf = path->nodes[0];
  301. slot = path->slots[0];
  302. ret = 0;
  303. btrfs_item_key_to_cpu(leaf, &key, slot);
  304. if (key.offset >= end || key.objectid != inode->i_ino) {
  305. goto out;
  306. }
  307. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
  308. goto out;
  309. }
  310. if (recow) {
  311. search_start = key.offset;
  312. continue;
  313. }
  314. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  315. extent = btrfs_item_ptr(leaf, slot,
  316. struct btrfs_file_extent_item);
  317. found_type = btrfs_file_extent_type(leaf, extent);
  318. if (found_type == BTRFS_FILE_EXTENT_REG) {
  319. extent_end = key.offset +
  320. btrfs_file_extent_num_bytes(leaf, extent);
  321. found_extent = 1;
  322. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  323. struct btrfs_item *item;
  324. item = btrfs_item_nr(leaf, slot);
  325. found_inline = 1;
  326. extent_end = key.offset +
  327. btrfs_file_extent_inline_len(leaf, item);
  328. }
  329. } else {
  330. extent_end = search_start;
  331. }
  332. /* we found nothing we can drop */
  333. if ((!found_extent && !found_inline) ||
  334. search_start >= extent_end) {
  335. int nextret;
  336. u32 nritems;
  337. nritems = btrfs_header_nritems(leaf);
  338. if (slot >= nritems - 1) {
  339. nextret = btrfs_next_leaf(root, path);
  340. if (nextret)
  341. goto out;
  342. recow = 1;
  343. } else {
  344. path->slots[0]++;
  345. }
  346. goto next_slot;
  347. }
  348. /* FIXME, there's only one inline extent allowed right now */
  349. if (found_inline) {
  350. u64 mask = root->sectorsize - 1;
  351. search_start = (extent_end + mask) & ~mask;
  352. } else
  353. search_start = extent_end;
  354. if (end < extent_end && end >= key.offset) {
  355. if (found_extent) {
  356. u64 disk_bytenr =
  357. btrfs_file_extent_disk_bytenr(leaf, extent);
  358. u64 disk_num_bytes =
  359. btrfs_file_extent_disk_num_bytes(leaf,
  360. extent);
  361. read_extent_buffer(leaf, &old,
  362. (unsigned long)extent,
  363. sizeof(old));
  364. if (disk_bytenr != 0) {
  365. ret = btrfs_inc_extent_ref(trans, root,
  366. disk_bytenr, disk_num_bytes);
  367. BUG_ON(ret);
  368. }
  369. }
  370. WARN_ON(found_inline);
  371. bookend = 1;
  372. }
  373. /* truncate existing extent */
  374. if (start > key.offset) {
  375. u64 new_num;
  376. u64 old_num;
  377. keep = 1;
  378. WARN_ON(start & (root->sectorsize - 1));
  379. if (found_extent) {
  380. new_num = start - key.offset;
  381. old_num = btrfs_file_extent_num_bytes(leaf,
  382. extent);
  383. *hint_byte =
  384. btrfs_file_extent_disk_bytenr(leaf,
  385. extent);
  386. if (btrfs_file_extent_disk_bytenr(leaf,
  387. extent)) {
  388. inode->i_blocks -=
  389. (old_num - new_num) >> 9;
  390. }
  391. btrfs_set_file_extent_num_bytes(leaf, extent,
  392. new_num);
  393. btrfs_mark_buffer_dirty(leaf);
  394. } else {
  395. WARN_ON(1);
  396. }
  397. }
  398. /* delete the entire extent */
  399. if (!keep) {
  400. u64 disk_bytenr = 0;
  401. u64 disk_num_bytes = 0;
  402. u64 extent_num_bytes = 0;
  403. if (found_extent) {
  404. disk_bytenr =
  405. btrfs_file_extent_disk_bytenr(leaf,
  406. extent);
  407. disk_num_bytes =
  408. btrfs_file_extent_disk_num_bytes(leaf,
  409. extent);
  410. extent_num_bytes =
  411. btrfs_file_extent_num_bytes(leaf, extent);
  412. *hint_byte =
  413. btrfs_file_extent_disk_bytenr(leaf,
  414. extent);
  415. }
  416. ret = btrfs_del_item(trans, root, path);
  417. /* TODO update progress marker and return */
  418. BUG_ON(ret);
  419. btrfs_release_path(root, path);
  420. extent = NULL;
  421. if (found_extent && disk_bytenr != 0) {
  422. inode->i_blocks -= extent_num_bytes >> 9;
  423. ret = btrfs_free_extent(trans, root,
  424. disk_bytenr,
  425. disk_num_bytes, 0);
  426. }
  427. BUG_ON(ret);
  428. if (!bookend && search_start >= end) {
  429. ret = 0;
  430. goto out;
  431. }
  432. if (!bookend)
  433. continue;
  434. }
  435. /* create bookend, splitting the extent in two */
  436. if (bookend && found_extent) {
  437. struct btrfs_key ins;
  438. ins.objectid = inode->i_ino;
  439. ins.offset = end;
  440. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  441. btrfs_release_path(root, path);
  442. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  443. sizeof(*extent));
  444. leaf = path->nodes[0];
  445. if (ret) {
  446. btrfs_print_leaf(root, leaf);
  447. 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);
  448. }
  449. BUG_ON(ret);
  450. extent = btrfs_item_ptr(leaf, path->slots[0],
  451. struct btrfs_file_extent_item);
  452. write_extent_buffer(leaf, &old,
  453. (unsigned long)extent, sizeof(old));
  454. btrfs_set_file_extent_offset(leaf, extent,
  455. le64_to_cpu(old.offset) + end - key.offset);
  456. WARN_ON(le64_to_cpu(old.num_bytes) <
  457. (extent_end - end));
  458. btrfs_set_file_extent_num_bytes(leaf, extent,
  459. extent_end - end);
  460. btrfs_set_file_extent_type(leaf, extent,
  461. BTRFS_FILE_EXTENT_REG);
  462. btrfs_mark_buffer_dirty(path->nodes[0]);
  463. if (le64_to_cpu(old.disk_bytenr) != 0) {
  464. inode->i_blocks +=
  465. btrfs_file_extent_num_bytes(leaf,
  466. extent) >> 9;
  467. }
  468. ret = 0;
  469. goto out;
  470. }
  471. }
  472. out:
  473. btrfs_free_path(path);
  474. return ret;
  475. }
  476. /*
  477. * this gets pages into the page cache and locks them down
  478. */
  479. static int prepare_pages(struct btrfs_root *root,
  480. struct file *file,
  481. struct page **pages,
  482. size_t num_pages,
  483. loff_t pos,
  484. unsigned long first_index,
  485. unsigned long last_index,
  486. size_t write_bytes)
  487. {
  488. int i;
  489. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  490. struct inode *inode = file->f_path.dentry->d_inode;
  491. int err = 0;
  492. u64 start_pos;
  493. start_pos = pos & ~((u64)root->sectorsize - 1);
  494. memset(pages, 0, num_pages * sizeof(struct page *));
  495. for (i = 0; i < num_pages; i++) {
  496. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  497. if (!pages[i]) {
  498. err = -ENOMEM;
  499. BUG_ON(1);
  500. }
  501. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  502. wait_on_page_writeback(pages[i]);
  503. set_page_extent_mapped(pages[i]);
  504. WARN_ON(!PageLocked(pages[i]));
  505. }
  506. return 0;
  507. }
  508. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  509. size_t count, loff_t *ppos)
  510. {
  511. loff_t pos;
  512. size_t num_written = 0;
  513. int err = 0;
  514. int ret = 0;
  515. struct inode *inode = file->f_path.dentry->d_inode;
  516. struct btrfs_root *root = BTRFS_I(inode)->root;
  517. struct page **pages = NULL;
  518. int nrptrs;
  519. struct page *pinned[2];
  520. unsigned long first_index;
  521. unsigned long last_index;
  522. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  523. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  524. pinned[0] = NULL;
  525. pinned[1] = NULL;
  526. if (file->f_flags & O_DIRECT)
  527. return -EINVAL;
  528. pos = *ppos;
  529. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  530. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  531. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  532. if (err)
  533. goto out;
  534. if (count == 0)
  535. goto out;
  536. err = remove_suid(file->f_path.dentry);
  537. if (err)
  538. goto out;
  539. file_update_time(file);
  540. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  541. mutex_lock(&inode->i_mutex);
  542. first_index = pos >> PAGE_CACHE_SHIFT;
  543. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  544. /*
  545. * there are lots of better ways to do this, but this code
  546. * makes sure the first and last page in the file range are
  547. * up to date and ready for cow
  548. */
  549. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  550. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  551. if (!PageUptodate(pinned[0])) {
  552. ret = btrfs_readpage(NULL, pinned[0]);
  553. BUG_ON(ret);
  554. wait_on_page_locked(pinned[0]);
  555. } else {
  556. unlock_page(pinned[0]);
  557. }
  558. }
  559. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  560. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  561. if (!PageUptodate(pinned[1])) {
  562. ret = btrfs_readpage(NULL, pinned[1]);
  563. BUG_ON(ret);
  564. wait_on_page_locked(pinned[1]);
  565. } else {
  566. unlock_page(pinned[1]);
  567. }
  568. }
  569. while(count > 0) {
  570. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  571. size_t write_bytes = min(count, nrptrs *
  572. (size_t)PAGE_CACHE_SIZE -
  573. offset);
  574. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  575. PAGE_CACHE_SHIFT;
  576. WARN_ON(num_pages > nrptrs);
  577. memset(pages, 0, sizeof(pages));
  578. ret = prepare_pages(root, file, pages, num_pages,
  579. pos, first_index, last_index,
  580. write_bytes);
  581. if (ret)
  582. goto out;
  583. ret = btrfs_copy_from_user(pos, num_pages,
  584. write_bytes, pages, buf);
  585. if (ret) {
  586. btrfs_drop_pages(pages, num_pages);
  587. goto out;
  588. }
  589. ret = dirty_and_release_pages(NULL, root, file, pages,
  590. num_pages, pos, write_bytes);
  591. btrfs_drop_pages(pages, num_pages);
  592. if (ret)
  593. goto out;
  594. buf += write_bytes;
  595. count -= write_bytes;
  596. pos += write_bytes;
  597. num_written += write_bytes;
  598. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  599. btrfs_btree_balance_dirty(root, 1);
  600. cond_resched();
  601. }
  602. mutex_unlock(&inode->i_mutex);
  603. out:
  604. kfree(pages);
  605. if (pinned[0])
  606. page_cache_release(pinned[0]);
  607. if (pinned[1])
  608. page_cache_release(pinned[1]);
  609. *ppos = pos;
  610. current->backing_dev_info = NULL;
  611. return num_written ? num_written : err;
  612. }
  613. static int btrfs_sync_file(struct file *file,
  614. struct dentry *dentry, int datasync)
  615. {
  616. struct inode *inode = dentry->d_inode;
  617. struct btrfs_root *root = BTRFS_I(inode)->root;
  618. int ret = 0;
  619. struct btrfs_trans_handle *trans;
  620. /*
  621. * check the transaction that last modified this inode
  622. * and see if its already been committed
  623. */
  624. mutex_lock(&root->fs_info->fs_mutex);
  625. if (!BTRFS_I(inode)->last_trans)
  626. goto out;
  627. mutex_lock(&root->fs_info->trans_mutex);
  628. if (BTRFS_I(inode)->last_trans <=
  629. root->fs_info->last_trans_committed) {
  630. BTRFS_I(inode)->last_trans = 0;
  631. mutex_unlock(&root->fs_info->trans_mutex);
  632. goto out;
  633. }
  634. mutex_unlock(&root->fs_info->trans_mutex);
  635. /*
  636. * ok we haven't committed the transaction yet, lets do a commit
  637. */
  638. trans = btrfs_start_transaction(root, 1);
  639. if (!trans) {
  640. ret = -ENOMEM;
  641. goto out;
  642. }
  643. ret = btrfs_commit_transaction(trans, root);
  644. out:
  645. mutex_unlock(&root->fs_info->fs_mutex);
  646. return ret > 0 ? EIO : ret;
  647. }
  648. static struct vm_operations_struct btrfs_file_vm_ops = {
  649. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  650. .nopage = filemap_nopage,
  651. .populate = filemap_populate,
  652. #else
  653. .fault = filemap_fault,
  654. #endif
  655. .page_mkwrite = btrfs_page_mkwrite,
  656. };
  657. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  658. {
  659. vma->vm_ops = &btrfs_file_vm_ops;
  660. file_accessed(filp);
  661. return 0;
  662. }
  663. struct file_operations btrfs_file_operations = {
  664. .llseek = generic_file_llseek,
  665. .read = do_sync_read,
  666. .aio_read = generic_file_aio_read,
  667. .write = btrfs_file_write,
  668. .mmap = btrfs_file_mmap,
  669. .open = generic_file_open,
  670. .fsync = btrfs_sync_file,
  671. .unlocked_ioctl = btrfs_ioctl,
  672. #ifdef CONFIG_COMPAT
  673. .compat_ioctl = btrfs_ioctl,
  674. #endif
  675. };