interface.c 15 KB

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