file.c 29 KB

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