file.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include <linux/time.h>
  22. #include <linux/init.h>
  23. #include <linux/string.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mpage.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/version.h>
  32. #include "ctree.h"
  33. #include "disk-io.h"
  34. #include "transaction.h"
  35. #include "btrfs_inode.h"
  36. #include "ioctl.h"
  37. #include "print-tree.h"
  38. #include "compat.h"
  39. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  40. struct page **prepared_pages,
  41. const char __user * buf)
  42. {
  43. long page_fault = 0;
  44. int i;
  45. int offset = pos & (PAGE_CACHE_SIZE - 1);
  46. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  47. size_t count = min_t(size_t,
  48. PAGE_CACHE_SIZE - offset, write_bytes);
  49. struct page *page = prepared_pages[i];
  50. fault_in_pages_readable(buf, count);
  51. /* Copy data from userspace to the current page */
  52. kmap(page);
  53. page_fault = __copy_from_user(page_address(page) + offset,
  54. buf, count);
  55. /* Flush processor's dcache for this page */
  56. flush_dcache_page(page);
  57. kunmap(page);
  58. buf += count;
  59. write_bytes -= count;
  60. if (page_fault)
  61. break;
  62. }
  63. return page_fault ? -EFAULT : 0;
  64. }
  65. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  66. {
  67. size_t i;
  68. for (i = 0; i < num_pages; i++) {
  69. if (!pages[i])
  70. break;
  71. ClearPageChecked(pages[i]);
  72. unlock_page(pages[i]);
  73. mark_page_accessed(pages[i]);
  74. page_cache_release(pages[i]);
  75. }
  76. }
  77. static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
  78. struct btrfs_root *root, struct inode *inode,
  79. u64 offset, size_t size,
  80. struct page **pages, size_t page_offset,
  81. int num_pages)
  82. {
  83. struct btrfs_key key;
  84. struct btrfs_path *path;
  85. struct extent_buffer *leaf;
  86. char *kaddr;
  87. unsigned long ptr;
  88. struct btrfs_file_extent_item *ei;
  89. struct page *page;
  90. u32 datasize;
  91. int err = 0;
  92. int ret;
  93. int i;
  94. ssize_t cur_size;
  95. path = btrfs_alloc_path();
  96. if (!path)
  97. return -ENOMEM;
  98. btrfs_set_trans_block_group(trans, inode);
  99. key.objectid = inode->i_ino;
  100. key.offset = offset;
  101. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  102. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  103. if (ret < 0) {
  104. err = ret;
  105. goto fail;
  106. }
  107. if (ret == 1) {
  108. struct btrfs_key found_key;
  109. if (path->slots[0] == 0)
  110. goto insert;
  111. path->slots[0]--;
  112. leaf = path->nodes[0];
  113. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  114. if (found_key.objectid != inode->i_ino)
  115. goto insert;
  116. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  117. goto insert;
  118. ei = btrfs_item_ptr(leaf, path->slots[0],
  119. struct btrfs_file_extent_item);
  120. if (btrfs_file_extent_type(leaf, ei) !=
  121. BTRFS_FILE_EXTENT_INLINE) {
  122. goto insert;
  123. }
  124. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  125. ret = 0;
  126. }
  127. if (ret == 0) {
  128. u32 found_size;
  129. u64 found_end;
  130. leaf = path->nodes[0];
  131. ei = btrfs_item_ptr(leaf, path->slots[0],
  132. struct btrfs_file_extent_item);
  133. if (btrfs_file_extent_type(leaf, ei) !=
  134. BTRFS_FILE_EXTENT_INLINE) {
  135. err = ret;
  136. btrfs_print_leaf(root, leaf);
  137. printk("found wasn't inline offset %Lu inode %lu\n",
  138. offset, inode->i_ino);
  139. goto fail;
  140. }
  141. found_size = btrfs_file_extent_inline_len(leaf,
  142. btrfs_item_nr(leaf, path->slots[0]));
  143. found_end = key.offset + found_size;
  144. if (found_end < offset + size) {
  145. btrfs_release_path(root, path);
  146. ret = btrfs_search_slot(trans, root, &key, path,
  147. offset + size - found_end, 1);
  148. BUG_ON(ret != 0);
  149. ret = btrfs_extend_item(trans, root, path,
  150. offset + size - found_end);
  151. if (ret) {
  152. err = ret;
  153. goto fail;
  154. }
  155. leaf = path->nodes[0];
  156. ei = btrfs_item_ptr(leaf, path->slots[0],
  157. struct btrfs_file_extent_item);
  158. inode->i_blocks += (offset + size - found_end) >> 9;
  159. }
  160. if (found_end < offset) {
  161. ptr = btrfs_file_extent_inline_start(ei) + found_size;
  162. memset_extent_buffer(leaf, 0, ptr, offset - found_end);
  163. }
  164. } else {
  165. insert:
  166. btrfs_release_path(root, path);
  167. datasize = offset + size - key.offset;
  168. inode->i_blocks += datasize >> 9;
  169. datasize = btrfs_file_extent_calc_inline_size(datasize);
  170. ret = btrfs_insert_empty_item(trans, root, path, &key,
  171. datasize);
  172. if (ret) {
  173. err = ret;
  174. printk("got bad ret %d\n", ret);
  175. goto fail;
  176. }
  177. leaf = path->nodes[0];
  178. ei = btrfs_item_ptr(leaf, path->slots[0],
  179. struct btrfs_file_extent_item);
  180. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  181. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  182. }
  183. ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
  184. cur_size = size;
  185. i = 0;
  186. while (size > 0) {
  187. page = pages[i];
  188. kaddr = kmap_atomic(page, KM_USER0);
  189. cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
  190. write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
  191. kunmap_atomic(kaddr, KM_USER0);
  192. page_offset = 0;
  193. ptr += cur_size;
  194. size -= cur_size;
  195. if (i >= num_pages) {
  196. printk("i %d num_pages %d\n", i, num_pages);
  197. }
  198. i++;
  199. }
  200. btrfs_mark_buffer_dirty(leaf);
  201. fail:
  202. btrfs_free_path(path);
  203. return err;
  204. }
  205. static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
  206. struct btrfs_root *root,
  207. struct file *file,
  208. struct page **pages,
  209. size_t num_pages,
  210. loff_t pos,
  211. size_t write_bytes)
  212. {
  213. int err = 0;
  214. int i;
  215. struct inode *inode = fdentry(file)->d_inode;
  216. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  217. u64 hint_byte;
  218. u64 num_bytes;
  219. u64 start_pos;
  220. u64 end_of_last_block;
  221. u64 end_pos = pos + write_bytes;
  222. u64 inline_size;
  223. int did_inline = 0;
  224. loff_t isize = i_size_read(inode);
  225. start_pos = pos & ~((u64)root->sectorsize - 1);
  226. num_bytes = (write_bytes + pos - start_pos +
  227. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  228. end_of_last_block = start_pos + num_bytes - 1;
  229. lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  230. trans = btrfs_start_transaction(root, 1);
  231. if (!trans) {
  232. err = -ENOMEM;
  233. goto out_unlock;
  234. }
  235. btrfs_set_trans_block_group(trans, inode);
  236. hint_byte = 0;
  237. if ((end_of_last_block & 4095) == 0) {
  238. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  239. }
  240. set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  241. /* FIXME...EIEIO, ENOSPC and more */
  242. /* insert any holes we need to create */
  243. if (isize < start_pos) {
  244. u64 last_pos_in_file;
  245. u64 hole_size;
  246. u64 mask = root->sectorsize - 1;
  247. last_pos_in_file = (isize + mask) & ~mask;
  248. hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
  249. if (hole_size > 0) {
  250. btrfs_wait_ordered_range(inode, last_pos_in_file,
  251. last_pos_in_file + hole_size);
  252. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  253. err = btrfs_drop_extents(trans, root, inode,
  254. last_pos_in_file,
  255. last_pos_in_file + hole_size,
  256. last_pos_in_file,
  257. &hint_byte);
  258. if (err)
  259. goto failed;
  260. err = btrfs_insert_file_extent(trans, root,
  261. inode->i_ino,
  262. last_pos_in_file,
  263. 0, 0, hole_size, 0);
  264. btrfs_drop_extent_cache(inode, last_pos_in_file,
  265. last_pos_in_file + hole_size -1);
  266. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  267. btrfs_check_file(root, inode);
  268. }
  269. if (err)
  270. goto failed;
  271. }
  272. /*
  273. * either allocate an extent for the new bytes or setup the key
  274. * to show we are doing inline data in the extent
  275. */
  276. inline_size = end_pos;
  277. if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  278. inline_size > root->fs_info->max_inline ||
  279. (inline_size & (root->sectorsize -1)) == 0 ||
  280. inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  281. /* check for reserved extents on each page, we don't want
  282. * to reset the delalloc bit on things that already have
  283. * extents reserved.
  284. */
  285. set_extent_delalloc(io_tree, start_pos,
  286. end_of_last_block, GFP_NOFS);
  287. for (i = 0; i < num_pages; i++) {
  288. struct page *p = pages[i];
  289. SetPageUptodate(p);
  290. ClearPageChecked(p);
  291. set_page_dirty(p);
  292. }
  293. } else {
  294. u64 aligned_end;
  295. /* step one, delete the existing extents in this range */
  296. aligned_end = (pos + write_bytes + root->sectorsize - 1) &
  297. ~((u64)root->sectorsize - 1);
  298. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  299. err = btrfs_drop_extents(trans, root, inode, start_pos,
  300. aligned_end, aligned_end, &hint_byte);
  301. if (err)
  302. goto failed;
  303. if (isize > inline_size)
  304. inline_size = min_t(u64, isize, aligned_end);
  305. inline_size -= start_pos;
  306. err = insert_inline_extent(trans, root, inode, start_pos,
  307. inline_size, pages, 0, num_pages);
  308. btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1);
  309. BUG_ON(err);
  310. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  311. did_inline = 1;
  312. }
  313. if (end_pos > isize) {
  314. i_size_write(inode, end_pos);
  315. if (did_inline)
  316. BTRFS_I(inode)->disk_i_size = end_pos;
  317. btrfs_update_inode(trans, root, inode);
  318. }
  319. failed:
  320. err = btrfs_end_transaction_throttle(trans, root);
  321. out_unlock:
  322. unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  323. return err;
  324. }
  325. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
  326. {
  327. struct extent_map *em;
  328. struct extent_map *split = NULL;
  329. struct extent_map *split2 = NULL;
  330. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  331. u64 len = end - start + 1;
  332. int ret;
  333. int testend = 1;
  334. WARN_ON(end < start);
  335. if (end == (u64)-1) {
  336. len = (u64)-1;
  337. testend = 0;
  338. }
  339. while(1) {
  340. if (!split)
  341. split = alloc_extent_map(GFP_NOFS);
  342. if (!split2)
  343. split2 = alloc_extent_map(GFP_NOFS);
  344. spin_lock(&em_tree->lock);
  345. em = lookup_extent_mapping(em_tree, start, len);
  346. if (!em) {
  347. spin_unlock(&em_tree->lock);
  348. break;
  349. }
  350. if (test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  351. printk(KERN_CRIT "inode %lu trying to drop pinned "
  352. "extent start %llu end %llu, em [%llu %llu]\n",
  353. inode->i_ino,
  354. (unsigned long long)start,
  355. (unsigned long long)end,
  356. (unsigned long long)em->start,
  357. (unsigned long long)em->len);
  358. }
  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 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 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. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  788. ClearPageDirty(pages[i]);
  789. #else
  790. cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
  791. #endif
  792. set_page_extent_mapped(pages[i]);
  793. WARN_ON(!PageLocked(pages[i]));
  794. }
  795. return 0;
  796. }
  797. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  798. size_t count, loff_t *ppos)
  799. {
  800. loff_t pos;
  801. loff_t start_pos;
  802. ssize_t num_written = 0;
  803. ssize_t err = 0;
  804. int ret = 0;
  805. struct inode *inode = fdentry(file)->d_inode;
  806. struct btrfs_root *root = BTRFS_I(inode)->root;
  807. struct page **pages = NULL;
  808. int nrptrs;
  809. struct page *pinned[2];
  810. unsigned long first_index;
  811. unsigned long last_index;
  812. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  813. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  814. pinned[0] = NULL;
  815. pinned[1] = NULL;
  816. pos = *ppos;
  817. start_pos = pos;
  818. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  819. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  820. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  821. if (err)
  822. goto out_nolock;
  823. if (count == 0)
  824. goto out_nolock;
  825. #ifdef REMOVE_SUID_PATH
  826. err = remove_suid(&file->f_path);
  827. #else
  828. err = remove_suid(fdentry(file));
  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. cond_resched();
  905. }
  906. out:
  907. mutex_unlock(&inode->i_mutex);
  908. out_nolock:
  909. kfree(pages);
  910. if (pinned[0])
  911. page_cache_release(pinned[0]);
  912. if (pinned[1])
  913. page_cache_release(pinned[1]);
  914. *ppos = pos;
  915. if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  916. err = sync_page_range(inode, inode->i_mapping,
  917. start_pos, num_written);
  918. if (err < 0)
  919. num_written = err;
  920. } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
  921. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
  922. do_sync_file_range(file, start_pos,
  923. start_pos + num_written - 1,
  924. SYNC_FILE_RANGE_WRITE |
  925. SYNC_FILE_RANGE_WAIT_AFTER);
  926. #else
  927. do_sync_mapping_range(inode->i_mapping, start_pos,
  928. start_pos + num_written - 1,
  929. SYNC_FILE_RANGE_WRITE |
  930. SYNC_FILE_RANGE_WAIT_AFTER);
  931. #endif
  932. invalidate_mapping_pages(inode->i_mapping,
  933. start_pos >> PAGE_CACHE_SHIFT,
  934. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  935. }
  936. current->backing_dev_info = NULL;
  937. return num_written ? num_written : err;
  938. }
  939. int btrfs_release_file(struct inode * inode, struct file * filp)
  940. {
  941. if (filp->private_data)
  942. btrfs_ioctl_trans_end(filp);
  943. return 0;
  944. }
  945. static int btrfs_sync_file(struct file *file,
  946. struct dentry *dentry, int datasync)
  947. {
  948. struct inode *inode = dentry->d_inode;
  949. struct btrfs_root *root = BTRFS_I(inode)->root;
  950. int ret = 0;
  951. struct btrfs_trans_handle *trans;
  952. /*
  953. * check the transaction that last modified this inode
  954. * and see if its already been committed
  955. */
  956. if (!BTRFS_I(inode)->last_trans)
  957. goto out;
  958. mutex_lock(&root->fs_info->trans_mutex);
  959. if (BTRFS_I(inode)->last_trans <=
  960. root->fs_info->last_trans_committed) {
  961. BTRFS_I(inode)->last_trans = 0;
  962. mutex_unlock(&root->fs_info->trans_mutex);
  963. goto out;
  964. }
  965. mutex_unlock(&root->fs_info->trans_mutex);
  966. /*
  967. * ok we haven't committed the transaction yet, lets do a commit
  968. */
  969. if (file->private_data)
  970. btrfs_ioctl_trans_end(file);
  971. trans = btrfs_start_transaction(root, 1);
  972. if (!trans) {
  973. ret = -ENOMEM;
  974. goto out;
  975. }
  976. ret = btrfs_commit_transaction(trans, root);
  977. out:
  978. return ret > 0 ? EIO : ret;
  979. }
  980. static struct vm_operations_struct btrfs_file_vm_ops = {
  981. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  982. .nopage = filemap_nopage,
  983. .populate = filemap_populate,
  984. #else
  985. .fault = filemap_fault,
  986. #endif
  987. .page_mkwrite = btrfs_page_mkwrite,
  988. };
  989. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  990. {
  991. vma->vm_ops = &btrfs_file_vm_ops;
  992. file_accessed(filp);
  993. return 0;
  994. }
  995. struct file_operations btrfs_file_operations = {
  996. .llseek = generic_file_llseek,
  997. .read = do_sync_read,
  998. .aio_read = generic_file_aio_read,
  999. .splice_read = generic_file_splice_read,
  1000. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  1001. .sendfile = generic_file_sendfile,
  1002. #endif
  1003. .write = btrfs_file_write,
  1004. .mmap = btrfs_file_mmap,
  1005. .open = generic_file_open,
  1006. .release = btrfs_release_file,
  1007. .fsync = btrfs_sync_file,
  1008. .unlocked_ioctl = btrfs_ioctl,
  1009. #ifdef CONFIG_COMPAT
  1010. .compat_ioctl = btrfs_ioctl,
  1011. #endif
  1012. };