file.c 37 KB

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