blob: 4d9e8275ed999ce9fc917468495863cdc4a75230 [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>
Jan Karaa90714c2008-10-09 19:38:40 +020038#include <linux/quotaops.h>
Jan Kara04eda1a2010-08-05 20:32:45 +020039#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040040#include <linux/backing-dev.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080041
Mark Fashehccd979b2005-12-15 14:31:24 -080042#include <cluster/masklog.h>
43
44#include "ocfs2.h"
45
46#include "alloc.h"
47#include "aops.h"
48#include "dir.h"
49#include "dlmglue.h"
50#include "extent_map.h"
51#include "file.h"
52#include "sysfile.h"
53#include "inode.h"
Herbert Poetzlca4d1472006-07-03 17:27:12 -070054#include "ioctl.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080055#include "journal.h"
Mark Fasheh53fc6222007-12-20 16:49:04 -080056#include "locks.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080057#include "mmap.h"
58#include "suballoc.h"
59#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080060#include "xattr.h"
Tiger Yang23fc2702008-11-14 11:17:18 +080061#include "acl.h"
Jan Karaa90714c2008-10-09 19:38:40 +020062#include "quota.h"
Tao Ma293b2f72009-08-25 08:02:48 +080063#include "refcounttree.h"
Tao Ma468eedd2011-02-22 22:14:41 +080064#include "ocfs2_trace.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080065
66#include "buffer_head_io.h"
67
Mark Fasheh53fc6222007-12-20 16:49:04 -080068static int ocfs2_init_file_private(struct inode *inode, struct file *file)
69{
70 struct ocfs2_file_private *fp;
71
72 fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
73 if (!fp)
74 return -ENOMEM;
75
76 fp->fp_file = file;
77 mutex_init(&fp->fp_mutex);
78 ocfs2_file_lock_res_init(&fp->fp_flock, fp);
79 file->private_data = fp;
80
81 return 0;
82}
83
84static void ocfs2_free_file_private(struct inode *inode, struct file *file)
85{
86 struct ocfs2_file_private *fp = file->private_data;
87 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
88
89 if (fp) {
90 ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
91 ocfs2_lock_res_free(&fp->fp_flock);
92 kfree(fp);
93 file->private_data = NULL;
94 }
95}
96
Mark Fashehccd979b2005-12-15 14:31:24 -080097static int ocfs2_file_open(struct inode *inode, struct file *file)
98{
99 int status;
100 int mode = file->f_flags;
101 struct ocfs2_inode_info *oi = OCFS2_I(inode);
102
Tao Ma468eedd2011-02-22 22:14:41 +0800103 trace_ocfs2_file_open(inode, file, file->f_path.dentry,
104 (unsigned long long)OCFS2_I(inode)->ip_blkno,
105 file->f_path.dentry->d_name.len,
106 file->f_path.dentry->d_name.name, mode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800107
Christoph Hellwig907f4552010-03-03 09:05:06 -0500108 if (file->f_mode & FMODE_WRITE)
Christoph Hellwig871a2932010-03-03 09:05:07 -0500109 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500110
Mark Fashehccd979b2005-12-15 14:31:24 -0800111 spin_lock(&oi->ip_lock);
112
113 /* Check that the inode hasn't been wiped from disk by another
114 * node. If it hasn't then we're safe as long as we hold the
115 * spin lock until our increment of open count. */
116 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
117 spin_unlock(&oi->ip_lock);
118
119 status = -ENOENT;
120 goto leave;
121 }
122
123 if (mode & O_DIRECT)
124 oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
125
126 oi->ip_open_count++;
127 spin_unlock(&oi->ip_lock);
Mark Fasheh53fc6222007-12-20 16:49:04 -0800128
129 status = ocfs2_init_file_private(inode, file);
130 if (status) {
131 /*
132 * We want to set open count back if we're failing the
133 * open.
134 */
135 spin_lock(&oi->ip_lock);
136 oi->ip_open_count--;
137 spin_unlock(&oi->ip_lock);
138 }
139
Mark Fashehccd979b2005-12-15 14:31:24 -0800140leave:
Mark Fashehccd979b2005-12-15 14:31:24 -0800141 return status;
142}
143
144static int ocfs2_file_release(struct inode *inode, struct file *file)
145{
146 struct ocfs2_inode_info *oi = OCFS2_I(inode);
147
Mark Fashehccd979b2005-12-15 14:31:24 -0800148 spin_lock(&oi->ip_lock);
149 if (!--oi->ip_open_count)
150 oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
Tao Ma468eedd2011-02-22 22:14:41 +0800151
152 trace_ocfs2_file_release(inode, file, file->f_path.dentry,
153 oi->ip_blkno,
154 file->f_path.dentry->d_name.len,
155 file->f_path.dentry->d_name.name,
156 oi->ip_open_count);
Mark Fashehccd979b2005-12-15 14:31:24 -0800157 spin_unlock(&oi->ip_lock);
158
Mark Fasheh53fc6222007-12-20 16:49:04 -0800159 ocfs2_free_file_private(inode, file);
160
Mark Fashehccd979b2005-12-15 14:31:24 -0800161 return 0;
162}
163
Mark Fasheh53fc6222007-12-20 16:49:04 -0800164static int ocfs2_dir_open(struct inode *inode, struct file *file)
165{
166 return ocfs2_init_file_private(inode, file);
167}
168
169static int ocfs2_dir_release(struct inode *inode, struct file *file)
170{
171 ocfs2_free_file_private(inode, file);
172 return 0;
173}
174
Josef Bacik02c24a82011-07-16 20:44:56 -0400175static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
176 int datasync)
Mark Fashehccd979b2005-12-15 14:31:24 -0800177{
178 int err = 0;
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200179 struct inode *inode = file->f_mapping->host;
Mark Fashehccd979b2005-12-15 14:31:24 -0800180 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Darrick J. Wong2931cdc2014-04-03 14:46:48 -0700181 struct ocfs2_inode_info *oi = OCFS2_I(inode);
182 journal_t *journal = osb->journal->j_journal;
183 int ret;
184 tid_t commit_tid;
185 bool needs_barrier = false;
Mark Fashehccd979b2005-12-15 14:31:24 -0800186
Tao Ma468eedd2011-02-22 22:14:41 +0800187 trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
188 OCFS2_I(inode)->ip_blkno,
189 file->f_path.dentry->d_name.len,
190 file->f_path.dentry->d_name.name,
191 (unsigned long long)datasync);
Mark Fashehccd979b2005-12-15 14:31:24 -0800192
Younger Liua987c7c2014-02-10 14:25:44 -0800193 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
194 return -EROFS;
195
Josef Bacik02c24a82011-07-16 20:44:56 -0400196 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
197 if (err)
198 return err;
199
Darrick J. Wong2931cdc2014-04-03 14:46:48 -0700200 commit_tid = datasync ? oi->i_datasync_tid : oi->i_sync_tid;
201 if (journal->j_flags & JBD2_BARRIER &&
202 !jbd2_trans_will_send_data_barrier(journal, commit_tid))
203 needs_barrier = true;
204 err = jbd2_complete_transaction(journal, commit_tid);
205 if (needs_barrier) {
206 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
207 if (!err)
208 err = ret;
Jan Kara04eda1a2010-08-05 20:32:45 +0200209 }
Hisashi Hifumie04cc152009-06-09 16:47:45 +0900210
Tao Mac1e8d352011-03-07 16:43:21 +0800211 if (err)
212 mlog_errno(err);
Mark Fashehccd979b2005-12-15 14:31:24 -0800213
214 return (err < 0) ? -EIO : 0;
215}
216
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800217int ocfs2_should_update_atime(struct inode *inode,
218 struct vfsmount *vfsmnt)
219{
220 struct timespec now;
221 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
222
223 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
224 return 0;
225
226 if ((inode->i_flags & S_NOATIME) ||
227 ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
228 return 0;
229
Mark Fasheh6c2aad02006-12-19 15:25:52 -0800230 /*
231 * We can be called with no vfsmnt structure - NFSD will
232 * sometimes do this.
233 *
234 * Note that our action here is different than touch_atime() -
235 * if we can't tell whether this is a noatime mount, then we
236 * don't know whether to trust the value of s_atime_quantum.
237 */
238 if (vfsmnt == NULL)
239 return 0;
240
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800241 if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
242 ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
243 return 0;
244
Mark Fasheh7e913c52006-12-13 00:34:35 -0800245 if (vfsmnt->mnt_flags & MNT_RELATIME) {
246 if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
247 (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
248 return 1;
249
250 return 0;
251 }
252
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800253 now = CURRENT_TIME;
254 if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
255 return 0;
256 else
257 return 1;
258}
259
260int ocfs2_update_inode_atime(struct inode *inode,
261 struct buffer_head *bh)
262{
263 int ret;
264 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
265 handle_t *handle;
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700266 struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800267
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800268 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Jan Karafa38e922008-10-20 19:23:51 +0200269 if (IS_ERR(handle)) {
270 ret = PTR_ERR(handle);
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800271 mlog_errno(ret);
272 goto out;
273 }
274
Joel Becker0cf2f762009-02-12 16:41:25 -0800275 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -0700276 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700277 if (ret) {
278 mlog_errno(ret);
279 goto out_commit;
280 }
281
282 /*
283 * Don't use ocfs2_mark_inode_dirty() here as we don't always
284 * have i_mutex to guard against concurrent changes to other
285 * inode fields.
286 */
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800287 inode->i_atime = CURRENT_TIME;
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700288 di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
289 di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700290 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Joel Beckerec20cec2010-03-19 14:13:52 -0700291 ocfs2_journal_dirty(handle, bh);
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800292
Mark Fashehc11e9fa2007-07-20 11:24:53 -0700293out_commit:
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800294 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
295out:
Tiger Yang7f1a37e2006-11-15 15:48:42 +0800296 return ret;
297}
298
Joseph Qi026749a2015-02-16 15:59:50 -0800299int ocfs2_set_inode_size(handle_t *handle,
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700300 struct inode *inode,
301 struct buffer_head *fe_bh,
302 u64 new_i_size)
Mark Fashehccd979b2005-12-15 14:31:24 -0800303{
304 int status;
305
Mark Fashehccd979b2005-12-15 14:31:24 -0800306 i_size_write(inode, new_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -0700307 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800308 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
309
310 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
311 if (status < 0) {
312 mlog_errno(status);
313 goto bail;
314 }
315
316bail:
Mark Fashehccd979b2005-12-15 14:31:24 -0800317 return status;
318}
319
Jan Kara9e33d692008-08-25 19:56:50 +0200320int ocfs2_simple_size_update(struct inode *inode,
321 struct buffer_head *di_bh,
322 u64 new_i_size)
Mark Fashehccd979b2005-12-15 14:31:24 -0800323{
324 int ret;
325 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700326 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800327
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700328 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Jan Karafa38e922008-10-20 19:23:51 +0200329 if (IS_ERR(handle)) {
330 ret = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800331 mlog_errno(ret);
332 goto out;
333 }
334
335 ret = ocfs2_set_inode_size(handle, inode, di_bh,
336 new_i_size);
337 if (ret < 0)
338 mlog_errno(ret);
339
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700340 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700341 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800342out:
343 return ret;
344}
345
Tao Ma37f8a2b2009-08-26 09:47:28 +0800346static int ocfs2_cow_file_pos(struct inode *inode,
347 struct buffer_head *fe_bh,
348 u64 offset)
349{
350 int status;
351 u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
352 unsigned int num_clusters = 0;
353 unsigned int ext_flags = 0;
354
355 /*
356 * If the new offset is aligned to the range of the cluster, there is
357 * no space for ocfs2_zero_range_for_truncate to fill, so no need to
358 * CoW either.
359 */
360 if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
361 return 0;
362
363 status = ocfs2_get_clusters(inode, cpos, &phys,
364 &num_clusters, &ext_flags);
365 if (status) {
366 mlog_errno(status);
367 goto out;
368 }
369
370 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
371 goto out;
372
Tiger Yangc7dd3392013-08-13 16:00:58 -0700373 return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
Tao Ma37f8a2b2009-08-26 09:47:28 +0800374
375out:
376 return status;
377}
378
Mark Fashehccd979b2005-12-15 14:31:24 -0800379static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
380 struct inode *inode,
381 struct buffer_head *fe_bh,
382 u64 new_i_size)
383{
384 int status;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700385 handle_t *handle;
Mark Fasheh60b11392007-02-16 11:46:50 -0800386 struct ocfs2_dinode *di;
Mark Fasheh35edec12007-07-06 14:41:18 -0700387 u64 cluster_bytes;
Mark Fashehccd979b2005-12-15 14:31:24 -0800388
Tao Ma37f8a2b2009-08-26 09:47:28 +0800389 /*
390 * We need to CoW the cluster contains the offset if it is reflinked
391 * since we will call ocfs2_zero_range_for_truncate later which will
392 * write "0" from offset to the end of the cluster.
393 */
394 status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
395 if (status) {
396 mlog_errno(status);
397 return status;
398 }
399
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 /* TODO: This needs to actually orphan the inode in this
401 * transaction. */
402
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700403 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800404 if (IS_ERR(handle)) {
405 status = PTR_ERR(handle);
406 mlog_errno(status);
407 goto out;
408 }
409
Joel Becker0cf2f762009-02-12 16:41:25 -0800410 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700411 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -0800412 if (status < 0) {
413 mlog_errno(status);
414 goto out_commit;
415 }
416
417 /*
418 * Do this before setting i_size.
419 */
Mark Fasheh35edec12007-07-06 14:41:18 -0700420 cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
421 status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
422 cluster_bytes);
Mark Fasheh60b11392007-02-16 11:46:50 -0800423 if (status) {
424 mlog_errno(status);
425 goto out_commit;
426 }
427
428 i_size_write(inode, new_i_size);
Mark Fasheh60b11392007-02-16 11:46:50 -0800429 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
430
431 di = (struct ocfs2_dinode *) fe_bh->b_data;
432 di->i_size = cpu_to_le64(new_i_size);
433 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
434 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700435 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Mark Fasheh60b11392007-02-16 11:46:50 -0800436
Joel Beckerec20cec2010-03-19 14:13:52 -0700437 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800438
Mark Fasheh60b11392007-02-16 11:46:50 -0800439out_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700440 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800441out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800442 return status;
443}
444
Joseph Qi026749a2015-02-16 15:59:50 -0800445int ocfs2_truncate_file(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800446 struct buffer_head *di_bh,
447 u64 new_i_size)
448{
449 int status = 0;
450 struct ocfs2_dinode *fe = NULL;
451 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800452
Joel Beckerb657c952008-11-13 14:49:11 -0800453 /* We trust di_bh because it comes from ocfs2_inode_lock(), which
454 * already validated it */
Mark Fashehccd979b2005-12-15 14:31:24 -0800455 fe = (struct ocfs2_dinode *) di_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800456
Tao Ma468eedd2011-02-22 22:14:41 +0800457 trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno,
458 (unsigned long long)le64_to_cpu(fe->i_size),
459 (unsigned long long)new_i_size);
460
Mark Fashehccd979b2005-12-15 14:31:24 -0800461 mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
Mark Fashehb06970532006-03-03 10:24:33 -0800462 "Inode %llu, inode i_size = %lld != di "
463 "i_size = %llu, i_flags = 0x%x\n",
464 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800465 i_size_read(inode),
Mark Fashehb06970532006-03-03 10:24:33 -0800466 (unsigned long long)le64_to_cpu(fe->i_size),
467 le32_to_cpu(fe->i_flags));
Mark Fashehccd979b2005-12-15 14:31:24 -0800468
469 if (new_i_size > le64_to_cpu(fe->i_size)) {
Tao Ma468eedd2011-02-22 22:14:41 +0800470 trace_ocfs2_truncate_file_error(
471 (unsigned long long)le64_to_cpu(fe->i_size),
472 (unsigned long long)new_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800473 status = -EINVAL;
474 mlog_errno(status);
475 goto bail;
476 }
477
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700478 down_write(&OCFS2_I(inode)->ip_alloc_sem);
479
Mark Fasheh4fe370a2009-12-07 13:15:40 -0800480 ocfs2_resv_discard(&osb->osb_la_resmap,
481 &OCFS2_I(inode)->ip_la_data_resv);
482
Mark Fashehc934a922007-10-18 15:23:46 -0700483 /*
484 * The inode lock forced other nodes to sync and drop their
485 * pages, which (correctly) happens even if we have a truncate
486 * without allocation change - ocfs2 cluster sizes can be much
487 * greater than page size, so we have to truncate them
488 * anyway.
489 */
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700490 unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
491 truncate_inode_pages(inode->i_mapping, new_i_size);
492
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700493 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
494 status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
Mark Fashehb1967d02007-11-20 11:56:39 -0800495 i_size_read(inode), 1);
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700496 if (status)
497 mlog_errno(status);
498
Mark Fashehc934a922007-10-18 15:23:46 -0700499 goto bail_unlock_sem;
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700500 }
501
Mark Fashehccd979b2005-12-15 14:31:24 -0800502 /* alright, we're going to need to do a full blown alloc size
503 * change. Orphan the inode so that recovery can complete the
504 * truncate if necessary. This does the task of marking
505 * i_size. */
506 status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
507 if (status < 0) {
508 mlog_errno(status);
Mark Fashehc934a922007-10-18 15:23:46 -0700509 goto bail_unlock_sem;
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 }
511
Tristan Ye78f94672010-05-11 17:54:42 +0800512 status = ocfs2_commit_truncate(osb, inode, di_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 if (status < 0) {
514 mlog_errno(status);
Mark Fashehc934a922007-10-18 15:23:46 -0700515 goto bail_unlock_sem;
Mark Fashehccd979b2005-12-15 14:31:24 -0800516 }
517
518 /* TODO: orphan dir cleanup here. */
Mark Fashehc934a922007-10-18 15:23:46 -0700519bail_unlock_sem:
Mark Fasheh2e89b2e2007-05-09 13:40:18 -0700520 up_write(&OCFS2_I(inode)->ip_alloc_sem);
521
Mark Fashehccd979b2005-12-15 14:31:24 -0800522bail:
Tao Ma8b2c0db2009-08-18 11:43:49 +0800523 if (!status && OCFS2_I(inode)->ip_clusters == 0)
524 status = ocfs2_try_remove_refcount_tree(inode, di_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800525
Mark Fashehccd979b2005-12-15 14:31:24 -0800526 return status;
527}
528
529/*
Tao Ma0eb8d472008-08-18 17:38:45 +0800530 * extend file allocation only here.
Mark Fashehccd979b2005-12-15 14:31:24 -0800531 * we'll update all the disk stuff, and oip->alloc_size
532 *
533 * expect stuff to be locked, a transaction started and enough data /
534 * metadata reservations in the contexts.
535 *
536 * Will return -EAGAIN, and a reason if a restart is needed.
537 * If passed in, *reason will always be set, even in error.
538 */
Tao Ma0eb8d472008-08-18 17:38:45 +0800539int ocfs2_add_inode_data(struct ocfs2_super *osb,
540 struct inode *inode,
541 u32 *logical_offset,
542 u32 clusters_to_add,
543 int mark_unwritten,
544 struct buffer_head *fe_bh,
545 handle_t *handle,
546 struct ocfs2_alloc_context *data_ac,
547 struct ocfs2_alloc_context *meta_ac,
548 enum ocfs2_alloc_restarted *reason_ret)
Mark Fashehccd979b2005-12-15 14:31:24 -0800549{
Joel Beckerf99b9b72008-08-20 19:36:33 -0700550 int ret;
551 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -0800552
Joel Becker5e404e92009-02-13 03:54:22 -0800553 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
Joel Beckercbee7e12009-02-13 03:34:15 -0800554 ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
555 clusters_to_add, mark_unwritten,
556 data_ac, meta_ac, reason_ret);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700557
558 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800559}
560
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800561static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
562 u32 clusters_to_add, int mark_unwritten)
Mark Fashehccd979b2005-12-15 14:31:24 -0800563{
564 int status = 0;
565 int restart_func = 0;
Mark Fashehabf8b152007-01-17 13:07:24 -0800566 int credits;
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800567 u32 prev_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -0800568 struct buffer_head *bh = NULL;
569 struct ocfs2_dinode *fe = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700570 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800571 struct ocfs2_alloc_context *data_ac = NULL;
572 struct ocfs2_alloc_context *meta_ac = NULL;
Junxiao Bi696cdf72015-02-10 14:08:46 -0800573 enum ocfs2_alloc_restarted why = RESTART_NONE;
Mark Fashehccd979b2005-12-15 14:31:24 -0800574 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700575 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +0200576 int did_quota = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800577
Mark Fashehdcd05382007-01-16 11:32:23 -0800578 /*
Junxiao Bif0cb0f02013-11-12 15:06:53 -0800579 * Unwritten extent only exists for file systems which
Mark Fashehdcd05382007-01-16 11:32:23 -0800580 * support holes.
581 */
Mark Fasheh2ae99a62007-03-09 16:43:28 -0800582 BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
Mark Fashehdcd05382007-01-16 11:32:23 -0800583
Joel Beckerb657c952008-11-13 14:49:11 -0800584 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800585 if (status < 0) {
586 mlog_errno(status);
587 goto leave;
588 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800589 fe = (struct ocfs2_dinode *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800590
591restart_all:
592 BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
593
Joel Becker5e404e92009-02-13 03:54:22 -0800594 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700595 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
596 &data_ac, &meta_ac);
Mark Fasheh9517bac2007-02-09 20:24:12 -0800597 if (status) {
598 mlog_errno(status);
599 goto leave;
600 }
601
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -0800602 credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list);
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700603 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800604 if (IS_ERR(handle)) {
605 status = PTR_ERR(handle);
606 handle = NULL;
607 mlog_errno(status);
608 goto leave;
609 }
610
611restarted_transaction:
Tao Ma468eedd2011-02-22 22:14:41 +0800612 trace_ocfs2_extend_allocation(
613 (unsigned long long)OCFS2_I(inode)->ip_blkno,
614 (unsigned long long)i_size_read(inode),
615 le32_to_cpu(fe->i_clusters), clusters_to_add,
616 why, restart_func);
617
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500618 status = dquot_alloc_space_nodirty(inode,
619 ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
620 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +0200621 goto leave;
Jan Karaa90714c2008-10-09 19:38:40 +0200622 did_quota = 1;
623
Mark Fashehccd979b2005-12-15 14:31:24 -0800624 /* reserve a write to the file entry early on - that we if we
625 * run out of credits in the allocation path, we can still
626 * update i_size. */
Joel Becker0cf2f762009-02-12 16:41:25 -0800627 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -0700628 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800629 if (status < 0) {
630 mlog_errno(status);
631 goto leave;
632 }
633
634 prev_clusters = OCFS2_I(inode)->ip_clusters;
635
Tao Ma0eb8d472008-08-18 17:38:45 +0800636 status = ocfs2_add_inode_data(osb,
637 inode,
638 &logical_start,
639 clusters_to_add,
640 mark_unwritten,
641 bh,
642 handle,
643 data_ac,
644 meta_ac,
645 &why);
Mark Fashehccd979b2005-12-15 14:31:24 -0800646 if ((status < 0) && (status != -EAGAIN)) {
647 if (status != -ENOSPC)
648 mlog_errno(status);
649 goto leave;
650 }
Darrick J. Wong2931cdc2014-04-03 14:46:48 -0700651 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Joel Beckerec20cec2010-03-19 14:13:52 -0700652 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800653
654 spin_lock(&OCFS2_I(inode)->ip_lock);
655 clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
656 spin_unlock(&OCFS2_I(inode)->ip_lock);
Jan Karaa90714c2008-10-09 19:38:40 +0200657 /* Release unused quota reservation */
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500658 dquot_free_space(inode,
Jan Karaa90714c2008-10-09 19:38:40 +0200659 ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
660 did_quota = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800661
662 if (why != RESTART_NONE && clusters_to_add) {
663 if (why == RESTART_META) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800664 restart_func = 1;
Tao Ma79681842010-04-16 13:59:25 +0800665 status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800666 } else {
667 BUG_ON(why != RESTART_TRANS);
668
Younger Liu2b1e55c2013-09-11 14:19:44 -0700669 status = ocfs2_allocate_extend_trans(handle, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800670 if (status < 0) {
671 /* handle still has to be committed at
672 * this point. */
673 status = -ENOMEM;
674 mlog_errno(status);
675 goto leave;
676 }
677 goto restarted_transaction;
678 }
679 }
680
Tao Ma468eedd2011-02-22 22:14:41 +0800681 trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700682 le32_to_cpu(fe->i_clusters),
Tao Ma468eedd2011-02-22 22:14:41 +0800683 (unsigned long long)le64_to_cpu(fe->i_size),
684 OCFS2_I(inode)->ip_clusters,
685 (unsigned long long)i_size_read(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -0800686
687leave:
Jan Karaa90714c2008-10-09 19:38:40 +0200688 if (status < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500689 dquot_free_space(inode,
Jan Karaa90714c2008-10-09 19:38:40 +0200690 ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 if (handle) {
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700692 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800693 handle = NULL;
694 }
695 if (data_ac) {
696 ocfs2_free_alloc_context(data_ac);
697 data_ac = NULL;
698 }
699 if (meta_ac) {
700 ocfs2_free_alloc_context(meta_ac);
701 meta_ac = NULL;
702 }
703 if ((!status) && restart_func) {
704 restart_func = 0;
705 goto restart_all;
706 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700707 brelse(bh);
708 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800709
Mark Fashehccd979b2005-12-15 14:31:24 -0800710 return status;
711}
712
Joseph Qi026749a2015-02-16 15:59:50 -0800713int ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
714 u32 clusters_to_add, int mark_unwritten)
715{
716 return __ocfs2_extend_allocation(inode, logical_start,
717 clusters_to_add, mark_unwritten);
718}
719
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700720/*
721 * While a write will already be ordering the data, a truncate will not.
722 * Thus, we need to explicitly order the zeroed pages.
723 */
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800724static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode,
725 struct buffer_head *di_bh)
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700726{
727 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
728 handle_t *handle = NULL;
729 int ret = 0;
730
731 if (!ocfs2_should_order_data(inode))
732 goto out;
733
734 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
735 if (IS_ERR(handle)) {
736 ret = -ENOMEM;
737 mlog_errno(ret);
738 goto out;
739 }
740
741 ret = ocfs2_jbd2_file_inode(handle, inode);
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800742 if (ret < 0) {
743 mlog_errno(ret);
744 goto out;
745 }
746
747 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
748 OCFS2_JOURNAL_ACCESS_WRITE);
749 if (ret)
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700750 mlog_errno(ret);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700751 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700752
753out:
754 if (ret) {
755 if (!IS_ERR(handle))
756 ocfs2_commit_trans(osb, handle);
757 handle = ERR_PTR(ret);
758 }
759 return handle;
760}
761
Mark Fashehccd979b2005-12-15 14:31:24 -0800762/* Some parts of this taken from generic_cont_expand, which turned out
763 * to be too fragile to do exactly what we need without us having to
Nick Piggin4e02ed42008-10-29 14:00:55 -0700764 * worry about recursive locking in ->write_begin() and ->write_end(). */
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700765static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800766 u64 abs_to, struct buffer_head *di_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -0800767{
768 struct address_space *mapping = inode->i_mapping;
769 struct page *page;
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700770 unsigned long index = abs_from >> PAGE_CACHE_SHIFT;
Junxiao Bif775da22014-10-09 15:25:15 -0700771 handle_t *handle;
Joel Becker54532582010-07-16 13:32:33 -0700772 int ret = 0;
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700773 unsigned zero_from, zero_to, block_start, block_end;
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800774 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800775
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700776 BUG_ON(abs_from >= abs_to);
777 BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT));
778 BUG_ON(abs_from & (inode->i_blkbits - 1));
Mark Fashehccd979b2005-12-15 14:31:24 -0800779
Junxiao Bif775da22014-10-09 15:25:15 -0700780 handle = ocfs2_zero_start_ordered_transaction(inode, di_bh);
781 if (IS_ERR(handle)) {
782 ret = PTR_ERR(handle);
783 goto out;
784 }
785
Jan Kara9b4c0ff2010-08-24 14:28:03 +0200786 page = find_or_create_page(mapping, index, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800787 if (!page) {
788 ret = -ENOMEM;
789 mlog_errno(ret);
Junxiao Bif775da22014-10-09 15:25:15 -0700790 goto out_commit_trans;
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 }
792
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700793 /* Get the offsets within the page that we want to zero */
794 zero_from = abs_from & (PAGE_CACHE_SIZE - 1);
795 zero_to = abs_to & (PAGE_CACHE_SIZE - 1);
796 if (!zero_to)
797 zero_to = PAGE_CACHE_SIZE;
Mark Fashehccd979b2005-12-15 14:31:24 -0800798
Tao Ma468eedd2011-02-22 22:14:41 +0800799 trace_ocfs2_write_zero_page(
800 (unsigned long long)OCFS2_I(inode)->ip_blkno,
801 (unsigned long long)abs_from,
802 (unsigned long long)abs_to,
803 index, zero_from, zero_to);
Joel Becker56934862010-07-01 15:13:31 -0700804
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700805 /* We know that zero_from is block aligned */
806 for (block_start = zero_from; block_start < zero_to;
807 block_start = block_end) {
808 block_end = block_start + (1 << inode->i_blkbits);
809
810 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200811 * block_start is block-aligned. Bump it by one to force
812 * __block_write_begin and block_commit_write to zero the
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700813 * whole block.
814 */
Christoph Hellwigebdec242010-10-06 10:47:23 +0200815 ret = __block_write_begin(page, block_start + 1, 0,
816 ocfs2_get_block);
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700817 if (ret < 0) {
818 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800819 goto out_unlock;
820 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800821
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700822
823 /* must not update i_size! */
824 ret = block_commit_write(page, block_start + 1,
825 block_start + 1);
826 if (ret < 0)
827 mlog_errno(ret);
828 else
829 ret = 0;
830 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800831
Junxiao Bif775da22014-10-09 15:25:15 -0700832 /*
833 * fs-writeback will release the dirty pages without page lock
834 * whose offset are over inode size, the release happens at
835 * block_write_full_page().
836 */
837 i_size_write(inode, abs_to);
838 inode->i_blocks = ocfs2_inode_sector_count(inode);
839 di->i_size = cpu_to_le64((u64)i_size_read(inode));
840 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
841 di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
842 di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
843 di->i_mtime_nsec = di->i_ctime_nsec;
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800844 if (handle) {
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800845 ocfs2_journal_dirty(handle, di_bh);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -0700846 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800847 }
Joel Beckera4bfb4c2010-07-06 14:36:06 -0700848
Mark Fashehccd979b2005-12-15 14:31:24 -0800849out_unlock:
850 unlock_page(page);
851 page_cache_release(page);
Junxiao Bif775da22014-10-09 15:25:15 -0700852out_commit_trans:
853 if (handle)
854 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800855out:
856 return ret;
857}
858
Joel Becker56934862010-07-01 15:13:31 -0700859/*
860 * Find the next range to zero. We do this in terms of bytes because
861 * that's what ocfs2_zero_extend() wants, and it is dealing with the
862 * pagecache. We may return multiple extents.
863 *
864 * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
865 * needs to be zeroed. range_start and range_end return the next zeroing
866 * range. A subsequent call should pass the previous range_end as its
867 * zero_start. If range_end is 0, there's nothing to do.
868 *
869 * Unwritten extents are skipped over. Refcounted extents are CoWd.
870 */
871static int ocfs2_zero_extend_get_range(struct inode *inode,
872 struct buffer_head *di_bh,
873 u64 zero_start, u64 zero_end,
874 u64 *range_start, u64 *range_end)
Mark Fashehccd979b2005-12-15 14:31:24 -0800875{
Joel Becker56934862010-07-01 15:13:31 -0700876 int rc = 0, needs_cow = 0;
877 u32 p_cpos, zero_clusters = 0;
878 u32 zero_cpos =
879 zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
880 u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
881 unsigned int num_clusters = 0;
882 unsigned int ext_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800883
Joel Becker56934862010-07-01 15:13:31 -0700884 while (zero_cpos < last_cpos) {
885 rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
886 &num_clusters, &ext_flags);
887 if (rc) {
888 mlog_errno(rc);
Mark Fashehccd979b2005-12-15 14:31:24 -0800889 goto out;
890 }
891
Joel Becker56934862010-07-01 15:13:31 -0700892 if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
893 zero_clusters = num_clusters;
894 if (ext_flags & OCFS2_EXT_REFCOUNTED)
895 needs_cow = 1;
896 break;
897 }
898
899 zero_cpos += num_clusters;
900 }
901 if (!zero_clusters) {
902 *range_end = 0;
903 goto out;
904 }
905
906 while ((zero_cpos + zero_clusters) < last_cpos) {
907 rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
908 &p_cpos, &num_clusters,
909 &ext_flags);
910 if (rc) {
911 mlog_errno(rc);
912 goto out;
913 }
914
915 if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
916 break;
917 if (ext_flags & OCFS2_EXT_REFCOUNTED)
918 needs_cow = 1;
919 zero_clusters += num_clusters;
920 }
921 if ((zero_cpos + zero_clusters) > last_cpos)
922 zero_clusters = last_cpos - zero_cpos;
923
924 if (needs_cow) {
Tiger Yangc7dd3392013-08-13 16:00:58 -0700925 rc = ocfs2_refcount_cow(inode, di_bh, zero_cpos,
Tao Ma15502712010-08-12 10:36:38 +0800926 zero_clusters, UINT_MAX);
Joel Becker56934862010-07-01 15:13:31 -0700927 if (rc) {
928 mlog_errno(rc);
929 goto out;
930 }
931 }
932
933 *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
934 *range_end = ocfs2_clusters_to_bytes(inode->i_sb,
935 zero_cpos + zero_clusters);
936
937out:
938 return rc;
939}
940
941/*
942 * Zero one range returned from ocfs2_zero_extend_get_range(). The caller
943 * has made sure that the entire range needs zeroing.
944 */
945static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800946 u64 range_end, struct buffer_head *di_bh)
Joel Becker56934862010-07-01 15:13:31 -0700947{
948 int rc = 0;
949 u64 next_pos;
950 u64 zero_pos = range_start;
951
Tao Ma468eedd2011-02-22 22:14:41 +0800952 trace_ocfs2_zero_extend_range(
953 (unsigned long long)OCFS2_I(inode)->ip_blkno,
954 (unsigned long long)range_start,
955 (unsigned long long)range_end);
Joel Becker56934862010-07-01 15:13:31 -0700956 BUG_ON(range_start >= range_end);
957
958 while (zero_pos < range_end) {
959 next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE;
960 if (next_pos > range_end)
961 next_pos = range_end;
Junxiao Bic7d2cbc2014-02-10 14:25:53 -0800962 rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh);
Joel Becker56934862010-07-01 15:13:31 -0700963 if (rc < 0) {
964 mlog_errno(rc);
965 break;
966 }
967 zero_pos = next_pos;
Mark Fashehe2057c52006-10-03 17:53:05 -0700968
969 /*
970 * Very large extends have the potential to lock up
971 * the cpu for extended periods of time.
972 */
973 cond_resched();
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 }
975
Joel Becker56934862010-07-01 15:13:31 -0700976 return rc;
977}
978
979int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
980 loff_t zero_to_size)
981{
982 int ret = 0;
983 u64 zero_start, range_start = 0, range_end = 0;
984 struct super_block *sb = inode->i_sb;
985
986 zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
Tao Ma468eedd2011-02-22 22:14:41 +0800987 trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
988 (unsigned long long)zero_start,
989 (unsigned long long)i_size_read(inode));
Joel Becker56934862010-07-01 15:13:31 -0700990 while (zero_start < zero_to_size) {
991 ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
992 zero_to_size,
993 &range_start,
994 &range_end);
995 if (ret) {
996 mlog_errno(ret);
997 break;
998 }
999 if (!range_end)
1000 break;
1001 /* Trim the ends */
1002 if (range_start < zero_start)
1003 range_start = zero_start;
1004 if (range_end > zero_to_size)
1005 range_end = zero_to_size;
1006
1007 ret = ocfs2_zero_extend_range(inode, range_start,
Junxiao Bic7d2cbc2014-02-10 14:25:53 -08001008 range_end, di_bh);
Joel Becker56934862010-07-01 15:13:31 -07001009 if (ret) {
1010 mlog_errno(ret);
1011 break;
1012 }
1013 zero_start = range_end;
1014 }
1015
Mark Fashehccd979b2005-12-15 14:31:24 -08001016 return ret;
1017}
1018
Joel Becker56934862010-07-01 15:13:31 -07001019int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
1020 u64 new_i_size, u64 zero_to)
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001021{
1022 int ret;
1023 u32 clusters_to_add;
1024 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1025
Joel Becker56934862010-07-01 15:13:31 -07001026 /*
1027 * Only quota files call this without a bh, and they can't be
1028 * refcounted.
1029 */
1030 BUG_ON(!di_bh && (oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
1031 BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
1032
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001033 clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
1034 if (clusters_to_add < oi->ip_clusters)
1035 clusters_to_add = 0;
1036 else
1037 clusters_to_add -= oi->ip_clusters;
1038
1039 if (clusters_to_add) {
1040 ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
1041 clusters_to_add, 0);
1042 if (ret) {
1043 mlog_errno(ret);
1044 goto out;
1045 }
1046 }
1047
1048 /*
1049 * Call this even if we don't add any clusters to the tree. We
1050 * still need to zero the area between the old i_size and the
1051 * new i_size.
1052 */
Joel Becker56934862010-07-01 15:13:31 -07001053 ret = ocfs2_zero_extend(inode, di_bh, zero_to);
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001054 if (ret < 0)
1055 mlog_errno(ret);
1056
1057out:
1058 return ret;
1059}
1060
Mark Fashehccd979b2005-12-15 14:31:24 -08001061static int ocfs2_extend_file(struct inode *inode,
1062 struct buffer_head *di_bh,
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001063 u64 new_i_size)
Mark Fashehccd979b2005-12-15 14:31:24 -08001064{
Mark Fashehc934a922007-10-18 15:23:46 -07001065 int ret = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001066 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001067
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001068 BUG_ON(!di_bh);
Mark Fasheh53013cb2006-05-05 19:04:03 -07001069
Mark Fashehccd979b2005-12-15 14:31:24 -08001070 /* setattr sometimes calls us like this. */
1071 if (new_i_size == 0)
1072 goto out;
1073
1074 if (i_size_read(inode) == new_i_size)
Joel Becker56934862010-07-01 15:13:31 -07001075 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08001076 BUG_ON(new_i_size < i_size_read(inode));
1077
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001078 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001079 * The alloc sem blocks people in read/write from reading our
1080 * allocation until we're done changing it. We depend on
1081 * i_mutex to block other extend/truncate calls while we're
Joel Becker56934862010-07-01 15:13:31 -07001082 * here. We even have to hold it for sparse files because there
1083 * might be some tail zeroing.
Mark Fasheh0effef72006-10-03 17:44:42 -07001084 */
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001085 down_write(&oi->ip_alloc_sem);
1086
1087 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1088 /*
1089 * We can optimize small extends by keeping the inodes
1090 * inline data.
1091 */
1092 if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
1093 up_write(&oi->ip_alloc_sem);
1094 goto out_update_size;
1095 }
1096
1097 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1098 if (ret) {
1099 up_write(&oi->ip_alloc_sem);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001100 mlog_errno(ret);
Mark Fashehc934a922007-10-18 15:23:46 -07001101 goto out;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001102 }
1103 }
1104
Joel Becker56934862010-07-01 15:13:31 -07001105 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
1106 ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
1107 else
1108 ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
1109 new_i_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001110
1111 up_write(&oi->ip_alloc_sem);
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001112
Mark Fasheh0effef72006-10-03 17:44:42 -07001113 if (ret < 0) {
1114 mlog_errno(ret);
Mark Fashehc934a922007-10-18 15:23:46 -07001115 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08001116 }
1117
Mark Fasheh3a0782d2007-01-17 12:53:31 -08001118out_update_size:
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001119 ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
1120 if (ret < 0)
1121 mlog_errno(ret);
Mark Fasheh53013cb2006-05-05 19:04:03 -07001122
Mark Fashehccd979b2005-12-15 14:31:24 -08001123out:
1124 return ret;
1125}
1126
1127int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
1128{
1129 int status = 0, size_change;
David Howells2b0143b2015-03-17 22:25:59 +00001130 struct inode *inode = d_inode(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001131 struct super_block *sb = inode->i_sb;
1132 struct ocfs2_super *osb = OCFS2_SB(sb);
1133 struct buffer_head *bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001134 handle_t *handle = NULL;
Jan Kara65bac572009-06-02 14:24:01 +02001135 struct dquot *transfer_to[MAXQUOTAS] = { };
Jan Kara52a9ee22010-05-13 20:18:45 +02001136 int qtype;
Mark Fashehccd979b2005-12-15 14:31:24 -08001137
Tao Ma468eedd2011-02-22 22:14:41 +08001138 trace_ocfs2_setattr(inode, dentry,
1139 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1140 dentry->d_name.len, dentry->d_name.name,
1141 attr->ia_valid, attr->ia_mode,
Eric W. Biedermanba613562013-01-31 17:33:53 -08001142 from_kuid(&init_user_ns, attr->ia_uid),
1143 from_kgid(&init_user_ns, attr->ia_gid));
Mark Fashehccd979b2005-12-15 14:31:24 -08001144
Sunil Mushranbc535802008-04-18 10:23:53 -07001145 /* ensuring we don't even attempt to truncate a symlink */
1146 if (S_ISLNK(inode->i_mode))
1147 attr->ia_valid &= ~ATTR_SIZE;
1148
Mark Fashehccd979b2005-12-15 14:31:24 -08001149#define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
1150 | ATTR_GID | ATTR_UID | ATTR_MODE)
Tao Ma468eedd2011-02-22 22:14:41 +08001151 if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001153
1154 status = inode_change_ok(inode, attr);
1155 if (status)
1156 return status;
1157
Dmitry Monakhov12755622010-04-08 22:04:20 +04001158 if (is_quota_modification(inode, attr))
1159 dquot_initialize(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001160 size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
1161 if (size_change) {
1162 status = ocfs2_rw_lock(inode, 1);
1163 if (status < 0) {
1164 mlog_errno(status);
1165 goto bail;
1166 }
1167 }
1168
Mark Fashehe63aecb62007-10-18 15:30:42 -07001169 status = ocfs2_inode_lock(inode, &bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001170 if (status < 0) {
1171 if (status != -ENOENT)
1172 mlog_errno(status);
1173 goto bail_unlock_rw;
1174 }
1175
Younger Liud62e74b2014-02-10 14:25:51 -08001176 if (size_change) {
Wengang Wang5051f762010-02-26 18:18:25 +08001177 status = inode_newsize_ok(inode, attr->ia_size);
1178 if (status)
Mark Fashehce76fd32007-07-20 12:02:14 -07001179 goto bail_unlock;
Mark Fashehce76fd32007-07-20 12:02:14 -07001180
Christoph Hellwig562c72a2011-06-24 14:29:45 -04001181 inode_dio_wait(inode);
1182
Younger Liud62e74b2014-02-10 14:25:51 -08001183 if (i_size_read(inode) >= attr->ia_size) {
Joel Becker2b4e30f2008-09-03 20:03:41 -07001184 if (ocfs2_should_order_data(inode)) {
1185 status = ocfs2_begin_ordered_truncate(inode,
1186 attr->ia_size);
1187 if (status)
1188 goto bail_unlock;
1189 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001190 status = ocfs2_truncate_file(inode, bh, attr->ia_size);
Joel Becker2b4e30f2008-09-03 20:03:41 -07001191 } else
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001192 status = ocfs2_extend_file(inode, bh, attr->ia_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001193 if (status < 0) {
1194 if (status != -ENOSPC)
1195 mlog_errno(status);
1196 status = -ENOSPC;
1197 goto bail_unlock;
1198 }
1199 }
1200
Eric W. Biederman488c8ef2013-01-31 17:35:29 -08001201 if ((attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
1202 (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Jan Kara65bac572009-06-02 14:24:01 +02001203 /*
1204 * Gather pointers to quota structures so that allocation /
1205 * freeing of quota structures happens here and not inside
Christoph Hellwigb43fa822010-03-03 09:05:03 -05001206 * dquot_transfer() where we have problems with lock ordering
Jan Kara65bac572009-06-02 14:24:01 +02001207 */
Eric W. Biederman488c8ef2013-01-31 17:35:29 -08001208 if (attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)
Jan Karaa90714c2008-10-09 19:38:40 +02001209 && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1210 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001211 transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid));
Jan Kara6184fc02015-06-24 18:07:02 +02001212 if (IS_ERR(transfer_to[USRQUOTA])) {
1213 status = PTR_ERR(transfer_to[USRQUOTA]);
Jan Karaa90714c2008-10-09 19:38:40 +02001214 goto bail_unlock;
Jan Kara65bac572009-06-02 14:24:01 +02001215 }
Jan Karaa90714c2008-10-09 19:38:40 +02001216 }
Eric W. Biederman488c8ef2013-01-31 17:35:29 -08001217 if (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid)
Jan Karaa90714c2008-10-09 19:38:40 +02001218 && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1219 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
Eric W. Biedermanaca645a2012-09-16 03:11:50 -07001220 transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid));
Jan Kara6184fc02015-06-24 18:07:02 +02001221 if (IS_ERR(transfer_to[GRPQUOTA])) {
1222 status = PTR_ERR(transfer_to[GRPQUOTA]);
Jan Karaa90714c2008-10-09 19:38:40 +02001223 goto bail_unlock;
Jan Kara65bac572009-06-02 14:24:01 +02001224 }
Jan Karaa90714c2008-10-09 19:38:40 +02001225 }
Jan Kara65bac572009-06-02 14:24:01 +02001226 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
1227 2 * ocfs2_quota_trans_credits(sb));
Jan Karaa90714c2008-10-09 19:38:40 +02001228 if (IS_ERR(handle)) {
1229 status = PTR_ERR(handle);
1230 mlog_errno(status);
1231 goto bail_unlock;
1232 }
Jan Kara52a9ee22010-05-13 20:18:45 +02001233 status = __dquot_transfer(inode, transfer_to);
Jan Karaa90714c2008-10-09 19:38:40 +02001234 if (status < 0)
1235 goto bail_commit;
1236 } else {
1237 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1238 if (IS_ERR(handle)) {
1239 status = PTR_ERR(handle);
1240 mlog_errno(status);
1241 goto bail_unlock;
1242 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001243 }
1244
Christoph Hellwig10257742010-06-04 11:30:02 +02001245 setattr_copy(inode, attr);
1246 mark_inode_dirty(inode);
1247
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 status = ocfs2_mark_inode_dirty(handle, inode, bh);
1249 if (status < 0)
1250 mlog_errno(status);
1251
1252bail_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001253 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001254bail_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001255 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001256bail_unlock_rw:
1257 if (size_change)
1258 ocfs2_rw_unlock(inode, 1);
1259bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001260 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001261
Jan Kara65bac572009-06-02 14:24:01 +02001262 /* Release quota pointers in case we acquired them */
Jan Kara52362812014-09-10 21:06:39 +02001263 for (qtype = 0; qtype < OCFS2_MAXQUOTAS; qtype++)
Jan Kara65bac572009-06-02 14:24:01 +02001264 dqput(transfer_to[qtype]);
Jan Kara65bac572009-06-02 14:24:01 +02001265
Tiger Yang060bc662008-11-14 11:17:29 +08001266 if (!status && attr->ia_valid & ATTR_MODE) {
Christoph Hellwig702e5bc2013-12-20 05:16:48 -08001267 status = posix_acl_chmod(inode, inode->i_mode);
Tiger Yang060bc662008-11-14 11:17:29 +08001268 if (status < 0)
1269 mlog_errno(status);
1270 }
1271
Mark Fashehccd979b2005-12-15 14:31:24 -08001272 return status;
1273}
1274
1275int ocfs2_getattr(struct vfsmount *mnt,
1276 struct dentry *dentry,
1277 struct kstat *stat)
1278{
David Howells2b0143b2015-03-17 22:25:59 +00001279 struct inode *inode = d_inode(dentry);
1280 struct super_block *sb = d_inode(dentry)->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 struct ocfs2_super *osb = sb->s_fs_info;
1282 int err;
1283
Mark Fashehccd979b2005-12-15 14:31:24 -08001284 err = ocfs2_inode_revalidate(dentry);
1285 if (err) {
1286 if (err != -ENOENT)
1287 mlog_errno(err);
1288 goto bail;
1289 }
1290
1291 generic_fillattr(inode, stat);
1292
1293 /* We set the blksize from the cluster size for performance */
1294 stat->blksize = osb->s_clustersize;
1295
1296bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001297 return err;
1298}
1299
Al Viro10556cb2011-06-20 19:28:19 -04001300int ocfs2_permission(struct inode *inode, int mask)
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001301{
1302 int ret;
1303
Al Viro10556cb2011-06-20 19:28:19 -04001304 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +11001305 return -ECHILD;
1306
Mark Fashehe63aecb62007-10-18 15:30:42 -07001307 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001308 if (ret) {
Mark Fasheha9f5f702007-04-26 11:43:43 -07001309 if (ret != -ENOENT)
1310 mlog_errno(ret);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001311 goto out;
1312 }
1313
Al Viro2830ba72011-06-20 19:16:29 -04001314 ret = generic_permission(inode, mask);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001315
Mark Fashehe63aecb62007-10-18 15:30:42 -07001316 ocfs2_inode_unlock(inode, 0);
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001317out:
Tiger Yangd38eb8d2006-11-27 09:59:21 +08001318 return ret;
1319}
1320
Mark Fashehb2580102007-03-09 16:53:21 -08001321static int __ocfs2_write_remove_suid(struct inode *inode,
1322 struct buffer_head *bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08001323{
1324 int ret;
Mark Fasheh1fabe142006-10-09 18:11:45 -07001325 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08001326 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1327 struct ocfs2_dinode *di;
1328
Tao Ma468eedd2011-02-22 22:14:41 +08001329 trace_ocfs2_write_remove_suid(
1330 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1331 inode->i_mode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001332
Mark Fasheh65eff9c2006-10-09 17:26:22 -07001333 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Jan Karafa38e922008-10-20 19:23:51 +02001334 if (IS_ERR(handle)) {
1335 ret = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001336 mlog_errno(ret);
1337 goto out;
1338 }
1339
Joel Becker0cf2f762009-02-12 16:41:25 -08001340 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -07001341 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001342 if (ret < 0) {
1343 mlog_errno(ret);
Mark Fashehb2580102007-03-09 16:53:21 -08001344 goto out_trans;
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 }
1346
1347 inode->i_mode &= ~S_ISUID;
1348 if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1349 inode->i_mode &= ~S_ISGID;
1350
1351 di = (struct ocfs2_dinode *) bh->b_data;
1352 di->i_mode = cpu_to_le16(inode->i_mode);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07001353 ocfs2_update_inode_fsync_trans(handle, inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001354
Joel Beckerec20cec2010-03-19 14:13:52 -07001355 ocfs2_journal_dirty(handle, bh);
Mark Fashehb2580102007-03-09 16:53:21 -08001356
Mark Fashehccd979b2005-12-15 14:31:24 -08001357out_trans:
Mark Fasheh02dc1af2006-10-09 16:48:10 -07001358 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08001359out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001360 return ret;
1361}
1362
Mark Fasheh9517bac2007-02-09 20:24:12 -08001363/*
1364 * Will look for holes and unwritten extents in the range starting at
1365 * pos for count bytes (inclusive).
1366 */
1367static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1368 size_t count)
1369{
1370 int ret = 0;
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001371 unsigned int extent_flags;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001372 u32 cpos, clusters, extent_len, phys_cpos;
1373 struct super_block *sb = inode->i_sb;
1374
1375 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
1376 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
1377
1378 while (clusters) {
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001379 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1380 &extent_flags);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001381 if (ret < 0) {
1382 mlog_errno(ret);
1383 goto out;
1384 }
1385
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001386 if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001387 ret = 1;
1388 break;
1389 }
1390
1391 if (extent_len > clusters)
1392 extent_len = clusters;
1393
1394 clusters -= extent_len;
1395 cpos += extent_len;
1396 }
1397out:
1398 return ret;
1399}
1400
Mark Fashehb2580102007-03-09 16:53:21 -08001401static int ocfs2_write_remove_suid(struct inode *inode)
1402{
1403 int ret;
1404 struct buffer_head *bh = NULL;
Mark Fashehb2580102007-03-09 16:53:21 -08001405
Joel Beckerb657c952008-11-13 14:49:11 -08001406 ret = ocfs2_read_inode_block(inode, &bh);
Mark Fashehb2580102007-03-09 16:53:21 -08001407 if (ret < 0) {
1408 mlog_errno(ret);
1409 goto out;
1410 }
1411
1412 ret = __ocfs2_write_remove_suid(inode, bh);
1413out:
1414 brelse(bh);
1415 return ret;
1416}
1417
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001418/*
1419 * Allocate enough extents to cover the region starting at byte offset
1420 * start for len bytes. Existing extents are skipped, any extents
1421 * added are marked as "unwritten".
1422 */
1423static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1424 u64 start, u64 len)
1425{
1426 int ret;
1427 u32 cpos, phys_cpos, clusters, alloc_size;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001428 u64 end = start + len;
1429 struct buffer_head *di_bh = NULL;
1430
1431 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
Joel Beckerb657c952008-11-13 14:49:11 -08001432 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001433 if (ret) {
1434 mlog_errno(ret);
1435 goto out;
1436 }
1437
1438 /*
1439 * Nothing to do if the requested reservation range
1440 * fits within the inode.
1441 */
1442 if (ocfs2_size_fits_inline_data(di_bh, end))
1443 goto out;
1444
1445 ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1446 if (ret) {
1447 mlog_errno(ret);
1448 goto out;
1449 }
1450 }
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001451
1452 /*
1453 * We consider both start and len to be inclusive.
1454 */
1455 cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
1456 clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
1457 clusters -= cpos;
1458
1459 while (clusters) {
1460 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1461 &alloc_size, NULL);
1462 if (ret) {
1463 mlog_errno(ret);
1464 goto out;
1465 }
1466
1467 /*
1468 * Hole or existing extent len can be arbitrary, so
1469 * cap it to our own allocation request.
1470 */
1471 if (alloc_size > clusters)
1472 alloc_size = clusters;
1473
1474 if (phys_cpos) {
1475 /*
1476 * We already have an allocation at this
1477 * region so we can safely skip it.
1478 */
1479 goto next;
1480 }
1481
1482 ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
1483 if (ret) {
1484 if (ret != -ENOSPC)
1485 mlog_errno(ret);
1486 goto out;
1487 }
1488
1489next:
1490 cpos += alloc_size;
1491 clusters -= alloc_size;
1492 }
1493
1494 ret = 0;
1495out:
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001496
1497 brelse(di_bh);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001498 return ret;
1499}
1500
Mark Fasheh063c4562007-07-03 13:34:11 -07001501/*
1502 * Truncate a byte range, avoiding pages within partial clusters. This
1503 * preserves those pages for the zeroing code to write to.
1504 */
1505static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
1506 u64 byte_len)
1507{
1508 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1509 loff_t start, end;
1510 struct address_space *mapping = inode->i_mapping;
1511
1512 start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
1513 end = byte_start + byte_len;
1514 end = end & ~(osb->s_clustersize - 1);
1515
1516 if (start < end) {
1517 unmap_mapping_range(mapping, start, end - start, 0);
1518 truncate_inode_pages_range(mapping, start, end - 1);
1519 }
1520}
1521
1522static int ocfs2_zero_partial_clusters(struct inode *inode,
1523 u64 start, u64 len)
1524{
1525 int ret = 0;
1526 u64 tmpend, end = start + len;
1527 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1528 unsigned int csize = osb->s_clustersize;
1529 handle_t *handle;
1530
1531 /*
1532 * The "start" and "end" values are NOT necessarily part of
1533 * the range whose allocation is being deleted. Rather, this
1534 * is what the user passed in with the request. We must zero
1535 * partial clusters here. There's no need to worry about
1536 * physical allocation - the zeroing code knows to skip holes.
1537 */
Tao Ma468eedd2011-02-22 22:14:41 +08001538 trace_ocfs2_zero_partial_clusters(
1539 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1540 (unsigned long long)start, (unsigned long long)end);
Mark Fasheh063c4562007-07-03 13:34:11 -07001541
1542 /*
1543 * If both edges are on a cluster boundary then there's no
1544 * zeroing required as the region is part of the allocation to
1545 * be truncated.
1546 */
1547 if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
1548 goto out;
1549
1550 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Jan Karafa38e922008-10-20 19:23:51 +02001551 if (IS_ERR(handle)) {
1552 ret = PTR_ERR(handle);
Mark Fasheh063c4562007-07-03 13:34:11 -07001553 mlog_errno(ret);
1554 goto out;
1555 }
1556
1557 /*
1558 * We want to get the byte offset of the end of the 1st cluster.
1559 */
1560 tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
1561 if (tmpend > end)
1562 tmpend = end;
1563
Tao Ma468eedd2011-02-22 22:14:41 +08001564 trace_ocfs2_zero_partial_clusters_range1((unsigned long long)start,
1565 (unsigned long long)tmpend);
Mark Fasheh063c4562007-07-03 13:34:11 -07001566
1567 ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
1568 if (ret)
1569 mlog_errno(ret);
1570
1571 if (tmpend < end) {
1572 /*
1573 * This may make start and end equal, but the zeroing
1574 * code will skip any work in that case so there's no
1575 * need to catch it up here.
1576 */
1577 start = end & ~(osb->s_clustersize - 1);
1578
Tao Ma468eedd2011-02-22 22:14:41 +08001579 trace_ocfs2_zero_partial_clusters_range2(
1580 (unsigned long long)start, (unsigned long long)end);
Mark Fasheh063c4562007-07-03 13:34:11 -07001581
1582 ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1583 if (ret)
1584 mlog_errno(ret);
1585 }
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07001586 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Mark Fasheh063c4562007-07-03 13:34:11 -07001587
1588 ocfs2_commit_trans(osb, handle);
1589out:
1590 return ret;
1591}
1592
Tristan Yec1631d42010-05-11 17:54:45 +08001593static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
1594{
1595 int i;
1596 struct ocfs2_extent_rec *rec = NULL;
1597
1598 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1599
1600 rec = &el->l_recs[i];
1601
1602 if (le32_to_cpu(rec->e_cpos) < pos)
1603 break;
1604 }
1605
1606 return i;
1607}
1608
1609/*
1610 * Helper to calculate the punching pos and length in one run, we handle the
1611 * following three cases in order:
1612 *
1613 * - remove the entire record
1614 * - remove a partial record
1615 * - no record needs to be removed (hole-punching completed)
1616*/
1617static void ocfs2_calc_trunc_pos(struct inode *inode,
1618 struct ocfs2_extent_list *el,
1619 struct ocfs2_extent_rec *rec,
1620 u32 trunc_start, u32 *trunc_cpos,
1621 u32 *trunc_len, u32 *trunc_end,
1622 u64 *blkno, int *done)
1623{
1624 int ret = 0;
1625 u32 coff, range;
1626
1627 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1628
1629 if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
Tristan Ye9a790ba2011-05-12 20:47:07 +08001630 /*
1631 * remove an entire extent record.
1632 */
Tristan Yec1631d42010-05-11 17:54:45 +08001633 *trunc_cpos = le32_to_cpu(rec->e_cpos);
1634 /*
1635 * Skip holes if any.
1636 */
1637 if (range < *trunc_end)
1638 *trunc_end = range;
1639 *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
1640 *blkno = le64_to_cpu(rec->e_blkno);
1641 *trunc_end = le32_to_cpu(rec->e_cpos);
1642 } else if (range > trunc_start) {
Tristan Ye9a790ba2011-05-12 20:47:07 +08001643 /*
1644 * remove a partial extent record, which means we're
1645 * removing the last extent record.
1646 */
Tristan Yec1631d42010-05-11 17:54:45 +08001647 *trunc_cpos = trunc_start;
Tristan Ye9a790ba2011-05-12 20:47:07 +08001648 /*
1649 * skip hole if any.
1650 */
1651 if (range < *trunc_end)
1652 *trunc_end = range;
Tristan Yec1631d42010-05-11 17:54:45 +08001653 *trunc_len = *trunc_end - trunc_start;
1654 coff = trunc_start - le32_to_cpu(rec->e_cpos);
1655 *blkno = le64_to_cpu(rec->e_blkno) +
1656 ocfs2_clusters_to_blocks(inode->i_sb, coff);
1657 *trunc_end = trunc_start;
1658 } else {
1659 /*
1660 * It may have two following possibilities:
1661 *
1662 * - last record has been removed
1663 * - trunc_start was within a hole
1664 *
1665 * both two cases mean the completion of hole punching.
1666 */
1667 ret = 1;
1668 }
1669
1670 *done = ret;
1671}
1672
Mark Fasheh063c4562007-07-03 13:34:11 -07001673static int ocfs2_remove_inode_range(struct inode *inode,
1674 struct buffer_head *di_bh, u64 byte_start,
1675 u64 byte_len)
1676{
Tristan Yec1631d42010-05-11 17:54:45 +08001677 int ret = 0, flags = 0, done = 0, i;
1678 u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
1679 u32 cluster_in_el;
Mark Fasheh063c4562007-07-03 13:34:11 -07001680 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1681 struct ocfs2_cached_dealloc_ctxt dealloc;
Mark Fashehb1967d02007-11-20 11:56:39 -08001682 struct address_space *mapping = inode->i_mapping;
Mark Fashehfecc0112008-11-12 15:16:38 -08001683 struct ocfs2_extent_tree et;
Tristan Yec1631d42010-05-11 17:54:45 +08001684 struct ocfs2_path *path = NULL;
1685 struct ocfs2_extent_list *el = NULL;
1686 struct ocfs2_extent_rec *rec = NULL;
Tristan Yee8aec062010-05-11 17:54:43 +08001687 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tristan Yec1631d42010-05-11 17:54:45 +08001688 u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
Mark Fasheh063c4562007-07-03 13:34:11 -07001689
Joel Becker5e404e92009-02-13 03:54:22 -08001690 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Mark Fasheh063c4562007-07-03 13:34:11 -07001691 ocfs2_init_dealloc_ctxt(&dealloc);
1692
Tao Ma468eedd2011-02-22 22:14:41 +08001693 trace_ocfs2_remove_inode_range(
1694 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1695 (unsigned long long)byte_start,
1696 (unsigned long long)byte_len);
1697
Mark Fasheh063c4562007-07-03 13:34:11 -07001698 if (byte_len == 0)
1699 return 0;
1700
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001701 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1702 ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
Mark Fashehb1967d02007-11-20 11:56:39 -08001703 byte_start + byte_len, 0);
1704 if (ret) {
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001705 mlog_errno(ret);
Mark Fashehb1967d02007-11-20 11:56:39 -08001706 goto out;
1707 }
1708 /*
1709 * There's no need to get fancy with the page cache
1710 * truncate of an inline-data inode. We're talking
1711 * about less than a page here, which will be cached
1712 * in the dinode buffer anyway.
1713 */
1714 unmap_mapping_range(mapping, 0, 0, 0);
1715 truncate_inode_pages(mapping, 0);
1716 goto out;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07001717 }
1718
Tristan Yee8aec062010-05-11 17:54:43 +08001719 /*
1720 * For reflinks, we may need to CoW 2 clusters which might be
1721 * partially zero'd later, if hole's start and end offset were
1722 * within one cluster(means is not exactly aligned to clustersize).
1723 */
Mark Fasheh063c4562007-07-03 13:34:11 -07001724
Tristan Yee8aec062010-05-11 17:54:43 +08001725 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
1726
1727 ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
1728 if (ret) {
1729 mlog_errno(ret);
1730 goto out;
1731 }
1732
1733 ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
1734 if (ret) {
1735 mlog_errno(ret);
1736 goto out;
1737 }
1738 }
1739
Mark Fasheh063c4562007-07-03 13:34:11 -07001740 trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
Tristan Yec1631d42010-05-11 17:54:45 +08001741 trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
1742 cluster_in_el = trunc_end;
Mark Fasheh063c4562007-07-03 13:34:11 -07001743
Mark Fasheh063c4562007-07-03 13:34:11 -07001744 ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1745 if (ret) {
1746 mlog_errno(ret);
1747 goto out;
1748 }
1749
Tristan Yec1631d42010-05-11 17:54:45 +08001750 path = ocfs2_new_path_from_et(&et);
1751 if (!path) {
1752 ret = -ENOMEM;
1753 mlog_errno(ret);
1754 goto out;
1755 }
1756
1757 while (trunc_end > trunc_start) {
1758
1759 ret = ocfs2_find_path(INODE_CACHE(inode), path,
1760 cluster_in_el);
Mark Fasheh063c4562007-07-03 13:34:11 -07001761 if (ret) {
1762 mlog_errno(ret);
1763 goto out;
1764 }
1765
Tristan Yec1631d42010-05-11 17:54:45 +08001766 el = path_leaf_el(path);
Mark Fasheh063c4562007-07-03 13:34:11 -07001767
Tristan Yec1631d42010-05-11 17:54:45 +08001768 i = ocfs2_find_rec(el, trunc_end);
1769 /*
1770 * Need to go to previous extent block.
1771 */
1772 if (i < 0) {
1773 if (path->p_tree_depth == 0)
1774 break;
1775
1776 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
1777 path,
1778 &cluster_in_el);
Mark Fasheh063c4562007-07-03 13:34:11 -07001779 if (ret) {
1780 mlog_errno(ret);
1781 goto out;
1782 }
Tristan Yec1631d42010-05-11 17:54:45 +08001783
1784 /*
1785 * We've reached the leftmost extent block,
1786 * it's safe to leave.
1787 */
1788 if (cluster_in_el == 0)
1789 break;
1790
1791 /*
1792 * The 'pos' searched for previous extent block is
1793 * always one cluster less than actual trunc_end.
1794 */
1795 trunc_end = cluster_in_el + 1;
1796
1797 ocfs2_reinit_path(path, 1);
1798
1799 continue;
1800
1801 } else
1802 rec = &el->l_recs[i];
1803
1804 ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
1805 &trunc_len, &trunc_end, &blkno, &done);
1806 if (done)
1807 break;
1808
1809 flags = rec->e_flags;
1810 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
1811
1812 ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
1813 phys_cpos, trunc_len, flags,
Junxiao Bif62f12b2014-12-18 16:17:32 -08001814 &dealloc, refcount_loc, false);
Tristan Yec1631d42010-05-11 17:54:45 +08001815 if (ret < 0) {
1816 mlog_errno(ret);
1817 goto out;
Mark Fasheh063c4562007-07-03 13:34:11 -07001818 }
1819
Tristan Yec1631d42010-05-11 17:54:45 +08001820 cluster_in_el = trunc_end;
1821
1822 ocfs2_reinit_path(path, 1);
Mark Fasheh063c4562007-07-03 13:34:11 -07001823 }
1824
1825 ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1826
1827out:
Younger Liu7aebff12013-09-11 14:19:59 -07001828 ocfs2_free_path(path);
Mark Fasheh063c4562007-07-03 13:34:11 -07001829 ocfs2_schedule_truncate_log_flush(osb, 1);
1830 ocfs2_run_deallocs(osb, &dealloc);
1831
1832 return ret;
1833}
1834
Mark Fashehb2580102007-03-09 16:53:21 -08001835/*
1836 * Parts of this function taken from xfs_change_file_space()
1837 */
Mark Fasheh385820a2007-07-19 00:14:38 -07001838static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1839 loff_t f_pos, unsigned int cmd,
1840 struct ocfs2_space_resv *sr,
1841 int change_size)
Mark Fashehb2580102007-03-09 16:53:21 -08001842{
1843 int ret;
1844 s64 llen;
Mark Fasheh385820a2007-07-19 00:14:38 -07001845 loff_t size;
Mark Fashehb2580102007-03-09 16:53:21 -08001846 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1847 struct buffer_head *di_bh = NULL;
1848 handle_t *handle;
Mark Fasheha00cce32007-07-20 11:28:30 -07001849 unsigned long long max_off = inode->i_sb->s_maxbytes;
Mark Fashehb2580102007-03-09 16:53:21 -08001850
Mark Fashehb2580102007-03-09 16:53:21 -08001851 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
1852 return -EROFS;
1853
1854 mutex_lock(&inode->i_mutex);
1855
1856 /*
1857 * This prevents concurrent writes on other nodes
1858 */
1859 ret = ocfs2_rw_lock(inode, 1);
1860 if (ret) {
1861 mlog_errno(ret);
1862 goto out;
1863 }
1864
Mark Fashehe63aecb62007-10-18 15:30:42 -07001865 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fashehb2580102007-03-09 16:53:21 -08001866 if (ret) {
1867 mlog_errno(ret);
1868 goto out_rw_unlock;
1869 }
1870
1871 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1872 ret = -EPERM;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001873 goto out_inode_unlock;
Mark Fashehb2580102007-03-09 16:53:21 -08001874 }
1875
1876 switch (sr->l_whence) {
1877 case 0: /*SEEK_SET*/
1878 break;
1879 case 1: /*SEEK_CUR*/
Mark Fasheh385820a2007-07-19 00:14:38 -07001880 sr->l_start += f_pos;
Mark Fashehb2580102007-03-09 16:53:21 -08001881 break;
1882 case 2: /*SEEK_END*/
1883 sr->l_start += i_size_read(inode);
1884 break;
1885 default:
1886 ret = -EINVAL;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001887 goto out_inode_unlock;
Mark Fashehb2580102007-03-09 16:53:21 -08001888 }
1889 sr->l_whence = 0;
1890
1891 llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
1892
1893 if (sr->l_start < 0
1894 || sr->l_start > max_off
1895 || (sr->l_start + llen) < 0
1896 || (sr->l_start + llen) > max_off) {
1897 ret = -EINVAL;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001898 goto out_inode_unlock;
Mark Fashehb2580102007-03-09 16:53:21 -08001899 }
Mark Fasheh385820a2007-07-19 00:14:38 -07001900 size = sr->l_start + sr->l_len;
Mark Fashehb2580102007-03-09 16:53:21 -08001901
Tariq Saeeda2a3b392014-01-21 15:48:38 -08001902 if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64 ||
1903 cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) {
Mark Fashehb2580102007-03-09 16:53:21 -08001904 if (sr->l_len <= 0) {
1905 ret = -EINVAL;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001906 goto out_inode_unlock;
Mark Fashehb2580102007-03-09 16:53:21 -08001907 }
1908 }
1909
Mark Fasheh385820a2007-07-19 00:14:38 -07001910 if (file && should_remove_suid(file->f_path.dentry)) {
Mark Fashehb2580102007-03-09 16:53:21 -08001911 ret = __ocfs2_write_remove_suid(inode, di_bh);
1912 if (ret) {
1913 mlog_errno(ret);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001914 goto out_inode_unlock;
Mark Fashehb2580102007-03-09 16:53:21 -08001915 }
1916 }
1917
1918 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1919 switch (cmd) {
1920 case OCFS2_IOC_RESVSP:
1921 case OCFS2_IOC_RESVSP64:
1922 /*
1923 * This takes unsigned offsets, but the signed ones we
1924 * pass have been checked against overflow above.
1925 */
1926 ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
1927 sr->l_len);
1928 break;
1929 case OCFS2_IOC_UNRESVSP:
1930 case OCFS2_IOC_UNRESVSP64:
1931 ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
1932 sr->l_len);
1933 break;
1934 default:
1935 ret = -EINVAL;
1936 }
1937 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1938 if (ret) {
1939 mlog_errno(ret);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001940 goto out_inode_unlock;
Mark Fashehb2580102007-03-09 16:53:21 -08001941 }
1942
1943 /*
1944 * We update c/mtime for these changes
1945 */
1946 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1947 if (IS_ERR(handle)) {
1948 ret = PTR_ERR(handle);
1949 mlog_errno(ret);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001950 goto out_inode_unlock;
Mark Fashehb2580102007-03-09 16:53:21 -08001951 }
1952
Mark Fasheh385820a2007-07-19 00:14:38 -07001953 if (change_size && i_size_read(inode) < size)
1954 i_size_write(inode, size);
1955
Mark Fashehb2580102007-03-09 16:53:21 -08001956 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1957 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1958 if (ret < 0)
1959 mlog_errno(ret);
1960
Luis Henriquesa4e08d02012-07-11 14:02:10 -07001961 if (file && (file->f_flags & O_SYNC))
Mark Fashehdf295d42011-11-16 12:03:10 -08001962 handle->h_sync = 1;
1963
Mark Fashehb2580102007-03-09 16:53:21 -08001964 ocfs2_commit_trans(osb, handle);
1965
Mark Fashehe63aecb62007-10-18 15:30:42 -07001966out_inode_unlock:
Mark Fashehb2580102007-03-09 16:53:21 -08001967 brelse(di_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001968 ocfs2_inode_unlock(inode, 1);
Mark Fashehb2580102007-03-09 16:53:21 -08001969out_rw_unlock:
1970 ocfs2_rw_unlock(inode, 1);
1971
Mark Fashehb2580102007-03-09 16:53:21 -08001972out:
Julia Lawallc259ae52008-07-21 09:59:15 +02001973 mutex_unlock(&inode->i_mutex);
Mark Fashehb2580102007-03-09 16:53:21 -08001974 return ret;
1975}
1976
Mark Fasheh385820a2007-07-19 00:14:38 -07001977int ocfs2_change_file_space(struct file *file, unsigned int cmd,
1978 struct ocfs2_space_resv *sr)
1979{
Al Viro496ad9a2013-01-23 17:07:38 -05001980 struct inode *inode = file_inode(file);
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08001981 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Jan Karafef69252012-06-12 16:20:40 +02001982 int ret;
Mark Fasheh385820a2007-07-19 00:14:38 -07001983
1984 if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
1985 !ocfs2_writes_unwritten_extents(osb))
1986 return -ENOTTY;
1987 else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
1988 !ocfs2_sparse_alloc(osb))
1989 return -ENOTTY;
1990
1991 if (!S_ISREG(inode->i_mode))
1992 return -EINVAL;
1993
1994 if (!(file->f_mode & FMODE_WRITE))
1995 return -EBADF;
1996
Jan Karafef69252012-06-12 16:20:40 +02001997 ret = mnt_want_write_file(file);
1998 if (ret)
1999 return ret;
2000 ret = __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
2001 mnt_drop_write_file(file);
2002 return ret;
Mark Fasheh385820a2007-07-19 00:14:38 -07002003}
2004
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002005static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
Mark Fasheh385820a2007-07-19 00:14:38 -07002006 loff_t len)
2007{
Al Viro496ad9a2013-01-23 17:07:38 -05002008 struct inode *inode = file_inode(file);
Mark Fasheh385820a2007-07-19 00:14:38 -07002009 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2010 struct ocfs2_space_resv sr;
2011 int change_size = 1;
Josef Bacikdb47fef2010-11-17 20:46:17 -05002012 int cmd = OCFS2_IOC_RESVSP64;
Mark Fasheh385820a2007-07-19 00:14:38 -07002013
Christoph Hellwig64c23e82011-01-14 13:07:30 +01002014 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2015 return -EOPNOTSUPP;
Mark Fasheh385820a2007-07-19 00:14:38 -07002016 if (!ocfs2_writes_unwritten_extents(osb))
2017 return -EOPNOTSUPP;
2018
Mark Fasheh385820a2007-07-19 00:14:38 -07002019 if (mode & FALLOC_FL_KEEP_SIZE)
2020 change_size = 0;
2021
Josef Bacikdb47fef2010-11-17 20:46:17 -05002022 if (mode & FALLOC_FL_PUNCH_HOLE)
2023 cmd = OCFS2_IOC_UNRESVSP64;
2024
Mark Fasheh385820a2007-07-19 00:14:38 -07002025 sr.l_whence = 0;
2026 sr.l_start = (s64)offset;
2027 sr.l_len = (s64)len;
2028
Josef Bacikdb47fef2010-11-17 20:46:17 -05002029 return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
2030 change_size);
Mark Fasheh385820a2007-07-19 00:14:38 -07002031}
2032
Tao Ma293b2f72009-08-25 08:02:48 +08002033int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
2034 size_t count)
2035{
2036 int ret = 0;
2037 unsigned int extent_flags;
2038 u32 cpos, clusters, extent_len, phys_cpos;
2039 struct super_block *sb = inode->i_sb;
2040
2041 if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
Tao Ma2f48d592009-10-15 11:10:49 +08002042 !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
2043 OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Tao Ma293b2f72009-08-25 08:02:48 +08002044 return 0;
2045
2046 cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
2047 clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
2048
2049 while (clusters) {
2050 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
2051 &extent_flags);
2052 if (ret < 0) {
2053 mlog_errno(ret);
2054 goto out;
2055 }
2056
2057 if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
2058 ret = 1;
2059 break;
2060 }
2061
2062 if (extent_len > clusters)
2063 extent_len = clusters;
2064
2065 clusters -= extent_len;
2066 cpos += extent_len;
2067 }
2068out:
2069 return ret;
2070}
2071
Mark Fasheha11f7e62011-06-22 14:23:38 -07002072static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
2073{
2074 int blockmask = inode->i_sb->s_blocksize - 1;
2075 loff_t final_size = pos + count;
2076
2077 if ((pos & blockmask) || (final_size & blockmask))
2078 return 1;
2079 return 0;
2080}
2081
Tao Ma293b2f72009-08-25 08:02:48 +08002082static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
Tao Mab8908232010-08-12 10:27:14 +08002083 struct file *file,
Tao Ma293b2f72009-08-25 08:02:48 +08002084 loff_t pos, size_t count,
2085 int *meta_level)
2086{
2087 int ret;
2088 struct buffer_head *di_bh = NULL;
2089 u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
2090 u32 clusters =
2091 ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
2092
2093 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2094 if (ret) {
2095 mlog_errno(ret);
2096 goto out;
2097 }
2098
2099 *meta_level = 1;
2100
Tiger Yangc7dd3392013-08-13 16:00:58 -07002101 ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
Tao Ma293b2f72009-08-25 08:02:48 +08002102 if (ret)
2103 mlog_errno(ret);
2104out:
2105 brelse(di_bh);
2106 return ret;
2107}
2108
Tao Mab8908232010-08-12 10:27:14 +08002109static int ocfs2_prepare_inode_for_write(struct file *file,
Al Viro90320252015-04-09 11:14:45 -04002110 loff_t pos,
Tiger Yang8659ac22006-10-17 18:29:52 -07002111 size_t count,
Mark Fasheh9517bac2007-02-09 20:24:12 -08002112 int appending,
Tao Ma86470e92009-12-03 21:55:05 +08002113 int *direct_io,
2114 int *has_refcount)
Mark Fashehccd979b2005-12-15 14:31:24 -08002115{
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002116 int ret = 0, meta_level = 0;
Tao Mab8908232010-08-12 10:27:14 +08002117 struct dentry *dentry = file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002118 struct inode *inode = d_inode(dentry);
Al Viro90320252015-04-09 11:14:45 -04002119 loff_t end;
Joseph Qid943d592015-02-16 16:00:06 -08002120 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2121 int full_coherency = !(osb->s_mount_opt &
2122 OCFS2_MOUNT_COHERENCY_BUFFERED);
Mark Fashehccd979b2005-12-15 14:31:24 -08002123
Sunil Mushran2bd63212010-01-25 16:57:38 -08002124 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002125 * We start with a read level meta lock and only jump to an ex
2126 * if we need to make modifications here.
Mark Fashehccd979b2005-12-15 14:31:24 -08002127 */
Mark Fashehccd979b2005-12-15 14:31:24 -08002128 for(;;) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07002129 ret = ocfs2_inode_lock(inode, NULL, meta_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002130 if (ret < 0) {
2131 meta_level = -1;
2132 mlog_errno(ret);
2133 goto out;
2134 }
2135
2136 /* Clear suid / sgid if necessary. We do this here
2137 * instead of later in the write path because
2138 * remove_suid() calls ->setattr without any hint that
2139 * we may have already done our cluster locking. Since
2140 * ocfs2_setattr() *must* take cluster locks to
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002141 * proceed, this will lead us to recursively lock the
Mark Fashehccd979b2005-12-15 14:31:24 -08002142 * inode. There's also the dinode i_size state which
2143 * can be lost via setattr during extending writes (we
2144 * set inode->i_size at the end of a write. */
Tiger Yang8659ac22006-10-17 18:29:52 -07002145 if (should_remove_suid(dentry)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002146 if (meta_level == 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07002147 ocfs2_inode_unlock(inode, meta_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002148 meta_level = 1;
2149 continue;
2150 }
2151
2152 ret = ocfs2_write_remove_suid(inode);
2153 if (ret < 0) {
2154 mlog_errno(ret);
Tiger Yang8659ac22006-10-17 18:29:52 -07002155 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 }
2157 }
2158
Al Viro90320252015-04-09 11:14:45 -04002159 end = pos + count;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08002160
Al Viro90320252015-04-09 11:14:45 -04002161 ret = ocfs2_check_range_for_refcount(inode, pos, count);
Tao Ma293b2f72009-08-25 08:02:48 +08002162 if (ret == 1) {
2163 ocfs2_inode_unlock(inode, meta_level);
2164 meta_level = -1;
2165
2166 ret = ocfs2_prepare_inode_for_refcount(inode,
Tao Mab8908232010-08-12 10:27:14 +08002167 file,
Al Viro90320252015-04-09 11:14:45 -04002168 pos,
Tao Ma293b2f72009-08-25 08:02:48 +08002169 count,
2170 &meta_level);
Tao Ma86470e92009-12-03 21:55:05 +08002171 if (has_refcount)
2172 *has_refcount = 1;
Wengang Wang96a1cc72010-02-09 14:57:45 +08002173 if (direct_io)
2174 *direct_io = 0;
Tao Ma293b2f72009-08-25 08:02:48 +08002175 }
2176
2177 if (ret < 0) {
2178 mlog_errno(ret);
2179 goto out_unlock;
2180 }
2181
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002182 /*
2183 * Skip the O_DIRECT checks if we don't need
2184 * them.
2185 */
2186 if (!direct_io || !(*direct_io))
2187 break;
Mark Fasheh9517bac2007-02-09 20:24:12 -08002188
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002189 /*
Mark Fasheh1afc32b2007-09-07 14:46:51 -07002190 * There's no sane way to do direct writes to an inode
2191 * with inline data.
2192 */
2193 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2194 *direct_io = 0;
2195 break;
2196 }
2197
2198 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002199 * Allowing concurrent direct writes means
2200 * i_size changes wouldn't be synchronized, so
2201 * one node could wind up truncating another
2202 * nodes writes.
2203 */
Joseph Qid943d592015-02-16 16:00:06 -08002204 if (end > i_size_read(inode) && !full_coherency) {
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002205 *direct_io = 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08002206 break;
2207 }
2208
Mark Fasheh3a0782d2007-01-17 12:53:31 -08002209 /*
Joseph Qi160cc262015-02-16 16:00:15 -08002210 * Fallback to old way if the feature bit is not set.
2211 */
2212 if (end > i_size_read(inode) &&
2213 !ocfs2_supports_append_dio(osb)) {
2214 *direct_io = 0;
2215 break;
2216 }
2217
2218 /*
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002219 * We don't fill holes during direct io, so
2220 * check for them here. If any are found, the
2221 * caller will have to retake some cluster
2222 * locks and initiate the io as buffered.
Mark Fasheh3a0782d2007-01-17 12:53:31 -08002223 */
Al Viro90320252015-04-09 11:14:45 -04002224 ret = ocfs2_check_range_for_holes(inode, pos, count);
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002225 if (ret == 1) {
Joseph Qi160cc262015-02-16 16:00:15 -08002226 /*
2227 * Fallback to old way if the feature bit is not set.
2228 * Otherwise try dio first and then complete the rest
2229 * request through buffer io.
2230 */
2231 if (!ocfs2_supports_append_dio(osb))
2232 *direct_io = 0;
Mark Fasheh65ed39d2007-08-28 17:13:23 -07002233 ret = 0;
2234 } else if (ret < 0)
2235 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08002236 break;
2237 }
2238
Tiger Yang8659ac22006-10-17 18:29:52 -07002239out_unlock:
Tao Ma468eedd2011-02-22 22:14:41 +08002240 trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
Al Viro90320252015-04-09 11:14:45 -04002241 pos, appending, count,
Tao Ma468eedd2011-02-22 22:14:41 +08002242 direct_io, has_refcount);
2243
Tao Ma293b2f72009-08-25 08:02:48 +08002244 if (meta_level >= 0)
2245 ocfs2_inode_unlock(inode, meta_level);
Tiger Yang8659ac22006-10-17 18:29:52 -07002246
2247out:
2248 return ret;
2249}
2250
Al Viro3ef045c2014-04-03 14:25:22 -04002251static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
2252 struct iov_iter *from)
Tiger Yang8659ac22006-10-17 18:29:52 -07002253{
WeiWei Wangfa5a0eb2015-06-24 16:55:15 -07002254 int direct_io, appending, rw_level;
Tao Ma86470e92009-12-03 21:55:05 +08002255 int can_do_direct, has_refcount = 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08002256 ssize_t written = 0;
Al Viro3309dd02015-04-09 12:55:47 -04002257 ssize_t ret;
Al Viro90320252015-04-09 11:14:45 -04002258 size_t count = iov_iter_count(from), orig_count;
Al Viro5dc31612015-04-09 07:25:03 -04002259 loff_t old_size;
Mark Fasheh9ea2d322007-10-18 14:14:45 -07002260 u32 old_clusters;
Mark Fasheh9517bac2007-02-09 20:24:12 -08002261 struct file *file = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -05002262 struct inode *inode = file_inode(file);
Mark Fasheh9ea2d322007-10-18 14:14:45 -07002263 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tristan Ye7bdb0d12010-10-11 16:46:39 +08002264 int full_coherency = !(osb->s_mount_opt &
2265 OCFS2_MOUNT_COHERENCY_BUFFERED);
Mark Fasheha11f7e62011-06-22 14:23:38 -07002266 int unaligned_dio = 0;
Al Viro7da839c2015-04-09 14:01:33 -04002267 int dropped_dio = 0;
Tiger Yang8659ac22006-10-17 18:29:52 -07002268
Tao Ma468eedd2011-02-22 22:14:41 +08002269 trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
2270 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2271 file->f_path.dentry->d_name.len,
2272 file->f_path.dentry->d_name.name,
Al Viro3ef045c2014-04-03 14:25:22 -04002273 (unsigned int)from->nr_segs); /* GRRRRR */
Tiger Yang8659ac22006-10-17 18:29:52 -07002274
Christoph Hellwig66ee59a2015-02-11 19:56:46 +01002275 if (count == 0)
Tiger Yang8659ac22006-10-17 18:29:52 -07002276 return 0;
2277
Al Viro2ba48ce2015-04-09 13:52:01 -04002278 appending = iocb->ki_flags & IOCB_APPEND ? 1 : 0;
2279 direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08002280
Tiger Yang8659ac22006-10-17 18:29:52 -07002281 mutex_lock(&inode->i_mutex);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002282
2283relock:
Tristan Ye7bdb0d12010-10-11 16:46:39 +08002284 /*
2285 * Concurrent O_DIRECT writes are allowed with
2286 * mount_option "coherency=buffered".
2287 */
2288 rw_level = (!direct_io || full_coherency);
2289
Tiger Yang8659ac22006-10-17 18:29:52 -07002290 ret = ocfs2_rw_lock(inode, rw_level);
2291 if (ret < 0) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08002292 mlog_errno(ret);
WeiWei Wangfa5a0eb2015-06-24 16:55:15 -07002293 goto out_mutex;
Mark Fasheh9517bac2007-02-09 20:24:12 -08002294 }
2295
Tristan Ye7bdb0d12010-10-11 16:46:39 +08002296 /*
2297 * O_DIRECT writes with "coherency=full" need to take EX cluster
2298 * inode_lock to guarantee coherency.
2299 */
2300 if (direct_io && full_coherency) {
2301 /*
2302 * We need to take and drop the inode lock to force
2303 * other nodes to drop their caches. Buffered I/O
2304 * already does this in write_begin().
2305 */
2306 ret = ocfs2_inode_lock(inode, NULL, 1);
2307 if (ret < 0) {
2308 mlog_errno(ret);
Joseph Qiafe1bb72013-05-24 15:55:12 -07002309 goto out;
Tristan Ye7bdb0d12010-10-11 16:46:39 +08002310 }
2311
2312 ocfs2_inode_unlock(inode, 1);
2313 }
2314
Al Viro3309dd02015-04-09 12:55:47 -04002315 orig_count = iov_iter_count(from);
2316 ret = generic_write_checks(iocb, from);
2317 if (ret <= 0) {
2318 if (ret)
2319 mlog_errno(ret);
Al Viro90320252015-04-09 11:14:45 -04002320 goto out;
2321 }
Al Viro3309dd02015-04-09 12:55:47 -04002322 count = ret;
Al Viro90320252015-04-09 11:14:45 -04002323
Mark Fasheh9517bac2007-02-09 20:24:12 -08002324 can_do_direct = direct_io;
Al Viro90320252015-04-09 11:14:45 -04002325 ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, count, appending,
Tao Ma86470e92009-12-03 21:55:05 +08002326 &can_do_direct, &has_refcount);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002327 if (ret < 0) {
Tiger Yang8659ac22006-10-17 18:29:52 -07002328 mlog_errno(ret);
2329 goto out;
2330 }
2331
Mark Fasheha11f7e62011-06-22 14:23:38 -07002332 if (direct_io && !is_sync_kiocb(iocb))
Al Viro5dc31612015-04-09 07:25:03 -04002333 unaligned_dio = ocfs2_is_io_unaligned(inode, count, iocb->ki_pos);
Mark Fasheha11f7e62011-06-22 14:23:38 -07002334
Mark Fasheh9517bac2007-02-09 20:24:12 -08002335 /*
2336 * We can't complete the direct I/O as requested, fall back to
2337 * buffered I/O.
2338 */
2339 if (direct_io && !can_do_direct) {
2340 ocfs2_rw_unlock(inode, rw_level);
Mark Fasheh9517bac2007-02-09 20:24:12 -08002341
Mark Fasheh9517bac2007-02-09 20:24:12 -08002342 rw_level = -1;
2343
2344 direct_io = 0;
Al Viro7da839c2015-04-09 14:01:33 -04002345 iocb->ki_flags &= ~IOCB_DIRECT;
Al Viro3309dd02015-04-09 12:55:47 -04002346 iov_iter_reexpand(from, orig_count);
Al Viro7da839c2015-04-09 14:01:33 -04002347 dropped_dio = 1;
Mark Fasheh9517bac2007-02-09 20:24:12 -08002348 goto relock;
Tiger Yang8659ac22006-10-17 18:29:52 -07002349 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002350
Mark Fasheha11f7e62011-06-22 14:23:38 -07002351 if (unaligned_dio) {
2352 /*
2353 * Wait on previous unaligned aio to complete before
2354 * proceeding.
2355 */
Wengang Wangc18ceab2014-04-03 14:46:46 -07002356 mutex_lock(&OCFS2_I(inode)->ip_unaligned_aio);
2357 /* Mark the iocb as needing an unlock in ocfs2_dio_end_io */
Mark Fasheha11f7e62011-06-22 14:23:38 -07002358 ocfs2_iocb_set_unaligned_aio(iocb);
2359 }
2360
Mark Fasheh9ea2d322007-10-18 14:14:45 -07002361 /*
2362 * To later detect whether a journal commit for sync writes is
2363 * necessary, we sample i_size, and cluster count here.
2364 */
2365 old_size = i_size_read(inode);
2366 old_clusters = OCFS2_I(inode)->ip_clusters;
2367
Mark Fashehccd979b2005-12-15 14:31:24 -08002368 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07002369 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002370
Al Viro7da839c2015-04-09 14:01:33 -04002371 written = __generic_file_write_iter(iocb, from);
Mark Fashehccd979b2005-12-15 14:31:24 -08002372 /* buffered aio wouldn't have proper lock coverage today */
Al Viro7da839c2015-04-09 14:01:33 -04002373 BUG_ON(written == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
Mark Fashehccd979b2005-12-15 14:31:24 -08002374
Al Viro64b4e252015-04-08 17:00:32 -04002375 if (unlikely(written <= 0))
2376 goto no_sync;
2377
Al Viro7da839c2015-04-09 14:01:33 -04002378 if (((file->f_flags & O_DSYNC) && !direct_io) ||
2379 IS_SYNC(inode) || dropped_dio) {
Al Viro64b4e252015-04-08 17:00:32 -04002380 ret = filemap_fdatawrite_range(file->f_mapping,
2381 iocb->ki_pos - written,
2382 iocb->ki_pos - 1);
Jan Kara918941a2009-08-17 18:50:08 +02002383 if (ret < 0)
2384 written = ret;
2385
Goldwyn Rodrigues86b9c6f2014-12-10 15:41:53 -08002386 if (!ret) {
Joel Becker2b4e30f2008-09-03 20:03:41 -07002387 ret = jbd2_journal_force_commit(osb->journal->j_journal);
Mark Fasheh9ea2d322007-10-18 14:14:45 -07002388 if (ret < 0)
2389 written = ret;
2390 }
Jan Kara918941a2009-08-17 18:50:08 +02002391
2392 if (!ret)
Al Viro64b4e252015-04-08 17:00:32 -04002393 ret = filemap_fdatawait_range(file->f_mapping,
2394 iocb->ki_pos - written,
2395 iocb->ki_pos - 1);
Mark Fasheh9ea2d322007-10-18 14:14:45 -07002396 }
2397
Al Viro64b4e252015-04-08 17:00:32 -04002398no_sync:
Sunil Mushran2bd63212010-01-25 16:57:38 -08002399 /*
Mark Fashehccd979b2005-12-15 14:31:24 -08002400 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
2401 * function pointer which is called when o_direct io completes so that
Christoph Hellwigbd5fe6c2011-06-24 14:29:43 -04002402 * it can unlock our rw lock.
Mark Fashehccd979b2005-12-15 14:31:24 -08002403 * Unfortunately there are error cases which call end_io and others
2404 * that don't. so we don't have to unlock the rw_lock if either an
2405 * async dio is going to do it in the future or an end_io after an
2406 * error has already done it.
2407 */
Coly Li66b116c2010-02-25 14:57:13 +08002408 if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002409 rw_level = -1;
Mark Fasheha11f7e62011-06-22 14:23:38 -07002410 unaligned_dio = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002411 }
2412
Junxiao Bi3e5d3c32012-06-27 17:09:55 +08002413 if (unaligned_dio) {
2414 ocfs2_iocb_clear_unaligned_aio(iocb);
Wengang Wangc18ceab2014-04-03 14:46:46 -07002415 mutex_unlock(&OCFS2_I(inode)->ip_unaligned_aio);
Junxiao Bi3e5d3c32012-06-27 17:09:55 +08002416 }
Mark Fasheha11f7e62011-06-22 14:23:38 -07002417
Mark Fashehccd979b2005-12-15 14:31:24 -08002418out:
Mark Fasheh9517bac2007-02-09 20:24:12 -08002419 if (rw_level != -1)
2420 ocfs2_rw_unlock(inode, rw_level);
2421
WeiWei Wangfa5a0eb2015-06-24 16:55:15 -07002422out_mutex:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002423 mutex_unlock(&inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002424
Wengang Wang812e7a62009-07-10 13:26:04 +08002425 if (written)
2426 ret = written;
Wengang Wang812e7a62009-07-10 13:26:04 +08002427 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002428}
2429
Tiger Yang8659ac22006-10-17 18:29:52 -07002430static ssize_t ocfs2_file_splice_read(struct file *in,
2431 loff_t *ppos,
2432 struct pipe_inode_info *pipe,
2433 size_t len,
2434 unsigned int flags)
2435{
Tao Ma1962f392009-06-19 15:36:52 +08002436 int ret = 0, lock_level = 0;
Al Viro496ad9a2013-01-23 17:07:38 -05002437 struct inode *inode = file_inode(in);
Tiger Yang8659ac22006-10-17 18:29:52 -07002438
Tao Ma468eedd2011-02-22 22:14:41 +08002439 trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry,
2440 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2441 in->f_path.dentry->d_name.len,
2442 in->f_path.dentry->d_name.name, len);
Tiger Yang8659ac22006-10-17 18:29:52 -07002443
2444 /*
Al Viro3cd9ad52014-04-02 14:44:18 -04002445 * See the comment in ocfs2_file_read_iter()
Tiger Yang8659ac22006-10-17 18:29:52 -07002446 */
Al Viro182be682013-01-24 02:21:54 -05002447 ret = ocfs2_inode_lock_atime(inode, in->f_path.mnt, &lock_level);
Tiger Yang8659ac22006-10-17 18:29:52 -07002448 if (ret < 0) {
2449 mlog_errno(ret);
2450 goto bail;
2451 }
Tao Ma1962f392009-06-19 15:36:52 +08002452 ocfs2_inode_unlock(inode, lock_level);
Tiger Yang8659ac22006-10-17 18:29:52 -07002453
2454 ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2455
2456bail:
Tiger Yang8659ac22006-10-17 18:29:52 -07002457 return ret;
2458}
2459
Al Viro3cd9ad52014-04-02 14:44:18 -04002460static ssize_t ocfs2_file_read_iter(struct kiocb *iocb,
2461 struct iov_iter *to)
Mark Fashehccd979b2005-12-15 14:31:24 -08002462{
WeiWei Wangfa5a0eb2015-06-24 16:55:15 -07002463 int ret = 0, rw_level = -1, lock_level = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002464 struct file *filp = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -05002465 struct inode *inode = file_inode(filp);
Mark Fashehccd979b2005-12-15 14:31:24 -08002466
Tao Ma468eedd2011-02-22 22:14:41 +08002467 trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry,
2468 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2469 filp->f_path.dentry->d_name.len,
Al Viro3cd9ad52014-04-02 14:44:18 -04002470 filp->f_path.dentry->d_name.name,
2471 to->nr_segs); /* GRRRRR */
Tao Ma468eedd2011-02-22 22:14:41 +08002472
Mark Fashehccd979b2005-12-15 14:31:24 -08002473
2474 if (!inode) {
2475 ret = -EINVAL;
2476 mlog_errno(ret);
2477 goto bail;
2478 }
2479
Sunil Mushran2bd63212010-01-25 16:57:38 -08002480 /*
Mark Fashehccd979b2005-12-15 14:31:24 -08002481 * buffered reads protect themselves in ->readpage(). O_DIRECT reads
2482 * need locks to protect pending reads from racing with truncate.
2483 */
Al Viro2ba48ce2015-04-09 13:52:01 -04002484 if (iocb->ki_flags & IOCB_DIRECT) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002485 ret = ocfs2_rw_lock(inode, 0);
2486 if (ret < 0) {
2487 mlog_errno(ret);
2488 goto bail;
2489 }
2490 rw_level = 0;
2491 /* communicate with ocfs2_dio_end_io */
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -07002492 ocfs2_iocb_set_rw_locked(iocb, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002493 }
2494
Mark Fashehc4374f82006-05-05 19:04:35 -07002495 /*
2496 * We're fine letting folks race truncates and extending
2497 * writes with read across the cluster, just like they can
2498 * locally. Hence no rw_lock during read.
Sunil Mushran2bd63212010-01-25 16:57:38 -08002499 *
Mark Fashehc4374f82006-05-05 19:04:35 -07002500 * Take and drop the meta data lock to update inode fields
2501 * like i_size. This allows the checks down below
Sunil Mushran2bd63212010-01-25 16:57:38 -08002502 * generic_file_aio_read() a chance of actually working.
Mark Fashehc4374f82006-05-05 19:04:35 -07002503 */
Al Viro182be682013-01-24 02:21:54 -05002504 ret = ocfs2_inode_lock_atime(inode, filp->f_path.mnt, &lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07002505 if (ret < 0) {
2506 mlog_errno(ret);
2507 goto bail;
2508 }
Mark Fashehe63aecb62007-10-18 15:30:42 -07002509 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehc4374f82006-05-05 19:04:35 -07002510
Al Viro3cd9ad52014-04-02 14:44:18 -04002511 ret = generic_file_read_iter(iocb, to);
Tao Ma468eedd2011-02-22 22:14:41 +08002512 trace_generic_file_aio_read_ret(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08002513
2514 /* buffered aio wouldn't have proper lock coverage today */
Al Viro2ba48ce2015-04-09 13:52:01 -04002515 BUG_ON(ret == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
Mark Fashehccd979b2005-12-15 14:31:24 -08002516
Al Viro3ef045c2014-04-03 14:25:22 -04002517 /* see ocfs2_file_write_iter */
Mark Fashehccd979b2005-12-15 14:31:24 -08002518 if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2519 rw_level = -1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002520 }
2521
2522bail:
Sunil Mushran2bd63212010-01-25 16:57:38 -08002523 if (rw_level != -1)
Mark Fashehccd979b2005-12-15 14:31:24 -08002524 ocfs2_rw_unlock(inode, rw_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002525
2526 return ret;
2527}
2528
Sunil Mushran93862d52011-07-25 14:58:15 -07002529/* Refer generic_file_llseek_unlocked() */
Andrew Morton965c8e52012-12-17 15:59:39 -08002530static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence)
Sunil Mushran93862d52011-07-25 14:58:15 -07002531{
2532 struct inode *inode = file->f_mapping->host;
2533 int ret = 0;
2534
2535 mutex_lock(&inode->i_mutex);
2536
Andrew Morton965c8e52012-12-17 15:59:39 -08002537 switch (whence) {
Sunil Mushran93862d52011-07-25 14:58:15 -07002538 case SEEK_SET:
2539 break;
2540 case SEEK_END:
Jensenc8d888d2014-04-03 14:47:01 -07002541 /* SEEK_END requires the OCFS2 inode lock for the file
2542 * because it references the file's size.
2543 */
2544 ret = ocfs2_inode_lock(inode, NULL, 0);
2545 if (ret < 0) {
2546 mlog_errno(ret);
2547 goto out;
2548 }
2549 offset += i_size_read(inode);
2550 ocfs2_inode_unlock(inode, 0);
Sunil Mushran93862d52011-07-25 14:58:15 -07002551 break;
2552 case SEEK_CUR:
2553 if (offset == 0) {
2554 offset = file->f_pos;
2555 goto out;
2556 }
2557 offset += file->f_pos;
2558 break;
2559 case SEEK_DATA:
2560 case SEEK_HOLE:
Andrew Morton965c8e52012-12-17 15:59:39 -08002561 ret = ocfs2_seek_data_hole_offset(file, &offset, whence);
Sunil Mushran93862d52011-07-25 14:58:15 -07002562 if (ret)
2563 goto out;
2564 break;
2565 default:
2566 ret = -EINVAL;
2567 goto out;
2568 }
2569
Jie Liu46a1c2c2013-06-25 12:02:13 +08002570 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Sunil Mushran93862d52011-07-25 14:58:15 -07002571
2572out:
2573 mutex_unlock(&inode->i_mutex);
2574 if (ret)
2575 return ret;
2576 return offset;
2577}
2578
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002579const struct inode_operations ocfs2_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002580 .setattr = ocfs2_setattr,
2581 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002582 .permission = ocfs2_permission,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002583 .setxattr = generic_setxattr,
2584 .getxattr = generic_getxattr,
2585 .listxattr = ocfs2_listxattr,
2586 .removexattr = generic_removexattr,
Mark Fasheh00dc4172008-10-03 17:32:11 -04002587 .fiemap = ocfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002588 .get_acl = ocfs2_iop_get_acl,
Christoph Hellwig702e5bc2013-12-20 05:16:48 -08002589 .set_acl = ocfs2_iop_set_acl,
Mark Fashehccd979b2005-12-15 14:31:24 -08002590};
2591
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002592const struct inode_operations ocfs2_special_file_iops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002593 .setattr = ocfs2_setattr,
2594 .getattr = ocfs2_getattr,
Tiger Yangd38eb8d2006-11-27 09:59:21 +08002595 .permission = ocfs2_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002596 .get_acl = ocfs2_iop_get_acl,
Christoph Hellwig702e5bc2013-12-20 05:16:48 -08002597 .set_acl = ocfs2_iop_set_acl,
Mark Fashehccd979b2005-12-15 14:31:24 -08002598};
2599
Mark Fasheh53da4932008-07-21 14:29:16 -07002600/*
2601 * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
2602 * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
2603 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002604const struct file_operations ocfs2_fops = {
Sunil Mushran93862d52011-07-25 14:58:15 -07002605 .llseek = ocfs2_file_llseek,
Mark Fashehccd979b2005-12-15 14:31:24 -08002606 .mmap = ocfs2_mmap,
2607 .fsync = ocfs2_sync_file,
2608 .release = ocfs2_file_release,
2609 .open = ocfs2_file_open,
Al Viro3cd9ad52014-04-02 14:44:18 -04002610 .read_iter = ocfs2_file_read_iter,
Al Viro3ef045c2014-04-03 14:25:22 -04002611 .write_iter = ocfs2_file_write_iter,
Andi Kleenc9ec1482008-01-27 03:17:17 +01002612 .unlocked_ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08002613#ifdef CONFIG_COMPAT
2614 .compat_ioctl = ocfs2_compat_ioctl,
2615#endif
Mark Fasheh53da4932008-07-21 14:29:16 -07002616 .lock = ocfs2_lock,
2617 .flock = ocfs2_flock,
2618 .splice_read = ocfs2_file_splice_read,
Al Viro6dc8bc02014-04-05 04:34:23 -04002619 .splice_write = iter_file_splice_write,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002620 .fallocate = ocfs2_fallocate,
Mark Fasheh53da4932008-07-21 14:29:16 -07002621};
2622
2623const struct file_operations ocfs2_dops = {
2624 .llseek = generic_file_llseek,
2625 .read = generic_read_dir,
Al Viro3704412b2013-05-22 21:06:00 -04002626 .iterate = ocfs2_readdir,
Mark Fasheh53da4932008-07-21 14:29:16 -07002627 .fsync = ocfs2_sync_file,
2628 .release = ocfs2_dir_release,
2629 .open = ocfs2_dir_open,
2630 .unlocked_ioctl = ocfs2_ioctl,
2631#ifdef CONFIG_COMPAT
2632 .compat_ioctl = ocfs2_compat_ioctl,
2633#endif
2634 .lock = ocfs2_lock,
2635 .flock = ocfs2_flock,
2636};
2637
2638/*
2639 * POSIX-lockless variants of our file_operations.
2640 *
2641 * These will be used if the underlying cluster stack does not support
2642 * posix file locking, if the user passes the "localflocks" mount
2643 * option, or if we have a local-only fs.
2644 *
2645 * ocfs2_flock is in here because all stacks handle UNIX file locks,
2646 * so we still want it in the case of no stack support for
2647 * plocks. Internally, it will do the right thing when asked to ignore
2648 * the cluster.
2649 */
2650const struct file_operations ocfs2_fops_no_plocks = {
Sunil Mushran93862d52011-07-25 14:58:15 -07002651 .llseek = ocfs2_file_llseek,
Mark Fasheh53da4932008-07-21 14:29:16 -07002652 .mmap = ocfs2_mmap,
2653 .fsync = ocfs2_sync_file,
2654 .release = ocfs2_file_release,
2655 .open = ocfs2_file_open,
Al Viro3cd9ad52014-04-02 14:44:18 -04002656 .read_iter = ocfs2_file_read_iter,
Al Viro3ef045c2014-04-03 14:25:22 -04002657 .write_iter = ocfs2_file_write_iter,
Mark Fasheh53da4932008-07-21 14:29:16 -07002658 .unlocked_ioctl = ocfs2_ioctl,
2659#ifdef CONFIG_COMPAT
2660 .compat_ioctl = ocfs2_compat_ioctl,
2661#endif
Mark Fasheh53fc6222007-12-20 16:49:04 -08002662 .flock = ocfs2_flock,
Tiger Yang8659ac22006-10-17 18:29:52 -07002663 .splice_read = ocfs2_file_splice_read,
Al Viro6dc8bc02014-04-05 04:34:23 -04002664 .splice_write = iter_file_splice_write,
Tristan Ye3d1c1822011-05-23 15:57:26 +08002665 .fallocate = ocfs2_fallocate,
Mark Fashehccd979b2005-12-15 14:31:24 -08002666};
2667
Mark Fasheh53da4932008-07-21 14:29:16 -07002668const struct file_operations ocfs2_dops_no_plocks = {
Jan Kara32c3c0e2007-12-19 15:24:52 +01002669 .llseek = generic_file_llseek,
Mark Fashehccd979b2005-12-15 14:31:24 -08002670 .read = generic_read_dir,
Al Viro3704412b2013-05-22 21:06:00 -04002671 .iterate = ocfs2_readdir,
Mark Fashehccd979b2005-12-15 14:31:24 -08002672 .fsync = ocfs2_sync_file,
Mark Fasheh53fc6222007-12-20 16:49:04 -08002673 .release = ocfs2_dir_release,
2674 .open = ocfs2_dir_open,
Andi Kleenc9ec1482008-01-27 03:17:17 +01002675 .unlocked_ioctl = ocfs2_ioctl,
Mark Fasheh586d2322007-03-09 15:56:28 -08002676#ifdef CONFIG_COMPAT
2677 .compat_ioctl = ocfs2_compat_ioctl,
2678#endif
Mark Fasheh53fc6222007-12-20 16:49:04 -08002679 .flock = ocfs2_flock,
Mark Fashehccd979b2005-12-15 14:31:24 -08002680};