blob: ec8b606b30e1d5883590db2b0b44654d7afb9c39 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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>
Mark Fasheh9517bac2007-02-09 20:24:12 -080027#include <linux/swap.h>
Mark Fasheh6af67d82007-03-06 17:24:46 -080028#include <linux/pipe_fs_i.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080029
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"
Mark Fasheh9517bac2007-02-09 20:24:12 -080042#include "suballoc.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080043#include "super.h"
44#include "symlink.h"
45
46#include "buffer_head_io.h"
47
48static 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)) {
Mark Fashehb06970532006-03-03 10:24:33 -080080 mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -070081 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
82 fe->i_signature);
Mark Fashehccd979b2005-12-15 14:31:24 -080083 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
129bail:
130 if (bh)
131 brelse(bh);
132
133 mlog_exit(err);
134 return err;
135}
136
137static int ocfs2_get_block(struct inode *inode, sector_t iblock,
138 struct buffer_head *bh_result, int create)
139{
140 int err = 0;
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800141 unsigned int ext_flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800142 u64 p_blkno, past_eof;
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800143 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800144
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
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800158 err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL,
159 &ext_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800160 if (err) {
161 mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
Mark Fashehb06970532006-03-03 10:24:33 -0800162 "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
163 (unsigned long long)p_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800164 goto bail;
165 }
166
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800167 /*
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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800176
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800177 /* Treat the unwritten extent as a hole for zeroing purposes. */
178 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800179 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 }
192
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);
196
197 if (create && (iblock >= past_eof))
198 set_buffer_new(bh_result);
Mark Fashehccd979b2005-12-15 14:31:24 -0800199 }
200
Mark Fashehccd979b2005-12-15 14:31:24 -0800201bail:
202 if (err < 0)
203 err = -EIO;
204
205 mlog_exit(err);
206 return err;
207}
208
209static int ocfs2_readpage(struct file *file, struct page *page)
210{
211 struct inode *inode = page->mapping->host;
212 loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
213 int ret, unlock = 1;
214
215 mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
216
Mark Fasheh4bcec182006-10-09 16:02:40 -0700217 ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page);
Mark Fashehccd979b2005-12-15 14:31:24 -0800218 if (ret != 0) {
219 if (ret == AOP_TRUNCATED_PAGE)
220 unlock = 0;
221 mlog_errno(ret);
222 goto out;
223 }
224
Mark Fashehe9dfc0b2007-05-14 11:38:51 -0700225 if (down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem) == 0) {
226 ret = AOP_TRUNCATED_PAGE;
227 goto out_meta_unlock;
228 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800229
230 /*
231 * i_size might have just been updated as we grabed the meta lock. We
232 * might now be discovering a truncate that hit on another node.
233 * block_read_full_page->get_block freaks out if it is asked to read
234 * beyond the end of a file, so we check here. Callers
235 * (generic_file_read, fault->nopage) are clever enough to check i_size
236 * and notice that the page they just read isn't needed.
237 *
238 * XXX sys_readahead() seems to get that wrong?
239 */
240 if (start >= i_size_read(inode)) {
Nate Diller5c3c6bb2007-05-10 22:56:01 -0700241 zero_user_page(page, 0, PAGE_SIZE, KM_USER0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800242 SetPageUptodate(page);
243 ret = 0;
244 goto out_alloc;
245 }
246
247 ret = ocfs2_data_lock_with_page(inode, 0, page);
248 if (ret != 0) {
249 if (ret == AOP_TRUNCATED_PAGE)
250 unlock = 0;
251 mlog_errno(ret);
252 goto out_alloc;
253 }
254
255 ret = block_read_full_page(page, ocfs2_get_block);
256 unlock = 0;
257
258 ocfs2_data_unlock(inode, 0);
259out_alloc:
260 up_read(&OCFS2_I(inode)->ip_alloc_sem);
Mark Fashehe9dfc0b2007-05-14 11:38:51 -0700261out_meta_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800262 ocfs2_meta_unlock(inode, 0);
263out:
264 if (unlock)
265 unlock_page(page);
266 mlog_exit(ret);
267 return ret;
268}
269
270/* Note: Because we don't support holes, our allocation has
271 * already happened (allocation writes zeros to the file data)
272 * so we don't have to worry about ordered writes in
273 * ocfs2_writepage.
274 *
275 * ->writepage is called during the process of invalidating the page cache
276 * during blocked lock processing. It can't block on any cluster locks
277 * to during block mapping. It's relying on the fact that the block
278 * mapping can't have disappeared under the dirty pages that it is
279 * being asked to write back.
280 */
281static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
282{
283 int ret;
284
285 mlog_entry("(0x%p)\n", page);
286
287 ret = block_write_full_page(page, ocfs2_get_block, wbc);
288
289 mlog_exit(ret);
290
291 return ret;
292}
293
Mark Fasheh50691202007-02-09 20:52:53 -0800294/*
295 * This is called from ocfs2_write_zero_page() which has handled it's
296 * own cluster locking and has ensured allocation exists for those
297 * blocks to be written.
298 */
Mark Fasheh53013cb2006-05-05 19:04:03 -0700299int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
300 unsigned from, unsigned to)
301{
302 int ret;
303
304 down_read(&OCFS2_I(inode)->ip_alloc_sem);
305
306 ret = block_prepare_write(page, from, to, ocfs2_get_block);
307
308 up_read(&OCFS2_I(inode)->ip_alloc_sem);
309
310 return ret;
311}
312
Mark Fashehccd979b2005-12-15 14:31:24 -0800313/* Taken from ext3. We don't necessarily need the full blown
314 * functionality yet, but IMHO it's better to cut and paste the whole
315 * thing so we can avoid introducing our own bugs (and easily pick up
316 * their fixes when they happen) --Mark */
Mark Fasheh60b11392007-02-16 11:46:50 -0800317int walk_page_buffers( handle_t *handle,
318 struct buffer_head *head,
319 unsigned from,
320 unsigned to,
321 int *partial,
322 int (*fn)( handle_t *handle,
323 struct buffer_head *bh))
Mark Fashehccd979b2005-12-15 14:31:24 -0800324{
325 struct buffer_head *bh;
326 unsigned block_start, block_end;
327 unsigned blocksize = head->b_size;
328 int err, ret = 0;
329 struct buffer_head *next;
330
331 for ( bh = head, block_start = 0;
332 ret == 0 && (bh != head || !block_start);
333 block_start = block_end, bh = next)
334 {
335 next = bh->b_this_page;
336 block_end = block_start + blocksize;
337 if (block_end <= from || block_start >= to) {
338 if (partial && !buffer_uptodate(bh))
339 *partial = 1;
340 continue;
341 }
342 err = (*fn)(handle, bh);
343 if (!ret)
344 ret = err;
345 }
346 return ret;
347}
348
Mark Fasheh1fabe142006-10-09 18:11:45 -0700349handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800350 struct page *page,
351 unsigned from,
352 unsigned to)
353{
354 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700355 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800356 int ret = 0;
357
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700358 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 if (!handle) {
360 ret = -ENOMEM;
361 mlog_errno(ret);
362 goto out;
363 }
364
365 if (ocfs2_should_order_data(inode)) {
Mark Fasheh1fabe142006-10-09 18:11:45 -0700366 ret = walk_page_buffers(handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800367 page_buffers(page),
368 from, to, NULL,
369 ocfs2_journal_dirty_data);
370 if (ret < 0)
371 mlog_errno(ret);
372 }
373out:
374 if (ret) {
375 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700376 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 handle = ERR_PTR(ret);
378 }
379 return handle;
380}
381
Mark Fashehccd979b2005-12-15 14:31:24 -0800382static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
383{
384 sector_t status;
385 u64 p_blkno = 0;
386 int err = 0;
387 struct inode *inode = mapping->host;
388
389 mlog_entry("(block = %llu)\n", (unsigned long long)block);
390
391 /* We don't need to lock journal system files, since they aren't
392 * accessed concurrently from multiple nodes.
393 */
394 if (!INODE_JOURNAL(inode)) {
Mark Fasheh4bcec182006-10-09 16:02:40 -0700395 err = ocfs2_meta_lock(inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800396 if (err) {
397 if (err != -ENOENT)
398 mlog_errno(err);
399 goto bail;
400 }
401 down_read(&OCFS2_I(inode)->ip_alloc_sem);
402 }
403
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800404 err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800405
406 if (!INODE_JOURNAL(inode)) {
407 up_read(&OCFS2_I(inode)->ip_alloc_sem);
408 ocfs2_meta_unlock(inode, 0);
409 }
410
411 if (err) {
412 mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
413 (unsigned long long)block);
414 mlog_errno(err);
415 goto bail;
416 }
417
418
419bail:
420 status = err ? 0 : p_blkno;
421
422 mlog_exit((int)status);
423
424 return status;
425}
426
427/*
428 * TODO: Make this into a generic get_blocks function.
429 *
430 * From do_direct_io in direct-io.c:
431 * "So what we do is to permit the ->get_blocks function to populate
432 * bh.b_size with the size of IO which is permitted at this offset and
433 * this i_blkbits."
434 *
435 * This function is called directly from get_more_blocks in direct-io.c.
436 *
437 * called like this: dio->get_blocks(dio->inode, fs_startblk,
438 * fs_count, map_bh, dio->rw == WRITE);
439 */
440static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
Mark Fashehccd979b2005-12-15 14:31:24 -0800441 struct buffer_head *bh_result, int create)
442{
443 int ret;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800444 u64 p_blkno, inode_blocks, contig_blocks;
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800445 unsigned int ext_flags;
Florin Malita184d7d22006-06-03 19:30:10 -0400446 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800447 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800448
Mark Fashehccd979b2005-12-15 14:31:24 -0800449 /* This function won't even be called if the request isn't all
450 * nicely aligned and of the right size, so there's no need
451 * for us to check any of that. */
452
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800453 inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
Mark Fasheh564f8a32006-12-14 13:01:05 -0800454
455 /*
456 * Any write past EOF is not allowed because we'd be extending.
457 */
458 if (create && (iblock + max_blocks) > inode_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800459 ret = -EIO;
460 goto bail;
461 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800462
463 /* This figures out the size of the next contiguous block, and
464 * our logical offset */
Mark Fasheh363041a2007-01-17 12:31:35 -0800465 ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800466 &contig_blocks, &ext_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800467 if (ret) {
468 mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
469 (unsigned long long)iblock);
470 ret = -EIO;
471 goto bail;
472 }
473
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800474 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
475 ocfs2_error(inode->i_sb,
476 "Inode %llu has a hole at block %llu\n",
477 (unsigned long long)OCFS2_I(inode)->ip_blkno,
478 (unsigned long long)iblock);
479 ret = -EROFS;
480 goto bail;
481 }
482
483 /*
484 * get_more_blocks() expects us to describe a hole by clearing
485 * the mapped bit on bh_result().
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800486 *
487 * Consider an unwritten extent as a hole.
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800488 */
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800489 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800490 map_bh(bh_result, inode->i_sb, p_blkno);
491 else {
492 /*
493 * ocfs2_prepare_inode_for_write() should have caught
494 * the case where we'd be filling a hole and triggered
495 * a buffered write instead.
496 */
497 if (create) {
498 ret = -EIO;
499 mlog_errno(ret);
500 goto bail;
501 }
502
503 clear_buffer_mapped(bh_result);
504 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800505
506 /* make sure we don't map more than max_blocks blocks here as
507 that's all the kernel will handle at this point. */
508 if (max_blocks < contig_blocks)
509 contig_blocks = max_blocks;
510 bh_result->b_size = contig_blocks << blocksize_bits;
511bail:
512 return ret;
513}
514
515/*
516 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
517 * particularly interested in the aio/dio case. Like the core uses
518 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
519 * truncation on another.
520 */
521static void ocfs2_dio_end_io(struct kiocb *iocb,
522 loff_t offset,
523 ssize_t bytes,
524 void *private)
525{
Josef Sipekd28c9172006-12-08 02:37:25 -0800526 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700527 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -0800528
529 /* this io's submitter should not have unlocked this before we could */
530 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700531
Mark Fashehccd979b2005-12-15 14:31:24 -0800532 ocfs2_iocb_clear_rw_locked(iocb);
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700533
534 level = ocfs2_iocb_rw_locked_level(iocb);
535 if (!level)
536 up_read(&inode->i_alloc_sem);
537 ocfs2_rw_unlock(inode, level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800538}
539
Joel Becker03f981c2007-01-04 14:54:41 -0800540/*
541 * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
542 * from ext3. PageChecked() bits have been removed as OCFS2 does not
543 * do journalled data.
544 */
545static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
546{
547 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
548
549 journal_invalidatepage(journal, page, offset);
550}
551
552static int ocfs2_releasepage(struct page *page, gfp_t wait)
553{
554 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
555
556 if (!page_has_buffers(page))
557 return 0;
558 return journal_try_to_free_buffers(journal, page, wait);
559}
560
Mark Fashehccd979b2005-12-15 14:31:24 -0800561static ssize_t ocfs2_direct_IO(int rw,
562 struct kiocb *iocb,
563 const struct iovec *iov,
564 loff_t offset,
565 unsigned long nr_segs)
566{
567 struct file *file = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -0800568 struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
Mark Fashehccd979b2005-12-15 14:31:24 -0800569 int ret;
570
571 mlog_entry_void();
Mark Fasheh53013cb2006-05-05 19:04:03 -0700572
Mark Fasheh9517bac2007-02-09 20:24:12 -0800573 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
574 /*
575 * We get PR data locks even for O_DIRECT. This
576 * allows concurrent O_DIRECT I/O but doesn't let
577 * O_DIRECT with extending and buffered zeroing writes
578 * race. If they did race then the buffered zeroing
579 * could be written back after the O_DIRECT I/O. It's
580 * one thing to tell people not to mix buffered and
581 * O_DIRECT writes, but expecting them to understand
582 * that file extension is also an implicit buffered
583 * write is too much. By getting the PR we force
584 * writeback of the buffered zeroing before
585 * proceeding.
586 */
587 ret = ocfs2_data_lock(inode, 0);
588 if (ret < 0) {
589 mlog_errno(ret);
590 goto out;
591 }
592 ocfs2_data_unlock(inode, 0);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700593 }
Mark Fasheh53013cb2006-05-05 19:04:03 -0700594
Mark Fashehccd979b2005-12-15 14:31:24 -0800595 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
596 inode->i_sb->s_bdev, iov, offset,
597 nr_segs,
598 ocfs2_direct_IO_get_blocks,
599 ocfs2_dio_end_io);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700600out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800601 mlog_exit(ret);
602 return ret;
603}
604
Mark Fasheh9517bac2007-02-09 20:24:12 -0800605static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
606 u32 cpos,
607 unsigned int *start,
608 unsigned int *end)
609{
610 unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
611
612 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
613 unsigned int cpp;
614
615 cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
616
617 cluster_start = cpos % cpp;
618 cluster_start = cluster_start << osb->s_clustersize_bits;
619
620 cluster_end = cluster_start + osb->s_clustersize;
621 }
622
623 BUG_ON(cluster_start > PAGE_SIZE);
624 BUG_ON(cluster_end > PAGE_SIZE);
625
626 if (start)
627 *start = cluster_start;
628 if (end)
629 *end = cluster_end;
630}
631
632/*
633 * 'from' and 'to' are the region in the page to avoid zeroing.
634 *
635 * If pagesize > clustersize, this function will avoid zeroing outside
636 * of the cluster boundary.
637 *
638 * from == to == 0 is code for "zero the entire cluster region"
639 */
640static void ocfs2_clear_page_regions(struct page *page,
641 struct ocfs2_super *osb, u32 cpos,
642 unsigned from, unsigned to)
643{
644 void *kaddr;
645 unsigned int cluster_start, cluster_end;
646
647 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
648
649 kaddr = kmap_atomic(page, KM_USER0);
650
651 if (from || to) {
652 if (from > cluster_start)
653 memset(kaddr + cluster_start, 0, from - cluster_start);
654 if (to < cluster_end)
655 memset(kaddr + to, 0, cluster_end - to);
656 } else {
657 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
658 }
659
660 kunmap_atomic(kaddr, KM_USER0);
661}
662
663/*
664 * Some of this taken from block_prepare_write(). We already have our
665 * mapping by now though, and the entire write will be allocating or
666 * it won't, so not much need to use BH_New.
667 *
668 * This will also skip zeroing, which is handled externally.
669 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800670int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
671 struct inode *inode, unsigned int from,
672 unsigned int to, int new)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800673{
674 int ret = 0;
675 struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
676 unsigned int block_end, block_start;
677 unsigned int bsize = 1 << inode->i_blkbits;
678
679 if (!page_has_buffers(page))
680 create_empty_buffers(page, bsize, 0);
681
682 head = page_buffers(page);
683 for (bh = head, block_start = 0; bh != head || !block_start;
684 bh = bh->b_this_page, block_start += bsize) {
685 block_end = block_start + bsize;
686
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700687 clear_buffer_new(bh);
688
Mark Fasheh9517bac2007-02-09 20:24:12 -0800689 /*
690 * Ignore blocks outside of our i/o range -
691 * they may belong to unallocated clusters.
692 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800693 if (block_start >= to || block_end <= from) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800694 if (PageUptodate(page))
695 set_buffer_uptodate(bh);
696 continue;
697 }
698
699 /*
700 * For an allocating write with cluster size >= page
701 * size, we always write the entire page.
702 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700703 if (new)
704 set_buffer_new(bh);
Mark Fasheh9517bac2007-02-09 20:24:12 -0800705
706 if (!buffer_mapped(bh)) {
707 map_bh(bh, inode->i_sb, *p_blkno);
708 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
709 }
710
711 if (PageUptodate(page)) {
712 if (!buffer_uptodate(bh))
713 set_buffer_uptodate(bh);
714 } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
Mark Fashehbce99762007-06-18 11:12:36 -0700715 !buffer_new(bh) &&
716 (block_start < from || block_end > to)) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800717 ll_rw_block(READ, 1, &bh);
718 *wait_bh++=bh;
719 }
720
721 *p_blkno = *p_blkno + 1;
722 }
723
724 /*
725 * If we issued read requests - let them complete.
726 */
727 while(wait_bh > wait) {
728 wait_on_buffer(*--wait_bh);
729 if (!buffer_uptodate(*wait_bh))
730 ret = -EIO;
731 }
732
733 if (ret == 0 || !new)
734 return ret;
735
736 /*
737 * If we get -EIO above, zero out any newly allocated blocks
738 * to avoid exposing stale data.
739 */
740 bh = head;
741 block_start = 0;
742 do {
743 void *kaddr;
744
745 block_end = block_start + bsize;
746 if (block_end <= from)
747 goto next_bh;
748 if (block_start >= to)
749 break;
750
751 kaddr = kmap_atomic(page, KM_USER0);
752 memset(kaddr+block_start, 0, bh->b_size);
753 flush_dcache_page(page);
754 kunmap_atomic(kaddr, KM_USER0);
755 set_buffer_uptodate(bh);
756 mark_buffer_dirty(bh);
757
758next_bh:
759 block_start = block_end;
760 bh = bh->b_this_page;
761 } while (bh != head);
762
763 return ret;
764}
765
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700766#if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
767#define OCFS2_MAX_CTXT_PAGES 1
768#else
769#define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
770#endif
Mark Fasheh6af67d82007-03-06 17:24:46 -0800771
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700772#define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
Mark Fasheh6af67d82007-03-06 17:24:46 -0800773
774/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700775 * Describe the state of a single cluster to be written to.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800776 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700777struct ocfs2_write_cluster_desc {
778 u32 c_cpos;
779 u32 c_phys;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800780 /*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700781 * Give this a unique field because c_phys eventually gets
782 * filled.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800783 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700784 unsigned c_new;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700785 unsigned c_unwritten;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700786};
Mark Fasheh9517bac2007-02-09 20:24:12 -0800787
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700788static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
789{
790 return d->c_new || d->c_unwritten;
791}
792
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700793struct ocfs2_write_ctxt {
794 /* Logical cluster position / len of write */
795 u32 w_cpos;
796 u32 w_clen;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800797
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700798 struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
Mark Fasheh9517bac2007-02-09 20:24:12 -0800799
800 /*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700801 * This is true if page_size > cluster_size.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800802 *
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700803 * It triggers a set of special cases during write which might
804 * have to deal with allocating writes to partial pages.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800805 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700806 unsigned int w_large_pages;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800807
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700808 /*
809 * Pages involved in this write.
810 *
811 * w_target_page is the page being written to by the user.
812 *
813 * w_pages is an array of pages which always contains
814 * w_target_page, and in the case of an allocating write with
815 * page_size < cluster size, it will contain zero'd and mapped
816 * pages adjacent to w_target_page which need to be written
817 * out in so that future reads from that region will get
818 * zero's.
819 */
820 struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
821 unsigned int w_num_pages;
822 struct page *w_target_page;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800823
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700824 /*
825 * ocfs2_write_end() uses this to know what the real range to
826 * write in the target should be.
827 */
828 unsigned int w_target_from;
829 unsigned int w_target_to;
830
831 /*
832 * We could use journal_current_handle() but this is cleaner,
833 * IMHO -Mark
834 */
835 handle_t *w_handle;
836
837 struct buffer_head *w_di_bh;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700838
839 struct ocfs2_cached_dealloc_ctxt w_dealloc;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700840};
841
842static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
843{
844 int i;
845
846 for(i = 0; i < wc->w_num_pages; i++) {
847 if (wc->w_pages[i] == NULL)
848 continue;
849
850 unlock_page(wc->w_pages[i]);
851 mark_page_accessed(wc->w_pages[i]);
852 page_cache_release(wc->w_pages[i]);
853 }
854
855 brelse(wc->w_di_bh);
856 kfree(wc);
857}
858
859static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
860 struct ocfs2_super *osb, loff_t pos,
Mark Fasheh607d44a2007-05-09 15:14:45 -0700861 unsigned len, struct buffer_head *di_bh)
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700862{
863 struct ocfs2_write_ctxt *wc;
864
865 wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
866 if (!wc)
867 return -ENOMEM;
868
869 wc->w_cpos = pos >> osb->s_clustersize_bits;
870 wc->w_clen = ocfs2_clusters_for_bytes(osb->sb, len);
Mark Fasheh607d44a2007-05-09 15:14:45 -0700871 get_bh(di_bh);
872 wc->w_di_bh = di_bh;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700873
874 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
875 wc->w_large_pages = 1;
876 else
877 wc->w_large_pages = 0;
878
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700879 ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
880
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700881 *wcp = wc;
882
883 return 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800884}
885
886/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700887 * If a page has any new buffers, zero them out here, and mark them uptodate
888 * and dirty so they'll be written out (in order to prevent uninitialised
889 * block data from leaking). And clear the new bit.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800890 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700891static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800892{
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700893 unsigned int block_start, block_end;
894 struct buffer_head *head, *bh;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800895
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700896 BUG_ON(!PageLocked(page));
897 if (!page_has_buffers(page))
898 return;
899
900 bh = head = page_buffers(page);
901 block_start = 0;
902 do {
903 block_end = block_start + bh->b_size;
904
905 if (buffer_new(bh)) {
906 if (block_end > from && block_start < to) {
907 if (!PageUptodate(page)) {
908 unsigned start, end;
909 void *kaddr;
910
911 start = max(from, block_start);
912 end = min(to, block_end);
913
914 kaddr = kmap_atomic(page, KM_USER0);
915 memset(kaddr+start, 0, end - start);
916 flush_dcache_page(page);
917 kunmap_atomic(kaddr, KM_USER0);
918 set_buffer_uptodate(bh);
919 }
920
921 clear_buffer_new(bh);
922 mark_buffer_dirty(bh);
923 }
924 }
925
926 block_start = block_end;
927 bh = bh->b_this_page;
928 } while (bh != head);
929}
930
931/*
932 * Only called when we have a failure during allocating write to write
933 * zero's to the newly allocated region.
934 */
935static void ocfs2_write_failure(struct inode *inode,
936 struct ocfs2_write_ctxt *wc,
937 loff_t user_pos, unsigned user_len)
938{
939 int i;
940 unsigned from, to;
941 struct page *tmppage;
942
943 ocfs2_zero_new_buffers(wc->w_target_page, user_pos, user_len);
944
945 if (wc->w_large_pages) {
946 from = wc->w_target_from;
947 to = wc->w_target_to;
948 } else {
949 from = 0;
950 to = PAGE_CACHE_SIZE;
951 }
952
953 for(i = 0; i < wc->w_num_pages; i++) {
954 tmppage = wc->w_pages[i];
955
956 if (ocfs2_should_order_data(inode))
957 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
958 from, to, NULL,
959 ocfs2_journal_dirty_data);
960
961 block_commit_write(tmppage, from, to);
962 }
963}
964
965static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
966 struct ocfs2_write_ctxt *wc,
967 struct page *page, u32 cpos,
968 loff_t user_pos, unsigned user_len,
969 int new)
970{
971 int ret;
972 unsigned int map_from = 0, map_to = 0;
973 unsigned int cluster_start, cluster_end;
974 unsigned int user_data_from = 0, user_data_to = 0;
975
976 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
Mark Fasheh9517bac2007-02-09 20:24:12 -0800977 &cluster_start, &cluster_end);
978
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700979 if (page == wc->w_target_page) {
980 map_from = user_pos & (PAGE_CACHE_SIZE - 1);
981 map_to = map_from + user_len;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800982
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700983 if (new)
984 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
985 cluster_start, cluster_end,
986 new);
987 else
988 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
989 map_from, map_to, new);
990 if (ret) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800991 mlog_errno(ret);
992 goto out;
993 }
994
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700995 user_data_from = map_from;
996 user_data_to = map_to;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800997 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700998 map_from = cluster_start;
999 map_to = cluster_end;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001000 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001001
1002 wc->w_target_from = map_from;
1003 wc->w_target_to = map_to;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001004 } else {
1005 /*
1006 * If we haven't allocated the new page yet, we
1007 * shouldn't be writing it out without copying user
1008 * data. This is likely a math error from the caller.
1009 */
1010 BUG_ON(!new);
1011
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001012 map_from = cluster_start;
1013 map_to = cluster_end;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001014
1015 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001016 cluster_start, cluster_end, new);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001017 if (ret) {
1018 mlog_errno(ret);
1019 goto out;
1020 }
1021 }
1022
1023 /*
1024 * Parts of newly allocated pages need to be zero'd.
1025 *
1026 * Above, we have also rewritten 'to' and 'from' - as far as
1027 * the rest of the function is concerned, the entire cluster
1028 * range inside of a page needs to be written.
1029 *
1030 * We can skip this if the page is up to date - it's already
1031 * been zero'd from being read in as a hole.
1032 */
1033 if (new && !PageUptodate(page))
1034 ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001035 cpos, user_data_from, user_data_to);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001036
1037 flush_dcache_page(page);
1038
Mark Fasheh9517bac2007-02-09 20:24:12 -08001039out:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001040 return ret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001041}
1042
1043/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001044 * This function will only grab one clusters worth of pages.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001045 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001046static int ocfs2_grab_pages_for_write(struct address_space *mapping,
1047 struct ocfs2_write_ctxt *wc,
Mark Fasheh7307de82007-05-09 15:16:19 -07001048 u32 cpos, loff_t user_pos, int new,
1049 struct page *mmap_page)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001050{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001051 int ret = 0, i;
1052 unsigned long start, target_index, index;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001053 struct inode *inode = mapping->host;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001054
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001055 target_index = user_pos >> PAGE_CACHE_SHIFT;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001056
1057 /*
1058 * Figure out how many pages we'll be manipulating here. For
Mark Fasheh60b11392007-02-16 11:46:50 -08001059 * non allocating write, we just change the one
1060 * page. Otherwise, we'll need a whole clusters worth.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001061 */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001062 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001063 wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
1064 start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001065 } else {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001066 wc->w_num_pages = 1;
1067 start = target_index;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001068 }
1069
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001070 for(i = 0; i < wc->w_num_pages; i++) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001071 index = start + i;
1072
Mark Fasheh7307de82007-05-09 15:16:19 -07001073 if (index == target_index && mmap_page) {
1074 /*
1075 * ocfs2_pagemkwrite() is a little different
1076 * and wants us to directly use the page
1077 * passed in.
1078 */
1079 lock_page(mmap_page);
1080
1081 if (mmap_page->mapping != mapping) {
1082 unlock_page(mmap_page);
1083 /*
1084 * Sanity check - the locking in
1085 * ocfs2_pagemkwrite() should ensure
1086 * that this code doesn't trigger.
1087 */
1088 ret = -EINVAL;
1089 mlog_errno(ret);
1090 goto out;
1091 }
1092
1093 page_cache_get(mmap_page);
1094 wc->w_pages[i] = mmap_page;
1095 } else {
1096 wc->w_pages[i] = find_or_create_page(mapping, index,
1097 GFP_NOFS);
1098 if (!wc->w_pages[i]) {
1099 ret = -ENOMEM;
1100 mlog_errno(ret);
1101 goto out;
1102 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001103 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001104
1105 if (index == target_index)
1106 wc->w_target_page = wc->w_pages[i];
Mark Fasheh9517bac2007-02-09 20:24:12 -08001107 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001108out:
1109 return ret;
1110}
1111
1112/*
1113 * Prepare a single cluster for write one cluster into the file.
1114 */
1115static int ocfs2_write_cluster(struct address_space *mapping,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001116 u32 phys, unsigned int unwritten,
1117 struct ocfs2_alloc_context *data_ac,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001118 struct ocfs2_alloc_context *meta_ac,
1119 struct ocfs2_write_ctxt *wc, u32 cpos,
1120 loff_t user_pos, unsigned user_len)
1121{
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001122 int ret, i, new, should_zero = 0;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001123 u64 v_blkno, p_blkno;
1124 struct inode *inode = mapping->host;
1125
1126 new = phys == 0 ? 1 : 0;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001127 if (new || unwritten)
1128 should_zero = 1;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001129
1130 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001131 u32 tmp_pos;
1132
Mark Fasheh9517bac2007-02-09 20:24:12 -08001133 /*
1134 * This is safe to call with the page locks - it won't take
1135 * any additional semaphores or cluster locks.
1136 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001137 tmp_pos = cpos;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001138 ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001139 &tmp_pos, 1, 0, wc->w_di_bh,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001140 wc->w_handle, data_ac,
1141 meta_ac, NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001142 /*
1143 * This shouldn't happen because we must have already
1144 * calculated the correct meta data allocation required. The
1145 * internal tree allocation code should know how to increase
1146 * transaction credits itself.
1147 *
1148 * If need be, we could handle -EAGAIN for a
1149 * RESTART_TRANS here.
1150 */
1151 mlog_bug_on_msg(ret == -EAGAIN,
1152 "Inode %llu: EAGAIN return during allocation.\n",
1153 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1154 if (ret < 0) {
1155 mlog_errno(ret);
1156 goto out;
1157 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001158 } else if (unwritten) {
1159 ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
1160 wc->w_handle, cpos, 1, phys,
1161 meta_ac, &wc->w_dealloc);
1162 if (ret < 0) {
1163 mlog_errno(ret);
1164 goto out;
1165 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001166 }
1167
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001168 if (should_zero)
1169 v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
1170 else
1171 v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
1172
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001173 /*
1174 * The only reason this should fail is due to an inability to
1175 * find the extent added.
1176 */
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001177 ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
1178 NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001179 if (ret < 0) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001180 ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
1181 "at logical block %llu",
1182 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1183 (unsigned long long)v_blkno);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001184 goto out;
1185 }
1186
1187 BUG_ON(p_blkno == 0);
1188
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001189 for(i = 0; i < wc->w_num_pages; i++) {
1190 int tmpret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001191
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001192 tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
1193 wc->w_pages[i], cpos,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001194 user_pos, user_len,
1195 should_zero);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001196 if (tmpret) {
1197 mlog_errno(tmpret);
1198 if (ret == 0)
1199 tmpret = ret;
1200 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001201 }
1202
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001203 /*
1204 * We only have cleanup to do in case of allocating write.
1205 */
1206 if (ret && new)
1207 ocfs2_write_failure(inode, wc, user_pos, user_len);
1208
Mark Fasheh9517bac2007-02-09 20:24:12 -08001209out:
Mark Fasheh9517bac2007-02-09 20:24:12 -08001210
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001211 return ret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001212}
1213
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001214static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
1215 struct ocfs2_alloc_context *data_ac,
1216 struct ocfs2_alloc_context *meta_ac,
1217 struct ocfs2_write_ctxt *wc,
1218 loff_t pos, unsigned len)
1219{
1220 int ret, i;
1221 struct ocfs2_write_cluster_desc *desc;
1222
1223 for (i = 0; i < wc->w_clen; i++) {
1224 desc = &wc->w_desc[i];
1225
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001226 ret = ocfs2_write_cluster(mapping, desc->c_phys,
1227 desc->c_unwritten, data_ac, meta_ac,
1228 wc, desc->c_cpos, pos, len);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001229 if (ret) {
1230 mlog_errno(ret);
1231 goto out;
1232 }
1233 }
1234
1235 ret = 0;
1236out:
1237 return ret;
1238}
1239
Mark Fasheh9517bac2007-02-09 20:24:12 -08001240/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001241 * ocfs2_write_end() wants to know which parts of the target page it
1242 * should complete the write on. It's easiest to compute them ahead of
1243 * time when a more complete view of the write is available.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001244 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001245static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
1246 struct ocfs2_write_ctxt *wc,
1247 loff_t pos, unsigned len, int alloc)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001248{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001249 struct ocfs2_write_cluster_desc *desc;
1250
1251 wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
1252 wc->w_target_to = wc->w_target_from + len;
1253
1254 if (alloc == 0)
1255 return;
1256
1257 /*
1258 * Allocating write - we may have different boundaries based
1259 * on page size and cluster size.
1260 *
1261 * NOTE: We can no longer compute one value from the other as
1262 * the actual write length and user provided length may be
1263 * different.
1264 */
1265
1266 if (wc->w_large_pages) {
1267 /*
1268 * We only care about the 1st and last cluster within
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001269 * our range and whether they should be zero'd or not. Either
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001270 * value may be extended out to the start/end of a
1271 * newly allocated cluster.
1272 */
1273 desc = &wc->w_desc[0];
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001274 if (ocfs2_should_zero_cluster(desc))
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001275 ocfs2_figure_cluster_boundaries(osb,
1276 desc->c_cpos,
1277 &wc->w_target_from,
1278 NULL);
1279
1280 desc = &wc->w_desc[wc->w_clen - 1];
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001281 if (ocfs2_should_zero_cluster(desc))
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001282 ocfs2_figure_cluster_boundaries(osb,
1283 desc->c_cpos,
1284 NULL,
1285 &wc->w_target_to);
1286 } else {
1287 wc->w_target_from = 0;
1288 wc->w_target_to = PAGE_CACHE_SIZE;
1289 }
1290}
1291
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001292/*
1293 * Populate each single-cluster write descriptor in the write context
1294 * with information about the i/o to be done.
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001295 *
1296 * Returns the number of clusters that will have to be allocated, as
1297 * well as a worst case estimate of the number of extent records that
1298 * would have to be created during a write to an unwritten region.
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001299 */
1300static int ocfs2_populate_write_desc(struct inode *inode,
1301 struct ocfs2_write_ctxt *wc,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001302 unsigned int *clusters_to_alloc,
1303 unsigned int *extents_to_split)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001304{
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001305 int ret;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001306 struct ocfs2_write_cluster_desc *desc;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001307 unsigned int num_clusters = 0;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001308 unsigned int ext_flags = 0;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001309 u32 phys = 0;
1310 int i;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001311
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001312 *clusters_to_alloc = 0;
1313 *extents_to_split = 0;
1314
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001315 for (i = 0; i < wc->w_clen; i++) {
1316 desc = &wc->w_desc[i];
1317 desc->c_cpos = wc->w_cpos + i;
1318
1319 if (num_clusters == 0) {
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001320 /*
1321 * Need to look up the next extent record.
1322 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001323 ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001324 &num_clusters, &ext_flags);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001325 if (ret) {
1326 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001327 goto out;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001328 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001329
1330 /*
1331 * Assume worst case - that we're writing in
1332 * the middle of the extent.
1333 *
1334 * We can assume that the write proceeds from
1335 * left to right, in which case the extent
1336 * insert code is smart enough to coalesce the
1337 * next splits into the previous records created.
1338 */
1339 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1340 *extents_to_split = *extents_to_split + 2;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001341 } else if (phys) {
1342 /*
1343 * Only increment phys if it doesn't describe
1344 * a hole.
1345 */
1346 phys++;
1347 }
1348
1349 desc->c_phys = phys;
1350 if (phys == 0) {
1351 desc->c_new = 1;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001352 *clusters_to_alloc = *clusters_to_alloc + 1;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001353 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001354 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1355 desc->c_unwritten = 1;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001356
1357 num_clusters--;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001358 }
1359
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001360 ret = 0;
1361out:
1362 return ret;
1363}
1364
1365int ocfs2_write_begin_nolock(struct address_space *mapping,
1366 loff_t pos, unsigned len, unsigned flags,
1367 struct page **pagep, void **fsdata,
1368 struct buffer_head *di_bh, struct page *mmap_page)
1369{
1370 int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001371 unsigned int clusters_to_alloc, extents_to_split;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001372 struct ocfs2_write_ctxt *wc;
1373 struct inode *inode = mapping->host;
1374 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1375 struct ocfs2_dinode *di;
1376 struct ocfs2_alloc_context *data_ac = NULL;
1377 struct ocfs2_alloc_context *meta_ac = NULL;
1378 handle_t *handle;
1379
1380 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
1381 if (ret) {
1382 mlog_errno(ret);
1383 return ret;
1384 }
1385
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001386 ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
1387 &extents_to_split);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001388 if (ret) {
1389 mlog_errno(ret);
1390 goto out;
1391 }
1392
1393 di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1394
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001395 /*
1396 * We set w_target_from, w_target_to here so that
1397 * ocfs2_write_end() knows which range in the target page to
1398 * write out. An allocation requires that we write the entire
1399 * cluster range.
1400 */
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001401 if (clusters_to_alloc || extents_to_split) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001402 /*
1403 * XXX: We are stretching the limits of
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001404 * ocfs2_lock_allocators(). It greatly over-estimates
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001405 * the work to be done.
1406 */
1407 ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001408 extents_to_split, &data_ac, &meta_ac);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001409 if (ret) {
1410 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001411 goto out;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001412 }
1413
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001414 credits = ocfs2_calc_extend_credits(inode->i_sb, di,
1415 clusters_to_alloc);
1416
Mark Fasheh9517bac2007-02-09 20:24:12 -08001417 }
1418
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001419 ocfs2_set_target_boundaries(osb, wc, pos, len,
1420 clusters_to_alloc + extents_to_split);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001421
Mark Fasheh9517bac2007-02-09 20:24:12 -08001422 handle = ocfs2_start_trans(osb, credits);
1423 if (IS_ERR(handle)) {
1424 ret = PTR_ERR(handle);
1425 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001426 goto out;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001427 }
1428
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001429 wc->w_handle = handle;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001430
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001431 /*
1432 * We don't want this to fail in ocfs2_write_end(), so do it
1433 * here.
1434 */
1435 ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001436 OCFS2_JOURNAL_ACCESS_WRITE);
1437 if (ret) {
1438 mlog_errno(ret);
1439 goto out_commit;
1440 }
1441
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001442 /*
1443 * Fill our page array first. That way we've grabbed enough so
1444 * that we can zero and flush if we error after adding the
1445 * extent.
1446 */
1447 ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001448 clusters_to_alloc + extents_to_split,
1449 mmap_page);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001450 if (ret) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001451 mlog_errno(ret);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001452 goto out_commit;
1453 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001454
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001455 ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
1456 len);
1457 if (ret) {
1458 mlog_errno(ret);
1459 goto out_commit;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001460 }
1461
1462 if (data_ac)
1463 ocfs2_free_alloc_context(data_ac);
1464 if (meta_ac)
1465 ocfs2_free_alloc_context(meta_ac);
1466
1467 *pagep = wc->w_target_page;
1468 *fsdata = wc;
1469 return 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001470out_commit:
1471 ocfs2_commit_trans(osb, handle);
1472
Mark Fasheh9517bac2007-02-09 20:24:12 -08001473out:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001474 ocfs2_free_write_ctxt(wc);
1475
Mark Fasheh9517bac2007-02-09 20:24:12 -08001476 if (data_ac)
1477 ocfs2_free_alloc_context(data_ac);
1478 if (meta_ac)
1479 ocfs2_free_alloc_context(meta_ac);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001480 return ret;
1481}
Mark Fasheh9517bac2007-02-09 20:24:12 -08001482
Mark Fasheh607d44a2007-05-09 15:14:45 -07001483int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1484 loff_t pos, unsigned len, unsigned flags,
1485 struct page **pagep, void **fsdata)
1486{
1487 int ret;
1488 struct buffer_head *di_bh = NULL;
1489 struct inode *inode = mapping->host;
1490
1491 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1492 if (ret) {
1493 mlog_errno(ret);
1494 return ret;
1495 }
1496
1497 /*
1498 * Take alloc sem here to prevent concurrent lookups. That way
1499 * the mapping, zeroing and tree manipulation within
1500 * ocfs2_write() will be safe against ->readpage(). This
1501 * should also serve to lock out allocation from a shared
1502 * writeable region.
1503 */
1504 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1505
1506 ret = ocfs2_data_lock(inode, 1);
1507 if (ret) {
1508 mlog_errno(ret);
1509 goto out_fail;
1510 }
1511
1512 ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
Mark Fasheh7307de82007-05-09 15:16:19 -07001513 fsdata, di_bh, NULL);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001514 if (ret) {
1515 mlog_errno(ret);
1516 goto out_fail_data;
1517 }
1518
1519 brelse(di_bh);
1520
1521 return 0;
1522
1523out_fail_data:
1524 ocfs2_data_unlock(inode, 1);
1525out_fail:
1526 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1527
1528 brelse(di_bh);
1529 ocfs2_meta_unlock(inode, 1);
1530
1531 return ret;
1532}
1533
Mark Fasheh7307de82007-05-09 15:16:19 -07001534int ocfs2_write_end_nolock(struct address_space *mapping,
1535 loff_t pos, unsigned len, unsigned copied,
1536 struct page *page, void *fsdata)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001537{
1538 int i;
1539 unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
1540 struct inode *inode = mapping->host;
1541 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1542 struct ocfs2_write_ctxt *wc = fsdata;
1543 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1544 handle_t *handle = wc->w_handle;
1545 struct page *tmppage;
1546
1547 if (unlikely(copied < len)) {
1548 if (!PageUptodate(wc->w_target_page))
1549 copied = 0;
1550
1551 ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
1552 start+len);
1553 }
1554 flush_dcache_page(wc->w_target_page);
1555
1556 for(i = 0; i < wc->w_num_pages; i++) {
1557 tmppage = wc->w_pages[i];
1558
1559 if (tmppage == wc->w_target_page) {
1560 from = wc->w_target_from;
1561 to = wc->w_target_to;
1562
1563 BUG_ON(from > PAGE_CACHE_SIZE ||
1564 to > PAGE_CACHE_SIZE ||
1565 to < from);
1566 } else {
1567 /*
1568 * Pages adjacent to the target (if any) imply
1569 * a hole-filling write in which case we want
1570 * to flush their entire range.
1571 */
1572 from = 0;
1573 to = PAGE_CACHE_SIZE;
1574 }
1575
1576 if (ocfs2_should_order_data(inode))
1577 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
1578 from, to, NULL,
1579 ocfs2_journal_dirty_data);
1580
1581 block_commit_write(tmppage, from, to);
1582 }
1583
1584 pos += copied;
1585 if (pos > inode->i_size) {
1586 i_size_write(inode, pos);
1587 mark_inode_dirty(inode);
1588 }
1589 inode->i_blocks = ocfs2_inode_sector_count(inode);
1590 di->i_size = cpu_to_le64((u64)i_size_read(inode));
1591 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1592 di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
1593 di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001594 ocfs2_journal_dirty(handle, wc->w_di_bh);
1595
1596 ocfs2_commit_trans(osb, handle);
Mark Fasheh59a5e412007-06-22 15:52:36 -07001597
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001598 ocfs2_run_deallocs(osb, &wc->w_dealloc);
1599
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001600 ocfs2_free_write_ctxt(wc);
1601
1602 return copied;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001603}
1604
Mark Fasheh607d44a2007-05-09 15:14:45 -07001605int ocfs2_write_end(struct file *file, struct address_space *mapping,
1606 loff_t pos, unsigned len, unsigned copied,
1607 struct page *page, void *fsdata)
1608{
1609 int ret;
1610 struct inode *inode = mapping->host;
1611
1612 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
1613
1614 ocfs2_data_unlock(inode, 1);
1615 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1616 ocfs2_meta_unlock(inode, 1);
1617
1618 return ret;
1619}
1620
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001621const struct address_space_operations ocfs2_aops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001622 .readpage = ocfs2_readpage,
1623 .writepage = ocfs2_writepage,
Mark Fashehccd979b2005-12-15 14:31:24 -08001624 .bmap = ocfs2_bmap,
1625 .sync_page = block_sync_page,
Joel Becker03f981c2007-01-04 14:54:41 -08001626 .direct_IO = ocfs2_direct_IO,
1627 .invalidatepage = ocfs2_invalidatepage,
1628 .releasepage = ocfs2_releasepage,
1629 .migratepage = buffer_migrate_page,
Mark Fashehccd979b2005-12-15 14:31:24 -08001630};