file.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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/smp_lock.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mpage.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/version.h>
  32. #include "ctree.h"
  33. #include "disk-io.h"
  34. #include "transaction.h"
  35. #include "btrfs_inode.h"
  36. #include "ioctl.h"
  37. #include "print-tree.h"
  38. #include "tree-log.h"
  39. #include "locking.h"
  40. #include "compat.h"
  41. /* simple helper to fault in pages and copy. This should go away
  42. * and be replaced with calls into generic code.
  43. */
  44. static int noinline btrfs_copy_from_user(loff_t pos, int num_pages,
  45. int write_bytes,
  46. struct page **prepared_pages,
  47. const char __user * buf)
  48. {
  49. long page_fault = 0;
  50. int i;
  51. int offset = pos & (PAGE_CACHE_SIZE - 1);
  52. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  53. size_t count = min_t(size_t,
  54. PAGE_CACHE_SIZE - offset, write_bytes);
  55. struct page *page = prepared_pages[i];
  56. fault_in_pages_readable(buf, count);
  57. /* Copy data from userspace to the current page */
  58. kmap(page);
  59. page_fault = __copy_from_user(page_address(page) + offset,
  60. buf, count);
  61. /* Flush processor's dcache for this page */
  62. flush_dcache_page(page);
  63. kunmap(page);
  64. buf += count;
  65. write_bytes -= count;
  66. if (page_fault)
  67. break;
  68. }
  69. return page_fault ? -EFAULT : 0;
  70. }
  71. /*
  72. * unlocks pages after btrfs_file_write is done with them
  73. */
  74. static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages)
  75. {
  76. size_t i;
  77. for (i = 0; i < num_pages; i++) {
  78. if (!pages[i])
  79. break;
  80. /* page checked is some magic around finding pages that
  81. * have been modified without going through btrfs_set_page_dirty
  82. * clear it here
  83. */
  84. ClearPageChecked(pages[i]);
  85. unlock_page(pages[i]);
  86. mark_page_accessed(pages[i]);
  87. page_cache_release(pages[i]);
  88. }
  89. }
  90. /*
  91. * after copy_from_user, pages need to be dirtied and we need to make
  92. * sure holes are created between the current EOF and the start of
  93. * any next extents (if required).
  94. *
  95. * this also makes the decision about creating an inline extent vs
  96. * doing real data extents, marking pages dirty and delalloc as required.
  97. */
  98. static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
  99. struct btrfs_root *root,
  100. struct file *file,
  101. struct page **pages,
  102. size_t num_pages,
  103. loff_t pos,
  104. size_t write_bytes)
  105. {
  106. int err = 0;
  107. int i;
  108. struct inode *inode = fdentry(file)->d_inode;
  109. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  110. u64 hint_byte;
  111. u64 num_bytes;
  112. u64 start_pos;
  113. u64 end_of_last_block;
  114. u64 end_pos = pos + write_bytes;
  115. loff_t isize = i_size_read(inode);
  116. start_pos = pos & ~((u64)root->sectorsize - 1);
  117. num_bytes = (write_bytes + pos - start_pos +
  118. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  119. end_of_last_block = start_pos + num_bytes - 1;
  120. lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  121. trans = btrfs_join_transaction(root, 1);
  122. if (!trans) {
  123. err = -ENOMEM;
  124. goto out_unlock;
  125. }
  126. btrfs_set_trans_block_group(trans, inode);
  127. hint_byte = 0;
  128. if ((end_of_last_block & 4095) == 0) {
  129. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  130. }
  131. set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  132. /* check for reserved extents on each page, we don't want
  133. * to reset the delalloc bit on things that already have
  134. * extents reserved.
  135. */
  136. btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
  137. for (i = 0; i < num_pages; i++) {
  138. struct page *p = pages[i];
  139. SetPageUptodate(p);
  140. ClearPageChecked(p);
  141. set_page_dirty(p);
  142. }
  143. if (end_pos > isize) {
  144. i_size_write(inode, end_pos);
  145. btrfs_update_inode(trans, root, inode);
  146. }
  147. err = btrfs_end_transaction(trans, root);
  148. out_unlock:
  149. unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  150. return err;
  151. }
  152. /*
  153. * this drops all the extents in the cache that intersect the range
  154. * [start, end]. Existing extents are split as required.
  155. */
  156. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  157. int skip_pinned)
  158. {
  159. struct extent_map *em;
  160. struct extent_map *split = NULL;
  161. struct extent_map *split2 = NULL;
  162. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  163. u64 len = end - start + 1;
  164. int ret;
  165. int testend = 1;
  166. unsigned long flags;
  167. int compressed = 0;
  168. WARN_ON(end < start);
  169. if (end == (u64)-1) {
  170. len = (u64)-1;
  171. testend = 0;
  172. }
  173. while(1) {
  174. if (!split)
  175. split = alloc_extent_map(GFP_NOFS);
  176. if (!split2)
  177. split2 = alloc_extent_map(GFP_NOFS);
  178. spin_lock(&em_tree->lock);
  179. em = lookup_extent_mapping(em_tree, start, len);
  180. if (!em) {
  181. spin_unlock(&em_tree->lock);
  182. break;
  183. }
  184. flags = em->flags;
  185. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  186. spin_unlock(&em_tree->lock);
  187. if (em->start <= start &&
  188. (!testend || em->start + em->len >= start + len)) {
  189. free_extent_map(em);
  190. break;
  191. }
  192. if (start < em->start) {
  193. len = em->start - start;
  194. } else {
  195. len = start + len - (em->start + em->len);
  196. start = em->start + em->len;
  197. }
  198. free_extent_map(em);
  199. continue;
  200. }
  201. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  202. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  203. remove_extent_mapping(em_tree, em);
  204. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  205. em->start < start) {
  206. split->start = em->start;
  207. split->len = start - em->start;
  208. split->block_start = em->block_start;
  209. if (compressed)
  210. split->block_len = em->block_len;
  211. else
  212. split->block_len = split->len;
  213. split->bdev = em->bdev;
  214. split->flags = flags;
  215. ret = add_extent_mapping(em_tree, split);
  216. BUG_ON(ret);
  217. free_extent_map(split);
  218. split = split2;
  219. split2 = NULL;
  220. }
  221. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  222. testend && em->start + em->len > start + len) {
  223. u64 diff = start + len - em->start;
  224. split->start = start + len;
  225. split->len = em->start + em->len - (start + len);
  226. split->bdev = em->bdev;
  227. split->flags = flags;
  228. if (compressed) {
  229. split->block_len = em->block_len;
  230. split->block_start = em->block_start;
  231. } else {
  232. split->block_len = split->len;
  233. split->block_start = em->block_start + diff;
  234. }
  235. ret = add_extent_mapping(em_tree, split);
  236. BUG_ON(ret);
  237. free_extent_map(split);
  238. split = NULL;
  239. }
  240. spin_unlock(&em_tree->lock);
  241. /* once for us */
  242. free_extent_map(em);
  243. /* once for the tree*/
  244. free_extent_map(em);
  245. }
  246. if (split)
  247. free_extent_map(split);
  248. if (split2)
  249. free_extent_map(split2);
  250. return 0;
  251. }
  252. int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
  253. {
  254. return 0;
  255. #if 0
  256. struct btrfs_path *path;
  257. struct btrfs_key found_key;
  258. struct extent_buffer *leaf;
  259. struct btrfs_file_extent_item *extent;
  260. u64 last_offset = 0;
  261. int nritems;
  262. int slot;
  263. int found_type;
  264. int ret;
  265. int err = 0;
  266. u64 extent_end = 0;
  267. path = btrfs_alloc_path();
  268. ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
  269. last_offset, 0);
  270. while(1) {
  271. nritems = btrfs_header_nritems(path->nodes[0]);
  272. if (path->slots[0] >= nritems) {
  273. ret = btrfs_next_leaf(root, path);
  274. if (ret)
  275. goto out;
  276. nritems = btrfs_header_nritems(path->nodes[0]);
  277. }
  278. slot = path->slots[0];
  279. leaf = path->nodes[0];
  280. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  281. if (found_key.objectid != inode->i_ino)
  282. break;
  283. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  284. goto out;
  285. if (found_key.offset < last_offset) {
  286. WARN_ON(1);
  287. btrfs_print_leaf(root, leaf);
  288. printk("inode %lu found offset %Lu expected %Lu\n",
  289. inode->i_ino, found_key.offset, last_offset);
  290. err = 1;
  291. goto out;
  292. }
  293. extent = btrfs_item_ptr(leaf, slot,
  294. struct btrfs_file_extent_item);
  295. found_type = btrfs_file_extent_type(leaf, extent);
  296. if (found_type == BTRFS_FILE_EXTENT_REG) {
  297. extent_end = found_key.offset +
  298. btrfs_file_extent_num_bytes(leaf, extent);
  299. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  300. struct btrfs_item *item;
  301. item = btrfs_item_nr(leaf, slot);
  302. extent_end = found_key.offset +
  303. btrfs_file_extent_inline_len(leaf, extent);
  304. extent_end = (extent_end + root->sectorsize - 1) &
  305. ~((u64)root->sectorsize -1 );
  306. }
  307. last_offset = extent_end;
  308. path->slots[0]++;
  309. }
  310. if (0 && last_offset < inode->i_size) {
  311. WARN_ON(1);
  312. btrfs_print_leaf(root, leaf);
  313. printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
  314. last_offset, inode->i_size);
  315. err = 1;
  316. }
  317. out:
  318. btrfs_free_path(path);
  319. return err;
  320. #endif
  321. }
  322. /*
  323. * this is very complex, but the basic idea is to drop all extents
  324. * in the range start - end. hint_block is filled in with a block number
  325. * that would be a good hint to the block allocator for this file.
  326. *
  327. * If an extent intersects the range but is not entirely inside the range
  328. * it is either truncated or split. Anything entirely inside the range
  329. * is deleted from the tree.
  330. *
  331. * inline_limit is used to tell this code which offsets in the file to keep
  332. * if they contain inline extents.
  333. */
  334. int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
  335. struct btrfs_root *root, struct inode *inode,
  336. u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
  337. {
  338. u64 extent_end = 0;
  339. u64 locked_end = end;
  340. u64 search_start = start;
  341. u64 leaf_start;
  342. u64 ram_bytes = 0;
  343. u64 orig_parent = 0;
  344. u64 disk_bytenr = 0;
  345. u8 compression;
  346. u8 encryption;
  347. u16 other_encoding = 0;
  348. u64 root_gen;
  349. u64 root_owner;
  350. struct extent_buffer *leaf;
  351. struct btrfs_file_extent_item *extent;
  352. struct btrfs_path *path;
  353. struct btrfs_key key;
  354. struct btrfs_file_extent_item old;
  355. int keep;
  356. int slot;
  357. int bookend;
  358. int found_type = 0;
  359. int found_extent;
  360. int found_inline;
  361. int recow;
  362. int ret;
  363. inline_limit = 0;
  364. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  365. path = btrfs_alloc_path();
  366. if (!path)
  367. return -ENOMEM;
  368. while(1) {
  369. recow = 0;
  370. btrfs_release_path(root, path);
  371. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  372. search_start, -1);
  373. if (ret < 0)
  374. goto out;
  375. if (ret > 0) {
  376. if (path->slots[0] == 0) {
  377. ret = 0;
  378. goto out;
  379. }
  380. path->slots[0]--;
  381. }
  382. next_slot:
  383. keep = 0;
  384. bookend = 0;
  385. found_extent = 0;
  386. found_inline = 0;
  387. leaf_start = 0;
  388. root_gen = 0;
  389. root_owner = 0;
  390. compression = 0;
  391. encryption = 0;
  392. extent = NULL;
  393. leaf = path->nodes[0];
  394. slot = path->slots[0];
  395. ret = 0;
  396. btrfs_item_key_to_cpu(leaf, &key, slot);
  397. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
  398. key.offset >= end) {
  399. goto out;
  400. }
  401. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  402. key.objectid != inode->i_ino) {
  403. goto out;
  404. }
  405. if (recow) {
  406. search_start = key.offset;
  407. continue;
  408. }
  409. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  410. extent = btrfs_item_ptr(leaf, slot,
  411. struct btrfs_file_extent_item);
  412. found_type = btrfs_file_extent_type(leaf, extent);
  413. compression = btrfs_file_extent_compression(leaf,
  414. extent);
  415. encryption = btrfs_file_extent_encryption(leaf,
  416. extent);
  417. other_encoding = btrfs_file_extent_other_encoding(leaf,
  418. extent);
  419. if (found_type == BTRFS_FILE_EXTENT_REG ||
  420. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  421. extent_end =
  422. btrfs_file_extent_disk_bytenr(leaf,
  423. extent);
  424. if (extent_end)
  425. *hint_byte = extent_end;
  426. extent_end = key.offset +
  427. btrfs_file_extent_num_bytes(leaf, extent);
  428. ram_bytes = btrfs_file_extent_ram_bytes(leaf,
  429. extent);
  430. found_extent = 1;
  431. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  432. found_inline = 1;
  433. extent_end = key.offset +
  434. btrfs_file_extent_inline_len(leaf, extent);
  435. }
  436. } else {
  437. extent_end = search_start;
  438. }
  439. /* we found nothing we can drop */
  440. if ((!found_extent && !found_inline) ||
  441. search_start >= extent_end) {
  442. int nextret;
  443. u32 nritems;
  444. nritems = btrfs_header_nritems(leaf);
  445. if (slot >= nritems - 1) {
  446. nextret = btrfs_next_leaf(root, path);
  447. if (nextret)
  448. goto out;
  449. recow = 1;
  450. } else {
  451. path->slots[0]++;
  452. }
  453. goto next_slot;
  454. }
  455. if (end <= extent_end && start >= key.offset && found_inline)
  456. *hint_byte = EXTENT_MAP_INLINE;
  457. if (found_extent) {
  458. read_extent_buffer(leaf, &old, (unsigned long)extent,
  459. sizeof(old));
  460. root_gen = btrfs_header_generation(leaf);
  461. root_owner = btrfs_header_owner(leaf);
  462. leaf_start = leaf->start;
  463. }
  464. if (end < extent_end && end >= key.offset) {
  465. bookend = 1;
  466. if (found_inline && start <= key.offset)
  467. keep = 1;
  468. }
  469. if (bookend && found_extent) {
  470. if (locked_end < extent_end) {
  471. ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
  472. locked_end, extent_end - 1,
  473. GFP_NOFS);
  474. if (!ret) {
  475. btrfs_release_path(root, path);
  476. lock_extent(&BTRFS_I(inode)->io_tree,
  477. locked_end, extent_end - 1,
  478. GFP_NOFS);
  479. locked_end = extent_end;
  480. continue;
  481. }
  482. locked_end = extent_end;
  483. }
  484. orig_parent = path->nodes[0]->start;
  485. disk_bytenr = le64_to_cpu(old.disk_bytenr);
  486. if (disk_bytenr != 0) {
  487. ret = btrfs_inc_extent_ref(trans, root,
  488. disk_bytenr,
  489. le64_to_cpu(old.disk_num_bytes),
  490. orig_parent, root->root_key.objectid,
  491. trans->transid, inode->i_ino);
  492. BUG_ON(ret);
  493. }
  494. }
  495. if (found_inline) {
  496. u64 mask = root->sectorsize - 1;
  497. search_start = (extent_end + mask) & ~mask;
  498. } else
  499. search_start = extent_end;
  500. /* truncate existing extent */
  501. if (start > key.offset) {
  502. u64 new_num;
  503. u64 old_num;
  504. keep = 1;
  505. WARN_ON(start & (root->sectorsize - 1));
  506. if (found_extent) {
  507. new_num = start - key.offset;
  508. old_num = btrfs_file_extent_num_bytes(leaf,
  509. extent);
  510. *hint_byte =
  511. btrfs_file_extent_disk_bytenr(leaf,
  512. extent);
  513. if (btrfs_file_extent_disk_bytenr(leaf,
  514. extent)) {
  515. inode_sub_bytes(inode, old_num -
  516. new_num);
  517. }
  518. if (!compression && !encryption) {
  519. btrfs_set_file_extent_ram_bytes(leaf,
  520. extent, new_num);
  521. }
  522. btrfs_set_file_extent_num_bytes(leaf,
  523. extent, new_num);
  524. btrfs_mark_buffer_dirty(leaf);
  525. } else if (key.offset < inline_limit &&
  526. (end > extent_end) &&
  527. (inline_limit < extent_end)) {
  528. u32 new_size;
  529. new_size = btrfs_file_extent_calc_inline_size(
  530. inline_limit - key.offset);
  531. inode_sub_bytes(inode, extent_end -
  532. inline_limit);
  533. btrfs_set_file_extent_ram_bytes(leaf, extent,
  534. new_size);
  535. if (!compression && !encryption) {
  536. btrfs_truncate_item(trans, root, path,
  537. new_size, 1);
  538. }
  539. }
  540. }
  541. /* delete the entire extent */
  542. if (!keep) {
  543. if (found_inline)
  544. inode_sub_bytes(inode, extent_end -
  545. key.offset);
  546. ret = btrfs_del_item(trans, root, path);
  547. /* TODO update progress marker and return */
  548. BUG_ON(ret);
  549. extent = NULL;
  550. btrfs_release_path(root, path);
  551. /* the extent will be freed later */
  552. }
  553. if (bookend && found_inline && start <= key.offset) {
  554. u32 new_size;
  555. new_size = btrfs_file_extent_calc_inline_size(
  556. extent_end - end);
  557. inode_sub_bytes(inode, end - key.offset);
  558. btrfs_set_file_extent_ram_bytes(leaf, extent,
  559. new_size);
  560. if (!compression && !encryption)
  561. ret = btrfs_truncate_item(trans, root, path,
  562. new_size, 0);
  563. BUG_ON(ret);
  564. }
  565. /* create bookend, splitting the extent in two */
  566. if (bookend && found_extent) {
  567. struct btrfs_key ins;
  568. ins.objectid = inode->i_ino;
  569. ins.offset = end;
  570. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  571. btrfs_release_path(root, path);
  572. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  573. sizeof(*extent));
  574. BUG_ON(ret);
  575. leaf = path->nodes[0];
  576. extent = btrfs_item_ptr(leaf, path->slots[0],
  577. struct btrfs_file_extent_item);
  578. write_extent_buffer(leaf, &old,
  579. (unsigned long)extent, sizeof(old));
  580. btrfs_set_file_extent_compression(leaf, extent,
  581. compression);
  582. btrfs_set_file_extent_encryption(leaf, extent,
  583. encryption);
  584. btrfs_set_file_extent_other_encoding(leaf, extent,
  585. other_encoding);
  586. btrfs_set_file_extent_offset(leaf, extent,
  587. le64_to_cpu(old.offset) + end - key.offset);
  588. WARN_ON(le64_to_cpu(old.num_bytes) <
  589. (extent_end - end));
  590. btrfs_set_file_extent_num_bytes(leaf, extent,
  591. extent_end - end);
  592. /*
  593. * set the ram bytes to the size of the full extent
  594. * before splitting. This is a worst case flag,
  595. * but its the best we can do because we don't know
  596. * how splitting affects compression
  597. */
  598. btrfs_set_file_extent_ram_bytes(leaf, extent,
  599. ram_bytes);
  600. btrfs_set_file_extent_type(leaf, extent, found_type);
  601. btrfs_mark_buffer_dirty(path->nodes[0]);
  602. if (disk_bytenr != 0) {
  603. ret = btrfs_update_extent_ref(trans, root,
  604. disk_bytenr, orig_parent,
  605. leaf->start,
  606. root->root_key.objectid,
  607. trans->transid, ins.objectid);
  608. BUG_ON(ret);
  609. }
  610. btrfs_release_path(root, path);
  611. if (disk_bytenr != 0) {
  612. inode_add_bytes(inode, extent_end - end);
  613. }
  614. }
  615. if (found_extent && !keep) {
  616. u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
  617. if (disk_bytenr != 0) {
  618. inode_sub_bytes(inode,
  619. le64_to_cpu(old.num_bytes));
  620. ret = btrfs_free_extent(trans, root,
  621. disk_bytenr,
  622. le64_to_cpu(old.disk_num_bytes),
  623. leaf_start, root_owner,
  624. root_gen, key.objectid, 0);
  625. BUG_ON(ret);
  626. *hint_byte = disk_bytenr;
  627. }
  628. }
  629. if (search_start >= end) {
  630. ret = 0;
  631. goto out;
  632. }
  633. }
  634. out:
  635. btrfs_free_path(path);
  636. if (locked_end > end) {
  637. unlock_extent(&BTRFS_I(inode)->io_tree, end, locked_end - 1,
  638. GFP_NOFS);
  639. }
  640. btrfs_check_file(root, inode);
  641. return ret;
  642. }
  643. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  644. u64 objectid, u64 bytenr, u64 *start, u64 *end)
  645. {
  646. struct btrfs_file_extent_item *fi;
  647. struct btrfs_key key;
  648. u64 extent_end;
  649. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  650. return 0;
  651. btrfs_item_key_to_cpu(leaf, &key, slot);
  652. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  653. return 0;
  654. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  655. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  656. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  657. btrfs_file_extent_compression(leaf, fi) ||
  658. btrfs_file_extent_encryption(leaf, fi) ||
  659. btrfs_file_extent_other_encoding(leaf, fi))
  660. return 0;
  661. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  662. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  663. return 0;
  664. *start = key.offset;
  665. *end = extent_end;
  666. return 1;
  667. }
  668. /*
  669. * Mark extent in the range start - end as written.
  670. *
  671. * This changes extent type from 'pre-allocated' to 'regular'. If only
  672. * part of extent is marked as written, the extent will be split into
  673. * two or three.
  674. */
  675. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  676. struct btrfs_root *root,
  677. struct inode *inode, u64 start, u64 end)
  678. {
  679. struct extent_buffer *leaf;
  680. struct btrfs_path *path;
  681. struct btrfs_file_extent_item *fi;
  682. struct btrfs_key key;
  683. u64 bytenr;
  684. u64 num_bytes;
  685. u64 extent_end;
  686. u64 extent_offset;
  687. u64 other_start;
  688. u64 other_end;
  689. u64 split = start;
  690. u64 locked_end = end;
  691. int extent_type;
  692. int split_end = 1;
  693. int ret;
  694. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  695. path = btrfs_alloc_path();
  696. BUG_ON(!path);
  697. again:
  698. key.objectid = inode->i_ino;
  699. key.type = BTRFS_EXTENT_DATA_KEY;
  700. if (split == start)
  701. key.offset = split;
  702. else
  703. key.offset = split - 1;
  704. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  705. if (ret > 0 && path->slots[0] > 0)
  706. path->slots[0]--;
  707. leaf = path->nodes[0];
  708. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  709. BUG_ON(key.objectid != inode->i_ino ||
  710. key.type != BTRFS_EXTENT_DATA_KEY);
  711. fi = btrfs_item_ptr(leaf, path->slots[0],
  712. struct btrfs_file_extent_item);
  713. extent_type = btrfs_file_extent_type(leaf, fi);
  714. BUG_ON(extent_type != BTRFS_FILE_EXTENT_PREALLOC);
  715. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  716. BUG_ON(key.offset > start || extent_end < end);
  717. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  718. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  719. extent_offset = btrfs_file_extent_offset(leaf, fi);
  720. if (key.offset == start)
  721. split = end;
  722. if (key.offset == start && extent_end == end) {
  723. int del_nr = 0;
  724. int del_slot = 0;
  725. u64 leaf_owner = btrfs_header_owner(leaf);
  726. u64 leaf_gen = btrfs_header_generation(leaf);
  727. other_start = end;
  728. other_end = 0;
  729. if (extent_mergeable(leaf, path->slots[0] + 1, inode->i_ino,
  730. bytenr, &other_start, &other_end)) {
  731. extent_end = other_end;
  732. del_slot = path->slots[0] + 1;
  733. del_nr++;
  734. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  735. leaf->start, leaf_owner,
  736. leaf_gen, inode->i_ino, 0);
  737. BUG_ON(ret);
  738. }
  739. other_start = 0;
  740. other_end = start;
  741. if (extent_mergeable(leaf, path->slots[0] - 1, inode->i_ino,
  742. bytenr, &other_start, &other_end)) {
  743. key.offset = other_start;
  744. del_slot = path->slots[0];
  745. del_nr++;
  746. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  747. leaf->start, leaf_owner,
  748. leaf_gen, inode->i_ino, 0);
  749. BUG_ON(ret);
  750. }
  751. split_end = 0;
  752. if (del_nr == 0) {
  753. btrfs_set_file_extent_type(leaf, fi,
  754. BTRFS_FILE_EXTENT_REG);
  755. goto done;
  756. }
  757. fi = btrfs_item_ptr(leaf, del_slot - 1,
  758. struct btrfs_file_extent_item);
  759. btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
  760. btrfs_set_file_extent_num_bytes(leaf, fi,
  761. extent_end - key.offset);
  762. btrfs_mark_buffer_dirty(leaf);
  763. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  764. BUG_ON(ret);
  765. goto done;
  766. } else if (split == start) {
  767. if (locked_end < extent_end) {
  768. ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
  769. locked_end, extent_end - 1, GFP_NOFS);
  770. if (!ret) {
  771. btrfs_release_path(root, path);
  772. lock_extent(&BTRFS_I(inode)->io_tree,
  773. locked_end, extent_end - 1, GFP_NOFS);
  774. locked_end = extent_end;
  775. goto again;
  776. }
  777. locked_end = extent_end;
  778. }
  779. btrfs_set_file_extent_num_bytes(leaf, fi, split - key.offset);
  780. extent_offset += split - key.offset;
  781. } else {
  782. BUG_ON(key.offset != start);
  783. btrfs_set_file_extent_offset(leaf, fi, extent_offset +
  784. split - key.offset);
  785. btrfs_set_file_extent_num_bytes(leaf, fi, extent_end - split);
  786. key.offset = split;
  787. btrfs_set_item_key_safe(trans, root, path, &key);
  788. extent_end = split;
  789. }
  790. if (extent_end == end) {
  791. split_end = 0;
  792. extent_type = BTRFS_FILE_EXTENT_REG;
  793. }
  794. if (extent_end == end && split == start) {
  795. other_start = end;
  796. other_end = 0;
  797. if (extent_mergeable(leaf, path->slots[0] + 1, inode->i_ino,
  798. bytenr, &other_start, &other_end)) {
  799. path->slots[0]++;
  800. fi = btrfs_item_ptr(leaf, path->slots[0],
  801. struct btrfs_file_extent_item);
  802. key.offset = split;
  803. btrfs_set_item_key_safe(trans, root, path, &key);
  804. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  805. btrfs_set_file_extent_num_bytes(leaf, fi,
  806. other_end - split);
  807. goto done;
  808. }
  809. }
  810. if (extent_end == end && split == end) {
  811. other_start = 0;
  812. other_end = start;
  813. if (extent_mergeable(leaf, path->slots[0] - 1 , inode->i_ino,
  814. bytenr, &other_start, &other_end)) {
  815. path->slots[0]--;
  816. fi = btrfs_item_ptr(leaf, path->slots[0],
  817. struct btrfs_file_extent_item);
  818. btrfs_set_file_extent_num_bytes(leaf, fi, extent_end -
  819. other_start);
  820. goto done;
  821. }
  822. }
  823. btrfs_mark_buffer_dirty(leaf);
  824. btrfs_release_path(root, path);
  825. key.offset = start;
  826. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*fi));
  827. BUG_ON(ret);
  828. leaf = path->nodes[0];
  829. fi = btrfs_item_ptr(leaf, path->slots[0],
  830. struct btrfs_file_extent_item);
  831. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  832. btrfs_set_file_extent_type(leaf, fi, extent_type);
  833. btrfs_set_file_extent_disk_bytenr(leaf, fi, bytenr);
  834. btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
  835. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  836. btrfs_set_file_extent_num_bytes(leaf, fi, extent_end - key.offset);
  837. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  838. btrfs_set_file_extent_compression(leaf, fi, 0);
  839. btrfs_set_file_extent_encryption(leaf, fi, 0);
  840. btrfs_set_file_extent_other_encoding(leaf, fi, 0);
  841. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
  842. leaf->start, root->root_key.objectid,
  843. trans->transid, inode->i_ino);
  844. BUG_ON(ret);
  845. done:
  846. btrfs_mark_buffer_dirty(leaf);
  847. btrfs_release_path(root, path);
  848. if (split_end && split == start) {
  849. split = end;
  850. goto again;
  851. }
  852. if (locked_end > end) {
  853. unlock_extent(&BTRFS_I(inode)->io_tree, end, locked_end - 1,
  854. GFP_NOFS);
  855. }
  856. btrfs_free_path(path);
  857. return 0;
  858. }
  859. /*
  860. * this gets pages into the page cache and locks them down, it also properly
  861. * waits for data=ordered extents to finish before allowing the pages to be
  862. * modified.
  863. */
  864. static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
  865. struct page **pages, size_t num_pages,
  866. loff_t pos, unsigned long first_index,
  867. unsigned long last_index, size_t write_bytes)
  868. {
  869. int i;
  870. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  871. struct inode *inode = fdentry(file)->d_inode;
  872. int err = 0;
  873. u64 start_pos;
  874. u64 last_pos;
  875. start_pos = pos & ~((u64)root->sectorsize - 1);
  876. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  877. if (start_pos > inode->i_size) {
  878. err = btrfs_cont_expand(inode, start_pos);
  879. if (err)
  880. return err;
  881. }
  882. memset(pages, 0, num_pages * sizeof(struct page *));
  883. again:
  884. for (i = 0; i < num_pages; i++) {
  885. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  886. if (!pages[i]) {
  887. err = -ENOMEM;
  888. BUG_ON(1);
  889. }
  890. wait_on_page_writeback(pages[i]);
  891. }
  892. if (start_pos < inode->i_size) {
  893. struct btrfs_ordered_extent *ordered;
  894. lock_extent(&BTRFS_I(inode)->io_tree,
  895. start_pos, last_pos - 1, GFP_NOFS);
  896. ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
  897. if (ordered &&
  898. ordered->file_offset + ordered->len > start_pos &&
  899. ordered->file_offset < last_pos) {
  900. btrfs_put_ordered_extent(ordered);
  901. unlock_extent(&BTRFS_I(inode)->io_tree,
  902. start_pos, last_pos - 1, GFP_NOFS);
  903. for (i = 0; i < num_pages; i++) {
  904. unlock_page(pages[i]);
  905. page_cache_release(pages[i]);
  906. }
  907. btrfs_wait_ordered_range(inode, start_pos,
  908. last_pos - start_pos);
  909. goto again;
  910. }
  911. if (ordered)
  912. btrfs_put_ordered_extent(ordered);
  913. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  914. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
  915. GFP_NOFS);
  916. unlock_extent(&BTRFS_I(inode)->io_tree,
  917. start_pos, last_pos - 1, GFP_NOFS);
  918. }
  919. for (i = 0; i < num_pages; i++) {
  920. clear_page_dirty_for_io(pages[i]);
  921. set_page_extent_mapped(pages[i]);
  922. WARN_ON(!PageLocked(pages[i]));
  923. }
  924. return 0;
  925. }
  926. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  927. size_t count, loff_t *ppos)
  928. {
  929. loff_t pos;
  930. loff_t start_pos;
  931. ssize_t num_written = 0;
  932. ssize_t err = 0;
  933. int ret = 0;
  934. struct inode *inode = fdentry(file)->d_inode;
  935. struct btrfs_root *root = BTRFS_I(inode)->root;
  936. struct page **pages = NULL;
  937. int nrptrs;
  938. struct page *pinned[2];
  939. unsigned long first_index;
  940. unsigned long last_index;
  941. int will_write;
  942. will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
  943. (file->f_flags & O_DIRECT));
  944. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  945. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  946. pinned[0] = NULL;
  947. pinned[1] = NULL;
  948. pos = *ppos;
  949. start_pos = pos;
  950. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  951. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  952. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  953. if (err)
  954. goto out_nolock;
  955. if (count == 0)
  956. goto out_nolock;
  957. err = file_remove_suid(file);
  958. if (err)
  959. goto out_nolock;
  960. file_update_time(file);
  961. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  962. mutex_lock(&inode->i_mutex);
  963. first_index = pos >> PAGE_CACHE_SHIFT;
  964. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  965. /*
  966. * if this is a nodatasum mount, force summing off for the inode
  967. * all the time. That way a later mount with summing on won't
  968. * get confused
  969. */
  970. if (btrfs_test_opt(root, NODATASUM))
  971. btrfs_set_flag(inode, NODATASUM);
  972. /*
  973. * there are lots of better ways to do this, but this code
  974. * makes sure the first and last page in the file range are
  975. * up to date and ready for cow
  976. */
  977. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  978. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  979. if (!PageUptodate(pinned[0])) {
  980. ret = btrfs_readpage(NULL, pinned[0]);
  981. BUG_ON(ret);
  982. wait_on_page_locked(pinned[0]);
  983. } else {
  984. unlock_page(pinned[0]);
  985. }
  986. }
  987. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  988. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  989. if (!PageUptodate(pinned[1])) {
  990. ret = btrfs_readpage(NULL, pinned[1]);
  991. BUG_ON(ret);
  992. wait_on_page_locked(pinned[1]);
  993. } else {
  994. unlock_page(pinned[1]);
  995. }
  996. }
  997. while(count > 0) {
  998. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  999. size_t write_bytes = min(count, nrptrs *
  1000. (size_t)PAGE_CACHE_SIZE -
  1001. offset);
  1002. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1003. PAGE_CACHE_SHIFT;
  1004. WARN_ON(num_pages > nrptrs);
  1005. memset(pages, 0, sizeof(pages));
  1006. ret = btrfs_check_free_space(root, write_bytes, 0);
  1007. if (ret)
  1008. goto out;
  1009. ret = prepare_pages(root, file, pages, num_pages,
  1010. pos, first_index, last_index,
  1011. write_bytes);
  1012. if (ret)
  1013. goto out;
  1014. ret = btrfs_copy_from_user(pos, num_pages,
  1015. write_bytes, pages, buf);
  1016. if (ret) {
  1017. btrfs_drop_pages(pages, num_pages);
  1018. goto out;
  1019. }
  1020. ret = dirty_and_release_pages(NULL, root, file, pages,
  1021. num_pages, pos, write_bytes);
  1022. btrfs_drop_pages(pages, num_pages);
  1023. if (ret)
  1024. goto out;
  1025. if (will_write) {
  1026. btrfs_fdatawrite_range(inode->i_mapping, pos,
  1027. pos + write_bytes - 1,
  1028. WB_SYNC_NONE);
  1029. } else {
  1030. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  1031. num_pages);
  1032. if (num_pages <
  1033. (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1034. btrfs_btree_balance_dirty(root, 1);
  1035. btrfs_throttle(root);
  1036. }
  1037. buf += write_bytes;
  1038. count -= write_bytes;
  1039. pos += write_bytes;
  1040. num_written += write_bytes;
  1041. cond_resched();
  1042. }
  1043. out:
  1044. mutex_unlock(&inode->i_mutex);
  1045. out_nolock:
  1046. kfree(pages);
  1047. if (pinned[0])
  1048. page_cache_release(pinned[0]);
  1049. if (pinned[1])
  1050. page_cache_release(pinned[1]);
  1051. *ppos = pos;
  1052. if (num_written > 0 && will_write) {
  1053. struct btrfs_trans_handle *trans;
  1054. err = btrfs_wait_ordered_range(inode, start_pos, num_written);
  1055. if (err)
  1056. num_written = err;
  1057. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  1058. trans = btrfs_start_transaction(root, 1);
  1059. ret = btrfs_log_dentry_safe(trans, root,
  1060. file->f_dentry);
  1061. if (ret == 0) {
  1062. btrfs_sync_log(trans, root);
  1063. btrfs_end_transaction(trans, root);
  1064. } else {
  1065. btrfs_commit_transaction(trans, root);
  1066. }
  1067. }
  1068. if (file->f_flags & O_DIRECT) {
  1069. invalidate_mapping_pages(inode->i_mapping,
  1070. start_pos >> PAGE_CACHE_SHIFT,
  1071. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  1072. }
  1073. }
  1074. current->backing_dev_info = NULL;
  1075. return num_written ? num_written : err;
  1076. }
  1077. int btrfs_release_file(struct inode * inode, struct file * filp)
  1078. {
  1079. if (filp->private_data)
  1080. btrfs_ioctl_trans_end(filp);
  1081. return 0;
  1082. }
  1083. /*
  1084. * fsync call for both files and directories. This logs the inode into
  1085. * the tree log instead of forcing full commits whenever possible.
  1086. *
  1087. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1088. * in the metadata btree are up to date for copying to the log.
  1089. *
  1090. * It drops the inode mutex before doing the tree log commit. This is an
  1091. * important optimization for directories because holding the mutex prevents
  1092. * new operations on the dir while we write to disk.
  1093. */
  1094. int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
  1095. {
  1096. struct inode *inode = dentry->d_inode;
  1097. struct btrfs_root *root = BTRFS_I(inode)->root;
  1098. int ret = 0;
  1099. struct btrfs_trans_handle *trans;
  1100. /*
  1101. * check the transaction that last modified this inode
  1102. * and see if its already been committed
  1103. */
  1104. if (!BTRFS_I(inode)->last_trans)
  1105. goto out;
  1106. mutex_lock(&root->fs_info->trans_mutex);
  1107. if (BTRFS_I(inode)->last_trans <=
  1108. root->fs_info->last_trans_committed) {
  1109. BTRFS_I(inode)->last_trans = 0;
  1110. mutex_unlock(&root->fs_info->trans_mutex);
  1111. goto out;
  1112. }
  1113. mutex_unlock(&root->fs_info->trans_mutex);
  1114. root->fs_info->tree_log_batch++;
  1115. filemap_fdatawait(inode->i_mapping);
  1116. root->fs_info->tree_log_batch++;
  1117. /*
  1118. * ok we haven't committed the transaction yet, lets do a commit
  1119. */
  1120. if (file->private_data)
  1121. btrfs_ioctl_trans_end(file);
  1122. trans = btrfs_start_transaction(root, 1);
  1123. if (!trans) {
  1124. ret = -ENOMEM;
  1125. goto out;
  1126. }
  1127. ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
  1128. if (ret < 0) {
  1129. goto out;
  1130. }
  1131. /* we've logged all the items and now have a consistent
  1132. * version of the file in the log. It is possible that
  1133. * someone will come in and modify the file, but that's
  1134. * fine because the log is consistent on disk, and we
  1135. * have references to all of the file's extents
  1136. *
  1137. * It is possible that someone will come in and log the
  1138. * file again, but that will end up using the synchronization
  1139. * inside btrfs_sync_log to keep things safe.
  1140. */
  1141. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  1142. if (ret > 0) {
  1143. ret = btrfs_commit_transaction(trans, root);
  1144. } else {
  1145. btrfs_sync_log(trans, root);
  1146. ret = btrfs_end_transaction(trans, root);
  1147. }
  1148. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  1149. out:
  1150. return ret > 0 ? EIO : ret;
  1151. }
  1152. static struct vm_operations_struct btrfs_file_vm_ops = {
  1153. .fault = filemap_fault,
  1154. .page_mkwrite = btrfs_page_mkwrite,
  1155. };
  1156. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1157. {
  1158. vma->vm_ops = &btrfs_file_vm_ops;
  1159. file_accessed(filp);
  1160. return 0;
  1161. }
  1162. struct file_operations btrfs_file_operations = {
  1163. .llseek = generic_file_llseek,
  1164. .read = do_sync_read,
  1165. .aio_read = generic_file_aio_read,
  1166. .splice_read = generic_file_splice_read,
  1167. .write = btrfs_file_write,
  1168. .mmap = btrfs_file_mmap,
  1169. .open = generic_file_open,
  1170. .release = btrfs_release_file,
  1171. .fsync = btrfs_sync_file,
  1172. .unlocked_ioctl = btrfs_ioctl,
  1173. #ifdef CONFIG_COMPAT
  1174. .compat_ioctl = btrfs_ioctl,
  1175. #endif
  1176. };