file.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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. u64 gen;
  418. int ret;
  419. int testend = 1;
  420. unsigned long flags;
  421. int compressed = 0;
  422. WARN_ON(end < start);
  423. if (end == (u64)-1) {
  424. len = (u64)-1;
  425. testend = 0;
  426. }
  427. while (1) {
  428. if (!split)
  429. split = alloc_extent_map();
  430. if (!split2)
  431. split2 = alloc_extent_map();
  432. BUG_ON(!split || !split2); /* -ENOMEM */
  433. write_lock(&em_tree->lock);
  434. em = lookup_extent_mapping(em_tree, start, len);
  435. if (!em) {
  436. write_unlock(&em_tree->lock);
  437. break;
  438. }
  439. flags = em->flags;
  440. gen = em->generation;
  441. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  442. if (testend && em->start + em->len >= start + len) {
  443. free_extent_map(em);
  444. write_unlock(&em_tree->lock);
  445. break;
  446. }
  447. start = em->start + em->len;
  448. if (testend)
  449. len = start + len - (em->start + em->len);
  450. free_extent_map(em);
  451. write_unlock(&em_tree->lock);
  452. continue;
  453. }
  454. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  455. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  456. remove_extent_mapping(em_tree, em);
  457. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  458. em->start < start) {
  459. split->start = em->start;
  460. split->len = start - em->start;
  461. split->orig_start = em->orig_start;
  462. split->block_start = em->block_start;
  463. if (compressed)
  464. split->block_len = em->block_len;
  465. else
  466. split->block_len = split->len;
  467. split->generation = gen;
  468. split->bdev = em->bdev;
  469. split->flags = flags;
  470. split->compress_type = em->compress_type;
  471. ret = add_extent_mapping(em_tree, split);
  472. BUG_ON(ret); /* Logic error */
  473. list_move(&split->list, &em_tree->modified_extents);
  474. free_extent_map(split);
  475. split = split2;
  476. split2 = NULL;
  477. }
  478. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  479. testend && em->start + em->len > start + len) {
  480. u64 diff = start + len - em->start;
  481. split->start = start + len;
  482. split->len = em->start + em->len - (start + len);
  483. split->bdev = em->bdev;
  484. split->flags = flags;
  485. split->compress_type = em->compress_type;
  486. split->generation = gen;
  487. if (compressed) {
  488. split->block_len = em->block_len;
  489. split->block_start = em->block_start;
  490. split->orig_start = em->orig_start;
  491. } else {
  492. split->block_len = split->len;
  493. split->block_start = em->block_start + diff;
  494. split->orig_start = split->start;
  495. }
  496. ret = add_extent_mapping(em_tree, split);
  497. BUG_ON(ret); /* Logic error */
  498. list_move(&split->list, &em_tree->modified_extents);
  499. free_extent_map(split);
  500. split = NULL;
  501. }
  502. write_unlock(&em_tree->lock);
  503. /* once for us */
  504. free_extent_map(em);
  505. /* once for the tree*/
  506. free_extent_map(em);
  507. }
  508. if (split)
  509. free_extent_map(split);
  510. if (split2)
  511. free_extent_map(split2);
  512. return 0;
  513. }
  514. /*
  515. * this is very complex, but the basic idea is to drop all extents
  516. * in the range start - end. hint_block is filled in with a block number
  517. * that would be a good hint to the block allocator for this file.
  518. *
  519. * If an extent intersects the range but is not entirely inside the range
  520. * it is either truncated or split. Anything entirely inside the range
  521. * is deleted from the tree.
  522. */
  523. int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
  524. struct btrfs_root *root, struct inode *inode,
  525. struct btrfs_path *path, u64 start, u64 end,
  526. u64 *hint_byte, int drop_cache)
  527. {
  528. struct extent_buffer *leaf;
  529. struct btrfs_file_extent_item *fi;
  530. struct btrfs_key key;
  531. struct btrfs_key new_key;
  532. u64 ino = btrfs_ino(inode);
  533. u64 search_start = start;
  534. u64 disk_bytenr = 0;
  535. u64 num_bytes = 0;
  536. u64 extent_offset = 0;
  537. u64 extent_end = 0;
  538. int del_nr = 0;
  539. int del_slot = 0;
  540. int extent_type;
  541. int recow;
  542. int ret;
  543. int modify_tree = -1;
  544. int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
  545. if (drop_cache)
  546. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  547. if (start >= BTRFS_I(inode)->disk_i_size)
  548. modify_tree = 0;
  549. while (1) {
  550. recow = 0;
  551. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  552. search_start, modify_tree);
  553. if (ret < 0)
  554. break;
  555. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  556. leaf = path->nodes[0];
  557. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  558. if (key.objectid == ino &&
  559. key.type == BTRFS_EXTENT_DATA_KEY)
  560. path->slots[0]--;
  561. }
  562. ret = 0;
  563. next_slot:
  564. leaf = path->nodes[0];
  565. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  566. BUG_ON(del_nr > 0);
  567. ret = btrfs_next_leaf(root, path);
  568. if (ret < 0)
  569. break;
  570. if (ret > 0) {
  571. ret = 0;
  572. break;
  573. }
  574. leaf = path->nodes[0];
  575. recow = 1;
  576. }
  577. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  578. if (key.objectid > ino ||
  579. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  580. break;
  581. fi = btrfs_item_ptr(leaf, path->slots[0],
  582. struct btrfs_file_extent_item);
  583. extent_type = btrfs_file_extent_type(leaf, fi);
  584. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  585. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  586. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  587. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  588. extent_offset = btrfs_file_extent_offset(leaf, fi);
  589. extent_end = key.offset +
  590. btrfs_file_extent_num_bytes(leaf, fi);
  591. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  592. extent_end = key.offset +
  593. btrfs_file_extent_inline_len(leaf, fi);
  594. } else {
  595. WARN_ON(1);
  596. extent_end = search_start;
  597. }
  598. if (extent_end <= search_start) {
  599. path->slots[0]++;
  600. goto next_slot;
  601. }
  602. search_start = max(key.offset, start);
  603. if (recow || !modify_tree) {
  604. modify_tree = -1;
  605. btrfs_release_path(path);
  606. continue;
  607. }
  608. /*
  609. * | - range to drop - |
  610. * | -------- extent -------- |
  611. */
  612. if (start > key.offset && end < extent_end) {
  613. BUG_ON(del_nr > 0);
  614. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  615. memcpy(&new_key, &key, sizeof(new_key));
  616. new_key.offset = start;
  617. ret = btrfs_duplicate_item(trans, root, path,
  618. &new_key);
  619. if (ret == -EAGAIN) {
  620. btrfs_release_path(path);
  621. continue;
  622. }
  623. if (ret < 0)
  624. break;
  625. leaf = path->nodes[0];
  626. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  627. struct btrfs_file_extent_item);
  628. btrfs_set_file_extent_num_bytes(leaf, fi,
  629. start - key.offset);
  630. fi = btrfs_item_ptr(leaf, path->slots[0],
  631. struct btrfs_file_extent_item);
  632. extent_offset += start - key.offset;
  633. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  634. btrfs_set_file_extent_num_bytes(leaf, fi,
  635. extent_end - start);
  636. btrfs_mark_buffer_dirty(leaf);
  637. if (update_refs && disk_bytenr > 0) {
  638. ret = btrfs_inc_extent_ref(trans, root,
  639. disk_bytenr, num_bytes, 0,
  640. root->root_key.objectid,
  641. new_key.objectid,
  642. start - extent_offset, 0);
  643. BUG_ON(ret); /* -ENOMEM */
  644. *hint_byte = disk_bytenr;
  645. }
  646. key.offset = start;
  647. }
  648. /*
  649. * | ---- range to drop ----- |
  650. * | -------- extent -------- |
  651. */
  652. if (start <= key.offset && end < extent_end) {
  653. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  654. memcpy(&new_key, &key, sizeof(new_key));
  655. new_key.offset = end;
  656. btrfs_set_item_key_safe(trans, root, path, &new_key);
  657. extent_offset += end - key.offset;
  658. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  659. btrfs_set_file_extent_num_bytes(leaf, fi,
  660. extent_end - end);
  661. btrfs_mark_buffer_dirty(leaf);
  662. if (update_refs && disk_bytenr > 0) {
  663. inode_sub_bytes(inode, end - key.offset);
  664. *hint_byte = disk_bytenr;
  665. }
  666. break;
  667. }
  668. search_start = extent_end;
  669. /*
  670. * | ---- range to drop ----- |
  671. * | -------- extent -------- |
  672. */
  673. if (start > key.offset && end >= extent_end) {
  674. BUG_ON(del_nr > 0);
  675. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  676. btrfs_set_file_extent_num_bytes(leaf, fi,
  677. start - key.offset);
  678. btrfs_mark_buffer_dirty(leaf);
  679. if (update_refs && disk_bytenr > 0) {
  680. inode_sub_bytes(inode, extent_end - start);
  681. *hint_byte = disk_bytenr;
  682. }
  683. if (end == extent_end)
  684. break;
  685. path->slots[0]++;
  686. goto next_slot;
  687. }
  688. /*
  689. * | ---- range to drop ----- |
  690. * | ------ extent ------ |
  691. */
  692. if (start <= key.offset && end >= extent_end) {
  693. if (del_nr == 0) {
  694. del_slot = path->slots[0];
  695. del_nr = 1;
  696. } else {
  697. BUG_ON(del_slot + del_nr != path->slots[0]);
  698. del_nr++;
  699. }
  700. if (update_refs &&
  701. extent_type == BTRFS_FILE_EXTENT_INLINE) {
  702. inode_sub_bytes(inode,
  703. extent_end - key.offset);
  704. extent_end = ALIGN(extent_end,
  705. root->sectorsize);
  706. } else if (update_refs && disk_bytenr > 0) {
  707. ret = btrfs_free_extent(trans, root,
  708. disk_bytenr, num_bytes, 0,
  709. root->root_key.objectid,
  710. key.objectid, key.offset -
  711. extent_offset, 0);
  712. BUG_ON(ret); /* -ENOMEM */
  713. inode_sub_bytes(inode,
  714. extent_end - key.offset);
  715. *hint_byte = disk_bytenr;
  716. }
  717. if (end == extent_end)
  718. break;
  719. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  720. path->slots[0]++;
  721. goto next_slot;
  722. }
  723. ret = btrfs_del_items(trans, root, path, del_slot,
  724. del_nr);
  725. if (ret) {
  726. btrfs_abort_transaction(trans, root, ret);
  727. break;
  728. }
  729. del_nr = 0;
  730. del_slot = 0;
  731. btrfs_release_path(path);
  732. continue;
  733. }
  734. BUG_ON(1);
  735. }
  736. if (!ret && del_nr > 0) {
  737. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  738. if (ret)
  739. btrfs_abort_transaction(trans, root, ret);
  740. }
  741. btrfs_release_path(path);
  742. return ret;
  743. }
  744. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  745. struct btrfs_root *root, struct inode *inode, u64 start,
  746. u64 end, u64 *hint_byte, int drop_cache)
  747. {
  748. struct btrfs_path *path;
  749. int ret;
  750. path = btrfs_alloc_path();
  751. if (!path)
  752. return -ENOMEM;
  753. ret = __btrfs_drop_extents(trans, root, inode, path, start, end,
  754. hint_byte, drop_cache);
  755. btrfs_free_path(path);
  756. return ret;
  757. }
  758. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  759. u64 objectid, u64 bytenr, u64 orig_offset,
  760. u64 *start, u64 *end)
  761. {
  762. struct btrfs_file_extent_item *fi;
  763. struct btrfs_key key;
  764. u64 extent_end;
  765. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  766. return 0;
  767. btrfs_item_key_to_cpu(leaf, &key, slot);
  768. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  769. return 0;
  770. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  771. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  772. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  773. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  774. btrfs_file_extent_compression(leaf, fi) ||
  775. btrfs_file_extent_encryption(leaf, fi) ||
  776. btrfs_file_extent_other_encoding(leaf, fi))
  777. return 0;
  778. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  779. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  780. return 0;
  781. *start = key.offset;
  782. *end = extent_end;
  783. return 1;
  784. }
  785. /*
  786. * Mark extent in the range start - end as written.
  787. *
  788. * This changes extent type from 'pre-allocated' to 'regular'. If only
  789. * part of extent is marked as written, the extent will be split into
  790. * two or three.
  791. */
  792. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  793. struct inode *inode, u64 start, u64 end)
  794. {
  795. struct btrfs_root *root = BTRFS_I(inode)->root;
  796. struct extent_buffer *leaf;
  797. struct btrfs_path *path;
  798. struct btrfs_file_extent_item *fi;
  799. struct btrfs_key key;
  800. struct btrfs_key new_key;
  801. u64 bytenr;
  802. u64 num_bytes;
  803. u64 extent_end;
  804. u64 orig_offset;
  805. u64 other_start;
  806. u64 other_end;
  807. u64 split;
  808. int del_nr = 0;
  809. int del_slot = 0;
  810. int recow;
  811. int ret;
  812. u64 ino = btrfs_ino(inode);
  813. path = btrfs_alloc_path();
  814. if (!path)
  815. return -ENOMEM;
  816. again:
  817. recow = 0;
  818. split = start;
  819. key.objectid = ino;
  820. key.type = BTRFS_EXTENT_DATA_KEY;
  821. key.offset = split;
  822. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  823. if (ret < 0)
  824. goto out;
  825. if (ret > 0 && path->slots[0] > 0)
  826. path->slots[0]--;
  827. leaf = path->nodes[0];
  828. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  829. BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
  830. fi = btrfs_item_ptr(leaf, path->slots[0],
  831. struct btrfs_file_extent_item);
  832. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  833. BTRFS_FILE_EXTENT_PREALLOC);
  834. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  835. BUG_ON(key.offset > start || extent_end < end);
  836. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  837. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  838. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  839. memcpy(&new_key, &key, sizeof(new_key));
  840. if (start == key.offset && end < extent_end) {
  841. other_start = 0;
  842. other_end = start;
  843. if (extent_mergeable(leaf, path->slots[0] - 1,
  844. ino, bytenr, orig_offset,
  845. &other_start, &other_end)) {
  846. new_key.offset = end;
  847. btrfs_set_item_key_safe(trans, root, path, &new_key);
  848. fi = btrfs_item_ptr(leaf, path->slots[0],
  849. struct btrfs_file_extent_item);
  850. btrfs_set_file_extent_generation(leaf, fi,
  851. trans->transid);
  852. btrfs_set_file_extent_num_bytes(leaf, fi,
  853. extent_end - end);
  854. btrfs_set_file_extent_offset(leaf, fi,
  855. end - orig_offset);
  856. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  857. struct btrfs_file_extent_item);
  858. btrfs_set_file_extent_generation(leaf, fi,
  859. trans->transid);
  860. btrfs_set_file_extent_num_bytes(leaf, fi,
  861. end - other_start);
  862. btrfs_mark_buffer_dirty(leaf);
  863. goto out;
  864. }
  865. }
  866. if (start > key.offset && end == extent_end) {
  867. other_start = end;
  868. other_end = 0;
  869. if (extent_mergeable(leaf, path->slots[0] + 1,
  870. ino, bytenr, orig_offset,
  871. &other_start, &other_end)) {
  872. fi = btrfs_item_ptr(leaf, path->slots[0],
  873. struct btrfs_file_extent_item);
  874. btrfs_set_file_extent_num_bytes(leaf, fi,
  875. start - key.offset);
  876. btrfs_set_file_extent_generation(leaf, fi,
  877. trans->transid);
  878. path->slots[0]++;
  879. new_key.offset = start;
  880. btrfs_set_item_key_safe(trans, root, path, &new_key);
  881. fi = btrfs_item_ptr(leaf, path->slots[0],
  882. struct btrfs_file_extent_item);
  883. btrfs_set_file_extent_generation(leaf, fi,
  884. trans->transid);
  885. btrfs_set_file_extent_num_bytes(leaf, fi,
  886. other_end - start);
  887. btrfs_set_file_extent_offset(leaf, fi,
  888. start - orig_offset);
  889. btrfs_mark_buffer_dirty(leaf);
  890. goto out;
  891. }
  892. }
  893. while (start > key.offset || end < extent_end) {
  894. if (key.offset == start)
  895. split = end;
  896. new_key.offset = split;
  897. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  898. if (ret == -EAGAIN) {
  899. btrfs_release_path(path);
  900. goto again;
  901. }
  902. if (ret < 0) {
  903. btrfs_abort_transaction(trans, root, ret);
  904. goto out;
  905. }
  906. leaf = path->nodes[0];
  907. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  908. struct btrfs_file_extent_item);
  909. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  910. btrfs_set_file_extent_num_bytes(leaf, fi,
  911. split - key.offset);
  912. fi = btrfs_item_ptr(leaf, path->slots[0],
  913. struct btrfs_file_extent_item);
  914. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  915. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  916. btrfs_set_file_extent_num_bytes(leaf, fi,
  917. extent_end - split);
  918. btrfs_mark_buffer_dirty(leaf);
  919. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  920. root->root_key.objectid,
  921. ino, orig_offset, 0);
  922. BUG_ON(ret); /* -ENOMEM */
  923. if (split == start) {
  924. key.offset = start;
  925. } else {
  926. BUG_ON(start != key.offset);
  927. path->slots[0]--;
  928. extent_end = end;
  929. }
  930. recow = 1;
  931. }
  932. other_start = end;
  933. other_end = 0;
  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. extent_end = other_end;
  942. del_slot = path->slots[0] + 1;
  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. other_start = 0;
  950. other_end = start;
  951. if (extent_mergeable(leaf, path->slots[0] - 1,
  952. ino, bytenr, orig_offset,
  953. &other_start, &other_end)) {
  954. if (recow) {
  955. btrfs_release_path(path);
  956. goto again;
  957. }
  958. key.offset = other_start;
  959. del_slot = path->slots[0];
  960. del_nr++;
  961. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  962. 0, root->root_key.objectid,
  963. ino, orig_offset, 0);
  964. BUG_ON(ret); /* -ENOMEM */
  965. }
  966. if (del_nr == 0) {
  967. fi = btrfs_item_ptr(leaf, path->slots[0],
  968. struct btrfs_file_extent_item);
  969. btrfs_set_file_extent_type(leaf, fi,
  970. BTRFS_FILE_EXTENT_REG);
  971. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  972. btrfs_mark_buffer_dirty(leaf);
  973. } else {
  974. fi = btrfs_item_ptr(leaf, del_slot - 1,
  975. struct btrfs_file_extent_item);
  976. btrfs_set_file_extent_type(leaf, fi,
  977. BTRFS_FILE_EXTENT_REG);
  978. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  979. btrfs_set_file_extent_num_bytes(leaf, fi,
  980. extent_end - key.offset);
  981. btrfs_mark_buffer_dirty(leaf);
  982. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  983. if (ret < 0) {
  984. btrfs_abort_transaction(trans, root, ret);
  985. goto out;
  986. }
  987. }
  988. out:
  989. btrfs_free_path(path);
  990. return 0;
  991. }
  992. /*
  993. * on error we return an unlocked page and the error value
  994. * on success we return a locked page and 0
  995. */
  996. static int prepare_uptodate_page(struct page *page, u64 pos,
  997. bool force_uptodate)
  998. {
  999. int ret = 0;
  1000. if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
  1001. !PageUptodate(page)) {
  1002. ret = btrfs_readpage(NULL, page);
  1003. if (ret)
  1004. return ret;
  1005. lock_page(page);
  1006. if (!PageUptodate(page)) {
  1007. unlock_page(page);
  1008. return -EIO;
  1009. }
  1010. }
  1011. return 0;
  1012. }
  1013. /*
  1014. * this gets pages into the page cache and locks them down, it also properly
  1015. * waits for data=ordered extents to finish before allowing the pages to be
  1016. * modified.
  1017. */
  1018. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  1019. struct page **pages, size_t num_pages,
  1020. loff_t pos, unsigned long first_index,
  1021. size_t write_bytes, bool force_uptodate)
  1022. {
  1023. struct extent_state *cached_state = NULL;
  1024. int i;
  1025. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1026. struct inode *inode = fdentry(file)->d_inode;
  1027. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1028. int err = 0;
  1029. int faili = 0;
  1030. u64 start_pos;
  1031. u64 last_pos;
  1032. start_pos = pos & ~((u64)root->sectorsize - 1);
  1033. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  1034. again:
  1035. for (i = 0; i < num_pages; i++) {
  1036. pages[i] = find_or_create_page(inode->i_mapping, index + i,
  1037. mask | __GFP_WRITE);
  1038. if (!pages[i]) {
  1039. faili = i - 1;
  1040. err = -ENOMEM;
  1041. goto fail;
  1042. }
  1043. if (i == 0)
  1044. err = prepare_uptodate_page(pages[i], pos,
  1045. force_uptodate);
  1046. if (i == num_pages - 1)
  1047. err = prepare_uptodate_page(pages[i],
  1048. pos + write_bytes, false);
  1049. if (err) {
  1050. page_cache_release(pages[i]);
  1051. faili = i - 1;
  1052. goto fail;
  1053. }
  1054. wait_on_page_writeback(pages[i]);
  1055. }
  1056. err = 0;
  1057. if (start_pos < inode->i_size) {
  1058. struct btrfs_ordered_extent *ordered;
  1059. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1060. start_pos, last_pos - 1, 0, &cached_state);
  1061. ordered = btrfs_lookup_first_ordered_extent(inode,
  1062. last_pos - 1);
  1063. if (ordered &&
  1064. ordered->file_offset + ordered->len > start_pos &&
  1065. ordered->file_offset < last_pos) {
  1066. btrfs_put_ordered_extent(ordered);
  1067. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1068. start_pos, last_pos - 1,
  1069. &cached_state, GFP_NOFS);
  1070. for (i = 0; i < num_pages; i++) {
  1071. unlock_page(pages[i]);
  1072. page_cache_release(pages[i]);
  1073. }
  1074. btrfs_wait_ordered_range(inode, start_pos,
  1075. last_pos - start_pos);
  1076. goto again;
  1077. }
  1078. if (ordered)
  1079. btrfs_put_ordered_extent(ordered);
  1080. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  1081. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1082. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  1083. GFP_NOFS);
  1084. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1085. start_pos, last_pos - 1, &cached_state,
  1086. GFP_NOFS);
  1087. }
  1088. for (i = 0; i < num_pages; i++) {
  1089. if (clear_page_dirty_for_io(pages[i]))
  1090. account_page_redirty(pages[i]);
  1091. set_page_extent_mapped(pages[i]);
  1092. WARN_ON(!PageLocked(pages[i]));
  1093. }
  1094. return 0;
  1095. fail:
  1096. while (faili >= 0) {
  1097. unlock_page(pages[faili]);
  1098. page_cache_release(pages[faili]);
  1099. faili--;
  1100. }
  1101. return err;
  1102. }
  1103. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  1104. struct iov_iter *i,
  1105. loff_t pos)
  1106. {
  1107. struct inode *inode = fdentry(file)->d_inode;
  1108. struct btrfs_root *root = BTRFS_I(inode)->root;
  1109. struct page **pages = NULL;
  1110. unsigned long first_index;
  1111. size_t num_written = 0;
  1112. int nrptrs;
  1113. int ret = 0;
  1114. bool force_page_uptodate = false;
  1115. nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
  1116. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  1117. (sizeof(struct page *)));
  1118. nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
  1119. nrptrs = max(nrptrs, 8);
  1120. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  1121. if (!pages)
  1122. return -ENOMEM;
  1123. first_index = pos >> PAGE_CACHE_SHIFT;
  1124. while (iov_iter_count(i) > 0) {
  1125. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1126. size_t write_bytes = min(iov_iter_count(i),
  1127. nrptrs * (size_t)PAGE_CACHE_SIZE -
  1128. offset);
  1129. size_t num_pages = (write_bytes + offset +
  1130. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1131. size_t dirty_pages;
  1132. size_t copied;
  1133. WARN_ON(num_pages > nrptrs);
  1134. /*
  1135. * Fault pages before locking them in prepare_pages
  1136. * to avoid recursive lock
  1137. */
  1138. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  1139. ret = -EFAULT;
  1140. break;
  1141. }
  1142. ret = btrfs_delalloc_reserve_space(inode,
  1143. num_pages << PAGE_CACHE_SHIFT);
  1144. if (ret)
  1145. break;
  1146. /*
  1147. * This is going to setup the pages array with the number of
  1148. * pages we want, so we don't really need to worry about the
  1149. * contents of pages from loop to loop
  1150. */
  1151. ret = prepare_pages(root, file, pages, num_pages,
  1152. pos, first_index, write_bytes,
  1153. force_page_uptodate);
  1154. if (ret) {
  1155. btrfs_delalloc_release_space(inode,
  1156. num_pages << PAGE_CACHE_SHIFT);
  1157. break;
  1158. }
  1159. copied = btrfs_copy_from_user(pos, num_pages,
  1160. write_bytes, pages, i);
  1161. /*
  1162. * if we have trouble faulting in the pages, fall
  1163. * back to one page at a time
  1164. */
  1165. if (copied < write_bytes)
  1166. nrptrs = 1;
  1167. if (copied == 0) {
  1168. force_page_uptodate = true;
  1169. dirty_pages = 0;
  1170. } else {
  1171. force_page_uptodate = false;
  1172. dirty_pages = (copied + offset +
  1173. PAGE_CACHE_SIZE - 1) >>
  1174. PAGE_CACHE_SHIFT;
  1175. }
  1176. /*
  1177. * If we had a short copy we need to release the excess delaloc
  1178. * bytes we reserved. We need to increment outstanding_extents
  1179. * because btrfs_delalloc_release_space will decrement it, but
  1180. * we still have an outstanding extent for the chunk we actually
  1181. * managed to copy.
  1182. */
  1183. if (num_pages > dirty_pages) {
  1184. if (copied > 0) {
  1185. spin_lock(&BTRFS_I(inode)->lock);
  1186. BTRFS_I(inode)->outstanding_extents++;
  1187. spin_unlock(&BTRFS_I(inode)->lock);
  1188. }
  1189. btrfs_delalloc_release_space(inode,
  1190. (num_pages - dirty_pages) <<
  1191. PAGE_CACHE_SHIFT);
  1192. }
  1193. if (copied > 0) {
  1194. ret = btrfs_dirty_pages(root, inode, pages,
  1195. dirty_pages, pos, copied,
  1196. NULL);
  1197. if (ret) {
  1198. btrfs_delalloc_release_space(inode,
  1199. dirty_pages << PAGE_CACHE_SHIFT);
  1200. btrfs_drop_pages(pages, num_pages);
  1201. break;
  1202. }
  1203. }
  1204. btrfs_drop_pages(pages, num_pages);
  1205. cond_resched();
  1206. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  1207. dirty_pages);
  1208. if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1209. btrfs_btree_balance_dirty(root, 1);
  1210. pos += copied;
  1211. num_written += copied;
  1212. }
  1213. kfree(pages);
  1214. return num_written ? num_written : ret;
  1215. }
  1216. static ssize_t __btrfs_direct_write(struct kiocb *iocb,
  1217. const struct iovec *iov,
  1218. unsigned long nr_segs, loff_t pos,
  1219. loff_t *ppos, size_t count, size_t ocount)
  1220. {
  1221. struct file *file = iocb->ki_filp;
  1222. struct iov_iter i;
  1223. ssize_t written;
  1224. ssize_t written_buffered;
  1225. loff_t endbyte;
  1226. int err;
  1227. written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
  1228. count, ocount);
  1229. if (written < 0 || written == count)
  1230. return written;
  1231. pos += written;
  1232. count -= written;
  1233. iov_iter_init(&i, iov, nr_segs, count, written);
  1234. written_buffered = __btrfs_buffered_write(file, &i, pos);
  1235. if (written_buffered < 0) {
  1236. err = written_buffered;
  1237. goto out;
  1238. }
  1239. endbyte = pos + written_buffered - 1;
  1240. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  1241. if (err)
  1242. goto out;
  1243. written += written_buffered;
  1244. *ppos = pos + written_buffered;
  1245. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
  1246. endbyte >> PAGE_CACHE_SHIFT);
  1247. out:
  1248. return written ? written : err;
  1249. }
  1250. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  1251. const struct iovec *iov,
  1252. unsigned long nr_segs, loff_t pos)
  1253. {
  1254. struct file *file = iocb->ki_filp;
  1255. struct inode *inode = fdentry(file)->d_inode;
  1256. struct btrfs_root *root = BTRFS_I(inode)->root;
  1257. loff_t *ppos = &iocb->ki_pos;
  1258. u64 start_pos;
  1259. ssize_t num_written = 0;
  1260. ssize_t err = 0;
  1261. size_t count, ocount;
  1262. sb_start_write(inode->i_sb);
  1263. mutex_lock(&inode->i_mutex);
  1264. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1265. if (err) {
  1266. mutex_unlock(&inode->i_mutex);
  1267. goto out;
  1268. }
  1269. count = ocount;
  1270. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1271. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1272. if (err) {
  1273. mutex_unlock(&inode->i_mutex);
  1274. goto out;
  1275. }
  1276. if (count == 0) {
  1277. mutex_unlock(&inode->i_mutex);
  1278. goto out;
  1279. }
  1280. err = file_remove_suid(file);
  1281. if (err) {
  1282. mutex_unlock(&inode->i_mutex);
  1283. goto out;
  1284. }
  1285. /*
  1286. * If BTRFS flips readonly due to some impossible error
  1287. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1288. * although we have opened a file as writable, we have
  1289. * to stop this write operation to ensure FS consistency.
  1290. */
  1291. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  1292. mutex_unlock(&inode->i_mutex);
  1293. err = -EROFS;
  1294. goto out;
  1295. }
  1296. err = file_update_time(file);
  1297. if (err) {
  1298. mutex_unlock(&inode->i_mutex);
  1299. goto out;
  1300. }
  1301. start_pos = round_down(pos, root->sectorsize);
  1302. if (start_pos > i_size_read(inode)) {
  1303. err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
  1304. if (err) {
  1305. mutex_unlock(&inode->i_mutex);
  1306. goto out;
  1307. }
  1308. }
  1309. if (unlikely(file->f_flags & O_DIRECT)) {
  1310. num_written = __btrfs_direct_write(iocb, iov, nr_segs,
  1311. pos, ppos, count, ocount);
  1312. } else {
  1313. struct iov_iter i;
  1314. iov_iter_init(&i, iov, nr_segs, count, num_written);
  1315. num_written = __btrfs_buffered_write(file, &i, pos);
  1316. if (num_written > 0)
  1317. *ppos = pos + num_written;
  1318. }
  1319. mutex_unlock(&inode->i_mutex);
  1320. /*
  1321. * we want to make sure fsync finds this change
  1322. * but we haven't joined a transaction running right now.
  1323. *
  1324. * Later on, someone is sure to update the inode and get the
  1325. * real transid recorded.
  1326. *
  1327. * We set last_trans now to the fs_info generation + 1,
  1328. * this will either be one more than the running transaction
  1329. * or the generation used for the next transaction if there isn't
  1330. * one running right now.
  1331. */
  1332. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  1333. if (num_written > 0 || num_written == -EIOCBQUEUED) {
  1334. err = generic_write_sync(file, pos, num_written);
  1335. if (err < 0 && num_written > 0)
  1336. num_written = err;
  1337. }
  1338. out:
  1339. sb_end_write(inode->i_sb);
  1340. current->backing_dev_info = NULL;
  1341. return num_written ? num_written : err;
  1342. }
  1343. int btrfs_release_file(struct inode *inode, struct file *filp)
  1344. {
  1345. /*
  1346. * ordered_data_close is set by settattr when we are about to truncate
  1347. * a file from a non-zero size to a zero size. This tries to
  1348. * flush down new bytes that may have been written if the
  1349. * application were using truncate to replace a file in place.
  1350. */
  1351. if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  1352. &BTRFS_I(inode)->runtime_flags)) {
  1353. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1354. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1355. filemap_flush(inode->i_mapping);
  1356. }
  1357. if (filp->private_data)
  1358. btrfs_ioctl_trans_end(filp);
  1359. return 0;
  1360. }
  1361. /*
  1362. * fsync call for both files and directories. This logs the inode into
  1363. * the tree log instead of forcing full commits whenever possible.
  1364. *
  1365. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1366. * in the metadata btree are up to date for copying to the log.
  1367. *
  1368. * It drops the inode mutex before doing the tree log commit. This is an
  1369. * important optimization for directories because holding the mutex prevents
  1370. * new operations on the dir while we write to disk.
  1371. */
  1372. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  1373. {
  1374. struct dentry *dentry = file->f_path.dentry;
  1375. struct inode *inode = dentry->d_inode;
  1376. struct btrfs_root *root = BTRFS_I(inode)->root;
  1377. int ret = 0;
  1378. struct btrfs_trans_handle *trans;
  1379. trace_btrfs_sync_file(file, datasync);
  1380. mutex_lock(&inode->i_mutex);
  1381. /*
  1382. * we wait first, since the writeback may change the inode, also wait
  1383. * ordered range does a filemape_write_and_wait_range which is why we
  1384. * don't do it above like other file systems.
  1385. */
  1386. root->log_batch++;
  1387. btrfs_wait_ordered_range(inode, start, end);
  1388. root->log_batch++;
  1389. /*
  1390. * check the transaction that last modified this inode
  1391. * and see if its already been committed
  1392. */
  1393. if (!BTRFS_I(inode)->last_trans) {
  1394. mutex_unlock(&inode->i_mutex);
  1395. goto out;
  1396. }
  1397. /*
  1398. * if the last transaction that changed this file was before
  1399. * the current transaction, we can bail out now without any
  1400. * syncing
  1401. */
  1402. smp_mb();
  1403. if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
  1404. BTRFS_I(inode)->last_trans <=
  1405. root->fs_info->last_trans_committed) {
  1406. BTRFS_I(inode)->last_trans = 0;
  1407. /*
  1408. * We'v had everything committed since the last time we were
  1409. * modified so clear this flag in case it was set for whatever
  1410. * reason, it's no longer relevant.
  1411. */
  1412. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1413. &BTRFS_I(inode)->runtime_flags);
  1414. mutex_unlock(&inode->i_mutex);
  1415. goto out;
  1416. }
  1417. /*
  1418. * ok we haven't committed the transaction yet, lets do a commit
  1419. */
  1420. if (file->private_data)
  1421. btrfs_ioctl_trans_end(file);
  1422. trans = btrfs_start_transaction(root, 0);
  1423. if (IS_ERR(trans)) {
  1424. ret = PTR_ERR(trans);
  1425. mutex_unlock(&inode->i_mutex);
  1426. goto out;
  1427. }
  1428. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1429. if (ret < 0) {
  1430. mutex_unlock(&inode->i_mutex);
  1431. goto out;
  1432. }
  1433. /* we've logged all the items and now have a consistent
  1434. * version of the file in the log. It is possible that
  1435. * someone will come in and modify the file, but that's
  1436. * fine because the log is consistent on disk, and we
  1437. * have references to all of the file's extents
  1438. *
  1439. * It is possible that someone will come in and log the
  1440. * file again, but that will end up using the synchronization
  1441. * inside btrfs_sync_log to keep things safe.
  1442. */
  1443. mutex_unlock(&inode->i_mutex);
  1444. if (ret != BTRFS_NO_LOG_SYNC) {
  1445. if (ret > 0) {
  1446. ret = btrfs_commit_transaction(trans, root);
  1447. } else {
  1448. ret = btrfs_sync_log(trans, root);
  1449. if (ret == 0)
  1450. ret = btrfs_end_transaction(trans, root);
  1451. else
  1452. ret = btrfs_commit_transaction(trans, root);
  1453. }
  1454. } else {
  1455. ret = btrfs_end_transaction(trans, root);
  1456. }
  1457. out:
  1458. return ret > 0 ? -EIO : ret;
  1459. }
  1460. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1461. .fault = filemap_fault,
  1462. .page_mkwrite = btrfs_page_mkwrite,
  1463. };
  1464. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1465. {
  1466. struct address_space *mapping = filp->f_mapping;
  1467. if (!mapping->a_ops->readpage)
  1468. return -ENOEXEC;
  1469. file_accessed(filp);
  1470. vma->vm_ops = &btrfs_file_vm_ops;
  1471. vma->vm_flags |= VM_CAN_NONLINEAR;
  1472. return 0;
  1473. }
  1474. static long btrfs_fallocate(struct file *file, int mode,
  1475. loff_t offset, loff_t len)
  1476. {
  1477. struct inode *inode = file->f_path.dentry->d_inode;
  1478. struct extent_state *cached_state = NULL;
  1479. u64 cur_offset;
  1480. u64 last_byte;
  1481. u64 alloc_start;
  1482. u64 alloc_end;
  1483. u64 alloc_hint = 0;
  1484. u64 locked_end;
  1485. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1486. struct extent_map *em;
  1487. int ret;
  1488. alloc_start = offset & ~mask;
  1489. alloc_end = (offset + len + mask) & ~mask;
  1490. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  1491. if (mode & ~FALLOC_FL_KEEP_SIZE)
  1492. return -EOPNOTSUPP;
  1493. /*
  1494. * Make sure we have enough space before we do the
  1495. * allocation.
  1496. */
  1497. ret = btrfs_check_data_free_space(inode, len);
  1498. if (ret)
  1499. return ret;
  1500. /*
  1501. * wait for ordered IO before we have any locks. We'll loop again
  1502. * below with the locks held.
  1503. */
  1504. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1505. mutex_lock(&inode->i_mutex);
  1506. ret = inode_newsize_ok(inode, alloc_end);
  1507. if (ret)
  1508. goto out;
  1509. if (alloc_start > inode->i_size) {
  1510. ret = btrfs_cont_expand(inode, i_size_read(inode),
  1511. alloc_start);
  1512. if (ret)
  1513. goto out;
  1514. }
  1515. locked_end = alloc_end - 1;
  1516. while (1) {
  1517. struct btrfs_ordered_extent *ordered;
  1518. /* the extent lock is ordered inside the running
  1519. * transaction
  1520. */
  1521. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1522. locked_end, 0, &cached_state);
  1523. ordered = btrfs_lookup_first_ordered_extent(inode,
  1524. alloc_end - 1);
  1525. if (ordered &&
  1526. ordered->file_offset + ordered->len > alloc_start &&
  1527. ordered->file_offset < alloc_end) {
  1528. btrfs_put_ordered_extent(ordered);
  1529. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1530. alloc_start, locked_end,
  1531. &cached_state, GFP_NOFS);
  1532. /*
  1533. * we can't wait on the range with the transaction
  1534. * running or with the extent lock held
  1535. */
  1536. btrfs_wait_ordered_range(inode, alloc_start,
  1537. alloc_end - alloc_start);
  1538. } else {
  1539. if (ordered)
  1540. btrfs_put_ordered_extent(ordered);
  1541. break;
  1542. }
  1543. }
  1544. cur_offset = alloc_start;
  1545. while (1) {
  1546. u64 actual_end;
  1547. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1548. alloc_end - cur_offset, 0);
  1549. if (IS_ERR_OR_NULL(em)) {
  1550. if (!em)
  1551. ret = -ENOMEM;
  1552. else
  1553. ret = PTR_ERR(em);
  1554. break;
  1555. }
  1556. last_byte = min(extent_map_end(em), alloc_end);
  1557. actual_end = min_t(u64, extent_map_end(em), offset + len);
  1558. last_byte = (last_byte + mask) & ~mask;
  1559. if (em->block_start == EXTENT_MAP_HOLE ||
  1560. (cur_offset >= inode->i_size &&
  1561. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1562. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1563. last_byte - cur_offset,
  1564. 1 << inode->i_blkbits,
  1565. offset + len,
  1566. &alloc_hint);
  1567. if (ret < 0) {
  1568. free_extent_map(em);
  1569. break;
  1570. }
  1571. } else if (actual_end > inode->i_size &&
  1572. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1573. /*
  1574. * We didn't need to allocate any more space, but we
  1575. * still extended the size of the file so we need to
  1576. * update i_size.
  1577. */
  1578. inode->i_ctime = CURRENT_TIME;
  1579. i_size_write(inode, actual_end);
  1580. btrfs_ordered_update_i_size(inode, actual_end, NULL);
  1581. }
  1582. free_extent_map(em);
  1583. cur_offset = last_byte;
  1584. if (cur_offset >= alloc_end) {
  1585. ret = 0;
  1586. break;
  1587. }
  1588. }
  1589. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  1590. &cached_state, GFP_NOFS);
  1591. out:
  1592. mutex_unlock(&inode->i_mutex);
  1593. /* Let go of our reservation. */
  1594. btrfs_free_reserved_data_space(inode, len);
  1595. return ret;
  1596. }
  1597. static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
  1598. {
  1599. struct btrfs_root *root = BTRFS_I(inode)->root;
  1600. struct extent_map *em;
  1601. struct extent_state *cached_state = NULL;
  1602. u64 lockstart = *offset;
  1603. u64 lockend = i_size_read(inode);
  1604. u64 start = *offset;
  1605. u64 orig_start = *offset;
  1606. u64 len = i_size_read(inode);
  1607. u64 last_end = 0;
  1608. int ret = 0;
  1609. lockend = max_t(u64, root->sectorsize, lockend);
  1610. if (lockend <= lockstart)
  1611. lockend = lockstart + root->sectorsize;
  1612. len = lockend - lockstart + 1;
  1613. len = max_t(u64, len, root->sectorsize);
  1614. if (inode->i_size == 0)
  1615. return -ENXIO;
  1616. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
  1617. &cached_state);
  1618. /*
  1619. * Delalloc is such a pain. If we have a hole and we have pending
  1620. * delalloc for a portion of the hole we will get back a hole that
  1621. * exists for the entire range since it hasn't been actually written
  1622. * yet. So to take care of this case we need to look for an extent just
  1623. * before the position we want in case there is outstanding delalloc
  1624. * going on here.
  1625. */
  1626. if (origin == SEEK_HOLE && start != 0) {
  1627. if (start <= root->sectorsize)
  1628. em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
  1629. root->sectorsize, 0);
  1630. else
  1631. em = btrfs_get_extent_fiemap(inode, NULL, 0,
  1632. start - root->sectorsize,
  1633. root->sectorsize, 0);
  1634. if (IS_ERR(em)) {
  1635. ret = PTR_ERR(em);
  1636. goto out;
  1637. }
  1638. last_end = em->start + em->len;
  1639. if (em->block_start == EXTENT_MAP_DELALLOC)
  1640. last_end = min_t(u64, last_end, inode->i_size);
  1641. free_extent_map(em);
  1642. }
  1643. while (1) {
  1644. em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
  1645. if (IS_ERR(em)) {
  1646. ret = PTR_ERR(em);
  1647. break;
  1648. }
  1649. if (em->block_start == EXTENT_MAP_HOLE) {
  1650. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1651. if (last_end <= orig_start) {
  1652. free_extent_map(em);
  1653. ret = -ENXIO;
  1654. break;
  1655. }
  1656. }
  1657. if (origin == SEEK_HOLE) {
  1658. *offset = start;
  1659. free_extent_map(em);
  1660. break;
  1661. }
  1662. } else {
  1663. if (origin == SEEK_DATA) {
  1664. if (em->block_start == EXTENT_MAP_DELALLOC) {
  1665. if (start >= inode->i_size) {
  1666. free_extent_map(em);
  1667. ret = -ENXIO;
  1668. break;
  1669. }
  1670. }
  1671. *offset = start;
  1672. free_extent_map(em);
  1673. break;
  1674. }
  1675. }
  1676. start = em->start + em->len;
  1677. last_end = em->start + em->len;
  1678. if (em->block_start == EXTENT_MAP_DELALLOC)
  1679. last_end = min_t(u64, last_end, inode->i_size);
  1680. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1681. free_extent_map(em);
  1682. ret = -ENXIO;
  1683. break;
  1684. }
  1685. free_extent_map(em);
  1686. cond_resched();
  1687. }
  1688. if (!ret)
  1689. *offset = min(*offset, inode->i_size);
  1690. out:
  1691. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1692. &cached_state, GFP_NOFS);
  1693. return ret;
  1694. }
  1695. static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
  1696. {
  1697. struct inode *inode = file->f_mapping->host;
  1698. int ret;
  1699. mutex_lock(&inode->i_mutex);
  1700. switch (origin) {
  1701. case SEEK_END:
  1702. case SEEK_CUR:
  1703. offset = generic_file_llseek(file, offset, origin);
  1704. goto out;
  1705. case SEEK_DATA:
  1706. case SEEK_HOLE:
  1707. if (offset >= i_size_read(inode)) {
  1708. mutex_unlock(&inode->i_mutex);
  1709. return -ENXIO;
  1710. }
  1711. ret = find_desired_extent(inode, &offset, origin);
  1712. if (ret) {
  1713. mutex_unlock(&inode->i_mutex);
  1714. return ret;
  1715. }
  1716. }
  1717. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
  1718. offset = -EINVAL;
  1719. goto out;
  1720. }
  1721. if (offset > inode->i_sb->s_maxbytes) {
  1722. offset = -EINVAL;
  1723. goto out;
  1724. }
  1725. /* Special lock needed here? */
  1726. if (offset != file->f_pos) {
  1727. file->f_pos = offset;
  1728. file->f_version = 0;
  1729. }
  1730. out:
  1731. mutex_unlock(&inode->i_mutex);
  1732. return offset;
  1733. }
  1734. const struct file_operations btrfs_file_operations = {
  1735. .llseek = btrfs_file_llseek,
  1736. .read = do_sync_read,
  1737. .write = do_sync_write,
  1738. .aio_read = generic_file_aio_read,
  1739. .splice_read = generic_file_splice_read,
  1740. .aio_write = btrfs_file_aio_write,
  1741. .mmap = btrfs_file_mmap,
  1742. .open = generic_file_open,
  1743. .release = btrfs_release_file,
  1744. .fsync = btrfs_sync_file,
  1745. .fallocate = btrfs_fallocate,
  1746. .unlocked_ioctl = btrfs_ioctl,
  1747. #ifdef CONFIG_COMPAT
  1748. .compat_ioctl = btrfs_ioctl,
  1749. #endif
  1750. };