xref: /linux/fs/ocfs2/file.c (revision 738ce4979c802ba0f1f8249a893aa7db1fbb2cf8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * file.c
4  *
5  * File open, close, extend, truncate
6  *
7  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
8  */
9 
10 #include <linux/capability.h>
11 #include <linux/fs.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/highmem.h>
15 #include <linux/pagemap.h>
16 #include <linux/uio.h>
17 #include <linux/sched.h>
18 #include <linux/splice.h>
19 #include <linux/mount.h>
20 #include <linux/writeback.h>
21 #include <linux/falloc.h>
22 #include <linux/filelock.h>
23 #include <linux/quotaops.h>
24 #include <linux/blkdev.h>
25 #include <linux/backing-dev.h>
26 
27 #include <cluster/masklog.h>
28 
29 #include "ocfs2.h"
30 
31 #include "alloc.h"
32 #include "aops.h"
33 #include "dir.h"
34 #include "dlmglue.h"
35 #include "extent_map.h"
36 #include "file.h"
37 #include "sysfile.h"
38 #include "inode.h"
39 #include "ioctl.h"
40 #include "journal.h"
41 #include "locks.h"
42 #include "mmap.h"
43 #include "suballoc.h"
44 #include "super.h"
45 #include "xattr.h"
46 #include "acl.h"
47 #include "quota.h"
48 #include "refcounttree.h"
49 #include "ocfs2_trace.h"
50 
51 #include "buffer_head_io.h"
52 
53 static int ocfs2_init_file_private(struct inode *inode, struct file *file)
54 {
55 	struct ocfs2_file_private *fp;
56 
57 	fp = kzalloc_obj(struct ocfs2_file_private);
58 	if (!fp)
59 		return -ENOMEM;
60 
61 	fp->fp_file = file;
62 	mutex_init(&fp->fp_mutex);
63 	ocfs2_file_lock_res_init(&fp->fp_flock, fp);
64 	file->private_data = fp;
65 
66 	return 0;
67 }
68 
69 static void ocfs2_free_file_private(struct inode *inode, struct file *file)
70 {
71 	struct ocfs2_file_private *fp = file->private_data;
72 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
73 
74 	if (fp) {
75 		ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
76 		ocfs2_lock_res_free(&fp->fp_flock);
77 		kfree(fp);
78 		file->private_data = NULL;
79 	}
80 }
81 
82 static int ocfs2_file_open(struct inode *inode, struct file *file)
83 {
84 	int status;
85 	int mode = file->f_flags;
86 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
87 
88 	trace_ocfs2_file_open(inode, file, file->f_path.dentry,
89 			      (unsigned long long)oi->ip_blkno,
90 			      file->f_path.dentry->d_name.len,
91 			      file->f_path.dentry->d_name.name, mode);
92 
93 	if (file->f_mode & FMODE_WRITE) {
94 		status = dquot_initialize(inode);
95 		if (status)
96 			goto leave;
97 	}
98 
99 	spin_lock(&oi->ip_lock);
100 
101 	/* Check that the inode hasn't been wiped from disk by another
102 	 * node. If it hasn't then we're safe as long as we hold the
103 	 * spin lock until our increment of open count. */
104 	if (oi->ip_flags & OCFS2_INODE_DELETED) {
105 		spin_unlock(&oi->ip_lock);
106 
107 		status = -ENOENT;
108 		goto leave;
109 	}
110 
111 	if (mode & O_DIRECT)
112 		oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
113 
114 	oi->ip_open_count++;
115 	spin_unlock(&oi->ip_lock);
116 
117 	status = ocfs2_init_file_private(inode, file);
118 	if (status) {
119 		/*
120 		 * We want to set open count back if we're failing the
121 		 * open.
122 		 */
123 		spin_lock(&oi->ip_lock);
124 		oi->ip_open_count--;
125 		spin_unlock(&oi->ip_lock);
126 	}
127 
128 	file->f_mode |= FMODE_NOWAIT;
129 
130 leave:
131 	return status;
132 }
133 
134 static int ocfs2_file_release(struct inode *inode, struct file *file)
135 {
136 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
137 
138 	spin_lock(&oi->ip_lock);
139 	if (!--oi->ip_open_count)
140 		oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
141 
142 	trace_ocfs2_file_release(inode, file, file->f_path.dentry,
143 				 oi->ip_blkno,
144 				 file->f_path.dentry->d_name.len,
145 				 file->f_path.dentry->d_name.name,
146 				 oi->ip_open_count);
147 	spin_unlock(&oi->ip_lock);
148 
149 	ocfs2_free_file_private(inode, file);
150 
151 	return 0;
152 }
153 
154 static int ocfs2_dir_open(struct inode *inode, struct file *file)
155 {
156 	return ocfs2_init_file_private(inode, file);
157 }
158 
159 static int ocfs2_dir_release(struct inode *inode, struct file *file)
160 {
161 	ocfs2_free_file_private(inode, file);
162 	return 0;
163 }
164 
165 static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
166 			   int datasync)
167 {
168 	int err = 0;
169 	struct inode *inode = file->f_mapping->host;
170 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
171 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
172 	journal_t *journal = osb->journal->j_journal;
173 	int ret;
174 	tid_t commit_tid;
175 	bool needs_barrier = false;
176 
177 	trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
178 			      oi->ip_blkno,
179 			      file->f_path.dentry->d_name.len,
180 			      file->f_path.dentry->d_name.name,
181 			      (unsigned long long)datasync);
182 
183 	if (unlikely(ocfs2_emergency_state(osb)))
184 		return -EROFS;
185 
186 	err = file_write_and_wait_range(file, start, end);
187 	if (err)
188 		return err;
189 
190 	commit_tid = datasync ? oi->i_datasync_tid : oi->i_sync_tid;
191 	if (journal->j_flags & JBD2_BARRIER &&
192 	    !jbd2_trans_will_send_data_barrier(journal, commit_tid))
193 		needs_barrier = true;
194 	err = jbd2_complete_transaction(journal, commit_tid);
195 	if (needs_barrier) {
196 		ret = blkdev_issue_flush(inode->i_sb->s_bdev);
197 		if (!err)
198 			err = ret;
199 	}
200 
201 	if (err)
202 		mlog_errno(err);
203 
204 	return (err < 0) ? -EIO : 0;
205 }
206 
207 int ocfs2_should_update_atime(struct inode *inode,
208 			      struct vfsmount *vfsmnt)
209 {
210 	struct timespec64 now;
211 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
212 
213 	if (unlikely(ocfs2_emergency_state(osb)))
214 		return 0;
215 
216 	if ((inode->i_flags & S_NOATIME) ||
217 	    ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode)))
218 		return 0;
219 
220 	/*
221 	 * We can be called with no vfsmnt structure - NFSD will
222 	 * sometimes do this.
223 	 *
224 	 * Note that our action here is different than touch_atime() -
225 	 * if we can't tell whether this is a noatime mount, then we
226 	 * don't know whether to trust the value of s_atime_quantum.
227 	 */
228 	if (vfsmnt == NULL)
229 		return 0;
230 
231 	if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
232 	    ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
233 		return 0;
234 
235 	if (vfsmnt->mnt_flags & MNT_RELATIME) {
236 		struct timespec64 ctime = inode_get_ctime(inode);
237 		struct timespec64 atime = inode_get_atime(inode);
238 		struct timespec64 mtime = inode_get_mtime(inode);
239 
240 		if ((timespec64_compare(&atime, &mtime) <= 0) ||
241 		    (timespec64_compare(&atime, &ctime) <= 0))
242 			return 1;
243 
244 		return 0;
245 	}
246 
247 	now = current_time(inode);
248 	if ((now.tv_sec - inode_get_atime_sec(inode) <= osb->s_atime_quantum))
249 		return 0;
250 	else
251 		return 1;
252 }
253 
254 int ocfs2_update_inode_atime(struct inode *inode,
255 			     struct buffer_head *bh)
256 {
257 	int ret;
258 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
259 	handle_t *handle;
260 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
261 
262 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
263 	if (IS_ERR(handle)) {
264 		ret = PTR_ERR(handle);
265 		mlog_errno(ret);
266 		goto out;
267 	}
268 
269 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
270 				      OCFS2_JOURNAL_ACCESS_WRITE);
271 	if (ret) {
272 		mlog_errno(ret);
273 		goto out_commit;
274 	}
275 
276 	/*
277 	 * Don't use ocfs2_mark_inode_dirty() here as we don't always
278 	 * have i_rwsem to guard against concurrent changes to other
279 	 * inode fields.
280 	 */
281 	inode_set_atime_to_ts(inode, current_time(inode));
282 	di->i_atime = cpu_to_le64(inode_get_atime_sec(inode));
283 	di->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode));
284 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
285 	ocfs2_journal_dirty(handle, bh);
286 
287 out_commit:
288 	ocfs2_commit_trans(osb, handle);
289 out:
290 	return ret;
291 }
292 
293 int ocfs2_set_inode_size(handle_t *handle,
294 				struct inode *inode,
295 				struct buffer_head *fe_bh,
296 				u64 new_i_size)
297 {
298 	int status;
299 
300 	i_size_write(inode, new_i_size);
301 	inode->i_blocks = ocfs2_inode_sector_count(inode);
302 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
303 
304 	status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
305 	if (status < 0) {
306 		mlog_errno(status);
307 		goto bail;
308 	}
309 
310 bail:
311 	return status;
312 }
313 
314 int ocfs2_simple_size_update(struct inode *inode,
315 			     struct buffer_head *di_bh,
316 			     u64 new_i_size)
317 {
318 	int ret;
319 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
320 	handle_t *handle = NULL;
321 
322 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
323 	if (IS_ERR(handle)) {
324 		ret = PTR_ERR(handle);
325 		mlog_errno(ret);
326 		goto out;
327 	}
328 
329 	ret = ocfs2_set_inode_size(handle, inode, di_bh,
330 				   new_i_size);
331 	if (ret < 0)
332 		mlog_errno(ret);
333 
334 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
335 	ocfs2_commit_trans(osb, handle);
336 out:
337 	return ret;
338 }
339 
340 static int ocfs2_cow_file_pos(struct inode *inode,
341 			      struct buffer_head *fe_bh,
342 			      u64 offset)
343 {
344 	int status;
345 	u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
346 	unsigned int num_clusters = 0;
347 	unsigned int ext_flags = 0;
348 
349 	/*
350 	 * If the new offset is aligned to the range of the cluster, there is
351 	 * no space for ocfs2_zero_range_for_truncate to fill, so no need to
352 	 * CoW either.
353 	 */
354 	if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
355 		return 0;
356 
357 	status = ocfs2_get_clusters(inode, cpos, &phys,
358 				    &num_clusters, &ext_flags);
359 	if (status) {
360 		mlog_errno(status);
361 		goto out;
362 	}
363 
364 	if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
365 		goto out;
366 
367 	return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
368 
369 out:
370 	return status;
371 }
372 
373 static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
374 				     struct inode *inode,
375 				     struct buffer_head *fe_bh,
376 				     u64 new_i_size)
377 {
378 	int status;
379 	handle_t *handle;
380 	struct ocfs2_dinode *di;
381 	u64 cluster_bytes;
382 
383 	/*
384 	 * We need to CoW the cluster contains the offset if it is reflinked
385 	 * since we will call ocfs2_zero_range_for_truncate later which will
386 	 * write "0" from offset to the end of the cluster.
387 	 */
388 	status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
389 	if (status) {
390 		mlog_errno(status);
391 		return status;
392 	}
393 
394 	/* TODO: This needs to actually orphan the inode in this
395 	 * transaction. */
396 
397 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
398 	if (IS_ERR(handle)) {
399 		status = PTR_ERR(handle);
400 		mlog_errno(status);
401 		goto out;
402 	}
403 
404 	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
405 					 OCFS2_JOURNAL_ACCESS_WRITE);
406 	if (status < 0) {
407 		mlog_errno(status);
408 		goto out_commit;
409 	}
410 
411 	/*
412 	 * Do this before setting i_size.
413 	 */
414 	cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
415 	status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
416 					       cluster_bytes);
417 	if (status) {
418 		mlog_errno(status);
419 		goto out_commit;
420 	}
421 
422 	i_size_write(inode, new_i_size);
423 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
424 
425 	di = (struct ocfs2_dinode *) fe_bh->b_data;
426 	di->i_size = cpu_to_le64(new_i_size);
427 	di->i_ctime = di->i_mtime = cpu_to_le64(inode_get_ctime_sec(inode));
428 	di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
429 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
430 
431 	ocfs2_journal_dirty(handle, fe_bh);
432 
433 out_commit:
434 	ocfs2_commit_trans(osb, handle);
435 out:
436 	return status;
437 }
438 
439 int ocfs2_truncate_file(struct inode *inode,
440 			       struct buffer_head *di_bh,
441 			       u64 new_i_size)
442 {
443 	int status = 0;
444 	struct ocfs2_dinode *fe = NULL;
445 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
446 
447 	/*
448 	 * On local mounts ocfs2_inode_lock_update() skips the inode
449 	 * refresh path, so truncation still needs to reject an inode
450 	 * state that no longer matches di_bh.
451 	 */
452 	fe = (struct ocfs2_dinode *) di_bh->b_data;
453 
454 	trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno,
455 				  (unsigned long long)le64_to_cpu(fe->i_size),
456 				  (unsigned long long)new_i_size);
457 
458 	if (unlikely(le64_to_cpu(fe->i_size) != i_size_read(inode))) {
459 		status = ocfs2_error(inode->i_sb,
460 				     "Inode %llu has inconsistent i_size: inode = %lld, dinode = %llu, i_flags = 0x%x\n",
461 				     (unsigned long long)OCFS2_I(inode)->ip_blkno,
462 				     i_size_read(inode),
463 				     (unsigned long long)le64_to_cpu(fe->i_size),
464 				     le32_to_cpu(fe->i_flags));
465 		goto bail;
466 	}
467 
468 	if (new_i_size > le64_to_cpu(fe->i_size)) {
469 		trace_ocfs2_truncate_file_error(
470 			(unsigned long long)le64_to_cpu(fe->i_size),
471 			(unsigned long long)new_i_size);
472 		status = -EINVAL;
473 		mlog_errno(status);
474 		goto bail;
475 	}
476 
477 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
478 
479 	ocfs2_resv_discard(&osb->osb_la_resmap,
480 			   &OCFS2_I(inode)->ip_la_data_resv);
481 
482 	/*
483 	 * The inode lock forced other nodes to sync and drop their
484 	 * pages, which (correctly) happens even if we have a truncate
485 	 * without allocation change - ocfs2 cluster sizes can be much
486 	 * greater than page size, so we have to truncate them
487 	 * anyway.
488 	 */
489 
490 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
491 		unmap_mapping_range(inode->i_mapping,
492 				    new_i_size + PAGE_SIZE - 1, 0, 1);
493 		truncate_inode_pages(inode->i_mapping, new_i_size);
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 	unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
513 	truncate_inode_pages(inode->i_mapping, new_i_size);
514 
515 	status = ocfs2_commit_truncate(osb, inode, di_bh);
516 	if (status < 0) {
517 		mlog_errno(status);
518 		goto bail_unlock_sem;
519 	}
520 
521 	/* TODO: orphan dir cleanup here. */
522 bail_unlock_sem:
523 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
524 
525 bail:
526 	if (!status && OCFS2_I(inode)->ip_clusters == 0)
527 		status = ocfs2_try_remove_refcount_tree(inode, di_bh);
528 
529 	return status;
530 }
531 
532 /*
533  * extend file allocation only here.
534  * we'll update all the disk stuff, and oip->alloc_size
535  *
536  * expect stuff to be locked, a transaction started and enough data /
537  * metadata reservations in the contexts.
538  *
539  * Will return -EAGAIN, and a reason if a restart is needed.
540  * If passed in, *reason will always be set, even in error.
541  */
542 int ocfs2_add_inode_data(struct ocfs2_super *osb,
543 			 struct inode *inode,
544 			 u32 *logical_offset,
545 			 u32 clusters_to_add,
546 			 int mark_unwritten,
547 			 struct buffer_head *fe_bh,
548 			 handle_t *handle,
549 			 struct ocfs2_alloc_context *data_ac,
550 			 struct ocfs2_alloc_context *meta_ac,
551 			 enum ocfs2_alloc_restarted *reason_ret)
552 {
553 	struct ocfs2_extent_tree et;
554 
555 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
556 	return ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
557 					   clusters_to_add, mark_unwritten,
558 					   data_ac, meta_ac, reason_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 /*
714  * While a write will already be ordering the data, a truncate will not.
715  * Thus, we need to explicitly order the zeroed pages.
716  */
717 static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode,
718 						      struct buffer_head *di_bh,
719 						      loff_t start_byte,
720 						      loff_t length)
721 {
722 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
723 	handle_t *handle = NULL;
724 	int ret = 0;
725 
726 	if (!ocfs2_should_order_data(inode))
727 		goto out;
728 
729 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
730 	if (IS_ERR(handle)) {
731 		ret = -ENOMEM;
732 		mlog_errno(ret);
733 		goto out;
734 	}
735 
736 	ret = ocfs2_jbd2_inode_add_write(handle, inode, start_byte, length);
737 	if (ret < 0) {
738 		mlog_errno(ret);
739 		goto out;
740 	}
741 
742 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
743 				      OCFS2_JOURNAL_ACCESS_WRITE);
744 	if (ret)
745 		mlog_errno(ret);
746 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
747 
748 out:
749 	if (ret) {
750 		if (!IS_ERR(handle))
751 			ocfs2_commit_trans(osb, handle);
752 		handle = ERR_PTR(ret);
753 	}
754 	return handle;
755 }
756 
757 /* Some parts of this taken from generic_cont_expand, which turned out
758  * to be too fragile to do exactly what we need without us having to
759  * worry about recursive locking in ->write_begin() and ->write_end(). */
760 static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
761 				 u64 abs_to, struct buffer_head *di_bh)
762 {
763 	struct address_space *mapping = inode->i_mapping;
764 	struct folio *folio;
765 	unsigned long index = abs_from >> PAGE_SHIFT;
766 	handle_t *handle;
767 	int ret = 0;
768 	unsigned zero_from, zero_to, block_start, block_end;
769 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
770 
771 	BUG_ON(abs_from >= abs_to);
772 	BUG_ON(abs_to > (((u64)index + 1) << PAGE_SHIFT));
773 	BUG_ON(abs_from & (inode->i_blkbits - 1));
774 
775 	handle = ocfs2_zero_start_ordered_transaction(inode, di_bh,
776 						      abs_from,
777 						      abs_to - abs_from);
778 	if (IS_ERR(handle)) {
779 		ret = PTR_ERR(handle);
780 		goto out;
781 	}
782 
783 	folio = __filemap_get_folio(mapping, index,
784 			FGP_LOCK | FGP_ACCESSED | FGP_CREAT, GFP_NOFS);
785 	if (IS_ERR(folio)) {
786 		ret = PTR_ERR(folio);
787 		mlog_errno(ret);
788 		goto out_commit_trans;
789 	}
790 
791 	/* Get the offsets within the folio that we want to zero */
792 	zero_from = offset_in_folio(folio, abs_from);
793 	zero_to = offset_in_folio(folio, abs_to);
794 	if (!zero_to)
795 		zero_to = folio_size(folio);
796 
797 	trace_ocfs2_write_zero_page(
798 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
799 			(unsigned long long)abs_from,
800 			(unsigned long long)abs_to,
801 			index, zero_from, zero_to);
802 
803 	/* We know that zero_from is block aligned */
804 	for (block_start = zero_from; block_start < zero_to;
805 	     block_start = block_end) {
806 		block_end = block_start + i_blocksize(inode);
807 
808 		/*
809 		 * block_start is block-aligned.  Bump it by one to force
810 		 * __block_write_begin and block_commit_write to zero the
811 		 * whole block.
812 		 */
813 		ret = __block_write_begin(folio, block_start + 1, 0,
814 					  ocfs2_get_block);
815 		if (ret < 0) {
816 			mlog_errno(ret);
817 			goto out_unlock;
818 		}
819 
820 
821 		/* must not update i_size! */
822 		block_commit_write(folio, block_start + 1, block_start + 1);
823 	}
824 
825 	/*
826 	 * fs-writeback will release the dirty pages without page lock
827 	 * whose offset are over inode size, the release happens at
828 	 * block_write_full_folio().
829 	 */
830 	i_size_write(inode, abs_to);
831 	inode->i_blocks = ocfs2_inode_sector_count(inode);
832 	di->i_size = cpu_to_le64((u64)i_size_read(inode));
833 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
834 	di->i_mtime = di->i_ctime = cpu_to_le64(inode_get_mtime_sec(inode));
835 	di->i_ctime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
836 	di->i_mtime_nsec = di->i_ctime_nsec;
837 	if (handle) {
838 		ocfs2_journal_dirty(handle, di_bh);
839 		ocfs2_update_inode_fsync_trans(handle, inode, 1);
840 	}
841 
842 out_unlock:
843 	folio_unlock(folio);
844 	folio_put(folio);
845 out_commit_trans:
846 	if (handle)
847 		ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
848 out:
849 	return ret;
850 }
851 
852 /*
853  * Find the next range to zero.  We do this in terms of bytes because
854  * that's what ocfs2_zero_extend() wants, and it is dealing with the
855  * pagecache.  We may return multiple extents.
856  *
857  * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
858  * needs to be zeroed.  range_start and range_end return the next zeroing
859  * range.  A subsequent call should pass the previous range_end as its
860  * zero_start.  If range_end is 0, there's nothing to do.
861  *
862  * Unwritten extents are skipped over.  Refcounted extents are CoWd.
863  */
864 static int ocfs2_zero_extend_get_range(struct inode *inode,
865 				       struct buffer_head *di_bh,
866 				       u64 zero_start, u64 zero_end,
867 				       u64 *range_start, u64 *range_end)
868 {
869 	int rc = 0, needs_cow = 0;
870 	u32 p_cpos, zero_clusters = 0;
871 	u32 zero_cpos =
872 		zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
873 	u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
874 	unsigned int num_clusters = 0;
875 	unsigned int ext_flags = 0;
876 
877 	while (zero_cpos < last_cpos) {
878 		rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
879 					&num_clusters, &ext_flags);
880 		if (rc) {
881 			mlog_errno(rc);
882 			goto out;
883 		}
884 
885 		if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
886 			zero_clusters = num_clusters;
887 			if (ext_flags & OCFS2_EXT_REFCOUNTED)
888 				needs_cow = 1;
889 			break;
890 		}
891 
892 		zero_cpos += num_clusters;
893 	}
894 	if (!zero_clusters) {
895 		*range_end = 0;
896 		goto out;
897 	}
898 
899 	while ((zero_cpos + zero_clusters) < last_cpos) {
900 		rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
901 					&p_cpos, &num_clusters,
902 					&ext_flags);
903 		if (rc) {
904 			mlog_errno(rc);
905 			goto out;
906 		}
907 
908 		if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
909 			break;
910 		if (ext_flags & OCFS2_EXT_REFCOUNTED)
911 			needs_cow = 1;
912 		zero_clusters += num_clusters;
913 	}
914 	if ((zero_cpos + zero_clusters) > last_cpos)
915 		zero_clusters = last_cpos - zero_cpos;
916 
917 	if (needs_cow) {
918 		rc = ocfs2_refcount_cow(inode, di_bh, zero_cpos,
919 					zero_clusters, UINT_MAX);
920 		if (rc) {
921 			mlog_errno(rc);
922 			goto out;
923 		}
924 	}
925 
926 	*range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
927 	*range_end = ocfs2_clusters_to_bytes(inode->i_sb,
928 					     zero_cpos + zero_clusters);
929 
930 out:
931 	return rc;
932 }
933 
934 /*
935  * Zero one range returned from ocfs2_zero_extend_get_range().  The caller
936  * has made sure that the entire range needs zeroing.
937  */
938 static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
939 				   u64 range_end, struct buffer_head *di_bh)
940 {
941 	int rc = 0;
942 	u64 next_pos;
943 	u64 zero_pos = range_start;
944 
945 	trace_ocfs2_zero_extend_range(
946 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
947 			(unsigned long long)range_start,
948 			(unsigned long long)range_end);
949 	BUG_ON(range_start >= range_end);
950 
951 	while (zero_pos < range_end) {
952 		next_pos = (zero_pos & PAGE_MASK) + PAGE_SIZE;
953 		if (next_pos > range_end)
954 			next_pos = range_end;
955 		rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh);
956 		if (rc < 0) {
957 			mlog_errno(rc);
958 			break;
959 		}
960 		zero_pos = next_pos;
961 
962 		/*
963 		 * Very large extends have the potential to lock up
964 		 * the cpu for extended periods of time.
965 		 */
966 		cond_resched();
967 	}
968 
969 	return rc;
970 }
971 
972 int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
973 		      loff_t zero_to_size)
974 {
975 	int ret = 0;
976 	u64 zero_start, range_start = 0, range_end = 0;
977 	struct super_block *sb = inode->i_sb;
978 
979 	zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
980 	trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
981 				(unsigned long long)zero_start,
982 				(unsigned long long)i_size_read(inode));
983 	while (zero_start < zero_to_size) {
984 		ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
985 						  zero_to_size,
986 						  &range_start,
987 						  &range_end);
988 		if (ret) {
989 			mlog_errno(ret);
990 			break;
991 		}
992 		if (!range_end)
993 			break;
994 		/* Trim the ends */
995 		if (range_start < zero_start)
996 			range_start = zero_start;
997 		if (range_end > zero_to_size)
998 			range_end = zero_to_size;
999 
1000 		ret = ocfs2_zero_extend_range(inode, range_start,
1001 					      range_end, di_bh);
1002 		if (ret) {
1003 			mlog_errno(ret);
1004 			break;
1005 		}
1006 		zero_start = range_end;
1007 	}
1008 
1009 	return ret;
1010 }
1011 
1012 int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
1013 			  u64 new_i_size, u64 zero_to)
1014 {
1015 	int ret;
1016 	u32 clusters_to_add;
1017 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1018 
1019 	/*
1020 	 * Only quota files call this without a bh, and they can't be
1021 	 * refcounted.
1022 	 */
1023 	BUG_ON(!di_bh && ocfs2_is_refcount_inode(inode));
1024 	BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
1025 
1026 	clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
1027 	if (clusters_to_add < oi->ip_clusters)
1028 		clusters_to_add = 0;
1029 	else
1030 		clusters_to_add -= oi->ip_clusters;
1031 
1032 	if (clusters_to_add) {
1033 		ret = ocfs2_extend_allocation(inode, oi->ip_clusters,
1034 					      clusters_to_add, 0);
1035 		if (ret) {
1036 			mlog_errno(ret);
1037 			goto out;
1038 		}
1039 	}
1040 
1041 	/*
1042 	 * Call this even if we don't add any clusters to the tree. We
1043 	 * still need to zero the area between the old i_size and the
1044 	 * new i_size.
1045 	 */
1046 	ret = ocfs2_zero_extend(inode, di_bh, zero_to);
1047 	if (ret < 0)
1048 		mlog_errno(ret);
1049 
1050 out:
1051 	return ret;
1052 }
1053 
1054 static int ocfs2_extend_file(struct inode *inode,
1055 			     struct buffer_head *di_bh,
1056 			     u64 new_i_size)
1057 {
1058 	int ret = 0;
1059 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1060 
1061 	BUG_ON(!di_bh);
1062 
1063 	/* setattr sometimes calls us like this. */
1064 	if (new_i_size == 0)
1065 		goto out;
1066 
1067 	if (i_size_read(inode) == new_i_size)
1068 		goto out;
1069 	BUG_ON(new_i_size < i_size_read(inode));
1070 
1071 	/*
1072 	 * The alloc sem blocks people in read/write from reading our
1073 	 * allocation until we're done changing it. We depend on
1074 	 * i_rwsem to block other extend/truncate calls while we're
1075 	 * here.  We even have to hold it for sparse files because there
1076 	 * might be some tail zeroing.
1077 	 */
1078 	down_write(&oi->ip_alloc_sem);
1079 
1080 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1081 		/*
1082 		 * We can optimize small extends by keeping the inodes
1083 		 * inline data.
1084 		 */
1085 		if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
1086 			up_write(&oi->ip_alloc_sem);
1087 			goto out_update_size;
1088 		}
1089 
1090 		ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1091 		if (ret) {
1092 			up_write(&oi->ip_alloc_sem);
1093 			mlog_errno(ret);
1094 			goto out;
1095 		}
1096 	}
1097 
1098 	if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
1099 		ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
1100 	else
1101 		ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
1102 					    new_i_size);
1103 
1104 	up_write(&oi->ip_alloc_sem);
1105 
1106 	if (ret < 0) {
1107 		mlog_errno(ret);
1108 		goto out;
1109 	}
1110 
1111 out_update_size:
1112 	ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
1113 	if (ret < 0)
1114 		mlog_errno(ret);
1115 
1116 out:
1117 	return ret;
1118 }
1119 
1120 int ocfs2_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
1121 		  struct iattr *attr)
1122 {
1123 	int status = 0, size_change;
1124 	int inode_locked = 0;
1125 	struct inode *inode = d_inode(dentry);
1126 	struct super_block *sb = inode->i_sb;
1127 	struct ocfs2_super *osb = OCFS2_SB(sb);
1128 	struct buffer_head *bh = NULL;
1129 	handle_t *handle = NULL;
1130 	struct dquot *transfer_to[MAXQUOTAS] = { };
1131 	int qtype;
1132 	int had_lock;
1133 	struct ocfs2_lock_holder oh;
1134 
1135 	trace_ocfs2_setattr(inode, dentry,
1136 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
1137 			    dentry->d_name.len, dentry->d_name.name,
1138 			    attr->ia_valid,
1139 				attr->ia_valid & ATTR_MODE ? attr->ia_mode : 0,
1140 				attr->ia_valid & ATTR_UID ?
1141 					from_kuid(&init_user_ns, attr->ia_uid) : 0,
1142 				attr->ia_valid & ATTR_GID ?
1143 					from_kgid(&init_user_ns, attr->ia_gid) : 0);
1144 
1145 	status = ocfs2_emergency_state(osb);
1146 	if (unlikely(status)) {
1147 		mlog_errno(status);
1148 		goto bail;
1149 	}
1150 
1151 	/* ensuring we don't even attempt to truncate a symlink */
1152 	if (S_ISLNK(inode->i_mode))
1153 		attr->ia_valid &= ~ATTR_SIZE;
1154 
1155 #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
1156 			   | ATTR_GID | ATTR_UID | ATTR_MODE)
1157 	if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
1158 		return 0;
1159 
1160 	status = setattr_prepare(&nop_mnt_idmap, dentry, attr);
1161 	if (status)
1162 		return status;
1163 
1164 	if (is_quota_modification(&nop_mnt_idmap, inode, attr)) {
1165 		status = dquot_initialize(inode);
1166 		if (status)
1167 			return status;
1168 	}
1169 	size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
1170 	if (size_change) {
1171 		/*
1172 		 * Here we should wait dio to finish before inode lock
1173 		 * to avoid a deadlock between ocfs2_setattr() and
1174 		 * ocfs2_dio_end_io_write()
1175 		 */
1176 		inode_dio_wait(inode);
1177 
1178 		status = ocfs2_rw_lock(inode, 1);
1179 		if (status < 0) {
1180 			mlog_errno(status);
1181 			goto bail;
1182 		}
1183 	}
1184 
1185 	had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
1186 	if (had_lock < 0) {
1187 		status = had_lock;
1188 		goto bail_unlock_rw;
1189 	} else if (had_lock) {
1190 		/*
1191 		 * As far as we know, ocfs2_setattr() could only be the first
1192 		 * VFS entry point in the call chain of recursive cluster
1193 		 * locking issue.
1194 		 *
1195 		 * For instance:
1196 		 * chmod_common()
1197 		 *  notify_change()
1198 		 *   ocfs2_setattr()
1199 		 *    posix_acl_chmod()
1200 		 *     ocfs2_iop_get_acl()
1201 		 *
1202 		 * But, we're not 100% sure if it's always true, because the
1203 		 * ordering of the VFS entry points in the call chain is out
1204 		 * of our control. So, we'd better dump the stack here to
1205 		 * catch the other cases of recursive locking.
1206 		 */
1207 		mlog(ML_ERROR, "Another case of recursive locking:\n");
1208 		dump_stack();
1209 	}
1210 	inode_locked = 1;
1211 
1212 	if (size_change) {
1213 		status = inode_newsize_ok(inode, attr->ia_size);
1214 		if (status)
1215 			goto bail_unlock;
1216 
1217 		if (i_size_read(inode) >= attr->ia_size) {
1218 			if (ocfs2_should_order_data(inode)) {
1219 				status = ocfs2_begin_ordered_truncate(inode,
1220 								      attr->ia_size);
1221 				if (status)
1222 					goto bail_unlock;
1223 			}
1224 			status = ocfs2_truncate_file(inode, bh, attr->ia_size);
1225 		} else
1226 			status = ocfs2_extend_file(inode, bh, attr->ia_size);
1227 		if (status < 0) {
1228 			if (status != -ENOSPC)
1229 				mlog_errno(status);
1230 			status = -ENOSPC;
1231 			goto bail_unlock;
1232 		}
1233 	}
1234 
1235 	if ((attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
1236 	    (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
1237 		/*
1238 		 * Gather pointers to quota structures so that allocation /
1239 		 * freeing of quota structures happens here and not inside
1240 		 * dquot_transfer() where we have problems with lock ordering
1241 		 */
1242 		if (attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)
1243 		    && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1244 		    OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
1245 			transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid));
1246 			if (IS_ERR(transfer_to[USRQUOTA])) {
1247 				status = PTR_ERR(transfer_to[USRQUOTA]);
1248 				transfer_to[USRQUOTA] = NULL;
1249 				goto bail_unlock;
1250 			}
1251 		}
1252 		if (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid)
1253 		    && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
1254 		    OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
1255 			transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid));
1256 			if (IS_ERR(transfer_to[GRPQUOTA])) {
1257 				status = PTR_ERR(transfer_to[GRPQUOTA]);
1258 				transfer_to[GRPQUOTA] = NULL;
1259 				goto bail_unlock;
1260 			}
1261 		}
1262 		down_write(&OCFS2_I(inode)->ip_alloc_sem);
1263 		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
1264 					   2 * ocfs2_quota_trans_credits(sb));
1265 		if (IS_ERR(handle)) {
1266 			status = PTR_ERR(handle);
1267 			mlog_errno(status);
1268 			goto bail_unlock_alloc;
1269 		}
1270 		status = __dquot_transfer(inode, transfer_to);
1271 		if (status < 0)
1272 			goto bail_commit;
1273 	} else {
1274 		down_write(&OCFS2_I(inode)->ip_alloc_sem);
1275 		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1276 		if (IS_ERR(handle)) {
1277 			status = PTR_ERR(handle);
1278 			mlog_errno(status);
1279 			goto bail_unlock_alloc;
1280 		}
1281 	}
1282 
1283 	setattr_copy(&nop_mnt_idmap, inode, attr);
1284 	mark_inode_dirty(inode);
1285 
1286 	status = ocfs2_mark_inode_dirty(handle, inode, bh);
1287 	if (status < 0)
1288 		mlog_errno(status);
1289 
1290 bail_commit:
1291 	ocfs2_commit_trans(osb, handle);
1292 bail_unlock_alloc:
1293 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
1294 bail_unlock:
1295 	if (status && inode_locked) {
1296 		ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
1297 		inode_locked = 0;
1298 	}
1299 bail_unlock_rw:
1300 	if (size_change)
1301 		ocfs2_rw_unlock(inode, 1);
1302 bail:
1303 
1304 	/* Release quota pointers in case we acquired them */
1305 	for (qtype = 0; qtype < OCFS2_MAXQUOTAS; qtype++)
1306 		dqput(transfer_to[qtype]);
1307 
1308 	if (!status && attr->ia_valid & ATTR_MODE) {
1309 		status = ocfs2_acl_chmod(inode, bh);
1310 		if (status < 0)
1311 			mlog_errno(status);
1312 	}
1313 	if (inode_locked)
1314 		ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
1315 
1316 	brelse(bh);
1317 	return status;
1318 }
1319 
1320 int ocfs2_getattr(struct mnt_idmap *idmap, const struct path *path,
1321 		  struct kstat *stat, u32 request_mask, unsigned int flags)
1322 {
1323 	struct inode *inode = d_inode(path->dentry);
1324 	struct super_block *sb = path->dentry->d_sb;
1325 	struct ocfs2_super *osb = sb->s_fs_info;
1326 	int err;
1327 
1328 	err = ocfs2_inode_revalidate(path->dentry);
1329 	if (err) {
1330 		if (err != -ENOENT)
1331 			mlog_errno(err);
1332 		goto bail;
1333 	}
1334 
1335 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1336 	/*
1337 	 * If there is inline data in the inode, the inode will normally not
1338 	 * have data blocks allocated (it may have an external xattr block).
1339 	 * Report at least one sector for such files, so tools like tar, rsync,
1340 	 * others don't incorrectly think the file is completely sparse.
1341 	 */
1342 	if (unlikely(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
1343 		stat->blocks += (stat->size + 511)>>9;
1344 
1345 	/* We set the blksize from the cluster size for performance */
1346 	stat->blksize = osb->s_clustersize;
1347 
1348 bail:
1349 	return err;
1350 }
1351 
1352 int ocfs2_permission(struct mnt_idmap *idmap, struct inode *inode,
1353 		     int mask)
1354 {
1355 	int ret, had_lock;
1356 	struct ocfs2_lock_holder oh;
1357 
1358 	if (mask & MAY_NOT_BLOCK)
1359 		return -ECHILD;
1360 
1361 	had_lock = ocfs2_inode_lock_tracker(inode, NULL, 0, &oh);
1362 	if (had_lock < 0) {
1363 		ret = had_lock;
1364 		goto out;
1365 	} else if (had_lock) {
1366 		/* See comments in ocfs2_setattr() for details.
1367 		 * The call chain of this case could be:
1368 		 * do_sys_open()
1369 		 *  may_open()
1370 		 *   inode_permission()
1371 		 *    ocfs2_permission()
1372 		 *     ocfs2_iop_get_acl()
1373 		 */
1374 		mlog(ML_ERROR, "Another case of recursive locking:\n");
1375 		dump_stack();
1376 	}
1377 
1378 	ret = generic_permission(&nop_mnt_idmap, inode, mask);
1379 
1380 	ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
1381 out:
1382 	return ret;
1383 }
1384 
1385 static int __ocfs2_write_remove_suid(struct inode *inode,
1386 				     struct buffer_head *bh)
1387 {
1388 	int ret;
1389 	handle_t *handle;
1390 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1391 	struct ocfs2_dinode *di;
1392 
1393 	trace_ocfs2_write_remove_suid(
1394 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
1395 			inode->i_mode);
1396 
1397 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1398 	if (IS_ERR(handle)) {
1399 		ret = PTR_ERR(handle);
1400 		mlog_errno(ret);
1401 		goto out;
1402 	}
1403 
1404 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1405 				      OCFS2_JOURNAL_ACCESS_WRITE);
1406 	if (ret < 0) {
1407 		mlog_errno(ret);
1408 		goto out_trans;
1409 	}
1410 
1411 	inode->i_mode &= ~S_ISUID;
1412 	if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
1413 		inode->i_mode &= ~S_ISGID;
1414 
1415 	di = (struct ocfs2_dinode *) bh->b_data;
1416 	di->i_mode = cpu_to_le16(inode->i_mode);
1417 	ocfs2_update_inode_fsync_trans(handle, inode, 0);
1418 
1419 	ocfs2_journal_dirty(handle, bh);
1420 
1421 out_trans:
1422 	ocfs2_commit_trans(osb, handle);
1423 out:
1424 	return ret;
1425 }
1426 
1427 static int ocfs2_write_remove_suid(struct inode *inode)
1428 {
1429 	int ret;
1430 	struct buffer_head *bh = NULL;
1431 
1432 	ret = ocfs2_read_inode_block(inode, &bh);
1433 	if (ret < 0) {
1434 		mlog_errno(ret);
1435 		goto out;
1436 	}
1437 
1438 	ret =  __ocfs2_write_remove_suid(inode, bh);
1439 out:
1440 	brelse(bh);
1441 	return ret;
1442 }
1443 
1444 /*
1445  * Allocate enough extents to cover the region starting at byte offset
1446  * start for len bytes. Existing extents are skipped, any extents
1447  * added are marked as "unwritten".
1448  */
1449 static int ocfs2_allocate_unwritten_extents(struct inode *inode,
1450 					    u64 start, u64 len)
1451 {
1452 	int ret;
1453 	u32 cpos, phys_cpos, clusters, alloc_size;
1454 	u64 end = start + len;
1455 	struct buffer_head *di_bh = NULL;
1456 
1457 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1458 		ret = ocfs2_read_inode_block(inode, &di_bh);
1459 		if (ret) {
1460 			mlog_errno(ret);
1461 			goto out;
1462 		}
1463 
1464 		/*
1465 		 * Nothing to do if the requested reservation range
1466 		 * fits within the inode.
1467 		 */
1468 		if (ocfs2_size_fits_inline_data(di_bh, end))
1469 			goto out;
1470 
1471 		ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
1472 		if (ret) {
1473 			mlog_errno(ret);
1474 			goto out;
1475 		}
1476 	}
1477 
1478 	/*
1479 	 * We consider both start and len to be inclusive.
1480 	 */
1481 	cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
1482 	clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
1483 	clusters -= cpos;
1484 
1485 	while (clusters) {
1486 		ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
1487 					 &alloc_size, NULL);
1488 		if (ret) {
1489 			mlog_errno(ret);
1490 			goto out;
1491 		}
1492 
1493 		/*
1494 		 * Hole or existing extent len can be arbitrary, so
1495 		 * cap it to our own allocation request.
1496 		 */
1497 		if (alloc_size > clusters)
1498 			alloc_size = clusters;
1499 
1500 		if (phys_cpos) {
1501 			/*
1502 			 * We already have an allocation at this
1503 			 * region so we can safely skip it.
1504 			 */
1505 			goto next;
1506 		}
1507 
1508 		ret = ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
1509 		if (ret) {
1510 			if (ret != -ENOSPC)
1511 				mlog_errno(ret);
1512 			goto out;
1513 		}
1514 
1515 next:
1516 		cpos += alloc_size;
1517 		clusters -= alloc_size;
1518 	}
1519 
1520 	ret = 0;
1521 out:
1522 
1523 	brelse(di_bh);
1524 	return ret;
1525 }
1526 
1527 /*
1528  * Truncate a byte range, avoiding pages within partial clusters. This
1529  * preserves those pages for the zeroing code to write to.
1530  */
1531 static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
1532 					 u64 byte_len)
1533 {
1534 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1535 	loff_t start, end;
1536 	struct address_space *mapping = inode->i_mapping;
1537 
1538 	start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
1539 	end = byte_start + byte_len;
1540 	end = end & ~(osb->s_clustersize - 1);
1541 
1542 	if (start < end) {
1543 		unmap_mapping_range(mapping, start, end - start, 0);
1544 		truncate_inode_pages_range(mapping, start, end - 1);
1545 	}
1546 }
1547 
1548 /*
1549  * zero out partial blocks of one cluster.
1550  *
1551  * start: file offset where zero starts, will be made upper block aligned.
1552  * len: it will be trimmed to the end of current cluster if "start + len"
1553  *      is bigger than it.
1554  */
1555 static int ocfs2_zeroout_partial_cluster(struct inode *inode,
1556 					u64 start, u64 len)
1557 {
1558 	int ret;
1559 	u64 start_block, end_block, nr_blocks;
1560 	u64 p_block, offset;
1561 	u32 cluster, p_cluster, nr_clusters;
1562 	struct super_block *sb = inode->i_sb;
1563 	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
1564 
1565 	if (start + len < end)
1566 		end = start + len;
1567 
1568 	start_block = ocfs2_blocks_for_bytes(sb, start);
1569 	end_block = ocfs2_blocks_for_bytes(sb, end);
1570 	nr_blocks = end_block - start_block;
1571 	if (!nr_blocks)
1572 		return 0;
1573 
1574 	cluster = ocfs2_bytes_to_clusters(sb, start);
1575 	ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
1576 				&nr_clusters, NULL);
1577 	if (ret)
1578 		return ret;
1579 	if (!p_cluster)
1580 		return 0;
1581 
1582 	offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
1583 	p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
1584 	return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
1585 }
1586 
1587 static int ocfs2_zero_partial_clusters(struct inode *inode,
1588 				       u64 start, u64 len)
1589 {
1590 	int ret = 0;
1591 	u64 tmpend = 0;
1592 	u64 end = start + len;
1593 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1594 	unsigned int csize = osb->s_clustersize;
1595 	handle_t *handle;
1596 	loff_t isize = i_size_read(inode);
1597 
1598 	/*
1599 	 * The "start" and "end" values are NOT necessarily part of
1600 	 * the range whose allocation is being deleted. Rather, this
1601 	 * is what the user passed in with the request. We must zero
1602 	 * partial clusters here. There's no need to worry about
1603 	 * physical allocation - the zeroing code knows to skip holes.
1604 	 */
1605 	trace_ocfs2_zero_partial_clusters(
1606 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
1607 		(unsigned long long)start, (unsigned long long)end);
1608 
1609 	/*
1610 	 * If both edges are on a cluster boundary then there's no
1611 	 * zeroing required as the region is part of the allocation to
1612 	 * be truncated.
1613 	 */
1614 	if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
1615 		goto out;
1616 
1617 	/* No page cache for EOF blocks, issue zero out to disk. */
1618 	if (end > isize) {
1619 		/*
1620 		 * zeroout eof blocks in last cluster starting from
1621 		 * "isize" even "start" > "isize" because it is
1622 		 * complicated to zeroout just at "start" as "start"
1623 		 * may be not aligned with block size, buffer write
1624 		 * would be required to do that, but out of eof buffer
1625 		 * write is not supported.
1626 		 */
1627 		ret = ocfs2_zeroout_partial_cluster(inode, isize,
1628 					end - isize);
1629 		if (ret) {
1630 			mlog_errno(ret);
1631 			goto out;
1632 		}
1633 		if (start >= isize)
1634 			goto out;
1635 		end = isize;
1636 	}
1637 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1638 	if (IS_ERR(handle)) {
1639 		ret = PTR_ERR(handle);
1640 		mlog_errno(ret);
1641 		goto out;
1642 	}
1643 
1644 	/*
1645 	 * If start is on a cluster boundary and end is somewhere in another
1646 	 * cluster, we have not COWed the cluster starting at start, unless
1647 	 * end is also within the same cluster. So, in this case, we skip this
1648 	 * first call to ocfs2_zero_range_for_truncate() truncate and move on
1649 	 * to the next one.
1650 	 */
1651 	if ((start & (csize - 1)) != 0) {
1652 		/*
1653 		 * We want to get the byte offset of the end of the 1st
1654 		 * cluster.
1655 		 */
1656 		tmpend = (u64)osb->s_clustersize +
1657 			(start & ~(osb->s_clustersize - 1));
1658 		if (tmpend > end)
1659 			tmpend = end;
1660 
1661 		trace_ocfs2_zero_partial_clusters_range1(
1662 			(unsigned long long)start,
1663 			(unsigned long long)tmpend);
1664 
1665 		ret = ocfs2_zero_range_for_truncate(inode, handle, start,
1666 						    tmpend);
1667 		if (ret)
1668 			mlog_errno(ret);
1669 	}
1670 
1671 	if (tmpend < end) {
1672 		/*
1673 		 * This may make start and end equal, but the zeroing
1674 		 * code will skip any work in that case so there's no
1675 		 * need to catch it up here.
1676 		 */
1677 		start = end & ~(osb->s_clustersize - 1);
1678 
1679 		trace_ocfs2_zero_partial_clusters_range2(
1680 			(unsigned long long)start, (unsigned long long)end);
1681 
1682 		ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
1683 		if (ret)
1684 			mlog_errno(ret);
1685 	}
1686 	ocfs2_update_inode_fsync_trans(handle, inode, 1);
1687 
1688 	ocfs2_commit_trans(osb, handle);
1689 out:
1690 	return ret;
1691 }
1692 
1693 static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
1694 {
1695 	int i;
1696 	struct ocfs2_extent_rec *rec = NULL;
1697 
1698 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1699 
1700 		rec = &el->l_recs[i];
1701 
1702 		if (le32_to_cpu(rec->e_cpos) < pos)
1703 			break;
1704 	}
1705 
1706 	return i;
1707 }
1708 
1709 /*
1710  * Helper to calculate the punching pos and length in one run, we handle the
1711  * following three cases in order:
1712  *
1713  * - remove the entire record
1714  * - remove a partial record
1715  * - no record needs to be removed (hole-punching completed)
1716 */
1717 static void ocfs2_calc_trunc_pos(struct inode *inode,
1718 				 struct ocfs2_extent_list *el,
1719 				 struct ocfs2_extent_rec *rec,
1720 				 u32 trunc_start, u32 *trunc_cpos,
1721 				 u32 *trunc_len, u32 *trunc_end,
1722 				 u64 *blkno, int *done)
1723 {
1724 	int ret = 0;
1725 	u32 coff, range;
1726 
1727 	range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1728 
1729 	if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
1730 		/*
1731 		 * remove an entire extent record.
1732 		 */
1733 		*trunc_cpos = le32_to_cpu(rec->e_cpos);
1734 		/*
1735 		 * Skip holes if any.
1736 		 */
1737 		if (range < *trunc_end)
1738 			*trunc_end = range;
1739 		*trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
1740 		*blkno = le64_to_cpu(rec->e_blkno);
1741 		*trunc_end = le32_to_cpu(rec->e_cpos);
1742 	} else if (range > trunc_start) {
1743 		/*
1744 		 * remove a partial extent record, which means we're
1745 		 * removing the last extent record.
1746 		 */
1747 		*trunc_cpos = trunc_start;
1748 		/*
1749 		 * skip hole if any.
1750 		 */
1751 		if (range < *trunc_end)
1752 			*trunc_end = range;
1753 		*trunc_len = *trunc_end - trunc_start;
1754 		coff = trunc_start - le32_to_cpu(rec->e_cpos);
1755 		*blkno = le64_to_cpu(rec->e_blkno) +
1756 				ocfs2_clusters_to_blocks(inode->i_sb, coff);
1757 		*trunc_end = trunc_start;
1758 	} else {
1759 		/*
1760 		 * It may have two following possibilities:
1761 		 *
1762 		 * - last record has been removed
1763 		 * - trunc_start was within a hole
1764 		 *
1765 		 * both two cases mean the completion of hole punching.
1766 		 */
1767 		ret = 1;
1768 	}
1769 
1770 	*done = ret;
1771 }
1772 
1773 int ocfs2_remove_inode_range(struct inode *inode,
1774 			     struct buffer_head *di_bh, u64 byte_start,
1775 			     u64 byte_len)
1776 {
1777 	int ret = 0, flags = 0, done = 0, i;
1778 	u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
1779 	u32 cluster_in_el;
1780 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1781 	struct ocfs2_cached_dealloc_ctxt dealloc;
1782 	struct address_space *mapping = inode->i_mapping;
1783 	struct ocfs2_extent_tree et;
1784 	struct ocfs2_path *path = NULL;
1785 	struct ocfs2_extent_list *el = NULL;
1786 	struct ocfs2_extent_rec *rec = NULL;
1787 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1788 	u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
1789 
1790 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
1791 	ocfs2_init_dealloc_ctxt(&dealloc);
1792 
1793 	trace_ocfs2_remove_inode_range(
1794 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
1795 			(unsigned long long)byte_start,
1796 			(unsigned long long)byte_len);
1797 
1798 	if (byte_len == 0)
1799 		return 0;
1800 
1801 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1802 		int id_count = ocfs2_max_inline_data_with_xattr(inode->i_sb, di);
1803 
1804 		if (byte_start > id_count || byte_start + byte_len > id_count) {
1805 			ret = -EINVAL;
1806 			mlog_errno(ret);
1807 			goto out;
1808 		}
1809 
1810 		ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
1811 					    byte_start + byte_len, 0);
1812 		if (ret) {
1813 			mlog_errno(ret);
1814 			goto out;
1815 		}
1816 		/*
1817 		 * There's no need to get fancy with the page cache
1818 		 * truncate of an inline-data inode. We're talking
1819 		 * about less than a page here, which will be cached
1820 		 * in the dinode buffer anyway.
1821 		 */
1822 		unmap_mapping_range(mapping, 0, 0, 0);
1823 		truncate_inode_pages(mapping, 0);
1824 		goto out;
1825 	}
1826 
1827 	/*
1828 	 * For reflinks, we may need to CoW 2 clusters which might be
1829 	 * partially zero'd later, if hole's start and end offset were
1830 	 * within one cluster(means is not exactly aligned to clustersize).
1831 	 */
1832 
1833 	if (ocfs2_is_refcount_inode(inode)) {
1834 		ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
1835 		if (ret) {
1836 			mlog_errno(ret);
1837 			goto out;
1838 		}
1839 
1840 		ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
1841 		if (ret) {
1842 			mlog_errno(ret);
1843 			goto out;
1844 		}
1845 	}
1846 
1847 	trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
1848 	trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
1849 	cluster_in_el = trunc_end;
1850 
1851 	ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
1852 	if (ret) {
1853 		mlog_errno(ret);
1854 		goto out;
1855 	}
1856 
1857 	path = ocfs2_new_path_from_et(&et);
1858 	if (!path) {
1859 		ret = -ENOMEM;
1860 		mlog_errno(ret);
1861 		goto out;
1862 	}
1863 
1864 	while (trunc_end > trunc_start) {
1865 
1866 		ret = ocfs2_find_path(INODE_CACHE(inode), path,
1867 				      cluster_in_el);
1868 		if (ret) {
1869 			mlog_errno(ret);
1870 			goto out;
1871 		}
1872 
1873 		el = path_leaf_el(path);
1874 
1875 		i = ocfs2_find_rec(el, trunc_end);
1876 		/*
1877 		 * Need to go to previous extent block.
1878 		 */
1879 		if (i < 0) {
1880 			if (path->p_tree_depth == 0)
1881 				break;
1882 
1883 			ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
1884 							    path,
1885 							    &cluster_in_el);
1886 			if (ret) {
1887 				mlog_errno(ret);
1888 				goto out;
1889 			}
1890 
1891 			/*
1892 			 * We've reached the leftmost extent block,
1893 			 * it's safe to leave.
1894 			 */
1895 			if (cluster_in_el == 0)
1896 				break;
1897 
1898 			/*
1899 			 * The 'pos' searched for previous extent block is
1900 			 * always one cluster less than actual trunc_end.
1901 			 */
1902 			trunc_end = cluster_in_el + 1;
1903 
1904 			ocfs2_reinit_path(path, 1);
1905 
1906 			continue;
1907 
1908 		} else
1909 			rec = &el->l_recs[i];
1910 
1911 		ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
1912 				     &trunc_len, &trunc_end, &blkno, &done);
1913 		if (done)
1914 			break;
1915 
1916 		flags = rec->e_flags;
1917 		phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
1918 
1919 		ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
1920 					       phys_cpos, trunc_len, flags,
1921 					       &dealloc, refcount_loc, false);
1922 		if (ret < 0) {
1923 			mlog_errno(ret);
1924 			goto out;
1925 		}
1926 
1927 		cluster_in_el = trunc_end;
1928 
1929 		ocfs2_reinit_path(path, 1);
1930 	}
1931 
1932 	ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
1933 
1934 out:
1935 	ocfs2_free_path(path);
1936 	ocfs2_schedule_truncate_log_flush(osb, 1);
1937 	ocfs2_run_deallocs(osb, &dealloc);
1938 
1939 	return ret;
1940 }
1941 
1942 /*
1943  * Parts of this function taken from xfs_change_file_space()
1944  */
1945 static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
1946 				     loff_t f_pos, unsigned int cmd,
1947 				     struct ocfs2_space_resv *sr,
1948 				     int change_size)
1949 {
1950 	int ret;
1951 	s64 llen;
1952 	loff_t size, orig_isize;
1953 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1954 	struct buffer_head *di_bh = NULL;
1955 	handle_t *handle;
1956 	unsigned long long max_off = inode->i_sb->s_maxbytes;
1957 
1958 	if (unlikely(ocfs2_emergency_state(osb)))
1959 		return -EROFS;
1960 
1961 	inode_lock(inode);
1962 
1963 	/* Wait all existing dio workers, newcomers will block on i_rwsem */
1964 	inode_dio_wait(inode);
1965 	/*
1966 	 * This prevents concurrent writes on other nodes
1967 	 */
1968 	ret = ocfs2_rw_lock(inode, 1);
1969 	if (ret) {
1970 		mlog_errno(ret);
1971 		goto out;
1972 	}
1973 
1974 	ret = ocfs2_inode_lock(inode, &di_bh, 1);
1975 	if (ret) {
1976 		mlog_errno(ret);
1977 		goto out_rw_unlock;
1978 	}
1979 
1980 	if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
1981 		ret = -EPERM;
1982 		goto out_inode_unlock;
1983 	}
1984 
1985 	switch (sr->l_whence) {
1986 	case 0: /*SEEK_SET*/
1987 		break;
1988 	case 1: /*SEEK_CUR*/
1989 		sr->l_start += f_pos;
1990 		break;
1991 	case 2: /*SEEK_END*/
1992 		sr->l_start += i_size_read(inode);
1993 		break;
1994 	default:
1995 		ret = -EINVAL;
1996 		goto out_inode_unlock;
1997 	}
1998 	sr->l_whence = 0;
1999 
2000 	llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
2001 
2002 	if (sr->l_start < 0
2003 	    || sr->l_start > max_off
2004 	    || (sr->l_start + llen) < 0
2005 	    || (sr->l_start + llen) > max_off) {
2006 		ret = -EINVAL;
2007 		goto out_inode_unlock;
2008 	}
2009 	size = sr->l_start + sr->l_len;
2010 
2011 	if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64 ||
2012 	    cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) {
2013 		if (sr->l_len <= 0) {
2014 			ret = -EINVAL;
2015 			goto out_inode_unlock;
2016 		}
2017 	}
2018 
2019 	if (file && setattr_should_drop_suidgid(&nop_mnt_idmap, file_inode(file))) {
2020 		ret = __ocfs2_write_remove_suid(inode, di_bh);
2021 		if (ret) {
2022 			mlog_errno(ret);
2023 			goto out_inode_unlock;
2024 		}
2025 	}
2026 
2027 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
2028 	switch (cmd) {
2029 	case OCFS2_IOC_RESVSP:
2030 	case OCFS2_IOC_RESVSP64:
2031 		/*
2032 		 * This takes unsigned offsets, but the signed ones we
2033 		 * pass have been checked against overflow above.
2034 		 */
2035 		ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
2036 						       sr->l_len);
2037 		break;
2038 	case OCFS2_IOC_UNRESVSP:
2039 	case OCFS2_IOC_UNRESVSP64:
2040 		ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
2041 					       sr->l_len);
2042 		break;
2043 	default:
2044 		ret = -EINVAL;
2045 	}
2046 
2047 	orig_isize = i_size_read(inode);
2048 	/* zeroout eof blocks in the cluster. */
2049 	if (!ret && change_size && orig_isize < size) {
2050 		ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
2051 					size - orig_isize);
2052 		if (!ret)
2053 			i_size_write(inode, size);
2054 	}
2055 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
2056 	if (ret) {
2057 		mlog_errno(ret);
2058 		goto out_inode_unlock;
2059 	}
2060 
2061 	/*
2062 	 * We update c/mtime for these changes
2063 	 */
2064 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
2065 	if (IS_ERR(handle)) {
2066 		ret = PTR_ERR(handle);
2067 		mlog_errno(ret);
2068 		goto out_inode_unlock;
2069 	}
2070 
2071 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2072 	ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2073 	if (ret < 0)
2074 		mlog_errno(ret);
2075 
2076 	if (file && (file->f_flags & O_SYNC))
2077 		handle->h_sync = 1;
2078 
2079 	ocfs2_commit_trans(osb, handle);
2080 
2081 out_inode_unlock:
2082 	brelse(di_bh);
2083 	ocfs2_inode_unlock(inode, 1);
2084 out_rw_unlock:
2085 	ocfs2_rw_unlock(inode, 1);
2086 
2087 out:
2088 	inode_unlock(inode);
2089 	return ret;
2090 }
2091 
2092 int ocfs2_change_file_space(struct file *file, unsigned int cmd,
2093 			    struct ocfs2_space_resv *sr)
2094 {
2095 	struct inode *inode = file_inode(file);
2096 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2097 	int ret;
2098 
2099 	if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
2100 	    !ocfs2_writes_unwritten_extents(osb))
2101 		return -ENOTTY;
2102 	else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
2103 		 !ocfs2_sparse_alloc(osb))
2104 		return -ENOTTY;
2105 
2106 	if (!S_ISREG(inode->i_mode))
2107 		return -EINVAL;
2108 
2109 	if (!(file->f_mode & FMODE_WRITE))
2110 		return -EBADF;
2111 
2112 	ret = mnt_want_write_file(file);
2113 	if (ret)
2114 		return ret;
2115 	ret = __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
2116 	mnt_drop_write_file(file);
2117 	return ret;
2118 }
2119 
2120 static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
2121 			    loff_t len)
2122 {
2123 	struct inode *inode = file_inode(file);
2124 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2125 	struct ocfs2_space_resv sr;
2126 	int change_size = 1;
2127 	int cmd = OCFS2_IOC_RESVSP64;
2128 	int ret = 0;
2129 
2130 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2131 		return -EOPNOTSUPP;
2132 	if (!ocfs2_writes_unwritten_extents(osb))
2133 		return -EOPNOTSUPP;
2134 
2135 	if (mode & FALLOC_FL_KEEP_SIZE) {
2136 		change_size = 0;
2137 	} else {
2138 		ret = inode_newsize_ok(inode, offset + len);
2139 		if (ret)
2140 			return ret;
2141 	}
2142 
2143 	if (mode & FALLOC_FL_PUNCH_HOLE)
2144 		cmd = OCFS2_IOC_UNRESVSP64;
2145 
2146 	sr.l_whence = 0;
2147 	sr.l_start = (s64)offset;
2148 	sr.l_len = (s64)len;
2149 
2150 	return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
2151 					 change_size);
2152 }
2153 
2154 int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
2155 				   size_t count)
2156 {
2157 	int ret = 0;
2158 	unsigned int extent_flags;
2159 	u32 cpos, clusters, extent_len, phys_cpos;
2160 	struct super_block *sb = inode->i_sb;
2161 
2162 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
2163 	    !ocfs2_is_refcount_inode(inode) ||
2164 	    OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2165 		return 0;
2166 
2167 	cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
2168 	clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
2169 
2170 	while (clusters) {
2171 		ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
2172 					 &extent_flags);
2173 		if (ret < 0) {
2174 			mlog_errno(ret);
2175 			goto out;
2176 		}
2177 
2178 		if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
2179 			ret = 1;
2180 			break;
2181 		}
2182 
2183 		if (extent_len > clusters)
2184 			extent_len = clusters;
2185 
2186 		clusters -= extent_len;
2187 		cpos += extent_len;
2188 	}
2189 out:
2190 	return ret;
2191 }
2192 
2193 static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
2194 {
2195 	int blockmask = inode->i_sb->s_blocksize - 1;
2196 	loff_t final_size = pos + count;
2197 
2198 	if ((pos & blockmask) || (final_size & blockmask))
2199 		return 1;
2200 	return 0;
2201 }
2202 
2203 static int ocfs2_inode_lock_for_extent_tree(struct inode *inode,
2204 					    struct buffer_head **di_bh,
2205 					    int meta_level,
2206 					    int write_sem,
2207 					    int wait)
2208 {
2209 	int ret = 0;
2210 
2211 	if (wait)
2212 		ret = ocfs2_inode_lock(inode, di_bh, meta_level);
2213 	else
2214 		ret = ocfs2_try_inode_lock(inode, di_bh, meta_level);
2215 	if (ret < 0)
2216 		goto out;
2217 
2218 	if (wait) {
2219 		if (write_sem)
2220 			down_write(&OCFS2_I(inode)->ip_alloc_sem);
2221 		else
2222 			down_read(&OCFS2_I(inode)->ip_alloc_sem);
2223 	} else {
2224 		if (write_sem)
2225 			ret = down_write_trylock(&OCFS2_I(inode)->ip_alloc_sem);
2226 		else
2227 			ret = down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem);
2228 
2229 		if (!ret) {
2230 			ret = -EAGAIN;
2231 			goto out_unlock;
2232 		}
2233 	}
2234 
2235 	return ret;
2236 
2237 out_unlock:
2238 	brelse(*di_bh);
2239 	*di_bh = NULL;
2240 	ocfs2_inode_unlock(inode, meta_level);
2241 out:
2242 	return ret;
2243 }
2244 
2245 static void ocfs2_inode_unlock_for_extent_tree(struct inode *inode,
2246 					       struct buffer_head **di_bh,
2247 					       int meta_level,
2248 					       int write_sem)
2249 {
2250 	if (write_sem)
2251 		up_write(&OCFS2_I(inode)->ip_alloc_sem);
2252 	else
2253 		up_read(&OCFS2_I(inode)->ip_alloc_sem);
2254 
2255 	brelse(*di_bh);
2256 	*di_bh = NULL;
2257 
2258 	if (meta_level >= 0)
2259 		ocfs2_inode_unlock(inode, meta_level);
2260 }
2261 
2262 static int ocfs2_prepare_inode_for_write(struct file *file,
2263 					 loff_t pos, size_t count, int wait)
2264 {
2265 	int ret = 0, meta_level = 0, overwrite_io = 0;
2266 	int write_sem = 0;
2267 	struct dentry *dentry = file->f_path.dentry;
2268 	struct inode *inode = d_inode(dentry);
2269 	struct buffer_head *di_bh = NULL;
2270 	u32 cpos;
2271 	u32 clusters;
2272 
2273 	/*
2274 	 * We start with a read level meta lock and only jump to an ex
2275 	 * if we need to make modifications here.
2276 	 */
2277 	for(;;) {
2278 		ret = ocfs2_inode_lock_for_extent_tree(inode,
2279 						       &di_bh,
2280 						       meta_level,
2281 						       write_sem,
2282 						       wait);
2283 		if (ret < 0) {
2284 			if (ret != -EAGAIN)
2285 				mlog_errno(ret);
2286 			goto out;
2287 		}
2288 
2289 		/*
2290 		 * Check if IO will overwrite allocated blocks in case
2291 		 * IOCB_NOWAIT flag is set.
2292 		 */
2293 		if (!wait && !overwrite_io) {
2294 			overwrite_io = 1;
2295 
2296 			ret = ocfs2_overwrite_io(inode, di_bh, pos, count);
2297 			if (ret < 0) {
2298 				if (ret != -EAGAIN)
2299 					mlog_errno(ret);
2300 				goto out_unlock;
2301 			}
2302 		}
2303 
2304 		/* Clear suid / sgid if necessary. We do this here
2305 		 * instead of later in the write path because
2306 		 * remove_suid() calls ->setattr without any hint that
2307 		 * we may have already done our cluster locking. Since
2308 		 * ocfs2_setattr() *must* take cluster locks to
2309 		 * proceed, this will lead us to recursively lock the
2310 		 * inode. There's also the dinode i_size state which
2311 		 * can be lost via setattr during extending writes (we
2312 		 * set inode->i_size at the end of a write. */
2313 		if (setattr_should_drop_suidgid(&nop_mnt_idmap, inode)) {
2314 			if (meta_level == 0) {
2315 				ocfs2_inode_unlock_for_extent_tree(inode,
2316 								   &di_bh,
2317 								   meta_level,
2318 								   write_sem);
2319 				meta_level = 1;
2320 				continue;
2321 			}
2322 
2323 			ret = ocfs2_write_remove_suid(inode);
2324 			if (ret < 0) {
2325 				mlog_errno(ret);
2326 				goto out_unlock;
2327 			}
2328 		}
2329 
2330 		ret = ocfs2_check_range_for_refcount(inode, pos, count);
2331 		if (ret == 1) {
2332 			ocfs2_inode_unlock_for_extent_tree(inode,
2333 							   &di_bh,
2334 							   meta_level,
2335 							   write_sem);
2336 			meta_level = 1;
2337 			write_sem = 1;
2338 			ret = ocfs2_inode_lock_for_extent_tree(inode,
2339 							       &di_bh,
2340 							       meta_level,
2341 							       write_sem,
2342 							       wait);
2343 			if (ret < 0) {
2344 				if (ret != -EAGAIN)
2345 					mlog_errno(ret);
2346 				goto out;
2347 			}
2348 
2349 			cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
2350 			clusters =
2351 				ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
2352 			ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
2353 		}
2354 
2355 		if (ret < 0) {
2356 			if (ret != -EAGAIN)
2357 				mlog_errno(ret);
2358 			goto out_unlock;
2359 		}
2360 
2361 		break;
2362 	}
2363 
2364 out_unlock:
2365 	trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
2366 					    pos, count, wait);
2367 
2368 	ocfs2_inode_unlock_for_extent_tree(inode,
2369 					   &di_bh,
2370 					   meta_level,
2371 					   write_sem);
2372 
2373 out:
2374 	return ret;
2375 }
2376 
2377 static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
2378 				    struct iov_iter *from)
2379 {
2380 	int rw_level;
2381 	ssize_t written = 0;
2382 	ssize_t ret;
2383 	size_t count = iov_iter_count(from);
2384 	struct file *file = iocb->ki_filp;
2385 	struct inode *inode = file_inode(file);
2386 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2387 	int full_coherency = !(osb->s_mount_opt &
2388 			       OCFS2_MOUNT_COHERENCY_BUFFERED);
2389 	void *saved_ki_complete = NULL;
2390 	int append_write = ((iocb->ki_pos + count) >=
2391 			i_size_read(inode) ? 1 : 0);
2392 	int direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0;
2393 	int nowait = iocb->ki_flags & IOCB_NOWAIT ? 1 : 0;
2394 
2395 	trace_ocfs2_file_write_iter(inode, file, file->f_path.dentry,
2396 		(unsigned long long)OCFS2_I(inode)->ip_blkno,
2397 		file->f_path.dentry->d_name.len,
2398 		file->f_path.dentry->d_name.name,
2399 		(unsigned int)from->nr_segs);	/* GRRRRR */
2400 
2401 	if (!direct_io && nowait)
2402 		return -EOPNOTSUPP;
2403 
2404 	if (count == 0)
2405 		return 0;
2406 
2407 	if (nowait) {
2408 		if (!inode_trylock(inode))
2409 			return -EAGAIN;
2410 	} else
2411 		inode_lock(inode);
2412 
2413 	ocfs2_iocb_init_rw_locked(iocb);
2414 
2415 	/*
2416 	 * Concurrent O_DIRECT writes are allowed with
2417 	 * mount_option "coherency=buffered".
2418 	 * For append write, we must take rw EX.
2419 	 */
2420 	rw_level = (!direct_io || full_coherency || append_write);
2421 
2422 	if (nowait)
2423 		ret = ocfs2_try_rw_lock(inode, rw_level);
2424 	else
2425 		ret = ocfs2_rw_lock(inode, rw_level);
2426 	if (ret < 0) {
2427 		if (ret != -EAGAIN)
2428 			mlog_errno(ret);
2429 		goto out_mutex;
2430 	}
2431 
2432 	/*
2433 	 * O_DIRECT writes with "coherency=full" need to take EX cluster
2434 	 * inode_lock to guarantee coherency.
2435 	 */
2436 	if (direct_io && full_coherency) {
2437 		/*
2438 		 * We need to take and drop the inode lock to force
2439 		 * other nodes to drop their caches.  Buffered I/O
2440 		 * already does this in write_begin().
2441 		 */
2442 		if (nowait)
2443 			ret = ocfs2_try_inode_lock(inode, NULL, 1);
2444 		else
2445 			ret = ocfs2_inode_lock(inode, NULL, 1);
2446 		if (ret < 0) {
2447 			if (ret != -EAGAIN)
2448 				mlog_errno(ret);
2449 			goto out;
2450 		}
2451 
2452 		ocfs2_inode_unlock(inode, 1);
2453 	}
2454 
2455 	ret = generic_write_checks(iocb, from);
2456 	if (ret <= 0) {
2457 		if (ret)
2458 			mlog_errno(ret);
2459 		goto out;
2460 	}
2461 	count = ret;
2462 
2463 	ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, count, !nowait);
2464 	if (ret < 0) {
2465 		if (ret != -EAGAIN)
2466 			mlog_errno(ret);
2467 		goto out;
2468 	}
2469 
2470 	if (direct_io && !is_sync_kiocb(iocb) &&
2471 	    ocfs2_is_io_unaligned(inode, count, iocb->ki_pos)) {
2472 		/*
2473 		 * Make it a sync io if it's an unaligned aio.
2474 		 */
2475 		saved_ki_complete = xchg(&iocb->ki_complete, NULL);
2476 	}
2477 
2478 	/* communicate with ocfs2_dio_end_io */
2479 	ocfs2_iocb_set_rw_locked(iocb, rw_level);
2480 
2481 	written = __generic_file_write_iter(iocb, from);
2482 	/* buffered aio wouldn't have proper lock coverage today */
2483 	BUG_ON(written == -EIOCBQUEUED && !direct_io);
2484 
2485 	/*
2486 	 * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
2487 	 * function pointer which is called when o_direct io completes so that
2488 	 * it can unlock our rw lock.
2489 	 * Unfortunately there are error cases which call end_io and others
2490 	 * that don't.  so we don't have to unlock the rw_lock if either an
2491 	 * async dio is going to do it in the future or an end_io after an
2492 	 * error has already done it.
2493 	 */
2494 	if ((written == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
2495 		rw_level = -1;
2496 	}
2497 
2498 	if (unlikely(written <= 0))
2499 		goto out;
2500 
2501 	if (((file->f_flags & O_DSYNC) && !direct_io) ||
2502 	    IS_SYNC(inode)) {
2503 		ret = filemap_fdatawrite_range(file->f_mapping,
2504 					       iocb->ki_pos - written,
2505 					       iocb->ki_pos - 1);
2506 		if (ret < 0)
2507 			written = ret;
2508 
2509 		if (!ret) {
2510 			ret = jbd2_journal_force_commit(osb->journal->j_journal);
2511 			if (ret < 0)
2512 				written = ret;
2513 		}
2514 
2515 		if (!ret)
2516 			ret = filemap_fdatawait_range(file->f_mapping,
2517 						      iocb->ki_pos - written,
2518 						      iocb->ki_pos - 1);
2519 	}
2520 
2521 out:
2522 	if (saved_ki_complete)
2523 		xchg(&iocb->ki_complete, saved_ki_complete);
2524 
2525 	if (rw_level != -1)
2526 		ocfs2_rw_unlock(inode, rw_level);
2527 
2528 out_mutex:
2529 	inode_unlock(inode);
2530 
2531 	if (written)
2532 		ret = written;
2533 	return ret;
2534 }
2535 
2536 static ssize_t ocfs2_file_read_iter(struct kiocb *iocb,
2537 				   struct iov_iter *to)
2538 {
2539 	int ret = 0, rw_level = -1, lock_level = 0;
2540 	struct file *filp = iocb->ki_filp;
2541 	struct inode *inode = file_inode(filp);
2542 	int direct_io = iocb->ki_flags & IOCB_DIRECT ? 1 : 0;
2543 	int nowait = iocb->ki_flags & IOCB_NOWAIT ? 1 : 0;
2544 
2545 	trace_ocfs2_file_read_iter(inode, filp, filp->f_path.dentry,
2546 			(unsigned long long)OCFS2_I(inode)->ip_blkno,
2547 			filp->f_path.dentry->d_name.len,
2548 			filp->f_path.dentry->d_name.name,
2549 			to->nr_segs);	/* GRRRRR */
2550 
2551 
2552 	if (!inode) {
2553 		ret = -EINVAL;
2554 		mlog_errno(ret);
2555 		goto bail;
2556 	}
2557 
2558 	if (!direct_io && nowait)
2559 		return -EOPNOTSUPP;
2560 
2561 	ocfs2_iocb_init_rw_locked(iocb);
2562 
2563 	/*
2564 	 * buffered reads protect themselves in ->read_folio().  O_DIRECT reads
2565 	 * need locks to protect pending reads from racing with truncate.
2566 	 */
2567 	if (direct_io) {
2568 		if (nowait)
2569 			ret = ocfs2_try_rw_lock(inode, 0);
2570 		else
2571 			ret = ocfs2_rw_lock(inode, 0);
2572 
2573 		if (ret < 0) {
2574 			if (ret != -EAGAIN)
2575 				mlog_errno(ret);
2576 			goto bail;
2577 		}
2578 		rw_level = 0;
2579 		/* communicate with ocfs2_dio_end_io */
2580 		ocfs2_iocb_set_rw_locked(iocb, rw_level);
2581 	}
2582 
2583 	/*
2584 	 * We're fine letting folks race truncates and extending
2585 	 * writes with read across the cluster, just like they can
2586 	 * locally. Hence no rw_lock during read.
2587 	 *
2588 	 * Take and drop the meta data lock to update inode fields
2589 	 * like i_size. This allows the checks down below
2590 	 * copy_splice_read() a chance of actually working.
2591 	 */
2592 	ret = ocfs2_inode_lock_atime(inode, filp->f_path.mnt, &lock_level,
2593 				     !nowait);
2594 	if (ret < 0) {
2595 		if (ret != -EAGAIN)
2596 			mlog_errno(ret);
2597 		goto bail;
2598 	}
2599 	ocfs2_inode_unlock(inode, lock_level);
2600 
2601 	ret = generic_file_read_iter(iocb, to);
2602 	trace_generic_file_read_iter_ret(ret);
2603 
2604 	/* buffered aio wouldn't have proper lock coverage today */
2605 	BUG_ON(ret == -EIOCBQUEUED && !direct_io);
2606 
2607 	/* see ocfs2_file_write_iter */
2608 	if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
2609 		rw_level = -1;
2610 	}
2611 
2612 bail:
2613 	if (rw_level != -1)
2614 		ocfs2_rw_unlock(inode, rw_level);
2615 
2616 	return ret;
2617 }
2618 
2619 static ssize_t ocfs2_file_splice_read(struct file *in, loff_t *ppos,
2620 				      struct pipe_inode_info *pipe,
2621 				      size_t len, unsigned int flags)
2622 {
2623 	struct inode *inode = file_inode(in);
2624 	ssize_t ret = 0;
2625 	int lock_level = 0;
2626 
2627 	trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry,
2628 				     (unsigned long long)OCFS2_I(inode)->ip_blkno,
2629 				     in->f_path.dentry->d_name.len,
2630 				     in->f_path.dentry->d_name.name,
2631 				     flags);
2632 
2633 	/*
2634 	 * We're fine letting folks race truncates and extending writes with
2635 	 * read across the cluster, just like they can locally.  Hence no
2636 	 * rw_lock during read.
2637 	 *
2638 	 * Take and drop the meta data lock to update inode fields like i_size.
2639 	 * This allows the checks down below filemap_splice_read() a chance of
2640 	 * actually working.
2641 	 */
2642 	ret = ocfs2_inode_lock_atime(inode, in->f_path.mnt, &lock_level, 1);
2643 	if (ret < 0) {
2644 		if (ret != -EAGAIN)
2645 			mlog_errno(ret);
2646 		goto bail;
2647 	}
2648 	ocfs2_inode_unlock(inode, lock_level);
2649 
2650 	ret = filemap_splice_read(in, ppos, pipe, len, flags);
2651 	trace_filemap_splice_read_ret(ret);
2652 bail:
2653 	return ret;
2654 }
2655 
2656 /* Refer generic_file_llseek_unlocked() */
2657 static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence)
2658 {
2659 	struct inode *inode = file->f_mapping->host;
2660 	int ret = 0;
2661 
2662 	inode_lock(inode);
2663 
2664 	switch (whence) {
2665 	case SEEK_SET:
2666 		break;
2667 	case SEEK_END:
2668 		/* SEEK_END requires the OCFS2 inode lock for the file
2669 		 * because it references the file's size.
2670 		 */
2671 		ret = ocfs2_inode_lock(inode, NULL, 0);
2672 		if (ret < 0) {
2673 			mlog_errno(ret);
2674 			goto out;
2675 		}
2676 		offset += i_size_read(inode);
2677 		ocfs2_inode_unlock(inode, 0);
2678 		break;
2679 	case SEEK_CUR:
2680 		if (offset == 0) {
2681 			offset = file->f_pos;
2682 			goto out;
2683 		}
2684 		offset += file->f_pos;
2685 		break;
2686 	case SEEK_DATA:
2687 	case SEEK_HOLE:
2688 		ret = ocfs2_seek_data_hole_offset(file, &offset, whence);
2689 		if (ret)
2690 			goto out;
2691 		break;
2692 	default:
2693 		ret = -EINVAL;
2694 		goto out;
2695 	}
2696 
2697 	offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
2698 
2699 out:
2700 	inode_unlock(inode);
2701 	if (ret)
2702 		return ret;
2703 	return offset;
2704 }
2705 
2706 static loff_t ocfs2_remap_file_range(struct file *file_in, loff_t pos_in,
2707 				     struct file *file_out, loff_t pos_out,
2708 				     loff_t len, unsigned int remap_flags)
2709 {
2710 	struct inode *inode_in = file_inode(file_in);
2711 	struct inode *inode_out = file_inode(file_out);
2712 	struct ocfs2_super *osb = OCFS2_SB(inode_in->i_sb);
2713 	struct buffer_head *in_bh = NULL, *out_bh = NULL;
2714 	bool same_inode = (inode_in == inode_out);
2715 	loff_t remapped = 0;
2716 	ssize_t ret;
2717 
2718 	if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
2719 		return -EINVAL;
2720 	if (!ocfs2_refcount_tree(osb))
2721 		return -EOPNOTSUPP;
2722 	if (unlikely(ocfs2_emergency_state(osb)))
2723 		return -EROFS;
2724 
2725 	/* Lock both files against IO */
2726 	ret = ocfs2_reflink_inodes_lock(inode_in, &in_bh, inode_out, &out_bh);
2727 	if (ret)
2728 		return ret;
2729 
2730 	/* Check file eligibility and prepare for block sharing. */
2731 	ret = -EINVAL;
2732 	if ((OCFS2_I(inode_in)->ip_flags & OCFS2_INODE_SYSTEM_FILE) ||
2733 	    (OCFS2_I(inode_out)->ip_flags & OCFS2_INODE_SYSTEM_FILE))
2734 		goto out_unlock;
2735 
2736 	ret = generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
2737 			&len, remap_flags);
2738 	if (ret < 0 || len == 0)
2739 		goto out_unlock;
2740 
2741 	/* Lock out changes to the allocation maps and remap. */
2742 	down_write(&OCFS2_I(inode_in)->ip_alloc_sem);
2743 	if (!same_inode)
2744 		down_write_nested(&OCFS2_I(inode_out)->ip_alloc_sem,
2745 				  SINGLE_DEPTH_NESTING);
2746 
2747 	/* Zap any page cache for the destination file's range. */
2748 	truncate_inode_pages_range(&inode_out->i_data,
2749 				   round_down(pos_out, PAGE_SIZE),
2750 				   round_up(pos_out + len, PAGE_SIZE) - 1);
2751 
2752 	remapped = ocfs2_reflink_remap_blocks(inode_in, in_bh, pos_in,
2753 			inode_out, out_bh, pos_out, len);
2754 	up_write(&OCFS2_I(inode_in)->ip_alloc_sem);
2755 	if (!same_inode)
2756 		up_write(&OCFS2_I(inode_out)->ip_alloc_sem);
2757 	if (remapped < 0) {
2758 		ret = remapped;
2759 		mlog_errno(ret);
2760 		goto out_unlock;
2761 	}
2762 
2763 	/*
2764 	 * Empty the extent map so that we may get the right extent
2765 	 * record from the disk.
2766 	 */
2767 	ocfs2_extent_map_trunc(inode_in, 0);
2768 	ocfs2_extent_map_trunc(inode_out, 0);
2769 
2770 	ret = ocfs2_reflink_update_dest(inode_out, out_bh, pos_out + len);
2771 	if (ret) {
2772 		mlog_errno(ret);
2773 		goto out_unlock;
2774 	}
2775 
2776 out_unlock:
2777 	ocfs2_reflink_inodes_unlock(inode_in, in_bh, inode_out, out_bh);
2778 	return remapped > 0 ? remapped : ret;
2779 }
2780 
2781 static loff_t ocfs2_dir_llseek(struct file *file, loff_t offset, int whence)
2782 {
2783 	struct ocfs2_file_private *fp = file->private_data;
2784 
2785 	return generic_llseek_cookie(file, offset, whence, &fp->cookie);
2786 }
2787 
2788 const struct inode_operations ocfs2_file_iops = {
2789 	.setattr	= ocfs2_setattr,
2790 	.getattr	= ocfs2_getattr,
2791 	.permission	= ocfs2_permission,
2792 	.listxattr	= ocfs2_listxattr,
2793 	.fiemap		= ocfs2_fiemap,
2794 	.get_inode_acl	= ocfs2_iop_get_acl,
2795 	.set_acl	= ocfs2_iop_set_acl,
2796 	.fileattr_get	= ocfs2_fileattr_get,
2797 	.fileattr_set	= ocfs2_fileattr_set,
2798 };
2799 
2800 const struct inode_operations ocfs2_special_file_iops = {
2801 	.setattr	= ocfs2_setattr,
2802 	.getattr	= ocfs2_getattr,
2803 	.listxattr	= ocfs2_listxattr,
2804 	.permission	= ocfs2_permission,
2805 	.get_inode_acl	= ocfs2_iop_get_acl,
2806 	.set_acl	= ocfs2_iop_set_acl,
2807 };
2808 
2809 /*
2810  * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
2811  * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
2812  */
2813 const struct file_operations ocfs2_fops = {
2814 	.llseek		= ocfs2_file_llseek,
2815 	.mmap_prepare	= ocfs2_mmap_prepare,
2816 	.fsync		= ocfs2_sync_file,
2817 	.release	= ocfs2_file_release,
2818 	.open		= ocfs2_file_open,
2819 	.read_iter	= ocfs2_file_read_iter,
2820 	.write_iter	= ocfs2_file_write_iter,
2821 	.unlocked_ioctl	= ocfs2_ioctl,
2822 #ifdef CONFIG_COMPAT
2823 	.compat_ioctl   = ocfs2_compat_ioctl,
2824 #endif
2825 	.lock		= ocfs2_lock,
2826 	.flock		= ocfs2_flock,
2827 	.splice_read	= ocfs2_file_splice_read,
2828 	.splice_write	= iter_file_splice_write,
2829 	.fallocate	= ocfs2_fallocate,
2830 	.remap_file_range = ocfs2_remap_file_range,
2831 	.fop_flags	= FOP_ASYNC_LOCK,
2832 	.setlease	= generic_setlease,
2833 };
2834 
2835 WRAP_DIR_ITER(ocfs2_readdir) // FIXME!
2836 const struct file_operations ocfs2_dops = {
2837 	.llseek		= ocfs2_dir_llseek,
2838 	.read		= generic_read_dir,
2839 	.iterate_shared	= shared_ocfs2_readdir,
2840 	.fsync		= ocfs2_sync_file,
2841 	.release	= ocfs2_dir_release,
2842 	.open		= ocfs2_dir_open,
2843 	.unlocked_ioctl	= ocfs2_ioctl,
2844 #ifdef CONFIG_COMPAT
2845 	.compat_ioctl   = ocfs2_compat_ioctl,
2846 #endif
2847 	.lock		= ocfs2_lock,
2848 	.flock		= ocfs2_flock,
2849 	.fop_flags	= FOP_ASYNC_LOCK,
2850 	.setlease	= generic_setlease,
2851 };
2852 
2853 /*
2854  * POSIX-lockless variants of our file_operations.
2855  *
2856  * These will be used if the underlying cluster stack does not support
2857  * posix file locking, if the user passes the "localflocks" mount
2858  * option, or if we have a local-only fs.
2859  *
2860  * ocfs2_flock is in here because all stacks handle UNIX file locks,
2861  * so we still want it in the case of no stack support for
2862  * plocks. Internally, it will do the right thing when asked to ignore
2863  * the cluster.
2864  */
2865 const struct file_operations ocfs2_fops_no_plocks = {
2866 	.llseek		= ocfs2_file_llseek,
2867 	.mmap_prepare	= ocfs2_mmap_prepare,
2868 	.fsync		= ocfs2_sync_file,
2869 	.release	= ocfs2_file_release,
2870 	.open		= ocfs2_file_open,
2871 	.read_iter	= ocfs2_file_read_iter,
2872 	.write_iter	= ocfs2_file_write_iter,
2873 	.unlocked_ioctl	= ocfs2_ioctl,
2874 #ifdef CONFIG_COMPAT
2875 	.compat_ioctl   = ocfs2_compat_ioctl,
2876 #endif
2877 	.flock		= ocfs2_flock,
2878 	.splice_read	= filemap_splice_read,
2879 	.splice_write	= iter_file_splice_write,
2880 	.fallocate	= ocfs2_fallocate,
2881 	.remap_file_range = ocfs2_remap_file_range,
2882 	.setlease	= generic_setlease,
2883 };
2884 
2885 const struct file_operations ocfs2_dops_no_plocks = {
2886 	.llseek		= ocfs2_dir_llseek,
2887 	.read		= generic_read_dir,
2888 	.iterate_shared	= shared_ocfs2_readdir,
2889 	.fsync		= ocfs2_sync_file,
2890 	.release	= ocfs2_dir_release,
2891 	.open		= ocfs2_dir_open,
2892 	.unlocked_ioctl	= ocfs2_ioctl,
2893 #ifdef CONFIG_COMPAT
2894 	.compat_ioctl   = ocfs2_compat_ioctl,
2895 #endif
2896 	.flock		= ocfs2_flock,
2897 	.setlease	= generic_setlease,
2898 };
2899