file.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. mutex_lock(&root->fs_info->fs_mutex);
  97. trans = btrfs_start_transaction(root, 1);
  98. btrfs_set_trans_block_group(trans, inode);
  99. bh = page_buffers(pages[i]);
  100. if (buffer_mapped(bh) && bh->b_blocknr == 0) {
  101. struct btrfs_key key;
  102. struct btrfs_path *path;
  103. char *ptr;
  104. u32 datasize;
  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. btrfs_memcpy(root, path->nodes[0]->b_data,
  125. ptr, bh->b_data, offset + write_bytes);
  126. mark_buffer_dirty(path->nodes[0]);
  127. btrfs_free_path(path);
  128. } else if (buffer_mapped(bh)) {
  129. /* csum the file data */
  130. btrfs_csum_file_block(trans, root, inode->i_ino,
  131. pages[i]->index << PAGE_CACHE_SHIFT,
  132. kmap(pages[i]), PAGE_CACHE_SIZE);
  133. kunmap(pages[i]);
  134. }
  135. SetPageChecked(pages[i]);
  136. ret = btrfs_end_transaction(trans, root);
  137. BUG_ON(ret);
  138. mutex_unlock(&root->fs_info->fs_mutex);
  139. ret = btrfs_commit_write(file, pages[i], offset,
  140. offset + this_write);
  141. pos += this_write;
  142. if (ret) {
  143. err = ret;
  144. goto failed;
  145. }
  146. WARN_ON(this_write > write_bytes);
  147. write_bytes -= this_write;
  148. }
  149. failed:
  150. return err;
  151. }
  152. /*
  153. * this is very complex, but the basic idea is to drop all extents
  154. * in the range start - end. hint_block is filled in with a block number
  155. * that would be a good hint to the block allocator for this file.
  156. *
  157. * If an extent intersects the range but is not entirely inside the range
  158. * it is either truncated or split. Anything entirely inside the range
  159. * is deleted from the tree.
  160. */
  161. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  162. struct btrfs_root *root, struct inode *inode,
  163. u64 start, u64 end, u64 *hint_block)
  164. {
  165. int ret;
  166. struct btrfs_key key;
  167. struct btrfs_leaf *leaf;
  168. int slot;
  169. struct btrfs_file_extent_item *extent;
  170. u64 extent_end = 0;
  171. int keep;
  172. struct btrfs_file_extent_item old;
  173. struct btrfs_path *path;
  174. u64 search_start = start;
  175. int bookend;
  176. int found_type;
  177. int found_extent;
  178. int found_inline;
  179. path = btrfs_alloc_path();
  180. if (!path)
  181. return -ENOMEM;
  182. while(1) {
  183. btrfs_release_path(root, path);
  184. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  185. search_start, -1);
  186. if (ret < 0)
  187. goto out;
  188. if (ret > 0) {
  189. if (path->slots[0] == 0) {
  190. ret = 0;
  191. goto out;
  192. }
  193. path->slots[0]--;
  194. }
  195. keep = 0;
  196. bookend = 0;
  197. found_extent = 0;
  198. found_inline = 0;
  199. extent = NULL;
  200. leaf = btrfs_buffer_leaf(path->nodes[0]);
  201. slot = path->slots[0];
  202. btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
  203. if (key.offset >= end || key.objectid != inode->i_ino) {
  204. ret = 0;
  205. goto out;
  206. }
  207. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
  208. ret = 0;
  209. goto out;
  210. }
  211. extent = btrfs_item_ptr(leaf, slot,
  212. struct btrfs_file_extent_item);
  213. found_type = btrfs_file_extent_type(extent);
  214. if (found_type == BTRFS_FILE_EXTENT_REG) {
  215. extent_end = key.offset +
  216. (btrfs_file_extent_num_blocks(extent) <<
  217. inode->i_blkbits);
  218. found_extent = 1;
  219. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  220. found_inline = 1;
  221. extent_end = key.offset +
  222. btrfs_file_extent_inline_len(leaf->items + slot);
  223. }
  224. /* we found nothing we can drop */
  225. if (!found_extent && !found_inline) {
  226. ret = 0;
  227. goto out;
  228. }
  229. /* we found nothing inside the range */
  230. if (search_start >= extent_end) {
  231. ret = 0;
  232. goto out;
  233. }
  234. /* FIXME, there's only one inline extent allowed right now */
  235. if (found_inline) {
  236. u64 mask = root->blocksize - 1;
  237. search_start = (extent_end + mask) & ~mask;
  238. } else
  239. search_start = extent_end;
  240. if (end < extent_end && end >= key.offset) {
  241. if (found_extent) {
  242. u64 disk_blocknr =
  243. btrfs_file_extent_disk_blocknr(extent);
  244. u64 disk_num_blocks =
  245. btrfs_file_extent_disk_num_blocks(extent);
  246. memcpy(&old, extent, sizeof(old));
  247. if (disk_blocknr != 0) {
  248. ret = btrfs_inc_extent_ref(trans, root,
  249. disk_blocknr, disk_num_blocks);
  250. BUG_ON(ret);
  251. }
  252. }
  253. WARN_ON(found_inline);
  254. bookend = 1;
  255. }
  256. /* truncate existing extent */
  257. if (start > key.offset) {
  258. u64 new_num;
  259. u64 old_num;
  260. keep = 1;
  261. WARN_ON(start & (root->blocksize - 1));
  262. if (found_extent) {
  263. new_num = (start - key.offset) >>
  264. inode->i_blkbits;
  265. old_num = btrfs_file_extent_num_blocks(extent);
  266. *hint_block =
  267. btrfs_file_extent_disk_blocknr(extent);
  268. if (btrfs_file_extent_disk_blocknr(extent)) {
  269. inode->i_blocks -=
  270. (old_num - new_num) << 3;
  271. }
  272. btrfs_set_file_extent_num_blocks(extent,
  273. new_num);
  274. mark_buffer_dirty(path->nodes[0]);
  275. } else {
  276. WARN_ON(1);
  277. }
  278. }
  279. /* delete the entire extent */
  280. if (!keep) {
  281. u64 disk_blocknr = 0;
  282. u64 disk_num_blocks = 0;
  283. u64 extent_num_blocks = 0;
  284. if (found_extent) {
  285. disk_blocknr =
  286. btrfs_file_extent_disk_blocknr(extent);
  287. disk_num_blocks =
  288. btrfs_file_extent_disk_num_blocks(extent);
  289. extent_num_blocks =
  290. btrfs_file_extent_num_blocks(extent);
  291. *hint_block =
  292. btrfs_file_extent_disk_blocknr(extent);
  293. }
  294. ret = btrfs_del_item(trans, root, path);
  295. BUG_ON(ret);
  296. btrfs_release_path(root, path);
  297. extent = NULL;
  298. if (found_extent && disk_blocknr != 0) {
  299. inode->i_blocks -= extent_num_blocks << 3;
  300. ret = btrfs_free_extent(trans, root,
  301. disk_blocknr,
  302. disk_num_blocks, 0);
  303. }
  304. BUG_ON(ret);
  305. if (!bookend && search_start >= end) {
  306. ret = 0;
  307. goto out;
  308. }
  309. if (!bookend)
  310. continue;
  311. }
  312. /* create bookend, splitting the extent in two */
  313. if (bookend && found_extent) {
  314. struct btrfs_key ins;
  315. ins.objectid = inode->i_ino;
  316. ins.offset = end;
  317. ins.flags = 0;
  318. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  319. btrfs_release_path(root, path);
  320. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  321. sizeof(*extent));
  322. BUG_ON(ret);
  323. extent = btrfs_item_ptr(
  324. btrfs_buffer_leaf(path->nodes[0]),
  325. path->slots[0],
  326. struct btrfs_file_extent_item);
  327. btrfs_set_file_extent_disk_blocknr(extent,
  328. btrfs_file_extent_disk_blocknr(&old));
  329. btrfs_set_file_extent_disk_num_blocks(extent,
  330. btrfs_file_extent_disk_num_blocks(&old));
  331. btrfs_set_file_extent_offset(extent,
  332. btrfs_file_extent_offset(&old) +
  333. ((end - key.offset) >> inode->i_blkbits));
  334. WARN_ON(btrfs_file_extent_num_blocks(&old) <
  335. (extent_end - end) >> inode->i_blkbits);
  336. btrfs_set_file_extent_num_blocks(extent,
  337. (extent_end - end) >> inode->i_blkbits);
  338. btrfs_set_file_extent_type(extent,
  339. BTRFS_FILE_EXTENT_REG);
  340. btrfs_set_file_extent_generation(extent,
  341. btrfs_file_extent_generation(&old));
  342. btrfs_mark_buffer_dirty(path->nodes[0]);
  343. if (btrfs_file_extent_disk_blocknr(&old) != 0) {
  344. inode->i_blocks +=
  345. btrfs_file_extent_num_blocks(extent) << 3;
  346. }
  347. ret = 0;
  348. goto out;
  349. }
  350. }
  351. out:
  352. btrfs_free_path(path);
  353. return ret;
  354. }
  355. /*
  356. * this gets pages into the page cache and locks them down
  357. */
  358. static int prepare_pages(struct btrfs_root *root,
  359. struct file *file,
  360. struct page **pages,
  361. size_t num_pages,
  362. loff_t pos,
  363. unsigned long first_index,
  364. unsigned long last_index,
  365. size_t write_bytes,
  366. u64 alloc_extent_start)
  367. {
  368. int i;
  369. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  370. struct inode *inode = file->f_path.dentry->d_inode;
  371. int offset;
  372. int err = 0;
  373. int this_write;
  374. struct buffer_head *bh;
  375. struct buffer_head *head;
  376. loff_t isize = i_size_read(inode);
  377. memset(pages, 0, num_pages * sizeof(struct page *));
  378. for (i = 0; i < num_pages; i++) {
  379. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  380. if (!pages[i]) {
  381. err = -ENOMEM;
  382. goto failed_release;
  383. }
  384. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  385. wait_on_page_writeback(pages[i]);
  386. offset = pos & (PAGE_CACHE_SIZE -1);
  387. this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes);
  388. if (!page_has_buffers(pages[i])) {
  389. create_empty_buffers(pages[i],
  390. root->fs_info->sb->s_blocksize,
  391. (1 << BH_Uptodate));
  392. }
  393. head = page_buffers(pages[i]);
  394. bh = head;
  395. do {
  396. err = btrfs_map_bh_to_logical(root, bh,
  397. alloc_extent_start);
  398. BUG_ON(err);
  399. if (err)
  400. goto failed_truncate;
  401. bh = bh->b_this_page;
  402. if (alloc_extent_start)
  403. alloc_extent_start++;
  404. } while (bh != head);
  405. pos += this_write;
  406. WARN_ON(this_write > write_bytes);
  407. write_bytes -= this_write;
  408. }
  409. return 0;
  410. failed_release:
  411. btrfs_drop_pages(pages, num_pages);
  412. return err;
  413. failed_truncate:
  414. btrfs_drop_pages(pages, num_pages);
  415. if (pos > isize)
  416. vmtruncate(inode, isize);
  417. return err;
  418. }
  419. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  420. size_t count, loff_t *ppos)
  421. {
  422. loff_t pos;
  423. size_t num_written = 0;
  424. int err = 0;
  425. int ret = 0;
  426. struct inode *inode = file->f_path.dentry->d_inode;
  427. struct btrfs_root *root = BTRFS_I(inode)->root;
  428. struct page *pages[8];
  429. struct page *pinned[2];
  430. unsigned long first_index;
  431. unsigned long last_index;
  432. u64 start_pos;
  433. u64 num_blocks;
  434. u64 alloc_extent_start;
  435. u64 hint_block;
  436. struct btrfs_trans_handle *trans;
  437. struct btrfs_key ins;
  438. pinned[0] = NULL;
  439. pinned[1] = NULL;
  440. if (file->f_flags & O_DIRECT)
  441. return -EINVAL;
  442. pos = *ppos;
  443. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  444. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  445. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  446. if (err)
  447. goto out;
  448. if (count == 0)
  449. goto out;
  450. err = remove_suid(file->f_path.dentry);
  451. if (err)
  452. goto out;
  453. file_update_time(file);
  454. start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
  455. num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
  456. inode->i_blkbits;
  457. mutex_lock(&inode->i_mutex);
  458. first_index = pos >> PAGE_CACHE_SHIFT;
  459. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  460. /*
  461. * there are lots of better ways to do this, but this code
  462. * makes sure the first and last page in the file range are
  463. * up to date and ready for cow
  464. */
  465. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  466. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  467. if (!PageUptodate(pinned[0])) {
  468. ret = mpage_readpage(pinned[0], btrfs_get_block);
  469. BUG_ON(ret);
  470. wait_on_page_locked(pinned[0]);
  471. } else {
  472. unlock_page(pinned[0]);
  473. }
  474. }
  475. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  476. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  477. if (!PageUptodate(pinned[1])) {
  478. ret = mpage_readpage(pinned[1], btrfs_get_block);
  479. BUG_ON(ret);
  480. wait_on_page_locked(pinned[1]);
  481. } else {
  482. unlock_page(pinned[1]);
  483. }
  484. }
  485. mutex_lock(&root->fs_info->fs_mutex);
  486. trans = btrfs_start_transaction(root, 1);
  487. if (!trans) {
  488. err = -ENOMEM;
  489. mutex_unlock(&root->fs_info->fs_mutex);
  490. goto out_unlock;
  491. }
  492. btrfs_set_trans_block_group(trans, inode);
  493. /* FIXME blocksize != 4096 */
  494. inode->i_blocks += num_blocks << 3;
  495. hint_block = 0;
  496. /* FIXME...EIEIO, ENOSPC and more */
  497. /* step one, delete the existing extents in this range */
  498. if (start_pos < inode->i_size) {
  499. /* FIXME blocksize != pagesize */
  500. ret = btrfs_drop_extents(trans, root, inode,
  501. start_pos,
  502. (pos + count + root->blocksize -1) &
  503. ~((u64)root->blocksize - 1),
  504. &hint_block);
  505. BUG_ON(ret);
  506. }
  507. /* insert any holes we need to create */
  508. if (inode->i_size < start_pos) {
  509. u64 last_pos_in_file;
  510. u64 hole_size;
  511. u64 mask = root->blocksize - 1;
  512. last_pos_in_file = (inode->i_size + mask) & ~mask;
  513. hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
  514. hole_size >>= inode->i_blkbits;
  515. if (last_pos_in_file < start_pos) {
  516. ret = btrfs_insert_file_extent(trans, root,
  517. inode->i_ino,
  518. last_pos_in_file,
  519. 0, 0, hole_size);
  520. }
  521. BUG_ON(ret);
  522. }
  523. /*
  524. * either allocate an extent for the new bytes or setup the key
  525. * to show we are doing inline data in the extent
  526. */
  527. if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
  528. pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  529. ret = btrfs_alloc_extent(trans, root, inode->i_ino,
  530. num_blocks, hint_block, (u64)-1,
  531. &ins, 1);
  532. BUG_ON(ret);
  533. ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
  534. start_pos, ins.objectid, ins.offset,
  535. ins.offset);
  536. BUG_ON(ret);
  537. } else {
  538. ins.offset = 0;
  539. ins.objectid = 0;
  540. }
  541. BUG_ON(ret);
  542. alloc_extent_start = ins.objectid;
  543. ret = btrfs_end_transaction(trans, root);
  544. mutex_unlock(&root->fs_info->fs_mutex);
  545. while(count > 0) {
  546. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  547. size_t write_bytes = min(count,
  548. (size_t)PAGE_CACHE_SIZE - offset);
  549. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  550. PAGE_CACHE_SHIFT;
  551. memset(pages, 0, sizeof(pages));
  552. ret = prepare_pages(root, file, pages, num_pages,
  553. pos, first_index, last_index,
  554. write_bytes, alloc_extent_start);
  555. BUG_ON(ret);
  556. /* FIXME blocks != pagesize */
  557. if (alloc_extent_start)
  558. alloc_extent_start += num_pages;
  559. ret = btrfs_copy_from_user(pos, num_pages,
  560. write_bytes, pages, buf);
  561. BUG_ON(ret);
  562. ret = dirty_and_release_pages(NULL, root, file, pages,
  563. num_pages, pos, write_bytes);
  564. BUG_ON(ret);
  565. btrfs_drop_pages(pages, num_pages);
  566. buf += write_bytes;
  567. count -= write_bytes;
  568. pos += write_bytes;
  569. num_written += write_bytes;
  570. balance_dirty_pages_ratelimited(inode->i_mapping);
  571. btrfs_btree_balance_dirty(root);
  572. cond_resched();
  573. }
  574. out_unlock:
  575. mutex_unlock(&inode->i_mutex);
  576. out:
  577. if (pinned[0])
  578. page_cache_release(pinned[0]);
  579. if (pinned[1])
  580. page_cache_release(pinned[1]);
  581. *ppos = pos;
  582. current->backing_dev_info = NULL;
  583. mark_inode_dirty(inode);
  584. return num_written ? num_written : err;
  585. }
  586. /*
  587. * FIXME, do this by stuffing the csum we want in the info hanging off
  588. * page->private. For now, verify file csums on read
  589. */
  590. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  591. unsigned long offset, unsigned long size)
  592. {
  593. char *kaddr;
  594. unsigned long left, count = desc->count;
  595. struct inode *inode = page->mapping->host;
  596. if (size > count)
  597. size = count;
  598. if (!PageChecked(page)) {
  599. /* FIXME, do it per block */
  600. struct btrfs_root *root = BTRFS_I(inode)->root;
  601. int ret;
  602. struct buffer_head *bh;
  603. if (page_has_buffers(page)) {
  604. bh = page_buffers(page);
  605. if (!buffer_mapped(bh)) {
  606. SetPageChecked(page);
  607. goto checked;
  608. }
  609. }
  610. ret = btrfs_csum_verify_file_block(root,
  611. page->mapping->host->i_ino,
  612. page->index << PAGE_CACHE_SHIFT,
  613. kmap(page), PAGE_CACHE_SIZE);
  614. if (ret) {
  615. if (ret != -ENOENT) {
  616. printk("failed to verify ino %lu page %lu ret %d\n",
  617. page->mapping->host->i_ino,
  618. page->index, ret);
  619. memset(page_address(page), 1, PAGE_CACHE_SIZE);
  620. flush_dcache_page(page);
  621. }
  622. }
  623. SetPageChecked(page);
  624. kunmap(page);
  625. }
  626. checked:
  627. /*
  628. * Faults on the destination of a read are common, so do it before
  629. * taking the kmap.
  630. */
  631. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  632. kaddr = kmap_atomic(page, KM_USER0);
  633. left = __copy_to_user_inatomic(desc->arg.buf,
  634. kaddr + offset, size);
  635. kunmap_atomic(kaddr, KM_USER0);
  636. if (left == 0)
  637. goto success;
  638. }
  639. /* Do it the slow way */
  640. kaddr = kmap(page);
  641. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  642. kunmap(page);
  643. if (left) {
  644. size -= left;
  645. desc->error = -EFAULT;
  646. }
  647. success:
  648. desc->count = count - size;
  649. desc->written += size;
  650. desc->arg.buf += size;
  651. return size;
  652. }
  653. /**
  654. * btrfs_file_aio_read - filesystem read routine, with a mod to csum verify
  655. * @iocb: kernel I/O control block
  656. * @iov: io vector request
  657. * @nr_segs: number of segments in the iovec
  658. * @pos: current file position
  659. */
  660. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  661. unsigned long nr_segs, loff_t pos)
  662. {
  663. struct file *filp = iocb->ki_filp;
  664. ssize_t retval;
  665. unsigned long seg;
  666. size_t count;
  667. loff_t *ppos = &iocb->ki_pos;
  668. count = 0;
  669. for (seg = 0; seg < nr_segs; seg++) {
  670. const struct iovec *iv = &iov[seg];
  671. /*
  672. * If any segment has a negative length, or the cumulative
  673. * length ever wraps negative then return -EINVAL.
  674. */
  675. count += iv->iov_len;
  676. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  677. return -EINVAL;
  678. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  679. continue;
  680. if (seg == 0)
  681. return -EFAULT;
  682. nr_segs = seg;
  683. count -= iv->iov_len; /* This segment is no good */
  684. break;
  685. }
  686. retval = 0;
  687. if (count) {
  688. for (seg = 0; seg < nr_segs; seg++) {
  689. read_descriptor_t desc;
  690. desc.written = 0;
  691. desc.arg.buf = iov[seg].iov_base;
  692. desc.count = iov[seg].iov_len;
  693. if (desc.count == 0)
  694. continue;
  695. desc.error = 0;
  696. do_generic_file_read(filp, ppos, &desc,
  697. btrfs_read_actor);
  698. retval += desc.written;
  699. if (desc.error) {
  700. retval = retval ?: desc.error;
  701. break;
  702. }
  703. }
  704. }
  705. return retval;
  706. }
  707. static int btrfs_sync_file(struct file *file,
  708. struct dentry *dentry, int datasync)
  709. {
  710. struct inode *inode = dentry->d_inode;
  711. struct btrfs_root *root = BTRFS_I(inode)->root;
  712. int ret;
  713. struct btrfs_trans_handle *trans;
  714. /*
  715. * FIXME, use inode generation number to check if we can skip the
  716. * commit
  717. */
  718. mutex_lock(&root->fs_info->fs_mutex);
  719. trans = btrfs_start_transaction(root, 1);
  720. if (!trans) {
  721. ret = -ENOMEM;
  722. goto out;
  723. }
  724. ret = btrfs_commit_transaction(trans, root);
  725. mutex_unlock(&root->fs_info->fs_mutex);
  726. out:
  727. return ret > 0 ? EIO : ret;
  728. }
  729. struct file_operations btrfs_file_operations = {
  730. .llseek = generic_file_llseek,
  731. .read = do_sync_read,
  732. .aio_read = btrfs_file_aio_read,
  733. .write = btrfs_file_write,
  734. .mmap = generic_file_mmap,
  735. .open = generic_file_open,
  736. .ioctl = btrfs_ioctl,
  737. .fsync = btrfs_sync_file,
  738. #ifdef CONFIG_COMPAT
  739. .compat_ioctl = btrfs_compat_ioctl,
  740. #endif
  741. };