file.c 31 KB

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