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> | |
27 | ||
28 | #define MLOG_MASK_PREFIX ML_FILE_IO | |
29 | #include <cluster/masklog.h> | |
30 | ||
31 | #include "ocfs2.h" | |
32 | ||
33 | #include "alloc.h" | |
34 | #include "aops.h" | |
35 | #include "dlmglue.h" | |
36 | #include "extent_map.h" | |
37 | #include "file.h" | |
38 | #include "inode.h" | |
39 | #include "journal.h" | |
40 | #include "super.h" | |
41 | #include "symlink.h" | |
42 | ||
43 | #include "buffer_head_io.h" | |
44 | ||
45 | static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock, | |
46 | struct buffer_head *bh_result, int create) | |
47 | { | |
48 | int err = -EIO; | |
49 | int status; | |
50 | struct ocfs2_dinode *fe = NULL; | |
51 | struct buffer_head *bh = NULL; | |
52 | struct buffer_head *buffer_cache_bh = NULL; | |
53 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
54 | void *kaddr; | |
55 | ||
56 | mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode, | |
57 | (unsigned long long)iblock, bh_result, create); | |
58 | ||
59 | BUG_ON(ocfs2_inode_is_fast_symlink(inode)); | |
60 | ||
61 | if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) { | |
62 | mlog(ML_ERROR, "block offset > PATH_MAX: %llu", | |
63 | (unsigned long long)iblock); | |
64 | goto bail; | |
65 | } | |
66 | ||
67 | status = ocfs2_read_block(OCFS2_SB(inode->i_sb), | |
68 | OCFS2_I(inode)->ip_blkno, | |
69 | &bh, OCFS2_BH_CACHED, inode); | |
70 | if (status < 0) { | |
71 | mlog_errno(status); | |
72 | goto bail; | |
73 | } | |
74 | fe = (struct ocfs2_dinode *) bh->b_data; | |
75 | ||
76 | if (!OCFS2_IS_VALID_DINODE(fe)) { | |
b0697053 MF |
77 | mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n", |
78 | (unsigned long long)fe->i_blkno, 7, fe->i_signature); | |
ccd979bd MF |
79 | goto bail; |
80 | } | |
81 | ||
82 | if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb, | |
83 | le32_to_cpu(fe->i_clusters))) { | |
84 | mlog(ML_ERROR, "block offset is outside the allocated size: " | |
85 | "%llu\n", (unsigned long long)iblock); | |
86 | goto bail; | |
87 | } | |
88 | ||
89 | /* We don't use the page cache to create symlink data, so if | |
90 | * need be, copy it over from the buffer cache. */ | |
91 | if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) { | |
92 | u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + | |
93 | iblock; | |
94 | buffer_cache_bh = sb_getblk(osb->sb, blkno); | |
95 | if (!buffer_cache_bh) { | |
96 | mlog(ML_ERROR, "couldn't getblock for symlink!\n"); | |
97 | goto bail; | |
98 | } | |
99 | ||
100 | /* we haven't locked out transactions, so a commit | |
101 | * could've happened. Since we've got a reference on | |
102 | * the bh, even if it commits while we're doing the | |
103 | * copy, the data is still good. */ | |
104 | if (buffer_jbd(buffer_cache_bh) | |
105 | && ocfs2_inode_is_new(inode)) { | |
106 | kaddr = kmap_atomic(bh_result->b_page, KM_USER0); | |
107 | if (!kaddr) { | |
108 | mlog(ML_ERROR, "couldn't kmap!\n"); | |
109 | goto bail; | |
110 | } | |
111 | memcpy(kaddr + (bh_result->b_size * iblock), | |
112 | buffer_cache_bh->b_data, | |
113 | bh_result->b_size); | |
114 | kunmap_atomic(kaddr, KM_USER0); | |
115 | set_buffer_uptodate(bh_result); | |
116 | } | |
117 | brelse(buffer_cache_bh); | |
118 | } | |
119 | ||
120 | map_bh(bh_result, inode->i_sb, | |
121 | le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock); | |
122 | ||
123 | err = 0; | |
124 | ||
125 | bail: | |
126 | if (bh) | |
127 | brelse(bh); | |
128 | ||
129 | mlog_exit(err); | |
130 | return err; | |
131 | } | |
132 | ||
133 | static int ocfs2_get_block(struct inode *inode, sector_t iblock, | |
134 | struct buffer_head *bh_result, int create) | |
135 | { | |
136 | int err = 0; | |
137 | u64 p_blkno, past_eof; | |
138 | ||
139 | mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode, | |
140 | (unsigned long long)iblock, bh_result, create); | |
141 | ||
142 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) | |
143 | mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n", | |
144 | inode, inode->i_ino); | |
145 | ||
146 | if (S_ISLNK(inode->i_mode)) { | |
147 | /* this always does I/O for some reason. */ | |
148 | err = ocfs2_symlink_get_block(inode, iblock, bh_result, create); | |
149 | goto bail; | |
150 | } | |
151 | ||
152 | /* this can happen if another node truncs after our extend! */ | |
153 | spin_lock(&OCFS2_I(inode)->ip_lock); | |
154 | if (iblock >= ocfs2_clusters_to_blocks(inode->i_sb, | |
155 | OCFS2_I(inode)->ip_clusters)) | |
156 | err = -EIO; | |
157 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
158 | if (err) | |
159 | goto bail; | |
160 | ||
363041a5 | 161 | err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL); |
ccd979bd MF |
162 | if (err) { |
163 | mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, " | |
b0697053 MF |
164 | "%llu, NULL)\n", err, inode, (unsigned long long)iblock, |
165 | (unsigned long long)p_blkno); | |
ccd979bd MF |
166 | goto bail; |
167 | } | |
168 | ||
169 | map_bh(bh_result, inode->i_sb, p_blkno); | |
170 | ||
171 | if (bh_result->b_blocknr == 0) { | |
172 | err = -EIO; | |
b0697053 MF |
173 | mlog(ML_ERROR, "iblock = %llu p_blkno = %llu blkno=(%llu)\n", |
174 | (unsigned long long)iblock, | |
175 | (unsigned long long)p_blkno, | |
176 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd MF |
177 | } |
178 | ||
179 | past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode)); | |
b0697053 MF |
180 | mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino, |
181 | (unsigned long long)past_eof); | |
ccd979bd MF |
182 | |
183 | if (create && (iblock >= past_eof)) | |
184 | set_buffer_new(bh_result); | |
185 | ||
186 | bail: | |
187 | if (err < 0) | |
188 | err = -EIO; | |
189 | ||
190 | mlog_exit(err); | |
191 | return err; | |
192 | } | |
193 | ||
194 | static int ocfs2_readpage(struct file *file, struct page *page) | |
195 | { | |
196 | struct inode *inode = page->mapping->host; | |
197 | loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT; | |
198 | int ret, unlock = 1; | |
199 | ||
200 | mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0)); | |
201 | ||
4bcec184 | 202 | ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page); |
ccd979bd MF |
203 | if (ret != 0) { |
204 | if (ret == AOP_TRUNCATED_PAGE) | |
205 | unlock = 0; | |
206 | mlog_errno(ret); | |
207 | goto out; | |
208 | } | |
209 | ||
210 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
211 | ||
212 | /* | |
213 | * i_size might have just been updated as we grabed the meta lock. We | |
214 | * might now be discovering a truncate that hit on another node. | |
215 | * block_read_full_page->get_block freaks out if it is asked to read | |
216 | * beyond the end of a file, so we check here. Callers | |
217 | * (generic_file_read, fault->nopage) are clever enough to check i_size | |
218 | * and notice that the page they just read isn't needed. | |
219 | * | |
220 | * XXX sys_readahead() seems to get that wrong? | |
221 | */ | |
222 | if (start >= i_size_read(inode)) { | |
223 | char *addr = kmap(page); | |
224 | memset(addr, 0, PAGE_SIZE); | |
225 | flush_dcache_page(page); | |
226 | kunmap(page); | |
227 | SetPageUptodate(page); | |
228 | ret = 0; | |
229 | goto out_alloc; | |
230 | } | |
231 | ||
232 | ret = ocfs2_data_lock_with_page(inode, 0, page); | |
233 | if (ret != 0) { | |
234 | if (ret == AOP_TRUNCATED_PAGE) | |
235 | unlock = 0; | |
236 | mlog_errno(ret); | |
237 | goto out_alloc; | |
238 | } | |
239 | ||
240 | ret = block_read_full_page(page, ocfs2_get_block); | |
241 | unlock = 0; | |
242 | ||
243 | ocfs2_data_unlock(inode, 0); | |
244 | out_alloc: | |
245 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
246 | ocfs2_meta_unlock(inode, 0); | |
247 | out: | |
248 | if (unlock) | |
249 | unlock_page(page); | |
250 | mlog_exit(ret); | |
251 | return ret; | |
252 | } | |
253 | ||
254 | /* Note: Because we don't support holes, our allocation has | |
255 | * already happened (allocation writes zeros to the file data) | |
256 | * so we don't have to worry about ordered writes in | |
257 | * ocfs2_writepage. | |
258 | * | |
259 | * ->writepage is called during the process of invalidating the page cache | |
260 | * during blocked lock processing. It can't block on any cluster locks | |
261 | * to during block mapping. It's relying on the fact that the block | |
262 | * mapping can't have disappeared under the dirty pages that it is | |
263 | * being asked to write back. | |
264 | */ | |
265 | static int ocfs2_writepage(struct page *page, struct writeback_control *wbc) | |
266 | { | |
267 | int ret; | |
268 | ||
269 | mlog_entry("(0x%p)\n", page); | |
270 | ||
271 | ret = block_write_full_page(page, ocfs2_get_block, wbc); | |
272 | ||
273 | mlog_exit(ret); | |
274 | ||
275 | return ret; | |
276 | } | |
277 | ||
53013cba MF |
278 | /* This can also be called from ocfs2_write_zero_page() which has done |
279 | * it's own cluster locking. */ | |
280 | int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page, | |
281 | unsigned from, unsigned to) | |
282 | { | |
283 | int ret; | |
284 | ||
285 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
286 | ||
287 | ret = block_prepare_write(page, from, to, ocfs2_get_block); | |
288 | ||
289 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
290 | ||
291 | return ret; | |
292 | } | |
293 | ||
ccd979bd MF |
294 | /* |
295 | * ocfs2_prepare_write() can be an outer-most ocfs2 call when it is called | |
296 | * from loopback. It must be able to perform its own locking around | |
297 | * ocfs2_get_block(). | |
298 | */ | |
53013cba MF |
299 | static int ocfs2_prepare_write(struct file *file, struct page *page, |
300 | unsigned from, unsigned to) | |
ccd979bd MF |
301 | { |
302 | struct inode *inode = page->mapping->host; | |
303 | int ret; | |
304 | ||
305 | mlog_entry("(0x%p, 0x%p, %u, %u)\n", file, page, from, to); | |
306 | ||
4bcec184 | 307 | ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page); |
ccd979bd MF |
308 | if (ret != 0) { |
309 | mlog_errno(ret); | |
310 | goto out; | |
311 | } | |
312 | ||
53013cba | 313 | ret = ocfs2_prepare_write_nolock(inode, page, from, to); |
ccd979bd MF |
314 | |
315 | ocfs2_meta_unlock(inode, 0); | |
316 | out: | |
317 | mlog_exit(ret); | |
318 | return ret; | |
319 | } | |
320 | ||
321 | /* Taken from ext3. We don't necessarily need the full blown | |
322 | * functionality yet, but IMHO it's better to cut and paste the whole | |
323 | * thing so we can avoid introducing our own bugs (and easily pick up | |
324 | * their fixes when they happen) --Mark */ | |
325 | static int walk_page_buffers( handle_t *handle, | |
326 | struct buffer_head *head, | |
327 | unsigned from, | |
328 | unsigned to, | |
329 | int *partial, | |
330 | int (*fn)( handle_t *handle, | |
331 | struct buffer_head *bh)) | |
332 | { | |
333 | struct buffer_head *bh; | |
334 | unsigned block_start, block_end; | |
335 | unsigned blocksize = head->b_size; | |
336 | int err, ret = 0; | |
337 | struct buffer_head *next; | |
338 | ||
339 | for ( bh = head, block_start = 0; | |
340 | ret == 0 && (bh != head || !block_start); | |
341 | block_start = block_end, bh = next) | |
342 | { | |
343 | next = bh->b_this_page; | |
344 | block_end = block_start + blocksize; | |
345 | if (block_end <= from || block_start >= to) { | |
346 | if (partial && !buffer_uptodate(bh)) | |
347 | *partial = 1; | |
348 | continue; | |
349 | } | |
350 | err = (*fn)(handle, bh); | |
351 | if (!ret) | |
352 | ret = err; | |
353 | } | |
354 | return ret; | |
355 | } | |
356 | ||
1fabe148 | 357 | handle_t *ocfs2_start_walk_page_trans(struct inode *inode, |
ccd979bd MF |
358 | struct page *page, |
359 | unsigned from, | |
360 | unsigned to) | |
361 | { | |
362 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
1fabe148 | 363 | handle_t *handle = NULL; |
ccd979bd MF |
364 | int ret = 0; |
365 | ||
65eff9cc | 366 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
ccd979bd MF |
367 | if (!handle) { |
368 | ret = -ENOMEM; | |
369 | mlog_errno(ret); | |
370 | goto out; | |
371 | } | |
372 | ||
373 | if (ocfs2_should_order_data(inode)) { | |
1fabe148 | 374 | ret = walk_page_buffers(handle, |
ccd979bd MF |
375 | page_buffers(page), |
376 | from, to, NULL, | |
377 | ocfs2_journal_dirty_data); | |
378 | if (ret < 0) | |
379 | mlog_errno(ret); | |
380 | } | |
381 | out: | |
382 | if (ret) { | |
383 | if (handle) | |
02dc1af4 | 384 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
385 | handle = ERR_PTR(ret); |
386 | } | |
387 | return handle; | |
388 | } | |
389 | ||
390 | static int ocfs2_commit_write(struct file *file, struct page *page, | |
391 | unsigned from, unsigned to) | |
392 | { | |
e0b4096d | 393 | int ret; |
ccd979bd MF |
394 | struct buffer_head *di_bh = NULL; |
395 | struct inode *inode = page->mapping->host; | |
1fabe148 | 396 | handle_t *handle = NULL; |
e0b4096d | 397 | struct ocfs2_dinode *di; |
ccd979bd MF |
398 | |
399 | mlog_entry("(0x%p, 0x%p, %u, %u)\n", file, page, from, to); | |
400 | ||
401 | /* NOTE: ocfs2_file_aio_write has ensured that it's safe for | |
e0b4096d MF |
402 | * us to continue here without rechecking the I/O against |
403 | * changed inode values. | |
ccd979bd MF |
404 | * |
405 | * 1) We're currently holding the inode alloc lock, so no | |
406 | * nodes can change it underneath us. | |
407 | * | |
408 | * 2) We've had to take the metadata lock at least once | |
e0b4096d MF |
409 | * already to check for extending writes, suid removal, etc. |
410 | * The meta data update code then ensures that we don't get a | |
411 | * stale inode allocation image (i_size, i_clusters, etc). | |
ccd979bd | 412 | */ |
ccd979bd | 413 | |
4bcec184 | 414 | ret = ocfs2_meta_lock_with_page(inode, &di_bh, 1, page); |
ccd979bd MF |
415 | if (ret != 0) { |
416 | mlog_errno(ret); | |
417 | goto out; | |
418 | } | |
419 | ||
420 | ret = ocfs2_data_lock_with_page(inode, 1, page); | |
421 | if (ret != 0) { | |
422 | mlog_errno(ret); | |
423 | goto out_unlock_meta; | |
424 | } | |
425 | ||
e0b4096d MF |
426 | handle = ocfs2_start_walk_page_trans(inode, page, from, to); |
427 | if (IS_ERR(handle)) { | |
428 | ret = PTR_ERR(handle); | |
429 | goto out_unlock_data; | |
430 | } | |
ccd979bd | 431 | |
e0b4096d MF |
432 | /* Mark our buffer early. We'd rather catch this error up here |
433 | * as opposed to after a successful commit_write which would | |
434 | * require us to set back inode->i_size. */ | |
435 | ret = ocfs2_journal_access(handle, inode, di_bh, | |
436 | OCFS2_JOURNAL_ACCESS_WRITE); | |
437 | if (ret < 0) { | |
438 | mlog_errno(ret); | |
439 | goto out_commit; | |
ccd979bd MF |
440 | } |
441 | ||
442 | /* might update i_size */ | |
443 | ret = generic_commit_write(file, page, from, to); | |
444 | if (ret < 0) { | |
445 | mlog_errno(ret); | |
446 | goto out_commit; | |
447 | } | |
448 | ||
e0b4096d | 449 | di = (struct ocfs2_dinode *)di_bh->b_data; |
ccd979bd | 450 | |
e0b4096d MF |
451 | /* ocfs2_mark_inode_dirty() is too heavy to use here. */ |
452 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
453 | di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec); | |
454 | di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); | |
ccd979bd | 455 | |
e0b4096d MF |
456 | inode->i_blocks = ocfs2_align_bytes_to_sectors((u64)(i_size_read(inode))); |
457 | di->i_size = cpu_to_le64((u64)i_size_read(inode)); | |
ccd979bd | 458 | |
e0b4096d MF |
459 | ret = ocfs2_journal_dirty(handle, di_bh); |
460 | if (ret < 0) { | |
461 | mlog_errno(ret); | |
462 | goto out_commit; | |
ccd979bd MF |
463 | } |
464 | ||
ccd979bd | 465 | out_commit: |
02dc1af4 | 466 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
ccd979bd MF |
467 | out_unlock_data: |
468 | ocfs2_data_unlock(inode, 1); | |
469 | out_unlock_meta: | |
e0b4096d | 470 | ocfs2_meta_unlock(inode, 1); |
ccd979bd MF |
471 | out: |
472 | if (di_bh) | |
473 | brelse(di_bh); | |
474 | ||
475 | mlog_exit(ret); | |
476 | return ret; | |
477 | } | |
478 | ||
479 | static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block) | |
480 | { | |
481 | sector_t status; | |
482 | u64 p_blkno = 0; | |
483 | int err = 0; | |
484 | struct inode *inode = mapping->host; | |
485 | ||
486 | mlog_entry("(block = %llu)\n", (unsigned long long)block); | |
487 | ||
488 | /* We don't need to lock journal system files, since they aren't | |
489 | * accessed concurrently from multiple nodes. | |
490 | */ | |
491 | if (!INODE_JOURNAL(inode)) { | |
4bcec184 | 492 | err = ocfs2_meta_lock(inode, NULL, 0); |
ccd979bd MF |
493 | if (err) { |
494 | if (err != -ENOENT) | |
495 | mlog_errno(err); | |
496 | goto bail; | |
497 | } | |
498 | down_read(&OCFS2_I(inode)->ip_alloc_sem); | |
499 | } | |
500 | ||
363041a5 | 501 | err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL); |
ccd979bd MF |
502 | |
503 | if (!INODE_JOURNAL(inode)) { | |
504 | up_read(&OCFS2_I(inode)->ip_alloc_sem); | |
505 | ocfs2_meta_unlock(inode, 0); | |
506 | } | |
507 | ||
508 | if (err) { | |
509 | mlog(ML_ERROR, "get_blocks() failed, block = %llu\n", | |
510 | (unsigned long long)block); | |
511 | mlog_errno(err); | |
512 | goto bail; | |
513 | } | |
514 | ||
515 | ||
516 | bail: | |
517 | status = err ? 0 : p_blkno; | |
518 | ||
519 | mlog_exit((int)status); | |
520 | ||
521 | return status; | |
522 | } | |
523 | ||
524 | /* | |
525 | * TODO: Make this into a generic get_blocks function. | |
526 | * | |
527 | * From do_direct_io in direct-io.c: | |
528 | * "So what we do is to permit the ->get_blocks function to populate | |
529 | * bh.b_size with the size of IO which is permitted at this offset and | |
530 | * this i_blkbits." | |
531 | * | |
532 | * This function is called directly from get_more_blocks in direct-io.c. | |
533 | * | |
534 | * called like this: dio->get_blocks(dio->inode, fs_startblk, | |
535 | * fs_count, map_bh, dio->rw == WRITE); | |
536 | */ | |
537 | static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock, | |
ccd979bd MF |
538 | struct buffer_head *bh_result, int create) |
539 | { | |
540 | int ret; | |
564f8a32 | 541 | u64 p_blkno, inode_blocks; |
ccd979bd | 542 | int contig_blocks; |
184d7d20 | 543 | unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; |
1d8fa7a2 | 544 | unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; |
ccd979bd | 545 | |
ccd979bd MF |
546 | /* This function won't even be called if the request isn't all |
547 | * nicely aligned and of the right size, so there's no need | |
548 | * for us to check any of that. */ | |
549 | ||
ccd979bd | 550 | spin_lock(&OCFS2_I(inode)->ip_lock); |
564f8a32 MF |
551 | inode_blocks = ocfs2_clusters_to_blocks(inode->i_sb, |
552 | OCFS2_I(inode)->ip_clusters); | |
553 | ||
554 | /* | |
555 | * For a read which begins past the end of file, we return a hole. | |
556 | */ | |
557 | if (!create && (iblock >= inode_blocks)) { | |
558 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
559 | ret = 0; | |
560 | goto bail; | |
561 | } | |
562 | ||
563 | /* | |
564 | * Any write past EOF is not allowed because we'd be extending. | |
565 | */ | |
566 | if (create && (iblock + max_blocks) > inode_blocks) { | |
ccd979bd MF |
567 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
568 | ret = -EIO; | |
569 | goto bail; | |
570 | } | |
571 | spin_unlock(&OCFS2_I(inode)->ip_lock); | |
572 | ||
573 | /* This figures out the size of the next contiguous block, and | |
574 | * our logical offset */ | |
363041a5 | 575 | ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, |
ccd979bd MF |
576 | &contig_blocks); |
577 | if (ret) { | |
578 | mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n", | |
579 | (unsigned long long)iblock); | |
580 | ret = -EIO; | |
581 | goto bail; | |
582 | } | |
583 | ||
584 | map_bh(bh_result, inode->i_sb, p_blkno); | |
585 | ||
586 | /* make sure we don't map more than max_blocks blocks here as | |
587 | that's all the kernel will handle at this point. */ | |
588 | if (max_blocks < contig_blocks) | |
589 | contig_blocks = max_blocks; | |
590 | bh_result->b_size = contig_blocks << blocksize_bits; | |
591 | bail: | |
592 | return ret; | |
593 | } | |
594 | ||
595 | /* | |
596 | * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're | |
597 | * particularly interested in the aio/dio case. Like the core uses | |
598 | * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from | |
599 | * truncation on another. | |
600 | */ | |
601 | static void ocfs2_dio_end_io(struct kiocb *iocb, | |
602 | loff_t offset, | |
603 | ssize_t bytes, | |
604 | void *private) | |
605 | { | |
d28c9174 | 606 | struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode; |
ccd979bd MF |
607 | |
608 | /* this io's submitter should not have unlocked this before we could */ | |
609 | BUG_ON(!ocfs2_iocb_is_rw_locked(iocb)); | |
610 | ocfs2_iocb_clear_rw_locked(iocb); | |
611 | up_read(&inode->i_alloc_sem); | |
612 | ocfs2_rw_unlock(inode, 0); | |
613 | } | |
614 | ||
03f981cf JB |
615 | /* |
616 | * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen | |
617 | * from ext3. PageChecked() bits have been removed as OCFS2 does not | |
618 | * do journalled data. | |
619 | */ | |
620 | static void ocfs2_invalidatepage(struct page *page, unsigned long offset) | |
621 | { | |
622 | journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal; | |
623 | ||
624 | journal_invalidatepage(journal, page, offset); | |
625 | } | |
626 | ||
627 | static int ocfs2_releasepage(struct page *page, gfp_t wait) | |
628 | { | |
629 | journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal; | |
630 | ||
631 | if (!page_has_buffers(page)) | |
632 | return 0; | |
633 | return journal_try_to_free_buffers(journal, page, wait); | |
634 | } | |
635 | ||
ccd979bd MF |
636 | static ssize_t ocfs2_direct_IO(int rw, |
637 | struct kiocb *iocb, | |
638 | const struct iovec *iov, | |
639 | loff_t offset, | |
640 | unsigned long nr_segs) | |
641 | { | |
642 | struct file *file = iocb->ki_filp; | |
d28c9174 | 643 | struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host; |
ccd979bd MF |
644 | int ret; |
645 | ||
646 | mlog_entry_void(); | |
53013cba MF |
647 | |
648 | /* | |
649 | * We get PR data locks even for O_DIRECT. This allows | |
650 | * concurrent O_DIRECT I/O but doesn't let O_DIRECT with | |
651 | * extending and buffered zeroing writes race. If they did | |
652 | * race then the buffered zeroing could be written back after | |
653 | * the O_DIRECT I/O. It's one thing to tell people not to mix | |
654 | * buffered and O_DIRECT writes, but expecting them to | |
655 | * understand that file extension is also an implicit buffered | |
656 | * write is too much. By getting the PR we force writeback of | |
657 | * the buffered zeroing before proceeding. | |
658 | */ | |
659 | ret = ocfs2_data_lock(inode, 0); | |
660 | if (ret < 0) { | |
661 | mlog_errno(ret); | |
662 | goto out; | |
663 | } | |
664 | ocfs2_data_unlock(inode, 0); | |
665 | ||
ccd979bd MF |
666 | ret = blockdev_direct_IO_no_locking(rw, iocb, inode, |
667 | inode->i_sb->s_bdev, iov, offset, | |
668 | nr_segs, | |
669 | ocfs2_direct_IO_get_blocks, | |
670 | ocfs2_dio_end_io); | |
53013cba | 671 | out: |
ccd979bd MF |
672 | mlog_exit(ret); |
673 | return ret; | |
674 | } | |
675 | ||
f5e54d6e | 676 | const struct address_space_operations ocfs2_aops = { |
ccd979bd MF |
677 | .readpage = ocfs2_readpage, |
678 | .writepage = ocfs2_writepage, | |
679 | .prepare_write = ocfs2_prepare_write, | |
680 | .commit_write = ocfs2_commit_write, | |
681 | .bmap = ocfs2_bmap, | |
682 | .sync_page = block_sync_page, | |
03f981cf JB |
683 | .direct_IO = ocfs2_direct_IO, |
684 | .invalidatepage = ocfs2_invalidatepage, | |
685 | .releasepage = ocfs2_releasepage, | |
686 | .migratepage = buffer_migrate_page, | |
ccd979bd | 687 | }; |