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