volume.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. rwlock_init(&volume->cb_v_break_lock);
  43. memcpy(volume->name, vldb->name, vldb->name_len + 1);
  44. slist = afs_alloc_server_list(params->cell, params->key, vldb, type_mask);
  45. if (IS_ERR(slist)) {
  46. ret = PTR_ERR(slist);
  47. goto error_1;
  48. }
  49. refcount_set(&slist->usage, 1);
  50. volume->servers = slist;
  51. return volume;
  52. error_1:
  53. afs_put_cell(params->net, volume->cell);
  54. kfree(volume);
  55. error_0:
  56. return ERR_PTR(ret);
  57. }
  58. /*
  59. * Look up a VLDB record for a volume.
  60. */
  61. static struct afs_vldb_entry *afs_vl_lookup_vldb(struct afs_cell *cell,
  62. struct key *key,
  63. const char *volname,
  64. size_t volnamesz)
  65. {
  66. struct afs_addr_cursor ac;
  67. struct afs_vldb_entry *vldb;
  68. int ret;
  69. ret = afs_set_vl_cursor(&ac, cell);
  70. if (ret < 0)
  71. return ERR_PTR(ret);
  72. while (afs_iterate_addresses(&ac)) {
  73. if (!test_bit(ac.index, &ac.alist->probed)) {
  74. ret = afs_vl_get_capabilities(cell->net, &ac, key);
  75. switch (ret) {
  76. case VL_SERVICE:
  77. clear_bit(ac.index, &ac.alist->yfs);
  78. set_bit(ac.index, &ac.alist->probed);
  79. ac.addr->srx_service = ret;
  80. break;
  81. case YFS_VL_SERVICE:
  82. set_bit(ac.index, &ac.alist->yfs);
  83. set_bit(ac.index, &ac.alist->probed);
  84. ac.addr->srx_service = ret;
  85. break;
  86. }
  87. }
  88. vldb = afs_vl_get_entry_by_name_u(cell->net, &ac, key,
  89. volname, volnamesz);
  90. switch (ac.error) {
  91. case 0:
  92. afs_end_cursor(&ac);
  93. return vldb;
  94. case -ECONNABORTED:
  95. ac.error = afs_abort_to_error(ac.abort_code);
  96. goto error;
  97. case -ENOMEM:
  98. case -ENONET:
  99. goto error;
  100. case -ENETUNREACH:
  101. case -EHOSTUNREACH:
  102. case -ECONNREFUSED:
  103. break;
  104. default:
  105. ac.error = -EIO;
  106. goto error;
  107. }
  108. }
  109. error:
  110. return ERR_PTR(afs_end_cursor(&ac));
  111. }
  112. /*
  113. * Look up a volume in the VL server and create a candidate volume record for
  114. * it.
  115. *
  116. * The volume name can be one of the following:
  117. * "%[cell:]volume[.]" R/W volume
  118. * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
  119. * or R/W (rwparent=1) volume
  120. * "%[cell:]volume.readonly" R/O volume
  121. * "#[cell:]volume.readonly" R/O volume
  122. * "%[cell:]volume.backup" Backup volume
  123. * "#[cell:]volume.backup" Backup volume
  124. *
  125. * The cell name is optional, and defaults to the current cell.
  126. *
  127. * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
  128. * Guide
  129. * - Rule 1: Explicit type suffix forces access of that type or nothing
  130. * (no suffix, then use Rule 2 & 3)
  131. * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
  132. * if not available
  133. * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
  134. * explicitly told otherwise
  135. */
  136. struct afs_volume *afs_create_volume(struct afs_mount_params *params)
  137. {
  138. struct afs_vldb_entry *vldb;
  139. struct afs_volume *volume;
  140. unsigned long type_mask = 1UL << params->type;
  141. vldb = afs_vl_lookup_vldb(params->cell, params->key,
  142. params->volname, params->volnamesz);
  143. if (IS_ERR(vldb))
  144. return ERR_CAST(vldb);
  145. if (test_bit(AFS_VLDB_QUERY_ERROR, &vldb->flags)) {
  146. volume = ERR_PTR(vldb->error);
  147. goto error;
  148. }
  149. /* Make the final decision on the type we want */
  150. volume = ERR_PTR(-ENOMEDIUM);
  151. if (params->force) {
  152. if (!(vldb->flags & type_mask))
  153. goto error;
  154. } else if (test_bit(AFS_VLDB_HAS_RO, &vldb->flags)) {
  155. params->type = AFSVL_ROVOL;
  156. } else if (test_bit(AFS_VLDB_HAS_RW, &vldb->flags)) {
  157. params->type = AFSVL_RWVOL;
  158. } else {
  159. goto error;
  160. }
  161. type_mask = 1UL << params->type;
  162. volume = afs_alloc_volume(params, vldb, type_mask);
  163. error:
  164. kfree(vldb);
  165. return volume;
  166. }
  167. /*
  168. * Destroy a volume record
  169. */
  170. static void afs_destroy_volume(struct afs_net *net, struct afs_volume *volume)
  171. {
  172. _enter("%p", volume);
  173. #ifdef CONFIG_AFS_FSCACHE
  174. ASSERTCMP(volume->cache, ==, NULL);
  175. #endif
  176. afs_put_serverlist(net, volume->servers);
  177. afs_put_cell(net, volume->cell);
  178. kfree(volume);
  179. _leave(" [destroyed]");
  180. }
  181. /*
  182. * Drop a reference on a volume record.
  183. */
  184. void afs_put_volume(struct afs_cell *cell, struct afs_volume *volume)
  185. {
  186. if (volume) {
  187. _enter("%s", volume->name);
  188. if (atomic_dec_and_test(&volume->usage))
  189. afs_destroy_volume(cell->net, volume);
  190. }
  191. }
  192. /*
  193. * Activate a volume.
  194. */
  195. void afs_activate_volume(struct afs_volume *volume)
  196. {
  197. #ifdef CONFIG_AFS_FSCACHE
  198. volume->cache = fscache_acquire_cookie(volume->cell->cache,
  199. &afs_volume_cache_index_def,
  200. &volume->vid, sizeof(volume->vid),
  201. NULL, 0,
  202. volume, 0, true);
  203. #endif
  204. write_lock(&volume->cell->proc_lock);
  205. list_add_tail(&volume->proc_link, &volume->cell->proc_volumes);
  206. write_unlock(&volume->cell->proc_lock);
  207. }
  208. /*
  209. * Deactivate a volume.
  210. */
  211. void afs_deactivate_volume(struct afs_volume *volume)
  212. {
  213. _enter("%s", volume->name);
  214. write_lock(&volume->cell->proc_lock);
  215. list_del_init(&volume->proc_link);
  216. write_unlock(&volume->cell->proc_lock);
  217. #ifdef CONFIG_AFS_FSCACHE
  218. fscache_relinquish_cookie(volume->cache, NULL,
  219. test_bit(AFS_VOLUME_DELETED, &volume->flags));
  220. volume->cache = NULL;
  221. #endif
  222. _leave("");
  223. }
  224. /*
  225. * Query the VL service to update the volume status.
  226. */
  227. static int afs_update_volume_status(struct afs_volume *volume, struct key *key)
  228. {
  229. struct afs_server_list *new, *old, *discard;
  230. struct afs_vldb_entry *vldb;
  231. char idbuf[16];
  232. int ret, idsz;
  233. _enter("");
  234. /* We look up an ID by passing it as a decimal string in the
  235. * operation's name parameter.
  236. */
  237. idsz = sprintf(idbuf, "%u", volume->vid);
  238. vldb = afs_vl_lookup_vldb(volume->cell, key, idbuf, idsz);
  239. if (IS_ERR(vldb)) {
  240. ret = PTR_ERR(vldb);
  241. goto error;
  242. }
  243. /* See if the volume got renamed. */
  244. if (vldb->name_len != volume->name_len ||
  245. memcmp(vldb->name, volume->name, vldb->name_len) != 0) {
  246. /* TODO: Use RCU'd string. */
  247. memcpy(volume->name, vldb->name, AFS_MAXVOLNAME);
  248. volume->name_len = vldb->name_len;
  249. }
  250. /* See if the volume's server list got updated. */
  251. new = afs_alloc_server_list(volume->cell, key,
  252. vldb, (1 << volume->type));
  253. if (IS_ERR(new)) {
  254. ret = PTR_ERR(new);
  255. goto error_vldb;
  256. }
  257. write_lock(&volume->servers_lock);
  258. discard = new;
  259. old = volume->servers;
  260. if (afs_annotate_server_list(new, old)) {
  261. new->seq = volume->servers_seq + 1;
  262. volume->servers = new;
  263. smp_wmb();
  264. volume->servers_seq++;
  265. discard = old;
  266. }
  267. volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
  268. clear_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
  269. write_unlock(&volume->servers_lock);
  270. ret = 0;
  271. afs_put_serverlist(volume->cell->net, discard);
  272. error_vldb:
  273. kfree(vldb);
  274. error:
  275. _leave(" = %d", ret);
  276. return ret;
  277. }
  278. /*
  279. * Make sure the volume record is up to date.
  280. */
  281. int afs_check_volume_status(struct afs_volume *volume, struct key *key)
  282. {
  283. time64_t now = ktime_get_real_seconds();
  284. int ret, retries = 0;
  285. _enter("");
  286. if (volume->update_at <= now)
  287. set_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
  288. retry:
  289. if (!test_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags) &&
  290. !test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
  291. _leave(" = 0");
  292. return 0;
  293. }
  294. if (!test_and_set_bit_lock(AFS_VOLUME_UPDATING, &volume->flags)) {
  295. ret = afs_update_volume_status(volume, key);
  296. clear_bit_unlock(AFS_VOLUME_WAIT, &volume->flags);
  297. clear_bit_unlock(AFS_VOLUME_UPDATING, &volume->flags);
  298. wake_up_bit(&volume->flags, AFS_VOLUME_WAIT);
  299. _leave(" = %d", ret);
  300. return ret;
  301. }
  302. if (!test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
  303. _leave(" = 0 [no wait]");
  304. return 0;
  305. }
  306. ret = wait_on_bit(&volume->flags, AFS_VOLUME_WAIT, TASK_INTERRUPTIBLE);
  307. if (ret == -ERESTARTSYS) {
  308. _leave(" = %d", ret);
  309. return ret;
  310. }
  311. retries++;
  312. if (retries == 4) {
  313. _leave(" = -ESTALE");
  314. return -ESTALE;
  315. }
  316. goto retry;
  317. }