file.c 19 KB

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