file.c 58 KB

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