]>
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 | 80 | mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n", |
1ca1a111 MF |
81 | (unsigned long long)le64_to_cpu(fe->i_blkno), 7, |
82 | fe->i_signature); | |
ccd979bd MF |
83 | goto bail; |
84 | } | |
85 | ||
86 | if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb, | |
87 | le32_to_cpu(fe->i_clusters))) { | |
88 | mlog(ML_ERROR, "block offset is outside the allocated size: " | |
89 | "%llu\n", (unsigned long long)iblock); | |
90 | goto bail; | |
91 | } | |
92 | ||
93 | /* We don't use the page cache to create symlink data, so if | |
94 | * need be, copy it over from the buffer cache. */ | |
95 | if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) { | |
96 | u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + | |
97 | iblock; | |
98 | buffer_cache_bh = sb_getblk(osb->sb, blkno); | |
99 | if (!buffer_cache_bh) { | |
100 | mlog(ML_ERROR, "couldn't getblock for symlink!\n"); | |
101 | goto bail; | |
102 | } | |
103 | ||
104 | /* we haven't locked out transactions, so a commit | |
105 | * could've happened. Since we've got a reference on | |
106 | * the bh, even if it commits while we're doing the | |
107 | * copy, the data is still good. */ | |
108 | if (buffer_jbd(buffer_cache_bh) | |
109 | && ocfs2_inode_is_new(inode)) { | |
110 | kaddr = kmap_atomic(bh_result->b_page, KM_USER0); | |
111 | if (!kaddr) { | |
112 | mlog(ML_ERROR, "couldn't kmap!\n"); | |
113 | goto bail; | |
114 | } | |
115 | memcpy(kaddr + (bh_result->b_size * iblock), | |
116 | buffer_cache_bh->b_data, | |
117 | bh_result->b_size); | |
118 | kunmap_atomic(kaddr, KM_USER0); | |
119 | set_buffer_uptodate(bh_result); | |
120 | } | |
121 | brelse(buffer_cache_bh); | |
122 | } | |
123 | ||
124 | map_bh(bh_result, inode->i_sb, | |
125 | le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock); | |
126 | ||
127 | err = 0; | |
128 | ||
129 | bail: | |
130 | if (bh) | |
131 | brelse(bh); | |
132 | ||
133 | mlog_exit(err); | |
134 | return err; | |
135 | } | |
136 | ||
137 | static int ocfs2_get_block(struct inode *inode, sector_t iblock, | |
138 | struct buffer_head *bh_result, int create) | |
139 | { | |
140 | int err = 0; | |
49cb8d2d | 141 | unsigned int ext_flags; |
ccd979bd | 142 | u64 p_blkno, past_eof; |
25baf2da | 143 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
ccd979bd MF |
144 | |
145 | mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode, | |
146 | (unsigned long long)iblock, bh_result, create); | |
147 | ||
148 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) | |
149 | mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n", | |
150 | inode, inode->i_ino); | |
151 | ||
152 | if (S_ISLNK(inode->i_mode)) { | |
153 | /* this always does I/O for some reason. */ | |
154 | err = ocfs2_symlink_get_block(inode, iblock, bh_result, create); | |
155 | goto bail; | |
156 | } | |
157 | ||
49cb8d2d MF |
158 | err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL, |
159 | &ext_flags); | |
ccd979bd MF |
160 | if (err) { |
161 | mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, " | |
b0697053 MF |
162 | "%llu, NULL)\n", err, inode, (unsigned long long)iblock, |
163 | (unsigned long long)p_blkno); | |
ccd979bd MF |
164 | goto bail; |
165 | } | |
166 | ||
25baf2da MF |
167 | /* |
168 | * ocfs2 never allocates in this function - the only time we | |
169 | * need to use BH_New is when we're extending i_size on a file | |
170 | * system which doesn't support holes, in which case BH_New | |
171 | * allows block_prepare_write() to zero. | |
172 | */ | |
173 | mlog_bug_on_msg(create && p_blkno == 0 && ocfs2_sparse_alloc(osb), | |
174 | "ino %lu, iblock %llu\n", inode->i_ino, | |
175 | (unsigned long long)iblock); | |
176 | ||
49cb8d2d MF |
177 | /* Treat the unwritten extent as a hole for zeroing purposes. */ |
178 | if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN)) | |
25baf2da MF |
179 | map_bh(bh_result, inode->i_sb, p_blkno); |
180 | ||
181 | if (!ocfs2_sparse_alloc(osb)) { | |
182 | if (p_blkno == 0) { | |
183 | err = -EIO; | |
184 | mlog(ML_ERROR, | |
185 | "iblock = %llu p_blkno = %llu blkno=(%llu)\n", | |
186 | (unsigned long long)iblock, | |
187 | (unsigned long long)p_blkno, | |
188 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
189 | mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters); | |
190 | dump_stack(); | |
191 | } | |
ccd979bd | 192 | |
25baf2da MF |
193 | past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode)); |
194 | mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino, | |
195 | (unsigned long long)past_eof); | |
ccd979bd | 196 | |
25baf2da MF |
197 | if (create && (iblock >= past_eof)) |
198 | set_buffer_new(bh_result); | |
199 | } | |
ccd979bd MF |
200 | |
201 | bail: | |
202 | if (err < 0) | |
203 | err = -EIO; | |
204 | ||
205 | mlog_exit(err); | |
206 | return err; | |
207 | } | |
208 | ||
1afc32b9 MF |
209 | int ocfs2_read_inline_data(struct inode *inode, struct page *page, |
210 | struct buffer_head *di_bh) | |
6798d35a MF |
211 | { |
212 | void *kaddr; | |
213 | unsigned int size; | |
214 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
215 | ||
216 | if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) { | |
217 | ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag", | |
218 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
219 | return -EROFS; | |
220 | } | |
221 | ||
222 | size = i_size_read(inode); | |
223 | ||
224 | if (size > PAGE_CACHE_SIZE || | |
225 | size > ocfs2_max_inline_data(inode->i_sb)) { | |
226 | ocfs2_error(inode->i_sb, | |
227 | "Inode %llu has with inline data has bad size: %u", | |
228 | (unsigned long long)OCFS2_I(inode)->ip_blkno, size); | |
229 | return -EROFS; | |
230 | } | |
231 | ||
232 | kaddr = kmap_atomic(page, KM_USER0); | |
233 | if (size) | |
234 | memcpy(kaddr, di->id2.i_data.id_data, size); | |
235 | /* Clear the remaining part of the page */ | |
236 | memset(kaddr + size, 0, PAGE_CACHE_SIZE - size); | |
237 | flush_dcache_page(page); | |
238 | kunmap_atomic(kaddr, KM_USER0); | |
239 | ||
240 | SetPageUptodate(page); | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
245 | static int ocfs2_readpage_inline(struct inode *inode, struct page *page) | |
246 | { | |
247 | int ret; | |
248 | struct buffer_head *di_bh = NULL; | |
249 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
250 | ||
251 | BUG_ON(!PageLocked(page)); | |
252 | BUG_ON(!OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL); | |
253 | ||
254 | ret = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &di_bh, | |
255 | OCFS2_BH_CACHED, inode); | |
256 | if (ret) { | |
257 | mlog_errno(ret); | |
258 | goto out; | |
259 | } | |
260 | ||
261 | ret = ocfs2_read_inline_data(inode, page, di_bh); | |
262 | out: | |
263 | unlock_page(page); | |
264 | ||
265 | brelse(di_bh); | |
266 | return ret; | |
267 | } | |
268 | ||
ccd979bd MF |
269 | static int ocfs2_readpage(struct file *file, struct page *page) |
270 | { | |
271 | struct inode *inode = page->mapping->host; | |
6798d35a | 272 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
ccd979bd MF |
273 | loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT; |
274 | int ret, unlock = 1; | |
275 | ||
276 | mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0)); | |
277 | ||
e63aecb6 | 278 | ret = ocfs2_inode_lock_with_page(inode, NULL, 0, page); |
ccd979bd MF |
279 | if (ret != 0) { |
280 | if (ret == AOP_TRUNCATED_PAGE) | |
281 | unlock = 0; | |
282 | mlog_errno(ret); | |
283 | goto out; | |
284 | } | |
285 | ||
6798d35a | 286 | if (down_read_trylock(&oi->ip_alloc_sem) == 0) { |
e9dfc0b2 | 287 | ret = AOP_TRUNCATED_PAGE; |
e63aecb6 | 288 | goto out_inode_unlock; |
e9dfc0b2 | 289 | } |
ccd979bd MF |
290 | |
291 | /* | |
292 | * i_size might have just been updated as we grabed the meta lock. We | |
293 | * might now be discovering a truncate that hit on another node. | |
294 | * block_read_full_page->get_block freaks out if it is asked to read | |
295 | * beyond the end of a file, so we check here. Callers | |
54cb8821 | 296 | * (generic_file_read, vm_ops->fault) are clever enough to check i_size |
ccd979bd MF |
297 | * and notice that the page they just read isn't needed. |
298 | * | |
299 | * XXX sys_readahead() seems to get that wrong? | |
300 | */ | |
301 | if (start >= i_size_read(inode)) { | |
5c3c6bb7 | 302 | zero_user_page(page, 0, PAGE_SIZE, KM_USER0); |
ccd979bd MF |
303 | SetPageUptodate(page); |
304 | ret = 0; | |
305 | goto out_alloc; | |
306 | } | |
307 | ||
6798d35a MF |
308 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
309 | ret = ocfs2_readpage_inline(inode, page); | |
310 | else | |
311 | ret = block_read_full_page(page, ocfs2_get_block); | |
ccd979bd MF |
312 | unlock = 0; |
313 | ||
ccd979bd MF |
314 | out_alloc: |
315 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
e63aecb6 MF |
316 | out_inode_unlock: |
317 | ocfs2_inode_unlock(inode, 0); | |
ccd979bd MF |
318 | out: |
319 | if (unlock) | |
320 | unlock_page(page); | |
321 | mlog_exit(ret); | |
322 | return ret; | |
323 | } | |
324 | ||
325 | /* Note: Because we don't support holes, our allocation has | |
326 | * already happened (allocation writes zeros to the file data) | |
327 | * so we don't have to worry about ordered writes in | |
328 | * ocfs2_writepage. | |
329 | * | |
330 | * ->writepage is called during the process of invalidating the page cache | |
331 | * during blocked lock processing. It can't block on any cluster locks | |
332 | * to during block mapping. It's relying on the fact that the block | |
333 | * mapping can't have disappeared under the dirty pages that it is | |
334 | * being asked to write back. | |
335 | */ | |
336 | static int ocfs2_writepage(struct page *page, struct writeback_control *wbc) | |
337 | { | |
338 | int ret; | |
339 | ||
340 | mlog_entry("(0x%p)\n", page); | |
341 | ||
342 | ret = block_write_full_page(page, ocfs2_get_block, wbc); | |
343 | ||
344 | mlog_exit(ret); | |
345 | ||
346 | return ret; | |
347 | } | |
348 | ||
5069120b MF |
349 | /* |
350 | * This is called from ocfs2_write_zero_page() which has handled it's | |
351 | * own cluster locking and has ensured allocation exists for those | |
352 | * blocks to be written. | |
353 | */ | |
53013cba MF |
354 | int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page, |
355 | unsigned from, unsigned to) | |
356 | { | |
357 | int ret; | |
358 | ||
53013cba MF |
359 | ret = block_prepare_write(page, from, to, ocfs2_get_block); |
360 | ||
53013cba MF |
361 | return ret; |
362 | } | |
363 | ||
ccd979bd MF |
364 | /* Taken from ext3. We don't necessarily need the full blown |
365 | * functionality yet, but IMHO it's better to cut and paste the whole | |
366 | * thing so we can avoid introducing our own bugs (and easily pick up | |
367 | * their fixes when they happen) --Mark */ | |
60b11392 MF |
368 | int walk_page_buffers( handle_t *handle, |
369 | struct buffer_head *head, | |
370 | unsigned from, | |
371 | unsigned to, | |
372 | int *partial, | |
373 | int (*fn)( handle_t *handle, | |
374 | struct buffer_head *bh)) | |
ccd979bd MF |
375 | { |
376 | struct buffer_head *bh; | |
377 | unsigned block_start, block_end; | |
378 | unsigned blocksize = head->b_size; | |
379 | int err, ret = 0; | |
380 | struct buffer_head *next; | |
381 | ||
382 | for ( bh = head, block_start = 0; | |
383 | ret == 0 && (bh != head || !block_start); | |
384 | block_start = block_end, bh = next) | |
385 | { | |
386 | next = bh->b_this_page; | |
387 | block_end = block_start + blocksize; | |
388 | if (block_end <= from || block_start >= to) { | |
389 | if (partial && !buffer_uptodate(bh)) | |
390 | *partial = 1; | |
391 | continue; | |
392 | } | |
393 | err = (*fn)(handle, bh); | |
394 | if (!ret) | |
395 | ret = err; | |
396 | } | |
397 | return ret; | |
398 | } | |
399 | ||
1fabe148 | 400 | handle_t *ocfs2_start_walk_page_trans(struct inode *inode, |
ccd979bd MF |
401 | struct page *page, |
402 | unsigned from, | |
403 | unsigned to) | |
404 | { | |
405 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1fabe148 | 406 | handle_t *handle = NULL; |
ccd979bd MF |
407 | int ret = 0; |
408 | ||
65eff9cc | 409 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
410 | if (!handle) { |
411 | ret = -ENOMEM; | |
412 | mlog_errno(ret); | |
413 | goto out; | |
414 | } | |
415 | ||
416 | if (ocfs2_should_order_data(inode)) { | |
1fabe148 | 417 | ret = walk_page_buffers(handle, |
ccd979bd MF |
418 | page_buffers(page), |
419 | from, to, NULL, | |
420 | ocfs2_journal_dirty_data); | |
421 | if (ret < 0) | |
422 | mlog_errno(ret); | |
423 | } | |
424 | out: | |
425 | if (ret) { | |
426 | if (handle) | |
02dc1af4 | 427 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
428 | handle = ERR_PTR(ret); |
429 | } | |
430 | return handle; | |
431 | } | |
432 | ||
ccd979bd MF |
433 | static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block) |
434 | { | |
435 | sector_t status; | |
436 | u64 p_blkno = 0; | |
437 | int err = 0; | |
438 | struct inode *inode = mapping->host; | |
439 | ||
440 | mlog_entry("(block = %llu)\n", (unsigned long long)block); | |
441 | ||
442 | /* We don't need to lock journal system files, since they aren't | |
443 | * accessed concurrently from multiple nodes. | |
444 | */ | |
445 | if (!INODE_JOURNAL(inode)) { | |
e63aecb6 | 446 | err = ocfs2_inode_lock(inode, NULL, 0); |
ccd979bd MF |
447 | if (err) { |
448 | if (err != -ENOENT) | |
449 | mlog_errno(err); | |
450 | goto bail; | |
451 | } | |
452 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
453 | } | |
454 | ||
6798d35a MF |
455 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)) |
456 | err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, | |
457 | NULL); | |
ccd979bd MF |
458 | |
459 | if (!INODE_JOURNAL(inode)) { | |
460 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
e63aecb6 | 461 | ocfs2_inode_unlock(inode, 0); |
ccd979bd MF |
462 | } |
463 | ||
464 | if (err) { | |
465 | mlog(ML_ERROR, "get_blocks() failed, block = %llu\n", | |
466 | (unsigned long long)block); | |
467 | mlog_errno(err); | |
468 | goto bail; | |
469 | } | |
470 | ||
ccd979bd MF |
471 | bail: |
472 | status = err ? 0 : p_blkno; | |
473 | ||
474 | mlog_exit((int)status); | |
475 | ||
476 | return status; | |
477 | } | |
478 | ||
479 | /* | |
480 | * TODO: Make this into a generic get_blocks function. | |
481 | * | |
482 | * From do_direct_io in direct-io.c: | |
483 | * "So what we do is to permit the ->get_blocks function to populate | |
484 | * bh.b_size with the size of IO which is permitted at this offset and | |
485 | * this i_blkbits." | |
486 | * | |
487 | * This function is called directly from get_more_blocks in direct-io.c. | |
488 | * | |
489 | * called like this: dio->get_blocks(dio->inode, fs_startblk, | |
490 | * fs_count, map_bh, dio->rw == WRITE); | |
491 | */ | |
492 | static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock, | |
ccd979bd MF |
493 | struct buffer_head *bh_result, int create) |
494 | { | |
495 | int ret; | |
4f902c37 | 496 | u64 p_blkno, inode_blocks, contig_blocks; |
49cb8d2d | 497 | unsigned int ext_flags; |
184d7d20 | 498 | unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; |
1d8fa7a2 | 499 | unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; |
ccd979bd | 500 | |
ccd979bd MF |
501 | /* This function won't even be called if the request isn't all |
502 | * nicely aligned and of the right size, so there's no need | |
503 | * for us to check any of that. */ | |
504 | ||
25baf2da | 505 | inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode)); |
564f8a32 MF |
506 | |
507 | /* | |
508 | * Any write past EOF is not allowed because we'd be extending. | |
509 | */ | |
510 | if (create && (iblock + max_blocks) > inode_blocks) { | |
ccd979bd MF |
511 | ret = -EIO; |
512 | goto bail; | |
513 | } | |
ccd979bd MF |
514 | |
515 | /* This figures out the size of the next contiguous block, and | |
516 | * our logical offset */ | |
363041a5 | 517 | ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, |
49cb8d2d | 518 | &contig_blocks, &ext_flags); |
ccd979bd MF |
519 | if (ret) { |
520 | mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n", | |
521 | (unsigned long long)iblock); | |
522 | ret = -EIO; | |
523 | goto bail; | |
524 | } | |
525 | ||
25baf2da MF |
526 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) { |
527 | ocfs2_error(inode->i_sb, | |
528 | "Inode %llu has a hole at block %llu\n", | |
529 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
530 | (unsigned long long)iblock); | |
531 | ret = -EROFS; | |
532 | goto bail; | |
533 | } | |
534 | ||
535 | /* | |
536 | * get_more_blocks() expects us to describe a hole by clearing | |
537 | * the mapped bit on bh_result(). | |
49cb8d2d MF |
538 | * |
539 | * Consider an unwritten extent as a hole. | |
25baf2da | 540 | */ |
49cb8d2d | 541 | if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN)) |
25baf2da MF |
542 | map_bh(bh_result, inode->i_sb, p_blkno); |
543 | else { | |
544 | /* | |
545 | * ocfs2_prepare_inode_for_write() should have caught | |
546 | * the case where we'd be filling a hole and triggered | |
547 | * a buffered write instead. | |
548 | */ | |
549 | if (create) { | |
550 | ret = -EIO; | |
551 | mlog_errno(ret); | |
552 | goto bail; | |
553 | } | |
554 | ||
555 | clear_buffer_mapped(bh_result); | |
556 | } | |
ccd979bd MF |
557 | |
558 | /* make sure we don't map more than max_blocks blocks here as | |
559 | that's all the kernel will handle at this point. */ | |
560 | if (max_blocks < contig_blocks) | |
561 | contig_blocks = max_blocks; | |
562 | bh_result->b_size = contig_blocks << blocksize_bits; | |
563 | bail: | |
564 | return ret; | |
565 | } | |
566 | ||
567 | /* | |
568 | * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're | |
569 | * particularly interested in the aio/dio case. Like the core uses | |
570 | * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from | |
571 | * truncation on another. | |
572 | */ | |
573 | static void ocfs2_dio_end_io(struct kiocb *iocb, | |
574 | loff_t offset, | |
575 | ssize_t bytes, | |
576 | void *private) | |
577 | { | |
d28c9174 | 578 | struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; |
7cdfc3a1 | 579 | int level; |
ccd979bd MF |
580 | |
581 | /* this io's submitter should not have unlocked this before we could */ | |
582 | BUG_ON(!ocfs2_iocb_is_rw_locked(iocb)); | |
7cdfc3a1 | 583 | |
ccd979bd | 584 | ocfs2_iocb_clear_rw_locked(iocb); |
7cdfc3a1 MF |
585 | |
586 | level = ocfs2_iocb_rw_locked_level(iocb); | |
587 | if (!level) | |
588 | up_read(&inode->i_alloc_sem); | |
589 | ocfs2_rw_unlock(inode, level); | |
ccd979bd MF |
590 | } |
591 | ||
03f981cf JB |
592 | /* |
593 | * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen | |
594 | * from ext3. PageChecked() bits have been removed as OCFS2 does not | |
595 | * do journalled data. | |
596 | */ | |
597 | static void ocfs2_invalidatepage(struct page *page, unsigned long offset) | |
598 | { | |
599 | journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal; | |
600 | ||
601 | journal_invalidatepage(journal, page, offset); | |
602 | } | |
603 | ||
604 | static int ocfs2_releasepage(struct page *page, gfp_t wait) | |
605 | { | |
606 | journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal; | |
607 | ||
608 | if (!page_has_buffers(page)) | |
609 | return 0; | |
610 | return journal_try_to_free_buffers(journal, page, wait); | |
611 | } | |
612 | ||
ccd979bd MF |
613 | static ssize_t ocfs2_direct_IO(int rw, |
614 | struct kiocb *iocb, | |
615 | const struct iovec *iov, | |
616 | loff_t offset, | |
617 | unsigned long nr_segs) | |
618 | { | |
619 | struct file *file = iocb->ki_filp; | |
d28c9174 | 620 | struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host; |
ccd979bd MF |
621 | int ret; |
622 | ||
623 | mlog_entry_void(); | |
53013cba | 624 | |
6798d35a MF |
625 | /* |
626 | * Fallback to buffered I/O if we see an inode without | |
627 | * extents. | |
628 | */ | |
629 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
630 | return 0; | |
631 | ||
ccd979bd MF |
632 | ret = blockdev_direct_IO_no_locking(rw, iocb, inode, |
633 | inode->i_sb->s_bdev, iov, offset, | |
634 | nr_segs, | |
635 | ocfs2_direct_IO_get_blocks, | |
636 | ocfs2_dio_end_io); | |
c934a92d | 637 | |
ccd979bd MF |
638 | mlog_exit(ret); |
639 | return ret; | |
640 | } | |
641 | ||
9517bac6 MF |
642 | static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb, |
643 | u32 cpos, | |
644 | unsigned int *start, | |
645 | unsigned int *end) | |
646 | { | |
647 | unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE; | |
648 | ||
649 | if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) { | |
650 | unsigned int cpp; | |
651 | ||
652 | cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits); | |
653 | ||
654 | cluster_start = cpos % cpp; | |
655 | cluster_start = cluster_start << osb->s_clustersize_bits; | |
656 | ||
657 | cluster_end = cluster_start + osb->s_clustersize; | |
658 | } | |
659 | ||
660 | BUG_ON(cluster_start > PAGE_SIZE); | |
661 | BUG_ON(cluster_end > PAGE_SIZE); | |
662 | ||
663 | if (start) | |
664 | *start = cluster_start; | |
665 | if (end) | |
666 | *end = cluster_end; | |
667 | } | |
668 | ||
669 | /* | |
670 | * 'from' and 'to' are the region in the page to avoid zeroing. | |
671 | * | |
672 | * If pagesize > clustersize, this function will avoid zeroing outside | |
673 | * of the cluster boundary. | |
674 | * | |
675 | * from == to == 0 is code for "zero the entire cluster region" | |
676 | */ | |
677 | static void ocfs2_clear_page_regions(struct page *page, | |
678 | struct ocfs2_super *osb, u32 cpos, | |
679 | unsigned from, unsigned to) | |
680 | { | |
681 | void *kaddr; | |
682 | unsigned int cluster_start, cluster_end; | |
683 | ||
684 | ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end); | |
685 | ||
686 | kaddr = kmap_atomic(page, KM_USER0); | |
687 | ||
688 | if (from || to) { | |
689 | if (from > cluster_start) | |
690 | memset(kaddr + cluster_start, 0, from - cluster_start); | |
691 | if (to < cluster_end) | |
692 | memset(kaddr + to, 0, cluster_end - to); | |
693 | } else { | |
694 | memset(kaddr + cluster_start, 0, cluster_end - cluster_start); | |
695 | } | |
696 | ||
697 | kunmap_atomic(kaddr, KM_USER0); | |
698 | } | |
699 | ||
4e9563fd MF |
700 | /* |
701 | * Nonsparse file systems fully allocate before we get to the write | |
702 | * code. This prevents ocfs2_write() from tagging the write as an | |
703 | * allocating one, which means ocfs2_map_page_blocks() might try to | |
704 | * read-in the blocks at the tail of our file. Avoid reading them by | |
705 | * testing i_size against each block offset. | |
706 | */ | |
707 | static int ocfs2_should_read_blk(struct inode *inode, struct page *page, | |
708 | unsigned int block_start) | |
709 | { | |
710 | u64 offset = page_offset(page) + block_start; | |
711 | ||
712 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) | |
713 | return 1; | |
714 | ||
715 | if (i_size_read(inode) > offset) | |
716 | return 1; | |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
9517bac6 MF |
721 | /* |
722 | * Some of this taken from block_prepare_write(). We already have our | |
723 | * mapping by now though, and the entire write will be allocating or | |
724 | * it won't, so not much need to use BH_New. | |
725 | * | |
726 | * This will also skip zeroing, which is handled externally. | |
727 | */ | |
60b11392 MF |
728 | int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno, |
729 | struct inode *inode, unsigned int from, | |
730 | unsigned int to, int new) | |
9517bac6 MF |
731 | { |
732 | int ret = 0; | |
733 | struct buffer_head *head, *bh, *wait[2], **wait_bh = wait; | |
734 | unsigned int block_end, block_start; | |
735 | unsigned int bsize = 1 << inode->i_blkbits; | |
736 | ||
737 | if (!page_has_buffers(page)) | |
738 | create_empty_buffers(page, bsize, 0); | |
739 | ||
740 | head = page_buffers(page); | |
741 | for (bh = head, block_start = 0; bh != head || !block_start; | |
742 | bh = bh->b_this_page, block_start += bsize) { | |
743 | block_end = block_start + bsize; | |
744 | ||
3a307ffc MF |
745 | clear_buffer_new(bh); |
746 | ||
9517bac6 MF |
747 | /* |
748 | * Ignore blocks outside of our i/o range - | |
749 | * they may belong to unallocated clusters. | |
750 | */ | |
60b11392 | 751 | if (block_start >= to || block_end <= from) { |
9517bac6 MF |
752 | if (PageUptodate(page)) |
753 | set_buffer_uptodate(bh); | |
754 | continue; | |
755 | } | |
756 | ||
757 | /* | |
758 | * For an allocating write with cluster size >= page | |
759 | * size, we always write the entire page. | |
760 | */ | |
3a307ffc MF |
761 | if (new) |
762 | set_buffer_new(bh); | |
9517bac6 MF |
763 | |
764 | if (!buffer_mapped(bh)) { | |
765 | map_bh(bh, inode->i_sb, *p_blkno); | |
766 | unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); | |
767 | } | |
768 | ||
769 | if (PageUptodate(page)) { | |
770 | if (!buffer_uptodate(bh)) | |
771 | set_buffer_uptodate(bh); | |
772 | } else if (!buffer_uptodate(bh) && !buffer_delay(bh) && | |
bce99768 | 773 | !buffer_new(bh) && |
4e9563fd | 774 | ocfs2_should_read_blk(inode, page, block_start) && |
bce99768 | 775 | (block_start < from || block_end > to)) { |
9517bac6 MF |
776 | ll_rw_block(READ, 1, &bh); |
777 | *wait_bh++=bh; | |
778 | } | |
779 | ||
780 | *p_blkno = *p_blkno + 1; | |
781 | } | |
782 | ||
783 | /* | |
784 | * If we issued read requests - let them complete. | |
785 | */ | |
786 | while(wait_bh > wait) { | |
787 | wait_on_buffer(*--wait_bh); | |
788 | if (!buffer_uptodate(*wait_bh)) | |
789 | ret = -EIO; | |
790 | } | |
791 | ||
792 | if (ret == 0 || !new) | |
793 | return ret; | |
794 | ||
795 | /* | |
796 | * If we get -EIO above, zero out any newly allocated blocks | |
797 | * to avoid exposing stale data. | |
798 | */ | |
799 | bh = head; | |
800 | block_start = 0; | |
801 | do { | |
9517bac6 MF |
802 | block_end = block_start + bsize; |
803 | if (block_end <= from) | |
804 | goto next_bh; | |
805 | if (block_start >= to) | |
806 | break; | |
807 | ||
54c57dc3 | 808 | zero_user_page(page, block_start, bh->b_size, KM_USER0); |
9517bac6 MF |
809 | set_buffer_uptodate(bh); |
810 | mark_buffer_dirty(bh); | |
811 | ||
812 | next_bh: | |
813 | block_start = block_end; | |
814 | bh = bh->b_this_page; | |
815 | } while (bh != head); | |
816 | ||
817 | return ret; | |
818 | } | |
819 | ||
3a307ffc MF |
820 | #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE) |
821 | #define OCFS2_MAX_CTXT_PAGES 1 | |
822 | #else | |
823 | #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE) | |
824 | #endif | |
825 | ||
826 | #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE) | |
827 | ||
6af67d82 | 828 | /* |
3a307ffc | 829 | * Describe the state of a single cluster to be written to. |
6af67d82 | 830 | */ |
3a307ffc MF |
831 | struct ocfs2_write_cluster_desc { |
832 | u32 c_cpos; | |
833 | u32 c_phys; | |
834 | /* | |
835 | * Give this a unique field because c_phys eventually gets | |
836 | * filled. | |
837 | */ | |
838 | unsigned c_new; | |
b27b7cbc | 839 | unsigned c_unwritten; |
3a307ffc | 840 | }; |
6af67d82 | 841 | |
b27b7cbc MF |
842 | static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d) |
843 | { | |
844 | return d->c_new || d->c_unwritten; | |
845 | } | |
846 | ||
3a307ffc MF |
847 | struct ocfs2_write_ctxt { |
848 | /* Logical cluster position / len of write */ | |
849 | u32 w_cpos; | |
850 | u32 w_clen; | |
6af67d82 | 851 | |
3a307ffc | 852 | struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE]; |
6af67d82 | 853 | |
3a307ffc MF |
854 | /* |
855 | * This is true if page_size > cluster_size. | |
856 | * | |
857 | * It triggers a set of special cases during write which might | |
858 | * have to deal with allocating writes to partial pages. | |
859 | */ | |
860 | unsigned int w_large_pages; | |
6af67d82 | 861 | |
3a307ffc MF |
862 | /* |
863 | * Pages involved in this write. | |
864 | * | |
865 | * w_target_page is the page being written to by the user. | |
866 | * | |
867 | * w_pages is an array of pages which always contains | |
868 | * w_target_page, and in the case of an allocating write with | |
869 | * page_size < cluster size, it will contain zero'd and mapped | |
870 | * pages adjacent to w_target_page which need to be written | |
871 | * out in so that future reads from that region will get | |
872 | * zero's. | |
873 | */ | |
874 | struct page *w_pages[OCFS2_MAX_CTXT_PAGES]; | |
875 | unsigned int w_num_pages; | |
876 | struct page *w_target_page; | |
eeb47d12 | 877 | |
3a307ffc MF |
878 | /* |
879 | * ocfs2_write_end() uses this to know what the real range to | |
880 | * write in the target should be. | |
881 | */ | |
882 | unsigned int w_target_from; | |
883 | unsigned int w_target_to; | |
884 | ||
885 | /* | |
886 | * We could use journal_current_handle() but this is cleaner, | |
887 | * IMHO -Mark | |
888 | */ | |
889 | handle_t *w_handle; | |
890 | ||
891 | struct buffer_head *w_di_bh; | |
b27b7cbc MF |
892 | |
893 | struct ocfs2_cached_dealloc_ctxt w_dealloc; | |
3a307ffc MF |
894 | }; |
895 | ||
1d410a6e | 896 | void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages) |
3a307ffc MF |
897 | { |
898 | int i; | |
899 | ||
1d410a6e MF |
900 | for(i = 0; i < num_pages; i++) { |
901 | if (pages[i]) { | |
902 | unlock_page(pages[i]); | |
903 | mark_page_accessed(pages[i]); | |
904 | page_cache_release(pages[i]); | |
905 | } | |
6af67d82 | 906 | } |
1d410a6e MF |
907 | } |
908 | ||
909 | static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc) | |
910 | { | |
911 | ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages); | |
6af67d82 | 912 | |
3a307ffc MF |
913 | brelse(wc->w_di_bh); |
914 | kfree(wc); | |
915 | } | |
916 | ||
917 | static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp, | |
918 | struct ocfs2_super *osb, loff_t pos, | |
607d44aa | 919 | unsigned len, struct buffer_head *di_bh) |
3a307ffc | 920 | { |
30b8548f | 921 | u32 cend; |
3a307ffc MF |
922 | struct ocfs2_write_ctxt *wc; |
923 | ||
924 | wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS); | |
925 | if (!wc) | |
926 | return -ENOMEM; | |
6af67d82 | 927 | |
3a307ffc | 928 | wc->w_cpos = pos >> osb->s_clustersize_bits; |
30b8548f | 929 | cend = (pos + len - 1) >> osb->s_clustersize_bits; |
930 | wc->w_clen = cend - wc->w_cpos + 1; | |
607d44aa MF |
931 | get_bh(di_bh); |
932 | wc->w_di_bh = di_bh; | |
6af67d82 | 933 | |
3a307ffc MF |
934 | if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) |
935 | wc->w_large_pages = 1; | |
936 | else | |
937 | wc->w_large_pages = 0; | |
938 | ||
b27b7cbc MF |
939 | ocfs2_init_dealloc_ctxt(&wc->w_dealloc); |
940 | ||
3a307ffc | 941 | *wcp = wc; |
6af67d82 | 942 | |
3a307ffc | 943 | return 0; |
6af67d82 MF |
944 | } |
945 | ||
9517bac6 | 946 | /* |
3a307ffc MF |
947 | * If a page has any new buffers, zero them out here, and mark them uptodate |
948 | * and dirty so they'll be written out (in order to prevent uninitialised | |
949 | * block data from leaking). And clear the new bit. | |
9517bac6 | 950 | */ |
3a307ffc | 951 | static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to) |
9517bac6 | 952 | { |
3a307ffc MF |
953 | unsigned int block_start, block_end; |
954 | struct buffer_head *head, *bh; | |
9517bac6 | 955 | |
3a307ffc MF |
956 | BUG_ON(!PageLocked(page)); |
957 | if (!page_has_buffers(page)) | |
958 | return; | |
9517bac6 | 959 | |
3a307ffc MF |
960 | bh = head = page_buffers(page); |
961 | block_start = 0; | |
962 | do { | |
963 | block_end = block_start + bh->b_size; | |
964 | ||
965 | if (buffer_new(bh)) { | |
966 | if (block_end > from && block_start < to) { | |
967 | if (!PageUptodate(page)) { | |
968 | unsigned start, end; | |
3a307ffc MF |
969 | |
970 | start = max(from, block_start); | |
971 | end = min(to, block_end); | |
972 | ||
54c57dc3 | 973 | zero_user_page(page, start, end - start, KM_USER0); |
3a307ffc MF |
974 | set_buffer_uptodate(bh); |
975 | } | |
976 | ||
977 | clear_buffer_new(bh); | |
978 | mark_buffer_dirty(bh); | |
979 | } | |
980 | } | |
9517bac6 | 981 | |
3a307ffc MF |
982 | block_start = block_end; |
983 | bh = bh->b_this_page; | |
984 | } while (bh != head); | |
985 | } | |
986 | ||
987 | /* | |
988 | * Only called when we have a failure during allocating write to write | |
989 | * zero's to the newly allocated region. | |
990 | */ | |
991 | static void ocfs2_write_failure(struct inode *inode, | |
992 | struct ocfs2_write_ctxt *wc, | |
993 | loff_t user_pos, unsigned user_len) | |
994 | { | |
995 | int i; | |
5c26a7b7 MF |
996 | unsigned from = user_pos & (PAGE_CACHE_SIZE - 1), |
997 | to = user_pos + user_len; | |
3a307ffc MF |
998 | struct page *tmppage; |
999 | ||
5c26a7b7 | 1000 | ocfs2_zero_new_buffers(wc->w_target_page, from, to); |
9517bac6 | 1001 | |
3a307ffc MF |
1002 | for(i = 0; i < wc->w_num_pages; i++) { |
1003 | tmppage = wc->w_pages[i]; | |
9517bac6 | 1004 | |
3a307ffc MF |
1005 | if (ocfs2_should_order_data(inode)) |
1006 | walk_page_buffers(wc->w_handle, page_buffers(tmppage), | |
1007 | from, to, NULL, | |
1008 | ocfs2_journal_dirty_data); | |
eeb47d12 | 1009 | |
3a307ffc | 1010 | block_commit_write(tmppage, from, to); |
9517bac6 | 1011 | } |
9517bac6 MF |
1012 | } |
1013 | ||
3a307ffc MF |
1014 | static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno, |
1015 | struct ocfs2_write_ctxt *wc, | |
1016 | struct page *page, u32 cpos, | |
1017 | loff_t user_pos, unsigned user_len, | |
1018 | int new) | |
9517bac6 | 1019 | { |
3a307ffc MF |
1020 | int ret; |
1021 | unsigned int map_from = 0, map_to = 0; | |
9517bac6 | 1022 | unsigned int cluster_start, cluster_end; |
3a307ffc | 1023 | unsigned int user_data_from = 0, user_data_to = 0; |
9517bac6 | 1024 | |
3a307ffc | 1025 | ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos, |
9517bac6 MF |
1026 | &cluster_start, &cluster_end); |
1027 | ||
3a307ffc MF |
1028 | if (page == wc->w_target_page) { |
1029 | map_from = user_pos & (PAGE_CACHE_SIZE - 1); | |
1030 | map_to = map_from + user_len; | |
1031 | ||
1032 | if (new) | |
1033 | ret = ocfs2_map_page_blocks(page, p_blkno, inode, | |
1034 | cluster_start, cluster_end, | |
1035 | new); | |
1036 | else | |
1037 | ret = ocfs2_map_page_blocks(page, p_blkno, inode, | |
1038 | map_from, map_to, new); | |
1039 | if (ret) { | |
9517bac6 MF |
1040 | mlog_errno(ret); |
1041 | goto out; | |
1042 | } | |
1043 | ||
3a307ffc MF |
1044 | user_data_from = map_from; |
1045 | user_data_to = map_to; | |
9517bac6 | 1046 | if (new) { |
3a307ffc MF |
1047 | map_from = cluster_start; |
1048 | map_to = cluster_end; | |
9517bac6 MF |
1049 | } |
1050 | } else { | |
1051 | /* | |
1052 | * If we haven't allocated the new page yet, we | |
1053 | * shouldn't be writing it out without copying user | |
1054 | * data. This is likely a math error from the caller. | |
1055 | */ | |
1056 | BUG_ON(!new); | |
1057 | ||
3a307ffc MF |
1058 | map_from = cluster_start; |
1059 | map_to = cluster_end; | |
9517bac6 MF |
1060 | |
1061 | ret = ocfs2_map_page_blocks(page, p_blkno, inode, | |
3a307ffc | 1062 | cluster_start, cluster_end, new); |
9517bac6 MF |
1063 | if (ret) { |
1064 | mlog_errno(ret); | |
1065 | goto out; | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | /* | |
1070 | * Parts of newly allocated pages need to be zero'd. | |
1071 | * | |
1072 | * Above, we have also rewritten 'to' and 'from' - as far as | |
1073 | * the rest of the function is concerned, the entire cluster | |
1074 | * range inside of a page needs to be written. | |
1075 | * | |
1076 | * We can skip this if the page is up to date - it's already | |
1077 | * been zero'd from being read in as a hole. | |
1078 | */ | |
1079 | if (new && !PageUptodate(page)) | |
1080 | ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb), | |
3a307ffc | 1081 | cpos, user_data_from, user_data_to); |
9517bac6 MF |
1082 | |
1083 | flush_dcache_page(page); | |
1084 | ||
9517bac6 | 1085 | out: |
3a307ffc | 1086 | return ret; |
9517bac6 MF |
1087 | } |
1088 | ||
1089 | /* | |
3a307ffc | 1090 | * This function will only grab one clusters worth of pages. |
9517bac6 | 1091 | */ |
3a307ffc MF |
1092 | static int ocfs2_grab_pages_for_write(struct address_space *mapping, |
1093 | struct ocfs2_write_ctxt *wc, | |
7307de80 MF |
1094 | u32 cpos, loff_t user_pos, int new, |
1095 | struct page *mmap_page) | |
9517bac6 | 1096 | { |
3a307ffc MF |
1097 | int ret = 0, i; |
1098 | unsigned long start, target_index, index; | |
9517bac6 | 1099 | struct inode *inode = mapping->host; |
9517bac6 | 1100 | |
3a307ffc | 1101 | target_index = user_pos >> PAGE_CACHE_SHIFT; |
9517bac6 MF |
1102 | |
1103 | /* | |
1104 | * Figure out how many pages we'll be manipulating here. For | |
60b11392 MF |
1105 | * non allocating write, we just change the one |
1106 | * page. Otherwise, we'll need a whole clusters worth. | |
9517bac6 | 1107 | */ |
9517bac6 | 1108 | if (new) { |
3a307ffc MF |
1109 | wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb); |
1110 | start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos); | |
9517bac6 | 1111 | } else { |
3a307ffc MF |
1112 | wc->w_num_pages = 1; |
1113 | start = target_index; | |
9517bac6 MF |
1114 | } |
1115 | ||
3a307ffc | 1116 | for(i = 0; i < wc->w_num_pages; i++) { |
9517bac6 MF |
1117 | index = start + i; |
1118 | ||
7307de80 MF |
1119 | if (index == target_index && mmap_page) { |
1120 | /* | |
1121 | * ocfs2_pagemkwrite() is a little different | |
1122 | * and wants us to directly use the page | |
1123 | * passed in. | |
1124 | */ | |
1125 | lock_page(mmap_page); | |
1126 | ||
1127 | if (mmap_page->mapping != mapping) { | |
1128 | unlock_page(mmap_page); | |
1129 | /* | |
1130 | * Sanity check - the locking in | |
1131 | * ocfs2_pagemkwrite() should ensure | |
1132 | * that this code doesn't trigger. | |
1133 | */ | |
1134 | ret = -EINVAL; | |
1135 | mlog_errno(ret); | |
1136 | goto out; | |
1137 | } | |
1138 | ||
1139 | page_cache_get(mmap_page); | |
1140 | wc->w_pages[i] = mmap_page; | |
1141 | } else { | |
1142 | wc->w_pages[i] = find_or_create_page(mapping, index, | |
1143 | GFP_NOFS); | |
1144 | if (!wc->w_pages[i]) { | |
1145 | ret = -ENOMEM; | |
1146 | mlog_errno(ret); | |
1147 | goto out; | |
1148 | } | |
9517bac6 | 1149 | } |
3a307ffc MF |
1150 | |
1151 | if (index == target_index) | |
1152 | wc->w_target_page = wc->w_pages[i]; | |
9517bac6 | 1153 | } |
3a307ffc MF |
1154 | out: |
1155 | return ret; | |
1156 | } | |
1157 | ||
1158 | /* | |
1159 | * Prepare a single cluster for write one cluster into the file. | |
1160 | */ | |
1161 | static int ocfs2_write_cluster(struct address_space *mapping, | |
b27b7cbc MF |
1162 | u32 phys, unsigned int unwritten, |
1163 | struct ocfs2_alloc_context *data_ac, | |
3a307ffc MF |
1164 | struct ocfs2_alloc_context *meta_ac, |
1165 | struct ocfs2_write_ctxt *wc, u32 cpos, | |
1166 | loff_t user_pos, unsigned user_len) | |
1167 | { | |
b27b7cbc | 1168 | int ret, i, new, should_zero = 0; |
3a307ffc MF |
1169 | u64 v_blkno, p_blkno; |
1170 | struct inode *inode = mapping->host; | |
1171 | ||
1172 | new = phys == 0 ? 1 : 0; | |
b27b7cbc MF |
1173 | if (new || unwritten) |
1174 | should_zero = 1; | |
9517bac6 MF |
1175 | |
1176 | if (new) { | |
3a307ffc MF |
1177 | u32 tmp_pos; |
1178 | ||
9517bac6 MF |
1179 | /* |
1180 | * This is safe to call with the page locks - it won't take | |
1181 | * any additional semaphores or cluster locks. | |
1182 | */ | |
3a307ffc | 1183 | tmp_pos = cpos; |
9517bac6 | 1184 | ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode, |
2ae99a60 | 1185 | &tmp_pos, 1, 0, wc->w_di_bh, |
3a307ffc MF |
1186 | wc->w_handle, data_ac, |
1187 | meta_ac, NULL); | |
9517bac6 MF |
1188 | /* |
1189 | * This shouldn't happen because we must have already | |
1190 | * calculated the correct meta data allocation required. The | |
1191 | * internal tree allocation code should know how to increase | |
1192 | * transaction credits itself. | |
1193 | * | |
1194 | * If need be, we could handle -EAGAIN for a | |
1195 | * RESTART_TRANS here. | |
1196 | */ | |
1197 | mlog_bug_on_msg(ret == -EAGAIN, | |
1198 | "Inode %llu: EAGAIN return during allocation.\n", | |
1199 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
1200 | if (ret < 0) { | |
1201 | mlog_errno(ret); | |
1202 | goto out; | |
1203 | } | |
b27b7cbc MF |
1204 | } else if (unwritten) { |
1205 | ret = ocfs2_mark_extent_written(inode, wc->w_di_bh, | |
1206 | wc->w_handle, cpos, 1, phys, | |
1207 | meta_ac, &wc->w_dealloc); | |
1208 | if (ret < 0) { | |
1209 | mlog_errno(ret); | |
1210 | goto out; | |
1211 | } | |
1212 | } | |
3a307ffc | 1213 | |
b27b7cbc | 1214 | if (should_zero) |
3a307ffc | 1215 | v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos); |
b27b7cbc | 1216 | else |
3a307ffc | 1217 | v_blkno = user_pos >> inode->i_sb->s_blocksize_bits; |
9517bac6 | 1218 | |
3a307ffc MF |
1219 | /* |
1220 | * The only reason this should fail is due to an inability to | |
1221 | * find the extent added. | |
1222 | */ | |
49cb8d2d MF |
1223 | ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL, |
1224 | NULL); | |
9517bac6 | 1225 | if (ret < 0) { |
3a307ffc MF |
1226 | ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, " |
1227 | "at logical block %llu", | |
1228 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
1229 | (unsigned long long)v_blkno); | |
9517bac6 MF |
1230 | goto out; |
1231 | } | |
1232 | ||
1233 | BUG_ON(p_blkno == 0); | |
1234 | ||
3a307ffc MF |
1235 | for(i = 0; i < wc->w_num_pages; i++) { |
1236 | int tmpret; | |
9517bac6 | 1237 | |
3a307ffc MF |
1238 | tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc, |
1239 | wc->w_pages[i], cpos, | |
b27b7cbc MF |
1240 | user_pos, user_len, |
1241 | should_zero); | |
3a307ffc MF |
1242 | if (tmpret) { |
1243 | mlog_errno(tmpret); | |
1244 | if (ret == 0) | |
1245 | tmpret = ret; | |
1246 | } | |
9517bac6 MF |
1247 | } |
1248 | ||
3a307ffc MF |
1249 | /* |
1250 | * We only have cleanup to do in case of allocating write. | |
1251 | */ | |
1252 | if (ret && new) | |
1253 | ocfs2_write_failure(inode, wc, user_pos, user_len); | |
1254 | ||
9517bac6 | 1255 | out: |
9517bac6 | 1256 | |
3a307ffc | 1257 | return ret; |
9517bac6 MF |
1258 | } |
1259 | ||
0d172baa MF |
1260 | static int ocfs2_write_cluster_by_desc(struct address_space *mapping, |
1261 | struct ocfs2_alloc_context *data_ac, | |
1262 | struct ocfs2_alloc_context *meta_ac, | |
1263 | struct ocfs2_write_ctxt *wc, | |
1264 | loff_t pos, unsigned len) | |
1265 | { | |
1266 | int ret, i; | |
db56246c MF |
1267 | loff_t cluster_off; |
1268 | unsigned int local_len = len; | |
0d172baa | 1269 | struct ocfs2_write_cluster_desc *desc; |
db56246c | 1270 | struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb); |
0d172baa MF |
1271 | |
1272 | for (i = 0; i < wc->w_clen; i++) { | |
1273 | desc = &wc->w_desc[i]; | |
1274 | ||
db56246c MF |
1275 | /* |
1276 | * We have to make sure that the total write passed in | |
1277 | * doesn't extend past a single cluster. | |
1278 | */ | |
1279 | local_len = len; | |
1280 | cluster_off = pos & (osb->s_clustersize - 1); | |
1281 | if ((cluster_off + local_len) > osb->s_clustersize) | |
1282 | local_len = osb->s_clustersize - cluster_off; | |
1283 | ||
b27b7cbc MF |
1284 | ret = ocfs2_write_cluster(mapping, desc->c_phys, |
1285 | desc->c_unwritten, data_ac, meta_ac, | |
db56246c | 1286 | wc, desc->c_cpos, pos, local_len); |
0d172baa MF |
1287 | if (ret) { |
1288 | mlog_errno(ret); | |
1289 | goto out; | |
1290 | } | |
db56246c MF |
1291 | |
1292 | len -= local_len; | |
1293 | pos += local_len; | |
0d172baa MF |
1294 | } |
1295 | ||
1296 | ret = 0; | |
1297 | out: | |
1298 | return ret; | |
1299 | } | |
1300 | ||
3a307ffc MF |
1301 | /* |
1302 | * ocfs2_write_end() wants to know which parts of the target page it | |
1303 | * should complete the write on. It's easiest to compute them ahead of | |
1304 | * time when a more complete view of the write is available. | |
1305 | */ | |
1306 | static void ocfs2_set_target_boundaries(struct ocfs2_super *osb, | |
1307 | struct ocfs2_write_ctxt *wc, | |
1308 | loff_t pos, unsigned len, int alloc) | |
9517bac6 | 1309 | { |
3a307ffc | 1310 | struct ocfs2_write_cluster_desc *desc; |
9517bac6 | 1311 | |
3a307ffc MF |
1312 | wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1); |
1313 | wc->w_target_to = wc->w_target_from + len; | |
1314 | ||
1315 | if (alloc == 0) | |
1316 | return; | |
1317 | ||
1318 | /* | |
1319 | * Allocating write - we may have different boundaries based | |
1320 | * on page size and cluster size. | |
1321 | * | |
1322 | * NOTE: We can no longer compute one value from the other as | |
1323 | * the actual write length and user provided length may be | |
1324 | * different. | |
1325 | */ | |
9517bac6 | 1326 | |
3a307ffc MF |
1327 | if (wc->w_large_pages) { |
1328 | /* | |
1329 | * We only care about the 1st and last cluster within | |
b27b7cbc | 1330 | * our range and whether they should be zero'd or not. Either |
3a307ffc MF |
1331 | * value may be extended out to the start/end of a |
1332 | * newly allocated cluster. | |
1333 | */ | |
1334 | desc = &wc->w_desc[0]; | |
b27b7cbc | 1335 | if (ocfs2_should_zero_cluster(desc)) |
3a307ffc MF |
1336 | ocfs2_figure_cluster_boundaries(osb, |
1337 | desc->c_cpos, | |
1338 | &wc->w_target_from, | |
1339 | NULL); | |
1340 | ||
1341 | desc = &wc->w_desc[wc->w_clen - 1]; | |
b27b7cbc | 1342 | if (ocfs2_should_zero_cluster(desc)) |
3a307ffc MF |
1343 | ocfs2_figure_cluster_boundaries(osb, |
1344 | desc->c_cpos, | |
1345 | NULL, | |
1346 | &wc->w_target_to); | |
1347 | } else { | |
1348 | wc->w_target_from = 0; | |
1349 | wc->w_target_to = PAGE_CACHE_SIZE; | |
1350 | } | |
9517bac6 MF |
1351 | } |
1352 | ||
0d172baa MF |
1353 | /* |
1354 | * Populate each single-cluster write descriptor in the write context | |
1355 | * with information about the i/o to be done. | |
b27b7cbc MF |
1356 | * |
1357 | * Returns the number of clusters that will have to be allocated, as | |
1358 | * well as a worst case estimate of the number of extent records that | |
1359 | * would have to be created during a write to an unwritten region. | |
0d172baa MF |
1360 | */ |
1361 | static int ocfs2_populate_write_desc(struct inode *inode, | |
1362 | struct ocfs2_write_ctxt *wc, | |
b27b7cbc MF |
1363 | unsigned int *clusters_to_alloc, |
1364 | unsigned int *extents_to_split) | |
9517bac6 | 1365 | { |
0d172baa | 1366 | int ret; |
3a307ffc | 1367 | struct ocfs2_write_cluster_desc *desc; |
0d172baa | 1368 | unsigned int num_clusters = 0; |
b27b7cbc | 1369 | unsigned int ext_flags = 0; |
0d172baa MF |
1370 | u32 phys = 0; |
1371 | int i; | |
9517bac6 | 1372 | |
b27b7cbc MF |
1373 | *clusters_to_alloc = 0; |
1374 | *extents_to_split = 0; | |
1375 | ||
3a307ffc MF |
1376 | for (i = 0; i < wc->w_clen; i++) { |
1377 | desc = &wc->w_desc[i]; | |
1378 | desc->c_cpos = wc->w_cpos + i; | |
1379 | ||
1380 | if (num_clusters == 0) { | |
b27b7cbc MF |
1381 | /* |
1382 | * Need to look up the next extent record. | |
1383 | */ | |
3a307ffc | 1384 | ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys, |
b27b7cbc | 1385 | &num_clusters, &ext_flags); |
3a307ffc MF |
1386 | if (ret) { |
1387 | mlog_errno(ret); | |
607d44aa | 1388 | goto out; |
3a307ffc | 1389 | } |
b27b7cbc MF |
1390 | |
1391 | /* | |
1392 | * Assume worst case - that we're writing in | |
1393 | * the middle of the extent. | |
1394 | * | |
1395 | * We can assume that the write proceeds from | |
1396 | * left to right, in which case the extent | |
1397 | * insert code is smart enough to coalesce the | |
1398 | * next splits into the previous records created. | |
1399 | */ | |
1400 | if (ext_flags & OCFS2_EXT_UNWRITTEN) | |
1401 | *extents_to_split = *extents_to_split + 2; | |
3a307ffc MF |
1402 | } else if (phys) { |
1403 | /* | |
1404 | * Only increment phys if it doesn't describe | |
1405 | * a hole. | |
1406 | */ | |
1407 | phys++; | |
1408 | } | |
1409 | ||
1410 | desc->c_phys = phys; | |
1411 | if (phys == 0) { | |
1412 | desc->c_new = 1; | |
0d172baa | 1413 | *clusters_to_alloc = *clusters_to_alloc + 1; |
3a307ffc | 1414 | } |
b27b7cbc MF |
1415 | if (ext_flags & OCFS2_EXT_UNWRITTEN) |
1416 | desc->c_unwritten = 1; | |
3a307ffc MF |
1417 | |
1418 | num_clusters--; | |
9517bac6 MF |
1419 | } |
1420 | ||
0d172baa MF |
1421 | ret = 0; |
1422 | out: | |
1423 | return ret; | |
1424 | } | |
1425 | ||
1afc32b9 MF |
1426 | static int ocfs2_write_begin_inline(struct address_space *mapping, |
1427 | struct inode *inode, | |
1428 | struct ocfs2_write_ctxt *wc) | |
1429 | { | |
1430 | int ret; | |
1431 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1432 | struct page *page; | |
1433 | handle_t *handle; | |
1434 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data; | |
1435 | ||
1436 | page = find_or_create_page(mapping, 0, GFP_NOFS); | |
1437 | if (!page) { | |
1438 | ret = -ENOMEM; | |
1439 | mlog_errno(ret); | |
1440 | goto out; | |
1441 | } | |
1442 | /* | |
1443 | * If we don't set w_num_pages then this page won't get unlocked | |
1444 | * and freed on cleanup of the write context. | |
1445 | */ | |
1446 | wc->w_pages[0] = wc->w_target_page = page; | |
1447 | wc->w_num_pages = 1; | |
1448 | ||
1449 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
1450 | if (IS_ERR(handle)) { | |
1451 | ret = PTR_ERR(handle); | |
1452 | mlog_errno(ret); | |
1453 | goto out; | |
1454 | } | |
1455 | ||
1456 | ret = ocfs2_journal_access(handle, inode, wc->w_di_bh, | |
1457 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1458 | if (ret) { | |
1459 | ocfs2_commit_trans(osb, handle); | |
1460 | ||
1461 | mlog_errno(ret); | |
1462 | goto out; | |
1463 | } | |
1464 | ||
1465 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)) | |
1466 | ocfs2_set_inode_data_inline(inode, di); | |
1467 | ||
1468 | if (!PageUptodate(page)) { | |
1469 | ret = ocfs2_read_inline_data(inode, page, wc->w_di_bh); | |
1470 | if (ret) { | |
1471 | ocfs2_commit_trans(osb, handle); | |
1472 | ||
1473 | goto out; | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | wc->w_handle = handle; | |
1478 | out: | |
1479 | return ret; | |
1480 | } | |
1481 | ||
1482 | int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size) | |
1483 | { | |
1484 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
1485 | ||
0d8a4e0c | 1486 | if (new_size <= le16_to_cpu(di->id2.i_data.id_count)) |
1afc32b9 MF |
1487 | return 1; |
1488 | return 0; | |
1489 | } | |
1490 | ||
1491 | static int ocfs2_try_to_write_inline_data(struct address_space *mapping, | |
1492 | struct inode *inode, loff_t pos, | |
1493 | unsigned len, struct page *mmap_page, | |
1494 | struct ocfs2_write_ctxt *wc) | |
1495 | { | |
1496 | int ret, written = 0; | |
1497 | loff_t end = pos + len; | |
1498 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
1499 | ||
1500 | mlog(0, "Inode %llu, write of %u bytes at off %llu. features: 0x%x\n", | |
1501 | (unsigned long long)oi->ip_blkno, len, (unsigned long long)pos, | |
1502 | oi->ip_dyn_features); | |
1503 | ||
1504 | /* | |
1505 | * Handle inodes which already have inline data 1st. | |
1506 | */ | |
1507 | if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | |
1508 | if (mmap_page == NULL && | |
1509 | ocfs2_size_fits_inline_data(wc->w_di_bh, end)) | |
1510 | goto do_inline_write; | |
1511 | ||
1512 | /* | |
1513 | * The write won't fit - we have to give this inode an | |
1514 | * inline extent list now. | |
1515 | */ | |
1516 | ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh); | |
1517 | if (ret) | |
1518 | mlog_errno(ret); | |
1519 | goto out; | |
1520 | } | |
1521 | ||
1522 | /* | |
1523 | * Check whether the inode can accept inline data. | |
1524 | */ | |
1525 | if (oi->ip_clusters != 0 || i_size_read(inode) != 0) | |
1526 | return 0; | |
1527 | ||
1528 | /* | |
1529 | * Check whether the write can fit. | |
1530 | */ | |
1531 | if (mmap_page || end > ocfs2_max_inline_data(inode->i_sb)) | |
1532 | return 0; | |
1533 | ||
1534 | do_inline_write: | |
1535 | ret = ocfs2_write_begin_inline(mapping, inode, wc); | |
1536 | if (ret) { | |
1537 | mlog_errno(ret); | |
1538 | goto out; | |
1539 | } | |
1540 | ||
1541 | /* | |
1542 | * This signals to the caller that the data can be written | |
1543 | * inline. | |
1544 | */ | |
1545 | written = 1; | |
1546 | out: | |
1547 | return written ? written : ret; | |
1548 | } | |
1549 | ||
65ed39d6 MF |
1550 | /* |
1551 | * This function only does anything for file systems which can't | |
1552 | * handle sparse files. | |
1553 | * | |
1554 | * What we want to do here is fill in any hole between the current end | |
1555 | * of allocation and the end of our write. That way the rest of the | |
1556 | * write path can treat it as an non-allocating write, which has no | |
1557 | * special case code for sparse/nonsparse files. | |
1558 | */ | |
1559 | static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos, | |
1560 | unsigned len, | |
1561 | struct ocfs2_write_ctxt *wc) | |
1562 | { | |
1563 | int ret; | |
1564 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1565 | loff_t newsize = pos + len; | |
1566 | ||
1567 | if (ocfs2_sparse_alloc(osb)) | |
1568 | return 0; | |
1569 | ||
1570 | if (newsize <= i_size_read(inode)) | |
1571 | return 0; | |
1572 | ||
1573 | ret = ocfs2_extend_no_holes(inode, newsize, newsize - len); | |
1574 | if (ret) | |
1575 | mlog_errno(ret); | |
1576 | ||
1577 | return ret; | |
1578 | } | |
1579 | ||
0d172baa MF |
1580 | int ocfs2_write_begin_nolock(struct address_space *mapping, |
1581 | loff_t pos, unsigned len, unsigned flags, | |
1582 | struct page **pagep, void **fsdata, | |
1583 | struct buffer_head *di_bh, struct page *mmap_page) | |
1584 | { | |
1585 | int ret, credits = OCFS2_INODE_UPDATE_CREDITS; | |
b27b7cbc | 1586 | unsigned int clusters_to_alloc, extents_to_split; |
0d172baa MF |
1587 | struct ocfs2_write_ctxt *wc; |
1588 | struct inode *inode = mapping->host; | |
1589 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1590 | struct ocfs2_dinode *di; | |
1591 | struct ocfs2_alloc_context *data_ac = NULL; | |
1592 | struct ocfs2_alloc_context *meta_ac = NULL; | |
1593 | handle_t *handle; | |
1594 | ||
1595 | ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh); | |
1596 | if (ret) { | |
1597 | mlog_errno(ret); | |
1598 | return ret; | |
1599 | } | |
1600 | ||
1afc32b9 MF |
1601 | if (ocfs2_supports_inline_data(osb)) { |
1602 | ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len, | |
1603 | mmap_page, wc); | |
1604 | if (ret == 1) { | |
1605 | ret = 0; | |
1606 | goto success; | |
1607 | } | |
1608 | if (ret < 0) { | |
1609 | mlog_errno(ret); | |
1610 | goto out; | |
1611 | } | |
1612 | } | |
1613 | ||
65ed39d6 MF |
1614 | ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc); |
1615 | if (ret) { | |
1616 | mlog_errno(ret); | |
1617 | goto out; | |
1618 | } | |
1619 | ||
b27b7cbc MF |
1620 | ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc, |
1621 | &extents_to_split); | |
0d172baa MF |
1622 | if (ret) { |
1623 | mlog_errno(ret); | |
1624 | goto out; | |
1625 | } | |
1626 | ||
1627 | di = (struct ocfs2_dinode *)wc->w_di_bh->b_data; | |
1628 | ||
3a307ffc MF |
1629 | /* |
1630 | * We set w_target_from, w_target_to here so that | |
1631 | * ocfs2_write_end() knows which range in the target page to | |
1632 | * write out. An allocation requires that we write the entire | |
1633 | * cluster range. | |
1634 | */ | |
b27b7cbc | 1635 | if (clusters_to_alloc || extents_to_split) { |
3a307ffc MF |
1636 | /* |
1637 | * XXX: We are stretching the limits of | |
b27b7cbc | 1638 | * ocfs2_lock_allocators(). It greatly over-estimates |
3a307ffc MF |
1639 | * the work to be done. |
1640 | */ | |
1641 | ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc, | |
b27b7cbc | 1642 | extents_to_split, &data_ac, &meta_ac); |
9517bac6 MF |
1643 | if (ret) { |
1644 | mlog_errno(ret); | |
607d44aa | 1645 | goto out; |
9517bac6 MF |
1646 | } |
1647 | ||
3a307ffc MF |
1648 | credits = ocfs2_calc_extend_credits(inode->i_sb, di, |
1649 | clusters_to_alloc); | |
1650 | ||
9517bac6 MF |
1651 | } |
1652 | ||
b27b7cbc MF |
1653 | ocfs2_set_target_boundaries(osb, wc, pos, len, |
1654 | clusters_to_alloc + extents_to_split); | |
3a307ffc | 1655 | |
9517bac6 MF |
1656 | handle = ocfs2_start_trans(osb, credits); |
1657 | if (IS_ERR(handle)) { | |
1658 | ret = PTR_ERR(handle); | |
1659 | mlog_errno(ret); | |
607d44aa | 1660 | goto out; |
9517bac6 MF |
1661 | } |
1662 | ||
3a307ffc MF |
1663 | wc->w_handle = handle; |
1664 | ||
1665 | /* | |
1666 | * We don't want this to fail in ocfs2_write_end(), so do it | |
1667 | * here. | |
1668 | */ | |
1669 | ret = ocfs2_journal_access(handle, inode, wc->w_di_bh, | |
1670 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1671 | if (ret) { | |
9517bac6 MF |
1672 | mlog_errno(ret); |
1673 | goto out_commit; | |
1674 | } | |
1675 | ||
3a307ffc MF |
1676 | /* |
1677 | * Fill our page array first. That way we've grabbed enough so | |
1678 | * that we can zero and flush if we error after adding the | |
1679 | * extent. | |
1680 | */ | |
1681 | ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos, | |
b27b7cbc MF |
1682 | clusters_to_alloc + extents_to_split, |
1683 | mmap_page); | |
9517bac6 MF |
1684 | if (ret) { |
1685 | mlog_errno(ret); | |
1686 | goto out_commit; | |
1687 | } | |
1688 | ||
0d172baa MF |
1689 | ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos, |
1690 | len); | |
1691 | if (ret) { | |
1692 | mlog_errno(ret); | |
1693 | goto out_commit; | |
9517bac6 | 1694 | } |
9517bac6 | 1695 | |
3a307ffc MF |
1696 | if (data_ac) |
1697 | ocfs2_free_alloc_context(data_ac); | |
1698 | if (meta_ac) | |
1699 | ocfs2_free_alloc_context(meta_ac); | |
9517bac6 | 1700 | |
1afc32b9 | 1701 | success: |
3a307ffc MF |
1702 | *pagep = wc->w_target_page; |
1703 | *fsdata = wc; | |
1704 | return 0; | |
9517bac6 MF |
1705 | out_commit: |
1706 | ocfs2_commit_trans(osb, handle); | |
1707 | ||
9517bac6 | 1708 | out: |
3a307ffc MF |
1709 | ocfs2_free_write_ctxt(wc); |
1710 | ||
9517bac6 MF |
1711 | if (data_ac) |
1712 | ocfs2_free_alloc_context(data_ac); | |
1713 | if (meta_ac) | |
1714 | ocfs2_free_alloc_context(meta_ac); | |
3a307ffc MF |
1715 | return ret; |
1716 | } | |
1717 | ||
b6af1bcd NP |
1718 | static int ocfs2_write_begin(struct file *file, struct address_space *mapping, |
1719 | loff_t pos, unsigned len, unsigned flags, | |
1720 | struct page **pagep, void **fsdata) | |
607d44aa MF |
1721 | { |
1722 | int ret; | |
1723 | struct buffer_head *di_bh = NULL; | |
1724 | struct inode *inode = mapping->host; | |
1725 | ||
e63aecb6 | 1726 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
607d44aa MF |
1727 | if (ret) { |
1728 | mlog_errno(ret); | |
1729 | return ret; | |
1730 | } | |
1731 | ||
1732 | /* | |
1733 | * Take alloc sem here to prevent concurrent lookups. That way | |
1734 | * the mapping, zeroing and tree manipulation within | |
1735 | * ocfs2_write() will be safe against ->readpage(). This | |
1736 | * should also serve to lock out allocation from a shared | |
1737 | * writeable region. | |
1738 | */ | |
1739 | down_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1740 | ||
607d44aa | 1741 | ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep, |
7307de80 | 1742 | fsdata, di_bh, NULL); |
607d44aa MF |
1743 | if (ret) { |
1744 | mlog_errno(ret); | |
c934a92d | 1745 | goto out_fail; |
607d44aa MF |
1746 | } |
1747 | ||
1748 | brelse(di_bh); | |
1749 | ||
1750 | return 0; | |
1751 | ||
607d44aa MF |
1752 | out_fail: |
1753 | up_write(&OCFS2_I(inode)->ip_alloc_sem); | |
1754 | ||
1755 | brelse(di_bh); | |
e63aecb6 | 1756 | ocfs2_inode_unlock(inode, 1); |
607d44aa MF |
1757 | |
1758 | return ret; | |
1759 | } | |
1760 | ||
1afc32b9 MF |
1761 | static void ocfs2_write_end_inline(struct inode *inode, loff_t pos, |
1762 | unsigned len, unsigned *copied, | |
1763 | struct ocfs2_dinode *di, | |
1764 | struct ocfs2_write_ctxt *wc) | |
1765 | { | |
1766 | void *kaddr; | |
1767 | ||
1768 | if (unlikely(*copied < len)) { | |
1769 | if (!PageUptodate(wc->w_target_page)) { | |
1770 | *copied = 0; | |
1771 | return; | |
1772 | } | |
1773 | } | |
1774 | ||
1775 | kaddr = kmap_atomic(wc->w_target_page, KM_USER0); | |
1776 | memcpy(di->id2.i_data.id_data + pos, kaddr + pos, *copied); | |
1777 | kunmap_atomic(kaddr, KM_USER0); | |
1778 | ||
1779 | mlog(0, "Data written to inode at offset %llu. " | |
1780 | "id_count = %u, copied = %u, i_dyn_features = 0x%x\n", | |
1781 | (unsigned long long)pos, *copied, | |
1782 | le16_to_cpu(di->id2.i_data.id_count), | |
1783 | le16_to_cpu(di->i_dyn_features)); | |
1784 | } | |
1785 | ||
7307de80 MF |
1786 | int ocfs2_write_end_nolock(struct address_space *mapping, |
1787 | loff_t pos, unsigned len, unsigned copied, | |
1788 | struct page *page, void *fsdata) | |
3a307ffc MF |
1789 | { |
1790 | int i; | |
1791 | unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1); | |
1792 | struct inode *inode = mapping->host; | |
1793 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1794 | struct ocfs2_write_ctxt *wc = fsdata; | |
1795 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data; | |
1796 | handle_t *handle = wc->w_handle; | |
1797 | struct page *tmppage; | |
1798 | ||
1afc32b9 MF |
1799 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
1800 | ocfs2_write_end_inline(inode, pos, len, &copied, di, wc); | |
1801 | goto out_write_size; | |
1802 | } | |
1803 | ||
3a307ffc MF |
1804 | if (unlikely(copied < len)) { |
1805 | if (!PageUptodate(wc->w_target_page)) | |
1806 | copied = 0; | |
1807 | ||
1808 | ocfs2_zero_new_buffers(wc->w_target_page, start+copied, | |
1809 | start+len); | |
1810 | } | |
1811 | flush_dcache_page(wc->w_target_page); | |
1812 | ||
1813 | for(i = 0; i < wc->w_num_pages; i++) { | |
1814 | tmppage = wc->w_pages[i]; | |
1815 | ||
1816 | if (tmppage == wc->w_target_page) { | |
1817 | from = wc->w_target_from; | |
1818 | to = wc->w_target_to; | |
1819 | ||
1820 | BUG_ON(from > PAGE_CACHE_SIZE || | |
1821 | to > PAGE_CACHE_SIZE || | |
1822 | to < from); | |
1823 | } else { | |
1824 | /* | |
1825 | * Pages adjacent to the target (if any) imply | |
1826 | * a hole-filling write in which case we want | |
1827 | * to flush their entire range. | |
1828 | */ | |
1829 | from = 0; | |
1830 | to = PAGE_CACHE_SIZE; | |
1831 | } | |
1832 | ||
1833 | if (ocfs2_should_order_data(inode)) | |
1834 | walk_page_buffers(wc->w_handle, page_buffers(tmppage), | |
1835 | from, to, NULL, | |
1836 | ocfs2_journal_dirty_data); | |
1837 | ||
1838 | block_commit_write(tmppage, from, to); | |
1839 | } | |
1840 | ||
1afc32b9 | 1841 | out_write_size: |
3a307ffc MF |
1842 | pos += copied; |
1843 | if (pos > inode->i_size) { | |
1844 | i_size_write(inode, pos); | |
1845 | mark_inode_dirty(inode); | |
1846 | } | |
1847 | inode->i_blocks = ocfs2_inode_sector_count(inode); | |
1848 | di->i_size = cpu_to_le64((u64)i_size_read(inode)); | |
1849 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
1850 | di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec); | |
1851 | di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); | |
3a307ffc MF |
1852 | ocfs2_journal_dirty(handle, wc->w_di_bh); |
1853 | ||
1854 | ocfs2_commit_trans(osb, handle); | |
59a5e416 | 1855 | |
b27b7cbc MF |
1856 | ocfs2_run_deallocs(osb, &wc->w_dealloc); |
1857 | ||
607d44aa MF |
1858 | ocfs2_free_write_ctxt(wc); |
1859 | ||
1860 | return copied; | |
1861 | } | |
1862 | ||
b6af1bcd NP |
1863 | static int ocfs2_write_end(struct file *file, struct address_space *mapping, |
1864 | loff_t pos, unsigned len, unsigned copied, | |
1865 | struct page *page, void *fsdata) | |
607d44aa MF |
1866 | { |
1867 | int ret; | |
1868 | struct inode *inode = mapping->host; | |
1869 | ||
1870 | ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata); | |
1871 | ||
3a307ffc | 1872 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
e63aecb6 | 1873 | ocfs2_inode_unlock(inode, 1); |
9517bac6 | 1874 | |
607d44aa | 1875 | return ret; |
9517bac6 MF |
1876 | } |
1877 | ||
f5e54d6e | 1878 | const struct address_space_operations ocfs2_aops = { |
ccd979bd MF |
1879 | .readpage = ocfs2_readpage, |
1880 | .writepage = ocfs2_writepage, | |
b6af1bcd NP |
1881 | .write_begin = ocfs2_write_begin, |
1882 | .write_end = ocfs2_write_end, | |
ccd979bd MF |
1883 | .bmap = ocfs2_bmap, |
1884 | .sync_page = block_sync_page, | |
03f981cf JB |
1885 | .direct_IO = ocfs2_direct_IO, |
1886 | .invalidatepage = ocfs2_invalidatepage, | |
1887 | .releasepage = ocfs2_releasepage, | |
1888 | .migratepage = buffer_migrate_page, | |
ccd979bd | 1889 | }; |