orangefs: avoid fsync service operation on flush
[linux-block.git] / fs / orangefs / file.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5db11c21
MM
2/*
3 * (C) 2001 Clemson University and The University of Chicago
85ac799c 4 * Copyright 2018 Omnibond Systems, L.L.C.
5db11c21
MM
5 *
6 * See COPYING in top-level directory.
7 */
8
9/*
10 * Linux VFS file operations.
11 */
12
13#include "protocol.h"
575e9461
MM
14#include "orangefs-kernel.h"
15#include "orangefs-bufmap.h"
5db11c21
MM
16#include <linux/fs.h>
17#include <linux/pagemap.h>
18
ed1e1587
MB
19static int flush_racache(struct inode *inode)
20{
21 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
22 struct orangefs_kernel_op_s *new_op;
23 int ret;
24
25 gossip_debug(GOSSIP_UTILS_DEBUG,
26 "%s: %pU: Handle is %pU | fs_id %d\n", __func__,
27 get_khandle_from_ino(inode), &orangefs_inode->refn.khandle,
28 orangefs_inode->refn.fs_id);
29
30 new_op = op_alloc(ORANGEFS_VFS_OP_RA_FLUSH);
31 if (!new_op)
32 return -ENOMEM;
33 new_op->upcall.req.ra_cache_flush.refn = orangefs_inode->refn;
34
35 ret = service_operation(new_op, "orangefs_flush_racache",
36 get_interruptible_flag(inode));
37
38 gossip_debug(GOSSIP_UTILS_DEBUG, "%s: got return value of %d\n",
39 __func__, ret);
40
41 op_release(new_op);
42 return ret;
43}
44
5db11c21
MM
45/*
46 * Post and wait for the I/O upcall to finish
47 */
c453dcfc 48ssize_t wait_for_direct_io(enum ORANGEFS_io_type type, struct inode *inode,
3c2fcfcb 49 loff_t *offset, struct iov_iter *iter,
4d1c4404 50 size_t total_size, loff_t readahead_size)
5db11c21 51{
8bb8aefd
YL
52 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
53 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
8bb8aefd 54 struct orangefs_kernel_op_s *new_op = NULL;
5db11c21
MM
55 int buffer_index = -1;
56 ssize_t ret;
57
8bb8aefd 58 new_op = op_alloc(ORANGEFS_VFS_OP_FILE_IO);
ed42fe05
AV
59 if (!new_op)
60 return -ENOMEM;
61
5db11c21 62 /* synchronous I/O */
5db11c21
MM
63 new_op->upcall.req.io.readahead_size = readahead_size;
64 new_op->upcall.req.io.io_type = type;
8bb8aefd 65 new_op->upcall.req.io.refn = orangefs_inode->refn;
5db11c21
MM
66
67populate_shared_memory:
68 /* get a shared buffer index */
b8a99a8f
AV
69 buffer_index = orangefs_bufmap_get();
70 if (buffer_index < 0) {
71 ret = buffer_index;
5db11c21 72 gossip_debug(GOSSIP_FILE_DEBUG,
b8a99a8f
AV
73 "%s: orangefs_bufmap_get failure (%zd)\n",
74 __func__, ret);
5db11c21
MM
75 goto out;
76 }
77 gossip_debug(GOSSIP_FILE_DEBUG,
78 "%s(%pU): GET op %p -> buffer_index %d\n",
79 __func__,
80 handle,
81 new_op,
82 buffer_index);
83
84 new_op->uses_shared_memory = 1;
85 new_op->upcall.req.io.buf_index = buffer_index;
86 new_op->upcall.req.io.count = total_size;
87 new_op->upcall.req.io.offset = *offset;
88
89 gossip_debug(GOSSIP_FILE_DEBUG,
3c2fcfcb 90 "%s(%pU): offset: %llu total_size: %zd\n",
5db11c21
MM
91 __func__,
92 handle,
5db11c21
MM
93 llu(*offset),
94 total_size);
95 /*
96 * Stage 1: copy the buffers into client-core's address space
5db11c21 97 */
dbcb5e7f
MB
98 if (type == ORANGEFS_IO_WRITE && total_size) {
99 ret = orangefs_bufmap_copy_from_iovec(iter, buffer_index,
100 total_size);
101 if (ret < 0) {
102 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
103 __func__, (long)ret);
5db11c21 104 goto out;
dbcb5e7f 105 }
5db11c21
MM
106 }
107
108 gossip_debug(GOSSIP_FILE_DEBUG,
109 "%s(%pU): Calling post_io_request with tag (%llu)\n",
110 __func__,
111 handle,
112 llu(new_op->tag));
113
114 /* Stage 2: Service the I/O operation */
115 ret = service_operation(new_op,
8bb8aefd 116 type == ORANGEFS_IO_WRITE ?
5db11c21
MM
117 "file_write" :
118 "file_read",
119 get_interruptible_flag(inode));
120
121 /*
122 * If service_operation() returns -EAGAIN #and# the operation was
8bb8aefd 123 * purged from orangefs_request_list or htable_ops_in_progress, then
5db11c21
MM
124 * we know that the client was restarted, causing the shared memory
125 * area to be wiped clean. To restart a write operation in this
126 * case, we must re-copy the data from the user's iovec to a NEW
127 * shared memory location. To restart a read operation, we must get
128 * a new shared memory location.
129 */
130 if (ret == -EAGAIN && op_state_purged(new_op)) {
1357d06d 131 orangefs_bufmap_put(buffer_index);
e17be9fd 132 buffer_index = -1;
7b9761af 133 if (type == ORANGEFS_IO_WRITE)
c63ed807 134 iov_iter_revert(iter, total_size);
5db11c21
MM
135 gossip_debug(GOSSIP_FILE_DEBUG,
136 "%s:going to repopulate_shared_memory.\n",
137 __func__);
138 goto populate_shared_memory;
139 }
140
141 if (ret < 0) {
162ada77
MM
142 if (ret == -EINTR) {
143 /*
144 * We can't return EINTR if any data was written,
145 * it's not POSIX. It is minimally acceptable
146 * to give a partial write, the way NFS does.
147 *
148 * It would be optimal to return all or nothing,
149 * but if a userspace write is bigger than
150 * an IO buffer, and the interrupt occurs
151 * between buffer writes, that would not be
152 * possible.
153 */
154 switch (new_op->op_state - OP_VFS_STATE_GIVEN_UP) {
155 /*
156 * If the op was waiting when the interrupt
157 * occurred, then the client-core did not
158 * trigger the write.
159 */
160 case OP_VFS_STATE_WAITING:
161 if (*offset == 0)
162 ret = -EINTR;
163 else
164 ret = 0;
165 break;
95f5f88f 166 /*
162ada77
MM
167 * If the op was in progress when the interrupt
168 * occurred, then the client-core was able to
169 * trigger the write.
170 */
171 case OP_VFS_STATE_INPROGR:
43f34576
MB
172 if (type == ORANGEFS_IO_READ)
173 ret = -EINTR;
174 else
175 ret = total_size;
162ada77
MM
176 break;
177 default:
178 gossip_err("%s: unexpected op state :%d:.\n",
179 __func__,
180 new_op->op_state);
181 ret = 0;
182 break;
183 }
5db11c21 184 gossip_debug(GOSSIP_FILE_DEBUG,
162ada77
MM
185 "%s: got EINTR, state:%d: %p\n",
186 __func__,
187 new_op->op_state,
188 new_op);
189 } else {
5db11c21
MM
190 gossip_err("%s: error in %s handle %pU, returning %zd\n",
191 __func__,
8bb8aefd 192 type == ORANGEFS_IO_READ ?
5db11c21
MM
193 "read from" : "write to",
194 handle, ret);
162ada77 195 }
78699e29
AV
196 if (orangefs_cancel_op_in_progress(new_op))
197 return ret;
198
897c5df6 199 goto out;
5db11c21
MM
200 }
201
202 /*
203 * Stage 3: Post copy buffers from client-core's address space
5db11c21 204 */
dbcb5e7f
MB
205 if (type == ORANGEFS_IO_READ && new_op->downcall.resp.io.amt_complete) {
206 /*
207 * NOTE: the iovector can either contain addresses which
208 * can futher be kernel-space or user-space addresses.
209 * or it can pointers to struct page's
210 */
211 ret = orangefs_bufmap_copy_to_iovec(iter, buffer_index,
212 new_op->downcall.resp.io.amt_complete);
213 if (ret < 0) {
214 gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
215 __func__, (long)ret);
897c5df6 216 goto out;
dbcb5e7f 217 }
5db11c21
MM
218 }
219 gossip_debug(GOSSIP_FILE_DEBUG,
9d9e7ba9 220 "%s(%pU): Amount %s, returned by the sys-io call:%d\n",
5db11c21
MM
221 __func__,
222 handle,
9d9e7ba9 223 type == ORANGEFS_IO_READ ? "read" : "written",
5db11c21
MM
224 (int)new_op->downcall.resp.io.amt_complete);
225
226 ret = new_op->downcall.resp.io.amt_complete;
227
5db11c21
MM
228out:
229 if (buffer_index >= 0) {
1357d06d 230 orangefs_bufmap_put(buffer_index);
5db11c21
MM
231 gossip_debug(GOSSIP_FILE_DEBUG,
232 "%s(%pU): PUT buffer_index %d\n",
233 __func__, handle, buffer_index);
234 buffer_index = -1;
235 }
ed42fe05 236 op_release(new_op);
5db11c21
MM
237 return ret;
238}
239
c453dcfc
MB
240static ssize_t orangefs_file_read_iter(struct kiocb *iocb,
241 struct iov_iter *iter)
5db11c21 242{
889d5f1b 243 orangefs_stats.reads++;
c453dcfc 244 return generic_file_read_iter(iocb, iter);
5db11c21
MM
245}
246
85ac799c
MB
247static ssize_t orangefs_file_write_iter(struct kiocb *iocb,
248 struct iov_iter *iter)
5db11c21 249{
889d5f1b 250 orangefs_stats.writes++;
85ac799c 251 return generic_file_write_iter(iocb, iter);
5db11c21
MM
252}
253
254/*
255 * Perform a miscellaneous operation on a file.
256 */
8bb8aefd 257static long orangefs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5db11c21
MM
258{
259 int ret = -ENOTTY;
260 __u64 val = 0;
261 unsigned long uval;
262
263 gossip_debug(GOSSIP_FILE_DEBUG,
8bb8aefd 264 "orangefs_ioctl: called with cmd %d\n",
5db11c21
MM
265 cmd);
266
267 /*
268 * we understand some general ioctls on files, such as the immutable
269 * and append flags
270 */
271 if (cmd == FS_IOC_GETFLAGS) {
272 val = 0;
8bb8aefd 273 ret = orangefs_inode_getxattr(file_inode(file),
8bb8aefd
YL
274 "user.pvfs2.meta_hint",
275 &val, sizeof(val));
5db11c21
MM
276 if (ret < 0 && ret != -ENODATA)
277 return ret;
278 else if (ret == -ENODATA)
279 val = 0;
280 uval = val;
281 gossip_debug(GOSSIP_FILE_DEBUG,
8bb8aefd 282 "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
5db11c21
MM
283 (unsigned long long)uval);
284 return put_user(uval, (int __user *)arg);
285 } else if (cmd == FS_IOC_SETFLAGS) {
286 ret = 0;
287 if (get_user(uval, (int __user *)arg))
288 return -EFAULT;
289 /*
8bb8aefd 290 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
5db11c21
MM
291 * is turned on for a file. The user is not allowed to turn
292 * on this bit, but the bit is present if the user first gets
293 * the flags and then updates the flags with some new
294 * settings. So, we ignore it in the following edit. bligon.
295 */
8bb8aefd 296 if ((uval & ~ORANGEFS_MIRROR_FL) &
5db11c21 297 (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
8bb8aefd 298 gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
5db11c21
MM
299 return -EINVAL;
300 }
301 val = uval;
302 gossip_debug(GOSSIP_FILE_DEBUG,
8bb8aefd 303 "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
5db11c21 304 (unsigned long long)val);
8bb8aefd 305 ret = orangefs_inode_setxattr(file_inode(file),
8bb8aefd
YL
306 "user.pvfs2.meta_hint",
307 &val, sizeof(val), 0);
5db11c21
MM
308 }
309
310 return ret;
311}
312
8bf782f6 313static vm_fault_t orangefs_fault(struct vm_fault *vmf)
a5135eea
MB
314{
315 struct file *file = vmf->vma->vm_file;
8bf782f6 316 int ret;
8b60785c
MB
317 ret = orangefs_inode_getattr(file->f_mapping->host,
318 ORANGEFS_GETATTR_SIZE);
8bf782f6
SJ
319 if (ret == -ESTALE)
320 ret = -EIO;
321 if (ret) {
8b60785c
MB
322 gossip_err("%s: orangefs_inode_getattr failed, "
323 "ret:%d:.\n", __func__, ret);
8bf782f6 324 return VM_FAULT_SIGBUS;
a5135eea
MB
325 }
326 return filemap_fault(vmf);
327}
328
ec62e95a 329static const struct vm_operations_struct orangefs_file_vm_ops = {
a5135eea
MB
330 .fault = orangefs_fault,
331 .map_pages = filemap_map_pages,
332 .page_mkwrite = filemap_page_mkwrite,
333};
334
5db11c21
MM
335/*
336 * Memory map a region of a file.
337 */
8bb8aefd 338static int orangefs_file_mmap(struct file *file, struct vm_area_struct *vma)
5db11c21
MM
339{
340 gossip_debug(GOSSIP_FILE_DEBUG,
8bb8aefd 341 "orangefs_file_mmap: called on %s\n",
5db11c21
MM
342 (file ?
343 (char *)file->f_path.dentry->d_name.name :
344 (char *)"Unknown"));
345
346 /* set the sequential readahead hint */
347 vma->vm_flags |= VM_SEQ_READ;
348 vma->vm_flags &= ~VM_RAND_READ;
35390803 349
a5135eea
MB
350 file_accessed(file);
351 vma->vm_ops = &orangefs_file_vm_ops;
352 return 0;
5db11c21
MM
353}
354
355#define mapping_nrpages(idata) ((idata)->nrpages)
356
357/*
358 * Called to notify the module that there are no more references to
359 * this file (i.e. no processes have it open).
360 *
361 * \note Not called when each file is closed.
362 */
8bb8aefd 363static int orangefs_file_release(struct inode *inode, struct file *file)
5db11c21
MM
364{
365 gossip_debug(GOSSIP_FILE_DEBUG,
f66debf1
AV
366 "orangefs_file_release: called on %pD\n",
367 file);
5db11c21 368
5db11c21 369 /*
6eaff8c7 370 * remove all associated inode pages from the page cache and
54804949
MM
371 * readahead cache (if any); this forces an expensive refresh of
372 * data for the next caller of mmap (or 'get_block' accesses)
5db11c21 373 */
d62a9025
AG
374 if (file_inode(file) &&
375 file_inode(file)->i_mapping &&
376 mapping_nrpages(&file_inode(file)->i_data)) {
c51e0129
MB
377 if (orangefs_features & ORANGEFS_FEATURE_READAHEAD) {
378 gossip_debug(GOSSIP_INODE_DEBUG,
379 "calling flush_racache on %pU\n",
380 get_khandle_from_ino(inode));
381 flush_racache(inode);
382 gossip_debug(GOSSIP_INODE_DEBUG,
383 "flush_racache finished\n");
384 }
ed1e1587 385 }
5db11c21
MM
386 return 0;
387}
388
389/*
390 * Push all data for a specific file onto permanent storage.
391 */
8bb8aefd 392static int orangefs_fsync(struct file *file,
84d02150
MM
393 loff_t start,
394 loff_t end,
395 int datasync)
5db11c21 396{
49e55713 397 int ret;
8bb8aefd 398 struct orangefs_inode_s *orangefs_inode =
d62a9025 399 ORANGEFS_I(file_inode(file));
8bb8aefd 400 struct orangefs_kernel_op_s *new_op = NULL;
5db11c21 401
85ac799c
MB
402 ret = filemap_write_and_wait_range(file_inode(file)->i_mapping,
403 start, end);
404 if (ret < 0)
405 return ret;
406
8bb8aefd 407 new_op = op_alloc(ORANGEFS_VFS_OP_FSYNC);
5db11c21
MM
408 if (!new_op)
409 return -ENOMEM;
8bb8aefd 410 new_op->upcall.req.fsync.refn = orangefs_inode->refn;
5db11c21
MM
411
412 ret = service_operation(new_op,
8bb8aefd 413 "orangefs_fsync",
d62a9025 414 get_interruptible_flag(file_inode(file)));
5db11c21
MM
415
416 gossip_debug(GOSSIP_FILE_DEBUG,
8bb8aefd 417 "orangefs_fsync got return value of %d\n",
5db11c21
MM
418 ret);
419
420 op_release(new_op);
5db11c21
MM
421 return ret;
422}
423
424/*
425 * Change the file pointer position for an instance of an open file.
426 *
427 * \note If .llseek is overriden, we must acquire lock as described in
428 * Documentation/filesystems/Locking.
429 *
430 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
431 * require much changes to the FS
432 */
8bb8aefd 433static loff_t orangefs_file_llseek(struct file *file, loff_t offset, int origin)
5db11c21
MM
434{
435 int ret = -EINVAL;
177f8fc4 436 struct inode *inode = file_inode(file);
5db11c21 437
177f8fc4 438 if (origin == SEEK_END) {
5db11c21
MM
439 /*
440 * revalidate the inode's file size.
441 * NOTE: We are only interested in file size here,
442 * so we set mask accordingly.
443 */
8b60785c
MB
444 ret = orangefs_inode_getattr(file->f_mapping->host,
445 ORANGEFS_GETATTR_SIZE);
e2f7f0d7
MB
446 if (ret == -ESTALE)
447 ret = -EIO;
5db11c21
MM
448 if (ret) {
449 gossip_debug(GOSSIP_FILE_DEBUG,
450 "%s:%s:%d calling make bad inode\n",
451 __FILE__,
452 __func__,
453 __LINE__);
5db11c21
MM
454 return ret;
455 }
456 }
457
458 gossip_debug(GOSSIP_FILE_DEBUG,
8bb8aefd 459 "orangefs_file_llseek: offset is %ld | origin is %d"
54804949 460 " | inode size is %lu\n",
5db11c21
MM
461 (long)offset,
462 origin,
177f8fc4 463 (unsigned long)i_size_read(inode));
5db11c21
MM
464
465 return generic_file_llseek(file, offset, origin);
466}
467
468/*
469 * Support local locks (locks that only this kernel knows about)
470 * if Orangefs was mounted -o local_lock.
471 */
8bb8aefd 472static int orangefs_lock(struct file *filp, int cmd, struct file_lock *fl)
5db11c21 473{
f957ae2d 474 int rc = -EINVAL;
5db11c21 475
45063097 476 if (ORANGEFS_SB(file_inode(filp)->i_sb)->flags & ORANGEFS_OPT_LOCAL_LOCK) {
5db11c21
MM
477 if (cmd == F_GETLK) {
478 rc = 0;
479 posix_test_lock(filp, fl);
480 } else {
481 rc = posix_lock_file(filp, fl, NULL);
482 }
483 }
484
485 return rc;
486}
487
85ac799c
MB
488static int orangefs_flush(struct file *file, fl_owner_t id)
489{
90fc0706
MB
490 /*
491 * This is vfs_fsync_range(file, 0, LLONG_MAX, 0) without the
492 * service_operation in orangefs_fsync.
493 *
494 * Do not send fsync to OrangeFS server on a close. Do send fsync
495 * on an explicit fsync call. This duplicates historical OrangeFS
496 * behavior.
497 */
498 struct inode *inode = file->f_mapping->host;
499 int r;
500
501 if (inode->i_state & I_DIRTY_TIME) {
502 spin_lock(&inode->i_lock);
503 inode->i_state &= ~I_DIRTY_TIME;
504 spin_unlock(&inode->i_lock);
505 mark_inode_dirty_sync(inode);
506 }
507
508 r = filemap_write_and_wait_range(file->f_mapping, 0, LLONG_MAX);
509 if (r > 0)
510 return 0;
511 else
512 return r;
85ac799c
MB
513}
514
8bb8aefd
YL
515/** ORANGEFS implementation of VFS file operations */
516const struct file_operations orangefs_file_operations = {
517 .llseek = orangefs_file_llseek,
518 .read_iter = orangefs_file_read_iter,
519 .write_iter = orangefs_file_write_iter,
520 .lock = orangefs_lock,
521 .unlocked_ioctl = orangefs_ioctl,
522 .mmap = orangefs_file_mmap,
5db11c21 523 .open = generic_file_open,
85ac799c 524 .flush = orangefs_flush,
8bb8aefd
YL
525 .release = orangefs_file_release,
526 .fsync = orangefs_fsync,
5db11c21 527};