file.c 33 KB

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