/* CoW any and all clusters between cpos and cpos+write_len. Don't CoW past max_cpos. If this returns successfully, all clusters between cpos and cpos+write_len are safe to modify.  */
int ocfs2_refcount_cow(struct inode *inode, struct buffer_head *di_bh, u32 cpos, u32 write_len, u32 max_cpos)
/* CoW any and all clusters between cpos and cpos+write_len. Don't CoW past max_cpos. If this returns successfully, all clusters between cpos and cpos+write_len are safe to modify.  */
int ocfs2_refcount_cow(struct inode *inode, struct buffer_head *di_bh, u32 cpos, u32 write_len, u32 max_cpos)
{
	int ret = 0;
	u32 p_cluster, num_clusters;
	unsigned int ext_flags;
	while (write_len) {
		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
					 &num_clusters, &ext_flags);
		if (ret) {
			mlog_errno(ret);
			break;
		}
		if (write_len < num_clusters)
			num_clusters = write_len;
		if (ext_flags & OCFS2_EXT_REFCOUNTED) {
			ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
						      num_clusters, max_cpos);
			if (ret) {
				mlog_errno(ret);
				break;
			}
		}
		write_len -= num_clusters;
		cpos += num_clusters;
	}
	return ret;
}