interface.c 13 KB

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