fscache.c 6.0 KB
Newer Older
S
Steve French 已提交
1
// SPDX-License-Identifier: LGPL-2.1
2
/*
3
 *   CIFS filesystem cache interface
4 5
 *
 *   Copyright (c) 2010 Novell, Inc.
6
 *   Author(s): Suresh Jayaraman <sjayaraman@suse.de>
7 8 9 10 11
 *
 */
#include "fscache.h"
#include "cifsglob.h"
#include "cifs_debug.h"
12
#include "cifs_fs_sb.h"
13
#include "cifsproto.h"
14

15 16 17
static void cifs_fscache_fill_volume_coherency(
	struct cifs_tcon *tcon,
	struct cifs_fscache_volume_coherency_data *cd)
18
{
19 20 21 22
	memset(cd, 0, sizeof(*cd));
	cd->resource_id		= cpu_to_le64(tcon->resource_id);
	cd->vol_create_time	= tcon->vol_create_time;
	cd->vol_serial_number	= cpu_to_le32(tcon->vol_serial_number);
23 24
}

25
int cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
26
{
27
	struct cifs_fscache_volume_coherency_data cd;
28
	struct TCP_Server_Info *server = tcon->ses->server;
29 30 31
	struct fscache_volume *vcookie;
	const struct sockaddr *sa = (struct sockaddr *)&server->dstaddr;
	size_t slen, i;
32
	char *sharename;
33 34
	char *key;
	int ret = -ENOMEM;
35

36 37 38 39 40 41 42 43 44 45 46
	tcon->fscache = NULL;
	switch (sa->sa_family) {
	case AF_INET:
	case AF_INET6:
		break;
	default:
		cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
		return -EINVAL;
	}

	memset(&key, 0, sizeof(key));
47

48
	sharename = extract_sharename(tcon->tree_name);
49 50
	if (IS_ERR(sharename)) {
		cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
51
		return -EINVAL;
52
	}
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	slen = strlen(sharename);
	for (i = 0; i < slen; i++)
		if (sharename[i] == '/')
			sharename[i] = ';';

	key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
	if (!key)
		goto out;

	cifs_fscache_fill_volume_coherency(tcon, &cd);
	vcookie = fscache_acquire_volume(key,
					 NULL, /* preferred_cache */
					 &cd, sizeof(cd));
	cifs_dbg(FYI, "%s: (%s/0x%p)\n", __func__, key, vcookie);
	if (IS_ERR(vcookie)) {
		if (vcookie != ERR_PTR(-EBUSY)) {
			ret = PTR_ERR(vcookie);
			goto out_2;
		}
		pr_err("Cache volume key already in use (%s)\n", key);
		vcookie = NULL;
	}
76

77 78 79 80 81
	tcon->fscache = vcookie;
	ret = 0;
out_2:
	kfree(key);
out:
82
	kfree(sharename);
83
	return ret;
84 85
}

86
void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
87
{
88
	struct cifs_fscache_volume_coherency_data cd;
89

90
	cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
91

92 93 94
	cifs_fscache_fill_volume_coherency(tcon, &cd);
	fscache_relinquish_volume(tcon->fscache, &cd, false);
	tcon->fscache = NULL;
95 96
}

97
void cifs_fscache_get_inode_cookie(struct inode *inode)
98
{
99
	struct cifs_fscache_inode_coherency_data cd;
100 101
	struct cifsInodeInfo *cifsi = CIFS_I(inode);
	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
102
	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
103

104
	cifs_fscache_fill_coherency(&cifsi->netfs.inode, &cd);
105

106
	cifsi->netfs.cache =
107 108 109
		fscache_acquire_cookie(tcon->fscache, 0,
				       &cifsi->uniqueid, sizeof(cifsi->uniqueid),
				       &cd, sizeof(cd),
110
				       i_size_read(&cifsi->netfs.inode));
111 112
}

113
void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
114
{
115 116 117
	if (update) {
		struct cifs_fscache_inode_coherency_data cd;
		loff_t i_size = i_size_read(inode);
118

119 120 121 122
		cifs_fscache_fill_coherency(inode, &cd);
		fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
	} else {
		fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
123 124 125
	}
}

126
void cifs_fscache_release_inode_cookie(struct inode *inode)
127 128
{
	struct cifsInodeInfo *cifsi = CIFS_I(inode);
D
David Howells 已提交
129
	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
130

D
David Howells 已提交
131 132 133
	if (cookie) {
		cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
		fscache_relinquish_cookie(cookie, false);
134
		cifsi->netfs.cache = NULL;
135 136
	}
}
137

138
/*
139
 * Fallback page reading interface.
140
 */
141
static int fscache_fallback_read_page(struct inode *inode, struct page *page)
142
{
143 144 145
	struct netfs_cache_resources cres;
	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
	struct iov_iter iter;
146
	struct bio_vec bvec;
147 148 149
	int ret;

	memset(&cres, 0, sizeof(cres));
150 151
	bvec_set_page(&bvec, page, PAGE_SIZE, 0);
	iov_iter_bvec(&iter, ITER_DEST, &bvec, 1, PAGE_SIZE);
152 153 154 155 156 157 158 159 160

	ret = fscache_begin_read_operation(&cres, cookie);
	if (ret < 0)
		return ret;

	ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
			   NULL, NULL);
	fscache_end_operation(&cres);
	return ret;
161 162 163
}

/*
164
 * Fallback page writing interface.
165
 */
166 167
static int fscache_fallback_write_pages(struct inode *inode, loff_t start, size_t len,
					bool no_space_allocated_yet)
168
{
169 170 171 172 173 174
	struct netfs_cache_resources cres;
	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
	struct iov_iter iter;
	int ret;

	memset(&cres, 0, sizeof(cres));
175
	iov_iter_xarray(&iter, ITER_SOURCE, &inode->i_mapping->i_pages, start, len);
176 177 178 179 180 181 182 183

	ret = fscache_begin_write_operation(&cres, cookie);
	if (ret < 0)
		return ret;

	ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
				      no_space_allocated_yet);
	if (ret == 0)
184
		ret = fscache_write(&cres, start, &iter, NULL, NULL);
185 186
	fscache_end_operation(&cres);
	return ret;
187 188
}

189 190 191 192
/*
 * Retrieve a page from FS-Cache
 */
int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
193
{
194
	int ret;
195

196 197
	cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
		 __func__, cifs_inode_cookie(inode), page, inode);
198

199 200 201 202 203 204 205 206 207
	ret = fscache_fallback_read_page(inode, page);
	if (ret < 0)
		return ret;

	/* Read completed synchronously */
	SetPageUptodate(page);
	return 0;
}

208
void __cifs_readahead_to_fscache(struct inode *inode, loff_t pos, size_t len)
209
{
210 211
	cifs_dbg(FYI, "%s: (fsc: %p, p: %llx, l: %zx, i: %p)\n",
		 __func__, cifs_inode_cookie(inode), pos, len, inode);
212

213
	fscache_fallback_write_pages(inode, pos, len, true);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
}

/*
 * Query the cache occupancy.
 */
int __cifs_fscache_query_occupancy(struct inode *inode,
				   pgoff_t first, unsigned int nr_pages,
				   pgoff_t *_data_first,
				   unsigned int *_data_nr_pages)
{
	struct netfs_cache_resources cres;
	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
	loff_t start, data_start;
	size_t len, data_len;
	int ret;
229

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	ret = fscache_begin_read_operation(&cres, cookie);
	if (ret < 0)
		return ret;

	start = first * PAGE_SIZE;
	len = nr_pages * PAGE_SIZE;
	ret = cres.ops->query_occupancy(&cres, start, len, PAGE_SIZE,
					&data_start, &data_len);
	if (ret == 0) {
		*_data_first = data_start / PAGE_SIZE;
		*_data_nr_pages = len / PAGE_SIZE;
	}

	fscache_end_operation(&cres);
	return ret;
245
}