file.c 36 KB

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