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