volume.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* AFS volume management
  2. *
  3. * Copyright (C) 2002, 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 License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include "internal.h"
  14. unsigned __read_mostly afs_volume_gc_delay = 10;
  15. unsigned __read_mostly afs_volume_record_life = 60 * 60;
  16. static const char *const afs_voltypes[] = { "R/W", "R/O", "BAK" };
  17. /*
  18. * Allocate a volume record and load it up from a vldb record.
  19. */
  20. static struct afs_volume *afs_alloc_volume(struct afs_mount_params *params,
  21. struct afs_vldb_entry *vldb,
  22. unsigned long type_mask)
  23. {
  24. struct afs_server_list *slist;
  25. struct afs_volume *volume;
  26. int ret = -ENOMEM, nr_servers = 0, i;
  27. for (i = 0; i < vldb->nr_servers; i++)
  28. if (vldb->fs_mask[i] & type_mask)
  29. nr_servers++;
  30. volume = kzalloc(sizeof(struct afs_volume), GFP_KERNEL);
  31. if (!volume)
  32. goto error_0;
  33. volume->vid = vldb->vid[params->type];
  34. volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
  35. volume->cell = afs_get_cell(params->cell);
  36. volume->type = params->type;
  37. volume->type_force = params->force;
  38. volume->name_len = vldb->name_len;
  39. atomic_set(&volume->usage, 1);
  40. INIT_LIST_HEAD(&volume->proc_link);
  41. rwlock_init(&volume->servers_lock);
  42. memcpy(volume->name, vldb->name, vldb->name_len + 1);
  43. slist = afs_alloc_server_list(params->cell, params->key, vldb, type_mask);
  44. if (IS_ERR(slist)) {
  45. ret = PTR_ERR(slist);
  46. goto error_1;
  47. }
  48. refcount_set(&slist->usage, 1);
  49. volume->servers = slist;
  50. return volume;
  51. error_1:
  52. afs_put_cell(params->net, volume->cell);
  53. kfree(volume);
  54. error_0:
  55. return ERR_PTR(ret);
  56. }
  57. /*
  58. * Look up a VLDB record for a volume.
  59. */
  60. static struct afs_vldb_entry *afs_vl_lookup_vldb(struct afs_cell *cell,
  61. struct key *key,
  62. const char *volname,
  63. size_t volnamesz)
  64. {
  65. struct afs_vldb_entry *vldb = ERR_PTR(-EDESTADDRREQ);
  66. struct afs_vl_cursor vc;
  67. int ret;
  68. if (!afs_begin_vlserver_operation(&vc, cell, key))
  69. return ERR_PTR(-ERESTARTSYS);
  70. while (afs_select_vlserver(&vc)) {
  71. vldb = afs_vl_get_entry_by_name_u(&vc, volname, volnamesz);
  72. }
  73. ret = afs_end_vlserver_operation(&vc);
  74. return ret < 0 ? ERR_PTR(ret) : vldb;
  75. }
  76. /*
  77. * Look up a volume in the VL server and create a candidate volume record for
  78. * it.
  79. *
  80. * The volume name can be one of the following:
  81. * "%[cell:]volume[.]" R/W volume
  82. * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
  83. * or R/W (rwparent=1) volume
  84. * "%[cell:]volume.readonly" R/O volume
  85. * "#[cell:]volume.readonly" R/O volume
  86. * "%[cell:]volume.backup" Backup volume
  87. * "#[cell:]volume.backup" Backup volume
  88. *
  89. * The cell name is optional, and defaults to the current cell.
  90. *
  91. * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
  92. * Guide
  93. * - Rule 1: Explicit type suffix forces access of that type or nothing
  94. * (no suffix, then use Rule 2 & 3)
  95. * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
  96. * if not available
  97. * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
  98. * explicitly told otherwise
  99. */
  100. struct afs_volume *afs_create_volume(struct afs_mount_params *params)
  101. {
  102. struct afs_vldb_entry *vldb;
  103. struct afs_volume *volume;
  104. unsigned long type_mask = 1UL << params->type;
  105. vldb = afs_vl_lookup_vldb(params->cell, params->key,
  106. params->volname, params->volnamesz);
  107. if (IS_ERR(vldb))
  108. return ERR_CAST(vldb);
  109. if (test_bit(AFS_VLDB_QUERY_ERROR, &vldb->flags)) {
  110. volume = ERR_PTR(vldb->error);
  111. goto error;
  112. }
  113. /* Make the final decision on the type we want */
  114. volume = ERR_PTR(-ENOMEDIUM);
  115. if (params->force) {
  116. if (!(vldb->flags & type_mask))
  117. goto error;
  118. } else if (test_bit(AFS_VLDB_HAS_RO, &vldb->flags)) {
  119. params->type = AFSVL_ROVOL;
  120. } else if (test_bit(AFS_VLDB_HAS_RW, &vldb->flags)) {
  121. params->type = AFSVL_RWVOL;
  122. } else {
  123. goto error;
  124. }
  125. type_mask = 1UL << params->type;
  126. volume = afs_alloc_volume(params, vldb, type_mask);
  127. error:
  128. kfree(vldb);
  129. return volume;
  130. }
  131. /*
  132. * Destroy a volume record
  133. */
  134. static void afs_destroy_volume(struct afs_net *net, struct afs_volume *volume)
  135. {
  136. _enter("%p", volume);
  137. #ifdef CONFIG_AFS_FSCACHE
  138. ASSERTCMP(volume->cache, ==, NULL);
  139. #endif
  140. afs_put_serverlist(net, volume->servers);
  141. afs_put_cell(net, volume->cell);
  142. kfree(volume);
  143. _leave(" [destroyed]");
  144. }
  145. /*
  146. * Drop a reference on a volume record.
  147. */
  148. void afs_put_volume(struct afs_cell *cell, struct afs_volume *volume)
  149. {
  150. if (volume) {
  151. _enter("%s", volume->name);
  152. if (atomic_dec_and_test(&volume->usage))
  153. afs_destroy_volume(cell->net, volume);
  154. }
  155. }
  156. /*
  157. * Activate a volume.
  158. */
  159. void afs_activate_volume(struct afs_volume *volume)
  160. {
  161. #ifdef CONFIG_AFS_FSCACHE
  162. volume->cache = fscache_acquire_cookie(volume->cell->cache,
  163. &afs_volume_cache_index_def,
  164. &volume->vid, sizeof(volume->vid),
  165. NULL, 0,
  166. volume, 0, true);
  167. #endif
  168. write_lock(&volume->cell->proc_lock);
  169. list_add_tail(&volume->proc_link, &volume->cell->proc_volumes);
  170. write_unlock(&volume->cell->proc_lock);
  171. }
  172. /*
  173. * Deactivate a volume.
  174. */
  175. void afs_deactivate_volume(struct afs_volume *volume)
  176. {
  177. _enter("%s", volume->name);
  178. write_lock(&volume->cell->proc_lock);
  179. list_del_init(&volume->proc_link);
  180. write_unlock(&volume->cell->proc_lock);
  181. #ifdef CONFIG_AFS_FSCACHE
  182. fscache_relinquish_cookie(volume->cache, NULL,
  183. test_bit(AFS_VOLUME_DELETED, &volume->flags));
  184. volume->cache = NULL;
  185. #endif
  186. _leave("");
  187. }
  188. /*
  189. * Query the VL service to update the volume status.
  190. */
  191. static int afs_update_volume_status(struct afs_volume *volume, struct key *key)
  192. {
  193. struct afs_server_list *new, *old, *discard;
  194. struct afs_vldb_entry *vldb;
  195. char idbuf[16];
  196. int ret, idsz;
  197. _enter("");
  198. /* We look up an ID by passing it as a decimal string in the
  199. * operation's name parameter.
  200. */
  201. idsz = sprintf(idbuf, "%llu", volume->vid);
  202. vldb = afs_vl_lookup_vldb(volume->cell, key, idbuf, idsz);
  203. if (IS_ERR(vldb)) {
  204. ret = PTR_ERR(vldb);
  205. goto error;
  206. }
  207. /* See if the volume got renamed. */
  208. if (vldb->name_len != volume->name_len ||
  209. memcmp(vldb->name, volume->name, vldb->name_len) != 0) {
  210. /* TODO: Use RCU'd string. */
  211. memcpy(volume->name, vldb->name, AFS_MAXVOLNAME);
  212. volume->name_len = vldb->name_len;
  213. }
  214. /* See if the volume's server list got updated. */
  215. new = afs_alloc_server_list(volume->cell, key,
  216. vldb, (1 << volume->type));
  217. if (IS_ERR(new)) {
  218. ret = PTR_ERR(new);
  219. goto error_vldb;
  220. }
  221. write_lock(&volume->servers_lock);
  222. discard = new;
  223. old = volume->servers;
  224. if (afs_annotate_server_list(new, old)) {
  225. new->seq = volume->servers_seq + 1;
  226. volume->servers = new;
  227. smp_wmb();
  228. volume->servers_seq++;
  229. discard = old;
  230. }
  231. volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
  232. clear_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
  233. write_unlock(&volume->servers_lock);
  234. ret = 0;
  235. afs_put_serverlist(volume->cell->net, discard);
  236. error_vldb:
  237. kfree(vldb);
  238. error:
  239. _leave(" = %d", ret);
  240. return ret;
  241. }
  242. /*
  243. * Make sure the volume record is up to date.
  244. */
  245. int afs_check_volume_status(struct afs_volume *volume, struct key *key)
  246. {
  247. time64_t now = ktime_get_real_seconds();
  248. int ret, retries = 0;
  249. _enter("");
  250. if (volume->update_at <= now)
  251. set_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
  252. retry:
  253. if (!test_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags) &&
  254. !test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
  255. _leave(" = 0");
  256. return 0;
  257. }
  258. if (!test_and_set_bit_lock(AFS_VOLUME_UPDATING, &volume->flags)) {
  259. ret = afs_update_volume_status(volume, key);
  260. clear_bit_unlock(AFS_VOLUME_WAIT, &volume->flags);
  261. clear_bit_unlock(AFS_VOLUME_UPDATING, &volume->flags);
  262. wake_up_bit(&volume->flags, AFS_VOLUME_WAIT);
  263. _leave(" = %d", ret);
  264. return ret;
  265. }
  266. if (!test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
  267. _leave(" = 0 [no wait]");
  268. return 0;
  269. }
  270. ret = wait_on_bit(&volume->flags, AFS_VOLUME_WAIT, TASK_INTERRUPTIBLE);
  271. if (ret == -ERESTARTSYS) {
  272. _leave(" = %d", ret);
  273. return ret;
  274. }
  275. retries++;
  276. if (retries == 4) {
  277. _leave(" = -ESTALE");
  278. return -ESTALE;
  279. }
  280. goto retry;
  281. }