NFS: Generalise the nfs_client structure
[deliverable/linux.git] / fs / nfs / client.c
1 /* client.c: NFS client sharing and management code
2 *
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16
17 #include <linux/time.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/string.h>
21 #include <linux/stat.h>
22 #include <linux/errno.h>
23 #include <linux/unistd.h>
24 #include <linux/sunrpc/clnt.h>
25 #include <linux/sunrpc/stats.h>
26 #include <linux/sunrpc/metrics.h>
27 #include <linux/nfs_fs.h>
28 #include <linux/nfs_mount.h>
29 #include <linux/nfs4_mount.h>
30 #include <linux/lockd/bind.h>
31 #include <linux/smp_lock.h>
32 #include <linux/seq_file.h>
33 #include <linux/mount.h>
34 #include <linux/nfs_idmap.h>
35 #include <linux/vfs.h>
36 #include <linux/inet.h>
37 #include <linux/nfs_xdr.h>
38
39 #include <asm/system.h>
40
41 #include "nfs4_fs.h"
42 #include "callback.h"
43 #include "delegation.h"
44 #include "iostat.h"
45 #include "internal.h"
46
47 #define NFSDBG_FACILITY NFSDBG_CLIENT
48
49 static DEFINE_SPINLOCK(nfs_client_lock);
50 static LIST_HEAD(nfs_client_list);
51 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
52
53 /*
54 * Allocate a shared client record
55 *
56 * Since these are allocated/deallocated very rarely, we don't
57 * bother putting them in a slab cache...
58 */
59 static struct nfs_client *nfs_alloc_client(const char *hostname,
60 const struct sockaddr_in *addr,
61 int nfsversion)
62 {
63 struct nfs_client *clp;
64 int error;
65
66 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
67 goto error_0;
68
69 error = rpciod_up();
70 if (error < 0) {
71 dprintk("%s: couldn't start rpciod! Error = %d\n",
72 __FUNCTION__, error);
73 __set_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
74 goto error_1;
75 }
76
77 if (nfsversion == 4) {
78 if (nfs_callback_up() < 0)
79 goto error_2;
80 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
81 }
82
83 atomic_set(&clp->cl_count, 1);
84 clp->cl_cons_state = NFS_CS_INITING;
85
86 clp->cl_nfsversion = nfsversion;
87 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
88
89 if (hostname) {
90 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
91 if (!clp->cl_hostname)
92 goto error_3;
93 }
94
95 INIT_LIST_HEAD(&clp->cl_superblocks);
96 clp->cl_rpcclient = ERR_PTR(-EINVAL);
97
98 #ifdef CONFIG_NFS_V4
99 init_rwsem(&clp->cl_sem);
100 INIT_LIST_HEAD(&clp->cl_delegations);
101 INIT_LIST_HEAD(&clp->cl_state_owners);
102 INIT_LIST_HEAD(&clp->cl_unused);
103 spin_lock_init(&clp->cl_lock);
104 INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
105 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
106 clp->cl_boot_time = CURRENT_TIME;
107 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
108 #endif
109
110 return clp;
111
112 error_3:
113 nfs_callback_down();
114 __clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
115 error_2:
116 rpciod_down();
117 __clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
118 error_1:
119 kfree(clp);
120 error_0:
121 return NULL;
122 }
123
124 /*
125 * Destroy a shared client record
126 */
127 static void nfs_free_client(struct nfs_client *clp)
128 {
129 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
130
131 #ifdef CONFIG_NFS_V4
132 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) {
133 while (!list_empty(&clp->cl_unused)) {
134 struct nfs4_state_owner *sp;
135
136 sp = list_entry(clp->cl_unused.next,
137 struct nfs4_state_owner,
138 so_list);
139 list_del(&sp->so_list);
140 kfree(sp);
141 }
142 BUG_ON(!list_empty(&clp->cl_state_owners));
143 nfs_idmap_delete(clp);
144 }
145 #endif
146
147 /* -EIO all pending I/O */
148 if (!IS_ERR(clp->cl_rpcclient))
149 rpc_shutdown_client(clp->cl_rpcclient);
150
151 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
152 nfs_callback_down();
153
154 if (__test_and_clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state))
155 rpciod_down();
156
157 kfree(clp->cl_hostname);
158 kfree(clp);
159
160 dprintk("<-- nfs_free_client()\n");
161 }
162
163 /*
164 * Release a reference to a shared client record
165 */
166 void nfs_put_client(struct nfs_client *clp)
167 {
168 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
169
170 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
171 list_del(&clp->cl_share_link);
172 spin_unlock(&nfs_client_lock);
173
174 BUG_ON(!list_empty(&clp->cl_superblocks));
175
176 nfs_free_client(clp);
177 }
178 }
179
180 /*
181 * Find a client by address
182 * - caller must hold nfs_client_lock
183 */
184 static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
185 {
186 struct nfs_client *clp;
187
188 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
189 /* Different NFS versions cannot share the same nfs_client */
190 if (clp->cl_nfsversion != nfsversion)
191 continue;
192
193 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
194 sizeof(clp->cl_addr.sin_addr)) != 0)
195 continue;
196
197 if (clp->cl_addr.sin_port == addr->sin_port)
198 goto found;
199 }
200
201 return NULL;
202
203 found:
204 atomic_inc(&clp->cl_count);
205 return clp;
206 }
207
208 /*
209 * Find a client by IP address and protocol version
210 * - returns NULL if no such client
211 */
212 struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
213 {
214 struct nfs_client *clp;
215
216 spin_lock(&nfs_client_lock);
217 clp = __nfs_find_client(addr, nfsversion);
218 spin_unlock(&nfs_client_lock);
219
220 BUG_ON(clp->cl_cons_state == 0);
221
222 return clp;
223 }
224
225 /*
226 * Look up a client by IP address and protocol version
227 * - creates a new record if one doesn't yet exist
228 */
229 struct nfs_client *nfs_get_client(const char *hostname,
230 const struct sockaddr_in *addr,
231 int nfsversion)
232 {
233 struct nfs_client *clp, *new = NULL;
234 int error;
235
236 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
237 hostname ?: "", NIPQUAD(addr->sin_addr),
238 addr->sin_port, nfsversion);
239
240 /* see if the client already exists */
241 do {
242 spin_lock(&nfs_client_lock);
243
244 clp = __nfs_find_client(addr, nfsversion);
245 if (clp)
246 goto found_client;
247 if (new)
248 goto install_client;
249
250 spin_unlock(&nfs_client_lock);
251
252 new = nfs_alloc_client(hostname, addr, nfsversion);
253 } while (new);
254
255 return ERR_PTR(-ENOMEM);
256
257 /* install a new client and return with it unready */
258 install_client:
259 clp = new;
260 list_add(&clp->cl_share_link, &nfs_client_list);
261 spin_unlock(&nfs_client_lock);
262 dprintk("--> nfs_get_client() = %p [new]\n", clp);
263 return clp;
264
265 /* found an existing client
266 * - make sure it's ready before returning
267 */
268 found_client:
269 spin_unlock(&nfs_client_lock);
270
271 if (new)
272 nfs_free_client(new);
273
274 if (clp->cl_cons_state == NFS_CS_INITING) {
275 DECLARE_WAITQUEUE(myself, current);
276
277 add_wait_queue(&nfs_client_active_wq, &myself);
278
279 for (;;) {
280 set_current_state(TASK_INTERRUPTIBLE);
281 if (signal_pending(current) ||
282 clp->cl_cons_state > NFS_CS_READY)
283 break;
284 schedule();
285 }
286
287 remove_wait_queue(&nfs_client_active_wq, &myself);
288
289 if (signal_pending(current)) {
290 nfs_put_client(clp);
291 return ERR_PTR(-ERESTARTSYS);
292 }
293 }
294
295 if (clp->cl_cons_state < NFS_CS_READY) {
296 error = clp->cl_cons_state;
297 nfs_put_client(clp);
298 return ERR_PTR(error);
299 }
300
301 dprintk("--> nfs_get_client() = %p [share]\n", clp);
302 return clp;
303 }
304
305 /*
306 * Mark a server as ready or failed
307 */
308 void nfs_mark_client_ready(struct nfs_client *clp, int state)
309 {
310 clp->cl_cons_state = state;
311 wake_up_all(&nfs_client_active_wq);
312 }
This page took 0.037525 seconds and 5 git commands to generate.