Skip to content

Commit 7fa0a1d

Browse files
jaharkestorvalds
authored andcommitted
coda: pass the host file in vma->vm_file on mmap
Patch series "Coda updates". The following patch series is a collection of various fixes for Coda, most of which were collected from linux-fsdevel or linux-kernel but which have as yet not found their way upstream. This patch (of 22): Various file systems expect that vma->vm_file points at their own file handle, several use file_inode(vma->vm_file) to get at their inode or use vma->vm_file->private_data. However the way Coda wrapped mmap on a host file broke this assumption, vm_file was still pointing at the Coda file and the host file systems would scribble over Coda's inode and private file data. This patch fixes the incorrect expectation and wraps vm_ops->open and vm_ops->close to allow Coda to track when the vm_area_struct is destroyed so we still release the reference on the Coda file handle at the right time. Link: http://lkml.kernel.org/r/0e850c6e59c0b147dc2dcd51a3af004c948c3697.1558117389.git.jaharkes@cs.cmu.edu Signed-off-by: Jan Harkes <jaharkes@cs.cmu.edu> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Colin Ian King <colin.king@canonical.com> Cc: Dan Carpenter <dan.carpenter@oracle.com> Cc: David Howells <dhowells@redhat.com> Cc: Fabian Frederick <fabf@skynet.be> Cc: Mikko Rapeli <mikko.rapeli@iki.fi> Cc: Sam Protsenko <semen.protsenko@linaro.org> Cc: Yann Droneaud <ydroneaud@opteya.com> Cc: Zhouyang Jia <jiazhouyang09@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b98cca4 commit 7fa0a1d

1 file changed

Lines changed: 68 additions & 2 deletions

File tree

‎fs/coda/file.c‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
#include "coda_linux.h"
2828
#include "coda_int.h"
2929

30+
struct coda_vm_ops {
31+
atomic_t refcnt;
32+
struct file *coda_file;
33+
const struct vm_operations_struct *host_vm_ops;
34+
struct vm_operations_struct vm_ops;
35+
};
36+
3037
static ssize_t
3138
coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
3239
{
@@ -61,13 +68,43 @@ coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
6168
return ret;
6269
}
6370

71+
static void
72+
coda_vm_open(struct vm_area_struct *vma)
73+
{
74+
struct coda_vm_ops *cvm_ops =
75+
container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
76+
77+
atomic_inc(&cvm_ops->refcnt);
78+
79+
if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open)
80+
cvm_ops->host_vm_ops->open(vma);
81+
}
82+
83+
static void
84+
coda_vm_close(struct vm_area_struct *vma)
85+
{
86+
struct coda_vm_ops *cvm_ops =
87+
container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
88+
89+
if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close)
90+
cvm_ops->host_vm_ops->close(vma);
91+
92+
if (atomic_dec_and_test(&cvm_ops->refcnt)) {
93+
vma->vm_ops = cvm_ops->host_vm_ops;
94+
fput(cvm_ops->coda_file);
95+
kfree(cvm_ops);
96+
}
97+
}
98+
6499
static int
65100
coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
66101
{
67102
struct coda_file_info *cfi;
68103
struct coda_inode_info *cii;
69104
struct file *host_file;
70105
struct inode *coda_inode, *host_inode;
106+
struct coda_vm_ops *cvm_ops;
107+
int ret;
71108

72109
cfi = CODA_FTOC(coda_file);
73110
BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
@@ -76,6 +113,13 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
76113
if (!host_file->f_op->mmap)
77114
return -ENODEV;
78115

116+
if (WARN_ON(coda_file != vma->vm_file))
117+
return -EIO;
118+
119+
cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL);
120+
if (!cvm_ops)
121+
return -ENOMEM;
122+
79123
coda_inode = file_inode(coda_file);
80124
host_inode = file_inode(host_file);
81125

@@ -89,6 +133,7 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
89133
* the container file on us! */
90134
else if (coda_inode->i_mapping != host_inode->i_mapping) {
91135
spin_unlock(&cii->c_lock);
136+
kfree(cvm_ops);
92137
return -EBUSY;
93138
}
94139

@@ -97,7 +142,29 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
97142
cfi->cfi_mapcount++;
98143
spin_unlock(&cii->c_lock);
99144

100-
return call_mmap(host_file, vma);
145+
vma->vm_file = get_file(host_file);
146+
ret = call_mmap(vma->vm_file, vma);
147+
148+
if (ret) {
149+
/* if call_mmap fails, our caller will put coda_file so we
150+
* should drop the reference to the host_file that we got.
151+
*/
152+
fput(host_file);
153+
kfree(cvm_ops);
154+
} else {
155+
/* here we add redirects for the open/close vm_operations */
156+
cvm_ops->host_vm_ops = vma->vm_ops;
157+
if (vma->vm_ops)
158+
cvm_ops->vm_ops = *vma->vm_ops;
159+
160+
cvm_ops->vm_ops.open = coda_vm_open;
161+
cvm_ops->vm_ops.close = coda_vm_close;
162+
cvm_ops->coda_file = coda_file;
163+
atomic_set(&cvm_ops->refcnt, 1);
164+
165+
vma->vm_ops = &cvm_ops->vm_ops;
166+
}
167+
return ret;
101168
}
102169

103170
int coda_open(struct inode *coda_inode, struct file *coda_file)
@@ -207,4 +274,3 @@ const struct file_operations coda_file_operations = {
207274
.fsync = coda_fsync,
208275
.splice_read = generic_file_splice_read,
209276
};
210-

0 commit comments

Comments
 (0)