file.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
  117. if (err)
  118. return err;
  119. for (i = 0; i < num_pages; i++) {
  120. struct page *p = pages[i];
  121. SetPageUptodate(p);
  122. ClearPageChecked(p);
  123. set_page_dirty(p);
  124. }
  125. if (end_pos > isize) {
  126. i_size_write(inode, end_pos);
  127. /* we've only changed i_size in ram, and we haven't updated
  128. * the disk i_size. There is no need to log the inode
  129. * at this time.
  130. */
  131. }
  132. return err;
  133. }
  134. /*
  135. * this drops all the extents in the cache that intersect the range
  136. * [start, end]. Existing extents are split as required.
  137. */
  138. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  139. int skip_pinned)
  140. {
  141. struct extent_map *em;
  142. struct extent_map *split = NULL;
  143. struct extent_map *split2 = NULL;
  144. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  145. u64 len = end - start + 1;
  146. int ret;
  147. int testend = 1;
  148. unsigned long flags;
  149. int compressed = 0;
  150. WARN_ON(end < start);
  151. if (end == (u64)-1) {
  152. len = (u64)-1;
  153. testend = 0;
  154. }
  155. while (1) {
  156. if (!split)
  157. split = alloc_extent_map(GFP_NOFS);
  158. if (!split2)
  159. split2 = alloc_extent_map(GFP_NOFS);
  160. write_lock(&em_tree->lock);
  161. em = lookup_extent_mapping(em_tree, start, len);
  162. if (!em) {
  163. write_unlock(&em_tree->lock);
  164. break;
  165. }
  166. flags = em->flags;
  167. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  168. if (testend && em->start + em->len >= start + len) {
  169. free_extent_map(em);
  170. write_unlock(&em_tree->lock);
  171. break;
  172. }
  173. start = em->start + em->len;
  174. if (testend)
  175. len = start + len - (em->start + em->len);
  176. free_extent_map(em);
  177. write_unlock(&em_tree->lock);
  178. continue;
  179. }
  180. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  181. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  182. remove_extent_mapping(em_tree, em);
  183. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  184. em->start < start) {
  185. split->start = em->start;
  186. split->len = start - em->start;
  187. split->orig_start = em->orig_start;
  188. split->block_start = em->block_start;
  189. if (compressed)
  190. split->block_len = em->block_len;
  191. else
  192. split->block_len = split->len;
  193. split->bdev = em->bdev;
  194. split->flags = flags;
  195. ret = add_extent_mapping(em_tree, split);
  196. BUG_ON(ret);
  197. free_extent_map(split);
  198. split = split2;
  199. split2 = NULL;
  200. }
  201. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  202. testend && em->start + em->len > start + len) {
  203. u64 diff = start + len - em->start;
  204. split->start = start + len;
  205. split->len = em->start + em->len - (start + len);
  206. split->bdev = em->bdev;
  207. split->flags = flags;
  208. if (compressed) {
  209. split->block_len = em->block_len;
  210. split->block_start = em->block_start;
  211. split->orig_start = em->orig_start;
  212. } else {
  213. split->block_len = split->len;
  214. split->block_start = em->block_start + diff;
  215. split->orig_start = split->start;
  216. }
  217. ret = add_extent_mapping(em_tree, split);
  218. BUG_ON(ret);
  219. free_extent_map(split);
  220. split = NULL;
  221. }
  222. write_unlock(&em_tree->lock);
  223. /* once for us */
  224. free_extent_map(em);
  225. /* once for the tree*/
  226. free_extent_map(em);
  227. }
  228. if (split)
  229. free_extent_map(split);
  230. if (split2)
  231. free_extent_map(split2);
  232. return 0;
  233. }
  234. /*
  235. * this is very complex, but the basic idea is to drop all extents
  236. * in the range start - end. hint_block is filled in with a block number
  237. * that would be a good hint to the block allocator for this file.
  238. *
  239. * If an extent intersects the range but is not entirely inside the range
  240. * it is either truncated or split. Anything entirely inside the range
  241. * is deleted from the tree.
  242. */
  243. int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
  244. u64 start, u64 end, u64 *hint_byte, int drop_cache)
  245. {
  246. struct btrfs_root *root = BTRFS_I(inode)->root;
  247. struct extent_buffer *leaf;
  248. struct btrfs_file_extent_item *fi;
  249. struct btrfs_path *path;
  250. struct btrfs_key key;
  251. struct btrfs_key new_key;
  252. u64 search_start = start;
  253. u64 disk_bytenr = 0;
  254. u64 num_bytes = 0;
  255. u64 extent_offset = 0;
  256. u64 extent_end = 0;
  257. int del_nr = 0;
  258. int del_slot = 0;
  259. int extent_type;
  260. int recow;
  261. int ret;
  262. if (drop_cache)
  263. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  264. path = btrfs_alloc_path();
  265. if (!path)
  266. return -ENOMEM;
  267. while (1) {
  268. recow = 0;
  269. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  270. search_start, -1);
  271. if (ret < 0)
  272. break;
  273. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  274. leaf = path->nodes[0];
  275. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  276. if (key.objectid == inode->i_ino &&
  277. key.type == BTRFS_EXTENT_DATA_KEY)
  278. path->slots[0]--;
  279. }
  280. ret = 0;
  281. next_slot:
  282. leaf = path->nodes[0];
  283. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  284. BUG_ON(del_nr > 0);
  285. ret = btrfs_next_leaf(root, path);
  286. if (ret < 0)
  287. break;
  288. if (ret > 0) {
  289. ret = 0;
  290. break;
  291. }
  292. leaf = path->nodes[0];
  293. recow = 1;
  294. }
  295. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  296. if (key.objectid > inode->i_ino ||
  297. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  298. break;
  299. fi = btrfs_item_ptr(leaf, path->slots[0],
  300. struct btrfs_file_extent_item);
  301. extent_type = btrfs_file_extent_type(leaf, fi);
  302. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  303. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  304. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  305. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  306. extent_offset = btrfs_file_extent_offset(leaf, fi);
  307. extent_end = key.offset +
  308. btrfs_file_extent_num_bytes(leaf, fi);
  309. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  310. extent_end = key.offset +
  311. btrfs_file_extent_inline_len(leaf, fi);
  312. } else {
  313. WARN_ON(1);
  314. extent_end = search_start;
  315. }
  316. if (extent_end <= search_start) {
  317. path->slots[0]++;
  318. goto next_slot;
  319. }
  320. search_start = max(key.offset, start);
  321. if (recow) {
  322. btrfs_release_path(root, path);
  323. continue;
  324. }
  325. /*
  326. * | - range to drop - |
  327. * | -------- extent -------- |
  328. */
  329. if (start > key.offset && end < extent_end) {
  330. BUG_ON(del_nr > 0);
  331. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  332. memcpy(&new_key, &key, sizeof(new_key));
  333. new_key.offset = start;
  334. ret = btrfs_duplicate_item(trans, root, path,
  335. &new_key);
  336. if (ret == -EAGAIN) {
  337. btrfs_release_path(root, path);
  338. continue;
  339. }
  340. if (ret < 0)
  341. break;
  342. leaf = path->nodes[0];
  343. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  344. struct btrfs_file_extent_item);
  345. btrfs_set_file_extent_num_bytes(leaf, fi,
  346. start - key.offset);
  347. fi = btrfs_item_ptr(leaf, path->slots[0],
  348. struct btrfs_file_extent_item);
  349. extent_offset += start - key.offset;
  350. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  351. btrfs_set_file_extent_num_bytes(leaf, fi,
  352. extent_end - start);
  353. btrfs_mark_buffer_dirty(leaf);
  354. if (disk_bytenr > 0) {
  355. ret = btrfs_inc_extent_ref(trans, root,
  356. disk_bytenr, num_bytes, 0,
  357. root->root_key.objectid,
  358. new_key.objectid,
  359. start - extent_offset);
  360. BUG_ON(ret);
  361. *hint_byte = disk_bytenr;
  362. }
  363. key.offset = start;
  364. }
  365. /*
  366. * | ---- range to drop ----- |
  367. * | -------- extent -------- |
  368. */
  369. if (start <= key.offset && end < extent_end) {
  370. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  371. memcpy(&new_key, &key, sizeof(new_key));
  372. new_key.offset = end;
  373. btrfs_set_item_key_safe(trans, root, path, &new_key);
  374. extent_offset += end - key.offset;
  375. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  376. btrfs_set_file_extent_num_bytes(leaf, fi,
  377. extent_end - end);
  378. btrfs_mark_buffer_dirty(leaf);
  379. if (disk_bytenr > 0) {
  380. inode_sub_bytes(inode, end - key.offset);
  381. *hint_byte = disk_bytenr;
  382. }
  383. break;
  384. }
  385. search_start = extent_end;
  386. /*
  387. * | ---- range to drop ----- |
  388. * | -------- extent -------- |
  389. */
  390. if (start > key.offset && end >= extent_end) {
  391. BUG_ON(del_nr > 0);
  392. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  393. btrfs_set_file_extent_num_bytes(leaf, fi,
  394. start - key.offset);
  395. btrfs_mark_buffer_dirty(leaf);
  396. if (disk_bytenr > 0) {
  397. inode_sub_bytes(inode, extent_end - start);
  398. *hint_byte = disk_bytenr;
  399. }
  400. if (end == extent_end)
  401. break;
  402. path->slots[0]++;
  403. goto next_slot;
  404. }
  405. /*
  406. * | ---- range to drop ----- |
  407. * | ------ extent ------ |
  408. */
  409. if (start <= key.offset && end >= extent_end) {
  410. if (del_nr == 0) {
  411. del_slot = path->slots[0];
  412. del_nr = 1;
  413. } else {
  414. BUG_ON(del_slot + del_nr != path->slots[0]);
  415. del_nr++;
  416. }
  417. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  418. inode_sub_bytes(inode,
  419. extent_end - key.offset);
  420. extent_end = ALIGN(extent_end,
  421. root->sectorsize);
  422. } else if (disk_bytenr > 0) {
  423. ret = btrfs_free_extent(trans, root,
  424. disk_bytenr, num_bytes, 0,
  425. root->root_key.objectid,
  426. key.objectid, key.offset -
  427. extent_offset);
  428. BUG_ON(ret);
  429. inode_sub_bytes(inode,
  430. extent_end - key.offset);
  431. *hint_byte = disk_bytenr;
  432. }
  433. if (end == extent_end)
  434. break;
  435. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  436. path->slots[0]++;
  437. goto next_slot;
  438. }
  439. ret = btrfs_del_items(trans, root, path, del_slot,
  440. del_nr);
  441. BUG_ON(ret);
  442. del_nr = 0;
  443. del_slot = 0;
  444. btrfs_release_path(root, path);
  445. continue;
  446. }
  447. BUG_ON(1);
  448. }
  449. if (del_nr > 0) {
  450. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  451. BUG_ON(ret);
  452. }
  453. btrfs_free_path(path);
  454. return ret;
  455. }
  456. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  457. u64 objectid, u64 bytenr, u64 *start, u64 *end)
  458. {
  459. struct btrfs_file_extent_item *fi;
  460. struct btrfs_key key;
  461. u64 extent_end;
  462. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  463. return 0;
  464. btrfs_item_key_to_cpu(leaf, &key, slot);
  465. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  466. return 0;
  467. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  468. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  469. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  470. btrfs_file_extent_compression(leaf, fi) ||
  471. btrfs_file_extent_encryption(leaf, fi) ||
  472. btrfs_file_extent_other_encoding(leaf, fi))
  473. return 0;
  474. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  475. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  476. return 0;
  477. *start = key.offset;
  478. *end = extent_end;
  479. return 1;
  480. }
  481. /*
  482. * Mark extent in the range start - end as written.
  483. *
  484. * This changes extent type from 'pre-allocated' to 'regular'. If only
  485. * part of extent is marked as written, the extent will be split into
  486. * two or three.
  487. */
  488. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  489. struct inode *inode, u64 start, u64 end)
  490. {
  491. struct btrfs_root *root = BTRFS_I(inode)->root;
  492. struct extent_buffer *leaf;
  493. struct btrfs_path *path;
  494. struct btrfs_file_extent_item *fi;
  495. struct btrfs_key key;
  496. struct btrfs_key new_key;
  497. u64 bytenr;
  498. u64 num_bytes;
  499. u64 extent_end;
  500. u64 orig_offset;
  501. u64 other_start;
  502. u64 other_end;
  503. u64 split;
  504. int del_nr = 0;
  505. int del_slot = 0;
  506. int ret;
  507. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  508. path = btrfs_alloc_path();
  509. BUG_ON(!path);
  510. again:
  511. split = start;
  512. key.objectid = inode->i_ino;
  513. key.type = BTRFS_EXTENT_DATA_KEY;
  514. key.offset = split;
  515. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  516. if (ret > 0 && path->slots[0] > 0)
  517. path->slots[0]--;
  518. leaf = path->nodes[0];
  519. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  520. BUG_ON(key.objectid != inode->i_ino ||
  521. key.type != BTRFS_EXTENT_DATA_KEY);
  522. fi = btrfs_item_ptr(leaf, path->slots[0],
  523. struct btrfs_file_extent_item);
  524. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  525. BTRFS_FILE_EXTENT_PREALLOC);
  526. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  527. BUG_ON(key.offset > start || extent_end < end);
  528. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  529. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  530. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  531. while (start > key.offset || end < extent_end) {
  532. if (key.offset == start)
  533. split = end;
  534. memcpy(&new_key, &key, sizeof(new_key));
  535. new_key.offset = split;
  536. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  537. if (ret == -EAGAIN) {
  538. btrfs_release_path(root, path);
  539. goto again;
  540. }
  541. BUG_ON(ret < 0);
  542. leaf = path->nodes[0];
  543. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  544. struct btrfs_file_extent_item);
  545. btrfs_set_file_extent_num_bytes(leaf, fi,
  546. split - key.offset);
  547. fi = btrfs_item_ptr(leaf, path->slots[0],
  548. struct btrfs_file_extent_item);
  549. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  550. btrfs_set_file_extent_num_bytes(leaf, fi,
  551. extent_end - split);
  552. btrfs_mark_buffer_dirty(leaf);
  553. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  554. root->root_key.objectid,
  555. inode->i_ino, orig_offset);
  556. BUG_ON(ret);
  557. if (split == start) {
  558. key.offset = start;
  559. } else {
  560. BUG_ON(start != key.offset);
  561. path->slots[0]--;
  562. extent_end = end;
  563. }
  564. }
  565. fi = btrfs_item_ptr(leaf, path->slots[0],
  566. struct btrfs_file_extent_item);
  567. other_start = end;
  568. other_end = 0;
  569. if (extent_mergeable(leaf, path->slots[0] + 1, inode->i_ino,
  570. bytenr, &other_start, &other_end)) {
  571. extent_end = other_end;
  572. del_slot = path->slots[0] + 1;
  573. del_nr++;
  574. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  575. 0, root->root_key.objectid,
  576. inode->i_ino, orig_offset);
  577. BUG_ON(ret);
  578. }
  579. other_start = 0;
  580. other_end = start;
  581. if (extent_mergeable(leaf, path->slots[0] - 1, inode->i_ino,
  582. bytenr, &other_start, &other_end)) {
  583. key.offset = other_start;
  584. del_slot = path->slots[0];
  585. del_nr++;
  586. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  587. 0, root->root_key.objectid,
  588. inode->i_ino, orig_offset);
  589. BUG_ON(ret);
  590. }
  591. if (del_nr == 0) {
  592. btrfs_set_file_extent_type(leaf, fi,
  593. BTRFS_FILE_EXTENT_REG);
  594. btrfs_mark_buffer_dirty(leaf);
  595. goto out;
  596. }
  597. fi = btrfs_item_ptr(leaf, del_slot - 1,
  598. struct btrfs_file_extent_item);
  599. btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
  600. btrfs_set_file_extent_num_bytes(leaf, fi,
  601. extent_end - key.offset);
  602. btrfs_mark_buffer_dirty(leaf);
  603. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  604. BUG_ON(ret);
  605. out:
  606. btrfs_free_path(path);
  607. return 0;
  608. }
  609. /*
  610. * this gets pages into the page cache and locks them down, it also properly
  611. * waits for data=ordered extents to finish before allowing the pages to be
  612. * modified.
  613. */
  614. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  615. struct page **pages, size_t num_pages,
  616. loff_t pos, unsigned long first_index,
  617. unsigned long last_index, size_t write_bytes)
  618. {
  619. int i;
  620. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  621. struct inode *inode = fdentry(file)->d_inode;
  622. int err = 0;
  623. u64 start_pos;
  624. u64 last_pos;
  625. start_pos = pos & ~((u64)root->sectorsize - 1);
  626. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  627. if (start_pos > inode->i_size) {
  628. err = btrfs_cont_expand(inode, start_pos);
  629. if (err)
  630. return err;
  631. }
  632. memset(pages, 0, num_pages * sizeof(struct page *));
  633. again:
  634. for (i = 0; i < num_pages; i++) {
  635. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  636. if (!pages[i]) {
  637. err = -ENOMEM;
  638. BUG_ON(1);
  639. }
  640. wait_on_page_writeback(pages[i]);
  641. }
  642. if (start_pos < inode->i_size) {
  643. struct btrfs_ordered_extent *ordered;
  644. lock_extent(&BTRFS_I(inode)->io_tree,
  645. start_pos, last_pos - 1, GFP_NOFS);
  646. ordered = btrfs_lookup_first_ordered_extent(inode,
  647. last_pos - 1);
  648. if (ordered &&
  649. ordered->file_offset + ordered->len > start_pos &&
  650. ordered->file_offset < last_pos) {
  651. btrfs_put_ordered_extent(ordered);
  652. unlock_extent(&BTRFS_I(inode)->io_tree,
  653. start_pos, last_pos - 1, GFP_NOFS);
  654. for (i = 0; i < num_pages; i++) {
  655. unlock_page(pages[i]);
  656. page_cache_release(pages[i]);
  657. }
  658. btrfs_wait_ordered_range(inode, start_pos,
  659. last_pos - start_pos);
  660. goto again;
  661. }
  662. if (ordered)
  663. btrfs_put_ordered_extent(ordered);
  664. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  665. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  666. EXTENT_DO_ACCOUNTING,
  667. GFP_NOFS);
  668. unlock_extent(&BTRFS_I(inode)->io_tree,
  669. start_pos, last_pos - 1, GFP_NOFS);
  670. }
  671. for (i = 0; i < num_pages; i++) {
  672. clear_page_dirty_for_io(pages[i]);
  673. set_page_extent_mapped(pages[i]);
  674. WARN_ON(!PageLocked(pages[i]));
  675. }
  676. return 0;
  677. }
  678. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  679. size_t count, loff_t *ppos)
  680. {
  681. loff_t pos;
  682. loff_t start_pos;
  683. ssize_t num_written = 0;
  684. ssize_t err = 0;
  685. int ret = 0;
  686. struct inode *inode = fdentry(file)->d_inode;
  687. struct btrfs_root *root = BTRFS_I(inode)->root;
  688. struct page **pages = NULL;
  689. int nrptrs;
  690. struct page *pinned[2];
  691. unsigned long first_index;
  692. unsigned long last_index;
  693. int will_write;
  694. will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
  695. (file->f_flags & O_DIRECT));
  696. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  697. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  698. pinned[0] = NULL;
  699. pinned[1] = NULL;
  700. pos = *ppos;
  701. start_pos = pos;
  702. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  703. /* do the reserve before the mutex lock in case we have to do some
  704. * flushing. We wouldn't deadlock, but this is more polite.
  705. */
  706. err = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  707. if (err)
  708. goto out_nolock;
  709. mutex_lock(&inode->i_mutex);
  710. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  711. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  712. if (err)
  713. goto out;
  714. if (count == 0)
  715. goto out;
  716. err = file_remove_suid(file);
  717. if (err)
  718. goto out;
  719. file_update_time(file);
  720. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  721. /* generic_write_checks can change our pos */
  722. start_pos = pos;
  723. BTRFS_I(inode)->sequence++;
  724. first_index = pos >> PAGE_CACHE_SHIFT;
  725. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  726. /*
  727. * there are lots of better ways to do this, but this code
  728. * makes sure the first and last page in the file range are
  729. * up to date and ready for cow
  730. */
  731. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  732. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  733. if (!PageUptodate(pinned[0])) {
  734. ret = btrfs_readpage(NULL, pinned[0]);
  735. BUG_ON(ret);
  736. wait_on_page_locked(pinned[0]);
  737. } else {
  738. unlock_page(pinned[0]);
  739. }
  740. }
  741. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  742. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  743. if (!PageUptodate(pinned[1])) {
  744. ret = btrfs_readpage(NULL, pinned[1]);
  745. BUG_ON(ret);
  746. wait_on_page_locked(pinned[1]);
  747. } else {
  748. unlock_page(pinned[1]);
  749. }
  750. }
  751. while (count > 0) {
  752. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  753. size_t write_bytes = min(count, nrptrs *
  754. (size_t)PAGE_CACHE_SIZE -
  755. offset);
  756. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  757. PAGE_CACHE_SHIFT;
  758. WARN_ON(num_pages > nrptrs);
  759. memset(pages, 0, sizeof(struct page *) * nrptrs);
  760. ret = btrfs_check_data_free_space(root, inode, write_bytes);
  761. if (ret)
  762. goto out;
  763. ret = prepare_pages(root, file, pages, num_pages,
  764. pos, first_index, last_index,
  765. write_bytes);
  766. if (ret) {
  767. btrfs_free_reserved_data_space(root, inode,
  768. write_bytes);
  769. goto out;
  770. }
  771. ret = btrfs_copy_from_user(pos, num_pages,
  772. write_bytes, pages, buf);
  773. if (ret) {
  774. btrfs_free_reserved_data_space(root, inode,
  775. write_bytes);
  776. btrfs_drop_pages(pages, num_pages);
  777. goto out;
  778. }
  779. ret = dirty_and_release_pages(NULL, root, file, pages,
  780. num_pages, pos, write_bytes);
  781. btrfs_drop_pages(pages, num_pages);
  782. if (ret) {
  783. btrfs_free_reserved_data_space(root, inode,
  784. write_bytes);
  785. goto out;
  786. }
  787. if (will_write) {
  788. filemap_fdatawrite_range(inode->i_mapping, pos,
  789. pos + write_bytes - 1);
  790. } else {
  791. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  792. num_pages);
  793. if (num_pages <
  794. (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  795. btrfs_btree_balance_dirty(root, 1);
  796. btrfs_throttle(root);
  797. }
  798. buf += write_bytes;
  799. count -= write_bytes;
  800. pos += write_bytes;
  801. num_written += write_bytes;
  802. cond_resched();
  803. }
  804. out:
  805. mutex_unlock(&inode->i_mutex);
  806. if (ret)
  807. err = ret;
  808. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  809. out_nolock:
  810. kfree(pages);
  811. if (pinned[0])
  812. page_cache_release(pinned[0]);
  813. if (pinned[1])
  814. page_cache_release(pinned[1]);
  815. *ppos = pos;
  816. /*
  817. * we want to make sure fsync finds this change
  818. * but we haven't joined a transaction running right now.
  819. *
  820. * Later on, someone is sure to update the inode and get the
  821. * real transid recorded.
  822. *
  823. * We set last_trans now to the fs_info generation + 1,
  824. * this will either be one more than the running transaction
  825. * or the generation used for the next transaction if there isn't
  826. * one running right now.
  827. */
  828. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  829. if (num_written > 0 && will_write) {
  830. struct btrfs_trans_handle *trans;
  831. err = btrfs_wait_ordered_range(inode, start_pos, num_written);
  832. if (err)
  833. num_written = err;
  834. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  835. trans = btrfs_start_transaction(root, 1);
  836. ret = btrfs_log_dentry_safe(trans, root,
  837. file->f_dentry);
  838. if (ret == 0) {
  839. ret = btrfs_sync_log(trans, root);
  840. if (ret == 0)
  841. btrfs_end_transaction(trans, root);
  842. else
  843. btrfs_commit_transaction(trans, root);
  844. } else if (ret != BTRFS_NO_LOG_SYNC) {
  845. btrfs_commit_transaction(trans, root);
  846. } else {
  847. btrfs_end_transaction(trans, root);
  848. }
  849. }
  850. if (file->f_flags & O_DIRECT) {
  851. invalidate_mapping_pages(inode->i_mapping,
  852. start_pos >> PAGE_CACHE_SHIFT,
  853. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  854. }
  855. }
  856. current->backing_dev_info = NULL;
  857. return num_written ? num_written : err;
  858. }
  859. int btrfs_release_file(struct inode *inode, struct file *filp)
  860. {
  861. /*
  862. * ordered_data_close is set by settattr when we are about to truncate
  863. * a file from a non-zero size to a zero size. This tries to
  864. * flush down new bytes that may have been written if the
  865. * application were using truncate to replace a file in place.
  866. */
  867. if (BTRFS_I(inode)->ordered_data_close) {
  868. BTRFS_I(inode)->ordered_data_close = 0;
  869. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  870. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  871. filemap_flush(inode->i_mapping);
  872. }
  873. if (filp->private_data)
  874. btrfs_ioctl_trans_end(filp);
  875. return 0;
  876. }
  877. /*
  878. * fsync call for both files and directories. This logs the inode into
  879. * the tree log instead of forcing full commits whenever possible.
  880. *
  881. * It needs to call filemap_fdatawait so that all ordered extent updates are
  882. * in the metadata btree are up to date for copying to the log.
  883. *
  884. * It drops the inode mutex before doing the tree log commit. This is an
  885. * important optimization for directories because holding the mutex prevents
  886. * new operations on the dir while we write to disk.
  887. */
  888. int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
  889. {
  890. struct inode *inode = dentry->d_inode;
  891. struct btrfs_root *root = BTRFS_I(inode)->root;
  892. int ret = 0;
  893. struct btrfs_trans_handle *trans;
  894. /* we wait first, since the writeback may change the inode */
  895. root->log_batch++;
  896. /* the VFS called filemap_fdatawrite for us */
  897. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  898. root->log_batch++;
  899. /*
  900. * check the transaction that last modified this inode
  901. * and see if its already been committed
  902. */
  903. if (!BTRFS_I(inode)->last_trans)
  904. goto out;
  905. /*
  906. * if the last transaction that changed this file was before
  907. * the current transaction, we can bail out now without any
  908. * syncing
  909. */
  910. mutex_lock(&root->fs_info->trans_mutex);
  911. if (BTRFS_I(inode)->last_trans <=
  912. root->fs_info->last_trans_committed) {
  913. BTRFS_I(inode)->last_trans = 0;
  914. mutex_unlock(&root->fs_info->trans_mutex);
  915. goto out;
  916. }
  917. mutex_unlock(&root->fs_info->trans_mutex);
  918. /*
  919. * ok we haven't committed the transaction yet, lets do a commit
  920. */
  921. if (file && file->private_data)
  922. btrfs_ioctl_trans_end(file);
  923. trans = btrfs_start_transaction(root, 1);
  924. if (!trans) {
  925. ret = -ENOMEM;
  926. goto out;
  927. }
  928. ret = btrfs_log_dentry_safe(trans, root, dentry);
  929. if (ret < 0)
  930. goto out;
  931. /* we've logged all the items and now have a consistent
  932. * version of the file in the log. It is possible that
  933. * someone will come in and modify the file, but that's
  934. * fine because the log is consistent on disk, and we
  935. * have references to all of the file's extents
  936. *
  937. * It is possible that someone will come in and log the
  938. * file again, but that will end up using the synchronization
  939. * inside btrfs_sync_log to keep things safe.
  940. */
  941. mutex_unlock(&dentry->d_inode->i_mutex);
  942. if (ret != BTRFS_NO_LOG_SYNC) {
  943. if (ret > 0) {
  944. ret = btrfs_commit_transaction(trans, root);
  945. } else {
  946. ret = btrfs_sync_log(trans, root);
  947. if (ret == 0)
  948. ret = btrfs_end_transaction(trans, root);
  949. else
  950. ret = btrfs_commit_transaction(trans, root);
  951. }
  952. } else {
  953. ret = btrfs_end_transaction(trans, root);
  954. }
  955. mutex_lock(&dentry->d_inode->i_mutex);
  956. out:
  957. return ret > 0 ? EIO : ret;
  958. }
  959. static const struct vm_operations_struct btrfs_file_vm_ops = {
  960. .fault = filemap_fault,
  961. .page_mkwrite = btrfs_page_mkwrite,
  962. };
  963. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  964. {
  965. vma->vm_ops = &btrfs_file_vm_ops;
  966. file_accessed(filp);
  967. return 0;
  968. }
  969. const struct file_operations btrfs_file_operations = {
  970. .llseek = generic_file_llseek,
  971. .read = do_sync_read,
  972. .aio_read = generic_file_aio_read,
  973. .splice_read = generic_file_splice_read,
  974. .write = btrfs_file_write,
  975. .mmap = btrfs_file_mmap,
  976. .open = generic_file_open,
  977. .release = btrfs_release_file,
  978. .fsync = btrfs_sync_file,
  979. .unlocked_ioctl = btrfs_ioctl,
  980. #ifdef CONFIG_COMPAT
  981. .compat_ioctl = btrfs_ioctl,
  982. #endif
  983. };