cell.c 19 KB

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