file.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include <linux/time.h>
  22. #include <linux/init.h>
  23. #include <linux/string.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mpage.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/version.h>
  32. #include "ctree.h"
  33. #include "disk-io.h"
  34. #include "transaction.h"
  35. #include "btrfs_inode.h"
  36. #include "ioctl.h"
  37. #include "print-tree.h"
  38. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  39. struct page **prepared_pages,
  40. const char __user * buf)
  41. {
  42. long page_fault = 0;
  43. int i;
  44. int offset = pos & (PAGE_CACHE_SIZE - 1);
  45. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  46. size_t count = min_t(size_t,
  47. PAGE_CACHE_SIZE - offset, write_bytes);
  48. struct page *page = prepared_pages[i];
  49. fault_in_pages_readable(buf, count);
  50. /* Copy data from userspace to the current page */
  51. kmap(page);
  52. page_fault = __copy_from_user(page_address(page) + offset,
  53. buf, count);
  54. /* Flush processor's dcache for this page */
  55. flush_dcache_page(page);
  56. kunmap(page);
  57. buf += count;
  58. write_bytes -= count;
  59. if (page_fault)
  60. break;
  61. }
  62. return page_fault ? -EFAULT : 0;
  63. }
  64. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  65. {
  66. size_t i;
  67. for (i = 0; i < num_pages; i++) {
  68. if (!pages[i])
  69. break;
  70. unlock_page(pages[i]);
  71. mark_page_accessed(pages[i]);
  72. page_cache_release(pages[i]);
  73. }
  74. }
  75. static int insert_inline_extent(struct btrfs_trans_handle *trans,
  76. struct btrfs_root *root, struct inode *inode,
  77. u64 offset, size_t size,
  78. struct page **pages, size_t page_offset,
  79. int num_pages)
  80. {
  81. struct btrfs_key key;
  82. struct btrfs_path *path;
  83. struct extent_buffer *leaf;
  84. char *kaddr;
  85. unsigned long ptr;
  86. struct btrfs_file_extent_item *ei;
  87. struct page *page;
  88. u32 datasize;
  89. int err = 0;
  90. int ret;
  91. int i;
  92. ssize_t cur_size;
  93. path = btrfs_alloc_path();
  94. if (!path)
  95. return -ENOMEM;
  96. btrfs_set_trans_block_group(trans, inode);
  97. key.objectid = inode->i_ino;
  98. key.offset = offset;
  99. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  100. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  101. if (ret < 0) {
  102. err = ret;
  103. goto fail;
  104. }
  105. if (ret == 1) {
  106. path->slots[0]--;
  107. leaf = path->nodes[0];
  108. ei = btrfs_item_ptr(leaf, path->slots[0],
  109. struct btrfs_file_extent_item);
  110. if (btrfs_file_extent_type(leaf, ei) !=
  111. BTRFS_FILE_EXTENT_INLINE) {
  112. goto insert;
  113. }
  114. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  115. ret = 0;
  116. }
  117. if (ret == 0) {
  118. u32 found_size;
  119. u64 found_end;
  120. leaf = path->nodes[0];
  121. ei = btrfs_item_ptr(leaf, path->slots[0],
  122. struct btrfs_file_extent_item);
  123. if (btrfs_file_extent_type(leaf, ei) !=
  124. BTRFS_FILE_EXTENT_INLINE) {
  125. err = ret;
  126. btrfs_print_leaf(root, leaf);
  127. printk("found wasn't inline offset %Lu inode %lu\n",
  128. offset, inode->i_ino);
  129. goto fail;
  130. }
  131. found_size = btrfs_file_extent_inline_len(leaf,
  132. btrfs_item_nr(leaf, path->slots[0]));
  133. found_end = key.offset + found_size;
  134. if (found_end < offset + size) {
  135. btrfs_release_path(root, path);
  136. ret = btrfs_search_slot(trans, root, &key, path,
  137. offset + size - found_end, 1);
  138. BUG_ON(ret != 0);
  139. ret = btrfs_extend_item(trans, root, path,
  140. offset + size - found_end);
  141. if (ret) {
  142. err = ret;
  143. goto fail;
  144. }
  145. leaf = path->nodes[0];
  146. ei = btrfs_item_ptr(leaf, path->slots[0],
  147. struct btrfs_file_extent_item);
  148. }
  149. if (found_end < offset) {
  150. ptr = btrfs_file_extent_inline_start(ei) + found_size;
  151. memset_extent_buffer(leaf, 0, ptr, offset - found_end);
  152. }
  153. } else {
  154. insert:
  155. btrfs_release_path(root, path);
  156. datasize = offset + size - key.offset;
  157. datasize = btrfs_file_extent_calc_inline_size(datasize);
  158. ret = btrfs_insert_empty_item(trans, root, path, &key,
  159. datasize);
  160. if (ret) {
  161. err = ret;
  162. printk("got bad ret %d\n", ret);
  163. goto fail;
  164. }
  165. leaf = path->nodes[0];
  166. ei = btrfs_item_ptr(leaf, path->slots[0],
  167. struct btrfs_file_extent_item);
  168. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  169. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  170. }
  171. ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
  172. cur_size = size;
  173. i = 0;
  174. while (size > 0) {
  175. page = pages[i];
  176. kaddr = kmap_atomic(page, KM_USER0);
  177. cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
  178. write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
  179. kunmap_atomic(kaddr, KM_USER0);
  180. page_offset = 0;
  181. ptr += cur_size;
  182. size -= cur_size;
  183. if (i >= num_pages) {
  184. printk("i %d num_pages %d\n", i, num_pages);
  185. }
  186. i++;
  187. }
  188. btrfs_mark_buffer_dirty(leaf);
  189. fail:
  190. btrfs_free_path(path);
  191. return err;
  192. }
  193. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  194. struct btrfs_root *root,
  195. struct file *file,
  196. struct page **pages,
  197. size_t num_pages,
  198. loff_t pos,
  199. size_t write_bytes)
  200. {
  201. int err = 0;
  202. int i;
  203. struct inode *inode = file->f_path.dentry->d_inode;
  204. struct extent_map *em;
  205. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  206. u64 hint_byte;
  207. u64 num_bytes;
  208. u64 start_pos;
  209. u64 end_of_last_block;
  210. u64 end_pos = pos + write_bytes;
  211. u32 inline_size;
  212. loff_t isize = i_size_read(inode);
  213. em = alloc_extent_map(GFP_NOFS);
  214. if (!em)
  215. return -ENOMEM;
  216. em->bdev = inode->i_sb->s_bdev;
  217. start_pos = pos & ~((u64)root->sectorsize - 1);
  218. num_bytes = (write_bytes + pos - start_pos +
  219. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  220. down_read(&BTRFS_I(inode)->root->snap_sem);
  221. end_of_last_block = start_pos + num_bytes - 1;
  222. lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  223. mutex_lock(&root->fs_info->fs_mutex);
  224. trans = btrfs_start_transaction(root, 1);
  225. if (!trans) {
  226. err = -ENOMEM;
  227. goto out_unlock;
  228. }
  229. btrfs_set_trans_block_group(trans, inode);
  230. inode->i_blocks += num_bytes >> 9;
  231. hint_byte = 0;
  232. if ((end_of_last_block & 4095) == 0) {
  233. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  234. }
  235. set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  236. /* FIXME...EIEIO, ENOSPC and more */
  237. /* insert any holes we need to create */
  238. if (inode->i_size < start_pos) {
  239. u64 last_pos_in_file;
  240. u64 hole_size;
  241. u64 mask = root->sectorsize - 1;
  242. last_pos_in_file = (isize + mask) & ~mask;
  243. hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
  244. if (last_pos_in_file < start_pos) {
  245. err = btrfs_drop_extents(trans, root, inode,
  246. last_pos_in_file,
  247. last_pos_in_file + hole_size,
  248. last_pos_in_file,
  249. &hint_byte);
  250. if (err)
  251. goto failed;
  252. err = btrfs_insert_file_extent(trans, root,
  253. inode->i_ino,
  254. last_pos_in_file,
  255. 0, 0, hole_size);
  256. }
  257. if (err)
  258. goto failed;
  259. }
  260. /*
  261. * either allocate an extent for the new bytes or setup the key
  262. * to show we are doing inline data in the extent
  263. */
  264. inline_size = end_pos;
  265. if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  266. inline_size > 8192 ||
  267. inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  268. u64 last_end;
  269. for (i = 0; i < num_pages; i++) {
  270. struct page *p = pages[i];
  271. SetPageUptodate(p);
  272. set_page_dirty(p);
  273. }
  274. last_end = (u64)(pages[num_pages -1]->index) <<
  275. PAGE_CACHE_SHIFT;
  276. last_end += PAGE_CACHE_SIZE - 1;
  277. set_extent_delalloc(em_tree, start_pos, end_of_last_block,
  278. GFP_NOFS);
  279. } else {
  280. u64 aligned_end;
  281. /* step one, delete the existing extents in this range */
  282. aligned_end = (pos + write_bytes + root->sectorsize - 1) &
  283. ~((u64)root->sectorsize - 1);
  284. err = btrfs_drop_extents(trans, root, inode, start_pos,
  285. aligned_end, end_pos, &hint_byte);
  286. if (err)
  287. goto failed;
  288. err = insert_inline_extent(trans, root, inode, start_pos,
  289. end_pos - start_pos, pages, 0,
  290. num_pages);
  291. BUG_ON(err);
  292. }
  293. if (end_pos > isize) {
  294. i_size_write(inode, end_pos);
  295. btrfs_update_inode(trans, root, inode);
  296. }
  297. failed:
  298. err = btrfs_end_transaction(trans, root);
  299. out_unlock:
  300. mutex_unlock(&root->fs_info->fs_mutex);
  301. unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  302. free_extent_map(em);
  303. up_read(&BTRFS_I(inode)->root->snap_sem);
  304. return err;
  305. }
  306. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
  307. {
  308. struct extent_map *em;
  309. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  310. while(1) {
  311. em = lookup_extent_mapping(em_tree, start, end);
  312. if (!em)
  313. break;
  314. remove_extent_mapping(em_tree, em);
  315. /* once for us */
  316. free_extent_map(em);
  317. /* once for the tree*/
  318. free_extent_map(em);
  319. }
  320. return 0;
  321. }
  322. /*
  323. * this is very complex, but the basic idea is to drop all extents
  324. * in the range start - end. hint_block is filled in with a block number
  325. * that would be a good hint to the block allocator for this file.
  326. *
  327. * If an extent intersects the range but is not entirely inside the range
  328. * it is either truncated or split. Anything entirely inside the range
  329. * is deleted from the tree.
  330. */
  331. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  332. struct btrfs_root *root, struct inode *inode,
  333. u64 start, u64 end, u64 inline_end, u64 *hint_byte)
  334. {
  335. int ret;
  336. struct btrfs_key key;
  337. struct extent_buffer *leaf;
  338. int slot;
  339. struct btrfs_file_extent_item *extent;
  340. u64 extent_end = 0;
  341. int keep;
  342. struct btrfs_file_extent_item old;
  343. struct btrfs_path *path;
  344. u64 search_start = start;
  345. int bookend;
  346. int found_type;
  347. int found_extent;
  348. int found_inline;
  349. int recow;
  350. btrfs_drop_extent_cache(inode, start, end - 1);
  351. path = btrfs_alloc_path();
  352. if (!path)
  353. return -ENOMEM;
  354. while(1) {
  355. recow = 0;
  356. btrfs_release_path(root, path);
  357. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  358. search_start, -1);
  359. if (ret < 0)
  360. goto out;
  361. if (ret > 0) {
  362. if (path->slots[0] == 0) {
  363. ret = 0;
  364. goto out;
  365. }
  366. path->slots[0]--;
  367. }
  368. next_slot:
  369. keep = 0;
  370. bookend = 0;
  371. found_extent = 0;
  372. found_inline = 0;
  373. extent = NULL;
  374. leaf = path->nodes[0];
  375. slot = path->slots[0];
  376. ret = 0;
  377. btrfs_item_key_to_cpu(leaf, &key, slot);
  378. if (key.offset >= end || key.objectid != inode->i_ino) {
  379. goto out;
  380. }
  381. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
  382. goto out;
  383. }
  384. if (recow) {
  385. search_start = key.offset;
  386. continue;
  387. }
  388. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  389. extent = btrfs_item_ptr(leaf, slot,
  390. struct btrfs_file_extent_item);
  391. found_type = btrfs_file_extent_type(leaf, extent);
  392. if (found_type == BTRFS_FILE_EXTENT_REG) {
  393. extent_end = key.offset +
  394. btrfs_file_extent_num_bytes(leaf, extent);
  395. found_extent = 1;
  396. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  397. struct btrfs_item *item;
  398. item = btrfs_item_nr(leaf, slot);
  399. found_inline = 1;
  400. extent_end = key.offset +
  401. btrfs_file_extent_inline_len(leaf, item);
  402. }
  403. } else {
  404. extent_end = search_start;
  405. }
  406. /* we found nothing we can drop */
  407. if ((!found_extent && !found_inline) ||
  408. search_start >= extent_end) {
  409. int nextret;
  410. u32 nritems;
  411. nritems = btrfs_header_nritems(leaf);
  412. if (slot >= nritems - 1) {
  413. nextret = btrfs_next_leaf(root, path);
  414. if (nextret)
  415. goto out;
  416. recow = 1;
  417. } else {
  418. path->slots[0]++;
  419. }
  420. goto next_slot;
  421. }
  422. /* FIXME, there's only one inline extent allowed right now */
  423. if (found_inline) {
  424. u64 mask = root->sectorsize - 1;
  425. search_start = (extent_end + mask) & ~mask;
  426. } else
  427. search_start = extent_end;
  428. if (end < extent_end && end >= key.offset) {
  429. if (found_extent) {
  430. u64 disk_bytenr =
  431. btrfs_file_extent_disk_bytenr(leaf, extent);
  432. u64 disk_num_bytes =
  433. btrfs_file_extent_disk_num_bytes(leaf,
  434. extent);
  435. read_extent_buffer(leaf, &old,
  436. (unsigned long)extent,
  437. sizeof(old));
  438. if (disk_bytenr != 0) {
  439. ret = btrfs_inc_extent_ref(trans, root,
  440. disk_bytenr, disk_num_bytes);
  441. BUG_ON(ret);
  442. }
  443. }
  444. if (!found_inline)
  445. bookend = 1;
  446. }
  447. /* truncate existing extent */
  448. if (start > key.offset) {
  449. u64 new_num;
  450. u64 old_num;
  451. keep = 1;
  452. WARN_ON(start & (root->sectorsize - 1));
  453. if (found_extent) {
  454. new_num = start - key.offset;
  455. old_num = btrfs_file_extent_num_bytes(leaf,
  456. extent);
  457. *hint_byte =
  458. btrfs_file_extent_disk_bytenr(leaf,
  459. extent);
  460. if (btrfs_file_extent_disk_bytenr(leaf,
  461. extent)) {
  462. inode->i_blocks -=
  463. (old_num - new_num) >> 9;
  464. }
  465. btrfs_set_file_extent_num_bytes(leaf, extent,
  466. new_num);
  467. btrfs_mark_buffer_dirty(leaf);
  468. } else if (end > extent_end &&
  469. key.offset < inline_end &&
  470. inline_end < extent_end) {
  471. u32 new_size;
  472. new_size = btrfs_file_extent_calc_inline_size(
  473. inline_end - key.offset);
  474. btrfs_truncate_item(trans, root, path,
  475. new_size);
  476. }
  477. }
  478. /* delete the entire extent */
  479. if (!keep) {
  480. u64 disk_bytenr = 0;
  481. u64 disk_num_bytes = 0;
  482. u64 extent_num_bytes = 0;
  483. if (found_extent) {
  484. disk_bytenr =
  485. btrfs_file_extent_disk_bytenr(leaf,
  486. extent);
  487. disk_num_bytes =
  488. btrfs_file_extent_disk_num_bytes(leaf,
  489. extent);
  490. extent_num_bytes =
  491. btrfs_file_extent_num_bytes(leaf, extent);
  492. *hint_byte =
  493. btrfs_file_extent_disk_bytenr(leaf,
  494. extent);
  495. }
  496. ret = btrfs_del_item(trans, root, path);
  497. /* TODO update progress marker and return */
  498. BUG_ON(ret);
  499. btrfs_release_path(root, path);
  500. extent = NULL;
  501. if (found_extent && disk_bytenr != 0) {
  502. inode->i_blocks -= extent_num_bytes >> 9;
  503. ret = btrfs_free_extent(trans, root,
  504. disk_bytenr,
  505. disk_num_bytes, 0);
  506. }
  507. BUG_ON(ret);
  508. if (!bookend && search_start >= end) {
  509. ret = 0;
  510. goto out;
  511. }
  512. if (!bookend)
  513. continue;
  514. }
  515. /* create bookend, splitting the extent in two */
  516. if (bookend && found_extent) {
  517. struct btrfs_key ins;
  518. ins.objectid = inode->i_ino;
  519. ins.offset = end;
  520. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  521. btrfs_release_path(root, path);
  522. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  523. sizeof(*extent));
  524. leaf = path->nodes[0];
  525. if (ret) {
  526. btrfs_print_leaf(root, leaf);
  527. printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep);
  528. }
  529. BUG_ON(ret);
  530. extent = btrfs_item_ptr(leaf, path->slots[0],
  531. struct btrfs_file_extent_item);
  532. write_extent_buffer(leaf, &old,
  533. (unsigned long)extent, sizeof(old));
  534. btrfs_set_file_extent_offset(leaf, extent,
  535. le64_to_cpu(old.offset) + end - key.offset);
  536. WARN_ON(le64_to_cpu(old.num_bytes) <
  537. (extent_end - end));
  538. btrfs_set_file_extent_num_bytes(leaf, extent,
  539. extent_end - end);
  540. btrfs_set_file_extent_type(leaf, extent,
  541. BTRFS_FILE_EXTENT_REG);
  542. btrfs_mark_buffer_dirty(path->nodes[0]);
  543. if (le64_to_cpu(old.disk_bytenr) != 0) {
  544. inode->i_blocks +=
  545. btrfs_file_extent_num_bytes(leaf,
  546. extent) >> 9;
  547. }
  548. ret = 0;
  549. goto out;
  550. }
  551. }
  552. out:
  553. btrfs_free_path(path);
  554. return ret;
  555. }
  556. /*
  557. * this gets pages into the page cache and locks them down
  558. */
  559. static int prepare_pages(struct btrfs_root *root,
  560. struct file *file,
  561. struct page **pages,
  562. size_t num_pages,
  563. loff_t pos,
  564. unsigned long first_index,
  565. unsigned long last_index,
  566. size_t write_bytes)
  567. {
  568. int i;
  569. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  570. struct inode *inode = file->f_path.dentry->d_inode;
  571. int err = 0;
  572. u64 start_pos;
  573. start_pos = pos & ~((u64)root->sectorsize - 1);
  574. memset(pages, 0, num_pages * sizeof(struct page *));
  575. for (i = 0; i < num_pages; i++) {
  576. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  577. if (!pages[i]) {
  578. err = -ENOMEM;
  579. BUG_ON(1);
  580. }
  581. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  582. wait_on_page_writeback(pages[i]);
  583. set_page_extent_mapped(pages[i]);
  584. WARN_ON(!PageLocked(pages[i]));
  585. }
  586. return 0;
  587. }
  588. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  589. size_t count, loff_t *ppos)
  590. {
  591. loff_t pos;
  592. loff_t start_pos;
  593. ssize_t num_written = 0;
  594. ssize_t err = 0;
  595. int ret = 0;
  596. struct inode *inode = file->f_path.dentry->d_inode;
  597. struct btrfs_root *root = BTRFS_I(inode)->root;
  598. struct page **pages = NULL;
  599. int nrptrs;
  600. struct page *pinned[2];
  601. unsigned long first_index;
  602. unsigned long last_index;
  603. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  604. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  605. pinned[0] = NULL;
  606. pinned[1] = NULL;
  607. if (file->f_flags & O_DIRECT)
  608. return -EINVAL;
  609. pos = *ppos;
  610. start_pos = pos;
  611. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  612. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  613. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  614. if (err)
  615. goto out;
  616. if (count == 0)
  617. goto out;
  618. err = remove_suid(file->f_path.dentry);
  619. if (err)
  620. goto out;
  621. file_update_time(file);
  622. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  623. mutex_lock(&inode->i_mutex);
  624. first_index = pos >> PAGE_CACHE_SHIFT;
  625. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  626. /*
  627. * there are lots of better ways to do this, but this code
  628. * makes sure the first and last page in the file range are
  629. * up to date and ready for cow
  630. */
  631. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  632. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  633. if (!PageUptodate(pinned[0])) {
  634. ret = btrfs_readpage(NULL, pinned[0]);
  635. BUG_ON(ret);
  636. wait_on_page_locked(pinned[0]);
  637. } else {
  638. unlock_page(pinned[0]);
  639. }
  640. }
  641. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  642. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  643. if (!PageUptodate(pinned[1])) {
  644. ret = btrfs_readpage(NULL, pinned[1]);
  645. BUG_ON(ret);
  646. wait_on_page_locked(pinned[1]);
  647. } else {
  648. unlock_page(pinned[1]);
  649. }
  650. }
  651. while(count > 0) {
  652. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  653. size_t write_bytes = min(count, nrptrs *
  654. (size_t)PAGE_CACHE_SIZE -
  655. offset);
  656. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  657. PAGE_CACHE_SHIFT;
  658. WARN_ON(num_pages > nrptrs);
  659. memset(pages, 0, sizeof(pages));
  660. ret = prepare_pages(root, file, pages, num_pages,
  661. pos, first_index, last_index,
  662. write_bytes);
  663. if (ret)
  664. goto out;
  665. ret = btrfs_copy_from_user(pos, num_pages,
  666. write_bytes, pages, buf);
  667. if (ret) {
  668. btrfs_drop_pages(pages, num_pages);
  669. goto out;
  670. }
  671. ret = dirty_and_release_pages(NULL, root, file, pages,
  672. num_pages, pos, write_bytes);
  673. btrfs_drop_pages(pages, num_pages);
  674. if (ret)
  675. goto out;
  676. buf += write_bytes;
  677. count -= write_bytes;
  678. pos += write_bytes;
  679. num_written += write_bytes;
  680. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  681. btrfs_btree_balance_dirty(root, 1);
  682. cond_resched();
  683. }
  684. mutex_unlock(&inode->i_mutex);
  685. out:
  686. kfree(pages);
  687. if (pinned[0])
  688. page_cache_release(pinned[0]);
  689. if (pinned[1])
  690. page_cache_release(pinned[1]);
  691. *ppos = pos;
  692. if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  693. err = sync_page_range(inode, inode->i_mapping,
  694. start_pos, num_written);
  695. if (err < 0)
  696. num_written = err;
  697. }
  698. current->backing_dev_info = NULL;
  699. return num_written ? num_written : err;
  700. }
  701. static int btrfs_sync_file(struct file *file,
  702. struct dentry *dentry, int datasync)
  703. {
  704. struct inode *inode = dentry->d_inode;
  705. struct btrfs_root *root = BTRFS_I(inode)->root;
  706. int ret = 0;
  707. struct btrfs_trans_handle *trans;
  708. /*
  709. * check the transaction that last modified this inode
  710. * and see if its already been committed
  711. */
  712. mutex_lock(&root->fs_info->fs_mutex);
  713. if (!BTRFS_I(inode)->last_trans)
  714. goto out;
  715. mutex_lock(&root->fs_info->trans_mutex);
  716. if (BTRFS_I(inode)->last_trans <=
  717. root->fs_info->last_trans_committed) {
  718. BTRFS_I(inode)->last_trans = 0;
  719. mutex_unlock(&root->fs_info->trans_mutex);
  720. goto out;
  721. }
  722. mutex_unlock(&root->fs_info->trans_mutex);
  723. /*
  724. * ok we haven't committed the transaction yet, lets do a commit
  725. */
  726. trans = btrfs_start_transaction(root, 1);
  727. if (!trans) {
  728. ret = -ENOMEM;
  729. goto out;
  730. }
  731. ret = btrfs_commit_transaction(trans, root);
  732. out:
  733. mutex_unlock(&root->fs_info->fs_mutex);
  734. return ret > 0 ? EIO : ret;
  735. }
  736. static struct vm_operations_struct btrfs_file_vm_ops = {
  737. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  738. .nopage = filemap_nopage,
  739. .populate = filemap_populate,
  740. #else
  741. .fault = filemap_fault,
  742. #endif
  743. .page_mkwrite = btrfs_page_mkwrite,
  744. };
  745. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  746. {
  747. vma->vm_ops = &btrfs_file_vm_ops;
  748. file_accessed(filp);
  749. return 0;
  750. }
  751. struct file_operations btrfs_file_operations = {
  752. .llseek = generic_file_llseek,
  753. .read = do_sync_read,
  754. .aio_read = generic_file_aio_read,
  755. .write = btrfs_file_write,
  756. .mmap = btrfs_file_mmap,
  757. .open = generic_file_open,
  758. .fsync = btrfs_sync_file,
  759. .unlocked_ioctl = btrfs_ioctl,
  760. #ifdef CONFIG_COMPAT
  761. .compat_ioctl = btrfs_ioctl,
  762. #endif
  763. };