2 * (C) 2001 Clemson University and The University of Chicago
4 * See COPYING in top-level directory.
8 * Linux VFS file operations.
12 #include "pvfs2-kernel.h"
13 #include "pvfs2-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 pvfs2_bufmap
*bufmap
,
34 struct iov_iter
*iter
,
39 * copy data from application/kernel by pulling it out
45 ret
= pvfs_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 pvfs2_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
= pvfs_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 PVFS_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 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;
107 new_op
= op_alloc(PVFS2_VFS_OP_FILE_IO
);
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
;
118 populate_shared_memory
:
119 /* get a shared buffer index */
120 ret
= pvfs_bufmap_get(&bufmap
, &buffer_index
);
122 gossip_debug(GOSSIP_FILE_DEBUG
,
123 "%s: pvfs_bufmap_get failure (%ld)\n",
124 __func__
, (long)ret
);
127 gossip_debug(GOSSIP_FILE_DEBUG
,
128 "%s(%pU): GET op %p -> buffer_index %d\n",
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
;
139 gossip_debug(GOSSIP_FILE_DEBUG
,
140 "%s(%pU): offset: %llu total_size: %zd\n",
146 * Stage 1: copy the buffers into client-core's address space
147 * precopy_buffers only pertains to writes.
149 if (type
== PVFS_IO_WRITE
) {
150 ret
= precopy_buffers(bufmap
,
158 gossip_debug(GOSSIP_FILE_DEBUG
,
159 "%s(%pU): Calling post_io_request with tag (%llu)\n",
164 /* Stage 2: Service the I/O operation */
165 ret
= service_operation(new_op
,
166 type
== PVFS_IO_WRITE
?
169 get_interruptible_flag(inode
));
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.
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",
185 goto populate_shared_memory
;
189 handle_io_error(); /* defined in pvfs2-kernel.h */
191 * 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)
196 gossip_debug(GOSSIP_FILE_DEBUG
,
197 "%s: returning error %ld\n", __func__
,
200 gossip_err("%s: error in %s handle %pU, returning %zd\n",
202 type
== PVFS_IO_READ
?
203 "read from" : "write to",
209 * Stage 3: Post copy buffers from client-core's address space
210 * postcopy_buffers only pertains to reads.
212 if (type
== PVFS_IO_READ
) {
213 ret
= postcopy_buffers(bufmap
,
216 new_op
->downcall
.resp
.io
.amt_complete
);
219 * put error codes in downcall so that handle_io_error()
220 * preserves it properly
222 new_op
->downcall
.status
= ret
;
227 gossip_debug(GOSSIP_FILE_DEBUG
,
228 "%s(%pU): Amount written as returned by the sys-io call:%d\n",
231 (int)new_op
->downcall
.resp
.io
.amt_complete
);
233 ret
= new_op
->downcall
.resp
.io
.amt_complete
;
236 * 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
241 wake_up_daemon_for_return(new_op
);
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
);
260 * 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.
266 static ssize_t
do_readv_writev(enum PVFS_io_type type
, struct file
*file
,
267 loff_t
*offset
, struct iov_iter
*iter
)
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
;
272 size_t count
= iov_iter_count(iter
);
273 ssize_t total_count
= 0;
274 ssize_t ret
= -EINVAL
;
276 gossip_debug(GOSSIP_FILE_DEBUG
,
277 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
282 if (type
== PVFS_IO_WRITE
) {
283 gossip_debug(GOSSIP_FILE_DEBUG
,
284 "%s(%pU): proceeding with offset : %llu, "
297 while (iov_iter_count(iter
)) {
298 size_t each_count
= iov_iter_count(iter
);
301 /* how much to transfer in this loop iteration */
302 if (each_count
> pvfs_bufmap_size_query())
303 each_count
= pvfs_bufmap_size_query();
305 gossip_debug(GOSSIP_FILE_DEBUG
,
306 "%s(%pU): size of each_count(%d)\n",
310 gossip_debug(GOSSIP_FILE_DEBUG
,
311 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
316 ret
= wait_for_direct_io(type
, inode
, offset
, iter
,
318 gossip_debug(GOSSIP_FILE_DEBUG
,
319 "%s(%pU): return from wait_for_io:%d\n",
331 gossip_debug(GOSSIP_FILE_DEBUG
,
332 "%s(%pU): AFTER wait_for_io: offset is %d\n",
338 * if we got a short I/O operations,
339 * fall out and return what we got so far
341 if (amt_complete
< each_count
)
349 if (type
== PVFS_IO_READ
) {
352 SetMtimeFlag(pvfs2_inode
);
353 inode
->i_mtime
= CURRENT_TIME
;
354 mark_inode_dirty_sync(inode
);
358 gossip_debug(GOSSIP_FILE_DEBUG
,
359 "%s(%pU): Value(%d) returned.\n",
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.
371 ssize_t
pvfs2_inode_read(struct inode
*inode
,
372 struct iov_iter
*iter
,
374 loff_t readahead_size
)
376 struct pvfs2_inode_s
*pvfs2_inode
= PVFS2_I(inode
);
377 size_t count
= iov_iter_count(iter
);
379 ssize_t ret
= -EINVAL
;
381 g_pvfs2_stats
.reads
++;
383 bufmap_size
= pvfs_bufmap_size_query();
384 if (count
> bufmap_size
) {
385 gossip_debug(GOSSIP_FILE_DEBUG
,
386 "%s: count is too large (%zd/%zd)!\n",
387 __func__
, count
, bufmap_size
);
391 gossip_debug(GOSSIP_FILE_DEBUG
,
392 "%s(%pU) %zd@%llu\n",
394 &pvfs2_inode
->refn
.khandle
,
398 ret
= wait_for_direct_io(PVFS_IO_READ
, inode
, offset
, iter
,
399 count
, readahead_size
);
403 gossip_debug(GOSSIP_FILE_DEBUG
,
404 "%s(%pU): Value(%zd) returned.\n",
406 &pvfs2_inode
->refn
.khandle
,
412 static ssize_t
pvfs2_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
414 struct file
*file
= iocb
->ki_filp
;
415 loff_t pos
= *(&iocb
->ki_pos
);
418 BUG_ON(iocb
->private);
420 gossip_debug(GOSSIP_FILE_DEBUG
, "pvfs2_file_read_iter\n");
422 g_pvfs2_stats
.reads
++;
424 rc
= do_readv_writev(PVFS_IO_READ
, file
, &pos
, iter
);
430 static ssize_t
pvfs2_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
432 struct file
*file
= iocb
->ki_filp
;
436 BUG_ON(iocb
->private);
438 gossip_debug(GOSSIP_FILE_DEBUG
, "pvfs2_file_write_iter\n");
440 mutex_lock(&file
->f_mapping
->host
->i_mutex
);
442 /* Make sure generic_write_checks sees an up to date inode size. */
443 if (file
->f_flags
& O_APPEND
) {
444 rc
= pvfs2_inode_getattr(file
->f_mapping
->host
,
447 gossip_err("%s: pvfs2_inode_getattr failed, rc:%zd:.\n",
453 if (file
->f_pos
> i_size_read(file
->f_mapping
->host
))
454 pvfs2_i_size_write(file
->f_mapping
->host
, file
->f_pos
);
456 rc
= generic_write_checks(iocb
, iter
);
459 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
465 * if we are appending, generic_write_checks would have updated
466 * pos to the end of the file, so we will wait till now to set
469 pos
= *(&iocb
->ki_pos
);
471 rc
= do_readv_writev(PVFS_IO_WRITE
,
476 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
482 g_pvfs2_stats
.writes
++;
486 mutex_unlock(&file
->f_mapping
->host
->i_mutex
);
491 * Perform a miscellaneous operation on a file.
493 static long pvfs2_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
499 gossip_debug(GOSSIP_FILE_DEBUG
,
500 "pvfs2_ioctl: called with cmd %d\n",
504 * we understand some general ioctls on files, such as the immutable
507 if (cmd
== FS_IOC_GETFLAGS
) {
509 ret
= pvfs2_inode_getxattr(file_inode(file
),
510 PVFS2_XATTR_NAME_DEFAULT_PREFIX
,
511 "user.pvfs2.meta_hint",
513 if (ret
< 0 && ret
!= -ENODATA
)
515 else if (ret
== -ENODATA
)
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
) {
524 if (get_user(uval
, (int __user
*)arg
))
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.
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");
539 gossip_debug(GOSSIP_FILE_DEBUG
,
540 "pvfs2_ioctl: FS_IOC_SETFLAGS: %llu\n",
541 (unsigned long long)val
);
542 ret
= pvfs2_inode_setxattr(file_inode(file
),
543 PVFS2_XATTR_NAME_DEFAULT_PREFIX
,
544 "user.pvfs2.meta_hint",
545 &val
, sizeof(val
), 0);
552 * Memory map a region of a file.
554 static int pvfs2_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
556 gossip_debug(GOSSIP_FILE_DEBUG
,
557 "pvfs2_file_mmap: called on %s\n",
559 (char *)file
->f_path
.dentry
->d_name
.name
:
562 /* set the sequential readahead hint */
563 vma
->vm_flags
|= VM_SEQ_READ
;
564 vma
->vm_flags
&= ~VM_RAND_READ
;
566 /* Use readonly mmap since we cannot support writable maps. */
567 return generic_file_readonly_mmap(file
, vma
);
570 #define mapping_nrpages(idata) ((idata)->nrpages)
573 * Called to notify the module that there are no more references to
574 * this file (i.e. no processes have it open).
576 * \note Not called when each file is closed.
578 static int pvfs2_file_release(struct inode
*inode
, struct file
*file
)
580 gossip_debug(GOSSIP_FILE_DEBUG
,
581 "pvfs2_file_release: called on %s\n",
582 file
->f_path
.dentry
->d_name
.name
);
584 pvfs2_flush_inode(inode
);
587 * remove all associated inode pages from the page cache and mmap
588 * readahead cache (if any); this forces an expensive refresh of
589 * data for the next caller of mmap (or 'get_block' accesses)
591 if (file
->f_path
.dentry
->d_inode
&&
592 file
->f_path
.dentry
->d_inode
->i_mapping
&&
593 mapping_nrpages(&file
->f_path
.dentry
->d_inode
->i_data
))
594 truncate_inode_pages(file
->f_path
.dentry
->d_inode
->i_mapping
,
600 * Push all data for a specific file onto permanent storage.
602 static int pvfs2_fsync(struct file
*file
,
608 struct pvfs2_inode_s
*pvfs2_inode
=
609 PVFS2_I(file
->f_path
.dentry
->d_inode
);
610 struct pvfs2_kernel_op_s
*new_op
= NULL
;
613 filemap_write_and_wait_range(file
->f_mapping
, start
, end
);
615 new_op
= op_alloc(PVFS2_VFS_OP_FSYNC
);
618 new_op
->upcall
.req
.fsync
.refn
= pvfs2_inode
->refn
;
620 ret
= service_operation(new_op
,
622 get_interruptible_flag(file
->f_path
.dentry
->d_inode
));
624 gossip_debug(GOSSIP_FILE_DEBUG
,
625 "pvfs2_fsync got return value of %d\n",
630 pvfs2_flush_inode(file
->f_path
.dentry
->d_inode
);
635 * Change the file pointer position for an instance of an open file.
637 * \note If .llseek is overriden, we must acquire lock as described in
638 * Documentation/filesystems/Locking.
640 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
641 * require much changes to the FS
643 static loff_t
pvfs2_file_llseek(struct file
*file
, loff_t offset
, int origin
)
646 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
649 gossip_err("pvfs2_file_llseek: invalid inode (NULL)\n");
653 if (origin
== PVFS2_SEEK_END
) {
655 * revalidate the inode's file size.
656 * NOTE: We are only interested in file size here,
657 * so we set mask accordingly.
659 ret
= pvfs2_inode_getattr(inode
, PVFS_ATTR_SYS_SIZE
);
661 gossip_debug(GOSSIP_FILE_DEBUG
,
662 "%s:%s:%d calling make bad inode\n",
666 pvfs2_make_bad_inode(inode
);
671 gossip_debug(GOSSIP_FILE_DEBUG
,
672 "pvfs2_file_llseek: offset is %ld | origin is %d"
673 " | inode size is %lu\n",
676 (unsigned long)file
->f_path
.dentry
->d_inode
->i_size
);
678 return generic_file_llseek(file
, offset
, origin
);
682 * Support local locks (locks that only this kernel knows about)
683 * if Orangefs was mounted -o local_lock.
685 static int pvfs2_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
689 if (PVFS2_SB(filp
->f_inode
->i_sb
)->flags
& PVFS2_OPT_LOCAL_LOCK
) {
690 if (cmd
== F_GETLK
) {
692 posix_test_lock(filp
, fl
);
694 rc
= posix_lock_file(filp
, fl
, NULL
);
701 /** PVFS2 implementation of VFS file operations */
702 const struct file_operations pvfs2_file_operations
= {
703 .llseek
= pvfs2_file_llseek
,
704 .read_iter
= pvfs2_file_read_iter
,
705 .write_iter
= pvfs2_file_write_iter
,
707 .unlocked_ioctl
= pvfs2_ioctl
,
708 .mmap
= pvfs2_file_mmap
,
709 .open
= generic_file_open
,
710 .release
= pvfs2_file_release
,
711 .fsync
= pvfs2_fsync
,