xref: /OK3568_Linux_fs/kernel/fs/orangefs/file.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * (C) 2001 Clemson University and The University of Chicago
4*4882a593Smuzhiyun  * Copyright 2018 Omnibond Systems, L.L.C.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * See COPYING in top-level directory.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  *  Linux VFS file operations.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "protocol.h"
14*4882a593Smuzhiyun #include "orangefs-kernel.h"
15*4882a593Smuzhiyun #include "orangefs-bufmap.h"
16*4882a593Smuzhiyun #include <linux/fs.h>
17*4882a593Smuzhiyun #include <linux/pagemap.h>
18*4882a593Smuzhiyun 
flush_racache(struct inode * inode)19*4882a593Smuzhiyun static int flush_racache(struct inode *inode)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
22*4882a593Smuzhiyun 	struct orangefs_kernel_op_s *new_op;
23*4882a593Smuzhiyun 	int ret;
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 	gossip_debug(GOSSIP_UTILS_DEBUG,
26*4882a593Smuzhiyun 	    "%s: %pU: Handle is %pU | fs_id %d\n", __func__,
27*4882a593Smuzhiyun 	    get_khandle_from_ino(inode), &orangefs_inode->refn.khandle,
28*4882a593Smuzhiyun 	    orangefs_inode->refn.fs_id);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	new_op = op_alloc(ORANGEFS_VFS_OP_RA_FLUSH);
31*4882a593Smuzhiyun 	if (!new_op)
32*4882a593Smuzhiyun 		return -ENOMEM;
33*4882a593Smuzhiyun 	new_op->upcall.req.ra_cache_flush.refn = orangefs_inode->refn;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	ret = service_operation(new_op, "orangefs_flush_racache",
36*4882a593Smuzhiyun 	    get_interruptible_flag(inode));
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: got return value of %d\n",
39*4882a593Smuzhiyun 	    __func__, ret);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	op_release(new_op);
42*4882a593Smuzhiyun 	return ret;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * Post and wait for the I/O upcall to finish
47*4882a593Smuzhiyun  */
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)48*4882a593Smuzhiyun ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inode,
49*4882a593Smuzhiyun 	loff_t *offset, struct iov_iter *iter, size_t total_size,
50*4882a593Smuzhiyun 	loff_t readahead_size, struct orangefs_write_range *wr,
51*4882a593Smuzhiyun 	int *index_return, struct file *file)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
54*4882a593Smuzhiyun 	struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
55*4882a593Smuzhiyun 	struct orangefs_kernel_op_s *new_op = NULL;
56*4882a593Smuzhiyun 	int buffer_index;
57*4882a593Smuzhiyun 	ssize_t ret;
58*4882a593Smuzhiyun 	size_t copy_amount;
59*4882a593Smuzhiyun 	int open_for_read;
60*4882a593Smuzhiyun 	int open_for_write;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO);
63*4882a593Smuzhiyun 	if (!new_op)
64*4882a593Smuzhiyun 		return -ENOMEM;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	/* synchronous I/O */
67*4882a593Smuzhiyun 	new_op->upcall.req.io.readahead_size = readahead_size;
68*4882a593Smuzhiyun 	new_op->upcall.req.io.io_type = type;
69*4882a593Smuzhiyun 	new_op->upcall.req.io.refn = orangefs_inode->refn;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun populate_shared_memory:
72*4882a593Smuzhiyun 	/* get a shared buffer index */
73*4882a593Smuzhiyun 	buffer_index = orangefs_bufmap_get();
74*4882a593Smuzhiyun 	if (buffer_index < 0) {
75*4882a593Smuzhiyun 		ret = buffer_index;
76*4882a593Smuzhiyun 		gossip_debug(GOSSIP_FILE_DEBUG,
77*4882a593Smuzhiyun 			     "%s: orangefs_bufmap_get failure (%zd)\n",
78*4882a593Smuzhiyun 			     __func__, ret);
79*4882a593Smuzhiyun 		goto out;
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
82*4882a593Smuzhiyun 		     "%s(%pU): GET op %p -> buffer_index %d\n",
83*4882a593Smuzhiyun 		     __func__,
84*4882a593Smuzhiyun 		     handle,
85*4882a593Smuzhiyun 		     new_op,
86*4882a593Smuzhiyun 		     buffer_index);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	new_op->uses_shared_memory = 1;
89*4882a593Smuzhiyun 	new_op->upcall.req.io.buf_index = buffer_index;
90*4882a593Smuzhiyun 	new_op->upcall.req.io.count = total_size;
91*4882a593Smuzhiyun 	new_op->upcall.req.io.offset = *offset;
92*4882a593Smuzhiyun 	if (type == ORANGEFS_IO_WRITE && wr) {
93*4882a593Smuzhiyun 		new_op->upcall.uid = from_kuid(&init_user_ns, wr->uid);
94*4882a593Smuzhiyun 		new_op->upcall.gid = from_kgid(&init_user_ns, wr->gid);
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 	/*
97*4882a593Smuzhiyun 	 * Orangefs has no open, and orangefs checks file permissions
98*4882a593Smuzhiyun 	 * on each file access. Posix requires that file permissions
99*4882a593Smuzhiyun 	 * be checked on open and nowhere else. Orangefs-through-the-kernel
100*4882a593Smuzhiyun 	 * needs to seem posix compliant.
101*4882a593Smuzhiyun 	 *
102*4882a593Smuzhiyun 	 * The VFS opens files, even if the filesystem provides no
103*4882a593Smuzhiyun 	 * method. We can see if a file was successfully opened for
104*4882a593Smuzhiyun 	 * read and or for write by looking at file->f_mode.
105*4882a593Smuzhiyun 	 *
106*4882a593Smuzhiyun 	 * When writes are flowing from the page cache, file is no
107*4882a593Smuzhiyun 	 * longer available. We can trust the VFS to have checked
108*4882a593Smuzhiyun 	 * file->f_mode before writing to the page cache.
109*4882a593Smuzhiyun 	 *
110*4882a593Smuzhiyun 	 * The mode of a file might change between when it is opened
111*4882a593Smuzhiyun 	 * and IO commences, or it might be created with an arbitrary mode.
112*4882a593Smuzhiyun 	 *
113*4882a593Smuzhiyun 	 * We'll make sure we don't hit EACCES during the IO stage by
114*4882a593Smuzhiyun 	 * using UID 0. Some of the time we have access without changing
115*4882a593Smuzhiyun 	 * to UID 0 - how to check?
116*4882a593Smuzhiyun 	 */
117*4882a593Smuzhiyun 	if (file) {
118*4882a593Smuzhiyun 		open_for_write = file->f_mode & FMODE_WRITE;
119*4882a593Smuzhiyun 		open_for_read = file->f_mode & FMODE_READ;
120*4882a593Smuzhiyun 	} else {
121*4882a593Smuzhiyun 		open_for_write = 1;
122*4882a593Smuzhiyun 		open_for_read = 0; /* not relevant? */
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 	if ((type == ORANGEFS_IO_WRITE) && open_for_write)
125*4882a593Smuzhiyun 		new_op->upcall.uid = 0;
126*4882a593Smuzhiyun 	if ((type == ORANGEFS_IO_READ) && open_for_read)
127*4882a593Smuzhiyun 		new_op->upcall.uid = 0;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
130*4882a593Smuzhiyun 		     "%s(%pU): offset: %llu total_size: %zd\n",
131*4882a593Smuzhiyun 		     __func__,
132*4882a593Smuzhiyun 		     handle,
133*4882a593Smuzhiyun 		     llu(*offset),
134*4882a593Smuzhiyun 		     total_size);
135*4882a593Smuzhiyun 	/*
136*4882a593Smuzhiyun 	 * Stage 1: copy the buffers into client-core's address space
137*4882a593Smuzhiyun 	 */
138*4882a593Smuzhiyun 	if (type == ORANGEFS_IO_WRITE && total_size) {
139*4882a593Smuzhiyun 		ret = orangefs_bufmap_copy_from_iovec(iter, buffer_index,
140*4882a593Smuzhiyun 		    total_size);
141*4882a593Smuzhiyun 		if (ret < 0) {
142*4882a593Smuzhiyun 			gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
143*4882a593Smuzhiyun 			    __func__, (long)ret);
144*4882a593Smuzhiyun 			goto out;
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
149*4882a593Smuzhiyun 		     "%s(%pU): Calling post_io_request with tag (%llu)\n",
150*4882a593Smuzhiyun 		     __func__,
151*4882a593Smuzhiyun 		     handle,
152*4882a593Smuzhiyun 		     llu(new_op->tag));
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/* Stage 2: Service the I/O operation */
155*4882a593Smuzhiyun 	ret = service_operation(new_op,
156*4882a593Smuzhiyun 				type == ORANGEFS_IO_WRITE ?
157*4882a593Smuzhiyun 					"file_write" :
158*4882a593Smuzhiyun 					"file_read",
159*4882a593Smuzhiyun 				get_interruptible_flag(inode));
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	/*
162*4882a593Smuzhiyun 	 * If service_operation() returns -EAGAIN #and# the operation was
163*4882a593Smuzhiyun 	 * purged from orangefs_request_list or htable_ops_in_progress, then
164*4882a593Smuzhiyun 	 * we know that the client was restarted, causing the shared memory
165*4882a593Smuzhiyun 	 * area to be wiped clean.  To restart a  write operation in this
166*4882a593Smuzhiyun 	 * case, we must re-copy the data from the user's iovec to a NEW
167*4882a593Smuzhiyun 	 * shared memory location. To restart a read operation, we must get
168*4882a593Smuzhiyun 	 * a new shared memory location.
169*4882a593Smuzhiyun 	 */
170*4882a593Smuzhiyun 	if (ret == -EAGAIN && op_state_purged(new_op)) {
171*4882a593Smuzhiyun 		orangefs_bufmap_put(buffer_index);
172*4882a593Smuzhiyun 		if (type == ORANGEFS_IO_WRITE)
173*4882a593Smuzhiyun 			iov_iter_revert(iter, total_size);
174*4882a593Smuzhiyun 		gossip_debug(GOSSIP_FILE_DEBUG,
175*4882a593Smuzhiyun 			     "%s:going to repopulate_shared_memory.\n",
176*4882a593Smuzhiyun 			     __func__);
177*4882a593Smuzhiyun 		goto populate_shared_memory;
178*4882a593Smuzhiyun 	}
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	if (ret < 0) {
181*4882a593Smuzhiyun 		if (ret == -EINTR) {
182*4882a593Smuzhiyun 			/*
183*4882a593Smuzhiyun 			 * We can't return EINTR if any data was written,
184*4882a593Smuzhiyun 			 * it's not POSIX. It is minimally acceptable
185*4882a593Smuzhiyun 			 * to give a partial write, the way NFS does.
186*4882a593Smuzhiyun 			 *
187*4882a593Smuzhiyun 			 * It would be optimal to return all or nothing,
188*4882a593Smuzhiyun 			 * but if a userspace write is bigger than
189*4882a593Smuzhiyun 			 * an IO buffer, and the interrupt occurs
190*4882a593Smuzhiyun 			 * between buffer writes, that would not be
191*4882a593Smuzhiyun 			 * possible.
192*4882a593Smuzhiyun 			 */
193*4882a593Smuzhiyun 			switch (new_op->op_state - OP_VFS_STATE_GIVEN_UP) {
194*4882a593Smuzhiyun 			/*
195*4882a593Smuzhiyun 			 * If the op was waiting when the interrupt
196*4882a593Smuzhiyun 			 * occurred, then the client-core did not
197*4882a593Smuzhiyun 			 * trigger the write.
198*4882a593Smuzhiyun 			 */
199*4882a593Smuzhiyun 			case OP_VFS_STATE_WAITING:
200*4882a593Smuzhiyun 				if (*offset == 0)
201*4882a593Smuzhiyun 					ret = -EINTR;
202*4882a593Smuzhiyun 				else
203*4882a593Smuzhiyun 					ret = 0;
204*4882a593Smuzhiyun 				break;
205*4882a593Smuzhiyun 			/*
206*4882a593Smuzhiyun 			 * If the op was in progress when the interrupt
207*4882a593Smuzhiyun 			 * occurred, then the client-core was able to
208*4882a593Smuzhiyun 			 * trigger the write.
209*4882a593Smuzhiyun 			 */
210*4882a593Smuzhiyun 			case OP_VFS_STATE_INPROGR:
211*4882a593Smuzhiyun 				if (type == ORANGEFS_IO_READ)
212*4882a593Smuzhiyun 					ret = -EINTR;
213*4882a593Smuzhiyun 				else
214*4882a593Smuzhiyun 					ret = total_size;
215*4882a593Smuzhiyun 				break;
216*4882a593Smuzhiyun 			default:
217*4882a593Smuzhiyun 				gossip_err("%s: unexpected op state :%d:.\n",
218*4882a593Smuzhiyun 					   __func__,
219*4882a593Smuzhiyun 					   new_op->op_state);
220*4882a593Smuzhiyun 				ret = 0;
221*4882a593Smuzhiyun 				break;
222*4882a593Smuzhiyun 			}
223*4882a593Smuzhiyun 			gossip_debug(GOSSIP_FILE_DEBUG,
224*4882a593Smuzhiyun 				     "%s: got EINTR, state:%d: %p\n",
225*4882a593Smuzhiyun 				     __func__,
226*4882a593Smuzhiyun 				     new_op->op_state,
227*4882a593Smuzhiyun 				     new_op);
228*4882a593Smuzhiyun 		} else {
229*4882a593Smuzhiyun 			gossip_err("%s: error in %s handle %pU, returning %zd\n",
230*4882a593Smuzhiyun 				__func__,
231*4882a593Smuzhiyun 				type == ORANGEFS_IO_READ ?
232*4882a593Smuzhiyun 					"read from" : "write to",
233*4882a593Smuzhiyun 				handle, ret);
234*4882a593Smuzhiyun 		}
235*4882a593Smuzhiyun 		if (orangefs_cancel_op_in_progress(new_op))
236*4882a593Smuzhiyun 			return ret;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		goto out;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/*
242*4882a593Smuzhiyun 	 * Stage 3: Post copy buffers from client-core's address space
243*4882a593Smuzhiyun 	 */
244*4882a593Smuzhiyun 	if (type == ORANGEFS_IO_READ && new_op->downcall.resp.io.amt_complete) {
245*4882a593Smuzhiyun 		/*
246*4882a593Smuzhiyun 		 * NOTE: the iovector can either contain addresses which
247*4882a593Smuzhiyun 		 *       can futher be kernel-space or user-space addresses.
248*4882a593Smuzhiyun 		 *       or it can pointers to struct page's
249*4882a593Smuzhiyun 		 */
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 		/*
252*4882a593Smuzhiyun 		 * When reading, readahead_size will only be zero when
253*4882a593Smuzhiyun 		 * we're doing O_DIRECT, otherwise we got here from
254*4882a593Smuzhiyun 		 * orangefs_readpage.
255*4882a593Smuzhiyun 		 *
256*4882a593Smuzhiyun 		 * If we got here from orangefs_readpage we want to
257*4882a593Smuzhiyun 		 * copy either a page or the whole file into the io
258*4882a593Smuzhiyun 		 * vector, whichever is smaller.
259*4882a593Smuzhiyun 		 */
260*4882a593Smuzhiyun 		if (readahead_size)
261*4882a593Smuzhiyun 			copy_amount =
262*4882a593Smuzhiyun 				min(new_op->downcall.resp.io.amt_complete,
263*4882a593Smuzhiyun 					(__s64)PAGE_SIZE);
264*4882a593Smuzhiyun 		else
265*4882a593Smuzhiyun 			copy_amount = new_op->downcall.resp.io.amt_complete;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 		ret = orangefs_bufmap_copy_to_iovec(iter, buffer_index,
268*4882a593Smuzhiyun 			copy_amount);
269*4882a593Smuzhiyun 		if (ret < 0) {
270*4882a593Smuzhiyun 			gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
271*4882a593Smuzhiyun 			    __func__, (long)ret);
272*4882a593Smuzhiyun 			goto out;
273*4882a593Smuzhiyun 		}
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
276*4882a593Smuzhiyun 	    "%s(%pU): Amount %s, returned by the sys-io call:%d\n",
277*4882a593Smuzhiyun 	    __func__,
278*4882a593Smuzhiyun 	    handle,
279*4882a593Smuzhiyun 	    type == ORANGEFS_IO_READ ?  "read" : "written",
280*4882a593Smuzhiyun 	    (int)new_op->downcall.resp.io.amt_complete);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	ret = new_op->downcall.resp.io.amt_complete;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun out:
285*4882a593Smuzhiyun 	if (buffer_index >= 0) {
286*4882a593Smuzhiyun 		if ((readahead_size) && (type == ORANGEFS_IO_READ)) {
287*4882a593Smuzhiyun 			/* readpage */
288*4882a593Smuzhiyun 			*index_return = buffer_index;
289*4882a593Smuzhiyun 			gossip_debug(GOSSIP_FILE_DEBUG,
290*4882a593Smuzhiyun 				"%s: hold on to buffer_index :%d:\n",
291*4882a593Smuzhiyun 				__func__, buffer_index);
292*4882a593Smuzhiyun 		} else {
293*4882a593Smuzhiyun 			/* O_DIRECT */
294*4882a593Smuzhiyun 			orangefs_bufmap_put(buffer_index);
295*4882a593Smuzhiyun 			gossip_debug(GOSSIP_FILE_DEBUG,
296*4882a593Smuzhiyun 				"%s(%pU): PUT buffer_index %d\n",
297*4882a593Smuzhiyun 				__func__, handle, buffer_index);
298*4882a593Smuzhiyun 		}
299*4882a593Smuzhiyun 	}
300*4882a593Smuzhiyun 	op_release(new_op);
301*4882a593Smuzhiyun 	return ret;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
orangefs_revalidate_mapping(struct inode * inode)304*4882a593Smuzhiyun int orangefs_revalidate_mapping(struct inode *inode)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
307*4882a593Smuzhiyun 	struct address_space *mapping = inode->i_mapping;
308*4882a593Smuzhiyun 	unsigned long *bitlock = &orangefs_inode->bitlock;
309*4882a593Smuzhiyun 	int ret;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	while (1) {
312*4882a593Smuzhiyun 		ret = wait_on_bit(bitlock, 1, TASK_KILLABLE);
313*4882a593Smuzhiyun 		if (ret)
314*4882a593Smuzhiyun 			return ret;
315*4882a593Smuzhiyun 		spin_lock(&inode->i_lock);
316*4882a593Smuzhiyun 		if (test_bit(1, bitlock)) {
317*4882a593Smuzhiyun 			spin_unlock(&inode->i_lock);
318*4882a593Smuzhiyun 			continue;
319*4882a593Smuzhiyun 		}
320*4882a593Smuzhiyun 		if (!time_before(jiffies, orangefs_inode->mapping_time))
321*4882a593Smuzhiyun 			break;
322*4882a593Smuzhiyun 		spin_unlock(&inode->i_lock);
323*4882a593Smuzhiyun 		return 0;
324*4882a593Smuzhiyun 	}
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	set_bit(1, bitlock);
327*4882a593Smuzhiyun 	smp_wmb();
328*4882a593Smuzhiyun 	spin_unlock(&inode->i_lock);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	unmap_mapping_range(mapping, 0, 0, 0);
331*4882a593Smuzhiyun 	ret = filemap_write_and_wait(mapping);
332*4882a593Smuzhiyun 	if (!ret)
333*4882a593Smuzhiyun 		ret = invalidate_inode_pages2(mapping);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	orangefs_inode->mapping_time = jiffies +
336*4882a593Smuzhiyun 	    orangefs_cache_timeout_msecs*HZ/1000;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	clear_bit(1, bitlock);
339*4882a593Smuzhiyun 	smp_mb__after_atomic();
340*4882a593Smuzhiyun 	wake_up_bit(bitlock, 1);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	return ret;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
orangefs_file_read_iter(struct kiocb * iocb,struct iov_iter * iter)345*4882a593Smuzhiyun static ssize_t orangefs_file_read_iter(struct kiocb *iocb,
346*4882a593Smuzhiyun     struct iov_iter *iter)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	int ret;
349*4882a593Smuzhiyun 	orangefs_stats.reads++;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	down_read(&file_inode(iocb->ki_filp)->i_rwsem);
352*4882a593Smuzhiyun 	ret = orangefs_revalidate_mapping(file_inode(iocb->ki_filp));
353*4882a593Smuzhiyun 	if (ret)
354*4882a593Smuzhiyun 		goto out;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	ret = generic_file_read_iter(iocb, iter);
357*4882a593Smuzhiyun out:
358*4882a593Smuzhiyun 	up_read(&file_inode(iocb->ki_filp)->i_rwsem);
359*4882a593Smuzhiyun 	return ret;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
orangefs_file_write_iter(struct kiocb * iocb,struct iov_iter * iter)362*4882a593Smuzhiyun static ssize_t orangefs_file_write_iter(struct kiocb *iocb,
363*4882a593Smuzhiyun     struct iov_iter *iter)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	int ret;
366*4882a593Smuzhiyun 	orangefs_stats.writes++;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (iocb->ki_pos > i_size_read(file_inode(iocb->ki_filp))) {
369*4882a593Smuzhiyun 		ret = orangefs_revalidate_mapping(file_inode(iocb->ki_filp));
370*4882a593Smuzhiyun 		if (ret)
371*4882a593Smuzhiyun 			return ret;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	ret = generic_file_write_iter(iocb, iter);
375*4882a593Smuzhiyun 	return ret;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
orangefs_getflags(struct inode * inode,unsigned long * uval)378*4882a593Smuzhiyun static int orangefs_getflags(struct inode *inode, unsigned long *uval)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	__u64 val = 0;
381*4882a593Smuzhiyun 	int ret;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	ret = orangefs_inode_getxattr(inode,
384*4882a593Smuzhiyun 				      "user.pvfs2.meta_hint",
385*4882a593Smuzhiyun 				      &val, sizeof(val));
386*4882a593Smuzhiyun 	if (ret < 0 && ret != -ENODATA)
387*4882a593Smuzhiyun 		return ret;
388*4882a593Smuzhiyun 	else if (ret == -ENODATA)
389*4882a593Smuzhiyun 		val = 0;
390*4882a593Smuzhiyun 	*uval = val;
391*4882a593Smuzhiyun 	return 0;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun /*
395*4882a593Smuzhiyun  * Perform a miscellaneous operation on a file.
396*4882a593Smuzhiyun  */
orangefs_ioctl(struct file * file,unsigned int cmd,unsigned long arg)397*4882a593Smuzhiyun static long orangefs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	struct inode *inode = file_inode(file);
400*4882a593Smuzhiyun 	int ret = -ENOTTY;
401*4882a593Smuzhiyun 	__u64 val = 0;
402*4882a593Smuzhiyun 	unsigned long uval;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
405*4882a593Smuzhiyun 		     "orangefs_ioctl: called with cmd %d\n",
406*4882a593Smuzhiyun 		     cmd);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/*
409*4882a593Smuzhiyun 	 * we understand some general ioctls on files, such as the immutable
410*4882a593Smuzhiyun 	 * and append flags
411*4882a593Smuzhiyun 	 */
412*4882a593Smuzhiyun 	if (cmd == FS_IOC_GETFLAGS) {
413*4882a593Smuzhiyun 		ret = orangefs_getflags(inode, &uval);
414*4882a593Smuzhiyun 		if (ret)
415*4882a593Smuzhiyun 			return ret;
416*4882a593Smuzhiyun 		gossip_debug(GOSSIP_FILE_DEBUG,
417*4882a593Smuzhiyun 			     "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
418*4882a593Smuzhiyun 			     (unsigned long long)uval);
419*4882a593Smuzhiyun 		return put_user(uval, (int __user *)arg);
420*4882a593Smuzhiyun 	} else if (cmd == FS_IOC_SETFLAGS) {
421*4882a593Smuzhiyun 		unsigned long old_uval;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 		ret = 0;
424*4882a593Smuzhiyun 		if (get_user(uval, (int __user *)arg))
425*4882a593Smuzhiyun 			return -EFAULT;
426*4882a593Smuzhiyun 		/*
427*4882a593Smuzhiyun 		 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
428*4882a593Smuzhiyun 		 * is turned on for a file. The user is not allowed to turn
429*4882a593Smuzhiyun 		 * on this bit, but the bit is present if the user first gets
430*4882a593Smuzhiyun 		 * the flags and then updates the flags with some new
431*4882a593Smuzhiyun 		 * settings. So, we ignore it in the following edit. bligon.
432*4882a593Smuzhiyun 		 */
433*4882a593Smuzhiyun 		if ((uval & ~ORANGEFS_MIRROR_FL) &
434*4882a593Smuzhiyun 		    (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
435*4882a593Smuzhiyun 			gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
436*4882a593Smuzhiyun 			return -EINVAL;
437*4882a593Smuzhiyun 		}
438*4882a593Smuzhiyun 		ret = orangefs_getflags(inode, &old_uval);
439*4882a593Smuzhiyun 		if (ret)
440*4882a593Smuzhiyun 			return ret;
441*4882a593Smuzhiyun 		ret = vfs_ioc_setflags_prepare(inode, old_uval, uval);
442*4882a593Smuzhiyun 		if (ret)
443*4882a593Smuzhiyun 			return ret;
444*4882a593Smuzhiyun 		val = uval;
445*4882a593Smuzhiyun 		gossip_debug(GOSSIP_FILE_DEBUG,
446*4882a593Smuzhiyun 			     "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
447*4882a593Smuzhiyun 			     (unsigned long long)val);
448*4882a593Smuzhiyun 		ret = orangefs_inode_setxattr(inode,
449*4882a593Smuzhiyun 					      "user.pvfs2.meta_hint",
450*4882a593Smuzhiyun 					      &val, sizeof(val), 0);
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	return ret;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
orangefs_fault(struct vm_fault * vmf)456*4882a593Smuzhiyun static vm_fault_t orangefs_fault(struct vm_fault *vmf)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun 	struct file *file = vmf->vma->vm_file;
459*4882a593Smuzhiyun 	int ret;
460*4882a593Smuzhiyun 	ret = orangefs_inode_getattr(file->f_mapping->host,
461*4882a593Smuzhiyun 	    ORANGEFS_GETATTR_SIZE);
462*4882a593Smuzhiyun 	if (ret == -ESTALE)
463*4882a593Smuzhiyun 		ret = -EIO;
464*4882a593Smuzhiyun 	if (ret) {
465*4882a593Smuzhiyun 		gossip_err("%s: orangefs_inode_getattr failed, "
466*4882a593Smuzhiyun 		    "ret:%d:.\n", __func__, ret);
467*4882a593Smuzhiyun 		return VM_FAULT_SIGBUS;
468*4882a593Smuzhiyun 	}
469*4882a593Smuzhiyun 	return filemap_fault(vmf);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun static const struct vm_operations_struct orangefs_file_vm_ops = {
473*4882a593Smuzhiyun 	.fault = orangefs_fault,
474*4882a593Smuzhiyun 	.map_pages = filemap_map_pages,
475*4882a593Smuzhiyun 	.page_mkwrite = orangefs_page_mkwrite,
476*4882a593Smuzhiyun };
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun /*
479*4882a593Smuzhiyun  * Memory map a region of a file.
480*4882a593Smuzhiyun  */
orangefs_file_mmap(struct file * file,struct vm_area_struct * vma)481*4882a593Smuzhiyun static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	int ret;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	ret = orangefs_revalidate_mapping(file_inode(file));
486*4882a593Smuzhiyun 	if (ret)
487*4882a593Smuzhiyun 		return ret;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
490*4882a593Smuzhiyun 		     "orangefs_file_mmap: called on %s\n",
491*4882a593Smuzhiyun 		     (file ?
492*4882a593Smuzhiyun 			(char *)file->f_path.dentry->d_name.name :
493*4882a593Smuzhiyun 			(char *)"Unknown"));
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	/* set the sequential readahead hint */
496*4882a593Smuzhiyun 	vma->vm_flags |= VM_SEQ_READ;
497*4882a593Smuzhiyun 	vma->vm_flags &= ~VM_RAND_READ;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	file_accessed(file);
500*4882a593Smuzhiyun 	vma->vm_ops = &orangefs_file_vm_ops;
501*4882a593Smuzhiyun 	return 0;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun #define mapping_nrpages(idata) ((idata)->nrpages)
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun /*
507*4882a593Smuzhiyun  * Called to notify the module that there are no more references to
508*4882a593Smuzhiyun  * this file (i.e. no processes have it open).
509*4882a593Smuzhiyun  *
510*4882a593Smuzhiyun  * \note Not called when each file is closed.
511*4882a593Smuzhiyun  */
orangefs_file_release(struct inode * inode,struct file * file)512*4882a593Smuzhiyun static int orangefs_file_release(struct inode *inode, struct file *file)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
515*4882a593Smuzhiyun 		     "orangefs_file_release: called on %pD\n",
516*4882a593Smuzhiyun 		     file);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/*
519*4882a593Smuzhiyun 	 * remove all associated inode pages from the page cache and
520*4882a593Smuzhiyun 	 * readahead cache (if any); this forces an expensive refresh of
521*4882a593Smuzhiyun 	 * data for the next caller of mmap (or 'get_block' accesses)
522*4882a593Smuzhiyun 	 */
523*4882a593Smuzhiyun 	if (file_inode(file) &&
524*4882a593Smuzhiyun 	    file_inode(file)->i_mapping &&
525*4882a593Smuzhiyun 	    mapping_nrpages(&file_inode(file)->i_data)) {
526*4882a593Smuzhiyun 		if (orangefs_features & ORANGEFS_FEATURE_READAHEAD) {
527*4882a593Smuzhiyun 			gossip_debug(GOSSIP_INODE_DEBUG,
528*4882a593Smuzhiyun 			    "calling flush_racache on %pU\n",
529*4882a593Smuzhiyun 			    get_khandle_from_ino(inode));
530*4882a593Smuzhiyun 			flush_racache(inode);
531*4882a593Smuzhiyun 			gossip_debug(GOSSIP_INODE_DEBUG,
532*4882a593Smuzhiyun 			    "flush_racache finished\n");
533*4882a593Smuzhiyun 		}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun 	return 0;
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun /*
540*4882a593Smuzhiyun  * Push all data for a specific file onto permanent storage.
541*4882a593Smuzhiyun  */
orangefs_fsync(struct file * file,loff_t start,loff_t end,int datasync)542*4882a593Smuzhiyun static int orangefs_fsync(struct file *file,
543*4882a593Smuzhiyun 		       loff_t start,
544*4882a593Smuzhiyun 		       loff_t end,
545*4882a593Smuzhiyun 		       int datasync)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	int ret;
548*4882a593Smuzhiyun 	struct orangefs_inode_s *orangefs_inode =
549*4882a593Smuzhiyun 		ORANGEFS_I(file_inode(file));
550*4882a593Smuzhiyun 	struct orangefs_kernel_op_s *new_op = NULL;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	ret = filemap_write_and_wait_range(file_inode(file)->i_mapping,
553*4882a593Smuzhiyun 	    start, end);
554*4882a593Smuzhiyun 	if (ret < 0)
555*4882a593Smuzhiyun 		return ret;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC);
558*4882a593Smuzhiyun 	if (!new_op)
559*4882a593Smuzhiyun 		return -ENOMEM;
560*4882a593Smuzhiyun 	new_op->upcall.req.fsync.refn = orangefs_inode->refn;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	ret = service_operation(new_op,
563*4882a593Smuzhiyun 			"orangefs_fsync",
564*4882a593Smuzhiyun 			get_interruptible_flag(file_inode(file)));
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
567*4882a593Smuzhiyun 		     "orangefs_fsync got return value of %d\n",
568*4882a593Smuzhiyun 		     ret);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	op_release(new_op);
571*4882a593Smuzhiyun 	return ret;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun /*
575*4882a593Smuzhiyun  * Change the file pointer position for an instance of an open file.
576*4882a593Smuzhiyun  *
577*4882a593Smuzhiyun  * \note If .llseek is overriden, we must acquire lock as described in
578*4882a593Smuzhiyun  *       Documentation/filesystems/locking.rst.
579*4882a593Smuzhiyun  *
580*4882a593Smuzhiyun  * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
581*4882a593Smuzhiyun  * require much changes to the FS
582*4882a593Smuzhiyun  */
orangefs_file_llseek(struct file * file,loff_t offset,int origin)583*4882a593Smuzhiyun static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	int ret = -EINVAL;
586*4882a593Smuzhiyun 	struct inode *inode = file_inode(file);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (origin == SEEK_END) {
589*4882a593Smuzhiyun 		/*
590*4882a593Smuzhiyun 		 * revalidate the inode's file size.
591*4882a593Smuzhiyun 		 * NOTE: We are only interested in file size here,
592*4882a593Smuzhiyun 		 * so we set mask accordingly.
593*4882a593Smuzhiyun 		 */
594*4882a593Smuzhiyun 		ret = orangefs_inode_getattr(file->f_mapping->host,
595*4882a593Smuzhiyun 		    ORANGEFS_GETATTR_SIZE);
596*4882a593Smuzhiyun 		if (ret == -ESTALE)
597*4882a593Smuzhiyun 			ret = -EIO;
598*4882a593Smuzhiyun 		if (ret) {
599*4882a593Smuzhiyun 			gossip_debug(GOSSIP_FILE_DEBUG,
600*4882a593Smuzhiyun 				     "%s:%s:%d calling make bad inode\n",
601*4882a593Smuzhiyun 				     __FILE__,
602*4882a593Smuzhiyun 				     __func__,
603*4882a593Smuzhiyun 				     __LINE__);
604*4882a593Smuzhiyun 			return ret;
605*4882a593Smuzhiyun 		}
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	gossip_debug(GOSSIP_FILE_DEBUG,
609*4882a593Smuzhiyun 		     "orangefs_file_llseek: offset is %ld | origin is %d"
610*4882a593Smuzhiyun 		     " | inode size is %lu\n",
611*4882a593Smuzhiyun 		     (long)offset,
612*4882a593Smuzhiyun 		     origin,
613*4882a593Smuzhiyun 		     (unsigned long)i_size_read(inode));
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	return generic_file_llseek(file, offset, origin);
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun /*
619*4882a593Smuzhiyun  * Support local locks (locks that only this kernel knows about)
620*4882a593Smuzhiyun  * if Orangefs was mounted -o local_lock.
621*4882a593Smuzhiyun  */
orangefs_lock(struct file * filp,int cmd,struct file_lock * fl)622*4882a593Smuzhiyun static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	int rc = -EINVAL;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	if (ORANGEFS_SB(file_inode(filp)->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) {
627*4882a593Smuzhiyun 		if (cmd == F_GETLK) {
628*4882a593Smuzhiyun 			rc = 0;
629*4882a593Smuzhiyun 			posix_test_lock(filp, fl);
630*4882a593Smuzhiyun 		} else {
631*4882a593Smuzhiyun 			rc = posix_lock_file(filp, fl, NULL);
632*4882a593Smuzhiyun 		}
633*4882a593Smuzhiyun 	}
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	return rc;
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun 
orangefs_flush(struct file * file,fl_owner_t id)638*4882a593Smuzhiyun static int orangefs_flush(struct file *file, fl_owner_t id)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun 	/*
641*4882a593Smuzhiyun 	 * This is vfs_fsync_range(file, 0, LLONG_MAX, 0) without the
642*4882a593Smuzhiyun 	 * service_operation in orangefs_fsync.
643*4882a593Smuzhiyun 	 *
644*4882a593Smuzhiyun 	 * Do not send fsync to OrangeFS server on a close.  Do send fsync
645*4882a593Smuzhiyun 	 * on an explicit fsync call.  This duplicates historical OrangeFS
646*4882a593Smuzhiyun 	 * behavior.
647*4882a593Smuzhiyun 	 */
648*4882a593Smuzhiyun 	int r;
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	r = filemap_write_and_wait_range(file->f_mapping, 0, LLONG_MAX);
651*4882a593Smuzhiyun 	if (r > 0)
652*4882a593Smuzhiyun 		return 0;
653*4882a593Smuzhiyun 	else
654*4882a593Smuzhiyun 		return r;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun /** ORANGEFS implementation of VFS file operations */
658*4882a593Smuzhiyun const struct file_operations orangefs_file_operations = {
659*4882a593Smuzhiyun 	.llseek		= orangefs_file_llseek,
660*4882a593Smuzhiyun 	.read_iter	= orangefs_file_read_iter,
661*4882a593Smuzhiyun 	.write_iter	= orangefs_file_write_iter,
662*4882a593Smuzhiyun 	.lock		= orangefs_lock,
663*4882a593Smuzhiyun 	.unlocked_ioctl	= orangefs_ioctl,
664*4882a593Smuzhiyun 	.mmap		= orangefs_file_mmap,
665*4882a593Smuzhiyun 	.open		= generic_file_open,
666*4882a593Smuzhiyun 	.flush		= orangefs_flush,
667*4882a593Smuzhiyun 	.release	= orangefs_file_release,
668*4882a593Smuzhiyun 	.fsync		= orangefs_fsync,
669*4882a593Smuzhiyun };
670