blob: 30306707b2caee141f39b69fb7decaf7d37f3045 [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)) {
241 char *addr = kmap(page);
242 memset(addr, 0, PAGE_SIZE);
243 flush_dcache_page(page);
244 kunmap(page);
245 SetPageUptodate(page);
246 ret = 0;
247 goto out_alloc;
248 }
249
250 ret = ocfs2_data_lock_with_page(inode, 0, page);
251 if (ret != 0) {
252 if (ret == AOP_TRUNCATED_PAGE)
253 unlock = 0;
254 mlog_errno(ret);
255 goto out_alloc;
256 }
257
258 ret = block_read_full_page(page, ocfs2_get_block);
259 unlock = 0;
260
261 ocfs2_data_unlock(inode, 0);
262out_alloc:
263 up_read(&OCFS2_I(inode)->ip_alloc_sem);
Mark Fashehe9dfc0b2007-05-14 11:38:51 -0700264out_meta_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800265 ocfs2_meta_unlock(inode, 0);
266out:
267 if (unlock)
268 unlock_page(page);
269 mlog_exit(ret);
270 return ret;
271}
272
273/* Note: Because we don't support holes, our allocation has
274 * already happened (allocation writes zeros to the file data)
275 * so we don't have to worry about ordered writes in
276 * ocfs2_writepage.
277 *
278 * ->writepage is called during the process of invalidating the page cache
279 * during blocked lock processing. It can't block on any cluster locks
280 * to during block mapping. It's relying on the fact that the block
281 * mapping can't have disappeared under the dirty pages that it is
282 * being asked to write back.
283 */
284static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
285{
286 int ret;
287
288 mlog_entry("(0x%p)\n", page);
289
290 ret = block_write_full_page(page, ocfs2_get_block, wbc);
291
292 mlog_exit(ret);
293
294 return ret;
295}
296
Mark Fasheh50691202007-02-09 20:52:53 -0800297/*
298 * This is called from ocfs2_write_zero_page() which has handled it's
299 * own cluster locking and has ensured allocation exists for those
300 * blocks to be written.
301 */
Mark Fasheh53013cb2006-05-05 19:04:03 -0700302int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
303 unsigned from, unsigned to)
304{
305 int ret;
306
307 down_read(&OCFS2_I(inode)->ip_alloc_sem);
308
309 ret = block_prepare_write(page, from, to, ocfs2_get_block);
310
311 up_read(&OCFS2_I(inode)->ip_alloc_sem);
312
313 return ret;
314}
315
Mark Fashehccd979b2005-12-15 14:31:24 -0800316/* Taken from ext3. We don't necessarily need the full blown
317 * functionality yet, but IMHO it's better to cut and paste the whole
318 * thing so we can avoid introducing our own bugs (and easily pick up
319 * their fixes when they happen) --Mark */
Mark Fasheh60b11392007-02-16 11:46:50 -0800320int walk_page_buffers( handle_t *handle,
321 struct buffer_head *head,
322 unsigned from,
323 unsigned to,
324 int *partial,
325 int (*fn)( handle_t *handle,
326 struct buffer_head *bh))
Mark Fashehccd979b2005-12-15 14:31:24 -0800327{
328 struct buffer_head *bh;
329 unsigned block_start, block_end;
330 unsigned blocksize = head->b_size;
331 int err, ret = 0;
332 struct buffer_head *next;
333
334 for ( bh = head, block_start = 0;
335 ret == 0 && (bh != head || !block_start);
336 block_start = block_end, bh = next)
337 {
338 next = bh->b_this_page;
339 block_end = block_start + blocksize;
340 if (block_end <= from || block_start >= to) {
341 if (partial && !buffer_uptodate(bh))
342 *partial = 1;
343 continue;
344 }
345 err = (*fn)(handle, bh);
346 if (!ret)
347 ret = err;
348 }
349 return ret;
350}
351
Mark Fasheh1fabe142006-10-09 18:11:45 -0700352handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800353 struct page *page,
354 unsigned from,
355 unsigned to)
356{
357 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700358 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 int ret = 0;
360
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700361 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800362 if (!handle) {
363 ret = -ENOMEM;
364 mlog_errno(ret);
365 goto out;
366 }
367
368 if (ocfs2_should_order_data(inode)) {
Mark Fasheh1fabe142006-10-09 18:11:45 -0700369 ret = walk_page_buffers(handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800370 page_buffers(page),
371 from, to, NULL,
372 ocfs2_journal_dirty_data);
373 if (ret < 0)
374 mlog_errno(ret);
375 }
376out:
377 if (ret) {
378 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700379 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800380 handle = ERR_PTR(ret);
381 }
382 return handle;
383}
384
Mark Fashehccd979b2005-12-15 14:31:24 -0800385static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
386{
387 sector_t status;
388 u64 p_blkno = 0;
389 int err = 0;
390 struct inode *inode = mapping->host;
391
392 mlog_entry("(block = %llu)\n", (unsigned long long)block);
393
394 /* We don't need to lock journal system files, since they aren't
395 * accessed concurrently from multiple nodes.
396 */
397 if (!INODE_JOURNAL(inode)) {
Mark Fasheh4bcec182006-10-09 16:02:40 -0700398 err = ocfs2_meta_lock(inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800399 if (err) {
400 if (err != -ENOENT)
401 mlog_errno(err);
402 goto bail;
403 }
404 down_read(&OCFS2_I(inode)->ip_alloc_sem);
405 }
406
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800407 err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800408
409 if (!INODE_JOURNAL(inode)) {
410 up_read(&OCFS2_I(inode)->ip_alloc_sem);
411 ocfs2_meta_unlock(inode, 0);
412 }
413
414 if (err) {
415 mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
416 (unsigned long long)block);
417 mlog_errno(err);
418 goto bail;
419 }
420
421
422bail:
423 status = err ? 0 : p_blkno;
424
425 mlog_exit((int)status);
426
427 return status;
428}
429
430/*
431 * TODO: Make this into a generic get_blocks function.
432 *
433 * From do_direct_io in direct-io.c:
434 * "So what we do is to permit the ->get_blocks function to populate
435 * bh.b_size with the size of IO which is permitted at this offset and
436 * this i_blkbits."
437 *
438 * This function is called directly from get_more_blocks in direct-io.c.
439 *
440 * called like this: dio->get_blocks(dio->inode, fs_startblk,
441 * fs_count, map_bh, dio->rw == WRITE);
442 */
443static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
Mark Fashehccd979b2005-12-15 14:31:24 -0800444 struct buffer_head *bh_result, int create)
445{
446 int ret;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800447 u64 p_blkno, inode_blocks, contig_blocks;
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800448 unsigned int ext_flags;
Florin Malita184d7d22006-06-03 19:30:10 -0400449 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800450 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800451
Mark Fashehccd979b2005-12-15 14:31:24 -0800452 /* This function won't even be called if the request isn't all
453 * nicely aligned and of the right size, so there's no need
454 * for us to check any of that. */
455
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800456 inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
Mark Fasheh564f8a32006-12-14 13:01:05 -0800457
458 /*
459 * Any write past EOF is not allowed because we'd be extending.
460 */
461 if (create && (iblock + max_blocks) > inode_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800462 ret = -EIO;
463 goto bail;
464 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800465
466 /* This figures out the size of the next contiguous block, and
467 * our logical offset */
Mark Fasheh363041a2007-01-17 12:31:35 -0800468 ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800469 &contig_blocks, &ext_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800470 if (ret) {
471 mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
472 (unsigned long long)iblock);
473 ret = -EIO;
474 goto bail;
475 }
476
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800477 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
478 ocfs2_error(inode->i_sb,
479 "Inode %llu has a hole at block %llu\n",
480 (unsigned long long)OCFS2_I(inode)->ip_blkno,
481 (unsigned long long)iblock);
482 ret = -EROFS;
483 goto bail;
484 }
485
486 /*
487 * get_more_blocks() expects us to describe a hole by clearing
488 * the mapped bit on bh_result().
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800489 *
490 * Consider an unwritten extent as a hole.
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800491 */
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800492 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800493 map_bh(bh_result, inode->i_sb, p_blkno);
494 else {
495 /*
496 * ocfs2_prepare_inode_for_write() should have caught
497 * the case where we'd be filling a hole and triggered
498 * a buffered write instead.
499 */
500 if (create) {
501 ret = -EIO;
502 mlog_errno(ret);
503 goto bail;
504 }
505
506 clear_buffer_mapped(bh_result);
507 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800508
509 /* make sure we don't map more than max_blocks blocks here as
510 that's all the kernel will handle at this point. */
511 if (max_blocks < contig_blocks)
512 contig_blocks = max_blocks;
513 bh_result->b_size = contig_blocks << blocksize_bits;
514bail:
515 return ret;
516}
517
518/*
519 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
520 * particularly interested in the aio/dio case. Like the core uses
521 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
522 * truncation on another.
523 */
524static void ocfs2_dio_end_io(struct kiocb *iocb,
525 loff_t offset,
526 ssize_t bytes,
527 void *private)
528{
Josef Sipekd28c9172006-12-08 02:37:25 -0800529 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700530 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -0800531
532 /* this io's submitter should not have unlocked this before we could */
533 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700534
Mark Fashehccd979b2005-12-15 14:31:24 -0800535 ocfs2_iocb_clear_rw_locked(iocb);
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700536
537 level = ocfs2_iocb_rw_locked_level(iocb);
538 if (!level)
539 up_read(&inode->i_alloc_sem);
540 ocfs2_rw_unlock(inode, level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800541}
542
Joel Becker03f981c2007-01-04 14:54:41 -0800543/*
544 * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
545 * from ext3. PageChecked() bits have been removed as OCFS2 does not
546 * do journalled data.
547 */
548static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
549{
550 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
551
552 journal_invalidatepage(journal, page, offset);
553}
554
555static int ocfs2_releasepage(struct page *page, gfp_t wait)
556{
557 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
558
559 if (!page_has_buffers(page))
560 return 0;
561 return journal_try_to_free_buffers(journal, page, wait);
562}
563
Mark Fashehccd979b2005-12-15 14:31:24 -0800564static ssize_t ocfs2_direct_IO(int rw,
565 struct kiocb *iocb,
566 const struct iovec *iov,
567 loff_t offset,
568 unsigned long nr_segs)
569{
570 struct file *file = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -0800571 struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
Mark Fashehccd979b2005-12-15 14:31:24 -0800572 int ret;
573
574 mlog_entry_void();
Mark Fasheh53013cb2006-05-05 19:04:03 -0700575
Mark Fasheh9517bac2007-02-09 20:24:12 -0800576 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
577 /*
578 * We get PR data locks even for O_DIRECT. This
579 * allows concurrent O_DIRECT I/O but doesn't let
580 * O_DIRECT with extending and buffered zeroing writes
581 * race. If they did race then the buffered zeroing
582 * could be written back after the O_DIRECT I/O. It's
583 * one thing to tell people not to mix buffered and
584 * O_DIRECT writes, but expecting them to understand
585 * that file extension is also an implicit buffered
586 * write is too much. By getting the PR we force
587 * writeback of the buffered zeroing before
588 * proceeding.
589 */
590 ret = ocfs2_data_lock(inode, 0);
591 if (ret < 0) {
592 mlog_errno(ret);
593 goto out;
594 }
595 ocfs2_data_unlock(inode, 0);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700596 }
Mark Fasheh53013cb2006-05-05 19:04:03 -0700597
Mark Fashehccd979b2005-12-15 14:31:24 -0800598 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
599 inode->i_sb->s_bdev, iov, offset,
600 nr_segs,
601 ocfs2_direct_IO_get_blocks,
602 ocfs2_dio_end_io);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700603out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800604 mlog_exit(ret);
605 return ret;
606}
607
Mark Fasheh9517bac2007-02-09 20:24:12 -0800608static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
609 u32 cpos,
610 unsigned int *start,
611 unsigned int *end)
612{
613 unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
614
615 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
616 unsigned int cpp;
617
618 cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
619
620 cluster_start = cpos % cpp;
621 cluster_start = cluster_start << osb->s_clustersize_bits;
622
623 cluster_end = cluster_start + osb->s_clustersize;
624 }
625
626 BUG_ON(cluster_start > PAGE_SIZE);
627 BUG_ON(cluster_end > PAGE_SIZE);
628
629 if (start)
630 *start = cluster_start;
631 if (end)
632 *end = cluster_end;
633}
634
635/*
636 * 'from' and 'to' are the region in the page to avoid zeroing.
637 *
638 * If pagesize > clustersize, this function will avoid zeroing outside
639 * of the cluster boundary.
640 *
641 * from == to == 0 is code for "zero the entire cluster region"
642 */
643static void ocfs2_clear_page_regions(struct page *page,
644 struct ocfs2_super *osb, u32 cpos,
645 unsigned from, unsigned to)
646{
647 void *kaddr;
648 unsigned int cluster_start, cluster_end;
649
650 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
651
652 kaddr = kmap_atomic(page, KM_USER0);
653
654 if (from || to) {
655 if (from > cluster_start)
656 memset(kaddr + cluster_start, 0, from - cluster_start);
657 if (to < cluster_end)
658 memset(kaddr + to, 0, cluster_end - to);
659 } else {
660 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
661 }
662
663 kunmap_atomic(kaddr, KM_USER0);
664}
665
666/*
667 * Some of this taken from block_prepare_write(). We already have our
668 * mapping by now though, and the entire write will be allocating or
669 * it won't, so not much need to use BH_New.
670 *
671 * This will also skip zeroing, which is handled externally.
672 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800673int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
674 struct inode *inode, unsigned int from,
675 unsigned int to, int new)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800676{
677 int ret = 0;
678 struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
679 unsigned int block_end, block_start;
680 unsigned int bsize = 1 << inode->i_blkbits;
681
682 if (!page_has_buffers(page))
683 create_empty_buffers(page, bsize, 0);
684
685 head = page_buffers(page);
686 for (bh = head, block_start = 0; bh != head || !block_start;
687 bh = bh->b_this_page, block_start += bsize) {
688 block_end = block_start + bsize;
689
690 /*
691 * Ignore blocks outside of our i/o range -
692 * they may belong to unallocated clusters.
693 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800694 if (block_start >= to || block_end <= from) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800695 if (PageUptodate(page))
696 set_buffer_uptodate(bh);
697 continue;
698 }
699
700 /*
701 * For an allocating write with cluster size >= page
702 * size, we always write the entire page.
703 */
704
705 if (buffer_new(bh))
706 clear_buffer_new(bh);
707
708 if (!buffer_mapped(bh)) {
709 map_bh(bh, inode->i_sb, *p_blkno);
710 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
711 }
712
713 if (PageUptodate(page)) {
714 if (!buffer_uptodate(bh))
715 set_buffer_uptodate(bh);
716 } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
717 (block_start < from || block_end > to)) {
718 ll_rw_block(READ, 1, &bh);
719 *wait_bh++=bh;
720 }
721
722 *p_blkno = *p_blkno + 1;
723 }
724
725 /*
726 * If we issued read requests - let them complete.
727 */
728 while(wait_bh > wait) {
729 wait_on_buffer(*--wait_bh);
730 if (!buffer_uptodate(*wait_bh))
731 ret = -EIO;
732 }
733
734 if (ret == 0 || !new)
735 return ret;
736
737 /*
738 * If we get -EIO above, zero out any newly allocated blocks
739 * to avoid exposing stale data.
740 */
741 bh = head;
742 block_start = 0;
743 do {
744 void *kaddr;
745
746 block_end = block_start + bsize;
747 if (block_end <= from)
748 goto next_bh;
749 if (block_start >= to)
750 break;
751
752 kaddr = kmap_atomic(page, KM_USER0);
753 memset(kaddr+block_start, 0, bh->b_size);
754 flush_dcache_page(page);
755 kunmap_atomic(kaddr, KM_USER0);
756 set_buffer_uptodate(bh);
757 mark_buffer_dirty(bh);
758
759next_bh:
760 block_start = block_end;
761 bh = bh->b_this_page;
762 } while (bh != head);
763
764 return ret;
765}
766
767/*
Mark Fasheh6af67d82007-03-06 17:24:46 -0800768 * This will copy user data from the buffer page in the splice
769 * context.
770 *
771 * For now, we ignore SPLICE_F_MOVE as that would require some extra
772 * communication out all the way to ocfs2_write().
773 */
774int ocfs2_map_and_write_splice_data(struct inode *inode,
775 struct ocfs2_write_ctxt *wc, u64 *p_blkno,
776 unsigned int *ret_from, unsigned int *ret_to)
777{
778 int ret;
779 unsigned int to, from, cluster_start, cluster_end;
780 char *src, *dst;
781 struct ocfs2_splice_write_priv *sp = wc->w_private;
782 struct pipe_buffer *buf = sp->s_buf;
783 unsigned long bytes, src_from;
784 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
785
786 ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start,
787 &cluster_end);
788
789 from = sp->s_offset;
790 src_from = sp->s_buf_offset;
791 bytes = wc->w_count;
792
793 if (wc->w_large_pages) {
794 /*
795 * For cluster size < page size, we have to
796 * calculate pos within the cluster and obey
797 * the rightmost boundary.
798 */
799 bytes = min(bytes, (unsigned long)(osb->s_clustersize
800 - (wc->w_pos & (osb->s_clustersize - 1))));
801 }
802 to = from + bytes;
803
804 if (wc->w_this_page_new)
805 ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
806 cluster_start, cluster_end, 1);
807 else
808 ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
809 from, to, 0);
810 if (ret) {
811 mlog_errno(ret);
812 goto out;
813 }
814
815 BUG_ON(from > PAGE_CACHE_SIZE);
816 BUG_ON(to > PAGE_CACHE_SIZE);
817 BUG_ON(from > osb->s_clustersize);
818 BUG_ON(to > osb->s_clustersize);
819
820 src = buf->ops->map(sp->s_pipe, buf, 1);
821 dst = kmap_atomic(wc->w_this_page, KM_USER1);
822 memcpy(dst + from, src + src_from, bytes);
823 kunmap_atomic(wc->w_this_page, KM_USER1);
824 buf->ops->unmap(sp->s_pipe, buf, src);
825
826 wc->w_finished_copy = 1;
827
828 *ret_from = from;
829 *ret_to = to;
830out:
831
832 return bytes ? (unsigned int)bytes : ret;
833}
834
835/*
Mark Fasheh9517bac2007-02-09 20:24:12 -0800836 * This will copy user data from the iovec in the buffered write
837 * context.
838 */
839int ocfs2_map_and_write_user_data(struct inode *inode,
840 struct ocfs2_write_ctxt *wc, u64 *p_blkno,
841 unsigned int *ret_from, unsigned int *ret_to)
842{
843 int ret;
844 unsigned int to, from, cluster_start, cluster_end;
845 unsigned long bytes, src_from;
846 char *dst;
847 struct ocfs2_buffered_write_priv *bp = wc->w_private;
848 const struct iovec *cur_iov = bp->b_cur_iov;
849 char __user *buf;
850 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
851
852 ocfs2_figure_cluster_boundaries(osb, wc->w_cpos, &cluster_start,
853 &cluster_end);
854
855 buf = cur_iov->iov_base + bp->b_cur_off;
856 src_from = (unsigned long)buf & ~PAGE_CACHE_MASK;
857
858 from = wc->w_pos & (PAGE_CACHE_SIZE - 1);
859
860 /*
861 * This is a lot of comparisons, but it reads quite
862 * easily, which is important here.
863 */
864 /* Stay within the src page */
865 bytes = PAGE_SIZE - src_from;
866 /* Stay within the vector */
867 bytes = min(bytes,
868 (unsigned long)(cur_iov->iov_len - bp->b_cur_off));
869 /* Stay within count */
870 bytes = min(bytes, (unsigned long)wc->w_count);
871 /*
872 * For clustersize > page size, just stay within
873 * target page, otherwise we have to calculate pos
874 * within the cluster and obey the rightmost
875 * boundary.
876 */
877 if (wc->w_large_pages) {
878 /*
879 * For cluster size < page size, we have to
880 * calculate pos within the cluster and obey
881 * the rightmost boundary.
882 */
883 bytes = min(bytes, (unsigned long)(osb->s_clustersize
884 - (wc->w_pos & (osb->s_clustersize - 1))));
885 } else {
886 /*
887 * cluster size > page size is the most common
888 * case - we just stay within the target page
889 * boundary.
890 */
891 bytes = min(bytes, PAGE_CACHE_SIZE - from);
892 }
893
894 to = from + bytes;
895
896 if (wc->w_this_page_new)
897 ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
898 cluster_start, cluster_end, 1);
899 else
900 ret = ocfs2_map_page_blocks(wc->w_this_page, p_blkno, inode,
901 from, to, 0);
902 if (ret) {
903 mlog_errno(ret);
904 goto out;
905 }
906
907 BUG_ON(from > PAGE_CACHE_SIZE);
908 BUG_ON(to > PAGE_CACHE_SIZE);
909 BUG_ON(from > osb->s_clustersize);
910 BUG_ON(to > osb->s_clustersize);
911
912 dst = kmap(wc->w_this_page);
913 memcpy(dst + from, bp->b_src_buf + src_from, bytes);
914 kunmap(wc->w_this_page);
915
916 /*
917 * XXX: This is slow, but simple. The caller of
918 * ocfs2_buffered_write_cluster() is responsible for
919 * passing through the iovecs, so it's difficult to
920 * predict what our next step is in here after our
921 * initial write. A future version should be pushing
922 * that iovec manipulation further down.
923 *
924 * By setting this, we indicate that a copy from user
925 * data was done, and subsequent calls for this
926 * cluster will skip copying more data.
927 */
928 wc->w_finished_copy = 1;
929
930 *ret_from = from;
931 *ret_to = to;
932out:
933
934 return bytes ? (unsigned int)bytes : ret;
935}
936
937/*
938 * Map, fill and write a page to disk.
939 *
940 * The work of copying data is done via callback. Newly allocated
941 * pages which don't take user data will be zero'd (set 'new' to
942 * indicate an allocating write)
943 *
944 * Returns a negative error code or the number of bytes copied into
945 * the page.
946 */
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700947static int ocfs2_write_data_page(struct inode *inode, handle_t *handle,
948 u64 *p_blkno, struct page *page,
949 struct ocfs2_write_ctxt *wc, int new)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800950{
951 int ret, copied = 0;
952 unsigned int from = 0, to = 0;
953 unsigned int cluster_start, cluster_end;
954 unsigned int zero_from = 0, zero_to = 0;
955
956 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), wc->w_cpos,
957 &cluster_start, &cluster_end);
958
959 if ((wc->w_pos >> PAGE_CACHE_SHIFT) == page->index
960 && !wc->w_finished_copy) {
961
962 wc->w_this_page = page;
963 wc->w_this_page_new = new;
964 ret = wc->w_write_data_page(inode, wc, p_blkno, &from, &to);
965 if (ret < 0) {
966 mlog_errno(ret);
967 goto out;
968 }
969
970 copied = ret;
971
972 zero_from = from;
973 zero_to = to;
974 if (new) {
975 from = cluster_start;
976 to = cluster_end;
977 }
978 } else {
979 /*
980 * If we haven't allocated the new page yet, we
981 * shouldn't be writing it out without copying user
982 * data. This is likely a math error from the caller.
983 */
984 BUG_ON(!new);
985
986 from = cluster_start;
987 to = cluster_end;
988
989 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
990 cluster_start, cluster_end, 1);
991 if (ret) {
992 mlog_errno(ret);
993 goto out;
994 }
995 }
996
997 /*
998 * Parts of newly allocated pages need to be zero'd.
999 *
1000 * Above, we have also rewritten 'to' and 'from' - as far as
1001 * the rest of the function is concerned, the entire cluster
1002 * range inside of a page needs to be written.
1003 *
1004 * We can skip this if the page is up to date - it's already
1005 * been zero'd from being read in as a hole.
1006 */
1007 if (new && !PageUptodate(page))
1008 ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
1009 wc->w_cpos, zero_from, zero_to);
1010
1011 flush_dcache_page(page);
1012
1013 if (ocfs2_should_order_data(inode)) {
1014 ret = walk_page_buffers(handle,
1015 page_buffers(page),
1016 from, to, NULL,
1017 ocfs2_journal_dirty_data);
1018 if (ret < 0)
1019 mlog_errno(ret);
1020 }
1021
1022 /*
1023 * We don't use generic_commit_write() because we need to
1024 * handle our own i_size update.
1025 */
1026 ret = block_commit_write(page, from, to);
1027 if (ret)
1028 mlog_errno(ret);
1029out:
1030
1031 return copied ? copied : ret;
1032}
1033
1034/*
1035 * Do the actual write of some data into an inode. Optionally allocate
1036 * in order to fulfill the write.
1037 *
1038 * cpos is the logical cluster offset within the file to write at
1039 *
1040 * 'phys' is the physical mapping of that offset. a 'phys' value of
1041 * zero indicates that allocation is required. In this case, data_ac
1042 * and meta_ac should be valid (meta_ac can be null if metadata
1043 * allocation isn't required).
1044 */
1045static ssize_t ocfs2_write(struct file *file, u32 phys, handle_t *handle,
1046 struct buffer_head *di_bh,
1047 struct ocfs2_alloc_context *data_ac,
1048 struct ocfs2_alloc_context *meta_ac,
1049 struct ocfs2_write_ctxt *wc)
1050{
1051 int ret, i, numpages = 1, new;
1052 unsigned int copied = 0;
1053 u32 tmp_pos;
1054 u64 v_blkno, p_blkno;
1055 struct address_space *mapping = file->f_mapping;
1056 struct inode *inode = mapping->host;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001057 unsigned long index, start;
1058 struct page **cpages;
1059
1060 new = phys == 0 ? 1 : 0;
1061
1062 /*
1063 * Figure out how many pages we'll be manipulating here. For
Mark Fasheh60b11392007-02-16 11:46:50 -08001064 * non allocating write, we just change the one
1065 * page. Otherwise, we'll need a whole clusters worth.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001066 */
Mark Fasheh60b11392007-02-16 11:46:50 -08001067 if (new)
1068 numpages = ocfs2_pages_per_cluster(inode->i_sb);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001069
1070 cpages = kzalloc(sizeof(*cpages) * numpages, GFP_NOFS);
1071 if (!cpages) {
1072 ret = -ENOMEM;
1073 mlog_errno(ret);
1074 return ret;
1075 }
1076
1077 /*
1078 * Fill our page array first. That way we've grabbed enough so
1079 * that we can zero and flush if we error after adding the
1080 * extent.
1081 */
1082 if (new) {
1083 start = ocfs2_align_clusters_to_page_index(inode->i_sb,
1084 wc->w_cpos);
1085 v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, wc->w_cpos);
1086 } else {
1087 start = wc->w_pos >> PAGE_CACHE_SHIFT;
1088 v_blkno = wc->w_pos >> inode->i_sb->s_blocksize_bits;
1089 }
1090
1091 for(i = 0; i < numpages; i++) {
1092 index = start + i;
1093
Mark Fasheh9315f132007-05-01 17:44:20 -07001094 cpages[i] = find_or_create_page(mapping, index, GFP_NOFS);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001095 if (!cpages[i]) {
1096 ret = -ENOMEM;
1097 mlog_errno(ret);
1098 goto out;
1099 }
1100 }
1101
1102 if (new) {
1103 /*
1104 * This is safe to call with the page locks - it won't take
1105 * any additional semaphores or cluster locks.
1106 */
1107 tmp_pos = wc->w_cpos;
1108 ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
1109 &tmp_pos, 1, di_bh, handle,
1110 data_ac, meta_ac, NULL);
1111 /*
1112 * This shouldn't happen because we must have already
1113 * calculated the correct meta data allocation required. The
1114 * internal tree allocation code should know how to increase
1115 * transaction credits itself.
1116 *
1117 * If need be, we could handle -EAGAIN for a
1118 * RESTART_TRANS here.
1119 */
1120 mlog_bug_on_msg(ret == -EAGAIN,
1121 "Inode %llu: EAGAIN return during allocation.\n",
1122 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1123 if (ret < 0) {
1124 mlog_errno(ret);
1125 goto out;
1126 }
1127 }
1128
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001129 ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
1130 NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001131 if (ret < 0) {
1132
1133 /*
1134 * XXX: Should we go readonly here?
1135 */
1136
1137 mlog_errno(ret);
1138 goto out;
1139 }
1140
1141 BUG_ON(p_blkno == 0);
1142
1143 for(i = 0; i < numpages; i++) {
1144 ret = ocfs2_write_data_page(inode, handle, &p_blkno, cpages[i],
1145 wc, new);
1146 if (ret < 0) {
1147 mlog_errno(ret);
1148 goto out;
1149 }
1150
1151 copied += ret;
1152 }
1153
1154out:
1155 for(i = 0; i < numpages; i++) {
1156 unlock_page(cpages[i]);
1157 mark_page_accessed(cpages[i]);
1158 page_cache_release(cpages[i]);
1159 }
1160 kfree(cpages);
1161
1162 return copied ? copied : ret;
1163}
1164
1165static void ocfs2_write_ctxt_init(struct ocfs2_write_ctxt *wc,
1166 struct ocfs2_super *osb, loff_t pos,
1167 size_t count, ocfs2_page_writer *cb,
1168 void *cb_priv)
1169{
1170 wc->w_count = count;
1171 wc->w_pos = pos;
1172 wc->w_cpos = wc->w_pos >> osb->s_clustersize_bits;
1173 wc->w_finished_copy = 0;
1174
1175 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
1176 wc->w_large_pages = 1;
1177 else
1178 wc->w_large_pages = 0;
1179
1180 wc->w_write_data_page = cb;
1181 wc->w_private = cb_priv;
1182}
1183
1184/*
1185 * Write a cluster to an inode. The cluster may not be allocated yet,
1186 * in which case it will be. This only exists for buffered writes -
1187 * O_DIRECT takes a more "traditional" path through the kernel.
1188 *
1189 * The caller is responsible for incrementing pos, written counts, etc
1190 *
1191 * For file systems that don't support sparse files, pre-allocation
1192 * and page zeroing up until cpos should be done prior to this
1193 * function call.
1194 *
1195 * Callers should be holding i_sem, and the rw cluster lock.
1196 *
1197 * Returns the number of user bytes written, or less than zero for
1198 * error.
1199 */
1200ssize_t ocfs2_buffered_write_cluster(struct file *file, loff_t pos,
1201 size_t count, ocfs2_page_writer *actor,
1202 void *priv)
1203{
1204 int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
1205 ssize_t written = 0;
1206 u32 phys;
1207 struct inode *inode = file->f_mapping->host;
1208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1209 struct buffer_head *di_bh = NULL;
1210 struct ocfs2_dinode *di;
1211 struct ocfs2_alloc_context *data_ac = NULL;
1212 struct ocfs2_alloc_context *meta_ac = NULL;
1213 handle_t *handle;
1214 struct ocfs2_write_ctxt wc;
1215
1216 ocfs2_write_ctxt_init(&wc, osb, pos, count, actor, priv);
1217
1218 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1219 if (ret) {
1220 mlog_errno(ret);
1221 goto out;
1222 }
1223 di = (struct ocfs2_dinode *)di_bh->b_data;
1224
1225 /*
1226 * Take alloc sem here to prevent concurrent lookups. That way
1227 * the mapping, zeroing and tree manipulation within
1228 * ocfs2_write() will be safe against ->readpage(). This
1229 * should also serve to lock out allocation from a shared
1230 * writeable region.
1231 */
1232 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1233
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001234 ret = ocfs2_get_clusters(inode, wc.w_cpos, &phys, NULL, NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001235 if (ret) {
1236 mlog_errno(ret);
1237 goto out_meta;
1238 }
1239
1240 /* phys == 0 means that allocation is required. */
1241 if (phys == 0) {
1242 ret = ocfs2_lock_allocators(inode, di, 1, &data_ac, &meta_ac);
1243 if (ret) {
1244 mlog_errno(ret);
1245 goto out_meta;
1246 }
1247
1248 credits = ocfs2_calc_extend_credits(inode->i_sb, di, 1);
1249 }
1250
1251 ret = ocfs2_data_lock(inode, 1);
1252 if (ret) {
1253 mlog_errno(ret);
1254 goto out_meta;
1255 }
1256
1257 handle = ocfs2_start_trans(osb, credits);
1258 if (IS_ERR(handle)) {
1259 ret = PTR_ERR(handle);
1260 mlog_errno(ret);
1261 goto out_data;
1262 }
1263
1264 written = ocfs2_write(file, phys, handle, di_bh, data_ac,
1265 meta_ac, &wc);
1266 if (written < 0) {
1267 ret = written;
1268 mlog_errno(ret);
1269 goto out_commit;
1270 }
1271
1272 ret = ocfs2_journal_access(handle, inode, di_bh,
1273 OCFS2_JOURNAL_ACCESS_WRITE);
1274 if (ret) {
1275 mlog_errno(ret);
1276 goto out_commit;
1277 }
1278
1279 pos += written;
1280 if (pos > inode->i_size) {
1281 i_size_write(inode, pos);
1282 mark_inode_dirty(inode);
1283 }
Mark Fasheh8110b072007-03-22 16:53:23 -07001284 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001285 di->i_size = cpu_to_le64((u64)i_size_read(inode));
1286 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1287 di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
1288 di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1289
1290 ret = ocfs2_journal_dirty(handle, di_bh);
1291 if (ret)
1292 mlog_errno(ret);
1293
1294out_commit:
1295 ocfs2_commit_trans(osb, handle);
1296
1297out_data:
1298 ocfs2_data_unlock(inode, 1);
1299
1300out_meta:
1301 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1302 ocfs2_meta_unlock(inode, 1);
1303
1304out:
1305 brelse(di_bh);
1306 if (data_ac)
1307 ocfs2_free_alloc_context(data_ac);
1308 if (meta_ac)
1309 ocfs2_free_alloc_context(meta_ac);
1310
1311 return written ? written : ret;
1312}
1313
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001314const struct address_space_operations ocfs2_aops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001315 .readpage = ocfs2_readpage,
1316 .writepage = ocfs2_writepage,
Mark Fashehccd979b2005-12-15 14:31:24 -08001317 .bmap = ocfs2_bmap,
1318 .sync_page = block_sync_page,
Joel Becker03f981c2007-01-04 14:54:41 -08001319 .direct_IO = ocfs2_direct_IO,
1320 .invalidatepage = ocfs2_invalidatepage,
1321 .releasepage = ocfs2_releasepage,
1322 .migratepage = buffer_migrate_page,
Mark Fashehccd979b2005-12-15 14:31:24 -08001323};