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