Skip to content

Commit c63f0e4

Browse files
neilbrownAnna Schumaker
authored andcommitted
nfsd: add nfsd_file_acquire_local()
nfsd_file_acquire_local() can be used to look up a file by filehandle without having a struct svc_rqst. This can be used by NFS LOCALIO to allow the NFS client to bypass the NFS protocol to directly access a file provided by the NFS server which is running in the same kernel. In nfsd_file_do_acquire() care is taken to always use fh_verify() if rqstp is not NULL (as is the case for non-LOCALIO callers). Otherwise the non-LOCALIO callers will not supply the correct and required arguments to __fh_verify (e.g. gssclient isn't passed). Introduce fh_verify_local() wrapper around __fh_verify to make it clear that LOCALIO is intended caller. Also, use GC for nfsd_file returned by nfsd_file_acquire_local. GC offers performance improvements if/when a file is reopened before launderette cleans it from the filecache's LRU. Suggested-by: Jeff Layton <[email protected]> # use filecache's GC Signed-off-by: NeilBrown <[email protected]> Co-developed-by: Mike Snitzer <[email protected]> Signed-off-by: Mike Snitzer <[email protected]> Signed-off-by: Chuck Lever <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Anna Schumaker <[email protected]>
1 parent 5e66d2d commit c63f0e4

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

fs/nfsd/filecache.c

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -982,12 +982,14 @@ nfsd_file_is_cached(struct inode *inode)
982982
}
983983

984984
static __be32
985-
nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
985+
nfsd_file_do_acquire(struct svc_rqst *rqstp, struct net *net,
986+
struct svc_cred *cred,
987+
struct auth_domain *client,
988+
struct svc_fh *fhp,
986989
unsigned int may_flags, struct file *file,
987990
struct nfsd_file **pnf, bool want_gc)
988991
{
989992
unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
990-
struct net *net = SVC_NET(rqstp);
991993
struct nfsd_file *new, *nf;
992994
bool stale_retry = true;
993995
bool open_retry = true;
@@ -996,8 +998,13 @@ nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
996998
int ret;
997999

9981000
retry:
999-
status = fh_verify(rqstp, fhp, S_IFREG,
1000-
may_flags|NFSD_MAY_OWNER_OVERRIDE);
1001+
if (rqstp) {
1002+
status = fh_verify(rqstp, fhp, S_IFREG,
1003+
may_flags|NFSD_MAY_OWNER_OVERRIDE);
1004+
} else {
1005+
status = fh_verify_local(net, cred, client, fhp, S_IFREG,
1006+
may_flags|NFSD_MAY_OWNER_OVERRIDE);
1007+
}
10011008
if (status != nfs_ok)
10021009
return status;
10031010
inode = d_inode(fhp->fh_dentry);
@@ -1143,7 +1150,8 @@ __be32
11431150
nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
11441151
unsigned int may_flags, struct nfsd_file **pnf)
11451152
{
1146-
return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, true);
1153+
return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1154+
fhp, may_flags, NULL, pnf, true);
11471155
}
11481156

11491157
/**
@@ -1167,7 +1175,55 @@ __be32
11671175
nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
11681176
unsigned int may_flags, struct nfsd_file **pnf)
11691177
{
1170-
return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, false);
1178+
return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1179+
fhp, may_flags, NULL, pnf, false);
1180+
}
1181+
1182+
/**
1183+
* nfsd_file_acquire_local - Get a struct nfsd_file with an open file for localio
1184+
* @net: The network namespace in which to perform a lookup
1185+
* @cred: the user credential with which to validate access
1186+
* @client: the auth_domain for LOCALIO lookup
1187+
* @fhp: the NFS filehandle of the file to be opened
1188+
* @may_flags: NFSD_MAY_ settings for the file
1189+
* @pnf: OUT: new or found "struct nfsd_file" object
1190+
*
1191+
* This file lookup interface provide access to a file given the
1192+
* filehandle and credential. No connection-based authorisation
1193+
* is performed and in that way it is quite different to other
1194+
* file access mediated by nfsd. It allows a kernel module such as the NFS
1195+
* client to reach across network and filesystem namespaces to access
1196+
* a file. The security implications of this should be carefully
1197+
* considered before use.
1198+
*
1199+
* The nfsd_file object returned by this API is reference-counted
1200+
* and garbage-collected. The object is retained for a few
1201+
* seconds after the final nfsd_file_put() in case the caller
1202+
* wants to re-use it.
1203+
*
1204+
* Return values:
1205+
* %nfs_ok - @pnf points to an nfsd_file with its reference
1206+
* count boosted.
1207+
*
1208+
* On error, an nfsstat value in network byte order is returned.
1209+
*/
1210+
__be32
1211+
nfsd_file_acquire_local(struct net *net, struct svc_cred *cred,
1212+
struct auth_domain *client, struct svc_fh *fhp,
1213+
unsigned int may_flags, struct nfsd_file **pnf)
1214+
{
1215+
/*
1216+
* Save creds before calling nfsd_file_do_acquire() (which calls
1217+
* nfsd_setuser). Important because caller (LOCALIO) is from
1218+
* client context.
1219+
*/
1220+
const struct cred *save_cred = get_current_cred();
1221+
__be32 beres;
1222+
1223+
beres = nfsd_file_do_acquire(NULL, net, cred, client,
1224+
fhp, may_flags, NULL, pnf, true);
1225+
revert_creds(save_cred);
1226+
return beres;
11711227
}
11721228

11731229
/**
@@ -1193,7 +1249,8 @@ nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
11931249
unsigned int may_flags, struct file *file,
11941250
struct nfsd_file **pnf)
11951251
{
1196-
return nfsd_file_do_acquire(rqstp, fhp, may_flags, file, pnf, false);
1252+
return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
1253+
fhp, may_flags, file, pnf, false);
11971254
}
11981255

11991256
/*

fs/nfsd/filecache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,8 @@ __be32 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
6666
__be32 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
6767
unsigned int may_flags, struct file *file,
6868
struct nfsd_file **nfp);
69+
__be32 nfsd_file_acquire_local(struct net *net, struct svc_cred *cred,
70+
struct auth_domain *client, struct svc_fh *fhp,
71+
unsigned int may_flags, struct nfsd_file **pnf);
6972
int nfsd_file_cache_stats_show(struct seq_file *m, void *v);
7073
#endif /* _FS_NFSD_FILECACHE_H */

fs/nfsd/nfsfh.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,29 @@ __fh_verify(struct svc_rqst *rqstp,
392392
return error;
393393
}
394394

395+
/**
396+
* fh_verify_local - filehandle lookup and access checking
397+
* @net: net namespace in which to perform the export lookup
398+
* @cred: RPC user credential
399+
* @client: RPC auth domain
400+
* @fhp: filehandle to be verified
401+
* @type: expected type of object pointed to by filehandle
402+
* @access: type of access needed to object
403+
*
404+
* This API can be used by callers who do not have an RPC
405+
* transaction context (ie are not running in an nfsd thread).
406+
*
407+
* See fh_verify() for further descriptions of @fhp, @type, and @access.
408+
*/
409+
__be32
410+
fh_verify_local(struct net *net, struct svc_cred *cred,
411+
struct auth_domain *client, struct svc_fh *fhp,
412+
umode_t type, int access)
413+
{
414+
return __fh_verify(NULL, net, cred, client, NULL,
415+
fhp, type, access);
416+
}
417+
395418
/**
396419
* fh_verify - filehandle lookup and access checking
397420
* @rqstp: pointer to current rpc request

fs/nfsd/nfsfh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ extern char * SVCFH_fmt(struct svc_fh *fhp);
217217
* Function prototypes
218218
*/
219219
__be32 fh_verify(struct svc_rqst *, struct svc_fh *, umode_t, int);
220+
__be32 fh_verify_local(struct net *, struct svc_cred *, struct auth_domain *,
221+
struct svc_fh *, umode_t, int);
220222
__be32 fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
221223
__be32 fh_update(struct svc_fh *);
222224
void fh_put(struct svc_fh *);

0 commit comments

Comments
 (0)