file.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include <linux/time.h>
  22. #include <linux/init.h>
  23. #include <linux/string.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mpage.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/version.h>
  32. #include "ctree.h"
  33. #include "disk-io.h"
  34. #include "transaction.h"
  35. #include "btrfs_inode.h"
  36. #include "ioctl.h"
  37. #include "print-tree.h"
  38. #include "tree-log.h"
  39. #include "locking.h"
  40. #include "compat.h"
  41. /* simple helper to fault in pages and copy. This should go away
  42. * and be replaced with calls into generic code.
  43. */
  44. static int noinline btrfs_copy_from_user(loff_t pos, int num_pages,
  45. int write_bytes,
  46. struct page **prepared_pages,
  47. const char __user * buf)
  48. {
  49. long page_fault = 0;
  50. int i;
  51. int offset = pos & (PAGE_CACHE_SIZE - 1);
  52. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  53. size_t count = min_t(size_t,
  54. PAGE_CACHE_SIZE - offset, write_bytes);
  55. struct page *page = prepared_pages[i];
  56. fault_in_pages_readable(buf, count);
  57. /* Copy data from userspace to the current page */
  58. kmap(page);
  59. page_fault = __copy_from_user(page_address(page) + offset,
  60. buf, count);
  61. /* Flush processor's dcache for this page */
  62. flush_dcache_page(page);
  63. kunmap(page);
  64. buf += count;
  65. write_bytes -= count;
  66. if (page_fault)
  67. break;
  68. }
  69. return page_fault ? -EFAULT : 0;
  70. }
  71. /*
  72. * unlocks pages after btrfs_file_write is done with them
  73. */
  74. static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages)
  75. {
  76. size_t i;
  77. for (i = 0; i < num_pages; i++) {
  78. if (!pages[i])
  79. break;
  80. /* page checked is some magic around finding pages that
  81. * have been modified without going through btrfs_set_page_dirty
  82. * clear it here
  83. */
  84. ClearPageChecked(pages[i]);
  85. unlock_page(pages[i]);
  86. mark_page_accessed(pages[i]);
  87. page_cache_release(pages[i]);
  88. }
  89. }
  90. /*
  91. * after copy_from_user, pages need to be dirtied and we need to make
  92. * sure holes are created between the current EOF and the start of
  93. * any next extents (if required).
  94. *
  95. * this also makes the decision about creating an inline extent vs
  96. * doing real data extents, marking pages dirty and delalloc as required.
  97. */
  98. static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
  99. struct btrfs_root *root,
  100. struct file *file,
  101. struct page **pages,
  102. size_t num_pages,
  103. loff_t pos,
  104. size_t write_bytes)
  105. {
  106. int err = 0;
  107. int i;
  108. struct inode *inode = fdentry(file)->d_inode;
  109. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  110. u64 hint_byte;
  111. u64 num_bytes;
  112. u64 start_pos;
  113. u64 end_of_last_block;
  114. u64 end_pos = pos + write_bytes;
  115. loff_t isize = i_size_read(inode);
  116. start_pos = pos & ~((u64)root->sectorsize - 1);
  117. num_bytes = (write_bytes + pos - start_pos +
  118. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  119. end_of_last_block = start_pos + num_bytes - 1;
  120. lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  121. trans = btrfs_join_transaction(root, 1);
  122. if (!trans) {
  123. err = -ENOMEM;
  124. goto out_unlock;
  125. }
  126. btrfs_set_trans_block_group(trans, inode);
  127. hint_byte = 0;
  128. if ((end_of_last_block & 4095) == 0) {
  129. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  130. }
  131. set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  132. /* check for reserved extents on each page, we don't want
  133. * to reset the delalloc bit on things that already have
  134. * extents reserved.
  135. */
  136. btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
  137. for (i = 0; i < num_pages; i++) {
  138. struct page *p = pages[i];
  139. SetPageUptodate(p);
  140. ClearPageChecked(p);
  141. set_page_dirty(p);
  142. }
  143. if (end_pos > isize) {
  144. i_size_write(inode, end_pos);
  145. btrfs_update_inode(trans, root, inode);
  146. }
  147. err = btrfs_end_transaction(trans, root);
  148. out_unlock:
  149. unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  150. return err;
  151. }
  152. /*
  153. * this drops all the extents in the cache that intersect the range
  154. * [start, end]. Existing extents are split as required.
  155. */
  156. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  157. int skip_pinned)
  158. {
  159. struct extent_map *em;
  160. struct extent_map *split = NULL;
  161. struct extent_map *split2 = NULL;
  162. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  163. u64 len = end - start + 1;
  164. int ret;
  165. int testend = 1;
  166. unsigned long flags;
  167. int compressed = 0;
  168. WARN_ON(end < start);
  169. if (end == (u64)-1) {
  170. len = (u64)-1;
  171. testend = 0;
  172. }
  173. while(1) {
  174. if (!split)
  175. split = alloc_extent_map(GFP_NOFS);
  176. if (!split2)
  177. split2 = alloc_extent_map(GFP_NOFS);
  178. spin_lock(&em_tree->lock);
  179. em = lookup_extent_mapping(em_tree, start, len);
  180. if (!em) {
  181. spin_unlock(&em_tree->lock);
  182. break;
  183. }
  184. flags = em->flags;
  185. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  186. spin_unlock(&em_tree->lock);
  187. if (em->start <= start &&
  188. (!testend || em->start + em->len >= start + len)) {
  189. free_extent_map(em);
  190. break;
  191. }
  192. if (start < em->start) {
  193. len = em->start - start;
  194. } else {
  195. len = start + len - (em->start + em->len);
  196. start = em->start + em->len;
  197. }
  198. free_extent_map(em);
  199. continue;
  200. }
  201. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  202. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  203. remove_extent_mapping(em_tree, em);
  204. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  205. em->start < start) {
  206. split->start = em->start;
  207. split->len = start - em->start;
  208. split->orig_start = em->orig_start;
  209. split->block_start = em->block_start;
  210. if (compressed)
  211. split->block_len = em->block_len;
  212. else
  213. split->block_len = split->len;
  214. split->bdev = em->bdev;
  215. split->flags = flags;
  216. ret = add_extent_mapping(em_tree, split);
  217. BUG_ON(ret);
  218. free_extent_map(split);
  219. split = split2;
  220. split2 = NULL;
  221. }
  222. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  223. testend && em->start + em->len > start + len) {
  224. u64 diff = start + len - em->start;
  225. split->start = start + len;
  226. split->len = em->start + em->len - (start + len);
  227. split->bdev = em->bdev;
  228. split->flags = flags;
  229. if (compressed) {
  230. split->block_len = em->block_len;
  231. split->block_start = em->block_start;
  232. split->orig_start = em->orig_start;
  233. } else {
  234. split->block_len = split->len;
  235. split->block_start = em->block_start + diff;
  236. split->orig_start = split->start;
  237. }
  238. ret = add_extent_mapping(em_tree, split);
  239. BUG_ON(ret);
  240. free_extent_map(split);
  241. split = NULL;
  242. }
  243. spin_unlock(&em_tree->lock);
  244. /* once for us */
  245. free_extent_map(em);
  246. /* once for the tree*/
  247. free_extent_map(em);
  248. }
  249. if (split)
  250. free_extent_map(split);
  251. if (split2)
  252. free_extent_map(split2);
  253. return 0;
  254. }
  255. int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
  256. {
  257. return 0;
  258. #if 0
  259. struct btrfs_path *path;
  260. struct btrfs_key found_key;
  261. struct extent_buffer *leaf;
  262. struct btrfs_file_extent_item *extent;
  263. u64 last_offset = 0;
  264. int nritems;
  265. int slot;
  266. int found_type;
  267. int ret;
  268. int err = 0;
  269. u64 extent_end = 0;
  270. path = btrfs_alloc_path();
  271. ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
  272. last_offset, 0);
  273. while(1) {
  274. nritems = btrfs_header_nritems(path->nodes[0]);
  275. if (path->slots[0] >= nritems) {
  276. ret = btrfs_next_leaf(root, path);
  277. if (ret)
  278. goto out;
  279. nritems = btrfs_header_nritems(path->nodes[0]);
  280. }
  281. slot = path->slots[0];
  282. leaf = path->nodes[0];
  283. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  284. if (found_key.objectid != inode->i_ino)
  285. break;
  286. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  287. goto out;
  288. if (found_key.offset < last_offset) {
  289. WARN_ON(1);
  290. btrfs_print_leaf(root, leaf);
  291. printk("inode %lu found offset %Lu expected %Lu\n",
  292. inode->i_ino, found_key.offset, last_offset);
  293. err = 1;
  294. goto out;
  295. }
  296. extent = btrfs_item_ptr(leaf, slot,
  297. struct btrfs_file_extent_item);
  298. found_type = btrfs_file_extent_type(leaf, extent);
  299. if (found_type == BTRFS_FILE_EXTENT_REG) {
  300. extent_end = found_key.offset +
  301. btrfs_file_extent_num_bytes(leaf, extent);
  302. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  303. struct btrfs_item *item;
  304. item = btrfs_item_nr(leaf, slot);
  305. extent_end = found_key.offset +
  306. btrfs_file_extent_inline_len(leaf, extent);
  307. extent_end = (extent_end + root->sectorsize - 1) &
  308. ~((u64)root->sectorsize -1 );
  309. }
  310. last_offset = extent_end;
  311. path->slots[0]++;
  312. }
  313. if (0 && last_offset < inode->i_size) {
  314. WARN_ON(1);
  315. btrfs_print_leaf(root, leaf);
  316. printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
  317. last_offset, inode->i_size);
  318. err = 1;
  319. }
  320. out:
  321. btrfs_free_path(path);
  322. return err;
  323. #endif
  324. }
  325. /*
  326. * this is very complex, but the basic idea is to drop all extents
  327. * in the range start - end. hint_block is filled in with a block number
  328. * that would be a good hint to the block allocator for this file.
  329. *
  330. * If an extent intersects the range but is not entirely inside the range
  331. * it is either truncated or split. Anything entirely inside the range
  332. * is deleted from the tree.
  333. *
  334. * inline_limit is used to tell this code which offsets in the file to keep
  335. * if they contain inline extents.
  336. */
  337. int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
  338. struct btrfs_root *root, struct inode *inode,
  339. u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
  340. {
  341. u64 extent_end = 0;
  342. u64 locked_end = end;
  343. u64 search_start = start;
  344. u64 leaf_start;
  345. u64 ram_bytes = 0;
  346. u64 orig_parent = 0;
  347. u64 disk_bytenr = 0;
  348. u8 compression;
  349. u8 encryption;
  350. u16 other_encoding = 0;
  351. u64 root_gen;
  352. u64 root_owner;
  353. struct extent_buffer *leaf;
  354. struct btrfs_file_extent_item *extent;
  355. struct btrfs_path *path;
  356. struct btrfs_key key;
  357. struct btrfs_file_extent_item old;
  358. int keep;
  359. int slot;
  360. int bookend;
  361. int found_type = 0;
  362. int found_extent;
  363. int found_inline;
  364. int recow;
  365. int ret;
  366. inline_limit = 0;
  367. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  368. path = btrfs_alloc_path();
  369. if (!path)
  370. return -ENOMEM;
  371. while(1) {
  372. recow = 0;
  373. btrfs_release_path(root, path);
  374. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  375. search_start, -1);
  376. if (ret < 0)
  377. goto out;
  378. if (ret > 0) {
  379. if (path->slots[0] == 0) {
  380. ret = 0;
  381. goto out;
  382. }
  383. path->slots[0]--;
  384. }
  385. next_slot:
  386. keep = 0;
  387. bookend = 0;
  388. found_extent = 0;
  389. found_inline = 0;
  390. leaf_start = 0;
  391. root_gen = 0;
  392. root_owner = 0;
  393. compression = 0;
  394. encryption = 0;
  395. extent = NULL;
  396. leaf = path->nodes[0];
  397. slot = path->slots[0];
  398. ret = 0;
  399. btrfs_item_key_to_cpu(leaf, &key, slot);
  400. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
  401. key.offset >= end) {
  402. goto out;
  403. }
  404. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  405. key.objectid != inode->i_ino) {
  406. goto out;
  407. }
  408. if (recow) {
  409. search_start = max(key.offset, start);
  410. continue;
  411. }
  412. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  413. extent = btrfs_item_ptr(leaf, slot,
  414. struct btrfs_file_extent_item);
  415. found_type = btrfs_file_extent_type(leaf, extent);
  416. compression = btrfs_file_extent_compression(leaf,
  417. extent);
  418. encryption = btrfs_file_extent_encryption(leaf,
  419. extent);
  420. other_encoding = btrfs_file_extent_other_encoding(leaf,
  421. extent);
  422. if (found_type == BTRFS_FILE_EXTENT_REG ||
  423. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  424. extent_end =
  425. btrfs_file_extent_disk_bytenr(leaf,
  426. extent);
  427. if (extent_end)
  428. *hint_byte = extent_end;
  429. extent_end = key.offset +
  430. btrfs_file_extent_num_bytes(leaf, extent);
  431. ram_bytes = btrfs_file_extent_ram_bytes(leaf,
  432. extent);
  433. found_extent = 1;
  434. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  435. found_inline = 1;
  436. extent_end = key.offset +
  437. btrfs_file_extent_inline_len(leaf, extent);
  438. }
  439. } else {
  440. extent_end = search_start;
  441. }
  442. /* we found nothing we can drop */
  443. if ((!found_extent && !found_inline) ||
  444. search_start >= extent_end) {
  445. int nextret;
  446. u32 nritems;
  447. nritems = btrfs_header_nritems(leaf);
  448. if (slot >= nritems - 1) {
  449. nextret = btrfs_next_leaf(root, path);
  450. if (nextret)
  451. goto out;
  452. recow = 1;
  453. } else {
  454. path->slots[0]++;
  455. }
  456. goto next_slot;
  457. }
  458. if (end <= extent_end && start >= key.offset && found_inline)
  459. *hint_byte = EXTENT_MAP_INLINE;
  460. if (found_extent) {
  461. read_extent_buffer(leaf, &old, (unsigned long)extent,
  462. sizeof(old));
  463. root_gen = btrfs_header_generation(leaf);
  464. root_owner = btrfs_header_owner(leaf);
  465. leaf_start = leaf->start;
  466. }
  467. if (end < extent_end && end >= key.offset) {
  468. bookend = 1;
  469. if (found_inline && start <= key.offset)
  470. keep = 1;
  471. }
  472. if (bookend && found_extent) {
  473. if (locked_end < extent_end) {
  474. ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
  475. locked_end, extent_end - 1,
  476. GFP_NOFS);
  477. if (!ret) {
  478. btrfs_release_path(root, path);
  479. lock_extent(&BTRFS_I(inode)->io_tree,
  480. locked_end, extent_end - 1,
  481. GFP_NOFS);
  482. locked_end = extent_end;
  483. continue;
  484. }
  485. locked_end = extent_end;
  486. }
  487. orig_parent = path->nodes[0]->start;
  488. disk_bytenr = le64_to_cpu(old.disk_bytenr);
  489. if (disk_bytenr != 0) {
  490. ret = btrfs_inc_extent_ref(trans, root,
  491. disk_bytenr,
  492. le64_to_cpu(old.disk_num_bytes),
  493. orig_parent, root->root_key.objectid,
  494. trans->transid, inode->i_ino);
  495. BUG_ON(ret);
  496. }
  497. }
  498. if (found_inline) {
  499. u64 mask = root->sectorsize - 1;
  500. search_start = (extent_end + mask) & ~mask;
  501. } else
  502. search_start = extent_end;
  503. /* truncate existing extent */
  504. if (start > key.offset) {
  505. u64 new_num;
  506. u64 old_num;
  507. keep = 1;
  508. WARN_ON(start & (root->sectorsize - 1));
  509. if (found_extent) {
  510. new_num = start - key.offset;
  511. old_num = btrfs_file_extent_num_bytes(leaf,
  512. extent);
  513. *hint_byte =
  514. btrfs_file_extent_disk_bytenr(leaf,
  515. extent);
  516. if (btrfs_file_extent_disk_bytenr(leaf,
  517. extent)) {
  518. inode_sub_bytes(inode, old_num -
  519. new_num);
  520. }
  521. if (!compression && !encryption) {
  522. btrfs_set_file_extent_ram_bytes(leaf,
  523. extent, new_num);
  524. }
  525. btrfs_set_file_extent_num_bytes(leaf,
  526. extent, new_num);
  527. btrfs_mark_buffer_dirty(leaf);
  528. } else if (key.offset < inline_limit &&
  529. (end > extent_end) &&
  530. (inline_limit < extent_end)) {
  531. u32 new_size;
  532. new_size = btrfs_file_extent_calc_inline_size(
  533. inline_limit - key.offset);
  534. inode_sub_bytes(inode, extent_end -
  535. inline_limit);
  536. btrfs_set_file_extent_ram_bytes(leaf, extent,
  537. new_size);
  538. if (!compression && !encryption) {
  539. btrfs_truncate_item(trans, root, path,
  540. new_size, 1);
  541. }
  542. }
  543. }
  544. /* delete the entire extent */
  545. if (!keep) {
  546. if (found_inline)
  547. inode_sub_bytes(inode, extent_end -
  548. key.offset);
  549. ret = btrfs_del_item(trans, root, path);
  550. /* TODO update progress marker and return */
  551. BUG_ON(ret);
  552. extent = NULL;
  553. btrfs_release_path(root, path);
  554. /* the extent will be freed later */
  555. }
  556. if (bookend && found_inline && start <= key.offset) {
  557. u32 new_size;
  558. new_size = btrfs_file_extent_calc_inline_size(
  559. extent_end - end);
  560. inode_sub_bytes(inode, end - key.offset);
  561. btrfs_set_file_extent_ram_bytes(leaf, extent,
  562. new_size);
  563. if (!compression && !encryption)
  564. ret = btrfs_truncate_item(trans, root, path,
  565. new_size, 0);
  566. BUG_ON(ret);
  567. }
  568. /* create bookend, splitting the extent in two */
  569. if (bookend && found_extent) {
  570. struct btrfs_key ins;
  571. ins.objectid = inode->i_ino;
  572. ins.offset = end;
  573. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  574. btrfs_release_path(root, path);
  575. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  576. sizeof(*extent));
  577. BUG_ON(ret);
  578. leaf = path->nodes[0];
  579. extent = btrfs_item_ptr(leaf, path->slots[0],
  580. struct btrfs_file_extent_item);
  581. write_extent_buffer(leaf, &old,
  582. (unsigned long)extent, sizeof(old));
  583. btrfs_set_file_extent_compression(leaf, extent,
  584. compression);
  585. btrfs_set_file_extent_encryption(leaf, extent,
  586. encryption);
  587. btrfs_set_file_extent_other_encoding(leaf, extent,
  588. other_encoding);
  589. btrfs_set_file_extent_offset(leaf, extent,
  590. le64_to_cpu(old.offset) + end - key.offset);
  591. WARN_ON(le64_to_cpu(old.num_bytes) <
  592. (extent_end - end));
  593. btrfs_set_file_extent_num_bytes(leaf, extent,
  594. extent_end - end);
  595. /*
  596. * set the ram bytes to the size of the full extent
  597. * before splitting. This is a worst case flag,
  598. * but its the best we can do because we don't know
  599. * how splitting affects compression
  600. */
  601. btrfs_set_file_extent_ram_bytes(leaf, extent,
  602. ram_bytes);
  603. btrfs_set_file_extent_type(leaf, extent, found_type);
  604. btrfs_mark_buffer_dirty(path->nodes[0]);
  605. if (disk_bytenr != 0) {
  606. ret = btrfs_update_extent_ref(trans, root,
  607. disk_bytenr, orig_parent,
  608. leaf->start,
  609. root->root_key.objectid,
  610. trans->transid, ins.objectid);
  611. BUG_ON(ret);
  612. }
  613. btrfs_release_path(root, path);
  614. if (disk_bytenr != 0) {
  615. inode_add_bytes(inode, extent_end - end);
  616. }
  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,
  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 int noinline 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, last_pos -1);
  909. if (ordered &&
  910. ordered->file_offset + ordered->len > start_pos &&
  911. ordered->file_offset < last_pos) {
  912. btrfs_put_ordered_extent(ordered);
  913. unlock_extent(&BTRFS_I(inode)->io_tree,
  914. start_pos, last_pos - 1, GFP_NOFS);
  915. for (i = 0; i < num_pages; i++) {
  916. unlock_page(pages[i]);
  917. page_cache_release(pages[i]);
  918. }
  919. btrfs_wait_ordered_range(inode, start_pos,
  920. last_pos - start_pos);
  921. goto again;
  922. }
  923. if (ordered)
  924. btrfs_put_ordered_extent(ordered);
  925. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  926. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
  927. GFP_NOFS);
  928. unlock_extent(&BTRFS_I(inode)->io_tree,
  929. start_pos, last_pos - 1, GFP_NOFS);
  930. }
  931. for (i = 0; i < num_pages; i++) {
  932. clear_page_dirty_for_io(pages[i]);
  933. set_page_extent_mapped(pages[i]);
  934. WARN_ON(!PageLocked(pages[i]));
  935. }
  936. return 0;
  937. }
  938. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  939. size_t count, loff_t *ppos)
  940. {
  941. loff_t pos;
  942. loff_t start_pos;
  943. ssize_t num_written = 0;
  944. ssize_t err = 0;
  945. int ret = 0;
  946. struct inode *inode = fdentry(file)->d_inode;
  947. struct btrfs_root *root = BTRFS_I(inode)->root;
  948. struct page **pages = NULL;
  949. int nrptrs;
  950. struct page *pinned[2];
  951. unsigned long first_index;
  952. unsigned long last_index;
  953. int will_write;
  954. will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
  955. (file->f_flags & O_DIRECT));
  956. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  957. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  958. pinned[0] = NULL;
  959. pinned[1] = NULL;
  960. pos = *ppos;
  961. start_pos = pos;
  962. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  963. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  964. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  965. if (err)
  966. goto out_nolock;
  967. if (count == 0)
  968. goto out_nolock;
  969. err = file_remove_suid(file);
  970. if (err)
  971. goto out_nolock;
  972. file_update_time(file);
  973. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  974. mutex_lock(&inode->i_mutex);
  975. first_index = pos >> PAGE_CACHE_SHIFT;
  976. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  977. /*
  978. * if this is a nodatasum mount, force summing off for the inode
  979. * all the time. That way a later mount with summing on won't
  980. * get confused
  981. */
  982. if (btrfs_test_opt(root, NODATASUM))
  983. btrfs_set_flag(inode, NODATASUM);
  984. /*
  985. * there are lots of better ways to do this, but this code
  986. * makes sure the first and last page in the file range are
  987. * up to date and ready for cow
  988. */
  989. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  990. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  991. if (!PageUptodate(pinned[0])) {
  992. ret = btrfs_readpage(NULL, pinned[0]);
  993. BUG_ON(ret);
  994. wait_on_page_locked(pinned[0]);
  995. } else {
  996. unlock_page(pinned[0]);
  997. }
  998. }
  999. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  1000. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  1001. if (!PageUptodate(pinned[1])) {
  1002. ret = btrfs_readpage(NULL, pinned[1]);
  1003. BUG_ON(ret);
  1004. wait_on_page_locked(pinned[1]);
  1005. } else {
  1006. unlock_page(pinned[1]);
  1007. }
  1008. }
  1009. while(count > 0) {
  1010. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1011. size_t write_bytes = min(count, nrptrs *
  1012. (size_t)PAGE_CACHE_SIZE -
  1013. offset);
  1014. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1015. PAGE_CACHE_SHIFT;
  1016. WARN_ON(num_pages > nrptrs);
  1017. memset(pages, 0, sizeof(pages));
  1018. ret = btrfs_check_free_space(root, write_bytes, 0);
  1019. if (ret)
  1020. goto out;
  1021. ret = prepare_pages(root, file, pages, num_pages,
  1022. pos, first_index, last_index,
  1023. write_bytes);
  1024. if (ret)
  1025. goto out;
  1026. ret = btrfs_copy_from_user(pos, num_pages,
  1027. write_bytes, pages, buf);
  1028. if (ret) {
  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. goto out;
  1037. if (will_write) {
  1038. btrfs_fdatawrite_range(inode->i_mapping, pos,
  1039. pos + write_bytes - 1,
  1040. WB_SYNC_NONE);
  1041. } else {
  1042. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  1043. num_pages);
  1044. if (num_pages <
  1045. (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1046. btrfs_btree_balance_dirty(root, 1);
  1047. btrfs_throttle(root);
  1048. }
  1049. buf += write_bytes;
  1050. count -= write_bytes;
  1051. pos += write_bytes;
  1052. num_written += write_bytes;
  1053. cond_resched();
  1054. }
  1055. out:
  1056. mutex_unlock(&inode->i_mutex);
  1057. out_nolock:
  1058. kfree(pages);
  1059. if (pinned[0])
  1060. page_cache_release(pinned[0]);
  1061. if (pinned[1])
  1062. page_cache_release(pinned[1]);
  1063. *ppos = pos;
  1064. if (num_written > 0 && will_write) {
  1065. struct btrfs_trans_handle *trans;
  1066. err = btrfs_wait_ordered_range(inode, start_pos, num_written);
  1067. if (err)
  1068. num_written = err;
  1069. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  1070. trans = btrfs_start_transaction(root, 1);
  1071. ret = btrfs_log_dentry_safe(trans, root,
  1072. file->f_dentry);
  1073. if (ret == 0) {
  1074. btrfs_sync_log(trans, root);
  1075. btrfs_end_transaction(trans, root);
  1076. } else {
  1077. btrfs_commit_transaction(trans, root);
  1078. }
  1079. }
  1080. if (file->f_flags & O_DIRECT) {
  1081. invalidate_mapping_pages(inode->i_mapping,
  1082. start_pos >> PAGE_CACHE_SHIFT,
  1083. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  1084. }
  1085. }
  1086. current->backing_dev_info = NULL;
  1087. return num_written ? num_written : err;
  1088. }
  1089. int btrfs_release_file(struct inode * inode, struct file * filp)
  1090. {
  1091. if (filp->private_data)
  1092. btrfs_ioctl_trans_end(filp);
  1093. return 0;
  1094. }
  1095. /*
  1096. * fsync call for both files and directories. This logs the inode into
  1097. * the tree log instead of forcing full commits whenever possible.
  1098. *
  1099. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1100. * in the metadata btree are up to date for copying to the log.
  1101. *
  1102. * It drops the inode mutex before doing the tree log commit. This is an
  1103. * important optimization for directories because holding the mutex prevents
  1104. * new operations on the dir while we write to disk.
  1105. */
  1106. int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
  1107. {
  1108. struct inode *inode = dentry->d_inode;
  1109. struct btrfs_root *root = BTRFS_I(inode)->root;
  1110. int ret = 0;
  1111. struct btrfs_trans_handle *trans;
  1112. /*
  1113. * check the transaction that last modified this inode
  1114. * and see if its already been committed
  1115. */
  1116. if (!BTRFS_I(inode)->last_trans)
  1117. goto out;
  1118. mutex_lock(&root->fs_info->trans_mutex);
  1119. if (BTRFS_I(inode)->last_trans <=
  1120. root->fs_info->last_trans_committed) {
  1121. BTRFS_I(inode)->last_trans = 0;
  1122. mutex_unlock(&root->fs_info->trans_mutex);
  1123. goto out;
  1124. }
  1125. mutex_unlock(&root->fs_info->trans_mutex);
  1126. root->fs_info->tree_log_batch++;
  1127. filemap_fdatawait(inode->i_mapping);
  1128. root->fs_info->tree_log_batch++;
  1129. /*
  1130. * ok we haven't committed the transaction yet, lets do a commit
  1131. */
  1132. if (file->private_data)
  1133. btrfs_ioctl_trans_end(file);
  1134. trans = btrfs_start_transaction(root, 1);
  1135. if (!trans) {
  1136. ret = -ENOMEM;
  1137. goto out;
  1138. }
  1139. ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
  1140. if (ret < 0) {
  1141. goto out;
  1142. }
  1143. /* we've logged all the items and now have a consistent
  1144. * version of the file in the log. It is possible that
  1145. * someone will come in and modify the file, but that's
  1146. * fine because the log is consistent on disk, and we
  1147. * have references to all of the file's extents
  1148. *
  1149. * It is possible that someone will come in and log the
  1150. * file again, but that will end up using the synchronization
  1151. * inside btrfs_sync_log to keep things safe.
  1152. */
  1153. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  1154. if (ret > 0) {
  1155. ret = btrfs_commit_transaction(trans, root);
  1156. } else {
  1157. btrfs_sync_log(trans, root);
  1158. ret = btrfs_end_transaction(trans, root);
  1159. }
  1160. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  1161. out:
  1162. return ret > 0 ? EIO : ret;
  1163. }
  1164. static struct vm_operations_struct btrfs_file_vm_ops = {
  1165. .fault = filemap_fault,
  1166. .page_mkwrite = btrfs_page_mkwrite,
  1167. };
  1168. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1169. {
  1170. vma->vm_ops = &btrfs_file_vm_ops;
  1171. file_accessed(filp);
  1172. return 0;
  1173. }
  1174. struct file_operations btrfs_file_operations = {
  1175. .llseek = generic_file_llseek,
  1176. .read = do_sync_read,
  1177. .aio_read = generic_file_aio_read,
  1178. .splice_read = generic_file_splice_read,
  1179. .write = btrfs_file_write,
  1180. .mmap = btrfs_file_mmap,
  1181. .open = generic_file_open,
  1182. .release = btrfs_release_file,
  1183. .fsync = btrfs_sync_file,
  1184. .unlocked_ioctl = btrfs_ioctl,
  1185. #ifdef CONFIG_COMPAT
  1186. .compat_ioctl = btrfs_ioctl,
  1187. #endif
  1188. };