file.c 21 KB

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