Blame view

fs/ceph/cache.c 10.2 KB
99ccbd229   Milosz Tanski   ceph: use fscache...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * Ceph cache definitions.
   *
   *  Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
   *  Written by Milosz Tanski (milosz@adfin.com)
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License version 2
   *  as published by the Free Software Foundation.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to:
   *  Free Software Foundation
   *  51 Franklin Street, Fifth Floor
   *  Boston, MA  02111-1301  USA
   *
   */
99ccbd229   Milosz Tanski   ceph: use fscache...
23
24
25
26
  #include "super.h"
  #include "cache.h"
  
  struct ceph_aux_inode {
f6973c094   Yan, Zheng   ceph: use i_versi...
27
  	u64 		version;
99ccbd229   Milosz Tanski   ceph: use fscache...
28
29
30
31
32
33
34
35
  	struct timespec	mtime;
  	loff_t          size;
  };
  
  struct fscache_netfs ceph_cache_netfs = {
  	.name		= "ceph",
  	.version	= 0,
  };
1d8f83604   Yan, Zheng   ceph: new mount o...
36
37
38
39
40
41
42
43
44
45
  static DEFINE_MUTEX(ceph_fscache_lock);
  static LIST_HEAD(ceph_fscache_list);
  
  struct ceph_fscache_entry {
  	struct list_head list;
  	struct fscache_cookie *fscache;
  	struct ceph_fsid fsid;
  	size_t uniq_len;
  	char uniquifier[0];
  };
99ccbd229   Milosz Tanski   ceph: use fscache...
46
47
48
49
  static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data,
  					     void *buffer, uint16_t maxbuf)
  {
  	const struct ceph_fs_client* fsc = cookie_netfs_data;
1d8f83604   Yan, Zheng   ceph: new mount o...
50
51
  	const char *fscache_uniq = fsc->mount_options->fscache_uniq;
  	uint16_t fsid_len, uniq_len;
99ccbd229   Milosz Tanski   ceph: use fscache...
52

1d8f83604   Yan, Zheng   ceph: new mount o...
53
54
55
  	fsid_len = sizeof(fsc->client->fsid);
  	uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
  	if (fsid_len + uniq_len > maxbuf)
99ccbd229   Milosz Tanski   ceph: use fscache...
56
  		return 0;
1d8f83604   Yan, Zheng   ceph: new mount o...
57
58
59
60
61
  	memcpy(buffer, &fsc->client->fsid, fsid_len);
  	if (uniq_len)
  		memcpy(buffer + fsid_len, fscache_uniq, uniq_len);
  
  	return fsid_len + uniq_len;
99ccbd229   Milosz Tanski   ceph: use fscache...
62
63
64
65
66
67
68
  }
  
  static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
  	.name		= "CEPH.fsid",
  	.type		= FSCACHE_COOKIE_TYPE_INDEX,
  	.get_key	= ceph_fscache_session_get_key,
  };
971f0bdea   Milosz Tanski   ceph: trivial bui...
69
  int ceph_fscache_register(void)
99ccbd229   Milosz Tanski   ceph: use fscache...
70
71
72
  {
  	return fscache_register_netfs(&ceph_cache_netfs);
  }
971f0bdea   Milosz Tanski   ceph: trivial bui...
73
  void ceph_fscache_unregister(void)
99ccbd229   Milosz Tanski   ceph: use fscache...
74
75
76
77
78
79
  {
  	fscache_unregister_netfs(&ceph_cache_netfs);
  }
  
  int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
  {
1d8f83604   Yan, Zheng   ceph: new mount o...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  	const struct ceph_fsid *fsid = &fsc->client->fsid;
  	const char *fscache_uniq = fsc->mount_options->fscache_uniq;
  	size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
  	struct ceph_fscache_entry *ent;
  	int err = 0;
  
  	mutex_lock(&ceph_fscache_lock);
  	list_for_each_entry(ent, &ceph_fscache_list, list) {
  		if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
  			continue;
  		if (ent->uniq_len != uniq_len)
  			continue;
  		if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
  			continue;
  
  		pr_err("fscache cookie already registered for fsid %pU
  ", fsid);
  		pr_err("  use fsc=%%s mount option to specify a uniquifier
  ");
  		err = -EBUSY;
  		goto out_unlock;
  	}
  
  	ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
  	if (!ent) {
  		err = -ENOMEM;
  		goto out_unlock;
  	}
99ccbd229   Milosz Tanski   ceph: use fscache...
108
109
  	fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
  					      &ceph_fscache_fsid_object_def,
94d30ae90   David Howells   FS-Cache: Provide...
110
  					      fsc, true);
99ccbd229   Milosz Tanski   ceph: use fscache...
111

1d8f83604   Yan, Zheng   ceph: new mount o...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  	if (fsc->fscache) {
  		memcpy(&ent->fsid, fsid, sizeof(*fsid));
  		if (uniq_len > 0) {
  			memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
  			ent->uniq_len = uniq_len;
  		}
  		ent->fscache = fsc->fscache;
  		list_add_tail(&ent->list, &ceph_fscache_list);
  	} else {
  		kfree(ent);
  		pr_err("unable to register fscache cookie for fsid %pU
  ",
  		       fsid);
  		/* all other fs ignore this error */
  	}
  out_unlock:
  	mutex_unlock(&ceph_fscache_lock);
  	return err;
99ccbd229   Milosz Tanski   ceph: use fscache...
130
131
132
133
134
135
136
  }
  
  static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
  					   void *buffer, uint16_t maxbuf)
  {
  	const struct ceph_inode_info* ci = cookie_netfs_data;
  	uint16_t klen;
1291fb950   Geliang Tang   ceph: fix a comme...
137
  	/* use ceph virtual inode (id + snapshot) */
99ccbd229   Milosz Tanski   ceph: use fscache...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  	klen = sizeof(ci->i_vino);
  	if (klen > maxbuf)
  		return 0;
  
  	memcpy(buffer, &ci->i_vino, klen);
  	return klen;
  }
  
  static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
  					   void *buffer, uint16_t bufmax)
  {
  	struct ceph_aux_inode aux;
  	const struct ceph_inode_info* ci = cookie_netfs_data;
  	const struct inode* inode = &ci->vfs_inode;
  
  	memset(&aux, 0, sizeof(aux));
f6973c094   Yan, Zheng   ceph: use i_versi...
154
  	aux.version = ci->i_version;
99ccbd229   Milosz Tanski   ceph: use fscache...
155
  	aux.mtime = inode->i_mtime;
99c88e690   Yan, Zheng   ceph: use i_size_...
156
  	aux.size = i_size_read(inode);
99ccbd229   Milosz Tanski   ceph: use fscache...
157
158
159
160
161
162
163
164
165
166
  
  	memcpy(buffer, &aux, sizeof(aux));
  
  	return sizeof(aux);
  }
  
  static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
  					uint64_t *size)
  {
  	const struct ceph_inode_info* ci = cookie_netfs_data;
99c88e690   Yan, Zheng   ceph: use i_size_...
167
  	*size = i_size_read(&ci->vfs_inode);
99ccbd229   Milosz Tanski   ceph: use fscache...
168
169
170
171
172
173
174
175
176
177
178
179
180
  }
  
  static enum fscache_checkaux ceph_fscache_inode_check_aux(
  	void *cookie_netfs_data, const void *data, uint16_t dlen)
  {
  	struct ceph_aux_inode aux;
  	struct ceph_inode_info* ci = cookie_netfs_data;
  	struct inode* inode = &ci->vfs_inode;
  
  	if (dlen != sizeof(aux))
  		return FSCACHE_CHECKAUX_OBSOLETE;
  
  	memset(&aux, 0, sizeof(aux));
f6973c094   Yan, Zheng   ceph: use i_versi...
181
  	aux.version = ci->i_version;
99ccbd229   Milosz Tanski   ceph: use fscache...
182
  	aux.mtime = inode->i_mtime;
99c88e690   Yan, Zheng   ceph: use i_size_...
183
  	aux.size = i_size_read(inode);
99ccbd229   Milosz Tanski   ceph: use fscache...
184
185
186
187
188
189
190
  
  	if (memcmp(data, &aux, sizeof(aux)) != 0)
  		return FSCACHE_CHECKAUX_OBSOLETE;
  
  	dout("ceph inode 0x%p cached okay", ci);
  	return FSCACHE_CHECKAUX_OKAY;
  }
99ccbd229   Milosz Tanski   ceph: use fscache...
191
192
193
194
195
196
197
  static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
  	.name		= "CEPH.inode",
  	.type		= FSCACHE_COOKIE_TYPE_DATAFILE,
  	.get_key	= ceph_fscache_inode_get_key,
  	.get_attr	= ceph_fscache_inode_get_attr,
  	.get_aux	= ceph_fscache_inode_get_aux,
  	.check_aux	= ceph_fscache_inode_check_aux,
99ccbd229   Milosz Tanski   ceph: use fscache...
198
  };
46b59b2be   Yan, Zheng   ceph: disable fsc...
199
  void ceph_fscache_register_inode_cookie(struct inode *inode)
99ccbd229   Milosz Tanski   ceph: use fscache...
200
  {
46b59b2be   Yan, Zheng   ceph: disable fsc...
201
202
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
99ccbd229   Milosz Tanski   ceph: use fscache...
203
204
  
  	/* No caching for filesystem */
d37b1d994   Markus Elfring   ceph: adjust 36 c...
205
  	if (!fsc->fscache)
99ccbd229   Milosz Tanski   ceph: use fscache...
206
207
208
  		return;
  
  	/* Only cache for regular files that are read only */
46b59b2be   Yan, Zheng   ceph: disable fsc...
209
  	if (!S_ISREG(inode->i_mode))
99ccbd229   Milosz Tanski   ceph: use fscache...
210
  		return;
46b59b2be   Yan, Zheng   ceph: disable fsc...
211
212
213
214
215
216
  	inode_lock_nested(inode, I_MUTEX_CHILD);
  	if (!ci->fscache) {
  		ci->fscache = fscache_acquire_cookie(fsc->fscache,
  					&ceph_fscache_inode_object_def,
  					ci, false);
  	}
5955102c9   Al Viro   wrappers for ->i_...
217
  	inode_unlock(inode);
99ccbd229   Milosz Tanski   ceph: use fscache...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  }
  
  void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
  {
  	struct fscache_cookie* cookie;
  
  	if ((cookie = ci->fscache) == NULL)
  		return;
  
  	ci->fscache = NULL;
  
  	fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
  	fscache_relinquish_cookie(cookie, 0);
  }
46b59b2be   Yan, Zheng   ceph: disable fsc...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  static bool ceph_fscache_can_enable(void *data)
  {
  	struct inode *inode = data;
  	return !inode_is_open_for_write(inode);
  }
  
  void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  
  	if (!fscache_cookie_valid(ci->fscache))
  		return;
  
  	if (inode_is_open_for_write(inode)) {
  		dout("fscache_file_set_cookie %p %p disabling cache
  ",
  		     inode, filp);
  		fscache_disable_cookie(ci->fscache, false);
  		fscache_uncache_all_inode_pages(ci->fscache, inode);
  	} else {
  		fscache_enable_cookie(ci->fscache, ceph_fscache_can_enable,
  				inode);
  		if (fscache_cookie_enabled(ci->fscache)) {
0fbc5360b   Colin Ian King   ceph: fix spellin...
255
256
  			dout("fscache_file_set_cookie %p %p enabling cache
  ",
46b59b2be   Yan, Zheng   ceph: disable fsc...
257
258
259
260
  			     inode, filp);
  		}
  	}
  }
dd2bc4734   Yan, Zheng   ceph: fix readpag...
261
  static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
99ccbd229   Milosz Tanski   ceph: use fscache...
262
263
264
265
266
267
  {
  	if (!error)
  		SetPageUptodate(page);
  
  	unlock_page(page);
  }
3b33f692c   Zhang Zhuoyu   ceph: make logica...
268
  static inline bool cache_valid(struct ceph_inode_info *ci)
99ccbd229   Milosz Tanski   ceph: use fscache...
269
  {
f7f7e7a06   Yan, Zheng   ceph: improve fsc...
270
  	return ci->i_fscache_gen == ci->i_rdcache_gen;
99ccbd229   Milosz Tanski   ceph: use fscache...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  }
  
  
  /* Atempt to read from the fscache,
   *
   * This function is called from the readpage_nounlock context. DO NOT attempt to
   * unlock the page here (or in the callback).
   */
  int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int ret;
  
  	if (!cache_valid(ci))
  		return -ENOBUFS;
  
  	ret = fscache_read_or_alloc_page(ci->fscache, page,
dd2bc4734   Yan, Zheng   ceph: fix readpag...
288
  					 ceph_readpage_from_fscache_complete, NULL,
99ccbd229   Milosz Tanski   ceph: use fscache...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  					 GFP_KERNEL);
  
  	switch (ret) {
  		case 0: /* Page found */
  			dout("page read submitted
  ");
  			return 0;
  		case -ENOBUFS: /* Pages were not found, and can't be */
  		case -ENODATA: /* Pages were not found */
  			dout("page/inode not in cache
  ");
  			return ret;
  		default:
  			dout("%s: unknown error ret = %i
  ", __func__, ret);
  			return ret;
  	}
  }
  
  int ceph_readpages_from_fscache(struct inode *inode,
  				  struct address_space *mapping,
  				  struct list_head *pages,
  				  unsigned *nr_pages)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int ret;
  
  	if (!cache_valid(ci))
  		return -ENOBUFS;
  
  	ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
dd2bc4734   Yan, Zheng   ceph: fix readpag...
320
  					  ceph_readpage_from_fscache_complete,
99ccbd229   Milosz Tanski   ceph: use fscache...
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  					  NULL, mapping_gfp_mask(mapping));
  
  	switch (ret) {
  		case 0: /* All pages found */
  			dout("all-page read submitted
  ");
  			return 0;
  		case -ENOBUFS: /* Some pages were not found, and can't be */
  		case -ENODATA: /* some pages were not found */
  			dout("page/inode not in cache
  ");
  			return ret;
  		default:
  			dout("%s: unknown error ret = %i
  ", __func__, ret);
  			return ret;
  	}
  }
  
  void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int ret;
9b8dd1e8a   Milosz Tanski   ceph: ceph_readpa...
344
345
  	if (!PageFsCache(page))
  		return;
99ccbd229   Milosz Tanski   ceph: use fscache...
346
347
348
349
350
351
352
353
354
355
356
  	if (!cache_valid(ci))
  		return;
  
  	ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
  	if (ret)
  		 fscache_uncache_page(ci->fscache, page);
  }
  
  void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
ffc79664d   Milosz Tanski   ceph: hung on cep...
357
358
  	if (!PageFsCache(page))
  		return;
99ccbd229   Milosz Tanski   ceph: use fscache...
359
360
361
362
363
364
  	fscache_wait_on_page_write(ci->fscache, page);
  	fscache_uncache_page(ci->fscache, page);
  }
  
  void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
  {
1d8f83604   Yan, Zheng   ceph: new mount o...
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  	if (fscache_cookie_valid(fsc->fscache)) {
  		struct ceph_fscache_entry *ent;
  		bool found = false;
  
  		mutex_lock(&ceph_fscache_lock);
  		list_for_each_entry(ent, &ceph_fscache_list, list) {
  			if (ent->fscache == fsc->fscache) {
  				list_del(&ent->list);
  				kfree(ent);
  				found = true;
  				break;
  			}
  		}
  		WARN_ON_ONCE(!found);
  		mutex_unlock(&ceph_fscache_lock);
  
  		__fscache_relinquish_cookie(fsc->fscache, 0);
  	}
99ccbd229   Milosz Tanski   ceph: use fscache...
383
384
  	fsc->fscache = NULL;
  }
f7f7e7a06   Yan, Zheng   ceph: improve fsc...
385
386
387
388
  /*
   * caller should hold CEPH_CAP_FILE_{RD,CACHE}
   */
  void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
99ccbd229   Milosz Tanski   ceph: use fscache...
389
  {
f7f7e7a06   Yan, Zheng   ceph: improve fsc...
390
  	if (cache_valid(ci))
e81568eb1   Milosz Tanski   ceph: Do not do i...
391
  		return;
f7f7e7a06   Yan, Zheng   ceph: improve fsc...
392
393
394
395
396
397
398
399
400
  	/* resue i_truncate_mutex. There should be no pending
  	 * truncate while the caller holds CEPH_CAP_FILE_RD */
  	mutex_lock(&ci->i_truncate_mutex);
  	if (!cache_valid(ci)) {
  		if (fscache_check_consistency(ci->fscache))
  			fscache_invalidate(ci->fscache);
  		spin_lock(&ci->i_ceph_lock);
  		ci->i_fscache_gen = ci->i_rdcache_gen;
  		spin_unlock(&ci->i_ceph_lock);
99ccbd229   Milosz Tanski   ceph: use fscache...
401
  	}
f7f7e7a06   Yan, Zheng   ceph: improve fsc...
402
  	mutex_unlock(&ci->i_truncate_mutex);
99ccbd229   Milosz Tanski   ceph: use fscache...
403
  }