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>
18 * Copy to client-core's address space from the buffers specified
19 * by the iovec upto total_size bytes.
20 * NOTE: the iovector can either contain addresses which
21 * can futher be kernel-space or user-space addresses.
22 * or it can pointers to struct page's
24 static int precopy_buffers(struct orangefs_bufmap
*bufmap
,
26 struct iov_iter
*iter
,
31 * copy data from application/kernel by pulling it out
37 ret
= orangefs_bufmap_copy_from_iovec(bufmap
,
42 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
48 gossip_err("%s: Failed to copy-in buffers. Please make sure that the pvfs2-client is running. %ld\n",
55 * Copy from client-core's address space to the buffers specified
56 * by the iovec upto total_size bytes.
57 * NOTE: the iovector can either contain addresses which
58 * can futher be kernel-space or user-space addresses.
59 * or it can pointers to struct page's
61 static int postcopy_buffers(struct orangefs_bufmap
*bufmap
,
63 struct iov_iter
*iter
,
68 * copy data to application/kernel by pushing it out to
69 * the iovec. NOTE; target buffers can be addresses or
70 * struct page pointers.
73 ret
= orangefs_bufmap_copy_to_iovec(bufmap
,
78 gossip_err("%s: Failed to copy-out buffers. Please make sure that the pvfs2-client is running (%ld)\n",
86 * Post and wait for the I/O upcall to finish
88 static ssize_t
wait_for_direct_io(enum ORANGEFS_io_type type
, struct inode
*inode
,
89 loff_t
*offset
, struct iov_iter
*iter
,
90 size_t total_size
, loff_t readahead_size
)
92 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
93 struct orangefs_khandle
*handle
= &orangefs_inode
->refn
.khandle
;
94 struct orangefs_bufmap
*bufmap
= NULL
;
95 struct orangefs_kernel_op_s
*new_op
= NULL
;
96 struct iov_iter saved
= *iter
;
97 int buffer_index
= -1;
100 new_op
= op_alloc(ORANGEFS_VFS_OP_FILE_IO
);
104 /* synchronous I/O */
105 new_op
->upcall
.req
.io
.async_vfs_io
= ORANGEFS_VFS_SYNC_IO
;
106 new_op
->upcall
.req
.io
.readahead_size
= readahead_size
;
107 new_op
->upcall
.req
.io
.io_type
= type
;
108 new_op
->upcall
.req
.io
.refn
= orangefs_inode
->refn
;
110 populate_shared_memory
:
111 /* get a shared buffer index */
112 ret
= orangefs_bufmap_get(&bufmap
, &buffer_index
);
114 gossip_debug(GOSSIP_FILE_DEBUG
,
115 "%s: orangefs_bufmap_get failure (%ld)\n",
116 __func__
, (long)ret
);
119 gossip_debug(GOSSIP_FILE_DEBUG
,
120 "%s(%pU): GET op %p -> buffer_index %d\n",
126 new_op
->uses_shared_memory
= 1;
127 new_op
->upcall
.req
.io
.buf_index
= buffer_index
;
128 new_op
->upcall
.req
.io
.count
= total_size
;
129 new_op
->upcall
.req
.io
.offset
= *offset
;
131 gossip_debug(GOSSIP_FILE_DEBUG
,
132 "%s(%pU): offset: %llu total_size: %zd\n",
138 * Stage 1: copy the buffers into client-core's address space
139 * precopy_buffers only pertains to writes.
141 if (type
== ORANGEFS_IO_WRITE
) {
142 ret
= precopy_buffers(bufmap
,
150 gossip_debug(GOSSIP_FILE_DEBUG
,
151 "%s(%pU): Calling post_io_request with tag (%llu)\n",
156 /* Stage 2: Service the I/O operation */
157 ret
= service_operation(new_op
,
158 type
== ORANGEFS_IO_WRITE
?
161 get_interruptible_flag(inode
));
164 * If service_operation() returns -EAGAIN #and# the operation was
165 * purged from orangefs_request_list or htable_ops_in_progress, then
166 * we know that the client was restarted, causing the shared memory
167 * area to be wiped clean. To restart a write operation in this
168 * case, we must re-copy the data from the user's iovec to a NEW
169 * shared memory location. To restart a read operation, we must get
170 * a new shared memory location.
172 if (ret
== -EAGAIN
&& op_state_purged(new_op
)) {
173 orangefs_bufmap_put(buffer_index
);
175 if (type
== ORANGEFS_IO_WRITE
)
177 gossip_debug(GOSSIP_FILE_DEBUG
,
178 "%s:going to repopulate_shared_memory.\n",
180 goto populate_shared_memory
;
185 * don't write an error to syslog on signaled operation
186 * termination unless we've got debugging turned on, as
187 * this can happen regularly (i.e. ctrl-c)
190 gossip_debug(GOSSIP_FILE_DEBUG
,
191 "%s: returning error %ld\n", __func__
,
194 gossip_err("%s: error in %s handle %pU, returning %zd\n",
196 type
== ORANGEFS_IO_READ
?
197 "read from" : "write to",
199 if (orangefs_cancel_op_in_progress(new_op
))
206 * Stage 3: Post copy buffers from client-core's address space
207 * postcopy_buffers only pertains to reads.
209 if (type
== ORANGEFS_IO_READ
) {
210 ret
= postcopy_buffers(bufmap
,
213 new_op
->downcall
.resp
.io
.amt_complete
);
217 gossip_debug(GOSSIP_FILE_DEBUG
,
218 "%s(%pU): Amount written as returned by the sys-io call:%d\n",
221 (int)new_op
->downcall
.resp
.io
.amt_complete
);
223 ret
= new_op
->downcall
.resp
.io
.amt_complete
;
227 * tell the device file owner waiting on I/O that this read has
228 * completed and it can return now.
230 complete(&new_op
->done
);
233 if (buffer_index
>= 0) {
234 orangefs_bufmap_put(buffer_index
);
235 gossip_debug(GOSSIP_FILE_DEBUG
,
236 "%s(%pU): PUT buffer_index %d\n",
237 __func__
, handle
, buffer_index
);
245 * Common entry point for read/write/readv/writev
246 * This function will dispatch it to either the direct I/O
247 * or buffered I/O path depending on the mount options and/or
248 * augmented/extended metadata attached to the file.
249 * Note: File extended attributes override any mount options.
251 static ssize_t
do_readv_writev(enum ORANGEFS_io_type type
, struct file
*file
,
252 loff_t
*offset
, struct iov_iter
*iter
)
254 struct inode
*inode
= file
->f_mapping
->host
;
255 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
256 struct orangefs_khandle
*handle
= &orangefs_inode
->refn
.khandle
;
257 size_t count
= iov_iter_count(iter
);
258 ssize_t total_count
= 0;
259 ssize_t ret
= -EINVAL
;
261 gossip_debug(GOSSIP_FILE_DEBUG
,
262 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
267 if (type
== ORANGEFS_IO_WRITE
) {
268 gossip_debug(GOSSIP_FILE_DEBUG
,
269 "%s(%pU): proceeding with offset : %llu, "
282 while (iov_iter_count(iter
)) {
283 size_t each_count
= iov_iter_count(iter
);
286 /* how much to transfer in this loop iteration */
287 if (each_count
> orangefs_bufmap_size_query())
288 each_count
= orangefs_bufmap_size_query();
290 gossip_debug(GOSSIP_FILE_DEBUG
,
291 "%s(%pU): size of each_count(%d)\n",
295 gossip_debug(GOSSIP_FILE_DEBUG
,
296 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
301 ret
= wait_for_direct_io(type
, inode
, offset
, iter
,
303 gossip_debug(GOSSIP_FILE_DEBUG
,
304 "%s(%pU): return from wait_for_io:%d\n",
316 gossip_debug(GOSSIP_FILE_DEBUG
,
317 "%s(%pU): AFTER wait_for_io: offset is %d\n",
323 * if we got a short I/O operations,
324 * fall out and return what we got so far
326 if (amt_complete
< each_count
)
334 if (type
== ORANGEFS_IO_READ
) {
337 SetMtimeFlag(orangefs_inode
);
338 inode
->i_mtime
= CURRENT_TIME
;
339 mark_inode_dirty_sync(inode
);
343 gossip_debug(GOSSIP_FILE_DEBUG
,
344 "%s(%pU): Value(%d) returned.\n",
353 * Read data from a specified offset in a file (referenced by inode).
354 * Data may be placed either in a user or kernel buffer.
356 ssize_t
orangefs_inode_read(struct inode
*inode
,
357 struct iov_iter
*iter
,
359 loff_t readahead_size
)
361 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
362 size_t count
= iov_iter_count(iter
);
364 ssize_t ret
= -EINVAL
;
366 g_orangefs_stats
.reads
++;
368 bufmap_size
= orangefs_bufmap_size_query();
369 if (count
> bufmap_size
) {
370 gossip_debug(GOSSIP_FILE_DEBUG
,
371 "%s: count is too large (%zd/%zd)!\n",
372 __func__
, count
, bufmap_size
);
376 gossip_debug(GOSSIP_FILE_DEBUG
,
377 "%s(%pU) %zd@%llu\n",
379 &orangefs_inode
->refn
.khandle
,
383 ret
= wait_for_direct_io(ORANGEFS_IO_READ
, inode
, offset
, iter
,
384 count
, readahead_size
);
388 gossip_debug(GOSSIP_FILE_DEBUG
,
389 "%s(%pU): Value(%zd) returned.\n",
391 &orangefs_inode
->refn
.khandle
,
397 static ssize_t
orangefs_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
399 struct file
*file
= iocb
->ki_filp
;
400 loff_t pos
= *(&iocb
->ki_pos
);
403 BUG_ON(iocb
->private);
405 gossip_debug(GOSSIP_FILE_DEBUG
, "orangefs_file_read_iter\n");
407 g_orangefs_stats
.reads
++;
409 rc
= do_readv_writev(ORANGEFS_IO_READ
, file
, &pos
, iter
);
415 static ssize_t
orangefs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
417 struct file
*file
= iocb
->ki_filp
;
421 BUG_ON(iocb
->private);
423 gossip_debug(GOSSIP_FILE_DEBUG
, "orangefs_file_write_iter\n");
425 mutex_lock(&file
->f_mapping
->host
->i_mutex
);
427 /* Make sure generic_write_checks sees an up to date inode size. */
428 if (file
->f_flags
& O_APPEND
) {
429 rc
= orangefs_inode_getattr(file
->f_mapping
->host
,
430 ORANGEFS_ATTR_SYS_SIZE
, 0);
432 gossip_err("%s: orangefs_inode_getattr failed, rc:%zd:.\n",
438 if (file
->f_pos
> i_size_read(file
->f_mapping
->host
))
439 orangefs_i_size_write(file
->f_mapping
->host
, file
->f_pos
);
441 rc
= generic_write_checks(iocb
, iter
);
444 gossip_err("%s: generic_write_checks failed, rc:%zd:.\n",
450 * if we are appending, generic_write_checks would have updated
451 * pos to the end of the file, so we will wait till now to set
454 pos
= *(&iocb
->ki_pos
);
456 rc
= do_readv_writev(ORANGEFS_IO_WRITE
,
461 gossip_err("%s: do_readv_writev failed, rc:%zd:.\n",
467 g_orangefs_stats
.writes
++;
471 mutex_unlock(&file
->f_mapping
->host
->i_mutex
);
476 * Perform a miscellaneous operation on a file.
478 static long orangefs_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
484 gossip_debug(GOSSIP_FILE_DEBUG
,
485 "orangefs_ioctl: called with cmd %d\n",
489 * we understand some general ioctls on files, such as the immutable
492 if (cmd
== FS_IOC_GETFLAGS
) {
494 ret
= orangefs_inode_getxattr(file_inode(file
),
495 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX
,
496 "user.pvfs2.meta_hint",
498 if (ret
< 0 && ret
!= -ENODATA
)
500 else if (ret
== -ENODATA
)
503 gossip_debug(GOSSIP_FILE_DEBUG
,
504 "orangefs_ioctl: FS_IOC_GETFLAGS: %llu\n",
505 (unsigned long long)uval
);
506 return put_user(uval
, (int __user
*)arg
);
507 } else if (cmd
== FS_IOC_SETFLAGS
) {
509 if (get_user(uval
, (int __user
*)arg
))
512 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode
513 * is turned on for a file. The user is not allowed to turn
514 * on this bit, but the bit is present if the user first gets
515 * the flags and then updates the flags with some new
516 * settings. So, we ignore it in the following edit. bligon.
518 if ((uval
& ~ORANGEFS_MIRROR_FL
) &
519 (~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| FS_NOATIME_FL
))) {
520 gossip_err("orangefs_ioctl: the FS_IOC_SETFLAGS only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n");
524 gossip_debug(GOSSIP_FILE_DEBUG
,
525 "orangefs_ioctl: FS_IOC_SETFLAGS: %llu\n",
526 (unsigned long long)val
);
527 ret
= orangefs_inode_setxattr(file_inode(file
),
528 ORANGEFS_XATTR_NAME_DEFAULT_PREFIX
,
529 "user.pvfs2.meta_hint",
530 &val
, sizeof(val
), 0);
537 * Memory map a region of a file.
539 static int orangefs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
541 gossip_debug(GOSSIP_FILE_DEBUG
,
542 "orangefs_file_mmap: called on %s\n",
544 (char *)file
->f_path
.dentry
->d_name
.name
:
547 /* set the sequential readahead hint */
548 vma
->vm_flags
|= VM_SEQ_READ
;
549 vma
->vm_flags
&= ~VM_RAND_READ
;
551 /* Use readonly mmap since we cannot support writable maps. */
552 return generic_file_readonly_mmap(file
, vma
);
555 #define mapping_nrpages(idata) ((idata)->nrpages)
558 * Called to notify the module that there are no more references to
559 * this file (i.e. no processes have it open).
561 * \note Not called when each file is closed.
563 static int orangefs_file_release(struct inode
*inode
, struct file
*file
)
565 gossip_debug(GOSSIP_FILE_DEBUG
,
566 "orangefs_file_release: called on %s\n",
567 file
->f_path
.dentry
->d_name
.name
);
569 orangefs_flush_inode(inode
);
572 * remove all associated inode pages from the page cache and mmap
573 * readahead cache (if any); this forces an expensive refresh of
574 * data for the next caller of mmap (or 'get_block' accesses)
576 if (file
->f_path
.dentry
->d_inode
&&
577 file
->f_path
.dentry
->d_inode
->i_mapping
&&
578 mapping_nrpages(&file
->f_path
.dentry
->d_inode
->i_data
))
579 truncate_inode_pages(file
->f_path
.dentry
->d_inode
->i_mapping
,
585 * Push all data for a specific file onto permanent storage.
587 static int orangefs_fsync(struct file
*file
,
593 struct orangefs_inode_s
*orangefs_inode
=
594 ORANGEFS_I(file
->f_path
.dentry
->d_inode
);
595 struct orangefs_kernel_op_s
*new_op
= NULL
;
598 filemap_write_and_wait_range(file
->f_mapping
, start
, end
);
600 new_op
= op_alloc(ORANGEFS_VFS_OP_FSYNC
);
603 new_op
->upcall
.req
.fsync
.refn
= orangefs_inode
->refn
;
605 ret
= service_operation(new_op
,
607 get_interruptible_flag(file
->f_path
.dentry
->d_inode
));
609 gossip_debug(GOSSIP_FILE_DEBUG
,
610 "orangefs_fsync got return value of %d\n",
615 orangefs_flush_inode(file
->f_path
.dentry
->d_inode
);
620 * Change the file pointer position for an instance of an open file.
622 * \note If .llseek is overriden, we must acquire lock as described in
623 * Documentation/filesystems/Locking.
625 * Future upgrade could support SEEK_DATA and SEEK_HOLE but would
626 * require much changes to the FS
628 static loff_t
orangefs_file_llseek(struct file
*file
, loff_t offset
, int origin
)
631 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
634 gossip_err("orangefs_file_llseek: invalid inode (NULL)\n");
638 if (origin
== ORANGEFS_SEEK_END
) {
640 * revalidate the inode's file size.
641 * NOTE: We are only interested in file size here,
642 * so we set mask accordingly.
644 ret
= orangefs_inode_getattr(inode
, ORANGEFS_ATTR_SYS_SIZE
, 0);
646 gossip_debug(GOSSIP_FILE_DEBUG
,
647 "%s:%s:%d calling make bad inode\n",
651 orangefs_make_bad_inode(inode
);
656 gossip_debug(GOSSIP_FILE_DEBUG
,
657 "orangefs_file_llseek: offset is %ld | origin is %d"
658 " | inode size is %lu\n",
661 (unsigned long)file
->f_path
.dentry
->d_inode
->i_size
);
663 return generic_file_llseek(file
, offset
, origin
);
667 * Support local locks (locks that only this kernel knows about)
668 * if Orangefs was mounted -o local_lock.
670 static int orangefs_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
674 if (ORANGEFS_SB(filp
->f_inode
->i_sb
)->flags
& ORANGEFS_OPT_LOCAL_LOCK
) {
675 if (cmd
== F_GETLK
) {
677 posix_test_lock(filp
, fl
);
679 rc
= posix_lock_file(filp
, fl
, NULL
);
686 /** ORANGEFS implementation of VFS file operations */
687 const struct file_operations orangefs_file_operations
= {
688 .llseek
= orangefs_file_llseek
,
689 .read_iter
= orangefs_file_read_iter
,
690 .write_iter
= orangefs_file_write_iter
,
691 .lock
= orangefs_lock
,
692 .unlocked_ioctl
= orangefs_ioctl
,
693 .mmap
= orangefs_file_mmap
,
694 .open
= generic_file_open
,
695 .release
= orangefs_file_release
,
696 .fsync
= orangefs_fsync
,