file.c 20 KB

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