file.c 31 KB

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