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