Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3786491ybi; Mon, 29 Jul 2019 12:36:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqwTIAefXjNNO5EoRXjxfmduRCGn7RBQLP3xM5IjndiIW4Z9NmFGiRxcSJiZZBXt9hkuUqcW X-Received: by 2002:a17:90a:9bca:: with SMTP id b10mr113221261pjw.90.1564428964260; Mon, 29 Jul 2019 12:36:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564428964; cv=none; d=google.com; s=arc-20160816; b=opCbPPvyTZC40vCD4vvfnFKbBU7HtP5Z/Wd7qIfwTZTbDvR4LnIhLKqBbXA2XtV2LY Za/f6dm1kbZbCXyLJDA+WCi5zA/Ui0fNOL9VwaUR+fBSnijRl00l/0DBh05+IbWy1as7 /6AVsr15rauTm8QXa+JODxSndSa1pO1EHdUAlBtc70QwaiLmZzWkpjJS16XbJJl+eOiT qIei3bthwET+u8yfTfpvvSEYh+9r1FnODb0CrA2Em73Zmd5bGR6ptMhZMQy89X9SUvVi yg+Kdom+7nwYGgdLGEZ3+uVOhPqwiKZnTwi6sFCa1ZE77yAt2HqTi+bQ/OTwiSkncizv VMeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Qg2/uU0h4wxWIM4vsfaSjMMuqi/+cWebTZi15aRw1zE=; b=LzbSwn87tT2jniAa9/yndY1QsDxZLZ+Lv45ZnKn85EvIBYySr1Y9A+KHeZF2u9naxT u/n0cKto0daU0rxsAQKjG7vA9GP7yVBQIiSZwNqcMUaaWOLHWOMZShcyWpdpCKjIwBSG 3ELipbomIRaynCSTVGirjuKIapoIjymzdJbrbWS9+BWmEsRtQUE8XDuqaAhbV1pwpQbB +Td7Uxg7DDZymYGDE0hxYkQmzIVwKtv6bH9wYGXgfInEgEy/HmYksPWTLcNwbI+pbP72 y56Wrrse1XDlWWCk8d3KmhKWXDHFBruMGW6n9WZtOyJbfGuKbhjlrj6S4amKNSJ2EbDU x4pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=osOxlIe9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g31si26753831pld.8.2019.07.29.12.35.49; Mon, 29 Jul 2019 12:36:04 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=osOxlIe9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730203AbfG2Tb5 (ORCPT + 99 others); Mon, 29 Jul 2019 15:31:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:44968 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728692AbfG2Tbz (ORCPT ); Mon, 29 Jul 2019 15:31:55 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0C05E217D6; Mon, 29 Jul 2019 19:31:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564428714; bh=2JuZ68chLQHkmtttpPeLALXBf11PAatz+Tf3pJxTQ94=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=osOxlIe9MM3yaId8ZrwRdJQ+o+7ipcGOoiiSbbjB2YqeV+2nzE/Zztss3k5gwtbrw 3VZqhnnC9wmgafI+GPqj8tpr8l6hk65hVPLfjQOgYFDzWcGTUXroYRWOzx7rm7L9Xt R772LWDcuGVJgoYwbe8JcarbA7Ccq1qDRW5voGEc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Harkes , Arnd Bergmann , Colin Ian King , Dan Carpenter , David Howells , Fabian Frederick , Mikko Rapeli , Sam Protsenko , Yann Droneaud , Zhouyang Jia , Andrew Morton , Linus Torvalds Subject: [PATCH 4.14 163/293] coda: pass the host file in vma->vm_file on mmap Date: Mon, 29 Jul 2019 21:20:54 +0200 Message-Id: <20190729190837.050620503@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190820.321094988@linuxfoundation.org> References: <20190729190820.321094988@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jan Harkes commit 7fa0a1da3dadfd9216df7745a1331fdaa0940d1c upstream. 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 Cc: Arnd Bergmann Cc: Colin Ian King Cc: Dan Carpenter Cc: David Howells Cc: Fabian Frederick Cc: Mikko Rapeli Cc: Sam Protsenko Cc: Yann Droneaud Cc: Zhouyang Jia Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/coda/file.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) --- a/fs/coda/file.c +++ b/fs/coda/file.c @@ -27,6 +27,13 @@ #include "coda_linux.h" #include "coda_int.h" +struct coda_vm_ops { + atomic_t refcnt; + struct file *coda_file; + const struct vm_operations_struct *host_vm_ops; + struct vm_operations_struct vm_ops; +}; + static ssize_t coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to) { @@ -61,6 +68,34 @@ coda_file_write_iter(struct kiocb *iocb, return ret; } +static void +coda_vm_open(struct vm_area_struct *vma) +{ + struct coda_vm_ops *cvm_ops = + container_of(vma->vm_ops, struct coda_vm_ops, vm_ops); + + atomic_inc(&cvm_ops->refcnt); + + if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open) + cvm_ops->host_vm_ops->open(vma); +} + +static void +coda_vm_close(struct vm_area_struct *vma) +{ + struct coda_vm_ops *cvm_ops = + container_of(vma->vm_ops, struct coda_vm_ops, vm_ops); + + if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close) + cvm_ops->host_vm_ops->close(vma); + + if (atomic_dec_and_test(&cvm_ops->refcnt)) { + vma->vm_ops = cvm_ops->host_vm_ops; + fput(cvm_ops->coda_file); + kfree(cvm_ops); + } +} + static int coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma) { @@ -68,6 +103,8 @@ coda_file_mmap(struct file *coda_file, s struct coda_inode_info *cii; struct file *host_file; struct inode *coda_inode, *host_inode; + struct coda_vm_ops *cvm_ops; + int ret; cfi = CODA_FTOC(coda_file); BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC); @@ -76,6 +113,13 @@ coda_file_mmap(struct file *coda_file, s if (!host_file->f_op->mmap) return -ENODEV; + if (WARN_ON(coda_file != vma->vm_file)) + return -EIO; + + cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL); + if (!cvm_ops) + return -ENOMEM; + coda_inode = file_inode(coda_file); host_inode = file_inode(host_file); @@ -89,6 +133,7 @@ coda_file_mmap(struct file *coda_file, s * the container file on us! */ else if (coda_inode->i_mapping != host_inode->i_mapping) { spin_unlock(&cii->c_lock); + kfree(cvm_ops); return -EBUSY; } @@ -97,7 +142,29 @@ coda_file_mmap(struct file *coda_file, s cfi->cfi_mapcount++; spin_unlock(&cii->c_lock); - return call_mmap(host_file, vma); + vma->vm_file = get_file(host_file); + ret = call_mmap(vma->vm_file, vma); + + if (ret) { + /* if call_mmap fails, our caller will put coda_file so we + * should drop the reference to the host_file that we got. + */ + fput(host_file); + kfree(cvm_ops); + } else { + /* here we add redirects for the open/close vm_operations */ + cvm_ops->host_vm_ops = vma->vm_ops; + if (vma->vm_ops) + cvm_ops->vm_ops = *vma->vm_ops; + + cvm_ops->vm_ops.open = coda_vm_open; + cvm_ops->vm_ops.close = coda_vm_close; + cvm_ops->coda_file = coda_file; + atomic_set(&cvm_ops->refcnt, 1); + + vma->vm_ops = &cvm_ops->vm_ops; + } + return ret; } int coda_open(struct inode *coda_inode, struct file *coda_file) @@ -207,4 +274,3 @@ const struct file_operations coda_file_o .fsync = coda_fsync, .splice_read = generic_file_splice_read, }; -