file.c 29 KB

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