file.c 29 KB

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