blob: 4ffa715be09cff4f8243418016c0e0a08e2da40c [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 * file.c
5 *
6 * File open, close, extend, truncate
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080026#include <linux/capability.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080027#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31#include <linux/pagemap.h>
32#include <linux/uio.h>
Mark Fashehe2057c52006-10-03 17:53:05 -070033#include <linux/sched.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020034#include <linux/splice.h>
Tiger Yang7f1a37e2006-11-15 15:48:42 +080035#include <linux/mount.h>
Mark Fasheh9517bac2007-02-09 20:24:12 -080036#include <linux/writeback.h>
Mark Fasheh385820a2007-07-19 00:14:38 -070037#include <linux/falloc.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080038
39#define MLOG_MASK_PREFIX ML_INODE
40#include <cluster/masklog.h>
41
42#include "ocfs2.h"
43
44#include "alloc.h"
45#include "aops.h"
46#include "dir.h"
47#include "dlmglue.h"
48#include "extent_map.h"
49#include "file.h"
50#include "sysfile.h"
51#include "inode.h"
Herbert Poetzlca4d1472006-07-03 17:27:12 -070052#include "ioctl.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053#include "journal.h"
54#include "mmap.h"
55#include "suballoc.h"
56#include "super.h"
57
58#include "buffer_head_io.h"
59
60static int ocfs2_sync_inode(struct inode *inode)
61{
62 filemap_fdatawrite(inode->i_mapping);
63 return sync_mapping_buffers(inode->i_mapping);
64}
65
66static int ocfs2_file_open(struct inode *inode, struct file *file)
67{
68 int status;
69 int mode = file->f_flags;
70 struct ocfs2_inode_info *oi = OCFS2_I(inode);
71
72 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
Josef Sipekd28c9172006-12-08 02:37:25 -080073 file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -080074
75 spin_lock(&oi->ip_lock);
76
77 /* Check that the inode hasn't been wiped from disk by another
78 * node. If it hasn't then we're safe as long as we hold the
79 * spin lock until our increment of open count. */
80 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
81 spin_unlock(&oi->ip_lock);
82
83 status = -ENOENT;
84 goto leave;
85 }
86
87 if (mode & O_DIRECT)
88 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
89
90 oi->ip_open_count++;
91 spin_unlock(&oi->ip_lock);
92 status = 0;
93leave:
94 mlog_exit(status);
95 return status;
96}
97
98static int ocfs2_file_release(struct inode *inode, struct file *file)
99{
100 struct ocfs2_inode_info *oi = OCFS2_I(inode);
101
102 mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
Josef Sipekd28c9172006-12-08 02:37:25 -0800103 file->f_path.dentry->d_name.len,
104 file->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800105
106 spin_lock(&oi->ip_lock);
107 if (!--oi->ip_open_count)
108 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
109 spin_unlock(&oi->ip_lock);
110
111 mlog_exit(0);
112
113 return 0;
114}
115
116static int ocfs2_sync_file(struct file *file,
117 struct dentry *dentry,
118 int datasync)
119{
120 int err = 0;
121 journal_t *journal;
122 struct inode *inode = dentry->d_inode;
123 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
124
125 mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
126 dentry->d_name.len, dentry->d_name.name);
127
128 err = ocfs2_sync_inode(dentry->d_inode);
129 if (err)
130 goto bail;
131
132 journal = osb->journal->j_journal;
133 err = journal_force_commit(journal);
134
135bail:
136 mlog_exit(err);
137
138 return (err < 0) ? -EIO : 0;
139}
140
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800141int ocfs2_should_update_atime(struct inode *inode,
142 struct vfsmount *vfsmnt)
143{
144 struct timespec now;
145 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
146
147 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
148 return 0;
149
150 if ((inode->i_flags & S_NOATIME) ||
151 ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
152 return 0;
153
Mark Fasheh6c2aad02006-12-19 15:25:52 -0800154 /*
155 * We can be called with no vfsmnt structure - NFSD will
156 * sometimes do this.
157 *
158 * Note that our action here is different than touch_atime() -
159 * if we can't tell whether this is a noatime mount, then we
160 * don't know whether to trust the value of s_atime_quantum.
161 */
162 if (vfsmnt == NULL)
163 return 0;
164
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800165 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
166 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
167 return 0;
168
Mark Fasheh7e913c52006-12-13 00:34:35 -0800169 if (vfsmnt->mnt_flags & MNT_RELATIME) {
170 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
171 (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
172 return 1;
173
174 return 0;
175 }
176
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800177 now = CURRENT_TIME;
178 if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
179 return 0;
180 else
181 return 1;
182}
183
184int ocfs2_update_inode_atime(struct inode *inode,
185 struct buffer_head *bh)
186{
187 int ret;
188 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
189 handle_t *handle;
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700190 struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800191
192 mlog_entry_void();
193
194 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
195 if (handle == NULL) {
196 ret = -ENOMEM;
197 mlog_errno(ret);
198 goto out;
199 }
200
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700201 ret = ocfs2_journal_access(handle, inode, bh,
202 OCFS2_JOURNAL_ACCESS_WRITE);
203 if (ret) {
204 mlog_errno(ret);
205 goto out_commit;
206 }
207
208 /*
209 * Don't use ocfs2_mark_inode_dirty() here as we don't always
210 * have i_mutex to guard against concurrent changes to other
211 * inode fields.
212 */
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800213 inode->i_atime = CURRENT_TIME;
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700214 di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
215 di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
216
217 ret = ocfs2_journal_dirty(handle, bh);
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800218 if (ret < 0)
219 mlog_errno(ret);
220
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700221out_commit:
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800222 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
223out:
224 mlog_exit(ret);
225 return ret;
226}
227
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700228static int ocfs2_set_inode_size(handle_t *handle,
229 struct inode *inode,
230 struct buffer_head *fe_bh,
231 u64 new_i_size)
Mark Fashehccd979b2005-12-15 14:31:24 -0800232{
233 int status;
234
235 mlog_entry_void();
236 i_size_write(inode, new_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -0700237 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800238 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
239
240 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
241 if (status < 0) {
242 mlog_errno(status);
243 goto bail;
244 }
245
246bail:
247 mlog_exit(status);
248 return status;
249}
250
251static int ocfs2_simple_size_update(struct inode *inode,
252 struct buffer_head *di_bh,
253 u64 new_i_size)
254{
255 int ret;
256 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700257 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800258
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700259 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800260 if (handle == NULL) {
261 ret = -ENOMEM;
262 mlog_errno(ret);
263 goto out;
264 }
265
266 ret = ocfs2_set_inode_size(handle, inode, di_bh,
267 new_i_size);
268 if (ret < 0)
269 mlog_errno(ret);
270
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700271 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800272out:
273 return ret;
274}
275
276static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
277 struct inode *inode,
278 struct buffer_head *fe_bh,
279 u64 new_i_size)
280{
281 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700282 handle_t *handle;
Mark Fasheh60b11392007-02-16 11:46:50 -0800283 struct ocfs2_dinode *di;
Mark Fasheh35edec12007-07-06 14:41:18 -0700284 u64 cluster_bytes;
Mark Fashehccd979b2005-12-15 14:31:24 -0800285
286 mlog_entry_void();
287
288 /* TODO: This needs to actually orphan the inode in this
289 * transaction. */
290
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700291 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800292 if (IS_ERR(handle)) {
293 status = PTR_ERR(handle);
294 mlog_errno(status);
295 goto out;
296 }
297
Mark Fasheh60b11392007-02-16 11:46:50 -0800298 status = ocfs2_journal_access(handle, inode, fe_bh,
299 OCFS2_JOURNAL_ACCESS_WRITE);
300 if (status < 0) {
301 mlog_errno(status);
302 goto out_commit;
303 }
304
305 /*
306 * Do this before setting i_size.
307 */
Mark Fasheh35edec12007-07-06 14:41:18 -0700308 cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
309 status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
310 cluster_bytes);
Mark Fasheh60b11392007-02-16 11:46:50 -0800311 if (status) {
312 mlog_errno(status);
313 goto out_commit;
314 }
315
316 i_size_write(inode, new_i_size);
317 inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
318 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
319
320 di = (struct ocfs2_dinode *) fe_bh->b_data;
321 di->i_size = cpu_to_le64(new_i_size);
322 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
323 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
324
325 status = ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800326 if (status < 0)
327 mlog_errno(status);
328
Mark Fasheh60b11392007-02-16 11:46:50 -0800329out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700330 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800331out:
Mark Fasheh60b11392007-02-16 11:46:50 -0800332
Mark Fashehccd979b2005-12-15 14:31:24 -0800333 mlog_exit(status);
334 return status;
335}
336
337static int ocfs2_truncate_file(struct inode *inode,
338 struct buffer_head *di_bh,
339 u64 new_i_size)
340{
341 int status = 0;
342 struct ocfs2_dinode *fe = NULL;
343 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
344 struct ocfs2_truncate_context *tc = NULL;
345
Mark Fashehb06970532006-03-03 10:24:33 -0800346 mlog_entry("(inode = %llu, new_i_size = %llu\n",
347 (unsigned long long)OCFS2_I(inode)->ip_blkno,
348 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800349
Mark Fashehccd979b2005-12-15 14:31:24 -0800350 fe = (struct ocfs2_dinode *) di_bh->b_data;
351 if (!OCFS2_IS_VALID_DINODE(fe)) {
352 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
353 status = -EIO;
354 goto bail;
355 }
356
357 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
Mark Fashehb06970532006-03-03 10:24:33 -0800358 "Inode %llu, inode i_size = %lld != di "
359 "i_size = %llu, i_flags = 0x%x\n",
360 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800361 i_size_read(inode),
Mark Fashehb06970532006-03-03 10:24:33 -0800362 (unsigned long long)le64_to_cpu(fe->i_size),
363 le32_to_cpu(fe->i_flags));
Mark Fashehccd979b2005-12-15 14:31:24 -0800364
365 if (new_i_size > le64_to_cpu(fe->i_size)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800366 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
367 (unsigned long long)le64_to_cpu(fe->i_size),
368 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800369 status = -EINVAL;
370 mlog_errno(status);
371 goto bail;
372 }
373
Mark Fashehb06970532006-03-03 10:24:33 -0800374 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
375 (unsigned long long)le64_to_cpu(fe->i_blkno),
376 (unsigned long long)le64_to_cpu(fe->i_size),
377 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800378
379 /* lets handle the simple truncate cases before doing any more
380 * cluster locking. */
381 if (new_i_size == le64_to_cpu(fe->i_size))
382 goto bail;
383
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700384 down_write(&OCFS2_I(inode)->ip_alloc_sem);
385
Mark Fashehab0920c2006-03-16 15:06:37 -0800386 /* This forces other nodes to sync and drop their pages. Do
387 * this even if we have a truncate without allocation change -
388 * ocfs2 cluster sizes can be much greater than page size, so
389 * we have to truncate them anyway. */
390 status = ocfs2_data_lock(inode, 1);
391 if (status < 0) {
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700392 up_write(&OCFS2_I(inode)->ip_alloc_sem);
393
Mark Fashehab0920c2006-03-16 15:06:37 -0800394 mlog_errno(status);
395 goto bail;
396 }
Mark Fashehab0920c2006-03-16 15:06:37 -0800397
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700398 unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
399 truncate_inode_pages(inode->i_mapping, new_i_size);
400
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 /* alright, we're going to need to do a full blown alloc size
402 * change. Orphan the inode so that recovery can complete the
403 * truncate if necessary. This does the task of marking
404 * i_size. */
405 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
406 if (status < 0) {
407 mlog_errno(status);
Mark Fasheh60b11392007-02-16 11:46:50 -0800408 goto bail_unlock_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800409 }
410
411 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
412 if (status < 0) {
413 mlog_errno(status);
Mark Fasheh60b11392007-02-16 11:46:50 -0800414 goto bail_unlock_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800415 }
416
417 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
418 if (status < 0) {
419 mlog_errno(status);
Mark Fasheh60b11392007-02-16 11:46:50 -0800420 goto bail_unlock_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800421 }
422
423 /* TODO: orphan dir cleanup here. */
Mark Fasheh60b11392007-02-16 11:46:50 -0800424bail_unlock_data:
425 ocfs2_data_unlock(inode, 1);
426
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700427 up_write(&OCFS2_I(inode)->ip_alloc_sem);
428
Mark Fashehccd979b2005-12-15 14:31:24 -0800429bail:
430
431 mlog_exit(status);
432 return status;
433}
434
435/*
436 * extend allocation only here.
437 * we'll update all the disk stuff, and oip->alloc_size
438 *
439 * expect stuff to be locked, a transaction started and enough data /
440 * metadata reservations in the contexts.
441 *
442 * Will return -EAGAIN, and a reason if a restart is needed.
443 * If passed in, *reason will always be set, even in error.
444 */
445int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
446 struct inode *inode,
Mark Fashehdcd05382007-01-16 11:32:23 -0800447 u32 *logical_offset,
Mark Fashehccd979b2005-12-15 14:31:24 -0800448 u32 clusters_to_add,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800449 int mark_unwritten,
Mark Fashehccd979b2005-12-15 14:31:24 -0800450 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700451 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800452 struct ocfs2_alloc_context *data_ac,
453 struct ocfs2_alloc_context *meta_ac,
454 enum ocfs2_alloc_restarted *reason_ret)
455{
456 int status = 0;
457 int free_extents;
458 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
459 enum ocfs2_alloc_restarted reason = RESTART_NONE;
460 u32 bit_off, num_bits;
461 u64 block;
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800462 u8 flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800463
464 BUG_ON(!clusters_to_add);
465
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800466 if (mark_unwritten)
467 flags = OCFS2_EXT_UNWRITTEN;
468
Mark Fashehccd979b2005-12-15 14:31:24 -0800469 free_extents = ocfs2_num_free_extents(osb, inode, fe);
470 if (free_extents < 0) {
471 status = free_extents;
472 mlog_errno(status);
473 goto leave;
474 }
475
476 /* there are two cases which could cause us to EAGAIN in the
477 * we-need-more-metadata case:
478 * 1) we haven't reserved *any*
479 * 2) we are so fragmented, we've needed to add metadata too
480 * many times. */
481 if (!free_extents && !meta_ac) {
482 mlog(0, "we haven't reserved any metadata!\n");
483 status = -EAGAIN;
484 reason = RESTART_META;
485 goto leave;
486 } else if ((!free_extents)
487 && (ocfs2_alloc_context_bits_left(meta_ac)
488 < ocfs2_extend_meta_needed(fe))) {
489 mlog(0, "filesystem is really fragmented...\n");
490 status = -EAGAIN;
491 reason = RESTART_META;
492 goto leave;
493 }
494
495 status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
496 &bit_off, &num_bits);
497 if (status < 0) {
498 if (status != -ENOSPC)
499 mlog_errno(status);
500 goto leave;
501 }
502
503 BUG_ON(num_bits > clusters_to_add);
504
505 /* reserve our write early -- insert_extent may update the inode */
506 status = ocfs2_journal_access(handle, inode, fe_bh,
507 OCFS2_JOURNAL_ACCESS_WRITE);
508 if (status < 0) {
509 mlog_errno(status);
510 goto leave;
511 }
512
513 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb06970532006-03-03 10:24:33 -0800514 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
515 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800516 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
517 *logical_offset, block, num_bits,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800518 flags, meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 if (status < 0) {
520 mlog_errno(status);
521 goto leave;
522 }
523
Mark Fashehccd979b2005-12-15 14:31:24 -0800524 status = ocfs2_journal_dirty(handle, fe_bh);
525 if (status < 0) {
526 mlog_errno(status);
527 goto leave;
528 }
529
530 clusters_to_add -= num_bits;
Mark Fashehdcd05382007-01-16 11:32:23 -0800531 *logical_offset += num_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800532
533 if (clusters_to_add) {
534 mlog(0, "need to alloc once more, clusters = %u, wanted = "
535 "%u\n", fe->i_clusters, clusters_to_add);
536 status = -EAGAIN;
537 reason = RESTART_TRANS;
538 }
539
540leave:
541 mlog_exit(status);
542 if (reason_ret)
543 *reason_ret = reason;
544 return status;
545}
546
Mark Fashehabf8b152007-01-17 13:07:24 -0800547/*
548 * For a given allocation, determine which allocators will need to be
549 * accessed, and lock them, reserving the appropriate number of bits.
550 *
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800551 * Sparse file systems call this from ocfs2_write_begin_nolock()
552 * and ocfs2_allocate_unwritten_extents().
553 *
554 * File systems which don't support holes call this from
555 * ocfs2_extend_allocation().
Mark Fashehabf8b152007-01-17 13:07:24 -0800556 */
Mark Fasheh9517bac2007-02-09 20:24:12 -0800557int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700558 u32 clusters_to_add, u32 extents_to_split,
Mark Fasheh9517bac2007-02-09 20:24:12 -0800559 struct ocfs2_alloc_context **data_ac,
560 struct ocfs2_alloc_context **meta_ac)
Mark Fashehabf8b152007-01-17 13:07:24 -0800561{
Mark Fasheh063c4562007-07-03 13:34:11 -0700562 int ret = 0, num_free_extents;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700563 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
Mark Fashehabf8b152007-01-17 13:07:24 -0800564 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
565
566 *meta_ac = NULL;
Mark Fasheh063c4562007-07-03 13:34:11 -0700567 if (data_ac)
568 *data_ac = NULL;
569
570 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
Mark Fashehabf8b152007-01-17 13:07:24 -0800571
572 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700573 "clusters_to_add = %u, extents_to_split = %u\n",
Mark Fashehabf8b152007-01-17 13:07:24 -0800574 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700575 le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
Mark Fashehabf8b152007-01-17 13:07:24 -0800576
577 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
578 if (num_free_extents < 0) {
579 ret = num_free_extents;
580 mlog_errno(ret);
581 goto out;
582 }
583
584 /*
585 * Sparse allocation file systems need to be more conservative
586 * with reserving room for expansion - the actual allocation
587 * happens while we've got a journal handle open so re-taking
588 * a cluster lock (because we ran out of room for another
589 * extent) will violate ordering rules.
590 *
Mark Fasheh9517bac2007-02-09 20:24:12 -0800591 * Most of the time we'll only be seeing this 1 cluster at a time
Mark Fashehabf8b152007-01-17 13:07:24 -0800592 * anyway.
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700593 *
594 * Always lock for any unwritten extents - we might want to
595 * add blocks during a split.
Mark Fashehabf8b152007-01-17 13:07:24 -0800596 */
597 if (!num_free_extents ||
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700598 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Mark Fashehabf8b152007-01-17 13:07:24 -0800599 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
600 if (ret < 0) {
601 if (ret != -ENOSPC)
602 mlog_errno(ret);
603 goto out;
604 }
605 }
606
Mark Fasheh063c4562007-07-03 13:34:11 -0700607 if (clusters_to_add == 0)
608 goto out;
609
Mark Fashehabf8b152007-01-17 13:07:24 -0800610 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
611 if (ret < 0) {
612 if (ret != -ENOSPC)
613 mlog_errno(ret);
614 goto out;
615 }
616
617out:
618 if (ret) {
619 if (*meta_ac) {
620 ocfs2_free_alloc_context(*meta_ac);
621 *meta_ac = NULL;
622 }
623
624 /*
625 * We cannot have an error and a non null *data_ac.
626 */
627 }
628
629 return ret;
630}
631
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800632static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
633 u32 clusters_to_add, int mark_unwritten)
Mark Fashehccd979b2005-12-15 14:31:24 -0800634{
635 int status = 0;
636 int restart_func = 0;
Mark Fashehabf8b152007-01-17 13:07:24 -0800637 int credits;
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800638 u32 prev_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 struct buffer_head *bh = NULL;
640 struct ocfs2_dinode *fe = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700641 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800642 struct ocfs2_alloc_context *data_ac = NULL;
643 struct ocfs2_alloc_context *meta_ac = NULL;
644 enum ocfs2_alloc_restarted why;
645 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
646
647 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
648
Mark Fashehdcd05382007-01-16 11:32:23 -0800649 /*
650 * This function only exists for file systems which don't
651 * support holes.
652 */
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800653 BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
Mark Fashehdcd05382007-01-16 11:32:23 -0800654
Mark Fashehccd979b2005-12-15 14:31:24 -0800655 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
656 OCFS2_BH_CACHED, inode);
657 if (status < 0) {
658 mlog_errno(status);
659 goto leave;
660 }
661
662 fe = (struct ocfs2_dinode *) bh->b_data;
663 if (!OCFS2_IS_VALID_DINODE(fe)) {
664 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
665 status = -EIO;
666 goto leave;
667 }
668
669restart_all:
670 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
671
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700672 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac,
Mark Fasheh9517bac2007-02-09 20:24:12 -0800673 &meta_ac);
674 if (status) {
675 mlog_errno(status);
676 goto leave;
677 }
678
Mark Fashehccd979b2005-12-15 14:31:24 -0800679 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700680 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800681 if (IS_ERR(handle)) {
682 status = PTR_ERR(handle);
683 handle = NULL;
684 mlog_errno(status);
685 goto leave;
686 }
687
688restarted_transaction:
689 /* reserve a write to the file entry early on - that we if we
690 * run out of credits in the allocation path, we can still
691 * update i_size. */
692 status = ocfs2_journal_access(handle, inode, bh,
693 OCFS2_JOURNAL_ACCESS_WRITE);
694 if (status < 0) {
695 mlog_errno(status);
696 goto leave;
697 }
698
699 prev_clusters = OCFS2_I(inode)->ip_clusters;
700
701 status = ocfs2_do_extend_allocation(osb,
702 inode,
Mark Fashehdcd05382007-01-16 11:32:23 -0800703 &logical_start,
Mark Fashehccd979b2005-12-15 14:31:24 -0800704 clusters_to_add,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800705 mark_unwritten,
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 bh,
707 handle,
708 data_ac,
709 meta_ac,
710 &why);
711 if ((status < 0) && (status != -EAGAIN)) {
712 if (status != -ENOSPC)
713 mlog_errno(status);
714 goto leave;
715 }
716
717 status = ocfs2_journal_dirty(handle, bh);
718 if (status < 0) {
719 mlog_errno(status);
720 goto leave;
721 }
722
723 spin_lock(&OCFS2_I(inode)->ip_lock);
724 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
725 spin_unlock(&OCFS2_I(inode)->ip_lock);
726
727 if (why != RESTART_NONE && clusters_to_add) {
728 if (why == RESTART_META) {
729 mlog(0, "restarting function.\n");
730 restart_func = 1;
731 } else {
732 BUG_ON(why != RESTART_TRANS);
733
734 mlog(0, "restarting transaction.\n");
735 /* TODO: This can be more intelligent. */
736 credits = ocfs2_calc_extend_credits(osb->sb,
737 fe,
738 clusters_to_add);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700739 status = ocfs2_extend_trans(handle, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800740 if (status < 0) {
741 /* handle still has to be committed at
742 * this point. */
743 status = -ENOMEM;
744 mlog_errno(status);
745 goto leave;
746 }
747 goto restarted_transaction;
748 }
749 }
750
Mark Fashehb06970532006-03-03 10:24:33 -0800751 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700752 le32_to_cpu(fe->i_clusters),
753 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -0800754 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
755 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
756
757leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800758 if (handle) {
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700759 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800760 handle = NULL;
761 }
762 if (data_ac) {
763 ocfs2_free_alloc_context(data_ac);
764 data_ac = NULL;
765 }
766 if (meta_ac) {
767 ocfs2_free_alloc_context(meta_ac);
768 meta_ac = NULL;
769 }
770 if ((!status) && restart_func) {
771 restart_func = 0;
772 goto restart_all;
773 }
774 if (bh) {
775 brelse(bh);
776 bh = NULL;
777 }
778
779 mlog_exit(status);
780 return status;
781}
782
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800783static int ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
784 u32 clusters_to_add, int mark_unwritten)
785{
786 int ret;
787
788 /*
789 * The alloc sem blocks peope in read/write from reading our
790 * allocation until we're done changing it. We depend on
791 * i_mutex to block other extend/truncate calls while we're
792 * here.
793 */
794 down_write(&OCFS2_I(inode)->ip_alloc_sem);
795 ret = __ocfs2_extend_allocation(inode, logical_start, clusters_to_add,
796 mark_unwritten);
797 up_write(&OCFS2_I(inode)->ip_alloc_sem);
798
799 return ret;
800}
801
Mark Fashehccd979b2005-12-15 14:31:24 -0800802/* Some parts of this taken from generic_cont_expand, which turned out
803 * to be too fragile to do exactly what we need without us having to
Mark Fasheh53013cb2006-05-05 19:04:03 -0700804 * worry about recursive locking in ->prepare_write() and
805 * ->commit_write(). */
Mark Fashehccd979b2005-12-15 14:31:24 -0800806static int ocfs2_write_zero_page(struct inode *inode,
807 u64 size)
808{
809 struct address_space *mapping = inode->i_mapping;
810 struct page *page;
811 unsigned long index;
812 unsigned int offset;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700813 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800814 int ret;
815
816 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
817 /* ugh. in prepare/commit_write, if from==to==start of block, we
818 ** skip the prepare. make sure we never send an offset for the start
819 ** of a block
820 */
821 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
822 offset++;
823 }
824 index = size >> PAGE_CACHE_SHIFT;
825
826 page = grab_cache_page(mapping, index);
827 if (!page) {
828 ret = -ENOMEM;
829 mlog_errno(ret);
830 goto out;
831 }
832
Mark Fasheh53013cb2006-05-05 19:04:03 -0700833 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
Mark Fashehccd979b2005-12-15 14:31:24 -0800834 if (ret < 0) {
835 mlog_errno(ret);
836 goto out_unlock;
837 }
838
839 if (ocfs2_should_order_data(inode)) {
840 handle = ocfs2_start_walk_page_trans(inode, page, offset,
841 offset);
842 if (IS_ERR(handle)) {
843 ret = PTR_ERR(handle);
844 handle = NULL;
845 goto out_unlock;
846 }
847 }
848
849 /* must not update i_size! */
850 ret = block_commit_write(page, offset, offset);
851 if (ret < 0)
852 mlog_errno(ret);
853 else
854 ret = 0;
855
856 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700857 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800858out_unlock:
859 unlock_page(page);
860 page_cache_release(page);
861out:
862 return ret;
863}
864
865static int ocfs2_zero_extend(struct inode *inode,
866 u64 zero_to_size)
867{
868 int ret = 0;
869 u64 start_off;
870 struct super_block *sb = inode->i_sb;
871
872 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
873 while (start_off < zero_to_size) {
874 ret = ocfs2_write_zero_page(inode, start_off);
875 if (ret < 0) {
876 mlog_errno(ret);
877 goto out;
878 }
879
880 start_off += sb->s_blocksize;
Mark Fashehe2057c52006-10-03 17:53:05 -0700881
882 /*
883 * Very large extends have the potential to lock up
884 * the cpu for extended periods of time.
885 */
886 cond_resched();
Mark Fashehccd979b2005-12-15 14:31:24 -0800887 }
888
889out:
890 return ret;
891}
892
Mark Fasheh53013cb2006-05-05 19:04:03 -0700893/*
894 * A tail_to_skip value > 0 indicates that we're being called from
895 * ocfs2_file_aio_write(). This has the following implications:
896 *
897 * - we don't want to update i_size
898 * - di_bh will be NULL, which is fine because it's only used in the
899 * case where we want to update i_size.
900 * - ocfs2_zero_extend() will then only be filling the hole created
901 * between i_size and the start of the write.
902 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800903static int ocfs2_extend_file(struct inode *inode,
904 struct buffer_head *di_bh,
Mark Fasheh53013cb2006-05-05 19:04:03 -0700905 u64 new_i_size,
906 size_t tail_to_skip)
Mark Fashehccd979b2005-12-15 14:31:24 -0800907{
908 int ret = 0;
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800909 u32 clusters_to_add = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800910
Mark Fasheh53013cb2006-05-05 19:04:03 -0700911 BUG_ON(!tail_to_skip && !di_bh);
912
Mark Fashehccd979b2005-12-15 14:31:24 -0800913 /* setattr sometimes calls us like this. */
914 if (new_i_size == 0)
915 goto out;
916
917 if (i_size_read(inode) == new_i_size)
918 goto out;
919 BUG_ON(new_i_size < i_size_read(inode));
920
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800921 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
922 BUG_ON(tail_to_skip != 0);
923 goto out_update_size;
924 }
925
Mark Fashehccd979b2005-12-15 14:31:24 -0800926 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
927 OCFS2_I(inode)->ip_clusters;
928
Mark Fasheh0effef72006-10-03 17:44:42 -0700929 /*
930 * protect the pages that ocfs2_zero_extend is going to be
931 * pulling into the page cache.. we do this before the
932 * metadata extend so that we don't get into the situation
933 * where we've extended the metadata but can't get the data
934 * lock to zero.
935 */
936 ret = ocfs2_data_lock(inode, 1);
937 if (ret < 0) {
938 mlog_errno(ret);
939 goto out;
940 }
Mark Fasheh53013cb2006-05-05 19:04:03 -0700941
Mark Fasheh0effef72006-10-03 17:44:42 -0700942 if (clusters_to_add) {
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800943 ret = ocfs2_extend_allocation(inode,
944 OCFS2_I(inode)->ip_clusters,
945 clusters_to_add, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800946 if (ret < 0) {
947 mlog_errno(ret);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700948 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800949 }
Mark Fasheh0effef72006-10-03 17:44:42 -0700950 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800951
Mark Fasheh0effef72006-10-03 17:44:42 -0700952 /*
953 * Call this even if we don't add any clusters to the tree. We
954 * still need to zero the area between the old i_size and the
955 * new i_size.
956 */
957 ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
958 if (ret < 0) {
959 mlog_errno(ret);
960 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -0800961 }
962
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800963out_update_size:
Mark Fasheh53013cb2006-05-05 19:04:03 -0700964 if (!tail_to_skip) {
965 /* We're being called from ocfs2_setattr() which wants
966 * us to update i_size */
967 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
968 if (ret < 0)
969 mlog_errno(ret);
970 }
971
972out_unlock:
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800973 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
974 ocfs2_data_unlock(inode, 1);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700975
Mark Fashehccd979b2005-12-15 14:31:24 -0800976out:
977 return ret;
978}
979
980int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
981{
982 int status = 0, size_change;
983 struct inode *inode = dentry->d_inode;
984 struct super_block *sb = inode->i_sb;
985 struct ocfs2_super *osb = OCFS2_SB(sb);
986 struct buffer_head *bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700987 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800988
989 mlog_entry("(0x%p, '%.*s')\n", dentry,
990 dentry->d_name.len, dentry->d_name.name);
991
992 if (attr->ia_valid & ATTR_MODE)
993 mlog(0, "mode change: %d\n", attr->ia_mode);
994 if (attr->ia_valid & ATTR_UID)
995 mlog(0, "uid change: %d\n", attr->ia_uid);
996 if (attr->ia_valid & ATTR_GID)
997 mlog(0, "gid change: %d\n", attr->ia_gid);
998 if (attr->ia_valid & ATTR_SIZE)
999 mlog(0, "size change...\n");
1000 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
1001 mlog(0, "time change...\n");
1002
1003#define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
1004 | ATTR_GID | ATTR_UID | ATTR_MODE)
1005 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
1006 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
1007 return 0;
1008 }
1009
1010 status = inode_change_ok(inode, attr);
1011 if (status)
1012 return status;
1013
1014 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
1015 if (size_change) {
1016 status = ocfs2_rw_lock(inode, 1);
1017 if (status < 0) {
1018 mlog_errno(status);
1019 goto bail;
1020 }
1021 }
1022
Mark Fasheh4bcec182006-10-09 16:02:40 -07001023 status = ocfs2_meta_lock(inode, &bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001024 if (status < 0) {
1025 if (status != -ENOENT)
1026 mlog_errno(status);
1027 goto bail_unlock_rw;
1028 }
1029
1030 if (size_change && attr->ia_size != i_size_read(inode)) {
Mark Fashehce76fd32007-07-20 12:02:14 -07001031 if (attr->ia_size > sb->s_maxbytes) {
1032 status = -EFBIG;
1033 goto bail_unlock;
1034 }
1035
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 if (i_size_read(inode) > attr->ia_size)
1037 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1038 else
Mark Fasheh53013cb2006-05-05 19:04:03 -07001039 status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 if (status < 0) {
1041 if (status != -ENOSPC)
1042 mlog_errno(status);
1043 status = -ENOSPC;
1044 goto bail_unlock;
1045 }
1046 }
1047
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001048 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001049 if (IS_ERR(handle)) {
1050 status = PTR_ERR(handle);
1051 mlog_errno(status);
1052 goto bail_unlock;
1053 }
1054
Mark Fasheh7307de82007-05-09 15:16:19 -07001055 /*
1056 * This will intentionally not wind up calling vmtruncate(),
1057 * since all the work for a size change has been done above.
1058 * Otherwise, we could get into problems with truncate as
1059 * ip_alloc_sem is used there to protect against i_size
1060 * changes.
1061 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001062 status = inode_setattr(inode, attr);
1063 if (status < 0) {
1064 mlog_errno(status);
1065 goto bail_commit;
1066 }
1067
1068 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1069 if (status < 0)
1070 mlog_errno(status);
1071
1072bail_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001073 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001074bail_unlock:
1075 ocfs2_meta_unlock(inode, 1);
1076bail_unlock_rw:
1077 if (size_change)
1078 ocfs2_rw_unlock(inode, 1);
1079bail:
1080 if (bh)
1081 brelse(bh);
1082
1083 mlog_exit(status);
1084 return status;
1085}
1086
1087int ocfs2_getattr(struct vfsmount *mnt,
1088 struct dentry *dentry,
1089 struct kstat *stat)
1090{
1091 struct inode *inode = dentry->d_inode;
1092 struct super_block *sb = dentry->d_inode->i_sb;
1093 struct ocfs2_super *osb = sb->s_fs_info;
1094 int err;
1095
1096 mlog_entry_void();
1097
1098 err = ocfs2_inode_revalidate(dentry);
1099 if (err) {
1100 if (err != -ENOENT)
1101 mlog_errno(err);
1102 goto bail;
1103 }
1104
1105 generic_fillattr(inode, stat);
1106
1107 /* We set the blksize from the cluster size for performance */
1108 stat->blksize = osb->s_clustersize;
1109
1110bail:
1111 mlog_exit(err);
1112
1113 return err;
1114}
1115
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001116int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1117{
1118 int ret;
1119
1120 mlog_entry_void();
1121
1122 ret = ocfs2_meta_lock(inode, NULL, 0);
1123 if (ret) {
Mark Fasheha9f5f702007-04-26 11:43:43 -07001124 if (ret != -ENOENT)
1125 mlog_errno(ret);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001126 goto out;
1127 }
1128
1129 ret = generic_permission(inode, mask, NULL);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001130
1131 ocfs2_meta_unlock(inode, 0);
1132out:
1133 mlog_exit(ret);
1134 return ret;
1135}
1136
Mark Fashehb2580102007-03-09 16:53:21 -08001137static int __ocfs2_write_remove_suid(struct inode *inode,
1138 struct buffer_head *bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001139{
1140 int ret;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001141 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08001142 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1143 struct ocfs2_dinode *di;
1144
Mark Fashehb06970532006-03-03 10:24:33 -08001145 mlog_entry("(Inode %llu, mode 0%o)\n",
Mark Fashehb2580102007-03-09 16:53:21 -08001146 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001147
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001148 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001149 if (handle == NULL) {
1150 ret = -ENOMEM;
1151 mlog_errno(ret);
1152 goto out;
1153 }
1154
Mark Fashehccd979b2005-12-15 14:31:24 -08001155 ret = ocfs2_journal_access(handle, inode, bh,
1156 OCFS2_JOURNAL_ACCESS_WRITE);
1157 if (ret < 0) {
1158 mlog_errno(ret);
Mark Fashehb2580102007-03-09 16:53:21 -08001159 goto out_trans;
Mark Fashehccd979b2005-12-15 14:31:24 -08001160 }
1161
1162 inode->i_mode &= ~S_ISUID;
1163 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1164 inode->i_mode &= ~S_ISGID;
1165
1166 di = (struct ocfs2_dinode *) bh->b_data;
1167 di->i_mode = cpu_to_le16(inode->i_mode);
1168
1169 ret = ocfs2_journal_dirty(handle, bh);
1170 if (ret < 0)
1171 mlog_errno(ret);
Mark Fashehb2580102007-03-09 16:53:21 -08001172
Mark Fashehccd979b2005-12-15 14:31:24 -08001173out_trans:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001174 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001175out:
1176 mlog_exit(ret);
1177 return ret;
1178}
1179
Mark Fasheh9517bac2007-02-09 20:24:12 -08001180/*
1181 * Will look for holes and unwritten extents in the range starting at
1182 * pos for count bytes (inclusive).
1183 */
1184static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1185 size_t count)
1186{
1187 int ret = 0;
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001188 unsigned int extent_flags;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001189 u32 cpos, clusters, extent_len, phys_cpos;
1190 struct super_block *sb = inode->i_sb;
1191
1192 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1193 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1194
1195 while (clusters) {
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001196 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1197 &extent_flags);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001198 if (ret < 0) {
1199 mlog_errno(ret);
1200 goto out;
1201 }
1202
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001203 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001204 ret = 1;
1205 break;
1206 }
1207
1208 if (extent_len > clusters)
1209 extent_len = clusters;
1210
1211 clusters -= extent_len;
1212 cpos += extent_len;
1213 }
1214out:
1215 return ret;
1216}
1217
Mark Fashehb2580102007-03-09 16:53:21 -08001218static int ocfs2_write_remove_suid(struct inode *inode)
1219{
1220 int ret;
1221 struct buffer_head *bh = NULL;
1222 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1223
1224 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1225 oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1226 if (ret < 0) {
1227 mlog_errno(ret);
1228 goto out;
1229 }
1230
1231 ret = __ocfs2_write_remove_suid(inode, bh);
1232out:
1233 brelse(bh);
1234 return ret;
1235}
1236
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001237/*
1238 * Allocate enough extents to cover the region starting at byte offset
1239 * start for len bytes. Existing extents are skipped, any extents
1240 * added are marked as "unwritten".
1241 */
1242static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1243 u64 start, u64 len)
1244{
1245 int ret;
1246 u32 cpos, phys_cpos, clusters, alloc_size;
1247
1248 /*
1249 * We consider both start and len to be inclusive.
1250 */
1251 cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
1252 clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
1253 clusters -= cpos;
1254
1255 while (clusters) {
1256 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1257 &alloc_size, NULL);
1258 if (ret) {
1259 mlog_errno(ret);
1260 goto out;
1261 }
1262
1263 /*
1264 * Hole or existing extent len can be arbitrary, so
1265 * cap it to our own allocation request.
1266 */
1267 if (alloc_size > clusters)
1268 alloc_size = clusters;
1269
1270 if (phys_cpos) {
1271 /*
1272 * We already have an allocation at this
1273 * region so we can safely skip it.
1274 */
1275 goto next;
1276 }
1277
1278 ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
1279 if (ret) {
1280 if (ret != -ENOSPC)
1281 mlog_errno(ret);
1282 goto out;
1283 }
1284
1285next:
1286 cpos += alloc_size;
1287 clusters -= alloc_size;
1288 }
1289
1290 ret = 0;
1291out:
1292 return ret;
1293}
1294
Mark Fasheh063c4562007-07-03 13:34:11 -07001295static int __ocfs2_remove_inode_range(struct inode *inode,
1296 struct buffer_head *di_bh,
1297 u32 cpos, u32 phys_cpos, u32 len,
1298 struct ocfs2_cached_dealloc_ctxt *dealloc)
1299{
1300 int ret;
1301 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
1302 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1303 struct inode *tl_inode = osb->osb_tl_inode;
1304 handle_t *handle;
1305 struct ocfs2_alloc_context *meta_ac = NULL;
1306 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1307
1308 ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac);
1309 if (ret) {
1310 mlog_errno(ret);
1311 return ret;
1312 }
1313
1314 mutex_lock(&tl_inode->i_mutex);
1315
1316 if (ocfs2_truncate_log_needs_flush(osb)) {
1317 ret = __ocfs2_flush_truncate_log(osb);
1318 if (ret < 0) {
1319 mlog_errno(ret);
1320 goto out;
1321 }
1322 }
1323
1324 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1325 if (handle == NULL) {
1326 ret = -ENOMEM;
1327 mlog_errno(ret);
1328 goto out;
1329 }
1330
1331 ret = ocfs2_journal_access(handle, inode, di_bh,
1332 OCFS2_JOURNAL_ACCESS_WRITE);
1333 if (ret) {
1334 mlog_errno(ret);
1335 goto out;
1336 }
1337
1338 ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
1339 dealloc);
1340 if (ret) {
1341 mlog_errno(ret);
1342 goto out_commit;
1343 }
1344
1345 OCFS2_I(inode)->ip_clusters -= len;
1346 di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1347
1348 ret = ocfs2_journal_dirty(handle, di_bh);
1349 if (ret) {
1350 mlog_errno(ret);
1351 goto out_commit;
1352 }
1353
1354 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
1355 if (ret)
1356 mlog_errno(ret);
1357
1358out_commit:
1359 ocfs2_commit_trans(osb, handle);
1360out:
1361 mutex_unlock(&tl_inode->i_mutex);
1362
1363 if (meta_ac)
1364 ocfs2_free_alloc_context(meta_ac);
1365
1366 return ret;
1367}
1368
1369/*
1370 * Truncate a byte range, avoiding pages within partial clusters. This
1371 * preserves those pages for the zeroing code to write to.
1372 */
1373static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
1374 u64 byte_len)
1375{
1376 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1377 loff_t start, end;
1378 struct address_space *mapping = inode->i_mapping;
1379
1380 start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
1381 end = byte_start + byte_len;
1382 end = end & ~(osb->s_clustersize - 1);
1383
1384 if (start < end) {
1385 unmap_mapping_range(mapping, start, end - start, 0);
1386 truncate_inode_pages_range(mapping, start, end - 1);
1387 }
1388}
1389
1390static int ocfs2_zero_partial_clusters(struct inode *inode,
1391 u64 start, u64 len)
1392{
1393 int ret = 0;
1394 u64 tmpend, end = start + len;
1395 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1396 unsigned int csize = osb->s_clustersize;
1397 handle_t *handle;
1398
1399 /*
1400 * The "start" and "end" values are NOT necessarily part of
1401 * the range whose allocation is being deleted. Rather, this
1402 * is what the user passed in with the request. We must zero
1403 * partial clusters here. There's no need to worry about
1404 * physical allocation - the zeroing code knows to skip holes.
1405 */
1406 mlog(0, "byte start: %llu, end: %llu\n",
1407 (unsigned long long)start, (unsigned long long)end);
1408
1409 /*
1410 * If both edges are on a cluster boundary then there's no
1411 * zeroing required as the region is part of the allocation to
1412 * be truncated.
1413 */
1414 if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
1415 goto out;
1416
1417 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1418 if (handle == NULL) {
1419 ret = -ENOMEM;
1420 mlog_errno(ret);
1421 goto out;
1422 }
1423
1424 /*
1425 * We want to get the byte offset of the end of the 1st cluster.
1426 */
1427 tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
1428 if (tmpend > end)
1429 tmpend = end;
1430
1431 mlog(0, "1st range: start: %llu, tmpend: %llu\n",
1432 (unsigned long long)start, (unsigned long long)tmpend);
1433
1434 ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
1435 if (ret)
1436 mlog_errno(ret);
1437
1438 if (tmpend < end) {
1439 /*
1440 * This may make start and end equal, but the zeroing
1441 * code will skip any work in that case so there's no
1442 * need to catch it up here.
1443 */
1444 start = end & ~(osb->s_clustersize - 1);
1445
1446 mlog(0, "2nd range: start: %llu, end: %llu\n",
1447 (unsigned long long)start, (unsigned long long)end);
1448
1449 ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1450 if (ret)
1451 mlog_errno(ret);
1452 }
1453
1454 ocfs2_commit_trans(osb, handle);
1455out:
1456 return ret;
1457}
1458
1459static int ocfs2_remove_inode_range(struct inode *inode,
1460 struct buffer_head *di_bh, u64 byte_start,
1461 u64 byte_len)
1462{
1463 int ret = 0;
1464 u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
1465 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1466 struct ocfs2_cached_dealloc_ctxt dealloc;
1467
1468 ocfs2_init_dealloc_ctxt(&dealloc);
1469
1470 if (byte_len == 0)
1471 return 0;
1472
1473 trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
1474 trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
1475 if (trunc_len >= trunc_start)
1476 trunc_len -= trunc_start;
1477 else
1478 trunc_len = 0;
1479
1480 mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
1481 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1482 (unsigned long long)byte_start,
1483 (unsigned long long)byte_len, trunc_start, trunc_len);
1484
1485 ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1486 if (ret) {
1487 mlog_errno(ret);
1488 goto out;
1489 }
1490
1491 cpos = trunc_start;
1492 while (trunc_len) {
1493 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1494 &alloc_size, NULL);
1495 if (ret) {
1496 mlog_errno(ret);
1497 goto out;
1498 }
1499
1500 if (alloc_size > trunc_len)
1501 alloc_size = trunc_len;
1502
1503 /* Only do work for non-holes */
1504 if (phys_cpos != 0) {
1505 ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
1506 phys_cpos, alloc_size,
1507 &dealloc);
1508 if (ret) {
1509 mlog_errno(ret);
1510 goto out;
1511 }
1512 }
1513
1514 cpos += alloc_size;
1515 trunc_len -= alloc_size;
1516 }
1517
1518 ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1519
1520out:
1521 ocfs2_schedule_truncate_log_flush(osb, 1);
1522 ocfs2_run_deallocs(osb, &dealloc);
1523
1524 return ret;
1525}
1526
Mark Fashehb2580102007-03-09 16:53:21 -08001527/*
1528 * Parts of this function taken from xfs_change_file_space()
1529 */
Mark Fasheh385820a2007-07-19 00:14:38 -07001530static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1531 loff_t f_pos, unsigned int cmd,
1532 struct ocfs2_space_resv *sr,
1533 int change_size)
Mark Fashehb2580102007-03-09 16:53:21 -08001534{
1535 int ret;
1536 s64 llen;
Mark Fasheh385820a2007-07-19 00:14:38 -07001537 loff_t size;
Mark Fashehb2580102007-03-09 16:53:21 -08001538 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1539 struct buffer_head *di_bh = NULL;
1540 handle_t *handle;
Mark Fasheha00cce32007-07-20 11:28:30 -07001541 unsigned long long max_off = inode->i_sb->s_maxbytes;
Mark Fashehb2580102007-03-09 16:53:21 -08001542
Mark Fashehb2580102007-03-09 16:53:21 -08001543 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
1544 return -EROFS;
1545
1546 mutex_lock(&inode->i_mutex);
1547
1548 /*
1549 * This prevents concurrent writes on other nodes
1550 */
1551 ret = ocfs2_rw_lock(inode, 1);
1552 if (ret) {
1553 mlog_errno(ret);
1554 goto out;
1555 }
1556
1557 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1558 if (ret) {
1559 mlog_errno(ret);
1560 goto out_rw_unlock;
1561 }
1562
1563 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1564 ret = -EPERM;
1565 goto out_meta_unlock;
1566 }
1567
1568 switch (sr->l_whence) {
1569 case 0: /*SEEK_SET*/
1570 break;
1571 case 1: /*SEEK_CUR*/
Mark Fasheh385820a2007-07-19 00:14:38 -07001572 sr->l_start += f_pos;
Mark Fashehb2580102007-03-09 16:53:21 -08001573 break;
1574 case 2: /*SEEK_END*/
1575 sr->l_start += i_size_read(inode);
1576 break;
1577 default:
1578 ret = -EINVAL;
1579 goto out_meta_unlock;
1580 }
1581 sr->l_whence = 0;
1582
1583 llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
1584
1585 if (sr->l_start < 0
1586 || sr->l_start > max_off
1587 || (sr->l_start + llen) < 0
1588 || (sr->l_start + llen) > max_off) {
1589 ret = -EINVAL;
1590 goto out_meta_unlock;
1591 }
Mark Fasheh385820a2007-07-19 00:14:38 -07001592 size = sr->l_start + sr->l_len;
Mark Fashehb2580102007-03-09 16:53:21 -08001593
1594 if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
1595 if (sr->l_len <= 0) {
1596 ret = -EINVAL;
1597 goto out_meta_unlock;
1598 }
1599 }
1600
Mark Fasheh385820a2007-07-19 00:14:38 -07001601 if (file && should_remove_suid(file->f_path.dentry)) {
Mark Fashehb2580102007-03-09 16:53:21 -08001602 ret = __ocfs2_write_remove_suid(inode, di_bh);
1603 if (ret) {
1604 mlog_errno(ret);
1605 goto out_meta_unlock;
1606 }
1607 }
1608
1609 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1610 switch (cmd) {
1611 case OCFS2_IOC_RESVSP:
1612 case OCFS2_IOC_RESVSP64:
1613 /*
1614 * This takes unsigned offsets, but the signed ones we
1615 * pass have been checked against overflow above.
1616 */
1617 ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
1618 sr->l_len);
1619 break;
1620 case OCFS2_IOC_UNRESVSP:
1621 case OCFS2_IOC_UNRESVSP64:
1622 ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
1623 sr->l_len);
1624 break;
1625 default:
1626 ret = -EINVAL;
1627 }
1628 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1629 if (ret) {
1630 mlog_errno(ret);
1631 goto out_meta_unlock;
1632 }
1633
1634 /*
1635 * We update c/mtime for these changes
1636 */
1637 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1638 if (IS_ERR(handle)) {
1639 ret = PTR_ERR(handle);
1640 mlog_errno(ret);
1641 goto out_meta_unlock;
1642 }
1643
Mark Fasheh385820a2007-07-19 00:14:38 -07001644 if (change_size && i_size_read(inode) < size)
1645 i_size_write(inode, size);
1646
Mark Fashehb2580102007-03-09 16:53:21 -08001647 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1648 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1649 if (ret < 0)
1650 mlog_errno(ret);
1651
1652 ocfs2_commit_trans(osb, handle);
1653
1654out_meta_unlock:
1655 brelse(di_bh);
1656 ocfs2_meta_unlock(inode, 1);
1657out_rw_unlock:
1658 ocfs2_rw_unlock(inode, 1);
1659
1660 mutex_unlock(&inode->i_mutex);
1661out:
1662 return ret;
1663}
1664
Mark Fasheh385820a2007-07-19 00:14:38 -07001665int ocfs2_change_file_space(struct file *file, unsigned int cmd,
1666 struct ocfs2_space_resv *sr)
1667{
1668 struct inode *inode = file->f_path.dentry->d_inode;
1669 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
1670
1671 if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
1672 !ocfs2_writes_unwritten_extents(osb))
1673 return -ENOTTY;
1674 else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
1675 !ocfs2_sparse_alloc(osb))
1676 return -ENOTTY;
1677
1678 if (!S_ISREG(inode->i_mode))
1679 return -EINVAL;
1680
1681 if (!(file->f_mode & FMODE_WRITE))
1682 return -EBADF;
1683
1684 return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
1685}
1686
1687static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1688 loff_t len)
1689{
1690 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1691 struct ocfs2_space_resv sr;
1692 int change_size = 1;
1693
1694 if (!ocfs2_writes_unwritten_extents(osb))
1695 return -EOPNOTSUPP;
1696
1697 if (S_ISDIR(inode->i_mode))
1698 return -ENODEV;
1699
1700 if (mode & FALLOC_FL_KEEP_SIZE)
1701 change_size = 0;
1702
1703 sr.l_whence = 0;
1704 sr.l_start = (s64)offset;
1705 sr.l_len = (s64)len;
1706
1707 return __ocfs2_change_file_space(NULL, inode, offset,
1708 OCFS2_IOC_RESVSP64, &sr, change_size);
1709}
1710
Tiger Yang8659ac22006-10-17 18:29:52 -07001711static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1712 loff_t *ppos,
1713 size_t count,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001714 int appending,
1715 int *direct_io)
Mark Fashehccd979b2005-12-15 14:31:24 -08001716{
Tiger Yang8659ac22006-10-17 18:29:52 -07001717 int ret = 0, meta_level = appending;
1718 struct inode *inode = dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08001719 u32 clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001720 loff_t newsize, saved_pos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001721
Mark Fashehccd979b2005-12-15 14:31:24 -08001722 /*
1723 * We sample i_size under a read level meta lock to see if our write
1724 * is extending the file, if it is we back off and get a write level
1725 * meta lock.
1726 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001727 for(;;) {
Mark Fasheh4bcec182006-10-09 16:02:40 -07001728 ret = ocfs2_meta_lock(inode, NULL, meta_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001729 if (ret < 0) {
1730 meta_level = -1;
1731 mlog_errno(ret);
1732 goto out;
1733 }
1734
1735 /* Clear suid / sgid if necessary. We do this here
1736 * instead of later in the write path because
1737 * remove_suid() calls ->setattr without any hint that
1738 * we may have already done our cluster locking. Since
1739 * ocfs2_setattr() *must* take cluster locks to
1740 * proceeed, this will lead us to recursively lock the
1741 * inode. There's also the dinode i_size state which
1742 * can be lost via setattr during extending writes (we
1743 * set inode->i_size at the end of a write. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001744 if (should_remove_suid(dentry)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001745 if (meta_level == 0) {
1746 ocfs2_meta_unlock(inode, meta_level);
1747 meta_level = 1;
1748 continue;
1749 }
1750
1751 ret = ocfs2_write_remove_suid(inode);
1752 if (ret < 0) {
1753 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07001754 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001755 }
1756 }
1757
1758 /* work on a copy of ppos until we're sure that we won't have
1759 * to recalculate it due to relocking. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001760 if (appending) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001761 saved_pos = i_size_read(inode);
1762 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1763 } else {
Tiger Yang8659ac22006-10-17 18:29:52 -07001764 saved_pos = *ppos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001765 }
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001766
Mark Fasheh9517bac2007-02-09 20:24:12 -08001767 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
1768 loff_t end = saved_pos + count;
1769
1770 /*
1771 * Skip the O_DIRECT checks if we don't need
1772 * them.
1773 */
1774 if (!direct_io || !(*direct_io))
1775 break;
1776
1777 /*
1778 * Allowing concurrent direct writes means
1779 * i_size changes wouldn't be synchronized, so
1780 * one node could wind up truncating another
1781 * nodes writes.
1782 */
1783 if (end > i_size_read(inode)) {
1784 *direct_io = 0;
1785 break;
1786 }
1787
1788 /*
1789 * We don't fill holes during direct io, so
1790 * check for them here. If any are found, the
1791 * caller will have to retake some cluster
1792 * locks and initiate the io as buffered.
1793 */
1794 ret = ocfs2_check_range_for_holes(inode, saved_pos,
1795 count);
1796 if (ret == 1) {
1797 *direct_io = 0;
1798 ret = 0;
1799 } else if (ret < 0)
1800 mlog_errno(ret);
1801 break;
1802 }
1803
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001804 /*
1805 * The rest of this loop is concerned with legacy file
1806 * systems which don't support sparse files.
1807 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001808
Tiger Yang8659ac22006-10-17 18:29:52 -07001809 newsize = count + saved_pos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001810
Mark Fasheh215c7f92006-02-01 16:42:10 -08001811 mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
1812 (long long) saved_pos, (long long) newsize,
1813 (long long) i_size_read(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08001814
1815 /* No need for a higher level metadata lock if we're
1816 * never going past i_size. */
1817 if (newsize <= i_size_read(inode))
1818 break;
1819
1820 if (meta_level == 0) {
1821 ocfs2_meta_unlock(inode, meta_level);
1822 meta_level = 1;
1823 continue;
1824 }
1825
1826 spin_lock(&OCFS2_I(inode)->ip_lock);
1827 clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
1828 OCFS2_I(inode)->ip_clusters;
1829 spin_unlock(&OCFS2_I(inode)->ip_lock);
1830
1831 mlog(0, "Writing at EOF, may need more allocation: "
Mark Fasheh215c7f92006-02-01 16:42:10 -08001832 "i_size = %lld, newsize = %lld, need %u clusters\n",
1833 (long long) i_size_read(inode), (long long) newsize,
1834 clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08001835
1836 /* We only want to continue the rest of this loop if
1837 * our extend will actually require more
1838 * allocation. */
1839 if (!clusters)
1840 break;
1841
Tiger Yang8659ac22006-10-17 18:29:52 -07001842 ret = ocfs2_extend_file(inode, NULL, newsize, count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001843 if (ret < 0) {
1844 if (ret != -ENOSPC)
1845 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07001846 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001847 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001848 break;
1849 }
1850
Tiger Yang8659ac22006-10-17 18:29:52 -07001851 if (appending)
1852 *ppos = saved_pos;
1853
1854out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -08001855 ocfs2_meta_unlock(inode, meta_level);
Tiger Yang8659ac22006-10-17 18:29:52 -07001856
1857out:
1858 return ret;
1859}
1860
Mark Fasheh9517bac2007-02-09 20:24:12 -08001861static inline void
1862ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
1863{
1864 const struct iovec *iov = *iovp;
1865 size_t base = *basep;
1866
1867 do {
1868 int copy = min(bytes, iov->iov_len - base);
1869
1870 bytes -= copy;
1871 base += copy;
1872 if (iov->iov_len == base) {
1873 iov++;
1874 base = 0;
1875 }
1876 } while (bytes);
1877 *iovp = iov;
1878 *basep = base;
1879}
1880
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001881static struct page * ocfs2_get_write_source(char **ret_src_buf,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001882 const struct iovec *cur_iov,
1883 size_t iov_offset)
1884{
1885 int ret;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001886 char *buf = cur_iov->iov_base + iov_offset;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001887 struct page *src_page = NULL;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001888 unsigned long off;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001889
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001890 off = (unsigned long)(buf) & ~PAGE_CACHE_MASK;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001891
1892 if (!segment_eq(get_fs(), KERNEL_DS)) {
1893 /*
1894 * Pull in the user page. We want to do this outside
1895 * of the meta data locks in order to preserve locking
1896 * order in case of page fault.
1897 */
1898 ret = get_user_pages(current, current->mm,
1899 (unsigned long)buf & PAGE_CACHE_MASK, 1,
1900 0, 0, &src_page, NULL);
1901 if (ret == 1)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001902 *ret_src_buf = kmap(src_page) + off;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001903 else
1904 src_page = ERR_PTR(-EFAULT);
1905 } else {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001906 *ret_src_buf = buf;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001907 }
1908
1909 return src_page;
1910}
1911
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001912static void ocfs2_put_write_source(struct page *page)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001913{
1914 if (page) {
1915 kunmap(page);
1916 page_cache_release(page);
1917 }
1918}
1919
1920static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
1921 const struct iovec *iov,
1922 unsigned long nr_segs,
1923 size_t count,
1924 ssize_t o_direct_written)
1925{
1926 int ret = 0;
1927 ssize_t copied, total = 0;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001928 size_t iov_offset = 0, bytes;
1929 loff_t pos;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001930 const struct iovec *cur_iov = iov;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001931 struct page *user_page, *page;
Jeff Garzik8e1c0912007-07-17 05:40:59 -04001932 char * uninitialized_var(buf);
1933 char *dst;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001934 void *fsdata;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001935
1936 /*
1937 * handle partial DIO write. Adjust cur_iov if needed.
1938 */
1939 ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
1940
1941 do {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001942 pos = *ppos;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001943
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001944 user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset);
1945 if (IS_ERR(user_page)) {
1946 ret = PTR_ERR(user_page);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001947 goto out;
1948 }
1949
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001950 /* Stay within our page boundaries */
1951 bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)),
1952 (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK)));
1953 /* Stay within the vector boundary */
1954 bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset);
1955 /* Stay within count */
1956 bytes = min(bytes, count);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001957
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001958 page = NULL;
1959 ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0,
1960 &page, &fsdata);
1961 if (ret) {
1962 mlog_errno(ret);
1963 goto out;
1964 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001965
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001966 dst = kmap_atomic(page, KM_USER0);
Mark Fasheh7c08d702007-07-20 11:58:36 -07001967 memcpy(dst + (pos & (loff_t)(PAGE_CACHE_SIZE - 1)), buf, bytes);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001968 kunmap_atomic(dst, KM_USER0);
1969 flush_dcache_page(page);
1970 ocfs2_put_write_source(user_page);
1971
1972 copied = ocfs2_write_end(file, file->f_mapping, pos, bytes,
1973 bytes, page, fsdata);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001974 if (copied < 0) {
1975 mlog_errno(copied);
1976 ret = copied;
1977 goto out;
1978 }
1979
1980 total += copied;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001981 *ppos = pos + copied;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001982 count -= copied;
1983
1984 ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
1985 } while(count);
1986
1987out:
1988 return total ? total : ret;
1989}
1990
Tiger Yang8659ac22006-10-17 18:29:52 -07001991static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1992 const struct iovec *iov,
1993 unsigned long nr_segs,
1994 loff_t pos)
1995{
Mark Fasheh9517bac2007-02-09 20:24:12 -08001996 int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
1997 int can_do_direct, sync = 0;
1998 ssize_t written = 0;
1999 size_t ocount; /* original count */
2000 size_t count; /* after file limit checks */
2001 loff_t *ppos = &iocb->ki_pos;
2002 struct file *file = iocb->ki_filp;
2003 struct inode *inode = file->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07002004
Mark Fasheh9517bac2007-02-09 20:24:12 -08002005 mlog_entry("(0x%p, %u, '%.*s')\n", file,
Tiger Yang8659ac22006-10-17 18:29:52 -07002006 (unsigned int)nr_segs,
Mark Fasheh9517bac2007-02-09 20:24:12 -08002007 file->f_path.dentry->d_name.len,
2008 file->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07002009
Tiger Yang8659ac22006-10-17 18:29:52 -07002010 if (iocb->ki_left == 0)
2011 return 0;
2012
Christoph Hellwigd9b08b92007-05-18 13:12:40 +02002013 ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002014 if (ret)
2015 return ret;
2016
2017 count = ocount;
2018
2019 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2020
2021 appending = file->f_flags & O_APPEND ? 1 : 0;
2022 direct_io = file->f_flags & O_DIRECT ? 1 : 0;
2023
Tiger Yang8659ac22006-10-17 18:29:52 -07002024 mutex_lock(&inode->i_mutex);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002025
2026relock:
Tiger Yang8659ac22006-10-17 18:29:52 -07002027 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
Mark Fasheh9517bac2007-02-09 20:24:12 -08002028 if (direct_io) {
Tiger Yang8659ac22006-10-17 18:29:52 -07002029 down_read(&inode->i_alloc_sem);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002030 have_alloc_sem = 1;
Tiger Yang8659ac22006-10-17 18:29:52 -07002031 }
2032
2033 /* concurrent O_DIRECT writes are allowed */
Mark Fasheh9517bac2007-02-09 20:24:12 -08002034 rw_level = !direct_io;
Tiger Yang8659ac22006-10-17 18:29:52 -07002035 ret = ocfs2_rw_lock(inode, rw_level);
2036 if (ret < 0) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08002037 mlog_errno(ret);
2038 goto out_sems;
2039 }
2040
2041 can_do_direct = direct_io;
2042 ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
2043 iocb->ki_left, appending,
2044 &can_do_direct);
2045 if (ret < 0) {
Tiger Yang8659ac22006-10-17 18:29:52 -07002046 mlog_errno(ret);
2047 goto out;
2048 }
2049
Mark Fasheh9517bac2007-02-09 20:24:12 -08002050 /*
2051 * We can't complete the direct I/O as requested, fall back to
2052 * buffered I/O.
2053 */
2054 if (direct_io && !can_do_direct) {
2055 ocfs2_rw_unlock(inode, rw_level);
2056 up_read(&inode->i_alloc_sem);
2057
2058 have_alloc_sem = 0;
2059 rw_level = -1;
2060
2061 direct_io = 0;
2062 sync = 1;
2063 goto relock;
Tiger Yang8659ac22006-10-17 18:29:52 -07002064 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002065
Mark Fasheh9517bac2007-02-09 20:24:12 -08002066 if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
2067 sync = 1;
2068
2069 /*
2070 * XXX: Is it ok to execute these checks a second time?
2071 */
2072 ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
2073 if (ret)
2074 goto out;
2075
2076 /*
2077 * Set pos so that sync_page_range_nolock() below understands
2078 * where to start from. We might've moved it around via the
2079 * calls above. The range we want to actually sync starts from
2080 * *ppos here.
2081 *
2082 */
2083 pos = *ppos;
2084
Mark Fashehccd979b2005-12-15 14:31:24 -08002085 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07002086 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002087
Mark Fasheh9517bac2007-02-09 20:24:12 -08002088 if (direct_io) {
2089 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
2090 ppos, count, ocount);
2091 if (written < 0) {
2092 ret = written;
2093 goto out_dio;
2094 }
2095 } else {
2096 written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
2097 count, written);
2098 if (written < 0) {
2099 ret = written;
2100 if (ret != -EFAULT || ret != -ENOSPC)
2101 mlog_errno(ret);
2102 goto out;
2103 }
2104 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002105
Mark Fasheh9517bac2007-02-09 20:24:12 -08002106out_dio:
Mark Fashehccd979b2005-12-15 14:31:24 -08002107 /* buffered aio wouldn't have proper lock coverage today */
Mark Fasheh9517bac2007-02-09 20:24:12 -08002108 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
Mark Fashehccd979b2005-12-15 14:31:24 -08002109
2110 /*
2111 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
2112 * function pointer which is called when o_direct io completes so that
2113 * it can unlock our rw lock. (it's the clustered equivalent of
2114 * i_alloc_sem; protects truncate from racing with pending ios).
2115 * Unfortunately there are error cases which call end_io and others
2116 * that don't. so we don't have to unlock the rw_lock if either an
2117 * async dio is going to do it in the future or an end_io after an
2118 * error has already done it.
2119 */
2120 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2121 rw_level = -1;
2122 have_alloc_sem = 0;
2123 }
2124
2125out:
Mark Fasheh9517bac2007-02-09 20:24:12 -08002126 if (rw_level != -1)
2127 ocfs2_rw_unlock(inode, rw_level);
2128
2129out_sems:
Mark Fashehccd979b2005-12-15 14:31:24 -08002130 if (have_alloc_sem)
2131 up_read(&inode->i_alloc_sem);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002132
2133 if (written > 0 && sync) {
2134 ssize_t err;
2135
2136 err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
2137 if (err < 0)
2138 written = err;
2139 }
2140
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002141 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002142
2143 mlog_exit(ret);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002144 return written ? written : ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002145}
2146
Mark Fasheh6af67d82007-03-06 17:24:46 -08002147static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
2148 struct pipe_buffer *buf,
2149 struct splice_desc *sd)
2150{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002151 int ret, count;
Mark Fasheh6af67d82007-03-06 17:24:46 -08002152 ssize_t copied = 0;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002153 struct file *file = sd->u.file;
2154 unsigned int offset;
2155 struct page *page = NULL;
2156 void *fsdata;
2157 char *src, *dst;
Mark Fasheh6af67d82007-03-06 17:24:46 -08002158
Jens Axboecac36bb02007-06-14 13:10:48 +02002159 ret = buf->ops->confirm(pipe, buf);
Mark Fasheh6af67d82007-03-06 17:24:46 -08002160 if (ret)
2161 goto out;
2162
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002163 offset = sd->pos & ~PAGE_CACHE_MASK;
Mark Fasheh6af67d82007-03-06 17:24:46 -08002164 count = sd->len;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002165 if (count + offset > PAGE_CACHE_SIZE)
2166 count = PAGE_CACHE_SIZE - offset;
Mark Fasheh6af67d82007-03-06 17:24:46 -08002167
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002168 ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0,
2169 &page, &fsdata);
2170 if (ret) {
2171 mlog_errno(ret);
2172 goto out;
2173 }
Mark Fasheh6af67d82007-03-06 17:24:46 -08002174
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002175 src = buf->ops->map(pipe, buf, 1);
2176 dst = kmap_atomic(page, KM_USER1);
2177 memcpy(dst + offset, src + buf->offset, count);
Jens Axboe3836df62007-07-24 10:17:50 +02002178 kunmap_atomic(dst, KM_USER1);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002179 buf->ops->unmap(pipe, buf, src);
Mark Fasheh6af67d82007-03-06 17:24:46 -08002180
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002181 copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count,
2182 page, fsdata);
2183 if (copied < 0) {
2184 mlog_errno(copied);
2185 ret = copied;
2186 goto out;
2187 }
Mark Fasheh6af67d82007-03-06 17:24:46 -08002188out:
2189
Mark Fasheh3a307ff2007-05-08 17:47:32 -07002190 return copied ? copied : ret;
Mark Fasheh6af67d82007-03-06 17:24:46 -08002191}
2192
2193static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
2194 struct file *out,
2195 loff_t *ppos,
2196 size_t len,
2197 unsigned int flags)
2198{
2199 int ret, err;
2200 struct address_space *mapping = out->f_mapping;
2201 struct inode *inode = mapping->host;
Jens Axboec66ab6f2007-06-12 21:17:17 +02002202 struct splice_desc sd = {
2203 .total_len = len,
2204 .flags = flags,
2205 .pos = *ppos,
Jens Axboe6a14b902007-06-14 13:08:55 +02002206 .u.file = out,
Jens Axboec66ab6f2007-06-12 21:17:17 +02002207 };
Mark Fasheh6af67d82007-03-06 17:24:46 -08002208
Jens Axboec66ab6f2007-06-12 21:17:17 +02002209 ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
Mark Fasheh6af67d82007-03-06 17:24:46 -08002210 if (ret > 0) {
2211 *ppos += ret;
2212
2213 if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
2214 err = generic_osync_inode(inode, mapping,
2215 OSYNC_METADATA|OSYNC_DATA);
2216 if (err)
2217 ret = err;
2218 }
2219 }
2220
2221 return ret;
2222}
2223
Tiger Yang8659ac22006-10-17 18:29:52 -07002224static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
2225 struct file *out,
2226 loff_t *ppos,
2227 size_t len,
2228 unsigned int flags)
2229{
2230 int ret;
Josef Sipekd28c9172006-12-08 02:37:25 -08002231 struct inode *inode = out->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07002232
2233 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
2234 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08002235 out->f_path.dentry->d_name.len,
2236 out->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07002237
2238 inode_double_lock(inode, pipe->inode);
2239
2240 ret = ocfs2_rw_lock(inode, 1);
2241 if (ret < 0) {
2242 mlog_errno(ret);
2243 goto out;
2244 }
2245
Mark Fasheh9517bac2007-02-09 20:24:12 -08002246 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
2247 NULL);
Tiger Yang8659ac22006-10-17 18:29:52 -07002248 if (ret < 0) {
2249 mlog_errno(ret);
2250 goto out_unlock;
2251 }
2252
2253 /* ok, we're done with i_size and alloc work */
Mark Fasheh6af67d82007-03-06 17:24:46 -08002254 ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
Tiger Yang8659ac22006-10-17 18:29:52 -07002255
2256out_unlock:
2257 ocfs2_rw_unlock(inode, 1);
2258out:
2259 inode_double_unlock(inode, pipe->inode);
2260
2261 mlog_exit(ret);
2262 return ret;
2263}
2264
2265static ssize_t ocfs2_file_splice_read(struct file *in,
2266 loff_t *ppos,
2267 struct pipe_inode_info *pipe,
2268 size_t len,
2269 unsigned int flags)
2270{
2271 int ret = 0;
Josef Sipekd28c9172006-12-08 02:37:25 -08002272 struct inode *inode = in->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07002273
2274 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
2275 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08002276 in->f_path.dentry->d_name.len,
2277 in->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07002278
2279 /*
2280 * See the comment in ocfs2_file_aio_read()
2281 */
2282 ret = ocfs2_meta_lock(inode, NULL, 0);
2283 if (ret < 0) {
2284 mlog_errno(ret);
2285 goto bail;
2286 }
2287 ocfs2_meta_unlock(inode, 0);
2288
2289 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2290
2291bail:
2292 mlog_exit(ret);
2293 return ret;
2294}
2295
Mark Fashehccd979b2005-12-15 14:31:24 -08002296static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -07002297 const struct iovec *iov,
2298 unsigned long nr_segs,
Mark Fashehccd979b2005-12-15 14:31:24 -08002299 loff_t pos)
2300{
Tiger Yang25899de2006-11-15 15:49:02 +08002301 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002302 struct file *filp = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -08002303 struct inode *inode = filp->f_path.dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08002304
Badari Pulavarty027445c2006-09-30 23:28:46 -07002305 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
2306 (unsigned int)nr_segs,
Josef Sipekd28c9172006-12-08 02:37:25 -08002307 filp->f_path.dentry->d_name.len,
2308 filp->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -08002309
2310 if (!inode) {
2311 ret = -EINVAL;
2312 mlog_errno(ret);
2313 goto bail;
2314 }
2315
Mark Fashehccd979b2005-12-15 14:31:24 -08002316 /*
2317 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
2318 * need locks to protect pending reads from racing with truncate.
2319 */
2320 if (filp->f_flags & O_DIRECT) {
2321 down_read(&inode->i_alloc_sem);
2322 have_alloc_sem = 1;
2323
2324 ret = ocfs2_rw_lock(inode, 0);
2325 if (ret < 0) {
2326 mlog_errno(ret);
2327 goto bail;
2328 }
2329 rw_level = 0;
2330 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07002331 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002332 }
2333
Mark Fashehc4374f82006-05-05 19:04:35 -07002334 /*
2335 * We're fine letting folks race truncates and extending
2336 * writes with read across the cluster, just like they can
2337 * locally. Hence no rw_lock during read.
2338 *
2339 * Take and drop the meta data lock to update inode fields
2340 * like i_size. This allows the checks down below
2341 * generic_file_aio_read() a chance of actually working.
2342 */
Tiger Yang25899de2006-11-15 15:49:02 +08002343 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07002344 if (ret < 0) {
2345 mlog_errno(ret);
2346 goto bail;
2347 }
Tiger Yang25899de2006-11-15 15:49:02 +08002348 ocfs2_meta_unlock(inode, lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07002349
Badari Pulavarty027445c2006-09-30 23:28:46 -07002350 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
Mark Fashehccd979b2005-12-15 14:31:24 -08002351 if (ret == -EINVAL)
2352 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
2353
2354 /* buffered aio wouldn't have proper lock coverage today */
2355 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
2356
2357 /* see ocfs2_file_aio_write */
2358 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2359 rw_level = -1;
2360 have_alloc_sem = 0;
2361 }
2362
2363bail:
2364 if (have_alloc_sem)
2365 up_read(&inode->i_alloc_sem);
2366 if (rw_level != -1)
2367 ocfs2_rw_unlock(inode, rw_level);
2368 mlog_exit(ret);
2369
2370 return ret;
2371}
2372
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002373const struct inode_operations ocfs2_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002374 .setattr = ocfs2_setattr,
2375 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002376 .permission = ocfs2_permission,
Mark Fasheh385820a2007-07-19 00:14:38 -07002377 .fallocate = ocfs2_fallocate,
Mark Fashehccd979b2005-12-15 14:31:24 -08002378};
2379
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002380const struct inode_operations ocfs2_special_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002381 .setattr = ocfs2_setattr,
2382 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002383 .permission = ocfs2_permission,
Mark Fashehccd979b2005-12-15 14:31:24 -08002384};
2385
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002386const struct file_operations ocfs2_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002387 .read = do_sync_read,
2388 .write = do_sync_write,
Mark Fashehccd979b2005-12-15 14:31:24 -08002389 .mmap = ocfs2_mmap,
2390 .fsync = ocfs2_sync_file,
2391 .release = ocfs2_file_release,
2392 .open = ocfs2_file_open,
2393 .aio_read = ocfs2_file_aio_read,
2394 .aio_write = ocfs2_file_aio_write,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002395 .ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08002396#ifdef CONFIG_COMPAT
2397 .compat_ioctl = ocfs2_compat_ioctl,
2398#endif
Tiger Yang8659ac22006-10-17 18:29:52 -07002399 .splice_read = ocfs2_file_splice_read,
2400 .splice_write = ocfs2_file_splice_write,
Mark Fashehccd979b2005-12-15 14:31:24 -08002401};
2402
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002403const struct file_operations ocfs2_dops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002404 .read = generic_read_dir,
2405 .readdir = ocfs2_readdir,
2406 .fsync = ocfs2_sync_file,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002407 .ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08002408#ifdef CONFIG_COMPAT
2409 .compat_ioctl = ocfs2_compat_ioctl,
2410#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08002411};