file.c 28 KB

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