blob: 6220dc2a3f2c246e31817902a674648321fd6873 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/svcshare.c
3 *
4 * Management of DOS shares.
5 *
6 * Copyright (C) 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
9#include <linux/time.h>
10#include <linux/unistd.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13
14#include <linux/sunrpc/clnt.h>
15#include <linux/sunrpc/svc.h>
16#include <linux/lockd/lockd.h>
17#include <linux/lockd/share.h>
18
19static inline int
20nlm_cmp_owner(struct nlm_share *share, struct xdr_netobj *oh)
21{
22 return share->s_owner.len == oh->len
23 && !memcmp(share->s_owner.data, oh->data, oh->len);
24}
25
Al Viro52921e02006-10-19 23:28:46 -070026__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070027nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file,
28 struct nlm_args *argp)
29{
30 struct nlm_share *share;
31 struct xdr_netobj *oh = &argp->lock.oh;
32 u8 *ohdata;
33
34 for (share = file->f_shares; share; share = share->s_next) {
35 if (share->s_host == host && nlm_cmp_owner(share, oh))
36 goto update;
37 if ((argp->fsm_access & share->s_mode)
38 || (argp->fsm_mode & share->s_access ))
39 return nlm_lck_denied;
40 }
41
42 share = (struct nlm_share *) kmalloc(sizeof(*share) + oh->len,
43 GFP_KERNEL);
44 if (share == NULL)
45 return nlm_lck_denied_nolocks;
46
47 /* Copy owner handle */
48 ohdata = (u8 *) (share + 1);
49 memcpy(ohdata, oh->data, oh->len);
50
51 share->s_file = file;
52 share->s_host = host;
53 share->s_owner.data = ohdata;
54 share->s_owner.len = oh->len;
55 share->s_next = file->f_shares;
56 file->f_shares = share;
57
58update:
59 share->s_access = argp->fsm_access;
60 share->s_mode = argp->fsm_mode;
61 return nlm_granted;
62}
63
64/*
65 * Delete a share.
66 */
Al Viro52921e02006-10-19 23:28:46 -070067__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070068nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file,
69 struct nlm_args *argp)
70{
71 struct nlm_share *share, **shpp;
72 struct xdr_netobj *oh = &argp->lock.oh;
73
74 for (shpp = &file->f_shares; (share = *shpp) != 0; shpp = &share->s_next) {
75 if (share->s_host == host && nlm_cmp_owner(share, oh)) {
76 *shpp = share->s_next;
77 kfree(share);
78 return nlm_granted;
79 }
80 }
81
82 /* X/Open spec says return success even if there was no
83 * corresponding share. */
84 return nlm_granted;
85}
86
87/*
Olaf Kirchf2af7932006-10-04 02:15:59 -070088 * Traverse all shares for a given file, and delete
89 * those owned by the given (type of) host
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Olaf Kirchf2af7932006-10-04 02:15:59 -070091void nlmsvc_traverse_shares(struct nlm_host *host, struct nlm_file *file,
92 nlm_host_match_fn_t match)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct nlm_share *share, **shpp;
95
96 shpp = &file->f_shares;
97 while ((share = *shpp) != NULL) {
Olaf Kirchf2af7932006-10-04 02:15:59 -070098 if (match(share->s_host, host)) {
99 *shpp = share->s_next;
100 kfree(share);
101 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 shpp = &share->s_next;
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}