]>
Commit | Line | Data |
---|---|---|
ccd979bd MF |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public | |
17 | * License along with this program; if not, write to the | |
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | * Boston, MA 021110-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <linux/fs.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/highmem.h> | |
25 | #include <linux/pagemap.h> | |
26 | #include <asm/byteorder.h> | |
9517bac6 | 27 | #include <linux/swap.h> |
6af67d82 | 28 | #include <linux/pipe_fs_i.h> |
ccd979bd MF |
29 | |
30 | #define MLOG_MASK_PREFIX ML_FILE_IO | |
31 | #include <cluster/masklog.h> | |
32 | ||
33 | #include "ocfs2.h" | |
34 | ||
35 | #include "alloc.h" | |
36 | #include "aops.h" | |
37 | #include "dlmglue.h" | |
38 | #include "extent_map.h" | |
39 | #include "file.h" | |
40 | #include "inode.h" | |
41 | #include "journal.h" | |
9517bac6 | 42 | #include "suballoc.h" |
ccd979bd MF |
43 | #include "super.h" |
44 | #include "symlink.h" | |
45 | ||
46 | #include "buffer_head_io.h" | |
47 | ||
48 | static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock, | |
49 | struct buffer_head *bh_result, int create) | |
50 | { | |
51 | int err = -EIO; | |
52 | int status; | |
53 | struct ocfs2_dinode *fe = NULL; | |
54 | struct buffer_head *bh = NULL; | |
55 | struct buffer_head *buffer_cache_bh = NULL; | |
56 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
57 | void *kaddr; | |
58 | ||
59 | mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode, | |
60 | (unsigned long long)iblock, bh_result, create); | |
61 | ||
62 | BUG_ON(ocfs2_inode_is_fast_symlink(inode)); | |
63 | ||
64 | if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) { | |
65 | mlog(ML_ERROR, "block offset > PATH_MAX: %llu", | |
66 | (unsigned long long)iblock); | |
67 | goto bail; | |
68 | } | |
69 | ||
70 | status = ocfs2_read_block(OCFS2_SB(inode->i_sb), | |
71 | OCFS2_I(inode)->ip_blkno, | |
72 | &bh, OCFS2_BH_CACHED, inode); | |
73 | if (status < 0) { | |
74 | mlog_errno(status); | |
75 | goto bail; | |
76 | } | |
77 | fe = (struct ocfs2_dinode *) bh->b_data; | |
78 | ||
79 | if (!OCFS2_IS_VALID_DINODE(fe)) { | |
b0697053 MF |
80 | mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n", |
81 | (unsigned long long)fe->i_blkno, 7, fe->i_signature); | |
ccd979bd MF |
82 | goto bail; |
83 | } | |
84 | ||
85 | if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb, | |
86 | le32_to_cpu(fe->i_clusters))) { | |
87 | mlog(ML_ERROR, "block offset is outside the allocated size: " | |
88 | "%llu\n", (unsigned long long)iblock); | |
89 | goto bail; | |
90 | } | |
91 | ||
92 | /* We don't use the page cache to create symlink data, so if | |
93 | * need be, copy it over from the buffer cache. */ | |
94 | if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) { | |
95 | u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + | |
96 | iblock; | |
97 | buffer_cache_bh = sb_getblk(osb->sb, blkno); | |
98 | if (!buffer_cache_bh) { | |
99 | mlog(ML_ERROR, "couldn't getblock for symlink!\n"); | |
100 | goto bail; | |
101 | } | |
102 | ||
103 | /* we haven't locked out transactions, so a commit | |
104 | * could've happened. Since we've got a reference on | |
105 | * the bh, even if it commits while we're doing the | |
106 | * copy, the data is still good. */ | |
107 | if (buffer_jbd(buffer_cache_bh) | |
108 | && ocfs2_inode_is_new(inode)) { | |
109 | kaddr = kmap_atomic(bh_result->b_page, KM_USER0); | |
110 | if (!kaddr) { | |
111 | mlog(ML_ERROR, "couldn't kmap!\n"); | |
112 | goto bail; | |
113 | } | |
114 | memcpy(kaddr + (bh_result->b_size * iblock), | |
115 | buffer_cache_bh->b_data, | |
116 | bh_result->b_size); | |
117 | kunmap_atomic(kaddr, KM_USER0); | |
118 | set_buffer_uptodate(bh_result); | |
119 | } | |
120 | brelse(buffer_cache_bh); | |
121 | } | |
122 | ||
123 | map_bh(bh_result, inode->i_sb, | |
124 | le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock); | |
125 | ||
126 | err = 0; | |
127 | ||
128 | bail: | |
129 | if (bh) | |
130 | brelse(bh); | |
131 | ||
132 | mlog_exit(err); | |
133 | return err; | |
134 | } | |
135 | ||
136 | static int ocfs2_get_block(struct inode *inode, sector_t iblock, | |
137 | struct buffer_head *bh_result, int create) | |
138 | { | |
139 | int err = 0; | |
49cb8d2d | 140 | unsigned int ext_flags; |
ccd979bd | 141 | u64 p_blkno, past_eof; |
25baf2da | 142 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
ccd979bd MF |
143 | |
144 | mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode, | |
145 | (unsigned long long)iblock, bh_result, create); | |
146 | ||
147 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) | |
148 | mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n", | |
149 | inode, inode->i_ino); | |
150 | ||
151 | if (S_ISLNK(inode->i_mode)) { | |
152 | /* this always does I/O for some reason. */ | |
153 | err = ocfs2_symlink_get_block(inode, iblock, bh_result, create); | |
154 | goto bail; | |
155 | } | |
156 | ||
49cb8d2d MF |
157 | err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL, |
158 | &ext_flags); | |
ccd979bd MF |
159 | if (err) { |
160 | mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, " | |
b0697053 MF |
161 | "%llu, NULL)\n", err, inode, (unsigned long long)iblock, |
162 | (unsigned long long)p_blkno); | |
ccd979bd MF |
163 | goto bail; |
164 | } | |
165 | ||
25baf2da MF |
166 | /* |
167 | * ocfs2 never allocates in this function - the only time we | |
168 | * need to use BH_New is when we're extending i_size on a file | |
169 | * system which doesn't support holes, in which case BH_New | |
170 | * allows block_prepare_write() to zero. | |
171 | */ | |
172 | mlog_bug_on_msg(create && p_blkno == 0 && ocfs2_sparse_alloc(osb), | |
173 | "ino %lu, iblock %llu\n", inode->i_ino, | |
174 | (unsigned long long)iblock); | |
175 | ||
49cb8d2d MF |
176 | /* Treat the unwritten extent as a hole for zeroing purposes. */ |
177 | if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN)) | |
25baf2da MF |
178 | map_bh(bh_result, inode->i_sb, p_blkno); |
179 | ||
180 | if (!ocfs2_sparse_alloc(osb)) { | |
181 | if (p_blkno == 0) { | |
182 | err = -EIO; | |
183 | mlog(ML_ERROR, | |
184 | "iblock = %llu p_blkno = %llu blkno=(%llu)\n", | |
185 | (unsigned long long)iblock, | |
186 | (unsigned long long)p_blkno, | |
187 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
188 | mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters); | |
189 | dump_stack(); | |
190 | } | |
ccd979bd | 191 | |
25baf2da MF |
192 | past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode)); |
193 | mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino, | |
194 | (unsigned long long)past_eof); | |
ccd979bd | 195 | |
25baf2da MF |
196 | if (create && (iblock >= past_eof)) |
197 | set_buffer_new(bh_result); | |
198 | } | |
ccd979bd MF |
199 | |
200 | bail: | |
201 | if (err < 0) | |
202 | err = -EIO; | |
203 | ||
204 | mlog_exit(err); | |
205 | return err; | |
206 | } | |
207 | ||
208 | static int ocfs2_readpage(struct file *file, struct page *page) | |
209 | { | |
210 | struct inode *inode = page->mapping->host; | |
211 | loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT; | |
212 | int ret, unlock = 1; | |
213 | ||
214 | mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0)); | |
215 | ||
4bcec184 | 216 | ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page); |
ccd979bd MF |
217 | if (ret != 0) { |
218 | if (ret == AOP_TRUNCATED_PAGE) | |
219 | unlock = 0; | |
220 | mlog_errno(ret); | |
221 | goto out; | |
222 | } | |
223 | ||
224 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
225 | ||
226 | /* | |
227 | * i_size might have just been updated as we grabed the meta lock. We | |
228 | * might now be discovering a truncate that hit on another node. | |
229 | * block_read_full_page->get_block freaks out if it is asked to read | |
230 | * beyond the end of a file, so we check here. Callers | |
231 | * (generic_file_read, fault->nopage) are clever enough to check i_size | |
232 | * and notice that the page they just read isn't needed. | |
233 | * | |
234 | * XXX sys_readahead() seems to get that wrong? | |
235 | */ | |
236 | if (start >= i_size_read(inode)) { | |
237 | char *addr = kmap(page); | |
238 | memset(addr, 0, PAGE_SIZE); | |
239 | flush_dcache_page(page); | |
240 | kunmap(page); | |
241 | SetPageUptodate(page); | |
242 | ret = 0; | |
243 | goto out_alloc; | |
244 | } | |
245 | ||
246 | ret = ocfs2_data_lock_with_page(inode, 0, page); | |
247 | if (ret != 0) { | |
248 | if (ret == AOP_TRUNCATED_PAGE) | |
249 | unlock = 0; | |
250 | mlog_errno(ret); | |
251 | goto out_alloc; | |
252 | } | |
253 | ||
254 | ret = block_read_full_page(page, ocfs2_get_block); | |
255 | unlock = 0; | |
256 | ||
257 | ocfs2_data_unlock(inode, 0); | |
258 | out_alloc: | |
259 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
260 | ocfs2_meta_unlock(inode, 0); | |
261 | out: | |
262 | if (unlock) | |
263 | unlock_page(page); | |
264 | mlog_exit(ret); | |
265 | return ret; | |
266 | } | |
267 | ||
268 | /* Note: Because we don't support holes, our allocation has | |
269 | * already happened (allocation writes zeros to the file data) | |
270 | * so we don't have to worry about ordered writes in | |
271 | * ocfs2_writepage. | |
272 | * | |
273 | * ->writepage is called during the process of invalidating the page cache | |
274 | * during blocked lock processing. It can't block on any cluster locks | |
275 | * to during block mapping. It's relying on the fact that the block | |
276 | * mapping can't have disappeared under the dirty pages that it is | |
277 | * being asked to write back. | |
278 | */ | |
279 | static int ocfs2_writepage(struct page *page, struct writeback_control *wbc) | |
280 | { | |
281 | int ret; | |
282 | ||
283 | mlog_entry("(0x%p)\n", page); | |
284 | ||
285 | ret = block_write_full_page(page, ocfs2_get_block, wbc); | |
286 | ||
287 | mlog_exit(ret); | |
288 | ||
289 | return ret; | |
290 | } | |
291 | ||
5069120b MF |
292 | /* |
293 | * This is called from ocfs2_write_zero_page() which has handled it's | |
294 | * own cluster locking and has ensured allocation exists for those | |
295 | * blocks to be written. | |
296 | */ | |
53013cba MF |
297 | int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page, |
298 | unsigned from, unsigned to) | |
299 | { | |
300 | int ret; | |
301 | ||
302 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
303 | ||
304 | ret = block_prepare_write(page, from, to, ocfs2_get_block); | |
305 | ||
306 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
307 | ||
308 | return ret; | |
309 | } | |
310 | ||
ccd979bd MF |
311 | /* Taken from ext3. We don't necessarily need the full blown |
312 | * functionality yet, but IMHO it's better to cut and paste the whole | |
313 | * thing so we can avoid introducing our own bugs (and easily pick up | |
314 | * their fixes when they happen) --Mark */ | |
60b11392 MF |
315 | int walk_page_buffers( handle_t *handle, |
316 | struct buffer_head *head, | |
317 | unsigned from, | |
318 | unsigned to, | |
319 | int *partial, | |
320 | int (*fn)( handle_t *handle, | |
321 | struct buffer_head *bh)) | |
ccd979bd MF |
322 | { |
323 | struct buffer_head *bh; | |
324 | unsigned block_start, block_end; | |
325 | unsigned blocksize = head->b_size; | |
326 | int err, ret = 0; | |
327 | struct buffer_head *next; | |
328 | ||
329 | for ( bh = head, block_start = 0; | |
330 | ret == 0 && (bh != head || !block_start); | |
331 | block_start = block_end, bh = next) | |
332 | { | |
333 | next = bh->b_this_page; | |
334 | block_end = block_start + blocksize; | |
335 | if (block_end <= from || block_start >= to) { | |
336 | if (partial && !buffer_uptodate(bh)) | |
337 | *partial = 1; | |
338 | continue; | |
339 | } | |
340 | err = (*fn)(handle, bh); | |
341 | if (!ret) | |
342 | ret = err; | |
343 | } | |
344 | return ret; | |
345 | } | |
346 | ||
1fabe148 | 347 | handle_t *ocfs2_start_walk_page_trans(struct inode *inode, |
ccd979bd MF |
348 | struct page *page, |
349 | unsigned from, | |
350 | unsigned to) | |
351 | { | |
352 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1fabe148 | 353 | handle_t *handle = NULL; |
ccd979bd MF |
354 | int ret = 0; |
355 | ||
65eff9cc | 356 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
357 | if (!handle) { |
358 | ret = -ENOMEM; | |
359 | mlog_errno(ret); | |
360 | goto out; | |
361 | } | |
362 | ||
363 | if (ocfs2_should_order_data(inode)) { | |
1fabe148 | 364 | ret = walk_page_buffers(handle, |
ccd979bd MF |
365 | page_buffers(page), |
366 | from, to, NULL, | |
367 | ocfs2_journal_dirty_data); | |
368 | if (ret < 0) | |
369 | mlog_errno(ret); | |
370 | } | |
371 | out: | |
372 | if (ret) { | |
373 | if (handle) | |
02dc1af4 | 374 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
375 | handle = ERR_PTR(ret); |
376 | } | |
377 | return handle; | |
378 | } | |
379 | ||
ccd979bd MF |
380 | static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block) |
381 | { | |
382 | sector_t status; | |
383 | u64 p_blkno = 0; | |
384 | int err = 0; | |
385 | struct inode *inode = mapping->host; | |
386 | ||
387 | mlog_entry("(block = %llu)\n", (unsigned long long)block); | |
388 | ||
389 | /* We don't need to lock journal system files, since they aren't | |
390 | * accessed concurrently from multiple nodes. | |
391 | */ | |
392 | if (!INODE_JOURNAL(inode)) { | |
4bcec184 | 393 | err = ocfs2_meta_lock(inode, NULL, 0); |
ccd979bd MF |
394 | if (err) { |
395 | if (err != -ENOENT) | |
396 | mlog_errno(err); | |
397 | goto bail; | |
398 | } | |
399 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
400 | } | |
401 | ||
49cb8d2d | 402 | err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL); |
ccd979bd MF |
403 | |
404 | if (!INODE_JOURNAL(inode)) { | |
405 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
406 | ocfs2_meta_unlock(inode, 0); | |
407 | } | |
408 | ||
409 | if (err) { | |
410 | mlog(ML_ERROR, "get_blocks() failed, block = %llu\n", | |
411 | (unsigned long long)block); | |
412 | mlog_errno(err); | |
413 | goto bail; | |
414 | } | |
415 | ||
416 | ||
417 | bail: | |
418 | status = err ? 0 : p_blkno; | |
419 | ||
420 | mlog_exit((int)status); | |
421 | ||
422 | return status; | |
423 | } | |
424 | ||
425 | /* | |
426 | * TODO: Make this into a generic get_blocks function. | |
427 | * | |
428 | * From do_direct_io in direct-io.c: | |
429 | * "So what we do is to permit the ->get_blocks function to populate | |
430 | * bh.b_size with the size of IO which is permitted at this offset and | |
431 | * this i_blkbits." | |
432 | * | |
433 | * This function is called directly from get_more_blocks in direct-io.c. | |
434 | * | |
435 | * called like this: dio->get_blocks(dio->inode, fs_startblk, | |
436 | * fs_count, map_bh, dio->rw == WRITE); | |
437 | */ | |
438 | static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock, | |
ccd979bd MF |
439 | struct buffer_head *bh_result, int create) |
440 | { | |
441 | int ret; | |
4f902c37 | 442 | u64 p_blkno, inode_blocks, contig_blocks; |
49cb8d2d | 443 | unsigned int ext_flags; |
184d7d20 | 444 | unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; |
1d8fa7a2 | 445 | unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; |
ccd979bd | 446 | |
ccd979bd MF |
447 | /* This function won't even be called if the request isn't all |
448 | * nicely aligned and of the right size, so there's no need | |
449 | * for us to check any of that. */ | |
450 | ||
25baf2da | 451 | inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode)); |
564f8a32 MF |
452 | |
453 | /* | |
454 | * Any write past EOF is not allowed because we'd be extending. | |
455 | */ | |
456 | if (create && (iblock + max_blocks) > inode_blocks) { | |
ccd979bd MF |
457 | ret = -EIO; |
458 | goto bail; | |
459 | } | |
ccd979bd MF |
460 | |
461 | /* This figures out the size of the next contiguous block, and | |
462 | * our logical offset */ | |
363041a5 | 463 | ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, |
49cb8d2d | 464 | &contig_blocks, &ext_flags); |
ccd979bd MF |
465 | if (ret) { |
466 | mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n", | |
467 | (unsigned long long)iblock); | |
468 | ret = -EIO; | |
469 | goto bail; | |
470 | } | |
471 | ||
25baf2da MF |
472 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) { |
473 | ocfs2_error(inode->i_sb, | |
474 | "Inode %llu has a hole at block %llu\n", | |
475 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
476 | (unsigned long long)iblock); | |
477 | ret = -EROFS; | |
478 | goto bail; | |
479 | } | |
480 | ||
481 | /* | |
482 | * get_more_blocks() expects us to describe a hole by clearing | |
483 | * the mapped bit on bh_result(). | |
49cb8d2d MF |
484 | * |
485 | * Consider an unwritten extent as a hole. | |
25baf2da | 486 | */ |
49cb8d2d | 487 | if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN)) |
25baf2da MF |
488 | map_bh(bh_result, inode->i_sb, p_blkno); |
489 | else { | |
490 | /* | |
491 | * ocfs2_prepare_inode_for_write() should have caught | |
492 | * the case where we'd be filling a hole and triggered | |
493 | * a buffered write instead. | |
494 | */ | |
495 | if (create) { | |
496 | ret = -EIO; | |
497 | mlog_errno(ret); | |
498 | goto bail; | |
499 | } | |
500 | ||
501 | clear_buffer_mapped(bh_result); | |
502 | } | |
ccd979bd MF |
503 | |
504 | /* make sure we don't map more than max_blocks blocks here as | |
505 | that's all the kernel will handle at this point. */ | |
506 | if (max_blocks < contig_blocks) | |
507 | contig_blocks = max_blocks; | |
508 | bh_result->b_size = contig_blocks << blocksize_bits; | |
509 | bail: | |
510 | return ret; | |
511 | } | |
512 | ||
513 | /* | |
514 | * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're | |
515 | * particularly interested in the aio/dio case. Like the core uses | |
516 | * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from | |
517 | * truncation on another. | |
518 | */ | |
519 | static void ocfs2_dio_end_io(struct kiocb *iocb, | |
520 | loff_t offset, | |
521 | ssize_t bytes, | |
522 | void *private) | |
523 | { | |
d28c9174 | 524 | struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; |
ccd979bd MF |
525 | |
526 | /* this io's submitter should not have unlocked this before we could */ | |
527 | BUG_ON(!ocfs2_iocb_is_rw_locked(iocb)); | |
528 | ocfs2_iocb_clear_rw_locked(iocb); | |
529 | up_read(&inode->i_alloc_sem); | |
530 | ocfs2_rw_unlock(inode, 0); | |
531 | } | |
532 | ||
03f981cf JB |
533 | /* |
534 | * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen | |
535 | * from ext3. PageChecked() bits have been removed as OCFS2 does not | |
536 | * do journalled data. | |
537 | */ | |
538 | static void ocfs2_invalidatepage(struct page *page, unsigned long offset) | |
539 | { | |
540 | journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal; | |
541 | ||
542 | journal_invalidatepage(journal, page, offset); | |
543 | } | |
544 | ||
545 | static int ocfs2_releasepage(struct page *page, gfp_t wait) | |
546 | { | |
547 | journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal; | |
548 | ||
549 | if (!page_has_buffers(page)) | |
550 | return 0; | |
551 | return journal_try_to_free_buffers(journal, page, wait); | |
552 | } | |
553 | ||
ccd979bd MF |
554 | static ssize_t ocfs2_direct_IO(int rw, |
555 | struct kiocb *iocb, | |
556 | const struct iovec *iov, | |
557 | loff_t offset, | |
558 | unsigned long nr_segs) | |
559 | { | |
560 | struct file *file = iocb->ki_filp; | |
d28c9174 | 561 | struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host; |
ccd979bd MF |
562 | int ret; |
563 | ||
564 | mlog_entry_void(); | |
53013cba | 565 | |
9517bac6 MF |
566 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
567 | /* | |
568 | * We get PR data locks even for O_DIRECT. This | |
569 | * allows concurrent O_DIRECT I/O but doesn't let | |
570 | * O_DIRECT with extending and buffered zeroing writes | |
571 | * race. If they did race then the buffered zeroing | |
572 | * could be written back after the O_DIRECT I/O. It's | |
573 | * one thing to tell people not to mix buffered and | |
574 | * O_DIRECT writes, but expecting them to understand | |
575 | * that file extension is also an implicit buffered | |
576 | * write is too much. By getting the PR we force | |
577 | * writeback of the buffered zeroing before | |
578 | * proceeding. | |
579 | */ | |
580 | ret = ocfs2_data_lock(inode, 0); | |
581 | if (ret < 0) { | |
582 | mlog_errno(ret); | |
583 | goto out; | |
584 | } | |
585 | ocfs2_data_unlock(inode, 0); | |
53013cba | 586 | } |
53013cba | 587 | |
ccd979bd MF |
588 | ret = blockdev_direct_IO_no_locking(rw, iocb, inode, |
589 | inode->i_sb->s_bdev, iov, offset, | |
590 | nr_segs, | |
591 | ocfs2_direct_IO_get_blocks, | |
592 | ocfs2_dio_end_io); | |
53013cba | 593 | out: |
ccd979bd MF |
594 | mlog_exit(ret); |
595 | return ret; | |
596 | } | |
597 | ||
9517bac6 MF |
598 | static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb, |
599 | u32 cpos, | |
600 | unsigned int *start, | |
601 | unsigned int *end) | |
602 | { | |
603 | unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE; | |
604 | ||
605 | if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) { | |
606 | unsigned int cpp; | |
607 | ||
608 | cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits); | |
609 | ||
610 | cluster_start = cpos % cpp; | |
611 | cluster_start = cluster_start << osb->s_clustersize_bits; | |
612 | ||
613 | cluster_end = cluster_start + osb->s_clustersize; | |
614 | } | |
615 | ||
616 | BUG_ON(cluster_start > PAGE_SIZE); | |
617 | BUG_ON(cluster_end > PAGE_SIZE); | |
618 | ||
619 | if (start) | |
620 | *start = cluster_start; | |
621 | if (end) | |
622 | *end = cluster_end; | |
623 | } | |
624 | ||
625 | /* | |
626 | * 'from' and 'to' are the region in the page to avoid zeroing. | |
627 | * | |
628 | * If pagesize > clustersize, this function will avoid zeroing outside | |
629 | * of the cluster boundary. | |
630 | * | |
631 | * from == to == 0 is code for "zero the entire cluster region" | |
632 | */ | |
633 | static void ocfs2_clear_page_regions(struct page *page, | |
634 | struct ocfs2_super *osb, u32 cpos, | |
635 | unsigned from, unsigned to) | |
636 | { | |
637 | void *kaddr; | |
638 | unsigned int cluster_start, cluster_end; | |
639 | ||
640 | ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end); | |
641 | ||
642 | kaddr = kmap_atomic(page, KM_USER0); | |
643 | ||
644 | if (from || to) { | |
645 | if (from > cluster_start) | |
646 | memset(kaddr + cluster_start, 0, from - cluster_start); | |
647 | if (to < cluster_end) | |
648 | memset(kaddr + to, 0, cluster_end - to); | |
649 | } else { | |
650 | memset(kaddr + cluster_start, 0, cluster_end - cluster_start); | |
651 | } | |
652 | ||
653 | kunmap_atomic(kaddr, KM_USER0); | |
654 | } | |
655 | ||
656 | /* | |
657 | * Some of this taken from block_prepare_write(). We already have our | |
658 | * mapping by now though, and the entire write will be allocating or | |
659 | * it won't, so not much need to use BH_New. | |
660 | * | |
661 | * This will also skip zeroing, which is handled externally. | |
662 | */ | |
60b11392 MF |
663 | int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno, |
664 | struct inode *inode, unsigned int from, | |
665 | unsigned int to, int new) | |
9517bac6 MF |
666 | { |
667 | int ret = 0; | |
668 | struct buffer_head *head, *bh, *wait[2], **wait_bh = wait; | |
669 | unsigned int block_end, block_start; | |
670 | unsigned int bsize = 1 << inode->i_blkbits; | |
671 | ||
672 | if (!page_has_buffers(page)) | |
673 | create_empty_buffers(page, bsize, 0); | |
674 | ||
675 | head = page_buffers(page); | |
676 | for (bh = head, block_start = 0; bh != head || !block_start; | |
677 | bh = bh->b_this_page, block_start += bsize) { | |
678 | block_end = block_start + bsize; | |
679 | ||
680 | /* | |
681 | * Ignore blocks outside of our i/o range - | |
682 | * they may belong to unallocated clusters. | |
683 | */ | |
60b11392 | 684 | if (block_start >= to || block_end <= from) { |
9517bac6 MF |
685 | if (PageUptodate(page)) |
686 | set_buffer_uptodate(bh); | |
687 | continue; | |
688 | } | |
689 | ||
690 | /* | |
691 | * For an allocating write with cluster size >= page | |
692 | * size, we always write the entire page. | |
693 | */ | |
694 | ||
695 | if (buffer_new(bh)) | |
696 | clear_buffer_new(bh); | |
697 | ||
698 | if (!buffer_mapped(bh)) { | |
699 | map_bh(bh, inode->i_sb, *p_blkno); | |
700 | unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); | |
701 | } | |
702 | ||
703 | if (PageUptodate(page)) { | |
704 | if (!buffer_uptodate(bh)) | |
705 | set_buffer_uptodate(bh); | |
706 | } else if (!buffer_uptodate(bh) && !buffer_delay(bh) && | |
707 | (block_start < from || block_end > to)) { | |
708 | ll_rw_block(READ, 1, &bh); | |
709 | *wait_bh++=bh; | |
710 | } | |
711 | ||
712 | *p_blkno = *p_blkno + 1; | |
713 | } | |
714 | ||
715 | /* | |
716 | * If we issued read requests - let them complete. | |
717 | */ | |
718 | while(wait_bh > wait) { | |
719 | wait_on_buffer(*--wait_bh); | |
720 | if (!buffer_uptodate(*wait_bh)) | |
721 | ret = -EIO; | |
722 | } | |
723 | ||
724 | if (ret == 0 || !new) | |
725 | return ret; | |
726 | ||
727 | /* | |
728 | * If we get -EIO above, zero out any newly allocated blocks | |
729 | * to avoid exposing stale data. | |
730 | */ | |
731 | bh = head; | |
732 | block_start = 0; | |
733 | do { | |
734 | void *kaddr; | |
735 | ||
736 | block_end = block_start + bsize; | |
737 | if (block_end <= from) | |
738 | goto next_bh; | |
739 | if (block_start >= to) | |
740 | break; | |
741 | ||
742 | kaddr = kmap_atomic(page, KM_USER0); | |
743 | memset(kaddr+block_start, 0, bh->b_size); | |
744 | flush_dcache_page(page); | |
745 | kunmap_atomic(kaddr, KM_USER0); | |
746 | set_buffer_uptodate(bh); | |
747 | mark_buffer_dirty(bh); | |
748 | ||
749 | next_bh: | |
750 | block_start = block_end; | |
751 | bh = bh->b_this_page; | |
752 | } while (bh != head); | |
753 | ||
754 | return ret; | |
755 | } | |
756 | ||
6af67d82 MF |
757 | /* |
758 | * This will copy user data from the buffer page in the splice | |
759 | * context. | |
760 | * | |
761 | * For now, we ignore SPLICE_F_MOVE as that would require some extra | |
762 | * communication out all the way to ocfs2_write(). | |
763 | */ | |
764 | int ocfs2_map_and_write_splice_data(struct inode *inode, | |
765 | struct ocfs2_write_ctxt *wc, u64 *p_blkno, | |
766 | unsigned int *ret_from, unsigned int *ret_to) | |
767 | { | |
768 | int ret; | |
769 | unsigned int to, from, cluster_start, cluster_end; | |
770 | char *src, *dst; | |
771 | struct ocfs2_splice_write_priv *sp = wc->w_private; | |
772 | struct pipe_buffer *buf = sp->s_buf; | |
773 | unsigned long bytes, src_from; | |
774 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
775 | ||
776 | ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start, | |
777 | &cluster_end); | |
778 | ||
779 | from = sp->s_offset; | |
780 | src_from = sp->s_buf_offset; | |
781 | bytes = wc->w_count; | |
782 | ||
783 | if (wc->w_large_pages) { | |
784 | /* | |
785 | * For cluster size < page size, we have to | |
786 | * calculate pos within the cluster and obey | |
787 | * the rightmost boundary. | |
788 | */ | |
789 | bytes = min(bytes, (unsigned long)(osb->s_clustersize | |
790 | - (wc->w_pos & (osb->s_clustersize - 1)))); | |
791 | } | |
792 | to = from + bytes; | |
793 | ||
794 | if (wc->w_this_page_new) | |
795 | ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode, | |
796 | cluster_start, cluster_end, 1); | |
797 | else | |
798 | ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode, | |
799 | from, to, 0); | |
800 | if (ret) { | |
801 | mlog_errno(ret); | |
802 | goto out; | |
803 | } | |
804 | ||
805 | BUG_ON(from > PAGE_CACHE_SIZE); | |
806 | BUG_ON(to > PAGE_CACHE_SIZE); | |
807 | BUG_ON(from > osb->s_clustersize); | |
808 | BUG_ON(to > osb->s_clustersize); | |
809 | ||
810 | src = buf->ops->map(sp->s_pipe, buf, 1); | |
811 | dst = kmap_atomic(wc->w_this_page, KM_USER1); | |
812 | memcpy(dst + from, src + src_from, bytes); | |
813 | kunmap_atomic(wc->w_this_page, KM_USER1); | |
814 | buf->ops->unmap(sp->s_pipe, buf, src); | |
815 | ||
816 | wc->w_finished_copy = 1; | |
817 | ||
818 | *ret_from = from; | |
819 | *ret_to = to; | |
820 | out: | |
821 | ||
822 | return bytes ? (unsigned int)bytes : ret; | |
823 | } | |
824 | ||
9517bac6 MF |
825 | /* |
826 | * This will copy user data from the iovec in the buffered write | |
827 | * context. | |
828 | */ | |
829 | int ocfs2_map_and_write_user_data(struct inode *inode, | |
830 | struct ocfs2_write_ctxt *wc, u64 *p_blkno, | |
831 | unsigned int *ret_from, unsigned int *ret_to) | |
832 | { | |
833 | int ret; | |
834 | unsigned int to, from, cluster_start, cluster_end; | |
835 | unsigned long bytes, src_from; | |
836 | char *dst; | |
837 | struct ocfs2_buffered_write_priv *bp = wc->w_private; | |
838 | const struct iovec *cur_iov = bp->b_cur_iov; | |
839 | char __user *buf; | |
840 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
841 | ||
842 | ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start, | |
843 | &cluster_end); | |
844 | ||
845 | buf = cur_iov->iov_base + bp->b_cur_off; | |
846 | src_from = (unsigned long)buf & ~PAGE_CACHE_MASK; | |
847 | ||
848 | from = wc->w_pos & (PAGE_CACHE_SIZE - 1); | |
849 | ||
850 | /* | |
851 | * This is a lot of comparisons, but it reads quite | |
852 | * easily, which is important here. | |
853 | */ | |
854 | /* Stay within the src page */ | |
855 | bytes = PAGE_SIZE - src_from; | |
856 | /* Stay within the vector */ | |
857 | bytes = min(bytes, | |
858 | (unsigned long)(cur_iov->iov_len - bp->b_cur_off)); | |
859 | /* Stay within count */ | |
860 | bytes = min(bytes, (unsigned long)wc->w_count); | |
861 | /* | |
862 | * For clustersize > page size, just stay within | |
863 | * target page, otherwise we have to calculate pos | |
864 | * within the cluster and obey the rightmost | |
865 | * boundary. | |
866 | */ | |
867 | if (wc->w_large_pages) { | |
868 | /* | |
869 | * For cluster size < page size, we have to | |
870 | * calculate pos within the cluster and obey | |
871 | * the rightmost boundary. | |
872 | */ | |
873 | bytes = min(bytes, (unsigned long)(osb->s_clustersize | |
874 | - (wc->w_pos & (osb->s_clustersize - 1)))); | |
875 | } else { | |
876 | /* | |
877 | * cluster size > page size is the most common | |
878 | * case - we just stay within the target page | |
879 | * boundary. | |
880 | */ | |
881 | bytes = min(bytes, PAGE_CACHE_SIZE - from); | |
882 | } | |
883 | ||
884 | to = from + bytes; | |
885 | ||
886 | if (wc->w_this_page_new) | |
887 | ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode, | |
888 | cluster_start, cluster_end, 1); | |
889 | else | |
890 | ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode, | |
891 | from, to, 0); | |
892 | if (ret) { | |
893 | mlog_errno(ret); | |
894 | goto out; | |
895 | } | |
896 | ||
897 | BUG_ON(from > PAGE_CACHE_SIZE); | |
898 | BUG_ON(to > PAGE_CACHE_SIZE); | |
899 | BUG_ON(from > osb->s_clustersize); | |
900 | BUG_ON(to > osb->s_clustersize); | |
901 | ||
902 | dst = kmap(wc->w_this_page); | |
903 | memcpy(dst + from, bp->b_src_buf + src_from, bytes); | |
904 | kunmap(wc->w_this_page); | |
905 | ||
906 | /* | |
907 | * XXX: This is slow, but simple. The caller of | |
908 | * ocfs2_buffered_write_cluster() is responsible for | |
909 | * passing through the iovecs, so it's difficult to | |
910 | * predict what our next step is in here after our | |
911 | * initial write. A future version should be pushing | |
912 | * that iovec manipulation further down. | |
913 | * | |
914 | * By setting this, we indicate that a copy from user | |
915 | * data was done, and subsequent calls for this | |
916 | * cluster will skip copying more data. | |
917 | */ | |
918 | wc->w_finished_copy = 1; | |
919 | ||
920 | *ret_from = from; | |
921 | *ret_to = to; | |
922 | out: | |
923 | ||
924 | return bytes ? (unsigned int)bytes : ret; | |
925 | } | |
926 | ||
927 | /* | |
928 | * Map, fill and write a page to disk. | |
929 | * | |
930 | * The work of copying data is done via callback. Newly allocated | |
931 | * pages which don't take user data will be zero'd (set 'new' to | |
932 | * indicate an allocating write) | |
933 | * | |
934 | * Returns a negative error code or the number of bytes copied into | |
935 | * the page. | |
936 | */ | |
937 | int ocfs2_write_data_page(struct inode *inode, handle_t *handle, | |
938 | u64 *p_blkno, struct page *page, | |
939 | struct ocfs2_write_ctxt *wc, int new) | |
940 | { | |
941 | int ret, copied = 0; | |
942 | unsigned int from = 0, to = 0; | |
943 | unsigned int cluster_start, cluster_end; | |
944 | unsigned int zero_from = 0, zero_to = 0; | |
945 | ||
946 | ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), wc->w_cpos, | |
947 | &cluster_start, &cluster_end); | |
948 | ||
949 | if ((wc->w_pos >> PAGE_CACHE_SHIFT) == page->index | |
950 | && !wc->w_finished_copy) { | |
951 | ||
952 | wc->w_this_page = page; | |
953 | wc->w_this_page_new = new; | |
954 | ret = wc->w_write_data_page(inode, wc, p_blkno, &from, &to); | |
955 | if (ret < 0) { | |
956 | mlog_errno(ret); | |
957 | goto out; | |
958 | } | |
959 | ||
960 | copied = ret; | |
961 | ||
962 | zero_from = from; | |
963 | zero_to = to; | |
964 | if (new) { | |
965 | from = cluster_start; | |
966 | to = cluster_end; | |
967 | } | |
968 | } else { | |
969 | /* | |
970 | * If we haven't allocated the new page yet, we | |
971 | * shouldn't be writing it out without copying user | |
972 | * data. This is likely a math error from the caller. | |
973 | */ | |
974 | BUG_ON(!new); | |
975 | ||
976 | from = cluster_start; | |
977 | to = cluster_end; | |
978 | ||
979 | ret = ocfs2_map_page_blocks(page, p_blkno, inode, | |
980 | cluster_start, cluster_end, 1); | |
981 | if (ret) { | |
982 | mlog_errno(ret); | |
983 | goto out; | |
984 | } | |
985 | } | |
986 | ||
987 | /* | |
988 | * Parts of newly allocated pages need to be zero'd. | |
989 | * | |
990 | * Above, we have also rewritten 'to' and 'from' - as far as | |
991 | * the rest of the function is concerned, the entire cluster | |
992 | * range inside of a page needs to be written. | |
993 | * | |
994 | * We can skip this if the page is up to date - it's already | |
995 | * been zero'd from being read in as a hole. | |
996 | */ | |
997 | if (new && !PageUptodate(page)) | |
998 | ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb), | |
999 | wc->w_cpos, zero_from, zero_to); | |
1000 | ||
1001 | flush_dcache_page(page); | |
1002 | ||
1003 | if (ocfs2_should_order_data(inode)) { | |
1004 | ret = walk_page_buffers(handle, | |
1005 | page_buffers(page), | |
1006 | from, to, NULL, | |
1007 | ocfs2_journal_dirty_data); | |
1008 | if (ret < 0) | |
1009 | mlog_errno(ret); | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * We don't use generic_commit_write() because we need to | |
1014 | * handle our own i_size update. | |
1015 | */ | |
1016 | ret = block_commit_write(page, from, to); | |
1017 | if (ret) | |
1018 | mlog_errno(ret); | |
1019 | out: | |
1020 | ||
1021 | return copied ? copied : ret; | |
1022 | } | |
1023 | ||
1024 | /* | |
1025 | * Do the actual write of some data into an inode. Optionally allocate | |
1026 | * in order to fulfill the write. | |
1027 | * | |
1028 | * cpos is the logical cluster offset within the file to write at | |
1029 | * | |
1030 | * 'phys' is the physical mapping of that offset. a 'phys' value of | |
1031 | * zero indicates that allocation is required. In this case, data_ac | |
1032 | * and meta_ac should be valid (meta_ac can be null if metadata | |
1033 | * allocation isn't required). | |
1034 | */ | |
1035 | static ssize_t ocfs2_write(struct file *file, u32 phys, handle_t *handle, | |
1036 | struct buffer_head *di_bh, | |
1037 | struct ocfs2_alloc_context *data_ac, | |
1038 | struct ocfs2_alloc_context *meta_ac, | |
1039 | struct ocfs2_write_ctxt *wc) | |
1040 | { | |
1041 | int ret, i, numpages = 1, new; | |
1042 | unsigned int copied = 0; | |
1043 | u32 tmp_pos; | |
1044 | u64 v_blkno, p_blkno; | |
1045 | struct address_space *mapping = file->f_mapping; | |
1046 | struct inode *inode = mapping->host; | |
9517bac6 MF |
1047 | unsigned long index, start; |
1048 | struct page **cpages; | |
1049 | ||
1050 | new = phys == 0 ? 1 : 0; | |
1051 | ||
1052 | /* | |
1053 | * Figure out how many pages we'll be manipulating here. For | |
60b11392 MF |
1054 | * non allocating write, we just change the one |
1055 | * page. Otherwise, we'll need a whole clusters worth. | |
9517bac6 | 1056 | */ |
60b11392 MF |
1057 | if (new) |
1058 | numpages = ocfs2_pages_per_cluster(inode->i_sb); | |
9517bac6 MF |
1059 | |
1060 | cpages = kzalloc(sizeof(*cpages) * numpages, GFP_NOFS); | |
1061 | if (!cpages) { | |
1062 | ret = -ENOMEM; | |
1063 | mlog_errno(ret); | |
1064 | return ret; | |
1065 | } | |
1066 | ||
1067 | /* | |
1068 | * Fill our page array first. That way we've grabbed enough so | |
1069 | * that we can zero and flush if we error after adding the | |
1070 | * extent. | |
1071 | */ | |
1072 | if (new) { | |
1073 | start = ocfs2_align_clusters_to_page_index(inode->i_sb, | |
1074 | wc->w_cpos); | |
1075 | v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, wc->w_cpos); | |
1076 | } else { | |
1077 | start = wc->w_pos >> PAGE_CACHE_SHIFT; | |
1078 | v_blkno = wc->w_pos >> inode->i_sb->s_blocksize_bits; | |
1079 | } | |
1080 | ||
1081 | for(i = 0; i < numpages; i++) { | |
1082 | index = start + i; | |
1083 | ||
1084 | cpages[i] = grab_cache_page(mapping, index); | |
1085 | if (!cpages[i]) { | |
1086 | ret = -ENOMEM; | |
1087 | mlog_errno(ret); | |
1088 | goto out; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | if (new) { | |
1093 | /* | |
1094 | * This is safe to call with the page locks - it won't take | |
1095 | * any additional semaphores or cluster locks. | |
1096 | */ | |
1097 | tmp_pos = wc->w_cpos; | |
1098 | ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode, | |
1099 | &tmp_pos, 1, di_bh, handle, | |
1100 | data_ac, meta_ac, NULL); | |
1101 | /* | |
1102 | * This shouldn't happen because we must have already | |
1103 | * calculated the correct meta data allocation required. The | |
1104 | * internal tree allocation code should know how to increase | |
1105 | * transaction credits itself. | |
1106 | * | |
1107 | * If need be, we could handle -EAGAIN for a | |
1108 | * RESTART_TRANS here. | |
1109 | */ | |
1110 | mlog_bug_on_msg(ret == -EAGAIN, | |
1111 | "Inode %llu: EAGAIN return during allocation.\n", | |
1112 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
1113 | if (ret < 0) { | |
1114 | mlog_errno(ret); | |
1115 | goto out; | |
1116 | } | |
1117 | } | |
1118 | ||
49cb8d2d MF |
1119 | ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL, |
1120 | NULL); | |
9517bac6 MF |
1121 | if (ret < 0) { |
1122 | ||
1123 | /* | |
1124 | * XXX: Should we go readonly here? | |
1125 | */ | |
1126 | ||
1127 | mlog_errno(ret); | |
1128 | goto out; | |
1129 | } | |
1130 | ||
1131 | BUG_ON(p_blkno == 0); | |
1132 | ||
1133 | for(i = 0; i < numpages; i++) { | |
1134 | ret = ocfs2_write_data_page(inode, handle, &p_blkno, cpages[i], | |
1135 | wc, new); | |
1136 | if (ret < 0) { | |
1137 | mlog_errno(ret); | |
1138 | goto out; | |
1139 | } | |
1140 | ||
1141 | copied += ret; | |
1142 | } | |
1143 | ||
1144 | out: | |
1145 | for(i = 0; i < numpages; i++) { | |
1146 | unlock_page(cpages[i]); | |
1147 | mark_page_accessed(cpages[i]); | |
1148 | page_cache_release(cpages[i]); | |
1149 | } | |
1150 | kfree(cpages); | |
1151 | ||
1152 | return copied ? copied : ret; | |
1153 | } | |
1154 | ||
1155 | static void ocfs2_write_ctxt_init(struct ocfs2_write_ctxt *wc, | |
1156 | struct ocfs2_super *osb, loff_t pos, | |
1157 | size_t count, ocfs2_page_writer *cb, | |
1158 | void *cb_priv) | |
1159 | { | |
1160 | wc->w_count = count; | |
1161 | wc->w_pos = pos; | |
1162 | wc->w_cpos = wc->w_pos >> osb->s_clustersize_bits; | |
1163 | wc->w_finished_copy = 0; | |
1164 | ||
1165 | if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) | |
1166 | wc->w_large_pages = 1; | |
1167 | else | |
1168 | wc->w_large_pages = 0; | |
1169 | ||
1170 | wc->w_write_data_page = cb; | |
1171 | wc->w_private = cb_priv; | |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * Write a cluster to an inode. The cluster may not be allocated yet, | |
1176 | * in which case it will be. This only exists for buffered writes - | |
1177 | * O_DIRECT takes a more "traditional" path through the kernel. | |
1178 | * | |
1179 | * The caller is responsible for incrementing pos, written counts, etc | |
1180 | * | |
1181 | * For file systems that don't support sparse files, pre-allocation | |
1182 | * and page zeroing up until cpos should be done prior to this | |
1183 | * function call. | |
1184 | * | |
1185 | * Callers should be holding i_sem, and the rw cluster lock. | |
1186 | * | |
1187 | * Returns the number of user bytes written, or less than zero for | |
1188 | * error. | |
1189 | */ | |
1190 | ssize_t ocfs2_buffered_write_cluster(struct file *file, loff_t pos, | |
1191 | size_t count, ocfs2_page_writer *actor, | |
1192 | void *priv) | |
1193 | { | |
1194 | int ret, credits = OCFS2_INODE_UPDATE_CREDITS; | |
1195 | ssize_t written = 0; | |
1196 | u32 phys; | |
1197 | struct inode *inode = file->f_mapping->host; | |
1198 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1199 | struct buffer_head *di_bh = NULL; | |
1200 | struct ocfs2_dinode *di; | |
1201 | struct ocfs2_alloc_context *data_ac = NULL; | |
1202 | struct ocfs2_alloc_context *meta_ac = NULL; | |
1203 | handle_t *handle; | |
1204 | struct ocfs2_write_ctxt wc; | |
1205 | ||
1206 | ocfs2_write_ctxt_init(&wc, osb, pos, count, actor, priv); | |
1207 | ||
1208 | ret = ocfs2_meta_lock(inode, &di_bh, 1); | |
1209 | if (ret) { | |
1210 | mlog_errno(ret); | |
1211 | goto out; | |
1212 | } | |
1213 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
1214 | ||
1215 | /* | |
1216 | * Take alloc sem here to prevent concurrent lookups. That way | |
1217 | * the mapping, zeroing and tree manipulation within | |
1218 | * ocfs2_write() will be safe against ->readpage(). This | |
1219 | * should also serve to lock out allocation from a shared | |
1220 | * writeable region. | |
1221 | */ | |
1222 | down_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1223 | ||
49cb8d2d | 1224 | ret = ocfs2_get_clusters(inode, wc.w_cpos, &phys, NULL, NULL); |
9517bac6 MF |
1225 | if (ret) { |
1226 | mlog_errno(ret); | |
1227 | goto out_meta; | |
1228 | } | |
1229 | ||
1230 | /* phys == 0 means that allocation is required. */ | |
1231 | if (phys == 0) { | |
1232 | ret = ocfs2_lock_allocators(inode, di, 1, &data_ac, &meta_ac); | |
1233 | if (ret) { | |
1234 | mlog_errno(ret); | |
1235 | goto out_meta; | |
1236 | } | |
1237 | ||
1238 | credits = ocfs2_calc_extend_credits(inode->i_sb, di, 1); | |
1239 | } | |
1240 | ||
1241 | ret = ocfs2_data_lock(inode, 1); | |
1242 | if (ret) { | |
1243 | mlog_errno(ret); | |
1244 | goto out_meta; | |
1245 | } | |
1246 | ||
1247 | handle = ocfs2_start_trans(osb, credits); | |
1248 | if (IS_ERR(handle)) { | |
1249 | ret = PTR_ERR(handle); | |
1250 | mlog_errno(ret); | |
1251 | goto out_data; | |
1252 | } | |
1253 | ||
1254 | written = ocfs2_write(file, phys, handle, di_bh, data_ac, | |
1255 | meta_ac, &wc); | |
1256 | if (written < 0) { | |
1257 | ret = written; | |
1258 | mlog_errno(ret); | |
1259 | goto out_commit; | |
1260 | } | |
1261 | ||
1262 | ret = ocfs2_journal_access(handle, inode, di_bh, | |
1263 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1264 | if (ret) { | |
1265 | mlog_errno(ret); | |
1266 | goto out_commit; | |
1267 | } | |
1268 | ||
1269 | pos += written; | |
1270 | if (pos > inode->i_size) { | |
1271 | i_size_write(inode, pos); | |
1272 | mark_inode_dirty(inode); | |
1273 | } | |
8110b073 | 1274 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
9517bac6 MF |
1275 | di->i_size = cpu_to_le64((u64)i_size_read(inode)); |
1276 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
1277 | di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec); | |
1278 | di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); | |
1279 | ||
1280 | ret = ocfs2_journal_dirty(handle, di_bh); | |
1281 | if (ret) | |
1282 | mlog_errno(ret); | |
1283 | ||
1284 | out_commit: | |
1285 | ocfs2_commit_trans(osb, handle); | |
1286 | ||
1287 | out_data: | |
1288 | ocfs2_data_unlock(inode, 1); | |
1289 | ||
1290 | out_meta: | |
1291 | up_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1292 | ocfs2_meta_unlock(inode, 1); | |
1293 | ||
1294 | out: | |
1295 | brelse(di_bh); | |
1296 | if (data_ac) | |
1297 | ocfs2_free_alloc_context(data_ac); | |
1298 | if (meta_ac) | |
1299 | ocfs2_free_alloc_context(meta_ac); | |
1300 | ||
1301 | return written ? written : ret; | |
1302 | } | |
1303 | ||
f5e54d6e | 1304 | const struct address_space_operations ocfs2_aops = { |
ccd979bd MF |
1305 | .readpage = ocfs2_readpage, |
1306 | .writepage = ocfs2_writepage, | |
ccd979bd MF |
1307 | .bmap = ocfs2_bmap, |
1308 | .sync_page = block_sync_page, | |
03f981cf JB |
1309 | .direct_IO = ocfs2_direct_IO, |
1310 | .invalidatepage = ocfs2_invalidatepage, | |
1311 | .releasepage = ocfs2_releasepage, | |
1312 | .migratepage = buffer_migrate_page, | |
ccd979bd | 1313 | }; |