interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/mount.h>
  12. #include <linux/buffer_head.h>
  13. #include "internal.h"
  14. #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
  15. struct cachefiles_lookup_data {
  16. struct cachefiles_xattr *auxdata; /* auxiliary data */
  17. char *key; /* key path */
  18. };
  19. static int cachefiles_attr_changed(struct fscache_object *_object);
  20. /*
  21. * allocate an object record for a cookie lookup and prepare the lookup data
  22. */
  23. static struct fscache_object *cachefiles_alloc_object(
  24. struct fscache_cache *_cache,
  25. struct fscache_cookie *cookie)
  26. {
  27. struct cachefiles_lookup_data *lookup_data;
  28. struct cachefiles_object *object;
  29. struct cachefiles_cache *cache;
  30. struct cachefiles_xattr *auxdata;
  31. unsigned keylen, auxlen;
  32. void *buffer;
  33. char *key;
  34. cache = container_of(_cache, struct cachefiles_cache, cache);
  35. _enter("{%s},%p,", cache->cache.identifier, cookie);
  36. lookup_data = kmalloc(sizeof(*lookup_data), GFP_KERNEL);
  37. if (!lookup_data)
  38. goto nomem_lookup_data;
  39. /* create a new object record and a temporary leaf image */
  40. object = kmem_cache_alloc(cachefiles_object_jar, GFP_KERNEL);
  41. if (!object)
  42. goto nomem_object;
  43. ASSERTCMP(object->backer, ==, NULL);
  44. BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  45. atomic_set(&object->usage, 1);
  46. fscache_object_init(&object->fscache, cookie, &cache->cache);
  47. object->type = cookie->def->type;
  48. /* get hold of the raw key
  49. * - stick the length on the front and leave space on the back for the
  50. * encoder
  51. */
  52. buffer = kmalloc((2 + 512) + 3, GFP_KERNEL);
  53. if (!buffer)
  54. goto nomem_buffer;
  55. keylen = cookie->def->get_key(cookie->netfs_data, buffer + 2, 512);
  56. ASSERTCMP(keylen, <, 512);
  57. *(uint16_t *)buffer = keylen;
  58. ((char *)buffer)[keylen + 2] = 0;
  59. ((char *)buffer)[keylen + 3] = 0;
  60. ((char *)buffer)[keylen + 4] = 0;
  61. /* turn the raw key into something that can work with as a filename */
  62. key = cachefiles_cook_key(buffer, keylen + 2, object->type);
  63. if (!key)
  64. goto nomem_key;
  65. /* get hold of the auxiliary data and prepend the object type */
  66. auxdata = buffer;
  67. auxlen = 0;
  68. if (cookie->def->get_aux) {
  69. auxlen = cookie->def->get_aux(cookie->netfs_data,
  70. auxdata->data, 511);
  71. ASSERTCMP(auxlen, <, 511);
  72. }
  73. auxdata->len = auxlen + 1;
  74. auxdata->type = cookie->def->type;
  75. lookup_data->auxdata = auxdata;
  76. lookup_data->key = key;
  77. object->lookup_data = lookup_data;
  78. _leave(" = %p [%p]", &object->fscache, lookup_data);
  79. return &object->fscache;
  80. nomem_key:
  81. kfree(buffer);
  82. nomem_buffer:
  83. BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  84. kmem_cache_free(cachefiles_object_jar, object);
  85. fscache_object_destroyed(&cache->cache);
  86. nomem_object:
  87. kfree(lookup_data);
  88. nomem_lookup_data:
  89. _leave(" = -ENOMEM");
  90. return ERR_PTR(-ENOMEM);
  91. }
  92. /*
  93. * attempt to look up the nominated node in this cache
  94. */
  95. static void cachefiles_lookup_object(struct fscache_object *_object)
  96. {
  97. struct cachefiles_lookup_data *lookup_data;
  98. struct cachefiles_object *parent, *object;
  99. struct cachefiles_cache *cache;
  100. const struct cred *saved_cred;
  101. int ret;
  102. _enter("{OBJ%x}", _object->debug_id);
  103. cache = container_of(_object->cache, struct cachefiles_cache, cache);
  104. parent = container_of(_object->parent,
  105. struct cachefiles_object, fscache);
  106. object = container_of(_object, struct cachefiles_object, fscache);
  107. lookup_data = object->lookup_data;
  108. ASSERTCMP(lookup_data, !=, NULL);
  109. /* look up the key, creating any missing bits */
  110. cachefiles_begin_secure(cache, &saved_cred);
  111. ret = cachefiles_walk_to_object(parent, object,
  112. lookup_data->key,
  113. lookup_data->auxdata);
  114. cachefiles_end_secure(cache, saved_cred);
  115. /* polish off by setting the attributes of non-index files */
  116. if (ret == 0 &&
  117. object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
  118. cachefiles_attr_changed(&object->fscache);
  119. if (ret < 0) {
  120. printk(KERN_WARNING "CacheFiles: Lookup failed error %d\n",
  121. ret);
  122. fscache_object_lookup_error(&object->fscache);
  123. }
  124. _leave(" [%d]", ret);
  125. }
  126. /*
  127. * indication of lookup completion
  128. */
  129. static void cachefiles_lookup_complete(struct fscache_object *_object)
  130. {
  131. struct cachefiles_object *object;
  132. object = container_of(_object, struct cachefiles_object, fscache);
  133. _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
  134. if (object->lookup_data) {
  135. kfree(object->lookup_data->key);
  136. kfree(object->lookup_data->auxdata);
  137. kfree(object->lookup_data);
  138. object->lookup_data = NULL;
  139. }
  140. }
  141. /*
  142. * increment the usage count on an inode object (may fail if unmounting)
  143. */
  144. static
  145. struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
  146. {
  147. struct cachefiles_object *object =
  148. container_of(_object, struct cachefiles_object, fscache);
  149. _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
  150. #ifdef CACHEFILES_DEBUG_SLAB
  151. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  152. #endif
  153. atomic_inc(&object->usage);
  154. return &object->fscache;
  155. }
  156. /*
  157. * update the auxilliary data for an object object on disk
  158. */
  159. static void cachefiles_update_object(struct fscache_object *_object)
  160. {
  161. struct cachefiles_object *object;
  162. struct cachefiles_xattr *auxdata;
  163. struct cachefiles_cache *cache;
  164. struct fscache_cookie *cookie;
  165. const struct cred *saved_cred;
  166. unsigned auxlen;
  167. _enter("{OBJ%x}", _object->debug_id);
  168. object = container_of(_object, struct cachefiles_object, fscache);
  169. cache = container_of(object->fscache.cache, struct cachefiles_cache,
  170. cache);
  171. cookie = object->fscache.cookie;
  172. if (!cookie->def->get_aux) {
  173. _leave(" [no aux]");
  174. return;
  175. }
  176. auxdata = kmalloc(2 + 512 + 3, GFP_KERNEL);
  177. if (!auxdata) {
  178. _leave(" [nomem]");
  179. return;
  180. }
  181. auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
  182. ASSERTCMP(auxlen, <, 511);
  183. auxdata->len = auxlen + 1;
  184. auxdata->type = cookie->def->type;
  185. cachefiles_begin_secure(cache, &saved_cred);
  186. cachefiles_update_object_xattr(object, auxdata);
  187. cachefiles_end_secure(cache, saved_cred);
  188. kfree(auxdata);
  189. _leave("");
  190. }
  191. /*
  192. * discard the resources pinned by an object and effect retirement if
  193. * requested
  194. */
  195. static void cachefiles_drop_object(struct fscache_object *_object)
  196. {
  197. struct cachefiles_object *object;
  198. struct cachefiles_cache *cache;
  199. const struct cred *saved_cred;
  200. ASSERT(_object);
  201. object = container_of(_object, struct cachefiles_object, fscache);
  202. _enter("{OBJ%x,%d}",
  203. object->fscache.debug_id, atomic_read(&object->usage));
  204. cache = container_of(object->fscache.cache,
  205. struct cachefiles_cache, cache);
  206. #ifdef CACHEFILES_DEBUG_SLAB
  207. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  208. #endif
  209. /* delete retired objects */
  210. if (object->fscache.state == FSCACHE_OBJECT_RECYCLING &&
  211. _object != cache->cache.fsdef
  212. ) {
  213. _debug("- retire object OBJ%x", object->fscache.debug_id);
  214. cachefiles_begin_secure(cache, &saved_cred);
  215. cachefiles_delete_object(cache, object);
  216. cachefiles_end_secure(cache, saved_cred);
  217. }
  218. /* close the filesystem stuff attached to the object */
  219. if (object->backer != object->dentry)
  220. dput(object->backer);
  221. object->backer = NULL;
  222. /* note that the object is now inactive */
  223. if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
  224. write_lock(&cache->active_lock);
  225. if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
  226. &object->flags))
  227. BUG();
  228. rb_erase(&object->active_node, &cache->active_nodes);
  229. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  230. write_unlock(&cache->active_lock);
  231. }
  232. dput(object->dentry);
  233. object->dentry = NULL;
  234. _leave("");
  235. }
  236. /*
  237. * dispose of a reference to an object
  238. */
  239. static void cachefiles_put_object(struct fscache_object *_object)
  240. {
  241. struct cachefiles_object *object;
  242. struct fscache_cache *cache;
  243. ASSERT(_object);
  244. object = container_of(_object, struct cachefiles_object, fscache);
  245. _enter("{OBJ%x,%d}",
  246. object->fscache.debug_id, atomic_read(&object->usage));
  247. #ifdef CACHEFILES_DEBUG_SLAB
  248. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  249. #endif
  250. ASSERTIFCMP(object->fscache.parent,
  251. object->fscache.parent->n_children, >, 0);
  252. if (atomic_dec_and_test(&object->usage)) {
  253. _debug("- kill object OBJ%x", object->fscache.debug_id);
  254. ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  255. ASSERTCMP(object->fscache.parent, ==, NULL);
  256. ASSERTCMP(object->backer, ==, NULL);
  257. ASSERTCMP(object->dentry, ==, NULL);
  258. ASSERTCMP(object->fscache.n_ops, ==, 0);
  259. ASSERTCMP(object->fscache.n_children, ==, 0);
  260. if (object->lookup_data) {
  261. kfree(object->lookup_data->key);
  262. kfree(object->lookup_data->auxdata);
  263. kfree(object->lookup_data);
  264. object->lookup_data = NULL;
  265. }
  266. cache = object->fscache.cache;
  267. kmem_cache_free(cachefiles_object_jar, object);
  268. fscache_object_destroyed(cache);
  269. }
  270. _leave("");
  271. }
  272. /*
  273. * sync a cache
  274. */
  275. static void cachefiles_sync_cache(struct fscache_cache *_cache)
  276. {
  277. struct cachefiles_cache *cache;
  278. const struct cred *saved_cred;
  279. int ret;
  280. _enter("%p", _cache);
  281. cache = container_of(_cache, struct cachefiles_cache, cache);
  282. /* make sure all pages pinned by operations on behalf of the netfs are
  283. * written to disc */
  284. cachefiles_begin_secure(cache, &saved_cred);
  285. down_read(&cache->mnt->mnt_sb->s_umount);
  286. ret = sync_filesystem(cache->mnt->mnt_sb);
  287. up_read(&cache->mnt->mnt_sb->s_umount);
  288. cachefiles_end_secure(cache, saved_cred);
  289. if (ret == -EIO)
  290. cachefiles_io_error(cache,
  291. "Attempt to sync backing fs superblock"
  292. " returned error %d",
  293. ret);
  294. }
  295. /*
  296. * notification the attributes on an object have changed
  297. * - called with reads/writes excluded by FS-Cache
  298. */
  299. static int cachefiles_attr_changed(struct fscache_object *_object)
  300. {
  301. struct cachefiles_object *object;
  302. struct cachefiles_cache *cache;
  303. const struct cred *saved_cred;
  304. struct iattr newattrs;
  305. uint64_t ni_size;
  306. loff_t oi_size;
  307. int ret;
  308. _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
  309. _enter("{OBJ%x},[%llu]",
  310. _object->debug_id, (unsigned long long) ni_size);
  311. object = container_of(_object, struct cachefiles_object, fscache);
  312. cache = container_of(object->fscache.cache,
  313. struct cachefiles_cache, cache);
  314. if (ni_size == object->i_size)
  315. return 0;
  316. if (!object->backer)
  317. return -ENOBUFS;
  318. ASSERT(S_ISREG(object->backer->d_inode->i_mode));
  319. fscache_set_store_limit(&object->fscache, ni_size);
  320. oi_size = i_size_read(object->backer->d_inode);
  321. if (oi_size == ni_size)
  322. return 0;
  323. newattrs.ia_size = ni_size;
  324. newattrs.ia_valid = ATTR_SIZE;
  325. cachefiles_begin_secure(cache, &saved_cred);
  326. mutex_lock(&object->backer->d_inode->i_mutex);
  327. ret = notify_change(object->backer, &newattrs);
  328. mutex_unlock(&object->backer->d_inode->i_mutex);
  329. cachefiles_end_secure(cache, saved_cred);
  330. if (ret == -EIO) {
  331. fscache_set_store_limit(&object->fscache, 0);
  332. cachefiles_io_error_obj(object, "Size set failed");
  333. ret = -ENOBUFS;
  334. }
  335. _leave(" = %d", ret);
  336. return ret;
  337. }
  338. /*
  339. * dissociate a cache from all the pages it was backing
  340. */
  341. static void cachefiles_dissociate_pages(struct fscache_cache *cache)
  342. {
  343. _enter("");
  344. }
  345. const struct fscache_cache_ops cachefiles_cache_ops = {
  346. .name = "cachefiles",
  347. .alloc_object = cachefiles_alloc_object,
  348. .lookup_object = cachefiles_lookup_object,
  349. .lookup_complete = cachefiles_lookup_complete,
  350. .grab_object = cachefiles_grab_object,
  351. .update_object = cachefiles_update_object,
  352. .drop_object = cachefiles_drop_object,
  353. .put_object = cachefiles_put_object,
  354. .sync_cache = cachefiles_sync_cache,
  355. .attr_changed = cachefiles_attr_changed,
  356. .read_or_alloc_page = cachefiles_read_or_alloc_page,
  357. .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
  358. .allocate_page = cachefiles_allocate_page,
  359. .allocate_pages = cachefiles_allocate_pages,
  360. .write_page = cachefiles_write_page,
  361. .uncache_page = cachefiles_uncache_page,
  362. .dissociate_pages = cachefiles_dissociate_pages,
  363. };