From patchwork Sat Aug 18 13:24:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 959199 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41t1FQ4Psfz9s4v for ; Sat, 18 Aug 2018 23:31:54 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="cSWf8I5i"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 41t1FQ2pwXzF12M for ; Sat, 18 Aug 2018 23:31:54 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="cSWf8I5i"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=rasmusvillemoes.dk (client-ip=2a00:1450:4864:20::542; helo=mail-ed1-x542.google.com; envelope-from=linux@rasmusvillemoes.dk; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=rasmusvillemoes.dk header.i=@rasmusvillemoes.dk header.b="cSWf8I5i"; dkim-atps=neutral Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 41t15G6WfXzF0dp for ; Sat, 18 Aug 2018 23:24:46 +1000 (AEST) Received: by mail-ed1-x542.google.com with SMTP id e6-v6so5973180edr.2 for ; Sat, 18 Aug 2018 06:24:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id; bh=FV0dNHdRcB4ZhsnEc2fPA731UxnGGox6WwKOreslgto=; b=cSWf8I5iIpCx97/UujKEH5f4Pm0C7XXGUtPWnFGZU9GSyRcVBp2qOKaGIboDU+Sl+w 5ECYG43t986TYdBu/urRth/3/g7/TSTesCmV5AWASVMFPs0tNgZuB3pS7wYbpTrXeoee nE2uxnbQSt4yhH3oxCefGIJk83OnLq+7Pa8Zw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=FV0dNHdRcB4ZhsnEc2fPA731UxnGGox6WwKOreslgto=; b=cj6C3tNn8uUyjhu63pkbWHZdYstsPu44CgvCJkTzOP/HbPwsn2cmF2qsO+Qun/1QWa 2HvNBWDQ3nQCwmu7jHU+BBCMR1u2KIeRtuL+wwDyL2UP3XHSIH2TWpqBjP34a0N5rMQI idfbnVLyQ/z/ZLif9Ni1ZLhUDl4kFb0X9Y0KXqCY7Ae/4L6WHxvC8wun2ywg/2SCE12H XXXNipquka3mQgWCrCrXL43HSm+ufxYEuAfSUZBHN0BnBeNZOATM0dZcQ0F8zLX+uq2n do7xRR2Fz8WaWsmZRcNP3MBWbXVBgkvsNYgJs7+zZnIpD1jbgoBHNTME3ZQoIWBPTBCi 6Hsg== X-Gm-Message-State: AOUpUlEkipB1gOvDOuodC0k7arnv1EXzJK+O5YdssJJudvggxWJsPmxU nzv3BuW8GoCXzuWDNe6Y+EuEnQ== X-Google-Smtp-Source: AA+uWPw+UKhHcsh5t3gZC8ty9gStC7DIEPFZMt8/J29uy22Ne6sIfHjTNhfp3IGa4AJ3alXFKM/0QQ== X-Received: by 2002:aa7:c68b:: with SMTP id n11-v6mr47564358edq.122.1534598683408; Sat, 18 Aug 2018 06:24:43 -0700 (PDT) Received: from prevas-ravi.waoo.dk (dhcp-5-186-114-212.cgn.ip.fibianet.dk. [5.186.114.212]) by smtp.gmail.com with ESMTPSA id a11-v6sm2739032edn.95.2018.08.18.06.24.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 18 Aug 2018 06:24:42 -0700 (PDT) From: Rasmus Villemoes To: Jonathan Corbet , Alexander Viro Subject: [PATCH v2 1/8] seq_file: introduce seq_open_data helper Date: Sat, 18 Aug 2018 15:24:27 +0200 Message-Id: <20180818132434.9515-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.16.4 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andreas Dilger , linux-nfs@vger.kernel.org, Lorenzo Pieralisi , linux-doc@vger.kernel.org, Rasmus Villemoes , linux-kernel@vger.kernel.org, cluster-devel@redhat.com, Thierry Reding , linux-fsdevel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Alexey Dobriyan Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" There are quite a few callers of seq_open that could be simplified by setting the ->private member via the seq_open call instead of fetching file->private_data afterwards. Signed-off-by: Rasmus Villemoes Reviewed-by: Andy Shevchenko --- v2: - Fix some copy-pastos spotted by Andreas. - Ensure everybody hit by an example patch also gets this cover letter/introducing patch. - Include a few fs/ examples. I've just included a few examples of possible users of this helper, there are many more similar cases. As a bonus, 7/8 fix a potential NULL deref (if one believes that seq_open can actually fail). seq_open_private would have been a better name, but that one is already taken... Documentation/filesystems/seq_file.txt | 9 +++++---- fs/seq_file.c | 16 ++++++++++++---- include/linux/seq_file.h | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Documentation/filesystems/seq_file.txt b/Documentation/filesystems/seq_file.txt index 9de4303201e1..68571b8275d8 100644 --- a/Documentation/filesystems/seq_file.txt +++ b/Documentation/filesystems/seq_file.txt @@ -234,10 +234,11 @@ Here, the call to seq_open() takes the seq_operations structure we created before, and gets set up to iterate through the virtual file. On a successful open, seq_open() stores the struct seq_file pointer in -file->private_data. If you have an application where the same iterator can -be used for more than one file, you can store an arbitrary pointer in the -private field of the seq_file structure; that value can then be retrieved -by the iterator functions. +file->private_data. If you have an application where the same iterator +can be used for more than one file, you can store an arbitrary pointer +in the private field of the seq_file structure; that value can then be +retrieved by the iterator functions. Using the wrapper seq_open_data() +allows you to set the initial value for that field. There is also a wrapper function to seq_open() called seq_open_private(). It kmallocs a zero filled block of memory and stores a pointer to it in the diff --git a/fs/seq_file.c b/fs/seq_file.c index 4cc090b50cc5..c8c86660f6db 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -33,11 +33,12 @@ static void *seq_buf_alloc(unsigned long size) } /** - * seq_open - initialize sequential file + * seq_open_data - initialize sequential file * @file: file we initialize * @op: method table describing the sequence + * @data: initial value for ->private field * - * seq_open() sets @file, associating it with a sequence described + * seq_open_data() sets @file, associating it with a sequence described * by @op. @op->start() sets the iterator up and returns the first * element of sequence. @op->stop() shuts it down. @op->next() * returns the next element of sequence. @op->show() prints element @@ -45,10 +46,10 @@ static void *seq_buf_alloc(unsigned long size) * ERR_PTR(error). In the end of sequence they return %NULL. ->show() * returns 0 in case of success and negative number in case of error. * Returning SEQ_SKIP means "discard this element and move on". - * Note: seq_open() will allocate a struct seq_file and store its + * Note: seq_open_data() will allocate a struct seq_file and store its * pointer in @file->private_data. This pointer should not be modified. */ -int seq_open(struct file *file, const struct seq_operations *op) +int seq_open_data(struct file *file, const struct seq_operations *op, void *data) { struct seq_file *p; @@ -62,6 +63,7 @@ int seq_open(struct file *file, const struct seq_operations *op) mutex_init(&p->lock); p->op = op; + p->private = data; // No refcounting: the lifetime of 'p' is constrained // to the lifetime of the file. @@ -86,6 +88,12 @@ int seq_open(struct file *file, const struct seq_operations *op) file->f_mode &= ~FMODE_PWRITE; return 0; } +EXPORT_SYMBOL(seq_open_data); + +int seq_open(struct file *file, const struct seq_operations *op) +{ + return seq_open_data(file, op, NULL); +} EXPORT_SYMBOL(seq_open); static int traverse(struct seq_file *m, loff_t offset) diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index a121982af0f5..1142e39bfad2 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -107,6 +107,7 @@ void seq_pad(struct seq_file *m, char c); char *mangle_path(char *s, const char *p, const char *esc); int seq_open(struct file *, const struct seq_operations *); +int seq_open_data(struct file *, const struct seq_operations *, void *); ssize_t seq_read(struct file *, char __user *, size_t, loff_t *); loff_t seq_lseek(struct file *, loff_t, int); int seq_release(struct inode *, struct file *);