volume.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS volume management
  3. *
  4. * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include "internal.h"
  10. static unsigned __read_mostly afs_volume_record_life = 60 * 60;
  11. static atomic_t afs_volume_debug_id;
  12. static void afs_destroy_volume(struct work_struct *work);
  13. /*
  14. * Insert a volume into a cell. If there's an existing volume record, that is
  15. * returned instead with a ref held.
  16. */
  17. static struct afs_volume *afs_insert_volume_into_cell(struct afs_cell *cell,
  18. struct afs_volume *volume)
  19. {
  20. struct afs_volume *p;
  21. struct rb_node *parent = NULL, **pp;
  22. write_seqlock(&cell->volume_lock);
  23. pp = &cell->volumes.rb_node;
  24. while (*pp) {
  25. parent = *pp;
  26. p = rb_entry(parent, struct afs_volume, cell_node);
  27. if (p->vid < volume->vid) {
  28. pp = &(*pp)->rb_left;
  29. } else if (p->vid > volume->vid) {
  30. pp = &(*pp)->rb_right;
  31. } else {
  32. if (afs_try_get_volume(p, afs_volume_trace_get_cell_insert)) {
  33. volume = p;
  34. goto found;
  35. }
  36. set_bit(AFS_VOLUME_RM_TREE, &volume->flags);
  37. rb_replace_node_rcu(&p->cell_node, &volume->cell_node, &cell->volumes);
  38. }
  39. }
  40. rb_link_node_rcu(&volume->cell_node, parent, pp);
  41. rb_insert_color(&volume->cell_node, &cell->volumes);
  42. hlist_add_head_rcu(&volume->proc_link, &cell->proc_volumes);
  43. found:
  44. write_sequnlock(&cell->volume_lock);
  45. return volume;
  46. }
  47. static void afs_remove_volume_from_cell(struct afs_volume *volume)
  48. {
  49. struct afs_cell *cell = volume->cell;
  50. if (!hlist_unhashed(&volume->proc_link)) {
  51. trace_afs_volume(volume->debug_id, volume->vid, refcount_read(&volume->ref),
  52. afs_volume_trace_remove);
  53. write_seqlock(&cell->volume_lock);
  54. hlist_del_rcu(&volume->proc_link);
  55. if (!test_and_set_bit(AFS_VOLUME_RM_TREE, &volume->flags))
  56. rb_erase(&volume->cell_node, &cell->volumes);
  57. write_sequnlock(&cell->volume_lock);
  58. }
  59. }
  60. /*
  61. * Allocate a volume record and load it up from a vldb record.
  62. */
  63. static struct afs_volume *afs_alloc_volume(struct afs_fs_context *params,
  64. struct afs_vldb_entry *vldb,
  65. struct afs_server_list **_slist)
  66. {
  67. struct afs_server_list *slist;
  68. struct afs_volume *volume;
  69. int ret = -ENOMEM, i;
  70. volume = kzalloc_obj(struct afs_volume);
  71. if (!volume)
  72. goto error_0;
  73. volume->debug_id = atomic_inc_return(&afs_volume_debug_id);
  74. volume->vid = vldb->vid[params->type];
  75. volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
  76. volume->cell = afs_get_cell(params->cell, afs_cell_trace_get_vol);
  77. volume->type = params->type;
  78. volume->type_force = params->force;
  79. volume->name_len = vldb->name_len;
  80. volume->creation_time = TIME64_MIN;
  81. volume->update_time = TIME64_MIN;
  82. refcount_set(&volume->ref, 1);
  83. INIT_HLIST_NODE(&volume->proc_link);
  84. INIT_WORK(&volume->destructor, afs_destroy_volume);
  85. rwlock_init(&volume->servers_lock);
  86. mutex_init(&volume->volsync_lock);
  87. mutex_init(&volume->cb_check_lock);
  88. rwlock_init(&volume->cb_v_break_lock);
  89. INIT_LIST_HEAD(&volume->open_mmaps);
  90. init_rwsem(&volume->open_mmaps_lock);
  91. memcpy(volume->name, vldb->name, vldb->name_len + 1);
  92. for (i = 0; i < AFS_MAXTYPES; i++)
  93. volume->vids[i] = vldb->vid[i];
  94. slist = afs_alloc_server_list(volume, params->key, vldb);
  95. if (IS_ERR(slist)) {
  96. ret = PTR_ERR(slist);
  97. goto error_1;
  98. }
  99. *_slist = slist;
  100. rcu_assign_pointer(volume->servers, slist);
  101. trace_afs_volume(volume->debug_id, volume->vid, 1, afs_volume_trace_alloc);
  102. return volume;
  103. error_1:
  104. afs_put_cell(volume->cell, afs_cell_trace_put_vol);
  105. kfree(volume);
  106. error_0:
  107. return ERR_PTR(ret);
  108. }
  109. /*
  110. * Look up or allocate a volume record.
  111. */
  112. static struct afs_volume *afs_lookup_volume(struct afs_fs_context *params,
  113. struct afs_vldb_entry *vldb)
  114. {
  115. struct afs_server_list *slist;
  116. struct afs_volume *candidate, *volume;
  117. candidate = afs_alloc_volume(params, vldb, &slist);
  118. if (IS_ERR(candidate))
  119. return candidate;
  120. volume = afs_insert_volume_into_cell(params->cell, candidate);
  121. if (volume == candidate)
  122. afs_attach_volume_to_servers(volume, slist);
  123. else
  124. afs_put_volume(candidate, afs_volume_trace_put_cell_dup);
  125. return volume;
  126. }
  127. /*
  128. * Look up a VLDB record for a volume.
  129. */
  130. static struct afs_vldb_entry *afs_vl_lookup_vldb(struct afs_cell *cell,
  131. struct key *key,
  132. const char *volname,
  133. size_t volnamesz)
  134. {
  135. struct afs_vldb_entry *vldb = ERR_PTR(-EDESTADDRREQ);
  136. struct afs_vl_cursor vc;
  137. int ret;
  138. if (!afs_begin_vlserver_operation(&vc, cell, key))
  139. return ERR_PTR(-ERESTARTSYS);
  140. while (afs_select_vlserver(&vc)) {
  141. vldb = afs_vl_get_entry_by_name_u(&vc, volname, volnamesz);
  142. }
  143. ret = afs_end_vlserver_operation(&vc);
  144. return ret < 0 ? ERR_PTR(ret) : vldb;
  145. }
  146. /*
  147. * Look up a volume in the VL server and create a candidate volume record for
  148. * it.
  149. *
  150. * The volume name can be one of the following:
  151. * "%[cell:]volume[.]" R/W volume
  152. * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
  153. * or R/W (rwparent=1) volume
  154. * "%[cell:]volume.readonly" R/O volume
  155. * "#[cell:]volume.readonly" R/O volume
  156. * "%[cell:]volume.backup" Backup volume
  157. * "#[cell:]volume.backup" Backup volume
  158. *
  159. * The cell name is optional, and defaults to the current cell.
  160. *
  161. * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
  162. * Guide
  163. * - Rule 1: Explicit type suffix forces access of that type or nothing
  164. * (no suffix, then use Rule 2 & 3)
  165. * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
  166. * if not available
  167. * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
  168. * explicitly told otherwise
  169. */
  170. struct afs_volume *afs_create_volume(struct afs_fs_context *params)
  171. {
  172. struct afs_vldb_entry *vldb;
  173. struct afs_volume *volume;
  174. unsigned long type_mask = 1UL << params->type;
  175. vldb = afs_vl_lookup_vldb(params->cell, params->key,
  176. params->volname, params->volnamesz);
  177. if (IS_ERR(vldb))
  178. return ERR_CAST(vldb);
  179. if (test_bit(AFS_VLDB_QUERY_ERROR, &vldb->flags)) {
  180. volume = ERR_PTR(vldb->error);
  181. goto error;
  182. }
  183. /* Make the final decision on the type we want */
  184. volume = ERR_PTR(-ENOMEDIUM);
  185. if (params->force) {
  186. if (!(vldb->flags & type_mask))
  187. goto error;
  188. } else if (test_bit(AFS_VLDB_HAS_RO, &vldb->flags)) {
  189. params->type = AFSVL_ROVOL;
  190. } else if (test_bit(AFS_VLDB_HAS_RW, &vldb->flags)) {
  191. params->type = AFSVL_RWVOL;
  192. } else {
  193. goto error;
  194. }
  195. volume = afs_lookup_volume(params, vldb);
  196. error:
  197. kfree(vldb);
  198. return volume;
  199. }
  200. /*
  201. * Destroy a volume record
  202. */
  203. static void afs_destroy_volume(struct work_struct *work)
  204. {
  205. struct afs_volume *volume = container_of(work, struct afs_volume, destructor);
  206. struct afs_server_list *slist = rcu_access_pointer(volume->servers);
  207. _enter("%p", volume);
  208. #ifdef CONFIG_AFS_FSCACHE
  209. ASSERTCMP(volume->cache, ==, NULL);
  210. #endif
  211. afs_detach_volume_from_servers(volume, slist);
  212. afs_remove_volume_from_cell(volume);
  213. afs_put_serverlist(volume->cell->net, slist);
  214. afs_put_cell(volume->cell, afs_cell_trace_put_vol);
  215. trace_afs_volume(volume->debug_id, volume->vid, refcount_read(&volume->ref),
  216. afs_volume_trace_free);
  217. kfree_rcu(volume, rcu);
  218. _leave(" [destroyed]");
  219. }
  220. /*
  221. * Try to get a reference on a volume record.
  222. */
  223. bool afs_try_get_volume(struct afs_volume *volume, enum afs_volume_trace reason)
  224. {
  225. int r;
  226. if (__refcount_inc_not_zero(&volume->ref, &r)) {
  227. trace_afs_volume(volume->debug_id, volume->vid, r + 1, reason);
  228. return true;
  229. }
  230. return false;
  231. }
  232. /*
  233. * Get a reference on a volume record.
  234. */
  235. struct afs_volume *afs_get_volume(struct afs_volume *volume,
  236. enum afs_volume_trace reason)
  237. {
  238. if (volume) {
  239. int r;
  240. __refcount_inc(&volume->ref, &r);
  241. trace_afs_volume(volume->debug_id, volume->vid, r + 1, reason);
  242. }
  243. return volume;
  244. }
  245. /*
  246. * Drop a reference on a volume record.
  247. */
  248. void afs_put_volume(struct afs_volume *volume, enum afs_volume_trace reason)
  249. {
  250. if (volume) {
  251. unsigned int debug_id = volume->debug_id;
  252. afs_volid_t vid = volume->vid;
  253. bool zero;
  254. int r;
  255. zero = __refcount_dec_and_test(&volume->ref, &r);
  256. trace_afs_volume(debug_id, vid, r - 1, reason);
  257. if (zero)
  258. schedule_work(&volume->destructor);
  259. }
  260. }
  261. /*
  262. * Activate a volume.
  263. */
  264. int afs_activate_volume(struct afs_volume *volume)
  265. {
  266. #ifdef CONFIG_AFS_FSCACHE
  267. struct fscache_volume *vcookie;
  268. char *name;
  269. name = kasprintf(GFP_KERNEL, "afs,%s,%llx",
  270. volume->cell->name, volume->vid);
  271. if (!name)
  272. return -ENOMEM;
  273. vcookie = fscache_acquire_volume(name, NULL, NULL, 0);
  274. if (IS_ERR(vcookie)) {
  275. if (vcookie != ERR_PTR(-EBUSY)) {
  276. kfree(name);
  277. return PTR_ERR(vcookie);
  278. }
  279. pr_err("AFS: Cache volume key already in use (%s)\n", name);
  280. vcookie = NULL;
  281. }
  282. volume->cache = vcookie;
  283. kfree(name);
  284. #endif
  285. return 0;
  286. }
  287. /*
  288. * Deactivate a volume.
  289. */
  290. void afs_deactivate_volume(struct afs_volume *volume)
  291. {
  292. _enter("%s", volume->name);
  293. #ifdef CONFIG_AFS_FSCACHE
  294. fscache_relinquish_volume(volume->cache, NULL,
  295. test_bit(AFS_VOLUME_DELETED, &volume->flags));
  296. volume->cache = NULL;
  297. #endif
  298. _leave("");
  299. }
  300. /*
  301. * Query the VL service to update the volume status.
  302. */
  303. static int afs_update_volume_status(struct afs_volume *volume, struct key *key)
  304. {
  305. struct afs_server_list *new, *old, *discard;
  306. struct afs_vldb_entry *vldb;
  307. char idbuf[24];
  308. int ret, idsz;
  309. _enter("");
  310. /* We look up an ID by passing it as a decimal string in the
  311. * operation's name parameter.
  312. */
  313. idsz = snprintf(idbuf, sizeof(idbuf), "%llu", volume->vid);
  314. vldb = afs_vl_lookup_vldb(volume->cell, key, idbuf, idsz);
  315. if (IS_ERR(vldb)) {
  316. ret = PTR_ERR(vldb);
  317. goto error;
  318. }
  319. /* See if the volume got renamed. */
  320. if (vldb->name_len != volume->name_len ||
  321. memcmp(vldb->name, volume->name, vldb->name_len) != 0) {
  322. /* TODO: Use RCU'd string. */
  323. memcpy(volume->name, vldb->name, AFS_MAXVOLNAME);
  324. volume->name_len = vldb->name_len;
  325. }
  326. /* See if the volume's server list got updated. */
  327. new = afs_alloc_server_list(volume, key, vldb);
  328. if (IS_ERR(new)) {
  329. ret = PTR_ERR(new);
  330. goto error_vldb;
  331. }
  332. write_lock(&volume->servers_lock);
  333. discard = new;
  334. old = rcu_dereference_protected(volume->servers,
  335. lockdep_is_held(&volume->servers_lock));
  336. if (afs_annotate_server_list(new, old)) {
  337. new->seq = volume->servers_seq + 1;
  338. rcu_assign_pointer(volume->servers, new);
  339. smp_wmb();
  340. volume->servers_seq++;
  341. discard = old;
  342. }
  343. /* Check more often if replication is ongoing. */
  344. if (new->ro_replicating)
  345. volume->update_at = ktime_get_real_seconds() + 10 * 60;
  346. else
  347. volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
  348. write_unlock(&volume->servers_lock);
  349. if (discard == old)
  350. afs_reattach_volume_to_servers(volume, new, old);
  351. afs_put_serverlist(volume->cell->net, discard);
  352. ret = 0;
  353. error_vldb:
  354. kfree(vldb);
  355. error:
  356. _leave(" = %d", ret);
  357. return ret;
  358. }
  359. /*
  360. * Make sure the volume record is up to date.
  361. */
  362. int afs_check_volume_status(struct afs_volume *volume, struct afs_operation *op)
  363. {
  364. int ret, retries = 0;
  365. _enter("");
  366. retry:
  367. if (test_bit(AFS_VOLUME_WAIT, &volume->flags))
  368. goto wait;
  369. if (volume->update_at <= ktime_get_real_seconds() ||
  370. test_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags))
  371. goto update;
  372. _leave(" = 0");
  373. return 0;
  374. update:
  375. if (!test_and_set_bit_lock(AFS_VOLUME_UPDATING, &volume->flags)) {
  376. clear_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
  377. ret = afs_update_volume_status(volume, op->key);
  378. if (ret < 0)
  379. set_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
  380. clear_bit_unlock(AFS_VOLUME_WAIT, &volume->flags);
  381. clear_bit_unlock(AFS_VOLUME_UPDATING, &volume->flags);
  382. wake_up_bit(&volume->flags, AFS_VOLUME_WAIT);
  383. _leave(" = %d", ret);
  384. return ret;
  385. }
  386. wait:
  387. if (!test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
  388. _leave(" = 0 [no wait]");
  389. return 0;
  390. }
  391. ret = wait_on_bit(&volume->flags, AFS_VOLUME_WAIT,
  392. (op->flags & AFS_OPERATION_UNINTR) ?
  393. TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
  394. if (ret == -ERESTARTSYS) {
  395. _leave(" = %d", ret);
  396. return ret;
  397. }
  398. retries++;
  399. if (retries == 4) {
  400. _leave(" = -ESTALE");
  401. return -ESTALE;
  402. }
  403. goto retry;
  404. }