file.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 "ordered-data.h"
  37. #include "ioctl.h"
  38. #include "print-tree.h"
  39. #include "compat.h"
  40. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  41. struct page **prepared_pages,
  42. const char __user * buf)
  43. {
  44. long page_fault = 0;
  45. int i;
  46. int offset = pos & (PAGE_CACHE_SIZE - 1);
  47. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  48. size_t count = min_t(size_t,
  49. PAGE_CACHE_SIZE - offset, write_bytes);
  50. struct page *page = prepared_pages[i];
  51. fault_in_pages_readable(buf, count);
  52. /* Copy data from userspace to the current page */
  53. kmap(page);
  54. page_fault = __copy_from_user(page_address(page) + offset,
  55. buf, count);
  56. /* Flush processor's dcache for this page */
  57. flush_dcache_page(page);
  58. kunmap(page);
  59. buf += count;
  60. write_bytes -= count;
  61. if (page_fault)
  62. break;
  63. }
  64. return page_fault ? -EFAULT : 0;
  65. }
  66. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  67. {
  68. size_t i;
  69. for (i = 0; i < num_pages; i++) {
  70. if (!pages[i])
  71. break;
  72. unlock_page(pages[i]);
  73. mark_page_accessed(pages[i]);
  74. page_cache_release(pages[i]);
  75. }
  76. }
  77. static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
  78. struct btrfs_root *root, struct inode *inode,
  79. u64 offset, size_t size,
  80. struct page **pages, size_t page_offset,
  81. int num_pages)
  82. {
  83. struct btrfs_key key;
  84. struct btrfs_path *path;
  85. struct extent_buffer *leaf;
  86. char *kaddr;
  87. unsigned long ptr;
  88. struct btrfs_file_extent_item *ei;
  89. struct page *page;
  90. u32 datasize;
  91. int err = 0;
  92. int ret;
  93. int i;
  94. ssize_t cur_size;
  95. path = btrfs_alloc_path();
  96. if (!path)
  97. return -ENOMEM;
  98. btrfs_set_trans_block_group(trans, inode);
  99. key.objectid = inode->i_ino;
  100. key.offset = offset;
  101. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  102. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  103. if (ret < 0) {
  104. err = ret;
  105. goto fail;
  106. }
  107. if (ret == 1) {
  108. struct btrfs_key found_key;
  109. if (path->slots[0] == 0)
  110. goto insert;
  111. path->slots[0]--;
  112. leaf = path->nodes[0];
  113. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  114. if (found_key.objectid != inode->i_ino)
  115. goto insert;
  116. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  117. goto insert;
  118. ei = btrfs_item_ptr(leaf, path->slots[0],
  119. struct btrfs_file_extent_item);
  120. if (btrfs_file_extent_type(leaf, ei) !=
  121. BTRFS_FILE_EXTENT_INLINE) {
  122. goto insert;
  123. }
  124. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  125. ret = 0;
  126. }
  127. if (ret == 0) {
  128. u32 found_size;
  129. u64 found_end;
  130. leaf = path->nodes[0];
  131. ei = btrfs_item_ptr(leaf, path->slots[0],
  132. struct btrfs_file_extent_item);
  133. if (btrfs_file_extent_type(leaf, ei) !=
  134. BTRFS_FILE_EXTENT_INLINE) {
  135. err = ret;
  136. btrfs_print_leaf(root, leaf);
  137. printk("found wasn't inline offset %Lu inode %lu\n",
  138. offset, inode->i_ino);
  139. goto fail;
  140. }
  141. found_size = btrfs_file_extent_inline_len(leaf,
  142. btrfs_item_nr(leaf, path->slots[0]));
  143. found_end = key.offset + found_size;
  144. if (found_end < offset + size) {
  145. btrfs_release_path(root, path);
  146. ret = btrfs_search_slot(trans, root, &key, path,
  147. offset + size - found_end, 1);
  148. BUG_ON(ret != 0);
  149. ret = btrfs_extend_item(trans, root, path,
  150. offset + size - found_end);
  151. if (ret) {
  152. err = ret;
  153. goto fail;
  154. }
  155. leaf = path->nodes[0];
  156. ei = btrfs_item_ptr(leaf, path->slots[0],
  157. struct btrfs_file_extent_item);
  158. inode->i_blocks += (offset + size - found_end) >> 9;
  159. }
  160. if (found_end < offset) {
  161. ptr = btrfs_file_extent_inline_start(ei) + found_size;
  162. memset_extent_buffer(leaf, 0, ptr, offset - found_end);
  163. }
  164. } else {
  165. insert:
  166. btrfs_release_path(root, path);
  167. datasize = offset + size - key.offset;
  168. inode->i_blocks += datasize >> 9;
  169. datasize = btrfs_file_extent_calc_inline_size(datasize);
  170. ret = btrfs_insert_empty_item(trans, root, path, &key,
  171. datasize);
  172. if (ret) {
  173. err = ret;
  174. printk("got bad ret %d\n", ret);
  175. goto fail;
  176. }
  177. leaf = path->nodes[0];
  178. ei = btrfs_item_ptr(leaf, path->slots[0],
  179. struct btrfs_file_extent_item);
  180. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  181. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  182. }
  183. ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
  184. cur_size = size;
  185. i = 0;
  186. while (size > 0) {
  187. page = pages[i];
  188. kaddr = kmap_atomic(page, KM_USER0);
  189. cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
  190. write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
  191. kunmap_atomic(kaddr, KM_USER0);
  192. page_offset = 0;
  193. ptr += cur_size;
  194. size -= cur_size;
  195. if (i >= num_pages) {
  196. printk("i %d num_pages %d\n", i, num_pages);
  197. }
  198. i++;
  199. }
  200. btrfs_mark_buffer_dirty(leaf);
  201. fail:
  202. btrfs_free_path(path);
  203. return err;
  204. }
  205. static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
  206. struct btrfs_root *root,
  207. struct file *file,
  208. struct page **pages,
  209. size_t num_pages,
  210. loff_t pos,
  211. size_t write_bytes)
  212. {
  213. int err = 0;
  214. int i;
  215. struct inode *inode = fdentry(file)->d_inode;
  216. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  217. u64 hint_byte;
  218. u64 num_bytes;
  219. u64 start_pos;
  220. u64 end_of_last_block;
  221. u64 end_pos = pos + write_bytes;
  222. u64 inline_size;
  223. loff_t isize = i_size_read(inode);
  224. start_pos = pos & ~((u64)root->sectorsize - 1);
  225. num_bytes = (write_bytes + pos - start_pos +
  226. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  227. end_of_last_block = start_pos + num_bytes - 1;
  228. lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  229. trans = btrfs_start_transaction(root, 1);
  230. if (!trans) {
  231. err = -ENOMEM;
  232. goto out_unlock;
  233. }
  234. btrfs_set_trans_block_group(trans, inode);
  235. hint_byte = 0;
  236. if ((end_of_last_block & 4095) == 0) {
  237. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  238. }
  239. set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  240. /* FIXME...EIEIO, ENOSPC and more */
  241. /* insert any holes we need to create */
  242. if (isize < end_pos) {
  243. u64 last_pos_in_file;
  244. u64 hole_size;
  245. u64 mask = root->sectorsize - 1;
  246. last_pos_in_file = (isize + mask) & ~mask;
  247. hole_size = (end_pos - last_pos_in_file + mask) & ~mask;
  248. if (last_pos_in_file < end_pos) {
  249. err = btrfs_drop_extents(trans, root, inode,
  250. last_pos_in_file,
  251. last_pos_in_file + hole_size,
  252. last_pos_in_file,
  253. &hint_byte);
  254. if (err)
  255. goto failed;
  256. err = btrfs_insert_file_extent(trans, root,
  257. inode->i_ino,
  258. last_pos_in_file,
  259. 0, 0, hole_size, 0);
  260. btrfs_drop_extent_cache(inode, last_pos_in_file,
  261. last_pos_in_file + hole_size -1);
  262. btrfs_check_file(root, inode);
  263. }
  264. if (err)
  265. goto failed;
  266. }
  267. /*
  268. * either allocate an extent for the new bytes or setup the key
  269. * to show we are doing inline data in the extent
  270. */
  271. inline_size = end_pos;
  272. if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  273. inline_size > root->fs_info->max_inline ||
  274. (inline_size & (root->sectorsize -1)) == 0 ||
  275. inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  276. u64 last_end;
  277. for (i = 0; i < num_pages; i++) {
  278. struct page *p = pages[i];
  279. SetPageUptodate(p);
  280. set_page_dirty(p);
  281. }
  282. last_end = (u64)(pages[num_pages -1]->index) <<
  283. PAGE_CACHE_SHIFT;
  284. last_end += PAGE_CACHE_SIZE - 1;
  285. set_extent_delalloc(io_tree, start_pos, end_of_last_block,
  286. GFP_NOFS);
  287. btrfs_add_ordered_inode(inode);
  288. } else {
  289. u64 aligned_end;
  290. /* step one, delete the existing extents in this range */
  291. aligned_end = (pos + write_bytes + root->sectorsize - 1) &
  292. ~((u64)root->sectorsize - 1);
  293. err = btrfs_drop_extents(trans, root, inode, start_pos,
  294. aligned_end, aligned_end, &hint_byte);
  295. if (err)
  296. goto failed;
  297. if (isize > inline_size)
  298. inline_size = min_t(u64, isize, aligned_end);
  299. inline_size -= start_pos;
  300. err = insert_inline_extent(trans, root, inode, start_pos,
  301. inline_size, pages, 0, num_pages);
  302. btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1);
  303. BUG_ON(err);
  304. }
  305. if (end_pos > isize) {
  306. i_size_write(inode, end_pos);
  307. btrfs_update_inode(trans, root, inode);
  308. }
  309. failed:
  310. err = btrfs_end_transaction(trans, root);
  311. out_unlock:
  312. unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  313. return err;
  314. }
  315. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
  316. {
  317. struct extent_map *em;
  318. struct extent_map *split = NULL;
  319. struct extent_map *split2 = NULL;
  320. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  321. u64 len = end - start + 1;
  322. int ret;
  323. int testend = 1;
  324. if (end == (u64)-1) {
  325. len = (u64)-1;
  326. testend = 0;
  327. }
  328. while(1) {
  329. if (!split)
  330. split = alloc_extent_map(GFP_NOFS);
  331. if (!split2)
  332. split2 = alloc_extent_map(GFP_NOFS);
  333. spin_lock(&em_tree->lock);
  334. em = lookup_extent_mapping(em_tree, start, len);
  335. if (!em) {
  336. spin_unlock(&em_tree->lock);
  337. break;
  338. }
  339. remove_extent_mapping(em_tree, em);
  340. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  341. em->start < start) {
  342. split->start = em->start;
  343. split->len = start - em->start;
  344. split->block_start = em->block_start;
  345. split->bdev = em->bdev;
  346. split->flags = em->flags;
  347. ret = add_extent_mapping(em_tree, split);
  348. BUG_ON(ret);
  349. free_extent_map(split);
  350. split = split2;
  351. split2 = NULL;
  352. }
  353. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  354. testend && em->start + em->len > start + len) {
  355. u64 diff = start + len - em->start;
  356. split->start = start + len;
  357. split->len = em->start + em->len - (start + len);
  358. split->bdev = em->bdev;
  359. split->flags = em->flags;
  360. split->block_start = em->block_start + diff;
  361. ret = add_extent_mapping(em_tree, split);
  362. BUG_ON(ret);
  363. free_extent_map(split);
  364. split = NULL;
  365. }
  366. spin_unlock(&em_tree->lock);
  367. /* once for us */
  368. free_extent_map(em);
  369. /* once for the tree*/
  370. free_extent_map(em);
  371. }
  372. if (split)
  373. free_extent_map(split);
  374. if (split2)
  375. free_extent_map(split2);
  376. return 0;
  377. }
  378. int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
  379. {
  380. return 0;
  381. #if 0
  382. struct btrfs_path *path;
  383. struct btrfs_key found_key;
  384. struct extent_buffer *leaf;
  385. struct btrfs_file_extent_item *extent;
  386. u64 last_offset = 0;
  387. int nritems;
  388. int slot;
  389. int found_type;
  390. int ret;
  391. int err = 0;
  392. u64 extent_end = 0;
  393. path = btrfs_alloc_path();
  394. ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
  395. last_offset, 0);
  396. while(1) {
  397. nritems = btrfs_header_nritems(path->nodes[0]);
  398. if (path->slots[0] >= nritems) {
  399. ret = btrfs_next_leaf(root, path);
  400. if (ret)
  401. goto out;
  402. nritems = btrfs_header_nritems(path->nodes[0]);
  403. }
  404. slot = path->slots[0];
  405. leaf = path->nodes[0];
  406. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  407. if (found_key.objectid != inode->i_ino)
  408. break;
  409. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  410. goto out;
  411. if (found_key.offset < last_offset) {
  412. WARN_ON(1);
  413. btrfs_print_leaf(root, leaf);
  414. printk("inode %lu found offset %Lu expected %Lu\n",
  415. inode->i_ino, found_key.offset, last_offset);
  416. err = 1;
  417. goto out;
  418. }
  419. extent = btrfs_item_ptr(leaf, slot,
  420. struct btrfs_file_extent_item);
  421. found_type = btrfs_file_extent_type(leaf, extent);
  422. if (found_type == BTRFS_FILE_EXTENT_REG) {
  423. extent_end = found_key.offset +
  424. btrfs_file_extent_num_bytes(leaf, extent);
  425. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  426. struct btrfs_item *item;
  427. item = btrfs_item_nr(leaf, slot);
  428. extent_end = found_key.offset +
  429. btrfs_file_extent_inline_len(leaf, item);
  430. extent_end = (extent_end + root->sectorsize - 1) &
  431. ~((u64)root->sectorsize -1 );
  432. }
  433. last_offset = extent_end;
  434. path->slots[0]++;
  435. }
  436. if (0 && last_offset < inode->i_size) {
  437. WARN_ON(1);
  438. btrfs_print_leaf(root, leaf);
  439. printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
  440. last_offset, inode->i_size);
  441. err = 1;
  442. }
  443. out:
  444. btrfs_free_path(path);
  445. return err;
  446. #endif
  447. }
  448. /*
  449. * this is very complex, but the basic idea is to drop all extents
  450. * in the range start - end. hint_block is filled in with a block number
  451. * that would be a good hint to the block allocator for this file.
  452. *
  453. * If an extent intersects the range but is not entirely inside the range
  454. * it is either truncated or split. Anything entirely inside the range
  455. * is deleted from the tree.
  456. */
  457. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  458. struct btrfs_root *root, struct inode *inode,
  459. u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
  460. {
  461. u64 extent_end = 0;
  462. u64 search_start = start;
  463. struct extent_buffer *leaf;
  464. struct btrfs_file_extent_item *extent;
  465. struct btrfs_path *path;
  466. struct btrfs_key key;
  467. struct btrfs_file_extent_item old;
  468. int keep;
  469. int slot;
  470. int bookend;
  471. int found_type;
  472. int found_extent;
  473. int found_inline;
  474. int recow;
  475. int ret;
  476. btrfs_drop_extent_cache(inode, start, end - 1);
  477. path = btrfs_alloc_path();
  478. if (!path)
  479. return -ENOMEM;
  480. while(1) {
  481. recow = 0;
  482. btrfs_release_path(root, path);
  483. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  484. search_start, -1);
  485. if (ret < 0)
  486. goto out;
  487. if (ret > 0) {
  488. if (path->slots[0] == 0) {
  489. ret = 0;
  490. goto out;
  491. }
  492. path->slots[0]--;
  493. }
  494. next_slot:
  495. keep = 0;
  496. bookend = 0;
  497. found_extent = 0;
  498. found_inline = 0;
  499. extent = NULL;
  500. leaf = path->nodes[0];
  501. slot = path->slots[0];
  502. ret = 0;
  503. btrfs_item_key_to_cpu(leaf, &key, slot);
  504. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
  505. key.offset >= end) {
  506. goto out;
  507. }
  508. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  509. key.objectid != inode->i_ino) {
  510. goto out;
  511. }
  512. if (recow) {
  513. search_start = key.offset;
  514. continue;
  515. }
  516. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  517. extent = btrfs_item_ptr(leaf, slot,
  518. struct btrfs_file_extent_item);
  519. found_type = btrfs_file_extent_type(leaf, extent);
  520. if (found_type == BTRFS_FILE_EXTENT_REG) {
  521. extent_end =
  522. btrfs_file_extent_disk_bytenr(leaf,
  523. extent);
  524. if (extent_end)
  525. *hint_byte = extent_end;
  526. extent_end = key.offset +
  527. btrfs_file_extent_num_bytes(leaf, extent);
  528. found_extent = 1;
  529. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  530. struct btrfs_item *item;
  531. item = btrfs_item_nr(leaf, slot);
  532. found_inline = 1;
  533. extent_end = key.offset +
  534. btrfs_file_extent_inline_len(leaf, item);
  535. }
  536. } else {
  537. extent_end = search_start;
  538. }
  539. /* we found nothing we can drop */
  540. if ((!found_extent && !found_inline) ||
  541. search_start >= extent_end) {
  542. int nextret;
  543. u32 nritems;
  544. nritems = btrfs_header_nritems(leaf);
  545. if (slot >= nritems - 1) {
  546. nextret = btrfs_next_leaf(root, path);
  547. if (nextret)
  548. goto out;
  549. recow = 1;
  550. } else {
  551. path->slots[0]++;
  552. }
  553. goto next_slot;
  554. }
  555. if (found_inline) {
  556. u64 mask = root->sectorsize - 1;
  557. search_start = (extent_end + mask) & ~mask;
  558. } else
  559. search_start = extent_end;
  560. if (end <= extent_end && start >= key.offset && found_inline) {
  561. *hint_byte = EXTENT_MAP_INLINE;
  562. continue;
  563. }
  564. if (end < extent_end && end >= key.offset) {
  565. if (found_extent) {
  566. u64 disk_bytenr =
  567. btrfs_file_extent_disk_bytenr(leaf, extent);
  568. u64 disk_num_bytes =
  569. btrfs_file_extent_disk_num_bytes(leaf,
  570. extent);
  571. read_extent_buffer(leaf, &old,
  572. (unsigned long)extent,
  573. sizeof(old));
  574. if (disk_bytenr != 0) {
  575. ret = btrfs_inc_extent_ref(trans, root,
  576. disk_bytenr, disk_num_bytes,
  577. root->root_key.objectid,
  578. trans->transid,
  579. key.objectid, end);
  580. BUG_ON(ret);
  581. }
  582. }
  583. bookend = 1;
  584. if (found_inline && start <= key.offset)
  585. keep = 1;
  586. }
  587. /* truncate existing extent */
  588. if (start > key.offset) {
  589. u64 new_num;
  590. u64 old_num;
  591. keep = 1;
  592. WARN_ON(start & (root->sectorsize - 1));
  593. if (found_extent) {
  594. new_num = start - key.offset;
  595. old_num = btrfs_file_extent_num_bytes(leaf,
  596. extent);
  597. *hint_byte =
  598. btrfs_file_extent_disk_bytenr(leaf,
  599. extent);
  600. if (btrfs_file_extent_disk_bytenr(leaf,
  601. extent)) {
  602. dec_i_blocks(inode, old_num - new_num);
  603. }
  604. btrfs_set_file_extent_num_bytes(leaf, extent,
  605. new_num);
  606. btrfs_mark_buffer_dirty(leaf);
  607. } else if (key.offset < inline_limit &&
  608. (end > extent_end) &&
  609. (inline_limit < extent_end)) {
  610. u32 new_size;
  611. new_size = btrfs_file_extent_calc_inline_size(
  612. inline_limit - key.offset);
  613. dec_i_blocks(inode, (extent_end - key.offset) -
  614. (inline_limit - key.offset));
  615. btrfs_truncate_item(trans, root, path,
  616. new_size, 1);
  617. }
  618. }
  619. /* delete the entire extent */
  620. if (!keep) {
  621. u64 disk_bytenr = 0;
  622. u64 disk_num_bytes = 0;
  623. u64 extent_num_bytes = 0;
  624. u64 root_gen;
  625. u64 root_owner;
  626. root_gen = btrfs_header_generation(leaf);
  627. root_owner = btrfs_header_owner(leaf);
  628. if (found_extent) {
  629. disk_bytenr =
  630. btrfs_file_extent_disk_bytenr(leaf,
  631. extent);
  632. disk_num_bytes =
  633. btrfs_file_extent_disk_num_bytes(leaf,
  634. extent);
  635. extent_num_bytes =
  636. btrfs_file_extent_num_bytes(leaf, extent);
  637. *hint_byte =
  638. btrfs_file_extent_disk_bytenr(leaf,
  639. extent);
  640. }
  641. ret = btrfs_del_item(trans, root, path);
  642. /* TODO update progress marker and return */
  643. BUG_ON(ret);
  644. btrfs_release_path(root, path);
  645. extent = NULL;
  646. if (found_extent && disk_bytenr != 0) {
  647. dec_i_blocks(inode, extent_num_bytes);
  648. ret = btrfs_free_extent(trans, root,
  649. disk_bytenr,
  650. disk_num_bytes,
  651. root_owner,
  652. root_gen, inode->i_ino,
  653. key.offset, 0);
  654. }
  655. BUG_ON(ret);
  656. if (!bookend && search_start >= end) {
  657. ret = 0;
  658. goto out;
  659. }
  660. if (!bookend)
  661. continue;
  662. }
  663. if (bookend && found_inline && start <= key.offset) {
  664. u32 new_size;
  665. new_size = btrfs_file_extent_calc_inline_size(
  666. extent_end - end);
  667. dec_i_blocks(inode, (extent_end - key.offset) -
  668. (extent_end - end));
  669. btrfs_truncate_item(trans, root, path, new_size, 0);
  670. }
  671. /* create bookend, splitting the extent in two */
  672. if (bookend && found_extent) {
  673. struct btrfs_key ins;
  674. ins.objectid = inode->i_ino;
  675. ins.offset = end;
  676. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  677. btrfs_release_path(root, path);
  678. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  679. sizeof(*extent));
  680. leaf = path->nodes[0];
  681. if (ret) {
  682. btrfs_print_leaf(root, leaf);
  683. 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);
  684. }
  685. BUG_ON(ret);
  686. extent = btrfs_item_ptr(leaf, path->slots[0],
  687. struct btrfs_file_extent_item);
  688. write_extent_buffer(leaf, &old,
  689. (unsigned long)extent, sizeof(old));
  690. btrfs_set_file_extent_offset(leaf, extent,
  691. le64_to_cpu(old.offset) + end - key.offset);
  692. WARN_ON(le64_to_cpu(old.num_bytes) <
  693. (extent_end - end));
  694. btrfs_set_file_extent_num_bytes(leaf, extent,
  695. extent_end - end);
  696. btrfs_set_file_extent_type(leaf, extent,
  697. BTRFS_FILE_EXTENT_REG);
  698. btrfs_mark_buffer_dirty(path->nodes[0]);
  699. if (le64_to_cpu(old.disk_bytenr) != 0) {
  700. inode->i_blocks +=
  701. btrfs_file_extent_num_bytes(leaf,
  702. extent) >> 9;
  703. }
  704. ret = 0;
  705. goto out;
  706. }
  707. }
  708. out:
  709. btrfs_free_path(path);
  710. btrfs_check_file(root, inode);
  711. return ret;
  712. }
  713. /*
  714. * this gets pages into the page cache and locks them down
  715. */
  716. static int prepare_pages(struct btrfs_root *root, struct file *file,
  717. struct page **pages, size_t num_pages,
  718. loff_t pos, unsigned long first_index,
  719. unsigned long last_index, size_t write_bytes)
  720. {
  721. int i;
  722. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  723. struct inode *inode = fdentry(file)->d_inode;
  724. int err = 0;
  725. u64 start_pos;
  726. start_pos = pos & ~((u64)root->sectorsize - 1);
  727. memset(pages, 0, num_pages * sizeof(struct page *));
  728. for (i = 0; i < num_pages; i++) {
  729. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  730. if (!pages[i]) {
  731. err = -ENOMEM;
  732. BUG_ON(1);
  733. }
  734. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  735. ClearPageDirty(pages[i]);
  736. #else
  737. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  738. #endif
  739. wait_on_page_writeback(pages[i]);
  740. set_page_extent_mapped(pages[i]);
  741. WARN_ON(!PageLocked(pages[i]));
  742. }
  743. if (start_pos < inode->i_size) {
  744. u64 last_pos;
  745. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  746. lock_extent(&BTRFS_I(inode)->io_tree,
  747. start_pos, last_pos - 1, GFP_NOFS);
  748. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  749. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
  750. GFP_NOFS);
  751. unlock_extent(&BTRFS_I(inode)->io_tree,
  752. start_pos, last_pos - 1, GFP_NOFS);
  753. }
  754. return 0;
  755. }
  756. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  757. size_t count, loff_t *ppos)
  758. {
  759. loff_t pos;
  760. loff_t start_pos;
  761. ssize_t num_written = 0;
  762. ssize_t err = 0;
  763. int ret = 0;
  764. struct inode *inode = fdentry(file)->d_inode;
  765. struct btrfs_root *root = BTRFS_I(inode)->root;
  766. struct page **pages = NULL;
  767. int nrptrs;
  768. struct page *pinned[2];
  769. unsigned long first_index;
  770. unsigned long last_index;
  771. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  772. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  773. pinned[0] = NULL;
  774. pinned[1] = NULL;
  775. pos = *ppos;
  776. start_pos = pos;
  777. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  778. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  779. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  780. if (err)
  781. goto out_nolock;
  782. if (count == 0)
  783. goto out_nolock;
  784. #ifdef REMOVE_SUID_PATH
  785. err = remove_suid(&file->f_path);
  786. #else
  787. err = remove_suid(fdentry(file));
  788. #endif
  789. if (err)
  790. goto out_nolock;
  791. file_update_time(file);
  792. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  793. mutex_lock(&inode->i_mutex);
  794. first_index = pos >> PAGE_CACHE_SHIFT;
  795. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  796. /*
  797. * if this is a nodatasum mount, force summing off for the inode
  798. * all the time. That way a later mount with summing on won't
  799. * get confused
  800. */
  801. if (btrfs_test_opt(root, NODATASUM))
  802. btrfs_set_flag(inode, NODATASUM);
  803. /*
  804. * there are lots of better ways to do this, but this code
  805. * makes sure the first and last page in the file range are
  806. * up to date and ready for cow
  807. */
  808. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  809. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  810. if (!PageUptodate(pinned[0])) {
  811. ret = btrfs_readpage(NULL, pinned[0]);
  812. BUG_ON(ret);
  813. wait_on_page_locked(pinned[0]);
  814. } else {
  815. unlock_page(pinned[0]);
  816. }
  817. }
  818. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  819. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  820. if (!PageUptodate(pinned[1])) {
  821. ret = btrfs_readpage(NULL, pinned[1]);
  822. BUG_ON(ret);
  823. wait_on_page_locked(pinned[1]);
  824. } else {
  825. unlock_page(pinned[1]);
  826. }
  827. }
  828. while(count > 0) {
  829. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  830. size_t write_bytes = min(count, nrptrs *
  831. (size_t)PAGE_CACHE_SIZE -
  832. offset);
  833. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  834. PAGE_CACHE_SHIFT;
  835. WARN_ON(num_pages > nrptrs);
  836. memset(pages, 0, sizeof(pages));
  837. ret = btrfs_check_free_space(root, write_bytes, 0);
  838. if (ret)
  839. goto out;
  840. ret = prepare_pages(root, file, pages, num_pages,
  841. pos, first_index, last_index,
  842. write_bytes);
  843. if (ret)
  844. goto out;
  845. ret = btrfs_copy_from_user(pos, num_pages,
  846. write_bytes, pages, buf);
  847. if (ret) {
  848. btrfs_drop_pages(pages, num_pages);
  849. goto out;
  850. }
  851. ret = dirty_and_release_pages(NULL, root, file, pages,
  852. num_pages, pos, write_bytes);
  853. btrfs_drop_pages(pages, num_pages);
  854. if (ret)
  855. goto out;
  856. buf += write_bytes;
  857. count -= write_bytes;
  858. pos += write_bytes;
  859. num_written += write_bytes;
  860. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  861. if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  862. btrfs_btree_balance_dirty(root, 1);
  863. btrfs_throttle(root);
  864. cond_resched();
  865. }
  866. out:
  867. mutex_unlock(&inode->i_mutex);
  868. out_nolock:
  869. kfree(pages);
  870. if (pinned[0])
  871. page_cache_release(pinned[0]);
  872. if (pinned[1])
  873. page_cache_release(pinned[1]);
  874. *ppos = pos;
  875. if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  876. err = sync_page_range(inode, inode->i_mapping,
  877. start_pos, num_written);
  878. if (err < 0)
  879. num_written = err;
  880. } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
  881. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
  882. do_sync_file_range(file, start_pos,
  883. start_pos + num_written - 1,
  884. SYNC_FILE_RANGE_WRITE |
  885. SYNC_FILE_RANGE_WAIT_AFTER);
  886. #else
  887. do_sync_mapping_range(inode->i_mapping, start_pos,
  888. start_pos + num_written - 1,
  889. SYNC_FILE_RANGE_WRITE |
  890. SYNC_FILE_RANGE_WAIT_AFTER);
  891. #endif
  892. invalidate_mapping_pages(inode->i_mapping,
  893. start_pos >> PAGE_CACHE_SHIFT,
  894. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  895. }
  896. current->backing_dev_info = NULL;
  897. btrfs_ordered_throttle(root, inode);
  898. return num_written ? num_written : err;
  899. }
  900. int btrfs_release_file(struct inode * inode, struct file * filp)
  901. {
  902. btrfs_del_ordered_inode(inode, 0);
  903. if (filp->private_data)
  904. btrfs_ioctl_trans_end(filp);
  905. return 0;
  906. }
  907. static int btrfs_sync_file(struct file *file,
  908. struct dentry *dentry, int datasync)
  909. {
  910. struct inode *inode = dentry->d_inode;
  911. struct btrfs_root *root = BTRFS_I(inode)->root;
  912. int ret = 0;
  913. struct btrfs_trans_handle *trans;
  914. /*
  915. * check the transaction that last modified this inode
  916. * and see if its already been committed
  917. */
  918. if (!BTRFS_I(inode)->last_trans)
  919. goto out;
  920. mutex_lock(&root->fs_info->trans_mutex);
  921. if (BTRFS_I(inode)->last_trans <=
  922. root->fs_info->last_trans_committed) {
  923. BTRFS_I(inode)->last_trans = 0;
  924. mutex_unlock(&root->fs_info->trans_mutex);
  925. goto out;
  926. }
  927. mutex_unlock(&root->fs_info->trans_mutex);
  928. /*
  929. * ok we haven't committed the transaction yet, lets do a commit
  930. */
  931. if (file->private_data)
  932. btrfs_ioctl_trans_end(file);
  933. trans = btrfs_start_transaction(root, 1);
  934. if (!trans) {
  935. ret = -ENOMEM;
  936. goto out;
  937. }
  938. ret = btrfs_commit_transaction(trans, root);
  939. out:
  940. return ret > 0 ? EIO : ret;
  941. }
  942. static struct vm_operations_struct btrfs_file_vm_ops = {
  943. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  944. .nopage = filemap_nopage,
  945. .populate = filemap_populate,
  946. #else
  947. .fault = filemap_fault,
  948. #endif
  949. .page_mkwrite = btrfs_page_mkwrite,
  950. };
  951. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  952. {
  953. vma->vm_ops = &btrfs_file_vm_ops;
  954. file_accessed(filp);
  955. return 0;
  956. }
  957. struct file_operations btrfs_file_operations = {
  958. .llseek = generic_file_llseek,
  959. .read = do_sync_read,
  960. .aio_read = generic_file_aio_read,
  961. .splice_read = generic_file_splice_read,
  962. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  963. .sendfile = generic_file_sendfile,
  964. #endif
  965. .write = btrfs_file_write,
  966. .mmap = btrfs_file_mmap,
  967. .open = generic_file_open,
  968. .release = btrfs_release_file,
  969. .fsync = btrfs_sync_file,
  970. .unlocked_ioctl = btrfs_ioctl,
  971. #ifdef CONFIG_COMPAT
  972. .compat_ioctl = btrfs_ioctl,
  973. #endif
  974. };