file.c 33 KB

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