file.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 = pages[num_pages -1]->index << PAGE_CACHE_SHIFT;
  275. last_end += PAGE_CACHE_SIZE - 1;
  276. set_extent_delalloc(em_tree, start_pos, end_of_last_block,
  277. GFP_NOFS);
  278. } else {
  279. u64 aligned_end;
  280. /* step one, delete the existing extents in this range */
  281. aligned_end = (pos + write_bytes + root->sectorsize - 1) &
  282. ~((u64)root->sectorsize - 1);
  283. err = btrfs_drop_extents(trans, root, inode, start_pos,
  284. aligned_end, end_pos, &hint_byte);
  285. if (err)
  286. goto failed;
  287. err = insert_inline_extent(trans, root, inode, start_pos,
  288. end_pos - start_pos, pages, 0,
  289. num_pages);
  290. BUG_ON(err);
  291. }
  292. if (end_pos > isize) {
  293. i_size_write(inode, end_pos);
  294. btrfs_update_inode(trans, root, inode);
  295. }
  296. failed:
  297. err = btrfs_end_transaction(trans, root);
  298. out_unlock:
  299. mutex_unlock(&root->fs_info->fs_mutex);
  300. unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
  301. free_extent_map(em);
  302. up_read(&BTRFS_I(inode)->root->snap_sem);
  303. return err;
  304. }
  305. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
  306. {
  307. struct extent_map *em;
  308. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  309. while(1) {
  310. em = lookup_extent_mapping(em_tree, start, end);
  311. if (!em)
  312. break;
  313. remove_extent_mapping(em_tree, em);
  314. /* once for us */
  315. free_extent_map(em);
  316. /* once for the tree*/
  317. free_extent_map(em);
  318. }
  319. return 0;
  320. }
  321. /*
  322. * this is very complex, but the basic idea is to drop all extents
  323. * in the range start - end. hint_block is filled in with a block number
  324. * that would be a good hint to the block allocator for this file.
  325. *
  326. * If an extent intersects the range but is not entirely inside the range
  327. * it is either truncated or split. Anything entirely inside the range
  328. * is deleted from the tree.
  329. */
  330. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  331. struct btrfs_root *root, struct inode *inode,
  332. u64 start, u64 end, u64 inline_end, u64 *hint_byte)
  333. {
  334. int ret;
  335. struct btrfs_key key;
  336. struct extent_buffer *leaf;
  337. int slot;
  338. struct btrfs_file_extent_item *extent;
  339. u64 extent_end = 0;
  340. int keep;
  341. struct btrfs_file_extent_item old;
  342. struct btrfs_path *path;
  343. u64 search_start = start;
  344. int bookend;
  345. int found_type;
  346. int found_extent;
  347. int found_inline;
  348. int recow;
  349. btrfs_drop_extent_cache(inode, start, end - 1);
  350. path = btrfs_alloc_path();
  351. if (!path)
  352. return -ENOMEM;
  353. while(1) {
  354. recow = 0;
  355. btrfs_release_path(root, path);
  356. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  357. search_start, -1);
  358. if (ret < 0)
  359. goto out;
  360. if (ret > 0) {
  361. if (path->slots[0] == 0) {
  362. ret = 0;
  363. goto out;
  364. }
  365. path->slots[0]--;
  366. }
  367. next_slot:
  368. keep = 0;
  369. bookend = 0;
  370. found_extent = 0;
  371. found_inline = 0;
  372. extent = NULL;
  373. leaf = path->nodes[0];
  374. slot = path->slots[0];
  375. ret = 0;
  376. btrfs_item_key_to_cpu(leaf, &key, slot);
  377. if (key.offset >= end || key.objectid != inode->i_ino) {
  378. goto out;
  379. }
  380. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
  381. goto out;
  382. }
  383. if (recow) {
  384. search_start = key.offset;
  385. continue;
  386. }
  387. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  388. extent = btrfs_item_ptr(leaf, slot,
  389. struct btrfs_file_extent_item);
  390. found_type = btrfs_file_extent_type(leaf, extent);
  391. if (found_type == BTRFS_FILE_EXTENT_REG) {
  392. extent_end = key.offset +
  393. btrfs_file_extent_num_bytes(leaf, extent);
  394. found_extent = 1;
  395. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  396. struct btrfs_item *item;
  397. item = btrfs_item_nr(leaf, slot);
  398. found_inline = 1;
  399. extent_end = key.offset +
  400. btrfs_file_extent_inline_len(leaf, item);
  401. }
  402. } else {
  403. extent_end = search_start;
  404. }
  405. /* we found nothing we can drop */
  406. if ((!found_extent && !found_inline) ||
  407. search_start >= extent_end) {
  408. int nextret;
  409. u32 nritems;
  410. nritems = btrfs_header_nritems(leaf);
  411. if (slot >= nritems - 1) {
  412. nextret = btrfs_next_leaf(root, path);
  413. if (nextret)
  414. goto out;
  415. recow = 1;
  416. } else {
  417. path->slots[0]++;
  418. }
  419. goto next_slot;
  420. }
  421. /* FIXME, there's only one inline extent allowed right now */
  422. if (found_inline) {
  423. u64 mask = root->sectorsize - 1;
  424. search_start = (extent_end + mask) & ~mask;
  425. } else
  426. search_start = extent_end;
  427. if (end < extent_end && end >= key.offset) {
  428. if (found_extent) {
  429. u64 disk_bytenr =
  430. btrfs_file_extent_disk_bytenr(leaf, extent);
  431. u64 disk_num_bytes =
  432. btrfs_file_extent_disk_num_bytes(leaf,
  433. extent);
  434. read_extent_buffer(leaf, &old,
  435. (unsigned long)extent,
  436. sizeof(old));
  437. if (disk_bytenr != 0) {
  438. ret = btrfs_inc_extent_ref(trans, root,
  439. disk_bytenr, disk_num_bytes);
  440. BUG_ON(ret);
  441. }
  442. }
  443. if (!found_inline)
  444. bookend = 1;
  445. }
  446. /* truncate existing extent */
  447. if (start > key.offset) {
  448. u64 new_num;
  449. u64 old_num;
  450. keep = 1;
  451. WARN_ON(start & (root->sectorsize - 1));
  452. if (found_extent) {
  453. new_num = start - key.offset;
  454. old_num = btrfs_file_extent_num_bytes(leaf,
  455. extent);
  456. *hint_byte =
  457. btrfs_file_extent_disk_bytenr(leaf,
  458. extent);
  459. if (btrfs_file_extent_disk_bytenr(leaf,
  460. extent)) {
  461. inode->i_blocks -=
  462. (old_num - new_num) >> 9;
  463. }
  464. btrfs_set_file_extent_num_bytes(leaf, extent,
  465. new_num);
  466. btrfs_mark_buffer_dirty(leaf);
  467. } else if (end > extent_end &&
  468. key.offset < inline_end &&
  469. inline_end < extent_end) {
  470. u32 new_size;
  471. new_size = btrfs_file_extent_calc_inline_size(
  472. inline_end - key.offset);
  473. btrfs_truncate_item(trans, root, path,
  474. new_size);
  475. }
  476. }
  477. /* delete the entire extent */
  478. if (!keep) {
  479. u64 disk_bytenr = 0;
  480. u64 disk_num_bytes = 0;
  481. u64 extent_num_bytes = 0;
  482. if (found_extent) {
  483. disk_bytenr =
  484. btrfs_file_extent_disk_bytenr(leaf,
  485. extent);
  486. disk_num_bytes =
  487. btrfs_file_extent_disk_num_bytes(leaf,
  488. extent);
  489. extent_num_bytes =
  490. btrfs_file_extent_num_bytes(leaf, extent);
  491. *hint_byte =
  492. btrfs_file_extent_disk_bytenr(leaf,
  493. extent);
  494. }
  495. ret = btrfs_del_item(trans, root, path);
  496. /* TODO update progress marker and return */
  497. BUG_ON(ret);
  498. btrfs_release_path(root, path);
  499. extent = NULL;
  500. if (found_extent && disk_bytenr != 0) {
  501. inode->i_blocks -= extent_num_bytes >> 9;
  502. ret = btrfs_free_extent(trans, root,
  503. disk_bytenr,
  504. disk_num_bytes, 0);
  505. }
  506. BUG_ON(ret);
  507. if (!bookend && search_start >= end) {
  508. ret = 0;
  509. goto out;
  510. }
  511. if (!bookend)
  512. continue;
  513. }
  514. /* create bookend, splitting the extent in two */
  515. if (bookend && found_extent) {
  516. struct btrfs_key ins;
  517. ins.objectid = inode->i_ino;
  518. ins.offset = end;
  519. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  520. btrfs_release_path(root, path);
  521. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  522. sizeof(*extent));
  523. leaf = path->nodes[0];
  524. if (ret) {
  525. btrfs_print_leaf(root, leaf);
  526. 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);
  527. }
  528. BUG_ON(ret);
  529. extent = btrfs_item_ptr(leaf, path->slots[0],
  530. struct btrfs_file_extent_item);
  531. write_extent_buffer(leaf, &old,
  532. (unsigned long)extent, sizeof(old));
  533. btrfs_set_file_extent_offset(leaf, extent,
  534. le64_to_cpu(old.offset) + end - key.offset);
  535. WARN_ON(le64_to_cpu(old.num_bytes) <
  536. (extent_end - end));
  537. btrfs_set_file_extent_num_bytes(leaf, extent,
  538. extent_end - end);
  539. btrfs_set_file_extent_type(leaf, extent,
  540. BTRFS_FILE_EXTENT_REG);
  541. btrfs_mark_buffer_dirty(path->nodes[0]);
  542. if (le64_to_cpu(old.disk_bytenr) != 0) {
  543. inode->i_blocks +=
  544. btrfs_file_extent_num_bytes(leaf,
  545. extent) >> 9;
  546. }
  547. ret = 0;
  548. goto out;
  549. }
  550. }
  551. out:
  552. btrfs_free_path(path);
  553. return ret;
  554. }
  555. /*
  556. * this gets pages into the page cache and locks them down
  557. */
  558. static int prepare_pages(struct btrfs_root *root,
  559. struct file *file,
  560. struct page **pages,
  561. size_t num_pages,
  562. loff_t pos,
  563. unsigned long first_index,
  564. unsigned long last_index,
  565. size_t write_bytes)
  566. {
  567. int i;
  568. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  569. struct inode *inode = file->f_path.dentry->d_inode;
  570. int err = 0;
  571. u64 start_pos;
  572. start_pos = pos & ~((u64)root->sectorsize - 1);
  573. memset(pages, 0, num_pages * sizeof(struct page *));
  574. for (i = 0; i < num_pages; i++) {
  575. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  576. if (!pages[i]) {
  577. err = -ENOMEM;
  578. BUG_ON(1);
  579. }
  580. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  581. wait_on_page_writeback(pages[i]);
  582. set_page_extent_mapped(pages[i]);
  583. WARN_ON(!PageLocked(pages[i]));
  584. }
  585. return 0;
  586. }
  587. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  588. size_t count, loff_t *ppos)
  589. {
  590. loff_t pos;
  591. size_t num_written = 0;
  592. int err = 0;
  593. int ret = 0;
  594. struct inode *inode = file->f_path.dentry->d_inode;
  595. struct btrfs_root *root = BTRFS_I(inode)->root;
  596. struct page **pages = NULL;
  597. int nrptrs;
  598. struct page *pinned[2];
  599. unsigned long first_index;
  600. unsigned long last_index;
  601. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  602. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  603. pinned[0] = NULL;
  604. pinned[1] = NULL;
  605. if (file->f_flags & O_DIRECT)
  606. return -EINVAL;
  607. pos = *ppos;
  608. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  609. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  610. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  611. if (err)
  612. goto out;
  613. if (count == 0)
  614. goto out;
  615. err = remove_suid(file->f_path.dentry);
  616. if (err)
  617. goto out;
  618. file_update_time(file);
  619. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  620. mutex_lock(&inode->i_mutex);
  621. first_index = pos >> PAGE_CACHE_SHIFT;
  622. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  623. /*
  624. * there are lots of better ways to do this, but this code
  625. * makes sure the first and last page in the file range are
  626. * up to date and ready for cow
  627. */
  628. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  629. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  630. if (!PageUptodate(pinned[0])) {
  631. ret = btrfs_readpage(NULL, pinned[0]);
  632. BUG_ON(ret);
  633. wait_on_page_locked(pinned[0]);
  634. } else {
  635. unlock_page(pinned[0]);
  636. }
  637. }
  638. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  639. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  640. if (!PageUptodate(pinned[1])) {
  641. ret = btrfs_readpage(NULL, pinned[1]);
  642. BUG_ON(ret);
  643. wait_on_page_locked(pinned[1]);
  644. } else {
  645. unlock_page(pinned[1]);
  646. }
  647. }
  648. while(count > 0) {
  649. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  650. size_t write_bytes = min(count, nrptrs *
  651. (size_t)PAGE_CACHE_SIZE -
  652. offset);
  653. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  654. PAGE_CACHE_SHIFT;
  655. WARN_ON(num_pages > nrptrs);
  656. memset(pages, 0, sizeof(pages));
  657. ret = prepare_pages(root, file, pages, num_pages,
  658. pos, first_index, last_index,
  659. write_bytes);
  660. if (ret)
  661. goto out;
  662. ret = btrfs_copy_from_user(pos, num_pages,
  663. write_bytes, pages, buf);
  664. if (ret) {
  665. btrfs_drop_pages(pages, num_pages);
  666. goto out;
  667. }
  668. ret = dirty_and_release_pages(NULL, root, file, pages,
  669. num_pages, pos, write_bytes);
  670. btrfs_drop_pages(pages, num_pages);
  671. if (ret)
  672. goto out;
  673. buf += write_bytes;
  674. count -= write_bytes;
  675. pos += write_bytes;
  676. num_written += write_bytes;
  677. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  678. btrfs_btree_balance_dirty(root, 1);
  679. cond_resched();
  680. }
  681. mutex_unlock(&inode->i_mutex);
  682. out:
  683. kfree(pages);
  684. if (pinned[0])
  685. page_cache_release(pinned[0]);
  686. if (pinned[1])
  687. page_cache_release(pinned[1]);
  688. *ppos = pos;
  689. current->backing_dev_info = NULL;
  690. return num_written ? num_written : err;
  691. }
  692. static int btrfs_sync_file(struct file *file,
  693. struct dentry *dentry, int datasync)
  694. {
  695. struct inode *inode = dentry->d_inode;
  696. struct btrfs_root *root = BTRFS_I(inode)->root;
  697. int ret = 0;
  698. struct btrfs_trans_handle *trans;
  699. /*
  700. * check the transaction that last modified this inode
  701. * and see if its already been committed
  702. */
  703. mutex_lock(&root->fs_info->fs_mutex);
  704. if (!BTRFS_I(inode)->last_trans)
  705. goto out;
  706. mutex_lock(&root->fs_info->trans_mutex);
  707. if (BTRFS_I(inode)->last_trans <=
  708. root->fs_info->last_trans_committed) {
  709. BTRFS_I(inode)->last_trans = 0;
  710. mutex_unlock(&root->fs_info->trans_mutex);
  711. goto out;
  712. }
  713. mutex_unlock(&root->fs_info->trans_mutex);
  714. /*
  715. * ok we haven't committed the transaction yet, lets do a commit
  716. */
  717. trans = btrfs_start_transaction(root, 1);
  718. if (!trans) {
  719. ret = -ENOMEM;
  720. goto out;
  721. }
  722. ret = btrfs_commit_transaction(trans, root);
  723. out:
  724. mutex_unlock(&root->fs_info->fs_mutex);
  725. return ret > 0 ? EIO : ret;
  726. }
  727. static struct vm_operations_struct btrfs_file_vm_ops = {
  728. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  729. .nopage = filemap_nopage,
  730. .populate = filemap_populate,
  731. #else
  732. .fault = filemap_fault,
  733. #endif
  734. .page_mkwrite = btrfs_page_mkwrite,
  735. };
  736. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  737. {
  738. vma->vm_ops = &btrfs_file_vm_ops;
  739. file_accessed(filp);
  740. return 0;
  741. }
  742. struct file_operations btrfs_file_operations = {
  743. .llseek = generic_file_llseek,
  744. .read = do_sync_read,
  745. .aio_read = generic_file_aio_read,
  746. .write = btrfs_file_write,
  747. .mmap = btrfs_file_mmap,
  748. .open = generic_file_open,
  749. .fsync = btrfs_sync_file,
  750. .unlocked_ioctl = btrfs_ioctl,
  751. #ifdef CONFIG_COMPAT
  752. .compat_ioctl = btrfs_ioctl,
  753. #endif
  754. };