blob: ff7fe37f5a220eca29b757d7e9d4fbb2d5c76870 [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"
12#include "pvfs2-kernel.h"
13#include "pvfs2-bufmap.h"
14#include <linux/fs.h>
15#include <linux/pagemap.h>
16
17#define wake_up_daemon_for_return(op) \
18do { \
19 spin_lock(&op->lock); \
20 op->io_completed = 1; \
21 spin_unlock(&op->lock); \
22 wake_up_interruptible(&op->io_completion_waitq);\
23} while (0)
24
25/*
26 * Copy to client-core's address space from the buffers specified
27 * by the iovec upto total_size bytes.
28 * NOTE: the iovector can either contain addresses which
29 * can futher be kernel-space or user-space addresses.
30 * or it can pointers to struct page's
31 */
32static int precopy_buffers(struct pvfs2_bufmap *bufmap,
33 int buffer_index,
Al Viroa5c126a2015-10-08 17:54:31 -040034 struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040035 size_t total_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040036{
37 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -040038 /*
39 * copy data from application/kernel by pulling it out
40 * of the iovec.
41 */
Mike Marshall4d1c4402015-09-04 10:31:16 -040042
43
44 if (total_size) {
Mike Marshall4d1c4402015-09-04 10:31:16 -040045 ret = pvfs_bufmap_copy_from_iovec(bufmap,
Al Viroa5c126a2015-10-08 17:54:31 -040046 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040047 buffer_index,
48 total_size);
49 if (ret < 0)
50 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
51 __func__,
52 (long)ret);
Mike Marshall4d1c4402015-09-04 10:31:16 -040053 }
54
Mike Marshall5db11c22015-07-17 10:38:12 -040055 if (ret < 0)
56 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
57 __func__,
58 (long)ret);
59 return ret;
60}
61
62/*
63 * Copy from client-core's address space to the buffers specified
64 * by the iovec upto total_size bytes.
65 * NOTE: the iovector can either contain addresses which
66 * can futher be kernel-space or user-space addresses.
67 * or it can pointers to struct page's
68 */
69static int postcopy_buffers(struct pvfs2_bufmap *bufmap,
70 int buffer_index,
Al Viro5f0e3c92015-10-08 17:52:44 -040071 struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040072 size_t total_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040073{
74 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -040075 /*
76 * copy data to application/kernel by pushing it out to
77 * the iovec. NOTE; target buffers can be addresses or
78 * struct page pointers.
79 */
80 if (total_size) {
Mike Marshall4d1c4402015-09-04 10:31:16 -040081 ret = pvfs_bufmap_copy_to_iovec(bufmap,
Al Viro5f0e3c92015-10-08 17:52:44 -040082 iter,
Al Viro5c278222015-10-08 17:43:58 -040083 buffer_index,
84 total_size);
Mike Marshall5db11c22015-07-17 10:38:12 -040085 if (ret < 0)
Mike Marshall4d1c4402015-09-04 10:31:16 -040086 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 -040087 __func__,
88 (long)ret);
89 }
90 return ret;
91}
92
93/*
94 * Post and wait for the I/O upcall to finish
95 */
96static ssize_t wait_for_direct_io(enum PVFS_io_type type, struct inode *inode,
Al Viro3c2fcfc2015-10-08 18:00:26 -040097 loff_t *offset, struct iov_iter *iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -040098 size_t total_size, loff_t readahead_size)
Mike Marshall5db11c22015-07-17 10:38:12 -040099{
100 struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
101 struct pvfs2_khandle *handle = &pvfs2_inode->refn.khandle;
102 struct pvfs2_bufmap *bufmap = NULL;
103 struct pvfs2_kernel_op_s *new_op = NULL;
104 int buffer_index = -1;
105 ssize_t ret;
106
107 new_op = op_alloc(PVFS2_VFS_OP_FILE_IO);
108 if (!new_op) {
109 ret = -ENOMEM;
110 goto out;
111 }
112 /* synchronous I/O */
113 new_op->upcall.req.io.async_vfs_io = PVFS_VFS_SYNC_IO;
114 new_op->upcall.req.io.readahead_size = readahead_size;
115 new_op->upcall.req.io.io_type = type;
116 new_op->upcall.req.io.refn = pvfs2_inode->refn;
117
118populate_shared_memory:
119 /* get a shared buffer index */
120 ret = pvfs_bufmap_get(&bufmap, &buffer_index);
121 if (ret < 0) {
122 gossip_debug(GOSSIP_FILE_DEBUG,
123 "%s: pvfs_bufmap_get failure (%ld)\n",
124 __func__, (long)ret);
125 goto out;
126 }
127 gossip_debug(GOSSIP_FILE_DEBUG,
128 "%s(%pU): GET op %p -> buffer_index %d\n",
129 __func__,
130 handle,
131 new_op,
132 buffer_index);
133
134 new_op->uses_shared_memory = 1;
135 new_op->upcall.req.io.buf_index = buffer_index;
136 new_op->upcall.req.io.count = total_size;
137 new_op->upcall.req.io.offset = *offset;
138
139 gossip_debug(GOSSIP_FILE_DEBUG,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400140 "%s(%pU): offset: %llu total_size: %zd\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400141 __func__,
142 handle,
Mike Marshall5db11c22015-07-17 10:38:12 -0400143 llu(*offset),
144 total_size);
145 /*
146 * Stage 1: copy the buffers into client-core's address space
147 * precopy_buffers only pertains to writes.
148 */
149 if (type == PVFS_IO_WRITE) {
150 ret = precopy_buffers(bufmap,
151 buffer_index,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400152 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400153 total_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400154 if (ret < 0)
155 goto out;
156 }
157
158 gossip_debug(GOSSIP_FILE_DEBUG,
159 "%s(%pU): Calling post_io_request with tag (%llu)\n",
160 __func__,
161 handle,
162 llu(new_op->tag));
163
164 /* Stage 2: Service the I/O operation */
165 ret = service_operation(new_op,
166 type == PVFS_IO_WRITE ?
167 "file_write" :
168 "file_read",
169 get_interruptible_flag(inode));
170
171 /*
172 * If service_operation() returns -EAGAIN #and# the operation was
173 * purged from pvfs2_request_list or htable_ops_in_progress, then
174 * we know that the client was restarted, causing the shared memory
175 * area to be wiped clean. To restart a write operation in this
176 * case, we must re-copy the data from the user's iovec to a NEW
177 * shared memory location. To restart a read operation, we must get
178 * a new shared memory location.
179 */
180 if (ret == -EAGAIN && op_state_purged(new_op)) {
181 pvfs_bufmap_put(bufmap, buffer_index);
182 gossip_debug(GOSSIP_FILE_DEBUG,
183 "%s:going to repopulate_shared_memory.\n",
184 __func__);
185 goto populate_shared_memory;
186 }
187
188 if (ret < 0) {
189 handle_io_error(); /* defined in pvfs2-kernel.h */
190 /*
Mike Marshall54804942015-10-05 13:44:24 -0400191 * don't write an error to syslog on signaled operation
192 * termination unless we've got debugging turned on, as
193 * this can happen regularly (i.e. ctrl-c)
Mike Marshall5db11c22015-07-17 10:38:12 -0400194 */
195 if (ret == -EINTR)
196 gossip_debug(GOSSIP_FILE_DEBUG,
197 "%s: returning error %ld\n", __func__,
198 (long)ret);
199 else
200 gossip_err("%s: error in %s handle %pU, returning %zd\n",
201 __func__,
202 type == PVFS_IO_READ ?
203 "read from" : "write to",
204 handle, ret);
205 goto out;
206 }
207
208 /*
209 * Stage 3: Post copy buffers from client-core's address space
210 * postcopy_buffers only pertains to reads.
211 */
212 if (type == PVFS_IO_READ) {
213 ret = postcopy_buffers(bufmap,
214 buffer_index,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400215 iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400216 new_op->downcall.resp.io.amt_complete);
Mike Marshall5db11c22015-07-17 10:38:12 -0400217 if (ret < 0) {
218 /*
219 * put error codes in downcall so that handle_io_error()
220 * preserves it properly
221 */
222 new_op->downcall.status = ret;
223 handle_io_error();
224 goto out;
225 }
226 }
227 gossip_debug(GOSSIP_FILE_DEBUG,
228 "%s(%pU): Amount written as returned by the sys-io call:%d\n",
229 __func__,
230 handle,
231 (int)new_op->downcall.resp.io.amt_complete);
232
233 ret = new_op->downcall.resp.io.amt_complete;
234
235 /*
Mike Marshall54804942015-10-05 13:44:24 -0400236 * tell the device file owner waiting on I/O that this read has
237 * completed and it can return now. in this exact case, on
238 * wakeup the daemon will free the op, so we *cannot* touch it
239 * after this.
Mike Marshall5db11c22015-07-17 10:38:12 -0400240 */
241 wake_up_daemon_for_return(new_op);
242 new_op = NULL;
243
244out:
245 if (buffer_index >= 0) {
246 pvfs_bufmap_put(bufmap, buffer_index);
247 gossip_debug(GOSSIP_FILE_DEBUG,
248 "%s(%pU): PUT buffer_index %d\n",
249 __func__, handle, buffer_index);
250 buffer_index = -1;
251 }
252 if (new_op) {
253 op_release(new_op);
254 new_op = NULL;
255 }
256 return ret;
257}
258
259/*
Mike Marshall5db11c22015-07-17 10:38:12 -0400260 * Common entry point for read/write/readv/writev
261 * This function will dispatch it to either the direct I/O
262 * or buffered I/O path depending on the mount options and/or
263 * augmented/extended metadata attached to the file.
264 * Note: File extended attributes override any mount options.
265 */
266static ssize_t do_readv_writev(enum PVFS_io_type type, struct file *file,
267 loff_t *offset, const struct iovec *iov, unsigned long nr_segs)
268{
269 struct inode *inode = file->f_mapping->host;
270 struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
271 struct pvfs2_khandle *handle = &pvfs2_inode->refn.khandle;
Al Virodc4067f2015-10-08 18:17:26 -0400272 struct iov_iter iter;
273 size_t count = iov_length(iov, nr_segs);
274 ssize_t total_count = 0;
275 ssize_t ret = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400276
277 gossip_debug(GOSSIP_FILE_DEBUG,
278 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
279 __func__,
280 handle,
281 (int)count);
282
283 if (type == PVFS_IO_WRITE) {
284 gossip_debug(GOSSIP_FILE_DEBUG,
285 "%s(%pU): proceeding with offset : %llu, "
286 "size %d\n",
287 __func__,
288 handle,
289 llu(*offset),
290 (int)count);
291 }
292
293 if (count == 0) {
294 ret = 0;
295 goto out;
296 }
297
Al Virodc4067f2015-10-08 18:17:26 -0400298 iov_iter_init(&iter, type == PVFS_IO_READ ? READ : WRITE,
299 iov, nr_segs, count);
Mike Marshall5db11c22015-07-17 10:38:12 -0400300
Mike Marshall5db11c22015-07-17 10:38:12 -0400301 while (total_count < count) {
302 size_t each_count;
303 size_t amt_complete;
304
305 /* how much to transfer in this loop iteration */
306 each_count =
307 (((count - total_count) > pvfs_bufmap_size_query()) ?
308 pvfs_bufmap_size_query() :
309 (count - total_count));
310
311 gossip_debug(GOSSIP_FILE_DEBUG,
312 "%s(%pU): size of each_count(%d)\n",
313 __func__,
314 handle,
315 (int)each_count);
316 gossip_debug(GOSSIP_FILE_DEBUG,
317 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
318 __func__,
319 handle,
320 (int)*offset);
321
Al Viro3c2fcfc2015-10-08 18:00:26 -0400322 ret = wait_for_direct_io(type, inode, offset, &iter,
323 each_count, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400324 gossip_debug(GOSSIP_FILE_DEBUG,
325 "%s(%pU): return from wait_for_io:%d\n",
326 __func__,
327 handle,
328 (int)ret);
329
330 if (ret < 0)
331 goto out;
332
Mike Marshall5db11c22015-07-17 10:38:12 -0400333 *offset += ret;
334 total_count += ret;
335 amt_complete = ret;
336
337 gossip_debug(GOSSIP_FILE_DEBUG,
338 "%s(%pU): AFTER wait_for_io: offset is %d\n",
339 __func__,
340 handle,
341 (int)*offset);
342
343 /*
344 * if we got a short I/O operations,
345 * fall out and return what we got so far
346 */
347 if (amt_complete < each_count)
348 break;
349 } /*end while */
350
351 if (total_count > 0)
352 ret = total_count;
353out:
Mike Marshall5db11c22015-07-17 10:38:12 -0400354 if (ret > 0) {
355 if (type == PVFS_IO_READ) {
356 file_accessed(file);
357 } else {
358 SetMtimeFlag(pvfs2_inode);
359 inode->i_mtime = CURRENT_TIME;
360 mark_inode_dirty_sync(inode);
361 }
362 }
363
364 gossip_debug(GOSSIP_FILE_DEBUG,
365 "%s(%pU): Value(%d) returned.\n",
366 __func__,
367 handle,
368 (int)ret);
369
370 return ret;
371}
372
373/*
374 * Read data from a specified offset in a file (referenced by inode).
375 * Data may be placed either in a user or kernel buffer.
376 */
377ssize_t pvfs2_inode_read(struct inode *inode,
378 char __user *buf,
379 size_t count,
380 loff_t *offset,
381 loff_t readahead_size)
382{
383 struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
384 size_t bufmap_size;
385 struct iovec vec;
Al Viro3c2fcfc2015-10-08 18:00:26 -0400386 struct iov_iter iter;
Mike Marshall5db11c22015-07-17 10:38:12 -0400387 ssize_t ret = -EINVAL;
388
389 g_pvfs2_stats.reads++;
390
391 vec.iov_base = buf;
392 vec.iov_len = count;
393
394 bufmap_size = pvfs_bufmap_size_query();
395 if (count > bufmap_size) {
396 gossip_debug(GOSSIP_FILE_DEBUG,
397 "%s: count is too large (%zd/%zd)!\n",
398 __func__, count, bufmap_size);
399 return -EINVAL;
400 }
401
402 gossip_debug(GOSSIP_FILE_DEBUG,
403 "%s(%pU) %zd@%llu\n",
404 __func__,
405 &pvfs2_inode->refn.khandle,
406 count,
407 llu(*offset));
408
Al Viro3c2fcfc2015-10-08 18:00:26 -0400409 iov_iter_init(&iter, READ, &vec, 1, count);
410 ret = wait_for_direct_io(PVFS_IO_READ, inode, offset, &iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400411 count, readahead_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400412 if (ret > 0)
413 *offset += ret;
414
415 gossip_debug(GOSSIP_FILE_DEBUG,
416 "%s(%pU): Value(%zd) returned.\n",
417 __func__,
418 &pvfs2_inode->refn.khandle,
419 ret);
420
421 return ret;
422}
423
424static ssize_t pvfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
425{
426 struct file *file = iocb->ki_filp;
427 loff_t pos = *(&iocb->ki_pos);
428 ssize_t rc = 0;
429 unsigned long nr_segs = iter->nr_segs;
430
431 BUG_ON(iocb->private);
432
433 gossip_debug(GOSSIP_FILE_DEBUG, "pvfs2_file_read_iter\n");
434
435 g_pvfs2_stats.reads++;
436
437 rc = do_readv_writev(PVFS_IO_READ,
438 file,
439 &pos,
440 iter->iov,
441 nr_segs);
442 iocb->ki_pos = pos;
443
444 return rc;
445}
446
447static ssize_t pvfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
448{
449 struct file *file = iocb->ki_filp;
450 loff_t pos = *(&iocb->ki_pos);
451 unsigned long nr_segs = iter->nr_segs;
452 ssize_t rc;
453
454 BUG_ON(iocb->private);
455
456 gossip_debug(GOSSIP_FILE_DEBUG, "pvfs2_file_write_iter\n");
457
458 mutex_lock(&file->f_mapping->host->i_mutex);
459
460 /* Make sure generic_write_checks sees an up to date inode size. */
461 if (file->f_flags & O_APPEND) {
462 rc = pvfs2_inode_getattr(file->f_mapping->host,
463 PVFS_ATTR_SYS_SIZE);
464 if (rc) {
465 gossip_err("%s: pvfs2_inode_getattr failed, rc:%zd:.\n",
466 __func__, rc);
467 goto out;
468 }
469 }
470
471 if (file->f_pos > i_size_read(file->f_mapping->host))
472 pvfs2_i_size_write(file->f_mapping->host, file->f_pos);
473
474 rc = generic_write_checks(iocb, iter);
475
476 if (rc <= 0) {
477 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
478 __func__, rc);
479 goto out;
480 }
481
482 rc = do_readv_writev(PVFS_IO_WRITE,
483 file,
484 &pos,
485 iter->iov,
486 nr_segs);
487 if (rc < 0) {
488 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
489 __func__, rc);
490 goto out;
491 }
492
493 iocb->ki_pos = pos;
494 g_pvfs2_stats.writes++;
495
496out:
497
498 mutex_unlock(&file->f_mapping->host->i_mutex);
499 return rc;
500}
501
502/*
503 * Perform a miscellaneous operation on a file.
504 */
Mike Marshall84d02152015-07-28 13:27:51 -0400505static long pvfs2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Mike Marshall5db11c22015-07-17 10:38:12 -0400506{
507 int ret = -ENOTTY;
508 __u64 val = 0;
509 unsigned long uval;
510
511 gossip_debug(GOSSIP_FILE_DEBUG,
512 "pvfs2_ioctl: called with cmd %d\n",
513 cmd);
514
515 /*
516 * we understand some general ioctls on files, such as the immutable
517 * and append flags
518 */
519 if (cmd == FS_IOC_GETFLAGS) {
520 val = 0;
521 ret = pvfs2_xattr_get_default(file->f_path.dentry,
522 "user.pvfs2.meta_hint",
523 &val,
524 sizeof(val),
525 0);
526 if (ret < 0 && ret != -ENODATA)
527 return ret;
528 else if (ret == -ENODATA)
529 val = 0;
530 uval = val;
531 gossip_debug(GOSSIP_FILE_DEBUG,
532 "pvfs2_ioctl: FS_IOC_GETFLAGS: %llu\n",
533 (unsigned long long)uval);
534 return put_user(uval, (int __user *)arg);
535 } else if (cmd == FS_IOC_SETFLAGS) {
536 ret = 0;
537 if (get_user(uval, (int __user *)arg))
538 return -EFAULT;
539 /*
540 * PVFS_MIRROR_FL is set internally when the mirroring mode
541 * is turned on for a file. The user is not allowed to turn
542 * on this bit, but the bit is present if the user first gets
543 * the flags and then updates the flags with some new
544 * settings. So, we ignore it in the following edit. bligon.
545 */
546 if ((uval & ~PVFS_MIRROR_FL) &
547 (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
548 gossip_err("pvfs2_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
549 return -EINVAL;
550 }
551 val = uval;
552 gossip_debug(GOSSIP_FILE_DEBUG,
553 "pvfs2_ioctl: FS_IOC_SETFLAGS: %llu\n",
554 (unsigned long long)val);
555 ret = pvfs2_xattr_set_default(file->f_path.dentry,
556 "user.pvfs2.meta_hint",
557 &val,
558 sizeof(val),
559 0,
560 0);
561 }
562
563 return ret;
564}
565
566/*
567 * Memory map a region of a file.
568 */
569static int pvfs2_file_mmap(struct file *file, struct vm_area_struct *vma)
570{
571 gossip_debug(GOSSIP_FILE_DEBUG,
572 "pvfs2_file_mmap: called on %s\n",
573 (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 */
Mike Marshall84d02152015-07-28 13:27:51 -0400593static int pvfs2_file_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400594{
595 gossip_debug(GOSSIP_FILE_DEBUG,
596 "pvfs2_file_release: called on %s\n",
597 file->f_path.dentry->d_name.name);
598
599 pvfs2_flush_inode(inode);
600
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 */
Mike Marshall84d02152015-07-28 13:27:51 -0400617static int pvfs2_fsync(struct file *file,
618 loff_t start,
619 loff_t end,
620 int datasync)
Mike Marshall5db11c22015-07-17 10:38:12 -0400621{
622 int ret = -EINVAL;
623 struct pvfs2_inode_s *pvfs2_inode =
624 PVFS2_I(file->f_path.dentry->d_inode);
625 struct pvfs2_kernel_op_s *new_op = NULL;
626
627 /* required call */
628 filemap_write_and_wait_range(file->f_mapping, start, end);
629
630 new_op = op_alloc(PVFS2_VFS_OP_FSYNC);
631 if (!new_op)
632 return -ENOMEM;
633 new_op->upcall.req.fsync.refn = pvfs2_inode->refn;
634
635 ret = service_operation(new_op,
636 "pvfs2_fsync",
637 get_interruptible_flag(file->f_path.dentry->d_inode));
638
639 gossip_debug(GOSSIP_FILE_DEBUG,
640 "pvfs2_fsync got return value of %d\n",
641 ret);
642
643 op_release(new_op);
644
645 pvfs2_flush_inode(file->f_path.dentry->d_inode);
646 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 */
Mike Marshall84d02152015-07-28 13:27:51 -0400658static loff_t pvfs2_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) {
664 gossip_err("pvfs2_file_llseek: invalid inode (NULL)\n");
665 return ret;
666 }
667
668 if (origin == PVFS2_SEEK_END) {
669 /*
670 * revalidate the inode's file size.
671 * NOTE: We are only interested in file size here,
672 * so we set mask accordingly.
673 */
674 ret = pvfs2_inode_getattr(inode, PVFS_ATTR_SYS_SIZE);
675 if (ret) {
676 gossip_debug(GOSSIP_FILE_DEBUG,
677 "%s:%s:%d calling make bad inode\n",
678 __FILE__,
679 __func__,
680 __LINE__);
681 pvfs2_make_bad_inode(inode);
682 return ret;
683 }
684 }
685
686 gossip_debug(GOSSIP_FILE_DEBUG,
Mike Marshall54804942015-10-05 13:44:24 -0400687 "pvfs2_file_llseek: offset is %ld | origin is %d"
688 " | inode size is %lu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400689 (long)offset,
690 origin,
691 (unsigned long)file->f_path.dentry->d_inode->i_size);
692
693 return generic_file_llseek(file, offset, origin);
694}
695
696/*
697 * Support local locks (locks that only this kernel knows about)
698 * if Orangefs was mounted -o local_lock.
699 */
Mike Marshall84d02152015-07-28 13:27:51 -0400700static int pvfs2_lock(struct file *filp, int cmd, struct file_lock *fl)
Mike Marshall5db11c22015-07-17 10:38:12 -0400701{
Mike Marshallf957ae22015-09-24 12:53:05 -0400702 int rc = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400703
704 if (PVFS2_SB(filp->f_inode->i_sb)->flags & PVFS2_OPT_LOCAL_LOCK) {
705 if (cmd == F_GETLK) {
706 rc = 0;
707 posix_test_lock(filp, fl);
708 } else {
709 rc = posix_lock_file(filp, fl, NULL);
710 }
711 }
712
713 return rc;
714}
715
716/** PVFS2 implementation of VFS file operations */
717const struct file_operations pvfs2_file_operations = {
718 .llseek = pvfs2_file_llseek,
719 .read_iter = pvfs2_file_read_iter,
720 .write_iter = pvfs2_file_write_iter,
721 .lock = pvfs2_lock,
722 .unlocked_ioctl = pvfs2_ioctl,
723 .mmap = pvfs2_file_mmap,
724 .open = generic_file_open,
725 .release = pvfs2_file_release,
726 .fsync = pvfs2_fsync,
727};