interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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;
  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->def->get_key(cookie->netfs_data, buffer + 2, 512);
  55. ASSERTCMP(keylen, <, 512);
  56. *(uint16_t *)buffer = keylen;
  57. ((char *)buffer)[keylen + 2] = 0;
  58. ((char *)buffer)[keylen + 3] = 0;
  59. ((char *)buffer)[keylen + 4] = 0;
  60. /* turn the raw key into something that can work with as a filename */
  61. key = cachefiles_cook_key(buffer, keylen + 2, object->type);
  62. if (!key)
  63. goto nomem_key;
  64. /* get hold of the auxiliary data and prepend the object type */
  65. auxdata = buffer;
  66. auxlen = 0;
  67. if (cookie->def->get_aux) {
  68. auxlen = cookie->def->get_aux(cookie->netfs_data,
  69. auxdata->data, 511);
  70. ASSERTCMP(auxlen, <, 511);
  71. }
  72. auxdata->len = auxlen + 1;
  73. auxdata->type = cookie->def->type;
  74. lookup_data->auxdata = auxdata;
  75. lookup_data->key = key;
  76. object->lookup_data = lookup_data;
  77. _leave(" = %p [%p]", &object->fscache, lookup_data);
  78. return &object->fscache;
  79. nomem_key:
  80. kfree(buffer);
  81. nomem_buffer:
  82. BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  83. kmem_cache_free(cachefiles_object_jar, object);
  84. fscache_object_destroyed(&cache->cache);
  85. nomem_object:
  86. kfree(lookup_data);
  87. nomem_lookup_data:
  88. _leave(" = -ENOMEM");
  89. return ERR_PTR(-ENOMEM);
  90. }
  91. /*
  92. * attempt to look up the nominated node in this cache
  93. * - return -ETIMEDOUT to be scheduled again
  94. */
  95. static int 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 && ret != -ETIMEDOUT) {
  120. if (ret != -ENOBUFS)
  121. printk(KERN_WARNING
  122. "CacheFiles: Lookup failed error %d\n", 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 auxiliary 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. if (!fscache_use_cookie(_object)) {
  174. _leave(" [relinq]");
  175. return;
  176. }
  177. cookie = object->fscache.cookie;
  178. if (!cookie->def->get_aux) {
  179. fscache_unuse_cookie(_object);
  180. _leave(" [no aux]");
  181. return;
  182. }
  183. auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp);
  184. if (!auxdata) {
  185. fscache_unuse_cookie(_object);
  186. _leave(" [nomem]");
  187. return;
  188. }
  189. auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
  190. fscache_unuse_cookie(_object);
  191. ASSERTCMP(auxlen, <, 511);
  192. auxdata->len = auxlen + 1;
  193. auxdata->type = cookie->def->type;
  194. cachefiles_begin_secure(cache, &saved_cred);
  195. cachefiles_update_object_xattr(object, auxdata);
  196. cachefiles_end_secure(cache, saved_cred);
  197. kfree(auxdata);
  198. _leave("");
  199. }
  200. /*
  201. * discard the resources pinned by an object and effect retirement if
  202. * requested
  203. */
  204. static void cachefiles_drop_object(struct fscache_object *_object)
  205. {
  206. struct cachefiles_object *object;
  207. struct cachefiles_cache *cache;
  208. const struct cred *saved_cred;
  209. ASSERT(_object);
  210. object = container_of(_object, struct cachefiles_object, fscache);
  211. _enter("{OBJ%x,%d}",
  212. object->fscache.debug_id, atomic_read(&object->usage));
  213. cache = container_of(object->fscache.cache,
  214. struct cachefiles_cache, cache);
  215. #ifdef CACHEFILES_DEBUG_SLAB
  216. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  217. #endif
  218. /* delete retired objects */
  219. if (test_bit(FSCACHE_COOKIE_RETIRED, &object->fscache.cookie->flags) &&
  220. _object != cache->cache.fsdef
  221. ) {
  222. _debug("- retire object OBJ%x", object->fscache.debug_id);
  223. cachefiles_begin_secure(cache, &saved_cred);
  224. cachefiles_delete_object(cache, object);
  225. cachefiles_end_secure(cache, saved_cred);
  226. }
  227. /* close the filesystem stuff attached to the object */
  228. if (object->backer != object->dentry)
  229. dput(object->backer);
  230. object->backer = NULL;
  231. /* note that the object is now inactive */
  232. if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
  233. write_lock(&cache->active_lock);
  234. if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
  235. &object->flags))
  236. BUG();
  237. rb_erase(&object->active_node, &cache->active_nodes);
  238. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  239. write_unlock(&cache->active_lock);
  240. }
  241. dput(object->dentry);
  242. object->dentry = NULL;
  243. _leave("");
  244. }
  245. /*
  246. * dispose of a reference to an object
  247. */
  248. static void cachefiles_put_object(struct fscache_object *_object)
  249. {
  250. struct cachefiles_object *object;
  251. struct fscache_cache *cache;
  252. ASSERT(_object);
  253. object = container_of(_object, struct cachefiles_object, fscache);
  254. _enter("{OBJ%x,%d}",
  255. object->fscache.debug_id, atomic_read(&object->usage));
  256. #ifdef CACHEFILES_DEBUG_SLAB
  257. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  258. #endif
  259. ASSERTIFCMP(object->fscache.parent,
  260. object->fscache.parent->n_children, >, 0);
  261. if (atomic_dec_and_test(&object->usage)) {
  262. _debug("- kill object OBJ%x", object->fscache.debug_id);
  263. ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  264. ASSERTCMP(object->fscache.parent, ==, NULL);
  265. ASSERTCMP(object->backer, ==, NULL);
  266. ASSERTCMP(object->dentry, ==, NULL);
  267. ASSERTCMP(object->fscache.n_ops, ==, 0);
  268. ASSERTCMP(object->fscache.n_children, ==, 0);
  269. if (object->lookup_data) {
  270. kfree(object->lookup_data->key);
  271. kfree(object->lookup_data->auxdata);
  272. kfree(object->lookup_data);
  273. object->lookup_data = NULL;
  274. }
  275. cache = object->fscache.cache;
  276. fscache_object_destroy(&object->fscache);
  277. kmem_cache_free(cachefiles_object_jar, object);
  278. fscache_object_destroyed(cache);
  279. }
  280. _leave("");
  281. }
  282. /*
  283. * sync a cache
  284. */
  285. static void cachefiles_sync_cache(struct fscache_cache *_cache)
  286. {
  287. struct cachefiles_cache *cache;
  288. const struct cred *saved_cred;
  289. int ret;
  290. _enter("%p", _cache);
  291. cache = container_of(_cache, struct cachefiles_cache, cache);
  292. /* make sure all pages pinned by operations on behalf of the netfs are
  293. * written to disc */
  294. cachefiles_begin_secure(cache, &saved_cred);
  295. down_read(&cache->mnt->mnt_sb->s_umount);
  296. ret = sync_filesystem(cache->mnt->mnt_sb);
  297. up_read(&cache->mnt->mnt_sb->s_umount);
  298. cachefiles_end_secure(cache, saved_cred);
  299. if (ret == -EIO)
  300. cachefiles_io_error(cache,
  301. "Attempt to sync backing fs superblock"
  302. " returned error %d",
  303. ret);
  304. }
  305. /*
  306. * notification the attributes on an object have changed
  307. * - called with reads/writes excluded by FS-Cache
  308. */
  309. static int cachefiles_attr_changed(struct fscache_object *_object)
  310. {
  311. struct cachefiles_object *object;
  312. struct cachefiles_cache *cache;
  313. const struct cred *saved_cred;
  314. struct iattr newattrs;
  315. uint64_t ni_size;
  316. loff_t oi_size;
  317. int ret;
  318. _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
  319. _enter("{OBJ%x},[%llu]",
  320. _object->debug_id, (unsigned long long) ni_size);
  321. object = container_of(_object, struct cachefiles_object, fscache);
  322. cache = container_of(object->fscache.cache,
  323. struct cachefiles_cache, cache);
  324. if (ni_size == object->i_size)
  325. return 0;
  326. if (!object->backer)
  327. return -ENOBUFS;
  328. ASSERT(S_ISREG(object->backer->d_inode->i_mode));
  329. fscache_set_store_limit(&object->fscache, ni_size);
  330. oi_size = i_size_read(object->backer->d_inode);
  331. if (oi_size == ni_size)
  332. return 0;
  333. cachefiles_begin_secure(cache, &saved_cred);
  334. mutex_lock(&object->backer->d_inode->i_mutex);
  335. /* if there's an extension to a partial page at the end of the backing
  336. * file, we need to discard the partial page so that we pick up new
  337. * data after it */
  338. if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
  339. _debug("discard tail %llx", oi_size);
  340. newattrs.ia_valid = ATTR_SIZE;
  341. newattrs.ia_size = oi_size & PAGE_MASK;
  342. ret = notify_change(object->backer, &newattrs);
  343. if (ret < 0)
  344. goto truncate_failed;
  345. }
  346. newattrs.ia_valid = ATTR_SIZE;
  347. newattrs.ia_size = ni_size;
  348. ret = notify_change(object->backer, &newattrs);
  349. truncate_failed:
  350. mutex_unlock(&object->backer->d_inode->i_mutex);
  351. cachefiles_end_secure(cache, saved_cred);
  352. if (ret == -EIO) {
  353. fscache_set_store_limit(&object->fscache, 0);
  354. cachefiles_io_error_obj(object, "Size set failed");
  355. ret = -ENOBUFS;
  356. }
  357. _leave(" = %d", ret);
  358. return ret;
  359. }
  360. /*
  361. * Invalidate an object
  362. */
  363. static void cachefiles_invalidate_object(struct fscache_operation *op)
  364. {
  365. struct cachefiles_object *object;
  366. struct cachefiles_cache *cache;
  367. const struct cred *saved_cred;
  368. struct path path;
  369. uint64_t ni_size;
  370. int ret;
  371. object = container_of(op->object, struct cachefiles_object, fscache);
  372. cache = container_of(object->fscache.cache,
  373. struct cachefiles_cache, cache);
  374. op->object->cookie->def->get_attr(op->object->cookie->netfs_data,
  375. &ni_size);
  376. _enter("{OBJ%x},[%llu]",
  377. op->object->debug_id, (unsigned long long)ni_size);
  378. if (object->backer) {
  379. ASSERT(S_ISREG(object->backer->d_inode->i_mode));
  380. fscache_set_store_limit(&object->fscache, ni_size);
  381. path.dentry = object->backer;
  382. path.mnt = cache->mnt;
  383. cachefiles_begin_secure(cache, &saved_cred);
  384. ret = vfs_truncate(&path, 0);
  385. if (ret == 0)
  386. ret = vfs_truncate(&path, ni_size);
  387. cachefiles_end_secure(cache, saved_cred);
  388. if (ret != 0) {
  389. fscache_set_store_limit(&object->fscache, 0);
  390. if (ret == -EIO)
  391. cachefiles_io_error_obj(object,
  392. "Invalidate failed");
  393. }
  394. }
  395. fscache_op_complete(op, true);
  396. _leave("");
  397. }
  398. /*
  399. * dissociate a cache from all the pages it was backing
  400. */
  401. static void cachefiles_dissociate_pages(struct fscache_cache *cache)
  402. {
  403. _enter("");
  404. }
  405. const struct fscache_cache_ops cachefiles_cache_ops = {
  406. .name = "cachefiles",
  407. .alloc_object = cachefiles_alloc_object,
  408. .lookup_object = cachefiles_lookup_object,
  409. .lookup_complete = cachefiles_lookup_complete,
  410. .grab_object = cachefiles_grab_object,
  411. .update_object = cachefiles_update_object,
  412. .invalidate_object = cachefiles_invalidate_object,
  413. .drop_object = cachefiles_drop_object,
  414. .put_object = cachefiles_put_object,
  415. .sync_cache = cachefiles_sync_cache,
  416. .attr_changed = cachefiles_attr_changed,
  417. .read_or_alloc_page = cachefiles_read_or_alloc_page,
  418. .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
  419. .allocate_page = cachefiles_allocate_page,
  420. .allocate_pages = cachefiles_allocate_pages,
  421. .write_page = cachefiles_write_page,
  422. .uncache_page = cachefiles_uncache_page,
  423. .dissociate_pages = cachefiles_dissociate_pages,
  424. };