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. struct inode *inode;
  209. blkcnt_t i_blocks = 0;
  210. ASSERT(_object);
  211. object = container_of(_object, struct cachefiles_object, fscache);
  212. _enter("{OBJ%x,%d}",
  213. object->fscache.debug_id, atomic_read(&object->usage));
  214. cache = container_of(object->fscache.cache,
  215. struct cachefiles_cache, cache);
  216. #ifdef CACHEFILES_DEBUG_SLAB
  217. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  218. #endif
  219. /* We need to tidy the object up if we did in fact manage to open it.
  220. * It's possible for us to get here before the object is fully
  221. * initialised if the parent goes away or the object gets retired
  222. * before we set it up.
  223. */
  224. if (object->dentry) {
  225. /* delete retired objects */
  226. if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
  227. _object != cache->cache.fsdef
  228. ) {
  229. _debug("- retire object OBJ%x", object->fscache.debug_id);
  230. inode = d_backing_inode(object->dentry);
  231. if (inode)
  232. i_blocks = inode->i_blocks;
  233. cachefiles_begin_secure(cache, &saved_cred);
  234. cachefiles_delete_object(cache, object);
  235. cachefiles_end_secure(cache, saved_cred);
  236. }
  237. /* close the filesystem stuff attached to the object */
  238. if (object->backer != object->dentry)
  239. dput(object->backer);
  240. object->backer = NULL;
  241. }
  242. /* note that the object is now inactive */
  243. if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags))
  244. cachefiles_mark_object_inactive(cache, object, i_blocks);
  245. dput(object->dentry);
  246. object->dentry = NULL;
  247. _leave("");
  248. }
  249. /*
  250. * dispose of a reference to an object
  251. */
  252. static void cachefiles_put_object(struct fscache_object *_object)
  253. {
  254. struct cachefiles_object *object;
  255. struct fscache_cache *cache;
  256. ASSERT(_object);
  257. object = container_of(_object, struct cachefiles_object, fscache);
  258. _enter("{OBJ%x,%d}",
  259. object->fscache.debug_id, atomic_read(&object->usage));
  260. #ifdef CACHEFILES_DEBUG_SLAB
  261. ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
  262. #endif
  263. ASSERTIFCMP(object->fscache.parent,
  264. object->fscache.parent->n_children, >, 0);
  265. if (atomic_dec_and_test(&object->usage)) {
  266. _debug("- kill object OBJ%x", object->fscache.debug_id);
  267. ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
  268. ASSERTCMP(object->fscache.parent, ==, NULL);
  269. ASSERTCMP(object->backer, ==, NULL);
  270. ASSERTCMP(object->dentry, ==, NULL);
  271. ASSERTCMP(object->fscache.n_ops, ==, 0);
  272. ASSERTCMP(object->fscache.n_children, ==, 0);
  273. if (object->lookup_data) {
  274. kfree(object->lookup_data->key);
  275. kfree(object->lookup_data->auxdata);
  276. kfree(object->lookup_data);
  277. object->lookup_data = NULL;
  278. }
  279. cache = object->fscache.cache;
  280. fscache_object_destroy(&object->fscache);
  281. kmem_cache_free(cachefiles_object_jar, object);
  282. fscache_object_destroyed(cache);
  283. }
  284. _leave("");
  285. }
  286. /*
  287. * sync a cache
  288. */
  289. static void cachefiles_sync_cache(struct fscache_cache *_cache)
  290. {
  291. struct cachefiles_cache *cache;
  292. const struct cred *saved_cred;
  293. int ret;
  294. _enter("%p", _cache);
  295. cache = container_of(_cache, struct cachefiles_cache, cache);
  296. /* make sure all pages pinned by operations on behalf of the netfs are
  297. * written to disc */
  298. cachefiles_begin_secure(cache, &saved_cred);
  299. down_read(&cache->mnt->mnt_sb->s_umount);
  300. ret = sync_filesystem(cache->mnt->mnt_sb);
  301. up_read(&cache->mnt->mnt_sb->s_umount);
  302. cachefiles_end_secure(cache, saved_cred);
  303. if (ret == -EIO)
  304. cachefiles_io_error(cache,
  305. "Attempt to sync backing fs superblock"
  306. " returned error %d",
  307. ret);
  308. }
  309. /*
  310. * check if the backing cache is updated to FS-Cache
  311. * - called by FS-Cache when evaluates if need to invalidate the cache
  312. */
  313. static int cachefiles_check_consistency(struct fscache_operation *op)
  314. {
  315. struct cachefiles_object *object;
  316. struct cachefiles_cache *cache;
  317. const struct cred *saved_cred;
  318. int ret;
  319. _enter("{OBJ%x}", op->object->debug_id);
  320. object = container_of(op->object, struct cachefiles_object, fscache);
  321. cache = container_of(object->fscache.cache,
  322. struct cachefiles_cache, cache);
  323. cachefiles_begin_secure(cache, &saved_cred);
  324. ret = cachefiles_check_auxdata(object);
  325. cachefiles_end_secure(cache, saved_cred);
  326. _leave(" = %d", ret);
  327. return ret;
  328. }
  329. /*
  330. * notification the attributes on an object have changed
  331. * - called with reads/writes excluded by FS-Cache
  332. */
  333. static int cachefiles_attr_changed(struct fscache_object *_object)
  334. {
  335. struct cachefiles_object *object;
  336. struct cachefiles_cache *cache;
  337. const struct cred *saved_cred;
  338. struct iattr newattrs;
  339. uint64_t ni_size;
  340. loff_t oi_size;
  341. int ret;
  342. _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
  343. _enter("{OBJ%x},[%llu]",
  344. _object->debug_id, (unsigned long long) ni_size);
  345. object = container_of(_object, struct cachefiles_object, fscache);
  346. cache = container_of(object->fscache.cache,
  347. struct cachefiles_cache, cache);
  348. if (ni_size == object->i_size)
  349. return 0;
  350. if (!object->backer)
  351. return -ENOBUFS;
  352. ASSERT(d_is_reg(object->backer));
  353. fscache_set_store_limit(&object->fscache, ni_size);
  354. oi_size = i_size_read(d_backing_inode(object->backer));
  355. if (oi_size == ni_size)
  356. return 0;
  357. cachefiles_begin_secure(cache, &saved_cred);
  358. inode_lock(d_inode(object->backer));
  359. /* if there's an extension to a partial page at the end of the backing
  360. * file, we need to discard the partial page so that we pick up new
  361. * data after it */
  362. if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
  363. _debug("discard tail %llx", oi_size);
  364. newattrs.ia_valid = ATTR_SIZE;
  365. newattrs.ia_size = oi_size & PAGE_MASK;
  366. ret = notify_change(object->backer, &newattrs, NULL);
  367. if (ret < 0)
  368. goto truncate_failed;
  369. }
  370. newattrs.ia_valid = ATTR_SIZE;
  371. newattrs.ia_size = ni_size;
  372. ret = notify_change(object->backer, &newattrs, NULL);
  373. truncate_failed:
  374. inode_unlock(d_inode(object->backer));
  375. cachefiles_end_secure(cache, saved_cred);
  376. if (ret == -EIO) {
  377. fscache_set_store_limit(&object->fscache, 0);
  378. cachefiles_io_error_obj(object, "Size set failed");
  379. ret = -ENOBUFS;
  380. }
  381. _leave(" = %d", ret);
  382. return ret;
  383. }
  384. /*
  385. * Invalidate an object
  386. */
  387. static void cachefiles_invalidate_object(struct fscache_operation *op)
  388. {
  389. struct cachefiles_object *object;
  390. struct cachefiles_cache *cache;
  391. const struct cred *saved_cred;
  392. struct path path;
  393. uint64_t ni_size;
  394. int ret;
  395. object = container_of(op->object, struct cachefiles_object, fscache);
  396. cache = container_of(object->fscache.cache,
  397. struct cachefiles_cache, cache);
  398. op->object->cookie->def->get_attr(op->object->cookie->netfs_data,
  399. &ni_size);
  400. _enter("{OBJ%x},[%llu]",
  401. op->object->debug_id, (unsigned long long)ni_size);
  402. if (object->backer) {
  403. ASSERT(d_is_reg(object->backer));
  404. fscache_set_store_limit(&object->fscache, ni_size);
  405. path.dentry = object->backer;
  406. path.mnt = cache->mnt;
  407. cachefiles_begin_secure(cache, &saved_cred);
  408. ret = vfs_truncate(&path, 0);
  409. if (ret == 0)
  410. ret = vfs_truncate(&path, ni_size);
  411. cachefiles_end_secure(cache, saved_cred);
  412. if (ret != 0) {
  413. fscache_set_store_limit(&object->fscache, 0);
  414. if (ret == -EIO)
  415. cachefiles_io_error_obj(object,
  416. "Invalidate failed");
  417. }
  418. }
  419. fscache_op_complete(op, true);
  420. _leave("");
  421. }
  422. /*
  423. * dissociate a cache from all the pages it was backing
  424. */
  425. static void cachefiles_dissociate_pages(struct fscache_cache *cache)
  426. {
  427. _enter("");
  428. }
  429. const struct fscache_cache_ops cachefiles_cache_ops = {
  430. .name = "cachefiles",
  431. .alloc_object = cachefiles_alloc_object,
  432. .lookup_object = cachefiles_lookup_object,
  433. .lookup_complete = cachefiles_lookup_complete,
  434. .grab_object = cachefiles_grab_object,
  435. .update_object = cachefiles_update_object,
  436. .invalidate_object = cachefiles_invalidate_object,
  437. .drop_object = cachefiles_drop_object,
  438. .put_object = cachefiles_put_object,
  439. .sync_cache = cachefiles_sync_cache,
  440. .attr_changed = cachefiles_attr_changed,
  441. .read_or_alloc_page = cachefiles_read_or_alloc_page,
  442. .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
  443. .allocate_page = cachefiles_allocate_page,
  444. .allocate_pages = cachefiles_allocate_pages,
  445. .write_page = cachefiles_write_page,
  446. .uncache_page = cachefiles_uncache_page,
  447. .dissociate_pages = cachefiles_dissociate_pages,
  448. .check_consistency = cachefiles_check_consistency,
  449. };