file.c 36 KB

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