^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-2016 Christoph Hellwig.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/iomap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/genhd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/pr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/nfsd/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <scsi/scsi_proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <scsi/scsi_common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <scsi/scsi_request.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "blocklayoutxdr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "pnfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "filecache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define NFSDDBG_FACILITY NFSDDBG_PNFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static __be32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct nfsd4_layoutget *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct nfsd4_layout_seg *seg = &args->lg_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 block_size = i_blocksize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct pnfs_block_extent *bex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct iomap iomap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u32 device_generation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (seg->offset & (block_size - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) dprintk("pnfsd: I/O misaligned\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) goto out_layoutunavailable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Some clients barf on non-zero block numbers for NONE or INVALID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * layouts, so make sure to zero the whole structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) bex = kzalloc(sizeof(*bex), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!bex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) args->lg_content = bex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) &iomap, seg->iomode != IOMODE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) &device_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (error == -ENXIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) goto out_layoutunavailable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (iomap.length < args->lg_minlength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) dprintk("pnfsd: extent smaller than minlength\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) goto out_layoutunavailable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) switch (iomap.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) case IOMAP_MAPPED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (seg->iomode == IOMODE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) bex->es = PNFS_BLOCK_READ_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) bex->es = PNFS_BLOCK_READWRITE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) bex->soff = iomap.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) case IOMAP_UNWRITTEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (seg->iomode & IOMODE_RW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * Crack monkey special case from section 2.3.1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (args->lg_minlength == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dprintk("pnfsd: no soup for you!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto out_layoutunavailable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bex->es = PNFS_BLOCK_INVALID_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) bex->soff = iomap.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case IOMAP_HOLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (seg->iomode == IOMODE_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) bex->es = PNFS_BLOCK_NONE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case IOMAP_DELALLOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) goto out_layoutunavailable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) bex->foff = iomap.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) bex->len = iomap.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) seg->offset = iomap.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) seg->length = iomap.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) seg->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return nfserrno(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) out_layoutunavailable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) seg->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return nfserr_layoutunavailable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static __be32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct iomap *iomaps, int nr_iomaps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) loff_t new_size = lcp->lc_last_wr + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct iattr iattr = { .ia_valid = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) timespec64_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) lcp->lc_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (new_size > i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) iattr.ia_valid |= ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) iattr.ia_size = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) nr_iomaps, &iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) kfree(iomaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return nfserrno(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #ifdef CONFIG_NFSD_BLOCKLAYOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) nfsd4_block_get_device_info_simple(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct nfsd4_getdeviceinfo *gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct pnfs_block_deviceaddr *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct pnfs_block_volume *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) sizeof(struct pnfs_block_volume), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) gdp->gd_device = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) dev->nr_volumes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) b = &dev->volumes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) b->type = PNFS_BLOCK_VOLUME_SIMPLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) &b->simple.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static __be32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct svc_rqst *rqstp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct nfs4_client *clp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct nfsd4_getdeviceinfo *gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (bdev_is_partition(sb->s_bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return nfserr_inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static __be32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) nfsd4_block_proc_layoutcommit(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct nfsd4_layoutcommit *lcp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct iomap *iomaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int nr_iomaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) lcp->lc_up_len, &iomaps, i_blocksize(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (nr_iomaps < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return nfserrno(nr_iomaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) const struct nfsd4_layout_ops bl_layout_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * Pretend that we send notification to the client. This is a blatant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * lie to force recent Linux clients to cache our device IDs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * We rarely ever change the device ID, so the harm of leaking deviceids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * in this regard, but I filed errata 4119 for this a while ago, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * hopefully the Linux client will eventually start caching deviceids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * without this again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .notify_types =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .proc_layoutget = nfsd4_block_proc_layoutget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .encode_layoutget = nfsd4_block_encode_layoutget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #endif /* CONFIG_NFSD_BLOCKLAYOUT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #ifdef CONFIG_NFSD_SCSILAYOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int nfsd4_scsi_identify_device(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct pnfs_block_volume *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct request_queue *q = bdev->bd_disk->queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct scsi_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * The allocation length (passed in bytes 3 and 4 of the INQUIRY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * command descriptor block) specifies the number of bytes that have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * been allocated for the data-in buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * 252 is the highest one-byte value that is a multiple of 4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * 65532 is the highest two-byte value that is a multiple of 4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) size_t bufflen = 252, maxlen = 65532, len, id_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) u8 *buf, *d, type, assoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int retries = 1, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (WARN_ON_ONCE(!blk_queue_scsi_passthrough(q)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) buf = kzalloc(bufflen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (IS_ERR(rq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) goto out_free_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) req = scsi_req(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) goto out_put_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) req->cmd[0] = INQUIRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) req->cmd[1] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) req->cmd[2] = 0x83;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) req->cmd[3] = bufflen >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) req->cmd[4] = bufflen & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) req->cmd_len = COMMAND_SIZE(INQUIRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) blk_execute_rq(rq->q, NULL, rq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (req->result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) req->result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) goto out_put_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) len = (buf[2] << 8) + buf[3] + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (len > bufflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (len <= maxlen && retries--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) blk_put_request(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) bufflen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) goto out_put_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) d = buf + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) for (d = buf + 4; d < buf + len; d += id_len + 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) id_len = d[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) type = d[1] & 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) assoc = (d[1] >> 4) & 0x3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * We only care about a EUI-64 and NAA designator types
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * with LU association.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (assoc != 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (type != 0x02 && type != 0x03)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (id_len != 8 && id_len != 12 && id_len != 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) b->scsi.code_set = PS_CODE_SET_BINARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) b->scsi.designator_type = type == 0x02 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) b->scsi.designator_len = id_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) memcpy(b->scsi.designator, d + 4, id_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * If we found a 8 or 12 byte descriptor continue on to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * see if a 16 byte one is available. If we find a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * 16 byte descriptor we're done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (id_len == 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) out_put_request:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) blk_put_request(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) out_free_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #define NFSD_MDS_PR_KEY 0x0100000000000000ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * We use the client ID as a unique key for the reservations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * This allows us to easily fence a client when recalls fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) nfsd4_block_get_device_info_scsi(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct nfs4_client *clp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct nfsd4_getdeviceinfo *gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct pnfs_block_deviceaddr *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct pnfs_block_volume *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) const struct pr_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) sizeof(struct pnfs_block_volume), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) gdp->gd_device = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) dev->nr_volumes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) b = &dev->volumes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) b->type = PNFS_BLOCK_VOLUME_SCSI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) error = nfsd4_scsi_identify_device(sb->s_bdev, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ops = sb->s_bdev->bd_disk->fops->pr_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) pr_err("pNFS: device %s does not support PRs.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) pr_err("pNFS: failed to register key for device %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) pr_err("pNFS: failed to reserve device %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static __be32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct svc_rqst *rqstp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct nfs4_client *clp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct nfsd4_getdeviceinfo *gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (bdev_is_partition(sb->s_bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return nfserr_inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static __be32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) nfsd4_scsi_proc_layoutcommit(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) struct nfsd4_layoutcommit *lcp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct iomap *iomaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int nr_iomaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) lcp->lc_up_len, &iomaps, i_blocksize(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (nr_iomaps < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return nfserrno(nr_iomaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct nfs4_client *clp = ls->ls_stid.sc_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct block_device *bdev = ls->ls_file->nf_file->f_path.mnt->mnt_sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) nfsd4_scsi_pr_key(clp), 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) const struct nfsd4_layout_ops scsi_layout_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * Pretend that we send notification to the client. This is a blatant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * lie to force recent Linux clients to cache our device IDs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * We rarely ever change the device ID, so the harm of leaking deviceids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * in this regard, but I filed errata 4119 for this a while ago, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * hopefully the Linux client will eventually start caching deviceids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * without this again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) .notify_types =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) .proc_layoutget = nfsd4_block_proc_layoutget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) .encode_layoutget = nfsd4_block_encode_layoutget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) .fence_client = nfsd4_scsi_fence_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) #endif /* CONFIG_NFSD_SCSILAYOUT */