2 * Copyright (c) 2014-2016 Christoph Hellwig.
4 #include <linux/exportfs.h>
5 #include <linux/iomap.h>
6 #include <linux/genhd.h>
7 #include <linux/slab.h>
10 #include <linux/nfsd/debug.h>
11 #include <scsi/scsi_proto.h>
12 #include <scsi/scsi_common.h>
14 #include "blocklayoutxdr.h"
17 #define NFSDDBG_FACILITY NFSDDBG_PNFS
21 nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
22 struct nfsd4_layoutget *args)
24 struct nfsd4_layout_seg *seg = &args->lg_seg;
25 struct super_block *sb = inode->i_sb;
26 u32 block_size = (1 << inode->i_blkbits);
27 struct pnfs_block_extent *bex;
29 u32 device_generation = 0;
32 if (seg->offset & (block_size - 1)) {
33 dprintk("pnfsd: I/O misaligned\n");
34 goto out_layoutunavailable;
38 * Some clients barf on non-zero block numbers for NONE or INVALID
39 * layouts, so make sure to zero the whole structure.
42 bex = kzalloc(sizeof(*bex), GFP_KERNEL);
45 args->lg_content = bex;
47 error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
48 &iomap, seg->iomode != IOMODE_READ,
52 goto out_layoutunavailable;
56 if (iomap.length < args->lg_minlength) {
57 dprintk("pnfsd: extent smaller than minlength\n");
58 goto out_layoutunavailable;
63 if (seg->iomode == IOMODE_READ)
64 bex->es = PNFS_BLOCK_READ_DATA;
66 bex->es = PNFS_BLOCK_READWRITE_DATA;
67 bex->soff = (iomap.blkno << 9);
70 if (seg->iomode & IOMODE_RW) {
72 * Crack monkey special case from section 2.3.1.
74 if (args->lg_minlength == 0) {
75 dprintk("pnfsd: no soup for you!\n");
76 goto out_layoutunavailable;
79 bex->es = PNFS_BLOCK_INVALID_DATA;
80 bex->soff = (iomap.blkno << 9);
85 if (seg->iomode == IOMODE_READ) {
86 bex->es = PNFS_BLOCK_NONE_DATA;
92 WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
93 goto out_layoutunavailable;
96 error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
99 bex->foff = iomap.offset;
100 bex->len = iomap.length;
102 seg->offset = iomap.offset;
103 seg->length = iomap.length;
105 dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
110 return nfserrno(error);
111 out_layoutunavailable:
113 return nfserr_layoutunavailable;
117 nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
118 struct iomap *iomaps, int nr_iomaps)
120 loff_t new_size = lcp->lc_last_wr + 1;
121 struct iattr iattr = { .ia_valid = 0 };
124 if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
125 timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
126 lcp->lc_mtime = current_time(inode);
127 iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
128 iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
130 if (new_size > i_size_read(inode)) {
131 iattr.ia_valid |= ATTR_SIZE;
132 iattr.ia_size = new_size;
135 error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
138 return nfserrno(error);
141 #ifdef CONFIG_NFSD_BLOCKLAYOUT
143 nfsd4_block_get_device_info_simple(struct super_block *sb,
144 struct nfsd4_getdeviceinfo *gdp)
146 struct pnfs_block_deviceaddr *dev;
147 struct pnfs_block_volume *b;
149 dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
150 sizeof(struct pnfs_block_volume), GFP_KERNEL);
153 gdp->gd_device = dev;
156 b = &dev->volumes[0];
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,
165 nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
166 struct svc_rqst *rqstp,
167 struct nfs4_client *clp,
168 struct nfsd4_getdeviceinfo *gdp)
170 if (sb->s_bdev != sb->s_bdev->bd_contains)
172 return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
176 nfsd4_block_proc_layoutcommit(struct inode *inode,
177 struct nfsd4_layoutcommit *lcp)
179 struct iomap *iomaps;
182 nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
183 lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits);
185 return nfserrno(nr_iomaps);
187 return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
190 const struct nfsd4_layout_ops bl_layout_ops = {
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.
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,
208 #endif /* CONFIG_NFSD_BLOCKLAYOUT */
210 #ifdef CONFIG_NFSD_SCSILAYOUT
211 static int nfsd4_scsi_identify_device(struct block_device *bdev,
212 struct pnfs_block_volume *b)
214 struct request_queue *q = bdev->bd_disk->queue;
216 size_t bufflen = 252, len, id_len;
217 u8 *buf, *d, type, assoc;
220 buf = kzalloc(bufflen, GFP_KERNEL);
224 rq = blk_get_request(q, READ, GFP_KERNEL);
229 blk_rq_set_block_pc(rq);
231 error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL);
233 goto out_put_request;
235 rq->cmd[0] = INQUIRY;
238 rq->cmd[3] = bufflen >> 8;
239 rq->cmd[4] = bufflen & 0xff;
240 rq->cmd_len = COMMAND_SIZE(INQUIRY);
242 error = blk_execute_rq(rq->q, NULL, rq, 1);
244 pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
246 goto out_put_request;
249 len = (buf[2] << 8) + buf[3] + 4;
251 pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
253 goto out_put_request;
257 for (d = buf + 4; d < buf + len; d += id_len + 4) {
260 assoc = (d[1] >> 4) & 0x3;
263 * We only care about a EUI-64 and NAA designator types
264 * with LU association.
268 if (type != 0x02 && type != 0x03)
270 if (id_len != 8 && id_len != 12 && id_len != 16)
273 b->scsi.code_set = PS_CODE_SET_BINARY;
274 b->scsi.designator_type = type == 0x02 ?
275 PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA;
276 b->scsi.designator_len = id_len;
277 memcpy(b->scsi.designator, d + 4, id_len);
280 * If we found a 8 or 12 byte descriptor continue on to
281 * see if a 16 byte one is available. If we find a
282 * 16 byte descriptor we're done.
295 #define NFSD_MDS_PR_KEY 0x0100000000000000ULL
298 * We use the client ID as a unique key for the reservations.
299 * This allows us to easily fence a client when recalls fail.
301 static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
303 return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
307 nfsd4_block_get_device_info_scsi(struct super_block *sb,
308 struct nfs4_client *clp,
309 struct nfsd4_getdeviceinfo *gdp)
311 struct pnfs_block_deviceaddr *dev;
312 struct pnfs_block_volume *b;
313 const struct pr_ops *ops;
316 dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
317 sizeof(struct pnfs_block_volume), GFP_KERNEL);
320 gdp->gd_device = dev;
323 b = &dev->volumes[0];
325 b->type = PNFS_BLOCK_VOLUME_SCSI;
326 b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
328 error = nfsd4_scsi_identify_device(sb->s_bdev, b);
332 ops = sb->s_bdev->bd_disk->fops->pr_ops;
334 pr_err("pNFS: device %s does not support PRs.\n",
339 error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
341 pr_err("pNFS: failed to register key for device %s.\n",
346 error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
347 PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
349 pr_err("pNFS: failed to reserve device %s.\n",
358 nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
359 struct svc_rqst *rqstp,
360 struct nfs4_client *clp,
361 struct nfsd4_getdeviceinfo *gdp)
363 if (sb->s_bdev != sb->s_bdev->bd_contains)
365 return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
368 nfsd4_scsi_proc_layoutcommit(struct inode *inode,
369 struct nfsd4_layoutcommit *lcp)
371 struct iomap *iomaps;
374 nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
375 lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits);
377 return nfserrno(nr_iomaps);
379 return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
383 nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
385 struct nfs4_client *clp = ls->ls_stid.sc_client;
386 struct block_device *bdev = ls->ls_file->f_path.mnt->mnt_sb->s_bdev;
388 bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
389 nfsd4_scsi_pr_key(clp), 0, true);
392 const struct nfsd4_layout_ops scsi_layout_ops = {
394 * Pretend that we send notification to the client. This is a blatant
395 * lie to force recent Linux clients to cache our device IDs.
396 * We rarely ever change the device ID, so the harm of leaking deviceids
397 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
398 * in this regard, but I filed errata 4119 for this a while ago, and
399 * hopefully the Linux client will eventually start caching deviceids
400 * without this again.
403 NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
404 .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo,
405 .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
406 .proc_layoutget = nfsd4_block_proc_layoutget,
407 .encode_layoutget = nfsd4_block_encode_layoutget,
408 .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit,
409 .fence_client = nfsd4_scsi_fence_client,
411 #endif /* CONFIG_NFSD_SCSILAYOUT */