file.c 33 KB

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