file.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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/smp_lock.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mpage.h>
  27. #include <linux/swap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/statfs.h>
  30. #include <linux/compat.h>
  31. #include <linux/version.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. /* simple helper to fault in pages and copy. This should go away
  42. * and be replaced with calls into generic code.
  43. */
  44. static int noinline btrfs_copy_from_user(loff_t pos, int num_pages,
  45. int write_bytes,
  46. struct page **prepared_pages,
  47. const char __user * buf)
  48. {
  49. long page_fault = 0;
  50. int i;
  51. int offset = pos & (PAGE_CACHE_SIZE - 1);
  52. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  53. size_t count = min_t(size_t,
  54. PAGE_CACHE_SIZE - offset, write_bytes);
  55. struct page *page = prepared_pages[i];
  56. fault_in_pages_readable(buf, count);
  57. /* Copy data from userspace to the current page */
  58. kmap(page);
  59. page_fault = __copy_from_user(page_address(page) + offset,
  60. buf, count);
  61. /* Flush processor's dcache for this page */
  62. flush_dcache_page(page);
  63. kunmap(page);
  64. buf += count;
  65. write_bytes -= count;
  66. if (page_fault)
  67. break;
  68. }
  69. return page_fault ? -EFAULT : 0;
  70. }
  71. /*
  72. * unlocks pages after btrfs_file_write is done with them
  73. */
  74. static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages)
  75. {
  76. size_t i;
  77. for (i = 0; i < num_pages; i++) {
  78. if (!pages[i])
  79. break;
  80. /* page checked is some magic around finding pages that
  81. * have been modified without going through btrfs_set_page_dirty
  82. * clear it here
  83. */
  84. ClearPageChecked(pages[i]);
  85. unlock_page(pages[i]);
  86. mark_page_accessed(pages[i]);
  87. page_cache_release(pages[i]);
  88. }
  89. }
  90. /* this does all the hard work for inserting an inline extent into
  91. * the btree. Any existing inline extent is extended as required to make room,
  92. * otherwise things are inserted as required into the btree
  93. */
  94. static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
  95. struct btrfs_root *root, struct inode *inode,
  96. u64 offset, size_t size,
  97. struct page **pages, size_t page_offset,
  98. int num_pages)
  99. {
  100. struct btrfs_key key;
  101. struct btrfs_path *path;
  102. struct extent_buffer *leaf;
  103. char *kaddr;
  104. unsigned long ptr;
  105. struct btrfs_file_extent_item *ei;
  106. struct page *page;
  107. u32 datasize;
  108. int err = 0;
  109. int ret;
  110. int i;
  111. ssize_t cur_size;
  112. path = btrfs_alloc_path();
  113. if (!path)
  114. return -ENOMEM;
  115. btrfs_set_trans_block_group(trans, inode);
  116. key.objectid = inode->i_ino;
  117. key.offset = offset;
  118. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  119. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  120. if (ret < 0) {
  121. err = ret;
  122. goto fail;
  123. }
  124. if (ret == 1) {
  125. struct btrfs_key found_key;
  126. if (path->slots[0] == 0)
  127. goto insert;
  128. path->slots[0]--;
  129. leaf = path->nodes[0];
  130. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  131. if (found_key.objectid != inode->i_ino)
  132. goto insert;
  133. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  134. goto insert;
  135. ei = btrfs_item_ptr(leaf, path->slots[0],
  136. struct btrfs_file_extent_item);
  137. if (btrfs_file_extent_type(leaf, ei) !=
  138. BTRFS_FILE_EXTENT_INLINE) {
  139. goto insert;
  140. }
  141. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  142. ret = 0;
  143. }
  144. if (ret == 0) {
  145. u32 found_size;
  146. u64 found_end;
  147. leaf = path->nodes[0];
  148. ei = btrfs_item_ptr(leaf, path->slots[0],
  149. struct btrfs_file_extent_item);
  150. if (btrfs_file_extent_type(leaf, ei) !=
  151. BTRFS_FILE_EXTENT_INLINE) {
  152. err = ret;
  153. btrfs_print_leaf(root, leaf);
  154. printk("found wasn't inline offset %Lu inode %lu\n",
  155. offset, inode->i_ino);
  156. goto fail;
  157. }
  158. found_size = btrfs_file_extent_inline_len(leaf,
  159. btrfs_item_nr(leaf, path->slots[0]));
  160. found_end = key.offset + found_size;
  161. if (found_end < offset + size) {
  162. btrfs_release_path(root, path);
  163. ret = btrfs_search_slot(trans, root, &key, path,
  164. offset + size - found_end, 1);
  165. BUG_ON(ret != 0);
  166. ret = btrfs_extend_item(trans, root, path,
  167. offset + size - found_end);
  168. if (ret) {
  169. err = ret;
  170. goto fail;
  171. }
  172. leaf = path->nodes[0];
  173. ei = btrfs_item_ptr(leaf, path->slots[0],
  174. struct btrfs_file_extent_item);
  175. inode_add_bytes(inode, offset + size - found_end);
  176. }
  177. if (found_end < offset) {
  178. ptr = btrfs_file_extent_inline_start(ei) + found_size;
  179. memset_extent_buffer(leaf, 0, ptr, offset - found_end);
  180. }
  181. } else {
  182. insert:
  183. btrfs_release_path(root, path);
  184. datasize = offset + size - key.offset;
  185. inode_add_bytes(inode, datasize);
  186. datasize = btrfs_file_extent_calc_inline_size(datasize);
  187. ret = btrfs_insert_empty_item(trans, root, path, &key,
  188. datasize);
  189. if (ret) {
  190. err = ret;
  191. printk("got bad ret %d\n", ret);
  192. goto fail;
  193. }
  194. leaf = path->nodes[0];
  195. ei = btrfs_item_ptr(leaf, path->slots[0],
  196. struct btrfs_file_extent_item);
  197. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  198. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  199. }
  200. ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
  201. cur_size = size;
  202. i = 0;
  203. while (size > 0) {
  204. page = pages[i];
  205. kaddr = kmap_atomic(page, KM_USER0);
  206. cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
  207. write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
  208. kunmap_atomic(kaddr, KM_USER0);
  209. page_offset = 0;
  210. ptr += cur_size;
  211. size -= cur_size;
  212. if (i >= num_pages) {
  213. printk("i %d num_pages %d\n", i, num_pages);
  214. }
  215. i++;
  216. }
  217. btrfs_mark_buffer_dirty(leaf);
  218. fail:
  219. btrfs_free_path(path);
  220. return err;
  221. }
  222. /*
  223. * after copy_from_user, pages need to be dirtied and we need to make
  224. * sure holes are created between the current EOF and the start of
  225. * any next extents (if required).
  226. *
  227. * this also makes the decision about creating an inline extent vs
  228. * doing real data extents, marking pages dirty and delalloc as required.
  229. */
  230. static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
  231. struct btrfs_root *root,
  232. struct file *file,
  233. struct page **pages,
  234. size_t num_pages,
  235. loff_t pos,
  236. size_t write_bytes)
  237. {
  238. int err = 0;
  239. int i;
  240. struct inode *inode = fdentry(file)->d_inode;
  241. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  242. u64 hint_byte;
  243. u64 num_bytes;
  244. u64 start_pos;
  245. u64 end_of_last_block;
  246. u64 end_pos = pos + write_bytes;
  247. u64 inline_size;
  248. int did_inline = 0;
  249. loff_t isize = i_size_read(inode);
  250. start_pos = pos & ~((u64)root->sectorsize - 1);
  251. num_bytes = (write_bytes + pos - start_pos +
  252. root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
  253. end_of_last_block = start_pos + num_bytes - 1;
  254. lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  255. trans = btrfs_join_transaction(root, 1);
  256. if (!trans) {
  257. err = -ENOMEM;
  258. goto out_unlock;
  259. }
  260. btrfs_set_trans_block_group(trans, inode);
  261. hint_byte = 0;
  262. if ((end_of_last_block & 4095) == 0) {
  263. printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
  264. }
  265. set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  266. /* FIXME...EIEIO, ENOSPC and more */
  267. /* insert any holes we need to create */
  268. if (isize < start_pos) {
  269. u64 last_pos_in_file;
  270. u64 hole_size;
  271. u64 mask = root->sectorsize - 1;
  272. last_pos_in_file = (isize + mask) & ~mask;
  273. hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
  274. if (hole_size > 0) {
  275. btrfs_wait_ordered_range(inode, last_pos_in_file,
  276. last_pos_in_file + hole_size);
  277. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  278. err = btrfs_drop_extents(trans, root, inode,
  279. last_pos_in_file,
  280. last_pos_in_file + hole_size,
  281. last_pos_in_file,
  282. &hint_byte);
  283. if (err)
  284. goto failed;
  285. err = btrfs_insert_file_extent(trans, root,
  286. inode->i_ino,
  287. last_pos_in_file,
  288. 0, 0, hole_size, 0);
  289. btrfs_drop_extent_cache(inode, last_pos_in_file,
  290. last_pos_in_file + hole_size - 1, 0);
  291. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  292. btrfs_check_file(root, inode);
  293. }
  294. if (err)
  295. goto failed;
  296. }
  297. /*
  298. * either allocate an extent for the new bytes or setup the key
  299. * to show we are doing inline data in the extent
  300. */
  301. inline_size = end_pos;
  302. if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  303. inline_size > root->fs_info->max_inline ||
  304. (inline_size & (root->sectorsize -1)) == 0 ||
  305. inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  306. /* check for reserved extents on each page, we don't want
  307. * to reset the delalloc bit on things that already have
  308. * extents reserved.
  309. */
  310. btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
  311. for (i = 0; i < num_pages; i++) {
  312. struct page *p = pages[i];
  313. SetPageUptodate(p);
  314. ClearPageChecked(p);
  315. set_page_dirty(p);
  316. }
  317. } else {
  318. u64 aligned_end;
  319. /* step one, delete the existing extents in this range */
  320. aligned_end = (pos + write_bytes + root->sectorsize - 1) &
  321. ~((u64)root->sectorsize - 1);
  322. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  323. err = btrfs_drop_extents(trans, root, inode, start_pos,
  324. aligned_end, aligned_end, &hint_byte);
  325. if (err)
  326. goto failed;
  327. if (isize > inline_size)
  328. inline_size = min_t(u64, isize, aligned_end);
  329. inline_size -= start_pos;
  330. err = insert_inline_extent(trans, root, inode, start_pos,
  331. inline_size, pages, 0, num_pages);
  332. btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1, 0);
  333. BUG_ON(err);
  334. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  335. /*
  336. * an ugly way to do all the prop accounting around
  337. * the page bits and mapping tags
  338. */
  339. set_page_writeback(pages[0]);
  340. end_page_writeback(pages[0]);
  341. did_inline = 1;
  342. }
  343. if (end_pos > isize) {
  344. i_size_write(inode, end_pos);
  345. if (did_inline)
  346. BTRFS_I(inode)->disk_i_size = end_pos;
  347. btrfs_update_inode(trans, root, inode);
  348. }
  349. failed:
  350. err = btrfs_end_transaction(trans, root);
  351. out_unlock:
  352. unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
  353. return err;
  354. }
  355. /*
  356. * this drops all the extents in the cache that intersect the range
  357. * [start, end]. Existing extents are split as required.
  358. */
  359. int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
  360. int skip_pinned)
  361. {
  362. struct extent_map *em;
  363. struct extent_map *split = NULL;
  364. struct extent_map *split2 = NULL;
  365. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  366. u64 len = end - start + 1;
  367. int ret;
  368. int testend = 1;
  369. unsigned long flags;
  370. WARN_ON(end < start);
  371. if (end == (u64)-1) {
  372. len = (u64)-1;
  373. testend = 0;
  374. }
  375. while(1) {
  376. if (!split)
  377. split = alloc_extent_map(GFP_NOFS);
  378. if (!split2)
  379. split2 = alloc_extent_map(GFP_NOFS);
  380. spin_lock(&em_tree->lock);
  381. em = lookup_extent_mapping(em_tree, start, len);
  382. if (!em) {
  383. spin_unlock(&em_tree->lock);
  384. break;
  385. }
  386. flags = em->flags;
  387. if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
  388. spin_unlock(&em_tree->lock);
  389. if (em->start <= start &&
  390. (!testend || em->start + em->len >= start + len)) {
  391. free_extent_map(em);
  392. break;
  393. }
  394. if (start < em->start) {
  395. len = em->start - start;
  396. } else {
  397. len = start + len - (em->start + em->len);
  398. start = em->start + em->len;
  399. }
  400. free_extent_map(em);
  401. continue;
  402. }
  403. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  404. remove_extent_mapping(em_tree, em);
  405. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  406. em->start < start) {
  407. split->start = em->start;
  408. split->len = start - em->start;
  409. split->block_start = em->block_start;
  410. split->bdev = em->bdev;
  411. split->flags = flags;
  412. ret = add_extent_mapping(em_tree, split);
  413. BUG_ON(ret);
  414. free_extent_map(split);
  415. split = split2;
  416. split2 = NULL;
  417. }
  418. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  419. testend && em->start + em->len > start + len) {
  420. u64 diff = start + len - em->start;
  421. split->start = start + len;
  422. split->len = em->start + em->len - (start + len);
  423. split->bdev = em->bdev;
  424. split->flags = flags;
  425. split->block_start = em->block_start + diff;
  426. ret = add_extent_mapping(em_tree, split);
  427. BUG_ON(ret);
  428. free_extent_map(split);
  429. split = NULL;
  430. }
  431. spin_unlock(&em_tree->lock);
  432. /* once for us */
  433. free_extent_map(em);
  434. /* once for the tree*/
  435. free_extent_map(em);
  436. }
  437. if (split)
  438. free_extent_map(split);
  439. if (split2)
  440. free_extent_map(split2);
  441. return 0;
  442. }
  443. int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
  444. {
  445. return 0;
  446. #if 0
  447. struct btrfs_path *path;
  448. struct btrfs_key found_key;
  449. struct extent_buffer *leaf;
  450. struct btrfs_file_extent_item *extent;
  451. u64 last_offset = 0;
  452. int nritems;
  453. int slot;
  454. int found_type;
  455. int ret;
  456. int err = 0;
  457. u64 extent_end = 0;
  458. path = btrfs_alloc_path();
  459. ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
  460. last_offset, 0);
  461. while(1) {
  462. nritems = btrfs_header_nritems(path->nodes[0]);
  463. if (path->slots[0] >= nritems) {
  464. ret = btrfs_next_leaf(root, path);
  465. if (ret)
  466. goto out;
  467. nritems = btrfs_header_nritems(path->nodes[0]);
  468. }
  469. slot = path->slots[0];
  470. leaf = path->nodes[0];
  471. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  472. if (found_key.objectid != inode->i_ino)
  473. break;
  474. if (found_key.type != BTRFS_EXTENT_DATA_KEY)
  475. goto out;
  476. if (found_key.offset < last_offset) {
  477. WARN_ON(1);
  478. btrfs_print_leaf(root, leaf);
  479. printk("inode %lu found offset %Lu expected %Lu\n",
  480. inode->i_ino, found_key.offset, last_offset);
  481. err = 1;
  482. goto out;
  483. }
  484. extent = btrfs_item_ptr(leaf, slot,
  485. struct btrfs_file_extent_item);
  486. found_type = btrfs_file_extent_type(leaf, extent);
  487. if (found_type == BTRFS_FILE_EXTENT_REG) {
  488. extent_end = found_key.offset +
  489. btrfs_file_extent_num_bytes(leaf, extent);
  490. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  491. struct btrfs_item *item;
  492. item = btrfs_item_nr(leaf, slot);
  493. extent_end = found_key.offset +
  494. btrfs_file_extent_inline_len(leaf, item);
  495. extent_end = (extent_end + root->sectorsize - 1) &
  496. ~((u64)root->sectorsize -1 );
  497. }
  498. last_offset = extent_end;
  499. path->slots[0]++;
  500. }
  501. if (0 && last_offset < inode->i_size) {
  502. WARN_ON(1);
  503. btrfs_print_leaf(root, leaf);
  504. printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
  505. last_offset, inode->i_size);
  506. err = 1;
  507. }
  508. out:
  509. btrfs_free_path(path);
  510. return err;
  511. #endif
  512. }
  513. /*
  514. * this is very complex, but the basic idea is to drop all extents
  515. * in the range start - end. hint_block is filled in with a block number
  516. * that would be a good hint to the block allocator for this file.
  517. *
  518. * If an extent intersects the range but is not entirely inside the range
  519. * it is either truncated or split. Anything entirely inside the range
  520. * is deleted from the tree.
  521. *
  522. * inline_limit is used to tell this code which offsets in the file to keep
  523. * if they contain inline extents.
  524. */
  525. int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
  526. struct btrfs_root *root, struct inode *inode,
  527. u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
  528. {
  529. u64 extent_end = 0;
  530. u64 search_start = start;
  531. u64 leaf_start;
  532. u64 root_gen;
  533. u64 root_owner;
  534. struct extent_buffer *leaf;
  535. struct btrfs_file_extent_item *extent;
  536. struct btrfs_path *path;
  537. struct btrfs_key key;
  538. struct btrfs_file_extent_item old;
  539. int keep;
  540. int slot;
  541. int bookend;
  542. int found_type;
  543. int found_extent;
  544. int found_inline;
  545. int recow;
  546. int ret;
  547. btrfs_drop_extent_cache(inode, start, end - 1, 0);
  548. path = btrfs_alloc_path();
  549. if (!path)
  550. return -ENOMEM;
  551. while(1) {
  552. recow = 0;
  553. btrfs_release_path(root, path);
  554. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  555. search_start, -1);
  556. if (ret < 0)
  557. goto out;
  558. if (ret > 0) {
  559. if (path->slots[0] == 0) {
  560. ret = 0;
  561. goto out;
  562. }
  563. path->slots[0]--;
  564. }
  565. next_slot:
  566. keep = 0;
  567. bookend = 0;
  568. found_extent = 0;
  569. found_inline = 0;
  570. leaf_start = 0;
  571. root_gen = 0;
  572. root_owner = 0;
  573. extent = NULL;
  574. leaf = path->nodes[0];
  575. slot = path->slots[0];
  576. ret = 0;
  577. btrfs_item_key_to_cpu(leaf, &key, slot);
  578. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
  579. key.offset >= end) {
  580. goto out;
  581. }
  582. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  583. key.objectid != inode->i_ino) {
  584. goto out;
  585. }
  586. if (recow) {
  587. search_start = key.offset;
  588. continue;
  589. }
  590. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  591. extent = btrfs_item_ptr(leaf, slot,
  592. struct btrfs_file_extent_item);
  593. found_type = btrfs_file_extent_type(leaf, extent);
  594. if (found_type == BTRFS_FILE_EXTENT_REG) {
  595. extent_end =
  596. btrfs_file_extent_disk_bytenr(leaf,
  597. extent);
  598. if (extent_end)
  599. *hint_byte = extent_end;
  600. extent_end = key.offset +
  601. btrfs_file_extent_num_bytes(leaf, extent);
  602. found_extent = 1;
  603. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  604. struct btrfs_item *item;
  605. item = btrfs_item_nr(leaf, slot);
  606. found_inline = 1;
  607. extent_end = key.offset +
  608. btrfs_file_extent_inline_len(leaf, item);
  609. }
  610. } else {
  611. extent_end = search_start;
  612. }
  613. /* we found nothing we can drop */
  614. if ((!found_extent && !found_inline) ||
  615. search_start >= extent_end) {
  616. int nextret;
  617. u32 nritems;
  618. nritems = btrfs_header_nritems(leaf);
  619. if (slot >= nritems - 1) {
  620. nextret = btrfs_next_leaf(root, path);
  621. if (nextret)
  622. goto out;
  623. recow = 1;
  624. } else {
  625. path->slots[0]++;
  626. }
  627. goto next_slot;
  628. }
  629. if (found_inline) {
  630. u64 mask = root->sectorsize - 1;
  631. search_start = (extent_end + mask) & ~mask;
  632. } else
  633. search_start = extent_end;
  634. if (end <= extent_end && start >= key.offset && found_inline) {
  635. *hint_byte = EXTENT_MAP_INLINE;
  636. goto out;
  637. }
  638. if (found_extent) {
  639. read_extent_buffer(leaf, &old, (unsigned long)extent,
  640. sizeof(old));
  641. root_gen = btrfs_header_generation(leaf);
  642. root_owner = btrfs_header_owner(leaf);
  643. leaf_start = leaf->start;
  644. }
  645. if (end < extent_end && end >= key.offset) {
  646. bookend = 1;
  647. if (found_inline && start <= key.offset)
  648. keep = 1;
  649. }
  650. /* truncate existing extent */
  651. if (start > key.offset) {
  652. u64 new_num;
  653. u64 old_num;
  654. keep = 1;
  655. WARN_ON(start & (root->sectorsize - 1));
  656. if (found_extent) {
  657. new_num = start - key.offset;
  658. old_num = btrfs_file_extent_num_bytes(leaf,
  659. extent);
  660. *hint_byte =
  661. btrfs_file_extent_disk_bytenr(leaf,
  662. extent);
  663. if (btrfs_file_extent_disk_bytenr(leaf,
  664. extent)) {
  665. inode_sub_bytes(inode, old_num -
  666. new_num);
  667. }
  668. btrfs_set_file_extent_num_bytes(leaf, extent,
  669. new_num);
  670. btrfs_mark_buffer_dirty(leaf);
  671. } else if (key.offset < inline_limit &&
  672. (end > extent_end) &&
  673. (inline_limit < extent_end)) {
  674. u32 new_size;
  675. new_size = btrfs_file_extent_calc_inline_size(
  676. inline_limit - key.offset);
  677. inode_sub_bytes(inode, extent_end -
  678. inline_limit);
  679. btrfs_truncate_item(trans, root, path,
  680. new_size, 1);
  681. }
  682. }
  683. /* delete the entire extent */
  684. if (!keep) {
  685. if (found_inline)
  686. inode_sub_bytes(inode, extent_end -
  687. key.offset);
  688. ret = btrfs_del_item(trans, root, path);
  689. /* TODO update progress marker and return */
  690. BUG_ON(ret);
  691. extent = NULL;
  692. btrfs_release_path(root, path);
  693. /* the extent will be freed later */
  694. }
  695. if (bookend && found_inline && start <= key.offset) {
  696. u32 new_size;
  697. new_size = btrfs_file_extent_calc_inline_size(
  698. extent_end - end);
  699. inode_sub_bytes(inode, end - key.offset);
  700. ret = btrfs_truncate_item(trans, root, path,
  701. new_size, 0);
  702. BUG_ON(ret);
  703. }
  704. /* create bookend, splitting the extent in two */
  705. if (bookend && found_extent) {
  706. u64 disk_bytenr;
  707. struct btrfs_key ins;
  708. ins.objectid = inode->i_ino;
  709. ins.offset = end;
  710. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  711. btrfs_release_path(root, path);
  712. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  713. sizeof(*extent));
  714. BUG_ON(ret);
  715. leaf = path->nodes[0];
  716. extent = btrfs_item_ptr(leaf, path->slots[0],
  717. struct btrfs_file_extent_item);
  718. write_extent_buffer(leaf, &old,
  719. (unsigned long)extent, sizeof(old));
  720. btrfs_set_file_extent_offset(leaf, extent,
  721. le64_to_cpu(old.offset) + end - key.offset);
  722. WARN_ON(le64_to_cpu(old.num_bytes) <
  723. (extent_end - end));
  724. btrfs_set_file_extent_num_bytes(leaf, extent,
  725. extent_end - end);
  726. btrfs_set_file_extent_type(leaf, extent,
  727. BTRFS_FILE_EXTENT_REG);
  728. btrfs_mark_buffer_dirty(path->nodes[0]);
  729. disk_bytenr = le64_to_cpu(old.disk_bytenr);
  730. if (disk_bytenr != 0) {
  731. ret = btrfs_inc_extent_ref(trans, root,
  732. disk_bytenr,
  733. le64_to_cpu(old.disk_num_bytes),
  734. leaf->start,
  735. root->root_key.objectid,
  736. trans->transid,
  737. ins.objectid, ins.offset);
  738. BUG_ON(ret);
  739. }
  740. btrfs_release_path(root, path);
  741. if (disk_bytenr != 0) {
  742. inode_add_bytes(inode, extent_end - end);
  743. }
  744. }
  745. if (found_extent && !keep) {
  746. u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
  747. if (disk_bytenr != 0) {
  748. inode_sub_bytes(inode,
  749. le64_to_cpu(old.num_bytes));
  750. ret = btrfs_free_extent(trans, root,
  751. disk_bytenr,
  752. le64_to_cpu(old.disk_num_bytes),
  753. leaf_start, root_owner,
  754. root_gen, key.objectid,
  755. key.offset, 0);
  756. BUG_ON(ret);
  757. *hint_byte = disk_bytenr;
  758. }
  759. }
  760. if (search_start >= end) {
  761. ret = 0;
  762. goto out;
  763. }
  764. }
  765. out:
  766. btrfs_free_path(path);
  767. btrfs_check_file(root, inode);
  768. return ret;
  769. }
  770. /*
  771. * this gets pages into the page cache and locks them down, it also properly
  772. * waits for data=ordered extents to finish before allowing the pages to be
  773. * modified.
  774. */
  775. static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
  776. struct page **pages, size_t num_pages,
  777. loff_t pos, unsigned long first_index,
  778. unsigned long last_index, size_t write_bytes)
  779. {
  780. int i;
  781. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  782. struct inode *inode = fdentry(file)->d_inode;
  783. int err = 0;
  784. u64 start_pos;
  785. u64 last_pos;
  786. start_pos = pos & ~((u64)root->sectorsize - 1);
  787. last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
  788. memset(pages, 0, num_pages * sizeof(struct page *));
  789. again:
  790. for (i = 0; i < num_pages; i++) {
  791. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  792. if (!pages[i]) {
  793. err = -ENOMEM;
  794. BUG_ON(1);
  795. }
  796. wait_on_page_writeback(pages[i]);
  797. }
  798. if (start_pos < inode->i_size) {
  799. struct btrfs_ordered_extent *ordered;
  800. lock_extent(&BTRFS_I(inode)->io_tree,
  801. start_pos, last_pos - 1, GFP_NOFS);
  802. ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
  803. if (ordered &&
  804. ordered->file_offset + ordered->len > start_pos &&
  805. ordered->file_offset < last_pos) {
  806. btrfs_put_ordered_extent(ordered);
  807. unlock_extent(&BTRFS_I(inode)->io_tree,
  808. start_pos, last_pos - 1, GFP_NOFS);
  809. for (i = 0; i < num_pages; i++) {
  810. unlock_page(pages[i]);
  811. page_cache_release(pages[i]);
  812. }
  813. btrfs_wait_ordered_range(inode, start_pos,
  814. last_pos - start_pos);
  815. goto again;
  816. }
  817. if (ordered)
  818. btrfs_put_ordered_extent(ordered);
  819. clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
  820. last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
  821. GFP_NOFS);
  822. unlock_extent(&BTRFS_I(inode)->io_tree,
  823. start_pos, last_pos - 1, GFP_NOFS);
  824. }
  825. for (i = 0; i < num_pages; i++) {
  826. clear_page_dirty_for_io(pages[i]);
  827. set_page_extent_mapped(pages[i]);
  828. WARN_ON(!PageLocked(pages[i]));
  829. }
  830. return 0;
  831. }
  832. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  833. size_t count, loff_t *ppos)
  834. {
  835. loff_t pos;
  836. loff_t start_pos;
  837. ssize_t num_written = 0;
  838. ssize_t err = 0;
  839. int ret = 0;
  840. struct inode *inode = fdentry(file)->d_inode;
  841. struct btrfs_root *root = BTRFS_I(inode)->root;
  842. struct page **pages = NULL;
  843. int nrptrs;
  844. struct page *pinned[2];
  845. unsigned long first_index;
  846. unsigned long last_index;
  847. int will_write;
  848. will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
  849. (file->f_flags & O_DIRECT));
  850. nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
  851. PAGE_CACHE_SIZE / (sizeof(struct page *)));
  852. pinned[0] = NULL;
  853. pinned[1] = NULL;
  854. pos = *ppos;
  855. start_pos = pos;
  856. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  857. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  858. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  859. if (err)
  860. goto out_nolock;
  861. if (count == 0)
  862. goto out_nolock;
  863. err = file_remove_suid(file);
  864. if (err)
  865. goto out_nolock;
  866. file_update_time(file);
  867. pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
  868. mutex_lock(&inode->i_mutex);
  869. first_index = pos >> PAGE_CACHE_SHIFT;
  870. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  871. /*
  872. * if this is a nodatasum mount, force summing off for the inode
  873. * all the time. That way a later mount with summing on won't
  874. * get confused
  875. */
  876. if (btrfs_test_opt(root, NODATASUM))
  877. btrfs_set_flag(inode, NODATASUM);
  878. /*
  879. * there are lots of better ways to do this, but this code
  880. * makes sure the first and last page in the file range are
  881. * up to date and ready for cow
  882. */
  883. if ((pos & (PAGE_CACHE_SIZE - 1))) {
  884. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  885. if (!PageUptodate(pinned[0])) {
  886. ret = btrfs_readpage(NULL, pinned[0]);
  887. BUG_ON(ret);
  888. wait_on_page_locked(pinned[0]);
  889. } else {
  890. unlock_page(pinned[0]);
  891. }
  892. }
  893. if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
  894. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  895. if (!PageUptodate(pinned[1])) {
  896. ret = btrfs_readpage(NULL, pinned[1]);
  897. BUG_ON(ret);
  898. wait_on_page_locked(pinned[1]);
  899. } else {
  900. unlock_page(pinned[1]);
  901. }
  902. }
  903. while(count > 0) {
  904. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  905. size_t write_bytes = min(count, nrptrs *
  906. (size_t)PAGE_CACHE_SIZE -
  907. offset);
  908. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  909. PAGE_CACHE_SHIFT;
  910. WARN_ON(num_pages > nrptrs);
  911. memset(pages, 0, sizeof(pages));
  912. ret = btrfs_check_free_space(root, write_bytes, 0);
  913. if (ret)
  914. goto out;
  915. ret = prepare_pages(root, file, pages, num_pages,
  916. pos, first_index, last_index,
  917. write_bytes);
  918. if (ret)
  919. goto out;
  920. ret = btrfs_copy_from_user(pos, num_pages,
  921. write_bytes, pages, buf);
  922. if (ret) {
  923. btrfs_drop_pages(pages, num_pages);
  924. goto out;
  925. }
  926. ret = dirty_and_release_pages(NULL, root, file, pages,
  927. num_pages, pos, write_bytes);
  928. btrfs_drop_pages(pages, num_pages);
  929. if (ret)
  930. goto out;
  931. if (will_write) {
  932. btrfs_fdatawrite_range(inode->i_mapping, pos,
  933. pos + write_bytes - 1,
  934. WB_SYNC_NONE);
  935. } else {
  936. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  937. num_pages);
  938. if (num_pages <
  939. (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
  940. btrfs_btree_balance_dirty(root, 1);
  941. btrfs_throttle(root);
  942. }
  943. buf += write_bytes;
  944. count -= write_bytes;
  945. pos += write_bytes;
  946. num_written += write_bytes;
  947. cond_resched();
  948. }
  949. out:
  950. mutex_unlock(&inode->i_mutex);
  951. out_nolock:
  952. kfree(pages);
  953. if (pinned[0])
  954. page_cache_release(pinned[0]);
  955. if (pinned[1])
  956. page_cache_release(pinned[1]);
  957. *ppos = pos;
  958. if (num_written > 0 && will_write) {
  959. struct btrfs_trans_handle *trans;
  960. err = btrfs_wait_ordered_range(inode, start_pos, num_written);
  961. if (err)
  962. num_written = err;
  963. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  964. trans = btrfs_start_transaction(root, 1);
  965. ret = btrfs_log_dentry_safe(trans, root,
  966. file->f_dentry);
  967. if (ret == 0) {
  968. btrfs_sync_log(trans, root);
  969. btrfs_end_transaction(trans, root);
  970. } else {
  971. btrfs_commit_transaction(trans, root);
  972. }
  973. }
  974. if (file->f_flags & O_DIRECT) {
  975. invalidate_mapping_pages(inode->i_mapping,
  976. start_pos >> PAGE_CACHE_SHIFT,
  977. (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
  978. }
  979. }
  980. current->backing_dev_info = NULL;
  981. return num_written ? num_written : err;
  982. }
  983. int btrfs_release_file(struct inode * inode, struct file * filp)
  984. {
  985. if (filp->private_data)
  986. btrfs_ioctl_trans_end(filp);
  987. return 0;
  988. }
  989. /*
  990. * fsync call for both files and directories. This logs the inode into
  991. * the tree log instead of forcing full commits whenever possible.
  992. *
  993. * It needs to call filemap_fdatawait so that all ordered extent updates are
  994. * in the metadata btree are up to date for copying to the log.
  995. *
  996. * It drops the inode mutex before doing the tree log commit. This is an
  997. * important optimization for directories because holding the mutex prevents
  998. * new operations on the dir while we write to disk.
  999. */
  1000. int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
  1001. {
  1002. struct inode *inode = dentry->d_inode;
  1003. struct btrfs_root *root = BTRFS_I(inode)->root;
  1004. int ret = 0;
  1005. struct btrfs_trans_handle *trans;
  1006. /*
  1007. * check the transaction that last modified this inode
  1008. * and see if its already been committed
  1009. */
  1010. if (!BTRFS_I(inode)->last_trans)
  1011. goto out;
  1012. mutex_lock(&root->fs_info->trans_mutex);
  1013. if (BTRFS_I(inode)->last_trans <=
  1014. root->fs_info->last_trans_committed) {
  1015. BTRFS_I(inode)->last_trans = 0;
  1016. mutex_unlock(&root->fs_info->trans_mutex);
  1017. goto out;
  1018. }
  1019. mutex_unlock(&root->fs_info->trans_mutex);
  1020. root->fs_info->tree_log_batch++;
  1021. filemap_fdatawait(inode->i_mapping);
  1022. root->fs_info->tree_log_batch++;
  1023. /*
  1024. * ok we haven't committed the transaction yet, lets do a commit
  1025. */
  1026. if (file->private_data)
  1027. btrfs_ioctl_trans_end(file);
  1028. trans = btrfs_start_transaction(root, 1);
  1029. if (!trans) {
  1030. ret = -ENOMEM;
  1031. goto out;
  1032. }
  1033. ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
  1034. if (ret < 0) {
  1035. goto out;
  1036. }
  1037. /* we've logged all the items and now have a consistent
  1038. * version of the file in the log. It is possible that
  1039. * someone will come in and modify the file, but that's
  1040. * fine because the log is consistent on disk, and we
  1041. * have references to all of the file's extents
  1042. *
  1043. * It is possible that someone will come in and log the
  1044. * file again, but that will end up using the synchronization
  1045. * inside btrfs_sync_log to keep things safe.
  1046. */
  1047. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  1048. if (ret > 0) {
  1049. ret = btrfs_commit_transaction(trans, root);
  1050. } else {
  1051. btrfs_sync_log(trans, root);
  1052. ret = btrfs_end_transaction(trans, root);
  1053. }
  1054. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  1055. out:
  1056. return ret > 0 ? EIO : ret;
  1057. }
  1058. static struct vm_operations_struct btrfs_file_vm_ops = {
  1059. .fault = filemap_fault,
  1060. .page_mkwrite = btrfs_page_mkwrite,
  1061. };
  1062. static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  1063. {
  1064. vma->vm_ops = &btrfs_file_vm_ops;
  1065. file_accessed(filp);
  1066. return 0;
  1067. }
  1068. struct file_operations btrfs_file_operations = {
  1069. .llseek = generic_file_llseek,
  1070. .read = do_sync_read,
  1071. .aio_read = generic_file_aio_read,
  1072. .splice_read = generic_file_splice_read,
  1073. .write = btrfs_file_write,
  1074. .mmap = btrfs_file_mmap,
  1075. .open = generic_file_open,
  1076. .release = btrfs_release_file,
  1077. .fsync = btrfs_sync_file,
  1078. .unlocked_ioctl = btrfs_ioctl,
  1079. #ifdef CONFIG_COMPAT
  1080. .compat_ioctl = btrfs_ioctl,
  1081. #endif
  1082. };