file.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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/falloc.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/slab.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 noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
  45. size_t write_bytes,
  46. struct page **prepared_pages,
  47. struct iov_iter *i)
  48. {
  49. size_t copied = 0;
  50. size_t total_copied = 0;
  51. int pg = 0;
  52. int offset = pos & (PAGE_CACHE_SIZE - 1);
  53. while (write_bytes > 0) {
  54. size_t count = min_t(size_t,
  55. PAGE_CACHE_SIZE - offset, write_bytes);
  56. struct page *page = prepared_pages[pg];
  57. /*
  58. * Copy data from userspace to the current page
  59. *
  60. * Disable pagefault to avoid recursive lock since
  61. * the pages are already locked
  62. */
  63. pagefault_disable();
  64. copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
  65. pagefault_enable();
  66. /* Flush processor's dcache for this page */
  67. flush_dcache_page(page);
  68. /*
  69. * if we get a partial write, we can end up with
  70. * partially up to date pages. These add
  71. * a lot of complexity, so make sure they don't
  72. * happen by forcing this copy to be retried.
  73. *
  74. * The rest of the btrfs_file_write code will fall
  75. * back to page at a time copies after we return 0.
  76. */
  77. if (!PageUptodate(page) && copied < count)
  78. copied = 0;
  79. iov_iter_advance(i, copied);
  80. write_bytes -= copied;
  81. total_copied += copied;
  82. /* Return to btrfs_file_aio_write to fault page */
  83. if (unlikely(copied == 0))
  84. break;
  85. if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
  86. offset += copied;
  87. } else {
  88. pg++;
  89. offset = 0;
  90. }
  91. }
  92. return total_copied;
  93. }
  94. /*
  95. * unlocks pages after btrfs_file_write is done with them
  96. */
  97. static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
  98. {
  99. size_t i;
  100. for (i = 0; i < num_pages; i++) {
  101. /* page checked is some magic around finding pages that
  102. * have been modified without going through btrfs_set_page_dirty
  103. * clear it here
  104. */
  105. ClearPageChecked(pages[i]);
  106. unlock_page(pages[i]);
  107. mark_page_accessed(pages[i]);
  108. page_cache_release(pages[i]);
  109. }
  110. }
  111. /*
  112. * after copy_from_user, pages need to be dirtied and we need to make
  113. * sure holes are created between the current EOF and the start of
  114. * any next extents (if required).
  115. *
  116. * this also makes the decision about creating an inline extent vs
  117. * doing real data extents, marking pages dirty and delalloc as required.
  118. */
  119. static noinline int dirty_and_release_pages(struct btrfs_root *root,
  120. struct file *file,
  121. struct page **pages,
  122. size_t num_pages,
  123. loff_t pos,
  124. size_t write_bytes)
  125. {
  126. int err = 0;
  127. int i;
  128. struct inode *inode = fdentry(file)->d_inode;
  129. u64 num_bytes;
  130. u64 start_pos;
  131. u64 end_of_last_block;
  132. u64 end_pos = pos + write_bytes;
  133. loff_t isize = i_size_read(inode);
  134. start_pos = pos & ~((u64)root->sectorsize - 1);
  135. num_bytes = (write_bytes + pos - start_pos +
  136. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  137. end_of_last_block = start_pos + num_bytes - 1;
  138. err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
  139. NULL);
  140. if (err)
  141. return err;
  142. for (i = 0; i < num_pages; i++) {
  143. struct page *p = pages[i];
  144. SetPageUptodate(p);
  145. ClearPageChecked(p);
  146. set_page_dirty(p);
  147. }
  148. /*
  149. * we've only changed i_size in ram, and we haven't updated
  150. * the disk i_size. There is no need to log the inode
  151. * at this time.
  152. */
  153. if (end_pos > isize)
  154. i_size_write(inode, end_pos);
  155. return 0;
  156. }
  157. /*
  158. * this drops all the extents in the cache that intersect the range
  159. * [start, end]. Existing extents are split as required.
  160. */
  161. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  162. int skip_pinned)
  163. {
  164. struct extent_map *em;
  165. struct extent_map *split = NULL;
  166. struct extent_map *split2 = NULL;
  167. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  168. u64 len = end - start + 1;
  169. int ret;
  170. int testend = 1;
  171. unsigned long flags;
  172. int compressed = 0;
  173. WARN_ON(end < start);
  174. if (end == (u64)-1) {
  175. len = (u64)-1;
  176. testend = 0;
  177. }
  178. while (1) {
  179. if (!split)
  180. split = alloc_extent_map(GFP_NOFS);
  181. if (!split2)
  182. split2 = alloc_extent_map(GFP_NOFS);
  183. BUG_ON(!split || !split2);
  184. write_lock(&em_tree->lock);
  185. em = lookup_extent_mapping(em_tree, start, len);
  186. if (!em) {
  187. write_unlock(&em_tree->lock);
  188. break;
  189. }
  190. flags = em->flags;
  191. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  192. if (testend && em->start + em->len >= start + len) {
  193. free_extent_map(em);
  194. write_unlock(&em_tree->lock);
  195. break;
  196. }
  197. start = em->start + em->len;
  198. if (testend)
  199. len = start + len - (em->start + em->len);
  200. free_extent_map(em);
  201. write_unlock(&em_tree->lock);
  202. continue;
  203. }
  204. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  205. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  206. remove_extent_mapping(em_tree, em);
  207. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  208. em->start < start) {
  209. split->start = em->start;
  210. split->len = start - em->start;
  211. split->orig_start = em->orig_start;
  212. split->block_start = em->block_start;
  213. if (compressed)
  214. split->block_len = em->block_len;
  215. else
  216. split->block_len = split->len;
  217. split->bdev = em->bdev;
  218. split->flags = flags;
  219. split->compress_type = em->compress_type;
  220. ret = add_extent_mapping(em_tree, split);
  221. BUG_ON(ret);
  222. free_extent_map(split);
  223. split = split2;
  224. split2 = NULL;
  225. }
  226. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  227. testend && em->start + em->len > start + len) {
  228. u64 diff = start + len - em->start;
  229. split->start = start + len;
  230. split->len = em->start + em->len - (start + len);
  231. split->bdev = em->bdev;
  232. split->flags = flags;
  233. split->compress_type = em->compress_type;
  234. if (compressed) {
  235. split->block_len = em->block_len;
  236. split->block_start = em->block_start;
  237. split->orig_start = em->orig_start;
  238. } else {
  239. split->block_len = split->len;
  240. split->block_start = em->block_start + diff;
  241. split->orig_start = split->start;
  242. }
  243. ret = add_extent_mapping(em_tree, split);
  244. BUG_ON(ret);
  245. free_extent_map(split);
  246. split = NULL;
  247. }
  248. write_unlock(&em_tree->lock);
  249. /* once for us */
  250. free_extent_map(em);
  251. /* once for the tree*/
  252. free_extent_map(em);
  253. }
  254. if (split)
  255. free_extent_map(split);
  256. if (split2)
  257. free_extent_map(split2);
  258. return 0;
  259. }
  260. /*
  261. * this is very complex, but the basic idea is to drop all extents
  262. * in the range start - end. hint_block is filled in with a block number
  263. * that would be a good hint to the block allocator for this file.
  264. *
  265. * If an extent intersects the range but is not entirely inside the range
  266. * it is either truncated or split. Anything entirely inside the range
  267. * is deleted from the tree.
  268. */
  269. int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
  270. u64 start, u64 end, u64 *hint_byte, int drop_cache)
  271. {
  272. struct btrfs_root *root = BTRFS_I(inode)->root;
  273. struct extent_buffer *leaf;
  274. struct btrfs_file_extent_item *fi;
  275. struct btrfs_path *path;
  276. struct btrfs_key key;
  277. struct btrfs_key new_key;
  278. u64 search_start = start;
  279. u64 disk_bytenr = 0;
  280. u64 num_bytes = 0;
  281. u64 extent_offset = 0;
  282. u64 extent_end = 0;
  283. int del_nr = 0;
  284. int del_slot = 0;
  285. int extent_type;
  286. int recow;
  287. int ret;
  288. if (drop_cache)
  289. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  290. path = btrfs_alloc_path();
  291. if (!path)
  292. return -ENOMEM;
  293. while (1) {
  294. recow = 0;
  295. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  296. search_start, -1);
  297. if (ret < 0)
  298. break;
  299. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  300. leaf = path->nodes[0];
  301. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  302. if (key.objectid == inode->i_ino &&
  303. key.type == BTRFS_EXTENT_DATA_KEY)
  304. path->slots[0]--;
  305. }
  306. ret = 0;
  307. next_slot:
  308. leaf = path->nodes[0];
  309. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  310. BUG_ON(del_nr > 0);
  311. ret = btrfs_next_leaf(root, path);
  312. if (ret < 0)
  313. break;
  314. if (ret > 0) {
  315. ret = 0;
  316. break;
  317. }
  318. leaf = path->nodes[0];
  319. recow = 1;
  320. }
  321. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  322. if (key.objectid > inode->i_ino ||
  323. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  324. break;
  325. fi = btrfs_item_ptr(leaf, path->slots[0],
  326. struct btrfs_file_extent_item);
  327. extent_type = btrfs_file_extent_type(leaf, fi);
  328. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  329. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  330. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  331. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  332. extent_offset = btrfs_file_extent_offset(leaf, fi);
  333. extent_end = key.offset +
  334. btrfs_file_extent_num_bytes(leaf, fi);
  335. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  336. extent_end = key.offset +
  337. btrfs_file_extent_inline_len(leaf, fi);
  338. } else {
  339. WARN_ON(1);
  340. extent_end = search_start;
  341. }
  342. if (extent_end <= search_start) {
  343. path->slots[0]++;
  344. goto next_slot;
  345. }
  346. search_start = max(key.offset, start);
  347. if (recow) {
  348. btrfs_release_path(root, path);
  349. continue;
  350. }
  351. /*
  352. * | - range to drop - |
  353. * | -------- extent -------- |
  354. */
  355. if (start > key.offset && end < extent_end) {
  356. BUG_ON(del_nr > 0);
  357. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  358. memcpy(&new_key, &key, sizeof(new_key));
  359. new_key.offset = start;
  360. ret = btrfs_duplicate_item(trans, root, path,
  361. &new_key);
  362. if (ret == -EAGAIN) {
  363. btrfs_release_path(root, path);
  364. continue;
  365. }
  366. if (ret < 0)
  367. break;
  368. leaf = path->nodes[0];
  369. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  370. struct btrfs_file_extent_item);
  371. btrfs_set_file_extent_num_bytes(leaf, fi,
  372. start - key.offset);
  373. fi = btrfs_item_ptr(leaf, path->slots[0],
  374. struct btrfs_file_extent_item);
  375. extent_offset += start - key.offset;
  376. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  377. btrfs_set_file_extent_num_bytes(leaf, fi,
  378. extent_end - start);
  379. btrfs_mark_buffer_dirty(leaf);
  380. if (disk_bytenr > 0) {
  381. ret = btrfs_inc_extent_ref(trans, root,
  382. disk_bytenr, num_bytes, 0,
  383. root->root_key.objectid,
  384. new_key.objectid,
  385. start - extent_offset);
  386. BUG_ON(ret);
  387. *hint_byte = disk_bytenr;
  388. }
  389. key.offset = start;
  390. }
  391. /*
  392. * | ---- range to drop ----- |
  393. * | -------- extent -------- |
  394. */
  395. if (start <= key.offset && end < extent_end) {
  396. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  397. memcpy(&new_key, &key, sizeof(new_key));
  398. new_key.offset = end;
  399. btrfs_set_item_key_safe(trans, root, path, &new_key);
  400. extent_offset += end - key.offset;
  401. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  402. btrfs_set_file_extent_num_bytes(leaf, fi,
  403. extent_end - end);
  404. btrfs_mark_buffer_dirty(leaf);
  405. if (disk_bytenr > 0) {
  406. inode_sub_bytes(inode, end - key.offset);
  407. *hint_byte = disk_bytenr;
  408. }
  409. break;
  410. }
  411. search_start = extent_end;
  412. /*
  413. * | ---- range to drop ----- |
  414. * | -------- extent -------- |
  415. */
  416. if (start > key.offset && end >= extent_end) {
  417. BUG_ON(del_nr > 0);
  418. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  419. btrfs_set_file_extent_num_bytes(leaf, fi,
  420. start - key.offset);
  421. btrfs_mark_buffer_dirty(leaf);
  422. if (disk_bytenr > 0) {
  423. inode_sub_bytes(inode, extent_end - start);
  424. *hint_byte = disk_bytenr;
  425. }
  426. if (end == extent_end)
  427. break;
  428. path->slots[0]++;
  429. goto next_slot;
  430. }
  431. /*
  432. * | ---- range to drop ----- |
  433. * | ------ extent ------ |
  434. */
  435. if (start <= key.offset && end >= extent_end) {
  436. if (del_nr == 0) {
  437. del_slot = path->slots[0];
  438. del_nr = 1;
  439. } else {
  440. BUG_ON(del_slot + del_nr != path->slots[0]);
  441. del_nr++;
  442. }
  443. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  444. inode_sub_bytes(inode,
  445. extent_end - key.offset);
  446. extent_end = ALIGN(extent_end,
  447. root->sectorsize);
  448. } else if (disk_bytenr > 0) {
  449. ret = btrfs_free_extent(trans, root,
  450. disk_bytenr, num_bytes, 0,
  451. root->root_key.objectid,
  452. key.objectid, key.offset -
  453. extent_offset);
  454. BUG_ON(ret);
  455. inode_sub_bytes(inode,
  456. extent_end - key.offset);
  457. *hint_byte = disk_bytenr;
  458. }
  459. if (end == extent_end)
  460. break;
  461. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  462. path->slots[0]++;
  463. goto next_slot;
  464. }
  465. ret = btrfs_del_items(trans, root, path, del_slot,
  466. del_nr);
  467. BUG_ON(ret);
  468. del_nr = 0;
  469. del_slot = 0;
  470. btrfs_release_path(root, path);
  471. continue;
  472. }
  473. BUG_ON(1);
  474. }
  475. if (del_nr > 0) {
  476. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  477. BUG_ON(ret);
  478. }
  479. btrfs_free_path(path);
  480. return ret;
  481. }
  482. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  483. u64 objectid, u64 bytenr, u64 orig_offset,
  484. u64 *start, u64 *end)
  485. {
  486. struct btrfs_file_extent_item *fi;
  487. struct btrfs_key key;
  488. u64 extent_end;
  489. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  490. return 0;
  491. btrfs_item_key_to_cpu(leaf, &key, slot);
  492. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  493. return 0;
  494. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  495. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  496. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  497. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  498. btrfs_file_extent_compression(leaf, fi) ||
  499. btrfs_file_extent_encryption(leaf, fi) ||
  500. btrfs_file_extent_other_encoding(leaf, fi))
  501. return 0;
  502. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  503. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  504. return 0;
  505. *start = key.offset;
  506. *end = extent_end;
  507. return 1;
  508. }
  509. /*
  510. * Mark extent in the range start - end as written.
  511. *
  512. * This changes extent type from 'pre-allocated' to 'regular'. If only
  513. * part of extent is marked as written, the extent will be split into
  514. * two or three.
  515. */
  516. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  517. struct inode *inode, u64 start, u64 end)
  518. {
  519. struct btrfs_root *root = BTRFS_I(inode)->root;
  520. struct extent_buffer *leaf;
  521. struct btrfs_path *path;
  522. struct btrfs_file_extent_item *fi;
  523. struct btrfs_key key;
  524. struct btrfs_key new_key;
  525. u64 bytenr;
  526. u64 num_bytes;
  527. u64 extent_end;
  528. u64 orig_offset;
  529. u64 other_start;
  530. u64 other_end;
  531. u64 split;
  532. int del_nr = 0;
  533. int del_slot = 0;
  534. int recow;
  535. int ret;
  536. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  537. path = btrfs_alloc_path();
  538. BUG_ON(!path);
  539. again:
  540. recow = 0;
  541. split = start;
  542. key.objectid = inode->i_ino;
  543. key.type = BTRFS_EXTENT_DATA_KEY;
  544. key.offset = split;
  545. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  546. if (ret < 0)
  547. goto out;
  548. if (ret > 0 && path->slots[0] > 0)
  549. path->slots[0]--;
  550. leaf = path->nodes[0];
  551. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  552. BUG_ON(key.objectid != inode->i_ino ||
  553. key.type != BTRFS_EXTENT_DATA_KEY);
  554. fi = btrfs_item_ptr(leaf, path->slots[0],
  555. struct btrfs_file_extent_item);
  556. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  557. BTRFS_FILE_EXTENT_PREALLOC);
  558. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  559. BUG_ON(key.offset > start || extent_end < end);
  560. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  561. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  562. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  563. memcpy(&new_key, &key, sizeof(new_key));
  564. if (start == key.offset && end < extent_end) {
  565. other_start = 0;
  566. other_end = start;
  567. if (extent_mergeable(leaf, path->slots[0] - 1,
  568. inode->i_ino, bytenr, orig_offset,
  569. &other_start, &other_end)) {
  570. new_key.offset = end;
  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. extent_end - end);
  576. btrfs_set_file_extent_offset(leaf, fi,
  577. end - orig_offset);
  578. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  579. struct btrfs_file_extent_item);
  580. btrfs_set_file_extent_num_bytes(leaf, fi,
  581. end - other_start);
  582. btrfs_mark_buffer_dirty(leaf);
  583. goto out;
  584. }
  585. }
  586. if (start > key.offset && end == extent_end) {
  587. other_start = end;
  588. other_end = 0;
  589. if (extent_mergeable(leaf, path->slots[0] + 1,
  590. inode->i_ino, bytenr, orig_offset,
  591. &other_start, &other_end)) {
  592. fi = btrfs_item_ptr(leaf, path->slots[0],
  593. struct btrfs_file_extent_item);
  594. btrfs_set_file_extent_num_bytes(leaf, fi,
  595. start - key.offset);
  596. path->slots[0]++;
  597. new_key.offset = start;
  598. btrfs_set_item_key_safe(trans, root, path, &new_key);
  599. fi = btrfs_item_ptr(leaf, path->slots[0],
  600. struct btrfs_file_extent_item);
  601. btrfs_set_file_extent_num_bytes(leaf, fi,
  602. other_end - start);
  603. btrfs_set_file_extent_offset(leaf, fi,
  604. start - orig_offset);
  605. btrfs_mark_buffer_dirty(leaf);
  606. goto out;
  607. }
  608. }
  609. while (start > key.offset || end < extent_end) {
  610. if (key.offset == start)
  611. split = end;
  612. new_key.offset = split;
  613. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  614. if (ret == -EAGAIN) {
  615. btrfs_release_path(root, path);
  616. goto again;
  617. }
  618. BUG_ON(ret < 0);
  619. leaf = path->nodes[0];
  620. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  621. struct btrfs_file_extent_item);
  622. btrfs_set_file_extent_num_bytes(leaf, fi,
  623. split - key.offset);
  624. fi = btrfs_item_ptr(leaf, path->slots[0],
  625. struct btrfs_file_extent_item);
  626. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  627. btrfs_set_file_extent_num_bytes(leaf, fi,
  628. extent_end - split);
  629. btrfs_mark_buffer_dirty(leaf);
  630. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  631. root->root_key.objectid,
  632. inode->i_ino, orig_offset);
  633. BUG_ON(ret);
  634. if (split == start) {
  635. key.offset = start;
  636. } else {
  637. BUG_ON(start != key.offset);
  638. path->slots[0]--;
  639. extent_end = end;
  640. }
  641. recow = 1;
  642. }
  643. other_start = end;
  644. other_end = 0;
  645. if (extent_mergeable(leaf, path->slots[0] + 1,
  646. inode->i_ino, bytenr, orig_offset,
  647. &other_start, &other_end)) {
  648. if (recow) {
  649. btrfs_release_path(root, path);
  650. goto again;
  651. }
  652. extent_end = other_end;
  653. del_slot = path->slots[0] + 1;
  654. del_nr++;
  655. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  656. 0, root->root_key.objectid,
  657. inode->i_ino, orig_offset);
  658. BUG_ON(ret);
  659. }
  660. other_start = 0;
  661. other_end = start;
  662. if (extent_mergeable(leaf, path->slots[0] - 1,
  663. inode->i_ino, bytenr, orig_offset,
  664. &other_start, &other_end)) {
  665. if (recow) {
  666. btrfs_release_path(root, path);
  667. goto again;
  668. }
  669. key.offset = other_start;
  670. del_slot = path->slots[0];
  671. del_nr++;
  672. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  673. 0, root->root_key.objectid,
  674. inode->i_ino, orig_offset);
  675. BUG_ON(ret);
  676. }
  677. if (del_nr == 0) {
  678. fi = btrfs_item_ptr(leaf, path->slots[0],
  679. struct btrfs_file_extent_item);
  680. btrfs_set_file_extent_type(leaf, fi,
  681. BTRFS_FILE_EXTENT_REG);
  682. btrfs_mark_buffer_dirty(leaf);
  683. } else {
  684. fi = btrfs_item_ptr(leaf, del_slot - 1,
  685. struct btrfs_file_extent_item);
  686. btrfs_set_file_extent_type(leaf, fi,
  687. BTRFS_FILE_EXTENT_REG);
  688. btrfs_set_file_extent_num_bytes(leaf, fi,
  689. extent_end - key.offset);
  690. btrfs_mark_buffer_dirty(leaf);
  691. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  692. BUG_ON(ret);
  693. }
  694. out:
  695. btrfs_free_path(path);
  696. return 0;
  697. }
  698. /*
  699. * on error we return an unlocked page and the error value
  700. * on success we return a locked page and 0
  701. */
  702. static int prepare_uptodate_page(struct page *page, u64 pos)
  703. {
  704. int ret = 0;
  705. if ((pos & (PAGE_CACHE_SIZE - 1)) && !PageUptodate(page)) {
  706. ret = btrfs_readpage(NULL, page);
  707. if (ret)
  708. return ret;
  709. lock_page(page);
  710. if (!PageUptodate(page)) {
  711. unlock_page(page);
  712. return -EIO;
  713. }
  714. }
  715. return 0;
  716. }
  717. /*
  718. * this gets pages into the page cache and locks them down, it also properly
  719. * waits for data=ordered extents to finish before allowing the pages to be
  720. * modified.
  721. */
  722. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  723. struct page **pages, size_t num_pages,
  724. loff_t pos, unsigned long first_index,
  725. unsigned long last_index, size_t write_bytes)
  726. {
  727. struct extent_state *cached_state = NULL;
  728. int i;
  729. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  730. struct inode *inode = fdentry(file)->d_inode;
  731. int err = 0;
  732. int faili = 0;
  733. u64 start_pos;
  734. u64 last_pos;
  735. start_pos = pos & ~((u64)root->sectorsize - 1);
  736. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  737. if (start_pos > inode->i_size) {
  738. err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
  739. if (err)
  740. return err;
  741. }
  742. again:
  743. for (i = 0; i < num_pages; i++) {
  744. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  745. if (!pages[i]) {
  746. faili = i - 1;
  747. err = -ENOMEM;
  748. goto fail;
  749. }
  750. if (i == 0)
  751. err = prepare_uptodate_page(pages[i], pos);
  752. if (i == num_pages - 1)
  753. err = prepare_uptodate_page(pages[i],
  754. pos + write_bytes);
  755. if (err) {
  756. page_cache_release(pages[i]);
  757. faili = i - 1;
  758. goto fail;
  759. }
  760. wait_on_page_writeback(pages[i]);
  761. }
  762. err = 0;
  763. if (start_pos < inode->i_size) {
  764. struct btrfs_ordered_extent *ordered;
  765. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  766. start_pos, last_pos - 1, 0, &cached_state,
  767. GFP_NOFS);
  768. ordered = btrfs_lookup_first_ordered_extent(inode,
  769. last_pos - 1);
  770. if (ordered &&
  771. ordered->file_offset + ordered->len > start_pos &&
  772. ordered->file_offset < last_pos) {
  773. btrfs_put_ordered_extent(ordered);
  774. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  775. start_pos, last_pos - 1,
  776. &cached_state, GFP_NOFS);
  777. for (i = 0; i < num_pages; i++) {
  778. unlock_page(pages[i]);
  779. page_cache_release(pages[i]);
  780. }
  781. btrfs_wait_ordered_range(inode, start_pos,
  782. last_pos - start_pos);
  783. goto again;
  784. }
  785. if (ordered)
  786. btrfs_put_ordered_extent(ordered);
  787. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  788. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  789. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  790. GFP_NOFS);
  791. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  792. start_pos, last_pos - 1, &cached_state,
  793. GFP_NOFS);
  794. }
  795. for (i = 0; i < num_pages; i++) {
  796. clear_page_dirty_for_io(pages[i]);
  797. set_page_extent_mapped(pages[i]);
  798. WARN_ON(!PageLocked(pages[i]));
  799. }
  800. return 0;
  801. fail:
  802. while (faili >= 0) {
  803. unlock_page(pages[faili]);
  804. page_cache_release(pages[faili]);
  805. faili--;
  806. }
  807. return err;
  808. }
  809. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  810. struct iov_iter *i,
  811. loff_t pos)
  812. {
  813. struct inode *inode = fdentry(file)->d_inode;
  814. struct btrfs_root *root = BTRFS_I(inode)->root;
  815. struct page **pages = NULL;
  816. unsigned long first_index;
  817. unsigned long last_index;
  818. size_t num_written = 0;
  819. int nrptrs;
  820. int ret;
  821. nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
  822. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  823. (sizeof(struct page *)));
  824. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  825. if (!pages)
  826. return -ENOMEM;
  827. first_index = pos >> PAGE_CACHE_SHIFT;
  828. last_index = (pos + iov_iter_count(i)) >> PAGE_CACHE_SHIFT;
  829. while (iov_iter_count(i) > 0) {
  830. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  831. size_t write_bytes = min(iov_iter_count(i),
  832. nrptrs * (size_t)PAGE_CACHE_SIZE -
  833. offset);
  834. size_t num_pages = (write_bytes + offset +
  835. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  836. size_t dirty_pages;
  837. size_t copied;
  838. WARN_ON(num_pages > nrptrs);
  839. /*
  840. * Fault pages before locking them in prepare_pages
  841. * to avoid recursive lock
  842. */
  843. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  844. ret = -EFAULT;
  845. break;
  846. }
  847. ret = btrfs_delalloc_reserve_space(inode,
  848. num_pages << PAGE_CACHE_SHIFT);
  849. if (ret)
  850. break;
  851. /*
  852. * This is going to setup the pages array with the number of
  853. * pages we want, so we don't really need to worry about the
  854. * contents of pages from loop to loop
  855. */
  856. ret = prepare_pages(root, file, pages, num_pages,
  857. pos, first_index, last_index,
  858. write_bytes);
  859. if (ret) {
  860. btrfs_delalloc_release_space(inode,
  861. num_pages << PAGE_CACHE_SHIFT);
  862. break;
  863. }
  864. copied = btrfs_copy_from_user(pos, num_pages,
  865. write_bytes, pages, i);
  866. /*
  867. * if we have trouble faulting in the pages, fall
  868. * back to one page at a time
  869. */
  870. if (copied < write_bytes)
  871. nrptrs = 1;
  872. if (copied == 0)
  873. dirty_pages = 0;
  874. else
  875. dirty_pages = (copied + offset +
  876. PAGE_CACHE_SIZE - 1) >>
  877. PAGE_CACHE_SHIFT;
  878. /*
  879. * If we had a short copy we need to release the excess delaloc
  880. * bytes we reserved. We need to increment outstanding_extents
  881. * because btrfs_delalloc_release_space will decrement it, but
  882. * we still have an outstanding extent for the chunk we actually
  883. * managed to copy.
  884. */
  885. if (num_pages > dirty_pages) {
  886. if (copied > 0)
  887. atomic_inc(
  888. &BTRFS_I(inode)->outstanding_extents);
  889. btrfs_delalloc_release_space(inode,
  890. (num_pages - dirty_pages) <<
  891. PAGE_CACHE_SHIFT);
  892. }
  893. if (copied > 0) {
  894. ret = dirty_and_release_pages(root, file, pages,
  895. dirty_pages, pos,
  896. copied);
  897. if (ret) {
  898. btrfs_delalloc_release_space(inode,
  899. dirty_pages << PAGE_CACHE_SHIFT);
  900. btrfs_drop_pages(pages, num_pages);
  901. break;
  902. }
  903. }
  904. btrfs_drop_pages(pages, num_pages);
  905. cond_resched();
  906. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  907. dirty_pages);
  908. if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  909. btrfs_btree_balance_dirty(root, 1);
  910. btrfs_throttle(root);
  911. pos += copied;
  912. num_written += copied;
  913. }
  914. kfree(pages);
  915. return num_written ? num_written : ret;
  916. }
  917. static ssize_t __btrfs_direct_write(struct kiocb *iocb,
  918. const struct iovec *iov,
  919. unsigned long nr_segs, loff_t pos,
  920. loff_t *ppos, size_t count, size_t ocount)
  921. {
  922. struct file *file = iocb->ki_filp;
  923. struct inode *inode = fdentry(file)->d_inode;
  924. struct iov_iter i;
  925. ssize_t written;
  926. ssize_t written_buffered;
  927. loff_t endbyte;
  928. int err;
  929. written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
  930. count, ocount);
  931. /*
  932. * the generic O_DIRECT will update in-memory i_size after the
  933. * DIOs are done. But our endio handlers that update the on
  934. * disk i_size never update past the in memory i_size. So we
  935. * need one more update here to catch any additions to the
  936. * file
  937. */
  938. if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
  939. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  940. mark_inode_dirty(inode);
  941. }
  942. if (written < 0 || written == count)
  943. return written;
  944. pos += written;
  945. count -= written;
  946. iov_iter_init(&i, iov, nr_segs, count, written);
  947. written_buffered = __btrfs_buffered_write(file, &i, pos);
  948. if (written_buffered < 0) {
  949. err = written_buffered;
  950. goto out;
  951. }
  952. endbyte = pos + written_buffered - 1;
  953. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  954. if (err)
  955. goto out;
  956. written += written_buffered;
  957. *ppos = pos + written_buffered;
  958. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
  959. endbyte >> PAGE_CACHE_SHIFT);
  960. out:
  961. return written ? written : err;
  962. }
  963. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  964. const struct iovec *iov,
  965. unsigned long nr_segs, loff_t pos)
  966. {
  967. struct file *file = iocb->ki_filp;
  968. struct inode *inode = fdentry(file)->d_inode;
  969. struct btrfs_root *root = BTRFS_I(inode)->root;
  970. loff_t *ppos = &iocb->ki_pos;
  971. ssize_t num_written = 0;
  972. ssize_t err = 0;
  973. size_t count, ocount;
  974. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  975. mutex_lock(&inode->i_mutex);
  976. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  977. if (err) {
  978. mutex_unlock(&inode->i_mutex);
  979. goto out;
  980. }
  981. count = ocount;
  982. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  983. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  984. if (err) {
  985. mutex_unlock(&inode->i_mutex);
  986. goto out;
  987. }
  988. if (count == 0) {
  989. mutex_unlock(&inode->i_mutex);
  990. goto out;
  991. }
  992. err = file_remove_suid(file);
  993. if (err) {
  994. mutex_unlock(&inode->i_mutex);
  995. goto out;
  996. }
  997. /*
  998. * If BTRFS flips readonly due to some impossible error
  999. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1000. * although we have opened a file as writable, we have
  1001. * to stop this write operation to ensure FS consistency.
  1002. */
  1003. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  1004. mutex_unlock(&inode->i_mutex);
  1005. err = -EROFS;
  1006. goto out;
  1007. }
  1008. file_update_time(file);
  1009. BTRFS_I(inode)->sequence++;
  1010. if (unlikely(file->f_flags & O_DIRECT)) {
  1011. num_written = __btrfs_direct_write(iocb, iov, nr_segs,
  1012. pos, ppos, count, ocount);
  1013. } else {
  1014. struct iov_iter i;
  1015. iov_iter_init(&i, iov, nr_segs, count, num_written);
  1016. num_written = __btrfs_buffered_write(file, &i, pos);
  1017. if (num_written > 0)
  1018. *ppos = pos + num_written;
  1019. }
  1020. mutex_unlock(&inode->i_mutex);
  1021. /*
  1022. * we want to make sure fsync finds this change
  1023. * but we haven't joined a transaction running right now.
  1024. *
  1025. * Later on, someone is sure to update the inode and get the
  1026. * real transid recorded.
  1027. *
  1028. * We set last_trans now to the fs_info generation + 1,
  1029. * this will either be one more than the running transaction
  1030. * or the generation used for the next transaction if there isn't
  1031. * one running right now.
  1032. */
  1033. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  1034. if (num_written > 0 || num_written == -EIOCBQUEUED) {
  1035. err = generic_write_sync(file, pos, num_written);
  1036. if (err < 0 && num_written > 0)
  1037. num_written = err;
  1038. }
  1039. out:
  1040. current->backing_dev_info = NULL;
  1041. return num_written ? num_written : err;
  1042. }
  1043. int btrfs_release_file(struct inode *inode, struct file *filp)
  1044. {
  1045. /*
  1046. * ordered_data_close is set by settattr when we are about to truncate
  1047. * a file from a non-zero size to a zero size. This tries to
  1048. * flush down new bytes that may have been written if the
  1049. * application were using truncate to replace a file in place.
  1050. */
  1051. if (BTRFS_I(inode)->ordered_data_close) {
  1052. BTRFS_I(inode)->ordered_data_close = 0;
  1053. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1054. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1055. filemap_flush(inode->i_mapping);
  1056. }
  1057. if (filp->private_data)
  1058. btrfs_ioctl_trans_end(filp);
  1059. return 0;
  1060. }
  1061. /*
  1062. * fsync call for both files and directories. This logs the inode into
  1063. * the tree log instead of forcing full commits whenever possible.
  1064. *
  1065. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1066. * in the metadata btree are up to date for copying to the log.
  1067. *
  1068. * It drops the inode mutex before doing the tree log commit. This is an
  1069. * important optimization for directories because holding the mutex prevents
  1070. * new operations on the dir while we write to disk.
  1071. */
  1072. int btrfs_sync_file(struct file *file, int datasync)
  1073. {
  1074. struct dentry *dentry = file->f_path.dentry;
  1075. struct inode *inode = dentry->d_inode;
  1076. struct btrfs_root *root = BTRFS_I(inode)->root;
  1077. int ret = 0;
  1078. struct btrfs_trans_handle *trans;
  1079. trace_btrfs_sync_file(file, datasync);
  1080. /* we wait first, since the writeback may change the inode */
  1081. root->log_batch++;
  1082. /* the VFS called filemap_fdatawrite for us */
  1083. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  1084. root->log_batch++;
  1085. /*
  1086. * check the transaction that last modified this inode
  1087. * and see if its already been committed
  1088. */
  1089. if (!BTRFS_I(inode)->last_trans)
  1090. goto out;
  1091. /*
  1092. * if the last transaction that changed this file was before
  1093. * the current transaction, we can bail out now without any
  1094. * syncing
  1095. */
  1096. mutex_lock(&root->fs_info->trans_mutex);
  1097. if (BTRFS_I(inode)->last_trans <=
  1098. root->fs_info->last_trans_committed) {
  1099. BTRFS_I(inode)->last_trans = 0;
  1100. mutex_unlock(&root->fs_info->trans_mutex);
  1101. goto out;
  1102. }
  1103. mutex_unlock(&root->fs_info->trans_mutex);
  1104. /*
  1105. * ok we haven't committed the transaction yet, lets do a commit
  1106. */
  1107. if (file->private_data)
  1108. btrfs_ioctl_trans_end(file);
  1109. trans = btrfs_start_transaction(root, 0);
  1110. if (IS_ERR(trans)) {
  1111. ret = PTR_ERR(trans);
  1112. goto out;
  1113. }
  1114. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1115. if (ret < 0)
  1116. goto out;
  1117. /* we've logged all the items and now have a consistent
  1118. * version of the file in the log. It is possible that
  1119. * someone will come in and modify the file, but that's
  1120. * fine because the log is consistent on disk, and we
  1121. * have references to all of the file's extents
  1122. *
  1123. * It is possible that someone will come in and log the
  1124. * file again, but that will end up using the synchronization
  1125. * inside btrfs_sync_log to keep things safe.
  1126. */
  1127. mutex_unlock(&dentry->d_inode->i_mutex);
  1128. if (ret != BTRFS_NO_LOG_SYNC) {
  1129. if (ret > 0) {
  1130. ret = btrfs_commit_transaction(trans, root);
  1131. } else {
  1132. ret = btrfs_sync_log(trans, root);
  1133. if (ret == 0)
  1134. ret = btrfs_end_transaction(trans, root);
  1135. else
  1136. ret = btrfs_commit_transaction(trans, root);
  1137. }
  1138. } else {
  1139. ret = btrfs_end_transaction(trans, root);
  1140. }
  1141. mutex_lock(&dentry->d_inode->i_mutex);
  1142. out:
  1143. return ret > 0 ? -EIO : ret;
  1144. }
  1145. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1146. .fault = filemap_fault,
  1147. .page_mkwrite = btrfs_page_mkwrite,
  1148. };
  1149. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1150. {
  1151. struct address_space *mapping = filp->f_mapping;
  1152. if (!mapping->a_ops->readpage)
  1153. return -ENOEXEC;
  1154. file_accessed(filp);
  1155. vma->vm_ops = &btrfs_file_vm_ops;
  1156. vma->vm_flags |= VM_CAN_NONLINEAR;
  1157. return 0;
  1158. }
  1159. static long btrfs_fallocate(struct file *file, int mode,
  1160. loff_t offset, loff_t len)
  1161. {
  1162. struct inode *inode = file->f_path.dentry->d_inode;
  1163. struct extent_state *cached_state = NULL;
  1164. u64 cur_offset;
  1165. u64 last_byte;
  1166. u64 alloc_start;
  1167. u64 alloc_end;
  1168. u64 alloc_hint = 0;
  1169. u64 locked_end;
  1170. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1171. struct extent_map *em;
  1172. int ret;
  1173. alloc_start = offset & ~mask;
  1174. alloc_end = (offset + len + mask) & ~mask;
  1175. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  1176. if (mode & ~FALLOC_FL_KEEP_SIZE)
  1177. return -EOPNOTSUPP;
  1178. /*
  1179. * wait for ordered IO before we have any locks. We'll loop again
  1180. * below with the locks held.
  1181. */
  1182. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1183. mutex_lock(&inode->i_mutex);
  1184. ret = inode_newsize_ok(inode, alloc_end);
  1185. if (ret)
  1186. goto out;
  1187. if (alloc_start > inode->i_size) {
  1188. ret = btrfs_cont_expand(inode, i_size_read(inode),
  1189. alloc_start);
  1190. if (ret)
  1191. goto out;
  1192. }
  1193. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
  1194. if (ret)
  1195. goto out;
  1196. locked_end = alloc_end - 1;
  1197. while (1) {
  1198. struct btrfs_ordered_extent *ordered;
  1199. /* the extent lock is ordered inside the running
  1200. * transaction
  1201. */
  1202. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1203. locked_end, 0, &cached_state, GFP_NOFS);
  1204. ordered = btrfs_lookup_first_ordered_extent(inode,
  1205. alloc_end - 1);
  1206. if (ordered &&
  1207. ordered->file_offset + ordered->len > alloc_start &&
  1208. ordered->file_offset < alloc_end) {
  1209. btrfs_put_ordered_extent(ordered);
  1210. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1211. alloc_start, locked_end,
  1212. &cached_state, GFP_NOFS);
  1213. /*
  1214. * we can't wait on the range with the transaction
  1215. * running or with the extent lock held
  1216. */
  1217. btrfs_wait_ordered_range(inode, alloc_start,
  1218. alloc_end - alloc_start);
  1219. } else {
  1220. if (ordered)
  1221. btrfs_put_ordered_extent(ordered);
  1222. break;
  1223. }
  1224. }
  1225. cur_offset = alloc_start;
  1226. while (1) {
  1227. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1228. alloc_end - cur_offset, 0);
  1229. BUG_ON(IS_ERR(em) || !em);
  1230. last_byte = min(extent_map_end(em), alloc_end);
  1231. last_byte = (last_byte + mask) & ~mask;
  1232. if (em->block_start == EXTENT_MAP_HOLE ||
  1233. (cur_offset >= inode->i_size &&
  1234. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1235. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1236. last_byte - cur_offset,
  1237. 1 << inode->i_blkbits,
  1238. offset + len,
  1239. &alloc_hint);
  1240. if (ret < 0) {
  1241. free_extent_map(em);
  1242. break;
  1243. }
  1244. }
  1245. free_extent_map(em);
  1246. cur_offset = last_byte;
  1247. if (cur_offset >= alloc_end) {
  1248. ret = 0;
  1249. break;
  1250. }
  1251. }
  1252. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  1253. &cached_state, GFP_NOFS);
  1254. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
  1255. out:
  1256. mutex_unlock(&inode->i_mutex);
  1257. return ret;
  1258. }
  1259. const struct file_operations btrfs_file_operations = {
  1260. .llseek = generic_file_llseek,
  1261. .read = do_sync_read,
  1262. .write = do_sync_write,
  1263. .aio_read = generic_file_aio_read,
  1264. .splice_read = generic_file_splice_read,
  1265. .aio_write = btrfs_file_aio_write,
  1266. .mmap = btrfs_file_mmap,
  1267. .open = generic_file_open,
  1268. .release = btrfs_release_file,
  1269. .fsync = btrfs_sync_file,
  1270. .fallocate = btrfs_fallocate,
  1271. .unlocked_ioctl = btrfs_ioctl,
  1272. #ifdef CONFIG_COMPAT
  1273. .compat_ioctl = btrfs_ioctl,
  1274. #endif
  1275. };