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