1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014-2016 Christoph Hellwig.
5 #include <linux/exportfs.h>
6 #include <linux/iomap.h>
7 #include <linux/genhd.h>
8 #include <linux/slab.h>
11 #include <linux/nfsd/debug.h>
12 #include <scsi/scsi_proto.h>
13 #include <scsi/scsi_common.h>
14 #include <scsi/scsi_request.h>
16 #include "blocklayoutxdr.h"
19 #define NFSDDBG_FACILITY NFSDDBG_PNFS
23 nfsd4_block_proc_layoutget(struct inode
*inode
, const struct svc_fh
*fhp
,
24 struct nfsd4_layoutget
*args
)
26 struct nfsd4_layout_seg
*seg
= &args
->lg_seg
;
27 struct super_block
*sb
= inode
->i_sb
;
28 u32 block_size
= i_blocksize(inode
);
29 struct pnfs_block_extent
*bex
;
31 u32 device_generation
= 0;
34 if (seg
->offset
& (block_size
- 1)) {
35 dprintk("pnfsd: I/O misaligned\n");
36 goto out_layoutunavailable
;
40 * Some clients barf on non-zero block numbers for NONE or INVALID
41 * layouts, so make sure to zero the whole structure.
44 bex
= kzalloc(sizeof(*bex
), GFP_KERNEL
);
47 args
->lg_content
= bex
;
49 error
= sb
->s_export_op
->map_blocks(inode
, seg
->offset
, seg
->length
,
50 &iomap
, seg
->iomode
!= IOMODE_READ
,
54 goto out_layoutunavailable
;
58 if (iomap
.length
< args
->lg_minlength
) {
59 dprintk("pnfsd: extent smaller than minlength\n");
60 goto out_layoutunavailable
;
65 if (seg
->iomode
== IOMODE_READ
)
66 bex
->es
= PNFS_BLOCK_READ_DATA
;
68 bex
->es
= PNFS_BLOCK_READWRITE_DATA
;
69 bex
->soff
= iomap
.addr
;
72 if (seg
->iomode
& IOMODE_RW
) {
74 * Crack monkey special case from section 2.3.1.
76 if (args
->lg_minlength
== 0) {
77 dprintk("pnfsd: no soup for you!\n");
78 goto out_layoutunavailable
;
81 bex
->es
= PNFS_BLOCK_INVALID_DATA
;
82 bex
->soff
= iomap
.addr
;
87 if (seg
->iomode
== IOMODE_READ
) {
88 bex
->es
= PNFS_BLOCK_NONE_DATA
;
94 WARN(1, "pnfsd: filesystem returned %d extent\n", iomap
.type
);
95 goto out_layoutunavailable
;
98 error
= nfsd4_set_deviceid(&bex
->vol_id
, fhp
, device_generation
);
101 bex
->foff
= iomap
.offset
;
102 bex
->len
= iomap
.length
;
104 seg
->offset
= iomap
.offset
;
105 seg
->length
= iomap
.length
;
107 dprintk("GET: 0x%llx:0x%llx %d\n", bex
->foff
, bex
->len
, bex
->es
);
112 return nfserrno(error
);
113 out_layoutunavailable
:
115 return nfserr_layoutunavailable
;
119 nfsd4_block_commit_blocks(struct inode
*inode
, struct nfsd4_layoutcommit
*lcp
,
120 struct iomap
*iomaps
, int nr_iomaps
)
122 loff_t new_size
= lcp
->lc_last_wr
+ 1;
123 struct iattr iattr
= { .ia_valid
= 0 };
126 if (lcp
->lc_mtime
.tv_nsec
== UTIME_NOW
||
127 timespec_compare(&lcp
->lc_mtime
, &inode
->i_mtime
) < 0)
128 lcp
->lc_mtime
= current_time(inode
);
129 iattr
.ia_valid
|= ATTR_ATIME
| ATTR_CTIME
| ATTR_MTIME
;
130 iattr
.ia_atime
= iattr
.ia_ctime
= iattr
.ia_mtime
= lcp
->lc_mtime
;
132 if (new_size
> i_size_read(inode
)) {
133 iattr
.ia_valid
|= ATTR_SIZE
;
134 iattr
.ia_size
= new_size
;
137 error
= inode
->i_sb
->s_export_op
->commit_blocks(inode
, iomaps
,
140 return nfserrno(error
);
143 #ifdef CONFIG_NFSD_BLOCKLAYOUT
145 nfsd4_block_get_device_info_simple(struct super_block
*sb
,
146 struct nfsd4_getdeviceinfo
*gdp
)
148 struct pnfs_block_deviceaddr
*dev
;
149 struct pnfs_block_volume
*b
;
151 dev
= kzalloc(sizeof(struct pnfs_block_deviceaddr
) +
152 sizeof(struct pnfs_block_volume
), GFP_KERNEL
);
155 gdp
->gd_device
= dev
;
158 b
= &dev
->volumes
[0];
160 b
->type
= PNFS_BLOCK_VOLUME_SIMPLE
;
161 b
->simple
.sig_len
= PNFS_BLOCK_UUID_LEN
;
162 return sb
->s_export_op
->get_uuid(sb
, b
->simple
.sig
, &b
->simple
.sig_len
,
167 nfsd4_block_proc_getdeviceinfo(struct super_block
*sb
,
168 struct svc_rqst
*rqstp
,
169 struct nfs4_client
*clp
,
170 struct nfsd4_getdeviceinfo
*gdp
)
172 if (sb
->s_bdev
!= sb
->s_bdev
->bd_contains
)
174 return nfserrno(nfsd4_block_get_device_info_simple(sb
, gdp
));
178 nfsd4_block_proc_layoutcommit(struct inode
*inode
,
179 struct nfsd4_layoutcommit
*lcp
)
181 struct iomap
*iomaps
;
184 nr_iomaps
= nfsd4_block_decode_layoutupdate(lcp
->lc_up_layout
,
185 lcp
->lc_up_len
, &iomaps
, i_blocksize(inode
));
187 return nfserrno(nr_iomaps
);
189 return nfsd4_block_commit_blocks(inode
, lcp
, iomaps
, nr_iomaps
);
192 const struct nfsd4_layout_ops bl_layout_ops
= {
194 * Pretend that we send notification to the client. This is a blatant
195 * lie to force recent Linux clients to cache our device IDs.
196 * We rarely ever change the device ID, so the harm of leaking deviceids
197 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
198 * in this regard, but I filed errata 4119 for this a while ago, and
199 * hopefully the Linux client will eventually start caching deviceids
200 * without this again.
203 NOTIFY_DEVICEID4_DELETE
| NOTIFY_DEVICEID4_CHANGE
,
204 .proc_getdeviceinfo
= nfsd4_block_proc_getdeviceinfo
,
205 .encode_getdeviceinfo
= nfsd4_block_encode_getdeviceinfo
,
206 .proc_layoutget
= nfsd4_block_proc_layoutget
,
207 .encode_layoutget
= nfsd4_block_encode_layoutget
,
208 .proc_layoutcommit
= nfsd4_block_proc_layoutcommit
,
210 #endif /* CONFIG_NFSD_BLOCKLAYOUT */
212 #ifdef CONFIG_NFSD_SCSILAYOUT
213 static int nfsd4_scsi_identify_device(struct block_device
*bdev
,
214 struct pnfs_block_volume
*b
)
216 struct request_queue
*q
= bdev
->bd_disk
->queue
;
218 struct scsi_request
*req
;
219 size_t bufflen
= 252, len
, id_len
;
220 u8
*buf
, *d
, type
, assoc
;
223 if (WARN_ON_ONCE(!blk_queue_scsi_passthrough(q
)))
226 buf
= kzalloc(bufflen
, GFP_KERNEL
);
230 rq
= blk_get_request(q
, REQ_OP_SCSI_IN
, GFP_KERNEL
);
237 error
= blk_rq_map_kern(q
, rq
, buf
, bufflen
, GFP_KERNEL
);
239 goto out_put_request
;
241 req
->cmd
[0] = INQUIRY
;
244 req
->cmd
[3] = bufflen
>> 8;
245 req
->cmd
[4] = bufflen
& 0xff;
246 req
->cmd_len
= COMMAND_SIZE(INQUIRY
);
248 blk_execute_rq(rq
->q
, NULL
, rq
, 1);
250 pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
253 goto out_put_request
;
256 len
= (buf
[2] << 8) + buf
[3] + 4;
258 pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
260 goto out_put_request
;
264 for (d
= buf
+ 4; d
< buf
+ len
; d
+= id_len
+ 4) {
267 assoc
= (d
[1] >> 4) & 0x3;
270 * We only care about a EUI-64 and NAA designator types
271 * with LU association.
275 if (type
!= 0x02 && type
!= 0x03)
277 if (id_len
!= 8 && id_len
!= 12 && id_len
!= 16)
280 b
->scsi
.code_set
= PS_CODE_SET_BINARY
;
281 b
->scsi
.designator_type
= type
== 0x02 ?
282 PS_DESIGNATOR_EUI64
: PS_DESIGNATOR_NAA
;
283 b
->scsi
.designator_len
= id_len
;
284 memcpy(b
->scsi
.designator
, d
+ 4, id_len
);
287 * If we found a 8 or 12 byte descriptor continue on to
288 * see if a 16 byte one is available. If we find a
289 * 16 byte descriptor we're done.
302 #define NFSD_MDS_PR_KEY 0x0100000000000000ULL
305 * We use the client ID as a unique key for the reservations.
306 * This allows us to easily fence a client when recalls fail.
308 static u64
nfsd4_scsi_pr_key(struct nfs4_client
*clp
)
310 return ((u64
)clp
->cl_clientid
.cl_boot
<< 32) | clp
->cl_clientid
.cl_id
;
314 nfsd4_block_get_device_info_scsi(struct super_block
*sb
,
315 struct nfs4_client
*clp
,
316 struct nfsd4_getdeviceinfo
*gdp
)
318 struct pnfs_block_deviceaddr
*dev
;
319 struct pnfs_block_volume
*b
;
320 const struct pr_ops
*ops
;
323 dev
= kzalloc(sizeof(struct pnfs_block_deviceaddr
) +
324 sizeof(struct pnfs_block_volume
), GFP_KERNEL
);
327 gdp
->gd_device
= dev
;
330 b
= &dev
->volumes
[0];
332 b
->type
= PNFS_BLOCK_VOLUME_SCSI
;
333 b
->scsi
.pr_key
= nfsd4_scsi_pr_key(clp
);
335 error
= nfsd4_scsi_identify_device(sb
->s_bdev
, b
);
339 ops
= sb
->s_bdev
->bd_disk
->fops
->pr_ops
;
341 pr_err("pNFS: device %s does not support PRs.\n",
346 error
= ops
->pr_register(sb
->s_bdev
, 0, NFSD_MDS_PR_KEY
, true);
348 pr_err("pNFS: failed to register key for device %s.\n",
353 error
= ops
->pr_reserve(sb
->s_bdev
, NFSD_MDS_PR_KEY
,
354 PR_EXCLUSIVE_ACCESS_REG_ONLY
, 0);
356 pr_err("pNFS: failed to reserve device %s.\n",
365 nfsd4_scsi_proc_getdeviceinfo(struct super_block
*sb
,
366 struct svc_rqst
*rqstp
,
367 struct nfs4_client
*clp
,
368 struct nfsd4_getdeviceinfo
*gdp
)
370 if (sb
->s_bdev
!= sb
->s_bdev
->bd_contains
)
372 return nfserrno(nfsd4_block_get_device_info_scsi(sb
, clp
, gdp
));
375 nfsd4_scsi_proc_layoutcommit(struct inode
*inode
,
376 struct nfsd4_layoutcommit
*lcp
)
378 struct iomap
*iomaps
;
381 nr_iomaps
= nfsd4_scsi_decode_layoutupdate(lcp
->lc_up_layout
,
382 lcp
->lc_up_len
, &iomaps
, i_blocksize(inode
));
384 return nfserrno(nr_iomaps
);
386 return nfsd4_block_commit_blocks(inode
, lcp
, iomaps
, nr_iomaps
);
390 nfsd4_scsi_fence_client(struct nfs4_layout_stateid
*ls
)
392 struct nfs4_client
*clp
= ls
->ls_stid
.sc_client
;
393 struct block_device
*bdev
= ls
->ls_file
->f_path
.mnt
->mnt_sb
->s_bdev
;
395 bdev
->bd_disk
->fops
->pr_ops
->pr_preempt(bdev
, NFSD_MDS_PR_KEY
,
396 nfsd4_scsi_pr_key(clp
), 0, true);
399 const struct nfsd4_layout_ops scsi_layout_ops
= {
401 * Pretend that we send notification to the client. This is a blatant
402 * lie to force recent Linux clients to cache our device IDs.
403 * We rarely ever change the device ID, so the harm of leaking deviceids
404 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
405 * in this regard, but I filed errata 4119 for this a while ago, and
406 * hopefully the Linux client will eventually start caching deviceids
407 * without this again.
410 NOTIFY_DEVICEID4_DELETE
| NOTIFY_DEVICEID4_CHANGE
,
411 .proc_getdeviceinfo
= nfsd4_scsi_proc_getdeviceinfo
,
412 .encode_getdeviceinfo
= nfsd4_block_encode_getdeviceinfo
,
413 .proc_layoutget
= nfsd4_block_proc_layoutget
,
414 .encode_layoutget
= nfsd4_block_encode_layoutget
,
415 .proc_layoutcommit
= nfsd4_scsi_proc_layoutcommit
,
416 .fence_client
= nfsd4_scsi_fence_client
,
418 #endif /* CONFIG_NFSD_SCSILAYOUT */