cifs: remove actimeo from cifs_sb
[linux-block.git] / fs / cifs / cifs_swn.c
CommitLineData
bf80e5d4
SC
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Witness Service client for CIFS
4 *
5 * Copyright (c) 2020 Samuel Cabrero <scabrero@suse.de>
6 */
7
8#include <linux/kref.h>
9#include <net/genetlink.h>
10#include <uapi/linux/cifs/cifs_netlink.h>
11
12#include "cifs_swn.h"
13#include "cifsglob.h"
14#include "cifsproto.h"
15#include "fscache.h"
16#include "cifs_debug.h"
17#include "netlink.h"
18
19static DEFINE_IDR(cifs_swnreg_idr);
20static DEFINE_MUTEX(cifs_swnreg_idr_mutex);
21
22struct cifs_swn_reg {
23 int id;
24 struct kref ref_count;
25
26 const char *net_name;
27 const char *share_name;
28 bool net_name_notify;
29 bool share_name_notify;
30 bool ip_notify;
31
32 struct cifs_tcon *tcon;
33};
34
35static int cifs_swn_auth_info_krb(struct cifs_tcon *tcon, struct sk_buff *skb)
36{
37 int ret;
38
39 ret = nla_put_flag(skb, CIFS_GENL_ATTR_SWN_KRB_AUTH);
40 if (ret < 0)
41 return ret;
42
43 return 0;
44}
45
46static int cifs_swn_auth_info_ntlm(struct cifs_tcon *tcon, struct sk_buff *skb)
47{
48 int ret;
49
50 if (tcon->ses->user_name != NULL) {
51 ret = nla_put_string(skb, CIFS_GENL_ATTR_SWN_USER_NAME, tcon->ses->user_name);
52 if (ret < 0)
53 return ret;
54 }
55
56 if (tcon->ses->password != NULL) {
57 ret = nla_put_string(skb, CIFS_GENL_ATTR_SWN_PASSWORD, tcon->ses->password);
58 if (ret < 0)
59 return ret;
60 }
61
62 if (tcon->ses->domainName != NULL) {
63 ret = nla_put_string(skb, CIFS_GENL_ATTR_SWN_DOMAIN_NAME, tcon->ses->domainName);
64 if (ret < 0)
65 return ret;
66 }
67
68 return 0;
69}
70
71/*
72 * Sends a register message to the userspace daemon based on the registration.
73 * The authentication information to connect to the witness service is bundled
74 * into the message.
75 */
76static int cifs_swn_send_register_message(struct cifs_swn_reg *swnreg)
77{
78 struct sk_buff *skb;
79 struct genlmsghdr *hdr;
80 enum securityEnum authtype;
81 int ret;
82
83 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
84 if (skb == NULL) {
85 ret = -ENOMEM;
86 goto fail;
87 }
88
89 hdr = genlmsg_put(skb, 0, 0, &cifs_genl_family, 0, CIFS_GENL_CMD_SWN_REGISTER);
90 if (hdr == NULL) {
91 ret = -ENOMEM;
92 goto nlmsg_fail;
93 }
94
95 ret = nla_put_u32(skb, CIFS_GENL_ATTR_SWN_REGISTRATION_ID, swnreg->id);
96 if (ret < 0)
97 goto nlmsg_fail;
98
99 ret = nla_put_string(skb, CIFS_GENL_ATTR_SWN_NET_NAME, swnreg->net_name);
100 if (ret < 0)
101 goto nlmsg_fail;
102
103 ret = nla_put_string(skb, CIFS_GENL_ATTR_SWN_SHARE_NAME, swnreg->share_name);
104 if (ret < 0)
105 goto nlmsg_fail;
106
107 ret = nla_put(skb, CIFS_GENL_ATTR_SWN_IP, sizeof(struct sockaddr_storage),
108 &swnreg->tcon->ses->server->dstaddr);
109 if (ret < 0)
110 goto nlmsg_fail;
111
112 if (swnreg->net_name_notify) {
113 ret = nla_put_flag(skb, CIFS_GENL_ATTR_SWN_NET_NAME_NOTIFY);
114 if (ret < 0)
115 goto nlmsg_fail;
116 }
117
118 if (swnreg->share_name_notify) {
119 ret = nla_put_flag(skb, CIFS_GENL_ATTR_SWN_SHARE_NAME_NOTIFY);
120 if (ret < 0)
121 goto nlmsg_fail;
122 }
123
124 if (swnreg->ip_notify) {
125 ret = nla_put_flag(skb, CIFS_GENL_ATTR_SWN_IP_NOTIFY);
126 if (ret < 0)
127 goto nlmsg_fail;
128 }
129
130 authtype = cifs_select_sectype(swnreg->tcon->ses->server, swnreg->tcon->ses->sectype);
131 switch (authtype) {
132 case Kerberos:
133 ret = cifs_swn_auth_info_krb(swnreg->tcon, skb);
134 if (ret < 0) {
135 cifs_dbg(VFS, "%s: Failed to get kerberos auth info: %d\n", __func__, ret);
136 goto nlmsg_fail;
137 }
138 break;
139 case LANMAN:
140 case NTLM:
141 case NTLMv2:
142 case RawNTLMSSP:
143 ret = cifs_swn_auth_info_ntlm(swnreg->tcon, skb);
144 if (ret < 0) {
145 cifs_dbg(VFS, "%s: Failed to get NTLM auth info: %d\n", __func__, ret);
146 goto nlmsg_fail;
147 }
148 break;
149 default:
150 cifs_dbg(VFS, "%s: secType %d not supported!\n", __func__, authtype);
151 ret = -EINVAL;
152 goto nlmsg_fail;
153 }
154
155 genlmsg_end(skb, hdr);
156 genlmsg_multicast(&cifs_genl_family, skb, 0, CIFS_GENL_MCGRP_SWN, GFP_ATOMIC);
157
158 cifs_dbg(FYI, "%s: Message to register for network name %s with id %d sent\n", __func__,
159 swnreg->net_name, swnreg->id);
160
161 return 0;
162
163nlmsg_fail:
164 genlmsg_cancel(skb, hdr);
165 nlmsg_free(skb);
166fail:
167 return ret;
168}
169
170/*
171 * Sends an uregister message to the userspace daemon based on the registration
172 */
173static int cifs_swn_send_unregister_message(struct cifs_swn_reg *swnreg)
174{
175 struct sk_buff *skb;
176 struct genlmsghdr *hdr;
177 int ret;
178
179 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
180 if (skb == NULL)
181 return -ENOMEM;
182
183 hdr = genlmsg_put(skb, 0, 0, &cifs_genl_family, 0, CIFS_GENL_CMD_SWN_UNREGISTER);
184 if (hdr == NULL) {
185 ret = -ENOMEM;
186 goto nlmsg_fail;
187 }
188
189 ret = nla_put_u32(skb, CIFS_GENL_ATTR_SWN_REGISTRATION_ID, swnreg->id);
190 if (ret < 0)
191 goto nlmsg_fail;
192
193 ret = nla_put_string(skb, CIFS_GENL_ATTR_SWN_NET_NAME, swnreg->net_name);
194 if (ret < 0)
195 goto nlmsg_fail;
196
197 ret = nla_put_string(skb, CIFS_GENL_ATTR_SWN_SHARE_NAME, swnreg->share_name);
198 if (ret < 0)
199 goto nlmsg_fail;
200
201 ret = nla_put(skb, CIFS_GENL_ATTR_SWN_IP, sizeof(struct sockaddr_storage),
202 &swnreg->tcon->ses->server->dstaddr);
203 if (ret < 0)
204 goto nlmsg_fail;
205
206 if (swnreg->net_name_notify) {
207 ret = nla_put_flag(skb, CIFS_GENL_ATTR_SWN_NET_NAME_NOTIFY);
208 if (ret < 0)
209 goto nlmsg_fail;
210 }
211
212 if (swnreg->share_name_notify) {
213 ret = nla_put_flag(skb, CIFS_GENL_ATTR_SWN_SHARE_NAME_NOTIFY);
214 if (ret < 0)
215 goto nlmsg_fail;
216 }
217
218 if (swnreg->ip_notify) {
219 ret = nla_put_flag(skb, CIFS_GENL_ATTR_SWN_IP_NOTIFY);
220 if (ret < 0)
221 goto nlmsg_fail;
222 }
223
224 genlmsg_end(skb, hdr);
225 genlmsg_multicast(&cifs_genl_family, skb, 0, CIFS_GENL_MCGRP_SWN, GFP_ATOMIC);
226
227 cifs_dbg(FYI, "%s: Message to unregister for network name %s with id %d sent\n", __func__,
228 swnreg->net_name, swnreg->id);
229
230 return 0;
231
232nlmsg_fail:
233 genlmsg_cancel(skb, hdr);
234 nlmsg_free(skb);
235 return ret;
236}
237
238/*
239 * Try to find a matching registration for the tcon's server name and share name.
240 * Calls to this funciton must be protected by cifs_swnreg_idr_mutex.
241 * TODO Try to avoid memory allocations
242 */
243static struct cifs_swn_reg *cifs_find_swn_reg(struct cifs_tcon *tcon)
244{
245 struct cifs_swn_reg *swnreg;
246 int id;
247 const char *share_name;
248 const char *net_name;
249
250 net_name = extract_hostname(tcon->treeName);
251 if (IS_ERR_OR_NULL(net_name)) {
252 int ret;
253
254 ret = PTR_ERR(net_name);
255 cifs_dbg(VFS, "%s: failed to extract host name from target '%s': %d\n",
256 __func__, tcon->treeName, ret);
257 return NULL;
258 }
259
260 share_name = extract_sharename(tcon->treeName);
261 if (IS_ERR_OR_NULL(share_name)) {
262 int ret;
263
264 ret = PTR_ERR(net_name);
265 cifs_dbg(VFS, "%s: failed to extract share name from target '%s': %d\n",
266 __func__, tcon->treeName, ret);
267 kfree(net_name);
268 return NULL;
269 }
270
271 idr_for_each_entry(&cifs_swnreg_idr, swnreg, id) {
272 if (strcasecmp(swnreg->net_name, net_name) != 0
273 || strcasecmp(swnreg->share_name, share_name) != 0) {
274 continue;
275 }
276
277 mutex_unlock(&cifs_swnreg_idr_mutex);
278
279 cifs_dbg(FYI, "Existing swn registration for %s:%s found\n", swnreg->net_name,
280 swnreg->share_name);
281
282 kfree(net_name);
283 kfree(share_name);
284
285 return swnreg;
286 }
287
288 kfree(net_name);
289 kfree(share_name);
290
291 return NULL;
292}
293
294/*
295 * Get a registration for the tcon's server and share name, allocating a new one if it does not
296 * exists
297 */
298static struct cifs_swn_reg *cifs_get_swn_reg(struct cifs_tcon *tcon)
299{
300 struct cifs_swn_reg *reg = NULL;
301 int ret;
302
303 mutex_lock(&cifs_swnreg_idr_mutex);
304
305 /* Check if we are already registered for this network and share names */
306 reg = cifs_find_swn_reg(tcon);
307 if (IS_ERR(reg)) {
308 return reg;
309 } else if (reg != NULL) {
310 kref_get(&reg->ref_count);
311 mutex_unlock(&cifs_swnreg_idr_mutex);
312 return reg;
313 }
314
315 reg = kmalloc(sizeof(struct cifs_swn_reg), GFP_ATOMIC);
316 if (reg == NULL) {
317 mutex_unlock(&cifs_swnreg_idr_mutex);
318 return ERR_PTR(-ENOMEM);
319 }
320
321 kref_init(&reg->ref_count);
322
323 reg->id = idr_alloc(&cifs_swnreg_idr, reg, 1, 0, GFP_ATOMIC);
324 if (reg->id < 0) {
325 cifs_dbg(FYI, "%s: failed to allocate registration id\n", __func__);
326 ret = reg->id;
327 goto fail;
328 }
329
330 reg->net_name = extract_hostname(tcon->treeName);
331 if (IS_ERR(reg->net_name)) {
332 ret = PTR_ERR(reg->net_name);
333 cifs_dbg(VFS, "%s: failed to extract host name from target: %d\n", __func__, ret);
334 goto fail_idr;
335 }
336
337 reg->share_name = extract_sharename(tcon->treeName);
338 if (IS_ERR(reg->share_name)) {
339 ret = PTR_ERR(reg->share_name);
340 cifs_dbg(VFS, "%s: failed to extract share name from target: %d\n", __func__, ret);
341 goto fail_net_name;
342 }
343
344 reg->net_name_notify = true;
345 reg->share_name_notify = true;
346 reg->ip_notify = (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT);
347
348 reg->tcon = tcon;
349
350 mutex_unlock(&cifs_swnreg_idr_mutex);
351
352 return reg;
353
354fail_net_name:
355 kfree(reg->net_name);
356fail_idr:
357 idr_remove(&cifs_swnreg_idr, reg->id);
358fail:
359 kfree(reg);
360 mutex_unlock(&cifs_swnreg_idr_mutex);
361 return ERR_PTR(ret);
362}
363
364static void cifs_swn_reg_release(struct kref *ref)
365{
366 struct cifs_swn_reg *swnreg = container_of(ref, struct cifs_swn_reg, ref_count);
367 int ret;
368
369 ret = cifs_swn_send_unregister_message(swnreg);
370 if (ret < 0)
371 cifs_dbg(VFS, "%s: Failed to send unregister message: %d\n", __func__, ret);
372
373 idr_remove(&cifs_swnreg_idr, swnreg->id);
374 kfree(swnreg->net_name);
375 kfree(swnreg->share_name);
376 kfree(swnreg);
377}
378
379static void cifs_put_swn_reg(struct cifs_swn_reg *swnreg)
380{
381 mutex_lock(&cifs_swnreg_idr_mutex);
382 kref_put(&swnreg->ref_count, cifs_swn_reg_release);
383 mutex_unlock(&cifs_swnreg_idr_mutex);
384}
385
fed979a7
SC
386static int cifs_swn_resource_state_changed(struct cifs_swn_reg *swnreg, const char *name, int state)
387{
388 int i;
389
390 switch (state) {
391 case CIFS_SWN_RESOURCE_STATE_UNAVAILABLE:
392 cifs_dbg(FYI, "%s: resource name '%s' become unavailable\n", __func__, name);
393 for (i = 0; i < swnreg->tcon->ses->chan_count; i++) {
394 spin_lock(&GlobalMid_Lock);
395 if (swnreg->tcon->ses->chans[i].server->tcpStatus != CifsExiting)
396 swnreg->tcon->ses->chans[i].server->tcpStatus = CifsNeedReconnect;
397 spin_unlock(&GlobalMid_Lock);
398 }
399 break;
400 case CIFS_SWN_RESOURCE_STATE_AVAILABLE:
401 cifs_dbg(FYI, "%s: resource name '%s' become available\n", __func__, name);
402 for (i = 0; i < swnreg->tcon->ses->chan_count; i++) {
403 spin_lock(&GlobalMid_Lock);
404 if (swnreg->tcon->ses->chans[i].server->tcpStatus != CifsExiting)
405 swnreg->tcon->ses->chans[i].server->tcpStatus = CifsNeedReconnect;
406 spin_unlock(&GlobalMid_Lock);
407 }
408 break;
409 case CIFS_SWN_RESOURCE_STATE_UNKNOWN:
410 cifs_dbg(FYI, "%s: resource name '%s' changed to unknown state\n", __func__, name);
411 break;
412 }
413 return 0;
414}
415
416int cifs_swn_notify(struct sk_buff *skb, struct genl_info *info)
417{
418 struct cifs_swn_reg *swnreg;
419 char name[256];
420 int type;
421
422 if (info->attrs[CIFS_GENL_ATTR_SWN_REGISTRATION_ID]) {
423 int swnreg_id;
424
425 swnreg_id = nla_get_u32(info->attrs[CIFS_GENL_ATTR_SWN_REGISTRATION_ID]);
426 mutex_lock(&cifs_swnreg_idr_mutex);
427 swnreg = idr_find(&cifs_swnreg_idr, swnreg_id);
428 mutex_unlock(&cifs_swnreg_idr_mutex);
429 if (swnreg == NULL) {
430 cifs_dbg(FYI, "%s: registration id %d not found\n", __func__, swnreg_id);
431 return -EINVAL;
432 }
433 } else {
434 cifs_dbg(FYI, "%s: missing registration id attribute\n", __func__);
435 return -EINVAL;
436 }
437
438 if (info->attrs[CIFS_GENL_ATTR_SWN_NOTIFICATION_TYPE]) {
439 type = nla_get_u32(info->attrs[CIFS_GENL_ATTR_SWN_NOTIFICATION_TYPE]);
440 } else {
441 cifs_dbg(FYI, "%s: missing notification type attribute\n", __func__);
442 return -EINVAL;
443 }
444
445 switch (type) {
446 case CIFS_SWN_NOTIFICATION_RESOURCE_CHANGE: {
447 int state;
448
449 if (info->attrs[CIFS_GENL_ATTR_SWN_RESOURCE_NAME]) {
450 nla_strlcpy(name, info->attrs[CIFS_GENL_ATTR_SWN_RESOURCE_NAME],
451 sizeof(name));
452 } else {
453 cifs_dbg(FYI, "%s: missing resource name attribute\n", __func__);
454 return -EINVAL;
455 }
456 if (info->attrs[CIFS_GENL_ATTR_SWN_RESOURCE_STATE]) {
457 state = nla_get_u32(info->attrs[CIFS_GENL_ATTR_SWN_RESOURCE_STATE]);
458 } else {
459 cifs_dbg(FYI, "%s: missing resource state attribute\n", __func__);
460 return -EINVAL;
461 }
462 return cifs_swn_resource_state_changed(swnreg, name, state);
463 }
464 default:
465 cifs_dbg(FYI, "%s: unknown notification type %d\n", __func__, type);
466 break;
467 }
468
469 return 0;
470}
471
bf80e5d4
SC
472int cifs_swn_register(struct cifs_tcon *tcon)
473{
474 struct cifs_swn_reg *swnreg;
475 int ret;
476
477 swnreg = cifs_get_swn_reg(tcon);
478 if (IS_ERR(swnreg))
479 return PTR_ERR(swnreg);
480
481 ret = cifs_swn_send_register_message(swnreg);
482 if (ret < 0) {
483 cifs_dbg(VFS, "%s: Failed to send swn register message: %d\n", __func__, ret);
484 /* Do not put the swnreg or return error, the echo task will retry */
485 }
486
487 return 0;
488}
489
490int cifs_swn_unregister(struct cifs_tcon *tcon)
491{
492 struct cifs_swn_reg *swnreg;
493
494 mutex_lock(&cifs_swnreg_idr_mutex);
495
496 swnreg = cifs_find_swn_reg(tcon);
497 if (swnreg == NULL) {
498 mutex_unlock(&cifs_swnreg_idr_mutex);
499 return -EEXIST;
500 }
501
502 mutex_unlock(&cifs_swnreg_idr_mutex);
503
504 cifs_put_swn_reg(swnreg);
505
506 return 0;
507}
20fab0da
SC
508
509void cifs_swn_dump(struct seq_file *m)
510{
511 struct cifs_swn_reg *swnreg;
512 struct sockaddr_in *sa;
513 struct sockaddr_in6 *sa6;
514 int id;
515
516 seq_puts(m, "Witness registrations:");
517
518 mutex_lock(&cifs_swnreg_idr_mutex);
519 idr_for_each_entry(&cifs_swnreg_idr, swnreg, id) {
520 seq_printf(m, "\nId: %u Refs: %u Network name: '%s'%s Share name: '%s'%s Ip address: ",
521 id, kref_read(&swnreg->ref_count),
522 swnreg->net_name, swnreg->net_name_notify ? "(y)" : "(n)",
523 swnreg->share_name, swnreg->share_name_notify ? "(y)" : "(n)");
524 switch (swnreg->tcon->ses->server->dstaddr.ss_family) {
525 case AF_INET:
526 sa = (struct sockaddr_in *) &swnreg->tcon->ses->server->dstaddr;
527 seq_printf(m, "%pI4", &sa->sin_addr.s_addr);
528 break;
529 case AF_INET6:
530 sa6 = (struct sockaddr_in6 *) &swnreg->tcon->ses->server->dstaddr;
531 seq_printf(m, "%pI6", &sa6->sin6_addr.s6_addr);
532 if (sa6->sin6_scope_id)
533 seq_printf(m, "%%%u", sa6->sin6_scope_id);
534 break;
535 default:
536 seq_puts(m, "(unknown)");
537 }
538 seq_printf(m, "%s", swnreg->ip_notify ? "(y)" : "(n)");
539 }
540 mutex_unlock(&cifs_swnreg_idr_mutex);
541 seq_puts(m, "\n");
542}
21077c62
SC
543
544void cifs_swn_check(void)
545{
546 struct cifs_swn_reg *swnreg;
547 int id;
548 int ret;
549
550 mutex_lock(&cifs_swnreg_idr_mutex);
551 idr_for_each_entry(&cifs_swnreg_idr, swnreg, id) {
552 ret = cifs_swn_send_register_message(swnreg);
553 if (ret < 0)
554 cifs_dbg(FYI, "%s: Failed to send register message: %d\n", __func__, ret);
555 }
556 mutex_unlock(&cifs_swnreg_idr_mutex);
557}