file.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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. if (drop_cache)
  551. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  552. if (start >= BTRFS_I(inode)->disk_i_size)
  553. modify_tree = 0;
  554. while (1) {
  555. recow = 0;
  556. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  557. search_start, modify_tree);
  558. if (ret < 0)
  559. break;
  560. if (ret > 0 && path->slots[0] > 0 && search_start == start) {
  561. leaf = path->nodes[0];
  562. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  563. if (key.objectid == ino &&
  564. key.type == BTRFS_EXTENT_DATA_KEY)
  565. path->slots[0]--;
  566. }
  567. ret = 0;
  568. next_slot:
  569. leaf = path->nodes[0];
  570. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  571. BUG_ON(del_nr > 0);
  572. ret = btrfs_next_leaf(root, path);
  573. if (ret < 0)
  574. break;
  575. if (ret > 0) {
  576. ret = 0;
  577. break;
  578. }
  579. leaf = path->nodes[0];
  580. recow = 1;
  581. }
  582. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  583. if (key.objectid > ino ||
  584. key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
  585. break;
  586. fi = btrfs_item_ptr(leaf, path->slots[0],
  587. struct btrfs_file_extent_item);
  588. extent_type = btrfs_file_extent_type(leaf, fi);
  589. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  590. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  591. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  592. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  593. extent_offset = btrfs_file_extent_offset(leaf, fi);
  594. extent_end = key.offset +
  595. btrfs_file_extent_num_bytes(leaf, fi);
  596. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  597. extent_end = key.offset +
  598. btrfs_file_extent_inline_len(leaf, fi);
  599. } else {
  600. WARN_ON(1);
  601. extent_end = search_start;
  602. }
  603. if (extent_end <= search_start) {
  604. path->slots[0]++;
  605. goto next_slot;
  606. }
  607. search_start = max(key.offset, start);
  608. if (recow || !modify_tree) {
  609. modify_tree = -1;
  610. btrfs_release_path(path);
  611. continue;
  612. }
  613. /*
  614. * | - range to drop - |
  615. * | -------- extent -------- |
  616. */
  617. if (start > key.offset && end < extent_end) {
  618. BUG_ON(del_nr > 0);
  619. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  620. memcpy(&new_key, &key, sizeof(new_key));
  621. new_key.offset = start;
  622. ret = btrfs_duplicate_item(trans, root, path,
  623. &new_key);
  624. if (ret == -EAGAIN) {
  625. btrfs_release_path(path);
  626. continue;
  627. }
  628. if (ret < 0)
  629. break;
  630. leaf = path->nodes[0];
  631. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  632. struct btrfs_file_extent_item);
  633. btrfs_set_file_extent_num_bytes(leaf, fi,
  634. start - key.offset);
  635. fi = btrfs_item_ptr(leaf, path->slots[0],
  636. struct btrfs_file_extent_item);
  637. extent_offset += start - key.offset;
  638. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  639. btrfs_set_file_extent_num_bytes(leaf, fi,
  640. extent_end - start);
  641. btrfs_mark_buffer_dirty(leaf);
  642. if (update_refs && disk_bytenr > 0) {
  643. ret = btrfs_inc_extent_ref(trans, root,
  644. disk_bytenr, num_bytes, 0,
  645. root->root_key.objectid,
  646. new_key.objectid,
  647. start - extent_offset, 0);
  648. BUG_ON(ret); /* -ENOMEM */
  649. }
  650. key.offset = start;
  651. }
  652. /*
  653. * | ---- range to drop ----- |
  654. * | -------- extent -------- |
  655. */
  656. if (start <= key.offset && end < extent_end) {
  657. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  658. memcpy(&new_key, &key, sizeof(new_key));
  659. new_key.offset = end;
  660. btrfs_set_item_key_safe(trans, root, path, &new_key);
  661. extent_offset += end - key.offset;
  662. btrfs_set_file_extent_offset(leaf, fi, extent_offset);
  663. btrfs_set_file_extent_num_bytes(leaf, fi,
  664. extent_end - end);
  665. btrfs_mark_buffer_dirty(leaf);
  666. if (update_refs && disk_bytenr > 0)
  667. inode_sub_bytes(inode, end - key.offset);
  668. break;
  669. }
  670. search_start = extent_end;
  671. /*
  672. * | ---- range to drop ----- |
  673. * | -------- extent -------- |
  674. */
  675. if (start > key.offset && end >= extent_end) {
  676. BUG_ON(del_nr > 0);
  677. BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
  678. btrfs_set_file_extent_num_bytes(leaf, fi,
  679. start - key.offset);
  680. btrfs_mark_buffer_dirty(leaf);
  681. if (update_refs && disk_bytenr > 0)
  682. inode_sub_bytes(inode, extent_end - start);
  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. }
  716. if (end == extent_end)
  717. break;
  718. if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
  719. path->slots[0]++;
  720. goto next_slot;
  721. }
  722. ret = btrfs_del_items(trans, root, path, del_slot,
  723. del_nr);
  724. if (ret) {
  725. btrfs_abort_transaction(trans, root, ret);
  726. break;
  727. }
  728. del_nr = 0;
  729. del_slot = 0;
  730. btrfs_release_path(path);
  731. continue;
  732. }
  733. BUG_ON(1);
  734. }
  735. if (!ret && del_nr > 0) {
  736. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  737. if (ret)
  738. btrfs_abort_transaction(trans, root, ret);
  739. }
  740. if (drop_end)
  741. *drop_end = min(end, extent_end);
  742. btrfs_release_path(path);
  743. return ret;
  744. }
  745. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  746. struct btrfs_root *root, struct inode *inode, u64 start,
  747. u64 end, int drop_cache)
  748. {
  749. struct btrfs_path *path;
  750. int ret;
  751. path = btrfs_alloc_path();
  752. if (!path)
  753. return -ENOMEM;
  754. ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
  755. drop_cache);
  756. btrfs_free_path(path);
  757. return ret;
  758. }
  759. static int extent_mergeable(struct extent_buffer *leaf, int slot,
  760. u64 objectid, u64 bytenr, u64 orig_offset,
  761. u64 *start, u64 *end)
  762. {
  763. struct btrfs_file_extent_item *fi;
  764. struct btrfs_key key;
  765. u64 extent_end;
  766. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  767. return 0;
  768. btrfs_item_key_to_cpu(leaf, &key, slot);
  769. if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
  770. return 0;
  771. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  772. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
  773. btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
  774. btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
  775. btrfs_file_extent_compression(leaf, fi) ||
  776. btrfs_file_extent_encryption(leaf, fi) ||
  777. btrfs_file_extent_other_encoding(leaf, fi))
  778. return 0;
  779. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  780. if ((*start && *start != key.offset) || (*end && *end != extent_end))
  781. return 0;
  782. *start = key.offset;
  783. *end = extent_end;
  784. return 1;
  785. }
  786. /*
  787. * Mark extent in the range start - end as written.
  788. *
  789. * This changes extent type from 'pre-allocated' to 'regular'. If only
  790. * part of extent is marked as written, the extent will be split into
  791. * two or three.
  792. */
  793. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  794. struct inode *inode, u64 start, u64 end)
  795. {
  796. struct btrfs_root *root = BTRFS_I(inode)->root;
  797. struct extent_buffer *leaf;
  798. struct btrfs_path *path;
  799. struct btrfs_file_extent_item *fi;
  800. struct btrfs_key key;
  801. struct btrfs_key new_key;
  802. u64 bytenr;
  803. u64 num_bytes;
  804. u64 extent_end;
  805. u64 orig_offset;
  806. u64 other_start;
  807. u64 other_end;
  808. u64 split;
  809. int del_nr = 0;
  810. int del_slot = 0;
  811. int recow;
  812. int ret;
  813. u64 ino = btrfs_ino(inode);
  814. path = btrfs_alloc_path();
  815. if (!path)
  816. return -ENOMEM;
  817. again:
  818. recow = 0;
  819. split = start;
  820. key.objectid = ino;
  821. key.type = BTRFS_EXTENT_DATA_KEY;
  822. key.offset = split;
  823. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  824. if (ret < 0)
  825. goto out;
  826. if (ret > 0 && path->slots[0] > 0)
  827. path->slots[0]--;
  828. leaf = path->nodes[0];
  829. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  830. BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
  831. fi = btrfs_item_ptr(leaf, path->slots[0],
  832. struct btrfs_file_extent_item);
  833. BUG_ON(btrfs_file_extent_type(leaf, fi) !=
  834. BTRFS_FILE_EXTENT_PREALLOC);
  835. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  836. BUG_ON(key.offset > start || extent_end < end);
  837. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  838. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  839. orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
  840. memcpy(&new_key, &key, sizeof(new_key));
  841. if (start == key.offset && end < extent_end) {
  842. other_start = 0;
  843. other_end = start;
  844. if (extent_mergeable(leaf, path->slots[0] - 1,
  845. ino, bytenr, orig_offset,
  846. &other_start, &other_end)) {
  847. new_key.offset = end;
  848. btrfs_set_item_key_safe(trans, root, path, &new_key);
  849. fi = btrfs_item_ptr(leaf, path->slots[0],
  850. struct btrfs_file_extent_item);
  851. btrfs_set_file_extent_generation(leaf, fi,
  852. trans->transid);
  853. btrfs_set_file_extent_num_bytes(leaf, fi,
  854. extent_end - end);
  855. btrfs_set_file_extent_offset(leaf, fi,
  856. end - orig_offset);
  857. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  858. struct btrfs_file_extent_item);
  859. btrfs_set_file_extent_generation(leaf, fi,
  860. trans->transid);
  861. btrfs_set_file_extent_num_bytes(leaf, fi,
  862. end - other_start);
  863. btrfs_mark_buffer_dirty(leaf);
  864. goto out;
  865. }
  866. }
  867. if (start > key.offset && end == extent_end) {
  868. other_start = end;
  869. other_end = 0;
  870. if (extent_mergeable(leaf, path->slots[0] + 1,
  871. ino, bytenr, orig_offset,
  872. &other_start, &other_end)) {
  873. fi = btrfs_item_ptr(leaf, path->slots[0],
  874. struct btrfs_file_extent_item);
  875. btrfs_set_file_extent_num_bytes(leaf, fi,
  876. start - key.offset);
  877. btrfs_set_file_extent_generation(leaf, fi,
  878. trans->transid);
  879. path->slots[0]++;
  880. new_key.offset = start;
  881. btrfs_set_item_key_safe(trans, root, path, &new_key);
  882. fi = btrfs_item_ptr(leaf, path->slots[0],
  883. struct btrfs_file_extent_item);
  884. btrfs_set_file_extent_generation(leaf, fi,
  885. trans->transid);
  886. btrfs_set_file_extent_num_bytes(leaf, fi,
  887. other_end - start);
  888. btrfs_set_file_extent_offset(leaf, fi,
  889. start - orig_offset);
  890. btrfs_mark_buffer_dirty(leaf);
  891. goto out;
  892. }
  893. }
  894. while (start > key.offset || end < extent_end) {
  895. if (key.offset == start)
  896. split = end;
  897. new_key.offset = split;
  898. ret = btrfs_duplicate_item(trans, root, path, &new_key);
  899. if (ret == -EAGAIN) {
  900. btrfs_release_path(path);
  901. goto again;
  902. }
  903. if (ret < 0) {
  904. btrfs_abort_transaction(trans, root, ret);
  905. goto out;
  906. }
  907. leaf = path->nodes[0];
  908. fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
  909. struct btrfs_file_extent_item);
  910. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  911. btrfs_set_file_extent_num_bytes(leaf, fi,
  912. split - key.offset);
  913. fi = btrfs_item_ptr(leaf, path->slots[0],
  914. struct btrfs_file_extent_item);
  915. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  916. btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
  917. btrfs_set_file_extent_num_bytes(leaf, fi,
  918. extent_end - split);
  919. btrfs_mark_buffer_dirty(leaf);
  920. ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
  921. root->root_key.objectid,
  922. ino, orig_offset, 0);
  923. BUG_ON(ret); /* -ENOMEM */
  924. if (split == start) {
  925. key.offset = start;
  926. } else {
  927. BUG_ON(start != key.offset);
  928. path->slots[0]--;
  929. extent_end = end;
  930. }
  931. recow = 1;
  932. }
  933. other_start = end;
  934. other_end = 0;
  935. if (extent_mergeable(leaf, path->slots[0] + 1,
  936. ino, bytenr, orig_offset,
  937. &other_start, &other_end)) {
  938. if (recow) {
  939. btrfs_release_path(path);
  940. goto again;
  941. }
  942. extent_end = other_end;
  943. del_slot = path->slots[0] + 1;
  944. del_nr++;
  945. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  946. 0, root->root_key.objectid,
  947. ino, orig_offset, 0);
  948. BUG_ON(ret); /* -ENOMEM */
  949. }
  950. other_start = 0;
  951. other_end = start;
  952. if (extent_mergeable(leaf, path->slots[0] - 1,
  953. ino, bytenr, orig_offset,
  954. &other_start, &other_end)) {
  955. if (recow) {
  956. btrfs_release_path(path);
  957. goto again;
  958. }
  959. key.offset = other_start;
  960. del_slot = path->slots[0];
  961. del_nr++;
  962. ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
  963. 0, root->root_key.objectid,
  964. ino, orig_offset, 0);
  965. BUG_ON(ret); /* -ENOMEM */
  966. }
  967. if (del_nr == 0) {
  968. fi = btrfs_item_ptr(leaf, path->slots[0],
  969. struct btrfs_file_extent_item);
  970. btrfs_set_file_extent_type(leaf, fi,
  971. BTRFS_FILE_EXTENT_REG);
  972. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  973. btrfs_mark_buffer_dirty(leaf);
  974. } else {
  975. fi = btrfs_item_ptr(leaf, del_slot - 1,
  976. struct btrfs_file_extent_item);
  977. btrfs_set_file_extent_type(leaf, fi,
  978. BTRFS_FILE_EXTENT_REG);
  979. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  980. btrfs_set_file_extent_num_bytes(leaf, fi,
  981. extent_end - key.offset);
  982. btrfs_mark_buffer_dirty(leaf);
  983. ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
  984. if (ret < 0) {
  985. btrfs_abort_transaction(trans, root, ret);
  986. goto out;
  987. }
  988. }
  989. out:
  990. btrfs_free_path(path);
  991. return 0;
  992. }
  993. /*
  994. * on error we return an unlocked page and the error value
  995. * on success we return a locked page and 0
  996. */
  997. static int prepare_uptodate_page(struct page *page, u64 pos,
  998. bool force_uptodate)
  999. {
  1000. int ret = 0;
  1001. if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
  1002. !PageUptodate(page)) {
  1003. ret = btrfs_readpage(NULL, page);
  1004. if (ret)
  1005. return ret;
  1006. lock_page(page);
  1007. if (!PageUptodate(page)) {
  1008. unlock_page(page);
  1009. return -EIO;
  1010. }
  1011. }
  1012. return 0;
  1013. }
  1014. /*
  1015. * this gets pages into the page cache and locks them down, it also properly
  1016. * waits for data=ordered extents to finish before allowing the pages to be
  1017. * modified.
  1018. */
  1019. static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
  1020. struct page **pages, size_t num_pages,
  1021. loff_t pos, unsigned long first_index,
  1022. size_t write_bytes, bool force_uptodate)
  1023. {
  1024. struct extent_state *cached_state = NULL;
  1025. int i;
  1026. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1027. struct inode *inode = fdentry(file)->d_inode;
  1028. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1029. int err = 0;
  1030. int faili = 0;
  1031. u64 start_pos;
  1032. u64 last_pos;
  1033. start_pos = pos & ~((u64)root->sectorsize - 1);
  1034. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  1035. again:
  1036. for (i = 0; i < num_pages; i++) {
  1037. pages[i] = find_or_create_page(inode->i_mapping, index + i,
  1038. mask | __GFP_WRITE);
  1039. if (!pages[i]) {
  1040. faili = i - 1;
  1041. err = -ENOMEM;
  1042. goto fail;
  1043. }
  1044. if (i == 0)
  1045. err = prepare_uptodate_page(pages[i], pos,
  1046. force_uptodate);
  1047. if (i == num_pages - 1)
  1048. err = prepare_uptodate_page(pages[i],
  1049. pos + write_bytes, false);
  1050. if (err) {
  1051. page_cache_release(pages[i]);
  1052. faili = i - 1;
  1053. goto fail;
  1054. }
  1055. wait_on_page_writeback(pages[i]);
  1056. }
  1057. err = 0;
  1058. if (start_pos < inode->i_size) {
  1059. struct btrfs_ordered_extent *ordered;
  1060. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1061. start_pos, last_pos - 1, 0, &cached_state);
  1062. ordered = btrfs_lookup_first_ordered_extent(inode,
  1063. last_pos - 1);
  1064. if (ordered &&
  1065. ordered->file_offset + ordered->len > start_pos &&
  1066. ordered->file_offset < last_pos) {
  1067. btrfs_put_ordered_extent(ordered);
  1068. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1069. start_pos, last_pos - 1,
  1070. &cached_state, GFP_NOFS);
  1071. for (i = 0; i < num_pages; i++) {
  1072. unlock_page(pages[i]);
  1073. page_cache_release(pages[i]);
  1074. }
  1075. btrfs_wait_ordered_range(inode, start_pos,
  1076. last_pos - start_pos);
  1077. goto again;
  1078. }
  1079. if (ordered)
  1080. btrfs_put_ordered_extent(ordered);
  1081. clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
  1082. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1083. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  1084. 0, 0, &cached_state, GFP_NOFS);
  1085. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1086. start_pos, last_pos - 1, &cached_state,
  1087. GFP_NOFS);
  1088. }
  1089. for (i = 0; i < num_pages; i++) {
  1090. if (clear_page_dirty_for_io(pages[i]))
  1091. account_page_redirty(pages[i]);
  1092. set_page_extent_mapped(pages[i]);
  1093. WARN_ON(!PageLocked(pages[i]));
  1094. }
  1095. return 0;
  1096. fail:
  1097. while (faili >= 0) {
  1098. unlock_page(pages[faili]);
  1099. page_cache_release(pages[faili]);
  1100. faili--;
  1101. }
  1102. return err;
  1103. }
  1104. static noinline ssize_t __btrfs_buffered_write(struct file *file,
  1105. struct iov_iter *i,
  1106. loff_t pos)
  1107. {
  1108. struct inode *inode = fdentry(file)->d_inode;
  1109. struct btrfs_root *root = BTRFS_I(inode)->root;
  1110. struct page **pages = NULL;
  1111. unsigned long first_index;
  1112. size_t num_written = 0;
  1113. int nrptrs;
  1114. int ret = 0;
  1115. bool force_page_uptodate = false;
  1116. nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
  1117. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
  1118. (sizeof(struct page *)));
  1119. nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
  1120. nrptrs = max(nrptrs, 8);
  1121. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  1122. if (!pages)
  1123. return -ENOMEM;
  1124. first_index = pos >> PAGE_CACHE_SHIFT;
  1125. while (iov_iter_count(i) > 0) {
  1126. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1127. size_t write_bytes = min(iov_iter_count(i),
  1128. nrptrs * (size_t)PAGE_CACHE_SIZE -
  1129. offset);
  1130. size_t num_pages = (write_bytes + offset +
  1131. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1132. size_t dirty_pages;
  1133. size_t copied;
  1134. WARN_ON(num_pages > nrptrs);
  1135. /*
  1136. * Fault pages before locking them in prepare_pages
  1137. * to avoid recursive lock
  1138. */
  1139. if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
  1140. ret = -EFAULT;
  1141. break;
  1142. }
  1143. ret = btrfs_delalloc_reserve_space(inode,
  1144. num_pages << PAGE_CACHE_SHIFT);
  1145. if (ret)
  1146. break;
  1147. /*
  1148. * This is going to setup the pages array with the number of
  1149. * pages we want, so we don't really need to worry about the
  1150. * contents of pages from loop to loop
  1151. */
  1152. ret = prepare_pages(root, file, pages, num_pages,
  1153. pos, first_index, write_bytes,
  1154. force_page_uptodate);
  1155. if (ret) {
  1156. btrfs_delalloc_release_space(inode,
  1157. num_pages << PAGE_CACHE_SHIFT);
  1158. break;
  1159. }
  1160. copied = btrfs_copy_from_user(pos, num_pages,
  1161. write_bytes, pages, i);
  1162. /*
  1163. * if we have trouble faulting in the pages, fall
  1164. * back to one page at a time
  1165. */
  1166. if (copied < write_bytes)
  1167. nrptrs = 1;
  1168. if (copied == 0) {
  1169. force_page_uptodate = true;
  1170. dirty_pages = 0;
  1171. } else {
  1172. force_page_uptodate = false;
  1173. dirty_pages = (copied + offset +
  1174. PAGE_CACHE_SIZE - 1) >>
  1175. PAGE_CACHE_SHIFT;
  1176. }
  1177. /*
  1178. * If we had a short copy we need to release the excess delaloc
  1179. * bytes we reserved. We need to increment outstanding_extents
  1180. * because btrfs_delalloc_release_space will decrement it, but
  1181. * we still have an outstanding extent for the chunk we actually
  1182. * managed to copy.
  1183. */
  1184. if (num_pages > dirty_pages) {
  1185. if (copied > 0) {
  1186. spin_lock(&BTRFS_I(inode)->lock);
  1187. BTRFS_I(inode)->outstanding_extents++;
  1188. spin_unlock(&BTRFS_I(inode)->lock);
  1189. }
  1190. btrfs_delalloc_release_space(inode,
  1191. (num_pages - dirty_pages) <<
  1192. PAGE_CACHE_SHIFT);
  1193. }
  1194. if (copied > 0) {
  1195. ret = btrfs_dirty_pages(root, inode, pages,
  1196. dirty_pages, pos, copied,
  1197. NULL);
  1198. if (ret) {
  1199. btrfs_delalloc_release_space(inode,
  1200. dirty_pages << PAGE_CACHE_SHIFT);
  1201. btrfs_drop_pages(pages, num_pages);
  1202. break;
  1203. }
  1204. }
  1205. btrfs_drop_pages(pages, num_pages);
  1206. cond_resched();
  1207. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  1208. dirty_pages);
  1209. if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  1210. btrfs_btree_balance_dirty(root, 1);
  1211. pos += copied;
  1212. num_written += copied;
  1213. }
  1214. kfree(pages);
  1215. return num_written ? num_written : ret;
  1216. }
  1217. static ssize_t __btrfs_direct_write(struct kiocb *iocb,
  1218. const struct iovec *iov,
  1219. unsigned long nr_segs, loff_t pos,
  1220. loff_t *ppos, size_t count, size_t ocount)
  1221. {
  1222. struct file *file = iocb->ki_filp;
  1223. struct iov_iter i;
  1224. ssize_t written;
  1225. ssize_t written_buffered;
  1226. loff_t endbyte;
  1227. int err;
  1228. written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
  1229. count, ocount);
  1230. if (written < 0 || written == count)
  1231. return written;
  1232. pos += written;
  1233. count -= written;
  1234. iov_iter_init(&i, iov, nr_segs, count, written);
  1235. written_buffered = __btrfs_buffered_write(file, &i, pos);
  1236. if (written_buffered < 0) {
  1237. err = written_buffered;
  1238. goto out;
  1239. }
  1240. endbyte = pos + written_buffered - 1;
  1241. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  1242. if (err)
  1243. goto out;
  1244. written += written_buffered;
  1245. *ppos = pos + written_buffered;
  1246. invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
  1247. endbyte >> PAGE_CACHE_SHIFT);
  1248. out:
  1249. return written ? written : err;
  1250. }
  1251. static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
  1252. const struct iovec *iov,
  1253. unsigned long nr_segs, loff_t pos)
  1254. {
  1255. struct file *file = iocb->ki_filp;
  1256. struct inode *inode = fdentry(file)->d_inode;
  1257. struct btrfs_root *root = BTRFS_I(inode)->root;
  1258. loff_t *ppos = &iocb->ki_pos;
  1259. u64 start_pos;
  1260. ssize_t num_written = 0;
  1261. ssize_t err = 0;
  1262. size_t count, ocount;
  1263. sb_start_write(inode->i_sb);
  1264. mutex_lock(&inode->i_mutex);
  1265. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1266. if (err) {
  1267. mutex_unlock(&inode->i_mutex);
  1268. goto out;
  1269. }
  1270. count = ocount;
  1271. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1272. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1273. if (err) {
  1274. mutex_unlock(&inode->i_mutex);
  1275. goto out;
  1276. }
  1277. if (count == 0) {
  1278. mutex_unlock(&inode->i_mutex);
  1279. goto out;
  1280. }
  1281. err = file_remove_suid(file);
  1282. if (err) {
  1283. mutex_unlock(&inode->i_mutex);
  1284. goto out;
  1285. }
  1286. /*
  1287. * If BTRFS flips readonly due to some impossible error
  1288. * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
  1289. * although we have opened a file as writable, we have
  1290. * to stop this write operation to ensure FS consistency.
  1291. */
  1292. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  1293. mutex_unlock(&inode->i_mutex);
  1294. err = -EROFS;
  1295. goto out;
  1296. }
  1297. err = file_update_time(file);
  1298. if (err) {
  1299. mutex_unlock(&inode->i_mutex);
  1300. goto out;
  1301. }
  1302. start_pos = round_down(pos, root->sectorsize);
  1303. if (start_pos > i_size_read(inode)) {
  1304. err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
  1305. if (err) {
  1306. mutex_unlock(&inode->i_mutex);
  1307. goto out;
  1308. }
  1309. }
  1310. if (unlikely(file->f_flags & O_DIRECT)) {
  1311. num_written = __btrfs_direct_write(iocb, iov, nr_segs,
  1312. pos, ppos, count, ocount);
  1313. } else {
  1314. struct iov_iter i;
  1315. iov_iter_init(&i, iov, nr_segs, count, num_written);
  1316. num_written = __btrfs_buffered_write(file, &i, pos);
  1317. if (num_written > 0)
  1318. *ppos = pos + num_written;
  1319. }
  1320. mutex_unlock(&inode->i_mutex);
  1321. /*
  1322. * we want to make sure fsync finds this change
  1323. * but we haven't joined a transaction running right now.
  1324. *
  1325. * Later on, someone is sure to update the inode and get the
  1326. * real transid recorded.
  1327. *
  1328. * We set last_trans now to the fs_info generation + 1,
  1329. * this will either be one more than the running transaction
  1330. * or the generation used for the next transaction if there isn't
  1331. * one running right now.
  1332. */
  1333. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  1334. if (num_written > 0 || num_written == -EIOCBQUEUED) {
  1335. err = generic_write_sync(file, pos, num_written);
  1336. if (err < 0 && num_written > 0)
  1337. num_written = err;
  1338. }
  1339. out:
  1340. sb_end_write(inode->i_sb);
  1341. current->backing_dev_info = NULL;
  1342. return num_written ? num_written : err;
  1343. }
  1344. int btrfs_release_file(struct inode *inode, struct file *filp)
  1345. {
  1346. /*
  1347. * ordered_data_close is set by settattr when we are about to truncate
  1348. * a file from a non-zero size to a zero size. This tries to
  1349. * flush down new bytes that may have been written if the
  1350. * application were using truncate to replace a file in place.
  1351. */
  1352. if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  1353. &BTRFS_I(inode)->runtime_flags)) {
  1354. btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
  1355. if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  1356. filemap_flush(inode->i_mapping);
  1357. }
  1358. if (filp->private_data)
  1359. btrfs_ioctl_trans_end(filp);
  1360. return 0;
  1361. }
  1362. /*
  1363. * fsync call for both files and directories. This logs the inode into
  1364. * the tree log instead of forcing full commits whenever possible.
  1365. *
  1366. * It needs to call filemap_fdatawait so that all ordered extent updates are
  1367. * in the metadata btree are up to date for copying to the log.
  1368. *
  1369. * It drops the inode mutex before doing the tree log commit. This is an
  1370. * important optimization for directories because holding the mutex prevents
  1371. * new operations on the dir while we write to disk.
  1372. */
  1373. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  1374. {
  1375. struct dentry *dentry = file->f_path.dentry;
  1376. struct inode *inode = dentry->d_inode;
  1377. struct btrfs_root *root = BTRFS_I(inode)->root;
  1378. int ret = 0;
  1379. struct btrfs_trans_handle *trans;
  1380. trace_btrfs_sync_file(file, datasync);
  1381. /*
  1382. * We write the dirty pages in the range and wait until they complete
  1383. * out of the ->i_mutex. If so, we can flush the dirty pages by
  1384. * multi-task, and make the performance up.
  1385. */
  1386. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1387. if (ret)
  1388. return ret;
  1389. mutex_lock(&inode->i_mutex);
  1390. /*
  1391. * We flush the dirty pages again to avoid some dirty pages in the
  1392. * range being left.
  1393. */
  1394. atomic_inc(&root->log_batch);
  1395. btrfs_wait_ordered_range(inode, start, end);
  1396. atomic_inc(&root->log_batch);
  1397. /*
  1398. * check the transaction that last modified this inode
  1399. * and see if its already been committed
  1400. */
  1401. if (!BTRFS_I(inode)->last_trans) {
  1402. mutex_unlock(&inode->i_mutex);
  1403. goto out;
  1404. }
  1405. /*
  1406. * if the last transaction that changed this file was before
  1407. * the current transaction, we can bail out now without any
  1408. * syncing
  1409. */
  1410. smp_mb();
  1411. if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
  1412. BTRFS_I(inode)->last_trans <=
  1413. root->fs_info->last_trans_committed) {
  1414. BTRFS_I(inode)->last_trans = 0;
  1415. /*
  1416. * We'v had everything committed since the last time we were
  1417. * modified so clear this flag in case it was set for whatever
  1418. * reason, it's no longer relevant.
  1419. */
  1420. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1421. &BTRFS_I(inode)->runtime_flags);
  1422. mutex_unlock(&inode->i_mutex);
  1423. goto out;
  1424. }
  1425. /*
  1426. * ok we haven't committed the transaction yet, lets do a commit
  1427. */
  1428. if (file->private_data)
  1429. btrfs_ioctl_trans_end(file);
  1430. trans = btrfs_start_transaction(root, 0);
  1431. if (IS_ERR(trans)) {
  1432. ret = PTR_ERR(trans);
  1433. mutex_unlock(&inode->i_mutex);
  1434. goto out;
  1435. }
  1436. ret = btrfs_log_dentry_safe(trans, root, dentry);
  1437. if (ret < 0) {
  1438. mutex_unlock(&inode->i_mutex);
  1439. goto out;
  1440. }
  1441. /* we've logged all the items and now have a consistent
  1442. * version of the file in the log. It is possible that
  1443. * someone will come in and modify the file, but that's
  1444. * fine because the log is consistent on disk, and we
  1445. * have references to all of the file's extents
  1446. *
  1447. * It is possible that someone will come in and log the
  1448. * file again, but that will end up using the synchronization
  1449. * inside btrfs_sync_log to keep things safe.
  1450. */
  1451. mutex_unlock(&inode->i_mutex);
  1452. if (ret != BTRFS_NO_LOG_SYNC) {
  1453. if (ret > 0) {
  1454. ret = btrfs_commit_transaction(trans, root);
  1455. } else {
  1456. ret = btrfs_sync_log(trans, root);
  1457. if (ret == 0)
  1458. ret = btrfs_end_transaction(trans, root);
  1459. else
  1460. ret = btrfs_commit_transaction(trans, root);
  1461. }
  1462. } else {
  1463. ret = btrfs_end_transaction(trans, root);
  1464. }
  1465. out:
  1466. return ret > 0 ? -EIO : ret;
  1467. }
  1468. static const struct vm_operations_struct btrfs_file_vm_ops = {
  1469. .fault = filemap_fault,
  1470. .page_mkwrite = btrfs_page_mkwrite,
  1471. };
  1472. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1473. {
  1474. struct address_space *mapping = filp->f_mapping;
  1475. if (!mapping->a_ops->readpage)
  1476. return -ENOEXEC;
  1477. file_accessed(filp);
  1478. vma->vm_ops = &btrfs_file_vm_ops;
  1479. vma->vm_flags |= VM_CAN_NONLINEAR;
  1480. return 0;
  1481. }
  1482. static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
  1483. int slot, u64 start, u64 end)
  1484. {
  1485. struct btrfs_file_extent_item *fi;
  1486. struct btrfs_key key;
  1487. if (slot < 0 || slot >= btrfs_header_nritems(leaf))
  1488. return 0;
  1489. btrfs_item_key_to_cpu(leaf, &key, slot);
  1490. if (key.objectid != btrfs_ino(inode) ||
  1491. key.type != BTRFS_EXTENT_DATA_KEY)
  1492. return 0;
  1493. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  1494. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
  1495. return 0;
  1496. if (btrfs_file_extent_disk_bytenr(leaf, fi))
  1497. return 0;
  1498. if (key.offset == end)
  1499. return 1;
  1500. if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
  1501. return 1;
  1502. return 0;
  1503. }
  1504. static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
  1505. struct btrfs_path *path, u64 offset, u64 end)
  1506. {
  1507. struct btrfs_root *root = BTRFS_I(inode)->root;
  1508. struct extent_buffer *leaf;
  1509. struct btrfs_file_extent_item *fi;
  1510. struct extent_map *hole_em;
  1511. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1512. struct btrfs_key key;
  1513. int ret;
  1514. key.objectid = btrfs_ino(inode);
  1515. key.type = BTRFS_EXTENT_DATA_KEY;
  1516. key.offset = offset;
  1517. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1518. if (ret < 0)
  1519. return ret;
  1520. BUG_ON(!ret);
  1521. leaf = path->nodes[0];
  1522. if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
  1523. u64 num_bytes;
  1524. path->slots[0]--;
  1525. fi = btrfs_item_ptr(leaf, path->slots[0],
  1526. struct btrfs_file_extent_item);
  1527. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
  1528. end - offset;
  1529. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1530. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  1531. btrfs_set_file_extent_offset(leaf, fi, 0);
  1532. btrfs_mark_buffer_dirty(leaf);
  1533. goto out;
  1534. }
  1535. if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
  1536. u64 num_bytes;
  1537. path->slots[0]++;
  1538. key.offset = offset;
  1539. btrfs_set_item_key_safe(trans, root, path, &key);
  1540. fi = btrfs_item_ptr(leaf, path->slots[0],
  1541. struct btrfs_file_extent_item);
  1542. num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
  1543. offset;
  1544. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1545. btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
  1546. btrfs_set_file_extent_offset(leaf, fi, 0);
  1547. btrfs_mark_buffer_dirty(leaf);
  1548. goto out;
  1549. }
  1550. btrfs_release_path(path);
  1551. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  1552. 0, 0, end - offset, 0, end - offset,
  1553. 0, 0, 0);
  1554. if (ret)
  1555. return ret;
  1556. out:
  1557. btrfs_release_path(path);
  1558. hole_em = alloc_extent_map();
  1559. if (!hole_em) {
  1560. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  1561. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1562. &BTRFS_I(inode)->runtime_flags);
  1563. } else {
  1564. hole_em->start = offset;
  1565. hole_em->len = end - offset;
  1566. hole_em->orig_start = offset;
  1567. hole_em->block_start = EXTENT_MAP_HOLE;
  1568. hole_em->block_len = 0;
  1569. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  1570. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  1571. hole_em->generation = trans->transid;
  1572. do {
  1573. btrfs_drop_extent_cache(inode, offset, end - 1, 0);
  1574. write_lock(&em_tree->lock);
  1575. ret = add_extent_mapping(em_tree, hole_em);
  1576. if (!ret)
  1577. list_move(&hole_em->list,
  1578. &em_tree->modified_extents);
  1579. write_unlock(&em_tree->lock);
  1580. } while (ret == -EEXIST);
  1581. free_extent_map(hole_em);
  1582. if (ret)
  1583. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  1584. &BTRFS_I(inode)->runtime_flags);
  1585. }
  1586. return 0;
  1587. }
  1588. static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  1589. {
  1590. struct btrfs_root *root = BTRFS_I(inode)->root;
  1591. struct extent_state *cached_state = NULL;
  1592. struct btrfs_path *path;
  1593. struct btrfs_block_rsv *rsv;
  1594. struct btrfs_trans_handle *trans;
  1595. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1596. u64 lockstart = (offset + mask) & ~mask;
  1597. u64 lockend = ((offset + len) & ~mask) - 1;
  1598. u64 cur_offset = lockstart;
  1599. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  1600. u64 drop_end;
  1601. unsigned long nr;
  1602. int ret = 0;
  1603. int err = 0;
  1604. bool same_page = (offset >> PAGE_CACHE_SHIFT) ==
  1605. ((offset + len) >> PAGE_CACHE_SHIFT);
  1606. btrfs_wait_ordered_range(inode, offset, len);
  1607. mutex_lock(&inode->i_mutex);
  1608. if (offset >= inode->i_size) {
  1609. mutex_unlock(&inode->i_mutex);
  1610. return 0;
  1611. }
  1612. /*
  1613. * Only do this if we are in the same page and we aren't doing the
  1614. * entire page.
  1615. */
  1616. if (same_page && len < PAGE_CACHE_SIZE) {
  1617. ret = btrfs_truncate_page(inode, offset, len, 0);
  1618. mutex_unlock(&inode->i_mutex);
  1619. return ret;
  1620. }
  1621. /* zero back part of the first page */
  1622. ret = btrfs_truncate_page(inode, offset, 0, 0);
  1623. if (ret) {
  1624. mutex_unlock(&inode->i_mutex);
  1625. return ret;
  1626. }
  1627. /* zero the front end of the last page */
  1628. ret = btrfs_truncate_page(inode, offset + len, 0, 1);
  1629. if (ret) {
  1630. mutex_unlock(&inode->i_mutex);
  1631. return ret;
  1632. }
  1633. if (lockend < lockstart) {
  1634. mutex_unlock(&inode->i_mutex);
  1635. return 0;
  1636. }
  1637. while (1) {
  1638. struct btrfs_ordered_extent *ordered;
  1639. truncate_pagecache_range(inode, lockstart, lockend);
  1640. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1641. 0, &cached_state);
  1642. ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
  1643. /*
  1644. * We need to make sure we have no ordered extents in this range
  1645. * and nobody raced in and read a page in this range, if we did
  1646. * we need to try again.
  1647. */
  1648. if ((!ordered ||
  1649. (ordered->file_offset + ordered->len < lockstart ||
  1650. ordered->file_offset > lockend)) &&
  1651. !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
  1652. lockend, EXTENT_UPTODATE, 0,
  1653. cached_state)) {
  1654. if (ordered)
  1655. btrfs_put_ordered_extent(ordered);
  1656. break;
  1657. }
  1658. if (ordered)
  1659. btrfs_put_ordered_extent(ordered);
  1660. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
  1661. lockend, &cached_state, GFP_NOFS);
  1662. btrfs_wait_ordered_range(inode, lockstart,
  1663. lockend - lockstart + 1);
  1664. }
  1665. path = btrfs_alloc_path();
  1666. if (!path) {
  1667. ret = -ENOMEM;
  1668. goto out;
  1669. }
  1670. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  1671. if (!rsv) {
  1672. ret = -ENOMEM;
  1673. goto out_free;
  1674. }
  1675. rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
  1676. rsv->failfast = 1;
  1677. /*
  1678. * 1 - update the inode
  1679. * 1 - removing the extents in the range
  1680. * 1 - adding the hole extent
  1681. */
  1682. trans = btrfs_start_transaction(root, 3);
  1683. if (IS_ERR(trans)) {
  1684. err = PTR_ERR(trans);
  1685. goto out_free;
  1686. }
  1687. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  1688. min_size);
  1689. BUG_ON(ret);
  1690. trans->block_rsv = rsv;
  1691. while (cur_offset < lockend) {
  1692. ret = __btrfs_drop_extents(trans, root, inode, path,
  1693. cur_offset, lockend + 1,
  1694. &drop_end, 1);
  1695. if (ret != -ENOSPC)
  1696. break;
  1697. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1698. ret = fill_holes(trans, inode, path, cur_offset, drop_end);
  1699. if (ret) {
  1700. err = ret;
  1701. break;
  1702. }
  1703. cur_offset = drop_end;
  1704. ret = btrfs_update_inode(trans, root, inode);
  1705. if (ret) {
  1706. err = ret;
  1707. break;
  1708. }
  1709. nr = trans->blocks_used;
  1710. btrfs_end_transaction(trans, root);
  1711. btrfs_btree_balance_dirty(root, nr);
  1712. trans = btrfs_start_transaction(root, 3);
  1713. if (IS_ERR(trans)) {
  1714. ret = PTR_ERR(trans);
  1715. trans = NULL;
  1716. break;
  1717. }
  1718. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  1719. rsv, min_size);
  1720. BUG_ON(ret); /* shouldn't happen */
  1721. trans->block_rsv = rsv;
  1722. }
  1723. if (ret) {
  1724. err = ret;
  1725. goto out_trans;
  1726. }
  1727. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1728. ret = fill_holes(trans, inode, path, cur_offset, drop_end);
  1729. if (ret) {
  1730. err = ret;
  1731. goto out_trans;
  1732. }
  1733. out_trans:
  1734. if (!trans)
  1735. goto out_free;
  1736. trans->block_rsv = &root->fs_info->trans_block_rsv;
  1737. ret = btrfs_update_inode(trans, root, inode);
  1738. nr = trans->blocks_used;
  1739. btrfs_end_transaction(trans, root);
  1740. btrfs_btree_balance_dirty(root, nr);
  1741. out_free:
  1742. btrfs_free_path(path);
  1743. btrfs_free_block_rsv(root, rsv);
  1744. out:
  1745. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1746. &cached_state, GFP_NOFS);
  1747. mutex_unlock(&inode->i_mutex);
  1748. if (ret && !err)
  1749. err = ret;
  1750. return err;
  1751. }
  1752. static long btrfs_fallocate(struct file *file, int mode,
  1753. loff_t offset, loff_t len)
  1754. {
  1755. struct inode *inode = file->f_path.dentry->d_inode;
  1756. struct extent_state *cached_state = NULL;
  1757. u64 cur_offset;
  1758. u64 last_byte;
  1759. u64 alloc_start;
  1760. u64 alloc_end;
  1761. u64 alloc_hint = 0;
  1762. u64 locked_end;
  1763. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  1764. struct extent_map *em;
  1765. int ret;
  1766. alloc_start = offset & ~mask;
  1767. alloc_end = (offset + len + mask) & ~mask;
  1768. /* Make sure we aren't being give some crap mode */
  1769. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1770. return -EOPNOTSUPP;
  1771. if (mode & FALLOC_FL_PUNCH_HOLE)
  1772. return btrfs_punch_hole(inode, offset, len);
  1773. /*
  1774. * Make sure we have enough space before we do the
  1775. * allocation.
  1776. */
  1777. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start + 1);
  1778. if (ret)
  1779. return ret;
  1780. /*
  1781. * wait for ordered IO before we have any locks. We'll loop again
  1782. * below with the locks held.
  1783. */
  1784. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  1785. mutex_lock(&inode->i_mutex);
  1786. ret = inode_newsize_ok(inode, alloc_end);
  1787. if (ret)
  1788. goto out;
  1789. if (alloc_start > inode->i_size) {
  1790. ret = btrfs_cont_expand(inode, i_size_read(inode),
  1791. alloc_start);
  1792. if (ret)
  1793. goto out;
  1794. }
  1795. locked_end = alloc_end - 1;
  1796. while (1) {
  1797. struct btrfs_ordered_extent *ordered;
  1798. /* the extent lock is ordered inside the running
  1799. * transaction
  1800. */
  1801. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  1802. locked_end, 0, &cached_state);
  1803. ordered = btrfs_lookup_first_ordered_extent(inode,
  1804. alloc_end - 1);
  1805. if (ordered &&
  1806. ordered->file_offset + ordered->len > alloc_start &&
  1807. ordered->file_offset < alloc_end) {
  1808. btrfs_put_ordered_extent(ordered);
  1809. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1810. alloc_start, locked_end,
  1811. &cached_state, GFP_NOFS);
  1812. /*
  1813. * we can't wait on the range with the transaction
  1814. * running or with the extent lock held
  1815. */
  1816. btrfs_wait_ordered_range(inode, alloc_start,
  1817. alloc_end - alloc_start);
  1818. } else {
  1819. if (ordered)
  1820. btrfs_put_ordered_extent(ordered);
  1821. break;
  1822. }
  1823. }
  1824. cur_offset = alloc_start;
  1825. while (1) {
  1826. u64 actual_end;
  1827. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  1828. alloc_end - cur_offset, 0);
  1829. if (IS_ERR_OR_NULL(em)) {
  1830. if (!em)
  1831. ret = -ENOMEM;
  1832. else
  1833. ret = PTR_ERR(em);
  1834. break;
  1835. }
  1836. last_byte = min(extent_map_end(em), alloc_end);
  1837. actual_end = min_t(u64, extent_map_end(em), offset + len);
  1838. last_byte = (last_byte + mask) & ~mask;
  1839. if (em->block_start == EXTENT_MAP_HOLE ||
  1840. (cur_offset >= inode->i_size &&
  1841. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  1842. ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
  1843. last_byte - cur_offset,
  1844. 1 << inode->i_blkbits,
  1845. offset + len,
  1846. &alloc_hint);
  1847. if (ret < 0) {
  1848. free_extent_map(em);
  1849. break;
  1850. }
  1851. } else if (actual_end > inode->i_size &&
  1852. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1853. /*
  1854. * We didn't need to allocate any more space, but we
  1855. * still extended the size of the file so we need to
  1856. * update i_size.
  1857. */
  1858. inode->i_ctime = CURRENT_TIME;
  1859. i_size_write(inode, actual_end);
  1860. btrfs_ordered_update_i_size(inode, actual_end, NULL);
  1861. }
  1862. free_extent_map(em);
  1863. cur_offset = last_byte;
  1864. if (cur_offset >= alloc_end) {
  1865. ret = 0;
  1866. break;
  1867. }
  1868. }
  1869. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  1870. &cached_state, GFP_NOFS);
  1871. out:
  1872. mutex_unlock(&inode->i_mutex);
  1873. /* Let go of our reservation. */
  1874. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start + 1);
  1875. return ret;
  1876. }
  1877. static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
  1878. {
  1879. struct btrfs_root *root = BTRFS_I(inode)->root;
  1880. struct extent_map *em;
  1881. struct extent_state *cached_state = NULL;
  1882. u64 lockstart = *offset;
  1883. u64 lockend = i_size_read(inode);
  1884. u64 start = *offset;
  1885. u64 orig_start = *offset;
  1886. u64 len = i_size_read(inode);
  1887. u64 last_end = 0;
  1888. int ret = 0;
  1889. lockend = max_t(u64, root->sectorsize, lockend);
  1890. if (lockend <= lockstart)
  1891. lockend = lockstart + root->sectorsize;
  1892. len = lockend - lockstart + 1;
  1893. len = max_t(u64, len, root->sectorsize);
  1894. if (inode->i_size == 0)
  1895. return -ENXIO;
  1896. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
  1897. &cached_state);
  1898. /*
  1899. * Delalloc is such a pain. If we have a hole and we have pending
  1900. * delalloc for a portion of the hole we will get back a hole that
  1901. * exists for the entire range since it hasn't been actually written
  1902. * yet. So to take care of this case we need to look for an extent just
  1903. * before the position we want in case there is outstanding delalloc
  1904. * going on here.
  1905. */
  1906. if (origin == SEEK_HOLE && start != 0) {
  1907. if (start <= root->sectorsize)
  1908. em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
  1909. root->sectorsize, 0);
  1910. else
  1911. em = btrfs_get_extent_fiemap(inode, NULL, 0,
  1912. start - root->sectorsize,
  1913. root->sectorsize, 0);
  1914. if (IS_ERR(em)) {
  1915. ret = PTR_ERR(em);
  1916. goto out;
  1917. }
  1918. last_end = em->start + em->len;
  1919. if (em->block_start == EXTENT_MAP_DELALLOC)
  1920. last_end = min_t(u64, last_end, inode->i_size);
  1921. free_extent_map(em);
  1922. }
  1923. while (1) {
  1924. em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
  1925. if (IS_ERR(em)) {
  1926. ret = PTR_ERR(em);
  1927. break;
  1928. }
  1929. if (em->block_start == EXTENT_MAP_HOLE) {
  1930. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1931. if (last_end <= orig_start) {
  1932. free_extent_map(em);
  1933. ret = -ENXIO;
  1934. break;
  1935. }
  1936. }
  1937. if (origin == SEEK_HOLE) {
  1938. *offset = start;
  1939. free_extent_map(em);
  1940. break;
  1941. }
  1942. } else {
  1943. if (origin == SEEK_DATA) {
  1944. if (em->block_start == EXTENT_MAP_DELALLOC) {
  1945. if (start >= inode->i_size) {
  1946. free_extent_map(em);
  1947. ret = -ENXIO;
  1948. break;
  1949. }
  1950. }
  1951. *offset = start;
  1952. free_extent_map(em);
  1953. break;
  1954. }
  1955. }
  1956. start = em->start + em->len;
  1957. last_end = em->start + em->len;
  1958. if (em->block_start == EXTENT_MAP_DELALLOC)
  1959. last_end = min_t(u64, last_end, inode->i_size);
  1960. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  1961. free_extent_map(em);
  1962. ret = -ENXIO;
  1963. break;
  1964. }
  1965. free_extent_map(em);
  1966. cond_resched();
  1967. }
  1968. if (!ret)
  1969. *offset = min(*offset, inode->i_size);
  1970. out:
  1971. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  1972. &cached_state, GFP_NOFS);
  1973. return ret;
  1974. }
  1975. static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
  1976. {
  1977. struct inode *inode = file->f_mapping->host;
  1978. int ret;
  1979. mutex_lock(&inode->i_mutex);
  1980. switch (origin) {
  1981. case SEEK_END:
  1982. case SEEK_CUR:
  1983. offset = generic_file_llseek(file, offset, origin);
  1984. goto out;
  1985. case SEEK_DATA:
  1986. case SEEK_HOLE:
  1987. if (offset >= i_size_read(inode)) {
  1988. mutex_unlock(&inode->i_mutex);
  1989. return -ENXIO;
  1990. }
  1991. ret = find_desired_extent(inode, &offset, origin);
  1992. if (ret) {
  1993. mutex_unlock(&inode->i_mutex);
  1994. return ret;
  1995. }
  1996. }
  1997. if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
  1998. offset = -EINVAL;
  1999. goto out;
  2000. }
  2001. if (offset > inode->i_sb->s_maxbytes) {
  2002. offset = -EINVAL;
  2003. goto out;
  2004. }
  2005. /* Special lock needed here? */
  2006. if (offset != file->f_pos) {
  2007. file->f_pos = offset;
  2008. file->f_version = 0;
  2009. }
  2010. out:
  2011. mutex_unlock(&inode->i_mutex);
  2012. return offset;
  2013. }
  2014. const struct file_operations btrfs_file_operations = {
  2015. .llseek = btrfs_file_llseek,
  2016. .read = do_sync_read,
  2017. .write = do_sync_write,
  2018. .aio_read = generic_file_aio_read,
  2019. .splice_read = generic_file_splice_read,
  2020. .aio_write = btrfs_file_aio_write,
  2021. .mmap = btrfs_file_mmap,
  2022. .open = generic_file_open,
  2023. .release = btrfs_release_file,
  2024. .fsync = btrfs_sync_file,
  2025. .fallocate = btrfs_fallocate,
  2026. .unlocked_ioctl = btrfs_ioctl,
  2027. #ifdef CONFIG_COMPAT
  2028. .compat_ioctl = btrfs_ioctl,
  2029. #endif
  2030. };