interface.c 15 KB

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