Linux Audio

Check our new training course

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