file.c 37 KB

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