cell.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* AFS cell and server record management
  2. *
  3. * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/key.h>
  13. #include <linux/ctype.h>
  14. #include <linux/dns_resolver.h>
  15. #include <linux/sched.h>
  16. #include <linux/inet.h>
  17. #include <linux/namei.h>
  18. #include <keys/rxrpc-type.h>
  19. #include "internal.h"
  20. static unsigned __read_mostly afs_cell_gc_delay = 10;
  21. static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
  22. static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
  23. static void afs_manage_cell(struct work_struct *);
  24. static void afs_dec_cells_outstanding(struct afs_net *net)
  25. {
  26. if (atomic_dec_and_test(&net->cells_outstanding))
  27. wake_up_var(&net->cells_outstanding);
  28. }
  29. /*
  30. * Set the cell timer to fire after a given delay, assuming it's not already
  31. * set for an earlier time.
  32. */
  33. static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
  34. {
  35. if (net->live) {
  36. atomic_inc(&net->cells_outstanding);
  37. if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
  38. afs_dec_cells_outstanding(net);
  39. }
  40. }
  41. /*
  42. * Look up and get an activation reference on a cell record under RCU
  43. * conditions. The caller must hold the RCU read lock.
  44. */
  45. struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
  46. const char *name, unsigned int namesz)
  47. {
  48. struct afs_cell *cell = NULL;
  49. struct rb_node *p;
  50. int n, seq = 0, ret = 0;
  51. _enter("%*.*s", namesz, namesz, name);
  52. if (name && namesz == 0)
  53. return ERR_PTR(-EINVAL);
  54. if (namesz > AFS_MAXCELLNAME)
  55. return ERR_PTR(-ENAMETOOLONG);
  56. do {
  57. /* Unfortunately, rbtree walking doesn't give reliable results
  58. * under just the RCU read lock, so we have to check for
  59. * changes.
  60. */
  61. if (cell)
  62. afs_put_cell(net, cell);
  63. cell = NULL;
  64. ret = -ENOENT;
  65. read_seqbegin_or_lock(&net->cells_lock, &seq);
  66. if (!name) {
  67. cell = rcu_dereference_raw(net->ws_cell);
  68. if (cell) {
  69. afs_get_cell(cell);
  70. break;
  71. }
  72. ret = -EDESTADDRREQ;
  73. continue;
  74. }
  75. p = rcu_dereference_raw(net->cells.rb_node);
  76. while (p) {
  77. cell = rb_entry(p, struct afs_cell, net_node);
  78. n = strncasecmp(cell->name, name,
  79. min_t(size_t, cell->name_len, namesz));
  80. if (n == 0)
  81. n = cell->name_len - namesz;
  82. if (n < 0) {
  83. p = rcu_dereference_raw(p->rb_left);
  84. } else if (n > 0) {
  85. p = rcu_dereference_raw(p->rb_right);
  86. } else {
  87. if (atomic_inc_not_zero(&cell->usage)) {
  88. ret = 0;
  89. break;
  90. }
  91. /* We want to repeat the search, this time with
  92. * the lock properly locked.
  93. */
  94. }
  95. cell = NULL;
  96. }
  97. } while (need_seqretry(&net->cells_lock, seq));
  98. done_seqretry(&net->cells_lock, seq);
  99. return ret == 0 ? cell : ERR_PTR(ret);
  100. }
  101. /*
  102. * Set up a cell record and fill in its name, VL server address list and
  103. * allocate an anonymous key
  104. */
  105. static struct afs_cell *afs_alloc_cell(struct afs_net *net,
  106. const char *name, unsigned int namelen,
  107. const char *addresses)
  108. {
  109. struct afs_cell *cell;
  110. int i, ret;
  111. ASSERT(name);
  112. if (namelen == 0)
  113. return ERR_PTR(-EINVAL);
  114. if (namelen > AFS_MAXCELLNAME) {
  115. _leave(" = -ENAMETOOLONG");
  116. return ERR_PTR(-ENAMETOOLONG);
  117. }
  118. if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
  119. return ERR_PTR(-EINVAL);
  120. _enter("%*.*s,%s", namelen, namelen, name, addresses);
  121. cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
  122. if (!cell) {
  123. _leave(" = -ENOMEM");
  124. return ERR_PTR(-ENOMEM);
  125. }
  126. cell->net = net;
  127. cell->name_len = namelen;
  128. for (i = 0; i < namelen; i++)
  129. cell->name[i] = tolower(name[i]);
  130. atomic_set(&cell->usage, 2);
  131. INIT_WORK(&cell->manager, afs_manage_cell);
  132. cell->flags = ((1 << AFS_CELL_FL_NOT_READY) |
  133. (1 << AFS_CELL_FL_NO_LOOKUP_YET));
  134. INIT_LIST_HEAD(&cell->proc_volumes);
  135. rwlock_init(&cell->proc_lock);
  136. rwlock_init(&cell->vl_servers_lock);
  137. /* Fill in the VL server list if we were given a list of addresses to
  138. * use.
  139. */
  140. if (addresses) {
  141. struct afs_vlserver_list *vllist;
  142. vllist = afs_parse_text_addrs(net,
  143. addresses, strlen(addresses), ':',
  144. VL_SERVICE, AFS_VL_PORT);
  145. if (IS_ERR(vllist)) {
  146. ret = PTR_ERR(vllist);
  147. goto parse_failed;
  148. }
  149. rcu_assign_pointer(cell->vl_servers, vllist);
  150. cell->dns_expiry = TIME64_MAX;
  151. } else {
  152. cell->dns_expiry = ktime_get_real_seconds();
  153. }
  154. _leave(" = %p", cell);
  155. return cell;
  156. parse_failed:
  157. if (ret == -EINVAL)
  158. printk(KERN_ERR "kAFS: bad VL server IP address\n");
  159. kfree(cell);
  160. _leave(" = %d", ret);
  161. return ERR_PTR(ret);
  162. }
  163. /*
  164. * afs_lookup_cell - Look up or create a cell record.
  165. * @net: The network namespace
  166. * @name: The name of the cell.
  167. * @namesz: The strlen of the cell name.
  168. * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
  169. * @excl: T if an error should be given if the cell name already exists.
  170. *
  171. * Look up a cell record by name and query the DNS for VL server addresses if
  172. * needed. Note that that actual DNS query is punted off to the manager thread
  173. * so that this function can return immediately if interrupted whilst allowing
  174. * cell records to be shared even if not yet fully constructed.
  175. */
  176. struct afs_cell *afs_lookup_cell(struct afs_net *net,
  177. const char *name, unsigned int namesz,
  178. const char *vllist, bool excl)
  179. {
  180. struct afs_cell *cell, *candidate, *cursor;
  181. struct rb_node *parent, **pp;
  182. int ret, n;
  183. _enter("%s,%s", name, vllist);
  184. if (!excl) {
  185. rcu_read_lock();
  186. cell = afs_lookup_cell_rcu(net, name, namesz);
  187. rcu_read_unlock();
  188. if (!IS_ERR(cell))
  189. goto wait_for_cell;
  190. }
  191. /* Assume we're probably going to create a cell and preallocate and
  192. * mostly set up a candidate record. We can then use this to stash the
  193. * name, the net namespace and VL server addresses.
  194. *
  195. * We also want to do this before we hold any locks as it may involve
  196. * upcalling to userspace to make DNS queries.
  197. */
  198. candidate = afs_alloc_cell(net, name, namesz, vllist);
  199. if (IS_ERR(candidate)) {
  200. _leave(" = %ld", PTR_ERR(candidate));
  201. return candidate;
  202. }
  203. /* Find the insertion point and check to see if someone else added a
  204. * cell whilst we were allocating.
  205. */
  206. write_seqlock(&net->cells_lock);
  207. pp = &net->cells.rb_node;
  208. parent = NULL;
  209. while (*pp) {
  210. parent = *pp;
  211. cursor = rb_entry(parent, struct afs_cell, net_node);
  212. n = strncasecmp(cursor->name, name,
  213. min_t(size_t, cursor->name_len, namesz));
  214. if (n == 0)
  215. n = cursor->name_len - namesz;
  216. if (n < 0)
  217. pp = &(*pp)->rb_left;
  218. else if (n > 0)
  219. pp = &(*pp)->rb_right;
  220. else
  221. goto cell_already_exists;
  222. }
  223. cell = candidate;
  224. candidate = NULL;
  225. rb_link_node_rcu(&cell->net_node, parent, pp);
  226. rb_insert_color(&cell->net_node, &net->cells);
  227. atomic_inc(&net->cells_outstanding);
  228. write_sequnlock(&net->cells_lock);
  229. queue_work(afs_wq, &cell->manager);
  230. wait_for_cell:
  231. _debug("wait_for_cell");
  232. ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE);
  233. smp_rmb();
  234. switch (READ_ONCE(cell->state)) {
  235. case AFS_CELL_FAILED:
  236. ret = cell->error;
  237. goto error;
  238. default:
  239. _debug("weird %u %d", cell->state, cell->error);
  240. goto error;
  241. case AFS_CELL_ACTIVE:
  242. break;
  243. }
  244. _leave(" = %p [cell]", cell);
  245. return cell;
  246. cell_already_exists:
  247. _debug("cell exists");
  248. cell = cursor;
  249. if (excl) {
  250. ret = -EEXIST;
  251. } else {
  252. afs_get_cell(cursor);
  253. ret = 0;
  254. }
  255. write_sequnlock(&net->cells_lock);
  256. kfree(candidate);
  257. if (ret == 0)
  258. goto wait_for_cell;
  259. goto error_noput;
  260. error:
  261. afs_put_cell(net, cell);
  262. error_noput:
  263. _leave(" = %d [error]", ret);
  264. return ERR_PTR(ret);
  265. }
  266. /*
  267. * set the root cell information
  268. * - can be called with a module parameter string
  269. * - can be called from a write to /proc/fs/afs/rootcell
  270. */
  271. int afs_cell_init(struct afs_net *net, const char *rootcell)
  272. {
  273. struct afs_cell *old_root, *new_root;
  274. const char *cp, *vllist;
  275. size_t len;
  276. _enter("");
  277. if (!rootcell) {
  278. /* module is loaded with no parameters, or built statically.
  279. * - in the future we might initialize cell DB here.
  280. */
  281. _leave(" = 0 [no root]");
  282. return 0;
  283. }
  284. cp = strchr(rootcell, ':');
  285. if (!cp) {
  286. _debug("kAFS: no VL server IP addresses specified");
  287. vllist = NULL;
  288. len = strlen(rootcell);
  289. } else {
  290. vllist = cp + 1;
  291. len = cp - rootcell;
  292. }
  293. /* allocate a cell record for the root cell */
  294. new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
  295. if (IS_ERR(new_root)) {
  296. _leave(" = %ld", PTR_ERR(new_root));
  297. return PTR_ERR(new_root);
  298. }
  299. if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
  300. afs_get_cell(new_root);
  301. /* install the new cell */
  302. write_seqlock(&net->cells_lock);
  303. old_root = rcu_access_pointer(net->ws_cell);
  304. rcu_assign_pointer(net->ws_cell, new_root);
  305. write_sequnlock(&net->cells_lock);
  306. afs_put_cell(net, old_root);
  307. _leave(" = 0");
  308. return 0;
  309. }
  310. /*
  311. * Update a cell's VL server address list from the DNS.
  312. */
  313. static void afs_update_cell(struct afs_cell *cell)
  314. {
  315. struct afs_vlserver_list *vllist, *old;
  316. unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
  317. unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
  318. time64_t now, expiry = 0;
  319. _enter("%s", cell->name);
  320. vllist = afs_dns_query(cell, &expiry);
  321. now = ktime_get_real_seconds();
  322. if (min_ttl > max_ttl)
  323. max_ttl = min_ttl;
  324. if (expiry < now + min_ttl)
  325. expiry = now + min_ttl;
  326. else if (expiry > now + max_ttl)
  327. expiry = now + max_ttl;
  328. if (IS_ERR(vllist)) {
  329. switch (PTR_ERR(vllist)) {
  330. case -ENODATA:
  331. case -EDESTADDRREQ:
  332. /* The DNS said that the cell does not exist or there
  333. * weren't any addresses to be had.
  334. */
  335. set_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
  336. clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
  337. cell->dns_expiry = expiry;
  338. break;
  339. case -EAGAIN:
  340. case -ECONNREFUSED:
  341. default:
  342. set_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
  343. cell->dns_expiry = now + 10;
  344. break;
  345. }
  346. cell->error = -EDESTADDRREQ;
  347. } else {
  348. clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
  349. clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
  350. /* Exclusion on changing vl_addrs is achieved by a
  351. * non-reentrant work item.
  352. */
  353. old = rcu_dereference_protected(cell->vl_servers, true);
  354. rcu_assign_pointer(cell->vl_servers, vllist);
  355. cell->dns_expiry = expiry;
  356. if (old)
  357. afs_put_vlserverlist(cell->net, old);
  358. }
  359. if (test_and_clear_bit(AFS_CELL_FL_NO_LOOKUP_YET, &cell->flags))
  360. wake_up_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET);
  361. now = ktime_get_real_seconds();
  362. afs_set_cell_timer(cell->net, cell->dns_expiry - now);
  363. _leave("");
  364. }
  365. /*
  366. * Destroy a cell record
  367. */
  368. static void afs_cell_destroy(struct rcu_head *rcu)
  369. {
  370. struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
  371. _enter("%p{%s}", cell, cell->name);
  372. ASSERTCMP(atomic_read(&cell->usage), ==, 0);
  373. afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
  374. key_put(cell->anonymous_key);
  375. kfree(cell);
  376. _leave(" [destroyed]");
  377. }
  378. /*
  379. * Queue the cell manager.
  380. */
  381. static void afs_queue_cell_manager(struct afs_net *net)
  382. {
  383. int outstanding = atomic_inc_return(&net->cells_outstanding);
  384. _enter("%d", outstanding);
  385. if (!queue_work(afs_wq, &net->cells_manager))
  386. afs_dec_cells_outstanding(net);
  387. }
  388. /*
  389. * Cell management timer. We have an increment on cells_outstanding that we
  390. * need to pass along to the work item.
  391. */
  392. void afs_cells_timer(struct timer_list *timer)
  393. {
  394. struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
  395. _enter("");
  396. if (!queue_work(afs_wq, &net->cells_manager))
  397. afs_dec_cells_outstanding(net);
  398. }
  399. /*
  400. * Get a reference on a cell record.
  401. */
  402. struct afs_cell *afs_get_cell(struct afs_cell *cell)
  403. {
  404. atomic_inc(&cell->usage);
  405. return cell;
  406. }
  407. /*
  408. * Drop a reference on a cell record.
  409. */
  410. void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
  411. {
  412. time64_t now, expire_delay;
  413. if (!cell)
  414. return;
  415. _enter("%s", cell->name);
  416. now = ktime_get_real_seconds();
  417. cell->last_inactive = now;
  418. expire_delay = 0;
  419. if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
  420. !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
  421. expire_delay = afs_cell_gc_delay;
  422. if (atomic_dec_return(&cell->usage) > 1)
  423. return;
  424. /* 'cell' may now be garbage collected. */
  425. afs_set_cell_timer(net, expire_delay);
  426. }
  427. /*
  428. * Allocate a key to use as a placeholder for anonymous user security.
  429. */
  430. static int afs_alloc_anon_key(struct afs_cell *cell)
  431. {
  432. struct key *key;
  433. char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
  434. /* Create a key to represent an anonymous user. */
  435. memcpy(keyname, "afs@", 4);
  436. dp = keyname + 4;
  437. cp = cell->name;
  438. do {
  439. *dp++ = tolower(*cp);
  440. } while (*cp++);
  441. key = rxrpc_get_null_key(keyname);
  442. if (IS_ERR(key))
  443. return PTR_ERR(key);
  444. cell->anonymous_key = key;
  445. _debug("anon key %p{%x}",
  446. cell->anonymous_key, key_serial(cell->anonymous_key));
  447. return 0;
  448. }
  449. /*
  450. * Activate a cell.
  451. */
  452. static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
  453. {
  454. struct hlist_node **p;
  455. struct afs_cell *pcell;
  456. int ret;
  457. if (!cell->anonymous_key) {
  458. ret = afs_alloc_anon_key(cell);
  459. if (ret < 0)
  460. return ret;
  461. }
  462. #ifdef CONFIG_AFS_FSCACHE
  463. cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
  464. &afs_cell_cache_index_def,
  465. cell->name, strlen(cell->name),
  466. NULL, 0,
  467. cell, 0, true);
  468. #endif
  469. ret = afs_proc_cell_setup(cell);
  470. if (ret < 0)
  471. return ret;
  472. mutex_lock(&net->proc_cells_lock);
  473. for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
  474. pcell = hlist_entry(*p, struct afs_cell, proc_link);
  475. if (strcmp(cell->name, pcell->name) < 0)
  476. break;
  477. }
  478. cell->proc_link.pprev = p;
  479. cell->proc_link.next = *p;
  480. rcu_assign_pointer(*p, &cell->proc_link.next);
  481. if (cell->proc_link.next)
  482. cell->proc_link.next->pprev = &cell->proc_link.next;
  483. afs_dynroot_mkdir(net, cell);
  484. mutex_unlock(&net->proc_cells_lock);
  485. return 0;
  486. }
  487. /*
  488. * Deactivate a cell.
  489. */
  490. static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
  491. {
  492. _enter("%s", cell->name);
  493. afs_proc_cell_remove(cell);
  494. mutex_lock(&net->proc_cells_lock);
  495. hlist_del_rcu(&cell->proc_link);
  496. afs_dynroot_rmdir(net, cell);
  497. mutex_unlock(&net->proc_cells_lock);
  498. #ifdef CONFIG_AFS_FSCACHE
  499. fscache_relinquish_cookie(cell->cache, NULL, false);
  500. cell->cache = NULL;
  501. #endif
  502. _leave("");
  503. }
  504. /*
  505. * Manage a cell record, initialising and destroying it, maintaining its DNS
  506. * records.
  507. */
  508. static void afs_manage_cell(struct work_struct *work)
  509. {
  510. struct afs_cell *cell = container_of(work, struct afs_cell, manager);
  511. struct afs_net *net = cell->net;
  512. bool deleted;
  513. int ret, usage;
  514. _enter("%s", cell->name);
  515. again:
  516. _debug("state %u", cell->state);
  517. switch (cell->state) {
  518. case AFS_CELL_INACTIVE:
  519. case AFS_CELL_FAILED:
  520. write_seqlock(&net->cells_lock);
  521. usage = 1;
  522. deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
  523. if (deleted)
  524. rb_erase(&cell->net_node, &net->cells);
  525. write_sequnlock(&net->cells_lock);
  526. if (deleted)
  527. goto final_destruction;
  528. if (cell->state == AFS_CELL_FAILED)
  529. goto done;
  530. cell->state = AFS_CELL_UNSET;
  531. goto again;
  532. case AFS_CELL_UNSET:
  533. cell->state = AFS_CELL_ACTIVATING;
  534. goto again;
  535. case AFS_CELL_ACTIVATING:
  536. ret = afs_activate_cell(net, cell);
  537. if (ret < 0)
  538. goto activation_failed;
  539. cell->state = AFS_CELL_ACTIVE;
  540. smp_wmb();
  541. clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
  542. wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
  543. goto again;
  544. case AFS_CELL_ACTIVE:
  545. if (atomic_read(&cell->usage) > 1) {
  546. time64_t now = ktime_get_real_seconds();
  547. if (cell->dns_expiry <= now && net->live)
  548. afs_update_cell(cell);
  549. goto done;
  550. }
  551. cell->state = AFS_CELL_DEACTIVATING;
  552. goto again;
  553. case AFS_CELL_DEACTIVATING:
  554. set_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
  555. if (atomic_read(&cell->usage) > 1)
  556. goto reverse_deactivation;
  557. afs_deactivate_cell(net, cell);
  558. cell->state = AFS_CELL_INACTIVE;
  559. goto again;
  560. default:
  561. break;
  562. }
  563. _debug("bad state %u", cell->state);
  564. BUG(); /* Unhandled state */
  565. activation_failed:
  566. cell->error = ret;
  567. afs_deactivate_cell(net, cell);
  568. cell->state = AFS_CELL_FAILED;
  569. smp_wmb();
  570. if (test_and_clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags))
  571. wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
  572. goto again;
  573. reverse_deactivation:
  574. cell->state = AFS_CELL_ACTIVE;
  575. smp_wmb();
  576. clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
  577. wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
  578. _leave(" [deact->act]");
  579. return;
  580. done:
  581. _leave(" [done %u]", cell->state);
  582. return;
  583. final_destruction:
  584. call_rcu(&cell->rcu, afs_cell_destroy);
  585. afs_dec_cells_outstanding(net);
  586. _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
  587. }
  588. /*
  589. * Manage the records of cells known to a network namespace. This includes
  590. * updating the DNS records and garbage collecting unused cells that were
  591. * automatically added.
  592. *
  593. * Note that constructed cell records may only be removed from net->cells by
  594. * this work item, so it is safe for this work item to stash a cursor pointing
  595. * into the tree and then return to caller (provided it skips cells that are
  596. * still under construction).
  597. *
  598. * Note also that we were given an increment on net->cells_outstanding by
  599. * whoever queued us that we need to deal with before returning.
  600. */
  601. void afs_manage_cells(struct work_struct *work)
  602. {
  603. struct afs_net *net = container_of(work, struct afs_net, cells_manager);
  604. struct rb_node *cursor;
  605. time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
  606. bool purging = !net->live;
  607. _enter("");
  608. /* Trawl the cell database looking for cells that have expired from
  609. * lack of use and cells whose DNS results have expired and dispatch
  610. * their managers.
  611. */
  612. read_seqlock_excl(&net->cells_lock);
  613. for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
  614. struct afs_cell *cell =
  615. rb_entry(cursor, struct afs_cell, net_node);
  616. unsigned usage;
  617. bool sched_cell = false;
  618. usage = atomic_read(&cell->usage);
  619. _debug("manage %s %u", cell->name, usage);
  620. ASSERTCMP(usage, >=, 1);
  621. if (purging) {
  622. if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
  623. usage = atomic_dec_return(&cell->usage);
  624. ASSERTCMP(usage, ==, 1);
  625. }
  626. if (usage == 1) {
  627. time64_t expire_at = cell->last_inactive;
  628. if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
  629. !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
  630. expire_at += afs_cell_gc_delay;
  631. if (purging || expire_at <= now)
  632. sched_cell = true;
  633. else if (expire_at < next_manage)
  634. next_manage = expire_at;
  635. }
  636. if (!purging) {
  637. if (cell->dns_expiry <= now)
  638. sched_cell = true;
  639. else if (cell->dns_expiry <= next_manage)
  640. next_manage = cell->dns_expiry;
  641. }
  642. if (sched_cell)
  643. queue_work(afs_wq, &cell->manager);
  644. }
  645. read_sequnlock_excl(&net->cells_lock);
  646. /* Update the timer on the way out. We have to pass an increment on
  647. * cells_outstanding in the namespace that we are in to the timer or
  648. * the work scheduler.
  649. */
  650. if (!purging && next_manage < TIME64_MAX) {
  651. now = ktime_get_real_seconds();
  652. if (next_manage - now <= 0) {
  653. if (queue_work(afs_wq, &net->cells_manager))
  654. atomic_inc(&net->cells_outstanding);
  655. } else {
  656. afs_set_cell_timer(net, next_manage - now);
  657. }
  658. }
  659. afs_dec_cells_outstanding(net);
  660. _leave(" [%d]", atomic_read(&net->cells_outstanding));
  661. }
  662. /*
  663. * Purge in-memory cell database.
  664. */
  665. void afs_cell_purge(struct afs_net *net)
  666. {
  667. struct afs_cell *ws;
  668. _enter("");
  669. write_seqlock(&net->cells_lock);
  670. ws = rcu_access_pointer(net->ws_cell);
  671. RCU_INIT_POINTER(net->ws_cell, NULL);
  672. write_sequnlock(&net->cells_lock);
  673. afs_put_cell(net, ws);
  674. _debug("del timer");
  675. if (del_timer_sync(&net->cells_timer))
  676. atomic_dec(&net->cells_outstanding);
  677. _debug("kick mgr");
  678. afs_queue_cell_manager(net);
  679. _debug("wait");
  680. wait_var_event(&net->cells_outstanding,
  681. !atomic_read(&net->cells_outstanding));
  682. _leave("");
  683. }