Skip to content

Commit fb89b45

Browse files
Dominique Martinetericvh
authored andcommitted
9P: introduction of a new cache=mmap model.
- Add cache=mmap option - Make mmap read-write while keeping it as synchronous as possible - Build writeback fid on mmap creation if it is writable Signed-off-by: Dominique Martinet <dominique.martinet@cea.fr> Signed-off-by: Eric Van Hensbergen <ericvh@gmail.com>
1 parent f94741f commit fb89b45

8 files changed

Lines changed: 179 additions & 19 deletions

File tree

‎fs/9p/v9fs.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum {
5656
/* Options that take no arguments */
5757
Opt_nodevmap,
5858
/* Cache options */
59-
Opt_cache_loose, Opt_fscache,
59+
Opt_cache_loose, Opt_fscache, Opt_mmap,
6060
/* Access options */
6161
Opt_access, Opt_posixacl,
6262
/* Error token */
@@ -74,6 +74,7 @@ static const match_table_t tokens = {
7474
{Opt_cache, "cache=%s"},
7575
{Opt_cache_loose, "loose"},
7676
{Opt_fscache, "fscache"},
77+
{Opt_mmap, "mmap"},
7778
{Opt_cachetag, "cachetag=%s"},
7879
{Opt_access, "access=%s"},
7980
{Opt_posixacl, "posixacl"},
@@ -91,6 +92,9 @@ static int get_cache_mode(char *s)
9192
} else if (!strcmp(s, "fscache")) {
9293
version = CACHE_FSCACHE;
9394
p9_debug(P9_DEBUG_9P, "Cache mode: fscache\n");
95+
} else if (!strcmp(s, "mmap")) {
96+
version = CACHE_MMAP;
97+
p9_debug(P9_DEBUG_9P, "Cache mode: mmap\n");
9498
} else if (!strcmp(s, "none")) {
9599
version = CACHE_NONE;
96100
p9_debug(P9_DEBUG_9P, "Cache mode: none\n");
@@ -220,6 +224,9 @@ static int v9fs_parse_options(struct v9fs_session_info *v9ses, char *opts)
220224
case Opt_fscache:
221225
v9ses->cache = CACHE_FSCACHE;
222226
break;
227+
case Opt_mmap:
228+
v9ses->cache = CACHE_MMAP;
229+
break;
223230
case Opt_cachetag:
224231
#ifdef CONFIG_9P_FSCACHE
225232
v9ses->cachetag = match_strdup(&args[0]);

‎fs/9p/v9fs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ enum p9_session_flags {
6464

6565
enum p9_cache_modes {
6666
CACHE_NONE,
67+
CACHE_MMAP,
6768
CACHE_LOOSE,
6869
CACHE_FSCACHE,
6970
};

‎fs/9p/v9fs_vfs.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ extern const struct dentry_operations v9fs_dentry_operations;
5050
extern const struct dentry_operations v9fs_cached_dentry_operations;
5151
extern const struct file_operations v9fs_cached_file_operations;
5252
extern const struct file_operations v9fs_cached_file_operations_dotl;
53+
extern const struct file_operations v9fs_mmap_file_operations;
54+
extern const struct file_operations v9fs_mmap_file_operations_dotl;
5355
extern struct kmem_cache *v9fs_inode_cache;
5456

5557
struct inode *v9fs_alloc_inode(struct super_block *sb);

‎fs/9p/vfs_addr.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
202202
{
203203
int retval;
204204

205+
p9_debug(P9_DEBUG_VFS, "page %p\n", page);
206+
205207
retval = v9fs_vfs_writepage_locked(page);
206208
if (retval < 0) {
207209
if (retval == -EAGAIN) {
@@ -282,6 +284,9 @@ static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
282284
pgoff_t index = pos >> PAGE_CACHE_SHIFT;
283285
struct inode *inode = mapping->host;
284286

287+
288+
p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
289+
285290
v9inode = V9FS_I(inode);
286291
start:
287292
page = grab_cache_page_write_begin(mapping, index, flags);
@@ -312,6 +317,8 @@ static int v9fs_write_end(struct file *filp, struct address_space *mapping,
312317
loff_t last_pos = pos + copied;
313318
struct inode *inode = page->mapping->host;
314319

320+
p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
321+
315322
if (unlikely(copied < len)) {
316323
/*
317324
* zero out the rest of the area

‎fs/9p/vfs_file.c‎

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "cache.h"
4646

4747
static 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;
111113
out_error:
@@ -579,17 +581,55 @@ int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
579581
}
580582

581583
static 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+
593633
static int
594634
v9fs_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+
661717
static ssize_t
662718
v9fs_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+
731833
static 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

738847
const 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+
};

‎fs/9p/vfs_inode.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,22 @@ int v9fs_init_inode(struct v9fs_session_info *v9ses,
299299
case S_IFREG:
300300
if (v9fs_proto_dotl(v9ses)) {
301301
inode->i_op = &v9fs_file_inode_operations_dotl;
302-
if (v9ses->cache)
302+
if (v9ses->cache == CACHE_LOOSE ||
303+
v9ses->cache == CACHE_FSCACHE)
303304
inode->i_fop =
304305
&v9fs_cached_file_operations_dotl;
306+
else if (v9ses->cache == CACHE_MMAP)
307+
inode->i_fop = &v9fs_mmap_file_operations_dotl;
305308
else
306309
inode->i_fop = &v9fs_file_operations_dotl;
307310
} else {
308311
inode->i_op = &v9fs_file_inode_operations;
309-
if (v9ses->cache)
310-
inode->i_fop = &v9fs_cached_file_operations;
312+
if (v9ses->cache == CACHE_LOOSE ||
313+
v9ses->cache == CACHE_FSCACHE)
314+
inode->i_fop =
315+
&v9fs_cached_file_operations;
316+
else if (v9ses->cache == CACHE_MMAP)
317+
inode->i_fop = &v9fs_mmap_file_operations;
311318
else
312319
inode->i_fop = &v9fs_file_operations;
313320
}
@@ -810,7 +817,7 @@ struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
810817
* unlink. For cached mode create calls request for new
811818
* inode. But with cache disabled, lookup should do this.
812819
*/
813-
if (v9ses->cache)
820+
if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
814821
inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
815822
else
816823
inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
@@ -876,7 +883,8 @@ v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
876883
v9fs_invalidate_inode_attr(dir);
877884
v9inode = V9FS_I(dentry->d_inode);
878885
mutex_lock(&v9inode->v_mutex);
879-
if (v9ses->cache && !v9inode->writeback_fid &&
886+
if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
887+
!v9inode->writeback_fid &&
880888
((flags & O_ACCMODE) != O_RDONLY)) {
881889
/*
882890
* clone a fid and add it to writeback_fid
@@ -899,7 +907,7 @@ v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
899907
goto error;
900908

901909
file->private_data = fid;
902-
if (v9ses->cache)
910+
if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
903911
v9fs_cache_inode_set_cookie(dentry->d_inode, file);
904912

905913
*opened |= FILE_CREATED;
@@ -1477,7 +1485,7 @@ int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
14771485
*/
14781486
i_size = inode->i_size;
14791487
v9fs_stat2inode(st, inode, inode->i_sb);
1480-
if (v9ses->cache)
1488+
if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
14811489
inode->i_size = i_size;
14821490
spin_unlock(&inode->i_lock);
14831491
out:

‎fs/9p/vfs_inode_dotl.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
330330

331331
v9inode = V9FS_I(inode);
332332
mutex_lock(&v9inode->v_mutex);
333-
if (v9ses->cache && !v9inode->writeback_fid &&
333+
if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
334+
!v9inode->writeback_fid &&
334335
((flags & O_ACCMODE) != O_RDONLY)) {
335336
/*
336337
* clone a fid and add it to writeback_fid
@@ -353,7 +354,7 @@ v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
353354
if (err)
354355
goto err_clunk_old_fid;
355356
file->private_data = ofid;
356-
if (v9ses->cache)
357+
if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
357358
v9fs_cache_inode_set_cookie(inode, file);
358359
*opened |= FILE_CREATED;
359360
out:
@@ -710,7 +711,7 @@ v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
710711
}
711712

712713
v9fs_invalidate_inode_attr(dir);
713-
if (v9ses->cache) {
714+
if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
714715
/* Now walk from the parent so we can get an unopened fid. */
715716
fid = p9_client_walk(dfid, 1, &name, 1);
716717
if (IS_ERR(fid)) {
@@ -965,7 +966,7 @@ int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
965966
*/
966967
i_size = inode->i_size;
967968
v9fs_stat2inode_dotl(st, inode);
968-
if (v9ses->cache)
969+
if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
969970
inode->i_size = i_size;
970971
spin_unlock(&inode->i_lock);
971972
out:

0 commit comments

Comments
 (0)