aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Schumaker <bjschuma@netapp.com>2012-07-16 16:39:18 -0400
committerTrond Myklebust <Trond.Myklebust@netapp.com>2012-07-17 13:33:53 -0400
commitfcf10398f641c4450119f8a4cc27e9e584edb010 (patch)
tree6c102abc69d152affb678ef1ca80f67c409839e4
parent428360d77c801932e4b28f15160aebbdb5f5a03e (diff)
NFS: Split out NFS v4 server creating code
These functions are specific to NFS v4 and can be moved to nfs4client.c to keep them out of the generic client. Signed-off-by: Bryan Schumaker <bjschuma@netapp.com> Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
-rw-r--r--fs/nfs/client.c457
-rw-r--r--fs/nfs/internal.h25
-rw-r--r--fs/nfs/nfs4client.c428
3 files changed, 462 insertions, 448 deletions
diff --git a/fs/nfs/client.c b/fs/nfs/client.c
index 5664c7bbe50d..0d50629d9e25 100644
--- a/fs/nfs/client.c
+++ b/fs/nfs/client.c
@@ -82,11 +82,6 @@ retry:
82#endif /* CONFIG_NFS_V4 */ 82#endif /* CONFIG_NFS_V4 */
83 83
84/* 84/*
85 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
86 */
87static bool nfs4_disable_idmapping = true;
88
89/*
90 * RPC cruft for NFS 85 * RPC cruft for NFS
91 */ 86 */
92static const struct rpc_version *nfs_version[5] = { 87static const struct rpc_version *nfs_version[5] = {
@@ -130,17 +125,6 @@ const struct rpc_program nfsacl_program = {
130}; 125};
131#endif /* CONFIG_NFS_V3_ACL */ 126#endif /* CONFIG_NFS_V3_ACL */
132 127
133struct nfs_client_initdata {
134 unsigned long init_flags;
135 const char *hostname;
136 const struct sockaddr *addr;
137 size_t addrlen;
138 const struct nfs_rpc_ops *rpc_ops;
139 int proto;
140 u32 minorversion;
141 struct net *net;
142};
143
144/* 128/*
145 * Allocate a shared client record 129 * Allocate a shared client record
146 * 130 *
@@ -282,13 +266,6 @@ static void pnfs_init_server(struct nfs_server *server)
282 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC"); 266 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
283} 267}
284 268
285static void nfs4_destroy_server(struct nfs_server *server)
286{
287 nfs_server_return_all_delegations(server);
288 unset_pnfs_layoutdriver(server);
289 nfs4_purge_state_owners(server);
290}
291
292#else 269#else
293void nfs_cleanup_cb_ident_idr(struct net *net) 270void nfs_cleanup_cb_ident_idr(struct net *net)
294{ 271{
@@ -426,8 +403,8 @@ static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
426 * Test if two socket addresses represent the same actual socket, 403 * Test if two socket addresses represent the same actual socket,
427 * by comparing (only) relevant fields, excluding the port number. 404 * by comparing (only) relevant fields, excluding the port number.
428 */ 405 */
429static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1, 406int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
430 const struct sockaddr *sa2) 407 const struct sockaddr *sa2)
431{ 408{
432 if (sa1->sa_family != sa2->sa_family) 409 if (sa1->sa_family != sa2->sa_family)
433 return 0; 410 return 0;
@@ -461,33 +438,6 @@ static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
461 return 0; 438 return 0;
462} 439}
463 440
464#if defined(CONFIG_NFS_V4_1)
465/* Common match routine for v4.0 and v4.1 callback services */
466static bool nfs4_cb_match_client(const struct sockaddr *addr,
467 struct nfs_client *clp, u32 minorversion)
468{
469 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
470
471 /* Don't match clients that failed to initialise */
472 if (!(clp->cl_cons_state == NFS_CS_READY ||
473 clp->cl_cons_state == NFS_CS_SESSION_INITING))
474 return false;
475
476 smp_rmb();
477
478 /* Match the version and minorversion */
479 if (clp->rpc_ops->version != 4 ||
480 clp->cl_minorversion != minorversion)
481 return false;
482
483 /* Match only the IP address, not the port number */
484 if (!nfs_sockaddr_match_ipaddr(addr, clap))
485 return false;
486
487 return true;
488}
489#endif /* CONFIG_NFS_V4_1 */
490
491/* 441/*
492 * Find an nfs_client on the list that matches the initialisation data 442 * Find an nfs_client on the list that matches the initialisation data
493 * that is supplied. 443 * that is supplied.
@@ -566,7 +516,7 @@ nfs_found_client(const struct nfs_client_initdata *cl_init,
566 * Look up a client by IP address and protocol version 516 * Look up a client by IP address and protocol version
567 * - creates a new record if one doesn't yet exist 517 * - creates a new record if one doesn't yet exist
568 */ 518 */
569static struct nfs_client * 519struct nfs_client *
570nfs_get_client(const struct nfs_client_initdata *cl_init, 520nfs_get_client(const struct nfs_client_initdata *cl_init,
571 const struct rpc_timeout *timeparms, 521 const struct rpc_timeout *timeparms,
572 const char *ip_addr, 522 const char *ip_addr,
@@ -621,7 +571,7 @@ void nfs_mark_client_ready(struct nfs_client *clp, int state)
621/* 571/*
622 * Initialise the timeout values for a connection 572 * Initialise the timeout values for a connection
623 */ 573 */
624static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, 574void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
625 unsigned int timeo, unsigned int retrans) 575 unsigned int timeo, unsigned int retrans)
626{ 576{
627 to->to_initval = timeo * HZ / 10; 577 to->to_initval = timeo * HZ / 10;
@@ -781,7 +731,7 @@ static inline void nfs_init_server_aclclient(struct nfs_server *server)
781/* 731/*
782 * Create a general RPC client 732 * Create a general RPC client
783 */ 733 */
784static int nfs_init_server_rpcclient(struct nfs_server *server, 734int nfs_init_server_rpcclient(struct nfs_server *server,
785 const struct rpc_timeout *timeo, 735 const struct rpc_timeout *timeo,
786 rpc_authflavor_t pseudoflavour) 736 rpc_authflavor_t pseudoflavour)
787{ 737{
@@ -1014,7 +964,7 @@ static void nfs_server_set_fsinfo(struct nfs_server *server,
1014/* 964/*
1015 * Probe filesystem information, including the FSID on v2/v3 965 * Probe filesystem information, including the FSID on v2/v3
1016 */ 966 */
1017static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) 967int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
1018{ 968{
1019 struct nfs_fsinfo fsinfo; 969 struct nfs_fsinfo fsinfo;
1020 struct nfs_client *clp = server->nfs_client; 970 struct nfs_client *clp = server->nfs_client;
@@ -1058,7 +1008,7 @@ out_error:
1058/* 1008/*
1059 * Copy useful information when duplicating a server record 1009 * Copy useful information when duplicating a server record
1060 */ 1010 */
1061static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) 1011void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1062{ 1012{
1063 target->flags = source->flags; 1013 target->flags = source->flags;
1064 target->rsize = source->rsize; 1014 target->rsize = source->rsize;
@@ -1071,7 +1021,7 @@ static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_serve
1071 target->options = source->options; 1021 target->options = source->options;
1072} 1022}
1073 1023
1074static void nfs_server_insert_lists(struct nfs_server *server) 1024void nfs_server_insert_lists(struct nfs_server *server)
1075{ 1025{
1076 struct nfs_client *clp = server->nfs_client; 1026 struct nfs_client *clp = server->nfs_client;
1077 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 1027 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
@@ -1105,7 +1055,7 @@ static void nfs_server_remove_lists(struct nfs_server *server)
1105/* 1055/*
1106 * Allocate and initialise a server record 1056 * Allocate and initialise a server record
1107 */ 1057 */
1108static struct nfs_server *nfs_alloc_server(void) 1058struct nfs_server *nfs_alloc_server(void)
1109{ 1059{
1110 struct nfs_server *server; 1060 struct nfs_server *server;
1111 1061
@@ -1238,391 +1188,6 @@ error:
1238 return ERR_PTR(error); 1188 return ERR_PTR(error);
1239} 1189}
1240 1190
1241#ifdef CONFIG_NFS_V4
1242/*
1243 * NFSv4.0 callback thread helper
1244 *
1245 * Find a client by callback identifier
1246 */
1247struct nfs_client *
1248nfs4_find_client_ident(struct net *net, int cb_ident)
1249{
1250 struct nfs_client *clp;
1251 struct nfs_net *nn = net_generic(net, nfs_net_id);
1252
1253 spin_lock(&nn->nfs_client_lock);
1254 clp = idr_find(&nn->cb_ident_idr, cb_ident);
1255 if (clp)
1256 atomic_inc(&clp->cl_count);
1257 spin_unlock(&nn->nfs_client_lock);
1258 return clp;
1259}
1260
1261#if defined(CONFIG_NFS_V4_1)
1262/*
1263 * NFSv4.1 callback thread helper
1264 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1265 * minorversion, and sessionID
1266 *
1267 * Returns NULL if no such client
1268 */
1269struct nfs_client *
1270nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
1271 struct nfs4_sessionid *sid)
1272{
1273 struct nfs_client *clp;
1274 struct nfs_net *nn = net_generic(net, nfs_net_id);
1275
1276 spin_lock(&nn->nfs_client_lock);
1277 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
1278 if (nfs4_cb_match_client(addr, clp, 1) == false)
1279 continue;
1280
1281 if (!nfs4_has_session(clp))
1282 continue;
1283
1284 /* Match sessionid*/
1285 if (memcmp(clp->cl_session->sess_id.data,
1286 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
1287 continue;
1288
1289 atomic_inc(&clp->cl_count);
1290 spin_unlock(&nn->nfs_client_lock);
1291 return clp;
1292 }
1293 spin_unlock(&nn->nfs_client_lock);
1294 return NULL;
1295}
1296
1297#else /* CONFIG_NFS_V4_1 */
1298
1299struct nfs_client *
1300nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
1301 struct nfs4_sessionid *sid)
1302{
1303 return NULL;
1304}
1305#endif /* CONFIG_NFS_V4_1 */
1306
1307/*
1308 * Set up an NFS4 client
1309 */
1310static int nfs4_set_client(struct nfs_server *server,
1311 const char *hostname,
1312 const struct sockaddr *addr,
1313 const size_t addrlen,
1314 const char *ip_addr,
1315 rpc_authflavor_t authflavour,
1316 int proto, const struct rpc_timeout *timeparms,
1317 u32 minorversion, struct net *net)
1318{
1319 struct nfs_client_initdata cl_init = {
1320 .hostname = hostname,
1321 .addr = addr,
1322 .addrlen = addrlen,
1323 .rpc_ops = &nfs_v4_clientops,
1324 .proto = proto,
1325 .minorversion = minorversion,
1326 .net = net,
1327 };
1328 struct nfs_client *clp;
1329 int error;
1330
1331 dprintk("--> nfs4_set_client()\n");
1332
1333 if (server->flags & NFS_MOUNT_NORESVPORT)
1334 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
1335
1336 /* Allocate or find a client reference we can use */
1337 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
1338 if (IS_ERR(clp)) {
1339 error = PTR_ERR(clp);
1340 goto error;
1341 }
1342
1343 /*
1344 * Query for the lease time on clientid setup or renewal
1345 *
1346 * Note that this will be set on nfs_clients that were created
1347 * only for the DS role and did not set this bit, but now will
1348 * serve a dual role.
1349 */
1350 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1351
1352 server->nfs_client = clp;
1353 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1354 return 0;
1355error:
1356 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1357 return error;
1358}
1359
1360/*
1361 * Set up a pNFS Data Server client.
1362 *
1363 * Return any existing nfs_client that matches server address,port,version
1364 * and minorversion.
1365 *
1366 * For a new nfs_client, use a soft mount (default), a low retrans and a
1367 * low timeout interval so that if a connection is lost, we retry through
1368 * the MDS.
1369 */
1370struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1371 const struct sockaddr *ds_addr, int ds_addrlen,
1372 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
1373{
1374 struct nfs_client_initdata cl_init = {
1375 .addr = ds_addr,
1376 .addrlen = ds_addrlen,
1377 .rpc_ops = &nfs_v4_clientops,
1378 .proto = ds_proto,
1379 .minorversion = mds_clp->cl_minorversion,
1380 .net = mds_clp->cl_net,
1381 };
1382 struct rpc_timeout ds_timeout;
1383 struct nfs_client *clp;
1384
1385 /*
1386 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1387 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1388 * (section 13.1 RFC 5661).
1389 */
1390 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
1391 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1392 mds_clp->cl_rpcclient->cl_auth->au_flavor);
1393
1394 dprintk("<-- %s %p\n", __func__, clp);
1395 return clp;
1396}
1397EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
1398
1399/*
1400 * Session has been established, and the client marked ready.
1401 * Set the mount rsize and wsize with negotiated fore channel
1402 * attributes which will be bound checked in nfs_server_set_fsinfo.
1403 */
1404static void nfs4_session_set_rwsize(struct nfs_server *server)
1405{
1406#ifdef CONFIG_NFS_V4_1
1407 struct nfs4_session *sess;
1408 u32 server_resp_sz;
1409 u32 server_rqst_sz;
1410
1411 if (!nfs4_has_session(server->nfs_client))
1412 return;
1413 sess = server->nfs_client->cl_session;
1414 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1415 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1416
1417 if (server->rsize > server_resp_sz)
1418 server->rsize = server_resp_sz;
1419 if (server->wsize > server_rqst_sz)
1420 server->wsize = server_rqst_sz;
1421#endif /* CONFIG_NFS_V4_1 */
1422}
1423
1424static int nfs4_server_common_setup(struct nfs_server *server,
1425 struct nfs_fh *mntfh)
1426{
1427 struct nfs_fattr *fattr;
1428 int error;
1429
1430 BUG_ON(!server->nfs_client);
1431 BUG_ON(!server->nfs_client->rpc_ops);
1432 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1433
1434 /* data servers support only a subset of NFSv4.1 */
1435 if (is_ds_only_client(server->nfs_client))
1436 return -EPROTONOSUPPORT;
1437
1438 fattr = nfs_alloc_fattr();
1439 if (fattr == NULL)
1440 return -ENOMEM;
1441
1442 /* We must ensure the session is initialised first */
1443 error = nfs4_init_session(server);
1444 if (error < 0)
1445 goto out;
1446
1447 /* Probe the root fh to retrieve its FSID and filehandle */
1448 error = nfs4_get_rootfh(server, mntfh);
1449 if (error < 0)
1450 goto out;
1451
1452 dprintk("Server FSID: %llx:%llx\n",
1453 (unsigned long long) server->fsid.major,
1454 (unsigned long long) server->fsid.minor);
1455 dprintk("Mount FH: %d\n", mntfh->size);
1456
1457 nfs4_session_set_rwsize(server);
1458
1459 error = nfs_probe_fsinfo(server, mntfh, fattr);
1460 if (error < 0)
1461 goto out;
1462
1463 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1464 server->namelen = NFS4_MAXNAMLEN;
1465
1466 nfs_server_insert_lists(server);
1467 server->mount_time = jiffies;
1468 server->destroy = nfs4_destroy_server;
1469out:
1470 nfs_free_fattr(fattr);
1471 return error;
1472}
1473
1474/*
1475 * Create a version 4 volume record
1476 */
1477static int nfs4_init_server(struct nfs_server *server,
1478 const struct nfs_parsed_mount_data *data)
1479{
1480 struct rpc_timeout timeparms;
1481 int error;
1482
1483 dprintk("--> nfs4_init_server()\n");
1484
1485 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1486 data->timeo, data->retrans);
1487
1488 /* Initialise the client representation from the mount data */
1489 server->flags = data->flags;
1490 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1491 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1492 server->caps |= NFS_CAP_READDIRPLUS;
1493 server->options = data->options;
1494
1495 /* Get a client record */
1496 error = nfs4_set_client(server,
1497 data->nfs_server.hostname,
1498 (const struct sockaddr *)&data->nfs_server.address,
1499 data->nfs_server.addrlen,
1500 data->client_address,
1501 data->auth_flavors[0],
1502 data->nfs_server.protocol,
1503 &timeparms,
1504 data->minorversion,
1505 data->net);
1506 if (error < 0)
1507 goto error;
1508
1509 /*
1510 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1511 * authentication.
1512 */
1513 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1514 server->caps |= NFS_CAP_UIDGID_NOMAP;
1515
1516 if (data->rsize)
1517 server->rsize = nfs_block_size(data->rsize, NULL);
1518 if (data->wsize)
1519 server->wsize = nfs_block_size(data->wsize, NULL);
1520
1521 server->acregmin = data->acregmin * HZ;
1522 server->acregmax = data->acregmax * HZ;
1523 server->acdirmin = data->acdirmin * HZ;
1524 server->acdirmax = data->acdirmax * HZ;
1525
1526 server->port = data->nfs_server.port;
1527
1528 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1529
1530error:
1531 /* Done */
1532 dprintk("<-- nfs4_init_server() = %d\n", error);
1533 return error;
1534}
1535
1536/*
1537 * Create a version 4 volume record
1538 * - keyed on server and FSID
1539 */
1540struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1541 struct nfs_fh *mntfh)
1542{
1543 struct nfs_server *server;
1544 int error;
1545
1546 dprintk("--> nfs4_create_server()\n");
1547
1548 server = nfs_alloc_server();
1549 if (!server)
1550 return ERR_PTR(-ENOMEM);
1551
1552 /* set up the general RPC client */
1553 error = nfs4_init_server(server, data);
1554 if (error < 0)
1555 goto error;
1556
1557 error = nfs4_server_common_setup(server, mntfh);
1558 if (error < 0)
1559 goto error;
1560
1561 dprintk("<-- nfs4_create_server() = %p\n", server);
1562 return server;
1563
1564error:
1565 nfs_free_server(server);
1566 dprintk("<-- nfs4_create_server() = error %d\n", error);
1567 return ERR_PTR(error);
1568}
1569
1570/*
1571 * Create an NFS4 referral server record
1572 */
1573struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1574 struct nfs_fh *mntfh)
1575{
1576 struct nfs_client *parent_client;
1577 struct nfs_server *server, *parent_server;
1578 int error;
1579
1580 dprintk("--> nfs4_create_referral_server()\n");
1581
1582 server = nfs_alloc_server();
1583 if (!server)
1584 return ERR_PTR(-ENOMEM);
1585
1586 parent_server = NFS_SB(data->sb);
1587 parent_client = parent_server->nfs_client;
1588
1589 /* Initialise the client representation from the parent server */
1590 nfs_server_copy_userdata(server, parent_server);
1591 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
1592
1593 /* Get a client representation.
1594 * Note: NFSv4 always uses TCP, */
1595 error = nfs4_set_client(server, data->hostname,
1596 data->addr,
1597 data->addrlen,
1598 parent_client->cl_ipaddr,
1599 data->authflavor,
1600 rpc_protocol(parent_server->client),
1601 parent_server->client->cl_timeout,
1602 parent_client->cl_mvops->minor_version,
1603 parent_client->cl_net);
1604 if (error < 0)
1605 goto error;
1606
1607 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1608 if (error < 0)
1609 goto error;
1610
1611 error = nfs4_server_common_setup(server, mntfh);
1612 if (error < 0)
1613 goto error;
1614
1615 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1616 return server;
1617
1618error:
1619 nfs_free_server(server);
1620 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1621 return ERR_PTR(error);
1622}
1623
1624#endif /* CONFIG_NFS_V4 */
1625
1626/* 1191/*
1627 * Clone an NFS2, NFS3 or NFS4 server record 1192 * Clone an NFS2, NFS3 or NFS4 server record
1628 */ 1193 */
@@ -1972,7 +1537,3 @@ void nfs_fs_proc_exit(void)
1972} 1537}
1973 1538
1974#endif /* CONFIG_PROC_FS */ 1539#endif /* CONFIG_PROC_FS */
1975
1976module_param(nfs4_disable_idmapping, bool, 0644);
1977MODULE_PARM_DESC(nfs4_disable_idmapping,
1978 "Turn off NFSv4 idmapping when using 'sec=sys'");
diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
index 10df28d14f83..b4a35705246c 100644
--- a/fs/nfs/internal.h
+++ b/fs/nfs/internal.h
@@ -85,6 +85,17 @@ struct nfs_clone_mount {
85 */ 85 */
86#define NFS_MAX_READDIR_PAGES 8 86#define NFS_MAX_READDIR_PAGES 8
87 87
88struct nfs_client_initdata {
89 unsigned long init_flags;
90 const char *hostname;
91 const struct sockaddr *addr;
92 size_t addrlen;
93 const struct nfs_rpc_ops *rpc_ops;
94 int proto;
95 u32 minorversion;
96 struct net *net;
97};
98
88/* 99/*
89 * In-kernel mount arguments 100 * In-kernel mount arguments
90 */ 101 */
@@ -150,6 +161,16 @@ extern const struct rpc_program nfs_program;
150extern void nfs_clients_init(struct net *net); 161extern void nfs_clients_init(struct net *net);
151extern struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *); 162extern struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *);
152int nfs_create_rpc_client(struct nfs_client *, const struct rpc_timeout *, rpc_authflavor_t); 163int nfs_create_rpc_client(struct nfs_client *, const struct rpc_timeout *, rpc_authflavor_t);
164struct nfs_client *nfs_get_client(const struct nfs_client_initdata *,
165 const struct rpc_timeout *, const char *,
166 rpc_authflavor_t);
167int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *, struct nfs_fattr *);
168void nfs_server_insert_lists(struct nfs_server *);
169void nfs_init_timeout_values(struct rpc_timeout *, int, unsigned int, unsigned int);
170int nfs_init_server_rpcclient(struct nfs_server *, const struct rpc_timeout *t,
171 rpc_authflavor_t);
172struct nfs_server *nfs_alloc_server(void);
173void nfs_server_copy_userdata(struct nfs_server *, struct nfs_server *);
153 174
154extern void nfs_cleanup_cb_ident_idr(struct net *); 175extern void nfs_cleanup_cb_ident_idr(struct net *);
155extern void nfs_put_client(struct nfs_client *); 176extern void nfs_put_client(struct nfs_client *);
@@ -191,6 +212,10 @@ static inline void nfs_fs_proc_exit(void)
191} 212}
192#endif 213#endif
193 214
215#ifdef CONFIG_NFS_V4_1
216int nfs_sockaddr_match_ipaddr(const struct sockaddr *, const struct sockaddr *);
217#endif
218
194/* callback_xdr.c */ 219/* callback_xdr.c */
195extern struct svc_version nfs4_callback_version1; 220extern struct svc_version nfs4_callback_version1;
196extern struct svc_version nfs4_callback_version4; 221extern struct svc_version nfs4_callback_version4;
diff --git a/fs/nfs/nfs4client.c b/fs/nfs/nfs4client.c
index c5234b589907..a71d95ecbea9 100644
--- a/fs/nfs/nfs4client.c
+++ b/fs/nfs/nfs4client.c
@@ -2,17 +2,27 @@
2 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 2 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
3 * Written by David Howells (dhowells@redhat.com) 3 * Written by David Howells (dhowells@redhat.com)
4 */ 4 */
5#include <linux/module.h>
5#include <linux/nfs_fs.h> 6#include <linux/nfs_fs.h>
6#include <linux/nfs_idmap.h> 7#include <linux/nfs_idmap.h>
8#include <linux/nfs_mount.h>
7#include <linux/sunrpc/auth.h> 9#include <linux/sunrpc/auth.h>
8#include <linux/sunrpc/xprt.h> 10#include <linux/sunrpc/xprt.h>
9#include <linux/sunrpc/bc_xprt.h> 11#include <linux/sunrpc/bc_xprt.h>
10#include "internal.h" 12#include "internal.h"
11#include "callback.h" 13#include "callback.h"
14#include "delegation.h"
15#include "pnfs.h"
16#include "netns.h"
12 17
13#define NFSDBG_FACILITY NFSDBG_CLIENT 18#define NFSDBG_FACILITY NFSDBG_CLIENT
14 19
15/* 20/*
21 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
22 */
23static bool nfs4_disable_idmapping = true;
24
25/*
16 * Initialize the NFS4 callback service 26 * Initialize the NFS4 callback service
17 */ 27 */
18static int nfs4_init_callback(struct nfs_client *clp) 28static int nfs4_init_callback(struct nfs_client *clp)
@@ -142,3 +152,421 @@ error:
142 dprintk("<-- nfs4_init_client() = xerror %d\n", error); 152 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
143 return ERR_PTR(error); 153 return ERR_PTR(error);
144} 154}
155
156static void nfs4_destroy_server(struct nfs_server *server)
157{
158 nfs_server_return_all_delegations(server);
159 unset_pnfs_layoutdriver(server);
160 nfs4_purge_state_owners(server);
161}
162
163/*
164 * NFSv4.0 callback thread helper
165 *
166 * Find a client by callback identifier
167 */
168struct nfs_client *
169nfs4_find_client_ident(struct net *net, int cb_ident)
170{
171 struct nfs_client *clp;
172 struct nfs_net *nn = net_generic(net, nfs_net_id);
173
174 spin_lock(&nn->nfs_client_lock);
175 clp = idr_find(&nn->cb_ident_idr, cb_ident);
176 if (clp)
177 atomic_inc(&clp->cl_count);
178 spin_unlock(&nn->nfs_client_lock);
179 return clp;
180}
181
182#if defined(CONFIG_NFS_V4_1)
183/* Common match routine for v4.0 and v4.1 callback services */
184static bool nfs4_cb_match_client(const struct sockaddr *addr,
185 struct nfs_client *clp, u32 minorversion)
186{
187 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
188
189 /* Don't match clients that failed to initialise */
190 if (!(clp->cl_cons_state == NFS_CS_READY ||
191 clp->cl_cons_state == NFS_CS_SESSION_INITING))
192 return false;
193
194 smp_rmb();
195
196 /* Match the version and minorversion */
197 if (clp->rpc_ops->version != 4 ||
198 clp->cl_minorversion != minorversion)
199 return false;
200
201 /* Match only the IP address, not the port number */
202 if (!nfs_sockaddr_match_ipaddr(addr, clap))
203 return false;
204
205 return true;
206}
207
208/*
209 * NFSv4.1 callback thread helper
210 * For CB_COMPOUND calls, find a client by IP address, protocol version,
211 * minorversion, and sessionID
212 *
213 * Returns NULL if no such client
214 */
215struct nfs_client *
216nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
217 struct nfs4_sessionid *sid)
218{
219 struct nfs_client *clp;
220 struct nfs_net *nn = net_generic(net, nfs_net_id);
221
222 spin_lock(&nn->nfs_client_lock);
223 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
224 if (nfs4_cb_match_client(addr, clp, 1) == false)
225 continue;
226
227 if (!nfs4_has_session(clp))
228 continue;
229
230 /* Match sessionid*/
231 if (memcmp(clp->cl_session->sess_id.data,
232 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
233 continue;
234
235 atomic_inc(&clp->cl_count);
236 spin_unlock(&nn->nfs_client_lock);
237 return clp;
238 }
239 spin_unlock(&nn->nfs_client_lock);
240 return NULL;
241}
242
243#else /* CONFIG_NFS_V4_1 */
244
245struct nfs_client *
246nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
247 struct nfs4_sessionid *sid)
248{
249 return NULL;
250}
251#endif /* CONFIG_NFS_V4_1 */
252
253/*
254 * Set up an NFS4 client
255 */
256static int nfs4_set_client(struct nfs_server *server,
257 const char *hostname,
258 const struct sockaddr *addr,
259 const size_t addrlen,
260 const char *ip_addr,
261 rpc_authflavor_t authflavour,
262 int proto, const struct rpc_timeout *timeparms,
263 u32 minorversion, struct net *net)
264{
265 struct nfs_client_initdata cl_init = {
266 .hostname = hostname,
267 .addr = addr,
268 .addrlen = addrlen,
269 .rpc_ops = &nfs_v4_clientops,
270 .proto = proto,
271 .minorversion = minorversion,
272 .net = net,
273 };
274 struct nfs_client *clp;
275 int error;
276
277 dprintk("--> nfs4_set_client()\n");
278
279 if (server->flags & NFS_MOUNT_NORESVPORT)
280 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
281
282 /* Allocate or find a client reference we can use */
283 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
284 if (IS_ERR(clp)) {
285 error = PTR_ERR(clp);
286 goto error;
287 }
288
289 /*
290 * Query for the lease time on clientid setup or renewal
291 *
292 * Note that this will be set on nfs_clients that were created
293 * only for the DS role and did not set this bit, but now will
294 * serve a dual role.
295 */
296 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
297
298 server->nfs_client = clp;
299 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
300 return 0;
301error:
302 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
303 return error;
304}
305
306/*
307 * Set up a pNFS Data Server client.
308 *
309 * Return any existing nfs_client that matches server address,port,version
310 * and minorversion.
311 *
312 * For a new nfs_client, use a soft mount (default), a low retrans and a
313 * low timeout interval so that if a connection is lost, we retry through
314 * the MDS.
315 */
316struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
317 const struct sockaddr *ds_addr, int ds_addrlen,
318 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
319{
320 struct nfs_client_initdata cl_init = {
321 .addr = ds_addr,
322 .addrlen = ds_addrlen,
323 .rpc_ops = &nfs_v4_clientops,
324 .proto = ds_proto,
325 .minorversion = mds_clp->cl_minorversion,
326 .net = mds_clp->cl_net,
327 };
328 struct rpc_timeout ds_timeout;
329 struct nfs_client *clp;
330
331 /*
332 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
333 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
334 * (section 13.1 RFC 5661).
335 */
336 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
337 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
338 mds_clp->cl_rpcclient->cl_auth->au_flavor);
339
340 dprintk("<-- %s %p\n", __func__, clp);
341 return clp;
342}
343EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
344
345/*
346 * Session has been established, and the client marked ready.
347 * Set the mount rsize and wsize with negotiated fore channel
348 * attributes which will be bound checked in nfs_server_set_fsinfo.
349 */
350static void nfs4_session_set_rwsize(struct nfs_server *server)
351{
352#ifdef CONFIG_NFS_V4_1
353 struct nfs4_session *sess;
354 u32 server_resp_sz;
355 u32 server_rqst_sz;
356
357 if (!nfs4_has_session(server->nfs_client))
358 return;
359 sess = server->nfs_client->cl_session;
360 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
361 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
362
363 if (server->rsize > server_resp_sz)
364 server->rsize = server_resp_sz;
365 if (server->wsize > server_rqst_sz)
366 server->wsize = server_rqst_sz;
367#endif /* CONFIG_NFS_V4_1 */
368}
369
370static int nfs4_server_common_setup(struct nfs_server *server,
371 struct nfs_fh *mntfh)
372{
373 struct nfs_fattr *fattr;
374 int error;
375
376 BUG_ON(!server->nfs_client);
377 BUG_ON(!server->nfs_client->rpc_ops);
378 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
379
380 /* data servers support only a subset of NFSv4.1 */
381 if (is_ds_only_client(server->nfs_client))
382 return -EPROTONOSUPPORT;
383
384 fattr = nfs_alloc_fattr();
385 if (fattr == NULL)
386 return -ENOMEM;
387
388 /* We must ensure the session is initialised first */
389 error = nfs4_init_session(server);
390 if (error < 0)
391 goto out;
392
393 /* Probe the root fh to retrieve its FSID and filehandle */
394 error = nfs4_get_rootfh(server, mntfh);
395 if (error < 0)
396 goto out;
397
398 dprintk("Server FSID: %llx:%llx\n",
399 (unsigned long long) server->fsid.major,
400 (unsigned long long) server->fsid.minor);
401 dprintk("Mount FH: %d\n", mntfh->size);
402
403 nfs4_session_set_rwsize(server);
404
405 error = nfs_probe_fsinfo(server, mntfh, fattr);
406 if (error < 0)
407 goto out;
408
409 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
410 server->namelen = NFS4_MAXNAMLEN;
411
412 nfs_server_insert_lists(server);
413 server->mount_time = jiffies;
414 server->destroy = nfs4_destroy_server;
415out:
416 nfs_free_fattr(fattr);
417 return error;
418}
419
420/*
421 * Create a version 4 volume record
422 */
423static int nfs4_init_server(struct nfs_server *server,
424 const struct nfs_parsed_mount_data *data)
425{
426 struct rpc_timeout timeparms;
427 int error;
428
429 dprintk("--> nfs4_init_server()\n");
430
431 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
432 data->timeo, data->retrans);
433
434 /* Initialise the client representation from the mount data */
435 server->flags = data->flags;
436 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
437 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
438 server->caps |= NFS_CAP_READDIRPLUS;
439 server->options = data->options;
440
441 /* Get a client record */
442 error = nfs4_set_client(server,
443 data->nfs_server.hostname,
444 (const struct sockaddr *)&data->nfs_server.address,
445 data->nfs_server.addrlen,
446 data->client_address,
447 data->auth_flavors[0],
448 data->nfs_server.protocol,
449 &timeparms,
450 data->minorversion,
451 data->net);
452 if (error < 0)
453 goto error;
454
455 /*
456 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
457 * authentication.
458 */
459 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
460 server->caps |= NFS_CAP_UIDGID_NOMAP;
461
462 if (data->rsize)
463 server->rsize = nfs_block_size(data->rsize, NULL);
464 if (data->wsize)
465 server->wsize = nfs_block_size(data->wsize, NULL);
466
467 server->acregmin = data->acregmin * HZ;
468 server->acregmax = data->acregmax * HZ;
469 server->acdirmin = data->acdirmin * HZ;
470 server->acdirmax = data->acdirmax * HZ;
471
472 server->port = data->nfs_server.port;
473
474 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
475
476error:
477 /* Done */
478 dprintk("<-- nfs4_init_server() = %d\n", error);
479 return error;
480}
481
482/*
483 * Create a version 4 volume record
484 * - keyed on server and FSID
485 */
486struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
487 struct nfs_fh *mntfh)
488{
489 struct nfs_server *server;
490 int error;
491
492 dprintk("--> nfs4_create_server()\n");
493
494 server = nfs_alloc_server();
495 if (!server)
496 return ERR_PTR(-ENOMEM);
497
498 /* set up the general RPC client */
499 error = nfs4_init_server(server, data);
500 if (error < 0)
501 goto error;
502
503 error = nfs4_server_common_setup(server, mntfh);
504 if (error < 0)
505 goto error;
506
507 dprintk("<-- nfs4_create_server() = %p\n", server);
508 return server;
509
510error:
511 nfs_free_server(server);
512 dprintk("<-- nfs4_create_server() = error %d\n", error);
513 return ERR_PTR(error);
514}
515
516/*
517 * Create an NFS4 referral server record
518 */
519struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
520 struct nfs_fh *mntfh)
521{
522 struct nfs_client *parent_client;
523 struct nfs_server *server, *parent_server;
524 int error;
525
526 dprintk("--> nfs4_create_referral_server()\n");
527
528 server = nfs_alloc_server();
529 if (!server)
530 return ERR_PTR(-ENOMEM);
531
532 parent_server = NFS_SB(data->sb);
533 parent_client = parent_server->nfs_client;
534
535 /* Initialise the client representation from the parent server */
536 nfs_server_copy_userdata(server, parent_server);
537 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
538
539 /* Get a client representation.
540 * Note: NFSv4 always uses TCP, */
541 error = nfs4_set_client(server, data->hostname,
542 data->addr,
543 data->addrlen,
544 parent_client->cl_ipaddr,
545 data->authflavor,
546 rpc_protocol(parent_server->client),
547 parent_server->client->cl_timeout,
548 parent_client->cl_mvops->minor_version,
549 parent_client->cl_net);
550 if (error < 0)
551 goto error;
552
553 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
554 if (error < 0)
555 goto error;
556
557 error = nfs4_server_common_setup(server, mntfh);
558 if (error < 0)
559 goto error;
560
561 dprintk("<-- nfs_create_referral_server() = %p\n", server);
562 return server;
563
564error:
565 nfs_free_server(server);
566 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
567 return ERR_PTR(error);
568}
569
570module_param(nfs4_disable_idmapping, bool, 0644);
571MODULE_PARM_DESC(nfs4_disable_idmapping,
572 "Turn off NFSv4 idmapping when using 'sec=sys'");