volume.c 12 KB

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