Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Christoph Hellwig. |
| 3 | */ |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 4 | #include <linux/kmod.h> |
| 5 | #include <linux/file.h> |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 6 | #include <linux/jhash.h> |
| 7 | #include <linux/sched.h> |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 8 | #include <linux/sunrpc/addr.h> |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 9 | |
| 10 | #include "pnfs.h" |
| 11 | #include "netns.h" |
| 12 | |
| 13 | #define NFSDDBG_FACILITY NFSDDBG_PNFS |
| 14 | |
| 15 | struct nfs4_layout { |
| 16 | struct list_head lo_perstate; |
| 17 | struct nfs4_layout_stateid *lo_state; |
| 18 | struct nfsd4_layout_seg lo_seg; |
| 19 | }; |
| 20 | |
| 21 | static struct kmem_cache *nfs4_layout_cache; |
| 22 | static struct kmem_cache *nfs4_layout_stateid_cache; |
| 23 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 24 | static struct nfsd4_callback_ops nfsd4_cb_layout_ops; |
| 25 | static const struct lock_manager_operations nfsd4_layouts_lm_ops; |
| 26 | |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 27 | const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] = { |
| 28 | }; |
| 29 | |
| 30 | /* pNFS device ID to export fsid mapping */ |
| 31 | #define DEVID_HASH_BITS 8 |
| 32 | #define DEVID_HASH_SIZE (1 << DEVID_HASH_BITS) |
| 33 | #define DEVID_HASH_MASK (DEVID_HASH_SIZE - 1) |
| 34 | static u64 nfsd_devid_seq = 1; |
| 35 | static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE]; |
| 36 | static DEFINE_SPINLOCK(nfsd_devid_lock); |
| 37 | |
| 38 | static inline u32 devid_hashfn(u64 idx) |
| 39 | { |
| 40 | return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK; |
| 41 | } |
| 42 | |
| 43 | static void |
| 44 | nfsd4_alloc_devid_map(const struct svc_fh *fhp) |
| 45 | { |
| 46 | const struct knfsd_fh *fh = &fhp->fh_handle; |
| 47 | size_t fsid_len = key_len(fh->fh_fsid_type); |
| 48 | struct nfsd4_deviceid_map *map, *old; |
| 49 | int i; |
| 50 | |
| 51 | map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL); |
| 52 | if (!map) |
| 53 | return; |
| 54 | |
| 55 | map->fsid_type = fh->fh_fsid_type; |
| 56 | memcpy(&map->fsid, fh->fh_fsid, fsid_len); |
| 57 | |
| 58 | spin_lock(&nfsd_devid_lock); |
| 59 | if (fhp->fh_export->ex_devid_map) |
| 60 | goto out_unlock; |
| 61 | |
| 62 | for (i = 0; i < DEVID_HASH_SIZE; i++) { |
| 63 | list_for_each_entry(old, &nfsd_devid_hash[i], hash) { |
| 64 | if (old->fsid_type != fh->fh_fsid_type) |
| 65 | continue; |
| 66 | if (memcmp(old->fsid, fh->fh_fsid, |
| 67 | key_len(old->fsid_type))) |
| 68 | continue; |
| 69 | |
| 70 | fhp->fh_export->ex_devid_map = old; |
| 71 | goto out_unlock; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | map->idx = nfsd_devid_seq++; |
| 76 | list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]); |
| 77 | fhp->fh_export->ex_devid_map = map; |
| 78 | map = NULL; |
| 79 | |
| 80 | out_unlock: |
| 81 | spin_unlock(&nfsd_devid_lock); |
| 82 | kfree(map); |
| 83 | } |
| 84 | |
| 85 | struct nfsd4_deviceid_map * |
| 86 | nfsd4_find_devid_map(int idx) |
| 87 | { |
| 88 | struct nfsd4_deviceid_map *map, *ret = NULL; |
| 89 | |
| 90 | rcu_read_lock(); |
| 91 | list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash) |
| 92 | if (map->idx == idx) |
| 93 | ret = map; |
| 94 | rcu_read_unlock(); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp, |
| 101 | u32 device_generation) |
| 102 | { |
| 103 | if (!fhp->fh_export->ex_devid_map) { |
| 104 | nfsd4_alloc_devid_map(fhp); |
| 105 | if (!fhp->fh_export->ex_devid_map) |
| 106 | return -ENOMEM; |
| 107 | } |
| 108 | |
| 109 | id->fsid_idx = fhp->fh_export->ex_devid_map->idx; |
| 110 | id->generation = device_generation; |
| 111 | id->pad = 0; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | void nfsd4_setup_layout_type(struct svc_export *exp) |
| 116 | { |
| 117 | if (exp->ex_flags & NFSEXP_NOPNFS) |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | nfsd4_free_layout_stateid(struct nfs4_stid *stid) |
| 123 | { |
| 124 | struct nfs4_layout_stateid *ls = layoutstateid(stid); |
| 125 | struct nfs4_client *clp = ls->ls_stid.sc_client; |
| 126 | struct nfs4_file *fp = ls->ls_stid.sc_file; |
| 127 | |
| 128 | spin_lock(&clp->cl_lock); |
| 129 | list_del_init(&ls->ls_perclnt); |
| 130 | spin_unlock(&clp->cl_lock); |
| 131 | |
| 132 | spin_lock(&fp->fi_lock); |
| 133 | list_del_init(&ls->ls_perfile); |
| 134 | spin_unlock(&fp->fi_lock); |
| 135 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 136 | vfs_setlease(ls->ls_file, F_UNLCK, NULL, (void **)&ls); |
| 137 | fput(ls->ls_file); |
| 138 | |
| 139 | if (ls->ls_recalled) |
| 140 | atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls); |
| 141 | |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 142 | kmem_cache_free(nfs4_layout_stateid_cache, ls); |
| 143 | } |
| 144 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 145 | static int |
| 146 | nfsd4_layout_setlease(struct nfs4_layout_stateid *ls) |
| 147 | { |
| 148 | struct file_lock *fl; |
| 149 | int status; |
| 150 | |
| 151 | fl = locks_alloc_lock(); |
| 152 | if (!fl) |
| 153 | return -ENOMEM; |
| 154 | locks_init_lock(fl); |
| 155 | fl->fl_lmops = &nfsd4_layouts_lm_ops; |
| 156 | fl->fl_flags = FL_LAYOUT; |
| 157 | fl->fl_type = F_RDLCK; |
| 158 | fl->fl_end = OFFSET_MAX; |
| 159 | fl->fl_owner = ls; |
| 160 | fl->fl_pid = current->tgid; |
| 161 | fl->fl_file = ls->ls_file; |
| 162 | |
| 163 | status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL); |
| 164 | if (status) { |
| 165 | locks_free_lock(fl); |
| 166 | return status; |
| 167 | } |
| 168 | BUG_ON(fl != NULL); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 172 | static struct nfs4_layout_stateid * |
| 173 | nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate, |
| 174 | struct nfs4_stid *parent, u32 layout_type) |
| 175 | { |
| 176 | struct nfs4_client *clp = cstate->clp; |
| 177 | struct nfs4_file *fp = parent->sc_file; |
| 178 | struct nfs4_layout_stateid *ls; |
| 179 | struct nfs4_stid *stp; |
| 180 | |
| 181 | stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache); |
| 182 | if (!stp) |
| 183 | return NULL; |
| 184 | stp->sc_free = nfsd4_free_layout_stateid; |
| 185 | get_nfs4_file(fp); |
| 186 | stp->sc_file = fp; |
| 187 | |
| 188 | ls = layoutstateid(stp); |
| 189 | INIT_LIST_HEAD(&ls->ls_perclnt); |
| 190 | INIT_LIST_HEAD(&ls->ls_perfile); |
| 191 | spin_lock_init(&ls->ls_lock); |
| 192 | INIT_LIST_HEAD(&ls->ls_layouts); |
| 193 | ls->ls_layout_type = layout_type; |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 194 | nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops, |
| 195 | NFSPROC4_CLNT_CB_LAYOUT); |
| 196 | |
| 197 | if (parent->sc_type == NFS4_DELEG_STID) |
| 198 | ls->ls_file = get_file(fp->fi_deleg_file); |
| 199 | else |
| 200 | ls->ls_file = find_any_file(fp); |
| 201 | BUG_ON(!ls->ls_file); |
| 202 | |
| 203 | if (nfsd4_layout_setlease(ls)) { |
| 204 | put_nfs4_file(fp); |
| 205 | kmem_cache_free(nfs4_layout_stateid_cache, ls); |
| 206 | return NULL; |
| 207 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 208 | |
| 209 | spin_lock(&clp->cl_lock); |
| 210 | stp->sc_type = NFS4_LAYOUT_STID; |
| 211 | list_add(&ls->ls_perclnt, &clp->cl_lo_states); |
| 212 | spin_unlock(&clp->cl_lock); |
| 213 | |
| 214 | spin_lock(&fp->fi_lock); |
| 215 | list_add(&ls->ls_perfile, &fp->fi_lo_states); |
| 216 | spin_unlock(&fp->fi_lock); |
| 217 | |
| 218 | return ls; |
| 219 | } |
| 220 | |
| 221 | __be32 |
| 222 | nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp, |
| 223 | struct nfsd4_compound_state *cstate, stateid_t *stateid, |
| 224 | bool create, u32 layout_type, struct nfs4_layout_stateid **lsp) |
| 225 | { |
| 226 | struct nfs4_layout_stateid *ls; |
| 227 | struct nfs4_stid *stid; |
| 228 | unsigned char typemask = NFS4_LAYOUT_STID; |
| 229 | __be32 status; |
| 230 | |
| 231 | if (create) |
| 232 | typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID); |
| 233 | |
| 234 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid, |
| 235 | net_generic(SVC_NET(rqstp), nfsd_net_id)); |
| 236 | if (status) |
| 237 | goto out; |
| 238 | |
| 239 | if (!fh_match(&cstate->current_fh.fh_handle, |
| 240 | &stid->sc_file->fi_fhandle)) { |
| 241 | status = nfserr_bad_stateid; |
| 242 | goto out_put_stid; |
| 243 | } |
| 244 | |
| 245 | if (stid->sc_type != NFS4_LAYOUT_STID) { |
| 246 | ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type); |
| 247 | nfs4_put_stid(stid); |
| 248 | |
| 249 | status = nfserr_jukebox; |
| 250 | if (!ls) |
| 251 | goto out; |
| 252 | } else { |
| 253 | ls = container_of(stid, struct nfs4_layout_stateid, ls_stid); |
| 254 | |
| 255 | status = nfserr_bad_stateid; |
| 256 | if (stateid->si_generation > stid->sc_stateid.si_generation) |
| 257 | goto out_put_stid; |
| 258 | if (layout_type != ls->ls_layout_type) |
| 259 | goto out_put_stid; |
| 260 | } |
| 261 | |
| 262 | *lsp = ls; |
| 263 | return 0; |
| 264 | |
| 265 | out_put_stid: |
| 266 | nfs4_put_stid(stid); |
| 267 | out: |
| 268 | return status; |
| 269 | } |
| 270 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 271 | static void |
| 272 | nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls) |
| 273 | { |
| 274 | spin_lock(&ls->ls_lock); |
| 275 | if (ls->ls_recalled) |
| 276 | goto out_unlock; |
| 277 | |
| 278 | ls->ls_recalled = true; |
| 279 | atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls); |
| 280 | if (list_empty(&ls->ls_layouts)) |
| 281 | goto out_unlock; |
| 282 | |
| 283 | atomic_inc(&ls->ls_stid.sc_count); |
| 284 | update_stateid(&ls->ls_stid.sc_stateid); |
| 285 | memcpy(&ls->ls_recall_sid, &ls->ls_stid.sc_stateid, sizeof(stateid_t)); |
| 286 | nfsd4_run_cb(&ls->ls_recall); |
| 287 | |
| 288 | out_unlock: |
| 289 | spin_unlock(&ls->ls_lock); |
| 290 | } |
| 291 | |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 292 | static inline u64 |
| 293 | layout_end(struct nfsd4_layout_seg *seg) |
| 294 | { |
| 295 | u64 end = seg->offset + seg->length; |
| 296 | return end >= seg->offset ? end : NFS4_MAX_UINT64; |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | layout_update_len(struct nfsd4_layout_seg *lo, u64 end) |
| 301 | { |
| 302 | if (end == NFS4_MAX_UINT64) |
| 303 | lo->length = NFS4_MAX_UINT64; |
| 304 | else |
| 305 | lo->length = end - lo->offset; |
| 306 | } |
| 307 | |
| 308 | static bool |
| 309 | layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s) |
| 310 | { |
| 311 | if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode) |
| 312 | return false; |
| 313 | if (layout_end(&lo->lo_seg) <= s->offset) |
| 314 | return false; |
| 315 | if (layout_end(s) <= lo->lo_seg.offset) |
| 316 | return false; |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | static bool |
| 321 | layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new) |
| 322 | { |
| 323 | if (lo->iomode != new->iomode) |
| 324 | return false; |
| 325 | if (layout_end(new) < lo->offset) |
| 326 | return false; |
| 327 | if (layout_end(lo) < new->offset) |
| 328 | return false; |
| 329 | |
| 330 | lo->offset = min(lo->offset, new->offset); |
| 331 | layout_update_len(lo, max(layout_end(lo), layout_end(new))); |
| 332 | return true; |
| 333 | } |
| 334 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 335 | static __be32 |
| 336 | nfsd4_recall_conflict(struct nfs4_layout_stateid *ls) |
| 337 | { |
| 338 | struct nfs4_file *fp = ls->ls_stid.sc_file; |
| 339 | struct nfs4_layout_stateid *l, *n; |
| 340 | __be32 nfserr = nfs_ok; |
| 341 | |
| 342 | assert_spin_locked(&fp->fi_lock); |
| 343 | |
| 344 | list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) { |
| 345 | if (l != ls) { |
| 346 | nfsd4_recall_file_layout(l); |
| 347 | nfserr = nfserr_recallconflict; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return nfserr; |
| 352 | } |
| 353 | |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 354 | __be32 |
| 355 | nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls) |
| 356 | { |
| 357 | struct nfsd4_layout_seg *seg = &lgp->lg_seg; |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 358 | struct nfs4_file *fp = ls->ls_stid.sc_file; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 359 | struct nfs4_layout *lp, *new = NULL; |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 360 | __be32 nfserr; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 361 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 362 | spin_lock(&fp->fi_lock); |
| 363 | nfserr = nfsd4_recall_conflict(ls); |
| 364 | if (nfserr) |
| 365 | goto out; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 366 | spin_lock(&ls->ls_lock); |
| 367 | list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) { |
| 368 | if (layouts_try_merge(&lp->lo_seg, seg)) |
| 369 | goto done; |
| 370 | } |
| 371 | spin_unlock(&ls->ls_lock); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 372 | spin_unlock(&fp->fi_lock); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 373 | |
| 374 | new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL); |
| 375 | if (!new) |
| 376 | return nfserr_jukebox; |
| 377 | memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg)); |
| 378 | new->lo_state = ls; |
| 379 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 380 | spin_lock(&fp->fi_lock); |
| 381 | nfserr = nfsd4_recall_conflict(ls); |
| 382 | if (nfserr) |
| 383 | goto out; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 384 | spin_lock(&ls->ls_lock); |
| 385 | list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) { |
| 386 | if (layouts_try_merge(&lp->lo_seg, seg)) |
| 387 | goto done; |
| 388 | } |
| 389 | |
| 390 | atomic_inc(&ls->ls_stid.sc_count); |
| 391 | list_add_tail(&new->lo_perstate, &ls->ls_layouts); |
| 392 | new = NULL; |
| 393 | done: |
| 394 | update_stateid(&ls->ls_stid.sc_stateid); |
| 395 | memcpy(&lgp->lg_sid, &ls->ls_stid.sc_stateid, sizeof(stateid_t)); |
| 396 | spin_unlock(&ls->ls_lock); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 397 | out: |
| 398 | spin_unlock(&fp->fi_lock); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 399 | if (new) |
| 400 | kmem_cache_free(nfs4_layout_cache, new); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 401 | return nfserr; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static void |
| 405 | nfsd4_free_layouts(struct list_head *reaplist) |
| 406 | { |
| 407 | while (!list_empty(reaplist)) { |
| 408 | struct nfs4_layout *lp = list_first_entry(reaplist, |
| 409 | struct nfs4_layout, lo_perstate); |
| 410 | |
| 411 | list_del(&lp->lo_perstate); |
| 412 | nfs4_put_stid(&lp->lo_state->ls_stid); |
| 413 | kmem_cache_free(nfs4_layout_cache, lp); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | static void |
| 418 | nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg, |
| 419 | struct list_head *reaplist) |
| 420 | { |
| 421 | struct nfsd4_layout_seg *lo = &lp->lo_seg; |
| 422 | u64 end = layout_end(lo); |
| 423 | |
| 424 | if (seg->offset <= lo->offset) { |
| 425 | if (layout_end(seg) >= end) { |
| 426 | list_move_tail(&lp->lo_perstate, reaplist); |
| 427 | return; |
| 428 | } |
| 429 | end = seg->offset; |
| 430 | } else { |
| 431 | /* retain the whole layout segment on a split. */ |
| 432 | if (layout_end(seg) < end) { |
| 433 | dprintk("%s: split not supported\n", __func__); |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | lo->offset = layout_end(seg); |
| 438 | } |
| 439 | |
| 440 | layout_update_len(lo, end); |
| 441 | } |
| 442 | |
| 443 | __be32 |
| 444 | nfsd4_return_file_layouts(struct svc_rqst *rqstp, |
| 445 | struct nfsd4_compound_state *cstate, |
| 446 | struct nfsd4_layoutreturn *lrp) |
| 447 | { |
| 448 | struct nfs4_layout_stateid *ls; |
| 449 | struct nfs4_layout *lp, *n; |
| 450 | LIST_HEAD(reaplist); |
| 451 | __be32 nfserr; |
| 452 | int found = 0; |
| 453 | |
| 454 | nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid, |
| 455 | false, lrp->lr_layout_type, |
| 456 | &ls); |
| 457 | if (nfserr) |
| 458 | return nfserr; |
| 459 | |
| 460 | spin_lock(&ls->ls_lock); |
| 461 | list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) { |
| 462 | if (layouts_overlapping(lp, &lrp->lr_seg)) { |
| 463 | nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist); |
| 464 | found++; |
| 465 | } |
| 466 | } |
| 467 | if (!list_empty(&ls->ls_layouts)) { |
| 468 | if (found) { |
| 469 | update_stateid(&ls->ls_stid.sc_stateid); |
| 470 | memcpy(&lrp->lr_sid, &ls->ls_stid.sc_stateid, |
| 471 | sizeof(stateid_t)); |
| 472 | } |
| 473 | lrp->lrs_present = 1; |
| 474 | } else { |
| 475 | nfs4_unhash_stid(&ls->ls_stid); |
| 476 | lrp->lrs_present = 0; |
| 477 | } |
| 478 | spin_unlock(&ls->ls_lock); |
| 479 | |
| 480 | nfs4_put_stid(&ls->ls_stid); |
| 481 | nfsd4_free_layouts(&reaplist); |
| 482 | return nfs_ok; |
| 483 | } |
| 484 | |
| 485 | __be32 |
| 486 | nfsd4_return_client_layouts(struct svc_rqst *rqstp, |
| 487 | struct nfsd4_compound_state *cstate, |
| 488 | struct nfsd4_layoutreturn *lrp) |
| 489 | { |
| 490 | struct nfs4_layout_stateid *ls, *n; |
| 491 | struct nfs4_client *clp = cstate->clp; |
| 492 | struct nfs4_layout *lp, *t; |
| 493 | LIST_HEAD(reaplist); |
| 494 | |
| 495 | lrp->lrs_present = 0; |
| 496 | |
| 497 | spin_lock(&clp->cl_lock); |
| 498 | list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) { |
| 499 | if (lrp->lr_return_type == RETURN_FSID && |
| 500 | !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle, |
| 501 | &cstate->current_fh.fh_handle)) |
| 502 | continue; |
| 503 | |
| 504 | spin_lock(&ls->ls_lock); |
| 505 | list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) { |
| 506 | if (lrp->lr_seg.iomode == IOMODE_ANY || |
| 507 | lrp->lr_seg.iomode == lp->lo_seg.iomode) |
| 508 | list_move_tail(&lp->lo_perstate, &reaplist); |
| 509 | } |
| 510 | spin_unlock(&ls->ls_lock); |
| 511 | } |
| 512 | spin_unlock(&clp->cl_lock); |
| 513 | |
| 514 | nfsd4_free_layouts(&reaplist); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static void |
| 519 | nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls, |
| 520 | struct list_head *reaplist) |
| 521 | { |
| 522 | spin_lock(&ls->ls_lock); |
| 523 | list_splice_init(&ls->ls_layouts, reaplist); |
| 524 | spin_unlock(&ls->ls_lock); |
| 525 | } |
| 526 | |
| 527 | void |
| 528 | nfsd4_return_all_client_layouts(struct nfs4_client *clp) |
| 529 | { |
| 530 | struct nfs4_layout_stateid *ls, *n; |
| 531 | LIST_HEAD(reaplist); |
| 532 | |
| 533 | spin_lock(&clp->cl_lock); |
| 534 | list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) |
| 535 | nfsd4_return_all_layouts(ls, &reaplist); |
| 536 | spin_unlock(&clp->cl_lock); |
| 537 | |
| 538 | nfsd4_free_layouts(&reaplist); |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp) |
| 543 | { |
| 544 | struct nfs4_layout_stateid *ls, *n; |
| 545 | LIST_HEAD(reaplist); |
| 546 | |
| 547 | spin_lock(&fp->fi_lock); |
| 548 | list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) { |
| 549 | if (ls->ls_stid.sc_client == clp) |
| 550 | nfsd4_return_all_layouts(ls, &reaplist); |
| 551 | } |
| 552 | spin_unlock(&fp->fi_lock); |
| 553 | |
| 554 | nfsd4_free_layouts(&reaplist); |
| 555 | } |
| 556 | |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 557 | static void |
| 558 | nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls) |
| 559 | { |
| 560 | struct nfs4_client *clp = ls->ls_stid.sc_client; |
| 561 | char addr_str[INET6_ADDRSTRLEN]; |
| 562 | static char *envp[] = { |
| 563 | "HOME=/", |
| 564 | "TERM=linux", |
| 565 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", |
| 566 | NULL |
| 567 | }; |
| 568 | char *argv[8]; |
| 569 | int error; |
| 570 | |
| 571 | rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str)); |
| 572 | |
| 573 | printk(KERN_WARNING |
| 574 | "nfsd: client %s failed to respond to layout recall. " |
| 575 | " Fencing..\n", addr_str); |
| 576 | |
| 577 | argv[0] = "/sbin/nfsd-recall-failed"; |
| 578 | argv[1] = addr_str; |
| 579 | argv[2] = ls->ls_file->f_path.mnt->mnt_sb->s_id; |
| 580 | argv[3] = NULL; |
| 581 | |
| 582 | error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); |
| 583 | if (error) { |
| 584 | printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n", |
| 585 | addr_str, error); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static int |
| 590 | nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task) |
| 591 | { |
| 592 | struct nfs4_layout_stateid *ls = |
| 593 | container_of(cb, struct nfs4_layout_stateid, ls_recall); |
| 594 | LIST_HEAD(reaplist); |
| 595 | |
| 596 | switch (task->tk_status) { |
| 597 | case 0: |
| 598 | return 1; |
| 599 | case -NFS4ERR_NOMATCHING_LAYOUT: |
| 600 | task->tk_status = 0; |
| 601 | return 1; |
| 602 | case -NFS4ERR_DELAY: |
| 603 | /* Poll the client until it's done with the layout */ |
| 604 | /* FIXME: cap number of retries. |
| 605 | * The pnfs standard states that we need to only expire |
| 606 | * the client after at-least "lease time" .eg lease-time * 2 |
| 607 | * when failing to communicate a recall |
| 608 | */ |
| 609 | rpc_delay(task, HZ/100); /* 10 mili-seconds */ |
| 610 | return 0; |
| 611 | default: |
| 612 | /* |
| 613 | * Unknown error or non-responding client, we'll need to fence. |
| 614 | */ |
| 615 | nfsd4_cb_layout_fail(ls); |
| 616 | return -1; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | static void |
| 621 | nfsd4_cb_layout_release(struct nfsd4_callback *cb) |
| 622 | { |
| 623 | struct nfs4_layout_stateid *ls = |
| 624 | container_of(cb, struct nfs4_layout_stateid, ls_recall); |
| 625 | LIST_HEAD(reaplist); |
| 626 | |
| 627 | nfsd4_return_all_layouts(ls, &reaplist); |
| 628 | nfsd4_free_layouts(&reaplist); |
| 629 | nfs4_put_stid(&ls->ls_stid); |
| 630 | } |
| 631 | |
| 632 | static struct nfsd4_callback_ops nfsd4_cb_layout_ops = { |
| 633 | .done = nfsd4_cb_layout_done, |
| 634 | .release = nfsd4_cb_layout_release, |
| 635 | }; |
| 636 | |
| 637 | static bool |
| 638 | nfsd4_layout_lm_break(struct file_lock *fl) |
| 639 | { |
| 640 | /* |
| 641 | * We don't want the locks code to timeout the lease for us; |
| 642 | * we'll remove it ourself if a layout isn't returned |
| 643 | * in time: |
| 644 | */ |
| 645 | fl->fl_break_time = 0; |
| 646 | nfsd4_recall_file_layout(fl->fl_owner); |
| 647 | return false; |
| 648 | } |
| 649 | |
| 650 | static int |
| 651 | nfsd4_layout_lm_change(struct file_lock *onlist, int arg, |
| 652 | struct list_head *dispose) |
| 653 | { |
| 654 | BUG_ON(!(arg & F_UNLCK)); |
| 655 | return lease_modify(onlist, arg, dispose); |
| 656 | } |
| 657 | |
| 658 | static const struct lock_manager_operations nfsd4_layouts_lm_ops = { |
| 659 | .lm_break = nfsd4_layout_lm_break, |
| 660 | .lm_change = nfsd4_layout_lm_change, |
| 661 | }; |
| 662 | |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 663 | int |
| 664 | nfsd4_init_pnfs(void) |
| 665 | { |
| 666 | int i; |
| 667 | |
| 668 | for (i = 0; i < DEVID_HASH_SIZE; i++) |
| 669 | INIT_LIST_HEAD(&nfsd_devid_hash[i]); |
| 670 | |
| 671 | nfs4_layout_cache = kmem_cache_create("nfs4_layout", |
| 672 | sizeof(struct nfs4_layout), 0, 0, NULL); |
| 673 | if (!nfs4_layout_cache) |
| 674 | return -ENOMEM; |
| 675 | |
| 676 | nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid", |
| 677 | sizeof(struct nfs4_layout_stateid), 0, 0, NULL); |
| 678 | if (!nfs4_layout_stateid_cache) { |
| 679 | kmem_cache_destroy(nfs4_layout_cache); |
| 680 | return -ENOMEM; |
| 681 | } |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | void |
| 686 | nfsd4_exit_pnfs(void) |
| 687 | { |
| 688 | int i; |
| 689 | |
| 690 | kmem_cache_destroy(nfs4_layout_cache); |
| 691 | kmem_cache_destroy(nfs4_layout_stateid_cache); |
| 692 | |
| 693 | for (i = 0; i < DEVID_HASH_SIZE; i++) { |
| 694 | struct nfsd4_deviceid_map *map, *n; |
| 695 | |
| 696 | list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash) |
| 697 | kfree(map); |
| 698 | } |
| 699 | } |