]>
Commit | Line | Data |
---|---|---|
8650b8a0 | 1 | /* |
f99d4fbd | 2 | * Copyright (c) 2014-2016 Christoph Hellwig. |
8650b8a0 CH |
3 | */ |
4 | #include <linux/exportfs.h> | |
199a31c6 | 5 | #include <linux/iomap.h> |
8650b8a0 CH |
6 | #include <linux/genhd.h> |
7 | #include <linux/slab.h> | |
f99d4fbd | 8 | #include <linux/pr.h> |
8650b8a0 CH |
9 | |
10 | #include <linux/nfsd/debug.h> | |
f99d4fbd CH |
11 | #include <scsi/scsi_proto.h> |
12 | #include <scsi/scsi_common.h> | |
82ed4db4 | 13 | #include <scsi/scsi_request.h> |
8650b8a0 CH |
14 | |
15 | #include "blocklayoutxdr.h" | |
16 | #include "pnfs.h" | |
17 | ||
18 | #define NFSDDBG_FACILITY NFSDDBG_PNFS | |
19 | ||
20 | ||
8650b8a0 CH |
21 | static __be32 |
22 | nfsd4_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; | |
27 | u32 block_size = (1 << inode->i_blkbits); | |
28 | struct pnfs_block_extent *bex; | |
29 | struct iomap iomap; | |
30 | u32 device_generation = 0; | |
31 | int error; | |
32 | ||
8650b8a0 CH |
33 | 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 | ||
85369523 | 106 | dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es); |
8650b8a0 CH |
107 | return 0; |
108 | ||
109 | out_error: | |
110 | seg->length = 0; | |
111 | return nfserrno(error); | |
112 | out_layoutunavailable: | |
113 | seg->length = 0; | |
114 | return nfserr_layoutunavailable; | |
115 | } | |
116 | ||
117 | static __be32 | |
368248ee CH |
118 | nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp, |
119 | struct iomap *iomaps, int nr_iomaps) | |
8650b8a0 CH |
120 | { |
121 | loff_t new_size = lcp->lc_last_wr + 1; | |
122 | struct iattr iattr = { .ia_valid = 0 }; | |
8650b8a0 CH |
123 | int error; |
124 | ||
8650b8a0 CH |
125 | if (lcp->lc_mtime.tv_nsec == UTIME_NOW || |
126 | timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0) | |
c2050a45 | 127 | lcp->lc_mtime = current_time(inode); |
8650b8a0 CH |
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 | ||
368248ee CH |
142 | #ifdef CONFIG_NFSD_BLOCKLAYOUT |
143 | static int | |
144 | nfsd4_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 | ||
165 | static __be32 | |
166 | nfsd4_block_proc_getdeviceinfo(struct super_block *sb, | |
d7c920d1 | 167 | struct svc_rqst *rqstp, |
f99d4fbd | 168 | struct nfs4_client *clp, |
368248ee CH |
169 | 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 | ||
176 | static __be32 | |
177 | nfsd4_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, | |
184 | lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits); | |
185 | if (nr_iomaps < 0) | |
186 | return nfserrno(nr_iomaps); | |
187 | ||
188 | return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps); | |
189 | } | |
190 | ||
8650b8a0 | 191 | const struct nfsd4_layout_ops bl_layout_ops = { |
40cdc7a5 CH |
192 | /* |
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, | |
8650b8a0 CH |
203 | .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 | }; | |
368248ee | 209 | #endif /* CONFIG_NFSD_BLOCKLAYOUT */ |
f99d4fbd CH |
210 | |
211 | #ifdef CONFIG_NFSD_SCSILAYOUT | |
212 | static 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; | |
82ed4db4 | 217 | struct scsi_request *req; |
f99d4fbd CH |
218 | size_t bufflen = 252, len, id_len; |
219 | u8 *buf, *d, type, assoc; | |
220 | int error; | |
221 | ||
222 | buf = kzalloc(bufflen, GFP_KERNEL); | |
223 | if (!buf) | |
224 | return -ENOMEM; | |
225 | ||
226 | rq = blk_get_request(q, READ, GFP_KERNEL); | |
227 | if (IS_ERR(rq)) { | |
228 | error = -ENOMEM; | |
229 | goto out_free_buf; | |
230 | } | |
82ed4db4 CH |
231 | req = scsi_req(rq); |
232 | scsi_req_init(rq); | |
f99d4fbd CH |
233 | |
234 | error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL); | |
235 | if (error) | |
236 | goto out_put_request; | |
237 | ||
82ed4db4 CH |
238 | req->cmd[0] = INQUIRY; |
239 | req->cmd[1] = 1; | |
240 | req->cmd[2] = 0x83; | |
241 | req->cmd[3] = bufflen >> 8; | |
242 | req->cmd[4] = bufflen & 0xff; | |
243 | req->cmd_len = COMMAND_SIZE(INQUIRY); | |
f99d4fbd CH |
244 | |
245 | error = blk_execute_rq(rq->q, NULL, rq, 1); | |
246 | if (error) { | |
247 | pr_err("pNFS: INQUIRY 0x83 failed with: %x\n", | |
248 | rq->errors); | |
249 | goto out_put_request; | |
250 | } | |
251 | ||
252 | len = (buf[2] << 8) + buf[3] + 4; | |
253 | if (len > bufflen) { | |
254 | pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n", | |
255 | len); | |
256 | goto out_put_request; | |
257 | } | |
258 | ||
259 | d = buf + 4; | |
260 | for (d = buf + 4; d < buf + len; d += id_len + 4) { | |
261 | id_len = d[3]; | |
262 | type = d[1] & 0xf; | |
263 | assoc = (d[1] >> 4) & 0x3; | |
264 | ||
265 | /* | |
266 | * We only care about a EUI-64 and NAA designator types | |
267 | * with LU association. | |
268 | */ | |
269 | if (assoc != 0x00) | |
270 | continue; | |
271 | if (type != 0x02 && type != 0x03) | |
272 | continue; | |
273 | if (id_len != 8 && id_len != 12 && id_len != 16) | |
274 | continue; | |
275 | ||
276 | b->scsi.code_set = PS_CODE_SET_BINARY; | |
277 | b->scsi.designator_type = type == 0x02 ? | |
278 | PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA; | |
279 | b->scsi.designator_len = id_len; | |
280 | memcpy(b->scsi.designator, d + 4, id_len); | |
281 | ||
282 | /* | |
283 | * If we found a 8 or 12 byte descriptor continue on to | |
284 | * see if a 16 byte one is available. If we find a | |
285 | * 16 byte descriptor we're done. | |
286 | */ | |
287 | if (id_len == 16) | |
288 | break; | |
289 | } | |
290 | ||
291 | out_put_request: | |
292 | blk_put_request(rq); | |
293 | out_free_buf: | |
294 | kfree(buf); | |
295 | return error; | |
296 | } | |
297 | ||
eee93016 | 298 | #define NFSD_MDS_PR_KEY 0x0100000000000000ULL |
f99d4fbd CH |
299 | |
300 | /* | |
301 | * We use the client ID as a unique key for the reservations. | |
302 | * This allows us to easily fence a client when recalls fail. | |
303 | */ | |
304 | static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp) | |
305 | { | |
306 | return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id; | |
307 | } | |
308 | ||
309 | static int | |
310 | nfsd4_block_get_device_info_scsi(struct super_block *sb, | |
311 | struct nfs4_client *clp, | |
312 | struct nfsd4_getdeviceinfo *gdp) | |
313 | { | |
314 | struct pnfs_block_deviceaddr *dev; | |
315 | struct pnfs_block_volume *b; | |
316 | const struct pr_ops *ops; | |
317 | int error; | |
318 | ||
319 | dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) + | |
320 | sizeof(struct pnfs_block_volume), GFP_KERNEL); | |
321 | if (!dev) | |
322 | return -ENOMEM; | |
323 | gdp->gd_device = dev; | |
324 | ||
325 | dev->nr_volumes = 1; | |
326 | b = &dev->volumes[0]; | |
327 | ||
328 | b->type = PNFS_BLOCK_VOLUME_SCSI; | |
329 | b->scsi.pr_key = nfsd4_scsi_pr_key(clp); | |
330 | ||
331 | error = nfsd4_scsi_identify_device(sb->s_bdev, b); | |
332 | if (error) | |
333 | return error; | |
334 | ||
335 | ops = sb->s_bdev->bd_disk->fops->pr_ops; | |
336 | if (!ops) { | |
337 | pr_err("pNFS: device %s does not support PRs.\n", | |
338 | sb->s_id); | |
339 | return -EINVAL; | |
340 | } | |
341 | ||
342 | error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true); | |
343 | if (error) { | |
344 | pr_err("pNFS: failed to register key for device %s.\n", | |
345 | sb->s_id); | |
346 | return -EINVAL; | |
347 | } | |
348 | ||
349 | error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY, | |
350 | PR_EXCLUSIVE_ACCESS_REG_ONLY, 0); | |
351 | if (error) { | |
352 | pr_err("pNFS: failed to reserve device %s.\n", | |
353 | sb->s_id); | |
354 | return -EINVAL; | |
355 | } | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | static __be32 | |
361 | nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb, | |
d7c920d1 | 362 | struct svc_rqst *rqstp, |
f99d4fbd CH |
363 | struct nfs4_client *clp, |
364 | struct nfsd4_getdeviceinfo *gdp) | |
365 | { | |
366 | if (sb->s_bdev != sb->s_bdev->bd_contains) | |
367 | return nfserr_inval; | |
368 | return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp)); | |
369 | } | |
370 | static __be32 | |
371 | nfsd4_scsi_proc_layoutcommit(struct inode *inode, | |
372 | struct nfsd4_layoutcommit *lcp) | |
373 | { | |
374 | struct iomap *iomaps; | |
375 | int nr_iomaps; | |
376 | ||
377 | nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout, | |
378 | lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits); | |
379 | if (nr_iomaps < 0) | |
380 | return nfserrno(nr_iomaps); | |
381 | ||
382 | return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps); | |
383 | } | |
384 | ||
385 | static void | |
386 | nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls) | |
387 | { | |
388 | struct nfs4_client *clp = ls->ls_stid.sc_client; | |
389 | struct block_device *bdev = ls->ls_file->f_path.mnt->mnt_sb->s_bdev; | |
390 | ||
391 | bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY, | |
392 | nfsd4_scsi_pr_key(clp), 0, true); | |
393 | } | |
394 | ||
395 | const struct nfsd4_layout_ops scsi_layout_ops = { | |
396 | /* | |
397 | * Pretend that we send notification to the client. This is a blatant | |
398 | * lie to force recent Linux clients to cache our device IDs. | |
399 | * We rarely ever change the device ID, so the harm of leaking deviceids | |
400 | * for a while isn't too bad. Unfortunately RFC5661 is a complete mess | |
401 | * in this regard, but I filed errata 4119 for this a while ago, and | |
402 | * hopefully the Linux client will eventually start caching deviceids | |
403 | * without this again. | |
404 | */ | |
405 | .notify_types = | |
406 | NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE, | |
407 | .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo, | |
408 | .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo, | |
409 | .proc_layoutget = nfsd4_block_proc_layoutget, | |
410 | .encode_layoutget = nfsd4_block_encode_layoutget, | |
411 | .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit, | |
412 | .fence_client = nfsd4_scsi_fence_client, | |
413 | }; | |
414 | #endif /* CONFIG_NFSD_SCSILAYOUT */ |