file.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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. /*
  42. * when auto defrag is enabled we
  43. * queue up these defrag structs to remember which
  44. * inodes need defragging passes
  45. */
  46. struct inode_defrag {
  47. struct rb_node rb_node;
  48. /* objectid */
  49. u64 ino;
  50. /*
  51. * transid where the defrag was added, we search for
  52. * extents newer than this
  53. */
  54. u64 transid;
  55. /* root objectid */
  56. u64 root;
  57. /* last offset we were able to defrag */
  58. u64 last_offset;
  59. /* if we've wrapped around back to zero once already */
  60. int cycled;
  61. };
  62. static int __compare_inode_defrag(struct inode_defrag *defrag1,
  63. struct inode_defrag *defrag2)
  64. {
  65. if (defrag1->root > defrag2->root)
  66. return 1;
  67. else if (defrag1->root < defrag2->root)
  68. return -1;
  69. else if (defrag1->ino > defrag2->ino)
  70. return 1;
  71. else if (defrag1->ino < defrag2->ino)
  72. return -1;
  73. else
  74. return 0;
  75. }
  76. /* pop a record for an inode into the defrag tree. The lock
  77. * must be held already
  78. *
  79. * If you're inserting a record for an older transid than an
  80. * existing record, the transid already in the tree is lowered
  81. *
  82. * If an existing record is found the defrag item you
  83. * pass in is freed
  84. */
  85. static void __btrfs_add_inode_defrag(struct inode *inode,
  86. struct inode_defrag *defrag)
  87. {
  88. struct btrfs_root *root = BTRFS_I(inode)->root;
  89. struct inode_defrag *entry;
  90. struct rb_node **p;
  91. struct rb_node *parent = NULL;
  92. int ret;
  93. p = &root->fs_info->defrag_inodes.rb_node;
  94. while (*p) {
  95. parent = *p;
  96. entry = rb_entry(parent, struct inode_defrag, rb_node);
  97. ret = __compare_inode_defrag(defrag, entry);
  98. if (ret < 0)
  99. p = &parent->rb_left;
  100. else if (ret > 0)
  101. p = &parent->rb_right;
  102. else {
  103. /* if we're reinserting an entry for
  104. * an old defrag run, make sure to
  105. * lower the transid of our existing record
  106. */
  107. if (defrag->transid < entry->transid)
  108. entry->transid = defrag->transid;
  109. if (defrag->last_offset > entry->last_offset)
  110. entry->last_offset = defrag->last_offset;
  111. goto exists;
  112. }
  113. }
  114. set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
  115. rb_link_node(&defrag->rb_node, parent, p);
  116. rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
  117. return;
  118. exists:
  119. kfree(defrag);
  120. return;
  121. }
  122. /*
  123. * insert a defrag record for this inode if auto defrag is
  124. * enabled
  125. */
  126. int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
  127. struct inode *inode)
  128. {
  129. struct btrfs_root *root = BTRFS_I(inode)->root;
  130. struct inode_defrag *defrag;
  131. u64 transid;
  132. if (!btrfs_test_opt(root, AUTO_DEFRAG))
  133. return 0;
  134. if (btrfs_fs_closing(root->fs_info))
  135. return 0;
  136. if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
  137. return 0;
  138. if (trans)
  139. transid = trans->transid;
  140. else
  141. transid = BTRFS_I(inode)->root->last_trans;
  142. defrag = kzalloc(sizeof(*defrag), GFP_NOFS);
  143. if (!defrag)
  144. return -ENOMEM;
  145. defrag->ino = btrfs_ino(inode);
  146. defrag->transid = transid;
  147. defrag->root = root->root_key.objectid;
  148. spin_lock(&root->fs_info->defrag_inodes_lock);
  149. if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
  150. __btrfs_add_inode_defrag(inode, defrag);
  151. else
  152. kfree(defrag);
  153. spin_unlock(&root->fs_info->defrag_inodes_lock);
  154. return 0;
  155. }
  156. /*
  157. * must be called with the defrag_inodes lock held
  158. */
  159. struct inode_defrag *btrfs_find_defrag_inode(struct btrfs_fs_info *info,
  160. u64 root, u64 ino,
  161. struct rb_node **next)
  162. {
  163. struct inode_defrag *entry = NULL;
  164. struct inode_defrag tmp;
  165. struct rb_node *p;
  166. struct rb_node *parent = NULL;
  167. int ret;
  168. tmp.ino = ino;
  169. tmp.root = root;
  170. p = info->defrag_inodes.rb_node;
  171. while (p) {
  172. parent = p;
  173. entry = rb_entry(parent, struct inode_defrag, rb_node);
  174. ret = __compare_inode_defrag(&tmp, entry);
  175. if (ret < 0)
  176. p = parent->rb_left;
  177. else if (ret > 0)
  178. p = parent->rb_right;
  179. else
  180. return entry;
  181. }
  182. if (next) {
  183. while (parent && __compare_inode_defrag(&tmp, entry) > 0) {
  184. parent = rb_next(parent);
  185. entry = rb_entry(parent, struct inode_defrag, rb_node);
  186. }
  187. *next = parent;
  188. }
  189. return NULL;
  190. }
  191. /*
  192. * run through the list of inodes in the FS that need
  193. * defragging
  194. */
  195. int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
  196. {
  197. struct inode_defrag *defrag;
  198. struct btrfs_root *inode_root;
  199. struct inode *inode;
  200. struct rb_node *n;
  201. struct btrfs_key key;
  202. struct btrfs_ioctl_defrag_range_args range;
  203. u64 first_ino = 0;
  204. u64 root_objectid = 0;
  205. int num_defrag;
  206. int defrag_batch = 1024;
  207. memset(&range, 0, sizeof(range));
  208. range.len = (u64)-1;
  209. atomic_inc(&fs_info->defrag_running);
  210. spin_lock(&fs_info->defrag_inodes_lock);
  211. while(1) {
  212. n = NULL;
  213. /* find an inode to defrag */
  214. defrag = btrfs_find_defrag_inode(fs_info, root_objectid,
  215. first_ino, &n);
  216. if (!defrag) {
  217. if (n) {
  218. defrag = rb_entry(n, struct inode_defrag,
  219. rb_node);
  220. } else if (root_objectid || first_ino) {
  221. root_objectid = 0;
  222. first_ino = 0;
  223. continue;
  224. } else {
  225. break;
  226. }
  227. }
  228. /* remove it from the rbtree */
  229. first_ino = defrag->ino + 1;
  230. root_objectid = defrag->root;
  231. rb_erase(&defrag->rb_node, &fs_info->defrag_inodes);
  232. if (btrfs_fs_closing(fs_info))
  233. goto next_free;
  234. spin_unlock(&fs_info->defrag_inodes_lock);
  235. /* get the inode */
  236. key.objectid = defrag->root;
  237. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  238. key.offset = (u64)-1;
  239. inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
  240. if (IS_ERR(inode_root))
  241. goto next;
  242. key.objectid = defrag->ino;
  243. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  244. key.offset = 0;
  245. inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
  246. if (IS_ERR(inode))
  247. goto next;
  248. /* do a chunk of defrag */
  249. clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
  250. range.start = defrag->last_offset;
  251. num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
  252. defrag_batch);
  253. /*
  254. * if we filled the whole defrag batch, there
  255. * must be more work to do. Queue this defrag
  256. * again
  257. */
  258. if (num_defrag == defrag_batch) {
  259. defrag->last_offset = range.start;
  260. __btrfs_add_inode_defrag(inode, defrag);
  261. /*
  262. * we don't want to kfree defrag, we added it back to
  263. * the rbtree
  264. */
  265. defrag = NULL;
  266. } else if (defrag->last_offset && !defrag->cycled) {
  267. /*
  268. * we didn't fill our defrag batch, but
  269. * we didn't start at zero. Make sure we loop
  270. * around to the start of the file.
  271. */
  272. defrag->last_offset = 0;
  273. defrag->cycled = 1;
  274. __btrfs_add_inode_defrag(inode, defrag);
  275. defrag = NULL;
  276. }
  277. iput(inode);
  278. next:
  279. spin_lock(&fs_info->defrag_inodes_lock);
  280. next_free:
  281. kfree(defrag);
  282. }
  283. spin_unlock(&fs_info->defrag_inodes_lock);
  284. atomic_dec(&fs_info->defrag_running);
  285. /*
  286. * during unmount, we use the transaction_wait queue to
  287. * wait for the defragger to stop
  288. */
  289. wake_up(&fs_info->transaction_wait);
  290. return 0;
  291. }
  292. /* simple helper to fault in pages and copy. This should go away
  293. * and be replaced with calls into generic code.
  294. */
  295. static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
  296. size_t write_bytes,
  297. struct page **prepared_pages,
  298. struct iov_iter *i)
  299. {
  300. size_t copied = 0;
  301. size_t total_copied = 0;
  302. int pg = 0;
  303. int offset = pos & (PAGE_CACHE_SIZE - 1);
  304. while (write_bytes > 0) {
  305. size_t count = min_t(size_t,
  306. PAGE_CACHE_SIZE - offset, write_bytes);
  307. struct page *page = prepared_pages[pg];
  308. /*
  309. * Copy data from userspace to the current page
  310. *
  311. * Disable pagefault to avoid recursive lock since
  312. * the pages are already locked
  313. */
  314. pagefault_disable();
  315. copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
  316. pagefault_enable();
  317. /* Flush processor's dcache for this page */
  318. flush_dcache_page(page);
  319. /*
  320. * if we get a partial write, we can end up with
  321. * partially up to date pages. These add
  322. * a lot of complexity, so make sure they don't
  323. * happen by forcing this copy to be retried.
  324. *
  325. * The rest of the btrfs_file_write code will fall
  326. * back to page at a time copies after we return 0.
  327. */
  328. if (!PageUptodate(page) && copied < count)
  329. copied = 0;
  330. iov_iter_advance(i, copied);
  331. write_bytes -= copied;
  332. total_copied += copied;
  333. /* Return to btrfs_file_aio_write to fault page */
  334. if (unlikely(copied == 0))
  335. break;
  336. if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
  337. offset += copied;
  338. } else {
  339. pg++;
  340. offset = 0;
  341. }
  342. }
  343. return total_copied;
  344. }
  345. /*
  346. * unlocks pages after btrfs_file_write is done with them
  347. */
  348. void btrfs_drop_pages(struct page **pages, size_t num_pages)
  349. {
  350. size_t i;
  351. for (i = 0; i < num_pages; i++) {
  352. /* page checked is some magic around finding pages that
  353. * have been modified without going through btrfs_set_page_dirty
  354. * clear it here
  355. */
  356. ClearPageChecked(pages[i]);
  357. unlock_page(pages[i]);
  358. mark_page_accessed(pages[i]);
  359. page_cache_release(pages[i]);
  360. }
  361. }
  362. /*
  363. * after copy_from_user, pages need to be dirtied and we need to make
  364. * sure holes are created between the current EOF and the start of
  365. * any next extents (if required).
  366. *
  367. * this also makes the decision about creating an inline extent vs
  368. * doing real data extents, marking pages dirty and delalloc as required.
  369. */
  370. int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
  371. struct page **pages, size_t num_pages,
  372. loff_t pos, size_t write_bytes,
  373. struct extent_state **cached)
  374. {
  375. int err = 0;
  376. int i;
  377. u64 num_bytes;
  378. u64 start_pos;
  379. u64 end_of_last_block;
  380. u64 end_pos = pos + write_bytes;
  381. loff_t isize = i_size_read(inode);
  382. start_pos = pos & ~((u64)root->sectorsize - 1);
  383. num_bytes = (write_bytes + pos - start_pos +
  384. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  385. end_of_last_block = start_pos + num_bytes - 1;
  386. err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
  387. cached);
  388. if (err)
  389. return err;
  390. for (i = 0; i < num_pages; i++) {
  391. struct page *p = pages[i];
  392. SetPageUptodate(p);
  393. ClearPageChecked(p);
  394. set_page_dirty(p);
  395. }
  396. /*
  397. * we've only changed i_size in ram, and we haven't updated
  398. * the disk i_size. There is no need to log the inode
  399. * at this time.
  400. */
  401. if (end_pos > isize)
  402. i_size_write(inode, end_pos);
  403. return 0;
  404. }
  405. /*
  406. * this drops all the extents in the cache that intersect the range
  407. * [start, end]. Existing extents are split as required.
  408. */
  409. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  410. int skip_pinned)
  411. {
  412. struct extent_map *em;
  413. struct extent_map *split = NULL;
  414. struct extent_map *split2 = NULL;
  415. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  416. u64 len = end - start + 1;
  417. int ret;
  418. int testend = 1;
  419. unsigned long flags;
  420. int compressed = 0;
  421. WARN_ON(end < start);
  422. if (end == (u64)-1) {
  423. len = (u64)-1;
  424. testend = 0;
  425. }
  426. while (1) {
  427. if (!split)
  428. split = alloc_extent_map();
  429. if (!split2)
  430. split2 = alloc_extent_map();
  431. BUG_ON(!split || !split2); /* -ENOMEM */
  432. write_lock(&em_tree->lock);
  433. em = lookup_extent_mapping(em_tree, start, len);
  434. if (!em) {
  435. write_unlock(&em_tree->lock);
  436. break;
  437. }
  438. flags = em->flags;
  439. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  440. if (testend && em->start + em->len >= start + len) {
  441. free_extent_map(em);
  442. write_unlock(&em_tree->lock);
  443. break;
  444. }
  445. start = em->start + em->len;
  446. if (testend)
  447. len = start + len - (em->start + em->len);
  448. free_extent_map(em);
  449. write_unlock(&em_tree->lock);
  450. continue;
  451. }
  452. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  453. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  454. remove_extent_mapping(em_tree, em);
  455. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  456. em->start < start) {
  457. split->start = em->start;
  458. split->len = start - em->start;
  459. split->orig_start = em->orig_start;
  460. split->block_start = em->block_start;
  461. if (compressed)
  462. split->block_len = em->block_len;
  463. else
  464. split->block_len = split->len;
  465. split->bdev = em->bdev;
  466. split->flags = flags;
  467. split->compress_type = em->compress_type;
  468. ret = add_extent_mapping(em_tree, split);
  469. BUG_ON(ret); /* Logic error */
  470. free_extent_map(split);
  471. split = split2;
  472. split2 = NULL;
  473. }
  474. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  475. testend && em->start + em->len > start + len) {
  476. u64 diff = start + len - em->start;
  477. split->start = start + len;
  478. split->len = em->start + em->len - (start + len);
  479. split->bdev = em->bdev;
  480. split->flags = flags;
  481. split->compress_type = em->compress_type;
  482. if (compressed) {
  483. split->block_len = em->block_len;
  484. split->block_start = em->block_start;
  485. split->orig_start = em->orig_start;
  486. } else {
  487. split->block_len = split->len;
  488. split->block_start = em->block_start + diff;
  489. split->orig_start = split->start;
  490. }
  491. ret = add_extent_mapping(em_tree, split);
  492. BUG_ON(ret); /* Logic error */
  493. free_extent_map(split);
  494. split = NULL;
  495. }
  496. write_unlock(&em_tree->lock);
  497. /* once for us */
  498. free_extent_map(em);
  499. /* once for the tree*/
  500. free_extent_map(em);
  501. }
  502. if (split)
  503. free_extent_map(split);
  504. if (split2)
  505. free_extent_map(split2);
  506. return 0;
  507. }
  508. /*
  509. * this is very complex, but the basic idea is to drop all extents
  510. * in the range start - end. hint_block is filled in with a block number
  511. * that would be a good hint to the block allocator for this file.
  512. *
  513. * If an extent intersects the range but is not entirely inside the range
  514. * it is either truncated or split. Anything entirely inside the range
  515. * is deleted from the tree.
  516. */
  517. int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
  518. u64 start, u64 end, u64 *hint_byte, int drop_cache)
  519. {
  520. struct btrfs_root *root = BTRFS_I(inode)->root;
  521. struct extent_buffer *leaf;
  522. struct btrfs_file_extent_item *fi;
  523. struct btrfs_path *path;
  524. struct btrfs_key key;
  525. struct btrfs_key new_key;
  526. u64 ino = btrfs_ino(inode);
  527. u64 search_start = start;
  528. u64 disk_bytenr = 0;
  529. u64 num_bytes = 0;
  530. u64 extent_offset = 0;
  531. u64 extent_end = 0;
  532. int del_nr = 0;
  533. int del_slot = 0;
  534. int extent_type;
  535. int recow;
  536. int ret;
  537. int modify_tree = -1;
  538. if (drop_cache)
  539. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  540. path = btrfs_alloc_path();
  541. if (!path)
  542. return -ENOMEM;
  543. if (start >= BTRFS_I(inode)->disk_i_size)
  544. modify_tree = 0;
  545. while (1) {
  546. recow = 0;
  547. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  548. search_start, modify_tree);
  549. if (ret < 0)
  550. break;
  551. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  552. leaf = path->nodes[0];
  553. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  554. if (key.objectid == ino &&
  555. key.type == BTRFS_EXTENT_DATA_KEY)
  556. path->slots[0]--;
  557. }
  558. ret = 0;
  559. next_slot:
  560. leaf = path->nodes[0];
  561. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  562. BUG_ON(del_nr > 0);
  563. ret = btrfs_next_leaf(root, path);
  564. if (ret < 0)
  565. break;
  566. if (ret > 0) {
  567. ret = 0;
  568. break;
  569. }
  570. leaf = path->nodes[0];
  571. recow = 1;
  572. }
  573. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  574. if (key.objectid > ino ||
  575. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  576. break;
  577. fi = btrfs_item_ptr(leaf, path->slots[0],
  578. struct btrfs_file_extent_item);
  579. extent_type = btrfs_file_extent_type(leaf, fi);
  580. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  581. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  582. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  583. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  584. extent_offset = btrfs_file_extent_offset(leaf, fi);
  585. extent_end = key.offset +
  586. btrfs_file_extent_num_bytes(leaf, fi);
  587. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  588. extent_end = key.offset +
  589. btrfs_file_extent_inline_len(leaf, fi);
  590. } else {
  591. WARN_ON(1);
  592. extent_end = search_start;
  593. }
  594. if (extent_end <= search_start) {
  595. path->slots[0]++;
  596. goto next_slot;
  597. }
  598. search_start = max(key.offset, start);
  599. if (recow || !modify_tree) {
  600. modify_tree = -1;
  601. btrfs_release_path(path);
  602. continue;
  603. }
  604. /*
  605. * | - range to drop - |
  606. * | -------- extent -------- |
  607. */
  608. if (start > key.offset && end < extent_end) {
  609. BUG_ON(del_nr > 0);
  610. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  611. memcpy(&new_key, &key, sizeof(new_key));
  612. new_key.offset = start;
  613. ret = btrfs_duplicate_item(trans, root, path,
  614. &new_key);
  615. if (ret == -EAGAIN) {
  616. btrfs_release_path(path);
  617. continue;
  618. }
  619. if (ret < 0)
  620. break;
  621. leaf = path->nodes[0];
  622. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  623. struct btrfs_file_extent_item);
  624. btrfs_set_file_extent_num_bytes(leaf, fi,
  625. start - key.offset);
  626. fi = btrfs_item_ptr(leaf, path->slots[0],
  627. struct btrfs_file_extent_item);
  628. extent_offset += start - key.offset;
  629. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  630. btrfs_set_file_extent_num_bytes(leaf, fi,
  631. extent_end - start);
  632. btrfs_mark_buffer_dirty(leaf);
  633. if (disk_bytenr > 0) {
  634. ret = btrfs_inc_extent_ref(trans, root,
  635. disk_bytenr, num_bytes, 0,
  636. root->root_key.objectid,
  637. new_key.objectid,
  638. start - extent_offset, 0);
  639. BUG_ON(ret); /* -ENOMEM */
  640. *hint_byte = disk_bytenr;
  641. }
  642. key.offset = start;
  643. }
  644. /*
  645. * | ---- range to drop ----- |
  646. * | -------- extent -------- |
  647. */
  648. if (start <= key.offset && end < extent_end) {
  649. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  650. memcpy(&new_key, &key, sizeof(new_key));
  651. new_key.offset = end;
  652. btrfs_set_item_key_safe(trans, root, path, &new_key);
  653. extent_offset += end - key.offset;
  654. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  655. btrfs_set_file_extent_num_bytes(leaf, fi,
  656. extent_end - end);
  657. btrfs_mark_buffer_dirty(leaf);
  658. if (disk_bytenr > 0) {
  659. inode_sub_bytes(inode, end - key.offset);
  660. *hint_byte = disk_bytenr;
  661. }
  662. break;
  663. }
  664. search_start = extent_end;
  665. /*
  666. * | ---- range to drop ----- |
  667. * | -------- extent -------- |
  668. */
  669. if (start > key.offset && end >= extent_end) {
  670. BUG_ON(del_nr > 0);
  671. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  672. btrfs_set_file_extent_num_bytes(leaf, fi,
  673. start - key.offset);
  674. btrfs_mark_buffer_dirty(leaf);
  675. if (disk_bytenr > 0) {
  676. inode_sub_bytes(inode, extent_end - start);
  677. *hint_byte = disk_bytenr;
  678. }
  679. if (end == extent_end)
  680. break;
  681. path->slots[0]++;
  682. goto next_slot;
  683. }
  684. /*
  685. * | ---- range to drop ----- |
  686. * | ------ extent ------ |
  687. */
  688. if (start <= key.offset && end >= extent_end) {
  689. if (del_nr == 0) {
  690. del_slot = path->slots[0];
  691. del_nr = 1;
  692. } else {
  693. BUG_ON(del_slot + del_nr != path->slots[0]);
  694. del_nr++;
  695. }
  696. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  697. inode_sub_bytes(inode,
  698. extent_end - key.offset);
  699. extent_end = ALIGN(extent_end,
  700. root->sectorsize);
  701. } else if (disk_bytenr > 0) {
  702. ret = btrfs_free_extent(trans, root,
  703. disk_bytenr, num_bytes, 0,
  704. root->root_key.objectid,
  705. key.objectid, key.offset -
  706. extent_offset, 0);
  707. BUG_ON(ret); /* -ENOMEM */
  708. inode_sub_bytes(inode,
  709. extent_end - key.offset);
  710. *hint_byte = disk_bytenr;
  711. }
  712. if (end == extent_end)
  713. break;
  714. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  715. path->slots[0]++;
  716. goto next_slot;
  717. }
  718. ret = btrfs_del_items(trans, root, path, del_slot,
  719. del_nr);
  720. if (ret) {
  721. btrfs_abort_transaction(trans, root, ret);
  722. goto out;
  723. }
  724. del_nr = 0;
  725. del_slot = 0;
  726. btrfs_release_path(path);
  727. continue;
  728. }
  729. BUG_ON(1);
  730. }
  731. if (!ret && del_nr > 0) {
  732. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  733. if (ret)
  734. btrfs_abort_transaction(trans, root, ret);
  735. }
  736. out:
  737. btrfs_free_path(path);
  738. return ret;
  739. }
  740. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  741. u64 objectid, u64 bytenr, u64 orig_offset,
  742. u64 *start, u64 *end)
  743. {
  744. struct btrfs_file_extent_item *fi;
  745. struct btrfs_key key;
  746. u64 extent_end;
  747. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  748. return 0;
  749. btrfs_item_key_to_cpu(leaf, &key, slot);
  750. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  751. return 0;
  752. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  753. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  754. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  755. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  756. btrfs_file_extent_compression(leaf, fi) ||
  757. btrfs_file_extent_encryption(leaf, fi) ||
  758. btrfs_file_extent_other_encoding(leaf, fi))
  759. return 0;
  760. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  761. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  762. return 0;
  763. *start = key.offset;
  764. *end = extent_end;
  765. return 1;
  766. }
  767. /*
  768. * Mark extent in the range start - end as written.
  769. *
  770. * This changes extent type from 'pre-allocated' to 'regular'. If only
  771. * part of extent is marked as written, the extent will be split into
  772. * two or three.
  773. */
  774. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  775. struct inode *inode, u64 start, u64 end)
  776. {
  777. struct btrfs_root *root = BTRFS_I(inode)->root;
  778. struct extent_buffer *leaf;
  779. struct btrfs_path *path;
  780. struct btrfs_file_extent_item *fi;
  781. struct btrfs_key key;
  782. struct btrfs_key new_key;
  783. u64 bytenr;
  784. u64 num_bytes;
  785. u64 extent_end;
  786. u64 orig_offset;
  787. u64 other_start;
  788. u64 other_end;
  789. u64 split;
  790. int del_nr = 0;
  791. int del_slot = 0;
  792. int recow;
  793. int ret;
  794. u64 ino = btrfs_ino(inode);
  795. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  796. path = btrfs_alloc_path();
  797. if (!path)
  798. return -ENOMEM;
  799. again:
  800. recow = 0;
  801. split = start;
  802. key.objectid = ino;
  803. key.type = BTRFS_EXTENT_DATA_KEY;
  804. key.offset = split;
  805. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  806. if (ret < 0)
  807. goto out;
  808. if (ret > 0 && path->slots[0] > 0)
  809. path->slots[0]--;
  810. leaf = path->nodes[0];
  811. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  812. BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
  813. fi = btrfs_item_ptr(leaf, path->slots[0],
  814. struct btrfs_file_extent_item);
  815. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  816. BTRFS_FILE_EXTENT_PREALLOC);
  817. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  818. BUG_ON(key.offset > start || extent_end < end);
  819. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  820. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  821. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  822. memcpy(&new_key, &key, sizeof(new_key));
  823. if (start == key.offset && end < extent_end) {
  824. other_start = 0;
  825. other_end = start;
  826. if (extent_mergeable(leaf, path->slots[0] - 1,
  827. ino, bytenr, orig_offset,
  828. &other_start, &other_end)) {
  829. new_key.offset = end;
  830. btrfs_set_item_key_safe(trans, root, path, &new_key);
  831. fi = btrfs_item_ptr(leaf, path->slots[0],
  832. struct btrfs_file_extent_item);
  833. btrfs_set_file_extent_generation(leaf, fi,
  834. trans->transid);
  835. btrfs_set_file_extent_num_bytes(leaf, fi,
  836. extent_end - end);
  837. btrfs_set_file_extent_offset(leaf, fi,
  838. end - orig_offset);
  839. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  840. struct btrfs_file_extent_item);
  841. btrfs_set_file_extent_generation(leaf, fi,
  842. trans->transid);
  843. btrfs_set_file_extent_num_bytes(leaf, fi,
  844. end - other_start);
  845. btrfs_mark_buffer_dirty(leaf);
  846. goto out;
  847. }
  848. }
  849. if (start > key.offset && end == extent_end) {
  850. other_start = end;
  851. other_end = 0;
  852. if (extent_mergeable(leaf, path->slots[0] + 1,
  853. ino, bytenr, orig_offset,
  854. &other_start, &other_end)) {
  855. fi = btrfs_item_ptr(leaf, path->slots[0],
  856. struct btrfs_file_extent_item);
  857. btrfs_set_file_extent_num_bytes(leaf, fi,
  858. start - key.offset);
  859. btrfs_set_file_extent_generation(leaf, fi,
  860. trans->transid);
  861. path->slots[0]++;
  862. new_key.offset = start;
  863. btrfs_set_item_key_safe(trans, root, path, &new_key);
  864. fi = btrfs_item_ptr(leaf, path->slots[0],
  865. struct btrfs_file_extent_item);
  866. btrfs_set_file_extent_generation(leaf, fi,
  867. trans->transid);
  868. btrfs_set_file_extent_num_bytes(leaf, fi,
  869. other_end - start);
  870. btrfs_set_file_extent_offset(leaf, fi,
  871. start - orig_offset);
  872. btrfs_mark_buffer_dirty(leaf);
  873. goto out;
  874. }
  875. }
  876. while (start > key.offset || end < extent_end) {
  877. if (key.offset == start)
  878. split = end;
  879. new_key.offset = split;
  880. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  881. if (ret == -EAGAIN) {
  882. btrfs_release_path(path);
  883. goto again;
  884. }
  885. if (ret < 0) {
  886. btrfs_abort_transaction(trans, root, ret);
  887. goto out;
  888. }
  889. leaf = path->nodes[0];
  890. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  891. struct btrfs_file_extent_item);
  892. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  893. btrfs_set_file_extent_num_bytes(leaf, fi,
  894. split - key.offset);
  895. fi = btrfs_item_ptr(leaf, path->slots[0],
  896. struct btrfs_file_extent_item);
  897. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  898. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  899. btrfs_set_file_extent_num_bytes(leaf, fi,
  900. extent_end - split);
  901. btrfs_mark_buffer_dirty(leaf);
  902. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  903. root->root_key.objectid,
  904. ino, orig_offset, 0);
  905. BUG_ON(ret); /* -ENOMEM */
  906. if (split == start) {
  907. key.offset = start;
  908. } else {
  909. BUG_ON(start != key.offset);
  910. path->slots[0]--;
  911. extent_end = end;
  912. }
  913. recow = 1;
  914. }
  915. other_start = end;
  916. other_end = 0;
  917. if (extent_mergeable(leaf, path->slots[0] + 1,
  918. ino, bytenr, orig_offset,
  919. &other_start, &other_end)) {
  920. if (recow) {
  921. btrfs_release_path(path);
  922. goto again;
  923. }
  924. extent_end = other_end;
  925. del_slot = path->slots[0] + 1;
  926. del_nr++;
  927. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  928. 0, root->root_key.objectid,
  929. ino, orig_offset, 0);
  930. BUG_ON(ret); /* -ENOMEM */
  931. }
  932. other_start = 0;
  933. other_end = start;
  934. if (extent_mergeable(leaf, path->slots[0] - 1,
  935. ino, bytenr, orig_offset,
  936. &other_start, &other_end)) {
  937. if (recow) {
  938. btrfs_release_path(path);
  939. goto again;
  940. }
  941. key.offset = other_start;
  942. del_slot = path->slots[0];
  943. del_nr++;
  944. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  945. 0, root->root_key.objectid,
  946. ino, orig_offset, 0);
  947. BUG_ON(ret); /* -ENOMEM */
  948. }
  949. if (del_nr == 0) {
  950. fi = btrfs_item_ptr(leaf, path->slots[0],
  951. struct btrfs_file_extent_item);
  952. btrfs_set_file_extent_type(leaf, fi,
  953. BTRFS_FILE_EXTENT_REG);
  954. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  955. btrfs_mark_buffer_dirty(leaf);
  956. } else {
  957. fi = btrfs_item_ptr(leaf, del_slot - 1,
  958. struct btrfs_file_extent_item);
  959. btrfs_set_file_extent_type(leaf, fi,
  960. BTRFS_FILE_EXTENT_REG);
  961. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  962. btrfs_set_file_extent_num_bytes(leaf, fi,
  963. extent_end - key.offset);
  964. btrfs_mark_buffer_dirty(leaf);
  965. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  966. if (ret < 0) {
  967. btrfs_abort_transaction(trans, root, ret);
  968. goto out;
  969. }
  970. }
  971. out:
  972. btrfs_free_path(path);
  973. return 0;
  974. }
  975. /*
  976. * on error we return an unlocked page and the error value
  977. * on success we return a locked page and 0
  978. */
  979. static int prepare_uptodate_page(struct page *page, u64 pos,
  980. bool force_uptodate)
  981. {
  982. int ret = 0;
  983. if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
  984. !PageUptodate(page)) {
  985. ret = btrfs_readpage(NULL, page);
  986. if (ret)
  987. return ret;
  988. lock_page(page);
  989. if (!PageUptodate(page)) {
  990. unlock_page(page);
  991. return -EIO;
  992. }
  993. }
  994. return 0;
  995. }
  996. /*
  997. * this gets pages into the page cache and locks them down, it also properly
  998. * waits for data=ordered extents to finish before allowing the pages to be
  999. * modified.
  1000. */
  1001. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  1002. struct page **pages, size_t num_pages,
  1003. loff_t pos, unsigned long first_index,
  1004. size_t write_bytes, bool force_uptodate)
  1005. {
  1006. struct extent_state *cached_state = NULL;
  1007. int i;
  1008. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1009. struct inode *inode = fdentry(file)->d_inode;
  1010. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1011. int err = 0;
  1012. int faili = 0;
  1013. u64 start_pos;
  1014. u64 last_pos;
  1015. start_pos = pos & ~((u64)root->sectorsize - 1);
  1016. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  1017. again:
  1018. for (i = 0; i < num_pages; i++) {
  1019. pages[i] = find_or_create_page(inode->i_mapping, index + i,
  1020. mask | __GFP_WRITE);
  1021. if (!pages[i]) {
  1022. faili = i - 1;
  1023. err = -ENOMEM;
  1024. goto fail;
  1025. }
  1026. if (i == 0)
  1027. err = prepare_uptodate_page(pages[i], pos,
  1028. force_uptodate);
  1029. if (i == num_pages - 1)
  1030. err = prepare_uptodate_page(pages[i],
  1031. pos + write_bytes, false);
  1032. if (err) {
  1033. page_cache_release(pages[i]);
  1034. faili = i - 1;
  1035. goto fail;
  1036. }
  1037. wait_on_page_writeback(pages[i]);
  1038. }
  1039. err = 0;
  1040. if (start_pos < inode->i_size) {
  1041. struct btrfs_ordered_extent *ordered;
  1042. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1043. start_pos, last_pos - 1, 0, &cached_state);
  1044. ordered = btrfs_lookup_first_ordered_extent(inode,
  1045. last_pos - 1);
  1046. if (ordered &&
  1047. ordered->file_offset + ordered->len > start_pos &&
  1048. ordered->file_offset < last_pos) {
  1049. btrfs_put_ordered_extent(ordered);
  1050. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1051. start_pos, last_pos - 1,
  1052. &cached_state, GFP_NOFS);
  1053. for (i = 0; i < num_pages; i++) {
  1054. unlock_page(pages[i]);
  1055. page_cache_release(pages[i]);
  1056. }
  1057. btrfs_wait_ordered_range(inode, start_pos,
  1058. last_pos - start_pos);
  1059. goto again;
  1060. }
  1061. if (ordered)
  1062. btrfs_put_ordered_extent(ordered);
  1063. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  1064. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1065. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  1066. GFP_NOFS);
  1067. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1068. start_pos, last_pos - 1, &cached_state,
  1069. GFP_NOFS);
  1070. }
  1071. for (i = 0; i < num_pages; i++) {
  1072. if (clear_page_dirty_for_io(pages[i]))
  1073. account_page_redirty(pages[i]);
  1074. set_page_extent_mapped(pages[i]);
  1075. WARN_ON(!PageLocked(pages[i]));
  1076. }
  1077. return 0;
  1078. fail:
  1079. while (faili >= 0) {
  1080. unlock_page(pages[faili]);
  1081. page_cache_release(pages[faili]);
  1082. faili--;
  1083. }
  1084. return err;
  1085. }
  1086. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  1087. struct iov_iter *i,
  1088. loff_t pos)
  1089. {
  1090. struct inode *inode = fdentry(file)->d_inode;
  1091. struct btrfs_root *root = BTRFS_I(inode)->root;
  1092. struct page **pages = NULL;
  1093. unsigned long first_index;
  1094. size_t num_written = 0;
  1095. int nrptrs;
  1096. int ret = 0;
  1097. bool force_page_uptodate = false;
  1098. nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
  1099. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  1100. (sizeof(struct page *)));
  1101. nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
  1102. nrptrs = max(nrptrs, 8);
  1103. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  1104. if (!pages)
  1105. return -ENOMEM;
  1106. first_index = pos >> PAGE_CACHE_SHIFT;
  1107. while (iov_iter_count(i) > 0) {
  1108. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1109. size_t write_bytes = min(iov_iter_count(i),
  1110. nrptrs * (size_t)PAGE_CACHE_SIZE -
  1111. offset);
  1112. size_t num_pages = (write_bytes + offset +
  1113. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1114. size_t dirty_pages;
  1115. size_t copied;
  1116. WARN_ON(num_pages > nrptrs);
  1117. /*
  1118. * Fault pages before locking them in prepare_pages
  1119. * to avoid recursive lock
  1120. */
  1121. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  1122. ret = -EFAULT;
  1123. break;
  1124. }
  1125. ret = btrfs_delalloc_reserve_space(inode,
  1126. num_pages << PAGE_CACHE_SHIFT);
  1127. if (ret)
  1128. break;
  1129. /*
  1130. * This is going to setup the pages array with the number of
  1131. * pages we want, so we don't really need to worry about the
  1132. * contents of pages from loop to loop
  1133. */
  1134. ret = prepare_pages(root, file, pages, num_pages,
  1135. pos, first_index, write_bytes,
  1136. force_page_uptodate);
  1137. if (ret) {
  1138. btrfs_delalloc_release_space(inode,
  1139. num_pages << PAGE_CACHE_SHIFT);
  1140. break;
  1141. }
  1142. copied = btrfs_copy_from_user(pos, num_pages,
  1143. write_bytes, pages, i);
  1144. /*
  1145. * if we have trouble faulting in the pages, fall
  1146. * back to one page at a time
  1147. */
  1148. if (copied < write_bytes)
  1149. nrptrs = 1;
  1150. if (copied == 0) {
  1151. force_page_uptodate = true;
  1152. dirty_pages = 0;
  1153. } else {
  1154. force_page_uptodate = false;
  1155. dirty_pages = (copied + offset +
  1156. PAGE_CACHE_SIZE - 1) >>
  1157. PAGE_CACHE_SHIFT;
  1158. }
  1159. /*
  1160. * If we had a short copy we need to release the excess delaloc
  1161. * bytes we reserved. We need to increment outstanding_extents
  1162. * because btrfs_delalloc_release_space will decrement it, but
  1163. * we still have an outstanding extent for the chunk we actually
  1164. * managed to copy.
  1165. */
  1166. if (num_pages > dirty_pages) {
  1167. if (copied > 0) {
  1168. spin_lock(&BTRFS_I(inode)->lock);
  1169. BTRFS_I(inode)->outstanding_extents++;
  1170. spin_unlock(&BTRFS_I(inode)->lock);
  1171. }
  1172. btrfs_delalloc_release_space(inode,
  1173. (num_pages - dirty_pages) <<
  1174. PAGE_CACHE_SHIFT);
  1175. }
  1176. if (copied > 0) {
  1177. ret = btrfs_dirty_pages(root, inode, pages,
  1178. dirty_pages, pos, copied,
  1179. NULL);
  1180. if (ret) {
  1181. btrfs_delalloc_release_space(inode,
  1182. dirty_pages << PAGE_CACHE_SHIFT);
  1183. btrfs_drop_pages(pages, num_pages);
  1184. break;
  1185. }
  1186. }
  1187. btrfs_drop_pages(pages, num_pages);
  1188. cond_resched();
  1189. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  1190. dirty_pages);
  1191. if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1192. btrfs_btree_balance_dirty(root, 1);
  1193. pos += copied;
  1194. num_written += copied;
  1195. }
  1196. kfree(pages);
  1197. return num_written ? num_written : ret;
  1198. }
  1199. static ssize_t __btrfs_direct_write(struct kiocb *iocb,
  1200. const struct iovec *iov,
  1201. unsigned long nr_segs, loff_t pos,
  1202. loff_t *ppos, size_t count, size_t ocount)
  1203. {
  1204. struct file *file = iocb->ki_filp;
  1205. struct iov_iter i;
  1206. ssize_t written;
  1207. ssize_t written_buffered;
  1208. loff_t endbyte;
  1209. int err;
  1210. written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
  1211. count, ocount);
  1212. if (written < 0 || written == count)
  1213. return written;
  1214. pos += written;
  1215. count -= written;
  1216. iov_iter_init(&i, iov, nr_segs, count, written);
  1217. written_buffered = __btrfs_buffered_write(file, &i, pos);
  1218. if (written_buffered < 0) {
  1219. err = written_buffered;
  1220. goto out;
  1221. }
  1222. endbyte = pos + written_buffered - 1;
  1223. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  1224. if (err)
  1225. goto out;
  1226. written += written_buffered;
  1227. *ppos = pos + written_buffered;
  1228. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
  1229. endbyte >> PAGE_CACHE_SHIFT);
  1230. out:
  1231. return written ? written : err;
  1232. }
  1233. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  1234. const struct iovec *iov,
  1235. unsigned long nr_segs, loff_t pos)
  1236. {
  1237. struct file *file = iocb->ki_filp;
  1238. struct inode *inode = fdentry(file)->d_inode;
  1239. struct btrfs_root *root = BTRFS_I(inode)->root;
  1240. loff_t *ppos = &iocb->ki_pos;
  1241. u64 start_pos;
  1242. ssize_t num_written = 0;
  1243. ssize_t err = 0;
  1244. size_t count, ocount;
  1245. sb_start_write(inode->i_sb);
  1246. mutex_lock(&inode->i_mutex);
  1247. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1248. if (err) {
  1249. mutex_unlock(&inode->i_mutex);
  1250. goto out;
  1251. }
  1252. count = ocount;
  1253. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1254. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1255. if (err) {
  1256. mutex_unlock(&inode->i_mutex);
  1257. goto out;
  1258. }
  1259. if (count == 0) {
  1260. mutex_unlock(&inode->i_mutex);
  1261. goto out;
  1262. }
  1263. err = file_remove_suid(file);
  1264. if (err) {
  1265. mutex_unlock(&inode->i_mutex);
  1266. goto out;
  1267. }
  1268. /*
  1269. * If BTRFS flips readonly due to some impossible error
  1270. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1271. * although we have opened a file as writable, we have
  1272. * to stop this write operation to ensure FS consistency.
  1273. */
  1274. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  1275. mutex_unlock(&inode->i_mutex);
  1276. err = -EROFS;
  1277. goto out;
  1278. }
  1279. err = file_update_time(file);
  1280. if (err) {
  1281. mutex_unlock(&inode->i_mutex);
  1282. goto out;
  1283. }
  1284. start_pos = round_down(pos, root->sectorsize);
  1285. if (start_pos > i_size_read(inode)) {
  1286. err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
  1287. if (err) {
  1288. mutex_unlock(&inode->i_mutex);
  1289. goto out;
  1290. }
  1291. }
  1292. if (unlikely(file->f_flags & O_DIRECT)) {
  1293. num_written = __btrfs_direct_write(iocb, iov, nr_segs,
  1294. pos, ppos, count, ocount);
  1295. } else {
  1296. struct iov_iter i;
  1297. iov_iter_init(&i, iov, nr_segs, count, num_written);
  1298. num_written = __btrfs_buffered_write(file, &i, pos);
  1299. if (num_written > 0)
  1300. *ppos = pos + num_written;
  1301. }
  1302. mutex_unlock(&inode->i_mutex);
  1303. /*
  1304. * we want to make sure fsync finds this change
  1305. * but we haven't joined a transaction running right now.
  1306. *
  1307. * Later on, someone is sure to update the inode and get the
  1308. * real transid recorded.
  1309. *
  1310. * We set last_trans now to the fs_info generation + 1,
  1311. * this will either be one more than the running transaction
  1312. * or the generation used for the next transaction if there isn't
  1313. * one running right now.
  1314. */
  1315. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  1316. if (num_written > 0 || num_written == -EIOCBQUEUED) {
  1317. err = generic_write_sync(file, pos, num_written);
  1318. if (err < 0 && num_written > 0)
  1319. num_written = err;
  1320. }
  1321. out:
  1322. sb_end_write(inode->i_sb);
  1323. current->backing_dev_info = NULL;
  1324. return num_written ? num_written : err;
  1325. }
  1326. int btrfs_release_file(struct inode *inode, struct file *filp)
  1327. {
  1328. /*
  1329. * ordered_data_close is set by settattr when we are about to truncate
  1330. * a file from a non-zero size to a zero size. This tries to
  1331. * flush down new bytes that may have been written if the
  1332. * application were using truncate to replace a file in place.
  1333. */
  1334. if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  1335. &BTRFS_I(inode)->runtime_flags)) {
  1336. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1337. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1338. filemap_flush(inode->i_mapping);
  1339. }
  1340. if (filp->private_data)
  1341. btrfs_ioctl_trans_end(filp);
  1342. return 0;
  1343. }
  1344. /*
  1345. * fsync call for both files and directories. This logs the inode into
  1346. * the tree log instead of forcing full commits whenever possible.
  1347. *
  1348. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1349. * in the metadata btree are up to date for copying to the log.
  1350. *
  1351. * It drops the inode mutex before doing the tree log commit. This is an
  1352. * important optimization for directories because holding the mutex prevents
  1353. * new operations on the dir while we write to disk.
  1354. */
  1355. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  1356. {
  1357. struct dentry *dentry = file->f_path.dentry;
  1358. struct inode *inode = dentry->d_inode;
  1359. struct btrfs_root *root = BTRFS_I(inode)->root;
  1360. int ret = 0;
  1361. struct btrfs_trans_handle *trans;
  1362. trace_btrfs_sync_file(file, datasync);
  1363. mutex_lock(&inode->i_mutex);
  1364. /*
  1365. * we wait first, since the writeback may change the inode, also wait
  1366. * ordered range does a filemape_write_and_wait_range which is why we
  1367. * don't do it above like other file systems.
  1368. */
  1369. root->log_batch++;
  1370. btrfs_wait_ordered_range(inode, start, end);
  1371. root->log_batch++;
  1372. /*
  1373. * check the transaction that last modified this inode
  1374. * and see if its already been committed
  1375. */
  1376. if (!BTRFS_I(inode)->last_trans) {
  1377. mutex_unlock(&inode->i_mutex);
  1378. goto out;
  1379. }
  1380. /*
  1381. * if the last transaction that changed this file was before
  1382. * the current transaction, we can bail out now without any
  1383. * syncing
  1384. */
  1385. smp_mb();
  1386. if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
  1387. BTRFS_I(inode)->last_trans <=
  1388. root->fs_info->last_trans_committed) {
  1389. BTRFS_I(inode)->last_trans = 0;
  1390. mutex_unlock(&inode->i_mutex);
  1391. goto out;
  1392. }
  1393. /*
  1394. * ok we haven't committed the transaction yet, lets do a commit
  1395. */
  1396. if (file->private_data)
  1397. btrfs_ioctl_trans_end(file);
  1398. trans = btrfs_start_transaction(root, 0);
  1399. if (IS_ERR(trans)) {
  1400. ret = PTR_ERR(trans);
  1401. mutex_unlock(&inode->i_mutex);
  1402. goto out;
  1403. }
  1404. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1405. if (ret < 0) {
  1406. mutex_unlock(&inode->i_mutex);
  1407. goto out;
  1408. }
  1409. /* we've logged all the items and now have a consistent
  1410. * version of the file in the log. It is possible that
  1411. * someone will come in and modify the file, but that's
  1412. * fine because the log is consistent on disk, and we
  1413. * have references to all of the file's extents
  1414. *
  1415. * It is possible that someone will come in and log the
  1416. * file again, but that will end up using the synchronization
  1417. * inside btrfs_sync_log to keep things safe.
  1418. */
  1419. mutex_unlock(&inode->i_mutex);
  1420. if (ret != BTRFS_NO_LOG_SYNC) {
  1421. if (ret > 0) {
  1422. ret = btrfs_commit_transaction(trans, root);
  1423. } else {
  1424. ret = btrfs_sync_log(trans, root);
  1425. if (ret == 0)
  1426. ret = btrfs_end_transaction(trans, root);
  1427. else
  1428. ret = btrfs_commit_transaction(trans, root);
  1429. }
  1430. } else {
  1431. ret = btrfs_end_transaction(trans, root);
  1432. }
  1433. out:
  1434. return ret > 0 ? -EIO : ret;
  1435. }
  1436. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1437. .fault = filemap_fault,
  1438. .page_mkwrite = btrfs_page_mkwrite,
  1439. };
  1440. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1441. {
  1442. struct address_space *mapping = filp->f_mapping;
  1443. if (!mapping->a_ops->readpage)
  1444. return -ENOEXEC;
  1445. file_accessed(filp);
  1446. vma->vm_ops = &btrfs_file_vm_ops;
  1447. vma->vm_flags |= VM_CAN_NONLINEAR;
  1448. return 0;
  1449. }
  1450. static long btrfs_fallocate(struct file *file, int mode,
  1451. loff_t offset, loff_t len)
  1452. {
  1453. struct inode *inode = file->f_path.dentry->d_inode;
  1454. struct extent_state *cached_state = NULL;
  1455. u64 cur_offset;
  1456. u64 last_byte;
  1457. u64 alloc_start;
  1458. u64 alloc_end;
  1459. u64 alloc_hint = 0;
  1460. u64 locked_end;
  1461. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1462. struct extent_map *em;
  1463. int ret;
  1464. alloc_start = offset & ~mask;
  1465. alloc_end = (offset + len + mask) & ~mask;
  1466. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  1467. if (mode & ~FALLOC_FL_KEEP_SIZE)
  1468. return -EOPNOTSUPP;
  1469. /*
  1470. * Make sure we have enough space before we do the
  1471. * allocation.
  1472. */
  1473. ret = btrfs_check_data_free_space(inode, len);
  1474. if (ret)
  1475. return ret;
  1476. /*
  1477. * wait for ordered IO before we have any locks. We'll loop again
  1478. * below with the locks held.
  1479. */
  1480. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1481. mutex_lock(&inode->i_mutex);
  1482. ret = inode_newsize_ok(inode, alloc_end);
  1483. if (ret)
  1484. goto out;
  1485. if (alloc_start > inode->i_size) {
  1486. ret = btrfs_cont_expand(inode, i_size_read(inode),
  1487. alloc_start);
  1488. if (ret)
  1489. goto out;
  1490. }
  1491. locked_end = alloc_end - 1;
  1492. while (1) {
  1493. struct btrfs_ordered_extent *ordered;
  1494. /* the extent lock is ordered inside the running
  1495. * transaction
  1496. */
  1497. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1498. locked_end, 0, &cached_state);
  1499. ordered = btrfs_lookup_first_ordered_extent(inode,
  1500. alloc_end - 1);
  1501. if (ordered &&
  1502. ordered->file_offset + ordered->len > alloc_start &&
  1503. ordered->file_offset < alloc_end) {
  1504. btrfs_put_ordered_extent(ordered);
  1505. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1506. alloc_start, locked_end,
  1507. &cached_state, GFP_NOFS);
  1508. /*
  1509. * we can't wait on the range with the transaction
  1510. * running or with the extent lock held
  1511. */
  1512. btrfs_wait_ordered_range(inode, alloc_start,
  1513. alloc_end - alloc_start);
  1514. } else {
  1515. if (ordered)
  1516. btrfs_put_ordered_extent(ordered);
  1517. break;
  1518. }
  1519. }
  1520. cur_offset = alloc_start;
  1521. while (1) {
  1522. u64 actual_end;
  1523. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1524. alloc_end - cur_offset, 0);
  1525. if (IS_ERR_OR_NULL(em)) {
  1526. if (!em)
  1527. ret = -ENOMEM;
  1528. else
  1529. ret = PTR_ERR(em);
  1530. break;
  1531. }
  1532. last_byte = min(extent_map_end(em), alloc_end);
  1533. actual_end = min_t(u64, extent_map_end(em), offset + len);
  1534. last_byte = (last_byte + mask) & ~mask;
  1535. if (em->block_start == EXTENT_MAP_HOLE ||
  1536. (cur_offset >= inode->i_size &&
  1537. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1538. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1539. last_byte - cur_offset,
  1540. 1 << inode->i_blkbits,
  1541. offset + len,
  1542. &alloc_hint);
  1543. if (ret < 0) {
  1544. free_extent_map(em);
  1545. break;
  1546. }
  1547. } else if (actual_end > inode->i_size &&
  1548. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1549. /*
  1550. * We didn't need to allocate any more space, but we
  1551. * still extended the size of the file so we need to
  1552. * update i_size.
  1553. */
  1554. inode->i_ctime = CURRENT_TIME;
  1555. i_size_write(inode, actual_end);
  1556. btrfs_ordered_update_i_size(inode, actual_end, NULL);
  1557. }
  1558. free_extent_map(em);
  1559. cur_offset = last_byte;
  1560. if (cur_offset >= alloc_end) {
  1561. ret = 0;
  1562. break;
  1563. }
  1564. }
  1565. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  1566. &cached_state, GFP_NOFS);
  1567. out:
  1568. mutex_unlock(&inode->i_mutex);
  1569. /* Let go of our reservation. */
  1570. btrfs_free_reserved_data_space(inode, len);
  1571. return ret;
  1572. }
  1573. static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
  1574. {
  1575. struct btrfs_root *root = BTRFS_I(inode)->root;
  1576. struct extent_map *em;
  1577. struct extent_state *cached_state = NULL;
  1578. u64 lockstart = *offset;
  1579. u64 lockend = i_size_read(inode);
  1580. u64 start = *offset;
  1581. u64 orig_start = *offset;
  1582. u64 len = i_size_read(inode);
  1583. u64 last_end = 0;
  1584. int ret = 0;
  1585. lockend = max_t(u64, root->sectorsize, lockend);
  1586. if (lockend <= lockstart)
  1587. lockend = lockstart + root->sectorsize;
  1588. len = lockend - lockstart + 1;
  1589. len = max_t(u64, len, root->sectorsize);
  1590. if (inode->i_size == 0)
  1591. return -ENXIO;
  1592. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
  1593. &cached_state);
  1594. /*
  1595. * Delalloc is such a pain. If we have a hole and we have pending
  1596. * delalloc for a portion of the hole we will get back a hole that
  1597. * exists for the entire range since it hasn't been actually written
  1598. * yet. So to take care of this case we need to look for an extent just
  1599. * before the position we want in case there is outstanding delalloc
  1600. * going on here.
  1601. */
  1602. if (origin == SEEK_HOLE && start != 0) {
  1603. if (start <= root->sectorsize)
  1604. em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
  1605. root->sectorsize, 0);
  1606. else
  1607. em = btrfs_get_extent_fiemap(inode, NULL, 0,
  1608. start - root->sectorsize,
  1609. root->sectorsize, 0);
  1610. if (IS_ERR(em)) {
  1611. ret = PTR_ERR(em);
  1612. goto out;
  1613. }
  1614. last_end = em->start + em->len;
  1615. if (em->block_start == EXTENT_MAP_DELALLOC)
  1616. last_end = min_t(u64, last_end, inode->i_size);
  1617. free_extent_map(em);
  1618. }
  1619. while (1) {
  1620. em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
  1621. if (IS_ERR(em)) {
  1622. ret = PTR_ERR(em);
  1623. break;
  1624. }
  1625. if (em->block_start == EXTENT_MAP_HOLE) {
  1626. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1627. if (last_end <= orig_start) {
  1628. free_extent_map(em);
  1629. ret = -ENXIO;
  1630. break;
  1631. }
  1632. }
  1633. if (origin == SEEK_HOLE) {
  1634. *offset = start;
  1635. free_extent_map(em);
  1636. break;
  1637. }
  1638. } else {
  1639. if (origin == SEEK_DATA) {
  1640. if (em->block_start == EXTENT_MAP_DELALLOC) {
  1641. if (start >= inode->i_size) {
  1642. free_extent_map(em);
  1643. ret = -ENXIO;
  1644. break;
  1645. }
  1646. }
  1647. *offset = start;
  1648. free_extent_map(em);
  1649. break;
  1650. }
  1651. }
  1652. start = em->start + em->len;
  1653. last_end = em->start + em->len;
  1654. if (em->block_start == EXTENT_MAP_DELALLOC)
  1655. last_end = min_t(u64, last_end, inode->i_size);
  1656. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1657. free_extent_map(em);
  1658. ret = -ENXIO;
  1659. break;
  1660. }
  1661. free_extent_map(em);
  1662. cond_resched();
  1663. }
  1664. if (!ret)
  1665. *offset = min(*offset, inode->i_size);
  1666. out:
  1667. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1668. &cached_state, GFP_NOFS);
  1669. return ret;
  1670. }
  1671. static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
  1672. {
  1673. struct inode *inode = file->f_mapping->host;
  1674. int ret;
  1675. mutex_lock(&inode->i_mutex);
  1676. switch (origin) {
  1677. case SEEK_END:
  1678. case SEEK_CUR:
  1679. offset = generic_file_llseek(file, offset, origin);
  1680. goto out;
  1681. case SEEK_DATA:
  1682. case SEEK_HOLE:
  1683. if (offset >= i_size_read(inode)) {
  1684. mutex_unlock(&inode->i_mutex);
  1685. return -ENXIO;
  1686. }
  1687. ret = find_desired_extent(inode, &offset, origin);
  1688. if (ret) {
  1689. mutex_unlock(&inode->i_mutex);
  1690. return ret;
  1691. }
  1692. }
  1693. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
  1694. offset = -EINVAL;
  1695. goto out;
  1696. }
  1697. if (offset > inode->i_sb->s_maxbytes) {
  1698. offset = -EINVAL;
  1699. goto out;
  1700. }
  1701. /* Special lock needed here? */
  1702. if (offset != file->f_pos) {
  1703. file->f_pos = offset;
  1704. file->f_version = 0;
  1705. }
  1706. out:
  1707. mutex_unlock(&inode->i_mutex);
  1708. return offset;
  1709. }
  1710. const struct file_operations btrfs_file_operations = {
  1711. .llseek = btrfs_file_llseek,
  1712. .read = do_sync_read,
  1713. .write = do_sync_write,
  1714. .aio_read = generic_file_aio_read,
  1715. .splice_read = generic_file_splice_read,
  1716. .aio_write = btrfs_file_aio_write,
  1717. .mmap = btrfs_file_mmap,
  1718. .open = generic_file_open,
  1719. .release = btrfs_release_file,
  1720. .fsync = btrfs_sync_file,
  1721. .fallocate = btrfs_fallocate,
  1722. .unlocked_ioctl = btrfs_ioctl,
  1723. #ifdef CONFIG_COMPAT
  1724. .compat_ioctl = btrfs_ioctl,
  1725. #endif
  1726. };