file.c 27 KB

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