interface.c 13 KB

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