Blame view

fs/nfs/pnfs_nfs.c 29.6 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
2
3
4
5
6
7
8
9
10
11
12
  /*
   * Common NFS I/O  operations for the pnfs file based
   * layout drivers.
   *
   * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
   *
   * Tom Haynes <loghyr@primarydata.com>
   */
  
  #include <linux/nfs_fs.h>
  #include <linux/nfs_page.h>
6b7f3cf96   Peng Tao   nfs41: pull decod...
13
  #include <linux/sunrpc/addr.h>
5f01d9539   Peng Tao   nfs41: create NFS...
14
  #include <linux/module.h>
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
15

7405f9e19   Peng Tao   nfs41: pull nfs4_...
16
  #include "nfs4session.h"
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
17
18
  #include "internal.h"
  #include "pnfs.h"
875ae0694   Peng Tao   nfs41: pull data ...
19
  #define NFSDBG_FACILITY		NFSDBG_PNFS
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
20
21
22
  void pnfs_generic_rw_release(void *data)
  {
  	struct nfs_pgio_header *hdr = data;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
23

f54bcf2ec   Tom Haynes   pnfs: Prepare for...
24
25
26
27
28
29
30
31
  	nfs_put_client(hdr->ds_clp);
  	hdr->mds_ops->rpc_release(data);
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
  
  /* Fake up some data that will cause nfs_commit_release to retry the writes. */
  void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
  {
221203ce6   Trond Myklebust   NFS/pnfs: Fix pnf...
32
  	struct nfs_writeverf *verf = data->res.verf;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
33
34
  
  	data->task.tk_status = 0;
221203ce6   Trond Myklebust   NFS/pnfs: Fix pnf...
35
36
  	memset(&verf->verifier, 0, sizeof(verf->verifier));
  	verf->committed = NFS_UNSTABLE;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
  
  void pnfs_generic_write_commit_done(struct rpc_task *task, void *data)
  {
  	struct nfs_commit_data *wdata = data;
  
  	/* Note this may cause RPC to be resent */
  	wdata->mds_ops->rpc_call_done(task, data);
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
  
  void pnfs_generic_commit_release(void *calldata)
  {
  	struct nfs_commit_data *data = calldata;
  
  	data->completion_ops->completion(data);
  	pnfs_put_lseg(data->lseg);
  	nfs_put_client(data->ds_clp);
  	nfs_commitdata_release(data);
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_commit_release);
c84bea594   Trond Myklebust   NFS/pNFS: Simplif...
59
60
61
62
63
64
65
66
67
68
  static struct pnfs_layout_segment *
  pnfs_free_bucket_lseg(struct pnfs_commit_bucket *bucket)
  {
  	if (list_empty(&bucket->committing) && list_empty(&bucket->written)) {
  		struct pnfs_layout_segment *freeme = bucket->lseg;
  		bucket->lseg = NULL;
  		return freeme;
  	}
  	return NULL;
  }
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
69
70
  /* The generic layer is about to remove the req from the commit list.
   * If this will make the bucket empty, it will need to put the lseg reference.
d0fbb1d8a   Trond Myklebust   NFSv4: Fix lockin...
71
   * Note this must be called holding nfsi->commit_mutex
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
72
73
74
75
76
   */
  void
  pnfs_generic_clear_request_commit(struct nfs_page *req,
  				  struct nfs_commit_info *cinfo)
  {
067aefcdf   Trond Myklebust   NFS/pNFS: Don't c...
77
  	struct pnfs_commit_bucket *bucket = NULL;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
78
79
80
81
  
  	if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
  		goto out;
  	cinfo->ds->nwritten--;
067aefcdf   Trond Myklebust   NFS/pNFS: Don't c...
82
  	if (list_is_singular(&req->wb_list))
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
83
  		bucket = list_first_entry(&req->wb_list,
067aefcdf   Trond Myklebust   NFS/pNFS: Don't c...
84
  					  struct pnfs_commit_bucket, written);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
85
86
  out:
  	nfs_request_remove_commit_list(req, cinfo);
067aefcdf   Trond Myklebust   NFS/pNFS: Don't c...
87
88
  	if (bucket)
  		pnfs_put_lseg(pnfs_free_bucket_lseg(bucket));
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
89
90
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
d7242c464   Trond Myklebust   pNFS: Add a helpe...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  struct pnfs_commit_array *
  pnfs_alloc_commit_array(size_t n, gfp_t gfp_flags)
  {
  	struct pnfs_commit_array *p;
  	struct pnfs_commit_bucket *b;
  
  	p = kmalloc(struct_size(p, buckets, n), gfp_flags);
  	if (!p)
  		return NULL;
  	p->nbuckets = n;
  	INIT_LIST_HEAD(&p->cinfo_list);
  	INIT_LIST_HEAD(&p->lseg_list);
  	p->lseg = NULL;
  	for (b = &p->buckets[0]; n != 0; b++, n--) {
  		INIT_LIST_HEAD(&b->written);
  		INIT_LIST_HEAD(&b->committing);
c84bea594   Trond Myklebust   NFS/pNFS: Simplif...
107
  		b->lseg = NULL;
d7242c464   Trond Myklebust   pNFS: Add a helpe...
108
109
110
111
112
113
114
115
116
117
118
119
  		b->direct_verf.committed = NFS_INVALID_STABLE_HOW;
  	}
  	return p;
  }
  EXPORT_SYMBOL_GPL(pnfs_alloc_commit_array);
  
  void
  pnfs_free_commit_array(struct pnfs_commit_array *p)
  {
  	kfree_rcu(p, rcu);
  }
  EXPORT_SYMBOL_GPL(pnfs_free_commit_array);
ba827c9ab   Trond Myklebust   pNFS: Enable per-...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  static struct pnfs_commit_array *
  pnfs_find_commit_array_by_lseg(struct pnfs_ds_commit_info *fl_cinfo,
  		struct pnfs_layout_segment *lseg)
  {
  	struct pnfs_commit_array *array;
  
  	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
  		if (array->lseg == lseg)
  			return array;
  	}
  	return NULL;
  }
  
  struct pnfs_commit_array *
  pnfs_add_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
  		struct pnfs_commit_array *new,
  		struct pnfs_layout_segment *lseg)
  {
  	struct pnfs_commit_array *array;
  
  	array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
  	if (array)
  		return array;
  	new->lseg = lseg;
  	refcount_set(&new->refcount, 1);
  	list_add_rcu(&new->cinfo_list, &fl_cinfo->commits);
  	list_add(&new->lseg_list, &lseg->pls_commits);
  	return new;
  }
  EXPORT_SYMBOL_GPL(pnfs_add_commit_array);
ba827c9ab   Trond Myklebust   pNFS: Enable per-...
150
151
152
153
154
155
156
157
158
159
  static struct pnfs_commit_array *
  pnfs_lookup_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
  		struct pnfs_layout_segment *lseg)
  {
  	struct pnfs_commit_array *array;
  
  	rcu_read_lock();
  	array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
  	if (!array) {
  		rcu_read_unlock();
9c455a8c1   Trond Myklebust   NFS/pNFS: Clean u...
160
  		fl_cinfo->ops->setup_ds_info(fl_cinfo, lseg);
ba827c9ab   Trond Myklebust   pNFS: Enable per-...
161
162
163
164
165
166
  		rcu_read_lock();
  		array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
  	}
  	rcu_read_unlock();
  	return array;
  }
a9901899b   Trond Myklebust   pNFS: Add infrast...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  static void
  pnfs_release_commit_array_locked(struct pnfs_commit_array *array)
  {
  	list_del_rcu(&array->cinfo_list);
  	list_del(&array->lseg_list);
  	pnfs_free_commit_array(array);
  }
  
  static void
  pnfs_put_commit_array_locked(struct pnfs_commit_array *array)
  {
  	if (refcount_dec_and_test(&array->refcount))
  		pnfs_release_commit_array_locked(array);
  }
  
  static void
  pnfs_put_commit_array(struct pnfs_commit_array *array, struct inode *inode)
  {
  	if (refcount_dec_and_lock(&array->refcount, &inode->i_lock)) {
  		pnfs_release_commit_array_locked(array);
  		spin_unlock(&inode->i_lock);
  	}
  }
  
  static struct pnfs_commit_array *
  pnfs_get_commit_array(struct pnfs_commit_array *array)
  {
  	if (refcount_inc_not_zero(&array->refcount))
  		return array;
  	return NULL;
  }
  
  static void
  pnfs_remove_and_free_commit_array(struct pnfs_commit_array *array)
  {
  	array->lseg = NULL;
  	list_del_init(&array->lseg_list);
  	pnfs_put_commit_array_locked(array);
  }
  
  void
  pnfs_generic_ds_cinfo_release_lseg(struct pnfs_ds_commit_info *fl_cinfo,
  		struct pnfs_layout_segment *lseg)
  {
  	struct pnfs_commit_array *array, *tmp;
  
  	list_for_each_entry_safe(array, tmp, &lseg->pls_commits, lseg_list)
  		pnfs_remove_and_free_commit_array(array);
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_release_lseg);
  
  void
  pnfs_generic_ds_cinfo_destroy(struct pnfs_ds_commit_info *fl_cinfo)
  {
  	struct pnfs_commit_array *array, *tmp;
  
  	list_for_each_entry_safe(array, tmp, &fl_cinfo->commits, cinfo_list)
  		pnfs_remove_and_free_commit_array(array);
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_destroy);
a8e3765e5   Trond Myklebust   NFSv4/pNFS: Scan ...
227
228
229
230
  /*
   * Locks the nfs_page requests for commit and moves them to
   * @bucket->committing.
   */
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
231
  static int
a8e3765e5   Trond Myklebust   NFSv4/pNFS: Scan ...
232
233
234
  pnfs_bucket_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
  				struct nfs_commit_info *cinfo,
  				int max)
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
235
236
237
238
  {
  	struct list_head *src = &bucket->written;
  	struct list_head *dst = &bucket->committing;
  	int ret;
e824f99ad   Trond Myklebust   NFSv4: Use a mute...
239
  	lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
5d2a9d9da   Trond Myklebust   NFS: Remove pnfs_...
240
  	ret = nfs_scan_commit_list(src, dst, cinfo, max);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
241
242
243
  	if (ret) {
  		cinfo->ds->nwritten -= ret;
  		cinfo->ds->ncommitting += ret;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
244
245
246
  	}
  	return ret;
  }
a8e3765e5   Trond Myklebust   NFSv4/pNFS: Scan ...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  static int pnfs_bucket_scan_array(struct nfs_commit_info *cinfo,
  				  struct pnfs_commit_bucket *buckets,
  				  unsigned int nbuckets,
  				  int max)
  {
  	unsigned int i;
  	int rv = 0, cnt;
  
  	for (i = 0; i < nbuckets && max != 0; i++) {
  		cnt = pnfs_bucket_scan_ds_commit_list(&buckets[i], cinfo, max);
  		rv += cnt;
  		max -= cnt;
  	}
  	return rv;
  }
085d1e33a   Tom Haynes   pnfs: Do not grab...
262
263
  /* Move reqs from written to committing lists, returning count
   * of number moved.
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
264
   */
a8e3765e5   Trond Myklebust   NFSv4/pNFS: Scan ...
265
  int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo, int max)
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
266
  {
a8e3765e5   Trond Myklebust   NFSv4/pNFS: Scan ...
267
268
269
  	struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
  	struct pnfs_commit_array *array;
  	int rv = 0, cnt;
a9901899b   Trond Myklebust   pNFS: Add infrast...
270
271
272
273
274
  	rcu_read_lock();
  	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
  		if (!array->lseg || !pnfs_get_commit_array(array))
  			continue;
  		rcu_read_unlock();
a8e3765e5   Trond Myklebust   NFSv4/pNFS: Scan ...
275
276
  		cnt = pnfs_bucket_scan_array(cinfo, array->buckets,
  				array->nbuckets, max);
a9901899b   Trond Myklebust   pNFS: Add infrast...
277
278
  		rcu_read_lock();
  		pnfs_put_commit_array(array, cinfo->inode);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
279
  		rv += cnt;
a8e3765e5   Trond Myklebust   NFSv4/pNFS: Scan ...
280
281
282
  		max -= cnt;
  		if (!max)
  			break;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
283
  	}
a9901899b   Trond Myklebust   pNFS: Add infrast...
284
  	rcu_read_unlock();
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
285
286
287
  	return rv;
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
fce9ed030   Trond Myklebust   pNFS: Support per...
288
289
290
291
292
  static unsigned int
  pnfs_bucket_recover_commit_reqs(struct list_head *dst,
  			        struct pnfs_commit_bucket *buckets,
  				unsigned int nbuckets,
  				struct nfs_commit_info *cinfo)
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
293
294
295
  {
  	struct pnfs_commit_bucket *b;
  	struct pnfs_layout_segment *freeme;
fce9ed030   Trond Myklebust   pNFS: Support per...
296
297
  	unsigned int nwritten, ret = 0;
  	unsigned int i;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
298
299
  
  restart:
fce9ed030   Trond Myklebust   pNFS: Support per...
300
  	for (i = 0, b = buckets; i < nbuckets; i++, b++) {
5d2a9d9da   Trond Myklebust   NFS: Remove pnfs_...
301
  		nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0);
e39928f94   Trond Myklebust   NFS: Fix a COMMIT...
302
303
  		if (!nwritten)
  			continue;
fce9ed030   Trond Myklebust   pNFS: Support per...
304
  		ret += nwritten;
c84bea594   Trond Myklebust   NFS/pNFS: Simplif...
305
306
  		freeme = pnfs_free_bucket_lseg(b);
  		if (freeme) {
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
307
308
309
310
  			pnfs_put_lseg(freeme);
  			goto restart;
  		}
  	}
fce9ed030   Trond Myklebust   pNFS: Support per...
311
312
313
314
315
316
317
318
319
320
321
322
  	return ret;
  }
  
  /* Pull everything off the committing lists and dump into @dst.  */
  void pnfs_generic_recover_commit_reqs(struct list_head *dst,
  				      struct nfs_commit_info *cinfo)
  {
  	struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
  	struct pnfs_commit_array *array;
  	unsigned int nwritten;
  
  	lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
a9901899b   Trond Myklebust   pNFS: Add infrast...
323
324
325
326
327
  	rcu_read_lock();
  	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
  		if (!array->lseg || !pnfs_get_commit_array(array))
  			continue;
  		rcu_read_unlock();
fce9ed030   Trond Myklebust   pNFS: Support per...
328
329
330
331
  		nwritten = pnfs_bucket_recover_commit_reqs(dst,
  							   array->buckets,
  							   array->nbuckets,
  							   cinfo);
a9901899b   Trond Myklebust   pNFS: Add infrast...
332
333
  		rcu_read_lock();
  		pnfs_put_commit_array(array, cinfo->inode);
fce9ed030   Trond Myklebust   pNFS: Support per...
334
335
  		fl_cinfo->nwritten -= nwritten;
  	}
a9901899b   Trond Myklebust   pNFS: Add infrast...
336
  	rcu_read_unlock();
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
337
338
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
fb6b53ba4   Trond Myklebust   NFS/pNFS: Add a h...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  static struct nfs_page *
  pnfs_bucket_search_commit_reqs(struct pnfs_commit_bucket *buckets,
  		unsigned int nbuckets, struct page *page)
  {
  	struct nfs_page *req;
  	struct pnfs_commit_bucket *b;
  	unsigned int i;
  
  	/* Linearly search the commit lists for each bucket until a matching
  	 * request is found */
  	for (i = 0, b = buckets; i < nbuckets; i++, b++) {
  		list_for_each_entry(req, &b->written, wb_list) {
  			if (req->wb_page == page)
  				return req->wb_head;
  		}
  		list_for_each_entry(req, &b->committing, wb_list) {
  			if (req->wb_page == page)
  				return req->wb_head;
  		}
  	}
  	return NULL;
  }
  
  /* pnfs_generic_search_commit_reqs - Search lists in @cinfo for the head reqest
   *				   for @page
   * @cinfo - commit info for current inode
   * @page - page to search for matching head request
   *
   * Returns a the head request if one is found, otherwise returns NULL.
   */
  struct nfs_page *
  pnfs_generic_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page)
  {
  	struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
  	struct pnfs_commit_array *array;
  	struct nfs_page *req;
fb6b53ba4   Trond Myklebust   NFS/pNFS: Add a h...
375
376
377
378
379
380
381
382
383
  	list_for_each_entry(array, &fl_cinfo->commits, cinfo_list) {
  		req = pnfs_bucket_search_commit_reqs(array->buckets,
  				array->nbuckets, page);
  		if (req)
  			return req;
  	}
  	return NULL;
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_search_commit_reqs);
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
384
385
386
387
  static struct pnfs_layout_segment *
  pnfs_bucket_get_committing(struct list_head *head,
  			   struct pnfs_commit_bucket *bucket,
  			   struct nfs_commit_info *cinfo)
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
388
  {
b666f394d   Trond Myklebust   NFS/pNFS: Don't l...
389
  	struct pnfs_layout_segment *lseg;
411818864   Trond Myklebust   NFS: Fix another ...
390
  	struct list_head *pos;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
391
392
393
394
  
  	list_for_each(pos, &bucket->committing)
  		cinfo->ds->ncommitting--;
  	list_splice_init(&bucket->committing, head);
b666f394d   Trond Myklebust   NFS/pNFS: Don't l...
395
396
397
398
  	lseg = pnfs_free_bucket_lseg(bucket);
  	if (!lseg)
  		lseg = pnfs_get_lseg(bucket->lseg);
  	return lseg;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  }
  
  static struct nfs_commit_data *
  pnfs_bucket_fetch_commitdata(struct pnfs_commit_bucket *bucket,
  			     struct nfs_commit_info *cinfo)
  {
  	struct nfs_commit_data *data = nfs_commitdata_alloc(false);
  
  	if (!data)
  		return NULL;
  	data->lseg = pnfs_bucket_get_committing(&data->pages, bucket, cinfo);
  	return data;
  }
  
  static void pnfs_generic_retry_commit(struct pnfs_commit_bucket *buckets,
  				      unsigned int nbuckets,
  				      struct nfs_commit_info *cinfo,
  				      unsigned int idx)
  {
  	struct pnfs_commit_bucket *bucket;
  	struct pnfs_layout_segment *freeme;
27571297a   Trond Myklebust   pNFS: Tighten up ...
420
  	LIST_HEAD(pages);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
421

19573c939   Trond Myklebust   NFS/pNFS: Refacto...
422
  	for (bucket = buckets; idx < nbuckets; bucket++, idx++) {
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
423
424
  		if (list_empty(&bucket->committing))
  			continue;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
425
426
  		mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  		freeme = pnfs_bucket_get_committing(&pages, bucket, cinfo);
d0fbb1d8a   Trond Myklebust   NFSv4: Fix lockin...
427
  		mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
428
  		nfs_retry_commit(&pages, freeme, cinfo, idx);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
429
430
431
432
433
  		pnfs_put_lseg(freeme);
  	}
  }
  
  static unsigned int
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
434
435
436
437
  pnfs_bucket_alloc_ds_commits(struct list_head *list,
  			     struct pnfs_commit_bucket *buckets,
  			     unsigned int nbuckets,
  			     struct nfs_commit_info *cinfo)
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
438
  {
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
439
440
  	struct pnfs_commit_bucket *bucket;
  	struct nfs_commit_data *data;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
441
  	unsigned int i;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
442
  	unsigned int nreq = 0;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
443
  	for (i = 0, bucket = buckets; i < nbuckets; i++, bucket++) {
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
444
445
  		if (list_empty(&bucket->committing))
  			continue;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
446
447
448
449
450
451
452
453
454
455
456
  		mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  		if (!list_empty(&bucket->committing)) {
  			data = pnfs_bucket_fetch_commitdata(bucket, cinfo);
  			if (!data)
  				goto out_error;
  			data->ds_commit_index = i;
  			list_add_tail(&data->list, list);
  			atomic_inc(&cinfo->mds->rpcs_out);
  			nreq++;
  		}
  		mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
457
  	}
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
458
  	return nreq;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
459
  out_error:
d0fbb1d8a   Trond Myklebust   NFSv4: Fix lockin...
460
  	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
461
462
463
  	/* Clean up on error */
  	pnfs_generic_retry_commit(buckets, nbuckets, cinfo, i);
  	return nreq;
ade8febde   Weston Andros Adamson   nfs: avoid race t...
464
  }
0cb1f6df8   Trond Myklebust   pNFS: Support per...
465
466
467
468
469
470
471
  static unsigned int
  pnfs_alloc_ds_commits_list(struct list_head *list,
  			   struct pnfs_ds_commit_info *fl_cinfo,
  			   struct nfs_commit_info *cinfo)
  {
  	struct pnfs_commit_array *array;
  	unsigned int ret = 0;
a9901899b   Trond Myklebust   pNFS: Add infrast...
472
473
474
475
476
  	rcu_read_lock();
  	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
  		if (!array->lseg || !pnfs_get_commit_array(array))
  			continue;
  		rcu_read_unlock();
0cb1f6df8   Trond Myklebust   pNFS: Support per...
477
478
  		ret += pnfs_bucket_alloc_ds_commits(list, array->buckets,
  				array->nbuckets, cinfo);
a9901899b   Trond Myklebust   pNFS: Add infrast...
479
480
481
  		rcu_read_lock();
  		pnfs_put_commit_array(array, cinfo->inode);
  	}
27d231c0c   Trond Myklebust   pNFS: Fix RCU loc...
482
  	rcu_read_unlock();
0cb1f6df8   Trond Myklebust   pNFS: Support per...
483
484
  	return ret;
  }
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
485
486
487
488
489
490
491
  /* This follows nfs_commit_list pretty closely */
  int
  pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  			     int how, struct nfs_commit_info *cinfo,
  			     int (*initiate_commit)(struct nfs_commit_data *data,
  						    int how))
  {
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
492
  	struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
493
494
495
496
497
  	struct nfs_commit_data *data, *tmp;
  	LIST_HEAD(list);
  	unsigned int nreq = 0;
  
  	if (!list_empty(mds_pages)) {
518662e0f   NeilBrown   NFS: fix usage of...
498
499
  		data = nfs_commitdata_alloc(true);
  		data->ds_commit_index = -1;
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
500
501
502
  		list_splice_init(mds_pages, &data->pages);
  		list_add_tail(&data->list, &list);
  		atomic_inc(&cinfo->mds->rpcs_out);
518662e0f   NeilBrown   NFS: fix usage of...
503
  		nreq++;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
504
  	}
0cb1f6df8   Trond Myklebust   pNFS: Support per...
505
  	nreq += pnfs_alloc_ds_commits_list(&list, fl_cinfo, cinfo);
af7cf0579   Trond Myklebust   NFS: Allow multip...
506
  	if (nreq == 0)
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
507
  		goto out;
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
508

19573c939   Trond Myklebust   NFS/pNFS: Refacto...
509
510
  	list_for_each_entry_safe(data, tmp, &list, list) {
  		list_del(&data->list);
27571297a   Trond Myklebust   pNFS: Tighten up ...
511
  		if (data->ds_commit_index < 0) {
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
512
  			nfs_init_commit(data, NULL, NULL, cinfo);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
513
  			nfs_initiate_commit(NFS_CLIENT(inode), data,
c36aae9ad   Peng Tao   nfs: allow differ...
514
  					    NFS_PROTO(data->inode),
4fa7ef69e   Trond Myklebust   NFS/pnfs: Don't u...
515
516
  					    data->mds_ops, how,
  					    RPC_TASK_CRED_NOREF);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
517
  		} else {
19573c939   Trond Myklebust   NFS/pNFS: Refacto...
518
  			nfs_init_commit(data, NULL, data->lseg, cinfo);
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
519
520
521
522
  			initiate_commit(data, how);
  		}
  	}
  out:
f54bcf2ec   Tom Haynes   pnfs: Prepare for...
523
524
525
  	return PNFS_ATTEMPTED;
  }
  EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
875ae0694   Peng Tao   nfs41: pull data ...
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
  
  /*
   * Data server cache
   *
   * Data servers can be mapped to different device ids.
   * nfs4_pnfs_ds reference counting
   *   - set to 1 on allocation
   *   - incremented when a device id maps a data server already in the cache.
   *   - decremented when deviceid is removed from the cache.
   */
  static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
  static LIST_HEAD(nfs4_data_server_cache);
  
  /* Debug routines */
  static void
  print_ds(struct nfs4_pnfs_ds *ds)
  {
  	if (ds == NULL) {
  		printk(KERN_WARNING "%s NULL device
  ", __func__);
  		return;
  	}
  	printk(KERN_WARNING "        ds %s
  "
  		"        ref count %d
  "
  		"        client %p
  "
  		"        cl_exchange_flags %x
  ",
  		ds->ds_remotestr,
a2a5dea7b   Elena Reshetova   fs, nfs: convert ...
557
  		refcount_read(&ds->ds_count), ds->ds_clp,
875ae0694   Peng Tao   nfs41: pull data ...
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
  		ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
  }
  
  static bool
  same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
  {
  	struct sockaddr_in *a, *b;
  	struct sockaddr_in6 *a6, *b6;
  
  	if (addr1->sa_family != addr2->sa_family)
  		return false;
  
  	switch (addr1->sa_family) {
  	case AF_INET:
  		a = (struct sockaddr_in *)addr1;
  		b = (struct sockaddr_in *)addr2;
  
  		if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
  		    a->sin_port == b->sin_port)
  			return true;
  		break;
  
  	case AF_INET6:
  		a6 = (struct sockaddr_in6 *)addr1;
  		b6 = (struct sockaddr_in6 *)addr2;
  
  		/* LINKLOCAL addresses must have matching scope_id */
  		if (ipv6_addr_src_scope(&a6->sin6_addr) ==
  		    IPV6_ADDR_SCOPE_LINKLOCAL &&
  		    a6->sin6_scope_id != b6->sin6_scope_id)
  			return false;
  
  		if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
  		    a6->sin6_port == b6->sin6_port)
  			return true;
  		break;
  
  	default:
  		dprintk("%s: unhandled address family: %u
  ",
  			__func__, addr1->sa_family);
  		return false;
  	}
  
  	return false;
  }
6f536936b   Trond Myklebust   NFSv4.1/pNFS: Fix...
604
605
606
607
  /*
   * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
   * declare a match.
   */
875ae0694   Peng Tao   nfs41: pull data ...
608
609
610
611
612
  static bool
  _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
  			       const struct list_head *dsaddrs2)
  {
  	struct nfs4_pnfs_ds_addr *da1, *da2;
6f536936b   Trond Myklebust   NFSv4.1/pNFS: Fix...
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  	struct sockaddr *sa1, *sa2;
  	bool match = false;
  
  	list_for_each_entry(da1, dsaddrs1, da_node) {
  		sa1 = (struct sockaddr *)&da1->da_addr;
  		match = false;
  		list_for_each_entry(da2, dsaddrs2, da_node) {
  			sa2 = (struct sockaddr *)&da2->da_addr;
  			match = same_sockaddr(sa1, sa2);
  			if (match)
  				break;
  		}
  		if (!match)
  			break;
875ae0694   Peng Tao   nfs41: pull data ...
627
  	}
6f536936b   Trond Myklebust   NFSv4.1/pNFS: Fix...
628
  	return match;
875ae0694   Peng Tao   nfs41: pull data ...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
  }
  
  /*
   * Lookup DS by addresses.  nfs4_ds_cache_lock is held
   */
  static struct nfs4_pnfs_ds *
  _data_server_lookup_locked(const struct list_head *dsaddrs)
  {
  	struct nfs4_pnfs_ds *ds;
  
  	list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
  		if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
  			return ds;
  	return NULL;
  }
  
  static void destroy_ds(struct nfs4_pnfs_ds *ds)
  {
  	struct nfs4_pnfs_ds_addr *da;
  
  	dprintk("--> %s
  ", __func__);
  	ifdebug(FACILITY)
  		print_ds(ds);
  
  	nfs_put_client(ds->ds_clp);
  
  	while (!list_empty(&ds->ds_addrs)) {
  		da = list_first_entry(&ds->ds_addrs,
  				      struct nfs4_pnfs_ds_addr,
  				      da_node);
  		list_del_init(&da->da_node);
  		kfree(da->da_remotestr);
  		kfree(da);
  	}
  
  	kfree(ds->ds_remotestr);
  	kfree(ds);
  }
  
  void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
  {
a2a5dea7b   Elena Reshetova   fs, nfs: convert ...
671
  	if (refcount_dec_and_lock(&ds->ds_count,
875ae0694   Peng Tao   nfs41: pull data ...
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
  				&nfs4_ds_cache_lock)) {
  		list_del_init(&ds->ds_node);
  		spin_unlock(&nfs4_ds_cache_lock);
  		destroy_ds(ds);
  	}
  }
  EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
  
  /*
   * Create a string with a human readable address and port to avoid
   * complicated setup around many dprinks.
   */
  static char *
  nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
  {
  	struct nfs4_pnfs_ds_addr *da;
  	char *remotestr;
  	size_t len;
  	char *p;
  
  	len = 3;        /* '{', '}' and eol */
  	list_for_each_entry(da, dsaddrs, da_node) {
  		len += strlen(da->da_remotestr) + 1;    /* string plus comma */
  	}
  
  	remotestr = kzalloc(len, gfp_flags);
  	if (!remotestr)
  		return NULL;
  
  	p = remotestr;
  	*(p++) = '{';
  	len--;
  	list_for_each_entry(da, dsaddrs, da_node) {
  		size_t ll = strlen(da->da_remotestr);
  
  		if (ll > len)
  			goto out_err;
  
  		memcpy(p, da->da_remotestr, ll);
  		p += ll;
  		len -= ll;
  
  		if (len < 1)
  			goto out_err;
  		(*p++) = ',';
  		len--;
  	}
  	if (len < 2)
  		goto out_err;
  	*(p++) = '}';
  	*p = '\0';
  	return remotestr;
  out_err:
  	kfree(remotestr);
  	return NULL;
  }
  
  /*
   * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
   * uncached and return cached struct nfs4_pnfs_ds.
   */
  struct nfs4_pnfs_ds *
  nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
  {
  	struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
  	char *remotestr;
  
  	if (list_empty(dsaddrs)) {
  		dprintk("%s: no addresses defined
  ", __func__);
  		goto out;
  	}
  
  	ds = kzalloc(sizeof(*ds), gfp_flags);
  	if (!ds)
  		goto out;
  
  	/* this is only used for debugging, so it's ok if its NULL */
  	remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
  
  	spin_lock(&nfs4_ds_cache_lock);
  	tmp_ds = _data_server_lookup_locked(dsaddrs);
  	if (tmp_ds == NULL) {
  		INIT_LIST_HEAD(&ds->ds_addrs);
  		list_splice_init(dsaddrs, &ds->ds_addrs);
  		ds->ds_remotestr = remotestr;
a2a5dea7b   Elena Reshetova   fs, nfs: convert ...
758
  		refcount_set(&ds->ds_count, 1);
875ae0694   Peng Tao   nfs41: pull data ...
759
760
761
762
763
764
765
766
767
  		INIT_LIST_HEAD(&ds->ds_node);
  		ds->ds_clp = NULL;
  		list_add(&ds->ds_node, &nfs4_data_server_cache);
  		dprintk("%s add new data server %s
  ", __func__,
  			ds->ds_remotestr);
  	} else {
  		kfree(remotestr);
  		kfree(ds);
a2a5dea7b   Elena Reshetova   fs, nfs: convert ...
768
  		refcount_inc(&tmp_ds->ds_count);
875ae0694   Peng Tao   nfs41: pull data ...
769
770
771
  		dprintk("%s data server %s found, inc'ed ds_count to %d
  ",
  			__func__, tmp_ds->ds_remotestr,
a2a5dea7b   Elena Reshetova   fs, nfs: convert ...
772
  			refcount_read(&tmp_ds->ds_count));
875ae0694   Peng Tao   nfs41: pull data ...
773
774
775
776
777
778
779
  		ds = tmp_ds;
  	}
  	spin_unlock(&nfs4_ds_cache_lock);
  out:
  	return ds;
  }
  EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
6b7f3cf96   Peng Tao   nfs41: pull decod...
780

7405f9e19   Peng Tao   nfs41: pull nfs4_...
781
782
783
784
785
786
787
788
789
790
791
792
793
794
  static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
  {
  	might_sleep();
  	wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
  			TASK_KILLABLE);
  }
  
  static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
  {
  	smp_mb__before_atomic();
  	clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
  	smp_mb__after_atomic();
  	wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
  }
5f01d9539   Peng Tao   nfs41: create NFS...
795
  static struct nfs_client *(*get_v3_ds_connect)(
b224f7cb6   Tigran Mkrtchyan   nfs4: flexfiles: ...
796
  			struct nfs_server *mds_srv,
5f01d9539   Peng Tao   nfs41: create NFS...
797
798
799
800
  			const struct sockaddr *ds_addr,
  			int ds_addrlen,
  			int ds_proto,
  			unsigned int ds_timeo,
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
801
  			unsigned int ds_retrans);
5f01d9539   Peng Tao   nfs41: create NFS...
802
803
804
805
806
807
808
809
810
811
  
  static bool load_v3_ds_connect(void)
  {
  	if (!get_v3_ds_connect) {
  		get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
  		WARN_ON_ONCE(!get_v3_ds_connect);
  	}
  
  	return(get_v3_ds_connect != NULL);
  }
df137bc12   Arnd Bergmann   nfs: do not expor...
812
  void nfs4_pnfs_v3_ds_connect_unload(void)
5f01d9539   Peng Tao   nfs41: create NFS...
813
814
815
816
817
818
  {
  	if (get_v3_ds_connect) {
  		symbol_put(nfs3_set_ds_client);
  		get_v3_ds_connect = NULL;
  	}
  }
5f01d9539   Peng Tao   nfs41: create NFS...
819
820
821
822
  
  static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
  				 struct nfs4_pnfs_ds *ds,
  				 unsigned int timeo,
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
823
  				 unsigned int retrans)
5f01d9539   Peng Tao   nfs41: create NFS...
824
825
826
827
  {
  	struct nfs_client *clp = ERR_PTR(-EIO);
  	struct nfs4_pnfs_ds_addr *da;
  	int status = 0;
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
828
829
  	dprintk("--> %s DS %s
  ", __func__, ds->ds_remotestr);
5f01d9539   Peng Tao   nfs41: create NFS...
830
831
832
833
834
835
836
837
  
  	if (!load_v3_ds_connect())
  		goto out;
  
  	list_for_each_entry(da, &ds->ds_addrs, da_node) {
  		dprintk("%s: DS %s: trying address %s
  ",
  			__func__, ds->ds_remotestr, da->da_remotestr);
fc821d592   Trond Myklebust   pnfs/NFSv4.1: Add...
838
839
840
841
842
843
844
845
846
847
848
  		if (!IS_ERR(clp)) {
  			struct xprt_create xprt_args = {
  				.ident = XPRT_TRANSPORT_TCP,
  				.net = clp->cl_net,
  				.dstaddr = (struct sockaddr *)&da->da_addr,
  				.addrlen = da->da_addrlen,
  				.servername = clp->cl_hostname,
  			};
  			/* Add this address as an alias */
  			rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
  					rpc_clnt_test_and_add_xprt, NULL);
bf2bf9b80   Trond Myklebust   pNFS/flexfiles: T...
849
850
851
852
853
854
855
856
857
858
  			continue;
  		}
  		clp = get_v3_ds_connect(mds_srv,
  				(struct sockaddr *)&da->da_addr,
  				da->da_addrlen, IPPROTO_TCP,
  				timeo, retrans);
  		if (IS_ERR(clp))
  			continue;
  		clp->cl_rpcclient->cl_softerr = 0;
  		clp->cl_rpcclient->cl_softrtry = 0;
5f01d9539   Peng Tao   nfs41: create NFS...
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
  	}
  
  	if (IS_ERR(clp)) {
  		status = PTR_ERR(clp);
  		goto out;
  	}
  
  	smp_wmb();
  	ds->ds_clp = clp;
  	dprintk("%s [new] addr: %s
  ", __func__, ds->ds_remotestr);
  out:
  	return status;
  }
  
  static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
7405f9e19   Peng Tao   nfs41: pull nfs4_...
875
876
  				 struct nfs4_pnfs_ds *ds,
  				 unsigned int timeo,
064172f34   Peng Tao   nfs41: allow LD t...
877
  				 unsigned int retrans,
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
878
  				 u32 minor_version)
7405f9e19   Peng Tao   nfs41: pull nfs4_...
879
880
881
882
  {
  	struct nfs_client *clp = ERR_PTR(-EIO);
  	struct nfs4_pnfs_ds_addr *da;
  	int status = 0;
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
883
884
  	dprintk("--> %s DS %s
  ", __func__, ds->ds_remotestr);
7405f9e19   Peng Tao   nfs41: pull nfs4_...
885
886
887
888
889
  
  	list_for_each_entry(da, &ds->ds_addrs, da_node) {
  		dprintk("%s: DS %s: trying address %s
  ",
  			__func__, ds->ds_remotestr, da->da_remotestr);
04fa2c6bb   Andy Adamson   NFS pnfs data ser...
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
  		if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
  			struct xprt_create xprt_args = {
  				.ident = XPRT_TRANSPORT_TCP,
  				.net = clp->cl_net,
  				.dstaddr = (struct sockaddr *)&da->da_addr,
  				.addrlen = da->da_addrlen,
  				.servername = clp->cl_hostname,
  			};
  			struct nfs4_add_xprt_data xprtdata = {
  				.clp = clp,
  				.cred = nfs4_get_clid_cred(clp),
  			};
  			struct rpc_add_xprt_test rpcdata = {
  				.add_xprt_test = clp->cl_mvops->session_trunk,
  				.data = &xprtdata,
  			};
  
  			/**
  			* Test this address for session trunking and
  			* add as an alias
  			*/
  			rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
  					  rpc_clnt_setup_test_and_add_xprt,
  					  &rpcdata);
  			if (xprtdata.cred)
a52458b48   NeilBrown   NFS/NFSD/SUNRPC: ...
915
  				put_cred(xprtdata.cred);
04fa2c6bb   Andy Adamson   NFS pnfs data ser...
916
917
918
919
  		} else {
  			clp = nfs4_set_ds_client(mds_srv,
  						(struct sockaddr *)&da->da_addr,
  						da->da_addrlen, IPPROTO_TCP,
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
920
  						timeo, retrans, minor_version);
04fa2c6bb   Andy Adamson   NFS pnfs data ser...
921
922
923
924
925
926
927
928
929
930
931
932
  			if (IS_ERR(clp))
  				continue;
  
  			status = nfs4_init_ds_session(clp,
  					mds_srv->nfs_client->cl_lease_time);
  			if (status) {
  				nfs_put_client(clp);
  				clp = ERR_PTR(-EIO);
  				continue;
  			}
  
  		}
7405f9e19   Peng Tao   nfs41: pull nfs4_...
933
934
935
936
937
938
  	}
  
  	if (IS_ERR(clp)) {
  		status = PTR_ERR(clp);
  		goto out;
  	}
7405f9e19   Peng Tao   nfs41: pull nfs4_...
939
940
941
942
943
944
  	smp_wmb();
  	ds->ds_clp = clp;
  	dprintk("%s [new] addr: %s
  ", __func__, ds->ds_remotestr);
  out:
  	return status;
7405f9e19   Peng Tao   nfs41: pull nfs4_...
945
946
947
948
949
  }
  
  /*
   * Create an rpc connection to the nfs4_pnfs_ds data server.
   * Currently only supports IPv4 and IPv6 addresses.
a33e4b036   Weston Andros Adamson   pNFS: return stat...
950
   * If connection fails, make devid unavailable and return a -errno.
7405f9e19   Peng Tao   nfs41: pull nfs4_...
951
   */
a33e4b036   Weston Andros Adamson   pNFS: return stat...
952
  int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
7405f9e19   Peng Tao   nfs41: pull nfs4_...
953
  			  struct nfs4_deviceid_node *devid, unsigned int timeo,
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
954
  			  unsigned int retrans, u32 version, u32 minor_version)
7405f9e19   Peng Tao   nfs41: pull nfs4_...
955
  {
da066f3f0   Weston Andros Adamson   pNFS/flexfiles: n...
956
  	int err;
7405f9e19   Peng Tao   nfs41: pull nfs4_...
957

da066f3f0   Weston Andros Adamson   pNFS/flexfiles: n...
958
959
960
  again:
  	err = 0;
  	if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
5f01d9539   Peng Tao   nfs41: create NFS...
961
962
  		if (version == 3) {
  			err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
963
  						       retrans);
5f01d9539   Peng Tao   nfs41: create NFS...
964
965
  		} else if (version == 4) {
  			err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
7d38de3ff   Anna Schumaker   NFS: Remove unuse...
966
  						       retrans, minor_version);
5f01d9539   Peng Tao   nfs41: create NFS...
967
968
969
970
971
972
  		} else {
  			dprintk("%s: unsupported DS version %d
  ", __func__,
  				version);
  			err = -EPROTONOSUPPORT;
  		}
7405f9e19   Peng Tao   nfs41: pull nfs4_...
973
974
975
  		nfs4_clear_ds_conn_bit(ds);
  	} else {
  		nfs4_wait_ds_connect(ds);
da066f3f0   Weston Andros Adamson   pNFS/flexfiles: n...
976
977
978
979
  
  		/* what was waited on didn't connect AND didn't mark unavail */
  		if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid))
  			goto again;
7405f9e19   Peng Tao   nfs41: pull nfs4_...
980
  	}
a33e4b036   Weston Andros Adamson   pNFS: return stat...
981
982
983
984
985
  
  	/*
  	 * At this point the ds->ds_clp should be ready, but it might have
  	 * hit an error.
  	 */
da066f3f0   Weston Andros Adamson   pNFS/flexfiles: n...
986
987
988
989
990
991
992
  	if (!err) {
  		if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
  			WARN_ON_ONCE(ds->ds_clp ||
  				!nfs4_test_deviceid_unavailable(devid));
  			return -EINVAL;
  		}
  		err = nfs_client_init_status(ds->ds_clp);
a33e4b036   Weston Andros Adamson   pNFS: return stat...
993
  	}
da066f3f0   Weston Andros Adamson   pNFS/flexfiles: n...
994
  	return err;
7405f9e19   Peng Tao   nfs41: pull nfs4_...
995
996
  }
  EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
6b7f3cf96   Peng Tao   nfs41: pull decod...
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
  /*
   * Currently only supports ipv4, ipv6 and one multi-path address.
   */
  struct nfs4_pnfs_ds_addr *
  nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
  {
  	struct nfs4_pnfs_ds_addr *da = NULL;
  	char *buf, *portstr;
  	__be16 port;
  	int nlen, rlen;
  	int tmp[2];
  	__be32 *p;
  	char *netid, *match_netid;
  	size_t len, match_netid_len;
  	char *startsep = "";
  	char *endsep = "";
  
  
  	/* r_netid */
  	p = xdr_inline_decode(xdr, 4);
  	if (unlikely(!p))
  		goto out_err;
  	nlen = be32_to_cpup(p++);
  
  	p = xdr_inline_decode(xdr, nlen);
  	if (unlikely(!p))
  		goto out_err;
  
  	netid = kmalloc(nlen+1, gfp_flags);
  	if (unlikely(!netid))
  		goto out_err;
  
  	netid[nlen] = '\0';
  	memcpy(netid, p, nlen);
  
  	/* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
  	p = xdr_inline_decode(xdr, 4);
  	if (unlikely(!p))
  		goto out_free_netid;
  	rlen = be32_to_cpup(p);
  
  	p = xdr_inline_decode(xdr, rlen);
  	if (unlikely(!p))
  		goto out_free_netid;
  
  	/* port is ".ABC.DEF", 8 chars max */
  	if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
  		dprintk("%s: Invalid address, length %d
  ", __func__,
  			rlen);
  		goto out_free_netid;
  	}
  	buf = kmalloc(rlen + 1, gfp_flags);
  	if (!buf) {
  		dprintk("%s: Not enough memory
  ", __func__);
  		goto out_free_netid;
  	}
  	buf[rlen] = '\0';
  	memcpy(buf, p, rlen);
  
  	/* replace port '.' with '-' */
  	portstr = strrchr(buf, '.');
  	if (!portstr) {
  		dprintk("%s: Failed finding expected dot in port
  ",
  			__func__);
  		goto out_free_buf;
  	}
  	*portstr = '-';
  
  	/* find '.' between address and port */
  	portstr = strrchr(buf, '.');
  	if (!portstr) {
  		dprintk("%s: Failed finding expected dot between address and "
  			"port
  ", __func__);
  		goto out_free_buf;
  	}
  	*portstr = '\0';
  
  	da = kzalloc(sizeof(*da), gfp_flags);
  	if (unlikely(!da))
  		goto out_free_buf;
  
  	INIT_LIST_HEAD(&da->da_node);
  
  	if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
  		      sizeof(da->da_addr))) {
  		dprintk("%s: error parsing address %s
  ", __func__, buf);
  		goto out_free_da;
  	}
  
  	portstr++;
  	sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
  	port = htons((tmp[0] << 8) | (tmp[1]));
  
  	switch (da->da_addr.ss_family) {
  	case AF_INET:
  		((struct sockaddr_in *)&da->da_addr)->sin_port = port;
  		da->da_addrlen = sizeof(struct sockaddr_in);
  		match_netid = "tcp";
  		match_netid_len = 3;
  		break;
  
  	case AF_INET6:
  		((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
  		da->da_addrlen = sizeof(struct sockaddr_in6);
  		match_netid = "tcp6";
  		match_netid_len = 4;
  		startsep = "[";
  		endsep = "]";
  		break;
  
  	default:
  		dprintk("%s: unsupported address family: %u
  ",
  			__func__, da->da_addr.ss_family);
  		goto out_free_da;
  	}
  
  	if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
  		dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"
  ",
  			__func__, netid, match_netid);
  		goto out_free_da;
  	}
  
  	/* save human readable address */
  	len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
  	da->da_remotestr = kzalloc(len, gfp_flags);
  
  	/* NULL is ok, only used for dprintk */
  	if (da->da_remotestr)
  		snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
  			 buf, endsep, ntohs(port));
  
  	dprintk("%s: Parsed DS addr %s
  ", __func__, da->da_remotestr);
  	kfree(buf);
  	kfree(netid);
  	return da;
  
  out_free_da:
  	kfree(da);
  out_free_buf:
  	dprintk("%s: Error parsing DS addr: %s
  ", __func__, buf);
  	kfree(buf);
  out_free_netid:
  	kfree(netid);
  out_err:
  	return NULL;
  }
  EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
338d00cfe   Tom Haynes   pnfs: Refactor th...
1153
1154
1155
1156
1157
1158
1159
1160
  
  void
  pnfs_layout_mark_request_commit(struct nfs_page *req,
  				struct pnfs_layout_segment *lseg,
  				struct nfs_commit_info *cinfo,
  				u32 ds_commit_idx)
  {
  	struct list_head *list;
ba827c9ab   Trond Myklebust   pNFS: Enable per-...
1161
  	struct pnfs_commit_array *array;
e18c18ebd   Trond Myklebust   NFS/pNFS: Fix pnf...
1162
  	struct pnfs_commit_bucket *bucket;
338d00cfe   Tom Haynes   pnfs: Refactor th...
1163

e824f99ad   Trond Myklebust   NFSv4: Use a mute...
1164
  	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
ba827c9ab   Trond Myklebust   pNFS: Enable per-...
1165
  	array = pnfs_lookup_commit_array(cinfo->ds, lseg);
e18c18ebd   Trond Myklebust   NFS/pNFS: Fix pnf...
1166
  	if (!array || !pnfs_is_valid_lseg(lseg))
ba827c9ab   Trond Myklebust   pNFS: Enable per-...
1167
  		goto out_resched;
e18c18ebd   Trond Myklebust   NFS/pNFS: Fix pnf...
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
  	bucket = &array->buckets[ds_commit_idx];
  	list = &bucket->written;
  	/* Non-empty buckets hold a reference on the lseg.  That ref
  	 * is normally transferred to the COMMIT call and released
  	 * there.  It could also be released if the last req is pulled
  	 * off due to a rewrite, in which case it will be done in
  	 * pnfs_common_clear_request_commit
  	 */
  	if (!bucket->lseg)
  		bucket->lseg = pnfs_get_lseg(lseg);
338d00cfe   Tom Haynes   pnfs: Refactor th...
1178
1179
  	set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  	cinfo->ds->nwritten++;
338d00cfe   Tom Haynes   pnfs: Refactor th...
1180

86d80f973   Trond Myklebust   NFSv4.1/pnfs: Fix...
1181
  	nfs_request_add_commit_list_locked(req, list, cinfo);
e824f99ad   Trond Myklebust   NFSv4: Use a mute...
1182
  	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
86d80f973   Trond Myklebust   NFSv4.1/pnfs: Fix...
1183
  	nfs_mark_page_unstable(req->wb_page, cinfo);
ba827c9ab   Trond Myklebust   pNFS: Enable per-...
1184
1185
1186
1187
  	return;
  out_resched:
  	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  	cinfo->completion_ops->resched_write(cinfo, req);
338d00cfe   Tom Haynes   pnfs: Refactor th...
1188
1189
  }
  EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
5bb89b470   Trond Myklebust   NFSv4.1/pnfs: Sep...
1190
1191
1192
1193
  
  int
  pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
  {
2e18d4d82   Trond Myklebust   pNFS: Files and f...
1194
1195
1196
1197
1198
1199
1200
  	int ret;
  
  	if (!pnfs_layoutcommit_outstanding(inode))
  		return 0;
  	ret = nfs_commit_inode(inode, FLUSH_SYNC);
  	if (ret < 0)
  		return ret;
5bb89b470   Trond Myklebust   NFSv4.1/pnfs: Sep...
1201
1202
1203
1204
1205
  	if (datasync)
  		return 0;
  	return pnfs_layoutcommit_inode(inode, true);
  }
  EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);