xref: /openbmc/linux/fs/orangefs/file.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25db11c21SMike Marshall /*
35db11c21SMike Marshall  * (C) 2001 Clemson University and The University of Chicago
485ac799cSMartin Brandenburg  * Copyright 2018 Omnibond Systems, L.L.C.
55db11c21SMike Marshall  *
65db11c21SMike Marshall  * See COPYING in top-level directory.
75db11c21SMike Marshall  */
85db11c21SMike Marshall 
95db11c21SMike Marshall /*
105db11c21SMike Marshall  *  Linux VFS file operations.
115db11c21SMike Marshall  */
125db11c21SMike Marshall 
135db11c21SMike Marshall #include "protocol.h"
14575e9461SMike Marshall #include "orangefs-kernel.h"
15575e9461SMike Marshall #include "orangefs-bufmap.h"
165db11c21SMike Marshall #include <linux/fs.h>
175970e15dSJeff Layton #include <linux/filelock.h>
185db11c21SMike Marshall #include <linux/pagemap.h>
195db11c21SMike Marshall 
flush_racache(struct inode * inode)20ed1e1587SMartin Brandenburg static int flush_racache(struct inode *inode)
21ed1e1587SMartin Brandenburg {
22ed1e1587SMartin Brandenburg 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
23ed1e1587SMartin Brandenburg 	struct orangefs_kernel_op_s *new_op;
24ed1e1587SMartin Brandenburg 	int ret;
25ed1e1587SMartin Brandenburg 
26ed1e1587SMartin Brandenburg 	gossip_debug(GOSSIP_UTILS_DEBUG,
27ed1e1587SMartin Brandenburg 	    "%s: %pU: Handle is %pU | fs_id %d\n", __func__,
28ed1e1587SMartin Brandenburg 	    get_khandle_from_ino(inode), &orangefs_inode->refn.khandle,
29ed1e1587SMartin Brandenburg 	    orangefs_inode->refn.fs_id);
30ed1e1587SMartin Brandenburg 
31ed1e1587SMartin Brandenburg 	new_op = op_alloc(ORANGEFS_VFS_OP_RA_FLUSH);
32ed1e1587SMartin Brandenburg 	if (!new_op)
33ed1e1587SMartin Brandenburg 		return -ENOMEM;
34ed1e1587SMartin Brandenburg 	new_op->upcall.req.ra_cache_flush.refn = orangefs_inode->refn;
35ed1e1587SMartin Brandenburg 
36ed1e1587SMartin Brandenburg 	ret = service_operation(new_op, "orangefs_flush_racache",
37ed1e1587SMartin Brandenburg 	    get_interruptible_flag(inode));
38ed1e1587SMartin Brandenburg 
39ed1e1587SMartin Brandenburg 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: got return value of %d\n",
40ed1e1587SMartin Brandenburg 	    __func__, ret);
41ed1e1587SMartin Brandenburg 
42ed1e1587SMartin Brandenburg 	op_release(new_op);
43ed1e1587SMartin Brandenburg 	return ret;
44ed1e1587SMartin Brandenburg }
45ed1e1587SMartin Brandenburg 
465db11c21SMike Marshall /*
475db11c21SMike Marshall  * Post and wait for the I/O upcall to finish
485db11c21SMike Marshall  */
wait_for_direct_io(enum ORANGEFS_io_type type,struct inode * inode,loff_t * offset,struct iov_iter * iter,size_t total_size,loff_t readahead_size,struct orangefs_write_range * wr,int * index_return,struct file * file)49c453dcfcSMartin Brandenburg ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inode,
5052e2d0a3SMartin Brandenburg 	loff_t *offset, struct iov_iter *iter, size_t total_size,
51f9bbb682SMike Marshall 	loff_t readahead_size, struct orangefs_write_range *wr,
52f9bbb682SMike Marshall 	int *index_return, struct file *file)
535db11c21SMike Marshall {
548bb8aefdSYi Liu 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
558bb8aefdSYi Liu 	struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
568bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op = NULL;
57f10789e4SColin Ian King 	int buffer_index;
585db11c21SMike Marshall 	ssize_t ret;
59dd59a647SMike Marshall 	size_t copy_amount;
60f9bbb682SMike Marshall 	int open_for_read;
61f9bbb682SMike Marshall 	int open_for_write;
625db11c21SMike Marshall 
638bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO);
64ed42fe05SAl Viro 	if (!new_op)
65ed42fe05SAl Viro 		return -ENOMEM;
66ed42fe05SAl Viro 
675db11c21SMike Marshall 	/* synchronous I/O */
685db11c21SMike Marshall 	new_op->upcall.req.io.readahead_size = readahead_size;
695db11c21SMike Marshall 	new_op->upcall.req.io.io_type = type;
708bb8aefdSYi Liu 	new_op->upcall.req.io.refn = orangefs_inode->refn;
715db11c21SMike Marshall 
725db11c21SMike Marshall populate_shared_memory:
735db11c21SMike Marshall 	/* get a shared buffer index */
74b8a99a8fSAl Viro 	buffer_index = orangefs_bufmap_get();
75b8a99a8fSAl Viro 	if (buffer_index < 0) {
76b8a99a8fSAl Viro 		ret = buffer_index;
775db11c21SMike Marshall 		gossip_debug(GOSSIP_FILE_DEBUG,
78b8a99a8fSAl Viro 			     "%s: orangefs_bufmap_get failure (%zd)\n",
79b8a99a8fSAl Viro 			     __func__, ret);
805db11c21SMike Marshall 		goto out;
815db11c21SMike Marshall 	}
825db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
835db11c21SMike Marshall 		     "%s(%pU): GET op %p -> buffer_index %d\n",
845db11c21SMike Marshall 		     __func__,
855db11c21SMike Marshall 		     handle,
865db11c21SMike Marshall 		     new_op,
875db11c21SMike Marshall 		     buffer_index);
885db11c21SMike Marshall 
895db11c21SMike Marshall 	new_op->uses_shared_memory = 1;
905db11c21SMike Marshall 	new_op->upcall.req.io.buf_index = buffer_index;
915db11c21SMike Marshall 	new_op->upcall.req.io.count = total_size;
925db11c21SMike Marshall 	new_op->upcall.req.io.offset = *offset;
9352e2d0a3SMartin Brandenburg 	if (type == ORANGEFS_IO_WRITE && wr) {
9452e2d0a3SMartin Brandenburg 		new_op->upcall.uid = from_kuid(&init_user_ns, wr->uid);
9552e2d0a3SMartin Brandenburg 		new_op->upcall.gid = from_kgid(&init_user_ns, wr->gid);
9652e2d0a3SMartin Brandenburg 	}
97f9bbb682SMike Marshall 	/*
98f9bbb682SMike Marshall 	 * Orangefs has no open, and orangefs checks file permissions
99f9bbb682SMike Marshall 	 * on each file access. Posix requires that file permissions
100f9bbb682SMike Marshall 	 * be checked on open and nowhere else. Orangefs-through-the-kernel
101f9bbb682SMike Marshall 	 * needs to seem posix compliant.
102f9bbb682SMike Marshall 	 *
103f9bbb682SMike Marshall 	 * The VFS opens files, even if the filesystem provides no
104f9bbb682SMike Marshall 	 * method. We can see if a file was successfully opened for
105f9bbb682SMike Marshall 	 * read and or for write by looking at file->f_mode.
106f9bbb682SMike Marshall 	 *
107f9bbb682SMike Marshall 	 * When writes are flowing from the page cache, file is no
108f9bbb682SMike Marshall 	 * longer available. We can trust the VFS to have checked
109f9bbb682SMike Marshall 	 * file->f_mode before writing to the page cache.
110f9bbb682SMike Marshall 	 *
111f9bbb682SMike Marshall 	 * The mode of a file might change between when it is opened
112f9bbb682SMike Marshall 	 * and IO commences, or it might be created with an arbitrary mode.
113f9bbb682SMike Marshall 	 *
114f9bbb682SMike Marshall 	 * We'll make sure we don't hit EACCES during the IO stage by
115f9bbb682SMike Marshall 	 * using UID 0. Some of the time we have access without changing
116f9bbb682SMike Marshall 	 * to UID 0 - how to check?
117f9bbb682SMike Marshall 	 */
118f9bbb682SMike Marshall 	if (file) {
119f9bbb682SMike Marshall 		open_for_write = file->f_mode & FMODE_WRITE;
120f9bbb682SMike Marshall 		open_for_read = file->f_mode & FMODE_READ;
121f9bbb682SMike Marshall 	} else {
122f9bbb682SMike Marshall 		open_for_write = 1;
123f9bbb682SMike Marshall 		open_for_read = 0; /* not relevant? */
124f9bbb682SMike Marshall 	}
125f9bbb682SMike Marshall 	if ((type == ORANGEFS_IO_WRITE) && open_for_write)
126f9bbb682SMike Marshall 		new_op->upcall.uid = 0;
127f9bbb682SMike Marshall 	if ((type == ORANGEFS_IO_READ) && open_for_read)
128f9bbb682SMike Marshall 		new_op->upcall.uid = 0;
1295db11c21SMike Marshall 
1305db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
1313c2fcfcbSAl Viro 		     "%s(%pU): offset: %llu total_size: %zd\n",
1325db11c21SMike Marshall 		     __func__,
1335db11c21SMike Marshall 		     handle,
1345db11c21SMike Marshall 		     llu(*offset),
1355db11c21SMike Marshall 		     total_size);
1365db11c21SMike Marshall 	/*
1375db11c21SMike Marshall 	 * Stage 1: copy the buffers into client-core's address space
1385db11c21SMike Marshall 	 */
139dbcb5e7fSMartin Brandenburg 	if (type == ORANGEFS_IO_WRITE && total_size) {
140dbcb5e7fSMartin Brandenburg 		ret = orangefs_bufmap_copy_from_iovec(iter, buffer_index,
1414d1c4404SMike Marshall 		    total_size);
142dbcb5e7fSMartin Brandenburg 		if (ret < 0) {
143dbcb5e7fSMartin Brandenburg 			gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
144dbcb5e7fSMartin Brandenburg 			    __func__, (long)ret);
1455db11c21SMike Marshall 			goto out;
1465db11c21SMike Marshall 		}
147dbcb5e7fSMartin Brandenburg 	}
1485db11c21SMike Marshall 
1495db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
1505db11c21SMike Marshall 		     "%s(%pU): Calling post_io_request with tag (%llu)\n",
1515db11c21SMike Marshall 		     __func__,
1525db11c21SMike Marshall 		     handle,
1535db11c21SMike Marshall 		     llu(new_op->tag));
1545db11c21SMike Marshall 
1555db11c21SMike Marshall 	/* Stage 2: Service the I/O operation */
1565db11c21SMike Marshall 	ret = service_operation(new_op,
1578bb8aefdSYi Liu 				type == ORANGEFS_IO_WRITE ?
1585db11c21SMike Marshall 					"file_write" :
1595db11c21SMike Marshall 					"file_read",
1605db11c21SMike Marshall 				get_interruptible_flag(inode));
1615db11c21SMike Marshall 
1625db11c21SMike Marshall 	/*
1635db11c21SMike Marshall 	 * If service_operation() returns -EAGAIN #and# the operation was
1648bb8aefdSYi Liu 	 * purged from orangefs_request_list or htable_ops_in_progress, then
1655db11c21SMike Marshall 	 * we know that the client was restarted, causing the shared memory
1665db11c21SMike Marshall 	 * area to be wiped clean.  To restart a  write operation in this
1675db11c21SMike Marshall 	 * case, we must re-copy the data from the user's iovec to a NEW
1685db11c21SMike Marshall 	 * shared memory location. To restart a read operation, we must get
1695db11c21SMike Marshall 	 * a new shared memory location.
1705db11c21SMike Marshall 	 */
1715db11c21SMike Marshall 	if (ret == -EAGAIN && op_state_purged(new_op)) {
1721357d06dSAl Viro 		orangefs_bufmap_put(buffer_index);
1737b9761afSAl Viro 		if (type == ORANGEFS_IO_WRITE)
174c63ed807SAl Viro 			iov_iter_revert(iter, total_size);
1755db11c21SMike Marshall 		gossip_debug(GOSSIP_FILE_DEBUG,
1765db11c21SMike Marshall 			     "%s:going to repopulate_shared_memory.\n",
1775db11c21SMike Marshall 			     __func__);
1785db11c21SMike Marshall 		goto populate_shared_memory;
1795db11c21SMike Marshall 	}
1805db11c21SMike Marshall 
1815db11c21SMike Marshall 	if (ret < 0) {
182162ada77SMike Marshall 		if (ret == -EINTR) {
183c0eae8cdSAl Viro 			/*
184162ada77SMike Marshall 			 * We can't return EINTR if any data was written,
185162ada77SMike Marshall 			 * it's not POSIX. It is minimally acceptable
186162ada77SMike Marshall 			 * to give a partial write, the way NFS does.
187162ada77SMike Marshall 			 *
188162ada77SMike Marshall 			 * It would be optimal to return all or nothing,
189162ada77SMike Marshall 			 * but if a userspace write is bigger than
190162ada77SMike Marshall 			 * an IO buffer, and the interrupt occurs
191162ada77SMike Marshall 			 * between buffer writes, that would not be
192162ada77SMike Marshall 			 * possible.
1935db11c21SMike Marshall 			 */
194162ada77SMike Marshall 			switch (new_op->op_state - OP_VFS_STATE_GIVEN_UP) {
195162ada77SMike Marshall 			/*
196162ada77SMike Marshall 			 * If the op was waiting when the interrupt
197162ada77SMike Marshall 			 * occurred, then the client-core did not
198162ada77SMike Marshall 			 * trigger the write.
199162ada77SMike Marshall 			 */
200162ada77SMike Marshall 			case OP_VFS_STATE_WAITING:
201162ada77SMike Marshall 				if (*offset == 0)
202162ada77SMike Marshall 					ret = -EINTR;
2035db11c21SMike Marshall 				else
204162ada77SMike Marshall 					ret = 0;
205162ada77SMike Marshall 				break;
206162ada77SMike Marshall 			/*
207162ada77SMike Marshall 			 * If the op was in progress when the interrupt
208162ada77SMike Marshall 			 * occurred, then the client-core was able to
209162ada77SMike Marshall 			 * trigger the write.
210162ada77SMike Marshall 			 */
211162ada77SMike Marshall 			case OP_VFS_STATE_INPROGR:
21243f34576SMartin Brandenburg 				if (type == ORANGEFS_IO_READ)
21343f34576SMartin Brandenburg 					ret = -EINTR;
21443f34576SMartin Brandenburg 				else
215162ada77SMike Marshall 					ret = total_size;
216162ada77SMike Marshall 				break;
217162ada77SMike Marshall 			default:
218162ada77SMike Marshall 				gossip_err("%s: unexpected op state :%d:.\n",
219162ada77SMike Marshall 					   __func__,
220162ada77SMike Marshall 					   new_op->op_state);
221162ada77SMike Marshall 				ret = 0;
222162ada77SMike Marshall 				break;
223162ada77SMike Marshall 			}
224162ada77SMike Marshall 			gossip_debug(GOSSIP_FILE_DEBUG,
225162ada77SMike Marshall 				     "%s: got EINTR, state:%d: %p\n",
226162ada77SMike Marshall 				     __func__,
227162ada77SMike Marshall 				     new_op->op_state,
228162ada77SMike Marshall 				     new_op);
229162ada77SMike Marshall 		} else {
2305db11c21SMike Marshall 			gossip_err("%s: error in %s handle %pU, returning %zd\n",
2315db11c21SMike Marshall 				__func__,
2328bb8aefdSYi Liu 				type == ORANGEFS_IO_READ ?
2335db11c21SMike Marshall 					"read from" : "write to",
2345db11c21SMike Marshall 				handle, ret);
235162ada77SMike Marshall 		}
23678699e29SAl Viro 		if (orangefs_cancel_op_in_progress(new_op))
23778699e29SAl Viro 			return ret;
23878699e29SAl Viro 
239897c5df6SAl Viro 		goto out;
2405db11c21SMike Marshall 	}
2415db11c21SMike Marshall 
2425db11c21SMike Marshall 	/*
2435db11c21SMike Marshall 	 * Stage 3: Post copy buffers from client-core's address space
2445db11c21SMike Marshall 	 */
245dbcb5e7fSMartin Brandenburg 	if (type == ORANGEFS_IO_READ && new_op->downcall.resp.io.amt_complete) {
246dbcb5e7fSMartin Brandenburg 		/*
247dbcb5e7fSMartin Brandenburg 		 * NOTE: the iovector can either contain addresses which
248dbcb5e7fSMartin Brandenburg 		 *       can futher be kernel-space or user-space addresses.
249dbcb5e7fSMartin Brandenburg 		 *       or it can pointers to struct page's
250dbcb5e7fSMartin Brandenburg 		 */
251dd59a647SMike Marshall 
252dd59a647SMike Marshall 		copy_amount = new_op->downcall.resp.io.amt_complete;
253dd59a647SMike Marshall 
254dbcb5e7fSMartin Brandenburg 		ret = orangefs_bufmap_copy_to_iovec(iter, buffer_index,
255dd59a647SMike Marshall 			copy_amount);
256dbcb5e7fSMartin Brandenburg 		if (ret < 0) {
257dbcb5e7fSMartin Brandenburg 			gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
258dbcb5e7fSMartin Brandenburg 			    __func__, (long)ret);
259897c5df6SAl Viro 			goto out;
2605db11c21SMike Marshall 		}
261dbcb5e7fSMartin Brandenburg 	}
2625db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
2639d9e7ba9SMike Marshall 	    "%s(%pU): Amount %s, returned by the sys-io call:%d\n",
2645db11c21SMike Marshall 	    __func__,
2655db11c21SMike Marshall 	    handle,
2669d9e7ba9SMike Marshall 	    type == ORANGEFS_IO_READ ?  "read" : "written",
2675db11c21SMike Marshall 	    (int)new_op->downcall.resp.io.amt_complete);
2685db11c21SMike Marshall 
2695db11c21SMike Marshall 	ret = new_op->downcall.resp.io.amt_complete;
2705db11c21SMike Marshall 
2715db11c21SMike Marshall out:
2725db11c21SMike Marshall 	if (buffer_index >= 0) {
2731357d06dSAl Viro 		orangefs_bufmap_put(buffer_index);
2745db11c21SMike Marshall 		gossip_debug(GOSSIP_FILE_DEBUG,
2755db11c21SMike Marshall 			"%s(%pU): PUT buffer_index %d\n",
2765db11c21SMike Marshall 			__func__, handle, buffer_index);
2775db11c21SMike Marshall 	}
2785db11c21SMike Marshall 	op_release(new_op);
2795db11c21SMike Marshall 	return ret;
2805db11c21SMike Marshall }
2815db11c21SMike Marshall 
orangefs_revalidate_mapping(struct inode * inode)2828f04e1beSMartin Brandenburg int orangefs_revalidate_mapping(struct inode *inode)
2838f04e1beSMartin Brandenburg {
2848f04e1beSMartin Brandenburg 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
2858f04e1beSMartin Brandenburg 	struct address_space *mapping = inode->i_mapping;
2868f04e1beSMartin Brandenburg 	unsigned long *bitlock = &orangefs_inode->bitlock;
2878f04e1beSMartin Brandenburg 	int ret;
2888f04e1beSMartin Brandenburg 
2898f04e1beSMartin Brandenburg 	while (1) {
2908f04e1beSMartin Brandenburg 		ret = wait_on_bit(bitlock, 1, TASK_KILLABLE);
2918f04e1beSMartin Brandenburg 		if (ret)
2928f04e1beSMartin Brandenburg 			return ret;
2938f04e1beSMartin Brandenburg 		spin_lock(&inode->i_lock);
2948f04e1beSMartin Brandenburg 		if (test_bit(1, bitlock)) {
2958f04e1beSMartin Brandenburg 			spin_unlock(&inode->i_lock);
2968f04e1beSMartin Brandenburg 			continue;
2978f04e1beSMartin Brandenburg 		}
2988f04e1beSMartin Brandenburg 		if (!time_before(jiffies, orangefs_inode->mapping_time))
2998f04e1beSMartin Brandenburg 			break;
3008f04e1beSMartin Brandenburg 		spin_unlock(&inode->i_lock);
3018f04e1beSMartin Brandenburg 		return 0;
3028f04e1beSMartin Brandenburg 	}
3038f04e1beSMartin Brandenburg 
3048f04e1beSMartin Brandenburg 	set_bit(1, bitlock);
3058f04e1beSMartin Brandenburg 	smp_wmb();
3068f04e1beSMartin Brandenburg 	spin_unlock(&inode->i_lock);
3078f04e1beSMartin Brandenburg 
3088f04e1beSMartin Brandenburg 	unmap_mapping_range(mapping, 0, 0, 0);
3098f04e1beSMartin Brandenburg 	ret = filemap_write_and_wait(mapping);
3108f04e1beSMartin Brandenburg 	if (!ret)
3118f04e1beSMartin Brandenburg 		ret = invalidate_inode_pages2(mapping);
3128f04e1beSMartin Brandenburg 
3138f04e1beSMartin Brandenburg 	orangefs_inode->mapping_time = jiffies +
3148f04e1beSMartin Brandenburg 	    orangefs_cache_timeout_msecs*HZ/1000;
3158f04e1beSMartin Brandenburg 
3168f04e1beSMartin Brandenburg 	clear_bit(1, bitlock);
3178f04e1beSMartin Brandenburg 	smp_mb__after_atomic();
3188f04e1beSMartin Brandenburg 	wake_up_bit(bitlock, 1);
3198f04e1beSMartin Brandenburg 
3208f04e1beSMartin Brandenburg 	return ret;
3218f04e1beSMartin Brandenburg }
3228f04e1beSMartin Brandenburg 
orangefs_file_read_iter(struct kiocb * iocb,struct iov_iter * iter)323c453dcfcSMartin Brandenburg static ssize_t orangefs_file_read_iter(struct kiocb *iocb,
324c453dcfcSMartin Brandenburg     struct iov_iter *iter)
3255db11c21SMike Marshall {
3268f04e1beSMartin Brandenburg 	int ret;
327889d5f1bSMartin Brandenburg 	orangefs_stats.reads++;
3288f04e1beSMartin Brandenburg 
3298f04e1beSMartin Brandenburg 	down_read(&file_inode(iocb->ki_filp)->i_rwsem);
3308f04e1beSMartin Brandenburg 	ret = orangefs_revalidate_mapping(file_inode(iocb->ki_filp));
3318f04e1beSMartin Brandenburg 	if (ret)
3328f04e1beSMartin Brandenburg 		goto out;
3338f04e1beSMartin Brandenburg 
3348f04e1beSMartin Brandenburg 	ret = generic_file_read_iter(iocb, iter);
3358f04e1beSMartin Brandenburg out:
3368f04e1beSMartin Brandenburg 	up_read(&file_inode(iocb->ki_filp)->i_rwsem);
3378f04e1beSMartin Brandenburg 	return ret;
3385db11c21SMike Marshall }
3395db11c21SMike Marshall 
orangefs_file_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)340*6bbf64beSDavid Howells static ssize_t orangefs_file_splice_read(struct file *in, loff_t *ppos,
341*6bbf64beSDavid Howells 					 struct pipe_inode_info *pipe,
342*6bbf64beSDavid Howells 					 size_t len, unsigned int flags)
343*6bbf64beSDavid Howells {
344*6bbf64beSDavid Howells 	struct inode *inode = file_inode(in);
345*6bbf64beSDavid Howells 	ssize_t ret;
346*6bbf64beSDavid Howells 
347*6bbf64beSDavid Howells 	orangefs_stats.reads++;
348*6bbf64beSDavid Howells 
349*6bbf64beSDavid Howells 	down_read(&inode->i_rwsem);
350*6bbf64beSDavid Howells 	ret = orangefs_revalidate_mapping(inode);
351*6bbf64beSDavid Howells 	if (ret)
352*6bbf64beSDavid Howells 		goto out;
353*6bbf64beSDavid Howells 
354*6bbf64beSDavid Howells 	ret = filemap_splice_read(in, ppos, pipe, len, flags);
355*6bbf64beSDavid Howells out:
356*6bbf64beSDavid Howells 	up_read(&inode->i_rwsem);
357*6bbf64beSDavid Howells 	return ret;
358*6bbf64beSDavid Howells }
359*6bbf64beSDavid Howells 
orangefs_file_write_iter(struct kiocb * iocb,struct iov_iter * iter)36085ac799cSMartin Brandenburg static ssize_t orangefs_file_write_iter(struct kiocb *iocb,
36185ac799cSMartin Brandenburg     struct iov_iter *iter)
3625db11c21SMike Marshall {
3638f04e1beSMartin Brandenburg 	int ret;
364889d5f1bSMartin Brandenburg 	orangefs_stats.writes++;
3658f04e1beSMartin Brandenburg 
3668f04e1beSMartin Brandenburg 	if (iocb->ki_pos > i_size_read(file_inode(iocb->ki_filp))) {
3678f04e1beSMartin Brandenburg 		ret = orangefs_revalidate_mapping(file_inode(iocb->ki_filp));
3688f04e1beSMartin Brandenburg 		if (ret)
3698f04e1beSMartin Brandenburg 			return ret;
3708f04e1beSMartin Brandenburg 	}
3718f04e1beSMartin Brandenburg 
3728f04e1beSMartin Brandenburg 	ret = generic_file_write_iter(iocb, iter);
3738f04e1beSMartin Brandenburg 	return ret;
3745db11c21SMike Marshall }
3755db11c21SMike Marshall 
orangefs_fault(struct vm_fault * vmf)3768bf782f6SSouptick Joarder static vm_fault_t orangefs_fault(struct vm_fault *vmf)
377a5135eeaSMartin Brandenburg {
378a5135eeaSMartin Brandenburg 	struct file *file = vmf->vma->vm_file;
3798bf782f6SSouptick Joarder 	int ret;
3808b60785cSMartin Brandenburg 	ret = orangefs_inode_getattr(file->f_mapping->host,
3818b60785cSMartin Brandenburg 	    ORANGEFS_GETATTR_SIZE);
3828bf782f6SSouptick Joarder 	if (ret == -ESTALE)
3838bf782f6SSouptick Joarder 		ret = -EIO;
3848bf782f6SSouptick Joarder 	if (ret) {
3858b60785cSMartin Brandenburg 		gossip_err("%s: orangefs_inode_getattr failed, "
3868b60785cSMartin Brandenburg 		    "ret:%d:.\n", __func__, ret);
3878bf782f6SSouptick Joarder 		return VM_FAULT_SIGBUS;
388a5135eeaSMartin Brandenburg 	}
389a5135eeaSMartin Brandenburg 	return filemap_fault(vmf);
390a5135eeaSMartin Brandenburg }
391a5135eeaSMartin Brandenburg 
392ec62e95aSColin Ian King static const struct vm_operations_struct orangefs_file_vm_ops = {
393a5135eeaSMartin Brandenburg 	.fault = orangefs_fault,
394a5135eeaSMartin Brandenburg 	.map_pages = filemap_map_pages,
39552e2d0a3SMartin Brandenburg 	.page_mkwrite = orangefs_page_mkwrite,
396a5135eeaSMartin Brandenburg };
397a5135eeaSMartin Brandenburg 
3985db11c21SMike Marshall /*
3995db11c21SMike Marshall  * Memory map a region of a file.
4005db11c21SMike Marshall  */
orangefs_file_mmap(struct file * file,struct vm_area_struct * vma)4018bb8aefdSYi Liu static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma)
4025db11c21SMike Marshall {
4038f04e1beSMartin Brandenburg 	int ret;
4048f04e1beSMartin Brandenburg 
4058f04e1beSMartin Brandenburg 	ret = orangefs_revalidate_mapping(file_inode(file));
4068f04e1beSMartin Brandenburg 	if (ret)
4078f04e1beSMartin Brandenburg 		return ret;
4088f04e1beSMartin Brandenburg 
4095db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
410d6756841SAl Viro 		     "orangefs_file_mmap: called on %pD\n", file);
4115db11c21SMike Marshall 
4125db11c21SMike Marshall 	/* set the sequential readahead hint */
4131c71222eSSuren Baghdasaryan 	vm_flags_mod(vma, VM_SEQ_READ, VM_RAND_READ);
41435390803SMartin Brandenburg 
415a5135eeaSMartin Brandenburg 	file_accessed(file);
416a5135eeaSMartin Brandenburg 	vma->vm_ops = &orangefs_file_vm_ops;
417a5135eeaSMartin Brandenburg 	return 0;
4185db11c21SMike Marshall }
4195db11c21SMike Marshall 
4205db11c21SMike Marshall #define mapping_nrpages(idata) ((idata)->nrpages)
4215db11c21SMike Marshall 
4225db11c21SMike Marshall /*
4235db11c21SMike Marshall  * Called to notify the module that there are no more references to
4245db11c21SMike Marshall  * this file (i.e. no processes have it open).
4255db11c21SMike Marshall  *
4265db11c21SMike Marshall  * \note Not called when each file is closed.
4275db11c21SMike Marshall  */
orangefs_file_release(struct inode * inode,struct file * file)4288bb8aefdSYi Liu static int orangefs_file_release(struct inode *inode, struct file *file)
4295db11c21SMike Marshall {
4305db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
431f66debf1SAl Viro 		     "orangefs_file_release: called on %pD\n",
432f66debf1SAl Viro 		     file);
4335db11c21SMike Marshall 
4345db11c21SMike Marshall 	/*
4356eaff8c7SMartin Brandenburg 	 * remove all associated inode pages from the page cache and
43654804949SMike Marshall 	 * readahead cache (if any); this forces an expensive refresh of
43754804949SMike Marshall 	 * data for the next caller of mmap (or 'get_block' accesses)
4385db11c21SMike Marshall 	 */
4394094d98eSAl Viro 	if (mapping_nrpages(file->f_mapping)) {
440c51e0129SMartin Brandenburg 		if (orangefs_features & ORANGEFS_FEATURE_READAHEAD) {
441ed1e1587SMartin Brandenburg 			gossip_debug(GOSSIP_INODE_DEBUG,
442ed1e1587SMartin Brandenburg 			    "calling flush_racache on %pU\n",
443ed1e1587SMartin Brandenburg 			    get_khandle_from_ino(inode));
444ed1e1587SMartin Brandenburg 			flush_racache(inode);
445c51e0129SMartin Brandenburg 			gossip_debug(GOSSIP_INODE_DEBUG,
446c51e0129SMartin Brandenburg 			    "flush_racache finished\n");
447c51e0129SMartin Brandenburg 		}
448c472ebc2SMartin Brandenburg 
449ed1e1587SMartin Brandenburg 	}
4505db11c21SMike Marshall 	return 0;
4515db11c21SMike Marshall }
4525db11c21SMike Marshall 
4535db11c21SMike Marshall /*
4545db11c21SMike Marshall  * Push all data for a specific file onto permanent storage.
4555db11c21SMike Marshall  */
orangefs_fsync(struct file * file,loff_t start,loff_t end,int datasync)4568bb8aefdSYi Liu static int orangefs_fsync(struct file *file,
45784d02150SMike Marshall 		       loff_t start,
45884d02150SMike Marshall 		       loff_t end,
45984d02150SMike Marshall 		       int datasync)
4605db11c21SMike Marshall {
46149e55713SJeff Layton 	int ret;
4628bb8aefdSYi Liu 	struct orangefs_inode_s *orangefs_inode =
463d62a9025SAmir Goldstein 		ORANGEFS_I(file_inode(file));
4648bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op = NULL;
4655db11c21SMike Marshall 
46685ac799cSMartin Brandenburg 	ret = filemap_write_and_wait_range(file_inode(file)->i_mapping,
46785ac799cSMartin Brandenburg 	    start, end);
46885ac799cSMartin Brandenburg 	if (ret < 0)
46985ac799cSMartin Brandenburg 		return ret;
47085ac799cSMartin Brandenburg 
4718bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC);
4725db11c21SMike Marshall 	if (!new_op)
4735db11c21SMike Marshall 		return -ENOMEM;
4748bb8aefdSYi Liu 	new_op->upcall.req.fsync.refn = orangefs_inode->refn;
4755db11c21SMike Marshall 
4765db11c21SMike Marshall 	ret = service_operation(new_op,
4778bb8aefdSYi Liu 			"orangefs_fsync",
478d62a9025SAmir Goldstein 			get_interruptible_flag(file_inode(file)));
4795db11c21SMike Marshall 
4805db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
4818bb8aefdSYi Liu 		     "orangefs_fsync got return value of %d\n",
4825db11c21SMike Marshall 		     ret);
4835db11c21SMike Marshall 
4845db11c21SMike Marshall 	op_release(new_op);
4855db11c21SMike Marshall 	return ret;
4865db11c21SMike Marshall }
4875db11c21SMike Marshall 
4885db11c21SMike Marshall /*
4895db11c21SMike Marshall  * Change the file pointer position for an instance of an open file.
4905db11c21SMike Marshall  *
4915db11c21SMike Marshall  * \note If .llseek is overriden, we must acquire lock as described in
492ec23eb54SMauro Carvalho Chehab  *       Documentation/filesystems/locking.rst.
4935db11c21SMike Marshall  *
4945db11c21SMike Marshall  * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
4955db11c21SMike Marshall  * require much changes to the FS
4965db11c21SMike Marshall  */
orangefs_file_llseek(struct file * file,loff_t offset,int origin)4978bb8aefdSYi Liu static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin)
4985db11c21SMike Marshall {
4995db11c21SMike Marshall 	int ret = -EINVAL;
500177f8fc4SAl Viro 	struct inode *inode = file_inode(file);
5015db11c21SMike Marshall 
502177f8fc4SAl Viro 	if (origin == SEEK_END) {
5035db11c21SMike Marshall 		/*
5045db11c21SMike Marshall 		 * revalidate the inode's file size.
5055db11c21SMike Marshall 		 * NOTE: We are only interested in file size here,
5065db11c21SMike Marshall 		 * so we set mask accordingly.
5075db11c21SMike Marshall 		 */
5088b60785cSMartin Brandenburg 		ret = orangefs_inode_getattr(file->f_mapping->host,
5098b60785cSMartin Brandenburg 		    ORANGEFS_GETATTR_SIZE);
510e2f7f0d7SMartin Brandenburg 		if (ret == -ESTALE)
511e2f7f0d7SMartin Brandenburg 			ret = -EIO;
5125db11c21SMike Marshall 		if (ret) {
5135db11c21SMike Marshall 			gossip_debug(GOSSIP_FILE_DEBUG,
5145db11c21SMike Marshall 				     "%s:%s:%d calling make bad inode\n",
5155db11c21SMike Marshall 				     __FILE__,
5165db11c21SMike Marshall 				     __func__,
5175db11c21SMike Marshall 				     __LINE__);
5185db11c21SMike Marshall 			return ret;
5195db11c21SMike Marshall 		}
5205db11c21SMike Marshall 	}
5215db11c21SMike Marshall 
5225db11c21SMike Marshall 	gossip_debug(GOSSIP_FILE_DEBUG,
5238bb8aefdSYi Liu 		     "orangefs_file_llseek: offset is %ld | origin is %d"
52454804949SMike Marshall 		     " | inode size is %lu\n",
5255db11c21SMike Marshall 		     (long)offset,
5265db11c21SMike Marshall 		     origin,
527177f8fc4SAl Viro 		     (unsigned long)i_size_read(inode));
5285db11c21SMike Marshall 
5295db11c21SMike Marshall 	return generic_file_llseek(file, offset, origin);
5305db11c21SMike Marshall }
5315db11c21SMike Marshall 
5325db11c21SMike Marshall /*
5335db11c21SMike Marshall  * Support local locks (locks that only this kernel knows about)
5345db11c21SMike Marshall  * if Orangefs was mounted -o local_lock.
5355db11c21SMike Marshall  */
orangefs_lock(struct file * filp,int cmd,struct file_lock * fl)5368bb8aefdSYi Liu static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl)
5375db11c21SMike Marshall {
538f957ae2dSMike Marshall 	int rc = -EINVAL;
5395db11c21SMike Marshall 
54045063097SAl Viro 	if (ORANGEFS_SB(file_inode(filp)->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) {
5415db11c21SMike Marshall 		if (cmd == F_GETLK) {
5425db11c21SMike Marshall 			rc = 0;
5435db11c21SMike Marshall 			posix_test_lock(filp, fl);
5445db11c21SMike Marshall 		} else {
5455db11c21SMike Marshall 			rc = posix_lock_file(filp, fl, NULL);
5465db11c21SMike Marshall 		}
5475db11c21SMike Marshall 	}
5485db11c21SMike Marshall 
5495db11c21SMike Marshall 	return rc;
5505db11c21SMike Marshall }
5515db11c21SMike Marshall 
orangefs_flush(struct file * file,fl_owner_t id)55285ac799cSMartin Brandenburg static int orangefs_flush(struct file *file, fl_owner_t id)
55385ac799cSMartin Brandenburg {
55490fc0706SMartin Brandenburg 	/*
55590fc0706SMartin Brandenburg 	 * This is vfs_fsync_range(file, 0, LLONG_MAX, 0) without the
55690fc0706SMartin Brandenburg 	 * service_operation in orangefs_fsync.
55790fc0706SMartin Brandenburg 	 *
55890fc0706SMartin Brandenburg 	 * Do not send fsync to OrangeFS server on a close.  Do send fsync
55990fc0706SMartin Brandenburg 	 * on an explicit fsync call.  This duplicates historical OrangeFS
56090fc0706SMartin Brandenburg 	 * behavior.
56190fc0706SMartin Brandenburg 	 */
56290fc0706SMartin Brandenburg 	int r;
56390fc0706SMartin Brandenburg 
56490fc0706SMartin Brandenburg 	r = filemap_write_and_wait_range(file->f_mapping, 0, LLONG_MAX);
56590fc0706SMartin Brandenburg 	if (r > 0)
56690fc0706SMartin Brandenburg 		return 0;
56790fc0706SMartin Brandenburg 	else
56890fc0706SMartin Brandenburg 		return r;
56985ac799cSMartin Brandenburg }
57085ac799cSMartin Brandenburg 
5718bb8aefdSYi Liu /** ORANGEFS implementation of VFS file operations */
5728bb8aefdSYi Liu const struct file_operations orangefs_file_operations = {
5738bb8aefdSYi Liu 	.llseek		= orangefs_file_llseek,
5748bb8aefdSYi Liu 	.read_iter	= orangefs_file_read_iter,
5758bb8aefdSYi Liu 	.write_iter	= orangefs_file_write_iter,
5768bb8aefdSYi Liu 	.lock		= orangefs_lock,
5778bb8aefdSYi Liu 	.mmap		= orangefs_file_mmap,
578ec95f1deSMike Marshall 	.open		= generic_file_open,
579*6bbf64beSDavid Howells 	.splice_read    = orangefs_file_splice_read,
580c1048828SMike Marshall 	.splice_write   = iter_file_splice_write,
58185ac799cSMartin Brandenburg 	.flush		= orangefs_flush,
5828bb8aefdSYi Liu 	.release	= orangefs_file_release,
5838bb8aefdSYi Liu 	.fsync		= orangefs_fsync,
5845db11c21SMike Marshall };
585