Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3312334ybt; Sat, 4 Jul 2020 12:42:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4/H+zILk59zj9lxGKpXkbAZFF1Rwyzijgx+jhEIChXqoBSZRW9zEe12Ix/JkNx4TccQQG X-Received: by 2002:a17:906:f104:: with SMTP id gv4mr38948940ejb.485.1593891779065; Sat, 04 Jul 2020 12:42:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593891779; cv=none; d=google.com; s=arc-20160816; b=Q3x80ufmSqCJPaXHcNnG5bdg/EyhkHER/OLIWO+hc+Z0+wngBxl8XaO/viGsMDZfoQ uizW/5t3ndqoOVzDoy21APldmQPT6Js6JI7HuB0h1bF0imxjjIQ6wR0eSsxjAZ830EZW EKNbJ9myY7jXFSfJ4YeKEfpALA7JvDOVpErNYDz/VE/LI6o0NF+u3HXNQXSK0OCiOYW8 fk0BsFbXVfoaKA0Ui87k8uxr9/e/2otAkK3FGfeAjCl7GpaQhqmevbWi1sIhdbWanJkR anSwrk32GPZzi0lFoxOIA9pjoW0VDEz0Y3GtcfuVqwYOBoBZdl+2Yao1FQxDyOnRhM+I OYmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=oYoqd/CUPo684lZ33nBT5H+rI2BWES+cDScLFKGmu2Y=; b=IsMEMiULSdlznS8yyBzZjUxEuL88GiMAvsZ2RxT41lU2MWVjv2RZUH6FqsHK4xf7QX dg7+e4yZDymt0cCxrU1Z+s+dQVN9Opc5IIU33CmSggghIG6ps9ncC1krKd3nys5EskOD TSdp0eL+Cdoiel3DW3YqxXjIJiW9AeVuYUe2Uo2nI8kWF8Rlji23aOLWFAH1H7gOCJrP Fucr3qRso3SyC76/gPn1vEyCMdntjQS7jCfAfcfh7mrdmq31qtnCS29qubFMy5zuW9ow 46Qm14HDTZ114QmPC5jLlJXW7ZUJ2bLgMsLml0drPn2i8x2WSq+cZ6+slO4B+MsJvevD 19QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=temperror (no key for signature) header.i=@szeredi.hu header.s=google header.b=ANB3jbE5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c2si9986537edq.228.2020.07.04.12.42.36; Sat, 04 Jul 2020 12:42:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=temperror (no key for signature) header.i=@szeredi.hu header.s=google header.b=ANB3jbE5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727835AbgGDTl1 (ORCPT + 99 others); Sat, 4 Jul 2020 15:41:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727831AbgGDTlW (ORCPT ); Sat, 4 Jul 2020 15:41:22 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3BA0C08C5E0 for ; Sat, 4 Jul 2020 12:41:21 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id y10so37874930eje.1 for ; Sat, 04 Jul 2020 12:41:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=szeredi.hu; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=oYoqd/CUPo684lZ33nBT5H+rI2BWES+cDScLFKGmu2Y=; b=ANB3jbE5Aespe1bV8GUSrufR+UgUOcLLAce0mIM6RWiw6ddEBfK4M4umN+gDfubuLQ Nkv6ZOnUULIggH34MXijLri4E67UdDtVYgNoi6Xp6EHGQgwkj0MrpFEGNmKuMCE7oaXy sVM/9FM1+ZIbdrCOCvBtEzrAS0Mh9st+EXtgg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=oYoqd/CUPo684lZ33nBT5H+rI2BWES+cDScLFKGmu2Y=; b=pnLyoiyl1XGfd/ls/fRzGpWWtsZV/2A7S1Rlle5jNaBIM94/57UvOszZAflbo1oS5J edvgOhIo1WA9LHTG17DY2zF585C7S3XSsOTVdGKxpR0Ts7mi+ESIbag5wkxlBDdmx5j4 6TVhih0lfIbSXyezDKl76lFVWLoi5WVan8vGCcIsWJhTg8k1Hg9tXFD/4w1rAOy1xjWM IRUv3im3sKQUjVEg3KM1ldebXOTC7MJ/QfG0Wm0k6iJ0Erulf9iLHxgf8Nk0Knd0pBol wXb2rVBky3qWjm50XR7fH7WLGfH2voEO8m0Z71dLKGQK1AmEd50RK1MP1QZIWAkcW/VJ 175A== X-Gm-Message-State: AOAM532ApH3dT4BeA6QGfzHinshKiJRLxgMJSk1Dp0kcst7iubhSifNf uLmxINEyhktYJc1TJX0Rys/ha9+wj7Jfp0IMtC4GdLP3TEZAKw== X-Received: by 2002:a17:906:1c05:: with SMTP id k5mr36355926ejg.320.1593891680498; Sat, 04 Jul 2020 12:41:20 -0700 (PDT) MIME-Version: 1.0 References: <20200704140250.423345-1-gregkh@linuxfoundation.org> <20200704140250.423345-2-gregkh@linuxfoundation.org> In-Reply-To: <20200704140250.423345-2-gregkh@linuxfoundation.org> From: Miklos Szeredi Date: Sat, 4 Jul 2020 21:41:09 +0200 Message-ID: Subject: Re: [PATCH 1/3] readfile: implement readfile syscall To: Greg Kroah-Hartman Cc: Al Viro , Michael Kerrisk , shuah@kernel.org, Linux API , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-man , linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jul 4, 2020 at 4:03 PM Greg Kroah-Hartman wrote: > > It's a tiny syscall, meant to allow a user to do a single "open this > file, read into this buffer, and close the file" all in a single shot. > > Should be good for reading "tiny" files like sysfs, procfs, and other > "small" files. > > There is no restarting the syscall, this is a "simple" syscall, with the > attempt to make reading "simple" files easier with less syscall > overhead. > > Cc: Alexander Viro > Signed-off-by: Greg Kroah-Hartman > --- > fs/open.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 50 insertions(+) > > diff --git a/fs/open.c b/fs/open.c > index 6cd48a61cda3..4469faa9379c 100644 > --- a/fs/open.c > +++ b/fs/open.c > @@ -1370,3 +1370,53 @@ int stream_open(struct inode *inode, struct file *filp) > } > > EXPORT_SYMBOL(stream_open); > + > +static struct file *readfile_open(int dfd, const char __user *filename, > + struct open_flags *op) > +{ > + struct filename *tmp; > + struct file *f; > + > + tmp = getname(filename); > + if (IS_ERR(tmp)) > + return (struct file *)tmp; > + > + f = do_filp_open(dfd, tmp, op); > + if (!IS_ERR(f)) > + fsnotify_open(f); > + > + putname(tmp); > + return f; > +} > + > +SYSCALL_DEFINE5(readfile, int, dfd, const char __user *, filename, > + char __user *, buffer, size_t, bufsize, int, flags) > +{ > + struct open_flags op; > + struct open_how how; > + struct file *file; > + loff_t pos = 0; > + int retval; > + > + /* only accept a small subset of O_ flags that make sense */ > + if ((flags & (O_NOFOLLOW | O_NOATIME)) != flags) > + return -EINVAL; > + > + /* add some needed flags to be able to open the file properly */ > + flags |= O_RDONLY | O_LARGEFILE; > + > + how = build_open_how(flags, 0000); > + retval = build_open_flags(&how, &op); > + if (retval) > + return retval; > + > + file = readfile_open(dfd, filename, &op); > + if (IS_ERR(file)) > + return PTR_ERR(file); > + > + retval = vfs_read(file, buffer, bufsize, &pos); > + > + filp_close(file, NULL); > + > + return retval; Manpage says: "doing the sequence of open() and then read() and then close()", which is exactly what it does. But then it goes on to say: "If the file is larger than the value provided in count then only count number of bytes will be copied into buf", which is only half true, it should be: "If the file is larger than the value provided in count then at most count number of bytes will be copied into buf", which is not a lot of information. And "If the size of file is smaller than the value provided in count then the whole file will be copied into buf", which is simply a lie; for example seq_file will happily return a smaller-than-PAGE_SIZE chunk if at least one record fits in there. You'll have a very hard time explaining that in the man page. So I think there are two possible ways forward: 1) just leave the first explanation (it's an open + read + close equivalent) and leave out the rest 2) add a loop around the vfs_read() in the code. I'd strongly prefer #2 because with the non-looping read it's impossible to detect whether the file was completely read or not, and that's just going to lead to surprises and bugs in userspace code. Thanks, Miklos