blob: d4a00ad26f6e3526c3ddfb358083b1822bde19ba [file] [log] [blame]
Mike Marshall5db11c22015-07-17 10:38:12 -04001/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7/*
8 * Linux VFS file operations.
9 */
10
11#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -050012#include "orangefs-kernel.h"
13#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040014#include <linux/fs.h>
15#include <linux/pagemap.h>
16
Mike Marshall5db11c22015-07-17 10:38:12 -040017/*
18 * Copy to client-core's address space from the buffers specified
19 * by the iovec upto total_size bytes.
20 * NOTE: the iovector can either contain addresses which
21 * can futher be kernel-space or user-space addresses.
22 * or it can pointers to struct page's
23 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050024static int precopy_buffers(struct orangefs_bufmap *bufmap,
Mike Marshall5db11c22015-07-17 10:38:12 -040025 int buffer_index,
Al Viroa5c126a2015-10-08 17:54:31 -040026 struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040027 size_t total_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040028{
29 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -040030 /*
31 * copy data from application/kernel by pulling it out
32 * of the iovec.
33 */
Mike Marshall4d1c4402015-09-04 10:31:16 -040034
35
36 if (total_size) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050037 ret = orangefs_bufmap_copy_from_iovec(bufmap,
38 iter,
39 buffer_index,
40 total_size);
Mike Marshall4d1c4402015-09-04 10:31:16 -040041 if (ret < 0)
42 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
43 __func__,
44 (long)ret);
Mike Marshall4d1c4402015-09-04 10:31:16 -040045 }
46
Mike Marshall5db11c22015-07-17 10:38:12 -040047 if (ret < 0)
48 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
49 __func__,
50 (long)ret);
51 return ret;
52}
53
54/*
55 * Copy from client-core's address space to the buffers specified
56 * by the iovec upto total_size bytes.
57 * NOTE: the iovector can either contain addresses which
58 * can futher be kernel-space or user-space addresses.
59 * or it can pointers to struct page's
60 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050061static int postcopy_buffers(struct orangefs_bufmap *bufmap,
Mike Marshall5db11c22015-07-17 10:38:12 -040062 int buffer_index,
Al Viro5f0e3c92015-10-08 17:52:44 -040063 struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040064 size_t total_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040065{
66 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -040067 /*
68 * copy data to application/kernel by pushing it out to
69 * the iovec. NOTE; target buffers can be addresses or
70 * struct page pointers.
71 */
72 if (total_size) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050073 ret = orangefs_bufmap_copy_to_iovec(bufmap,
74 iter,
75 buffer_index,
76 total_size);
Mike Marshall5db11c22015-07-17 10:38:12 -040077 if (ret < 0)
Mike Marshall4d1c4402015-09-04 10:31:16 -040078 gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
Mike Marshall5db11c22015-07-17 10:38:12 -040079 __func__,
80 (long)ret);
81 }
82 return ret;
83}
84
85/*
86 * Post and wait for the I/O upcall to finish
87 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050088static ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inode,
Al Viro3c2fcfc2015-10-08 18:00:26 -040089 loff_t *offset, struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040090 size_t total_size, loff_t readahead_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040091{
Yi Liu8bb8aef2015-11-24 15:12:14 -050092 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
93 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
94 struct orangefs_bufmap *bufmap = NULL;
95 struct orangefs_kernel_op_s *new_op = NULL;
Al Viro7b9761a2016-02-07 01:25:06 -050096 struct iov_iter saved = *iter;
Mike Marshall5db11c22015-07-17 10:38:12 -040097 int buffer_index = -1;
98 ssize_t ret;
99
Yi Liu8bb8aef2015-11-24 15:12:14 -0500100 new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO);
Al Viroed42fe02016-01-22 19:47:47 -0500101 if (!new_op)
102 return -ENOMEM;
103
Mike Marshall5db11c22015-07-17 10:38:12 -0400104 /* synchronous I/O */
Mike Marshall5db11c22015-07-17 10:38:12 -0400105 new_op->upcall.req.io.readahead_size = readahead_size;
106 new_op->upcall.req.io.io_type = type;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500107 new_op->upcall.req.io.refn = orangefs_inode->refn;
Mike Marshall5db11c22015-07-17 10:38:12 -0400108
109populate_shared_memory:
110 /* get a shared buffer index */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500111 ret = orangefs_bufmap_get(&bufmap, &buffer_index);
Mike Marshall5db11c22015-07-17 10:38:12 -0400112 if (ret < 0) {
113 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500114 "%s: orangefs_bufmap_get failure (%ld)\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400115 __func__, (long)ret);
116 goto out;
117 }
118 gossip_debug(GOSSIP_FILE_DEBUG,
119 "%s(%pU): GET op %p -> buffer_index %d\n",
120 __func__,
121 handle,
122 new_op,
123 buffer_index);
124
125 new_op->uses_shared_memory = 1;
126 new_op->upcall.req.io.buf_index = buffer_index;
127 new_op->upcall.req.io.count = total_size;
128 new_op->upcall.req.io.offset = *offset;
129
130 gossip_debug(GOSSIP_FILE_DEBUG,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400131 "%s(%pU): offset: %llu total_size: %zd\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400132 __func__,
133 handle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400134 llu(*offset),
135 total_size);
136 /*
137 * Stage 1: copy the buffers into client-core's address space
138 * precopy_buffers only pertains to writes.
139 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500140 if (type == ORANGEFS_IO_WRITE) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400141 ret = precopy_buffers(bufmap,
142 buffer_index,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400143 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400144 total_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400145 if (ret < 0)
146 goto out;
147 }
148
149 gossip_debug(GOSSIP_FILE_DEBUG,
150 "%s(%pU): Calling post_io_request with tag (%llu)\n",
151 __func__,
152 handle,
153 llu(new_op->tag));
154
155 /* Stage 2: Service the I/O operation */
156 ret = service_operation(new_op,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500157 type == ORANGEFS_IO_WRITE ?
Mike Marshall5db11c22015-07-17 10:38:12 -0400158 "file_write" :
159 "file_read",
160 get_interruptible_flag(inode));
161
162 /*
163 * If service_operation() returns -EAGAIN #and# the operation was
Yi Liu8bb8aef2015-11-24 15:12:14 -0500164 * purged from orangefs_request_list or htable_ops_in_progress, then
Mike Marshall5db11c22015-07-17 10:38:12 -0400165 * we know that the client was restarted, causing the shared memory
166 * area to be wiped clean. To restart a write operation in this
167 * case, we must re-copy the data from the user's iovec to a NEW
168 * shared memory location. To restart a read operation, we must get
169 * a new shared memory location.
170 */
171 if (ret == -EAGAIN && op_state_purged(new_op)) {
Al Viro1357d062016-02-11 21:34:52 -0500172 orangefs_bufmap_put(buffer_index);
Al Viroe17be9f2016-02-06 14:59:38 -0500173 buffer_index = -1;
Al Viro7b9761a2016-02-07 01:25:06 -0500174 if (type == ORANGEFS_IO_WRITE)
175 *iter = saved;
Mike Marshall5db11c22015-07-17 10:38:12 -0400176 gossip_debug(GOSSIP_FILE_DEBUG,
177 "%s:going to repopulate_shared_memory.\n",
178 __func__);
179 goto populate_shared_memory;
180 }
181
182 if (ret < 0) {
Mike Marshall162ada72016-03-09 13:12:37 -0500183 if (ret == -EINTR) {
184 /*
185 * We can't return EINTR if any data was written,
186 * it's not POSIX. It is minimally acceptable
187 * to give a partial write, the way NFS does.
188 *
189 * It would be optimal to return all or nothing,
190 * but if a userspace write is bigger than
191 * an IO buffer, and the interrupt occurs
192 * between buffer writes, that would not be
193 * possible.
194 */
195 switch (new_op->op_state - OP_VFS_STATE_GIVEN_UP) {
196 /*
197 * If the op was waiting when the interrupt
198 * occurred, then the client-core did not
199 * trigger the write.
200 */
201 case OP_VFS_STATE_WAITING:
202 if (*offset == 0)
203 ret = -EINTR;
204 else
205 ret = 0;
206 break;
207 /*
208 * If the op was in progress when the interrupt
209 * occurred, then the client-core was able to
210 * trigger the write.
211 */
212 case OP_VFS_STATE_INPROGR:
213 ret = total_size;
214 break;
215 default:
216 gossip_err("%s: unexpected op state :%d:.\n",
217 __func__,
218 new_op->op_state);
219 ret = 0;
220 break;
221 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400222 gossip_debug(GOSSIP_FILE_DEBUG,
Mike Marshall162ada72016-03-09 13:12:37 -0500223 "%s: got EINTR, state:%d: %p\n",
224 __func__,
225 new_op->op_state,
226 new_op);
227 } else {
Mike Marshall5db11c22015-07-17 10:38:12 -0400228 gossip_err("%s: error in %s handle %pU, returning %zd\n",
229 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500230 type == ORANGEFS_IO_READ ?
Mike Marshall5db11c22015-07-17 10:38:12 -0400231 "read from" : "write to",
232 handle, ret);
Mike Marshall162ada72016-03-09 13:12:37 -0500233 }
Al Viro78699e22016-02-11 23:07:19 -0500234 if (orangefs_cancel_op_in_progress(new_op))
235 return ret;
236
Al Viro897c5df2016-02-13 21:06:50 -0500237 goto out;
Mike Marshall5db11c22015-07-17 10:38:12 -0400238 }
239
240 /*
241 * Stage 3: Post copy buffers from client-core's address space
242 * postcopy_buffers only pertains to reads.
243 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500244 if (type == ORANGEFS_IO_READ) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400245 ret = postcopy_buffers(bufmap,
246 buffer_index,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400247 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400248 new_op->downcall.resp.io.amt_complete);
Al Viroc0eae8c2016-02-11 21:28:52 -0500249 if (ret < 0)
Al Viro897c5df2016-02-13 21:06:50 -0500250 goto out;
Mike Marshall5db11c22015-07-17 10:38:12 -0400251 }
252 gossip_debug(GOSSIP_FILE_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500253 "%s(%pU): Amount %s, returned by the sys-io call:%d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400254 __func__,
255 handle,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500256 type == ORANGEFS_IO_READ ? "read" : "written",
Mike Marshall5db11c22015-07-17 10:38:12 -0400257 (int)new_op->downcall.resp.io.amt_complete);
258
259 ret = new_op->downcall.resp.io.amt_complete;
260
Mike Marshall5db11c22015-07-17 10:38:12 -0400261out:
262 if (buffer_index >= 0) {
Al Viro1357d062016-02-11 21:34:52 -0500263 orangefs_bufmap_put(buffer_index);
Mike Marshall5db11c22015-07-17 10:38:12 -0400264 gossip_debug(GOSSIP_FILE_DEBUG,
265 "%s(%pU): PUT buffer_index %d\n",
266 __func__, handle, buffer_index);
267 buffer_index = -1;
268 }
Al Viroed42fe02016-01-22 19:47:47 -0500269 op_release(new_op);
Mike Marshall5db11c22015-07-17 10:38:12 -0400270 return ret;
271}
272
273/*
Mike Marshall5db11c22015-07-17 10:38:12 -0400274 * Common entry point for read/write/readv/writev
275 * This function will dispatch it to either the direct I/O
276 * or buffered I/O path depending on the mount options and/or
277 * augmented/extended metadata attached to the file.
278 * Note: File extended attributes override any mount options.
279 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500280static ssize_t do_readv_writev(enum ORANGEFS_io_type type, struct file *file,
Al Viro0071ed12015-10-08 18:22:08 -0400281 loff_t *offset, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400282{
283 struct inode *inode = file->f_mapping->host;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500284 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
285 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
Al Viro0071ed12015-10-08 18:22:08 -0400286 size_t count = iov_iter_count(iter);
Al Virodc4067f2015-10-08 18:17:26 -0400287 ssize_t total_count = 0;
288 ssize_t ret = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400289
290 gossip_debug(GOSSIP_FILE_DEBUG,
291 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
292 __func__,
293 handle,
294 (int)count);
295
Yi Liu8bb8aef2015-11-24 15:12:14 -0500296 if (type == ORANGEFS_IO_WRITE) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400297 gossip_debug(GOSSIP_FILE_DEBUG,
298 "%s(%pU): proceeding with offset : %llu, "
299 "size %d\n",
300 __func__,
301 handle,
302 llu(*offset),
303 (int)count);
304 }
305
306 if (count == 0) {
307 ret = 0;
308 goto out;
309 }
310
Al Viro0071ed12015-10-08 18:22:08 -0400311 while (iov_iter_count(iter)) {
312 size_t each_count = iov_iter_count(iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400313 size_t amt_complete;
314
315 /* how much to transfer in this loop iteration */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500316 if (each_count > orangefs_bufmap_size_query())
317 each_count = orangefs_bufmap_size_query();
Mike Marshall5db11c22015-07-17 10:38:12 -0400318
319 gossip_debug(GOSSIP_FILE_DEBUG,
320 "%s(%pU): size of each_count(%d)\n",
321 __func__,
322 handle,
323 (int)each_count);
324 gossip_debug(GOSSIP_FILE_DEBUG,
325 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
326 __func__,
327 handle,
328 (int)*offset);
329
Al Viro0071ed12015-10-08 18:22:08 -0400330 ret = wait_for_direct_io(type, inode, offset, iter,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400331 each_count, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400332 gossip_debug(GOSSIP_FILE_DEBUG,
333 "%s(%pU): return from wait_for_io:%d\n",
334 __func__,
335 handle,
336 (int)ret);
337
338 if (ret < 0)
339 goto out;
340
Mike Marshall5db11c22015-07-17 10:38:12 -0400341 *offset += ret;
342 total_count += ret;
343 amt_complete = ret;
344
345 gossip_debug(GOSSIP_FILE_DEBUG,
346 "%s(%pU): AFTER wait_for_io: offset is %d\n",
347 __func__,
348 handle,
349 (int)*offset);
350
351 /*
352 * if we got a short I/O operations,
353 * fall out and return what we got so far
354 */
355 if (amt_complete < each_count)
356 break;
357 } /*end while */
358
359 if (total_count > 0)
360 ret = total_count;
361out:
Mike Marshall5db11c22015-07-17 10:38:12 -0400362 if (ret > 0) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500363 if (type == ORANGEFS_IO_READ) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400364 file_accessed(file);
365 } else {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500366 SetMtimeFlag(orangefs_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400367 inode->i_mtime = CURRENT_TIME;
368 mark_inode_dirty_sync(inode);
369 }
370 }
371
372 gossip_debug(GOSSIP_FILE_DEBUG,
373 "%s(%pU): Value(%d) returned.\n",
374 __func__,
375 handle,
376 (int)ret);
377
378 return ret;
379}
380
381/*
382 * Read data from a specified offset in a file (referenced by inode).
383 * Data may be placed either in a user or kernel buffer.
384 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500385ssize_t orangefs_inode_read(struct inode *inode,
386 struct iov_iter *iter,
387 loff_t *offset,
388 loff_t readahead_size)
Mike Marshall5db11c22015-07-17 10:38:12 -0400389{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500390 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
Al Viro74f68fc2015-10-08 18:31:05 -0400391 size_t count = iov_iter_count(iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400392 size_t bufmap_size;
Mike Marshall5db11c22015-07-17 10:38:12 -0400393 ssize_t ret = -EINVAL;
394
Yi Liu8bb8aef2015-11-24 15:12:14 -0500395 g_orangefs_stats.reads++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400396
Yi Liu8bb8aef2015-11-24 15:12:14 -0500397 bufmap_size = orangefs_bufmap_size_query();
Mike Marshall5db11c22015-07-17 10:38:12 -0400398 if (count > bufmap_size) {
399 gossip_debug(GOSSIP_FILE_DEBUG,
400 "%s: count is too large (%zd/%zd)!\n",
401 __func__, count, bufmap_size);
402 return -EINVAL;
403 }
404
405 gossip_debug(GOSSIP_FILE_DEBUG,
406 "%s(%pU) %zd@%llu\n",
407 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500408 &orangefs_inode->refn.khandle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400409 count,
410 llu(*offset));
411
Yi Liu8bb8aef2015-11-24 15:12:14 -0500412 ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, offset, iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400413 count, readahead_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400414 if (ret > 0)
415 *offset += ret;
416
417 gossip_debug(GOSSIP_FILE_DEBUG,
418 "%s(%pU): Value(%zd) returned.\n",
419 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500420 &orangefs_inode->refn.khandle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400421 ret);
422
423 return ret;
424}
425
Yi Liu8bb8aef2015-11-24 15:12:14 -0500426static ssize_t orangefs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400427{
428 struct file *file = iocb->ki_filp;
429 loff_t pos = *(&iocb->ki_pos);
430 ssize_t rc = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400431
432 BUG_ON(iocb->private);
433
Yi Liu8bb8aef2015-11-24 15:12:14 -0500434 gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_read_iter\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400435
Yi Liu8bb8aef2015-11-24 15:12:14 -0500436 g_orangefs_stats.reads++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400437
Yi Liu8bb8aef2015-11-24 15:12:14 -0500438 rc = do_readv_writev(ORANGEFS_IO_READ, file, &pos, iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400439 iocb->ki_pos = pos;
440
441 return rc;
442}
443
Yi Liu8bb8aef2015-11-24 15:12:14 -0500444static ssize_t orangefs_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400445{
446 struct file *file = iocb->ki_filp;
Mike Marshall3f1b6942015-11-13 13:05:11 -0500447 loff_t pos;
Mike Marshall5db11c22015-07-17 10:38:12 -0400448 ssize_t rc;
449
450 BUG_ON(iocb->private);
451
Yi Liu8bb8aef2015-11-24 15:12:14 -0500452 gossip_debug(GOSSIP_FILE_DEBUG, "orangefs_file_write_iter\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400453
454 mutex_lock(&file->f_mapping->host->i_mutex);
455
456 /* Make sure generic_write_checks sees an up to date inode size. */
457 if (file->f_flags & O_APPEND) {
Martin Brandenburge2f7f0d2016-03-15 12:33:20 -0400458 rc = orangefs_inode_getattr(file->f_mapping->host, 0, 1);
459 if (rc == -ESTALE)
460 rc = -EIO;
Mike Marshall5db11c22015-07-17 10:38:12 -0400461 if (rc) {
Martin Brandenburge2f7f0d2016-03-15 12:33:20 -0400462 gossip_err("%s: orangefs_inode_getattr failed, "
463 "rc:%zd:.\n", __func__, rc);
Mike Marshall5db11c22015-07-17 10:38:12 -0400464 goto out;
465 }
466 }
467
468 if (file->f_pos > i_size_read(file->f_mapping->host))
Yi Liu8bb8aef2015-11-24 15:12:14 -0500469 orangefs_i_size_write(file->f_mapping->host, file->f_pos);
Mike Marshall5db11c22015-07-17 10:38:12 -0400470
471 rc = generic_write_checks(iocb, iter);
472
473 if (rc <= 0) {
474 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
475 __func__, rc);
476 goto out;
477 }
478
Mike Marshall3f1b6942015-11-13 13:05:11 -0500479 /*
480 * if we are appending, generic_write_checks would have updated
481 * pos to the end of the file, so we will wait till now to set
482 * pos...
483 */
484 pos = *(&iocb->ki_pos);
485
Yi Liu8bb8aef2015-11-24 15:12:14 -0500486 rc = do_readv_writev(ORANGEFS_IO_WRITE,
Mike Marshall5db11c22015-07-17 10:38:12 -0400487 file,
488 &pos,
Al Viro0071ed12015-10-08 18:22:08 -0400489 iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400490 if (rc < 0) {
491 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
492 __func__, rc);
493 goto out;
494 }
495
496 iocb->ki_pos = pos;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500497 g_orangefs_stats.writes++;
Mike Marshall5db11c22015-07-17 10:38:12 -0400498
499out:
500
501 mutex_unlock(&file->f_mapping->host->i_mutex);
502 return rc;
503}
504
505/*
506 * Perform a miscellaneous operation on a file.
507 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500508static long orangefs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Mike Marshall5db11c22015-07-17 10:38:12 -0400509{
510 int ret = -ENOTTY;
511 __u64 val = 0;
512 unsigned long uval;
513
514 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500515 "orangefs_ioctl: called with cmd %d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400516 cmd);
517
518 /*
519 * we understand some general ioctls on files, such as the immutable
520 * and append flags
521 */
522 if (cmd == FS_IOC_GETFLAGS) {
523 val = 0;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500524 ret = orangefs_inode_getxattr(file_inode(file),
525 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
526 "user.pvfs2.meta_hint",
527 &val, sizeof(val));
Mike Marshall5db11c22015-07-17 10:38:12 -0400528 if (ret < 0 && ret != -ENODATA)
529 return ret;
530 else if (ret == -ENODATA)
531 val = 0;
532 uval = val;
533 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500534 "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400535 (unsigned long long)uval);
536 return put_user(uval, (int __user *)arg);
537 } else if (cmd == FS_IOC_SETFLAGS) {
538 ret = 0;
539 if (get_user(uval, (int __user *)arg))
540 return -EFAULT;
541 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500542 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
Mike Marshall5db11c22015-07-17 10:38:12 -0400543 * is turned on for a file. The user is not allowed to turn
544 * on this bit, but the bit is present if the user first gets
545 * the flags and then updates the flags with some new
546 * settings. So, we ignore it in the following edit. bligon.
547 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500548 if ((uval & ~ORANGEFS_MIRROR_FL) &
Mike Marshall5db11c22015-07-17 10:38:12 -0400549 (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500550 gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400551 return -EINVAL;
552 }
553 val = uval;
554 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500555 "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400556 (unsigned long long)val);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500557 ret = orangefs_inode_setxattr(file_inode(file),
558 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
559 "user.pvfs2.meta_hint",
560 &val, sizeof(val), 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400561 }
562
563 return ret;
564}
565
566/*
567 * Memory map a region of a file.
568 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500569static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma)
Mike Marshall5db11c22015-07-17 10:38:12 -0400570{
571 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500572 "orangefs_file_mmap: called on %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400573 (file ?
574 (char *)file->f_path.dentry->d_name.name :
575 (char *)"Unknown"));
576
577 /* set the sequential readahead hint */
578 vma->vm_flags |= VM_SEQ_READ;
579 vma->vm_flags &= ~VM_RAND_READ;
Martin Brandenburg35390802015-09-30 13:11:54 -0400580
581 /* Use readonly mmap since we cannot support writable maps. */
582 return generic_file_readonly_mmap(file, vma);
Mike Marshall5db11c22015-07-17 10:38:12 -0400583}
584
585#define mapping_nrpages(idata) ((idata)->nrpages)
586
587/*
588 * Called to notify the module that there are no more references to
589 * this file (i.e. no processes have it open).
590 *
591 * \note Not called when each file is closed.
592 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500593static int orangefs_file_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400594{
595 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500596 "orangefs_file_release: called on %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400597 file->f_path.dentry->d_name.name);
598
Yi Liu8bb8aef2015-11-24 15:12:14 -0500599 orangefs_flush_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400600
601 /*
Mike Marshall54804942015-10-05 13:44:24 -0400602 * remove all associated inode pages from the page cache and mmap
603 * readahead cache (if any); this forces an expensive refresh of
604 * data for the next caller of mmap (or 'get_block' accesses)
Mike Marshall5db11c22015-07-17 10:38:12 -0400605 */
606 if (file->f_path.dentry->d_inode &&
607 file->f_path.dentry->d_inode->i_mapping &&
608 mapping_nrpages(&file->f_path.dentry->d_inode->i_data))
609 truncate_inode_pages(file->f_path.dentry->d_inode->i_mapping,
610 0);
611 return 0;
612}
613
614/*
615 * Push all data for a specific file onto permanent storage.
616 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500617static int orangefs_fsync(struct file *file,
Mike Marshall84d02152015-07-28 13:27:51 -0400618 loff_t start,
619 loff_t end,
620 int datasync)
Mike Marshall5db11c22015-07-17 10:38:12 -0400621{
622 int ret = -EINVAL;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500623 struct orangefs_inode_s *orangefs_inode =
624 ORANGEFS_I(file->f_path.dentry->d_inode);
625 struct orangefs_kernel_op_s *new_op = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400626
627 /* required call */
628 filemap_write_and_wait_range(file->f_mapping, start, end);
629
Yi Liu8bb8aef2015-11-24 15:12:14 -0500630 new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC);
Mike Marshall5db11c22015-07-17 10:38:12 -0400631 if (!new_op)
632 return -ENOMEM;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500633 new_op->upcall.req.fsync.refn = orangefs_inode->refn;
Mike Marshall5db11c22015-07-17 10:38:12 -0400634
635 ret = service_operation(new_op,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500636 "orangefs_fsync",
Mike Marshall5db11c22015-07-17 10:38:12 -0400637 get_interruptible_flag(file->f_path.dentry->d_inode));
638
639 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500640 "orangefs_fsync got return value of %d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400641 ret);
642
643 op_release(new_op);
644
Yi Liu8bb8aef2015-11-24 15:12:14 -0500645 orangefs_flush_inode(file->f_path.dentry->d_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400646 return ret;
647}
648
649/*
650 * Change the file pointer position for an instance of an open file.
651 *
652 * \note If .llseek is overriden, we must acquire lock as described in
653 * Documentation/filesystems/Locking.
654 *
655 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
656 * require much changes to the FS
657 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500658static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin)
Mike Marshall5db11c22015-07-17 10:38:12 -0400659{
660 int ret = -EINVAL;
661 struct inode *inode = file->f_path.dentry->d_inode;
662
663 if (!inode) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500664 gossip_err("orangefs_file_llseek: invalid inode (NULL)\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400665 return ret;
666 }
667
Yi Liu8bb8aef2015-11-24 15:12:14 -0500668 if (origin == ORANGEFS_SEEK_END) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400669 /*
670 * revalidate the inode's file size.
671 * NOTE: We are only interested in file size here,
672 * so we set mask accordingly.
673 */
Martin Brandenburge2f7f0d2016-03-15 12:33:20 -0400674 ret = orangefs_inode_getattr(file->f_mapping->host, 0, 1);
675 if (ret == -ESTALE)
676 ret = -EIO;
Mike Marshall5db11c22015-07-17 10:38:12 -0400677 if (ret) {
678 gossip_debug(GOSSIP_FILE_DEBUG,
679 "%s:%s:%d calling make bad inode\n",
680 __FILE__,
681 __func__,
682 __LINE__);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500683 orangefs_make_bad_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400684 return ret;
685 }
686 }
687
688 gossip_debug(GOSSIP_FILE_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500689 "orangefs_file_llseek: offset is %ld | origin is %d"
Mike Marshall54804942015-10-05 13:44:24 -0400690 " | inode size is %lu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400691 (long)offset,
692 origin,
693 (unsigned long)file->f_path.dentry->d_inode->i_size);
694
695 return generic_file_llseek(file, offset, origin);
696}
697
698/*
699 * Support local locks (locks that only this kernel knows about)
700 * if Orangefs was mounted -o local_lock.
701 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500702static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl)
Mike Marshall5db11c22015-07-17 10:38:12 -0400703{
Mike Marshallf957ae22015-09-24 12:53:05 -0400704 int rc = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400705
Yi Liu8bb8aef2015-11-24 15:12:14 -0500706 if (ORANGEFS_SB(filp->f_inode->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400707 if (cmd == F_GETLK) {
708 rc = 0;
709 posix_test_lock(filp, fl);
710 } else {
711 rc = posix_lock_file(filp, fl, NULL);
712 }
713 }
714
715 return rc;
716}
717
Yi Liu8bb8aef2015-11-24 15:12:14 -0500718/** ORANGEFS implementation of VFS file operations */
719const struct file_operations orangefs_file_operations = {
720 .llseek = orangefs_file_llseek,
721 .read_iter = orangefs_file_read_iter,
722 .write_iter = orangefs_file_write_iter,
723 .lock = orangefs_lock,
724 .unlocked_ioctl = orangefs_ioctl,
725 .mmap = orangefs_file_mmap,
Mike Marshall5db11c22015-07-17 10:38:12 -0400726 .open = generic_file_open,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500727 .release = orangefs_file_release,
728 .fsync = orangefs_fsync,
Mike Marshall5db11c22015-07-17 10:38:12 -0400729};