1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/lockd/svcshare.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Management of DOS shares.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Copyright (C) 1996 Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds #include <linux/time.h>
111da177e4SLinus Torvalds #include <linux/unistd.h>
121da177e4SLinus Torvalds #include <linux/string.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
161da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
171da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
181da177e4SLinus Torvalds #include <linux/lockd/share.h>
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds static inline int
nlm_cmp_owner(struct nlm_share * share,struct xdr_netobj * oh)211da177e4SLinus Torvalds nlm_cmp_owner(struct nlm_share *share, struct xdr_netobj *oh)
221da177e4SLinus Torvalds {
231da177e4SLinus Torvalds return share->s_owner.len == oh->len
241da177e4SLinus Torvalds && !memcmp(share->s_owner.data, oh->data, oh->len);
251da177e4SLinus Torvalds }
261da177e4SLinus Torvalds
2752921e02SAl Viro __be32
nlmsvc_share_file(struct nlm_host * host,struct nlm_file * file,struct nlm_args * argp)281da177e4SLinus Torvalds nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file,
291da177e4SLinus Torvalds struct nlm_args *argp)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds struct nlm_share *share;
321da177e4SLinus Torvalds struct xdr_netobj *oh = &argp->lock.oh;
331da177e4SLinus Torvalds u8 *ohdata;
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds for (share = file->f_shares; share; share = share->s_next) {
361da177e4SLinus Torvalds if (share->s_host == host && nlm_cmp_owner(share, oh))
371da177e4SLinus Torvalds goto update;
381da177e4SLinus Torvalds if ((argp->fsm_access & share->s_mode)
391da177e4SLinus Torvalds || (argp->fsm_mode & share->s_access ))
401da177e4SLinus Torvalds return nlm_lck_denied;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds
435cbded58SRobert P. J. Day share = kmalloc(sizeof(*share) + oh->len,
441da177e4SLinus Torvalds GFP_KERNEL);
451da177e4SLinus Torvalds if (share == NULL)
461da177e4SLinus Torvalds return nlm_lck_denied_nolocks;
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds /* Copy owner handle */
491da177e4SLinus Torvalds ohdata = (u8 *) (share + 1);
501da177e4SLinus Torvalds memcpy(ohdata, oh->data, oh->len);
511da177e4SLinus Torvalds
521da177e4SLinus Torvalds share->s_file = file;
531da177e4SLinus Torvalds share->s_host = host;
541da177e4SLinus Torvalds share->s_owner.data = ohdata;
551da177e4SLinus Torvalds share->s_owner.len = oh->len;
561da177e4SLinus Torvalds share->s_next = file->f_shares;
571da177e4SLinus Torvalds file->f_shares = share;
581da177e4SLinus Torvalds
591da177e4SLinus Torvalds update:
601da177e4SLinus Torvalds share->s_access = argp->fsm_access;
611da177e4SLinus Torvalds share->s_mode = argp->fsm_mode;
621da177e4SLinus Torvalds return nlm_granted;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds * Delete a share.
671da177e4SLinus Torvalds */
6852921e02SAl Viro __be32
nlmsvc_unshare_file(struct nlm_host * host,struct nlm_file * file,struct nlm_args * argp)691da177e4SLinus Torvalds nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file,
701da177e4SLinus Torvalds struct nlm_args *argp)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds struct nlm_share *share, **shpp;
731da177e4SLinus Torvalds struct xdr_netobj *oh = &argp->lock.oh;
741da177e4SLinus Torvalds
7593245d11SHarvey Harrison for (shpp = &file->f_shares; (share = *shpp) != NULL;
7693245d11SHarvey Harrison shpp = &share->s_next) {
771da177e4SLinus Torvalds if (share->s_host == host && nlm_cmp_owner(share, oh)) {
781da177e4SLinus Torvalds *shpp = share->s_next;
791da177e4SLinus Torvalds kfree(share);
801da177e4SLinus Torvalds return nlm_granted;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds
841da177e4SLinus Torvalds /* X/Open spec says return success even if there was no
851da177e4SLinus Torvalds * corresponding share. */
861da177e4SLinus Torvalds return nlm_granted;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds /*
90f2af793dSOlaf Kirch * Traverse all shares for a given file, and delete
91f2af793dSOlaf Kirch * those owned by the given (type of) host
921da177e4SLinus Torvalds */
nlmsvc_traverse_shares(struct nlm_host * host,struct nlm_file * file,nlm_host_match_fn_t match)93f2af793dSOlaf Kirch void nlmsvc_traverse_shares(struct nlm_host *host, struct nlm_file *file,
94f2af793dSOlaf Kirch nlm_host_match_fn_t match)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds struct nlm_share *share, **shpp;
971da177e4SLinus Torvalds
981da177e4SLinus Torvalds shpp = &file->f_shares;
991da177e4SLinus Torvalds while ((share = *shpp) != NULL) {
100f2af793dSOlaf Kirch if (match(share->s_host, host)) {
1011da177e4SLinus Torvalds *shpp = share->s_next;
1021da177e4SLinus Torvalds kfree(share);
1031da177e4SLinus Torvalds continue;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds shpp = &share->s_next;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds }
108