file.c 33 KB

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