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