interface.c 13 KB

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