1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * This file contains all the stubs needed when communicating with lockd.
41da177e4SLinus Torvalds * This level of indirection is necessary so we can run nfsd+lockd without
51da177e4SLinus Torvalds * requiring the nfs client to be compiled in/loaded, and vice versa.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds #include <linux/file.h>
111da177e4SLinus Torvalds #include <linux/lockd/bind.h>
129a74af21SBoaz Harrosh #include "nfsd.h"
130a3adadeSJ. Bruce Fields #include "vfs.h"
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds #define NFSDDBG_FACILITY NFSDDBG_LOCKD
161da177e4SLinus Torvalds
17cc77b152SMiklos Szeredi #ifdef CONFIG_LOCKD_V4
18cc77b152SMiklos Szeredi #define nlm_stale_fh nlm4_stale_fh
19cc77b152SMiklos Szeredi #define nlm_failed nlm4_failed
20cc77b152SMiklos Szeredi #else
21cc77b152SMiklos Szeredi #define nlm_stale_fh nlm_lck_denied_nolocks
22cc77b152SMiklos Szeredi #define nlm_failed nlm_lck_denied_nolocks
23cc77b152SMiklos Szeredi #endif
241da177e4SLinus Torvalds /*
251da177e4SLinus Torvalds * Note: we hold the dentry use count while the file is open.
261da177e4SLinus Torvalds */
27e8c5c045SAl Viro static __be32
nlm_fopen(struct svc_rqst * rqstp,struct nfs_fh * f,struct file ** filp,int mode)287f024fcdSJ. Bruce Fields nlm_fopen(struct svc_rqst *rqstp, struct nfs_fh *f, struct file **filp,
297f024fcdSJ. Bruce Fields int mode)
301da177e4SLinus Torvalds {
31c7afef1fSAl Viro __be32 nfserr;
327f024fcdSJ. Bruce Fields int access;
331da177e4SLinus Torvalds struct svc_fh fh;
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds /* must initialize before using! but maxsize doesn't matter */
361da177e4SLinus Torvalds fh_init(&fh,0);
371da177e4SLinus Torvalds fh.fh_handle.fh_size = f->size;
38*d8b26071SNeilBrown memcpy(&fh.fh_handle.fh_raw, f->data, f->size);
391da177e4SLinus Torvalds fh.fh_export = NULL;
401da177e4SLinus Torvalds
417f024fcdSJ. Bruce Fields access = (mode == O_WRONLY) ? NFSD_MAY_WRITE : NFSD_MAY_READ;
427f024fcdSJ. Bruce Fields access |= NFSD_MAY_LOCK;
437f024fcdSJ. Bruce Fields nfserr = nfsd_open(rqstp, &fh, S_IFREG, access, filp);
441da177e4SLinus Torvalds fh_put(&fh);
45d343fce1SNeilBrown /* We return nlm error codes as nlm doesn't know
46d343fce1SNeilBrown * about nfsd, but nfsd does know about nlm..
471da177e4SLinus Torvalds */
481da177e4SLinus Torvalds switch (nfserr) {
491da177e4SLinus Torvalds case nfs_ok:
501da177e4SLinus Torvalds return 0;
51d343fce1SNeilBrown case nfserr_dropit:
52d343fce1SNeilBrown return nlm_drop_reply;
531da177e4SLinus Torvalds case nfserr_stale:
54cc77b152SMiklos Szeredi return nlm_stale_fh;
551da177e4SLinus Torvalds default:
56cc77b152SMiklos Szeredi return nlm_failed;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds static void
nlm_fclose(struct file * filp)611da177e4SLinus Torvalds nlm_fclose(struct file *filp)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds fput(filp);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds
662a297450SJulia Lawall static const struct nlmsvc_binding nfsd_nlm_ops = {
671da177e4SLinus Torvalds .fopen = nlm_fopen, /* open file for locking */
681da177e4SLinus Torvalds .fclose = nlm_fclose, /* close file */
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds void
nfsd_lockd_init(void)721da177e4SLinus Torvalds nfsd_lockd_init(void)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds dprintk("nfsd: initializing lockd\n");
751da177e4SLinus Torvalds nlmsvc_ops = &nfsd_nlm_ops;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds void
nfsd_lockd_shutdown(void)791da177e4SLinus Torvalds nfsd_lockd_shutdown(void)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds nlmsvc_ops = NULL;
821da177e4SLinus Torvalds }
83