file.c 37 KB

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