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