blob: c862c2489df0eba4ece4e8878b9bb8da447472d5 [file] [log] [blame]
Christoph Hellwig8650b8a2015-01-21 11:40:00 +01001/*
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01002 * Copyright (c) 2014-2016 Christoph Hellwig.
Christoph Hellwig8650b8a2015-01-21 11:40:00 +01003 */
4#include <linux/exportfs.h>
Christoph Hellwig199a31c2016-06-21 09:22:39 +10005#include <linux/iomap.h>
Christoph Hellwig8650b8a2015-01-21 11:40:00 +01006#include <linux/genhd.h>
7#include <linux/slab.h>
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01008#include <linux/pr.h>
Christoph Hellwig8650b8a2015-01-21 11:40:00 +01009
10#include <linux/nfsd/debug.h>
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +010011#include <scsi/scsi_proto.h>
12#include <scsi/scsi_common.h>
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010013#include <scsi/scsi_request.h>
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010014
15#include "blocklayoutxdr.h"
16#include "pnfs.h"
17
18#define NFSDDBG_FACILITY NFSDDBG_PNFS
19
20
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010021static __be32
22nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
23 struct nfsd4_layoutget *args)
24{
25 struct nfsd4_layout_seg *seg = &args->lg_seg;
26 struct super_block *sb = inode->i_sb;
Fabian Frederick93407472017-02-27 14:28:32 -080027 u32 block_size = i_blocksize(inode);
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010028 struct pnfs_block_extent *bex;
29 struct iomap iomap;
30 u32 device_generation = 0;
31 int error;
32
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010033 if (seg->offset & (block_size - 1)) {
34 dprintk("pnfsd: I/O misaligned\n");
35 goto out_layoutunavailable;
36 }
37
38 /*
39 * Some clients barf on non-zero block numbers for NONE or INVALID
40 * layouts, so make sure to zero the whole structure.
41 */
42 error = -ENOMEM;
43 bex = kzalloc(sizeof(*bex), GFP_KERNEL);
44 if (!bex)
45 goto out_error;
46 args->lg_content = bex;
47
48 error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
49 &iomap, seg->iomode != IOMODE_READ,
50 &device_generation);
51 if (error) {
52 if (error == -ENXIO)
53 goto out_layoutunavailable;
54 goto out_error;
55 }
56
57 if (iomap.length < args->lg_minlength) {
58 dprintk("pnfsd: extent smaller than minlength\n");
59 goto out_layoutunavailable;
60 }
61
62 switch (iomap.type) {
63 case IOMAP_MAPPED:
64 if (seg->iomode == IOMODE_READ)
65 bex->es = PNFS_BLOCK_READ_DATA;
66 else
67 bex->es = PNFS_BLOCK_READWRITE_DATA;
68 bex->soff = (iomap.blkno << 9);
69 break;
70 case IOMAP_UNWRITTEN:
71 if (seg->iomode & IOMODE_RW) {
72 /*
73 * Crack monkey special case from section 2.3.1.
74 */
75 if (args->lg_minlength == 0) {
76 dprintk("pnfsd: no soup for you!\n");
77 goto out_layoutunavailable;
78 }
79
80 bex->es = PNFS_BLOCK_INVALID_DATA;
81 bex->soff = (iomap.blkno << 9);
82 break;
83 }
84 /*FALLTHRU*/
85 case IOMAP_HOLE:
86 if (seg->iomode == IOMODE_READ) {
87 bex->es = PNFS_BLOCK_NONE_DATA;
88 break;
89 }
90 /*FALLTHRU*/
91 case IOMAP_DELALLOC:
92 default:
93 WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
94 goto out_layoutunavailable;
95 }
96
97 error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
98 if (error)
99 goto out_error;
100 bex->foff = iomap.offset;
101 bex->len = iomap.length;
102
103 seg->offset = iomap.offset;
104 seg->length = iomap.length;
105
Kinglong Mee85369522015-03-22 22:16:40 +0800106 dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100107 return 0;
108
109out_error:
110 seg->length = 0;
111 return nfserrno(error);
112out_layoutunavailable:
113 seg->length = 0;
114 return nfserr_layoutunavailable;
115}
116
117static __be32
Christoph Hellwig368248e2016-03-04 20:46:17 +0100118nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
119 struct iomap *iomaps, int nr_iomaps)
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100120{
121 loff_t new_size = lcp->lc_last_wr + 1;
122 struct iattr iattr = { .ia_valid = 0 };
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100123 int error;
124
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100125 if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
126 timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700127 lcp->lc_mtime = current_time(inode);
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100128 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
Christoph Hellwig368248e2016-03-04 20:46:17 +0100142#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(sizeof(struct pnfs_block_deviceaddr) +
151 sizeof(struct pnfs_block_volume), GFP_KERNEL);
152 if (!dev)
153 return -ENOMEM;
154 gdp->gd_device = dev;
155
156 dev->nr_volumes = 1;
157 b = &dev->volumes[0];
158
159 b->type = PNFS_BLOCK_VOLUME_SIMPLE;
160 b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
161 return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
162 &b->simple.offset);
163}
164
165static __be32
166nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
Tom Haynesd7c920d2016-06-14 13:41:27 -0700167 struct svc_rqst *rqstp,
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100168 struct nfs4_client *clp,
Christoph Hellwig368248e2016-03-04 20:46:17 +0100169 struct nfsd4_getdeviceinfo *gdp)
170{
171 if (sb->s_bdev != sb->s_bdev->bd_contains)
172 return nfserr_inval;
173 return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
174}
175
176static __be32
177nfsd4_block_proc_layoutcommit(struct inode *inode,
178 struct nfsd4_layoutcommit *lcp)
179{
180 struct iomap *iomaps;
181 int nr_iomaps;
182
183 nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
Fabian Frederick93407472017-02-27 14:28:32 -0800184 lcp->lc_up_len, &iomaps, i_blocksize(inode));
Christoph Hellwig368248e2016-03-04 20:46:17 +0100185 if (nr_iomaps < 0)
186 return nfserrno(nr_iomaps);
187
188 return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
189}
190
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100191const struct nfsd4_layout_ops bl_layout_ops = {
Christoph Hellwig40cdc7a2015-04-27 14:50:13 +0200192 /*
193 * Pretend that we send notification to the client. This is a blatant
194 * lie to force recent Linux clients to cache our device IDs.
195 * We rarely ever change the device ID, so the harm of leaking deviceids
196 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
197 * in this regard, but I filed errata 4119 for this a while ago, and
198 * hopefully the Linux client will eventually start caching deviceids
199 * without this again.
200 */
201 .notify_types =
202 NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100203 .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
204 .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
205 .proc_layoutget = nfsd4_block_proc_layoutget,
206 .encode_layoutget = nfsd4_block_encode_layoutget,
207 .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
208};
Christoph Hellwig368248e2016-03-04 20:46:17 +0100209#endif /* CONFIG_NFSD_BLOCKLAYOUT */
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100210
211#ifdef CONFIG_NFSD_SCSILAYOUT
212static int nfsd4_scsi_identify_device(struct block_device *bdev,
213 struct pnfs_block_volume *b)
214{
215 struct request_queue *q = bdev->bd_disk->queue;
216 struct request *rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100217 struct scsi_request *req;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100218 size_t bufflen = 252, len, id_len;
219 u8 *buf, *d, type, assoc;
220 int error;
221
Bart Van Assche30181fa2017-05-31 14:43:50 -0700222 if (WARN_ON_ONCE(!blk_queue_scsi_passthrough(q)))
223 return -EINVAL;
224
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100225 buf = kzalloc(bufflen, GFP_KERNEL);
226 if (!buf)
227 return -ENOMEM;
228
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100229 rq = blk_get_request(q, REQ_OP_SCSI_IN, GFP_KERNEL);
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100230 if (IS_ERR(rq)) {
231 error = -ENOMEM;
232 goto out_free_buf;
233 }
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100234 req = scsi_req(rq);
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100235
236 error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL);
237 if (error)
238 goto out_put_request;
239
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100240 req->cmd[0] = INQUIRY;
241 req->cmd[1] = 1;
242 req->cmd[2] = 0x83;
243 req->cmd[3] = bufflen >> 8;
244 req->cmd[4] = bufflen & 0xff;
245 req->cmd_len = COMMAND_SIZE(INQUIRY);
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100246
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200247 blk_execute_rq(rq->q, NULL, rq, 1);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200248 if (req->result) {
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100249 pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
Christoph Hellwig17d53632017-04-20 16:03:01 +0200250 req->result);
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200251 error = -EIO;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100252 goto out_put_request;
253 }
254
255 len = (buf[2] << 8) + buf[3] + 4;
256 if (len > bufflen) {
257 pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
258 len);
259 goto out_put_request;
260 }
261
262 d = buf + 4;
263 for (d = buf + 4; d < buf + len; d += id_len + 4) {
264 id_len = d[3];
265 type = d[1] & 0xf;
266 assoc = (d[1] >> 4) & 0x3;
267
268 /*
269 * We only care about a EUI-64 and NAA designator types
270 * with LU association.
271 */
272 if (assoc != 0x00)
273 continue;
274 if (type != 0x02 && type != 0x03)
275 continue;
276 if (id_len != 8 && id_len != 12 && id_len != 16)
277 continue;
278
279 b->scsi.code_set = PS_CODE_SET_BINARY;
280 b->scsi.designator_type = type == 0x02 ?
281 PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA;
282 b->scsi.designator_len = id_len;
283 memcpy(b->scsi.designator, d + 4, id_len);
284
285 /*
286 * If we found a 8 or 12 byte descriptor continue on to
287 * see if a 16 byte one is available. If we find a
288 * 16 byte descriptor we're done.
289 */
290 if (id_len == 16)
291 break;
292 }
293
294out_put_request:
295 blk_put_request(rq);
296out_free_buf:
297 kfree(buf);
298 return error;
299}
300
Geert Uytterhoeveneee93012016-03-25 11:47:11 +0100301#define NFSD_MDS_PR_KEY 0x0100000000000000ULL
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100302
303/*
304 * We use the client ID as a unique key for the reservations.
305 * This allows us to easily fence a client when recalls fail.
306 */
307static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
308{
309 return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
310}
311
312static int
313nfsd4_block_get_device_info_scsi(struct super_block *sb,
314 struct nfs4_client *clp,
315 struct nfsd4_getdeviceinfo *gdp)
316{
317 struct pnfs_block_deviceaddr *dev;
318 struct pnfs_block_volume *b;
319 const struct pr_ops *ops;
320 int error;
321
322 dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
323 sizeof(struct pnfs_block_volume), GFP_KERNEL);
324 if (!dev)
325 return -ENOMEM;
326 gdp->gd_device = dev;
327
328 dev->nr_volumes = 1;
329 b = &dev->volumes[0];
330
331 b->type = PNFS_BLOCK_VOLUME_SCSI;
332 b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
333
334 error = nfsd4_scsi_identify_device(sb->s_bdev, b);
335 if (error)
336 return error;
337
338 ops = sb->s_bdev->bd_disk->fops->pr_ops;
339 if (!ops) {
340 pr_err("pNFS: device %s does not support PRs.\n",
341 sb->s_id);
342 return -EINVAL;
343 }
344
345 error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
346 if (error) {
347 pr_err("pNFS: failed to register key for device %s.\n",
348 sb->s_id);
349 return -EINVAL;
350 }
351
352 error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
353 PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
354 if (error) {
355 pr_err("pNFS: failed to reserve device %s.\n",
356 sb->s_id);
357 return -EINVAL;
358 }
359
360 return 0;
361}
362
363static __be32
364nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
Tom Haynesd7c920d2016-06-14 13:41:27 -0700365 struct svc_rqst *rqstp,
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100366 struct nfs4_client *clp,
367 struct nfsd4_getdeviceinfo *gdp)
368{
369 if (sb->s_bdev != sb->s_bdev->bd_contains)
370 return nfserr_inval;
371 return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
372}
373static __be32
374nfsd4_scsi_proc_layoutcommit(struct inode *inode,
375 struct nfsd4_layoutcommit *lcp)
376{
377 struct iomap *iomaps;
378 int nr_iomaps;
379
380 nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
Fabian Frederick93407472017-02-27 14:28:32 -0800381 lcp->lc_up_len, &iomaps, i_blocksize(inode));
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100382 if (nr_iomaps < 0)
383 return nfserrno(nr_iomaps);
384
385 return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
386}
387
388static void
389nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
390{
391 struct nfs4_client *clp = ls->ls_stid.sc_client;
392 struct block_device *bdev = ls->ls_file->f_path.mnt->mnt_sb->s_bdev;
393
394 bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
395 nfsd4_scsi_pr_key(clp), 0, true);
396}
397
398const struct nfsd4_layout_ops scsi_layout_ops = {
399 /*
400 * Pretend that we send notification to the client. This is a blatant
401 * lie to force recent Linux clients to cache our device IDs.
402 * We rarely ever change the device ID, so the harm of leaking deviceids
403 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
404 * in this regard, but I filed errata 4119 for this a while ago, and
405 * hopefully the Linux client will eventually start caching deviceids
406 * without this again.
407 */
408 .notify_types =
409 NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
410 .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo,
411 .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
412 .proc_layoutget = nfsd4_block_proc_layoutget,
413 .encode_layoutget = nfsd4_block_encode_layoutget,
414 .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit,
415 .fence_client = nfsd4_scsi_fence_client,
416};
417#endif /* CONFIG_NFSD_SCSILAYOUT */