hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/sunrpc/svc.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/net/sunrpc/svc.c
34 *
....@@ -87,15 +88,15 @@
8788 switch (*ip)
8889 {
8990 case SVC_POOL_AUTO:
90
- return strlcpy(buf, "auto", 20);
91
+ return strlcpy(buf, "auto\n", 20);
9192 case SVC_POOL_GLOBAL:
92
- return strlcpy(buf, "global", 20);
93
+ return strlcpy(buf, "global\n", 20);
9394 case SVC_POOL_PERCPU:
94
- return strlcpy(buf, "percpu", 20);
95
+ return strlcpy(buf, "percpu\n", 20);
9596 case SVC_POOL_PERNODE:
96
- return strlcpy(buf, "pernode", 20);
97
+ return strlcpy(buf, "pernode\n", 20);
9798 default:
98
- return sprintf(buf, "%d", *ip);
99
+ return sprintf(buf, "%d\n", *ip);
99100 }
100101 }
101102
....@@ -799,6 +800,7 @@
799800 static int
800801 svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
801802 {
803
+ struct svc_rqst *rqstp;
802804 struct task_struct *task;
803805 unsigned int state = serv->sv_nrthreads-1;
804806
....@@ -807,7 +809,10 @@
807809 task = choose_victim(serv, pool, &state);
808810 if (task == NULL)
809811 break;
810
- kthread_stop(task);
812
+ rqstp = kthread_data(task);
813
+ /* Did we lose a race to svo_function threadfn? */
814
+ if (kthread_stop(task) == -EINTR)
815
+ svc_exit_thread(rqstp);
811816 nrservs++;
812817 } while (nrservs < 0);
813818 return 0;
....@@ -990,8 +995,53 @@
990995 #endif
991996 }
992997
998
+ trace_svc_register(progname, version, family, protocol, port, error);
993999 return error;
9941000 }
1001
+
1002
+int svc_rpcbind_set_version(struct net *net,
1003
+ const struct svc_program *progp,
1004
+ u32 version, int family,
1005
+ unsigned short proto,
1006
+ unsigned short port)
1007
+{
1008
+ return __svc_register(net, progp->pg_name, progp->pg_prog,
1009
+ version, family, proto, port);
1010
+
1011
+}
1012
+EXPORT_SYMBOL_GPL(svc_rpcbind_set_version);
1013
+
1014
+int svc_generic_rpcbind_set(struct net *net,
1015
+ const struct svc_program *progp,
1016
+ u32 version, int family,
1017
+ unsigned short proto,
1018
+ unsigned short port)
1019
+{
1020
+ const struct svc_version *vers = progp->pg_vers[version];
1021
+ int error;
1022
+
1023
+ if (vers == NULL)
1024
+ return 0;
1025
+
1026
+ if (vers->vs_hidden) {
1027
+ trace_svc_noregister(progp->pg_name, version, proto,
1028
+ port, family, 0);
1029
+ return 0;
1030
+ }
1031
+
1032
+ /*
1033
+ * Don't register a UDP port if we need congestion
1034
+ * control.
1035
+ */
1036
+ if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
1037
+ return 0;
1038
+
1039
+ error = svc_rpcbind_set_version(net, progp, version,
1040
+ family, proto, port);
1041
+
1042
+ return (vers->vs_rpcb_optnl) ? 0 : error;
1043
+}
1044
+EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set);
9951045
9961046 /**
9971047 * svc_register - register an RPC service with the local portmapper
....@@ -1008,7 +1058,6 @@
10081058 const unsigned short port)
10091059 {
10101060 struct svc_program *progp;
1011
- const struct svc_version *vers;
10121061 unsigned int i;
10131062 int error = 0;
10141063
....@@ -1018,37 +1067,9 @@
10181067
10191068 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
10201069 for (i = 0; i < progp->pg_nvers; i++) {
1021
- vers = progp->pg_vers[i];
1022
- if (vers == NULL)
1023
- continue;
10241070
1025
- dprintk("svc: svc_register(%sv%d, %s, %u, %u)%s\n",
1026
- progp->pg_name,
1027
- i,
1028
- proto == IPPROTO_UDP? "udp" : "tcp",
1029
- port,
1030
- family,
1031
- vers->vs_hidden ?
1032
- " (but not telling portmap)" : "");
1033
-
1034
- if (vers->vs_hidden)
1035
- continue;
1036
-
1037
- /*
1038
- * Don't register a UDP port if we need congestion
1039
- * control.
1040
- */
1041
- if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
1042
- continue;
1043
-
1044
- error = __svc_register(net, progp->pg_name, progp->pg_prog,
1045
- i, family, proto, port);
1046
-
1047
- if (vers->vs_rpcb_optnl) {
1048
- error = 0;
1049
- continue;
1050
- }
1051
-
1071
+ error = progp->pg_rpcbind_set(net, progp, i,
1072
+ family, proto, port);
10521073 if (error < 0) {
10531074 printk(KERN_WARNING "svc: failed to register "
10541075 "%sv%u RPC service (errno %d).\n",
....@@ -1082,8 +1103,7 @@
10821103 if (error == -EPROTONOSUPPORT)
10831104 error = rpcb_register(net, program, version, 0, 0);
10841105
1085
- dprintk("svc: %s(%sv%u), error %d\n",
1086
- __func__, progname, version, error);
1106
+ trace_svc_unregister(progname, version, error);
10871107 }
10881108
10891109 /*
....@@ -1108,9 +1128,6 @@
11081128 continue;
11091129 if (progp->pg_vers[i]->vs_hidden)
11101130 continue;
1111
-
1112
- dprintk("svc: attempting to unregister %sv%u\n",
1113
- progp->pg_name, i);
11141131 __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
11151132 }
11161133 }
....@@ -1144,8 +1161,6 @@
11441161 static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
11451162 #endif
11461163
1147
-extern void svc_tcp_prep_reply_hdr(struct svc_rqst *);
1148
-
11491164 __be32
11501165 svc_return_autherr(struct svc_rqst *rqstp, __be32 auth_err)
11511166 {
....@@ -1162,6 +1177,98 @@
11621177 return rpc_auth_ok;
11631178 }
11641179
1180
+static int
1181
+svc_generic_dispatch(struct svc_rqst *rqstp, __be32 *statp)
1182
+{
1183
+ struct kvec *argv = &rqstp->rq_arg.head[0];
1184
+ struct kvec *resv = &rqstp->rq_res.head[0];
1185
+ const struct svc_procedure *procp = rqstp->rq_procinfo;
1186
+
1187
+ /*
1188
+ * Decode arguments
1189
+ * XXX: why do we ignore the return value?
1190
+ */
1191
+ if (procp->pc_decode &&
1192
+ !procp->pc_decode(rqstp, argv->iov_base)) {
1193
+ *statp = rpc_garbage_args;
1194
+ return 1;
1195
+ }
1196
+
1197
+ *statp = procp->pc_func(rqstp);
1198
+
1199
+ if (*statp == rpc_drop_reply ||
1200
+ test_bit(RQ_DROPME, &rqstp->rq_flags))
1201
+ return 0;
1202
+
1203
+ if (test_bit(RQ_AUTHERR, &rqstp->rq_flags))
1204
+ return 1;
1205
+
1206
+ if (*statp != rpc_success)
1207
+ return 1;
1208
+
1209
+ /* Encode reply */
1210
+ if (procp->pc_encode &&
1211
+ !procp->pc_encode(rqstp, resv->iov_base + resv->iov_len)) {
1212
+ dprintk("svc: failed to encode reply\n");
1213
+ /* serv->sv_stats->rpcsystemerr++; */
1214
+ *statp = rpc_system_err;
1215
+ }
1216
+ return 1;
1217
+}
1218
+
1219
+__be32
1220
+svc_generic_init_request(struct svc_rqst *rqstp,
1221
+ const struct svc_program *progp,
1222
+ struct svc_process_info *ret)
1223
+{
1224
+ const struct svc_version *versp = NULL; /* compiler food */
1225
+ const struct svc_procedure *procp = NULL;
1226
+
1227
+ if (rqstp->rq_vers >= progp->pg_nvers )
1228
+ goto err_bad_vers;
1229
+ versp = progp->pg_vers[rqstp->rq_vers];
1230
+ if (!versp)
1231
+ goto err_bad_vers;
1232
+
1233
+ /*
1234
+ * Some protocol versions (namely NFSv4) require some form of
1235
+ * congestion control. (See RFC 7530 section 3.1 paragraph 2)
1236
+ * In other words, UDP is not allowed. We mark those when setting
1237
+ * up the svc_xprt, and verify that here.
1238
+ *
1239
+ * The spec is not very clear about what error should be returned
1240
+ * when someone tries to access a server that is listening on UDP
1241
+ * for lower versions. RPC_PROG_MISMATCH seems to be the closest
1242
+ * fit.
1243
+ */
1244
+ if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
1245
+ !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
1246
+ goto err_bad_vers;
1247
+
1248
+ if (rqstp->rq_proc >= versp->vs_nproc)
1249
+ goto err_bad_proc;
1250
+ rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc];
1251
+ if (!procp)
1252
+ goto err_bad_proc;
1253
+
1254
+ /* Initialize storage for argp and resp */
1255
+ memset(rqstp->rq_argp, 0, procp->pc_argsize);
1256
+ memset(rqstp->rq_resp, 0, procp->pc_ressize);
1257
+
1258
+ /* Bump per-procedure stats counter */
1259
+ versp->vs_count[rqstp->rq_proc]++;
1260
+
1261
+ ret->dispatch = versp->vs_dispatch;
1262
+ return rpc_success;
1263
+err_bad_vers:
1264
+ ret->mismatch.lovers = progp->pg_lovers;
1265
+ ret->mismatch.hivers = progp->pg_hivers;
1266
+ return rpc_prog_mismatch;
1267
+err_bad_proc:
1268
+ return rpc_proc_unavail;
1269
+}
1270
+EXPORT_SYMBOL_GPL(svc_generic_init_request);
1271
+
11651272 /*
11661273 * Common routine for processing the RPC request.
11671274 */
....@@ -1169,11 +1276,11 @@
11691276 svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
11701277 {
11711278 struct svc_program *progp;
1172
- const struct svc_version *versp = NULL; /* compiler food */
11731279 const struct svc_procedure *procp = NULL;
11741280 struct svc_serv *serv = rqstp->rq_server;
1281
+ struct svc_process_info process;
11751282 __be32 *statp;
1176
- u32 prog, vers, proc;
1283
+ u32 prog, vers;
11771284 __be32 auth_stat, rpc_stat;
11781285 int auth_res;
11791286 __be32 *reply_statp;
....@@ -1188,10 +1295,6 @@
11881295 /* Will be turned off only when NFSv4 Sessions are used */
11891296 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
11901297 clear_bit(RQ_DROPME, &rqstp->rq_flags);
1191
-
1192
- /* Setup reply header */
1193
- if (rqstp->rq_prot == IPPROTO_TCP)
1194
- svc_tcp_prep_reply_hdr(rqstp);
11951298
11961299 svc_putu32(resv, rqstp->rq_xid);
11971300
....@@ -1209,8 +1312,8 @@
12091312 svc_putnl(resv, 0); /* ACCEPT */
12101313
12111314 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
1212
- rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
1213
- rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
1315
+ rqstp->rq_vers = svc_getnl(argv); /* version number */
1316
+ rqstp->rq_proc = svc_getnl(argv); /* procedure number */
12141317
12151318 for (progp = serv->sv_program; progp; progp = progp->pg_next)
12161319 if (prog == progp->pg_prog)
....@@ -1227,6 +1330,8 @@
12271330 auth_stat = rpc_autherr_badcred;
12281331 auth_res = progp->pg_authenticate(rqstp);
12291332 }
1333
+ if (auth_res != SVC_OK)
1334
+ trace_svc_authenticate(rqstp, auth_res, auth_stat);
12301335 switch (auth_res) {
12311336 case SVC_OK:
12321337 break;
....@@ -1248,29 +1353,22 @@
12481353 if (progp == NULL)
12491354 goto err_bad_prog;
12501355
1251
- if (vers >= progp->pg_nvers ||
1252
- !(versp = progp->pg_vers[vers]))
1356
+ rpc_stat = progp->pg_init_request(rqstp, progp, &process);
1357
+ switch (rpc_stat) {
1358
+ case rpc_success:
1359
+ break;
1360
+ case rpc_prog_unavail:
1361
+ goto err_bad_prog;
1362
+ case rpc_prog_mismatch:
12531363 goto err_bad_vers;
1254
-
1255
- /*
1256
- * Some protocol versions (namely NFSv4) require some form of
1257
- * congestion control. (See RFC 7530 section 3.1 paragraph 2)
1258
- * In other words, UDP is not allowed. We mark those when setting
1259
- * up the svc_xprt, and verify that here.
1260
- *
1261
- * The spec is not very clear about what error should be returned
1262
- * when someone tries to access a server that is listening on UDP
1263
- * for lower versions. RPC_PROG_MISMATCH seems to be the closest
1264
- * fit.
1265
- */
1266
- if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
1267
- !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
1268
- goto err_bad_vers;
1269
-
1270
- procp = versp->vs_proc + proc;
1271
- if (proc >= versp->vs_nproc || !procp->pc_func)
1364
+ case rpc_proc_unavail:
12721365 goto err_bad_proc;
1273
- rqstp->rq_procinfo = procp;
1366
+ }
1367
+
1368
+ procp = rqstp->rq_procinfo;
1369
+ /* Should this check go into the dispatcher? */
1370
+ if (!procp || !procp->pc_func)
1371
+ goto err_bad_proc;
12741372
12751373 /* Syntactic check complete */
12761374 serv->sv_stats->rpccnt++;
....@@ -1280,13 +1378,6 @@
12801378 statp = resv->iov_base +resv->iov_len;
12811379 svc_putnl(resv, RPC_SUCCESS);
12821380
1283
- /* Bump per-procedure stats counter */
1284
- versp->vs_count[proc]++;
1285
-
1286
- /* Initialize storage for argp and resp */
1287
- memset(rqstp->rq_argp, 0, procp->pc_argsize);
1288
- memset(rqstp->rq_resp, 0, procp->pc_ressize);
1289
-
12901381 /* un-reserve some of the out-queue now that we have a
12911382 * better idea of reply size
12921383 */
....@@ -1294,41 +1385,18 @@
12941385 svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
12951386
12961387 /* Call the function that processes the request. */
1297
- if (!versp->vs_dispatch) {
1298
- /*
1299
- * Decode arguments
1300
- * XXX: why do we ignore the return value?
1301
- */
1302
- if (procp->pc_decode &&
1303
- !procp->pc_decode(rqstp, argv->iov_base))
1388
+ if (!process.dispatch) {
1389
+ if (!svc_generic_dispatch(rqstp, statp))
1390
+ goto release_dropit;
1391
+ if (*statp == rpc_garbage_args)
13041392 goto err_garbage;
1305
-
1306
- *statp = procp->pc_func(rqstp);
1307
-
1308
- /* Encode reply */
1309
- if (*statp == rpc_drop_reply ||
1310
- test_bit(RQ_DROPME, &rqstp->rq_flags)) {
1311
- if (procp->pc_release)
1312
- procp->pc_release(rqstp);
1313
- goto dropit;
1314
- }
13151393 auth_stat = svc_get_autherr(rqstp, statp);
13161394 if (auth_stat != rpc_auth_ok)
13171395 goto err_release_bad_auth;
1318
- if (*statp == rpc_success && procp->pc_encode &&
1319
- !procp->pc_encode(rqstp, resv->iov_base + resv->iov_len)) {
1320
- dprintk("svc: failed to encode reply\n");
1321
- /* serv->sv_stats->rpcsystemerr++; */
1322
- *statp = rpc_system_err;
1323
- }
13241396 } else {
13251397 dprintk("svc: calling dispatcher\n");
1326
- if (!versp->vs_dispatch(rqstp, statp)) {
1327
- /* Release reply info */
1328
- if (procp->pc_release)
1329
- procp->pc_release(rqstp);
1330
- goto dropit;
1331
- }
1398
+ if (!process.dispatch(rqstp, statp))
1399
+ goto release_dropit; /* Release reply info */
13321400 }
13331401
13341402 /* Check RPC status result */
....@@ -1347,6 +1415,9 @@
13471415 goto close_xprt;
13481416 return 1; /* Caller can now send it */
13491417
1418
+release_dropit:
1419
+ if (procp->pc_release)
1420
+ procp->pc_release(rqstp);
13501421 dropit:
13511422 svc_authorise(rqstp); /* doesn't hurt to call this twice */
13521423 dprintk("svc: svc_process dropit\n");
....@@ -1394,16 +1465,16 @@
13941465
13951466 err_bad_vers:
13961467 svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
1397
- vers, prog, progp->pg_name);
1468
+ rqstp->rq_vers, rqstp->rq_prog, progp->pg_name);
13981469
13991470 serv->sv_stats->rpcbadfmt++;
14001471 svc_putnl(resv, RPC_PROG_MISMATCH);
1401
- svc_putnl(resv, progp->pg_lovers);
1402
- svc_putnl(resv, progp->pg_hivers);
1472
+ svc_putnl(resv, process.mismatch.lovers);
1473
+ svc_putnl(resv, process.mismatch.hivers);
14031474 goto sendit;
14041475
14051476 err_bad_proc:
1406
- svc_printk(rqstp, "unknown procedure (%d)\n", proc);
1477
+ svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc);
14071478
14081479 serv->sv_stats->rpcbadfmt++;
14091480 svc_putnl(resv, RPC_PROC_UNAVAIL);
....@@ -1517,13 +1588,13 @@
15171588 /* Parse and execute the bc call */
15181589 proc_error = svc_process_common(rqstp, argv, resv);
15191590
1520
- atomic_inc(&req->rq_xprt->bc_free_slots);
1591
+ atomic_dec(&req->rq_xprt->bc_slot_count);
15211592 if (!proc_error) {
15221593 /* Processing error: drop the request */
15231594 xprt_free_bc_request(req);
1524
- return 0;
1595
+ error = -EINVAL;
1596
+ goto out;
15251597 }
1526
-
15271598 /* Finally, send the reply synchronously */
15281599 memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
15291600 task = rpc_run_bc_task(req);
....@@ -1557,6 +1628,22 @@
15571628 EXPORT_SYMBOL_GPL(svc_max_payload);
15581629
15591630 /**
1631
+ * svc_encode_read_payload - mark a range of bytes as a READ payload
1632
+ * @rqstp: svc_rqst to operate on
1633
+ * @offset: payload's byte offset in rqstp->rq_res
1634
+ * @length: size of payload, in bytes
1635
+ *
1636
+ * Returns zero on success, or a negative errno if a permanent
1637
+ * error occurred.
1638
+ */
1639
+int svc_encode_read_payload(struct svc_rqst *rqstp, unsigned int offset,
1640
+ unsigned int length)
1641
+{
1642
+ return rqstp->rq_xprt->xpt_ops->xpo_read_payload(rqstp, offset, length);
1643
+}
1644
+EXPORT_SYMBOL_GPL(svc_encode_read_payload);
1645
+
1646
+/**
15601647 * svc_fill_write_vector - Construct data argument for VFS write call
15611648 * @rqstp: svc_rqst to operate on
15621649 * @pages: list of pages containing data payload