forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
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
....@@ -990,8 +991,53 @@
990991 #endif
991992 }
992993
994
+ trace_svc_register(progname, version, protocol, port, family, error);
993995 return error;
994996 }
997
+
998
+int svc_rpcbind_set_version(struct net *net,
999
+ const struct svc_program *progp,
1000
+ u32 version, int family,
1001
+ unsigned short proto,
1002
+ unsigned short port)
1003
+{
1004
+ return __svc_register(net, progp->pg_name, progp->pg_prog,
1005
+ version, family, proto, port);
1006
+
1007
+}
1008
+EXPORT_SYMBOL_GPL(svc_rpcbind_set_version);
1009
+
1010
+int svc_generic_rpcbind_set(struct net *net,
1011
+ const struct svc_program *progp,
1012
+ u32 version, int family,
1013
+ unsigned short proto,
1014
+ unsigned short port)
1015
+{
1016
+ const struct svc_version *vers = progp->pg_vers[version];
1017
+ int error;
1018
+
1019
+ if (vers == NULL)
1020
+ return 0;
1021
+
1022
+ if (vers->vs_hidden) {
1023
+ trace_svc_noregister(progp->pg_name, version, proto,
1024
+ port, family, 0);
1025
+ return 0;
1026
+ }
1027
+
1028
+ /*
1029
+ * Don't register a UDP port if we need congestion
1030
+ * control.
1031
+ */
1032
+ if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
1033
+ return 0;
1034
+
1035
+ error = svc_rpcbind_set_version(net, progp, version,
1036
+ family, proto, port);
1037
+
1038
+ return (vers->vs_rpcb_optnl) ? 0 : error;
1039
+}
1040
+EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set);
9951041
9961042 /**
9971043 * svc_register - register an RPC service with the local portmapper
....@@ -1008,7 +1054,6 @@
10081054 const unsigned short port)
10091055 {
10101056 struct svc_program *progp;
1011
- const struct svc_version *vers;
10121057 unsigned int i;
10131058 int error = 0;
10141059
....@@ -1018,37 +1063,9 @@
10181063
10191064 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
10201065 for (i = 0; i < progp->pg_nvers; i++) {
1021
- vers = progp->pg_vers[i];
1022
- if (vers == NULL)
1023
- continue;
10241066
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
-
1067
+ error = progp->pg_rpcbind_set(net, progp, i,
1068
+ family, proto, port);
10521069 if (error < 0) {
10531070 printk(KERN_WARNING "svc: failed to register "
10541071 "%sv%u RPC service (errno %d).\n",
....@@ -1082,8 +1099,7 @@
10821099 if (error == -EPROTONOSUPPORT)
10831100 error = rpcb_register(net, program, version, 0, 0);
10841101
1085
- dprintk("svc: %s(%sv%u), error %d\n",
1086
- __func__, progname, version, error);
1102
+ trace_svc_unregister(progname, version, error);
10871103 }
10881104
10891105 /*
....@@ -1108,9 +1124,6 @@
11081124 continue;
11091125 if (progp->pg_vers[i]->vs_hidden)
11101126 continue;
1111
-
1112
- dprintk("svc: attempting to unregister %sv%u\n",
1113
- progp->pg_name, i);
11141127 __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
11151128 }
11161129 }
....@@ -1144,8 +1157,6 @@
11441157 static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
11451158 #endif
11461159
1147
-extern void svc_tcp_prep_reply_hdr(struct svc_rqst *);
1148
-
11491160 __be32
11501161 svc_return_autherr(struct svc_rqst *rqstp, __be32 auth_err)
11511162 {
....@@ -1162,6 +1173,98 @@
11621173 return rpc_auth_ok;
11631174 }
11641175
1176
+static int
1177
+svc_generic_dispatch(struct svc_rqst *rqstp, __be32 *statp)
1178
+{
1179
+ struct kvec *argv = &rqstp->rq_arg.head[0];
1180
+ struct kvec *resv = &rqstp->rq_res.head[0];
1181
+ const struct svc_procedure *procp = rqstp->rq_procinfo;
1182
+
1183
+ /*
1184
+ * Decode arguments
1185
+ * XXX: why do we ignore the return value?
1186
+ */
1187
+ if (procp->pc_decode &&
1188
+ !procp->pc_decode(rqstp, argv->iov_base)) {
1189
+ *statp = rpc_garbage_args;
1190
+ return 1;
1191
+ }
1192
+
1193
+ *statp = procp->pc_func(rqstp);
1194
+
1195
+ if (*statp == rpc_drop_reply ||
1196
+ test_bit(RQ_DROPME, &rqstp->rq_flags))
1197
+ return 0;
1198
+
1199
+ if (test_bit(RQ_AUTHERR, &rqstp->rq_flags))
1200
+ return 1;
1201
+
1202
+ if (*statp != rpc_success)
1203
+ return 1;
1204
+
1205
+ /* Encode reply */
1206
+ if (procp->pc_encode &&
1207
+ !procp->pc_encode(rqstp, resv->iov_base + resv->iov_len)) {
1208
+ dprintk("svc: failed to encode reply\n");
1209
+ /* serv->sv_stats->rpcsystemerr++; */
1210
+ *statp = rpc_system_err;
1211
+ }
1212
+ return 1;
1213
+}
1214
+
1215
+__be32
1216
+svc_generic_init_request(struct svc_rqst *rqstp,
1217
+ const struct svc_program *progp,
1218
+ struct svc_process_info *ret)
1219
+{
1220
+ const struct svc_version *versp = NULL; /* compiler food */
1221
+ const struct svc_procedure *procp = NULL;
1222
+
1223
+ if (rqstp->rq_vers >= progp->pg_nvers )
1224
+ goto err_bad_vers;
1225
+ versp = progp->pg_vers[rqstp->rq_vers];
1226
+ if (!versp)
1227
+ goto err_bad_vers;
1228
+
1229
+ /*
1230
+ * Some protocol versions (namely NFSv4) require some form of
1231
+ * congestion control. (See RFC 7530 section 3.1 paragraph 2)
1232
+ * In other words, UDP is not allowed. We mark those when setting
1233
+ * up the svc_xprt, and verify that here.
1234
+ *
1235
+ * The spec is not very clear about what error should be returned
1236
+ * when someone tries to access a server that is listening on UDP
1237
+ * for lower versions. RPC_PROG_MISMATCH seems to be the closest
1238
+ * fit.
1239
+ */
1240
+ if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
1241
+ !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
1242
+ goto err_bad_vers;
1243
+
1244
+ if (rqstp->rq_proc >= versp->vs_nproc)
1245
+ goto err_bad_proc;
1246
+ rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc];
1247
+ if (!procp)
1248
+ goto err_bad_proc;
1249
+
1250
+ /* Initialize storage for argp and resp */
1251
+ memset(rqstp->rq_argp, 0, procp->pc_argsize);
1252
+ memset(rqstp->rq_resp, 0, procp->pc_ressize);
1253
+
1254
+ /* Bump per-procedure stats counter */
1255
+ versp->vs_count[rqstp->rq_proc]++;
1256
+
1257
+ ret->dispatch = versp->vs_dispatch;
1258
+ return rpc_success;
1259
+err_bad_vers:
1260
+ ret->mismatch.lovers = progp->pg_lovers;
1261
+ ret->mismatch.hivers = progp->pg_hivers;
1262
+ return rpc_prog_mismatch;
1263
+err_bad_proc:
1264
+ return rpc_proc_unavail;
1265
+}
1266
+EXPORT_SYMBOL_GPL(svc_generic_init_request);
1267
+
11651268 /*
11661269 * Common routine for processing the RPC request.
11671270 */
....@@ -1169,11 +1272,11 @@
11691272 svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
11701273 {
11711274 struct svc_program *progp;
1172
- const struct svc_version *versp = NULL; /* compiler food */
11731275 const struct svc_procedure *procp = NULL;
11741276 struct svc_serv *serv = rqstp->rq_server;
1277
+ struct svc_process_info process;
11751278 __be32 *statp;
1176
- u32 prog, vers, proc;
1279
+ u32 prog, vers;
11771280 __be32 auth_stat, rpc_stat;
11781281 int auth_res;
11791282 __be32 *reply_statp;
....@@ -1188,10 +1291,6 @@
11881291 /* Will be turned off only when NFSv4 Sessions are used */
11891292 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
11901293 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);
11951294
11961295 svc_putu32(resv, rqstp->rq_xid);
11971296
....@@ -1209,8 +1308,8 @@
12091308 svc_putnl(resv, 0); /* ACCEPT */
12101309
12111310 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 */
1311
+ rqstp->rq_vers = svc_getnl(argv); /* version number */
1312
+ rqstp->rq_proc = svc_getnl(argv); /* procedure number */
12141313
12151314 for (progp = serv->sv_program; progp; progp = progp->pg_next)
12161315 if (prog == progp->pg_prog)
....@@ -1227,6 +1326,8 @@
12271326 auth_stat = rpc_autherr_badcred;
12281327 auth_res = progp->pg_authenticate(rqstp);
12291328 }
1329
+ if (auth_res != SVC_OK)
1330
+ trace_svc_authenticate(rqstp, auth_res, auth_stat);
12301331 switch (auth_res) {
12311332 case SVC_OK:
12321333 break;
....@@ -1248,29 +1349,22 @@
12481349 if (progp == NULL)
12491350 goto err_bad_prog;
12501351
1251
- if (vers >= progp->pg_nvers ||
1252
- !(versp = progp->pg_vers[vers]))
1352
+ rpc_stat = progp->pg_init_request(rqstp, progp, &process);
1353
+ switch (rpc_stat) {
1354
+ case rpc_success:
1355
+ break;
1356
+ case rpc_prog_unavail:
1357
+ goto err_bad_prog;
1358
+ case rpc_prog_mismatch:
12531359 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)
1360
+ case rpc_proc_unavail:
12721361 goto err_bad_proc;
1273
- rqstp->rq_procinfo = procp;
1362
+ }
1363
+
1364
+ procp = rqstp->rq_procinfo;
1365
+ /* Should this check go into the dispatcher? */
1366
+ if (!procp || !procp->pc_func)
1367
+ goto err_bad_proc;
12741368
12751369 /* Syntactic check complete */
12761370 serv->sv_stats->rpccnt++;
....@@ -1280,13 +1374,6 @@
12801374 statp = resv->iov_base +resv->iov_len;
12811375 svc_putnl(resv, RPC_SUCCESS);
12821376
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
-
12901377 /* un-reserve some of the out-queue now that we have a
12911378 * better idea of reply size
12921379 */
....@@ -1294,41 +1381,18 @@
12941381 svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
12951382
12961383 /* 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))
1384
+ if (!process.dispatch) {
1385
+ if (!svc_generic_dispatch(rqstp, statp))
1386
+ goto release_dropit;
1387
+ if (*statp == rpc_garbage_args)
13041388 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
- }
13151389 auth_stat = svc_get_autherr(rqstp, statp);
13161390 if (auth_stat != rpc_auth_ok)
13171391 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
- }
13241392 } else {
13251393 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
- }
1394
+ if (!process.dispatch(rqstp, statp))
1395
+ goto release_dropit; /* Release reply info */
13321396 }
13331397
13341398 /* Check RPC status result */
....@@ -1347,6 +1411,9 @@
13471411 goto close_xprt;
13481412 return 1; /* Caller can now send it */
13491413
1414
+release_dropit:
1415
+ if (procp->pc_release)
1416
+ procp->pc_release(rqstp);
13501417 dropit:
13511418 svc_authorise(rqstp); /* doesn't hurt to call this twice */
13521419 dprintk("svc: svc_process dropit\n");
....@@ -1394,16 +1461,16 @@
13941461
13951462 err_bad_vers:
13961463 svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
1397
- vers, prog, progp->pg_name);
1464
+ rqstp->rq_vers, rqstp->rq_prog, progp->pg_name);
13981465
13991466 serv->sv_stats->rpcbadfmt++;
14001467 svc_putnl(resv, RPC_PROG_MISMATCH);
1401
- svc_putnl(resv, progp->pg_lovers);
1402
- svc_putnl(resv, progp->pg_hivers);
1468
+ svc_putnl(resv, process.mismatch.lovers);
1469
+ svc_putnl(resv, process.mismatch.hivers);
14031470 goto sendit;
14041471
14051472 err_bad_proc:
1406
- svc_printk(rqstp, "unknown procedure (%d)\n", proc);
1473
+ svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc);
14071474
14081475 serv->sv_stats->rpcbadfmt++;
14091476 svc_putnl(resv, RPC_PROC_UNAVAIL);
....@@ -1517,13 +1584,13 @@
15171584 /* Parse and execute the bc call */
15181585 proc_error = svc_process_common(rqstp, argv, resv);
15191586
1520
- atomic_inc(&req->rq_xprt->bc_free_slots);
1587
+ atomic_dec(&req->rq_xprt->bc_slot_count);
15211588 if (!proc_error) {
15221589 /* Processing error: drop the request */
15231590 xprt_free_bc_request(req);
1524
- return 0;
1591
+ error = -EINVAL;
1592
+ goto out;
15251593 }
1526
-
15271594 /* Finally, send the reply synchronously */
15281595 memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
15291596 task = rpc_run_bc_task(req);
....@@ -1557,6 +1624,22 @@
15571624 EXPORT_SYMBOL_GPL(svc_max_payload);
15581625
15591626 /**
1627
+ * svc_encode_read_payload - mark a range of bytes as a READ payload
1628
+ * @rqstp: svc_rqst to operate on
1629
+ * @offset: payload's byte offset in rqstp->rq_res
1630
+ * @length: size of payload, in bytes
1631
+ *
1632
+ * Returns zero on success, or a negative errno if a permanent
1633
+ * error occurred.
1634
+ */
1635
+int svc_encode_read_payload(struct svc_rqst *rqstp, unsigned int offset,
1636
+ unsigned int length)
1637
+{
1638
+ return rqstp->rq_xprt->xpt_ops->xpo_read_payload(rqstp, offset, length);
1639
+}
1640
+EXPORT_SYMBOL_GPL(svc_encode_read_payload);
1641
+
1642
+/**
15601643 * svc_fill_write_vector - Construct data argument for VFS write call
15611644 * @rqstp: svc_rqst to operate on
15621645 * @pages: list of pages containing data payload