file.c 34 KB

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