blob: ac8c3905571703f0a6cd5f0ff26474927c1ddb88 [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
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700209int ocfs2_read_inline_data(struct inode *inode, struct page *page,
210 struct buffer_head *di_bh)
Mark Fasheh6798d352007-09-07 14:05:51 -0700211{
212 void *kaddr;
213 unsigned int size;
214 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
215
216 if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
217 ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag",
218 (unsigned long long)OCFS2_I(inode)->ip_blkno);
219 return -EROFS;
220 }
221
222 size = i_size_read(inode);
223
224 if (size > PAGE_CACHE_SIZE ||
225 size > ocfs2_max_inline_data(inode->i_sb)) {
226 ocfs2_error(inode->i_sb,
227 "Inode %llu has with inline data has bad size: %u",
228 (unsigned long long)OCFS2_I(inode)->ip_blkno, size);
229 return -EROFS;
230 }
231
232 kaddr = kmap_atomic(page, KM_USER0);
233 if (size)
234 memcpy(kaddr, di->id2.i_data.id_data, size);
235 /* Clear the remaining part of the page */
236 memset(kaddr + size, 0, PAGE_CACHE_SIZE - size);
237 flush_dcache_page(page);
238 kunmap_atomic(kaddr, KM_USER0);
239
240 SetPageUptodate(page);
241
242 return 0;
243}
244
245static int ocfs2_readpage_inline(struct inode *inode, struct page *page)
246{
247 int ret;
248 struct buffer_head *di_bh = NULL;
249 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
250
251 BUG_ON(!PageLocked(page));
252 BUG_ON(!OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
253
254 ret = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &di_bh,
255 OCFS2_BH_CACHED, inode);
256 if (ret) {
257 mlog_errno(ret);
258 goto out;
259 }
260
261 ret = ocfs2_read_inline_data(inode, page, di_bh);
262out:
263 unlock_page(page);
264
265 brelse(di_bh);
266 return ret;
267}
268
Mark Fashehccd979b2005-12-15 14:31:24 -0800269static int ocfs2_readpage(struct file *file, struct page *page)
270{
271 struct inode *inode = page->mapping->host;
Mark Fasheh6798d352007-09-07 14:05:51 -0700272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800273 loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
274 int ret, unlock = 1;
275
276 mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
277
Mark Fashehe63aecb62007-10-18 15:30:42 -0700278 ret = ocfs2_inode_lock_with_page(inode, NULL, 0, page);
Mark Fashehccd979b2005-12-15 14:31:24 -0800279 if (ret != 0) {
280 if (ret == AOP_TRUNCATED_PAGE)
281 unlock = 0;
282 mlog_errno(ret);
283 goto out;
284 }
285
Mark Fasheh6798d352007-09-07 14:05:51 -0700286 if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
Mark Fashehe9dfc0b2007-05-14 11:38:51 -0700287 ret = AOP_TRUNCATED_PAGE;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700288 goto out_inode_unlock;
Mark Fashehe9dfc0b2007-05-14 11:38:51 -0700289 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800290
291 /*
292 * i_size might have just been updated as we grabed the meta lock. We
293 * might now be discovering a truncate that hit on another node.
294 * block_read_full_page->get_block freaks out if it is asked to read
295 * beyond the end of a file, so we check here. Callers
Nick Piggin54cb8822007-07-19 01:46:59 -0700296 * (generic_file_read, vm_ops->fault) are clever enough to check i_size
Mark Fashehccd979b2005-12-15 14:31:24 -0800297 * and notice that the page they just read isn't needed.
298 *
299 * XXX sys_readahead() seems to get that wrong?
300 */
301 if (start >= i_size_read(inode)) {
Nate Diller5c3c6bb2007-05-10 22:56:01 -0700302 zero_user_page(page, 0, PAGE_SIZE, KM_USER0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800303 SetPageUptodate(page);
304 ret = 0;
305 goto out_alloc;
306 }
307
Mark Fasheh6798d352007-09-07 14:05:51 -0700308 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
309 ret = ocfs2_readpage_inline(inode, page);
310 else
311 ret = block_read_full_page(page, ocfs2_get_block);
Mark Fashehccd979b2005-12-15 14:31:24 -0800312 unlock = 0;
313
Mark Fashehccd979b2005-12-15 14:31:24 -0800314out_alloc:
315 up_read(&OCFS2_I(inode)->ip_alloc_sem);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700316out_inode_unlock:
317 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800318out:
319 if (unlock)
320 unlock_page(page);
321 mlog_exit(ret);
322 return ret;
323}
324
325/* Note: Because we don't support holes, our allocation has
326 * already happened (allocation writes zeros to the file data)
327 * so we don't have to worry about ordered writes in
328 * ocfs2_writepage.
329 *
330 * ->writepage is called during the process of invalidating the page cache
331 * during blocked lock processing. It can't block on any cluster locks
332 * to during block mapping. It's relying on the fact that the block
333 * mapping can't have disappeared under the dirty pages that it is
334 * being asked to write back.
335 */
336static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
337{
338 int ret;
339
340 mlog_entry("(0x%p)\n", page);
341
342 ret = block_write_full_page(page, ocfs2_get_block, wbc);
343
344 mlog_exit(ret);
345
346 return ret;
347}
348
Mark Fasheh50691202007-02-09 20:52:53 -0800349/*
350 * This is called from ocfs2_write_zero_page() which has handled it's
351 * own cluster locking and has ensured allocation exists for those
352 * blocks to be written.
353 */
Mark Fasheh53013cb2006-05-05 19:04:03 -0700354int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
355 unsigned from, unsigned to)
356{
357 int ret;
358
Mark Fasheh53013cb2006-05-05 19:04:03 -0700359 ret = block_prepare_write(page, from, to, ocfs2_get_block);
360
Mark Fasheh53013cb2006-05-05 19:04:03 -0700361 return ret;
362}
363
Mark Fashehccd979b2005-12-15 14:31:24 -0800364/* Taken from ext3. We don't necessarily need the full blown
365 * functionality yet, but IMHO it's better to cut and paste the whole
366 * thing so we can avoid introducing our own bugs (and easily pick up
367 * their fixes when they happen) --Mark */
Mark Fasheh60b11392007-02-16 11:46:50 -0800368int walk_page_buffers( handle_t *handle,
369 struct buffer_head *head,
370 unsigned from,
371 unsigned to,
372 int *partial,
373 int (*fn)( handle_t *handle,
374 struct buffer_head *bh))
Mark Fashehccd979b2005-12-15 14:31:24 -0800375{
376 struct buffer_head *bh;
377 unsigned block_start, block_end;
378 unsigned blocksize = head->b_size;
379 int err, ret = 0;
380 struct buffer_head *next;
381
382 for ( bh = head, block_start = 0;
383 ret == 0 && (bh != head || !block_start);
384 block_start = block_end, bh = next)
385 {
386 next = bh->b_this_page;
387 block_end = block_start + blocksize;
388 if (block_end <= from || block_start >= to) {
389 if (partial && !buffer_uptodate(bh))
390 *partial = 1;
391 continue;
392 }
393 err = (*fn)(handle, bh);
394 if (!ret)
395 ret = err;
396 }
397 return ret;
398}
399
Mark Fasheh1fabe142006-10-09 18:11:45 -0700400handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 struct page *page,
402 unsigned from,
403 unsigned to)
404{
405 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700406 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800407 int ret = 0;
408
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700409 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800410 if (!handle) {
411 ret = -ENOMEM;
412 mlog_errno(ret);
413 goto out;
414 }
415
416 if (ocfs2_should_order_data(inode)) {
Mark Fasheh1fabe142006-10-09 18:11:45 -0700417 ret = walk_page_buffers(handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800418 page_buffers(page),
419 from, to, NULL,
420 ocfs2_journal_dirty_data);
421 if (ret < 0)
422 mlog_errno(ret);
423 }
424out:
425 if (ret) {
426 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700427 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800428 handle = ERR_PTR(ret);
429 }
430 return handle;
431}
432
Mark Fashehccd979b2005-12-15 14:31:24 -0800433static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
434{
435 sector_t status;
436 u64 p_blkno = 0;
437 int err = 0;
438 struct inode *inode = mapping->host;
439
440 mlog_entry("(block = %llu)\n", (unsigned long long)block);
441
442 /* We don't need to lock journal system files, since they aren't
443 * accessed concurrently from multiple nodes.
444 */
445 if (!INODE_JOURNAL(inode)) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700446 err = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800447 if (err) {
448 if (err != -ENOENT)
449 mlog_errno(err);
450 goto bail;
451 }
452 down_read(&OCFS2_I(inode)->ip_alloc_sem);
453 }
454
Mark Fasheh6798d352007-09-07 14:05:51 -0700455 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
456 err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
457 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800458
459 if (!INODE_JOURNAL(inode)) {
460 up_read(&OCFS2_I(inode)->ip_alloc_sem);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700461 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800462 }
463
464 if (err) {
465 mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
466 (unsigned long long)block);
467 mlog_errno(err);
468 goto bail;
469 }
470
Mark Fashehccd979b2005-12-15 14:31:24 -0800471bail:
472 status = err ? 0 : p_blkno;
473
474 mlog_exit((int)status);
475
476 return status;
477}
478
479/*
480 * TODO: Make this into a generic get_blocks function.
481 *
482 * From do_direct_io in direct-io.c:
483 * "So what we do is to permit the ->get_blocks function to populate
484 * bh.b_size with the size of IO which is permitted at this offset and
485 * this i_blkbits."
486 *
487 * This function is called directly from get_more_blocks in direct-io.c.
488 *
489 * called like this: dio->get_blocks(dio->inode, fs_startblk,
490 * fs_count, map_bh, dio->rw == WRITE);
491 */
492static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
Mark Fashehccd979b2005-12-15 14:31:24 -0800493 struct buffer_head *bh_result, int create)
494{
495 int ret;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800496 u64 p_blkno, inode_blocks, contig_blocks;
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800497 unsigned int ext_flags;
Florin Malita184d7d22006-06-03 19:30:10 -0400498 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800499 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800500
Mark Fashehccd979b2005-12-15 14:31:24 -0800501 /* This function won't even be called if the request isn't all
502 * nicely aligned and of the right size, so there's no need
503 * for us to check any of that. */
504
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800505 inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
Mark Fasheh564f8a32006-12-14 13:01:05 -0800506
507 /*
508 * Any write past EOF is not allowed because we'd be extending.
509 */
510 if (create && (iblock + max_blocks) > inode_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800511 ret = -EIO;
512 goto bail;
513 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800514
515 /* This figures out the size of the next contiguous block, and
516 * our logical offset */
Mark Fasheh363041a2007-01-17 12:31:35 -0800517 ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800518 &contig_blocks, &ext_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 if (ret) {
520 mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
521 (unsigned long long)iblock);
522 ret = -EIO;
523 goto bail;
524 }
525
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800526 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
527 ocfs2_error(inode->i_sb,
528 "Inode %llu has a hole at block %llu\n",
529 (unsigned long long)OCFS2_I(inode)->ip_blkno,
530 (unsigned long long)iblock);
531 ret = -EROFS;
532 goto bail;
533 }
534
535 /*
536 * get_more_blocks() expects us to describe a hole by clearing
537 * the mapped bit on bh_result().
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800538 *
539 * Consider an unwritten extent as a hole.
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800540 */
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800541 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800542 map_bh(bh_result, inode->i_sb, p_blkno);
543 else {
544 /*
545 * ocfs2_prepare_inode_for_write() should have caught
546 * the case where we'd be filling a hole and triggered
547 * a buffered write instead.
548 */
549 if (create) {
550 ret = -EIO;
551 mlog_errno(ret);
552 goto bail;
553 }
554
555 clear_buffer_mapped(bh_result);
556 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800557
558 /* make sure we don't map more than max_blocks blocks here as
559 that's all the kernel will handle at this point. */
560 if (max_blocks < contig_blocks)
561 contig_blocks = max_blocks;
562 bh_result->b_size = contig_blocks << blocksize_bits;
563bail:
564 return ret;
565}
566
567/*
568 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
569 * particularly interested in the aio/dio case. Like the core uses
570 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
571 * truncation on another.
572 */
573static void ocfs2_dio_end_io(struct kiocb *iocb,
574 loff_t offset,
575 ssize_t bytes,
576 void *private)
577{
Josef Sipekd28c9172006-12-08 02:37:25 -0800578 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700579 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -0800580
581 /* this io's submitter should not have unlocked this before we could */
582 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700583
Mark Fashehccd979b2005-12-15 14:31:24 -0800584 ocfs2_iocb_clear_rw_locked(iocb);
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700585
586 level = ocfs2_iocb_rw_locked_level(iocb);
587 if (!level)
588 up_read(&inode->i_alloc_sem);
589 ocfs2_rw_unlock(inode, level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800590}
591
Joel Becker03f981c2007-01-04 14:54:41 -0800592/*
593 * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
594 * from ext3. PageChecked() bits have been removed as OCFS2 does not
595 * do journalled data.
596 */
597static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
598{
599 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
600
601 journal_invalidatepage(journal, page, offset);
602}
603
604static int ocfs2_releasepage(struct page *page, gfp_t wait)
605{
606 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
607
608 if (!page_has_buffers(page))
609 return 0;
610 return journal_try_to_free_buffers(journal, page, wait);
611}
612
Mark Fashehccd979b2005-12-15 14:31:24 -0800613static ssize_t ocfs2_direct_IO(int rw,
614 struct kiocb *iocb,
615 const struct iovec *iov,
616 loff_t offset,
617 unsigned long nr_segs)
618{
619 struct file *file = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -0800620 struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
Mark Fashehccd979b2005-12-15 14:31:24 -0800621 int ret;
622
623 mlog_entry_void();
Mark Fasheh53013cb2006-05-05 19:04:03 -0700624
Mark Fasheh6798d352007-09-07 14:05:51 -0700625 /*
626 * Fallback to buffered I/O if we see an inode without
627 * extents.
628 */
629 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
630 return 0;
631
Mark Fashehccd979b2005-12-15 14:31:24 -0800632 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
633 inode->i_sb->s_bdev, iov, offset,
634 nr_segs,
635 ocfs2_direct_IO_get_blocks,
636 ocfs2_dio_end_io);
Mark Fashehc934a922007-10-18 15:23:46 -0700637
Mark Fashehccd979b2005-12-15 14:31:24 -0800638 mlog_exit(ret);
639 return ret;
640}
641
Mark Fasheh9517bac2007-02-09 20:24:12 -0800642static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
643 u32 cpos,
644 unsigned int *start,
645 unsigned int *end)
646{
647 unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
648
649 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
650 unsigned int cpp;
651
652 cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
653
654 cluster_start = cpos % cpp;
655 cluster_start = cluster_start << osb->s_clustersize_bits;
656
657 cluster_end = cluster_start + osb->s_clustersize;
658 }
659
660 BUG_ON(cluster_start > PAGE_SIZE);
661 BUG_ON(cluster_end > PAGE_SIZE);
662
663 if (start)
664 *start = cluster_start;
665 if (end)
666 *end = cluster_end;
667}
668
669/*
670 * 'from' and 'to' are the region in the page to avoid zeroing.
671 *
672 * If pagesize > clustersize, this function will avoid zeroing outside
673 * of the cluster boundary.
674 *
675 * from == to == 0 is code for "zero the entire cluster region"
676 */
677static void ocfs2_clear_page_regions(struct page *page,
678 struct ocfs2_super *osb, u32 cpos,
679 unsigned from, unsigned to)
680{
681 void *kaddr;
682 unsigned int cluster_start, cluster_end;
683
684 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
685
686 kaddr = kmap_atomic(page, KM_USER0);
687
688 if (from || to) {
689 if (from > cluster_start)
690 memset(kaddr + cluster_start, 0, from - cluster_start);
691 if (to < cluster_end)
692 memset(kaddr + to, 0, cluster_end - to);
693 } else {
694 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
695 }
696
697 kunmap_atomic(kaddr, KM_USER0);
698}
699
700/*
Mark Fasheh4e9563f2007-11-01 11:37:48 -0700701 * Nonsparse file systems fully allocate before we get to the write
702 * code. This prevents ocfs2_write() from tagging the write as an
703 * allocating one, which means ocfs2_map_page_blocks() might try to
704 * read-in the blocks at the tail of our file. Avoid reading them by
705 * testing i_size against each block offset.
706 */
707static int ocfs2_should_read_blk(struct inode *inode, struct page *page,
708 unsigned int block_start)
709{
710 u64 offset = page_offset(page) + block_start;
711
712 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
713 return 1;
714
715 if (i_size_read(inode) > offset)
716 return 1;
717
718 return 0;
719}
720
721/*
Mark Fasheh9517bac2007-02-09 20:24:12 -0800722 * Some of this taken from block_prepare_write(). We already have our
723 * mapping by now though, and the entire write will be allocating or
724 * it won't, so not much need to use BH_New.
725 *
726 * This will also skip zeroing, which is handled externally.
727 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800728int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
729 struct inode *inode, unsigned int from,
730 unsigned int to, int new)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800731{
732 int ret = 0;
733 struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
734 unsigned int block_end, block_start;
735 unsigned int bsize = 1 << inode->i_blkbits;
736
737 if (!page_has_buffers(page))
738 create_empty_buffers(page, bsize, 0);
739
740 head = page_buffers(page);
741 for (bh = head, block_start = 0; bh != head || !block_start;
742 bh = bh->b_this_page, block_start += bsize) {
743 block_end = block_start + bsize;
744
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700745 clear_buffer_new(bh);
746
Mark Fasheh9517bac2007-02-09 20:24:12 -0800747 /*
748 * Ignore blocks outside of our i/o range -
749 * they may belong to unallocated clusters.
750 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800751 if (block_start >= to || block_end <= from) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800752 if (PageUptodate(page))
753 set_buffer_uptodate(bh);
754 continue;
755 }
756
757 /*
758 * For an allocating write with cluster size >= page
759 * size, we always write the entire page.
760 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700761 if (new)
762 set_buffer_new(bh);
Mark Fasheh9517bac2007-02-09 20:24:12 -0800763
764 if (!buffer_mapped(bh)) {
765 map_bh(bh, inode->i_sb, *p_blkno);
766 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
767 }
768
769 if (PageUptodate(page)) {
770 if (!buffer_uptodate(bh))
771 set_buffer_uptodate(bh);
772 } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
Mark Fashehbce99762007-06-18 11:12:36 -0700773 !buffer_new(bh) &&
Mark Fasheh4e9563f2007-11-01 11:37:48 -0700774 ocfs2_should_read_blk(inode, page, block_start) &&
Mark Fashehbce99762007-06-18 11:12:36 -0700775 (block_start < from || block_end > to)) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800776 ll_rw_block(READ, 1, &bh);
777 *wait_bh++=bh;
778 }
779
780 *p_blkno = *p_blkno + 1;
781 }
782
783 /*
784 * If we issued read requests - let them complete.
785 */
786 while(wait_bh > wait) {
787 wait_on_buffer(*--wait_bh);
788 if (!buffer_uptodate(*wait_bh))
789 ret = -EIO;
790 }
791
792 if (ret == 0 || !new)
793 return ret;
794
795 /*
796 * If we get -EIO above, zero out any newly allocated blocks
797 * to avoid exposing stale data.
798 */
799 bh = head;
800 block_start = 0;
801 do {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800802 block_end = block_start + bsize;
803 if (block_end <= from)
804 goto next_bh;
805 if (block_start >= to)
806 break;
807
Eric Sandeen54c57dc2007-06-20 17:15:10 -0700808 zero_user_page(page, block_start, bh->b_size, KM_USER0);
Mark Fasheh9517bac2007-02-09 20:24:12 -0800809 set_buffer_uptodate(bh);
810 mark_buffer_dirty(bh);
811
812next_bh:
813 block_start = block_end;
814 bh = bh->b_this_page;
815 } while (bh != head);
816
817 return ret;
818}
819
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700820#if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
821#define OCFS2_MAX_CTXT_PAGES 1
822#else
823#define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
824#endif
Mark Fasheh6af67d82007-03-06 17:24:46 -0800825
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700826#define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
Mark Fasheh6af67d82007-03-06 17:24:46 -0800827
828/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700829 * Describe the state of a single cluster to be written to.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800830 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700831struct ocfs2_write_cluster_desc {
832 u32 c_cpos;
833 u32 c_phys;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800834 /*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700835 * Give this a unique field because c_phys eventually gets
836 * filled.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800837 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700838 unsigned c_new;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700839 unsigned c_unwritten;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700840};
Mark Fasheh9517bac2007-02-09 20:24:12 -0800841
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700842static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
843{
844 return d->c_new || d->c_unwritten;
845}
846
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700847struct ocfs2_write_ctxt {
848 /* Logical cluster position / len of write */
849 u32 w_cpos;
850 u32 w_clen;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800851
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700852 struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
Mark Fasheh9517bac2007-02-09 20:24:12 -0800853
854 /*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700855 * This is true if page_size > cluster_size.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800856 *
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700857 * It triggers a set of special cases during write which might
858 * have to deal with allocating writes to partial pages.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800859 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700860 unsigned int w_large_pages;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800861
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700862 /*
863 * Pages involved in this write.
864 *
865 * w_target_page is the page being written to by the user.
866 *
867 * w_pages is an array of pages which always contains
868 * w_target_page, and in the case of an allocating write with
869 * page_size < cluster size, it will contain zero'd and mapped
870 * pages adjacent to w_target_page which need to be written
871 * out in so that future reads from that region will get
872 * zero's.
873 */
874 struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
875 unsigned int w_num_pages;
876 struct page *w_target_page;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800877
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700878 /*
879 * ocfs2_write_end() uses this to know what the real range to
880 * write in the target should be.
881 */
882 unsigned int w_target_from;
883 unsigned int w_target_to;
884
885 /*
886 * We could use journal_current_handle() but this is cleaner,
887 * IMHO -Mark
888 */
889 handle_t *w_handle;
890
891 struct buffer_head *w_di_bh;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700892
893 struct ocfs2_cached_dealloc_ctxt w_dealloc;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700894};
895
Mark Fasheh1d410a62007-09-07 14:20:45 -0700896void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700897{
898 int i;
899
Mark Fasheh1d410a62007-09-07 14:20:45 -0700900 for(i = 0; i < num_pages; i++) {
901 if (pages[i]) {
902 unlock_page(pages[i]);
903 mark_page_accessed(pages[i]);
904 page_cache_release(pages[i]);
905 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700906 }
Mark Fasheh1d410a62007-09-07 14:20:45 -0700907}
908
909static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
910{
911 ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700912
913 brelse(wc->w_di_bh);
914 kfree(wc);
915}
916
917static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
918 struct ocfs2_super *osb, loff_t pos,
Mark Fasheh607d44a2007-05-09 15:14:45 -0700919 unsigned len, struct buffer_head *di_bh)
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700920{
tao.ma@oracle.com30b85482007-09-06 08:02:25 +0800921 u32 cend;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700922 struct ocfs2_write_ctxt *wc;
923
924 wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
925 if (!wc)
926 return -ENOMEM;
927
928 wc->w_cpos = pos >> osb->s_clustersize_bits;
tao.ma@oracle.com30b85482007-09-06 08:02:25 +0800929 cend = (pos + len - 1) >> osb->s_clustersize_bits;
930 wc->w_clen = cend - wc->w_cpos + 1;
Mark Fasheh607d44a2007-05-09 15:14:45 -0700931 get_bh(di_bh);
932 wc->w_di_bh = di_bh;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700933
934 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
935 wc->w_large_pages = 1;
936 else
937 wc->w_large_pages = 0;
938
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700939 ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
940
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700941 *wcp = wc;
942
943 return 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800944}
945
946/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700947 * If a page has any new buffers, zero them out here, and mark them uptodate
948 * and dirty so they'll be written out (in order to prevent uninitialised
949 * block data from leaking). And clear the new bit.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800950 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700951static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800952{
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700953 unsigned int block_start, block_end;
954 struct buffer_head *head, *bh;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800955
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700956 BUG_ON(!PageLocked(page));
957 if (!page_has_buffers(page))
958 return;
959
960 bh = head = page_buffers(page);
961 block_start = 0;
962 do {
963 block_end = block_start + bh->b_size;
964
965 if (buffer_new(bh)) {
966 if (block_end > from && block_start < to) {
967 if (!PageUptodate(page)) {
968 unsigned start, end;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700969
970 start = max(from, block_start);
971 end = min(to, block_end);
972
Eric Sandeen54c57dc2007-06-20 17:15:10 -0700973 zero_user_page(page, start, end - start, KM_USER0);
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700974 set_buffer_uptodate(bh);
975 }
976
977 clear_buffer_new(bh);
978 mark_buffer_dirty(bh);
979 }
980 }
981
982 block_start = block_end;
983 bh = bh->b_this_page;
984 } while (bh != head);
985}
986
987/*
988 * Only called when we have a failure during allocating write to write
989 * zero's to the newly allocated region.
990 */
991static void ocfs2_write_failure(struct inode *inode,
992 struct ocfs2_write_ctxt *wc,
993 loff_t user_pos, unsigned user_len)
994{
995 int i;
Mark Fasheh5c26a7b2007-09-18 17:49:29 -0700996 unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
997 to = user_pos + user_len;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700998 struct page *tmppage;
999
Mark Fasheh5c26a7b2007-09-18 17:49:29 -07001000 ocfs2_zero_new_buffers(wc->w_target_page, from, to);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001001
1002 for(i = 0; i < wc->w_num_pages; i++) {
1003 tmppage = wc->w_pages[i];
1004
1005 if (ocfs2_should_order_data(inode))
1006 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
1007 from, to, NULL,
1008 ocfs2_journal_dirty_data);
1009
1010 block_commit_write(tmppage, from, to);
1011 }
1012}
1013
1014static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
1015 struct ocfs2_write_ctxt *wc,
1016 struct page *page, u32 cpos,
1017 loff_t user_pos, unsigned user_len,
1018 int new)
1019{
1020 int ret;
1021 unsigned int map_from = 0, map_to = 0;
1022 unsigned int cluster_start, cluster_end;
1023 unsigned int user_data_from = 0, user_data_to = 0;
1024
1025 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001026 &cluster_start, &cluster_end);
1027
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001028 if (page == wc->w_target_page) {
1029 map_from = user_pos & (PAGE_CACHE_SIZE - 1);
1030 map_to = map_from + user_len;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001031
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001032 if (new)
1033 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
1034 cluster_start, cluster_end,
1035 new);
1036 else
1037 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
1038 map_from, map_to, new);
1039 if (ret) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001040 mlog_errno(ret);
1041 goto out;
1042 }
1043
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001044 user_data_from = map_from;
1045 user_data_to = map_to;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001046 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001047 map_from = cluster_start;
1048 map_to = cluster_end;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001049 }
1050 } else {
1051 /*
1052 * If we haven't allocated the new page yet, we
1053 * shouldn't be writing it out without copying user
1054 * data. This is likely a math error from the caller.
1055 */
1056 BUG_ON(!new);
1057
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001058 map_from = cluster_start;
1059 map_to = cluster_end;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001060
1061 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001062 cluster_start, cluster_end, new);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001063 if (ret) {
1064 mlog_errno(ret);
1065 goto out;
1066 }
1067 }
1068
1069 /*
1070 * Parts of newly allocated pages need to be zero'd.
1071 *
1072 * Above, we have also rewritten 'to' and 'from' - as far as
1073 * the rest of the function is concerned, the entire cluster
1074 * range inside of a page needs to be written.
1075 *
1076 * We can skip this if the page is up to date - it's already
1077 * been zero'd from being read in as a hole.
1078 */
1079 if (new && !PageUptodate(page))
1080 ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001081 cpos, user_data_from, user_data_to);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001082
1083 flush_dcache_page(page);
1084
Mark Fasheh9517bac2007-02-09 20:24:12 -08001085out:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001086 return ret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001087}
1088
1089/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001090 * This function will only grab one clusters worth of pages.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001091 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001092static int ocfs2_grab_pages_for_write(struct address_space *mapping,
1093 struct ocfs2_write_ctxt *wc,
Mark Fasheh7307de82007-05-09 15:16:19 -07001094 u32 cpos, loff_t user_pos, int new,
1095 struct page *mmap_page)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001096{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001097 int ret = 0, i;
1098 unsigned long start, target_index, index;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001099 struct inode *inode = mapping->host;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001100
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001101 target_index = user_pos >> PAGE_CACHE_SHIFT;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001102
1103 /*
1104 * Figure out how many pages we'll be manipulating here. For
Mark Fasheh60b11392007-02-16 11:46:50 -08001105 * non allocating write, we just change the one
1106 * page. Otherwise, we'll need a whole clusters worth.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001107 */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001108 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001109 wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
1110 start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001111 } else {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001112 wc->w_num_pages = 1;
1113 start = target_index;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001114 }
1115
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001116 for(i = 0; i < wc->w_num_pages; i++) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001117 index = start + i;
1118
Mark Fasheh7307de82007-05-09 15:16:19 -07001119 if (index == target_index && mmap_page) {
1120 /*
1121 * ocfs2_pagemkwrite() is a little different
1122 * and wants us to directly use the page
1123 * passed in.
1124 */
1125 lock_page(mmap_page);
1126
1127 if (mmap_page->mapping != mapping) {
1128 unlock_page(mmap_page);
1129 /*
1130 * Sanity check - the locking in
1131 * ocfs2_pagemkwrite() should ensure
1132 * that this code doesn't trigger.
1133 */
1134 ret = -EINVAL;
1135 mlog_errno(ret);
1136 goto out;
1137 }
1138
1139 page_cache_get(mmap_page);
1140 wc->w_pages[i] = mmap_page;
1141 } else {
1142 wc->w_pages[i] = find_or_create_page(mapping, index,
1143 GFP_NOFS);
1144 if (!wc->w_pages[i]) {
1145 ret = -ENOMEM;
1146 mlog_errno(ret);
1147 goto out;
1148 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001149 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001150
1151 if (index == target_index)
1152 wc->w_target_page = wc->w_pages[i];
Mark Fasheh9517bac2007-02-09 20:24:12 -08001153 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001154out:
1155 return ret;
1156}
1157
1158/*
1159 * Prepare a single cluster for write one cluster into the file.
1160 */
1161static int ocfs2_write_cluster(struct address_space *mapping,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001162 u32 phys, unsigned int unwritten,
1163 struct ocfs2_alloc_context *data_ac,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001164 struct ocfs2_alloc_context *meta_ac,
1165 struct ocfs2_write_ctxt *wc, u32 cpos,
1166 loff_t user_pos, unsigned user_len)
1167{
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001168 int ret, i, new, should_zero = 0;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001169 u64 v_blkno, p_blkno;
1170 struct inode *inode = mapping->host;
1171
1172 new = phys == 0 ? 1 : 0;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001173 if (new || unwritten)
1174 should_zero = 1;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001175
1176 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001177 u32 tmp_pos;
1178
Mark Fasheh9517bac2007-02-09 20:24:12 -08001179 /*
1180 * This is safe to call with the page locks - it won't take
1181 * any additional semaphores or cluster locks.
1182 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001183 tmp_pos = cpos;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001184 ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001185 &tmp_pos, 1, 0, wc->w_di_bh,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001186 wc->w_handle, data_ac,
1187 meta_ac, NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001188 /*
1189 * This shouldn't happen because we must have already
1190 * calculated the correct meta data allocation required. The
1191 * internal tree allocation code should know how to increase
1192 * transaction credits itself.
1193 *
1194 * If need be, we could handle -EAGAIN for a
1195 * RESTART_TRANS here.
1196 */
1197 mlog_bug_on_msg(ret == -EAGAIN,
1198 "Inode %llu: EAGAIN return during allocation.\n",
1199 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1200 if (ret < 0) {
1201 mlog_errno(ret);
1202 goto out;
1203 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001204 } else if (unwritten) {
1205 ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
1206 wc->w_handle, cpos, 1, phys,
1207 meta_ac, &wc->w_dealloc);
1208 if (ret < 0) {
1209 mlog_errno(ret);
1210 goto out;
1211 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001212 }
1213
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001214 if (should_zero)
1215 v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
1216 else
1217 v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
1218
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001219 /*
1220 * The only reason this should fail is due to an inability to
1221 * find the extent added.
1222 */
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001223 ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
1224 NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001225 if (ret < 0) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001226 ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
1227 "at logical block %llu",
1228 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1229 (unsigned long long)v_blkno);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001230 goto out;
1231 }
1232
1233 BUG_ON(p_blkno == 0);
1234
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001235 for(i = 0; i < wc->w_num_pages; i++) {
1236 int tmpret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001237
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001238 tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
1239 wc->w_pages[i], cpos,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001240 user_pos, user_len,
1241 should_zero);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001242 if (tmpret) {
1243 mlog_errno(tmpret);
1244 if (ret == 0)
1245 tmpret = ret;
1246 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001247 }
1248
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001249 /*
1250 * We only have cleanup to do in case of allocating write.
1251 */
1252 if (ret && new)
1253 ocfs2_write_failure(inode, wc, user_pos, user_len);
1254
Mark Fasheh9517bac2007-02-09 20:24:12 -08001255out:
Mark Fasheh9517bac2007-02-09 20:24:12 -08001256
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001257 return ret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001258}
1259
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001260static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
1261 struct ocfs2_alloc_context *data_ac,
1262 struct ocfs2_alloc_context *meta_ac,
1263 struct ocfs2_write_ctxt *wc,
1264 loff_t pos, unsigned len)
1265{
1266 int ret, i;
Mark Fashehdb562462007-09-17 09:06:29 -07001267 loff_t cluster_off;
1268 unsigned int local_len = len;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001269 struct ocfs2_write_cluster_desc *desc;
Mark Fashehdb562462007-09-17 09:06:29 -07001270 struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001271
1272 for (i = 0; i < wc->w_clen; i++) {
1273 desc = &wc->w_desc[i];
1274
Mark Fashehdb562462007-09-17 09:06:29 -07001275 /*
1276 * We have to make sure that the total write passed in
1277 * doesn't extend past a single cluster.
1278 */
1279 local_len = len;
1280 cluster_off = pos & (osb->s_clustersize - 1);
1281 if ((cluster_off + local_len) > osb->s_clustersize)
1282 local_len = osb->s_clustersize - cluster_off;
1283
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001284 ret = ocfs2_write_cluster(mapping, desc->c_phys,
1285 desc->c_unwritten, data_ac, meta_ac,
Mark Fashehdb562462007-09-17 09:06:29 -07001286 wc, desc->c_cpos, pos, local_len);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001287 if (ret) {
1288 mlog_errno(ret);
1289 goto out;
1290 }
Mark Fashehdb562462007-09-17 09:06:29 -07001291
1292 len -= local_len;
1293 pos += local_len;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001294 }
1295
1296 ret = 0;
1297out:
1298 return ret;
1299}
1300
Mark Fasheh9517bac2007-02-09 20:24:12 -08001301/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001302 * ocfs2_write_end() wants to know which parts of the target page it
1303 * should complete the write on. It's easiest to compute them ahead of
1304 * time when a more complete view of the write is available.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001305 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001306static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
1307 struct ocfs2_write_ctxt *wc,
1308 loff_t pos, unsigned len, int alloc)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001309{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001310 struct ocfs2_write_cluster_desc *desc;
1311
1312 wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
1313 wc->w_target_to = wc->w_target_from + len;
1314
1315 if (alloc == 0)
1316 return;
1317
1318 /*
1319 * Allocating write - we may have different boundaries based
1320 * on page size and cluster size.
1321 *
1322 * NOTE: We can no longer compute one value from the other as
1323 * the actual write length and user provided length may be
1324 * different.
1325 */
1326
1327 if (wc->w_large_pages) {
1328 /*
1329 * We only care about the 1st and last cluster within
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001330 * our range and whether they should be zero'd or not. Either
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001331 * value may be extended out to the start/end of a
1332 * newly allocated cluster.
1333 */
1334 desc = &wc->w_desc[0];
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001335 if (ocfs2_should_zero_cluster(desc))
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001336 ocfs2_figure_cluster_boundaries(osb,
1337 desc->c_cpos,
1338 &wc->w_target_from,
1339 NULL);
1340
1341 desc = &wc->w_desc[wc->w_clen - 1];
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001342 if (ocfs2_should_zero_cluster(desc))
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001343 ocfs2_figure_cluster_boundaries(osb,
1344 desc->c_cpos,
1345 NULL,
1346 &wc->w_target_to);
1347 } else {
1348 wc->w_target_from = 0;
1349 wc->w_target_to = PAGE_CACHE_SIZE;
1350 }
1351}
1352
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001353/*
1354 * Populate each single-cluster write descriptor in the write context
1355 * with information about the i/o to be done.
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001356 *
1357 * Returns the number of clusters that will have to be allocated, as
1358 * well as a worst case estimate of the number of extent records that
1359 * would have to be created during a write to an unwritten region.
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001360 */
1361static int ocfs2_populate_write_desc(struct inode *inode,
1362 struct ocfs2_write_ctxt *wc,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001363 unsigned int *clusters_to_alloc,
1364 unsigned int *extents_to_split)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001365{
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001366 int ret;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001367 struct ocfs2_write_cluster_desc *desc;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001368 unsigned int num_clusters = 0;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001369 unsigned int ext_flags = 0;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001370 u32 phys = 0;
1371 int i;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001372
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001373 *clusters_to_alloc = 0;
1374 *extents_to_split = 0;
1375
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001376 for (i = 0; i < wc->w_clen; i++) {
1377 desc = &wc->w_desc[i];
1378 desc->c_cpos = wc->w_cpos + i;
1379
1380 if (num_clusters == 0) {
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001381 /*
1382 * Need to look up the next extent record.
1383 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001384 ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001385 &num_clusters, &ext_flags);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001386 if (ret) {
1387 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001388 goto out;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001389 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001390
1391 /*
1392 * Assume worst case - that we're writing in
1393 * the middle of the extent.
1394 *
1395 * We can assume that the write proceeds from
1396 * left to right, in which case the extent
1397 * insert code is smart enough to coalesce the
1398 * next splits into the previous records created.
1399 */
1400 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1401 *extents_to_split = *extents_to_split + 2;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001402 } else if (phys) {
1403 /*
1404 * Only increment phys if it doesn't describe
1405 * a hole.
1406 */
1407 phys++;
1408 }
1409
1410 desc->c_phys = phys;
1411 if (phys == 0) {
1412 desc->c_new = 1;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001413 *clusters_to_alloc = *clusters_to_alloc + 1;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001414 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001415 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1416 desc->c_unwritten = 1;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001417
1418 num_clusters--;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001419 }
1420
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001421 ret = 0;
1422out:
1423 return ret;
1424}
1425
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001426static int ocfs2_write_begin_inline(struct address_space *mapping,
1427 struct inode *inode,
1428 struct ocfs2_write_ctxt *wc)
1429{
1430 int ret;
1431 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1432 struct page *page;
1433 handle_t *handle;
1434 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1435
1436 page = find_or_create_page(mapping, 0, GFP_NOFS);
1437 if (!page) {
1438 ret = -ENOMEM;
1439 mlog_errno(ret);
1440 goto out;
1441 }
1442 /*
1443 * If we don't set w_num_pages then this page won't get unlocked
1444 * and freed on cleanup of the write context.
1445 */
1446 wc->w_pages[0] = wc->w_target_page = page;
1447 wc->w_num_pages = 1;
1448
1449 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1450 if (IS_ERR(handle)) {
1451 ret = PTR_ERR(handle);
1452 mlog_errno(ret);
1453 goto out;
1454 }
1455
1456 ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
1457 OCFS2_JOURNAL_ACCESS_WRITE);
1458 if (ret) {
1459 ocfs2_commit_trans(osb, handle);
1460
1461 mlog_errno(ret);
1462 goto out;
1463 }
1464
1465 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
1466 ocfs2_set_inode_data_inline(inode, di);
1467
1468 if (!PageUptodate(page)) {
1469 ret = ocfs2_read_inline_data(inode, page, wc->w_di_bh);
1470 if (ret) {
1471 ocfs2_commit_trans(osb, handle);
1472
1473 goto out;
1474 }
1475 }
1476
1477 wc->w_handle = handle;
1478out:
1479 return ret;
1480}
1481
1482int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size)
1483{
1484 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1485
Mark Fasheh0d8a4e02007-11-20 11:48:41 -08001486 if (new_size <= le16_to_cpu(di->id2.i_data.id_count))
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001487 return 1;
1488 return 0;
1489}
1490
1491static int ocfs2_try_to_write_inline_data(struct address_space *mapping,
1492 struct inode *inode, loff_t pos,
1493 unsigned len, struct page *mmap_page,
1494 struct ocfs2_write_ctxt *wc)
1495{
1496 int ret, written = 0;
1497 loff_t end = pos + len;
1498 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1499
1500 mlog(0, "Inode %llu, write of %u bytes at off %llu. features: 0x%x\n",
1501 (unsigned long long)oi->ip_blkno, len, (unsigned long long)pos,
1502 oi->ip_dyn_features);
1503
1504 /*
1505 * Handle inodes which already have inline data 1st.
1506 */
1507 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1508 if (mmap_page == NULL &&
1509 ocfs2_size_fits_inline_data(wc->w_di_bh, end))
1510 goto do_inline_write;
1511
1512 /*
1513 * The write won't fit - we have to give this inode an
1514 * inline extent list now.
1515 */
1516 ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh);
1517 if (ret)
1518 mlog_errno(ret);
1519 goto out;
1520 }
1521
1522 /*
1523 * Check whether the inode can accept inline data.
1524 */
1525 if (oi->ip_clusters != 0 || i_size_read(inode) != 0)
1526 return 0;
1527
1528 /*
1529 * Check whether the write can fit.
1530 */
1531 if (mmap_page || end > ocfs2_max_inline_data(inode->i_sb))
1532 return 0;
1533
1534do_inline_write:
1535 ret = ocfs2_write_begin_inline(mapping, inode, wc);
1536 if (ret) {
1537 mlog_errno(ret);
1538 goto out;
1539 }
1540
1541 /*
1542 * This signals to the caller that the data can be written
1543 * inline.
1544 */
1545 written = 1;
1546out:
1547 return written ? written : ret;
1548}
1549
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001550/*
1551 * This function only does anything for file systems which can't
1552 * handle sparse files.
1553 *
1554 * What we want to do here is fill in any hole between the current end
1555 * of allocation and the end of our write. That way the rest of the
1556 * write path can treat it as an non-allocating write, which has no
1557 * special case code for sparse/nonsparse files.
1558 */
1559static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos,
1560 unsigned len,
1561 struct ocfs2_write_ctxt *wc)
1562{
1563 int ret;
1564 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1565 loff_t newsize = pos + len;
1566
1567 if (ocfs2_sparse_alloc(osb))
1568 return 0;
1569
1570 if (newsize <= i_size_read(inode))
1571 return 0;
1572
1573 ret = ocfs2_extend_no_holes(inode, newsize, newsize - len);
1574 if (ret)
1575 mlog_errno(ret);
1576
1577 return ret;
1578}
1579
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001580int ocfs2_write_begin_nolock(struct address_space *mapping,
1581 loff_t pos, unsigned len, unsigned flags,
1582 struct page **pagep, void **fsdata,
1583 struct buffer_head *di_bh, struct page *mmap_page)
1584{
1585 int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001586 unsigned int clusters_to_alloc, extents_to_split;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001587 struct ocfs2_write_ctxt *wc;
1588 struct inode *inode = mapping->host;
1589 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1590 struct ocfs2_dinode *di;
1591 struct ocfs2_alloc_context *data_ac = NULL;
1592 struct ocfs2_alloc_context *meta_ac = NULL;
1593 handle_t *handle;
1594
1595 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
1596 if (ret) {
1597 mlog_errno(ret);
1598 return ret;
1599 }
1600
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001601 if (ocfs2_supports_inline_data(osb)) {
1602 ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
1603 mmap_page, wc);
1604 if (ret == 1) {
1605 ret = 0;
1606 goto success;
1607 }
1608 if (ret < 0) {
1609 mlog_errno(ret);
1610 goto out;
1611 }
1612 }
1613
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001614 ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc);
1615 if (ret) {
1616 mlog_errno(ret);
1617 goto out;
1618 }
1619
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001620 ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
1621 &extents_to_split);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001622 if (ret) {
1623 mlog_errno(ret);
1624 goto out;
1625 }
1626
1627 di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1628
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001629 /*
1630 * We set w_target_from, w_target_to here so that
1631 * ocfs2_write_end() knows which range in the target page to
1632 * write out. An allocation requires that we write the entire
1633 * cluster range.
1634 */
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001635 if (clusters_to_alloc || extents_to_split) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001636 /*
1637 * XXX: We are stretching the limits of
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001638 * ocfs2_lock_allocators(). It greatly over-estimates
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001639 * the work to be done.
1640 */
1641 ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001642 extents_to_split, &data_ac, &meta_ac);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001643 if (ret) {
1644 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001645 goto out;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001646 }
1647
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001648 credits = ocfs2_calc_extend_credits(inode->i_sb, di,
1649 clusters_to_alloc);
1650
Mark Fasheh9517bac2007-02-09 20:24:12 -08001651 }
1652
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001653 ocfs2_set_target_boundaries(osb, wc, pos, len,
1654 clusters_to_alloc + extents_to_split);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001655
Mark Fasheh9517bac2007-02-09 20:24:12 -08001656 handle = ocfs2_start_trans(osb, credits);
1657 if (IS_ERR(handle)) {
1658 ret = PTR_ERR(handle);
1659 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001660 goto out;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001661 }
1662
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001663 wc->w_handle = handle;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001664
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001665 /*
1666 * We don't want this to fail in ocfs2_write_end(), so do it
1667 * here.
1668 */
1669 ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001670 OCFS2_JOURNAL_ACCESS_WRITE);
1671 if (ret) {
1672 mlog_errno(ret);
1673 goto out_commit;
1674 }
1675
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001676 /*
1677 * Fill our page array first. That way we've grabbed enough so
1678 * that we can zero and flush if we error after adding the
1679 * extent.
1680 */
1681 ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001682 clusters_to_alloc + extents_to_split,
1683 mmap_page);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001684 if (ret) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001685 mlog_errno(ret);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001686 goto out_commit;
1687 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001688
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001689 ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
1690 len);
1691 if (ret) {
1692 mlog_errno(ret);
1693 goto out_commit;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001694 }
1695
1696 if (data_ac)
1697 ocfs2_free_alloc_context(data_ac);
1698 if (meta_ac)
1699 ocfs2_free_alloc_context(meta_ac);
1700
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001701success:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001702 *pagep = wc->w_target_page;
1703 *fsdata = wc;
1704 return 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001705out_commit:
1706 ocfs2_commit_trans(osb, handle);
1707
Mark Fasheh9517bac2007-02-09 20:24:12 -08001708out:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001709 ocfs2_free_write_ctxt(wc);
1710
Mark Fasheh9517bac2007-02-09 20:24:12 -08001711 if (data_ac)
1712 ocfs2_free_alloc_context(data_ac);
1713 if (meta_ac)
1714 ocfs2_free_alloc_context(meta_ac);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001715 return ret;
1716}
Mark Fasheh9517bac2007-02-09 20:24:12 -08001717
Nick Pigginb6af1bc2007-10-16 01:25:24 -07001718static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1719 loff_t pos, unsigned len, unsigned flags,
1720 struct page **pagep, void **fsdata)
Mark Fasheh607d44a2007-05-09 15:14:45 -07001721{
1722 int ret;
1723 struct buffer_head *di_bh = NULL;
1724 struct inode *inode = mapping->host;
1725
Mark Fashehe63aecb62007-10-18 15:30:42 -07001726 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001727 if (ret) {
1728 mlog_errno(ret);
1729 return ret;
1730 }
1731
1732 /*
1733 * Take alloc sem here to prevent concurrent lookups. That way
1734 * the mapping, zeroing and tree manipulation within
1735 * ocfs2_write() will be safe against ->readpage(). This
1736 * should also serve to lock out allocation from a shared
1737 * writeable region.
1738 */
1739 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1740
Mark Fasheh607d44a2007-05-09 15:14:45 -07001741 ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
Mark Fasheh7307de82007-05-09 15:16:19 -07001742 fsdata, di_bh, NULL);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001743 if (ret) {
1744 mlog_errno(ret);
Mark Fashehc934a922007-10-18 15:23:46 -07001745 goto out_fail;
Mark Fasheh607d44a2007-05-09 15:14:45 -07001746 }
1747
1748 brelse(di_bh);
1749
1750 return 0;
1751
Mark Fasheh607d44a2007-05-09 15:14:45 -07001752out_fail:
1753 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1754
1755 brelse(di_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001756 ocfs2_inode_unlock(inode, 1);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001757
1758 return ret;
1759}
1760
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001761static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
1762 unsigned len, unsigned *copied,
1763 struct ocfs2_dinode *di,
1764 struct ocfs2_write_ctxt *wc)
1765{
1766 void *kaddr;
1767
1768 if (unlikely(*copied < len)) {
1769 if (!PageUptodate(wc->w_target_page)) {
1770 *copied = 0;
1771 return;
1772 }
1773 }
1774
1775 kaddr = kmap_atomic(wc->w_target_page, KM_USER0);
1776 memcpy(di->id2.i_data.id_data + pos, kaddr + pos, *copied);
1777 kunmap_atomic(kaddr, KM_USER0);
1778
1779 mlog(0, "Data written to inode at offset %llu. "
1780 "id_count = %u, copied = %u, i_dyn_features = 0x%x\n",
1781 (unsigned long long)pos, *copied,
1782 le16_to_cpu(di->id2.i_data.id_count),
1783 le16_to_cpu(di->i_dyn_features));
1784}
1785
Mark Fasheh7307de82007-05-09 15:16:19 -07001786int ocfs2_write_end_nolock(struct address_space *mapping,
1787 loff_t pos, unsigned len, unsigned copied,
1788 struct page *page, void *fsdata)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001789{
1790 int i;
1791 unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
1792 struct inode *inode = mapping->host;
1793 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1794 struct ocfs2_write_ctxt *wc = fsdata;
1795 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1796 handle_t *handle = wc->w_handle;
1797 struct page *tmppage;
1798
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001799 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1800 ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
1801 goto out_write_size;
1802 }
1803
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001804 if (unlikely(copied < len)) {
1805 if (!PageUptodate(wc->w_target_page))
1806 copied = 0;
1807
1808 ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
1809 start+len);
1810 }
1811 flush_dcache_page(wc->w_target_page);
1812
1813 for(i = 0; i < wc->w_num_pages; i++) {
1814 tmppage = wc->w_pages[i];
1815
1816 if (tmppage == wc->w_target_page) {
1817 from = wc->w_target_from;
1818 to = wc->w_target_to;
1819
1820 BUG_ON(from > PAGE_CACHE_SIZE ||
1821 to > PAGE_CACHE_SIZE ||
1822 to < from);
1823 } else {
1824 /*
1825 * Pages adjacent to the target (if any) imply
1826 * a hole-filling write in which case we want
1827 * to flush their entire range.
1828 */
1829 from = 0;
1830 to = PAGE_CACHE_SIZE;
1831 }
1832
1833 if (ocfs2_should_order_data(inode))
1834 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
1835 from, to, NULL,
1836 ocfs2_journal_dirty_data);
1837
1838 block_commit_write(tmppage, from, to);
1839 }
1840
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001841out_write_size:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001842 pos += copied;
1843 if (pos > inode->i_size) {
1844 i_size_write(inode, pos);
1845 mark_inode_dirty(inode);
1846 }
1847 inode->i_blocks = ocfs2_inode_sector_count(inode);
1848 di->i_size = cpu_to_le64((u64)i_size_read(inode));
1849 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1850 di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
1851 di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001852 ocfs2_journal_dirty(handle, wc->w_di_bh);
1853
1854 ocfs2_commit_trans(osb, handle);
Mark Fasheh59a5e412007-06-22 15:52:36 -07001855
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001856 ocfs2_run_deallocs(osb, &wc->w_dealloc);
1857
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001858 ocfs2_free_write_ctxt(wc);
1859
1860 return copied;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001861}
1862
Nick Pigginb6af1bc2007-10-16 01:25:24 -07001863static int ocfs2_write_end(struct file *file, struct address_space *mapping,
1864 loff_t pos, unsigned len, unsigned copied,
1865 struct page *page, void *fsdata)
Mark Fasheh607d44a2007-05-09 15:14:45 -07001866{
1867 int ret;
1868 struct inode *inode = mapping->host;
1869
1870 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
1871
Mark Fasheh607d44a2007-05-09 15:14:45 -07001872 up_write(&OCFS2_I(inode)->ip_alloc_sem);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001873 ocfs2_inode_unlock(inode, 1);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001874
1875 return ret;
1876}
1877
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001878const struct address_space_operations ocfs2_aops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001879 .readpage = ocfs2_readpage,
1880 .writepage = ocfs2_writepage,
Nick Pigginb6af1bc2007-10-16 01:25:24 -07001881 .write_begin = ocfs2_write_begin,
1882 .write_end = ocfs2_write_end,
Mark Fashehccd979b2005-12-15 14:31:24 -08001883 .bmap = ocfs2_bmap,
1884 .sync_page = block_sync_page,
Joel Becker03f981c2007-01-04 14:54:41 -08001885 .direct_IO = ocfs2_direct_IO,
1886 .invalidatepage = ocfs2_invalidatepage,
1887 .releasepage = ocfs2_releasepage,
1888 .migratepage = buffer_migrate_page,
Mark Fashehccd979b2005-12-15 14:31:24 -08001889};