4545#include "cache.h"
4646
4747static const struct vm_operations_struct v9fs_file_vm_ops ;
48+ static const struct vm_operations_struct v9fs_mmap_file_vm_ops ;
4849
4950/**
5051 * v9fs_file_open - open a file (or directory)
@@ -87,7 +88,8 @@ int v9fs_file_open(struct inode *inode, struct file *file)
8788
8889 file -> private_data = fid ;
8990 mutex_lock (& v9inode -> v_mutex );
90- if (v9ses -> cache && !v9inode -> writeback_fid &&
91+ if ((v9ses -> cache == CACHE_LOOSE || v9ses -> cache == CACHE_FSCACHE ) &&
92+ !v9inode -> writeback_fid &&
9193 ((file -> f_flags & O_ACCMODE ) != O_RDONLY )) {
9294 /*
9395 * clone a fid and add it to writeback_fid
@@ -105,7 +107,7 @@ int v9fs_file_open(struct inode *inode, struct file *file)
105107 v9inode -> writeback_fid = (void * ) fid ;
106108 }
107109 mutex_unlock (& v9inode -> v_mutex );
108- if (v9ses -> cache )
110+ if (v9ses -> cache == CACHE_LOOSE || v9ses -> cache == CACHE_FSCACHE )
109111 v9fs_cache_inode_set_cookie (inode , file );
110112 return 0 ;
111113out_error :
@@ -579,17 +581,55 @@ int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
579581}
580582
581583static int
582- v9fs_file_mmap (struct file * file , struct vm_area_struct * vma )
584+ v9fs_file_mmap (struct file * filp , struct vm_area_struct * vma )
583585{
584586 int retval ;
585587
586- retval = generic_file_mmap (file , vma );
588+
589+ retval = generic_file_mmap (filp , vma );
587590 if (!retval )
588591 vma -> vm_ops = & v9fs_file_vm_ops ;
589592
590593 return retval ;
591594}
592595
596+ static int
597+ v9fs_mmap_file_mmap (struct file * filp , struct vm_area_struct * vma )
598+ {
599+ int retval ;
600+ struct inode * inode ;
601+ struct v9fs_inode * v9inode ;
602+ struct p9_fid * fid ;
603+
604+ inode = file_inode (filp );
605+ v9inode = V9FS_I (inode );
606+ mutex_lock (& v9inode -> v_mutex );
607+ if (!v9inode -> writeback_fid &&
608+ (vma -> vm_flags & VM_WRITE )) {
609+ /*
610+ * clone a fid and add it to writeback_fid
611+ * we do it during mmap instead of
612+ * page dirty time via write_begin/page_mkwrite
613+ * because we want write after unlink usecase
614+ * to work.
615+ */
616+ fid = v9fs_writeback_fid (filp -> f_path .dentry );
617+ if (IS_ERR (fid )) {
618+ retval = PTR_ERR (fid );
619+ mutex_unlock (& v9inode -> v_mutex );
620+ return retval ;
621+ }
622+ v9inode -> writeback_fid = (void * ) fid ;
623+ }
624+ mutex_unlock (& v9inode -> v_mutex );
625+
626+ retval = generic_file_mmap (filp , vma );
627+ if (!retval )
628+ vma -> vm_ops = & v9fs_mmap_file_vm_ops ;
629+
630+ return retval ;
631+ }
632+
593633static int
594634v9fs_vm_page_mkwrite (struct vm_area_struct * vma , struct vm_fault * vmf )
595635{
@@ -658,6 +698,22 @@ v9fs_cached_file_read(struct file *filp, char __user *data, size_t count,
658698 return do_sync_read (filp , data , count , offset );
659699}
660700
701+ /**
702+ * v9fs_mmap_file_read - read from a file
703+ * @filp: file pointer to read
704+ * @udata: user data buffer to read data into
705+ * @count: size of buffer
706+ * @offset: offset at which to read data
707+ *
708+ */
709+ static ssize_t
710+ v9fs_mmap_file_read (struct file * filp , char __user * data , size_t count ,
711+ loff_t * offset )
712+ {
713+ /* TODO: Check if there are dirty pages */
714+ return v9fs_file_read (filp , data , count , offset );
715+ }
716+
661717static ssize_t
662718v9fs_direct_write (struct file * filp , const char __user * data ,
663719 size_t count , loff_t * offsetp )
@@ -728,12 +784,65 @@ v9fs_cached_file_write(struct file *filp, const char __user * data,
728784 return do_sync_write (filp , data , count , offset );
729785}
730786
787+
788+ /**
789+ * v9fs_mmap_file_write - write to a file
790+ * @filp: file pointer to write
791+ * @data: data buffer to write data from
792+ * @count: size of buffer
793+ * @offset: offset at which to write data
794+ *
795+ */
796+ static ssize_t
797+ v9fs_mmap_file_write (struct file * filp , const char __user * data ,
798+ size_t count , loff_t * offset )
799+ {
800+ /*
801+ * TODO: invalidate mmaps on filp's inode between
802+ * offset and offset+count
803+ */
804+ return v9fs_file_write (filp , data , count , offset );
805+ }
806+
807+ static void v9fs_mmap_vm_close (struct vm_area_struct * vma )
808+ {
809+ struct inode * inode ;
810+
811+ struct writeback_control wbc = {
812+ .nr_to_write = LONG_MAX ,
813+ .sync_mode = WB_SYNC_ALL ,
814+ .range_start = vma -> vm_pgoff * PAGE_SIZE ,
815+ /* absolute end, byte at end included */
816+ .range_end = vma -> vm_pgoff * PAGE_SIZE +
817+ (vma -> vm_end - vma -> vm_start - 1 ),
818+ };
819+
820+
821+ p9_debug (P9_DEBUG_VFS , "9p VMA close, %p, flushing" , vma );
822+
823+ inode = file_inode (vma -> vm_file );
824+
825+ if (!mapping_cap_writeback_dirty (inode -> i_mapping ))
826+ wbc .nr_to_write = 0 ;
827+
828+ might_sleep ();
829+ sync_inode (inode , & wbc );
830+ }
831+
832+
731833static const struct vm_operations_struct v9fs_file_vm_ops = {
732834 .fault = filemap_fault ,
733835 .page_mkwrite = v9fs_vm_page_mkwrite ,
734836 .remap_pages = generic_file_remap_pages ,
735837};
736838
839+ static const struct vm_operations_struct v9fs_mmap_file_vm_ops = {
840+ .close = v9fs_mmap_vm_close ,
841+ .fault = filemap_fault ,
842+ .page_mkwrite = v9fs_vm_page_mkwrite ,
843+ .remap_pages = generic_file_remap_pages ,
844+ };
845+
737846
738847const struct file_operations v9fs_cached_file_operations = {
739848 .llseek = generic_file_llseek ,
@@ -784,3 +893,26 @@ const struct file_operations v9fs_file_operations_dotl = {
784893 .mmap = generic_file_readonly_mmap ,
785894 .fsync = v9fs_file_fsync_dotl ,
786895};
896+
897+ const struct file_operations v9fs_mmap_file_operations = {
898+ .llseek = generic_file_llseek ,
899+ .read = v9fs_mmap_file_read ,
900+ .write = v9fs_mmap_file_write ,
901+ .open = v9fs_file_open ,
902+ .release = v9fs_dir_release ,
903+ .lock = v9fs_file_lock ,
904+ .mmap = v9fs_mmap_file_mmap ,
905+ .fsync = v9fs_file_fsync ,
906+ };
907+
908+ const struct file_operations v9fs_mmap_file_operations_dotl = {
909+ .llseek = generic_file_llseek ,
910+ .read = v9fs_mmap_file_read ,
911+ .write = v9fs_mmap_file_write ,
912+ .open = v9fs_file_open ,
913+ .release = v9fs_dir_release ,
914+ .lock = v9fs_file_lock_dotl ,
915+ .flock = v9fs_file_flock_dotl ,
916+ .mmap = v9fs_mmap_file_mmap ,
917+ .fsync = v9fs_file_fsync_dotl ,
918+ };
0 commit comments