file.c 19 KB

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