file.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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. err = -ENOMEM;
  717. BUG_ON(1);
  718. }
  719. wait_on_page_writeback(pages[i]);
  720. }
  721. if (start_pos < inode->i_size) {
  722. struct btrfs_ordered_extent *ordered;
  723. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  724. start_pos, last_pos - 1, 0, &cached_state,
  725. GFP_NOFS);
  726. ordered = btrfs_lookup_first_ordered_extent(inode,
  727. last_pos - 1);
  728. if (ordered &&
  729. ordered->file_offset + ordered->len > start_pos &&
  730. ordered->file_offset < last_pos) {
  731. btrfs_put_ordered_extent(ordered);
  732. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  733. start_pos, last_pos - 1,
  734. &cached_state, GFP_NOFS);
  735. for (i = 0; i < num_pages; i++) {
  736. unlock_page(pages[i]);
  737. page_cache_release(pages[i]);
  738. }
  739. btrfs_wait_ordered_range(inode, start_pos,
  740. last_pos - start_pos);
  741. goto again;
  742. }
  743. if (ordered)
  744. btrfs_put_ordered_extent(ordered);
  745. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  746. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  747. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  748. GFP_NOFS);
  749. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  750. start_pos, last_pos - 1, &cached_state,
  751. GFP_NOFS);
  752. }
  753. for (i = 0; i < num_pages; i++) {
  754. clear_page_dirty_for_io(pages[i]);
  755. set_page_extent_mapped(pages[i]);
  756. WARN_ON(!PageLocked(pages[i]));
  757. }
  758. return 0;
  759. }
  760. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  761. const struct iovec *iov,
  762. unsigned long nr_segs, loff_t pos)
  763. {
  764. struct file *file = iocb->ki_filp;
  765. struct inode *inode = fdentry(file)->d_inode;
  766. struct btrfs_root *root = BTRFS_I(inode)->root;
  767. struct page *pinned[2];
  768. struct page **pages = NULL;
  769. struct iov_iter i;
  770. loff_t *ppos = &iocb->ki_pos;
  771. loff_t start_pos;
  772. ssize_t num_written = 0;
  773. ssize_t err = 0;
  774. size_t count;
  775. size_t ocount;
  776. int ret = 0;
  777. int nrptrs;
  778. unsigned long first_index;
  779. unsigned long last_index;
  780. int will_write;
  781. int buffered = 0;
  782. int copied = 0;
  783. int dirty_pages = 0;
  784. will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
  785. (file->f_flags & O_DIRECT));
  786. pinned[0] = NULL;
  787. pinned[1] = NULL;
  788. start_pos = pos;
  789. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  790. mutex_lock(&inode->i_mutex);
  791. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  792. if (err)
  793. goto out;
  794. count = ocount;
  795. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  796. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  797. if (err)
  798. goto out;
  799. if (count == 0)
  800. goto out;
  801. err = file_remove_suid(file);
  802. if (err)
  803. goto out;
  804. /*
  805. * If BTRFS flips readonly due to some impossible error
  806. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  807. * although we have opened a file as writable, we have
  808. * to stop this write operation to ensure FS consistency.
  809. */
  810. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  811. err = -EROFS;
  812. goto out;
  813. }
  814. file_update_time(file);
  815. BTRFS_I(inode)->sequence++;
  816. if (unlikely(file->f_flags & O_DIRECT)) {
  817. num_written = generic_file_direct_write(iocb, iov, &nr_segs,
  818. pos, ppos, count,
  819. ocount);
  820. /*
  821. * the generic O_DIRECT will update in-memory i_size after the
  822. * DIOs are done. But our endio handlers that update the on
  823. * disk i_size never update past the in memory i_size. So we
  824. * need one more update here to catch any additions to the
  825. * file
  826. */
  827. if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
  828. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  829. mark_inode_dirty(inode);
  830. }
  831. if (num_written < 0) {
  832. ret = num_written;
  833. num_written = 0;
  834. goto out;
  835. } else if (num_written == count) {
  836. /* pick up pos changes done by the generic code */
  837. pos = *ppos;
  838. goto out;
  839. }
  840. /*
  841. * We are going to do buffered for the rest of the range, so we
  842. * need to make sure to invalidate the buffered pages when we're
  843. * done.
  844. */
  845. buffered = 1;
  846. pos += num_written;
  847. }
  848. iov_iter_init(&i, iov, nr_segs, count, num_written);
  849. nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
  850. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  851. (sizeof(struct page *)));
  852. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  853. /* generic_write_checks can change our pos */
  854. start_pos = pos;
  855. first_index = pos >> PAGE_CACHE_SHIFT;
  856. last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
  857. /*
  858. * there are lots of better ways to do this, but this code
  859. * makes sure the first and last page in the file range are
  860. * up to date and ready for cow
  861. */
  862. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  863. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  864. if (!PageUptodate(pinned[0])) {
  865. ret = btrfs_readpage(NULL, pinned[0]);
  866. BUG_ON(ret);
  867. wait_on_page_locked(pinned[0]);
  868. } else {
  869. unlock_page(pinned[0]);
  870. }
  871. }
  872. if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
  873. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  874. if (!PageUptodate(pinned[1])) {
  875. ret = btrfs_readpage(NULL, pinned[1]);
  876. BUG_ON(ret);
  877. wait_on_page_locked(pinned[1]);
  878. } else {
  879. unlock_page(pinned[1]);
  880. }
  881. }
  882. while (iov_iter_count(&i) > 0) {
  883. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  884. size_t write_bytes = min(iov_iter_count(&i),
  885. nrptrs * (size_t)PAGE_CACHE_SIZE -
  886. offset);
  887. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  888. PAGE_CACHE_SHIFT;
  889. WARN_ON(num_pages > nrptrs);
  890. memset(pages, 0, sizeof(struct page *) * nrptrs);
  891. /*
  892. * Fault pages before locking them in prepare_pages
  893. * to avoid recursive lock
  894. */
  895. if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
  896. ret = -EFAULT;
  897. goto out;
  898. }
  899. ret = btrfs_delalloc_reserve_space(inode,
  900. num_pages << PAGE_CACHE_SHIFT);
  901. if (ret)
  902. goto out;
  903. ret = prepare_pages(root, file, pages, num_pages,
  904. pos, first_index, last_index,
  905. write_bytes);
  906. if (ret) {
  907. btrfs_delalloc_release_space(inode,
  908. num_pages << PAGE_CACHE_SHIFT);
  909. goto out;
  910. }
  911. copied = btrfs_copy_from_user(pos, num_pages,
  912. write_bytes, pages, &i);
  913. dirty_pages = (copied + PAGE_CACHE_SIZE - 1) >>
  914. PAGE_CACHE_SHIFT;
  915. if (num_pages > dirty_pages) {
  916. if (copied > 0)
  917. atomic_inc(
  918. &BTRFS_I(inode)->outstanding_extents);
  919. btrfs_delalloc_release_space(inode,
  920. (num_pages - dirty_pages) <<
  921. PAGE_CACHE_SHIFT);
  922. }
  923. if (copied > 0) {
  924. dirty_and_release_pages(NULL, root, file, pages,
  925. dirty_pages, pos, copied);
  926. }
  927. btrfs_drop_pages(pages, num_pages);
  928. if (copied > 0) {
  929. if (will_write) {
  930. filemap_fdatawrite_range(inode->i_mapping, pos,
  931. pos + copied - 1);
  932. } else {
  933. balance_dirty_pages_ratelimited_nr(
  934. inode->i_mapping,
  935. dirty_pages);
  936. if (dirty_pages <
  937. (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  938. btrfs_btree_balance_dirty(root, 1);
  939. btrfs_throttle(root);
  940. }
  941. }
  942. pos += copied;
  943. num_written += copied;
  944. cond_resched();
  945. }
  946. out:
  947. mutex_unlock(&inode->i_mutex);
  948. if (ret)
  949. err = ret;
  950. kfree(pages);
  951. if (pinned[0])
  952. page_cache_release(pinned[0]);
  953. if (pinned[1])
  954. page_cache_release(pinned[1]);
  955. *ppos = pos;
  956. /*
  957. * we want to make sure fsync finds this change
  958. * but we haven't joined a transaction running right now.
  959. *
  960. * Later on, someone is sure to update the inode and get the
  961. * real transid recorded.
  962. *
  963. * We set last_trans now to the fs_info generation + 1,
  964. * this will either be one more than the running transaction
  965. * or the generation used for the next transaction if there isn't
  966. * one running right now.
  967. */
  968. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  969. if (num_written > 0 && will_write) {
  970. struct btrfs_trans_handle *trans;
  971. err = btrfs_wait_ordered_range(inode, start_pos, num_written);
  972. if (err)
  973. num_written = err;
  974. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  975. trans = btrfs_start_transaction(root, 0);
  976. if (IS_ERR(trans)) {
  977. num_written = PTR_ERR(trans);
  978. goto done;
  979. }
  980. mutex_lock(&inode->i_mutex);
  981. ret = btrfs_log_dentry_safe(trans, root,
  982. file->f_dentry);
  983. mutex_unlock(&inode->i_mutex);
  984. if (ret == 0) {
  985. ret = btrfs_sync_log(trans, root);
  986. if (ret == 0)
  987. btrfs_end_transaction(trans, root);
  988. else
  989. btrfs_commit_transaction(trans, root);
  990. } else if (ret != BTRFS_NO_LOG_SYNC) {
  991. btrfs_commit_transaction(trans, root);
  992. } else {
  993. btrfs_end_transaction(trans, root);
  994. }
  995. }
  996. if (file->f_flags & O_DIRECT && buffered) {
  997. invalidate_mapping_pages(inode->i_mapping,
  998. start_pos >> PAGE_CACHE_SHIFT,
  999. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  1000. }
  1001. }
  1002. done:
  1003. current->backing_dev_info = NULL;
  1004. return num_written ? num_written : err;
  1005. }
  1006. int btrfs_release_file(struct inode *inode, struct file *filp)
  1007. {
  1008. /*
  1009. * ordered_data_close is set by settattr when we are about to truncate
  1010. * a file from a non-zero size to a zero size. This tries to
  1011. * flush down new bytes that may have been written if the
  1012. * application were using truncate to replace a file in place.
  1013. */
  1014. if (BTRFS_I(inode)->ordered_data_close) {
  1015. BTRFS_I(inode)->ordered_data_close = 0;
  1016. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1017. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1018. filemap_flush(inode->i_mapping);
  1019. }
  1020. if (filp->private_data)
  1021. btrfs_ioctl_trans_end(filp);
  1022. return 0;
  1023. }
  1024. /*
  1025. * fsync call for both files and directories. This logs the inode into
  1026. * the tree log instead of forcing full commits whenever possible.
  1027. *
  1028. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1029. * in the metadata btree are up to date for copying to the log.
  1030. *
  1031. * It drops the inode mutex before doing the tree log commit. This is an
  1032. * important optimization for directories because holding the mutex prevents
  1033. * new operations on the dir while we write to disk.
  1034. */
  1035. int btrfs_sync_file(struct file *file, int datasync)
  1036. {
  1037. struct dentry *dentry = file->f_path.dentry;
  1038. struct inode *inode = dentry->d_inode;
  1039. struct btrfs_root *root = BTRFS_I(inode)->root;
  1040. int ret = 0;
  1041. struct btrfs_trans_handle *trans;
  1042. /* we wait first, since the writeback may change the inode */
  1043. root->log_batch++;
  1044. /* the VFS called filemap_fdatawrite for us */
  1045. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  1046. root->log_batch++;
  1047. /*
  1048. * check the transaction that last modified this inode
  1049. * and see if its already been committed
  1050. */
  1051. if (!BTRFS_I(inode)->last_trans)
  1052. goto out;
  1053. /*
  1054. * if the last transaction that changed this file was before
  1055. * the current transaction, we can bail out now without any
  1056. * syncing
  1057. */
  1058. mutex_lock(&root->fs_info->trans_mutex);
  1059. if (BTRFS_I(inode)->last_trans <=
  1060. root->fs_info->last_trans_committed) {
  1061. BTRFS_I(inode)->last_trans = 0;
  1062. mutex_unlock(&root->fs_info->trans_mutex);
  1063. goto out;
  1064. }
  1065. mutex_unlock(&root->fs_info->trans_mutex);
  1066. /*
  1067. * ok we haven't committed the transaction yet, lets do a commit
  1068. */
  1069. if (file->private_data)
  1070. btrfs_ioctl_trans_end(file);
  1071. trans = btrfs_start_transaction(root, 0);
  1072. if (IS_ERR(trans)) {
  1073. ret = PTR_ERR(trans);
  1074. goto out;
  1075. }
  1076. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1077. if (ret < 0)
  1078. goto out;
  1079. /* we've logged all the items and now have a consistent
  1080. * version of the file in the log. It is possible that
  1081. * someone will come in and modify the file, but that's
  1082. * fine because the log is consistent on disk, and we
  1083. * have references to all of the file's extents
  1084. *
  1085. * It is possible that someone will come in and log the
  1086. * file again, but that will end up using the synchronization
  1087. * inside btrfs_sync_log to keep things safe.
  1088. */
  1089. mutex_unlock(&dentry->d_inode->i_mutex);
  1090. if (ret != BTRFS_NO_LOG_SYNC) {
  1091. if (ret > 0) {
  1092. ret = btrfs_commit_transaction(trans, root);
  1093. } else {
  1094. ret = btrfs_sync_log(trans, root);
  1095. if (ret == 0)
  1096. ret = btrfs_end_transaction(trans, root);
  1097. else
  1098. ret = btrfs_commit_transaction(trans, root);
  1099. }
  1100. } else {
  1101. ret = btrfs_end_transaction(trans, root);
  1102. }
  1103. mutex_lock(&dentry->d_inode->i_mutex);
  1104. out:
  1105. return ret > 0 ? -EIO : ret;
  1106. }
  1107. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1108. .fault = filemap_fault,
  1109. .page_mkwrite = btrfs_page_mkwrite,
  1110. };
  1111. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1112. {
  1113. struct address_space *mapping = filp->f_mapping;
  1114. if (!mapping->a_ops->readpage)
  1115. return -ENOEXEC;
  1116. file_accessed(filp);
  1117. vma->vm_ops = &btrfs_file_vm_ops;
  1118. vma->vm_flags |= VM_CAN_NONLINEAR;
  1119. return 0;
  1120. }
  1121. static long btrfs_fallocate(struct file *file, int mode,
  1122. loff_t offset, loff_t len)
  1123. {
  1124. struct inode *inode = file->f_path.dentry->d_inode;
  1125. struct extent_state *cached_state = NULL;
  1126. u64 cur_offset;
  1127. u64 last_byte;
  1128. u64 alloc_start;
  1129. u64 alloc_end;
  1130. u64 alloc_hint = 0;
  1131. u64 locked_end;
  1132. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1133. struct extent_map *em;
  1134. int ret;
  1135. alloc_start = offset & ~mask;
  1136. alloc_end = (offset + len + mask) & ~mask;
  1137. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  1138. if (mode & ~FALLOC_FL_KEEP_SIZE)
  1139. return -EOPNOTSUPP;
  1140. /*
  1141. * wait for ordered IO before we have any locks. We'll loop again
  1142. * below with the locks held.
  1143. */
  1144. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1145. mutex_lock(&inode->i_mutex);
  1146. ret = inode_newsize_ok(inode, alloc_end);
  1147. if (ret)
  1148. goto out;
  1149. if (alloc_start > inode->i_size) {
  1150. ret = btrfs_cont_expand(inode, alloc_start);
  1151. if (ret)
  1152. goto out;
  1153. }
  1154. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
  1155. if (ret)
  1156. goto out;
  1157. locked_end = alloc_end - 1;
  1158. while (1) {
  1159. struct btrfs_ordered_extent *ordered;
  1160. /* the extent lock is ordered inside the running
  1161. * transaction
  1162. */
  1163. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1164. locked_end, 0, &cached_state, GFP_NOFS);
  1165. ordered = btrfs_lookup_first_ordered_extent(inode,
  1166. alloc_end - 1);
  1167. if (ordered &&
  1168. ordered->file_offset + ordered->len > alloc_start &&
  1169. ordered->file_offset < alloc_end) {
  1170. btrfs_put_ordered_extent(ordered);
  1171. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1172. alloc_start, locked_end,
  1173. &cached_state, GFP_NOFS);
  1174. /*
  1175. * we can't wait on the range with the transaction
  1176. * running or with the extent lock held
  1177. */
  1178. btrfs_wait_ordered_range(inode, alloc_start,
  1179. alloc_end - alloc_start);
  1180. } else {
  1181. if (ordered)
  1182. btrfs_put_ordered_extent(ordered);
  1183. break;
  1184. }
  1185. }
  1186. cur_offset = alloc_start;
  1187. while (1) {
  1188. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1189. alloc_end - cur_offset, 0);
  1190. BUG_ON(IS_ERR(em) || !em);
  1191. last_byte = min(extent_map_end(em), alloc_end);
  1192. last_byte = (last_byte + mask) & ~mask;
  1193. if (em->block_start == EXTENT_MAP_HOLE ||
  1194. (cur_offset >= inode->i_size &&
  1195. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1196. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1197. last_byte - cur_offset,
  1198. 1 << inode->i_blkbits,
  1199. offset + len,
  1200. &alloc_hint);
  1201. if (ret < 0) {
  1202. free_extent_map(em);
  1203. break;
  1204. }
  1205. }
  1206. free_extent_map(em);
  1207. cur_offset = last_byte;
  1208. if (cur_offset >= alloc_end) {
  1209. ret = 0;
  1210. break;
  1211. }
  1212. }
  1213. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  1214. &cached_state, GFP_NOFS);
  1215. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
  1216. out:
  1217. mutex_unlock(&inode->i_mutex);
  1218. return ret;
  1219. }
  1220. const struct file_operations btrfs_file_operations = {
  1221. .llseek = generic_file_llseek,
  1222. .read = do_sync_read,
  1223. .write = do_sync_write,
  1224. .aio_read = generic_file_aio_read,
  1225. .splice_read = generic_file_splice_read,
  1226. .aio_write = btrfs_file_aio_write,
  1227. .mmap = btrfs_file_mmap,
  1228. .open = generic_file_open,
  1229. .release = btrfs_release_file,
  1230. .fsync = btrfs_sync_file,
  1231. .fallocate = btrfs_fallocate,
  1232. .unlocked_ioctl = btrfs_ioctl,
  1233. #ifdef CONFIG_COMPAT
  1234. .compat_ioctl = btrfs_ioctl,
  1235. #endif
  1236. };