2 * (C) 2001 Clemson University and The University of Chicago
4 * See COPYING in top-level directory.
8 * Linux VFS file operations.
12 #include "orangefs-kernel.h"
13 #include "orangefs-bufmap.h"
15 #include <linux/pagemap.h>
17 #define wake_up_daemon_for_return(op) \
19 spin_lock(&op->lock); \
20 op->io_completed = 1; \
21 spin_unlock(&op->lock); \
22 wake_up_interruptible(&op->io_completion_waitq);\
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
32 static int precopy_buffers(struct orangefs_bufmap
*bufmap
,
34 struct iov_iter
*iter
,
39 * copy data from application/kernel by pulling it out
45 ret
= orangefs_bufmap_copy_from_iovec(bufmap
,
50 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
56 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
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
69 static int postcopy_buffers(struct orangefs_bufmap
*bufmap
,
71 struct iov_iter
*iter
,
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.
81 ret
= orangefs_bufmap_copy_to_iovec(bufmap
,
86 gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
94 * Post and wait for the I/O upcall to finish
96 static ssize_t
wait_for_direct_io(enum ORANGEFS_io_type type
, struct inode
*inode
,
97 loff_t
*offset
, struct iov_iter
*iter
,
98 size_t total_size
, loff_t readahead_size
)
100 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
101 struct orangefs_khandle
*handle
= &orangefs_inode
->refn
.khandle
;
102 struct orangefs_bufmap
*bufmap
= NULL
;
103 struct orangefs_kernel_op_s
*new_op
= NULL
;
104 int buffer_index
= -1;
107 new_op
= op_alloc(ORANGEFS_VFS_OP_FILE_IO
);
111 /* synchronous I/O */
112 new_op
->upcall
.req
.io
.async_vfs_io
= ORANGEFS_VFS_SYNC_IO
;
113 new_op
->upcall
.req
.io
.readahead_size
= readahead_size
;
114 new_op
->upcall
.req
.io
.io_type
= type
;
115 new_op
->upcall
.req
.io
.refn
= orangefs_inode
->refn
;
117 populate_shared_memory
:
118 /* get a shared buffer index */
119 ret
= orangefs_bufmap_get(&bufmap
, &buffer_index
);
121 gossip_debug(GOSSIP_FILE_DEBUG
,
122 "%s: orangefs_bufmap_get failure (%ld)\n",
123 __func__
, (long)ret
);
126 gossip_debug(GOSSIP_FILE_DEBUG
,
127 "%s(%pU): GET op %p -> buffer_index %d\n",
133 new_op
->uses_shared_memory
= 1;
134 new_op
->upcall
.req
.io
.buf_index
= buffer_index
;
135 new_op
->upcall
.req
.io
.count
= total_size
;
136 new_op
->upcall
.req
.io
.offset
= *offset
;
138 gossip_debug(GOSSIP_FILE_DEBUG
,
139 "%s(%pU): offset: %llu total_size: %zd\n",
145 * Stage 1: copy the buffers into client-core's address space
146 * precopy_buffers only pertains to writes.
148 if (type
== ORANGEFS_IO_WRITE
) {
149 ret
= precopy_buffers(bufmap
,
157 gossip_debug(GOSSIP_FILE_DEBUG
,
158 "%s(%pU): Calling post_io_request with tag (%llu)\n",
163 /* Stage 2: Service the I/O operation */
164 ret
= service_operation(new_op
,
165 type
== ORANGEFS_IO_WRITE
?
168 get_interruptible_flag(inode
));
171 * If service_operation() returns -EAGAIN #and# the operation was
172 * purged from orangefs_request_list or htable_ops_in_progress, then
173 * we know that the client was restarted, causing the shared memory
174 * area to be wiped clean. To restart a write operation in this
175 * case, we must re-copy the data from the user's iovec to a NEW
176 * shared memory location. To restart a read operation, we must get
177 * a new shared memory location.
179 if (ret
== -EAGAIN
&& op_state_purged(new_op
)) {
180 orangefs_bufmap_put(bufmap
, buffer_index
);
181 gossip_debug(GOSSIP_FILE_DEBUG
,
182 "%s:going to repopulate_shared_memory.\n",
184 goto populate_shared_memory
;
190 * don't write an error to syslog on signaled operation
191 * termination unless we've got debugging turned on, as
192 * this can happen regularly (i.e. ctrl-c)
195 gossip_debug(GOSSIP_FILE_DEBUG
,
196 "%s: returning error %ld\n", __func__
,
199 gossip_err("%s: error in %s handle %pU, returning %zd\n",
201 type
== ORANGEFS_IO_READ
?
202 "read from" : "write to",
208 * Stage 3: Post copy buffers from client-core's address space
209 * postcopy_buffers only pertains to reads.
211 if (type
== ORANGEFS_IO_READ
) {
212 ret
= postcopy_buffers(bufmap
,
215 new_op
->downcall
.resp
.io
.amt_complete
);
218 * put error codes in downcall so that handle_io_error()
219 * preserves it properly
221 new_op
->downcall
.status
= ret
;
226 gossip_debug(GOSSIP_FILE_DEBUG
,
227 "%s(%pU): Amount written as returned by the sys-io call:%d\n",
230 (int)new_op
->downcall
.resp
.io
.amt_complete
);
232 ret
= new_op
->downcall
.resp
.io
.amt_complete
;
235 * tell the device file owner waiting on I/O that this read has
236 * completed and it can return now.
238 wake_up_daemon_for_return(new_op
);
241 if (buffer_index
>= 0) {
242 orangefs_bufmap_put(bufmap
, buffer_index
);
243 gossip_debug(GOSSIP_FILE_DEBUG
,
244 "%s(%pU): PUT buffer_index %d\n",
245 __func__
, handle
, buffer_index
);
253 * Common entry point for read/write/readv/writev
254 * This function will dispatch it to either the direct I/O
255 * or buffered I/O path depending on the mount options and/or
256 * augmented/extended metadata attached to the file.
257 * Note: File extended attributes override any mount options.
259 static ssize_t
do_readv_writev(enum ORANGEFS_io_type type
, struct file
*file
,
260 loff_t
*offset
, struct iov_iter
*iter
)
262 struct inode
*inode
= file
->f_mapping
->host
;
263 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
264 struct orangefs_khandle
*handle
= &orangefs_inode
->refn
.khandle
;
265 size_t count
= iov_iter_count(iter
);
266 ssize_t total_count
= 0;
267 ssize_t ret
= -EINVAL
;
269 gossip_debug(GOSSIP_FILE_DEBUG
,
270 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
275 if (type
== ORANGEFS_IO_WRITE
) {
276 gossip_debug(GOSSIP_FILE_DEBUG
,
277 "%s(%pU): proceeding with offset : %llu, "
290 while (iov_iter_count(iter
)) {
291 size_t each_count
= iov_iter_count(iter
);
294 /* how much to transfer in this loop iteration */
295 if (each_count
> orangefs_bufmap_size_query())
296 each_count
= orangefs_bufmap_size_query();
298 gossip_debug(GOSSIP_FILE_DEBUG
,
299 "%s(%pU): size of each_count(%d)\n",
303 gossip_debug(GOSSIP_FILE_DEBUG
,
304 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
309 ret
= wait_for_direct_io(type
, inode
, offset
, iter
,
311 gossip_debug(GOSSIP_FILE_DEBUG
,
312 "%s(%pU): return from wait_for_io:%d\n",
324 gossip_debug(GOSSIP_FILE_DEBUG
,
325 "%s(%pU): AFTER wait_for_io: offset is %d\n",
331 * if we got a short I/O operations,
332 * fall out and return what we got so far
334 if (amt_complete
< each_count
)
342 if (type
== ORANGEFS_IO_READ
) {
345 SetMtimeFlag(orangefs_inode
);
346 inode
->i_mtime
= CURRENT_TIME
;
347 mark_inode_dirty_sync(inode
);
351 gossip_debug(GOSSIP_FILE_DEBUG
,
352 "%s(%pU): Value(%d) returned.\n",
361 * Read data from a specified offset in a file (referenced by inode).
362 * Data may be placed either in a user or kernel buffer.
364 ssize_t
orangefs_inode_read(struct inode
*inode
,
365 struct iov_iter
*iter
,
367 loff_t readahead_size
)
369 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
370 size_t count
= iov_iter_count(iter
);
372 ssize_t ret
= -EINVAL
;
374 g_orangefs_stats
.reads
++;
376 bufmap_size
= orangefs_bufmap_size_query();
377 if (count
> bufmap_size
) {
378 gossip_debug(GOSSIP_FILE_DEBUG
,
379 "%s: count is too large (%zd/%zd)!\n",
380 __func__
, count
, bufmap_size
);
384 gossip_debug(GOSSIP_FILE_DEBUG
,
385 "%s(%pU) %zd@%llu\n",
387 &orangefs_inode
->refn
.khandle
,
391 ret
= wait_for_direct_io(ORANGEFS_IO_READ
, inode
, offset
, iter
,
392 count
, readahead_size
);
396 gossip_debug(GOSSIP_FILE_DEBUG
,
397 "%s(%pU): Value(%zd) returned.\n",
399 &orangefs_inode
->refn
.khandle
,
405 static ssize_t
orangefs_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
407 struct file
*file
= iocb
->ki_filp
;
408 loff_t pos
= *(&iocb
->ki_pos
);
411 BUG_ON(iocb
->private);
413 gossip_debug(GOSSIP_FILE_DEBUG
, "orangefs_file_read_iter\n");
415 g_orangefs_stats
.reads
++;
417 rc
= do_readv_writev(ORANGEFS_IO_READ
, file
, &pos
, iter
);
423 static ssize_t
orangefs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
425 struct file
*file
= iocb
->ki_filp
;
429 BUG_ON(iocb
->private);
431 gossip_debug(GOSSIP_FILE_DEBUG
, "orangefs_file_write_iter\n");
433 mutex_lock(&file
->f_mapping
->host
->i_mutex
);
435 /* Make sure generic_write_checks sees an up to date inode size. */
436 if (file
->f_flags
& O_APPEND
) {
437 rc
= orangefs_inode_getattr(file
->f_mapping
->host
,
438 ORANGEFS_ATTR_SYS_SIZE
);
440 gossip_err("%s: orangefs_inode_getattr failed, rc:%zd:.\n",
446 if (file
->f_pos
> i_size_read(file
->f_mapping
->host
))
447 orangefs_i_size_write(file
->f_mapping
->host
, file
->f_pos
);
449 rc
= generic_write_checks(iocb
, iter
);
452 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
458 * if we are appending, generic_write_checks would have updated
459 * pos to the end of the file, so we will wait till now to set
462 pos
= *(&iocb
->ki_pos
);
464 rc
= do_readv_writev(ORANGEFS_IO_WRITE
,
469 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
475 g_orangefs_stats
.writes
++;
479 mutex_unlock(&file
->f_mapping
->host
->i_mutex
);
484 * Perform a miscellaneous operation on a file.
486 static long orangefs_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
492 gossip_debug(GOSSIP_FILE_DEBUG
,
493 "orangefs_ioctl: called with cmd %d\n",
497 * we understand some general ioctls on files, such as the immutable
500 if (cmd
== FS_IOC_GETFLAGS
) {
502 ret
= orangefs_inode_getxattr(file_inode(file
),
503 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX
,
504 "user.pvfs2.meta_hint",
506 if (ret
< 0 && ret
!= -ENODATA
)
508 else if (ret
== -ENODATA
)
511 gossip_debug(GOSSIP_FILE_DEBUG
,
512 "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
513 (unsigned long long)uval
);
514 return put_user(uval
, (int __user
*)arg
);
515 } else if (cmd
== FS_IOC_SETFLAGS
) {
517 if (get_user(uval
, (int __user
*)arg
))
520 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
521 * is turned on for a file. The user is not allowed to turn
522 * on this bit, but the bit is present if the user first gets
523 * the flags and then updates the flags with some new
524 * settings. So, we ignore it in the following edit. bligon.
526 if ((uval
& ~ORANGEFS_MIRROR_FL
) &
527 (~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| FS_NOATIME_FL
))) {
528 gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
532 gossip_debug(GOSSIP_FILE_DEBUG
,
533 "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
534 (unsigned long long)val
);
535 ret
= orangefs_inode_setxattr(file_inode(file
),
536 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX
,
537 "user.pvfs2.meta_hint",
538 &val
, sizeof(val
), 0);
545 * Memory map a region of a file.
547 static int orangefs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
549 gossip_debug(GOSSIP_FILE_DEBUG
,
550 "orangefs_file_mmap: called on %s\n",
552 (char *)file
->f_path
.dentry
->d_name
.name
:
555 /* set the sequential readahead hint */
556 vma
->vm_flags
|= VM_SEQ_READ
;
557 vma
->vm_flags
&= ~VM_RAND_READ
;
559 /* Use readonly mmap since we cannot support writable maps. */
560 return generic_file_readonly_mmap(file
, vma
);
563 #define mapping_nrpages(idata) ((idata)->nrpages)
566 * Called to notify the module that there are no more references to
567 * this file (i.e. no processes have it open).
569 * \note Not called when each file is closed.
571 static int orangefs_file_release(struct inode
*inode
, struct file
*file
)
573 gossip_debug(GOSSIP_FILE_DEBUG
,
574 "orangefs_file_release: called on %s\n",
575 file
->f_path
.dentry
->d_name
.name
);
577 orangefs_flush_inode(inode
);
580 * remove all associated inode pages from the page cache and mmap
581 * readahead cache (if any); this forces an expensive refresh of
582 * data for the next caller of mmap (or 'get_block' accesses)
584 if (file
->f_path
.dentry
->d_inode
&&
585 file
->f_path
.dentry
->d_inode
->i_mapping
&&
586 mapping_nrpages(&file
->f_path
.dentry
->d_inode
->i_data
))
587 truncate_inode_pages(file
->f_path
.dentry
->d_inode
->i_mapping
,
593 * Push all data for a specific file onto permanent storage.
595 static int orangefs_fsync(struct file
*file
,
601 struct orangefs_inode_s
*orangefs_inode
=
602 ORANGEFS_I(file
->f_path
.dentry
->d_inode
);
603 struct orangefs_kernel_op_s
*new_op
= NULL
;
606 filemap_write_and_wait_range(file
->f_mapping
, start
, end
);
608 new_op
= op_alloc(ORANGEFS_VFS_OP_FSYNC
);
611 new_op
->upcall
.req
.fsync
.refn
= orangefs_inode
->refn
;
613 ret
= service_operation(new_op
,
615 get_interruptible_flag(file
->f_path
.dentry
->d_inode
));
617 gossip_debug(GOSSIP_FILE_DEBUG
,
618 "orangefs_fsync got return value of %d\n",
623 orangefs_flush_inode(file
->f_path
.dentry
->d_inode
);
628 * Change the file pointer position for an instance of an open file.
630 * \note If .llseek is overriden, we must acquire lock as described in
631 * Documentation/filesystems/Locking.
633 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
634 * require much changes to the FS
636 static loff_t
orangefs_file_llseek(struct file
*file
, loff_t offset
, int origin
)
639 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
642 gossip_err("orangefs_file_llseek: invalid inode (NULL)\n");
646 if (origin
== ORANGEFS_SEEK_END
) {
648 * revalidate the inode's file size.
649 * NOTE: We are only interested in file size here,
650 * so we set mask accordingly.
652 ret
= orangefs_inode_getattr(inode
, ORANGEFS_ATTR_SYS_SIZE
);
654 gossip_debug(GOSSIP_FILE_DEBUG
,
655 "%s:%s:%d calling make bad inode\n",
659 orangefs_make_bad_inode(inode
);
664 gossip_debug(GOSSIP_FILE_DEBUG
,
665 "orangefs_file_llseek: offset is %ld | origin is %d"
666 " | inode size is %lu\n",
669 (unsigned long)file
->f_path
.dentry
->d_inode
->i_size
);
671 return generic_file_llseek(file
, offset
, origin
);
675 * Support local locks (locks that only this kernel knows about)
676 * if Orangefs was mounted -o local_lock.
678 static int orangefs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
682 if (ORANGEFS_SB(filp
->f_inode
->i_sb
)->flags
& ORANGEFS_OPT_LOCAL_LOCK
) {
683 if (cmd
== F_GETLK
) {
685 posix_test_lock(filp
, fl
);
687 rc
= posix_lock_file(filp
, fl
, NULL
);
694 /** ORANGEFS implementation of VFS file operations */
695 const struct file_operations orangefs_file_operations
= {
696 .llseek
= orangefs_file_llseek
,
697 .read_iter
= orangefs_file_read_iter
,
698 .write_iter
= orangefs_file_write_iter
,
699 .lock
= orangefs_lock
,
700 .unlocked_ioctl
= orangefs_ioctl
,
701 .mmap
= orangefs_file_mmap
,
702 .open
= generic_file_open
,
703 .release
= orangefs_file_release
,
704 .fsync
= orangefs_fsync
,