file.c 29 KB

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