Commit | Line | Data |
---|---|---|
6cbd5570 CM |
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 | ||
39279cc3 CM |
19 | #include <linux/buffer_head.h> |
20 | #include <linux/fs.h> | |
21 | #include <linux/pagemap.h> | |
22 | #include <linux/highmem.h> | |
23 | #include <linux/time.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/string.h> | |
26 | #include <linux/smp_lock.h> | |
27 | #include <linux/backing-dev.h> | |
28 | #include <linux/mpage.h> | |
29 | #include <linux/swap.h> | |
30 | #include <linux/writeback.h> | |
31 | #include <linux/statfs.h> | |
32 | #include <linux/compat.h> | |
92fee66d | 33 | #include <linux/version.h> |
39279cc3 CM |
34 | #include "ctree.h" |
35 | #include "disk-io.h" | |
36 | #include "transaction.h" | |
37 | #include "btrfs_inode.h" | |
38 | #include "ioctl.h" | |
39 | #include "print-tree.h" | |
40 | ||
41 | ||
42 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, | |
43 | struct page **prepared_pages, | |
44 | const char __user * buf) | |
45 | { | |
46 | long page_fault = 0; | |
47 | int i; | |
48 | int offset = pos & (PAGE_CACHE_SIZE - 1); | |
49 | ||
50 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { | |
51 | size_t count = min_t(size_t, | |
52 | PAGE_CACHE_SIZE - offset, write_bytes); | |
53 | struct page *page = prepared_pages[i]; | |
54 | fault_in_pages_readable(buf, count); | |
55 | ||
56 | /* Copy data from userspace to the current page */ | |
57 | kmap(page); | |
58 | page_fault = __copy_from_user(page_address(page) + offset, | |
59 | buf, count); | |
60 | /* Flush processor's dcache for this page */ | |
61 | flush_dcache_page(page); | |
62 | kunmap(page); | |
63 | buf += count; | |
64 | write_bytes -= count; | |
65 | ||
66 | if (page_fault) | |
67 | break; | |
68 | } | |
69 | return page_fault ? -EFAULT : 0; | |
70 | } | |
71 | ||
72 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) | |
73 | { | |
74 | size_t i; | |
75 | for (i = 0; i < num_pages; i++) { | |
76 | if (!pages[i]) | |
77 | break; | |
78 | unlock_page(pages[i]); | |
79 | mark_page_accessed(pages[i]); | |
80 | page_cache_release(pages[i]); | |
81 | } | |
82 | } | |
83 | ||
54aa1f4d CM |
84 | static int insert_inline_extent(struct btrfs_root *root, struct inode *inode, |
85 | u64 offset, ssize_t size, | |
86 | struct buffer_head *bh) | |
87 | { | |
88 | struct btrfs_key key; | |
89 | struct btrfs_path *path; | |
90 | char *ptr, *kaddr; | |
91 | struct btrfs_trans_handle *trans; | |
92 | struct btrfs_file_extent_item *ei; | |
93 | u32 datasize; | |
94 | int err = 0; | |
95 | int ret; | |
96 | ||
97 | path = btrfs_alloc_path(); | |
98 | if (!path) | |
99 | return -ENOMEM; | |
100 | ||
101 | mutex_lock(&root->fs_info->fs_mutex); | |
102 | trans = btrfs_start_transaction(root, 1); | |
103 | btrfs_set_trans_block_group(trans, inode); | |
104 | ||
105 | key.objectid = inode->i_ino; | |
106 | key.offset = offset; | |
107 | key.flags = 0; | |
108 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); | |
109 | BUG_ON(size >= PAGE_CACHE_SIZE); | |
110 | datasize = btrfs_file_extent_calc_inline_size(size); | |
111 | ||
112 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
113 | datasize); | |
114 | if (ret) { | |
115 | err = ret; | |
116 | goto fail; | |
117 | } | |
118 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
119 | path->slots[0], struct btrfs_file_extent_item); | |
120 | btrfs_set_file_extent_generation(ei, trans->transid); | |
121 | btrfs_set_file_extent_type(ei, | |
122 | BTRFS_FILE_EXTENT_INLINE); | |
123 | ptr = btrfs_file_extent_inline_start(ei); | |
124 | ||
125 | kaddr = kmap_atomic(bh->b_page, KM_USER0); | |
126 | btrfs_memcpy(root, path->nodes[0]->b_data, | |
127 | ptr, kaddr + bh_offset(bh), | |
128 | size); | |
129 | kunmap_atomic(kaddr, KM_USER0); | |
ccd467d6 | 130 | btrfs_mark_buffer_dirty(path->nodes[0]); |
54aa1f4d CM |
131 | fail: |
132 | btrfs_free_path(path); | |
133 | ret = btrfs_end_transaction(trans, root); | |
134 | if (ret && !err) | |
135 | err = ret; | |
136 | mutex_unlock(&root->fs_info->fs_mutex); | |
137 | return err; | |
138 | } | |
139 | ||
39279cc3 CM |
140 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
141 | struct btrfs_root *root, | |
142 | struct file *file, | |
143 | struct page **pages, | |
144 | size_t num_pages, | |
145 | loff_t pos, | |
146 | size_t write_bytes) | |
147 | { | |
148 | int i; | |
149 | int offset; | |
150 | int err = 0; | |
151 | int ret; | |
152 | int this_write; | |
153 | struct inode *inode = file->f_path.dentry->d_inode; | |
154 | struct buffer_head *bh; | |
39279cc3 CM |
155 | |
156 | for (i = 0; i < num_pages; i++) { | |
157 | offset = pos & (PAGE_CACHE_SIZE -1); | |
84f54cfa | 158 | this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes); |
39279cc3 | 159 | |
54aa1f4d | 160 | /* FIXME, one block at a time */ |
39279cc3 CM |
161 | bh = page_buffers(pages[i]); |
162 | ||
163 | if (buffer_mapped(bh) && bh->b_blocknr == 0) { | |
54aa1f4d CM |
164 | ret = insert_inline_extent(root, inode, |
165 | pages[i]->index << PAGE_CACHE_SHIFT, | |
166 | offset + this_write, bh); | |
167 | if (ret) { | |
168 | err = ret; | |
169 | goto failed; | |
170 | } | |
39279cc3 | 171 | } |
39279cc3 CM |
172 | |
173 | ret = btrfs_commit_write(file, pages[i], offset, | |
174 | offset + this_write); | |
175 | pos += this_write; | |
176 | if (ret) { | |
177 | err = ret; | |
178 | goto failed; | |
179 | } | |
180 | WARN_ON(this_write > write_bytes); | |
181 | write_bytes -= this_write; | |
182 | } | |
183 | failed: | |
184 | return err; | |
185 | } | |
186 | ||
187 | /* | |
188 | * this is very complex, but the basic idea is to drop all extents | |
189 | * in the range start - end. hint_block is filled in with a block number | |
190 | * that would be a good hint to the block allocator for this file. | |
191 | * | |
192 | * If an extent intersects the range but is not entirely inside the range | |
193 | * it is either truncated or split. Anything entirely inside the range | |
194 | * is deleted from the tree. | |
195 | */ | |
196 | int btrfs_drop_extents(struct btrfs_trans_handle *trans, | |
197 | struct btrfs_root *root, struct inode *inode, | |
198 | u64 start, u64 end, u64 *hint_block) | |
199 | { | |
200 | int ret; | |
201 | struct btrfs_key key; | |
202 | struct btrfs_leaf *leaf; | |
203 | int slot; | |
204 | struct btrfs_file_extent_item *extent; | |
205 | u64 extent_end = 0; | |
206 | int keep; | |
207 | struct btrfs_file_extent_item old; | |
208 | struct btrfs_path *path; | |
209 | u64 search_start = start; | |
210 | int bookend; | |
211 | int found_type; | |
212 | int found_extent; | |
213 | int found_inline; | |
ccd467d6 | 214 | int recow; |
39279cc3 CM |
215 | |
216 | path = btrfs_alloc_path(); | |
217 | if (!path) | |
218 | return -ENOMEM; | |
219 | while(1) { | |
ccd467d6 | 220 | recow = 0; |
39279cc3 CM |
221 | btrfs_release_path(root, path); |
222 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, | |
223 | search_start, -1); | |
224 | if (ret < 0) | |
225 | goto out; | |
226 | if (ret > 0) { | |
227 | if (path->slots[0] == 0) { | |
228 | ret = 0; | |
229 | goto out; | |
230 | } | |
231 | path->slots[0]--; | |
232 | } | |
8c2383c3 | 233 | next_slot: |
39279cc3 CM |
234 | keep = 0; |
235 | bookend = 0; | |
236 | found_extent = 0; | |
237 | found_inline = 0; | |
238 | extent = NULL; | |
239 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
240 | slot = path->slots[0]; | |
8c2383c3 | 241 | ret = 0; |
39279cc3 CM |
242 | btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); |
243 | if (key.offset >= end || key.objectid != inode->i_ino) { | |
39279cc3 CM |
244 | goto out; |
245 | } | |
8c2383c3 | 246 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) { |
39279cc3 CM |
247 | goto out; |
248 | } | |
ccd467d6 CM |
249 | if (recow) { |
250 | search_start = key.offset; | |
251 | continue; | |
252 | } | |
8c2383c3 CM |
253 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
254 | extent = btrfs_item_ptr(leaf, slot, | |
255 | struct btrfs_file_extent_item); | |
256 | found_type = btrfs_file_extent_type(extent); | |
257 | if (found_type == BTRFS_FILE_EXTENT_REG) { | |
258 | extent_end = key.offset + | |
259 | (btrfs_file_extent_num_blocks(extent) << | |
260 | inode->i_blkbits); | |
261 | found_extent = 1; | |
262 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
263 | found_inline = 1; | |
264 | extent_end = key.offset + | |
265 | btrfs_file_extent_inline_len(leaf->items + | |
266 | slot); | |
267 | } | |
268 | } else { | |
269 | extent_end = search_start; | |
39279cc3 CM |
270 | } |
271 | ||
272 | /* we found nothing we can drop */ | |
8c2383c3 CM |
273 | if ((!found_extent && !found_inline) || |
274 | search_start >= extent_end) { | |
275 | int nextret; | |
276 | u32 nritems; | |
277 | nritems = btrfs_header_nritems( | |
278 | btrfs_buffer_header(path->nodes[0])); | |
279 | if (slot >= nritems - 1) { | |
280 | nextret = btrfs_next_leaf(root, path); | |
281 | if (nextret) | |
282 | goto out; | |
ccd467d6 | 283 | recow = 1; |
8c2383c3 CM |
284 | } else { |
285 | path->slots[0]++; | |
286 | } | |
287 | goto next_slot; | |
39279cc3 CM |
288 | } |
289 | ||
290 | /* FIXME, there's only one inline extent allowed right now */ | |
291 | if (found_inline) { | |
292 | u64 mask = root->blocksize - 1; | |
293 | search_start = (extent_end + mask) & ~mask; | |
294 | } else | |
295 | search_start = extent_end; | |
296 | ||
297 | if (end < extent_end && end >= key.offset) { | |
298 | if (found_extent) { | |
299 | u64 disk_blocknr = | |
300 | btrfs_file_extent_disk_blocknr(extent); | |
301 | u64 disk_num_blocks = | |
302 | btrfs_file_extent_disk_num_blocks(extent); | |
303 | memcpy(&old, extent, sizeof(old)); | |
304 | if (disk_blocknr != 0) { | |
305 | ret = btrfs_inc_extent_ref(trans, root, | |
306 | disk_blocknr, disk_num_blocks); | |
307 | BUG_ON(ret); | |
308 | } | |
309 | } | |
310 | WARN_ON(found_inline); | |
311 | bookend = 1; | |
312 | } | |
39279cc3 CM |
313 | /* truncate existing extent */ |
314 | if (start > key.offset) { | |
315 | u64 new_num; | |
316 | u64 old_num; | |
317 | keep = 1; | |
318 | WARN_ON(start & (root->blocksize - 1)); | |
319 | if (found_extent) { | |
320 | new_num = (start - key.offset) >> | |
321 | inode->i_blkbits; | |
322 | old_num = btrfs_file_extent_num_blocks(extent); | |
323 | *hint_block = | |
324 | btrfs_file_extent_disk_blocknr(extent); | |
325 | if (btrfs_file_extent_disk_blocknr(extent)) { | |
326 | inode->i_blocks -= | |
327 | (old_num - new_num) << 3; | |
328 | } | |
329 | btrfs_set_file_extent_num_blocks(extent, | |
330 | new_num); | |
ccd467d6 | 331 | btrfs_mark_buffer_dirty(path->nodes[0]); |
39279cc3 CM |
332 | } else { |
333 | WARN_ON(1); | |
334 | } | |
335 | } | |
336 | /* delete the entire extent */ | |
337 | if (!keep) { | |
338 | u64 disk_blocknr = 0; | |
339 | u64 disk_num_blocks = 0; | |
340 | u64 extent_num_blocks = 0; | |
341 | if (found_extent) { | |
342 | disk_blocknr = | |
343 | btrfs_file_extent_disk_blocknr(extent); | |
344 | disk_num_blocks = | |
345 | btrfs_file_extent_disk_num_blocks(extent); | |
346 | extent_num_blocks = | |
347 | btrfs_file_extent_num_blocks(extent); | |
348 | *hint_block = | |
349 | btrfs_file_extent_disk_blocknr(extent); | |
350 | } | |
351 | ret = btrfs_del_item(trans, root, path); | |
54aa1f4d | 352 | /* TODO update progress marker and return */ |
39279cc3 CM |
353 | BUG_ON(ret); |
354 | btrfs_release_path(root, path); | |
355 | extent = NULL; | |
356 | if (found_extent && disk_blocknr != 0) { | |
357 | inode->i_blocks -= extent_num_blocks << 3; | |
358 | ret = btrfs_free_extent(trans, root, | |
359 | disk_blocknr, | |
360 | disk_num_blocks, 0); | |
361 | } | |
362 | ||
363 | BUG_ON(ret); | |
364 | if (!bookend && search_start >= end) { | |
365 | ret = 0; | |
366 | goto out; | |
367 | } | |
368 | if (!bookend) | |
369 | continue; | |
370 | } | |
371 | /* create bookend, splitting the extent in two */ | |
372 | if (bookend && found_extent) { | |
373 | struct btrfs_key ins; | |
374 | ins.objectid = inode->i_ino; | |
375 | ins.offset = end; | |
376 | ins.flags = 0; | |
377 | btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY); | |
39279cc3 CM |
378 | btrfs_release_path(root, path); |
379 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
380 | sizeof(*extent)); | |
8c2383c3 CM |
381 | |
382 | if (ret) { | |
383 | btrfs_print_leaf(root, btrfs_buffer_leaf(path->nodes[0])); | |
384 | printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu\n", ret , ins.objectid, ins.flags, ins.offset, start, end, key.offset, extent_end); | |
385 | } | |
39279cc3 CM |
386 | BUG_ON(ret); |
387 | extent = btrfs_item_ptr( | |
388 | btrfs_buffer_leaf(path->nodes[0]), | |
389 | path->slots[0], | |
390 | struct btrfs_file_extent_item); | |
391 | btrfs_set_file_extent_disk_blocknr(extent, | |
392 | btrfs_file_extent_disk_blocknr(&old)); | |
393 | btrfs_set_file_extent_disk_num_blocks(extent, | |
394 | btrfs_file_extent_disk_num_blocks(&old)); | |
395 | ||
396 | btrfs_set_file_extent_offset(extent, | |
397 | btrfs_file_extent_offset(&old) + | |
398 | ((end - key.offset) >> inode->i_blkbits)); | |
399 | WARN_ON(btrfs_file_extent_num_blocks(&old) < | |
400 | (extent_end - end) >> inode->i_blkbits); | |
401 | btrfs_set_file_extent_num_blocks(extent, | |
402 | (extent_end - end) >> inode->i_blkbits); | |
403 | ||
404 | btrfs_set_file_extent_type(extent, | |
405 | BTRFS_FILE_EXTENT_REG); | |
406 | btrfs_set_file_extent_generation(extent, | |
407 | btrfs_file_extent_generation(&old)); | |
408 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
409 | if (btrfs_file_extent_disk_blocknr(&old) != 0) { | |
410 | inode->i_blocks += | |
411 | btrfs_file_extent_num_blocks(extent) << 3; | |
412 | } | |
413 | ret = 0; | |
414 | goto out; | |
415 | } | |
416 | } | |
417 | out: | |
418 | btrfs_free_path(path); | |
419 | return ret; | |
420 | } | |
421 | ||
422 | /* | |
423 | * this gets pages into the page cache and locks them down | |
424 | */ | |
425 | static int prepare_pages(struct btrfs_root *root, | |
426 | struct file *file, | |
427 | struct page **pages, | |
428 | size_t num_pages, | |
429 | loff_t pos, | |
430 | unsigned long first_index, | |
431 | unsigned long last_index, | |
8c2383c3 | 432 | size_t write_bytes) |
39279cc3 CM |
433 | { |
434 | int i; | |
435 | unsigned long index = pos >> PAGE_CACHE_SHIFT; | |
436 | struct inode *inode = file->f_path.dentry->d_inode; | |
437 | int offset; | |
438 | int err = 0; | |
439 | int this_write; | |
440 | struct buffer_head *bh; | |
441 | struct buffer_head *head; | |
442 | loff_t isize = i_size_read(inode); | |
8c2383c3 CM |
443 | struct btrfs_trans_handle *trans; |
444 | u64 hint_block; | |
445 | u64 num_blocks; | |
446 | u64 alloc_extent_start; | |
447 | u64 start_pos; | |
448 | struct btrfs_key ins; | |
449 | ||
450 | start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1); | |
451 | num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >> | |
452 | inode->i_blkbits; | |
39279cc3 CM |
453 | |
454 | memset(pages, 0, num_pages * sizeof(struct page *)); | |
455 | ||
456 | for (i = 0; i < num_pages; i++) { | |
457 | pages[i] = grab_cache_page(inode->i_mapping, index + i); | |
458 | if (!pages[i]) { | |
459 | err = -ENOMEM; | |
460 | goto failed_release; | |
461 | } | |
ccd467d6 CM |
462 | cancel_dirty_page(pages[i], PAGE_CACHE_SIZE); |
463 | wait_on_page_writeback(pages[i]); | |
8c2383c3 CM |
464 | } |
465 | ||
466 | mutex_lock(&root->fs_info->fs_mutex); | |
467 | trans = btrfs_start_transaction(root, 1); | |
468 | if (!trans) { | |
469 | err = -ENOMEM; | |
470 | mutex_unlock(&root->fs_info->fs_mutex); | |
471 | goto out_unlock; | |
472 | } | |
473 | btrfs_set_trans_block_group(trans, inode); | |
474 | /* FIXME blocksize != 4096 */ | |
475 | inode->i_blocks += num_blocks << 3; | |
476 | hint_block = 0; | |
477 | ||
478 | /* FIXME...EIEIO, ENOSPC and more */ | |
479 | ||
480 | /* step one, delete the existing extents in this range */ | |
481 | /* FIXME blocksize != pagesize */ | |
482 | if (start_pos < inode->i_size) { | |
483 | err = btrfs_drop_extents(trans, root, inode, | |
484 | start_pos, (pos + write_bytes + root->blocksize -1) & | |
485 | ~((u64)root->blocksize - 1), &hint_block); | |
54aa1f4d CM |
486 | if (err) |
487 | goto failed_release; | |
8c2383c3 CM |
488 | } |
489 | ||
490 | /* insert any holes we need to create */ | |
491 | if (inode->i_size < start_pos) { | |
492 | u64 last_pos_in_file; | |
493 | u64 hole_size; | |
494 | u64 mask = root->blocksize - 1; | |
495 | last_pos_in_file = (isize + mask) & ~mask; | |
496 | hole_size = (start_pos - last_pos_in_file + mask) & ~mask; | |
497 | hole_size >>= inode->i_blkbits; | |
498 | if (last_pos_in_file < start_pos) { | |
499 | err = btrfs_insert_file_extent(trans, root, | |
500 | inode->i_ino, | |
501 | last_pos_in_file, | |
502 | 0, 0, hole_size); | |
503 | } | |
54aa1f4d CM |
504 | if (err) |
505 | goto failed_release; | |
8c2383c3 CM |
506 | } |
507 | ||
508 | /* | |
509 | * either allocate an extent for the new bytes or setup the key | |
510 | * to show we are doing inline data in the extent | |
511 | */ | |
512 | if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size || | |
513 | pos + write_bytes - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) { | |
514 | err = btrfs_alloc_extent(trans, root, inode->i_ino, | |
6702ed49 | 515 | num_blocks, 0, hint_block, (u64)-1, |
8c2383c3 | 516 | &ins, 1); |
54aa1f4d CM |
517 | if (err) |
518 | goto failed_truncate; | |
8c2383c3 CM |
519 | err = btrfs_insert_file_extent(trans, root, inode->i_ino, |
520 | start_pos, ins.objectid, ins.offset, | |
521 | ins.offset); | |
54aa1f4d CM |
522 | if (err) |
523 | goto failed_truncate; | |
8c2383c3 CM |
524 | } else { |
525 | ins.offset = 0; | |
526 | ins.objectid = 0; | |
527 | } | |
528 | BUG_ON(err); | |
529 | alloc_extent_start = ins.objectid; | |
530 | err = btrfs_end_transaction(trans, root); | |
531 | mutex_unlock(&root->fs_info->fs_mutex); | |
532 | ||
533 | for (i = 0; i < num_pages; i++) { | |
39279cc3 | 534 | offset = pos & (PAGE_CACHE_SIZE -1); |
84f54cfa | 535 | this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes); |
39279cc3 CM |
536 | if (!page_has_buffers(pages[i])) { |
537 | create_empty_buffers(pages[i], | |
538 | root->fs_info->sb->s_blocksize, | |
539 | (1 << BH_Uptodate)); | |
540 | } | |
541 | head = page_buffers(pages[i]); | |
542 | bh = head; | |
543 | do { | |
544 | err = btrfs_map_bh_to_logical(root, bh, | |
545 | alloc_extent_start); | |
546 | BUG_ON(err); | |
547 | if (err) | |
548 | goto failed_truncate; | |
549 | bh = bh->b_this_page; | |
550 | if (alloc_extent_start) | |
551 | alloc_extent_start++; | |
552 | } while (bh != head); | |
553 | pos += this_write; | |
554 | WARN_ON(this_write > write_bytes); | |
555 | write_bytes -= this_write; | |
556 | } | |
557 | return 0; | |
558 | ||
559 | failed_release: | |
560 | btrfs_drop_pages(pages, num_pages); | |
561 | return err; | |
562 | ||
563 | failed_truncate: | |
564 | btrfs_drop_pages(pages, num_pages); | |
565 | if (pos > isize) | |
566 | vmtruncate(inode, isize); | |
567 | return err; | |
8c2383c3 CM |
568 | |
569 | out_unlock: | |
570 | mutex_unlock(&root->fs_info->fs_mutex); | |
571 | goto failed_release; | |
572 | ||
39279cc3 CM |
573 | } |
574 | ||
575 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, | |
576 | size_t count, loff_t *ppos) | |
577 | { | |
578 | loff_t pos; | |
579 | size_t num_written = 0; | |
580 | int err = 0; | |
581 | int ret = 0; | |
582 | struct inode *inode = file->f_path.dentry->d_inode; | |
583 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8c2383c3 CM |
584 | struct page **pages = NULL; |
585 | int nrptrs; | |
39279cc3 CM |
586 | struct page *pinned[2]; |
587 | unsigned long first_index; | |
588 | unsigned long last_index; | |
8c2383c3 CM |
589 | |
590 | nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE, | |
591 | PAGE_CACHE_SIZE / (sizeof(struct page *))); | |
39279cc3 CM |
592 | pinned[0] = NULL; |
593 | pinned[1] = NULL; | |
594 | if (file->f_flags & O_DIRECT) | |
595 | return -EINVAL; | |
596 | pos = *ppos; | |
597 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
598 | current->backing_dev_info = inode->i_mapping->backing_dev_info; | |
599 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
600 | if (err) | |
601 | goto out; | |
602 | if (count == 0) | |
603 | goto out; | |
604 | err = remove_suid(file->f_path.dentry); | |
605 | if (err) | |
606 | goto out; | |
607 | file_update_time(file); | |
608 | ||
8c2383c3 | 609 | pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); |
39279cc3 CM |
610 | |
611 | mutex_lock(&inode->i_mutex); | |
612 | first_index = pos >> PAGE_CACHE_SHIFT; | |
613 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; | |
614 | ||
615 | /* | |
616 | * there are lots of better ways to do this, but this code | |
617 | * makes sure the first and last page in the file range are | |
618 | * up to date and ready for cow | |
619 | */ | |
620 | if ((pos & (PAGE_CACHE_SIZE - 1))) { | |
621 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); | |
622 | if (!PageUptodate(pinned[0])) { | |
9ebefb18 | 623 | ret = btrfs_readpage(NULL, pinned[0]); |
39279cc3 CM |
624 | BUG_ON(ret); |
625 | wait_on_page_locked(pinned[0]); | |
626 | } else { | |
627 | unlock_page(pinned[0]); | |
628 | } | |
629 | } | |
630 | if ((pos + count) & (PAGE_CACHE_SIZE - 1)) { | |
631 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); | |
632 | if (!PageUptodate(pinned[1])) { | |
9ebefb18 | 633 | ret = btrfs_readpage(NULL, pinned[1]); |
39279cc3 CM |
634 | BUG_ON(ret); |
635 | wait_on_page_locked(pinned[1]); | |
636 | } else { | |
637 | unlock_page(pinned[1]); | |
638 | } | |
639 | } | |
640 | ||
39279cc3 CM |
641 | while(count > 0) { |
642 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); | |
11bd143f CM |
643 | size_t write_bytes = min(count, nrptrs * |
644 | (size_t)PAGE_CACHE_SIZE - | |
8c2383c3 | 645 | offset); |
39279cc3 CM |
646 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
647 | PAGE_CACHE_SHIFT; | |
648 | ||
8c2383c3 | 649 | WARN_ON(num_pages > nrptrs); |
39279cc3 CM |
650 | memset(pages, 0, sizeof(pages)); |
651 | ret = prepare_pages(root, file, pages, num_pages, | |
652 | pos, first_index, last_index, | |
8c2383c3 | 653 | write_bytes); |
54aa1f4d CM |
654 | if (ret) |
655 | goto out; | |
39279cc3 | 656 | |
39279cc3 CM |
657 | ret = btrfs_copy_from_user(pos, num_pages, |
658 | write_bytes, pages, buf); | |
54aa1f4d CM |
659 | if (ret) { |
660 | btrfs_drop_pages(pages, num_pages); | |
661 | goto out; | |
662 | } | |
39279cc3 CM |
663 | |
664 | ret = dirty_and_release_pages(NULL, root, file, pages, | |
665 | num_pages, pos, write_bytes); | |
39279cc3 | 666 | btrfs_drop_pages(pages, num_pages); |
54aa1f4d CM |
667 | if (ret) |
668 | goto out; | |
39279cc3 CM |
669 | |
670 | buf += write_bytes; | |
671 | count -= write_bytes; | |
672 | pos += write_bytes; | |
673 | num_written += write_bytes; | |
674 | ||
8c2383c3 | 675 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages); |
39279cc3 CM |
676 | btrfs_btree_balance_dirty(root); |
677 | cond_resched(); | |
678 | } | |
39279cc3 CM |
679 | mutex_unlock(&inode->i_mutex); |
680 | out: | |
8c2383c3 | 681 | kfree(pages); |
39279cc3 CM |
682 | if (pinned[0]) |
683 | page_cache_release(pinned[0]); | |
684 | if (pinned[1]) | |
685 | page_cache_release(pinned[1]); | |
686 | *ppos = pos; | |
687 | current->backing_dev_info = NULL; | |
688 | mark_inode_dirty(inode); | |
689 | return num_written ? num_written : err; | |
690 | } | |
691 | ||
39279cc3 CM |
692 | static int btrfs_sync_file(struct file *file, |
693 | struct dentry *dentry, int datasync) | |
694 | { | |
695 | struct inode *inode = dentry->d_inode; | |
696 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
697 | int ret; | |
698 | struct btrfs_trans_handle *trans; | |
699 | ||
700 | /* | |
701 | * FIXME, use inode generation number to check if we can skip the | |
702 | * commit | |
703 | */ | |
704 | mutex_lock(&root->fs_info->fs_mutex); | |
705 | trans = btrfs_start_transaction(root, 1); | |
706 | if (!trans) { | |
707 | ret = -ENOMEM; | |
708 | goto out; | |
709 | } | |
710 | ret = btrfs_commit_transaction(trans, root); | |
711 | mutex_unlock(&root->fs_info->fs_mutex); | |
712 | out: | |
713 | return ret > 0 ? EIO : ret; | |
714 | } | |
715 | ||
9ebefb18 | 716 | static struct vm_operations_struct btrfs_file_vm_ops = { |
92fee66d CM |
717 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
718 | .nopage = filemap_nopage, | |
719 | .populate = filemap_populate, | |
720 | #else | |
721 | .fault = filemap_fault, | |
722 | #endif | |
9ebefb18 CM |
723 | .page_mkwrite = btrfs_page_mkwrite, |
724 | }; | |
725 | ||
726 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) | |
727 | { | |
728 | vma->vm_ops = &btrfs_file_vm_ops; | |
729 | file_accessed(filp); | |
730 | return 0; | |
731 | } | |
732 | ||
39279cc3 CM |
733 | struct file_operations btrfs_file_operations = { |
734 | .llseek = generic_file_llseek, | |
735 | .read = do_sync_read, | |
9ebefb18 | 736 | .aio_read = generic_file_aio_read, |
39279cc3 | 737 | .write = btrfs_file_write, |
9ebefb18 | 738 | .mmap = btrfs_file_mmap, |
39279cc3 CM |
739 | .open = generic_file_open, |
740 | .ioctl = btrfs_ioctl, | |
741 | .fsync = btrfs_sync_file, | |
742 | #ifdef CONFIG_COMPAT | |
743 | .compat_ioctl = btrfs_compat_ioctl, | |
744 | #endif | |
745 | }; | |
746 |