cell.c 24 KB

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