Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Common NFS I/O operations for the pnfs file based |
| 4 | * layout drivers. |
| 5 | * |
| 6 | * Copyright (c) 2014, Primary Data, Inc. All rights reserved. |
| 7 | * |
| 8 | * Tom Haynes <loghyr@primarydata.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/nfs_fs.h> |
| 12 | #include <linux/nfs_page.h> |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 13 | #include <linux/sunrpc/addr.h> |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 14 | #include <linux/module.h> |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 15 | |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 16 | #include "nfs4session.h" |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | #include "pnfs.h" |
| 19 | |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 20 | #define NFSDBG_FACILITY NFSDBG_PNFS |
| 21 | |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 22 | void pnfs_generic_rw_release(void *data) |
| 23 | { |
| 24 | struct nfs_pgio_header *hdr = data; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 25 | |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 26 | nfs_put_client(hdr->ds_clp); |
| 27 | hdr->mds_ops->rpc_release(data); |
| 28 | } |
| 29 | EXPORT_SYMBOL_GPL(pnfs_generic_rw_release); |
| 30 | |
| 31 | /* Fake up some data that will cause nfs_commit_release to retry the writes. */ |
| 32 | void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data) |
| 33 | { |
Trond Myklebust | 221203c | 2020-01-06 15:25:04 -0500 | [diff] [blame] | 34 | struct nfs_writeverf *verf = data->res.verf; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 35 | |
| 36 | data->task.tk_status = 0; |
Trond Myklebust | 221203c | 2020-01-06 15:25:04 -0500 | [diff] [blame] | 37 | memset(&verf->verifier, 0, sizeof(verf->verifier)); |
| 38 | verf->committed = NFS_UNSTABLE; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 39 | } |
| 40 | EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes); |
| 41 | |
| 42 | void pnfs_generic_write_commit_done(struct rpc_task *task, void *data) |
| 43 | { |
| 44 | struct nfs_commit_data *wdata = data; |
| 45 | |
| 46 | /* Note this may cause RPC to be resent */ |
| 47 | wdata->mds_ops->rpc_call_done(task, data); |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done); |
| 50 | |
| 51 | void pnfs_generic_commit_release(void *calldata) |
| 52 | { |
| 53 | struct nfs_commit_data *data = calldata; |
| 54 | |
| 55 | data->completion_ops->completion(data); |
| 56 | pnfs_put_lseg(data->lseg); |
| 57 | nfs_put_client(data->ds_clp); |
| 58 | nfs_commitdata_release(data); |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(pnfs_generic_commit_release); |
| 61 | |
Trond Myklebust | c84bea5 | 2020-03-22 14:47:38 -0400 | [diff] [blame] | 62 | static struct pnfs_layout_segment * |
| 63 | pnfs_free_bucket_lseg(struct pnfs_commit_bucket *bucket) |
| 64 | { |
| 65 | if (list_empty(&bucket->committing) && list_empty(&bucket->written)) { |
| 66 | struct pnfs_layout_segment *freeme = bucket->lseg; |
| 67 | bucket->lseg = NULL; |
| 68 | return freeme; |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 73 | /* The generic layer is about to remove the req from the commit list. |
| 74 | * If this will make the bucket empty, it will need to put the lseg reference. |
Trond Myklebust | d0fbb1d | 2018-08-14 17:25:37 -0400 | [diff] [blame] | 75 | * Note this must be called holding nfsi->commit_mutex |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 76 | */ |
| 77 | void |
| 78 | pnfs_generic_clear_request_commit(struct nfs_page *req, |
| 79 | struct nfs_commit_info *cinfo) |
| 80 | { |
Trond Myklebust | 1757655 | 2021-01-06 11:28:30 -0500 | [diff] [blame] | 81 | struct pnfs_commit_bucket *bucket = NULL; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 82 | |
| 83 | if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags)) |
| 84 | goto out; |
| 85 | cinfo->ds->nwritten--; |
Trond Myklebust | 1757655 | 2021-01-06 11:28:30 -0500 | [diff] [blame] | 86 | if (list_is_singular(&req->wb_list)) |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 87 | bucket = list_first_entry(&req->wb_list, |
Trond Myklebust | 1757655 | 2021-01-06 11:28:30 -0500 | [diff] [blame] | 88 | struct pnfs_commit_bucket, written); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 89 | out: |
| 90 | nfs_request_remove_commit_list(req, cinfo); |
Trond Myklebust | 1757655 | 2021-01-06 11:28:30 -0500 | [diff] [blame] | 91 | if (bucket) |
| 92 | pnfs_put_lseg(pnfs_free_bucket_lseg(bucket)); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 93 | } |
| 94 | EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit); |
| 95 | |
Trond Myklebust | d7242c4 | 2020-03-18 17:22:47 -0400 | [diff] [blame] | 96 | struct pnfs_commit_array * |
| 97 | pnfs_alloc_commit_array(size_t n, gfp_t gfp_flags) |
| 98 | { |
| 99 | struct pnfs_commit_array *p; |
| 100 | struct pnfs_commit_bucket *b; |
| 101 | |
| 102 | p = kmalloc(struct_size(p, buckets, n), gfp_flags); |
| 103 | if (!p) |
| 104 | return NULL; |
| 105 | p->nbuckets = n; |
| 106 | INIT_LIST_HEAD(&p->cinfo_list); |
| 107 | INIT_LIST_HEAD(&p->lseg_list); |
| 108 | p->lseg = NULL; |
| 109 | for (b = &p->buckets[0]; n != 0; b++, n--) { |
| 110 | INIT_LIST_HEAD(&b->written); |
| 111 | INIT_LIST_HEAD(&b->committing); |
Trond Myklebust | c84bea5 | 2020-03-22 14:47:38 -0400 | [diff] [blame] | 112 | b->lseg = NULL; |
Trond Myklebust | d7242c4 | 2020-03-18 17:22:47 -0400 | [diff] [blame] | 113 | b->direct_verf.committed = NFS_INVALID_STABLE_HOW; |
| 114 | } |
| 115 | return p; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(pnfs_alloc_commit_array); |
| 118 | |
| 119 | void |
| 120 | pnfs_free_commit_array(struct pnfs_commit_array *p) |
| 121 | { |
| 122 | kfree_rcu(p, rcu); |
| 123 | } |
| 124 | EXPORT_SYMBOL_GPL(pnfs_free_commit_array); |
| 125 | |
Trond Myklebust | ba827c9 | 2020-03-20 18:34:33 -0400 | [diff] [blame] | 126 | static struct pnfs_commit_array * |
| 127 | pnfs_find_commit_array_by_lseg(struct pnfs_ds_commit_info *fl_cinfo, |
| 128 | struct pnfs_layout_segment *lseg) |
| 129 | { |
| 130 | struct pnfs_commit_array *array; |
| 131 | |
| 132 | list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { |
| 133 | if (array->lseg == lseg) |
| 134 | return array; |
| 135 | } |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | struct pnfs_commit_array * |
| 140 | pnfs_add_commit_array(struct pnfs_ds_commit_info *fl_cinfo, |
| 141 | struct pnfs_commit_array *new, |
| 142 | struct pnfs_layout_segment *lseg) |
| 143 | { |
| 144 | struct pnfs_commit_array *array; |
| 145 | |
| 146 | array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg); |
| 147 | if (array) |
| 148 | return array; |
| 149 | new->lseg = lseg; |
| 150 | refcount_set(&new->refcount, 1); |
| 151 | list_add_rcu(&new->cinfo_list, &fl_cinfo->commits); |
| 152 | list_add(&new->lseg_list, &lseg->pls_commits); |
| 153 | return new; |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(pnfs_add_commit_array); |
| 156 | |
Trond Myklebust | ba827c9 | 2020-03-20 18:34:33 -0400 | [diff] [blame] | 157 | static struct pnfs_commit_array * |
| 158 | pnfs_lookup_commit_array(struct pnfs_ds_commit_info *fl_cinfo, |
| 159 | struct pnfs_layout_segment *lseg) |
| 160 | { |
| 161 | struct pnfs_commit_array *array; |
| 162 | |
| 163 | rcu_read_lock(); |
| 164 | array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg); |
| 165 | if (!array) { |
| 166 | rcu_read_unlock(); |
Trond Myklebust | 9c455a8 | 2020-03-21 11:13:05 -0400 | [diff] [blame] | 167 | fl_cinfo->ops->setup_ds_info(fl_cinfo, lseg); |
Trond Myklebust | ba827c9 | 2020-03-20 18:34:33 -0400 | [diff] [blame] | 168 | rcu_read_lock(); |
| 169 | array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg); |
| 170 | } |
| 171 | rcu_read_unlock(); |
| 172 | return array; |
| 173 | } |
| 174 | |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 175 | static void |
| 176 | pnfs_release_commit_array_locked(struct pnfs_commit_array *array) |
| 177 | { |
| 178 | list_del_rcu(&array->cinfo_list); |
| 179 | list_del(&array->lseg_list); |
| 180 | pnfs_free_commit_array(array); |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | pnfs_put_commit_array_locked(struct pnfs_commit_array *array) |
| 185 | { |
| 186 | if (refcount_dec_and_test(&array->refcount)) |
| 187 | pnfs_release_commit_array_locked(array); |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | pnfs_put_commit_array(struct pnfs_commit_array *array, struct inode *inode) |
| 192 | { |
| 193 | if (refcount_dec_and_lock(&array->refcount, &inode->i_lock)) { |
| 194 | pnfs_release_commit_array_locked(array); |
| 195 | spin_unlock(&inode->i_lock); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static struct pnfs_commit_array * |
| 200 | pnfs_get_commit_array(struct pnfs_commit_array *array) |
| 201 | { |
| 202 | if (refcount_inc_not_zero(&array->refcount)) |
| 203 | return array; |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | pnfs_remove_and_free_commit_array(struct pnfs_commit_array *array) |
| 209 | { |
| 210 | array->lseg = NULL; |
| 211 | list_del_init(&array->lseg_list); |
| 212 | pnfs_put_commit_array_locked(array); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | pnfs_generic_ds_cinfo_release_lseg(struct pnfs_ds_commit_info *fl_cinfo, |
| 217 | struct pnfs_layout_segment *lseg) |
| 218 | { |
| 219 | struct pnfs_commit_array *array, *tmp; |
| 220 | |
| 221 | list_for_each_entry_safe(array, tmp, &lseg->pls_commits, lseg_list) |
| 222 | pnfs_remove_and_free_commit_array(array); |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_release_lseg); |
| 225 | |
| 226 | void |
| 227 | pnfs_generic_ds_cinfo_destroy(struct pnfs_ds_commit_info *fl_cinfo) |
| 228 | { |
| 229 | struct pnfs_commit_array *array, *tmp; |
| 230 | |
| 231 | list_for_each_entry_safe(array, tmp, &fl_cinfo->commits, cinfo_list) |
| 232 | pnfs_remove_and_free_commit_array(array); |
| 233 | } |
| 234 | EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_destroy); |
| 235 | |
Trond Myklebust | a8e3765 | 2020-03-19 13:41:08 -0400 | [diff] [blame] | 236 | /* |
| 237 | * Locks the nfs_page requests for commit and moves them to |
| 238 | * @bucket->committing. |
| 239 | */ |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 240 | static int |
Trond Myklebust | a8e3765 | 2020-03-19 13:41:08 -0400 | [diff] [blame] | 241 | pnfs_bucket_scan_ds_commit_list(struct pnfs_commit_bucket *bucket, |
| 242 | struct nfs_commit_info *cinfo, |
| 243 | int max) |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 244 | { |
| 245 | struct list_head *src = &bucket->written; |
| 246 | struct list_head *dst = &bucket->committing; |
| 247 | int ret; |
| 248 | |
Trond Myklebust | e824f99 | 2017-08-01 11:53:49 -0400 | [diff] [blame] | 249 | lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex); |
Trond Myklebust | 5d2a9d9 | 2017-09-09 12:40:38 -0400 | [diff] [blame] | 250 | ret = nfs_scan_commit_list(src, dst, cinfo, max); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 251 | if (ret) { |
| 252 | cinfo->ds->nwritten -= ret; |
| 253 | cinfo->ds->ncommitting += ret; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 254 | } |
| 255 | return ret; |
| 256 | } |
| 257 | |
Trond Myklebust | a8e3765 | 2020-03-19 13:41:08 -0400 | [diff] [blame] | 258 | static int pnfs_bucket_scan_array(struct nfs_commit_info *cinfo, |
| 259 | struct pnfs_commit_bucket *buckets, |
| 260 | unsigned int nbuckets, |
| 261 | int max) |
| 262 | { |
| 263 | unsigned int i; |
| 264 | int rv = 0, cnt; |
| 265 | |
| 266 | for (i = 0; i < nbuckets && max != 0; i++) { |
| 267 | cnt = pnfs_bucket_scan_ds_commit_list(&buckets[i], cinfo, max); |
| 268 | rv += cnt; |
| 269 | max -= cnt; |
| 270 | } |
| 271 | return rv; |
| 272 | } |
| 273 | |
Tom Haynes | 085d1e3 | 2014-12-11 13:04:55 -0500 | [diff] [blame] | 274 | /* Move reqs from written to committing lists, returning count |
| 275 | * of number moved. |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 276 | */ |
Trond Myklebust | a8e3765 | 2020-03-19 13:41:08 -0400 | [diff] [blame] | 277 | int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo, int max) |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 278 | { |
Trond Myklebust | a8e3765 | 2020-03-19 13:41:08 -0400 | [diff] [blame] | 279 | struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; |
| 280 | struct pnfs_commit_array *array; |
| 281 | int rv = 0, cnt; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 282 | |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 283 | rcu_read_lock(); |
| 284 | list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { |
| 285 | if (!array->lseg || !pnfs_get_commit_array(array)) |
| 286 | continue; |
| 287 | rcu_read_unlock(); |
Trond Myklebust | a8e3765 | 2020-03-19 13:41:08 -0400 | [diff] [blame] | 288 | cnt = pnfs_bucket_scan_array(cinfo, array->buckets, |
| 289 | array->nbuckets, max); |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 290 | rcu_read_lock(); |
| 291 | pnfs_put_commit_array(array, cinfo->inode); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 292 | rv += cnt; |
Trond Myklebust | a8e3765 | 2020-03-19 13:41:08 -0400 | [diff] [blame] | 293 | max -= cnt; |
| 294 | if (!max) |
| 295 | break; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 296 | } |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 297 | rcu_read_unlock(); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 298 | return rv; |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists); |
| 301 | |
Trond Myklebust | fce9ed0 | 2020-03-19 15:48:42 -0400 | [diff] [blame] | 302 | static unsigned int |
| 303 | pnfs_bucket_recover_commit_reqs(struct list_head *dst, |
| 304 | struct pnfs_commit_bucket *buckets, |
| 305 | unsigned int nbuckets, |
| 306 | struct nfs_commit_info *cinfo) |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 307 | { |
| 308 | struct pnfs_commit_bucket *b; |
| 309 | struct pnfs_layout_segment *freeme; |
Trond Myklebust | fce9ed0 | 2020-03-19 15:48:42 -0400 | [diff] [blame] | 310 | unsigned int nwritten, ret = 0; |
| 311 | unsigned int i; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 312 | |
| 313 | restart: |
Trond Myklebust | fce9ed0 | 2020-03-19 15:48:42 -0400 | [diff] [blame] | 314 | for (i = 0, b = buckets; i < nbuckets; i++, b++) { |
Trond Myklebust | 5d2a9d9 | 2017-09-09 12:40:38 -0400 | [diff] [blame] | 315 | nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0); |
Trond Myklebust | e39928f | 2017-07-12 19:10:56 -0400 | [diff] [blame] | 316 | if (!nwritten) |
| 317 | continue; |
Trond Myklebust | fce9ed0 | 2020-03-19 15:48:42 -0400 | [diff] [blame] | 318 | ret += nwritten; |
Trond Myklebust | c84bea5 | 2020-03-22 14:47:38 -0400 | [diff] [blame] | 319 | freeme = pnfs_free_bucket_lseg(b); |
| 320 | if (freeme) { |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 321 | pnfs_put_lseg(freeme); |
| 322 | goto restart; |
| 323 | } |
| 324 | } |
Trond Myklebust | fce9ed0 | 2020-03-19 15:48:42 -0400 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | /* Pull everything off the committing lists and dump into @dst. */ |
| 329 | void pnfs_generic_recover_commit_reqs(struct list_head *dst, |
| 330 | struct nfs_commit_info *cinfo) |
| 331 | { |
| 332 | struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; |
| 333 | struct pnfs_commit_array *array; |
| 334 | unsigned int nwritten; |
| 335 | |
| 336 | lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex); |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 337 | rcu_read_lock(); |
| 338 | list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { |
| 339 | if (!array->lseg || !pnfs_get_commit_array(array)) |
| 340 | continue; |
| 341 | rcu_read_unlock(); |
Trond Myklebust | fce9ed0 | 2020-03-19 15:48:42 -0400 | [diff] [blame] | 342 | nwritten = pnfs_bucket_recover_commit_reqs(dst, |
| 343 | array->buckets, |
| 344 | array->nbuckets, |
| 345 | cinfo); |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 346 | rcu_read_lock(); |
| 347 | pnfs_put_commit_array(array, cinfo->inode); |
Trond Myklebust | fce9ed0 | 2020-03-19 15:48:42 -0400 | [diff] [blame] | 348 | fl_cinfo->nwritten -= nwritten; |
| 349 | } |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 350 | rcu_read_unlock(); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 351 | } |
| 352 | EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs); |
| 353 | |
Trond Myklebust | fb6b53b | 2020-03-20 19:24:19 -0400 | [diff] [blame] | 354 | static struct nfs_page * |
| 355 | pnfs_bucket_search_commit_reqs(struct pnfs_commit_bucket *buckets, |
| 356 | unsigned int nbuckets, struct page *page) |
| 357 | { |
| 358 | struct nfs_page *req; |
| 359 | struct pnfs_commit_bucket *b; |
| 360 | unsigned int i; |
| 361 | |
| 362 | /* Linearly search the commit lists for each bucket until a matching |
| 363 | * request is found */ |
| 364 | for (i = 0, b = buckets; i < nbuckets; i++, b++) { |
| 365 | list_for_each_entry(req, &b->written, wb_list) { |
| 366 | if (req->wb_page == page) |
| 367 | return req->wb_head; |
| 368 | } |
| 369 | list_for_each_entry(req, &b->committing, wb_list) { |
| 370 | if (req->wb_page == page) |
| 371 | return req->wb_head; |
| 372 | } |
| 373 | } |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | /* pnfs_generic_search_commit_reqs - Search lists in @cinfo for the head reqest |
| 378 | * for @page |
| 379 | * @cinfo - commit info for current inode |
| 380 | * @page - page to search for matching head request |
| 381 | * |
| 382 | * Returns a the head request if one is found, otherwise returns NULL. |
| 383 | */ |
| 384 | struct nfs_page * |
| 385 | pnfs_generic_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page) |
| 386 | { |
| 387 | struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; |
| 388 | struct pnfs_commit_array *array; |
| 389 | struct nfs_page *req; |
| 390 | |
Trond Myklebust | fb6b53b | 2020-03-20 19:24:19 -0400 | [diff] [blame] | 391 | list_for_each_entry(array, &fl_cinfo->commits, cinfo_list) { |
| 392 | req = pnfs_bucket_search_commit_reqs(array->buckets, |
| 393 | array->nbuckets, page); |
| 394 | if (req) |
| 395 | return req; |
| 396 | } |
| 397 | return NULL; |
| 398 | } |
| 399 | EXPORT_SYMBOL_GPL(pnfs_generic_search_commit_reqs); |
| 400 | |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 401 | static struct pnfs_layout_segment * |
| 402 | pnfs_bucket_get_committing(struct list_head *head, |
| 403 | struct pnfs_commit_bucket *bucket, |
| 404 | struct nfs_commit_info *cinfo) |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 405 | { |
Trond Myklebust | 46c9ea1 | 2021-01-06 11:54:57 -0500 | [diff] [blame] | 406 | struct pnfs_layout_segment *lseg; |
Trond Myklebust | 4118188 | 2017-07-12 19:10:57 -0400 | [diff] [blame] | 407 | struct list_head *pos; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 408 | |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 409 | list_for_each(pos, &bucket->committing) |
| 410 | cinfo->ds->ncommitting--; |
| 411 | list_splice_init(&bucket->committing, head); |
Trond Myklebust | 46c9ea1 | 2021-01-06 11:54:57 -0500 | [diff] [blame] | 412 | lseg = pnfs_free_bucket_lseg(bucket); |
| 413 | if (!lseg) |
| 414 | lseg = pnfs_get_lseg(bucket->lseg); |
| 415 | return lseg; |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static struct nfs_commit_data * |
| 419 | pnfs_bucket_fetch_commitdata(struct pnfs_commit_bucket *bucket, |
| 420 | struct nfs_commit_info *cinfo) |
| 421 | { |
| 422 | struct nfs_commit_data *data = nfs_commitdata_alloc(false); |
| 423 | |
| 424 | if (!data) |
| 425 | return NULL; |
| 426 | data->lseg = pnfs_bucket_get_committing(&data->pages, bucket, cinfo); |
| 427 | return data; |
| 428 | } |
| 429 | |
| 430 | static void pnfs_generic_retry_commit(struct pnfs_commit_bucket *buckets, |
| 431 | unsigned int nbuckets, |
| 432 | struct nfs_commit_info *cinfo, |
| 433 | unsigned int idx) |
| 434 | { |
| 435 | struct pnfs_commit_bucket *bucket; |
| 436 | struct pnfs_layout_segment *freeme; |
| 437 | LIST_HEAD(pages); |
| 438 | |
| 439 | for (bucket = buckets; idx < nbuckets; bucket++, idx++) { |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 440 | if (list_empty(&bucket->committing)) |
| 441 | continue; |
Trond Myklebust | d0fbb1d | 2018-08-14 17:25:37 -0400 | [diff] [blame] | 442 | mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 443 | freeme = pnfs_bucket_get_committing(&pages, bucket, cinfo); |
| 444 | mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); |
| 445 | nfs_retry_commit(&pages, freeme, cinfo, idx); |
| 446 | pnfs_put_lseg(freeme); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | static unsigned int |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 451 | pnfs_bucket_alloc_ds_commits(struct list_head *list, |
| 452 | struct pnfs_commit_bucket *buckets, |
| 453 | unsigned int nbuckets, |
| 454 | struct nfs_commit_info *cinfo) |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 455 | { |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 456 | struct pnfs_commit_bucket *bucket; |
| 457 | struct nfs_commit_data *data; |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 458 | unsigned int i; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 459 | unsigned int nreq = 0; |
| 460 | |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 461 | for (i = 0, bucket = buckets; i < nbuckets; i++, bucket++) { |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 462 | if (list_empty(&bucket->committing)) |
| 463 | continue; |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 464 | mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); |
| 465 | if (!list_empty(&bucket->committing)) { |
| 466 | data = pnfs_bucket_fetch_commitdata(bucket, cinfo); |
| 467 | if (!data) |
| 468 | goto out_error; |
| 469 | data->ds_commit_index = i; |
| 470 | list_add_tail(&data->list, list); |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 471 | nreq++; |
| 472 | } |
| 473 | mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 474 | } |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 475 | return nreq; |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 476 | out_error: |
Trond Myklebust | d0fbb1d | 2018-08-14 17:25:37 -0400 | [diff] [blame] | 477 | mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 478 | /* Clean up on error */ |
| 479 | pnfs_generic_retry_commit(buckets, nbuckets, cinfo, i); |
| 480 | return nreq; |
Weston Andros Adamson | ade8feb | 2016-05-25 10:07:23 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Trond Myklebust | 0cb1f6d | 2020-03-19 19:31:00 -0400 | [diff] [blame] | 483 | static unsigned int |
| 484 | pnfs_alloc_ds_commits_list(struct list_head *list, |
| 485 | struct pnfs_ds_commit_info *fl_cinfo, |
| 486 | struct nfs_commit_info *cinfo) |
| 487 | { |
| 488 | struct pnfs_commit_array *array; |
| 489 | unsigned int ret = 0; |
| 490 | |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 491 | rcu_read_lock(); |
| 492 | list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { |
| 493 | if (!array->lseg || !pnfs_get_commit_array(array)) |
| 494 | continue; |
| 495 | rcu_read_unlock(); |
Trond Myklebust | 0cb1f6d | 2020-03-19 19:31:00 -0400 | [diff] [blame] | 496 | ret += pnfs_bucket_alloc_ds_commits(list, array->buckets, |
| 497 | array->nbuckets, cinfo); |
Trond Myklebust | a990189 | 2020-03-20 16:04:06 -0400 | [diff] [blame] | 498 | rcu_read_lock(); |
| 499 | pnfs_put_commit_array(array, cinfo->inode); |
| 500 | } |
Trond Myklebust | 27d231c | 2020-04-11 11:37:18 -0400 | [diff] [blame] | 501 | rcu_read_unlock(); |
Trond Myklebust | 0cb1f6d | 2020-03-19 19:31:00 -0400 | [diff] [blame] | 502 | return ret; |
| 503 | } |
| 504 | |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 505 | /* This follows nfs_commit_list pretty closely */ |
| 506 | int |
| 507 | pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages, |
| 508 | int how, struct nfs_commit_info *cinfo, |
| 509 | int (*initiate_commit)(struct nfs_commit_data *data, |
| 510 | int how)) |
| 511 | { |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 512 | struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 513 | struct nfs_commit_data *data, *tmp; |
| 514 | LIST_HEAD(list); |
| 515 | unsigned int nreq = 0; |
| 516 | |
| 517 | if (!list_empty(mds_pages)) { |
NeilBrown | 518662e | 2017-04-10 12:22:09 +1000 | [diff] [blame] | 518 | data = nfs_commitdata_alloc(true); |
| 519 | data->ds_commit_index = -1; |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 520 | list_splice_init(mds_pages, &data->pages); |
| 521 | list_add_tail(&data->list, &list); |
NeilBrown | 518662e | 2017-04-10 12:22:09 +1000 | [diff] [blame] | 522 | nreq++; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Trond Myklebust | 0cb1f6d | 2020-03-19 19:31:00 -0400 | [diff] [blame] | 525 | nreq += pnfs_alloc_ds_commits_list(&list, fl_cinfo, cinfo); |
Trond Myklebust | af7cf05 | 2015-09-29 20:34:05 -0400 | [diff] [blame] | 526 | if (nreq == 0) |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 527 | goto out; |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 528 | |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 529 | list_for_each_entry_safe(data, tmp, &list, list) { |
| 530 | list_del(&data->list); |
Trond Myklebust | 2757129 | 2015-08-03 17:38:33 -0400 | [diff] [blame] | 531 | if (data->ds_commit_index < 0) { |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 532 | nfs_init_commit(data, NULL, NULL, cinfo); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 533 | nfs_initiate_commit(NFS_CLIENT(inode), data, |
Peng Tao | c36aae9 | 2014-06-09 07:10:14 +0800 | [diff] [blame] | 534 | NFS_PROTO(data->inode), |
Trond Myklebust | 4fa7ef6 | 2020-05-13 09:55:36 -0400 | [diff] [blame] | 535 | data->mds_ops, how, |
| 536 | RPC_TASK_CRED_NOREF); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 537 | } else { |
Trond Myklebust | 19573c9 | 2020-03-19 17:29:12 -0400 | [diff] [blame] | 538 | nfs_init_commit(data, NULL, data->lseg, cinfo); |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 539 | initiate_commit(data, how); |
| 540 | } |
| 541 | } |
| 542 | out: |
Tom Haynes | f54bcf2 | 2014-12-11 15:34:59 -0500 | [diff] [blame] | 543 | return PNFS_ATTEMPTED; |
| 544 | } |
| 545 | EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist); |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Data server cache |
| 549 | * |
| 550 | * Data servers can be mapped to different device ids. |
| 551 | * nfs4_pnfs_ds reference counting |
| 552 | * - set to 1 on allocation |
| 553 | * - incremented when a device id maps a data server already in the cache. |
| 554 | * - decremented when deviceid is removed from the cache. |
| 555 | */ |
| 556 | static DEFINE_SPINLOCK(nfs4_ds_cache_lock); |
| 557 | static LIST_HEAD(nfs4_data_server_cache); |
| 558 | |
| 559 | /* Debug routines */ |
| 560 | static void |
| 561 | print_ds(struct nfs4_pnfs_ds *ds) |
| 562 | { |
| 563 | if (ds == NULL) { |
| 564 | printk(KERN_WARNING "%s NULL device\n", __func__); |
| 565 | return; |
| 566 | } |
| 567 | printk(KERN_WARNING " ds %s\n" |
| 568 | " ref count %d\n" |
| 569 | " client %p\n" |
| 570 | " cl_exchange_flags %x\n", |
| 571 | ds->ds_remotestr, |
Elena Reshetova | a2a5dea | 2017-10-20 12:53:31 +0300 | [diff] [blame] | 572 | refcount_read(&ds->ds_count), ds->ds_clp, |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 573 | ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0); |
| 574 | } |
| 575 | |
| 576 | static bool |
| 577 | same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2) |
| 578 | { |
| 579 | struct sockaddr_in *a, *b; |
| 580 | struct sockaddr_in6 *a6, *b6; |
| 581 | |
| 582 | if (addr1->sa_family != addr2->sa_family) |
| 583 | return false; |
| 584 | |
| 585 | switch (addr1->sa_family) { |
| 586 | case AF_INET: |
| 587 | a = (struct sockaddr_in *)addr1; |
| 588 | b = (struct sockaddr_in *)addr2; |
| 589 | |
| 590 | if (a->sin_addr.s_addr == b->sin_addr.s_addr && |
| 591 | a->sin_port == b->sin_port) |
| 592 | return true; |
| 593 | break; |
| 594 | |
| 595 | case AF_INET6: |
| 596 | a6 = (struct sockaddr_in6 *)addr1; |
| 597 | b6 = (struct sockaddr_in6 *)addr2; |
| 598 | |
| 599 | /* LINKLOCAL addresses must have matching scope_id */ |
| 600 | if (ipv6_addr_src_scope(&a6->sin6_addr) == |
| 601 | IPV6_ADDR_SCOPE_LINKLOCAL && |
| 602 | a6->sin6_scope_id != b6->sin6_scope_id) |
| 603 | return false; |
| 604 | |
| 605 | if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) && |
| 606 | a6->sin6_port == b6->sin6_port) |
| 607 | return true; |
| 608 | break; |
| 609 | |
| 610 | default: |
| 611 | dprintk("%s: unhandled address family: %u\n", |
| 612 | __func__, addr1->sa_family); |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | return false; |
| 617 | } |
| 618 | |
Trond Myklebust | 6f53693 | 2015-08-13 10:59:07 -0400 | [diff] [blame] | 619 | /* |
| 620 | * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does, |
| 621 | * declare a match. |
| 622 | */ |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 623 | static bool |
| 624 | _same_data_server_addrs_locked(const struct list_head *dsaddrs1, |
| 625 | const struct list_head *dsaddrs2) |
| 626 | { |
| 627 | struct nfs4_pnfs_ds_addr *da1, *da2; |
Trond Myklebust | 6f53693 | 2015-08-13 10:59:07 -0400 | [diff] [blame] | 628 | struct sockaddr *sa1, *sa2; |
| 629 | bool match = false; |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 630 | |
Trond Myklebust | 6f53693 | 2015-08-13 10:59:07 -0400 | [diff] [blame] | 631 | list_for_each_entry(da1, dsaddrs1, da_node) { |
| 632 | sa1 = (struct sockaddr *)&da1->da_addr; |
| 633 | match = false; |
| 634 | list_for_each_entry(da2, dsaddrs2, da_node) { |
| 635 | sa2 = (struct sockaddr *)&da2->da_addr; |
| 636 | match = same_sockaddr(sa1, sa2); |
| 637 | if (match) |
| 638 | break; |
| 639 | } |
| 640 | if (!match) |
| 641 | break; |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 642 | } |
Trond Myklebust | 6f53693 | 2015-08-13 10:59:07 -0400 | [diff] [blame] | 643 | return match; |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Lookup DS by addresses. nfs4_ds_cache_lock is held |
| 648 | */ |
| 649 | static struct nfs4_pnfs_ds * |
| 650 | _data_server_lookup_locked(const struct list_head *dsaddrs) |
| 651 | { |
| 652 | struct nfs4_pnfs_ds *ds; |
| 653 | |
| 654 | list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) |
| 655 | if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs)) |
| 656 | return ds; |
| 657 | return NULL; |
| 658 | } |
| 659 | |
Trond Myklebust | 190c75a | 2020-11-10 09:21:48 -0500 | [diff] [blame] | 660 | static struct nfs4_pnfs_ds_addr *nfs4_pnfs_ds_addr_alloc(gfp_t gfp_flags) |
| 661 | { |
| 662 | struct nfs4_pnfs_ds_addr *da = kzalloc(sizeof(*da), gfp_flags); |
| 663 | if (da) |
| 664 | INIT_LIST_HEAD(&da->da_node); |
| 665 | return da; |
| 666 | } |
| 667 | |
| 668 | static void nfs4_pnfs_ds_addr_free(struct nfs4_pnfs_ds_addr *da) |
| 669 | { |
| 670 | kfree(da->da_remotestr); |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 671 | kfree(da->da_netid); |
Trond Myklebust | 190c75a | 2020-11-10 09:21:48 -0500 | [diff] [blame] | 672 | kfree(da); |
| 673 | } |
| 674 | |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 675 | static void destroy_ds(struct nfs4_pnfs_ds *ds) |
| 676 | { |
| 677 | struct nfs4_pnfs_ds_addr *da; |
| 678 | |
| 679 | dprintk("--> %s\n", __func__); |
| 680 | ifdebug(FACILITY) |
| 681 | print_ds(ds); |
| 682 | |
| 683 | nfs_put_client(ds->ds_clp); |
| 684 | |
| 685 | while (!list_empty(&ds->ds_addrs)) { |
| 686 | da = list_first_entry(&ds->ds_addrs, |
| 687 | struct nfs4_pnfs_ds_addr, |
| 688 | da_node); |
| 689 | list_del_init(&da->da_node); |
Trond Myklebust | 190c75a | 2020-11-10 09:21:48 -0500 | [diff] [blame] | 690 | nfs4_pnfs_ds_addr_free(da); |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | kfree(ds->ds_remotestr); |
| 694 | kfree(ds); |
| 695 | } |
| 696 | |
| 697 | void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds) |
| 698 | { |
Elena Reshetova | a2a5dea | 2017-10-20 12:53:31 +0300 | [diff] [blame] | 699 | if (refcount_dec_and_lock(&ds->ds_count, |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 700 | &nfs4_ds_cache_lock)) { |
| 701 | list_del_init(&ds->ds_node); |
| 702 | spin_unlock(&nfs4_ds_cache_lock); |
| 703 | destroy_ds(ds); |
| 704 | } |
| 705 | } |
| 706 | EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put); |
| 707 | |
| 708 | /* |
| 709 | * Create a string with a human readable address and port to avoid |
| 710 | * complicated setup around many dprinks. |
| 711 | */ |
| 712 | static char * |
| 713 | nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags) |
| 714 | { |
| 715 | struct nfs4_pnfs_ds_addr *da; |
| 716 | char *remotestr; |
| 717 | size_t len; |
| 718 | char *p; |
| 719 | |
| 720 | len = 3; /* '{', '}' and eol */ |
| 721 | list_for_each_entry(da, dsaddrs, da_node) { |
| 722 | len += strlen(da->da_remotestr) + 1; /* string plus comma */ |
| 723 | } |
| 724 | |
| 725 | remotestr = kzalloc(len, gfp_flags); |
| 726 | if (!remotestr) |
| 727 | return NULL; |
| 728 | |
| 729 | p = remotestr; |
| 730 | *(p++) = '{'; |
| 731 | len--; |
| 732 | list_for_each_entry(da, dsaddrs, da_node) { |
| 733 | size_t ll = strlen(da->da_remotestr); |
| 734 | |
| 735 | if (ll > len) |
| 736 | goto out_err; |
| 737 | |
| 738 | memcpy(p, da->da_remotestr, ll); |
| 739 | p += ll; |
| 740 | len -= ll; |
| 741 | |
| 742 | if (len < 1) |
| 743 | goto out_err; |
| 744 | (*p++) = ','; |
| 745 | len--; |
| 746 | } |
| 747 | if (len < 2) |
| 748 | goto out_err; |
| 749 | *(p++) = '}'; |
| 750 | *p = '\0'; |
| 751 | return remotestr; |
| 752 | out_err: |
| 753 | kfree(remotestr); |
| 754 | return NULL; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if |
| 759 | * uncached and return cached struct nfs4_pnfs_ds. |
| 760 | */ |
| 761 | struct nfs4_pnfs_ds * |
| 762 | nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags) |
| 763 | { |
| 764 | struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; |
| 765 | char *remotestr; |
| 766 | |
| 767 | if (list_empty(dsaddrs)) { |
| 768 | dprintk("%s: no addresses defined\n", __func__); |
| 769 | goto out; |
| 770 | } |
| 771 | |
| 772 | ds = kzalloc(sizeof(*ds), gfp_flags); |
| 773 | if (!ds) |
| 774 | goto out; |
| 775 | |
| 776 | /* this is only used for debugging, so it's ok if its NULL */ |
| 777 | remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags); |
| 778 | |
| 779 | spin_lock(&nfs4_ds_cache_lock); |
| 780 | tmp_ds = _data_server_lookup_locked(dsaddrs); |
| 781 | if (tmp_ds == NULL) { |
| 782 | INIT_LIST_HEAD(&ds->ds_addrs); |
| 783 | list_splice_init(dsaddrs, &ds->ds_addrs); |
| 784 | ds->ds_remotestr = remotestr; |
Elena Reshetova | a2a5dea | 2017-10-20 12:53:31 +0300 | [diff] [blame] | 785 | refcount_set(&ds->ds_count, 1); |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 786 | INIT_LIST_HEAD(&ds->ds_node); |
| 787 | ds->ds_clp = NULL; |
| 788 | list_add(&ds->ds_node, &nfs4_data_server_cache); |
| 789 | dprintk("%s add new data server %s\n", __func__, |
| 790 | ds->ds_remotestr); |
| 791 | } else { |
| 792 | kfree(remotestr); |
| 793 | kfree(ds); |
Elena Reshetova | a2a5dea | 2017-10-20 12:53:31 +0300 | [diff] [blame] | 794 | refcount_inc(&tmp_ds->ds_count); |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 795 | dprintk("%s data server %s found, inc'ed ds_count to %d\n", |
| 796 | __func__, tmp_ds->ds_remotestr, |
Elena Reshetova | a2a5dea | 2017-10-20 12:53:31 +0300 | [diff] [blame] | 797 | refcount_read(&tmp_ds->ds_count)); |
Peng Tao | 875ae06 | 2014-05-29 21:06:57 +0800 | [diff] [blame] | 798 | ds = tmp_ds; |
| 799 | } |
| 800 | spin_unlock(&nfs4_ds_cache_lock); |
| 801 | out: |
| 802 | return ds; |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add); |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 805 | |
Trond Myklebust | f46f849 | 2021-07-03 14:34:20 -0400 | [diff] [blame] | 806 | static int nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds) |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 807 | { |
| 808 | might_sleep(); |
Trond Myklebust | f46f849 | 2021-07-03 14:34:20 -0400 | [diff] [blame] | 809 | return wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING, TASK_KILLABLE); |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds) |
| 813 | { |
| 814 | smp_mb__before_atomic(); |
Trond Myklebust | f46f849 | 2021-07-03 14:34:20 -0400 | [diff] [blame] | 815 | clear_and_wake_up_bit(NFS4DS_CONNECTING, &ds->ds_state); |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 816 | } |
| 817 | |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 818 | static struct nfs_client *(*get_v3_ds_connect)( |
Tigran Mkrtchyan | b224f7c | 2016-06-13 20:52:00 +0200 | [diff] [blame] | 819 | struct nfs_server *mds_srv, |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 820 | const struct sockaddr *ds_addr, |
| 821 | int ds_addrlen, |
| 822 | int ds_proto, |
| 823 | unsigned int ds_timeo, |
Anna Schumaker | 7d38de3 | 2016-11-17 15:15:55 -0500 | [diff] [blame] | 824 | unsigned int ds_retrans); |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 825 | |
| 826 | static bool load_v3_ds_connect(void) |
| 827 | { |
| 828 | if (!get_v3_ds_connect) { |
| 829 | get_v3_ds_connect = symbol_request(nfs3_set_ds_client); |
| 830 | WARN_ON_ONCE(!get_v3_ds_connect); |
| 831 | } |
| 832 | |
| 833 | return(get_v3_ds_connect != NULL); |
| 834 | } |
| 835 | |
Arnd Bergmann | df137bc | 2015-03-11 14:37:25 +0100 | [diff] [blame] | 836 | void nfs4_pnfs_v3_ds_connect_unload(void) |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 837 | { |
| 838 | if (get_v3_ds_connect) { |
| 839 | symbol_put(nfs3_set_ds_client); |
| 840 | get_v3_ds_connect = NULL; |
| 841 | } |
| 842 | } |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 843 | |
| 844 | static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv, |
| 845 | struct nfs4_pnfs_ds *ds, |
| 846 | unsigned int timeo, |
Anna Schumaker | 7d38de3 | 2016-11-17 15:15:55 -0500 | [diff] [blame] | 847 | unsigned int retrans) |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 848 | { |
| 849 | struct nfs_client *clp = ERR_PTR(-EIO); |
| 850 | struct nfs4_pnfs_ds_addr *da; |
| 851 | int status = 0; |
| 852 | |
Anna Schumaker | 7d38de3 | 2016-11-17 15:15:55 -0500 | [diff] [blame] | 853 | dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr); |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 854 | |
| 855 | if (!load_v3_ds_connect()) |
Trond Myklebust | dd5c153 | 2021-07-03 14:31:49 -0400 | [diff] [blame] | 856 | return -EPROTONOSUPPORT; |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 857 | |
| 858 | list_for_each_entry(da, &ds->ds_addrs, da_node) { |
| 859 | dprintk("%s: DS %s: trying address %s\n", |
| 860 | __func__, ds->ds_remotestr, da->da_remotestr); |
| 861 | |
Trond Myklebust | fc821d5 | 2016-01-31 14:53:08 -0500 | [diff] [blame] | 862 | if (!IS_ERR(clp)) { |
| 863 | struct xprt_create xprt_args = { |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 864 | .ident = da->da_transport, |
Trond Myklebust | fc821d5 | 2016-01-31 14:53:08 -0500 | [diff] [blame] | 865 | .net = clp->cl_net, |
| 866 | .dstaddr = (struct sockaddr *)&da->da_addr, |
| 867 | .addrlen = da->da_addrlen, |
| 868 | .servername = clp->cl_hostname, |
| 869 | }; |
Trond Myklebust | a12f996 | 2020-11-06 16:58:53 -0500 | [diff] [blame] | 870 | |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 871 | if (da->da_transport != clp->cl_proto) |
| 872 | continue; |
Trond Myklebust | a12f996 | 2020-11-06 16:58:53 -0500 | [diff] [blame] | 873 | if (da->da_addr.ss_family != clp->cl_addr.ss_family) |
| 874 | continue; |
Trond Myklebust | fc821d5 | 2016-01-31 14:53:08 -0500 | [diff] [blame] | 875 | /* Add this address as an alias */ |
| 876 | rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args, |
| 877 | rpc_clnt_test_and_add_xprt, NULL); |
Trond Myklebust | bf2bf9b | 2019-08-15 20:18:48 -0400 | [diff] [blame] | 878 | continue; |
| 879 | } |
| 880 | clp = get_v3_ds_connect(mds_srv, |
| 881 | (struct sockaddr *)&da->da_addr, |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 882 | da->da_addrlen, da->da_transport, |
Trond Myklebust | bf2bf9b | 2019-08-15 20:18:48 -0400 | [diff] [blame] | 883 | timeo, retrans); |
| 884 | if (IS_ERR(clp)) |
| 885 | continue; |
| 886 | clp->cl_rpcclient->cl_softerr = 0; |
| 887 | clp->cl_rpcclient->cl_softrtry = 0; |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if (IS_ERR(clp)) { |
| 891 | status = PTR_ERR(clp); |
| 892 | goto out; |
| 893 | } |
| 894 | |
| 895 | smp_wmb(); |
Baptiste Lepers | a2915fa | 2021-09-06 11:59:24 +1000 | [diff] [blame] | 896 | WRITE_ONCE(ds->ds_clp, clp); |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 897 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); |
| 898 | out: |
| 899 | return status; |
| 900 | } |
| 901 | |
| 902 | static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv, |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 903 | struct nfs4_pnfs_ds *ds, |
| 904 | unsigned int timeo, |
Peng Tao | 064172f | 2014-05-29 21:07:00 +0800 | [diff] [blame] | 905 | unsigned int retrans, |
Anna Schumaker | 7d38de3 | 2016-11-17 15:15:55 -0500 | [diff] [blame] | 906 | u32 minor_version) |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 907 | { |
| 908 | struct nfs_client *clp = ERR_PTR(-EIO); |
| 909 | struct nfs4_pnfs_ds_addr *da; |
| 910 | int status = 0; |
| 911 | |
Anna Schumaker | 7d38de3 | 2016-11-17 15:15:55 -0500 | [diff] [blame] | 912 | dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr); |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 913 | |
| 914 | list_for_each_entry(da, &ds->ds_addrs, da_node) { |
| 915 | dprintk("%s: DS %s: trying address %s\n", |
| 916 | __func__, ds->ds_remotestr, da->da_remotestr); |
| 917 | |
Andy Adamson | 04fa2c6 | 2016-09-09 09:22:29 -0400 | [diff] [blame] | 918 | if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) { |
| 919 | struct xprt_create xprt_args = { |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 920 | .ident = da->da_transport, |
Andy Adamson | 04fa2c6 | 2016-09-09 09:22:29 -0400 | [diff] [blame] | 921 | .net = clp->cl_net, |
| 922 | .dstaddr = (struct sockaddr *)&da->da_addr, |
| 923 | .addrlen = da->da_addrlen, |
| 924 | .servername = clp->cl_hostname, |
| 925 | }; |
| 926 | struct nfs4_add_xprt_data xprtdata = { |
| 927 | .clp = clp, |
Andy Adamson | 04fa2c6 | 2016-09-09 09:22:29 -0400 | [diff] [blame] | 928 | }; |
| 929 | struct rpc_add_xprt_test rpcdata = { |
| 930 | .add_xprt_test = clp->cl_mvops->session_trunk, |
| 931 | .data = &xprtdata, |
| 932 | }; |
| 933 | |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 934 | if (da->da_transport != clp->cl_proto) |
| 935 | continue; |
Trond Myklebust | a12f996 | 2020-11-06 16:58:53 -0500 | [diff] [blame] | 936 | if (da->da_addr.ss_family != clp->cl_addr.ss_family) |
| 937 | continue; |
Andy Adamson | 04fa2c6 | 2016-09-09 09:22:29 -0400 | [diff] [blame] | 938 | /** |
| 939 | * Test this address for session trunking and |
| 940 | * add as an alias |
| 941 | */ |
Trond Myklebust | a12f996 | 2020-11-06 16:58:53 -0500 | [diff] [blame] | 942 | xprtdata.cred = nfs4_get_clid_cred(clp), |
Andy Adamson | 04fa2c6 | 2016-09-09 09:22:29 -0400 | [diff] [blame] | 943 | rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args, |
| 944 | rpc_clnt_setup_test_and_add_xprt, |
| 945 | &rpcdata); |
| 946 | if (xprtdata.cred) |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 947 | put_cred(xprtdata.cred); |
Andy Adamson | 04fa2c6 | 2016-09-09 09:22:29 -0400 | [diff] [blame] | 948 | } else { |
| 949 | clp = nfs4_set_ds_client(mds_srv, |
| 950 | (struct sockaddr *)&da->da_addr, |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 951 | da->da_addrlen, |
| 952 | da->da_transport, timeo, |
| 953 | retrans, minor_version); |
Andy Adamson | 04fa2c6 | 2016-09-09 09:22:29 -0400 | [diff] [blame] | 954 | if (IS_ERR(clp)) |
| 955 | continue; |
| 956 | |
| 957 | status = nfs4_init_ds_session(clp, |
| 958 | mds_srv->nfs_client->cl_lease_time); |
| 959 | if (status) { |
| 960 | nfs_put_client(clp); |
| 961 | clp = ERR_PTR(-EIO); |
| 962 | continue; |
| 963 | } |
| 964 | |
| 965 | } |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if (IS_ERR(clp)) { |
| 969 | status = PTR_ERR(clp); |
| 970 | goto out; |
| 971 | } |
| 972 | |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 973 | smp_wmb(); |
Baptiste Lepers | a2915fa | 2021-09-06 11:59:24 +1000 | [diff] [blame] | 974 | WRITE_ONCE(ds->ds_clp, clp); |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 975 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); |
| 976 | out: |
| 977 | return status; |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Create an rpc connection to the nfs4_pnfs_ds data server. |
| 982 | * Currently only supports IPv4 and IPv6 addresses. |
Weston Andros Adamson | a33e4b0 | 2017-03-09 12:56:48 -0500 | [diff] [blame] | 983 | * If connection fails, make devid unavailable and return a -errno. |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 984 | */ |
Weston Andros Adamson | a33e4b0 | 2017-03-09 12:56:48 -0500 | [diff] [blame] | 985 | int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds, |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 986 | struct nfs4_deviceid_node *devid, unsigned int timeo, |
Anna Schumaker | 7d38de3 | 2016-11-17 15:15:55 -0500 | [diff] [blame] | 987 | unsigned int retrans, u32 version, u32 minor_version) |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 988 | { |
Weston Andros Adamson | da066f3 | 2017-03-09 12:56:49 -0500 | [diff] [blame] | 989 | int err; |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 990 | |
Trond Myklebust | f46f849 | 2021-07-03 14:34:20 -0400 | [diff] [blame] | 991 | do { |
| 992 | err = nfs4_wait_ds_connect(ds); |
| 993 | if (err || ds->ds_clp) |
| 994 | goto out; |
| 995 | if (nfs4_test_deviceid_unavailable(devid)) |
| 996 | return -ENODEV; |
| 997 | } while (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) != 0); |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 998 | |
Trond Myklebust | f46f849 | 2021-07-03 14:34:20 -0400 | [diff] [blame] | 999 | if (ds->ds_clp) |
| 1000 | goto connect_done; |
Weston Andros Adamson | da066f3 | 2017-03-09 12:56:49 -0500 | [diff] [blame] | 1001 | |
Trond Myklebust | f46f849 | 2021-07-03 14:34:20 -0400 | [diff] [blame] | 1002 | switch (version) { |
| 1003 | case 3: |
| 1004 | err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo, retrans); |
| 1005 | break; |
| 1006 | case 4: |
| 1007 | err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo, retrans, |
| 1008 | minor_version); |
| 1009 | break; |
| 1010 | default: |
| 1011 | dprintk("%s: unsupported DS version %d\n", __func__, version); |
| 1012 | err = -EPROTONOSUPPORT; |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 1013 | } |
Weston Andros Adamson | a33e4b0 | 2017-03-09 12:56:48 -0500 | [diff] [blame] | 1014 | |
Trond Myklebust | f46f849 | 2021-07-03 14:34:20 -0400 | [diff] [blame] | 1015 | connect_done: |
| 1016 | nfs4_clear_ds_conn_bit(ds); |
| 1017 | out: |
Weston Andros Adamson | a33e4b0 | 2017-03-09 12:56:48 -0500 | [diff] [blame] | 1018 | /* |
| 1019 | * At this point the ds->ds_clp should be ready, but it might have |
| 1020 | * hit an error. |
| 1021 | */ |
Weston Andros Adamson | da066f3 | 2017-03-09 12:56:49 -0500 | [diff] [blame] | 1022 | if (!err) { |
| 1023 | if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) { |
| 1024 | WARN_ON_ONCE(ds->ds_clp || |
| 1025 | !nfs4_test_deviceid_unavailable(devid)); |
| 1026 | return -EINVAL; |
| 1027 | } |
| 1028 | err = nfs_client_init_status(ds->ds_clp); |
Weston Andros Adamson | a33e4b0 | 2017-03-09 12:56:48 -0500 | [diff] [blame] | 1029 | } |
| 1030 | |
Weston Andros Adamson | da066f3 | 2017-03-09 12:56:49 -0500 | [diff] [blame] | 1031 | return err; |
Peng Tao | 7405f9e | 2014-05-29 21:06:58 +0800 | [diff] [blame] | 1032 | } |
| 1033 | EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect); |
| 1034 | |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1035 | /* |
| 1036 | * Currently only supports ipv4, ipv6 and one multi-path address. |
| 1037 | */ |
| 1038 | struct nfs4_pnfs_ds_addr * |
| 1039 | nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags) |
| 1040 | { |
| 1041 | struct nfs4_pnfs_ds_addr *da = NULL; |
| 1042 | char *buf, *portstr; |
| 1043 | __be16 port; |
Trond Myklebust | 9889981 | 2020-11-09 16:06:15 -0500 | [diff] [blame] | 1044 | ssize_t nlen, rlen; |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1045 | int tmp[2]; |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 1046 | char *netid; |
| 1047 | size_t len; |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1048 | char *startsep = ""; |
| 1049 | char *endsep = ""; |
| 1050 | |
| 1051 | |
| 1052 | /* r_netid */ |
Trond Myklebust | 9889981 | 2020-11-09 16:06:15 -0500 | [diff] [blame] | 1053 | nlen = xdr_stream_decode_string_dup(xdr, &netid, XDR_MAX_NETOBJ, |
| 1054 | gfp_flags); |
| 1055 | if (unlikely(nlen < 0)) |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1056 | goto out_err; |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1057 | |
| 1058 | /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */ |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1059 | /* port is ".ABC.DEF", 8 chars max */ |
Trond Myklebust | 9889981 | 2020-11-09 16:06:15 -0500 | [diff] [blame] | 1060 | rlen = xdr_stream_decode_string_dup(xdr, &buf, INET6_ADDRSTRLEN + |
| 1061 | IPV6_SCOPE_ID_LEN + 8, gfp_flags); |
| 1062 | if (unlikely(rlen < 0)) |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1063 | goto out_free_netid; |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1064 | |
| 1065 | /* replace port '.' with '-' */ |
| 1066 | portstr = strrchr(buf, '.'); |
| 1067 | if (!portstr) { |
| 1068 | dprintk("%s: Failed finding expected dot in port\n", |
| 1069 | __func__); |
| 1070 | goto out_free_buf; |
| 1071 | } |
| 1072 | *portstr = '-'; |
| 1073 | |
| 1074 | /* find '.' between address and port */ |
| 1075 | portstr = strrchr(buf, '.'); |
| 1076 | if (!portstr) { |
| 1077 | dprintk("%s: Failed finding expected dot between address and " |
| 1078 | "port\n", __func__); |
| 1079 | goto out_free_buf; |
| 1080 | } |
| 1081 | *portstr = '\0'; |
| 1082 | |
Trond Myklebust | 190c75a | 2020-11-10 09:21:48 -0500 | [diff] [blame] | 1083 | da = nfs4_pnfs_ds_addr_alloc(gfp_flags); |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1084 | if (unlikely(!da)) |
| 1085 | goto out_free_buf; |
| 1086 | |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1087 | if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr, |
| 1088 | sizeof(da->da_addr))) { |
| 1089 | dprintk("%s: error parsing address %s\n", __func__, buf); |
| 1090 | goto out_free_da; |
| 1091 | } |
| 1092 | |
| 1093 | portstr++; |
| 1094 | sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); |
| 1095 | port = htons((tmp[0] << 8) | (tmp[1])); |
| 1096 | |
| 1097 | switch (da->da_addr.ss_family) { |
| 1098 | case AF_INET: |
| 1099 | ((struct sockaddr_in *)&da->da_addr)->sin_port = port; |
| 1100 | da->da_addrlen = sizeof(struct sockaddr_in); |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1101 | break; |
| 1102 | |
| 1103 | case AF_INET6: |
| 1104 | ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port; |
| 1105 | da->da_addrlen = sizeof(struct sockaddr_in6); |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1106 | startsep = "["; |
| 1107 | endsep = "]"; |
| 1108 | break; |
| 1109 | |
| 1110 | default: |
| 1111 | dprintk("%s: unsupported address family: %u\n", |
| 1112 | __func__, da->da_addr.ss_family); |
| 1113 | goto out_free_da; |
| 1114 | } |
| 1115 | |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 1116 | da->da_transport = xprt_find_transport_ident(netid); |
| 1117 | if (da->da_transport < 0) { |
| 1118 | dprintk("%s: ERROR: unknown r_netid \"%s\"\n", |
| 1119 | __func__, netid); |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1120 | goto out_free_da; |
| 1121 | } |
| 1122 | |
Trond Myklebust | 4be78d2 | 2020-11-06 16:10:52 -0500 | [diff] [blame] | 1123 | da->da_netid = netid; |
| 1124 | |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1125 | /* save human readable address */ |
| 1126 | len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7; |
| 1127 | da->da_remotestr = kzalloc(len, gfp_flags); |
| 1128 | |
| 1129 | /* NULL is ok, only used for dprintk */ |
| 1130 | if (da->da_remotestr) |
| 1131 | snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep, |
| 1132 | buf, endsep, ntohs(port)); |
| 1133 | |
| 1134 | dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr); |
| 1135 | kfree(buf); |
Peng Tao | 6b7f3cf | 2014-05-29 21:06:59 +0800 | [diff] [blame] | 1136 | return da; |
| 1137 | |
| 1138 | out_free_da: |
| 1139 | kfree(da); |
| 1140 | out_free_buf: |
| 1141 | dprintk("%s: Error parsing DS addr: %s\n", __func__, buf); |
| 1142 | kfree(buf); |
| 1143 | out_free_netid: |
| 1144 | kfree(netid); |
| 1145 | out_err: |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr); |
Tom Haynes | 338d00c | 2015-02-17 14:58:15 -0800 | [diff] [blame] | 1149 | |
| 1150 | void |
| 1151 | pnfs_layout_mark_request_commit(struct nfs_page *req, |
| 1152 | struct pnfs_layout_segment *lseg, |
| 1153 | struct nfs_commit_info *cinfo, |
| 1154 | u32 ds_commit_idx) |
| 1155 | { |
| 1156 | struct list_head *list; |
Trond Myklebust | ba827c9 | 2020-03-20 18:34:33 -0400 | [diff] [blame] | 1157 | struct pnfs_commit_array *array; |
Trond Myklebust | e18c18e | 2020-03-22 16:08:55 -0400 | [diff] [blame] | 1158 | struct pnfs_commit_bucket *bucket; |
Tom Haynes | 338d00c | 2015-02-17 14:58:15 -0800 | [diff] [blame] | 1159 | |
Trond Myklebust | e824f99 | 2017-08-01 11:53:49 -0400 | [diff] [blame] | 1160 | mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); |
Trond Myklebust | ba827c9 | 2020-03-20 18:34:33 -0400 | [diff] [blame] | 1161 | array = pnfs_lookup_commit_array(cinfo->ds, lseg); |
Trond Myklebust | e18c18e | 2020-03-22 16:08:55 -0400 | [diff] [blame] | 1162 | if (!array || !pnfs_is_valid_lseg(lseg)) |
Trond Myklebust | ba827c9 | 2020-03-20 18:34:33 -0400 | [diff] [blame] | 1163 | goto out_resched; |
Trond Myklebust | e18c18e | 2020-03-22 16:08:55 -0400 | [diff] [blame] | 1164 | bucket = &array->buckets[ds_commit_idx]; |
| 1165 | list = &bucket->written; |
| 1166 | /* Non-empty buckets hold a reference on the lseg. That ref |
| 1167 | * is normally transferred to the COMMIT call and released |
| 1168 | * there. It could also be released if the last req is pulled |
| 1169 | * off due to a rewrite, in which case it will be done in |
| 1170 | * pnfs_common_clear_request_commit |
| 1171 | */ |
| 1172 | if (!bucket->lseg) |
| 1173 | bucket->lseg = pnfs_get_lseg(lseg); |
Tom Haynes | 338d00c | 2015-02-17 14:58:15 -0800 | [diff] [blame] | 1174 | set_bit(PG_COMMIT_TO_DS, &req->wb_flags); |
| 1175 | cinfo->ds->nwritten++; |
Tom Haynes | 338d00c | 2015-02-17 14:58:15 -0800 | [diff] [blame] | 1176 | |
Trond Myklebust | 86d80f9 | 2015-07-31 16:24:30 -0400 | [diff] [blame] | 1177 | nfs_request_add_commit_list_locked(req, list, cinfo); |
Trond Myklebust | e824f99 | 2017-08-01 11:53:49 -0400 | [diff] [blame] | 1178 | mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); |
Trond Myklebust | 86d80f9 | 2015-07-31 16:24:30 -0400 | [diff] [blame] | 1179 | nfs_mark_page_unstable(req->wb_page, cinfo); |
Trond Myklebust | ba827c9 | 2020-03-20 18:34:33 -0400 | [diff] [blame] | 1180 | return; |
| 1181 | out_resched: |
| 1182 | mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); |
| 1183 | cinfo->completion_ops->resched_write(cinfo, req); |
Tom Haynes | 338d00c | 2015-02-17 14:58:15 -0800 | [diff] [blame] | 1184 | } |
| 1185 | EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit); |
Trond Myklebust | 5bb89b4 | 2015-03-25 14:14:42 -0400 | [diff] [blame] | 1186 | |
| 1187 | int |
| 1188 | pnfs_nfs_generic_sync(struct inode *inode, bool datasync) |
| 1189 | { |
Trond Myklebust | 2e18d4d | 2016-06-26 18:54:58 -0400 | [diff] [blame] | 1190 | int ret; |
| 1191 | |
| 1192 | if (!pnfs_layoutcommit_outstanding(inode)) |
| 1193 | return 0; |
| 1194 | ret = nfs_commit_inode(inode, FLUSH_SYNC); |
| 1195 | if (ret < 0) |
| 1196 | return ret; |
Trond Myklebust | 5bb89b4 | 2015-03-25 14:14:42 -0400 | [diff] [blame] | 1197 | if (datasync) |
| 1198 | return 0; |
| 1199 | return pnfs_layoutcommit_inode(inode, true); |
| 1200 | } |
| 1201 | EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync); |
| 1202 | |