blob: 8dae04dc9df4c5d5ecd677001fcad95414ade149 [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,
Al Viro0071ed12015-10-08 18:22:08 -0400267 loff_t *offset, struct iov_iter *iter)
Mike Marshall5db11c22015-07-17 10:38:12 -0400268{
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 Viro0071ed12015-10-08 18:22:08 -0400272 size_t count = iov_iter_count(iter);
Al Virodc4067f2015-10-08 18:17:26 -0400273 ssize_t total_count = 0;
274 ssize_t ret = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400275
276 gossip_debug(GOSSIP_FILE_DEBUG,
277 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
278 __func__,
279 handle,
280 (int)count);
281
282 if (type == PVFS_IO_WRITE) {
283 gossip_debug(GOSSIP_FILE_DEBUG,
284 "%s(%pU): proceeding with offset : %llu, "
285 "size %d\n",
286 __func__,
287 handle,
288 llu(*offset),
289 (int)count);
290 }
291
292 if (count == 0) {
293 ret = 0;
294 goto out;
295 }
296
Al Viro0071ed12015-10-08 18:22:08 -0400297 while (iov_iter_count(iter)) {
298 size_t each_count = iov_iter_count(iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400299 size_t amt_complete;
300
301 /* how much to transfer in this loop iteration */
Al Viro0071ed12015-10-08 18:22:08 -0400302 if (each_count > pvfs_bufmap_size_query())
303 each_count = pvfs_bufmap_size_query();
Mike Marshall5db11c22015-07-17 10:38:12 -0400304
305 gossip_debug(GOSSIP_FILE_DEBUG,
306 "%s(%pU): size of each_count(%d)\n",
307 __func__,
308 handle,
309 (int)each_count);
310 gossip_debug(GOSSIP_FILE_DEBUG,
311 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
312 __func__,
313 handle,
314 (int)*offset);
315
Al Viro0071ed12015-10-08 18:22:08 -0400316 ret = wait_for_direct_io(type, inode, offset, iter,
Al Viro3c2fcfc2015-10-08 18:00:26 -0400317 each_count, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400318 gossip_debug(GOSSIP_FILE_DEBUG,
319 "%s(%pU): return from wait_for_io:%d\n",
320 __func__,
321 handle,
322 (int)ret);
323
324 if (ret < 0)
325 goto out;
326
Mike Marshall5db11c22015-07-17 10:38:12 -0400327 *offset += ret;
328 total_count += ret;
329 amt_complete = ret;
330
331 gossip_debug(GOSSIP_FILE_DEBUG,
332 "%s(%pU): AFTER wait_for_io: offset is %d\n",
333 __func__,
334 handle,
335 (int)*offset);
336
337 /*
338 * if we got a short I/O operations,
339 * fall out and return what we got so far
340 */
341 if (amt_complete < each_count)
342 break;
343 } /*end while */
344
345 if (total_count > 0)
346 ret = total_count;
347out:
Mike Marshall5db11c22015-07-17 10:38:12 -0400348 if (ret > 0) {
349 if (type == PVFS_IO_READ) {
350 file_accessed(file);
351 } else {
352 SetMtimeFlag(pvfs2_inode);
353 inode->i_mtime = CURRENT_TIME;
354 mark_inode_dirty_sync(inode);
355 }
356 }
357
358 gossip_debug(GOSSIP_FILE_DEBUG,
359 "%s(%pU): Value(%d) returned.\n",
360 __func__,
361 handle,
362 (int)ret);
363
364 return ret;
365}
366
367/*
368 * Read data from a specified offset in a file (referenced by inode).
369 * Data may be placed either in a user or kernel buffer.
370 */
371ssize_t pvfs2_inode_read(struct inode *inode,
372 char __user *buf,
373 size_t count,
374 loff_t *offset,
375 loff_t readahead_size)
376{
377 struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
378 size_t bufmap_size;
379 struct iovec vec;
Al Viro3c2fcfc2015-10-08 18:00:26 -0400380 struct iov_iter iter;
Mike Marshall5db11c22015-07-17 10:38:12 -0400381 ssize_t ret = -EINVAL;
382
383 g_pvfs2_stats.reads++;
384
385 vec.iov_base = buf;
386 vec.iov_len = count;
387
388 bufmap_size = pvfs_bufmap_size_query();
389 if (count > bufmap_size) {
390 gossip_debug(GOSSIP_FILE_DEBUG,
391 "%s: count is too large (%zd/%zd)!\n",
392 __func__, count, bufmap_size);
393 return -EINVAL;
394 }
395
396 gossip_debug(GOSSIP_FILE_DEBUG,
397 "%s(%pU) %zd@%llu\n",
398 __func__,
399 &pvfs2_inode->refn.khandle,
400 count,
401 llu(*offset));
402
Al Viro3c2fcfc2015-10-08 18:00:26 -0400403 iov_iter_init(&iter, READ, &vec, 1, count);
404 ret = wait_for_direct_io(PVFS_IO_READ, inode, offset, &iter,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400405 count, readahead_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400406 if (ret > 0)
407 *offset += ret;
408
409 gossip_debug(GOSSIP_FILE_DEBUG,
410 "%s(%pU): Value(%zd) returned.\n",
411 __func__,
412 &pvfs2_inode->refn.khandle,
413 ret);
414
415 return ret;
416}
417
418static ssize_t pvfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
419{
420 struct file *file = iocb->ki_filp;
421 loff_t pos = *(&iocb->ki_pos);
422 ssize_t rc = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400423
424 BUG_ON(iocb->private);
425
426 gossip_debug(GOSSIP_FILE_DEBUG, "pvfs2_file_read_iter\n");
427
428 g_pvfs2_stats.reads++;
429
Al Viro0071ed12015-10-08 18:22:08 -0400430 rc = do_readv_writev(PVFS_IO_READ, file, &pos, iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400431 iocb->ki_pos = pos;
432
433 return rc;
434}
435
436static ssize_t pvfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
437{
438 struct file *file = iocb->ki_filp;
439 loff_t pos = *(&iocb->ki_pos);
Mike Marshall5db11c22015-07-17 10:38:12 -0400440 ssize_t rc;
441
442 BUG_ON(iocb->private);
443
444 gossip_debug(GOSSIP_FILE_DEBUG, "pvfs2_file_write_iter\n");
445
446 mutex_lock(&file->f_mapping->host->i_mutex);
447
448 /* Make sure generic_write_checks sees an up to date inode size. */
449 if (file->f_flags & O_APPEND) {
450 rc = pvfs2_inode_getattr(file->f_mapping->host,
451 PVFS_ATTR_SYS_SIZE);
452 if (rc) {
453 gossip_err("%s: pvfs2_inode_getattr failed, rc:%zd:.\n",
454 __func__, rc);
455 goto out;
456 }
457 }
458
459 if (file->f_pos > i_size_read(file->f_mapping->host))
460 pvfs2_i_size_write(file->f_mapping->host, file->f_pos);
461
462 rc = generic_write_checks(iocb, iter);
463
464 if (rc <= 0) {
465 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
466 __func__, rc);
467 goto out;
468 }
469
470 rc = do_readv_writev(PVFS_IO_WRITE,
471 file,
472 &pos,
Al Viro0071ed12015-10-08 18:22:08 -0400473 iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400474 if (rc < 0) {
475 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
476 __func__, rc);
477 goto out;
478 }
479
480 iocb->ki_pos = pos;
481 g_pvfs2_stats.writes++;
482
483out:
484
485 mutex_unlock(&file->f_mapping->host->i_mutex);
486 return rc;
487}
488
489/*
490 * Perform a miscellaneous operation on a file.
491 */
Mike Marshall84d02152015-07-28 13:27:51 -0400492static long pvfs2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Mike Marshall5db11c22015-07-17 10:38:12 -0400493{
494 int ret = -ENOTTY;
495 __u64 val = 0;
496 unsigned long uval;
497
498 gossip_debug(GOSSIP_FILE_DEBUG,
499 "pvfs2_ioctl: called with cmd %d\n",
500 cmd);
501
502 /*
503 * we understand some general ioctls on files, such as the immutable
504 * and append flags
505 */
506 if (cmd == FS_IOC_GETFLAGS) {
507 val = 0;
508 ret = pvfs2_xattr_get_default(file->f_path.dentry,
509 "user.pvfs2.meta_hint",
510 &val,
511 sizeof(val),
512 0);
513 if (ret < 0 && ret != -ENODATA)
514 return ret;
515 else if (ret == -ENODATA)
516 val = 0;
517 uval = val;
518 gossip_debug(GOSSIP_FILE_DEBUG,
519 "pvfs2_ioctl: FS_IOC_GETFLAGS: %llu\n",
520 (unsigned long long)uval);
521 return put_user(uval, (int __user *)arg);
522 } else if (cmd == FS_IOC_SETFLAGS) {
523 ret = 0;
524 if (get_user(uval, (int __user *)arg))
525 return -EFAULT;
526 /*
527 * PVFS_MIRROR_FL is set internally when the mirroring mode
528 * is turned on for a file. The user is not allowed to turn
529 * on this bit, but the bit is present if the user first gets
530 * the flags and then updates the flags with some new
531 * settings. So, we ignore it in the following edit. bligon.
532 */
533 if ((uval & ~PVFS_MIRROR_FL) &
534 (~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL))) {
535 gossip_err("pvfs2_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
536 return -EINVAL;
537 }
538 val = uval;
539 gossip_debug(GOSSIP_FILE_DEBUG,
540 "pvfs2_ioctl: FS_IOC_SETFLAGS: %llu\n",
541 (unsigned long long)val);
542 ret = pvfs2_xattr_set_default(file->f_path.dentry,
543 "user.pvfs2.meta_hint",
544 &val,
545 sizeof(val),
546 0,
547 0);
548 }
549
550 return ret;
551}
552
553/*
554 * Memory map a region of a file.
555 */
556static int pvfs2_file_mmap(struct file *file, struct vm_area_struct *vma)
557{
558 gossip_debug(GOSSIP_FILE_DEBUG,
559 "pvfs2_file_mmap: called on %s\n",
560 (file ?
561 (char *)file->f_path.dentry->d_name.name :
562 (char *)"Unknown"));
563
564 /* set the sequential readahead hint */
565 vma->vm_flags |= VM_SEQ_READ;
566 vma->vm_flags &= ~VM_RAND_READ;
Martin Brandenburg35390802015-09-30 13:11:54 -0400567
568 /* Use readonly mmap since we cannot support writable maps. */
569 return generic_file_readonly_mmap(file, vma);
Mike Marshall5db11c22015-07-17 10:38:12 -0400570}
571
572#define mapping_nrpages(idata) ((idata)->nrpages)
573
574/*
575 * Called to notify the module that there are no more references to
576 * this file (i.e. no processes have it open).
577 *
578 * \note Not called when each file is closed.
579 */
Mike Marshall84d02152015-07-28 13:27:51 -0400580static int pvfs2_file_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400581{
582 gossip_debug(GOSSIP_FILE_DEBUG,
583 "pvfs2_file_release: called on %s\n",
584 file->f_path.dentry->d_name.name);
585
586 pvfs2_flush_inode(inode);
587
588 /*
Mike Marshall54804942015-10-05 13:44:24 -0400589 * remove all associated inode pages from the page cache and mmap
590 * readahead cache (if any); this forces an expensive refresh of
591 * data for the next caller of mmap (or 'get_block' accesses)
Mike Marshall5db11c22015-07-17 10:38:12 -0400592 */
593 if (file->f_path.dentry->d_inode &&
594 file->f_path.dentry->d_inode->i_mapping &&
595 mapping_nrpages(&file->f_path.dentry->d_inode->i_data))
596 truncate_inode_pages(file->f_path.dentry->d_inode->i_mapping,
597 0);
598 return 0;
599}
600
601/*
602 * Push all data for a specific file onto permanent storage.
603 */
Mike Marshall84d02152015-07-28 13:27:51 -0400604static int pvfs2_fsync(struct file *file,
605 loff_t start,
606 loff_t end,
607 int datasync)
Mike Marshall5db11c22015-07-17 10:38:12 -0400608{
609 int ret = -EINVAL;
610 struct pvfs2_inode_s *pvfs2_inode =
611 PVFS2_I(file->f_path.dentry->d_inode);
612 struct pvfs2_kernel_op_s *new_op = NULL;
613
614 /* required call */
615 filemap_write_and_wait_range(file->f_mapping, start, end);
616
617 new_op = op_alloc(PVFS2_VFS_OP_FSYNC);
618 if (!new_op)
619 return -ENOMEM;
620 new_op->upcall.req.fsync.refn = pvfs2_inode->refn;
621
622 ret = service_operation(new_op,
623 "pvfs2_fsync",
624 get_interruptible_flag(file->f_path.dentry->d_inode));
625
626 gossip_debug(GOSSIP_FILE_DEBUG,
627 "pvfs2_fsync got return value of %d\n",
628 ret);
629
630 op_release(new_op);
631
632 pvfs2_flush_inode(file->f_path.dentry->d_inode);
633 return ret;
634}
635
636/*
637 * Change the file pointer position for an instance of an open file.
638 *
639 * \note If .llseek is overriden, we must acquire lock as described in
640 * Documentation/filesystems/Locking.
641 *
642 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
643 * require much changes to the FS
644 */
Mike Marshall84d02152015-07-28 13:27:51 -0400645static loff_t pvfs2_file_llseek(struct file *file, loff_t offset, int origin)
Mike Marshall5db11c22015-07-17 10:38:12 -0400646{
647 int ret = -EINVAL;
648 struct inode *inode = file->f_path.dentry->d_inode;
649
650 if (!inode) {
651 gossip_err("pvfs2_file_llseek: invalid inode (NULL)\n");
652 return ret;
653 }
654
655 if (origin == PVFS2_SEEK_END) {
656 /*
657 * revalidate the inode's file size.
658 * NOTE: We are only interested in file size here,
659 * so we set mask accordingly.
660 */
661 ret = pvfs2_inode_getattr(inode, PVFS_ATTR_SYS_SIZE);
662 if (ret) {
663 gossip_debug(GOSSIP_FILE_DEBUG,
664 "%s:%s:%d calling make bad inode\n",
665 __FILE__,
666 __func__,
667 __LINE__);
668 pvfs2_make_bad_inode(inode);
669 return ret;
670 }
671 }
672
673 gossip_debug(GOSSIP_FILE_DEBUG,
Mike Marshall54804942015-10-05 13:44:24 -0400674 "pvfs2_file_llseek: offset is %ld | origin is %d"
675 " | inode size is %lu\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400676 (long)offset,
677 origin,
678 (unsigned long)file->f_path.dentry->d_inode->i_size);
679
680 return generic_file_llseek(file, offset, origin);
681}
682
683/*
684 * Support local locks (locks that only this kernel knows about)
685 * if Orangefs was mounted -o local_lock.
686 */
Mike Marshall84d02152015-07-28 13:27:51 -0400687static int pvfs2_lock(struct file *filp, int cmd, struct file_lock *fl)
Mike Marshall5db11c22015-07-17 10:38:12 -0400688{
Mike Marshallf957ae22015-09-24 12:53:05 -0400689 int rc = -EINVAL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400690
691 if (PVFS2_SB(filp->f_inode->i_sb)->flags & PVFS2_OPT_LOCAL_LOCK) {
692 if (cmd == F_GETLK) {
693 rc = 0;
694 posix_test_lock(filp, fl);
695 } else {
696 rc = posix_lock_file(filp, fl, NULL);
697 }
698 }
699
700 return rc;
701}
702
703/** PVFS2 implementation of VFS file operations */
704const struct file_operations pvfs2_file_operations = {
705 .llseek = pvfs2_file_llseek,
706 .read_iter = pvfs2_file_read_iter,
707 .write_iter = pvfs2_file_write_iter,
708 .lock = pvfs2_lock,
709 .unlocked_ioctl = pvfs2_ioctl,
710 .mmap = pvfs2_file_mmap,
711 .open = generic_file_open,
712 .release = pvfs2_file_release,
713 .fsync = pvfs2_fsync,
714};