file.c 36 KB

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