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