Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1789242pxb; Wed, 9 Feb 2022 04:39:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJz5TMzsEZuj+Uxt7grdsJCUZTOvJVqKrVyvQ+gdr7a6TJDOjg+QEAEVW2ssEB0khqYp42n4 X-Received: by 2002:a17:90b:1a8f:: with SMTP id ng15mr2652958pjb.198.1644410393102; Wed, 09 Feb 2022 04:39:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644410393; cv=none; d=google.com; s=arc-20160816; b=TUAyQEZ4qHPeV2+3Ry5wViX2Hzz2Kmcgu8NoX9Qxk0khKbn/oTzVkQ2Qm8nS+O6z2b aiwDAFYpDKu+V+bdaedkXVlpbR9rm15FIqK9K9jKoNXLM6wAQPUiGOloOfcdeLzqCtn6 53bjKatjfB7wZvtWNePZQb/s2nZ7u8ho6DGoZO/nwag3E+/fjUiZg5WneETvsOuY/Yfp I1FGHbLiy7K2D+4VBNS5X5kZ9EjGugBZ18zdkCCljgIbpBBh6A7pvhjCPUq9GB/rAMWr YDe/URvWkZVwcAZD25RPkGenZqxyEyPAaaCE9SpH5bR8GZEBOx7zvSzMmHFRrXee8olF Ir0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=t8ia/7VrGc3ppPQbetLrFX8Lx0V3siIiXFpK6+yn4tg=; b=YE0RBMc9rP9l4ytuC0SRPB+6ssFFleXgp/knD+miClJEkr6dHH8RvbWCKFdgKnFK4e j4KLqbsRMhKCDguhBvwUwFqzFzuCayEfwHAbA+XTXItVooeDinga7nP+Dg/WTMEA5k7I udcSS7S+0gikdABxV0p4MYJ+oADoZTNPdx/xbK8U6sA8gFa3hqXcW2JW/MiJ/RAdki7K +d1qrYAVuziGzn/bYXqBzOlGFeg2hps5myhyeYKYZwjrEmbXT2Bkn7DjSgFW1uSW7RVt 8lVeHBWGrzi+3u/Q9g/SSe4O1JZRT2AipWAHsEcLz5oGIqg7Lz0S6Q4dfenEJCXnhner RDcw== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id 34si15694102pgq.376.2022.02.09.04.39.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 04:39:53 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D9EE5E050270; Wed, 9 Feb 2022 02:25:22 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236616AbiBIGDB (ORCPT + 99 others); Wed, 9 Feb 2022 01:03:01 -0500 Received: from gmail-smtp-in.l.google.com ([23.128.96.19]:51908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234321AbiBIGBO (ORCPT ); Wed, 9 Feb 2022 01:01:14 -0500 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8D93C050CC1; Tue, 8 Feb 2022 22:01:17 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04423;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=15;SR=0;TI=SMTPD_---0V3zwJkJ_1644386472; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0V3zwJkJ_1644386472) by smtp.aliyun-inc.com(127.0.0.1); Wed, 09 Feb 2022 14:01:13 +0800 From: Jeffle Xu To: dhowells@redhat.com, linux-cachefs@redhat.com, xiang@kernel.org, chao@kernel.org, linux-erofs@lists.ozlabs.org Cc: torvalds@linux-foundation.org, gregkh@linuxfoundation.org, willy@infradead.org, linux-fsdevel@vger.kernel.org, joseph.qi@linux.alibaba.com, bo.liu@linux.alibaba.com, tao.peng@linux.alibaba.com, gerry@linux.alibaba.com, eguan@linux.alibaba.com, linux-kernel@vger.kernel.org Subject: [PATCH v3 03/22] cachefiles: extract generic function for daemon methods Date: Wed, 9 Feb 2022 14:00:49 +0800 Message-Id: <20220209060108.43051-4-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220209060108.43051-1-jefflexu@linux.alibaba.com> References: <20220209060108.43051-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ... so that the following new devnode can reuse most of the code when implementing its own methods. Signed-off-by: Jeffle Xu --- fs/cachefiles/daemon.c | 70 +++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c index 7ac04ee2c0a0..6b8d7c5bbe5d 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -78,6 +78,34 @@ static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = { { "", NULL } }; +static struct cachefiles_cache *cachefiles_daemon_open_cache(void) +{ + struct cachefiles_cache *cache; + + /* allocate a cache record */ + cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL); + if (cache) { + mutex_init(&cache->daemon_mutex); + init_waitqueue_head(&cache->daemon_pollwq); + INIT_LIST_HEAD(&cache->volumes); + INIT_LIST_HEAD(&cache->object_list); + spin_lock_init(&cache->object_list_lock); + + /* set default caching limits + * - limit at 1% free space and/or free files + * - cull below 5% free space and/or free files + * - cease culling above 7% free space and/or free files + */ + cache->frun_percent = 7; + cache->fcull_percent = 5; + cache->fstop_percent = 1; + cache->brun_percent = 7; + cache->bcull_percent = 5; + cache->bstop_percent = 1; + } + + return cache; +} /* * Prepare a cache for caching. @@ -96,31 +124,13 @@ static int cachefiles_daemon_open(struct inode *inode, struct file *file) if (xchg(&cachefiles_open, 1) == 1) return -EBUSY; - /* allocate a cache record */ - cache = kzalloc(sizeof(struct cachefiles_cache), GFP_KERNEL); + + cache = cachefiles_daemon_open_cache(); if (!cache) { cachefiles_open = 0; return -ENOMEM; } - mutex_init(&cache->daemon_mutex); - init_waitqueue_head(&cache->daemon_pollwq); - INIT_LIST_HEAD(&cache->volumes); - INIT_LIST_HEAD(&cache->object_list); - spin_lock_init(&cache->object_list_lock); - - /* set default caching limits - * - limit at 1% free space and/or free files - * - cull below 5% free space and/or free files - * - cease culling above 7% free space and/or free files - */ - cache->frun_percent = 7; - cache->fcull_percent = 5; - cache->fstop_percent = 1; - cache->brun_percent = 7; - cache->bcull_percent = 5; - cache->bstop_percent = 1; - file->private_data = cache; cache->cachefilesd = file; return 0; @@ -209,10 +219,11 @@ static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer, /* * Take a command from cachefilesd, parse it and act on it. */ -static ssize_t cachefiles_daemon_write(struct file *file, - const char __user *_data, - size_t datalen, - loff_t *pos) +static ssize_t cachefiles_daemon_do_write(struct file *file, + const char __user *_data, + size_t datalen, + loff_t *pos, + const struct cachefiles_daemon_cmd *cmds) { const struct cachefiles_daemon_cmd *cmd; struct cachefiles_cache *cache = file->private_data; @@ -261,7 +272,7 @@ static ssize_t cachefiles_daemon_write(struct file *file, } /* run the appropriate command handler */ - for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++) + for (cmd = cmds; cmd->name[0]; cmd++) if (strcmp(cmd->name, data) == 0) goto found_command; @@ -284,6 +295,15 @@ static ssize_t cachefiles_daemon_write(struct file *file, goto error; } +static ssize_t cachefiles_daemon_write(struct file *file, + const char __user *_data, + size_t datalen, + loff_t *pos) +{ + return cachefiles_daemon_do_write(file, _data, datalen, pos, + cachefiles_daemon_cmds); +} + /* * Poll for culling state * - use EPOLLOUT to indicate culling state -- 2.27.0