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