file.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. struct extent_buffer *leaf;
  484. struct btrfs_file_extent_item *extent;
  485. struct btrfs_path *path;
  486. struct btrfs_key key;
  487. struct btrfs_file_extent_item old;
  488. int keep;
  489. int slot;
  490. int bookend;
  491. int found_type;
  492. int found_extent;
  493. int found_inline;
  494. int recow;
  495. int ret;
  496. btrfs_drop_extent_cache(inode, start, end - 1);
  497. path = btrfs_alloc_path();
  498. if (!path)
  499. return -ENOMEM;
  500. while(1) {
  501. recow = 0;
  502. btrfs_release_path(root, path);
  503. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  504. search_start, -1);
  505. if (ret < 0)
  506. goto out;
  507. if (ret > 0) {
  508. if (path->slots[0] == 0) {
  509. ret = 0;
  510. goto out;
  511. }
  512. path->slots[0]--;
  513. }
  514. next_slot:
  515. keep = 0;
  516. bookend = 0;
  517. found_extent = 0;
  518. found_inline = 0;
  519. extent = NULL;
  520. leaf = path->nodes[0];
  521. slot = path->slots[0];
  522. ret = 0;
  523. btrfs_item_key_to_cpu(leaf, &key, slot);
  524. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
  525. key.offset >= end) {
  526. goto out;
  527. }
  528. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  529. key.objectid != inode->i_ino) {
  530. goto out;
  531. }
  532. if (recow) {
  533. search_start = key.offset;
  534. continue;
  535. }
  536. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  537. extent = btrfs_item_ptr(leaf, slot,
  538. struct btrfs_file_extent_item);
  539. found_type = btrfs_file_extent_type(leaf, extent);
  540. if (found_type == BTRFS_FILE_EXTENT_REG) {
  541. extent_end =
  542. btrfs_file_extent_disk_bytenr(leaf,
  543. extent);
  544. if (extent_end)
  545. *hint_byte = extent_end;
  546. extent_end = key.offset +
  547. btrfs_file_extent_num_bytes(leaf, extent);
  548. found_extent = 1;
  549. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  550. struct btrfs_item *item;
  551. item = btrfs_item_nr(leaf, slot);
  552. found_inline = 1;
  553. extent_end = key.offset +
  554. btrfs_file_extent_inline_len(leaf, item);
  555. }
  556. } else {
  557. extent_end = search_start;
  558. }
  559. /* we found nothing we can drop */
  560. if ((!found_extent && !found_inline) ||
  561. search_start >= extent_end) {
  562. int nextret;
  563. u32 nritems;
  564. nritems = btrfs_header_nritems(leaf);
  565. if (slot >= nritems - 1) {
  566. nextret = btrfs_next_leaf(root, path);
  567. if (nextret)
  568. goto out;
  569. recow = 1;
  570. } else {
  571. path->slots[0]++;
  572. }
  573. goto next_slot;
  574. }
  575. if (found_inline) {
  576. u64 mask = root->sectorsize - 1;
  577. search_start = (extent_end + mask) & ~mask;
  578. } else
  579. search_start = extent_end;
  580. if (end <= extent_end && start >= key.offset && found_inline) {
  581. *hint_byte = EXTENT_MAP_INLINE;
  582. continue;
  583. }
  584. if (end < extent_end && end >= key.offset) {
  585. if (found_extent) {
  586. u64 disk_bytenr =
  587. btrfs_file_extent_disk_bytenr(leaf, extent);
  588. u64 disk_num_bytes =
  589. btrfs_file_extent_disk_num_bytes(leaf,
  590. extent);
  591. read_extent_buffer(leaf, &old,
  592. (unsigned long)extent,
  593. sizeof(old));
  594. if (disk_bytenr != 0) {
  595. ret = btrfs_inc_extent_ref(trans, root,
  596. disk_bytenr, disk_num_bytes,
  597. root->root_key.objectid,
  598. trans->transid,
  599. key.objectid, end);
  600. BUG_ON(ret);
  601. }
  602. }
  603. bookend = 1;
  604. if (found_inline && start <= key.offset)
  605. keep = 1;
  606. }
  607. /* truncate existing extent */
  608. if (start > key.offset) {
  609. u64 new_num;
  610. u64 old_num;
  611. keep = 1;
  612. WARN_ON(start & (root->sectorsize - 1));
  613. if (found_extent) {
  614. new_num = start - key.offset;
  615. old_num = btrfs_file_extent_num_bytes(leaf,
  616. extent);
  617. *hint_byte =
  618. btrfs_file_extent_disk_bytenr(leaf,
  619. extent);
  620. if (btrfs_file_extent_disk_bytenr(leaf,
  621. extent)) {
  622. dec_i_blocks(inode, old_num - new_num);
  623. }
  624. btrfs_set_file_extent_num_bytes(leaf, extent,
  625. new_num);
  626. btrfs_mark_buffer_dirty(leaf);
  627. } else if (key.offset < inline_limit &&
  628. (end > extent_end) &&
  629. (inline_limit < extent_end)) {
  630. u32 new_size;
  631. new_size = btrfs_file_extent_calc_inline_size(
  632. inline_limit - key.offset);
  633. dec_i_blocks(inode, (extent_end - key.offset) -
  634. (inline_limit - key.offset));
  635. btrfs_truncate_item(trans, root, path,
  636. new_size, 1);
  637. }
  638. }
  639. /* delete the entire extent */
  640. if (!keep) {
  641. u64 disk_bytenr = 0;
  642. u64 disk_num_bytes = 0;
  643. u64 extent_num_bytes = 0;
  644. u64 root_gen;
  645. u64 root_owner;
  646. root_gen = btrfs_header_generation(leaf);
  647. root_owner = btrfs_header_owner(leaf);
  648. if (found_extent) {
  649. disk_bytenr =
  650. btrfs_file_extent_disk_bytenr(leaf,
  651. extent);
  652. disk_num_bytes =
  653. btrfs_file_extent_disk_num_bytes(leaf,
  654. extent);
  655. extent_num_bytes =
  656. btrfs_file_extent_num_bytes(leaf, extent);
  657. *hint_byte =
  658. btrfs_file_extent_disk_bytenr(leaf,
  659. extent);
  660. }
  661. ret = btrfs_del_item(trans, root, path);
  662. /* TODO update progress marker and return */
  663. BUG_ON(ret);
  664. btrfs_release_path(root, path);
  665. extent = NULL;
  666. if (found_extent && disk_bytenr != 0) {
  667. dec_i_blocks(inode, extent_num_bytes);
  668. ret = btrfs_free_extent(trans, root,
  669. disk_bytenr,
  670. disk_num_bytes,
  671. root_owner,
  672. root_gen, inode->i_ino,
  673. key.offset, 0);
  674. }
  675. BUG_ON(ret);
  676. if (!bookend && search_start >= end) {
  677. ret = 0;
  678. goto out;
  679. }
  680. if (!bookend)
  681. continue;
  682. }
  683. if (bookend && found_inline && start <= key.offset) {
  684. u32 new_size;
  685. new_size = btrfs_file_extent_calc_inline_size(
  686. extent_end - end);
  687. dec_i_blocks(inode, (extent_end - key.offset) -
  688. (extent_end - end));
  689. btrfs_truncate_item(trans, root, path, new_size, 0);
  690. }
  691. /* create bookend, splitting the extent in two */
  692. if (bookend && found_extent) {
  693. struct btrfs_key ins;
  694. ins.objectid = inode->i_ino;
  695. ins.offset = end;
  696. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  697. btrfs_release_path(root, path);
  698. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  699. sizeof(*extent));
  700. leaf = path->nodes[0];
  701. if (ret) {
  702. btrfs_print_leaf(root, leaf);
  703. 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);
  704. }
  705. BUG_ON(ret);
  706. extent = btrfs_item_ptr(leaf, path->slots[0],
  707. struct btrfs_file_extent_item);
  708. write_extent_buffer(leaf, &old,
  709. (unsigned long)extent, sizeof(old));
  710. btrfs_set_file_extent_offset(leaf, extent,
  711. le64_to_cpu(old.offset) + end - key.offset);
  712. WARN_ON(le64_to_cpu(old.num_bytes) <
  713. (extent_end - end));
  714. btrfs_set_file_extent_num_bytes(leaf, extent,
  715. extent_end - end);
  716. btrfs_set_file_extent_type(leaf, extent,
  717. BTRFS_FILE_EXTENT_REG);
  718. btrfs_mark_buffer_dirty(path->nodes[0]);
  719. if (le64_to_cpu(old.disk_bytenr) != 0) {
  720. inode->i_blocks +=
  721. btrfs_file_extent_num_bytes(leaf,
  722. extent) >> 9;
  723. }
  724. ret = 0;
  725. goto out;
  726. }
  727. }
  728. out:
  729. btrfs_free_path(path);
  730. btrfs_check_file(root, inode);
  731. return ret;
  732. }
  733. /*
  734. * this gets pages into the page cache and locks them down
  735. */
  736. static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
  737. struct page **pages, size_t num_pages,
  738. loff_t pos, unsigned long first_index,
  739. unsigned long last_index, size_t write_bytes)
  740. {
  741. int i;
  742. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  743. struct inode *inode = fdentry(file)->d_inode;
  744. int err = 0;
  745. u64 start_pos;
  746. u64 last_pos;
  747. start_pos = pos & ~((u64)root->sectorsize - 1);
  748. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  749. memset(pages, 0, num_pages * sizeof(struct page *));
  750. again:
  751. for (i = 0; i < num_pages; i++) {
  752. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  753. if (!pages[i]) {
  754. err = -ENOMEM;
  755. BUG_ON(1);
  756. }
  757. wait_on_page_writeback(pages[i]);
  758. }
  759. if (start_pos < inode->i_size) {
  760. struct btrfs_ordered_extent *ordered;
  761. lock_extent(&BTRFS_I(inode)->io_tree,
  762. start_pos, last_pos - 1, GFP_NOFS);
  763. ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
  764. if (ordered &&
  765. ordered->file_offset + ordered->len > start_pos &&
  766. ordered->file_offset < last_pos) {
  767. btrfs_put_ordered_extent(ordered);
  768. unlock_extent(&BTRFS_I(inode)->io_tree,
  769. start_pos, last_pos - 1, GFP_NOFS);
  770. for (i = 0; i < num_pages; i++) {
  771. unlock_page(pages[i]);
  772. page_cache_release(pages[i]);
  773. }
  774. btrfs_wait_ordered_range(inode, start_pos,
  775. last_pos - start_pos);
  776. goto again;
  777. }
  778. if (ordered)
  779. btrfs_put_ordered_extent(ordered);
  780. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  781. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
  782. GFP_NOFS);
  783. unlock_extent(&BTRFS_I(inode)->io_tree,
  784. start_pos, last_pos - 1, GFP_NOFS);
  785. }
  786. for (i = 0; i < num_pages; i++) {
  787. clear_page_dirty_for_io(pages[i]);
  788. set_page_extent_mapped(pages[i]);
  789. WARN_ON(!PageLocked(pages[i]));
  790. }
  791. return 0;
  792. }
  793. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  794. size_t count, loff_t *ppos)
  795. {
  796. loff_t pos;
  797. loff_t start_pos;
  798. ssize_t num_written = 0;
  799. ssize_t err = 0;
  800. int ret = 0;
  801. struct inode *inode = fdentry(file)->d_inode;
  802. struct btrfs_root *root = BTRFS_I(inode)->root;
  803. struct page **pages = NULL;
  804. int nrptrs;
  805. struct page *pinned[2];
  806. unsigned long first_index;
  807. unsigned long last_index;
  808. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  809. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  810. pinned[0] = NULL;
  811. pinned[1] = NULL;
  812. pos = *ppos;
  813. start_pos = pos;
  814. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  815. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  816. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  817. if (err)
  818. goto out_nolock;
  819. if (count == 0)
  820. goto out_nolock;
  821. #ifdef REMOVE_SUID_PATH
  822. err = remove_suid(&file->f_path);
  823. #else
  824. # if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
  825. err = file_remove_suid(file);
  826. # else
  827. err = remove_suid(fdentry(file));
  828. # endif
  829. #endif
  830. if (err)
  831. goto out_nolock;
  832. file_update_time(file);
  833. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  834. mutex_lock(&inode->i_mutex);
  835. first_index = pos >> PAGE_CACHE_SHIFT;
  836. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  837. /*
  838. * if this is a nodatasum mount, force summing off for the inode
  839. * all the time. That way a later mount with summing on won't
  840. * get confused
  841. */
  842. if (btrfs_test_opt(root, NODATASUM))
  843. btrfs_set_flag(inode, NODATASUM);
  844. /*
  845. * there are lots of better ways to do this, but this code
  846. * makes sure the first and last page in the file range are
  847. * up to date and ready for cow
  848. */
  849. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  850. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  851. if (!PageUptodate(pinned[0])) {
  852. ret = btrfs_readpage(NULL, pinned[0]);
  853. BUG_ON(ret);
  854. wait_on_page_locked(pinned[0]);
  855. } else {
  856. unlock_page(pinned[0]);
  857. }
  858. }
  859. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  860. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  861. if (!PageUptodate(pinned[1])) {
  862. ret = btrfs_readpage(NULL, pinned[1]);
  863. BUG_ON(ret);
  864. wait_on_page_locked(pinned[1]);
  865. } else {
  866. unlock_page(pinned[1]);
  867. }
  868. }
  869. while(count > 0) {
  870. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  871. size_t write_bytes = min(count, nrptrs *
  872. (size_t)PAGE_CACHE_SIZE -
  873. offset);
  874. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  875. PAGE_CACHE_SHIFT;
  876. WARN_ON(num_pages > nrptrs);
  877. memset(pages, 0, sizeof(pages));
  878. ret = btrfs_check_free_space(root, write_bytes, 0);
  879. if (ret)
  880. goto out;
  881. ret = prepare_pages(root, file, pages, num_pages,
  882. pos, first_index, last_index,
  883. write_bytes);
  884. if (ret)
  885. goto out;
  886. ret = btrfs_copy_from_user(pos, num_pages,
  887. write_bytes, pages, buf);
  888. if (ret) {
  889. btrfs_drop_pages(pages, num_pages);
  890. goto out;
  891. }
  892. ret = dirty_and_release_pages(NULL, root, file, pages,
  893. num_pages, pos, write_bytes);
  894. btrfs_drop_pages(pages, num_pages);
  895. if (ret)
  896. goto out;
  897. buf += write_bytes;
  898. count -= write_bytes;
  899. pos += write_bytes;
  900. num_written += write_bytes;
  901. balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
  902. if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  903. btrfs_btree_balance_dirty(root, 1);
  904. btrfs_throttle(root);
  905. cond_resched();
  906. }
  907. out:
  908. mutex_unlock(&inode->i_mutex);
  909. out_nolock:
  910. kfree(pages);
  911. if (pinned[0])
  912. page_cache_release(pinned[0]);
  913. if (pinned[1])
  914. page_cache_release(pinned[1]);
  915. *ppos = pos;
  916. if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  917. struct btrfs_trans_handle *trans;
  918. err = btrfs_fdatawrite_range(inode->i_mapping, start_pos,
  919. start_pos + num_written -1,
  920. WB_SYNC_NONE);
  921. if (err < 0)
  922. num_written = err;
  923. err = btrfs_wait_on_page_writeback_range(inode->i_mapping,
  924. start_pos, start_pos + num_written - 1);
  925. if (err < 0)
  926. num_written = err;
  927. trans = btrfs_start_transaction(root, 1);
  928. ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
  929. if (ret == 0) {
  930. btrfs_sync_log(trans, root);
  931. btrfs_end_transaction(trans, root);
  932. } else {
  933. btrfs_commit_transaction(trans, root);
  934. }
  935. } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
  936. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
  937. do_sync_file_range(file, start_pos,
  938. start_pos + num_written - 1,
  939. SYNC_FILE_RANGE_WRITE |
  940. SYNC_FILE_RANGE_WAIT_AFTER);
  941. #else
  942. do_sync_mapping_range(inode->i_mapping, start_pos,
  943. start_pos + num_written - 1,
  944. SYNC_FILE_RANGE_WRITE |
  945. SYNC_FILE_RANGE_WAIT_AFTER);
  946. #endif
  947. invalidate_mapping_pages(inode->i_mapping,
  948. start_pos >> PAGE_CACHE_SHIFT,
  949. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  950. }
  951. current->backing_dev_info = NULL;
  952. return num_written ? num_written : err;
  953. }
  954. int btrfs_release_file(struct inode * inode, struct file * filp)
  955. {
  956. if (filp->private_data)
  957. btrfs_ioctl_trans_end(filp);
  958. return 0;
  959. }
  960. int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
  961. {
  962. struct inode *inode = dentry->d_inode;
  963. struct btrfs_root *root = BTRFS_I(inode)->root;
  964. int ret = 0;
  965. struct btrfs_trans_handle *trans;
  966. /*
  967. * check the transaction that last modified this inode
  968. * and see if its already been committed
  969. */
  970. if (!BTRFS_I(inode)->last_trans)
  971. goto out;
  972. mutex_lock(&root->fs_info->trans_mutex);
  973. if (BTRFS_I(inode)->last_trans <=
  974. root->fs_info->last_trans_committed) {
  975. BTRFS_I(inode)->last_trans = 0;
  976. mutex_unlock(&root->fs_info->trans_mutex);
  977. goto out;
  978. }
  979. mutex_unlock(&root->fs_info->trans_mutex);
  980. root->fs_info->tree_log_batch++;
  981. filemap_fdatawait(inode->i_mapping);
  982. root->fs_info->tree_log_batch++;
  983. /*
  984. * ok we haven't committed the transaction yet, lets do a commit
  985. */
  986. if (file->private_data)
  987. btrfs_ioctl_trans_end(file);
  988. trans = btrfs_start_transaction(root, 1);
  989. if (!trans) {
  990. ret = -ENOMEM;
  991. goto out;
  992. }
  993. ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
  994. if (ret < 0) {
  995. goto out;
  996. }
  997. /* we've logged all the items and now have a consistent
  998. * version of the file in the log. It is possible that
  999. * someone will come in and modify the file, but that's
  1000. * fine because the log is consistent on disk, and we
  1001. * have references to all of the file's extents
  1002. *
  1003. * It is possible that someone will come in and log the
  1004. * file again, but that will end up using the synchronization
  1005. * inside btrfs_sync_log to keep things safe.
  1006. */
  1007. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  1008. if (ret > 0) {
  1009. ret = btrfs_commit_transaction(trans, root);
  1010. } else {
  1011. btrfs_sync_log(trans, root);
  1012. ret = btrfs_end_transaction(trans, root);
  1013. }
  1014. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  1015. out:
  1016. return ret > 0 ? EIO : ret;
  1017. }
  1018. static struct vm_operations_struct btrfs_file_vm_ops = {
  1019. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  1020. .nopage = filemap_nopage,
  1021. .populate = filemap_populate,
  1022. #else
  1023. .fault = filemap_fault,
  1024. #endif
  1025. .page_mkwrite = btrfs_page_mkwrite,
  1026. };
  1027. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1028. {
  1029. vma->vm_ops = &btrfs_file_vm_ops;
  1030. file_accessed(filp);
  1031. return 0;
  1032. }
  1033. struct file_operations btrfs_file_operations = {
  1034. .llseek = generic_file_llseek,
  1035. .read = do_sync_read,
  1036. .aio_read = generic_file_aio_read,
  1037. .splice_read = generic_file_splice_read,
  1038. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  1039. .sendfile = generic_file_sendfile,
  1040. #endif
  1041. .write = btrfs_file_write,
  1042. .mmap = btrfs_file_mmap,
  1043. .open = generic_file_open,
  1044. .release = btrfs_release_file,
  1045. .fsync = btrfs_sync_file,
  1046. .unlocked_ioctl = btrfs_ioctl,
  1047. #ifdef CONFIG_COMPAT
  1048. .compat_ioctl = btrfs_ioctl,
  1049. #endif
  1050. };