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