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