cell.c 23 KB

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