xref: /linux/fs/ocfs2/file.c (revision 3b27d13940c3710a1128527c43719cb0bb05d73b)
1 /* -*- 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 
26 #include <linux/capability.h>
27 #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>
33 #include <linux/sched.h>
34 #include <linux/splice.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
37 #include <linux/falloc.h>
38 #include <linux/quotaops.h>
39 #include <linux/blkdev.h>
40 #include <linux/backing-dev.h>
41 
42 #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"
54 #include "ioctl.h"
55 #include "journal.h"
56 #include "locks.h"
57 #include "mmap.h"
58 #include "suballoc.h"
59 #include "super.h"
60 #include "xattr.h"
61 #include "acl.h"
62 #include "quota.h"
63 #include "refcounttree.h"
64 #include "ocfs2_trace.h"
65 
66 #include "buffer_head_io.h"
67 
68 static 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 
84 static 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 
97 static 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 
103 	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);
107 
108 	if (file->f_mode & FMODE_WRITE)
109 		dquot_initialize(inode);
110 
111 	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);
128 
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 
140 leave:
141 	return status;
142 }
143 
144 static int ocfs2_file_release(struct inode *inode, struct file *file)
145 {
146 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
147 
148 	spin_lock(&oi->ip_lock);
149 	if (!--oi->ip_open_count)
150 		oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
151 
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);
157 	spin_unlock(&oi->ip_lock);
158 
159 	ocfs2_free_file_private(inode, file);
160 
161 	return 0;
162 }
163 
164 static int ocfs2_dir_open(struct inode *inode, struct file *file)
165 {
166 	return ocfs2_init_file_private(inode, file);
167 }
168 
169 static int ocfs2_dir_release(struct inode *inode, struct file *file)
170 {
171 	ocfs2_free_file_private(inode, file);
172 	return 0;
173 }
174 
175 static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
176 			   int datasync)
177 {
178 	int err = 0;
179 	struct inode *inode = file->f_mapping->host;
180 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
181 	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;
186 
187 	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);
192 
193 	if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
194 		return -EROFS;
195 
196 	err = filemap_write_and_wait_range(inode->i_mapping, start, end);
197 	if (err)
198 		return err;
199 
200 	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;
209 	}
210 
211 	if (err)
212 		mlog_errno(err);
213 
214 	return (err < 0) ? -EIO : 0;
215 }
216 
217 int 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 
230 	/*
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 
241 	if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
242 	    ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
243 		return 0;
244 
245 	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 
253 	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 
260 int 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;
266 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
267 
268 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
269 	if (IS_ERR(handle)) {
270 		ret = PTR_ERR(handle);
271 		mlog_errno(ret);
272 		goto out;
273 	}
274 
275 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
276 				      OCFS2_JOURNAL_ACCESS_WRITE);
277 	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 	 */
287 	inode->i_atime = CURRENT_TIME;
288 	di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
289 	di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
290 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
291 	ocfs2_journal_dirty(handle, bh);
292 
293 out_commit:
294 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
295 out:
296 	return ret;
297 }
298 
299 int ocfs2_set_inode_size(handle_t *handle,
300 				struct inode *inode,
301 				struct buffer_head *fe_bh,
302 				u64 new_i_size)
303 {
304 	int status;
305 
306 	i_size_write(inode, new_i_size);
307 	inode->i_blocks = ocfs2_inode_sector_count(inode);
308 	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 
316 bail:
317 	return status;
318 }
319 
320 int ocfs2_simple_size_update(struct inode *inode,
321 			     struct buffer_head *di_bh,
322 			     u64 new_i_size)
323 {
324 	int ret;
325 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
326 	handle_t *handle = NULL;
327 
328 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
329 	if (IS_ERR(handle)) {
330 		ret = PTR_ERR(handle);
331 		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 
340 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
341 	ocfs2_commit_trans(osb, handle);
342 out:
343 	return ret;
344 }
345 
346 static 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 
373 	return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
374 
375 out:
376 	return status;
377 }
378 
379 static 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;
385 	handle_t *handle;
386 	struct ocfs2_dinode *di;
387 	u64 cluster_bytes;
388 
389 	/*
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 
400 	/* TODO: This needs to actually orphan the inode in this
401 	 * transaction. */
402 
403 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
404 	if (IS_ERR(handle)) {
405 		status = PTR_ERR(handle);
406 		mlog_errno(status);
407 		goto out;
408 	}
409 
410 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
411 					 OCFS2_JOURNAL_ACCESS_WRITE);
412 	if (status < 0) {
413 		mlog_errno(status);
414 		goto out_commit;
415 	}
416 
417 	/*
418 	 * Do this before setting i_size.
419 	 */
420 	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);
423 	if (status) {
424 		mlog_errno(status);
425 		goto out_commit;
426 	}
427 
428 	i_size_write(inode, new_i_size);
429 	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);
435 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
436 
437 	ocfs2_journal_dirty(handle, fe_bh);
438 
439 out_commit:
440 	ocfs2_commit_trans(osb, handle);
441 out:
442 	return status;
443 }
444 
445 int ocfs2_truncate_file(struct inode *inode,
446 			       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);
452 
453 	/* We trust di_bh because it comes from ocfs2_inode_lock(), which
454 	 * already validated it */
455 	fe = (struct ocfs2_dinode *) di_bh->b_data;
456 
457 	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 
461 	mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
462 			"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,
465 			i_size_read(inode),
466 			(unsigned long long)le64_to_cpu(fe->i_size),
467 			le32_to_cpu(fe->i_flags));
468 
469 	if (new_i_size > le64_to_cpu(fe->i_size)) {
470 		trace_ocfs2_truncate_file_error(
471 			(unsigned long long)le64_to_cpu(fe->i_size),
472 			(unsigned long long)new_i_size);
473 		status = -EINVAL;
474 		mlog_errno(status);
475 		goto bail;
476 	}
477 
478 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
479 
480 	ocfs2_resv_discard(&osb->osb_la_resmap,
481 			   &OCFS2_I(inode)->ip_la_data_resv);
482 
483 	/*
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 	 */
490 	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 
493 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
494 		status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
495 					       i_size_read(inode), 1);
496 		if (status)
497 			mlog_errno(status);
498 
499 		goto bail_unlock_sem;
500 	}
501 
502 	/* 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);
509 		goto bail_unlock_sem;
510 	}
511 
512 	status = ocfs2_commit_truncate(osb, inode, di_bh);
513 	if (status < 0) {
514 		mlog_errno(status);
515 		goto bail_unlock_sem;
516 	}
517 
518 	/* TODO: orphan dir cleanup here. */
519 bail_unlock_sem:
520 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
521 
522 bail:
523 	if (!status && OCFS2_I(inode)->ip_clusters == 0)
524 		status = ocfs2_try_remove_refcount_tree(inode, di_bh);
525 
526 	return status;
527 }
528 
529 /*
530  * extend file allocation only here.
531  * 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  */
539 int 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)
549 {
550 	int ret;
551 	struct ocfs2_extent_tree et;
552 
553 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
554 	ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
555 					  clusters_to_add, mark_unwritten,
556 					  data_ac, meta_ac, reason_ret);
557 
558 	return ret;
559 }
560 
561 static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
562 				     u32 clusters_to_add, int mark_unwritten)
563 {
564 	int status = 0;
565 	int restart_func = 0;
566 	int credits;
567 	u32 prev_clusters;
568 	struct buffer_head *bh = NULL;
569 	struct ocfs2_dinode *fe = NULL;
570 	handle_t *handle = NULL;
571 	struct ocfs2_alloc_context *data_ac = NULL;
572 	struct ocfs2_alloc_context *meta_ac = NULL;
573 	enum ocfs2_alloc_restarted why = RESTART_NONE;
574 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
575 	struct ocfs2_extent_tree et;
576 	int did_quota = 0;
577 
578 	/*
579 	 * Unwritten extent only exists for file systems which
580 	 * support holes.
581 	 */
582 	BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
583 
584 	status = ocfs2_read_inode_block(inode, &bh);
585 	if (status < 0) {
586 		mlog_errno(status);
587 		goto leave;
588 	}
589 	fe = (struct ocfs2_dinode *) bh->b_data;
590 
591 restart_all:
592 	BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
593 
594 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
595 	status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
596 				       &data_ac, &meta_ac);
597 	if (status) {
598 		mlog_errno(status);
599 		goto leave;
600 	}
601 
602 	credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list);
603 	handle = ocfs2_start_trans(osb, credits);
604 	if (IS_ERR(handle)) {
605 		status = PTR_ERR(handle);
606 		handle = NULL;
607 		mlog_errno(status);
608 		goto leave;
609 	}
610 
611 restarted_transaction:
612 	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 
618 	status = dquot_alloc_space_nodirty(inode,
619 			ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
620 	if (status)
621 		goto leave;
622 	did_quota = 1;
623 
624 	/* 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. */
627 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
628 					 OCFS2_JOURNAL_ACCESS_WRITE);
629 	if (status < 0) {
630 		mlog_errno(status);
631 		goto leave;
632 	}
633 
634 	prev_clusters = OCFS2_I(inode)->ip_clusters;
635 
636 	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);
646 	if ((status < 0) && (status != -EAGAIN)) {
647 		if (status != -ENOSPC)
648 			mlog_errno(status);
649 		goto leave;
650 	}
651 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
652 	ocfs2_journal_dirty(handle, bh);
653 
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);
657 	/* Release unused quota reservation */
658 	dquot_free_space(inode,
659 			ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
660 	did_quota = 0;
661 
662 	if (why != RESTART_NONE && clusters_to_add) {
663 		if (why == RESTART_META) {
664 			restart_func = 1;
665 			status = 0;
666 		} else {
667 			BUG_ON(why != RESTART_TRANS);
668 
669 			status = ocfs2_allocate_extend_trans(handle, 1);
670 			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 
681 	trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
682 	     le32_to_cpu(fe->i_clusters),
683 	     (unsigned long long)le64_to_cpu(fe->i_size),
684 	     OCFS2_I(inode)->ip_clusters,
685 	     (unsigned long long)i_size_read(inode));
686 
687 leave:
688 	if (status < 0 && did_quota)
689 		dquot_free_space(inode,
690 			ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
691 	if (handle) {
692 		ocfs2_commit_trans(osb, handle);
693 		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 	}
707 	brelse(bh);
708 	bh = NULL;
709 
710 	return status;
711 }
712 
713 int 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 
720 /*
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  */
724 static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode,
725 						struct buffer_head *di_bh)
726 {
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);
742 	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)
750 		mlog_errno(ret);
751 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
752 
753 out:
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 
762 /* 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
764  * worry about recursive locking in ->write_begin() and ->write_end(). */
765 static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
766 				 u64 abs_to, struct buffer_head *di_bh)
767 {
768 	struct address_space *mapping = inode->i_mapping;
769 	struct page *page;
770 	unsigned long index = abs_from >> PAGE_CACHE_SHIFT;
771 	handle_t *handle;
772 	int ret = 0;
773 	unsigned zero_from, zero_to, block_start, block_end;
774 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
775 
776 	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));
779 
780 	handle = ocfs2_zero_start_ordered_transaction(inode, di_bh);
781 	if (IS_ERR(handle)) {
782 		ret = PTR_ERR(handle);
783 		goto out;
784 	}
785 
786 	page = find_or_create_page(mapping, index, GFP_NOFS);
787 	if (!page) {
788 		ret = -ENOMEM;
789 		mlog_errno(ret);
790 		goto out_commit_trans;
791 	}
792 
793 	/* 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;
798 
799 	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);
804 
805 	/* 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 		/*
811 		 * block_start is block-aligned.  Bump it by one to force
812 		 * __block_write_begin and block_commit_write to zero the
813 		 * whole block.
814 		 */
815 		ret = __block_write_begin(page, block_start + 1, 0,
816 					  ocfs2_get_block);
817 		if (ret < 0) {
818 			mlog_errno(ret);
819 			goto out_unlock;
820 		}
821 
822 
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 	}
831 
832 	/*
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;
844 	if (handle) {
845 		ocfs2_journal_dirty(handle, di_bh);
846 		ocfs2_update_inode_fsync_trans(handle, inode, 1);
847 	}
848 
849 out_unlock:
850 	unlock_page(page);
851 	page_cache_release(page);
852 out_commit_trans:
853 	if (handle)
854 		ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
855 out:
856 	return ret;
857 }
858 
859 /*
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  */
871 static 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)
875 {
876 	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;
883 
884 	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);
889 			goto out;
890 		}
891 
892 		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) {
925 		rc = ocfs2_refcount_cow(inode, di_bh, zero_cpos,
926 					zero_clusters, UINT_MAX);
927 		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 
937 out:
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  */
945 static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
946 				   u64 range_end, struct buffer_head *di_bh)
947 {
948 	int rc = 0;
949 	u64 next_pos;
950 	u64 zero_pos = range_start;
951 
952 	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);
956 	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;
962 		rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh);
963 		if (rc < 0) {
964 			mlog_errno(rc);
965 			break;
966 		}
967 		zero_pos = next_pos;
968 
969 		/*
970 		 * Very large extends have the potential to lock up
971 		 * the cpu for extended periods of time.
972 		 */
973 		cond_resched();
974 	}
975 
976 	return rc;
977 }
978 
979 int 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));
987 	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));
990 	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,
1008 					      range_end, di_bh);
1009 		if (ret) {
1010 			mlog_errno(ret);
1011 			break;
1012 		}
1013 		zero_start = range_end;
1014 	}
1015 
1016 	return ret;
1017 }
1018 
1019 int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
1020 			  u64 new_i_size, u64 zero_to)
1021 {
1022 	int ret;
1023 	u32 clusters_to_add;
1024 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1025 
1026 	/*
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 
1033 	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 	 */
1053 	ret = ocfs2_zero_extend(inode, di_bh, zero_to);
1054 	if (ret < 0)
1055 		mlog_errno(ret);
1056 
1057 out:
1058 	return ret;
1059 }
1060 
1061 static int ocfs2_extend_file(struct inode *inode,
1062 			     struct buffer_head *di_bh,
1063 			     u64 new_i_size)
1064 {
1065 	int ret = 0;
1066 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1067 
1068 	BUG_ON(!di_bh);
1069 
1070 	/* 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)
1075 		goto out;
1076 	BUG_ON(new_i_size < i_size_read(inode));
1077 
1078 	/*
1079 	 * 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
1082 	 * here.  We even have to hold it for sparse files because there
1083 	 * might be some tail zeroing.
1084 	 */
1085 	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);
1100 			mlog_errno(ret);
1101 			goto out;
1102 		}
1103 	}
1104 
1105 	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);
1110 
1111 	up_write(&oi->ip_alloc_sem);
1112 
1113 	if (ret < 0) {
1114 		mlog_errno(ret);
1115 		goto out;
1116 	}
1117 
1118 out_update_size:
1119 	ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
1120 	if (ret < 0)
1121 		mlog_errno(ret);
1122 
1123 out:
1124 	return ret;
1125 }
1126 
1127 int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
1128 {
1129 	int status = 0, size_change;
1130 	struct inode *inode = d_inode(dentry);
1131 	struct super_block *sb = inode->i_sb;
1132 	struct ocfs2_super *osb = OCFS2_SB(sb);
1133 	struct buffer_head *bh = NULL;
1134 	handle_t *handle = NULL;
1135 	struct dquot *transfer_to[MAXQUOTAS] = { };
1136 	int qtype;
1137 
1138 	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,
1142 			    from_kuid(&init_user_ns, attr->ia_uid),
1143 			    from_kgid(&init_user_ns, attr->ia_gid));
1144 
1145 	/* ensuring we don't even attempt to truncate a symlink */
1146 	if (S_ISLNK(inode->i_mode))
1147 		attr->ia_valid &= ~ATTR_SIZE;
1148 
1149 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
1150 			   | ATTR_GID | ATTR_UID | ATTR_MODE)
1151 	if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
1152 		return 0;
1153 
1154 	status = inode_change_ok(inode, attr);
1155 	if (status)
1156 		return status;
1157 
1158 	if (is_quota_modification(inode, attr))
1159 		dquot_initialize(inode);
1160 	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 
1169 	status = ocfs2_inode_lock(inode, &bh, 1);
1170 	if (status < 0) {
1171 		if (status != -ENOENT)
1172 			mlog_errno(status);
1173 		goto bail_unlock_rw;
1174 	}
1175 
1176 	if (size_change) {
1177 		status = inode_newsize_ok(inode, attr->ia_size);
1178 		if (status)
1179 			goto bail_unlock;
1180 
1181 		inode_dio_wait(inode);
1182 
1183 		if (i_size_read(inode) >= attr->ia_size) {
1184 			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 			}
1190 			status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1191 		} else
1192 			status = ocfs2_extend_file(inode, bh, attr->ia_size);
1193 		if (status < 0) {
1194 			if (status != -ENOSPC)
1195 				mlog_errno(status);
1196 			status = -ENOSPC;
1197 			goto bail_unlock;
1198 		}
1199 	}
1200 
1201 	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))) {
1203 		/*
1204 		 * Gather pointers to quota structures so that allocation /
1205 		 * freeing of quota structures happens here and not inside
1206 		 * dquot_transfer() where we have problems with lock ordering
1207 		 */
1208 		if (attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)
1209 		    && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1210 		    OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1211 			transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid));
1212 			if (!transfer_to[USRQUOTA]) {
1213 				status = -ESRCH;
1214 				goto bail_unlock;
1215 			}
1216 		}
1217 		if (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid)
1218 		    && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1219 		    OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1220 			transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid));
1221 			if (!transfer_to[GRPQUOTA]) {
1222 				status = -ESRCH;
1223 				goto bail_unlock;
1224 			}
1225 		}
1226 		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
1227 					   2 * ocfs2_quota_trans_credits(sb));
1228 		if (IS_ERR(handle)) {
1229 			status = PTR_ERR(handle);
1230 			mlog_errno(status);
1231 			goto bail_unlock;
1232 		}
1233 		status = __dquot_transfer(inode, transfer_to);
1234 		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 		}
1243 	}
1244 
1245 	setattr_copy(inode, attr);
1246 	mark_inode_dirty(inode);
1247 
1248 	status = ocfs2_mark_inode_dirty(handle, inode, bh);
1249 	if (status < 0)
1250 		mlog_errno(status);
1251 
1252 bail_commit:
1253 	ocfs2_commit_trans(osb, handle);
1254 bail_unlock:
1255 	ocfs2_inode_unlock(inode, 1);
1256 bail_unlock_rw:
1257 	if (size_change)
1258 		ocfs2_rw_unlock(inode, 1);
1259 bail:
1260 	brelse(bh);
1261 
1262 	/* Release quota pointers in case we acquired them */
1263 	for (qtype = 0; qtype < OCFS2_MAXQUOTAS; qtype++)
1264 		dqput(transfer_to[qtype]);
1265 
1266 	if (!status && attr->ia_valid & ATTR_MODE) {
1267 		status = posix_acl_chmod(inode, inode->i_mode);
1268 		if (status < 0)
1269 			mlog_errno(status);
1270 	}
1271 
1272 	return status;
1273 }
1274 
1275 int ocfs2_getattr(struct vfsmount *mnt,
1276 		  struct dentry *dentry,
1277 		  struct kstat *stat)
1278 {
1279 	struct inode *inode = d_inode(dentry);
1280 	struct super_block *sb = d_inode(dentry)->i_sb;
1281 	struct ocfs2_super *osb = sb->s_fs_info;
1282 	int err;
1283 
1284 	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 
1296 bail:
1297 	return err;
1298 }
1299 
1300 int ocfs2_permission(struct inode *inode, int mask)
1301 {
1302 	int ret;
1303 
1304 	if (mask & MAY_NOT_BLOCK)
1305 		return -ECHILD;
1306 
1307 	ret = ocfs2_inode_lock(inode, NULL, 0);
1308 	if (ret) {
1309 		if (ret != -ENOENT)
1310 			mlog_errno(ret);
1311 		goto out;
1312 	}
1313 
1314 	ret = generic_permission(inode, mask);
1315 
1316 	ocfs2_inode_unlock(inode, 0);
1317 out:
1318 	return ret;
1319 }
1320 
1321 static int __ocfs2_write_remove_suid(struct inode *inode,
1322 				     struct buffer_head *bh)
1323 {
1324 	int ret;
1325 	handle_t *handle;
1326 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1327 	struct ocfs2_dinode *di;
1328 
1329 	trace_ocfs2_write_remove_suid(
1330 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
1331 			inode->i_mode);
1332 
1333 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1334 	if (IS_ERR(handle)) {
1335 		ret = PTR_ERR(handle);
1336 		mlog_errno(ret);
1337 		goto out;
1338 	}
1339 
1340 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1341 				      OCFS2_JOURNAL_ACCESS_WRITE);
1342 	if (ret < 0) {
1343 		mlog_errno(ret);
1344 		goto out_trans;
1345 	}
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);
1353 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
1354 
1355 	ocfs2_journal_dirty(handle, bh);
1356 
1357 out_trans:
1358 	ocfs2_commit_trans(osb, handle);
1359 out:
1360 	return ret;
1361 }
1362 
1363 /*
1364  * Will look for holes and unwritten extents in the range starting at
1365  * pos for count bytes (inclusive).
1366  */
1367 static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
1368 				       size_t count)
1369 {
1370 	int ret = 0;
1371 	unsigned int extent_flags;
1372 	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) {
1379 		ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
1380 					 &extent_flags);
1381 		if (ret < 0) {
1382 			mlog_errno(ret);
1383 			goto out;
1384 		}
1385 
1386 		if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
1387 			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 	}
1397 out:
1398 	return ret;
1399 }
1400 
1401 static int ocfs2_write_remove_suid(struct inode *inode)
1402 {
1403 	int ret;
1404 	struct buffer_head *bh = NULL;
1405 
1406 	ret = ocfs2_read_inode_block(inode, &bh);
1407 	if (ret < 0) {
1408 		mlog_errno(ret);
1409 		goto out;
1410 	}
1411 
1412 	ret =  __ocfs2_write_remove_suid(inode, bh);
1413 out:
1414 	brelse(bh);
1415 	return ret;
1416 }
1417 
1418 /*
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  */
1423 static 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;
1428 	u64 end = start + len;
1429 	struct buffer_head *di_bh = NULL;
1430 
1431 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1432 		ret = ocfs2_read_inode_block(inode, &di_bh);
1433 		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 	}
1451 
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 
1489 next:
1490 		cpos += alloc_size;
1491 		clusters -= alloc_size;
1492 	}
1493 
1494 	ret = 0;
1495 out:
1496 
1497 	brelse(di_bh);
1498 	return ret;
1499 }
1500 
1501 /*
1502  * Truncate a byte range, avoiding pages within partial clusters. This
1503  * preserves those pages for the zeroing code to write to.
1504  */
1505 static 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 
1522 static 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 	 */
1538 	trace_ocfs2_zero_partial_clusters(
1539 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
1540 		(unsigned long long)start, (unsigned long long)end);
1541 
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);
1551 	if (IS_ERR(handle)) {
1552 		ret = PTR_ERR(handle);
1553 		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 
1564 	trace_ocfs2_zero_partial_clusters_range1((unsigned long long)start,
1565 						 (unsigned long long)tmpend);
1566 
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 
1579 		trace_ocfs2_zero_partial_clusters_range2(
1580 			(unsigned long long)start, (unsigned long long)end);
1581 
1582 		ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1583 		if (ret)
1584 			mlog_errno(ret);
1585 	}
1586 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
1587 
1588 	ocfs2_commit_trans(osb, handle);
1589 out:
1590 	return ret;
1591 }
1592 
1593 static 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 */
1617 static 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) {
1630 		/*
1631 		 * remove an entire extent record.
1632 		 */
1633 		*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) {
1643 		/*
1644 		 * remove a partial extent record, which means we're
1645 		 * removing the last extent record.
1646 		 */
1647 		*trunc_cpos = trunc_start;
1648 		/*
1649 		 * skip hole if any.
1650 		 */
1651 		if (range < *trunc_end)
1652 			*trunc_end = range;
1653 		*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 
1673 static int ocfs2_remove_inode_range(struct inode *inode,
1674 				    struct buffer_head *di_bh, u64 byte_start,
1675 				    u64 byte_len)
1676 {
1677 	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;
1680 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1681 	struct ocfs2_cached_dealloc_ctxt dealloc;
1682 	struct address_space *mapping = inode->i_mapping;
1683 	struct ocfs2_extent_tree et;
1684 	struct ocfs2_path *path = NULL;
1685 	struct ocfs2_extent_list *el = NULL;
1686 	struct ocfs2_extent_rec *rec = NULL;
1687 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1688 	u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
1689 
1690 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
1691 	ocfs2_init_dealloc_ctxt(&dealloc);
1692 
1693 	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 
1698 	if (byte_len == 0)
1699 		return 0;
1700 
1701 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1702 		ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
1703 					    byte_start + byte_len, 0);
1704 		if (ret) {
1705 			mlog_errno(ret);
1706 			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;
1717 	}
1718 
1719 	/*
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 	 */
1724 
1725 	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 
1740 	trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
1741 	trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
1742 	cluster_in_el = trunc_end;
1743 
1744 	ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1745 	if (ret) {
1746 		mlog_errno(ret);
1747 		goto out;
1748 	}
1749 
1750 	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);
1761 		if (ret) {
1762 			mlog_errno(ret);
1763 			goto out;
1764 		}
1765 
1766 		el = path_leaf_el(path);
1767 
1768 		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);
1779 			if (ret) {
1780 				mlog_errno(ret);
1781 				goto out;
1782 			}
1783 
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,
1814 					       &dealloc, refcount_loc, false);
1815 		if (ret < 0) {
1816 			mlog_errno(ret);
1817 			goto out;
1818 		}
1819 
1820 		cluster_in_el = trunc_end;
1821 
1822 		ocfs2_reinit_path(path, 1);
1823 	}
1824 
1825 	ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1826 
1827 out:
1828 	ocfs2_free_path(path);
1829 	ocfs2_schedule_truncate_log_flush(osb, 1);
1830 	ocfs2_run_deallocs(osb, &dealloc);
1831 
1832 	return ret;
1833 }
1834 
1835 /*
1836  * Parts of this function taken from xfs_change_file_space()
1837  */
1838 static 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)
1842 {
1843 	int ret;
1844 	s64 llen;
1845 	loff_t size;
1846 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1847 	struct buffer_head *di_bh = NULL;
1848 	handle_t *handle;
1849 	unsigned long long max_off = inode->i_sb->s_maxbytes;
1850 
1851 	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 
1865 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
1866 	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;
1873 		goto out_inode_unlock;
1874 	}
1875 
1876 	switch (sr->l_whence) {
1877 	case 0: /*SEEK_SET*/
1878 		break;
1879 	case 1: /*SEEK_CUR*/
1880 		sr->l_start += f_pos;
1881 		break;
1882 	case 2: /*SEEK_END*/
1883 		sr->l_start += i_size_read(inode);
1884 		break;
1885 	default:
1886 		ret = -EINVAL;
1887 		goto out_inode_unlock;
1888 	}
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;
1898 		goto out_inode_unlock;
1899 	}
1900 	size = sr->l_start + sr->l_len;
1901 
1902 	if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64 ||
1903 	    cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) {
1904 		if (sr->l_len <= 0) {
1905 			ret = -EINVAL;
1906 			goto out_inode_unlock;
1907 		}
1908 	}
1909 
1910 	if (file && should_remove_suid(file->f_path.dentry)) {
1911 		ret = __ocfs2_write_remove_suid(inode, di_bh);
1912 		if (ret) {
1913 			mlog_errno(ret);
1914 			goto out_inode_unlock;
1915 		}
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);
1940 		goto out_inode_unlock;
1941 	}
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);
1950 		goto out_inode_unlock;
1951 	}
1952 
1953 	if (change_size && i_size_read(inode) < size)
1954 		i_size_write(inode, size);
1955 
1956 	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 
1961 	if (file && (file->f_flags & O_SYNC))
1962 		handle->h_sync = 1;
1963 
1964 	ocfs2_commit_trans(osb, handle);
1965 
1966 out_inode_unlock:
1967 	brelse(di_bh);
1968 	ocfs2_inode_unlock(inode, 1);
1969 out_rw_unlock:
1970 	ocfs2_rw_unlock(inode, 1);
1971 
1972 out:
1973 	mutex_unlock(&inode->i_mutex);
1974 	return ret;
1975 }
1976 
1977 int ocfs2_change_file_space(struct file *file, unsigned int cmd,
1978 			    struct ocfs2_space_resv *sr)
1979 {
1980 	struct inode *inode = file_inode(file);
1981 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1982 	int ret;
1983 
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 
1997 	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;
2003 }
2004 
2005 static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
2006 			    loff_t len)
2007 {
2008 	struct inode *inode = file_inode(file);
2009 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2010 	struct ocfs2_space_resv sr;
2011 	int change_size = 1;
2012 	int cmd = OCFS2_IOC_RESVSP64;
2013 
2014 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2015 		return -EOPNOTSUPP;
2016 	if (!ocfs2_writes_unwritten_extents(osb))
2017 		return -EOPNOTSUPP;
2018 
2019 	if (mode & FALLOC_FL_KEEP_SIZE)
2020 		change_size = 0;
2021 
2022 	if (mode & FALLOC_FL_PUNCH_HOLE)
2023 		cmd = OCFS2_IOC_UNRESVSP64;
2024 
2025 	sr.l_whence = 0;
2026 	sr.l_start = (s64)offset;
2027 	sr.l_len = (s64)len;
2028 
2029 	return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
2030 					 change_size);
2031 }
2032 
2033 int 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)) ||
2042 	    !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
2043 	    OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2044 		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 	}
2068 out:
2069 	return ret;
2070 }
2071 
2072 static 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 
2082 static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
2083 					    struct file *file,
2084 					    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 
2101 	ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
2102 	if (ret)
2103 		mlog_errno(ret);
2104 out:
2105 	brelse(di_bh);
2106 	return ret;
2107 }
2108 
2109 static int ocfs2_prepare_inode_for_write(struct file *file,
2110 					 loff_t pos,
2111 					 size_t count,
2112 					 int appending,
2113 					 int *direct_io,
2114 					 int *has_refcount)
2115 {
2116 	int ret = 0, meta_level = 0;
2117 	struct dentry *dentry = file->f_path.dentry;
2118 	struct inode *inode = d_inode(dentry);
2119 	loff_t end;
2120 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2121 	int full_coherency = !(osb->s_mount_opt &
2122 		OCFS2_MOUNT_COHERENCY_BUFFERED);
2123 
2124 	/*
2125 	 * We start with a read level meta lock and only jump to an ex
2126 	 * if we need to make modifications here.
2127 	 */
2128 	for(;;) {
2129 		ret = ocfs2_inode_lock(inode, NULL, meta_level);
2130 		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
2141 		 * proceed, this will lead us to recursively lock the
2142 		 * 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. */
2145 		if (should_remove_suid(dentry)) {
2146 			if (meta_level == 0) {
2147 				ocfs2_inode_unlock(inode, meta_level);
2148 				meta_level = 1;
2149 				continue;
2150 			}
2151 
2152 			ret = ocfs2_write_remove_suid(inode);
2153 			if (ret < 0) {
2154 				mlog_errno(ret);
2155 				goto out_unlock;
2156 			}
2157 		}
2158 
2159 		end = pos + count;
2160 
2161 		ret = ocfs2_check_range_for_refcount(inode, pos, count);
2162 		if (ret == 1) {
2163 			ocfs2_inode_unlock(inode, meta_level);
2164 			meta_level = -1;
2165 
2166 			ret = ocfs2_prepare_inode_for_refcount(inode,
2167 							       file,
2168 							       pos,
2169 							       count,
2170 							       &meta_level);
2171 			if (has_refcount)
2172 				*has_refcount = 1;
2173 			if (direct_io)
2174 				*direct_io = 0;
2175 		}
2176 
2177 		if (ret < 0) {
2178 			mlog_errno(ret);
2179 			goto out_unlock;
2180 		}
2181 
2182 		/*
2183 		 * Skip the O_DIRECT checks if we don't need
2184 		 * them.
2185 		 */
2186 		if (!direct_io || !(*direct_io))
2187 			break;
2188 
2189 		/*
2190 		 * 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 		/*
2199 		 * 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 		 */
2204 		if (end > i_size_read(inode) && !full_coherency) {
2205 			*direct_io = 0;
2206 			break;
2207 		}
2208 
2209 		/*
2210 		 * 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 		/*
2219 		 * 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.
2223 		 */
2224 		ret = ocfs2_check_range_for_holes(inode, pos, count);
2225 		if (ret == 1) {
2226 			/*
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;
2233 			ret = 0;
2234 		} else if (ret < 0)
2235 			mlog_errno(ret);
2236 		break;
2237 	}
2238 
2239 out_unlock:
2240 	trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
2241 					    pos, appending, count,
2242 					    direct_io, has_refcount);
2243 
2244 	if (meta_level >= 0)
2245 		ocfs2_inode_unlock(inode, meta_level);
2246 
2247 out:
2248 	return ret;
2249 }
2250 
2251 static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
2252 				    struct iov_iter *from)
2253 {
2254 	int direct_io, appending, rw_level;
2255 	int can_do_direct, has_refcount = 0;
2256 	ssize_t written = 0;
2257 	ssize_t ret;
2258 	size_t count = iov_iter_count(from), orig_count;
2259 	loff_t old_size;
2260 	u32 old_clusters;
2261 	struct file *file = iocb->ki_filp;
2262 	struct inode *inode = file_inode(file);
2263 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2264 	int full_coherency = !(osb->s_mount_opt &
2265 			       OCFS2_MOUNT_COHERENCY_BUFFERED);
2266 	int unaligned_dio = 0;
2267 	int dropped_dio = 0;
2268 
2269 	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,
2273 		(unsigned int)from->nr_segs);	/* GRRRRR */
2274 
2275 	if (count == 0)
2276 		return 0;
2277 
2278 	appending = iocb->ki_flags & IOCB_APPEND ? 1 : 0;
2279 	direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0;
2280 
2281 	mutex_lock(&inode->i_mutex);
2282 
2283 relock:
2284 	/*
2285 	 * Concurrent O_DIRECT writes are allowed with
2286 	 * mount_option "coherency=buffered".
2287 	 */
2288 	rw_level = (!direct_io || full_coherency);
2289 
2290 	ret = ocfs2_rw_lock(inode, rw_level);
2291 	if (ret < 0) {
2292 		mlog_errno(ret);
2293 		goto out_mutex;
2294 	}
2295 
2296 	/*
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);
2309 			goto out;
2310 		}
2311 
2312 		ocfs2_inode_unlock(inode, 1);
2313 	}
2314 
2315 	orig_count = iov_iter_count(from);
2316 	ret = generic_write_checks(iocb, from);
2317 	if (ret <= 0) {
2318 		if (ret)
2319 			mlog_errno(ret);
2320 		goto out;
2321 	}
2322 	count = ret;
2323 
2324 	can_do_direct = direct_io;
2325 	ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, count, appending,
2326 					    &can_do_direct, &has_refcount);
2327 	if (ret < 0) {
2328 		mlog_errno(ret);
2329 		goto out;
2330 	}
2331 
2332 	if (direct_io && !is_sync_kiocb(iocb))
2333 		unaligned_dio = ocfs2_is_io_unaligned(inode, count, iocb->ki_pos);
2334 
2335 	/*
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);
2341 
2342 		rw_level = -1;
2343 
2344 		direct_io = 0;
2345 		iocb->ki_flags &= ~IOCB_DIRECT;
2346 		iov_iter_reexpand(from, orig_count);
2347 		dropped_dio = 1;
2348 		goto relock;
2349 	}
2350 
2351 	if (unaligned_dio) {
2352 		/*
2353 		 * Wait on previous unaligned aio to complete before
2354 		 * proceeding.
2355 		 */
2356 		mutex_lock(&OCFS2_I(inode)->ip_unaligned_aio);
2357 		/* Mark the iocb as needing an unlock in ocfs2_dio_end_io */
2358 		ocfs2_iocb_set_unaligned_aio(iocb);
2359 	}
2360 
2361 	/*
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 
2368 	/* communicate with ocfs2_dio_end_io */
2369 	ocfs2_iocb_set_rw_locked(iocb, rw_level);
2370 
2371 	written = __generic_file_write_iter(iocb, from);
2372 	/* buffered aio wouldn't have proper lock coverage today */
2373 	BUG_ON(written == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
2374 
2375 	if (unlikely(written <= 0))
2376 		goto no_sync;
2377 
2378 	if (((file->f_flags & O_DSYNC) && !direct_io) ||
2379 	    IS_SYNC(inode) || dropped_dio) {
2380 		ret = filemap_fdatawrite_range(file->f_mapping,
2381 					       iocb->ki_pos - written,
2382 					       iocb->ki_pos - 1);
2383 		if (ret < 0)
2384 			written = ret;
2385 
2386 		if (!ret) {
2387 			ret = jbd2_journal_force_commit(osb->journal->j_journal);
2388 			if (ret < 0)
2389 				written = ret;
2390 		}
2391 
2392 		if (!ret)
2393 			ret = filemap_fdatawait_range(file->f_mapping,
2394 						      iocb->ki_pos - written,
2395 						      iocb->ki_pos - 1);
2396 	}
2397 
2398 no_sync:
2399 	/*
2400 	 * 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
2402 	 * it can unlock our rw lock.
2403 	 * 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 	 */
2408 	if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
2409 		rw_level = -1;
2410 		unaligned_dio = 0;
2411 	}
2412 
2413 	if (unaligned_dio) {
2414 		ocfs2_iocb_clear_unaligned_aio(iocb);
2415 		mutex_unlock(&OCFS2_I(inode)->ip_unaligned_aio);
2416 	}
2417 
2418 out:
2419 	if (rw_level != -1)
2420 		ocfs2_rw_unlock(inode, rw_level);
2421 
2422 out_mutex:
2423 	mutex_unlock(&inode->i_mutex);
2424 
2425 	if (written)
2426 		ret = written;
2427 	return ret;
2428 }
2429 
2430 static 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 {
2436 	int ret = 0, lock_level = 0;
2437 	struct inode *inode = file_inode(in);
2438 
2439 	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);
2443 
2444 	/*
2445 	 * See the comment in ocfs2_file_read_iter()
2446 	 */
2447 	ret = ocfs2_inode_lock_atime(inode, in->f_path.mnt, &lock_level);
2448 	if (ret < 0) {
2449 		mlog_errno(ret);
2450 		goto bail;
2451 	}
2452 	ocfs2_inode_unlock(inode, lock_level);
2453 
2454 	ret = generic_file_splice_read(in, ppos, pipe, len, flags);
2455 
2456 bail:
2457 	return ret;
2458 }
2459 
2460 static ssize_t ocfs2_file_read_iter(struct kiocb *iocb,
2461 				   struct iov_iter *to)
2462 {
2463 	int ret = 0, rw_level = -1, lock_level = 0;
2464 	struct file *filp = iocb->ki_filp;
2465 	struct inode *inode = file_inode(filp);
2466 
2467 	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,
2470 			filp->f_path.dentry->d_name.name,
2471 			to->nr_segs);	/* GRRRRR */
2472 
2473 
2474 	if (!inode) {
2475 		ret = -EINVAL;
2476 		mlog_errno(ret);
2477 		goto bail;
2478 	}
2479 
2480 	/*
2481 	 * buffered reads protect themselves in ->readpage().  O_DIRECT reads
2482 	 * need locks to protect pending reads from racing with truncate.
2483 	 */
2484 	if (iocb->ki_flags & IOCB_DIRECT) {
2485 		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 */
2492 		ocfs2_iocb_set_rw_locked(iocb, rw_level);
2493 	}
2494 
2495 	/*
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.
2499 	 *
2500 	 * Take and drop the meta data lock to update inode fields
2501 	 * like i_size. This allows the checks down below
2502 	 * generic_file_aio_read() a chance of actually working.
2503 	 */
2504 	ret = ocfs2_inode_lock_atime(inode, filp->f_path.mnt, &lock_level);
2505 	if (ret < 0) {
2506 		mlog_errno(ret);
2507 		goto bail;
2508 	}
2509 	ocfs2_inode_unlock(inode, lock_level);
2510 
2511 	ret = generic_file_read_iter(iocb, to);
2512 	trace_generic_file_aio_read_ret(ret);
2513 
2514 	/* buffered aio wouldn't have proper lock coverage today */
2515 	BUG_ON(ret == -EIOCBQUEUED && !(iocb->ki_flags & IOCB_DIRECT));
2516 
2517 	/* see ocfs2_file_write_iter */
2518 	if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2519 		rw_level = -1;
2520 	}
2521 
2522 bail:
2523 	if (rw_level != -1)
2524 		ocfs2_rw_unlock(inode, rw_level);
2525 
2526 	return ret;
2527 }
2528 
2529 /* Refer generic_file_llseek_unlocked() */
2530 static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence)
2531 {
2532 	struct inode *inode = file->f_mapping->host;
2533 	int ret = 0;
2534 
2535 	mutex_lock(&inode->i_mutex);
2536 
2537 	switch (whence) {
2538 	case SEEK_SET:
2539 		break;
2540 	case SEEK_END:
2541 		/* 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);
2551 		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:
2561 		ret = ocfs2_seek_data_hole_offset(file, &offset, whence);
2562 		if (ret)
2563 			goto out;
2564 		break;
2565 	default:
2566 		ret = -EINVAL;
2567 		goto out;
2568 	}
2569 
2570 	offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
2571 
2572 out:
2573 	mutex_unlock(&inode->i_mutex);
2574 	if (ret)
2575 		return ret;
2576 	return offset;
2577 }
2578 
2579 const struct inode_operations ocfs2_file_iops = {
2580 	.setattr	= ocfs2_setattr,
2581 	.getattr	= ocfs2_getattr,
2582 	.permission	= ocfs2_permission,
2583 	.setxattr	= generic_setxattr,
2584 	.getxattr	= generic_getxattr,
2585 	.listxattr	= ocfs2_listxattr,
2586 	.removexattr	= generic_removexattr,
2587 	.fiemap		= ocfs2_fiemap,
2588 	.get_acl	= ocfs2_iop_get_acl,
2589 	.set_acl	= ocfs2_iop_set_acl,
2590 };
2591 
2592 const struct inode_operations ocfs2_special_file_iops = {
2593 	.setattr	= ocfs2_setattr,
2594 	.getattr	= ocfs2_getattr,
2595 	.permission	= ocfs2_permission,
2596 	.get_acl	= ocfs2_iop_get_acl,
2597 	.set_acl	= ocfs2_iop_set_acl,
2598 };
2599 
2600 /*
2601  * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
2602  * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
2603  */
2604 const struct file_operations ocfs2_fops = {
2605 	.llseek		= ocfs2_file_llseek,
2606 	.mmap		= ocfs2_mmap,
2607 	.fsync		= ocfs2_sync_file,
2608 	.release	= ocfs2_file_release,
2609 	.open		= ocfs2_file_open,
2610 	.read_iter	= ocfs2_file_read_iter,
2611 	.write_iter	= ocfs2_file_write_iter,
2612 	.unlocked_ioctl	= ocfs2_ioctl,
2613 #ifdef CONFIG_COMPAT
2614 	.compat_ioctl   = ocfs2_compat_ioctl,
2615 #endif
2616 	.lock		= ocfs2_lock,
2617 	.flock		= ocfs2_flock,
2618 	.splice_read	= ocfs2_file_splice_read,
2619 	.splice_write	= iter_file_splice_write,
2620 	.fallocate	= ocfs2_fallocate,
2621 };
2622 
2623 const struct file_operations ocfs2_dops = {
2624 	.llseek		= generic_file_llseek,
2625 	.read		= generic_read_dir,
2626 	.iterate	= ocfs2_readdir,
2627 	.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  */
2650 const struct file_operations ocfs2_fops_no_plocks = {
2651 	.llseek		= ocfs2_file_llseek,
2652 	.mmap		= ocfs2_mmap,
2653 	.fsync		= ocfs2_sync_file,
2654 	.release	= ocfs2_file_release,
2655 	.open		= ocfs2_file_open,
2656 	.read_iter	= ocfs2_file_read_iter,
2657 	.write_iter	= ocfs2_file_write_iter,
2658 	.unlocked_ioctl	= ocfs2_ioctl,
2659 #ifdef CONFIG_COMPAT
2660 	.compat_ioctl   = ocfs2_compat_ioctl,
2661 #endif
2662 	.flock		= ocfs2_flock,
2663 	.splice_read	= ocfs2_file_splice_read,
2664 	.splice_write	= iter_file_splice_write,
2665 	.fallocate	= ocfs2_fallocate,
2666 };
2667 
2668 const struct file_operations ocfs2_dops_no_plocks = {
2669 	.llseek		= generic_file_llseek,
2670 	.read		= generic_read_dir,
2671 	.iterate	= ocfs2_readdir,
2672 	.fsync		= ocfs2_sync_file,
2673 	.release	= ocfs2_dir_release,
2674 	.open		= ocfs2_dir_open,
2675 	.unlocked_ioctl	= ocfs2_ioctl,
2676 #ifdef CONFIG_COMPAT
2677 	.compat_ioctl   = ocfs2_compat_ioctl,
2678 #endif
2679 	.flock		= ocfs2_flock,
2680 };
2681