file.c 37 KB

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