interface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* FS-Cache interface to CacheFiles
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/mount.h>
  13. #include "internal.h"
  14. struct cachefiles_lookup_data {
  15. struct cachefiles_xattr *auxdata; /* auxiliary data */
  16. char *key; /* key path */
  17. };
  18. static int cachefiles_attr_changed(struct fscache_object *_object);
  19. /*
  20. * allocate an object record for a cookie lookup and prepare the lookup data
  21. */
  22. static struct fscache_object *cachefiles_alloc_object(
  23. struct fscache_cache *_cache,
  24. struct fscache_cookie *cookie)
  25. {
  26. struct cachefiles_lookup_data *lookup_data;
  27. struct cachefiles_object *object;
  28. struct cachefiles_cache *cache;
  29. struct cachefiles_xattr *auxdata;
  30. unsigned keylen, auxlen;
  31. void *buffer, *p;
  32. char *key;
  33. cache = container_of(_cache, struct cachefiles_cache, cache);
  34. _enter("{%s},%p,", cache->cache.identifier, cookie);
  35. lookup_data = kmalloc(sizeof(*lookup_data), cachefiles_gfp);
  36. if (!lookup_data)
  37. goto nomem_lookup_data;
  38. /* create a new object record and a temporary leaf image */
  39. object = kmem_cache_alloc(cachefiles_object_jar, cachefiles_gfp);
  40. if (!object)
  41. goto nomem_object;
  42. ASSERTCMP(object->backer, ==, NULL);
  43. BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  44. atomic_set(&object->usage, 1);
  45. fscache_object_init(&object->fscache, cookie, &cache->cache);
  46. object->type = cookie->def->type;
  47. /* get hold of the raw key
  48. * - stick the length on the front and leave space on the back for the
  49. * encoder
  50. */
  51. buffer = kmalloc((2 + 512) + 3, cachefiles_gfp);
  52. if (!buffer)
  53. goto nomem_buffer;
  54. keylen = cookie->key_len;
  55. if (keylen <= sizeof(cookie->inline_key))
  56. p = cookie->inline_key;
  57. else
  58. p = cookie->key;
  59. memcpy(buffer + 2, p, keylen);
  60. *(uint16_t *)buffer = keylen;
  61. ((char *)buffer)[keylen + 2] = 0;
  62. ((char *)buffer)[keylen + 3] = 0;
  63. ((char *)buffer)[keylen + 4] = 0;
  64. /* turn the raw key into something that can work with as a filename */
  65. key = cachefiles_cook_key(buffer, keylen + 2, object->type);
  66. if (!key)
  67. goto nomem_key;
  68. /* get hold of the auxiliary data and prepend the object type */
  69. auxdata = buffer;
  70. auxlen = cookie->aux_len;
  71. if (auxlen) {
  72. if (auxlen <= sizeof(cookie->inline_aux))
  73. p = cookie->inline_aux;
  74. else
  75. p = cookie->aux;
  76. memcpy(auxdata->data, p, auxlen);
  77. }
  78. auxdata->len = auxlen + 1;
  79. auxdata->type = cookie->type;
  80. lookup_data->auxdata = auxdata;
  81. lookup_data->key = key;
  82. object->lookup_data = lookup_data;
  83. _leave(" = %p [%p]", &object->fscache, lookup_data);
  84. return &object->fscache;
  85. nomem_key:
  86. kfree(buffer);
  87. nomem_buffer:
  88. BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  89. kmem_cache_free(cachefiles_object_jar, object);
  90. fscache_object_destroyed(&cache->cache);
  91. nomem_object:
  92. kfree(lookup_data);
  93. nomem_lookup_data:
  94. _leave(" = -ENOMEM");
  95. return ERR_PTR(-ENOMEM);
  96. }
  97. /*
  98. * attempt to look up the nominated node in this cache
  99. * - return -ETIMEDOUT to be scheduled again
  100. */
  101. static int cachefiles_lookup_object(struct fscache_object *_object)
  102. {
  103. struct cachefiles_lookup_data *lookup_data;
  104. struct cachefiles_object *parent, *object;
  105. struct cachefiles_cache *cache;
  106. const struct cred *saved_cred;
  107. int ret;
  108. _enter("{OBJ%x}", _object->debug_id);
  109. cache = container_of(_object->cache, struct cachefiles_cache, cache);
  110. parent = container_of(_object->parent,
  111. struct cachefiles_object, fscache);
  112. object = container_of(_object, struct cachefiles_object, fscache);
  113. lookup_data = object->lookup_data;
  114. ASSERTCMP(lookup_data, !=, NULL);
  115. /* look up the key, creating any missing bits */
  116. cachefiles_begin_secure(cache, &saved_cred);
  117. ret = cachefiles_walk_to_object(parent, object,
  118. lookup_data->key,
  119. lookup_data->auxdata);
  120. cachefiles_end_secure(cache, saved_cred);
  121. /* polish off by setting the attributes of non-index files */
  122. if (ret == 0 &&
  123. object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
  124. cachefiles_attr_changed(&object->fscache);
  125. if (ret < 0 && ret != -ETIMEDOUT) {
  126. if (ret != -ENOBUFS)
  127. pr_warn("Lookup failed error %d\n", ret);
  128. fscache_object_lookup_error(&object->fscache);
  129. }
  130. _leave(" [%d]", ret);
  131. return ret;
  132. }
  133. /*
  134. * indication of lookup completion
  135. */
  136. static void cachefiles_lookup_complete(struct fscache_object *_object)
  137. {
  138. struct cachefiles_object *object;
  139. object = container_of(_object, struct cachefiles_object, fscache);
  140. _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
  141. if (object->lookup_data) {
  142. kfree(object->lookup_data->key);
  143. kfree(object->lookup_data->auxdata);
  144. kfree(object->lookup_data);
  145. object->lookup_data = NULL;
  146. }
  147. }
  148. /*
  149. * increment the usage count on an inode object (may fail if unmounting)
  150. */
  151. static
  152. struct fscache_object *cachefiles_grab_object(struct fscache_object *_object,
  153. enum fscache_obj_ref_trace why)
  154. {
  155. struct cachefiles_object *object =
  156. container_of(_object, struct cachefiles_object, fscache);
  157. int u;
  158. _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
  159. #ifdef CACHEFILES_DEBUG_SLAB
  160. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  161. #endif
  162. u = atomic_inc_return(&object->usage);
  163. trace_cachefiles_ref(object, _object->cookie,
  164. (enum cachefiles_obj_ref_trace)why, u);
  165. return &object->fscache;
  166. }
  167. /*
  168. * update the auxiliary data for an object object on disk
  169. */
  170. static void cachefiles_update_object(struct fscache_object *_object)
  171. {
  172. struct cachefiles_object *object;
  173. struct cachefiles_xattr *auxdata;
  174. struct cachefiles_cache *cache;
  175. struct fscache_cookie *cookie;
  176. const struct cred *saved_cred;
  177. const void *aux;
  178. unsigned auxlen;
  179. _enter("{OBJ%x}", _object->debug_id);
  180. object = container_of(_object, struct cachefiles_object, fscache);
  181. cache = container_of(object->fscache.cache, struct cachefiles_cache,
  182. cache);
  183. if (!fscache_use_cookie(_object)) {
  184. _leave(" [relinq]");
  185. return;
  186. }
  187. cookie = object->fscache.cookie;
  188. auxlen = cookie->aux_len;
  189. if (!auxlen) {
  190. fscache_unuse_cookie(_object);
  191. _leave(" [no aux]");
  192. return;
  193. }
  194. auxdata = kmalloc(2 + auxlen + 3, cachefiles_gfp);
  195. if (!auxdata) {
  196. fscache_unuse_cookie(_object);
  197. _leave(" [nomem]");
  198. return;
  199. }
  200. aux = (auxlen <= sizeof(cookie->inline_aux)) ?
  201. cookie->inline_aux : cookie->aux;
  202. memcpy(auxdata->data, aux, auxlen);
  203. fscache_unuse_cookie(_object);
  204. auxdata->len = auxlen + 1;
  205. auxdata->type = cookie->type;
  206. cachefiles_begin_secure(cache, &saved_cred);
  207. cachefiles_update_object_xattr(object, auxdata);
  208. cachefiles_end_secure(cache, saved_cred);
  209. kfree(auxdata);
  210. _leave("");
  211. }
  212. /*
  213. * discard the resources pinned by an object and effect retirement if
  214. * requested
  215. */
  216. static void cachefiles_drop_object(struct fscache_object *_object)
  217. {
  218. struct cachefiles_object *object;
  219. struct cachefiles_cache *cache;
  220. const struct cred *saved_cred;
  221. struct inode *inode;
  222. blkcnt_t i_blocks = 0;
  223. ASSERT(_object);
  224. object = container_of(_object, struct cachefiles_object, fscache);
  225. _enter("{OBJ%x,%d}",
  226. object->fscache.debug_id, atomic_read(&object->usage));
  227. cache = container_of(object->fscache.cache,
  228. struct cachefiles_cache, cache);
  229. #ifdef CACHEFILES_DEBUG_SLAB
  230. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  231. #endif
  232. /* We need to tidy the object up if we did in fact manage to open it.
  233. * It's possible for us to get here before the object is fully
  234. * initialised if the parent goes away or the object gets retired
  235. * before we set it up.
  236. */
  237. if (object->dentry) {
  238. /* delete retired objects */
  239. if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
  240. _object != cache->cache.fsdef
  241. ) {
  242. _debug("- retire object OBJ%x", object->fscache.debug_id);
  243. inode = d_backing_inode(object->dentry);
  244. if (inode)
  245. i_blocks = inode->i_blocks;
  246. cachefiles_begin_secure(cache, &saved_cred);
  247. cachefiles_delete_object(cache, object);
  248. cachefiles_end_secure(cache, saved_cred);
  249. }
  250. /* close the filesystem stuff attached to the object */
  251. if (object->backer != object->dentry)
  252. dput(object->backer);
  253. object->backer = NULL;
  254. }
  255. /* note that the object is now inactive */
  256. if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags))
  257. cachefiles_mark_object_inactive(cache, object, i_blocks);
  258. dput(object->dentry);
  259. object->dentry = NULL;
  260. _leave("");
  261. }
  262. /*
  263. * dispose of a reference to an object
  264. */
  265. static void cachefiles_put_object(struct fscache_object *_object,
  266. enum fscache_obj_ref_trace why)
  267. {
  268. struct cachefiles_object *object;
  269. struct fscache_cache *cache;
  270. int u;
  271. ASSERT(_object);
  272. object = container_of(_object, struct cachefiles_object, fscache);
  273. _enter("{OBJ%x,%d}",
  274. object->fscache.debug_id, atomic_read(&object->usage));
  275. #ifdef CACHEFILES_DEBUG_SLAB
  276. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  277. #endif
  278. ASSERTIFCMP(object->fscache.parent,
  279. object->fscache.parent->n_children, >, 0);
  280. u = atomic_dec_return(&object->usage);
  281. trace_cachefiles_ref(object, _object->cookie,
  282. (enum cachefiles_obj_ref_trace)why, u);
  283. ASSERTCMP(u, !=, -1);
  284. if (u == 0) {
  285. _debug("- kill object OBJ%x", object->fscache.debug_id);
  286. ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  287. ASSERTCMP(object->fscache.parent, ==, NULL);
  288. ASSERTCMP(object->backer, ==, NULL);
  289. ASSERTCMP(object->dentry, ==, NULL);
  290. ASSERTCMP(object->fscache.n_ops, ==, 0);
  291. ASSERTCMP(object->fscache.n_children, ==, 0);
  292. if (object->lookup_data) {
  293. kfree(object->lookup_data->key);
  294. kfree(object->lookup_data->auxdata);
  295. kfree(object->lookup_data);
  296. object->lookup_data = NULL;
  297. }
  298. cache = object->fscache.cache;
  299. fscache_object_destroy(&object->fscache);
  300. kmem_cache_free(cachefiles_object_jar, object);
  301. fscache_object_destroyed(cache);
  302. }
  303. _leave("");
  304. }
  305. /*
  306. * sync a cache
  307. */
  308. static void cachefiles_sync_cache(struct fscache_cache *_cache)
  309. {
  310. struct cachefiles_cache *cache;
  311. const struct cred *saved_cred;
  312. int ret;
  313. _enter("%p", _cache);
  314. cache = container_of(_cache, struct cachefiles_cache, cache);
  315. /* make sure all pages pinned by operations on behalf of the netfs are
  316. * written to disc */
  317. cachefiles_begin_secure(cache, &saved_cred);
  318. down_read(&cache->mnt->mnt_sb->s_umount);
  319. ret = sync_filesystem(cache->mnt->mnt_sb);
  320. up_read(&cache->mnt->mnt_sb->s_umount);
  321. cachefiles_end_secure(cache, saved_cred);
  322. if (ret == -EIO)
  323. cachefiles_io_error(cache,
  324. "Attempt to sync backing fs superblock"
  325. " returned error %d",
  326. ret);
  327. }
  328. /*
  329. * check if the backing cache is updated to FS-Cache
  330. * - called by FS-Cache when evaluates if need to invalidate the cache
  331. */
  332. static int cachefiles_check_consistency(struct fscache_operation *op)
  333. {
  334. struct cachefiles_object *object;
  335. struct cachefiles_cache *cache;
  336. const struct cred *saved_cred;
  337. int ret;
  338. _enter("{OBJ%x}", op->object->debug_id);
  339. object = container_of(op->object, struct cachefiles_object, fscache);
  340. cache = container_of(object->fscache.cache,
  341. struct cachefiles_cache, cache);
  342. cachefiles_begin_secure(cache, &saved_cred);
  343. ret = cachefiles_check_auxdata(object);
  344. cachefiles_end_secure(cache, saved_cred);
  345. _leave(" = %d", ret);
  346. return ret;
  347. }
  348. /*
  349. * notification the attributes on an object have changed
  350. * - called with reads/writes excluded by FS-Cache
  351. */
  352. static int cachefiles_attr_changed(struct fscache_object *_object)
  353. {
  354. struct cachefiles_object *object;
  355. struct cachefiles_cache *cache;
  356. const struct cred *saved_cred;
  357. struct iattr newattrs;
  358. uint64_t ni_size;
  359. loff_t oi_size;
  360. int ret;
  361. ni_size = _object->store_limit_l;
  362. _enter("{OBJ%x},[%llu]",
  363. _object->debug_id, (unsigned long long) ni_size);
  364. object = container_of(_object, struct cachefiles_object, fscache);
  365. cache = container_of(object->fscache.cache,
  366. struct cachefiles_cache, cache);
  367. if (ni_size == object->i_size)
  368. return 0;
  369. if (!object->backer)
  370. return -ENOBUFS;
  371. ASSERT(d_is_reg(object->backer));
  372. fscache_set_store_limit(&object->fscache, ni_size);
  373. oi_size = i_size_read(d_backing_inode(object->backer));
  374. if (oi_size == ni_size)
  375. return 0;
  376. cachefiles_begin_secure(cache, &saved_cred);
  377. inode_lock(d_inode(object->backer));
  378. /* if there's an extension to a partial page at the end of the backing
  379. * file, we need to discard the partial page so that we pick up new
  380. * data after it */
  381. if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
  382. _debug("discard tail %llx", oi_size);
  383. newattrs.ia_valid = ATTR_SIZE;
  384. newattrs.ia_size = oi_size & PAGE_MASK;
  385. ret = notify_change(object->backer, &newattrs, NULL);
  386. if (ret < 0)
  387. goto truncate_failed;
  388. }
  389. newattrs.ia_valid = ATTR_SIZE;
  390. newattrs.ia_size = ni_size;
  391. ret = notify_change(object->backer, &newattrs, NULL);
  392. truncate_failed:
  393. inode_unlock(d_inode(object->backer));
  394. cachefiles_end_secure(cache, saved_cred);
  395. if (ret == -EIO) {
  396. fscache_set_store_limit(&object->fscache, 0);
  397. cachefiles_io_error_obj(object, "Size set failed");
  398. ret = -ENOBUFS;
  399. }
  400. _leave(" = %d", ret);
  401. return ret;
  402. }
  403. /*
  404. * Invalidate an object
  405. */
  406. static void cachefiles_invalidate_object(struct fscache_operation *op)
  407. {
  408. struct cachefiles_object *object;
  409. struct cachefiles_cache *cache;
  410. const struct cred *saved_cred;
  411. struct path path;
  412. uint64_t ni_size;
  413. int ret;
  414. object = container_of(op->object, struct cachefiles_object, fscache);
  415. cache = container_of(object->fscache.cache,
  416. struct cachefiles_cache, cache);
  417. ni_size = op->object->store_limit_l;
  418. _enter("{OBJ%x},[%llu]",
  419. op->object->debug_id, (unsigned long long)ni_size);
  420. if (object->backer) {
  421. ASSERT(d_is_reg(object->backer));
  422. fscache_set_store_limit(&object->fscache, ni_size);
  423. path.dentry = object->backer;
  424. path.mnt = cache->mnt;
  425. cachefiles_begin_secure(cache, &saved_cred);
  426. ret = vfs_truncate(&path, 0);
  427. if (ret == 0)
  428. ret = vfs_truncate(&path, ni_size);
  429. cachefiles_end_secure(cache, saved_cred);
  430. if (ret != 0) {
  431. fscache_set_store_limit(&object->fscache, 0);
  432. if (ret == -EIO)
  433. cachefiles_io_error_obj(object,
  434. "Invalidate failed");
  435. }
  436. }
  437. fscache_op_complete(op, true);
  438. _leave("");
  439. }
  440. /*
  441. * dissociate a cache from all the pages it was backing
  442. */
  443. static void cachefiles_dissociate_pages(struct fscache_cache *cache)
  444. {
  445. _enter("");
  446. }
  447. const struct fscache_cache_ops cachefiles_cache_ops = {
  448. .name = "cachefiles",
  449. .alloc_object = cachefiles_alloc_object,
  450. .lookup_object = cachefiles_lookup_object,
  451. .lookup_complete = cachefiles_lookup_complete,
  452. .grab_object = cachefiles_grab_object,
  453. .update_object = cachefiles_update_object,
  454. .invalidate_object = cachefiles_invalidate_object,
  455. .drop_object = cachefiles_drop_object,
  456. .put_object = cachefiles_put_object,
  457. .sync_cache = cachefiles_sync_cache,
  458. .attr_changed = cachefiles_attr_changed,
  459. .read_or_alloc_page = cachefiles_read_or_alloc_page,
  460. .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
  461. .allocate_page = cachefiles_allocate_page,
  462. .allocate_pages = cachefiles_allocate_pages,
  463. .write_page = cachefiles_write_page,
  464. .uncache_page = cachefiles_uncache_page,
  465. .dissociate_pages = cachefiles_dissociate_pages,
  466. .check_consistency = cachefiles_check_consistency,
  467. };