xref: /kernel/linux/linux-5.10/fs/fscache/netfs.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* FS-Cache netfs (client) registration
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define FSCACHE_DEBUG_LEVEL COOKIE
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include "internal.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * register a network filesystem for caching
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ciint __fscache_register_netfs(struct fscache_netfs *netfs)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	struct fscache_cookie *candidate, *cookie;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	_enter("{%s}", netfs->name);
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	/* allocate a cookie for the primary index */
238c2ecf20Sopenharmony_ci	candidate = fscache_alloc_cookie(&fscache_fsdef_index,
248c2ecf20Sopenharmony_ci					 &fscache_fsdef_netfs_def,
258c2ecf20Sopenharmony_ci					 netfs->name, strlen(netfs->name),
268c2ecf20Sopenharmony_ci					 &netfs->version, sizeof(netfs->version),
278c2ecf20Sopenharmony_ci					 netfs, 0);
288c2ecf20Sopenharmony_ci	if (!candidate) {
298c2ecf20Sopenharmony_ci		_leave(" = -ENOMEM");
308c2ecf20Sopenharmony_ci		return -ENOMEM;
318c2ecf20Sopenharmony_ci	}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	candidate->flags = 1 << FSCACHE_COOKIE_ENABLED;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	/* check the netfs type is not already present */
368c2ecf20Sopenharmony_ci	cookie = fscache_hash_cookie(candidate);
378c2ecf20Sopenharmony_ci	if (!cookie)
388c2ecf20Sopenharmony_ci		goto already_registered;
398c2ecf20Sopenharmony_ci	if (cookie != candidate) {
408c2ecf20Sopenharmony_ci		trace_fscache_cookie(candidate, fscache_cookie_discard, 1);
418c2ecf20Sopenharmony_ci		fscache_free_cookie(candidate);
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	fscache_cookie_get(cookie->parent, fscache_cookie_get_register_netfs);
458c2ecf20Sopenharmony_ci	atomic_inc(&cookie->parent->n_children);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	netfs->primary_index = cookie;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	pr_notice("Netfs '%s' registered for caching\n", netfs->name);
508c2ecf20Sopenharmony_ci	trace_fscache_netfs(netfs);
518c2ecf20Sopenharmony_ci	_leave(" = 0");
528c2ecf20Sopenharmony_ci	return 0;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cialready_registered:
558c2ecf20Sopenharmony_ci	fscache_cookie_put(candidate, fscache_cookie_put_dup_netfs);
568c2ecf20Sopenharmony_ci	_leave(" = -EEXIST");
578c2ecf20Sopenharmony_ci	return -EEXIST;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__fscache_register_netfs);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * unregister a network filesystem from the cache
638c2ecf20Sopenharmony_ci * - all cookies must have been released first
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_civoid __fscache_unregister_netfs(struct fscache_netfs *netfs)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	_enter("{%s.%u}", netfs->name, netfs->version);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	fscache_relinquish_cookie(netfs->primary_index, NULL, false);
708c2ecf20Sopenharmony_ci	pr_notice("Netfs '%s' unregistered from caching\n", netfs->name);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	_leave("");
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__fscache_unregister_netfs);
75