file.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  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_num_bytes(leaf, fi,
  834. extent_end - end);
  835. btrfs_set_file_extent_offset(leaf, fi,
  836. end - orig_offset);
  837. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  838. struct btrfs_file_extent_item);
  839. btrfs_set_file_extent_num_bytes(leaf, fi,
  840. end - other_start);
  841. btrfs_mark_buffer_dirty(leaf);
  842. goto out;
  843. }
  844. }
  845. if (start > key.offset && end == extent_end) {
  846. other_start = end;
  847. other_end = 0;
  848. if (extent_mergeable(leaf, path->slots[0] + 1,
  849. ino, bytenr, orig_offset,
  850. &other_start, &other_end)) {
  851. fi = btrfs_item_ptr(leaf, path->slots[0],
  852. struct btrfs_file_extent_item);
  853. btrfs_set_file_extent_num_bytes(leaf, fi,
  854. start - key.offset);
  855. path->slots[0]++;
  856. new_key.offset = start;
  857. btrfs_set_item_key_safe(trans, root, path, &new_key);
  858. fi = btrfs_item_ptr(leaf, path->slots[0],
  859. struct btrfs_file_extent_item);
  860. btrfs_set_file_extent_num_bytes(leaf, fi,
  861. other_end - start);
  862. btrfs_set_file_extent_offset(leaf, fi,
  863. start - orig_offset);
  864. btrfs_mark_buffer_dirty(leaf);
  865. goto out;
  866. }
  867. }
  868. while (start > key.offset || end < extent_end) {
  869. if (key.offset == start)
  870. split = end;
  871. new_key.offset = split;
  872. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  873. if (ret == -EAGAIN) {
  874. btrfs_release_path(path);
  875. goto again;
  876. }
  877. if (ret < 0) {
  878. btrfs_abort_transaction(trans, root, ret);
  879. goto out;
  880. }
  881. leaf = path->nodes[0];
  882. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  883. struct btrfs_file_extent_item);
  884. btrfs_set_file_extent_num_bytes(leaf, fi,
  885. split - key.offset);
  886. fi = btrfs_item_ptr(leaf, path->slots[0],
  887. struct btrfs_file_extent_item);
  888. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  889. btrfs_set_file_extent_num_bytes(leaf, fi,
  890. extent_end - split);
  891. btrfs_mark_buffer_dirty(leaf);
  892. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  893. root->root_key.objectid,
  894. ino, orig_offset, 0);
  895. BUG_ON(ret); /* -ENOMEM */
  896. if (split == start) {
  897. key.offset = start;
  898. } else {
  899. BUG_ON(start != key.offset);
  900. path->slots[0]--;
  901. extent_end = end;
  902. }
  903. recow = 1;
  904. }
  905. other_start = end;
  906. other_end = 0;
  907. if (extent_mergeable(leaf, path->slots[0] + 1,
  908. ino, bytenr, orig_offset,
  909. &other_start, &other_end)) {
  910. if (recow) {
  911. btrfs_release_path(path);
  912. goto again;
  913. }
  914. extent_end = other_end;
  915. del_slot = path->slots[0] + 1;
  916. del_nr++;
  917. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  918. 0, root->root_key.objectid,
  919. ino, orig_offset, 0);
  920. BUG_ON(ret); /* -ENOMEM */
  921. }
  922. other_start = 0;
  923. other_end = start;
  924. if (extent_mergeable(leaf, path->slots[0] - 1,
  925. ino, bytenr, orig_offset,
  926. &other_start, &other_end)) {
  927. if (recow) {
  928. btrfs_release_path(path);
  929. goto again;
  930. }
  931. key.offset = other_start;
  932. del_slot = path->slots[0];
  933. del_nr++;
  934. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  935. 0, root->root_key.objectid,
  936. ino, orig_offset, 0);
  937. BUG_ON(ret); /* -ENOMEM */
  938. }
  939. if (del_nr == 0) {
  940. fi = btrfs_item_ptr(leaf, path->slots[0],
  941. struct btrfs_file_extent_item);
  942. btrfs_set_file_extent_type(leaf, fi,
  943. BTRFS_FILE_EXTENT_REG);
  944. btrfs_mark_buffer_dirty(leaf);
  945. } else {
  946. fi = btrfs_item_ptr(leaf, del_slot - 1,
  947. struct btrfs_file_extent_item);
  948. btrfs_set_file_extent_type(leaf, fi,
  949. BTRFS_FILE_EXTENT_REG);
  950. btrfs_set_file_extent_num_bytes(leaf, fi,
  951. extent_end - key.offset);
  952. btrfs_mark_buffer_dirty(leaf);
  953. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  954. if (ret < 0) {
  955. btrfs_abort_transaction(trans, root, ret);
  956. goto out;
  957. }
  958. }
  959. out:
  960. btrfs_free_path(path);
  961. return 0;
  962. }
  963. /*
  964. * on error we return an unlocked page and the error value
  965. * on success we return a locked page and 0
  966. */
  967. static int prepare_uptodate_page(struct page *page, u64 pos,
  968. bool force_uptodate)
  969. {
  970. int ret = 0;
  971. if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
  972. !PageUptodate(page)) {
  973. ret = btrfs_readpage(NULL, page);
  974. if (ret)
  975. return ret;
  976. lock_page(page);
  977. if (!PageUptodate(page)) {
  978. unlock_page(page);
  979. return -EIO;
  980. }
  981. }
  982. return 0;
  983. }
  984. /*
  985. * this gets pages into the page cache and locks them down, it also properly
  986. * waits for data=ordered extents to finish before allowing the pages to be
  987. * modified.
  988. */
  989. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  990. struct page **pages, size_t num_pages,
  991. loff_t pos, unsigned long first_index,
  992. size_t write_bytes, bool force_uptodate)
  993. {
  994. struct extent_state *cached_state = NULL;
  995. int i;
  996. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  997. struct inode *inode = fdentry(file)->d_inode;
  998. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  999. int err = 0;
  1000. int faili = 0;
  1001. u64 start_pos;
  1002. u64 last_pos;
  1003. start_pos = pos & ~((u64)root->sectorsize - 1);
  1004. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  1005. again:
  1006. for (i = 0; i < num_pages; i++) {
  1007. pages[i] = find_or_create_page(inode->i_mapping, index + i,
  1008. mask | __GFP_WRITE);
  1009. if (!pages[i]) {
  1010. faili = i - 1;
  1011. err = -ENOMEM;
  1012. goto fail;
  1013. }
  1014. if (i == 0)
  1015. err = prepare_uptodate_page(pages[i], pos,
  1016. force_uptodate);
  1017. if (i == num_pages - 1)
  1018. err = prepare_uptodate_page(pages[i],
  1019. pos + write_bytes, false);
  1020. if (err) {
  1021. page_cache_release(pages[i]);
  1022. faili = i - 1;
  1023. goto fail;
  1024. }
  1025. wait_on_page_writeback(pages[i]);
  1026. }
  1027. err = 0;
  1028. if (start_pos < inode->i_size) {
  1029. struct btrfs_ordered_extent *ordered;
  1030. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1031. start_pos, last_pos - 1, 0, &cached_state);
  1032. ordered = btrfs_lookup_first_ordered_extent(inode,
  1033. last_pos - 1);
  1034. if (ordered &&
  1035. ordered->file_offset + ordered->len > start_pos &&
  1036. ordered->file_offset < last_pos) {
  1037. btrfs_put_ordered_extent(ordered);
  1038. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1039. start_pos, last_pos - 1,
  1040. &cached_state, GFP_NOFS);
  1041. for (i = 0; i < num_pages; i++) {
  1042. unlock_page(pages[i]);
  1043. page_cache_release(pages[i]);
  1044. }
  1045. btrfs_wait_ordered_range(inode, start_pos,
  1046. last_pos - start_pos);
  1047. goto again;
  1048. }
  1049. if (ordered)
  1050. btrfs_put_ordered_extent(ordered);
  1051. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  1052. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1053. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  1054. GFP_NOFS);
  1055. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1056. start_pos, last_pos - 1, &cached_state,
  1057. GFP_NOFS);
  1058. }
  1059. for (i = 0; i < num_pages; i++) {
  1060. if (clear_page_dirty_for_io(pages[i]))
  1061. account_page_redirty(pages[i]);
  1062. set_page_extent_mapped(pages[i]);
  1063. WARN_ON(!PageLocked(pages[i]));
  1064. }
  1065. return 0;
  1066. fail:
  1067. while (faili >= 0) {
  1068. unlock_page(pages[faili]);
  1069. page_cache_release(pages[faili]);
  1070. faili--;
  1071. }
  1072. return err;
  1073. }
  1074. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  1075. struct iov_iter *i,
  1076. loff_t pos)
  1077. {
  1078. struct inode *inode = fdentry(file)->d_inode;
  1079. struct btrfs_root *root = BTRFS_I(inode)->root;
  1080. struct page **pages = NULL;
  1081. unsigned long first_index;
  1082. size_t num_written = 0;
  1083. int nrptrs;
  1084. int ret = 0;
  1085. bool force_page_uptodate = false;
  1086. nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
  1087. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  1088. (sizeof(struct page *)));
  1089. nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
  1090. nrptrs = max(nrptrs, 8);
  1091. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  1092. if (!pages)
  1093. return -ENOMEM;
  1094. first_index = pos >> PAGE_CACHE_SHIFT;
  1095. while (iov_iter_count(i) > 0) {
  1096. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1097. size_t write_bytes = min(iov_iter_count(i),
  1098. nrptrs * (size_t)PAGE_CACHE_SIZE -
  1099. offset);
  1100. size_t num_pages = (write_bytes + offset +
  1101. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1102. size_t dirty_pages;
  1103. size_t copied;
  1104. WARN_ON(num_pages > nrptrs);
  1105. /*
  1106. * Fault pages before locking them in prepare_pages
  1107. * to avoid recursive lock
  1108. */
  1109. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  1110. ret = -EFAULT;
  1111. break;
  1112. }
  1113. ret = btrfs_delalloc_reserve_space(inode,
  1114. num_pages << PAGE_CACHE_SHIFT);
  1115. if (ret)
  1116. break;
  1117. /*
  1118. * This is going to setup the pages array with the number of
  1119. * pages we want, so we don't really need to worry about the
  1120. * contents of pages from loop to loop
  1121. */
  1122. ret = prepare_pages(root, file, pages, num_pages,
  1123. pos, first_index, write_bytes,
  1124. force_page_uptodate);
  1125. if (ret) {
  1126. btrfs_delalloc_release_space(inode,
  1127. num_pages << PAGE_CACHE_SHIFT);
  1128. break;
  1129. }
  1130. copied = btrfs_copy_from_user(pos, num_pages,
  1131. write_bytes, pages, i);
  1132. /*
  1133. * if we have trouble faulting in the pages, fall
  1134. * back to one page at a time
  1135. */
  1136. if (copied < write_bytes)
  1137. nrptrs = 1;
  1138. if (copied == 0) {
  1139. force_page_uptodate = true;
  1140. dirty_pages = 0;
  1141. } else {
  1142. force_page_uptodate = false;
  1143. dirty_pages = (copied + offset +
  1144. PAGE_CACHE_SIZE - 1) >>
  1145. PAGE_CACHE_SHIFT;
  1146. }
  1147. /*
  1148. * If we had a short copy we need to release the excess delaloc
  1149. * bytes we reserved. We need to increment outstanding_extents
  1150. * because btrfs_delalloc_release_space will decrement it, but
  1151. * we still have an outstanding extent for the chunk we actually
  1152. * managed to copy.
  1153. */
  1154. if (num_pages > dirty_pages) {
  1155. if (copied > 0) {
  1156. spin_lock(&BTRFS_I(inode)->lock);
  1157. BTRFS_I(inode)->outstanding_extents++;
  1158. spin_unlock(&BTRFS_I(inode)->lock);
  1159. }
  1160. btrfs_delalloc_release_space(inode,
  1161. (num_pages - dirty_pages) <<
  1162. PAGE_CACHE_SHIFT);
  1163. }
  1164. if (copied > 0) {
  1165. ret = btrfs_dirty_pages(root, inode, pages,
  1166. dirty_pages, pos, copied,
  1167. NULL);
  1168. if (ret) {
  1169. btrfs_delalloc_release_space(inode,
  1170. dirty_pages << PAGE_CACHE_SHIFT);
  1171. btrfs_drop_pages(pages, num_pages);
  1172. break;
  1173. }
  1174. }
  1175. btrfs_drop_pages(pages, num_pages);
  1176. cond_resched();
  1177. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  1178. dirty_pages);
  1179. if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1180. btrfs_btree_balance_dirty(root, 1);
  1181. pos += copied;
  1182. num_written += copied;
  1183. }
  1184. kfree(pages);
  1185. return num_written ? num_written : ret;
  1186. }
  1187. static ssize_t __btrfs_direct_write(struct kiocb *iocb,
  1188. const struct iovec *iov,
  1189. unsigned long nr_segs, loff_t pos,
  1190. loff_t *ppos, size_t count, size_t ocount)
  1191. {
  1192. struct file *file = iocb->ki_filp;
  1193. struct iov_iter i;
  1194. ssize_t written;
  1195. ssize_t written_buffered;
  1196. loff_t endbyte;
  1197. int err;
  1198. written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
  1199. count, ocount);
  1200. if (written < 0 || written == count)
  1201. return written;
  1202. pos += written;
  1203. count -= written;
  1204. iov_iter_init(&i, iov, nr_segs, count, written);
  1205. written_buffered = __btrfs_buffered_write(file, &i, pos);
  1206. if (written_buffered < 0) {
  1207. err = written_buffered;
  1208. goto out;
  1209. }
  1210. endbyte = pos + written_buffered - 1;
  1211. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  1212. if (err)
  1213. goto out;
  1214. written += written_buffered;
  1215. *ppos = pos + written_buffered;
  1216. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
  1217. endbyte >> PAGE_CACHE_SHIFT);
  1218. out:
  1219. return written ? written : err;
  1220. }
  1221. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  1222. const struct iovec *iov,
  1223. unsigned long nr_segs, loff_t pos)
  1224. {
  1225. struct file *file = iocb->ki_filp;
  1226. struct inode *inode = fdentry(file)->d_inode;
  1227. struct btrfs_root *root = BTRFS_I(inode)->root;
  1228. loff_t *ppos = &iocb->ki_pos;
  1229. u64 start_pos;
  1230. ssize_t num_written = 0;
  1231. ssize_t err = 0;
  1232. size_t count, ocount;
  1233. sb_start_write(inode->i_sb);
  1234. mutex_lock(&inode->i_mutex);
  1235. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1236. if (err) {
  1237. mutex_unlock(&inode->i_mutex);
  1238. goto out;
  1239. }
  1240. count = ocount;
  1241. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1242. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1243. if (err) {
  1244. mutex_unlock(&inode->i_mutex);
  1245. goto out;
  1246. }
  1247. if (count == 0) {
  1248. mutex_unlock(&inode->i_mutex);
  1249. goto out;
  1250. }
  1251. err = file_remove_suid(file);
  1252. if (err) {
  1253. mutex_unlock(&inode->i_mutex);
  1254. goto out;
  1255. }
  1256. /*
  1257. * If BTRFS flips readonly due to some impossible error
  1258. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1259. * although we have opened a file as writable, we have
  1260. * to stop this write operation to ensure FS consistency.
  1261. */
  1262. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  1263. mutex_unlock(&inode->i_mutex);
  1264. err = -EROFS;
  1265. goto out;
  1266. }
  1267. err = file_update_time(file);
  1268. if (err) {
  1269. mutex_unlock(&inode->i_mutex);
  1270. goto out;
  1271. }
  1272. start_pos = round_down(pos, root->sectorsize);
  1273. if (start_pos > i_size_read(inode)) {
  1274. err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
  1275. if (err) {
  1276. mutex_unlock(&inode->i_mutex);
  1277. goto out;
  1278. }
  1279. }
  1280. if (unlikely(file->f_flags & O_DIRECT)) {
  1281. num_written = __btrfs_direct_write(iocb, iov, nr_segs,
  1282. pos, ppos, count, ocount);
  1283. } else {
  1284. struct iov_iter i;
  1285. iov_iter_init(&i, iov, nr_segs, count, num_written);
  1286. num_written = __btrfs_buffered_write(file, &i, pos);
  1287. if (num_written > 0)
  1288. *ppos = pos + num_written;
  1289. }
  1290. mutex_unlock(&inode->i_mutex);
  1291. /*
  1292. * we want to make sure fsync finds this change
  1293. * but we haven't joined a transaction running right now.
  1294. *
  1295. * Later on, someone is sure to update the inode and get the
  1296. * real transid recorded.
  1297. *
  1298. * We set last_trans now to the fs_info generation + 1,
  1299. * this will either be one more than the running transaction
  1300. * or the generation used for the next transaction if there isn't
  1301. * one running right now.
  1302. */
  1303. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  1304. if (num_written > 0 || num_written == -EIOCBQUEUED) {
  1305. err = generic_write_sync(file, pos, num_written);
  1306. if (err < 0 && num_written > 0)
  1307. num_written = err;
  1308. }
  1309. out:
  1310. sb_end_write(inode->i_sb);
  1311. current->backing_dev_info = NULL;
  1312. return num_written ? num_written : err;
  1313. }
  1314. int btrfs_release_file(struct inode *inode, struct file *filp)
  1315. {
  1316. /*
  1317. * ordered_data_close is set by settattr when we are about to truncate
  1318. * a file from a non-zero size to a zero size. This tries to
  1319. * flush down new bytes that may have been written if the
  1320. * application were using truncate to replace a file in place.
  1321. */
  1322. if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  1323. &BTRFS_I(inode)->runtime_flags)) {
  1324. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1325. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1326. filemap_flush(inode->i_mapping);
  1327. }
  1328. if (filp->private_data)
  1329. btrfs_ioctl_trans_end(filp);
  1330. return 0;
  1331. }
  1332. /*
  1333. * fsync call for both files and directories. This logs the inode into
  1334. * the tree log instead of forcing full commits whenever possible.
  1335. *
  1336. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1337. * in the metadata btree are up to date for copying to the log.
  1338. *
  1339. * It drops the inode mutex before doing the tree log commit. This is an
  1340. * important optimization for directories because holding the mutex prevents
  1341. * new operations on the dir while we write to disk.
  1342. */
  1343. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  1344. {
  1345. struct dentry *dentry = file->f_path.dentry;
  1346. struct inode *inode = dentry->d_inode;
  1347. struct btrfs_root *root = BTRFS_I(inode)->root;
  1348. int ret = 0;
  1349. struct btrfs_trans_handle *trans;
  1350. trace_btrfs_sync_file(file, datasync);
  1351. mutex_lock(&inode->i_mutex);
  1352. /*
  1353. * we wait first, since the writeback may change the inode, also wait
  1354. * ordered range does a filemape_write_and_wait_range which is why we
  1355. * don't do it above like other file systems.
  1356. */
  1357. root->log_batch++;
  1358. btrfs_wait_ordered_range(inode, start, end);
  1359. root->log_batch++;
  1360. /*
  1361. * check the transaction that last modified this inode
  1362. * and see if its already been committed
  1363. */
  1364. if (!BTRFS_I(inode)->last_trans) {
  1365. mutex_unlock(&inode->i_mutex);
  1366. goto out;
  1367. }
  1368. /*
  1369. * if the last transaction that changed this file was before
  1370. * the current transaction, we can bail out now without any
  1371. * syncing
  1372. */
  1373. smp_mb();
  1374. if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
  1375. BTRFS_I(inode)->last_trans <=
  1376. root->fs_info->last_trans_committed) {
  1377. BTRFS_I(inode)->last_trans = 0;
  1378. mutex_unlock(&inode->i_mutex);
  1379. goto out;
  1380. }
  1381. /*
  1382. * ok we haven't committed the transaction yet, lets do a commit
  1383. */
  1384. if (file->private_data)
  1385. btrfs_ioctl_trans_end(file);
  1386. trans = btrfs_start_transaction(root, 0);
  1387. if (IS_ERR(trans)) {
  1388. ret = PTR_ERR(trans);
  1389. mutex_unlock(&inode->i_mutex);
  1390. goto out;
  1391. }
  1392. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1393. if (ret < 0) {
  1394. mutex_unlock(&inode->i_mutex);
  1395. goto out;
  1396. }
  1397. /* we've logged all the items and now have a consistent
  1398. * version of the file in the log. It is possible that
  1399. * someone will come in and modify the file, but that's
  1400. * fine because the log is consistent on disk, and we
  1401. * have references to all of the file's extents
  1402. *
  1403. * It is possible that someone will come in and log the
  1404. * file again, but that will end up using the synchronization
  1405. * inside btrfs_sync_log to keep things safe.
  1406. */
  1407. mutex_unlock(&inode->i_mutex);
  1408. if (ret != BTRFS_NO_LOG_SYNC) {
  1409. if (ret > 0) {
  1410. ret = btrfs_commit_transaction(trans, root);
  1411. } else {
  1412. ret = btrfs_sync_log(trans, root);
  1413. if (ret == 0)
  1414. ret = btrfs_end_transaction(trans, root);
  1415. else
  1416. ret = btrfs_commit_transaction(trans, root);
  1417. }
  1418. } else {
  1419. ret = btrfs_end_transaction(trans, root);
  1420. }
  1421. out:
  1422. return ret > 0 ? -EIO : ret;
  1423. }
  1424. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1425. .fault = filemap_fault,
  1426. .page_mkwrite = btrfs_page_mkwrite,
  1427. };
  1428. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1429. {
  1430. struct address_space *mapping = filp->f_mapping;
  1431. if (!mapping->a_ops->readpage)
  1432. return -ENOEXEC;
  1433. file_accessed(filp);
  1434. vma->vm_ops = &btrfs_file_vm_ops;
  1435. vma->vm_flags |= VM_CAN_NONLINEAR;
  1436. return 0;
  1437. }
  1438. static long btrfs_fallocate(struct file *file, int mode,
  1439. loff_t offset, loff_t len)
  1440. {
  1441. struct inode *inode = file->f_path.dentry->d_inode;
  1442. struct extent_state *cached_state = NULL;
  1443. u64 cur_offset;
  1444. u64 last_byte;
  1445. u64 alloc_start;
  1446. u64 alloc_end;
  1447. u64 alloc_hint = 0;
  1448. u64 locked_end;
  1449. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1450. struct extent_map *em;
  1451. int ret;
  1452. alloc_start = offset & ~mask;
  1453. alloc_end = (offset + len + mask) & ~mask;
  1454. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  1455. if (mode & ~FALLOC_FL_KEEP_SIZE)
  1456. return -EOPNOTSUPP;
  1457. /*
  1458. * Make sure we have enough space before we do the
  1459. * allocation.
  1460. */
  1461. ret = btrfs_check_data_free_space(inode, len);
  1462. if (ret)
  1463. return ret;
  1464. /*
  1465. * wait for ordered IO before we have any locks. We'll loop again
  1466. * below with the locks held.
  1467. */
  1468. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1469. mutex_lock(&inode->i_mutex);
  1470. ret = inode_newsize_ok(inode, alloc_end);
  1471. if (ret)
  1472. goto out;
  1473. if (alloc_start > inode->i_size) {
  1474. ret = btrfs_cont_expand(inode, i_size_read(inode),
  1475. alloc_start);
  1476. if (ret)
  1477. goto out;
  1478. }
  1479. locked_end = alloc_end - 1;
  1480. while (1) {
  1481. struct btrfs_ordered_extent *ordered;
  1482. /* the extent lock is ordered inside the running
  1483. * transaction
  1484. */
  1485. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1486. locked_end, 0, &cached_state);
  1487. ordered = btrfs_lookup_first_ordered_extent(inode,
  1488. alloc_end - 1);
  1489. if (ordered &&
  1490. ordered->file_offset + ordered->len > alloc_start &&
  1491. ordered->file_offset < alloc_end) {
  1492. btrfs_put_ordered_extent(ordered);
  1493. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1494. alloc_start, locked_end,
  1495. &cached_state, GFP_NOFS);
  1496. /*
  1497. * we can't wait on the range with the transaction
  1498. * running or with the extent lock held
  1499. */
  1500. btrfs_wait_ordered_range(inode, alloc_start,
  1501. alloc_end - alloc_start);
  1502. } else {
  1503. if (ordered)
  1504. btrfs_put_ordered_extent(ordered);
  1505. break;
  1506. }
  1507. }
  1508. cur_offset = alloc_start;
  1509. while (1) {
  1510. u64 actual_end;
  1511. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1512. alloc_end - cur_offset, 0);
  1513. if (IS_ERR_OR_NULL(em)) {
  1514. if (!em)
  1515. ret = -ENOMEM;
  1516. else
  1517. ret = PTR_ERR(em);
  1518. break;
  1519. }
  1520. last_byte = min(extent_map_end(em), alloc_end);
  1521. actual_end = min_t(u64, extent_map_end(em), offset + len);
  1522. last_byte = (last_byte + mask) & ~mask;
  1523. if (em->block_start == EXTENT_MAP_HOLE ||
  1524. (cur_offset >= inode->i_size &&
  1525. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1526. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1527. last_byte - cur_offset,
  1528. 1 << inode->i_blkbits,
  1529. offset + len,
  1530. &alloc_hint);
  1531. if (ret < 0) {
  1532. free_extent_map(em);
  1533. break;
  1534. }
  1535. } else if (actual_end > inode->i_size &&
  1536. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1537. /*
  1538. * We didn't need to allocate any more space, but we
  1539. * still extended the size of the file so we need to
  1540. * update i_size.
  1541. */
  1542. inode->i_ctime = CURRENT_TIME;
  1543. i_size_write(inode, actual_end);
  1544. btrfs_ordered_update_i_size(inode, actual_end, NULL);
  1545. }
  1546. free_extent_map(em);
  1547. cur_offset = last_byte;
  1548. if (cur_offset >= alloc_end) {
  1549. ret = 0;
  1550. break;
  1551. }
  1552. }
  1553. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  1554. &cached_state, GFP_NOFS);
  1555. out:
  1556. mutex_unlock(&inode->i_mutex);
  1557. /* Let go of our reservation. */
  1558. btrfs_free_reserved_data_space(inode, len);
  1559. return ret;
  1560. }
  1561. static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
  1562. {
  1563. struct btrfs_root *root = BTRFS_I(inode)->root;
  1564. struct extent_map *em;
  1565. struct extent_state *cached_state = NULL;
  1566. u64 lockstart = *offset;
  1567. u64 lockend = i_size_read(inode);
  1568. u64 start = *offset;
  1569. u64 orig_start = *offset;
  1570. u64 len = i_size_read(inode);
  1571. u64 last_end = 0;
  1572. int ret = 0;
  1573. lockend = max_t(u64, root->sectorsize, lockend);
  1574. if (lockend <= lockstart)
  1575. lockend = lockstart + root->sectorsize;
  1576. len = lockend - lockstart + 1;
  1577. len = max_t(u64, len, root->sectorsize);
  1578. if (inode->i_size == 0)
  1579. return -ENXIO;
  1580. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
  1581. &cached_state);
  1582. /*
  1583. * Delalloc is such a pain. If we have a hole and we have pending
  1584. * delalloc for a portion of the hole we will get back a hole that
  1585. * exists for the entire range since it hasn't been actually written
  1586. * yet. So to take care of this case we need to look for an extent just
  1587. * before the position we want in case there is outstanding delalloc
  1588. * going on here.
  1589. */
  1590. if (origin == SEEK_HOLE && start != 0) {
  1591. if (start <= root->sectorsize)
  1592. em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
  1593. root->sectorsize, 0);
  1594. else
  1595. em = btrfs_get_extent_fiemap(inode, NULL, 0,
  1596. start - root->sectorsize,
  1597. root->sectorsize, 0);
  1598. if (IS_ERR(em)) {
  1599. ret = PTR_ERR(em);
  1600. goto out;
  1601. }
  1602. last_end = em->start + em->len;
  1603. if (em->block_start == EXTENT_MAP_DELALLOC)
  1604. last_end = min_t(u64, last_end, inode->i_size);
  1605. free_extent_map(em);
  1606. }
  1607. while (1) {
  1608. em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
  1609. if (IS_ERR(em)) {
  1610. ret = PTR_ERR(em);
  1611. break;
  1612. }
  1613. if (em->block_start == EXTENT_MAP_HOLE) {
  1614. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1615. if (last_end <= orig_start) {
  1616. free_extent_map(em);
  1617. ret = -ENXIO;
  1618. break;
  1619. }
  1620. }
  1621. if (origin == SEEK_HOLE) {
  1622. *offset = start;
  1623. free_extent_map(em);
  1624. break;
  1625. }
  1626. } else {
  1627. if (origin == SEEK_DATA) {
  1628. if (em->block_start == EXTENT_MAP_DELALLOC) {
  1629. if (start >= inode->i_size) {
  1630. free_extent_map(em);
  1631. ret = -ENXIO;
  1632. break;
  1633. }
  1634. }
  1635. *offset = start;
  1636. free_extent_map(em);
  1637. break;
  1638. }
  1639. }
  1640. start = em->start + em->len;
  1641. last_end = em->start + em->len;
  1642. if (em->block_start == EXTENT_MAP_DELALLOC)
  1643. last_end = min_t(u64, last_end, inode->i_size);
  1644. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1645. free_extent_map(em);
  1646. ret = -ENXIO;
  1647. break;
  1648. }
  1649. free_extent_map(em);
  1650. cond_resched();
  1651. }
  1652. if (!ret)
  1653. *offset = min(*offset, inode->i_size);
  1654. out:
  1655. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1656. &cached_state, GFP_NOFS);
  1657. return ret;
  1658. }
  1659. static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
  1660. {
  1661. struct inode *inode = file->f_mapping->host;
  1662. int ret;
  1663. mutex_lock(&inode->i_mutex);
  1664. switch (origin) {
  1665. case SEEK_END:
  1666. case SEEK_CUR:
  1667. offset = generic_file_llseek(file, offset, origin);
  1668. goto out;
  1669. case SEEK_DATA:
  1670. case SEEK_HOLE:
  1671. if (offset >= i_size_read(inode)) {
  1672. mutex_unlock(&inode->i_mutex);
  1673. return -ENXIO;
  1674. }
  1675. ret = find_desired_extent(inode, &offset, origin);
  1676. if (ret) {
  1677. mutex_unlock(&inode->i_mutex);
  1678. return ret;
  1679. }
  1680. }
  1681. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
  1682. offset = -EINVAL;
  1683. goto out;
  1684. }
  1685. if (offset > inode->i_sb->s_maxbytes) {
  1686. offset = -EINVAL;
  1687. goto out;
  1688. }
  1689. /* Special lock needed here? */
  1690. if (offset != file->f_pos) {
  1691. file->f_pos = offset;
  1692. file->f_version = 0;
  1693. }
  1694. out:
  1695. mutex_unlock(&inode->i_mutex);
  1696. return offset;
  1697. }
  1698. const struct file_operations btrfs_file_operations = {
  1699. .llseek = btrfs_file_llseek,
  1700. .read = do_sync_read,
  1701. .write = do_sync_write,
  1702. .aio_read = generic_file_aio_read,
  1703. .splice_read = generic_file_splice_read,
  1704. .aio_write = btrfs_file_aio_write,
  1705. .mmap = btrfs_file_mmap,
  1706. .open = generic_file_open,
  1707. .release = btrfs_release_file,
  1708. .fsync = btrfs_sync_file,
  1709. .fallocate = btrfs_fallocate,
  1710. .unlocked_ioctl = btrfs_ioctl,
  1711. #ifdef CONFIG_COMPAT
  1712. .compat_ioctl = btrfs_ioctl,
  1713. #endif
  1714. };