file.c 34 KB

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