cell.c 19 KB

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