file.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 orig_offset,
  458. u64 *start, u64 *end)
  459. {
  460. struct btrfs_file_extent_item *fi;
  461. struct btrfs_key key;
  462. u64 extent_end;
  463. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  464. return 0;
  465. btrfs_item_key_to_cpu(leaf, &key, slot);
  466. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  467. return 0;
  468. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  469. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  470. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  471. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  472. btrfs_file_extent_compression(leaf, fi) ||
  473. btrfs_file_extent_encryption(leaf, fi) ||
  474. btrfs_file_extent_other_encoding(leaf, fi))
  475. return 0;
  476. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  477. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  478. return 0;
  479. *start = key.offset;
  480. *end = extent_end;
  481. return 1;
  482. }
  483. /*
  484. * Mark extent in the range start - end as written.
  485. *
  486. * This changes extent type from 'pre-allocated' to 'regular'. If only
  487. * part of extent is marked as written, the extent will be split into
  488. * two or three.
  489. */
  490. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  491. struct inode *inode, u64 start, u64 end)
  492. {
  493. struct btrfs_root *root = BTRFS_I(inode)->root;
  494. struct extent_buffer *leaf;
  495. struct btrfs_path *path;
  496. struct btrfs_file_extent_item *fi;
  497. struct btrfs_key key;
  498. struct btrfs_key new_key;
  499. u64 bytenr;
  500. u64 num_bytes;
  501. u64 extent_end;
  502. u64 orig_offset;
  503. u64 other_start;
  504. u64 other_end;
  505. u64 split;
  506. int del_nr = 0;
  507. int del_slot = 0;
  508. int recow;
  509. int ret;
  510. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  511. path = btrfs_alloc_path();
  512. BUG_ON(!path);
  513. again:
  514. recow = 0;
  515. split = start;
  516. key.objectid = inode->i_ino;
  517. key.type = BTRFS_EXTENT_DATA_KEY;
  518. key.offset = split;
  519. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  520. if (ret > 0 && path->slots[0] > 0)
  521. path->slots[0]--;
  522. leaf = path->nodes[0];
  523. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  524. BUG_ON(key.objectid != inode->i_ino ||
  525. key.type != BTRFS_EXTENT_DATA_KEY);
  526. fi = btrfs_item_ptr(leaf, path->slots[0],
  527. struct btrfs_file_extent_item);
  528. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  529. BTRFS_FILE_EXTENT_PREALLOC);
  530. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  531. BUG_ON(key.offset > start || extent_end < end);
  532. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  533. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  534. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  535. memcpy(&new_key, &key, sizeof(new_key));
  536. if (start == key.offset && end < extent_end) {
  537. other_start = 0;
  538. other_end = start;
  539. if (extent_mergeable(leaf, path->slots[0] - 1,
  540. inode->i_ino, bytenr, orig_offset,
  541. &other_start, &other_end)) {
  542. new_key.offset = end;
  543. btrfs_set_item_key_safe(trans, root, path, &new_key);
  544. fi = btrfs_item_ptr(leaf, path->slots[0],
  545. struct btrfs_file_extent_item);
  546. btrfs_set_file_extent_num_bytes(leaf, fi,
  547. extent_end - end);
  548. btrfs_set_file_extent_offset(leaf, fi,
  549. end - orig_offset);
  550. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  551. struct btrfs_file_extent_item);
  552. btrfs_set_file_extent_num_bytes(leaf, fi,
  553. end - other_start);
  554. btrfs_mark_buffer_dirty(leaf);
  555. goto out;
  556. }
  557. }
  558. if (start > key.offset && end == extent_end) {
  559. other_start = end;
  560. other_end = 0;
  561. if (extent_mergeable(leaf, path->slots[0] + 1,
  562. inode->i_ino, bytenr, orig_offset,
  563. &other_start, &other_end)) {
  564. fi = btrfs_item_ptr(leaf, path->slots[0],
  565. struct btrfs_file_extent_item);
  566. btrfs_set_file_extent_num_bytes(leaf, fi,
  567. start - key.offset);
  568. path->slots[0]++;
  569. new_key.offset = start;
  570. btrfs_set_item_key_safe(trans, root, path, &new_key);
  571. fi = btrfs_item_ptr(leaf, path->slots[0],
  572. struct btrfs_file_extent_item);
  573. btrfs_set_file_extent_num_bytes(leaf, fi,
  574. other_end - start);
  575. btrfs_set_file_extent_offset(leaf, fi,
  576. start - orig_offset);
  577. btrfs_mark_buffer_dirty(leaf);
  578. goto out;
  579. }
  580. }
  581. while (start > key.offset || end < extent_end) {
  582. if (key.offset == start)
  583. split = end;
  584. new_key.offset = split;
  585. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  586. if (ret == -EAGAIN) {
  587. btrfs_release_path(root, path);
  588. goto again;
  589. }
  590. BUG_ON(ret < 0);
  591. leaf = path->nodes[0];
  592. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  593. struct btrfs_file_extent_item);
  594. btrfs_set_file_extent_num_bytes(leaf, fi,
  595. split - key.offset);
  596. fi = btrfs_item_ptr(leaf, path->slots[0],
  597. struct btrfs_file_extent_item);
  598. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  599. btrfs_set_file_extent_num_bytes(leaf, fi,
  600. extent_end - split);
  601. btrfs_mark_buffer_dirty(leaf);
  602. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  603. root->root_key.objectid,
  604. inode->i_ino, orig_offset);
  605. BUG_ON(ret);
  606. if (split == start) {
  607. key.offset = start;
  608. } else {
  609. BUG_ON(start != key.offset);
  610. path->slots[0]--;
  611. extent_end = end;
  612. }
  613. recow = 1;
  614. }
  615. other_start = end;
  616. other_end = 0;
  617. if (extent_mergeable(leaf, path->slots[0] + 1,
  618. inode->i_ino, bytenr, orig_offset,
  619. &other_start, &other_end)) {
  620. if (recow) {
  621. btrfs_release_path(root, path);
  622. goto again;
  623. }
  624. extent_end = other_end;
  625. del_slot = path->slots[0] + 1;
  626. del_nr++;
  627. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  628. 0, root->root_key.objectid,
  629. inode->i_ino, orig_offset);
  630. BUG_ON(ret);
  631. }
  632. other_start = 0;
  633. other_end = start;
  634. if (extent_mergeable(leaf, path->slots[0] - 1,
  635. inode->i_ino, bytenr, orig_offset,
  636. &other_start, &other_end)) {
  637. if (recow) {
  638. btrfs_release_path(root, path);
  639. goto again;
  640. }
  641. key.offset = other_start;
  642. del_slot = path->slots[0];
  643. del_nr++;
  644. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  645. 0, root->root_key.objectid,
  646. inode->i_ino, orig_offset);
  647. BUG_ON(ret);
  648. }
  649. fi = btrfs_item_ptr(leaf, path->slots[0],
  650. struct btrfs_file_extent_item);
  651. if (del_nr == 0) {
  652. btrfs_set_file_extent_type(leaf, fi,
  653. BTRFS_FILE_EXTENT_REG);
  654. btrfs_mark_buffer_dirty(leaf);
  655. } else {
  656. btrfs_set_file_extent_type(leaf, fi,
  657. BTRFS_FILE_EXTENT_REG);
  658. btrfs_set_file_extent_num_bytes(leaf, fi,
  659. extent_end - key.offset);
  660. btrfs_mark_buffer_dirty(leaf);
  661. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  662. BUG_ON(ret);
  663. }
  664. out:
  665. btrfs_free_path(path);
  666. return 0;
  667. }
  668. /*
  669. * this gets pages into the page cache and locks them down, it also properly
  670. * waits for data=ordered extents to finish before allowing the pages to be
  671. * modified.
  672. */
  673. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  674. struct page **pages, size_t num_pages,
  675. loff_t pos, unsigned long first_index,
  676. unsigned long last_index, size_t write_bytes)
  677. {
  678. int i;
  679. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  680. struct inode *inode = fdentry(file)->d_inode;
  681. int err = 0;
  682. u64 start_pos;
  683. u64 last_pos;
  684. start_pos = pos & ~((u64)root->sectorsize - 1);
  685. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  686. if (start_pos > inode->i_size) {
  687. err = btrfs_cont_expand(inode, start_pos);
  688. if (err)
  689. return err;
  690. }
  691. memset(pages, 0, num_pages * sizeof(struct page *));
  692. again:
  693. for (i = 0; i < num_pages; i++) {
  694. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  695. if (!pages[i]) {
  696. err = -ENOMEM;
  697. BUG_ON(1);
  698. }
  699. wait_on_page_writeback(pages[i]);
  700. }
  701. if (start_pos < inode->i_size) {
  702. struct btrfs_ordered_extent *ordered;
  703. lock_extent(&BTRFS_I(inode)->io_tree,
  704. start_pos, last_pos - 1, GFP_NOFS);
  705. ordered = btrfs_lookup_first_ordered_extent(inode,
  706. last_pos - 1);
  707. if (ordered &&
  708. ordered->file_offset + ordered->len > start_pos &&
  709. ordered->file_offset < last_pos) {
  710. btrfs_put_ordered_extent(ordered);
  711. unlock_extent(&BTRFS_I(inode)->io_tree,
  712. start_pos, last_pos - 1, GFP_NOFS);
  713. for (i = 0; i < num_pages; i++) {
  714. unlock_page(pages[i]);
  715. page_cache_release(pages[i]);
  716. }
  717. btrfs_wait_ordered_range(inode, start_pos,
  718. last_pos - start_pos);
  719. goto again;
  720. }
  721. if (ordered)
  722. btrfs_put_ordered_extent(ordered);
  723. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  724. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  725. EXTENT_DO_ACCOUNTING,
  726. GFP_NOFS);
  727. unlock_extent(&BTRFS_I(inode)->io_tree,
  728. start_pos, last_pos - 1, GFP_NOFS);
  729. }
  730. for (i = 0; i < num_pages; i++) {
  731. clear_page_dirty_for_io(pages[i]);
  732. set_page_extent_mapped(pages[i]);
  733. WARN_ON(!PageLocked(pages[i]));
  734. }
  735. return 0;
  736. }
  737. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  738. size_t count, loff_t *ppos)
  739. {
  740. loff_t pos;
  741. loff_t start_pos;
  742. ssize_t num_written = 0;
  743. ssize_t err = 0;
  744. int ret = 0;
  745. struct inode *inode = fdentry(file)->d_inode;
  746. struct btrfs_root *root = BTRFS_I(inode)->root;
  747. struct page **pages = NULL;
  748. int nrptrs;
  749. struct page *pinned[2];
  750. unsigned long first_index;
  751. unsigned long last_index;
  752. int will_write;
  753. will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
  754. (file->f_flags & O_DIRECT));
  755. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  756. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  757. pinned[0] = NULL;
  758. pinned[1] = NULL;
  759. pos = *ppos;
  760. start_pos = pos;
  761. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  762. /* do the reserve before the mutex lock in case we have to do some
  763. * flushing. We wouldn't deadlock, but this is more polite.
  764. */
  765. err = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  766. if (err)
  767. goto out_nolock;
  768. mutex_lock(&inode->i_mutex);
  769. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  770. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  771. if (err)
  772. goto out;
  773. if (count == 0)
  774. goto out;
  775. err = file_remove_suid(file);
  776. if (err)
  777. goto out;
  778. file_update_time(file);
  779. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  780. /* generic_write_checks can change our pos */
  781. start_pos = pos;
  782. BTRFS_I(inode)->sequence++;
  783. first_index = pos >> PAGE_CACHE_SHIFT;
  784. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  785. /*
  786. * there are lots of better ways to do this, but this code
  787. * makes sure the first and last page in the file range are
  788. * up to date and ready for cow
  789. */
  790. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  791. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  792. if (!PageUptodate(pinned[0])) {
  793. ret = btrfs_readpage(NULL, pinned[0]);
  794. BUG_ON(ret);
  795. wait_on_page_locked(pinned[0]);
  796. } else {
  797. unlock_page(pinned[0]);
  798. }
  799. }
  800. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  801. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  802. if (!PageUptodate(pinned[1])) {
  803. ret = btrfs_readpage(NULL, pinned[1]);
  804. BUG_ON(ret);
  805. wait_on_page_locked(pinned[1]);
  806. } else {
  807. unlock_page(pinned[1]);
  808. }
  809. }
  810. while (count > 0) {
  811. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  812. size_t write_bytes = min(count, nrptrs *
  813. (size_t)PAGE_CACHE_SIZE -
  814. offset);
  815. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  816. PAGE_CACHE_SHIFT;
  817. WARN_ON(num_pages > nrptrs);
  818. memset(pages, 0, sizeof(struct page *) * nrptrs);
  819. ret = btrfs_check_data_free_space(root, inode, write_bytes);
  820. if (ret)
  821. goto out;
  822. ret = prepare_pages(root, file, pages, num_pages,
  823. pos, first_index, last_index,
  824. write_bytes);
  825. if (ret) {
  826. btrfs_free_reserved_data_space(root, inode,
  827. write_bytes);
  828. goto out;
  829. }
  830. ret = btrfs_copy_from_user(pos, num_pages,
  831. write_bytes, pages, buf);
  832. if (ret) {
  833. btrfs_free_reserved_data_space(root, inode,
  834. write_bytes);
  835. btrfs_drop_pages(pages, num_pages);
  836. goto out;
  837. }
  838. ret = dirty_and_release_pages(NULL, root, file, pages,
  839. num_pages, pos, write_bytes);
  840. btrfs_drop_pages(pages, num_pages);
  841. if (ret) {
  842. btrfs_free_reserved_data_space(root, inode,
  843. write_bytes);
  844. goto out;
  845. }
  846. if (will_write) {
  847. filemap_fdatawrite_range(inode->i_mapping, pos,
  848. pos + write_bytes - 1);
  849. } else {
  850. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  851. num_pages);
  852. if (num_pages <
  853. (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  854. btrfs_btree_balance_dirty(root, 1);
  855. btrfs_throttle(root);
  856. }
  857. buf += write_bytes;
  858. count -= write_bytes;
  859. pos += write_bytes;
  860. num_written += write_bytes;
  861. cond_resched();
  862. }
  863. out:
  864. mutex_unlock(&inode->i_mutex);
  865. if (ret)
  866. err = ret;
  867. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  868. out_nolock:
  869. kfree(pages);
  870. if (pinned[0])
  871. page_cache_release(pinned[0]);
  872. if (pinned[1])
  873. page_cache_release(pinned[1]);
  874. *ppos = pos;
  875. /*
  876. * we want to make sure fsync finds this change
  877. * but we haven't joined a transaction running right now.
  878. *
  879. * Later on, someone is sure to update the inode and get the
  880. * real transid recorded.
  881. *
  882. * We set last_trans now to the fs_info generation + 1,
  883. * this will either be one more than the running transaction
  884. * or the generation used for the next transaction if there isn't
  885. * one running right now.
  886. */
  887. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  888. if (num_written > 0 && will_write) {
  889. struct btrfs_trans_handle *trans;
  890. err = btrfs_wait_ordered_range(inode, start_pos, num_written);
  891. if (err)
  892. num_written = err;
  893. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  894. trans = btrfs_start_transaction(root, 1);
  895. ret = btrfs_log_dentry_safe(trans, root,
  896. file->f_dentry);
  897. if (ret == 0) {
  898. ret = btrfs_sync_log(trans, root);
  899. if (ret == 0)
  900. btrfs_end_transaction(trans, root);
  901. else
  902. btrfs_commit_transaction(trans, root);
  903. } else if (ret != BTRFS_NO_LOG_SYNC) {
  904. btrfs_commit_transaction(trans, root);
  905. } else {
  906. btrfs_end_transaction(trans, root);
  907. }
  908. }
  909. if (file->f_flags & O_DIRECT) {
  910. invalidate_mapping_pages(inode->i_mapping,
  911. start_pos >> PAGE_CACHE_SHIFT,
  912. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  913. }
  914. }
  915. current->backing_dev_info = NULL;
  916. return num_written ? num_written : err;
  917. }
  918. int btrfs_release_file(struct inode *inode, struct file *filp)
  919. {
  920. /*
  921. * ordered_data_close is set by settattr when we are about to truncate
  922. * a file from a non-zero size to a zero size. This tries to
  923. * flush down new bytes that may have been written if the
  924. * application were using truncate to replace a file in place.
  925. */
  926. if (BTRFS_I(inode)->ordered_data_close) {
  927. BTRFS_I(inode)->ordered_data_close = 0;
  928. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  929. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  930. filemap_flush(inode->i_mapping);
  931. }
  932. if (filp->private_data)
  933. btrfs_ioctl_trans_end(filp);
  934. return 0;
  935. }
  936. /*
  937. * fsync call for both files and directories. This logs the inode into
  938. * the tree log instead of forcing full commits whenever possible.
  939. *
  940. * It needs to call filemap_fdatawait so that all ordered extent updates are
  941. * in the metadata btree are up to date for copying to the log.
  942. *
  943. * It drops the inode mutex before doing the tree log commit. This is an
  944. * important optimization for directories because holding the mutex prevents
  945. * new operations on the dir while we write to disk.
  946. */
  947. int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
  948. {
  949. struct inode *inode = dentry->d_inode;
  950. struct btrfs_root *root = BTRFS_I(inode)->root;
  951. int ret = 0;
  952. struct btrfs_trans_handle *trans;
  953. /* we wait first, since the writeback may change the inode */
  954. root->log_batch++;
  955. /* the VFS called filemap_fdatawrite for us */
  956. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  957. root->log_batch++;
  958. /*
  959. * check the transaction that last modified this inode
  960. * and see if its already been committed
  961. */
  962. if (!BTRFS_I(inode)->last_trans)
  963. goto out;
  964. /*
  965. * if the last transaction that changed this file was before
  966. * the current transaction, we can bail out now without any
  967. * syncing
  968. */
  969. mutex_lock(&root->fs_info->trans_mutex);
  970. if (BTRFS_I(inode)->last_trans <=
  971. root->fs_info->last_trans_committed) {
  972. BTRFS_I(inode)->last_trans = 0;
  973. mutex_unlock(&root->fs_info->trans_mutex);
  974. goto out;
  975. }
  976. mutex_unlock(&root->fs_info->trans_mutex);
  977. /*
  978. * ok we haven't committed the transaction yet, lets do a commit
  979. */
  980. if (file && file->private_data)
  981. btrfs_ioctl_trans_end(file);
  982. trans = btrfs_start_transaction(root, 1);
  983. if (!trans) {
  984. ret = -ENOMEM;
  985. goto out;
  986. }
  987. ret = btrfs_log_dentry_safe(trans, root, dentry);
  988. if (ret < 0)
  989. goto out;
  990. /* we've logged all the items and now have a consistent
  991. * version of the file in the log. It is possible that
  992. * someone will come in and modify the file, but that's
  993. * fine because the log is consistent on disk, and we
  994. * have references to all of the file's extents
  995. *
  996. * It is possible that someone will come in and log the
  997. * file again, but that will end up using the synchronization
  998. * inside btrfs_sync_log to keep things safe.
  999. */
  1000. mutex_unlock(&dentry->d_inode->i_mutex);
  1001. if (ret != BTRFS_NO_LOG_SYNC) {
  1002. if (ret > 0) {
  1003. ret = btrfs_commit_transaction(trans, root);
  1004. } else {
  1005. ret = btrfs_sync_log(trans, root);
  1006. if (ret == 0)
  1007. ret = btrfs_end_transaction(trans, root);
  1008. else
  1009. ret = btrfs_commit_transaction(trans, root);
  1010. }
  1011. } else {
  1012. ret = btrfs_end_transaction(trans, root);
  1013. }
  1014. mutex_lock(&dentry->d_inode->i_mutex);
  1015. out:
  1016. return ret > 0 ? -EIO : ret;
  1017. }
  1018. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1019. .fault = filemap_fault,
  1020. .page_mkwrite = btrfs_page_mkwrite,
  1021. };
  1022. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1023. {
  1024. vma->vm_ops = &btrfs_file_vm_ops;
  1025. file_accessed(filp);
  1026. return 0;
  1027. }
  1028. const struct file_operations btrfs_file_operations = {
  1029. .llseek = generic_file_llseek,
  1030. .read = do_sync_read,
  1031. .aio_read = generic_file_aio_read,
  1032. .splice_read = generic_file_splice_read,
  1033. .write = btrfs_file_write,
  1034. .mmap = btrfs_file_mmap,
  1035. .open = generic_file_open,
  1036. .release = btrfs_release_file,
  1037. .fsync = btrfs_sync_file,
  1038. .unlocked_ioctl = btrfs_ioctl,
  1039. #ifdef CONFIG_COMPAT
  1040. .compat_ioctl = btrfs_ioctl,
  1041. #endif
  1042. };