file.c 33 KB

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