blob: c5c183ac41fe5ffc1f836e9103b89f2e2df5e20b [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);
Mark Fasheh60b11392007-02-16 11:46:50 -0800317 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
318
319 di = (struct ocfs2_dinode *) fe_bh->b_data;
320 di->i_size = cpu_to_le64(new_i_size);
321 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
322 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
323
324 status = ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800325 if (status < 0)
326 mlog_errno(status);
327
Mark Fasheh60b11392007-02-16 11:46:50 -0800328out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700329 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800330out:
Mark Fasheh60b11392007-02-16 11:46:50 -0800331
Mark Fashehccd979b2005-12-15 14:31:24 -0800332 mlog_exit(status);
333 return status;
334}
335
336static int ocfs2_truncate_file(struct inode *inode,
337 struct buffer_head *di_bh,
338 u64 new_i_size)
339{
340 int status = 0;
341 struct ocfs2_dinode *fe = NULL;
342 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
343 struct ocfs2_truncate_context *tc = NULL;
344
Mark Fashehb06970532006-03-03 10:24:33 -0800345 mlog_entry("(inode = %llu, new_i_size = %llu\n",
346 (unsigned long long)OCFS2_I(inode)->ip_blkno,
347 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800348
Mark Fashehccd979b2005-12-15 14:31:24 -0800349 fe = (struct ocfs2_dinode *) di_bh->b_data;
350 if (!OCFS2_IS_VALID_DINODE(fe)) {
351 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
352 status = -EIO;
353 goto bail;
354 }
355
356 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
Mark Fashehb06970532006-03-03 10:24:33 -0800357 "Inode %llu, inode i_size = %lld != di "
358 "i_size = %llu, i_flags = 0x%x\n",
359 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800360 i_size_read(inode),
Mark Fashehb06970532006-03-03 10:24:33 -0800361 (unsigned long long)le64_to_cpu(fe->i_size),
362 le32_to_cpu(fe->i_flags));
Mark Fashehccd979b2005-12-15 14:31:24 -0800363
364 if (new_i_size > le64_to_cpu(fe->i_size)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800365 mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
366 (unsigned long long)le64_to_cpu(fe->i_size),
367 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800368 status = -EINVAL;
369 mlog_errno(status);
370 goto bail;
371 }
372
Mark Fashehb06970532006-03-03 10:24:33 -0800373 mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
374 (unsigned long long)le64_to_cpu(fe->i_blkno),
375 (unsigned long long)le64_to_cpu(fe->i_size),
376 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800377
378 /* lets handle the simple truncate cases before doing any more
379 * cluster locking. */
380 if (new_i_size == le64_to_cpu(fe->i_size))
381 goto bail;
382
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700383 down_write(&OCFS2_I(inode)->ip_alloc_sem);
384
Mark Fashehc934a922007-10-18 15:23:46 -0700385 /*
386 * The inode lock forced other nodes to sync and drop their
387 * pages, which (correctly) happens even if we have a truncate
388 * without allocation change - ocfs2 cluster sizes can be much
389 * greater than page size, so we have to truncate them
390 * anyway.
391 */
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700392 unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
393 truncate_inode_pages(inode->i_mapping, new_i_size);
394
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700395 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
396 status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
Mark Fashehb1967d02007-11-20 11:56:39 -0800397 i_size_read(inode), 1);
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700398 if (status)
399 mlog_errno(status);
400
Mark Fashehc934a922007-10-18 15:23:46 -0700401 goto bail_unlock_sem;
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700402 }
403
Mark Fashehccd979b2005-12-15 14:31:24 -0800404 /* alright, we're going to need to do a full blown alloc size
405 * change. Orphan the inode so that recovery can complete the
406 * truncate if necessary. This does the task of marking
407 * i_size. */
408 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
409 if (status < 0) {
410 mlog_errno(status);
Mark Fashehc934a922007-10-18 15:23:46 -0700411 goto bail_unlock_sem;
Mark Fashehccd979b2005-12-15 14:31:24 -0800412 }
413
414 status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
415 if (status < 0) {
416 mlog_errno(status);
Mark Fashehc934a922007-10-18 15:23:46 -0700417 goto bail_unlock_sem;
Mark Fashehccd979b2005-12-15 14:31:24 -0800418 }
419
420 status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
421 if (status < 0) {
422 mlog_errno(status);
Mark Fashehc934a922007-10-18 15:23:46 -0700423 goto bail_unlock_sem;
Mark Fashehccd979b2005-12-15 14:31:24 -0800424 }
425
426 /* TODO: orphan dir cleanup here. */
Mark Fashehc934a922007-10-18 15:23:46 -0700427bail_unlock_sem:
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700428 up_write(&OCFS2_I(inode)->ip_alloc_sem);
429
Mark Fashehccd979b2005-12-15 14:31:24 -0800430bail:
431
432 mlog_exit(status);
433 return status;
434}
435
436/*
437 * extend allocation only here.
438 * we'll update all the disk stuff, and oip->alloc_size
439 *
440 * expect stuff to be locked, a transaction started and enough data /
441 * metadata reservations in the contexts.
442 *
443 * Will return -EAGAIN, and a reason if a restart is needed.
444 * If passed in, *reason will always be set, even in error.
445 */
446int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
447 struct inode *inode,
Mark Fashehdcd05382007-01-16 11:32:23 -0800448 u32 *logical_offset,
Mark Fashehccd979b2005-12-15 14:31:24 -0800449 u32 clusters_to_add,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800450 int mark_unwritten,
Mark Fashehccd979b2005-12-15 14:31:24 -0800451 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700452 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800453 struct ocfs2_alloc_context *data_ac,
454 struct ocfs2_alloc_context *meta_ac,
455 enum ocfs2_alloc_restarted *reason_ret)
456{
457 int status = 0;
458 int free_extents;
459 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
460 enum ocfs2_alloc_restarted reason = RESTART_NONE;
461 u32 bit_off, num_bits;
462 u64 block;
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800463 u8 flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800464
465 BUG_ON(!clusters_to_add);
466
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800467 if (mark_unwritten)
468 flags = OCFS2_EXT_UNWRITTEN;
469
Mark Fashehccd979b2005-12-15 14:31:24 -0800470 free_extents = ocfs2_num_free_extents(osb, inode, fe);
471 if (free_extents < 0) {
472 status = free_extents;
473 mlog_errno(status);
474 goto leave;
475 }
476
477 /* there are two cases which could cause us to EAGAIN in the
478 * we-need-more-metadata case:
479 * 1) we haven't reserved *any*
480 * 2) we are so fragmented, we've needed to add metadata too
481 * many times. */
482 if (!free_extents && !meta_ac) {
483 mlog(0, "we haven't reserved any metadata!\n");
484 status = -EAGAIN;
485 reason = RESTART_META;
486 goto leave;
487 } else if ((!free_extents)
488 && (ocfs2_alloc_context_bits_left(meta_ac)
489 < ocfs2_extend_meta_needed(fe))) {
490 mlog(0, "filesystem is really fragmented...\n");
491 status = -EAGAIN;
492 reason = RESTART_META;
493 goto leave;
494 }
495
Mark Fasheh415cb802007-09-16 20:10:16 -0700496 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
497 clusters_to_add, &bit_off, &num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800498 if (status < 0) {
499 if (status != -ENOSPC)
500 mlog_errno(status);
501 goto leave;
502 }
503
504 BUG_ON(num_bits > clusters_to_add);
505
506 /* reserve our write early -- insert_extent may update the inode */
507 status = ocfs2_journal_access(handle, inode, fe_bh,
508 OCFS2_JOURNAL_ACCESS_WRITE);
509 if (status < 0) {
510 mlog_errno(status);
511 goto leave;
512 }
513
514 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb06970532006-03-03 10:24:33 -0800515 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
516 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800517 status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
518 *logical_offset, block, num_bits,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800519 flags, meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800520 if (status < 0) {
521 mlog_errno(status);
522 goto leave;
523 }
524
Mark Fashehccd979b2005-12-15 14:31:24 -0800525 status = ocfs2_journal_dirty(handle, fe_bh);
526 if (status < 0) {
527 mlog_errno(status);
528 goto leave;
529 }
530
531 clusters_to_add -= num_bits;
Mark Fashehdcd05382007-01-16 11:32:23 -0800532 *logical_offset += num_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800533
534 if (clusters_to_add) {
535 mlog(0, "need to alloc once more, clusters = %u, wanted = "
536 "%u\n", fe->i_clusters, clusters_to_add);
537 status = -EAGAIN;
538 reason = RESTART_TRANS;
539 }
540
541leave:
542 mlog_exit(status);
543 if (reason_ret)
544 *reason_ret = reason;
545 return status;
546}
547
Mark Fashehabf8b152007-01-17 13:07:24 -0800548/*
549 * For a given allocation, determine which allocators will need to be
550 * accessed, and lock them, reserving the appropriate number of bits.
551 *
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800552 * Sparse file systems call this from ocfs2_write_begin_nolock()
553 * and ocfs2_allocate_unwritten_extents().
554 *
555 * File systems which don't support holes call this from
556 * ocfs2_extend_allocation().
Mark Fashehabf8b152007-01-17 13:07:24 -0800557 */
Mark Fasheh9517bac2007-02-09 20:24:12 -0800558int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700559 u32 clusters_to_add, u32 extents_to_split,
Mark Fasheh9517bac2007-02-09 20:24:12 -0800560 struct ocfs2_alloc_context **data_ac,
561 struct ocfs2_alloc_context **meta_ac)
Mark Fashehabf8b152007-01-17 13:07:24 -0800562{
Mark Fasheh063c4562007-07-03 13:34:11 -0700563 int ret = 0, num_free_extents;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700564 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
Mark Fashehabf8b152007-01-17 13:07:24 -0800565 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
566
567 *meta_ac = NULL;
Mark Fasheh063c4562007-07-03 13:34:11 -0700568 if (data_ac)
569 *data_ac = NULL;
570
571 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
Mark Fashehabf8b152007-01-17 13:07:24 -0800572
573 mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700574 "clusters_to_add = %u, extents_to_split = %u\n",
Mark Fashehabf8b152007-01-17 13:07:24 -0800575 (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700576 le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
Mark Fashehabf8b152007-01-17 13:07:24 -0800577
578 num_free_extents = ocfs2_num_free_extents(osb, inode, di);
579 if (num_free_extents < 0) {
580 ret = num_free_extents;
581 mlog_errno(ret);
582 goto out;
583 }
584
585 /*
586 * Sparse allocation file systems need to be more conservative
587 * with reserving room for expansion - the actual allocation
588 * happens while we've got a journal handle open so re-taking
589 * a cluster lock (because we ran out of room for another
590 * extent) will violate ordering rules.
591 *
Mark Fasheh9517bac2007-02-09 20:24:12 -0800592 * Most of the time we'll only be seeing this 1 cluster at a time
Mark Fashehabf8b152007-01-17 13:07:24 -0800593 * anyway.
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700594 *
595 * Always lock for any unwritten extents - we might want to
596 * add blocks during a split.
Mark Fashehabf8b152007-01-17 13:07:24 -0800597 */
598 if (!num_free_extents ||
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700599 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Mark Fashehabf8b152007-01-17 13:07:24 -0800600 ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
601 if (ret < 0) {
602 if (ret != -ENOSPC)
603 mlog_errno(ret);
604 goto out;
605 }
606 }
607
Mark Fasheh063c4562007-07-03 13:34:11 -0700608 if (clusters_to_add == 0)
609 goto out;
610
Mark Fashehabf8b152007-01-17 13:07:24 -0800611 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
612 if (ret < 0) {
613 if (ret != -ENOSPC)
614 mlog_errno(ret);
615 goto out;
616 }
617
618out:
619 if (ret) {
620 if (*meta_ac) {
621 ocfs2_free_alloc_context(*meta_ac);
622 *meta_ac = NULL;
623 }
624
625 /*
626 * We cannot have an error and a non null *data_ac.
627 */
628 }
629
630 return ret;
631}
632
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800633static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
634 u32 clusters_to_add, int mark_unwritten)
Mark Fashehccd979b2005-12-15 14:31:24 -0800635{
636 int status = 0;
637 int restart_func = 0;
Mark Fashehabf8b152007-01-17 13:07:24 -0800638 int credits;
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800639 u32 prev_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -0800640 struct buffer_head *bh = NULL;
641 struct ocfs2_dinode *fe = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700642 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800643 struct ocfs2_alloc_context *data_ac = NULL;
644 struct ocfs2_alloc_context *meta_ac = NULL;
645 enum ocfs2_alloc_restarted why;
646 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
647
648 mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
649
Mark Fashehdcd05382007-01-16 11:32:23 -0800650 /*
651 * This function only exists for file systems which don't
652 * support holes.
653 */
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800654 BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
Mark Fashehdcd05382007-01-16 11:32:23 -0800655
Mark Fashehccd979b2005-12-15 14:31:24 -0800656 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
657 OCFS2_BH_CACHED, inode);
658 if (status < 0) {
659 mlog_errno(status);
660 goto leave;
661 }
662
663 fe = (struct ocfs2_dinode *) bh->b_data;
664 if (!OCFS2_IS_VALID_DINODE(fe)) {
665 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
666 status = -EIO;
667 goto leave;
668 }
669
670restart_all:
671 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
672
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700673 status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac,
Mark Fasheh9517bac2007-02-09 20:24:12 -0800674 &meta_ac);
675 if (status) {
676 mlog_errno(status);
677 goto leave;
678 }
679
Mark Fashehccd979b2005-12-15 14:31:24 -0800680 credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700681 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800682 if (IS_ERR(handle)) {
683 status = PTR_ERR(handle);
684 handle = NULL;
685 mlog_errno(status);
686 goto leave;
687 }
688
689restarted_transaction:
690 /* reserve a write to the file entry early on - that we if we
691 * run out of credits in the allocation path, we can still
692 * update i_size. */
693 status = ocfs2_journal_access(handle, inode, bh,
694 OCFS2_JOURNAL_ACCESS_WRITE);
695 if (status < 0) {
696 mlog_errno(status);
697 goto leave;
698 }
699
700 prev_clusters = OCFS2_I(inode)->ip_clusters;
701
702 status = ocfs2_do_extend_allocation(osb,
703 inode,
Mark Fashehdcd05382007-01-16 11:32:23 -0800704 &logical_start,
Mark Fashehccd979b2005-12-15 14:31:24 -0800705 clusters_to_add,
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800706 mark_unwritten,
Mark Fashehccd979b2005-12-15 14:31:24 -0800707 bh,
708 handle,
709 data_ac,
710 meta_ac,
711 &why);
712 if ((status < 0) && (status != -EAGAIN)) {
713 if (status != -ENOSPC)
714 mlog_errno(status);
715 goto leave;
716 }
717
718 status = ocfs2_journal_dirty(handle, bh);
719 if (status < 0) {
720 mlog_errno(status);
721 goto leave;
722 }
723
724 spin_lock(&OCFS2_I(inode)->ip_lock);
725 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
726 spin_unlock(&OCFS2_I(inode)->ip_lock);
727
728 if (why != RESTART_NONE && clusters_to_add) {
729 if (why == RESTART_META) {
730 mlog(0, "restarting function.\n");
731 restart_func = 1;
732 } else {
733 BUG_ON(why != RESTART_TRANS);
734
735 mlog(0, "restarting transaction.\n");
736 /* TODO: This can be more intelligent. */
737 credits = ocfs2_calc_extend_credits(osb->sb,
738 fe,
739 clusters_to_add);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700740 status = ocfs2_extend_trans(handle, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 if (status < 0) {
742 /* handle still has to be committed at
743 * this point. */
744 status = -ENOMEM;
745 mlog_errno(status);
746 goto leave;
747 }
748 goto restarted_transaction;
749 }
750 }
751
Mark Fashehb06970532006-03-03 10:24:33 -0800752 mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700753 le32_to_cpu(fe->i_clusters),
754 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -0800755 mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
756 OCFS2_I(inode)->ip_clusters, i_size_read(inode));
757
758leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800759 if (handle) {
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700760 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800761 handle = NULL;
762 }
763 if (data_ac) {
764 ocfs2_free_alloc_context(data_ac);
765 data_ac = NULL;
766 }
767 if (meta_ac) {
768 ocfs2_free_alloc_context(meta_ac);
769 meta_ac = NULL;
770 }
771 if ((!status) && restart_func) {
772 restart_func = 0;
773 goto restart_all;
774 }
775 if (bh) {
776 brelse(bh);
777 bh = NULL;
778 }
779
780 mlog_exit(status);
781 return status;
782}
783
784/* Some parts of this taken from generic_cont_expand, which turned out
785 * to be too fragile to do exactly what we need without us having to
Mark Fasheh53013cb2006-05-05 19:04:03 -0700786 * worry about recursive locking in ->prepare_write() and
787 * ->commit_write(). */
Mark Fashehccd979b2005-12-15 14:31:24 -0800788static int ocfs2_write_zero_page(struct inode *inode,
789 u64 size)
790{
791 struct address_space *mapping = inode->i_mapping;
792 struct page *page;
793 unsigned long index;
794 unsigned int offset;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700795 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800796 int ret;
797
798 offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
799 /* ugh. in prepare/commit_write, if from==to==start of block, we
800 ** skip the prepare. make sure we never send an offset for the start
801 ** of a block
802 */
803 if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
804 offset++;
805 }
806 index = size >> PAGE_CACHE_SHIFT;
807
808 page = grab_cache_page(mapping, index);
809 if (!page) {
810 ret = -ENOMEM;
811 mlog_errno(ret);
812 goto out;
813 }
814
Mark Fasheh53013cb2006-05-05 19:04:03 -0700815 ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
Mark Fashehccd979b2005-12-15 14:31:24 -0800816 if (ret < 0) {
817 mlog_errno(ret);
818 goto out_unlock;
819 }
820
821 if (ocfs2_should_order_data(inode)) {
822 handle = ocfs2_start_walk_page_trans(inode, page, offset,
823 offset);
824 if (IS_ERR(handle)) {
825 ret = PTR_ERR(handle);
826 handle = NULL;
827 goto out_unlock;
828 }
829 }
830
831 /* must not update i_size! */
832 ret = block_commit_write(page, offset, offset);
833 if (ret < 0)
834 mlog_errno(ret);
835 else
836 ret = 0;
837
838 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700839 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800840out_unlock:
841 unlock_page(page);
842 page_cache_release(page);
843out:
844 return ret;
845}
846
847static int ocfs2_zero_extend(struct inode *inode,
848 u64 zero_to_size)
849{
850 int ret = 0;
851 u64 start_off;
852 struct super_block *sb = inode->i_sb;
853
854 start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
855 while (start_off < zero_to_size) {
856 ret = ocfs2_write_zero_page(inode, start_off);
857 if (ret < 0) {
858 mlog_errno(ret);
859 goto out;
860 }
861
862 start_off += sb->s_blocksize;
Mark Fashehe2057c52006-10-03 17:53:05 -0700863
864 /*
865 * Very large extends have the potential to lock up
866 * the cpu for extended periods of time.
867 */
868 cond_resched();
Mark Fashehccd979b2005-12-15 14:31:24 -0800869 }
870
871out:
872 return ret;
873}
874
Mark Fasheh65ed39d2007-08-28 17:13:23 -0700875int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
876{
877 int ret;
878 u32 clusters_to_add;
879 struct ocfs2_inode_info *oi = OCFS2_I(inode);
880
881 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
882 if (clusters_to_add < oi->ip_clusters)
883 clusters_to_add = 0;
884 else
885 clusters_to_add -= oi->ip_clusters;
886
887 if (clusters_to_add) {
888 ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
889 clusters_to_add, 0);
890 if (ret) {
891 mlog_errno(ret);
892 goto out;
893 }
894 }
895
896 /*
897 * Call this even if we don't add any clusters to the tree. We
898 * still need to zero the area between the old i_size and the
899 * new i_size.
900 */
901 ret = ocfs2_zero_extend(inode, zero_to);
902 if (ret < 0)
903 mlog_errno(ret);
904
905out:
906 return ret;
907}
908
Mark Fashehccd979b2005-12-15 14:31:24 -0800909static int ocfs2_extend_file(struct inode *inode,
910 struct buffer_head *di_bh,
Mark Fasheh65ed39d2007-08-28 17:13:23 -0700911 u64 new_i_size)
Mark Fashehccd979b2005-12-15 14:31:24 -0800912{
Mark Fashehc934a922007-10-18 15:23:46 -0700913 int ret = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700914 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800915
Mark Fasheh65ed39d2007-08-28 17:13:23 -0700916 BUG_ON(!di_bh);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700917
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 /* setattr sometimes calls us like this. */
919 if (new_i_size == 0)
920 goto out;
921
922 if (i_size_read(inode) == new_i_size)
923 goto out;
924 BUG_ON(new_i_size < i_size_read(inode));
925
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700926 /*
927 * Fall through for converting inline data, even if the fs
928 * supports sparse files.
929 *
930 * The check for inline data here is legal - nobody can add
931 * the feature since we have i_mutex. We must check it again
932 * after acquiring ip_alloc_sem though, as paths like mmap
933 * might have raced us to converting the inode to extents.
934 */
935 if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
936 && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800937 goto out_update_size;
Mark Fashehccd979b2005-12-15 14:31:24 -0800938
Mark Fasheh0effef72006-10-03 17:44:42 -0700939 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -0700940 * The alloc sem blocks people in read/write from reading our
941 * allocation until we're done changing it. We depend on
942 * i_mutex to block other extend/truncate calls while we're
943 * here.
Mark Fasheh0effef72006-10-03 17:44:42 -0700944 */
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700945 down_write(&oi->ip_alloc_sem);
946
947 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
948 /*
949 * We can optimize small extends by keeping the inodes
950 * inline data.
951 */
952 if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
953 up_write(&oi->ip_alloc_sem);
954 goto out_update_size;
955 }
956
957 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
958 if (ret) {
959 up_write(&oi->ip_alloc_sem);
960
961 mlog_errno(ret);
Mark Fashehc934a922007-10-18 15:23:46 -0700962 goto out;
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700963 }
964 }
965
966 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
967 ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
968
969 up_write(&oi->ip_alloc_sem);
Mark Fasheh65ed39d2007-08-28 17:13:23 -0700970
Mark Fasheh0effef72006-10-03 17:44:42 -0700971 if (ret < 0) {
972 mlog_errno(ret);
Mark Fashehc934a922007-10-18 15:23:46 -0700973 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 }
975
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800976out_update_size:
Mark Fasheh65ed39d2007-08-28 17:13:23 -0700977 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
978 if (ret < 0)
979 mlog_errno(ret);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700980
Mark Fashehccd979b2005-12-15 14:31:24 -0800981out:
982 return ret;
983}
984
985int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
986{
987 int status = 0, size_change;
988 struct inode *inode = dentry->d_inode;
989 struct super_block *sb = inode->i_sb;
990 struct ocfs2_super *osb = OCFS2_SB(sb);
991 struct buffer_head *bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700992 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800993
994 mlog_entry("(0x%p, '%.*s')\n", dentry,
995 dentry->d_name.len, dentry->d_name.name);
996
997 if (attr->ia_valid & ATTR_MODE)
998 mlog(0, "mode change: %d\n", attr->ia_mode);
999 if (attr->ia_valid & ATTR_UID)
1000 mlog(0, "uid change: %d\n", attr->ia_uid);
1001 if (attr->ia_valid & ATTR_GID)
1002 mlog(0, "gid change: %d\n", attr->ia_gid);
1003 if (attr->ia_valid & ATTR_SIZE)
1004 mlog(0, "size change...\n");
1005 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
1006 mlog(0, "time change...\n");
1007
1008#define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
1009 | ATTR_GID | ATTR_UID | ATTR_MODE)
1010 if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
1011 mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
1012 return 0;
1013 }
1014
1015 status = inode_change_ok(inode, attr);
1016 if (status)
1017 return status;
1018
1019 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
1020 if (size_change) {
1021 status = ocfs2_rw_lock(inode, 1);
1022 if (status < 0) {
1023 mlog_errno(status);
1024 goto bail;
1025 }
1026 }
1027
Mark Fasheh4bcec182006-10-09 16:02:40 -07001028 status = ocfs2_meta_lock(inode, &bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001029 if (status < 0) {
1030 if (status != -ENOENT)
1031 mlog_errno(status);
1032 goto bail_unlock_rw;
1033 }
1034
1035 if (size_change && attr->ia_size != i_size_read(inode)) {
Mark Fashehce76fd32007-07-20 12:02:14 -07001036 if (attr->ia_size > sb->s_maxbytes) {
1037 status = -EFBIG;
1038 goto bail_unlock;
1039 }
1040
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 if (i_size_read(inode) > attr->ia_size)
1042 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1043 else
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001044 status = ocfs2_extend_file(inode, bh, attr->ia_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 if (status < 0) {
1046 if (status != -ENOSPC)
1047 mlog_errno(status);
1048 status = -ENOSPC;
1049 goto bail_unlock;
1050 }
1051 }
1052
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001053 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 if (IS_ERR(handle)) {
1055 status = PTR_ERR(handle);
1056 mlog_errno(status);
1057 goto bail_unlock;
1058 }
1059
Mark Fasheh7307de82007-05-09 15:16:19 -07001060 /*
1061 * This will intentionally not wind up calling vmtruncate(),
1062 * since all the work for a size change has been done above.
1063 * Otherwise, we could get into problems with truncate as
1064 * ip_alloc_sem is used there to protect against i_size
1065 * changes.
1066 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001067 status = inode_setattr(inode, attr);
1068 if (status < 0) {
1069 mlog_errno(status);
1070 goto bail_commit;
1071 }
1072
1073 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1074 if (status < 0)
1075 mlog_errno(status);
1076
1077bail_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001078 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001079bail_unlock:
1080 ocfs2_meta_unlock(inode, 1);
1081bail_unlock_rw:
1082 if (size_change)
1083 ocfs2_rw_unlock(inode, 1);
1084bail:
1085 if (bh)
1086 brelse(bh);
1087
1088 mlog_exit(status);
1089 return status;
1090}
1091
1092int ocfs2_getattr(struct vfsmount *mnt,
1093 struct dentry *dentry,
1094 struct kstat *stat)
1095{
1096 struct inode *inode = dentry->d_inode;
1097 struct super_block *sb = dentry->d_inode->i_sb;
1098 struct ocfs2_super *osb = sb->s_fs_info;
1099 int err;
1100
1101 mlog_entry_void();
1102
1103 err = ocfs2_inode_revalidate(dentry);
1104 if (err) {
1105 if (err != -ENOENT)
1106 mlog_errno(err);
1107 goto bail;
1108 }
1109
1110 generic_fillattr(inode, stat);
1111
1112 /* We set the blksize from the cluster size for performance */
1113 stat->blksize = osb->s_clustersize;
1114
1115bail:
1116 mlog_exit(err);
1117
1118 return err;
1119}
1120
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001121int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
1122{
1123 int ret;
1124
1125 mlog_entry_void();
1126
1127 ret = ocfs2_meta_lock(inode, NULL, 0);
1128 if (ret) {
Mark Fasheha9f5f702007-04-26 11:43:43 -07001129 if (ret != -ENOENT)
1130 mlog_errno(ret);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001131 goto out;
1132 }
1133
1134 ret = generic_permission(inode, mask, NULL);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001135
1136 ocfs2_meta_unlock(inode, 0);
1137out:
1138 mlog_exit(ret);
1139 return ret;
1140}
1141
Mark Fashehb2580102007-03-09 16:53:21 -08001142static int __ocfs2_write_remove_suid(struct inode *inode,
1143 struct buffer_head *bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001144{
1145 int ret;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001146 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08001147 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1148 struct ocfs2_dinode *di;
1149
Mark Fashehb06970532006-03-03 10:24:33 -08001150 mlog_entry("(Inode %llu, mode 0%o)\n",
Mark Fashehb2580102007-03-09 16:53:21 -08001151 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001152
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001153 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001154 if (handle == NULL) {
1155 ret = -ENOMEM;
1156 mlog_errno(ret);
1157 goto out;
1158 }
1159
Mark Fashehccd979b2005-12-15 14:31:24 -08001160 ret = ocfs2_journal_access(handle, inode, bh,
1161 OCFS2_JOURNAL_ACCESS_WRITE);
1162 if (ret < 0) {
1163 mlog_errno(ret);
Mark Fashehb2580102007-03-09 16:53:21 -08001164 goto out_trans;
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 }
1166
1167 inode->i_mode &= ~S_ISUID;
1168 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1169 inode->i_mode &= ~S_ISGID;
1170
1171 di = (struct ocfs2_dinode *) bh->b_data;
1172 di->i_mode = cpu_to_le16(inode->i_mode);
1173
1174 ret = ocfs2_journal_dirty(handle, bh);
1175 if (ret < 0)
1176 mlog_errno(ret);
Mark Fashehb2580102007-03-09 16:53:21 -08001177
Mark Fashehccd979b2005-12-15 14:31:24 -08001178out_trans:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001179 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001180out:
1181 mlog_exit(ret);
1182 return ret;
1183}
1184
Mark Fasheh9517bac2007-02-09 20:24:12 -08001185/*
1186 * Will look for holes and unwritten extents in the range starting at
1187 * pos for count bytes (inclusive).
1188 */
1189static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1190 size_t count)
1191{
1192 int ret = 0;
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001193 unsigned int extent_flags;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001194 u32 cpos, clusters, extent_len, phys_cpos;
1195 struct super_block *sb = inode->i_sb;
1196
1197 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1198 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1199
1200 while (clusters) {
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001201 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1202 &extent_flags);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001203 if (ret < 0) {
1204 mlog_errno(ret);
1205 goto out;
1206 }
1207
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001208 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001209 ret = 1;
1210 break;
1211 }
1212
1213 if (extent_len > clusters)
1214 extent_len = clusters;
1215
1216 clusters -= extent_len;
1217 cpos += extent_len;
1218 }
1219out:
1220 return ret;
1221}
1222
Mark Fashehb2580102007-03-09 16:53:21 -08001223static int ocfs2_write_remove_suid(struct inode *inode)
1224{
1225 int ret;
1226 struct buffer_head *bh = NULL;
1227 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1228
1229 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1230 oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
1231 if (ret < 0) {
1232 mlog_errno(ret);
1233 goto out;
1234 }
1235
1236 ret = __ocfs2_write_remove_suid(inode, bh);
1237out:
1238 brelse(bh);
1239 return ret;
1240}
1241
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001242/*
1243 * Allocate enough extents to cover the region starting at byte offset
1244 * start for len bytes. Existing extents are skipped, any extents
1245 * added are marked as "unwritten".
1246 */
1247static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1248 u64 start, u64 len)
1249{
1250 int ret;
1251 u32 cpos, phys_cpos, clusters, alloc_size;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001252 u64 end = start + len;
1253 struct buffer_head *di_bh = NULL;
1254
1255 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1256 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1257 OCFS2_I(inode)->ip_blkno, &di_bh,
1258 OCFS2_BH_CACHED, inode);
1259 if (ret) {
1260 mlog_errno(ret);
1261 goto out;
1262 }
1263
1264 /*
1265 * Nothing to do if the requested reservation range
1266 * fits within the inode.
1267 */
1268 if (ocfs2_size_fits_inline_data(di_bh, end))
1269 goto out;
1270
1271 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1272 if (ret) {
1273 mlog_errno(ret);
1274 goto out;
1275 }
1276 }
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001277
1278 /*
1279 * We consider both start and len to be inclusive.
1280 */
1281 cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
1282 clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
1283 clusters -= cpos;
1284
1285 while (clusters) {
1286 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1287 &alloc_size, NULL);
1288 if (ret) {
1289 mlog_errno(ret);
1290 goto out;
1291 }
1292
1293 /*
1294 * Hole or existing extent len can be arbitrary, so
1295 * cap it to our own allocation request.
1296 */
1297 if (alloc_size > clusters)
1298 alloc_size = clusters;
1299
1300 if (phys_cpos) {
1301 /*
1302 * We already have an allocation at this
1303 * region so we can safely skip it.
1304 */
1305 goto next;
1306 }
1307
1308 ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
1309 if (ret) {
1310 if (ret != -ENOSPC)
1311 mlog_errno(ret);
1312 goto out;
1313 }
1314
1315next:
1316 cpos += alloc_size;
1317 clusters -= alloc_size;
1318 }
1319
1320 ret = 0;
1321out:
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001322
1323 brelse(di_bh);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001324 return ret;
1325}
1326
Mark Fasheh063c4562007-07-03 13:34:11 -07001327static int __ocfs2_remove_inode_range(struct inode *inode,
1328 struct buffer_head *di_bh,
1329 u32 cpos, u32 phys_cpos, u32 len,
1330 struct ocfs2_cached_dealloc_ctxt *dealloc)
1331{
1332 int ret;
1333 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
1334 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1335 struct inode *tl_inode = osb->osb_tl_inode;
1336 handle_t *handle;
1337 struct ocfs2_alloc_context *meta_ac = NULL;
1338 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1339
1340 ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac);
1341 if (ret) {
1342 mlog_errno(ret);
1343 return ret;
1344 }
1345
1346 mutex_lock(&tl_inode->i_mutex);
1347
1348 if (ocfs2_truncate_log_needs_flush(osb)) {
1349 ret = __ocfs2_flush_truncate_log(osb);
1350 if (ret < 0) {
1351 mlog_errno(ret);
1352 goto out;
1353 }
1354 }
1355
1356 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1357 if (handle == NULL) {
1358 ret = -ENOMEM;
1359 mlog_errno(ret);
1360 goto out;
1361 }
1362
1363 ret = ocfs2_journal_access(handle, inode, di_bh,
1364 OCFS2_JOURNAL_ACCESS_WRITE);
1365 if (ret) {
1366 mlog_errno(ret);
1367 goto out;
1368 }
1369
1370 ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
1371 dealloc);
1372 if (ret) {
1373 mlog_errno(ret);
1374 goto out_commit;
1375 }
1376
1377 OCFS2_I(inode)->ip_clusters -= len;
1378 di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1379
1380 ret = ocfs2_journal_dirty(handle, di_bh);
1381 if (ret) {
1382 mlog_errno(ret);
1383 goto out_commit;
1384 }
1385
1386 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
1387 if (ret)
1388 mlog_errno(ret);
1389
1390out_commit:
1391 ocfs2_commit_trans(osb, handle);
1392out:
1393 mutex_unlock(&tl_inode->i_mutex);
1394
1395 if (meta_ac)
1396 ocfs2_free_alloc_context(meta_ac);
1397
1398 return ret;
1399}
1400
1401/*
1402 * Truncate a byte range, avoiding pages within partial clusters. This
1403 * preserves those pages for the zeroing code to write to.
1404 */
1405static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
1406 u64 byte_len)
1407{
1408 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1409 loff_t start, end;
1410 struct address_space *mapping = inode->i_mapping;
1411
1412 start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
1413 end = byte_start + byte_len;
1414 end = end & ~(osb->s_clustersize - 1);
1415
1416 if (start < end) {
1417 unmap_mapping_range(mapping, start, end - start, 0);
1418 truncate_inode_pages_range(mapping, start, end - 1);
1419 }
1420}
1421
1422static int ocfs2_zero_partial_clusters(struct inode *inode,
1423 u64 start, u64 len)
1424{
1425 int ret = 0;
1426 u64 tmpend, end = start + len;
1427 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1428 unsigned int csize = osb->s_clustersize;
1429 handle_t *handle;
1430
1431 /*
1432 * The "start" and "end" values are NOT necessarily part of
1433 * the range whose allocation is being deleted. Rather, this
1434 * is what the user passed in with the request. We must zero
1435 * partial clusters here. There's no need to worry about
1436 * physical allocation - the zeroing code knows to skip holes.
1437 */
1438 mlog(0, "byte start: %llu, end: %llu\n",
1439 (unsigned long long)start, (unsigned long long)end);
1440
1441 /*
1442 * If both edges are on a cluster boundary then there's no
1443 * zeroing required as the region is part of the allocation to
1444 * be truncated.
1445 */
1446 if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
1447 goto out;
1448
1449 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1450 if (handle == NULL) {
1451 ret = -ENOMEM;
1452 mlog_errno(ret);
1453 goto out;
1454 }
1455
1456 /*
1457 * We want to get the byte offset of the end of the 1st cluster.
1458 */
1459 tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
1460 if (tmpend > end)
1461 tmpend = end;
1462
1463 mlog(0, "1st range: start: %llu, tmpend: %llu\n",
1464 (unsigned long long)start, (unsigned long long)tmpend);
1465
1466 ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
1467 if (ret)
1468 mlog_errno(ret);
1469
1470 if (tmpend < end) {
1471 /*
1472 * This may make start and end equal, but the zeroing
1473 * code will skip any work in that case so there's no
1474 * need to catch it up here.
1475 */
1476 start = end & ~(osb->s_clustersize - 1);
1477
1478 mlog(0, "2nd range: start: %llu, end: %llu\n",
1479 (unsigned long long)start, (unsigned long long)end);
1480
1481 ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1482 if (ret)
1483 mlog_errno(ret);
1484 }
1485
1486 ocfs2_commit_trans(osb, handle);
1487out:
1488 return ret;
1489}
1490
1491static int ocfs2_remove_inode_range(struct inode *inode,
1492 struct buffer_head *di_bh, u64 byte_start,
1493 u64 byte_len)
1494{
1495 int ret = 0;
1496 u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
1497 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1498 struct ocfs2_cached_dealloc_ctxt dealloc;
Mark Fashehb1967d02007-11-20 11:56:39 -08001499 struct address_space *mapping = inode->i_mapping;
Mark Fasheh063c4562007-07-03 13:34:11 -07001500
1501 ocfs2_init_dealloc_ctxt(&dealloc);
1502
1503 if (byte_len == 0)
1504 return 0;
1505
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001506 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1507 ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
Mark Fashehb1967d02007-11-20 11:56:39 -08001508 byte_start + byte_len, 0);
1509 if (ret) {
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001510 mlog_errno(ret);
Mark Fashehb1967d02007-11-20 11:56:39 -08001511 goto out;
1512 }
1513 /*
1514 * There's no need to get fancy with the page cache
1515 * truncate of an inline-data inode. We're talking
1516 * about less than a page here, which will be cached
1517 * in the dinode buffer anyway.
1518 */
1519 unmap_mapping_range(mapping, 0, 0, 0);
1520 truncate_inode_pages(mapping, 0);
1521 goto out;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001522 }
1523
Mark Fasheh063c4562007-07-03 13:34:11 -07001524 trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
1525 trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
1526 if (trunc_len >= trunc_start)
1527 trunc_len -= trunc_start;
1528 else
1529 trunc_len = 0;
1530
1531 mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
1532 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1533 (unsigned long long)byte_start,
1534 (unsigned long long)byte_len, trunc_start, trunc_len);
1535
1536 ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1537 if (ret) {
1538 mlog_errno(ret);
1539 goto out;
1540 }
1541
1542 cpos = trunc_start;
1543 while (trunc_len) {
1544 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1545 &alloc_size, NULL);
1546 if (ret) {
1547 mlog_errno(ret);
1548 goto out;
1549 }
1550
1551 if (alloc_size > trunc_len)
1552 alloc_size = trunc_len;
1553
1554 /* Only do work for non-holes */
1555 if (phys_cpos != 0) {
1556 ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
1557 phys_cpos, alloc_size,
1558 &dealloc);
1559 if (ret) {
1560 mlog_errno(ret);
1561 goto out;
1562 }
1563 }
1564
1565 cpos += alloc_size;
1566 trunc_len -= alloc_size;
1567 }
1568
1569 ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1570
1571out:
1572 ocfs2_schedule_truncate_log_flush(osb, 1);
1573 ocfs2_run_deallocs(osb, &dealloc);
1574
1575 return ret;
1576}
1577
Mark Fashehb2580102007-03-09 16:53:21 -08001578/*
1579 * Parts of this function taken from xfs_change_file_space()
1580 */
Mark Fasheh385820a2007-07-19 00:14:38 -07001581static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1582 loff_t f_pos, unsigned int cmd,
1583 struct ocfs2_space_resv *sr,
1584 int change_size)
Mark Fashehb2580102007-03-09 16:53:21 -08001585{
1586 int ret;
1587 s64 llen;
Mark Fasheh385820a2007-07-19 00:14:38 -07001588 loff_t size;
Mark Fashehb2580102007-03-09 16:53:21 -08001589 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1590 struct buffer_head *di_bh = NULL;
1591 handle_t *handle;
Mark Fasheha00cce32007-07-20 11:28:30 -07001592 unsigned long long max_off = inode->i_sb->s_maxbytes;
Mark Fashehb2580102007-03-09 16:53:21 -08001593
Mark Fashehb2580102007-03-09 16:53:21 -08001594 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
1595 return -EROFS;
1596
1597 mutex_lock(&inode->i_mutex);
1598
1599 /*
1600 * This prevents concurrent writes on other nodes
1601 */
1602 ret = ocfs2_rw_lock(inode, 1);
1603 if (ret) {
1604 mlog_errno(ret);
1605 goto out;
1606 }
1607
1608 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1609 if (ret) {
1610 mlog_errno(ret);
1611 goto out_rw_unlock;
1612 }
1613
1614 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1615 ret = -EPERM;
1616 goto out_meta_unlock;
1617 }
1618
1619 switch (sr->l_whence) {
1620 case 0: /*SEEK_SET*/
1621 break;
1622 case 1: /*SEEK_CUR*/
Mark Fasheh385820a2007-07-19 00:14:38 -07001623 sr->l_start += f_pos;
Mark Fashehb2580102007-03-09 16:53:21 -08001624 break;
1625 case 2: /*SEEK_END*/
1626 sr->l_start += i_size_read(inode);
1627 break;
1628 default:
1629 ret = -EINVAL;
1630 goto out_meta_unlock;
1631 }
1632 sr->l_whence = 0;
1633
1634 llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
1635
1636 if (sr->l_start < 0
1637 || sr->l_start > max_off
1638 || (sr->l_start + llen) < 0
1639 || (sr->l_start + llen) > max_off) {
1640 ret = -EINVAL;
1641 goto out_meta_unlock;
1642 }
Mark Fasheh385820a2007-07-19 00:14:38 -07001643 size = sr->l_start + sr->l_len;
Mark Fashehb2580102007-03-09 16:53:21 -08001644
1645 if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
1646 if (sr->l_len <= 0) {
1647 ret = -EINVAL;
1648 goto out_meta_unlock;
1649 }
1650 }
1651
Mark Fasheh385820a2007-07-19 00:14:38 -07001652 if (file && should_remove_suid(file->f_path.dentry)) {
Mark Fashehb2580102007-03-09 16:53:21 -08001653 ret = __ocfs2_write_remove_suid(inode, di_bh);
1654 if (ret) {
1655 mlog_errno(ret);
1656 goto out_meta_unlock;
1657 }
1658 }
1659
1660 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1661 switch (cmd) {
1662 case OCFS2_IOC_RESVSP:
1663 case OCFS2_IOC_RESVSP64:
1664 /*
1665 * This takes unsigned offsets, but the signed ones we
1666 * pass have been checked against overflow above.
1667 */
1668 ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
1669 sr->l_len);
1670 break;
1671 case OCFS2_IOC_UNRESVSP:
1672 case OCFS2_IOC_UNRESVSP64:
1673 ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
1674 sr->l_len);
1675 break;
1676 default:
1677 ret = -EINVAL;
1678 }
1679 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1680 if (ret) {
1681 mlog_errno(ret);
1682 goto out_meta_unlock;
1683 }
1684
1685 /*
1686 * We update c/mtime for these changes
1687 */
1688 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1689 if (IS_ERR(handle)) {
1690 ret = PTR_ERR(handle);
1691 mlog_errno(ret);
1692 goto out_meta_unlock;
1693 }
1694
Mark Fasheh385820a2007-07-19 00:14:38 -07001695 if (change_size && i_size_read(inode) < size)
1696 i_size_write(inode, size);
1697
Mark Fashehb2580102007-03-09 16:53:21 -08001698 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1699 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1700 if (ret < 0)
1701 mlog_errno(ret);
1702
1703 ocfs2_commit_trans(osb, handle);
1704
1705out_meta_unlock:
1706 brelse(di_bh);
1707 ocfs2_meta_unlock(inode, 1);
1708out_rw_unlock:
1709 ocfs2_rw_unlock(inode, 1);
1710
1711 mutex_unlock(&inode->i_mutex);
1712out:
1713 return ret;
1714}
1715
Mark Fasheh385820a2007-07-19 00:14:38 -07001716int ocfs2_change_file_space(struct file *file, unsigned int cmd,
1717 struct ocfs2_space_resv *sr)
1718{
1719 struct inode *inode = file->f_path.dentry->d_inode;
1720 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
1721
1722 if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
1723 !ocfs2_writes_unwritten_extents(osb))
1724 return -ENOTTY;
1725 else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
1726 !ocfs2_sparse_alloc(osb))
1727 return -ENOTTY;
1728
1729 if (!S_ISREG(inode->i_mode))
1730 return -EINVAL;
1731
1732 if (!(file->f_mode & FMODE_WRITE))
1733 return -EBADF;
1734
1735 return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
1736}
1737
1738static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1739 loff_t len)
1740{
1741 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1742 struct ocfs2_space_resv sr;
1743 int change_size = 1;
1744
1745 if (!ocfs2_writes_unwritten_extents(osb))
1746 return -EOPNOTSUPP;
1747
1748 if (S_ISDIR(inode->i_mode))
1749 return -ENODEV;
1750
1751 if (mode & FALLOC_FL_KEEP_SIZE)
1752 change_size = 0;
1753
1754 sr.l_whence = 0;
1755 sr.l_start = (s64)offset;
1756 sr.l_len = (s64)len;
1757
1758 return __ocfs2_change_file_space(NULL, inode, offset,
1759 OCFS2_IOC_RESVSP64, &sr, change_size);
1760}
1761
Tiger Yang8659ac22006-10-17 18:29:52 -07001762static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
1763 loff_t *ppos,
1764 size_t count,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001765 int appending,
1766 int *direct_io)
Mark Fashehccd979b2005-12-15 14:31:24 -08001767{
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001768 int ret = 0, meta_level = 0;
Tiger Yang8659ac22006-10-17 18:29:52 -07001769 struct inode *inode = dentry->d_inode;
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001770 loff_t saved_pos, end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001771
Mark Fashehccd979b2005-12-15 14:31:24 -08001772 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001773 * We start with a read level meta lock and only jump to an ex
1774 * if we need to make modifications here.
Mark Fashehccd979b2005-12-15 14:31:24 -08001775 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001776 for(;;) {
Mark Fasheh4bcec182006-10-09 16:02:40 -07001777 ret = ocfs2_meta_lock(inode, NULL, meta_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001778 if (ret < 0) {
1779 meta_level = -1;
1780 mlog_errno(ret);
1781 goto out;
1782 }
1783
1784 /* Clear suid / sgid if necessary. We do this here
1785 * instead of later in the write path because
1786 * remove_suid() calls ->setattr without any hint that
1787 * we may have already done our cluster locking. Since
1788 * ocfs2_setattr() *must* take cluster locks to
1789 * proceeed, this will lead us to recursively lock the
1790 * inode. There's also the dinode i_size state which
1791 * can be lost via setattr during extending writes (we
1792 * set inode->i_size at the end of a write. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001793 if (should_remove_suid(dentry)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001794 if (meta_level == 0) {
1795 ocfs2_meta_unlock(inode, meta_level);
1796 meta_level = 1;
1797 continue;
1798 }
1799
1800 ret = ocfs2_write_remove_suid(inode);
1801 if (ret < 0) {
1802 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07001803 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001804 }
1805 }
1806
1807 /* work on a copy of ppos until we're sure that we won't have
1808 * to recalculate it due to relocking. */
Tiger Yang8659ac22006-10-17 18:29:52 -07001809 if (appending) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001810 saved_pos = i_size_read(inode);
1811 mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
1812 } else {
Tiger Yang8659ac22006-10-17 18:29:52 -07001813 saved_pos = *ppos;
Mark Fashehccd979b2005-12-15 14:31:24 -08001814 }
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001815
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001816 end = saved_pos + count;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001817
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001818 /*
1819 * Skip the O_DIRECT checks if we don't need
1820 * them.
1821 */
1822 if (!direct_io || !(*direct_io))
1823 break;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001824
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001825 /*
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001826 * There's no sane way to do direct writes to an inode
1827 * with inline data.
1828 */
1829 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1830 *direct_io = 0;
1831 break;
1832 }
1833
1834 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001835 * Allowing concurrent direct writes means
1836 * i_size changes wouldn't be synchronized, so
1837 * one node could wind up truncating another
1838 * nodes writes.
1839 */
1840 if (end > i_size_read(inode)) {
1841 *direct_io = 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001842 break;
1843 }
1844
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001845 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001846 * We don't fill holes during direct io, so
1847 * check for them here. If any are found, the
1848 * caller will have to retake some cluster
1849 * locks and initiate the io as buffered.
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001850 */
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001851 ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
1852 if (ret == 1) {
1853 *direct_io = 0;
1854 ret = 0;
1855 } else if (ret < 0)
1856 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001857 break;
1858 }
1859
Tiger Yang8659ac22006-10-17 18:29:52 -07001860 if (appending)
1861 *ppos = saved_pos;
1862
1863out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -08001864 ocfs2_meta_unlock(inode, meta_level);
Tiger Yang8659ac22006-10-17 18:29:52 -07001865
1866out:
1867 return ret;
1868}
1869
1870static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
1871 const struct iovec *iov,
1872 unsigned long nr_segs,
1873 loff_t pos)
1874{
Mark Fasheh9517bac2007-02-09 20:24:12 -08001875 int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
Nick Pigginb6af1bc2007-10-16 01:25:24 -07001876 int can_do_direct;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001877 ssize_t written = 0;
1878 size_t ocount; /* original count */
1879 size_t count; /* after file limit checks */
Mark Fasheh9ea2d322007-10-18 14:14:45 -07001880 loff_t old_size, *ppos = &iocb->ki_pos;
1881 u32 old_clusters;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001882 struct file *file = iocb->ki_filp;
1883 struct inode *inode = file->f_path.dentry->d_inode;
Mark Fasheh9ea2d322007-10-18 14:14:45 -07001884 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tiger Yang8659ac22006-10-17 18:29:52 -07001885
Mark Fasheh9517bac2007-02-09 20:24:12 -08001886 mlog_entry("(0x%p, %u, '%.*s')\n", file,
Tiger Yang8659ac22006-10-17 18:29:52 -07001887 (unsigned int)nr_segs,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001888 file->f_path.dentry->d_name.len,
1889 file->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07001890
Tiger Yang8659ac22006-10-17 18:29:52 -07001891 if (iocb->ki_left == 0)
1892 return 0;
1893
Mark Fasheh9517bac2007-02-09 20:24:12 -08001894 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1895
1896 appending = file->f_flags & O_APPEND ? 1 : 0;
1897 direct_io = file->f_flags & O_DIRECT ? 1 : 0;
1898
Tiger Yang8659ac22006-10-17 18:29:52 -07001899 mutex_lock(&inode->i_mutex);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001900
1901relock:
Tiger Yang8659ac22006-10-17 18:29:52 -07001902 /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001903 if (direct_io) {
Tiger Yang8659ac22006-10-17 18:29:52 -07001904 down_read(&inode->i_alloc_sem);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001905 have_alloc_sem = 1;
Tiger Yang8659ac22006-10-17 18:29:52 -07001906 }
1907
1908 /* concurrent O_DIRECT writes are allowed */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001909 rw_level = !direct_io;
Tiger Yang8659ac22006-10-17 18:29:52 -07001910 ret = ocfs2_rw_lock(inode, rw_level);
1911 if (ret < 0) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001912 mlog_errno(ret);
1913 goto out_sems;
1914 }
1915
1916 can_do_direct = direct_io;
1917 ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
1918 iocb->ki_left, appending,
1919 &can_do_direct);
1920 if (ret < 0) {
Tiger Yang8659ac22006-10-17 18:29:52 -07001921 mlog_errno(ret);
1922 goto out;
1923 }
1924
Mark Fasheh9517bac2007-02-09 20:24:12 -08001925 /*
1926 * We can't complete the direct I/O as requested, fall back to
1927 * buffered I/O.
1928 */
1929 if (direct_io && !can_do_direct) {
1930 ocfs2_rw_unlock(inode, rw_level);
1931 up_read(&inode->i_alloc_sem);
1932
1933 have_alloc_sem = 0;
1934 rw_level = -1;
1935
1936 direct_io = 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001937 goto relock;
Tiger Yang8659ac22006-10-17 18:29:52 -07001938 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001939
Mark Fasheh9ea2d322007-10-18 14:14:45 -07001940 /*
1941 * To later detect whether a journal commit for sync writes is
1942 * necessary, we sample i_size, and cluster count here.
1943 */
1944 old_size = i_size_read(inode);
1945 old_clusters = OCFS2_I(inode)->ip_clusters;
1946
Mark Fashehccd979b2005-12-15 14:31:24 -08001947 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07001948 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001949
Mark Fasheh9517bac2007-02-09 20:24:12 -08001950 if (direct_io) {
Nick Pigginb6af1bc2007-10-16 01:25:24 -07001951 ret = generic_segment_checks(iov, &nr_segs, &ocount,
1952 VERIFY_READ);
1953 if (ret)
1954 goto out_dio;
1955
1956 ret = generic_write_checks(file, ppos, &count,
1957 S_ISBLK(inode->i_mode));
1958 if (ret)
1959 goto out_dio;
1960
Mark Fasheh9517bac2007-02-09 20:24:12 -08001961 written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
1962 ppos, count, ocount);
1963 if (written < 0) {
1964 ret = written;
1965 goto out_dio;
1966 }
1967 } else {
Nick Pigginb6af1bc2007-10-16 01:25:24 -07001968 written = generic_file_aio_write_nolock(iocb, iov, nr_segs,
1969 *ppos);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001970 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001971
Mark Fasheh9517bac2007-02-09 20:24:12 -08001972out_dio:
Mark Fashehccd979b2005-12-15 14:31:24 -08001973 /* buffered aio wouldn't have proper lock coverage today */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001974 BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
Mark Fashehccd979b2005-12-15 14:31:24 -08001975
Mark Fasheh9ea2d322007-10-18 14:14:45 -07001976 if ((file->f_flags & O_SYNC && !direct_io) || IS_SYNC(inode)) {
1977 /*
1978 * The generic write paths have handled getting data
1979 * to disk, but since we don't make use of the dirty
1980 * inode list, a manual journal commit is necessary
1981 * here.
1982 */
1983 if (old_size != i_size_read(inode) ||
1984 old_clusters != OCFS2_I(inode)->ip_clusters) {
1985 ret = journal_force_commit(osb->journal->j_journal);
1986 if (ret < 0)
1987 written = ret;
1988 }
1989 }
1990
Mark Fashehccd979b2005-12-15 14:31:24 -08001991 /*
1992 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
1993 * function pointer which is called when o_direct io completes so that
1994 * it can unlock our rw lock. (it's the clustered equivalent of
1995 * i_alloc_sem; protects truncate from racing with pending ios).
1996 * Unfortunately there are error cases which call end_io and others
1997 * that don't. so we don't have to unlock the rw_lock if either an
1998 * async dio is going to do it in the future or an end_io after an
1999 * error has already done it.
2000 */
2001 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2002 rw_level = -1;
2003 have_alloc_sem = 0;
2004 }
2005
2006out:
Mark Fasheh9517bac2007-02-09 20:24:12 -08002007 if (rw_level != -1)
2008 ocfs2_rw_unlock(inode, rw_level);
2009
2010out_sems:
Mark Fashehccd979b2005-12-15 14:31:24 -08002011 if (have_alloc_sem)
2012 up_read(&inode->i_alloc_sem);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002013
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002014 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002015
2016 mlog_exit(ret);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002017 return written ? written : ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002018}
2019
Tiger Yang8659ac22006-10-17 18:29:52 -07002020static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
2021 struct file *out,
2022 loff_t *ppos,
2023 size_t len,
2024 unsigned int flags)
2025{
2026 int ret;
Josef Sipekd28c9172006-12-08 02:37:25 -08002027 struct inode *inode = out->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07002028
2029 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
2030 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08002031 out->f_path.dentry->d_name.len,
2032 out->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07002033
2034 inode_double_lock(inode, pipe->inode);
2035
2036 ret = ocfs2_rw_lock(inode, 1);
2037 if (ret < 0) {
2038 mlog_errno(ret);
2039 goto out;
2040 }
2041
Mark Fasheh9517bac2007-02-09 20:24:12 -08002042 ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
2043 NULL);
Tiger Yang8659ac22006-10-17 18:29:52 -07002044 if (ret < 0) {
2045 mlog_errno(ret);
2046 goto out_unlock;
2047 }
2048
Nick Pigginb6af1bc2007-10-16 01:25:24 -07002049 ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
Tiger Yang8659ac22006-10-17 18:29:52 -07002050
2051out_unlock:
2052 ocfs2_rw_unlock(inode, 1);
2053out:
2054 inode_double_unlock(inode, pipe->inode);
2055
2056 mlog_exit(ret);
2057 return ret;
2058}
2059
2060static ssize_t ocfs2_file_splice_read(struct file *in,
2061 loff_t *ppos,
2062 struct pipe_inode_info *pipe,
2063 size_t len,
2064 unsigned int flags)
2065{
2066 int ret = 0;
Josef Sipekd28c9172006-12-08 02:37:25 -08002067 struct inode *inode = in->f_path.dentry->d_inode;
Tiger Yang8659ac22006-10-17 18:29:52 -07002068
2069 mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
2070 (unsigned int)len,
Josef Sipekd28c9172006-12-08 02:37:25 -08002071 in->f_path.dentry->d_name.len,
2072 in->f_path.dentry->d_name.name);
Tiger Yang8659ac22006-10-17 18:29:52 -07002073
2074 /*
2075 * See the comment in ocfs2_file_aio_read()
2076 */
2077 ret = ocfs2_meta_lock(inode, NULL, 0);
2078 if (ret < 0) {
2079 mlog_errno(ret);
2080 goto bail;
2081 }
2082 ocfs2_meta_unlock(inode, 0);
2083
2084 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2085
2086bail:
2087 mlog_exit(ret);
2088 return ret;
2089}
2090
Mark Fashehccd979b2005-12-15 14:31:24 -08002091static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
Badari Pulavarty027445c2006-09-30 23:28:46 -07002092 const struct iovec *iov,
2093 unsigned long nr_segs,
Mark Fashehccd979b2005-12-15 14:31:24 -08002094 loff_t pos)
2095{
Tiger Yang25899de2006-11-15 15:49:02 +08002096 int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002097 struct file *filp = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -08002098 struct inode *inode = filp->f_path.dentry->d_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -08002099
Badari Pulavarty027445c2006-09-30 23:28:46 -07002100 mlog_entry("(0x%p, %u, '%.*s')\n", filp,
2101 (unsigned int)nr_segs,
Josef Sipekd28c9172006-12-08 02:37:25 -08002102 filp->f_path.dentry->d_name.len,
2103 filp->f_path.dentry->d_name.name);
Mark Fashehccd979b2005-12-15 14:31:24 -08002104
2105 if (!inode) {
2106 ret = -EINVAL;
2107 mlog_errno(ret);
2108 goto bail;
2109 }
2110
Mark Fashehccd979b2005-12-15 14:31:24 -08002111 /*
2112 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
2113 * need locks to protect pending reads from racing with truncate.
2114 */
2115 if (filp->f_flags & O_DIRECT) {
2116 down_read(&inode->i_alloc_sem);
2117 have_alloc_sem = 1;
2118
2119 ret = ocfs2_rw_lock(inode, 0);
2120 if (ret < 0) {
2121 mlog_errno(ret);
2122 goto bail;
2123 }
2124 rw_level = 0;
2125 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07002126 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002127 }
2128
Mark Fashehc4374f82006-05-05 19:04:35 -07002129 /*
2130 * We're fine letting folks race truncates and extending
2131 * writes with read across the cluster, just like they can
2132 * locally. Hence no rw_lock during read.
2133 *
2134 * Take and drop the meta data lock to update inode fields
2135 * like i_size. This allows the checks down below
2136 * generic_file_aio_read() a chance of actually working.
2137 */
Tiger Yang25899de2006-11-15 15:49:02 +08002138 ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07002139 if (ret < 0) {
2140 mlog_errno(ret);
2141 goto bail;
2142 }
Tiger Yang25899de2006-11-15 15:49:02 +08002143 ocfs2_meta_unlock(inode, lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07002144
Badari Pulavarty027445c2006-09-30 23:28:46 -07002145 ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
Mark Fashehccd979b2005-12-15 14:31:24 -08002146 if (ret == -EINVAL)
2147 mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
2148
2149 /* buffered aio wouldn't have proper lock coverage today */
2150 BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
2151
2152 /* see ocfs2_file_aio_write */
2153 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2154 rw_level = -1;
2155 have_alloc_sem = 0;
2156 }
2157
2158bail:
2159 if (have_alloc_sem)
2160 up_read(&inode->i_alloc_sem);
2161 if (rw_level != -1)
2162 ocfs2_rw_unlock(inode, rw_level);
2163 mlog_exit(ret);
2164
2165 return ret;
2166}
2167
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002168const struct inode_operations ocfs2_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002169 .setattr = ocfs2_setattr,
2170 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002171 .permission = ocfs2_permission,
Mark Fasheh385820a2007-07-19 00:14:38 -07002172 .fallocate = ocfs2_fallocate,
Mark Fashehccd979b2005-12-15 14:31:24 -08002173};
2174
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002175const struct inode_operations ocfs2_special_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002176 .setattr = ocfs2_setattr,
2177 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002178 .permission = ocfs2_permission,
Mark Fashehccd979b2005-12-15 14:31:24 -08002179};
2180
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002181const struct file_operations ocfs2_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002182 .read = do_sync_read,
2183 .write = do_sync_write,
Mark Fashehccd979b2005-12-15 14:31:24 -08002184 .mmap = ocfs2_mmap,
2185 .fsync = ocfs2_sync_file,
2186 .release = ocfs2_file_release,
2187 .open = ocfs2_file_open,
2188 .aio_read = ocfs2_file_aio_read,
2189 .aio_write = ocfs2_file_aio_write,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002190 .ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08002191#ifdef CONFIG_COMPAT
2192 .compat_ioctl = ocfs2_compat_ioctl,
2193#endif
Tiger Yang8659ac22006-10-17 18:29:52 -07002194 .splice_read = ocfs2_file_splice_read,
2195 .splice_write = ocfs2_file_splice_write,
Mark Fashehccd979b2005-12-15 14:31:24 -08002196};
2197
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002198const struct file_operations ocfs2_dops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002199 .read = generic_read_dir,
2200 .readdir = ocfs2_readdir,
2201 .fsync = ocfs2_sync_file,
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002202 .ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08002203#ifdef CONFIG_COMPAT
2204 .compat_ioctl = ocfs2_compat_ioctl,
2205#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08002206};