cell.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS cell and server record management
  3. *
  4. * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/key.h>
  9. #include <linux/ctype.h>
  10. #include <linux/dns_resolver.h>
  11. #include <linux/sched.h>
  12. #include <linux/inet.h>
  13. #include <linux/namei.h>
  14. #include <keys/rxrpc-type.h>
  15. #include "internal.h"
  16. static unsigned __read_mostly afs_cell_gc_delay = 10;
  17. static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
  18. static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
  19. static atomic_t cell_debug_id;
  20. static void afs_queue_cell_manager(struct afs_net *);
  21. static void afs_manage_cell_work(struct work_struct *);
  22. static void afs_dec_cells_outstanding(struct afs_net *net)
  23. {
  24. if (atomic_dec_and_test(&net->cells_outstanding))
  25. wake_up_var(&net->cells_outstanding);
  26. }
  27. /*
  28. * Set the cell timer to fire after a given delay, assuming it's not already
  29. * set for an earlier time.
  30. */
  31. static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
  32. {
  33. if (net->live) {
  34. atomic_inc(&net->cells_outstanding);
  35. if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
  36. afs_dec_cells_outstanding(net);
  37. } else {
  38. afs_queue_cell_manager(net);
  39. }
  40. }
  41. /*
  42. * Look up and get an activation reference on a cell record. The caller must
  43. * hold net->cells_lock at least read-locked.
  44. */
  45. static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
  46. const char *name, unsigned int namesz,
  47. enum afs_cell_trace reason)
  48. {
  49. struct afs_cell *cell = NULL;
  50. struct rb_node *p;
  51. int n;
  52. _enter("%*.*s", namesz, namesz, name);
  53. if (name && namesz == 0)
  54. return ERR_PTR(-EINVAL);
  55. if (namesz > AFS_MAXCELLNAME)
  56. return ERR_PTR(-ENAMETOOLONG);
  57. if (!name) {
  58. cell = net->ws_cell;
  59. if (!cell)
  60. return ERR_PTR(-EDESTADDRREQ);
  61. goto found;
  62. }
  63. p = net->cells.rb_node;
  64. while (p) {
  65. cell = rb_entry(p, struct afs_cell, net_node);
  66. n = strncasecmp(cell->name, name,
  67. min_t(size_t, cell->name_len, namesz));
  68. if (n == 0)
  69. n = cell->name_len - namesz;
  70. if (n < 0)
  71. p = p->rb_left;
  72. else if (n > 0)
  73. p = p->rb_right;
  74. else
  75. goto found;
  76. }
  77. return ERR_PTR(-ENOENT);
  78. found:
  79. return afs_use_cell(cell, reason);
  80. }
  81. /*
  82. * Look up and get an activation reference on a cell record.
  83. */
  84. struct afs_cell *afs_find_cell(struct afs_net *net,
  85. const char *name, unsigned int namesz,
  86. enum afs_cell_trace reason)
  87. {
  88. struct afs_cell *cell;
  89. down_read(&net->cells_lock);
  90. cell = afs_find_cell_locked(net, name, namesz, reason);
  91. up_read(&net->cells_lock);
  92. return cell;
  93. }
  94. /*
  95. * Set up a cell record and fill in its name, VL server address list and
  96. * allocate an anonymous key
  97. */
  98. static struct afs_cell *afs_alloc_cell(struct afs_net *net,
  99. const char *name, unsigned int namelen,
  100. const char *addresses)
  101. {
  102. struct afs_vlserver_list *vllist;
  103. struct afs_cell *cell;
  104. int i, ret;
  105. ASSERT(name);
  106. if (namelen == 0)
  107. return ERR_PTR(-EINVAL);
  108. if (namelen > AFS_MAXCELLNAME) {
  109. _leave(" = -ENAMETOOLONG");
  110. return ERR_PTR(-ENAMETOOLONG);
  111. }
  112. /* Prohibit cell names that contain unprintable chars, '/' and '@' or
  113. * that begin with a dot. This also precludes "@cell".
  114. */
  115. if (name[0] == '.')
  116. return ERR_PTR(-EINVAL);
  117. for (i = 0; i < namelen; i++) {
  118. char ch = name[i];
  119. if (!isprint(ch) || ch == '/' || ch == '@')
  120. return ERR_PTR(-EINVAL);
  121. }
  122. _enter("%*.*s,%s", namelen, namelen, name, addresses);
  123. cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
  124. if (!cell) {
  125. _leave(" = -ENOMEM");
  126. return ERR_PTR(-ENOMEM);
  127. }
  128. cell->name = kmalloc(namelen + 1, GFP_KERNEL);
  129. if (!cell->name) {
  130. kfree(cell);
  131. return ERR_PTR(-ENOMEM);
  132. }
  133. cell->net = net;
  134. cell->name_len = namelen;
  135. for (i = 0; i < namelen; i++)
  136. cell->name[i] = tolower(name[i]);
  137. cell->name[i] = 0;
  138. refcount_set(&cell->ref, 1);
  139. atomic_set(&cell->active, 0);
  140. INIT_WORK(&cell->manager, afs_manage_cell_work);
  141. init_rwsem(&cell->vs_lock);
  142. cell->volumes = RB_ROOT;
  143. INIT_HLIST_HEAD(&cell->proc_volumes);
  144. seqlock_init(&cell->volume_lock);
  145. cell->fs_servers = RB_ROOT;
  146. seqlock_init(&cell->fs_lock);
  147. rwlock_init(&cell->vl_servers_lock);
  148. cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
  149. /* Provide a VL server list, filling it in if we were given a list of
  150. * addresses to use.
  151. */
  152. if (addresses) {
  153. vllist = afs_parse_text_addrs(net,
  154. addresses, strlen(addresses), ':',
  155. VL_SERVICE, AFS_VL_PORT);
  156. if (IS_ERR(vllist)) {
  157. ret = PTR_ERR(vllist);
  158. goto parse_failed;
  159. }
  160. vllist->source = DNS_RECORD_FROM_CONFIG;
  161. vllist->status = DNS_LOOKUP_NOT_DONE;
  162. cell->dns_expiry = TIME64_MAX;
  163. } else {
  164. ret = -ENOMEM;
  165. vllist = afs_alloc_vlserver_list(0);
  166. if (!vllist)
  167. goto error;
  168. vllist->source = DNS_RECORD_UNAVAILABLE;
  169. vllist->status = DNS_LOOKUP_NOT_DONE;
  170. cell->dns_expiry = ktime_get_real_seconds();
  171. }
  172. rcu_assign_pointer(cell->vl_servers, vllist);
  173. cell->dns_source = vllist->source;
  174. cell->dns_status = vllist->status;
  175. smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
  176. atomic_inc(&net->cells_outstanding);
  177. cell->debug_id = atomic_inc_return(&cell_debug_id);
  178. trace_afs_cell(cell->debug_id, 1, 0, afs_cell_trace_alloc);
  179. _leave(" = %p", cell);
  180. return cell;
  181. parse_failed:
  182. if (ret == -EINVAL)
  183. printk(KERN_ERR "kAFS: bad VL server IP address\n");
  184. error:
  185. kfree(cell->name);
  186. kfree(cell);
  187. _leave(" = %d", ret);
  188. return ERR_PTR(ret);
  189. }
  190. /*
  191. * afs_lookup_cell - Look up or create a cell record.
  192. * @net: The network namespace
  193. * @name: The name of the cell.
  194. * @namesz: The strlen of the cell name.
  195. * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
  196. * @excl: T if an error should be given if the cell name already exists.
  197. *
  198. * Look up a cell record by name and query the DNS for VL server addresses if
  199. * needed. Note that that actual DNS query is punted off to the manager thread
  200. * so that this function can return immediately if interrupted whilst allowing
  201. * cell records to be shared even if not yet fully constructed.
  202. */
  203. struct afs_cell *afs_lookup_cell(struct afs_net *net,
  204. const char *name, unsigned int namesz,
  205. const char *vllist, bool excl)
  206. {
  207. struct afs_cell *cell, *candidate, *cursor;
  208. struct rb_node *parent, **pp;
  209. enum afs_cell_state state;
  210. int ret, n;
  211. _enter("%s,%s", name, vllist);
  212. if (!excl) {
  213. cell = afs_find_cell(net, name, namesz, afs_cell_trace_use_lookup);
  214. if (!IS_ERR(cell))
  215. goto wait_for_cell;
  216. }
  217. /* Assume we're probably going to create a cell and preallocate and
  218. * mostly set up a candidate record. We can then use this to stash the
  219. * name, the net namespace and VL server addresses.
  220. *
  221. * We also want to do this before we hold any locks as it may involve
  222. * upcalling to userspace to make DNS queries.
  223. */
  224. candidate = afs_alloc_cell(net, name, namesz, vllist);
  225. if (IS_ERR(candidate)) {
  226. _leave(" = %ld", PTR_ERR(candidate));
  227. return candidate;
  228. }
  229. /* Find the insertion point and check to see if someone else added a
  230. * cell whilst we were allocating.
  231. */
  232. down_write(&net->cells_lock);
  233. pp = &net->cells.rb_node;
  234. parent = NULL;
  235. while (*pp) {
  236. parent = *pp;
  237. cursor = rb_entry(parent, struct afs_cell, net_node);
  238. n = strncasecmp(cursor->name, name,
  239. min_t(size_t, cursor->name_len, namesz));
  240. if (n == 0)
  241. n = cursor->name_len - namesz;
  242. if (n < 0)
  243. pp = &(*pp)->rb_left;
  244. else if (n > 0)
  245. pp = &(*pp)->rb_right;
  246. else
  247. goto cell_already_exists;
  248. }
  249. cell = candidate;
  250. candidate = NULL;
  251. atomic_set(&cell->active, 2);
  252. trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 2, afs_cell_trace_insert);
  253. rb_link_node_rcu(&cell->net_node, parent, pp);
  254. rb_insert_color(&cell->net_node, &net->cells);
  255. up_write(&net->cells_lock);
  256. afs_queue_cell(cell, afs_cell_trace_get_queue_new);
  257. wait_for_cell:
  258. trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), atomic_read(&cell->active),
  259. afs_cell_trace_wait);
  260. _debug("wait_for_cell");
  261. wait_var_event(&cell->state,
  262. ({
  263. state = smp_load_acquire(&cell->state); /* vs error */
  264. state == AFS_CELL_ACTIVE || state == AFS_CELL_REMOVED;
  265. }));
  266. /* Check the state obtained from the wait check. */
  267. if (state == AFS_CELL_REMOVED) {
  268. ret = cell->error;
  269. goto error;
  270. }
  271. _leave(" = %p [cell]", cell);
  272. return cell;
  273. cell_already_exists:
  274. _debug("cell exists");
  275. cell = cursor;
  276. if (excl) {
  277. ret = -EEXIST;
  278. } else {
  279. afs_use_cell(cursor, afs_cell_trace_use_lookup);
  280. ret = 0;
  281. }
  282. up_write(&net->cells_lock);
  283. if (candidate)
  284. afs_put_cell(candidate, afs_cell_trace_put_candidate);
  285. if (ret == 0)
  286. goto wait_for_cell;
  287. goto error_noput;
  288. error:
  289. afs_unuse_cell(net, cell, afs_cell_trace_unuse_lookup);
  290. error_noput:
  291. _leave(" = %d [error]", ret);
  292. return ERR_PTR(ret);
  293. }
  294. /*
  295. * set the root cell information
  296. * - can be called with a module parameter string
  297. * - can be called from a write to /proc/fs/afs/rootcell
  298. */
  299. int afs_cell_init(struct afs_net *net, const char *rootcell)
  300. {
  301. struct afs_cell *old_root, *new_root;
  302. const char *cp, *vllist;
  303. size_t len;
  304. _enter("");
  305. if (!rootcell) {
  306. /* module is loaded with no parameters, or built statically.
  307. * - in the future we might initialize cell DB here.
  308. */
  309. _leave(" = 0 [no root]");
  310. return 0;
  311. }
  312. cp = strchr(rootcell, ':');
  313. if (!cp) {
  314. _debug("kAFS: no VL server IP addresses specified");
  315. vllist = NULL;
  316. len = strlen(rootcell);
  317. } else {
  318. vllist = cp + 1;
  319. len = cp - rootcell;
  320. }
  321. /* allocate a cell record for the root cell */
  322. new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
  323. if (IS_ERR(new_root)) {
  324. _leave(" = %ld", PTR_ERR(new_root));
  325. return PTR_ERR(new_root);
  326. }
  327. if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
  328. afs_use_cell(new_root, afs_cell_trace_use_pin);
  329. /* install the new cell */
  330. down_write(&net->cells_lock);
  331. afs_see_cell(new_root, afs_cell_trace_see_ws);
  332. old_root = net->ws_cell;
  333. net->ws_cell = new_root;
  334. up_write(&net->cells_lock);
  335. afs_unuse_cell(net, old_root, afs_cell_trace_unuse_ws);
  336. _leave(" = 0");
  337. return 0;
  338. }
  339. /*
  340. * Update a cell's VL server address list from the DNS.
  341. */
  342. static int afs_update_cell(struct afs_cell *cell)
  343. {
  344. struct afs_vlserver_list *vllist, *old = NULL, *p;
  345. unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
  346. unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
  347. time64_t now, expiry = 0;
  348. int ret = 0;
  349. _enter("%s", cell->name);
  350. vllist = afs_dns_query(cell, &expiry);
  351. if (IS_ERR(vllist)) {
  352. ret = PTR_ERR(vllist);
  353. _debug("%s: fail %d", cell->name, ret);
  354. if (ret == -ENOMEM)
  355. goto out_wake;
  356. vllist = afs_alloc_vlserver_list(0);
  357. if (!vllist) {
  358. if (ret >= 0)
  359. ret = -ENOMEM;
  360. goto out_wake;
  361. }
  362. switch (ret) {
  363. case -ENODATA:
  364. case -EDESTADDRREQ:
  365. vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
  366. break;
  367. case -EAGAIN:
  368. case -ECONNREFUSED:
  369. vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
  370. break;
  371. default:
  372. vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
  373. break;
  374. }
  375. }
  376. _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
  377. cell->dns_status = vllist->status;
  378. now = ktime_get_real_seconds();
  379. if (min_ttl > max_ttl)
  380. max_ttl = min_ttl;
  381. if (expiry < now + min_ttl)
  382. expiry = now + min_ttl;
  383. else if (expiry > now + max_ttl)
  384. expiry = now + max_ttl;
  385. _debug("%s: status %d", cell->name, vllist->status);
  386. if (vllist->source == DNS_RECORD_UNAVAILABLE) {
  387. switch (vllist->status) {
  388. case DNS_LOOKUP_GOT_NOT_FOUND:
  389. /* The DNS said that the cell does not exist or there
  390. * weren't any addresses to be had.
  391. */
  392. cell->dns_expiry = expiry;
  393. break;
  394. case DNS_LOOKUP_BAD:
  395. case DNS_LOOKUP_GOT_LOCAL_FAILURE:
  396. case DNS_LOOKUP_GOT_TEMP_FAILURE:
  397. case DNS_LOOKUP_GOT_NS_FAILURE:
  398. default:
  399. cell->dns_expiry = now + 10;
  400. break;
  401. }
  402. } else {
  403. cell->dns_expiry = expiry;
  404. }
  405. /* Replace the VL server list if the new record has servers or the old
  406. * record doesn't.
  407. */
  408. write_lock(&cell->vl_servers_lock);
  409. p = rcu_dereference_protected(cell->vl_servers, true);
  410. if (vllist->nr_servers > 0 || p->nr_servers == 0) {
  411. rcu_assign_pointer(cell->vl_servers, vllist);
  412. cell->dns_source = vllist->source;
  413. old = p;
  414. }
  415. write_unlock(&cell->vl_servers_lock);
  416. afs_put_vlserverlist(cell->net, old);
  417. out_wake:
  418. smp_store_release(&cell->dns_lookup_count,
  419. cell->dns_lookup_count + 1); /* vs source/status */
  420. wake_up_var(&cell->dns_lookup_count);
  421. _leave(" = %d", ret);
  422. return ret;
  423. }
  424. /*
  425. * Destroy a cell record
  426. */
  427. static void afs_cell_destroy(struct rcu_head *rcu)
  428. {
  429. struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
  430. struct afs_net *net = cell->net;
  431. int r;
  432. _enter("%p{%s}", cell, cell->name);
  433. r = refcount_read(&cell->ref);
  434. ASSERTCMP(r, ==, 0);
  435. trace_afs_cell(cell->debug_id, r, atomic_read(&cell->active), afs_cell_trace_free);
  436. afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
  437. afs_unuse_cell(net, cell->alias_of, afs_cell_trace_unuse_alias);
  438. key_put(cell->anonymous_key);
  439. kfree(cell->name);
  440. kfree(cell);
  441. afs_dec_cells_outstanding(net);
  442. _leave(" [destroyed]");
  443. }
  444. /*
  445. * Queue the cell manager.
  446. */
  447. static void afs_queue_cell_manager(struct afs_net *net)
  448. {
  449. int outstanding = atomic_inc_return(&net->cells_outstanding);
  450. _enter("%d", outstanding);
  451. if (!queue_work(afs_wq, &net->cells_manager))
  452. afs_dec_cells_outstanding(net);
  453. }
  454. /*
  455. * Cell management timer. We have an increment on cells_outstanding that we
  456. * need to pass along to the work item.
  457. */
  458. void afs_cells_timer(struct timer_list *timer)
  459. {
  460. struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
  461. _enter("");
  462. if (!queue_work(afs_wq, &net->cells_manager))
  463. afs_dec_cells_outstanding(net);
  464. }
  465. /*
  466. * Get a reference on a cell record.
  467. */
  468. struct afs_cell *afs_get_cell(struct afs_cell *cell, enum afs_cell_trace reason)
  469. {
  470. int r;
  471. __refcount_inc(&cell->ref, &r);
  472. trace_afs_cell(cell->debug_id, r + 1, atomic_read(&cell->active), reason);
  473. return cell;
  474. }
  475. /*
  476. * Drop a reference on a cell record.
  477. */
  478. void afs_put_cell(struct afs_cell *cell, enum afs_cell_trace reason)
  479. {
  480. if (cell) {
  481. unsigned int debug_id = cell->debug_id;
  482. unsigned int a;
  483. bool zero;
  484. int r;
  485. a = atomic_read(&cell->active);
  486. zero = __refcount_dec_and_test(&cell->ref, &r);
  487. trace_afs_cell(debug_id, r - 1, a, reason);
  488. if (zero) {
  489. a = atomic_read(&cell->active);
  490. WARN(a != 0, "Cell active count %u > 0\n", a);
  491. call_rcu(&cell->rcu, afs_cell_destroy);
  492. }
  493. }
  494. }
  495. /*
  496. * Note a cell becoming more active.
  497. */
  498. struct afs_cell *afs_use_cell(struct afs_cell *cell, enum afs_cell_trace reason)
  499. {
  500. int r, a;
  501. r = refcount_read(&cell->ref);
  502. WARN_ON(r == 0);
  503. a = atomic_inc_return(&cell->active);
  504. trace_afs_cell(cell->debug_id, r, a, reason);
  505. return cell;
  506. }
  507. /*
  508. * Record a cell becoming less active. When the active counter reaches 1, it
  509. * is scheduled for destruction, but may get reactivated.
  510. */
  511. void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell, enum afs_cell_trace reason)
  512. {
  513. unsigned int debug_id;
  514. time64_t now, expire_delay;
  515. int r, a;
  516. if (!cell)
  517. return;
  518. _enter("%s", cell->name);
  519. now = ktime_get_real_seconds();
  520. cell->last_inactive = now;
  521. expire_delay = 0;
  522. if (cell->vl_servers->nr_servers)
  523. expire_delay = afs_cell_gc_delay;
  524. debug_id = cell->debug_id;
  525. r = refcount_read(&cell->ref);
  526. a = atomic_dec_return(&cell->active);
  527. trace_afs_cell(debug_id, r, a, reason);
  528. WARN_ON(a == 0);
  529. if (a == 1)
  530. /* 'cell' may now be garbage collected. */
  531. afs_set_cell_timer(net, expire_delay);
  532. }
  533. /*
  534. * Note that a cell has been seen.
  535. */
  536. void afs_see_cell(struct afs_cell *cell, enum afs_cell_trace reason)
  537. {
  538. int r, a;
  539. r = refcount_read(&cell->ref);
  540. a = atomic_read(&cell->active);
  541. trace_afs_cell(cell->debug_id, r, a, reason);
  542. }
  543. /*
  544. * Queue a cell for management, giving the workqueue a ref to hold.
  545. */
  546. void afs_queue_cell(struct afs_cell *cell, enum afs_cell_trace reason)
  547. {
  548. afs_get_cell(cell, reason);
  549. if (!queue_work(afs_wq, &cell->manager))
  550. afs_put_cell(cell, afs_cell_trace_put_queue_fail);
  551. }
  552. /*
  553. * Allocate a key to use as a placeholder for anonymous user security.
  554. */
  555. static int afs_alloc_anon_key(struct afs_cell *cell)
  556. {
  557. struct key *key;
  558. char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
  559. /* Create a key to represent an anonymous user. */
  560. memcpy(keyname, "afs@", 4);
  561. dp = keyname + 4;
  562. cp = cell->name;
  563. do {
  564. *dp++ = tolower(*cp);
  565. } while (*cp++);
  566. key = rxrpc_get_null_key(keyname);
  567. if (IS_ERR(key))
  568. return PTR_ERR(key);
  569. cell->anonymous_key = key;
  570. _debug("anon key %p{%x}",
  571. cell->anonymous_key, key_serial(cell->anonymous_key));
  572. return 0;
  573. }
  574. /*
  575. * Activate a cell.
  576. */
  577. static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
  578. {
  579. struct hlist_node **p;
  580. struct afs_cell *pcell;
  581. int ret;
  582. if (!cell->anonymous_key) {
  583. ret = afs_alloc_anon_key(cell);
  584. if (ret < 0)
  585. return ret;
  586. }
  587. ret = afs_proc_cell_setup(cell);
  588. if (ret < 0)
  589. return ret;
  590. mutex_lock(&net->proc_cells_lock);
  591. for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
  592. pcell = hlist_entry(*p, struct afs_cell, proc_link);
  593. if (strcmp(cell->name, pcell->name) < 0)
  594. break;
  595. }
  596. cell->proc_link.pprev = p;
  597. cell->proc_link.next = *p;
  598. rcu_assign_pointer(*p, &cell->proc_link.next);
  599. if (cell->proc_link.next)
  600. cell->proc_link.next->pprev = &cell->proc_link.next;
  601. afs_dynroot_mkdir(net, cell);
  602. mutex_unlock(&net->proc_cells_lock);
  603. return 0;
  604. }
  605. /*
  606. * Deactivate a cell.
  607. */
  608. static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
  609. {
  610. _enter("%s", cell->name);
  611. afs_proc_cell_remove(cell);
  612. mutex_lock(&net->proc_cells_lock);
  613. hlist_del_rcu(&cell->proc_link);
  614. afs_dynroot_rmdir(net, cell);
  615. mutex_unlock(&net->proc_cells_lock);
  616. _leave("");
  617. }
  618. /*
  619. * Manage a cell record, initialising and destroying it, maintaining its DNS
  620. * records.
  621. */
  622. static void afs_manage_cell(struct afs_cell *cell)
  623. {
  624. struct afs_net *net = cell->net;
  625. int ret, active;
  626. _enter("%s", cell->name);
  627. again:
  628. _debug("state %u", cell->state);
  629. switch (cell->state) {
  630. case AFS_CELL_INACTIVE:
  631. case AFS_CELL_FAILED:
  632. down_write(&net->cells_lock);
  633. active = 1;
  634. if (atomic_try_cmpxchg_relaxed(&cell->active, &active, 0)) {
  635. rb_erase(&cell->net_node, &net->cells);
  636. trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 0,
  637. afs_cell_trace_unuse_delete);
  638. smp_store_release(&cell->state, AFS_CELL_REMOVED);
  639. }
  640. up_write(&net->cells_lock);
  641. if (cell->state == AFS_CELL_REMOVED) {
  642. wake_up_var(&cell->state);
  643. goto final_destruction;
  644. }
  645. if (cell->state == AFS_CELL_FAILED)
  646. goto done;
  647. smp_store_release(&cell->state, AFS_CELL_UNSET);
  648. wake_up_var(&cell->state);
  649. goto again;
  650. case AFS_CELL_UNSET:
  651. smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
  652. wake_up_var(&cell->state);
  653. goto again;
  654. case AFS_CELL_ACTIVATING:
  655. ret = afs_activate_cell(net, cell);
  656. if (ret < 0)
  657. goto activation_failed;
  658. smp_store_release(&cell->state, AFS_CELL_ACTIVE);
  659. wake_up_var(&cell->state);
  660. goto again;
  661. case AFS_CELL_ACTIVE:
  662. if (atomic_read(&cell->active) > 1) {
  663. if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
  664. ret = afs_update_cell(cell);
  665. if (ret < 0)
  666. cell->error = ret;
  667. }
  668. goto done;
  669. }
  670. smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
  671. wake_up_var(&cell->state);
  672. goto again;
  673. case AFS_CELL_DEACTIVATING:
  674. if (atomic_read(&cell->active) > 1)
  675. goto reverse_deactivation;
  676. afs_deactivate_cell(net, cell);
  677. smp_store_release(&cell->state, AFS_CELL_INACTIVE);
  678. wake_up_var(&cell->state);
  679. goto again;
  680. case AFS_CELL_REMOVED:
  681. goto done;
  682. default:
  683. break;
  684. }
  685. _debug("bad state %u", cell->state);
  686. BUG(); /* Unhandled state */
  687. activation_failed:
  688. cell->error = ret;
  689. afs_deactivate_cell(net, cell);
  690. smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
  691. wake_up_var(&cell->state);
  692. goto again;
  693. reverse_deactivation:
  694. smp_store_release(&cell->state, AFS_CELL_ACTIVE);
  695. wake_up_var(&cell->state);
  696. _leave(" [deact->act]");
  697. return;
  698. done:
  699. _leave(" [done %u]", cell->state);
  700. return;
  701. final_destruction:
  702. /* The root volume is pinning the cell */
  703. afs_put_volume(cell->root_volume, afs_volume_trace_put_cell_root);
  704. cell->root_volume = NULL;
  705. afs_put_cell(cell, afs_cell_trace_put_destroy);
  706. }
  707. static void afs_manage_cell_work(struct work_struct *work)
  708. {
  709. struct afs_cell *cell = container_of(work, struct afs_cell, manager);
  710. afs_manage_cell(cell);
  711. afs_put_cell(cell, afs_cell_trace_put_queue_work);
  712. }
  713. /*
  714. * Manage the records of cells known to a network namespace. This includes
  715. * updating the DNS records and garbage collecting unused cells that were
  716. * automatically added.
  717. *
  718. * Note that constructed cell records may only be removed from net->cells by
  719. * this work item, so it is safe for this work item to stash a cursor pointing
  720. * into the tree and then return to caller (provided it skips cells that are
  721. * still under construction).
  722. *
  723. * Note also that we were given an increment on net->cells_outstanding by
  724. * whoever queued us that we need to deal with before returning.
  725. */
  726. void afs_manage_cells(struct work_struct *work)
  727. {
  728. struct afs_net *net = container_of(work, struct afs_net, cells_manager);
  729. struct rb_node *cursor;
  730. time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
  731. bool purging = !net->live;
  732. _enter("");
  733. /* Trawl the cell database looking for cells that have expired from
  734. * lack of use and cells whose DNS results have expired and dispatch
  735. * their managers.
  736. */
  737. down_read(&net->cells_lock);
  738. for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
  739. struct afs_cell *cell =
  740. rb_entry(cursor, struct afs_cell, net_node);
  741. unsigned active;
  742. bool sched_cell = false;
  743. active = atomic_read(&cell->active);
  744. trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
  745. active, afs_cell_trace_manage);
  746. ASSERTCMP(active, >=, 1);
  747. if (purging) {
  748. if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) {
  749. active = atomic_dec_return(&cell->active);
  750. trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
  751. active, afs_cell_trace_unuse_pin);
  752. }
  753. }
  754. if (active == 1) {
  755. struct afs_vlserver_list *vllist;
  756. time64_t expire_at = cell->last_inactive;
  757. read_lock(&cell->vl_servers_lock);
  758. vllist = rcu_dereference_protected(
  759. cell->vl_servers,
  760. lockdep_is_held(&cell->vl_servers_lock));
  761. if (vllist->nr_servers > 0)
  762. expire_at += afs_cell_gc_delay;
  763. read_unlock(&cell->vl_servers_lock);
  764. if (purging || expire_at <= now)
  765. sched_cell = true;
  766. else if (expire_at < next_manage)
  767. next_manage = expire_at;
  768. }
  769. if (!purging) {
  770. if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
  771. sched_cell = true;
  772. }
  773. if (sched_cell)
  774. afs_queue_cell(cell, afs_cell_trace_get_queue_manage);
  775. }
  776. up_read(&net->cells_lock);
  777. /* Update the timer on the way out. We have to pass an increment on
  778. * cells_outstanding in the namespace that we are in to the timer or
  779. * the work scheduler.
  780. */
  781. if (!purging && next_manage < TIME64_MAX) {
  782. now = ktime_get_real_seconds();
  783. if (next_manage - now <= 0) {
  784. if (queue_work(afs_wq, &net->cells_manager))
  785. atomic_inc(&net->cells_outstanding);
  786. } else {
  787. afs_set_cell_timer(net, next_manage - now);
  788. }
  789. }
  790. afs_dec_cells_outstanding(net);
  791. _leave(" [%d]", atomic_read(&net->cells_outstanding));
  792. }
  793. /*
  794. * Purge in-memory cell database.
  795. */
  796. void afs_cell_purge(struct afs_net *net)
  797. {
  798. struct afs_cell *ws;
  799. _enter("");
  800. down_write(&net->cells_lock);
  801. ws = net->ws_cell;
  802. net->ws_cell = NULL;
  803. up_write(&net->cells_lock);
  804. afs_unuse_cell(net, ws, afs_cell_trace_unuse_ws);
  805. _debug("del timer");
  806. if (del_timer_sync(&net->cells_timer))
  807. atomic_dec(&net->cells_outstanding);
  808. _debug("kick mgr");
  809. afs_queue_cell_manager(net);
  810. _debug("wait");
  811. wait_var_event(&net->cells_outstanding,
  812. !atomic_read(&net->cells_outstanding));
  813. _leave("");
  814. }