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/slab.h> 8 #include <linux/pr.h> 9 10 #include <linux/nfsd/debug.h> 11 12 #include "blocklayoutxdr.h" 13 #include "pnfs.h" 14 #include "filecache.h" 15 #include "vfs.h" 16 17 #define NFSDDBG_FACILITY NFSDDBG_PNFS 18 19 20 /* 21 * Get an extent from the file system that starts at offset or below 22 * and may be shorter than the requested length. 23 */ 24 static __be32 25 nfsd4_block_map_extent(struct inode *inode, const struct svc_fh *fhp, 26 u64 offset, u64 length, u32 iomode, u64 minlength, 27 struct pnfs_block_extent *bex) 28 { 29 struct super_block *sb = inode->i_sb; 30 struct iomap iomap; 31 u32 device_generation = 0; 32 int error; 33 34 error = sb->s_export_op->map_blocks(inode, offset, length, &iomap, 35 iomode != IOMODE_READ, &device_generation); 36 if (error) { 37 if (error == -ENXIO) 38 return nfserr_layoutunavailable; 39 return nfserrno(error); 40 } 41 42 switch (iomap.type) { 43 case IOMAP_MAPPED: 44 if (iomode == IOMODE_READ) 45 bex->es = PNFS_BLOCK_READ_DATA; 46 else 47 bex->es = PNFS_BLOCK_READWRITE_DATA; 48 bex->soff = iomap.addr; 49 break; 50 case IOMAP_UNWRITTEN: 51 if (iomode & IOMODE_RW) { 52 /* 53 * Crack monkey special case from section 2.3.1. 54 */ 55 if (minlength == 0) { 56 dprintk("pnfsd: no soup for you!\n"); 57 return nfserr_layoutunavailable; 58 } 59 60 bex->es = PNFS_BLOCK_INVALID_DATA; 61 bex->soff = iomap.addr; 62 break; 63 } 64 fallthrough; 65 case IOMAP_HOLE: 66 if (iomode == IOMODE_READ) { 67 bex->es = PNFS_BLOCK_NONE_DATA; 68 break; 69 } 70 fallthrough; 71 case IOMAP_DELALLOC: 72 default: 73 WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type); 74 return nfserr_layoutunavailable; 75 } 76 77 error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation); 78 if (error) 79 return nfserrno(error); 80 81 bex->foff = iomap.offset; 82 bex->len = iomap.length; 83 return nfs_ok; 84 } 85 86 static __be32 87 nfsd4_block_proc_layoutget(struct svc_rqst *rqstp, struct inode *inode, 88 const struct svc_fh *fhp, struct nfsd4_layoutget *args) 89 { 90 struct nfsd4_layout_seg *seg = &args->lg_seg; 91 struct pnfs_block_layout *bl; 92 struct pnfs_block_extent *first_bex, *last_bex; 93 u64 offset = seg->offset, length = seg->length; 94 u32 i, nr_extents_max, block_size = i_blocksize(inode); 95 __be32 nfserr; 96 97 if (locks_in_grace(SVC_NET(rqstp))) 98 return nfserr_grace; 99 100 nfserr = nfserr_layoutunavailable; 101 if (seg->offset & (block_size - 1)) { 102 dprintk("pnfsd: I/O misaligned\n"); 103 goto out_error; 104 } 105 106 /* 107 * RFC 8881, section 3.3.17: 108 * The layout4 data type defines a layout for a file. 109 * 110 * RFC 8881, section 18.43.3: 111 * The loga_maxcount field specifies the maximum layout size 112 * (in bytes) that the client can handle. If the size of the 113 * layout structure exceeds the size specified by maxcount, 114 * the metadata server will return the NFS4ERR_TOOSMALL error. 115 */ 116 nfserr = nfserr_toosmall; 117 if (args->lg_maxcount < PNFS_BLOCK_LAYOUT4_SIZE + 118 PNFS_BLOCK_EXTENT_SIZE) 119 goto out_error; 120 121 /* 122 * Limit the maximum layout size to avoid allocating 123 * a large buffer on the server for each layout request. 124 */ 125 nr_extents_max = (min(args->lg_maxcount, PAGE_SIZE) - 126 PNFS_BLOCK_LAYOUT4_SIZE) / PNFS_BLOCK_EXTENT_SIZE; 127 128 /* 129 * Some clients barf on non-zero block numbers for NONE or INVALID 130 * layouts, so make sure to zero the whole structure. 131 */ 132 nfserr = nfserrno(-ENOMEM); 133 bl = kzalloc(struct_size(bl, extents, nr_extents_max), GFP_KERNEL); 134 if (!bl) 135 goto out_error; 136 bl->nr_extents = nr_extents_max; 137 args->lg_content = bl; 138 139 for (i = 0; i < bl->nr_extents; i++) { 140 struct pnfs_block_extent *bex = bl->extents + i; 141 u64 bex_length; 142 143 nfserr = nfsd4_block_map_extent(inode, fhp, offset, length, 144 seg->iomode, args->lg_minlength, bex); 145 if (nfserr != nfs_ok) 146 goto out_error; 147 148 bex_length = bex->len - (offset - bex->foff); 149 if (bex_length >= length) { 150 bl->nr_extents = i + 1; 151 break; 152 } 153 154 offset = bex->foff + bex->len; 155 length -= bex_length; 156 } 157 158 first_bex = bl->extents; 159 last_bex = bl->extents + bl->nr_extents - 1; 160 161 nfserr = nfserr_layoutunavailable; 162 length = last_bex->foff + last_bex->len - seg->offset; 163 if (length < args->lg_minlength) { 164 dprintk("pnfsd: extent smaller than minlength\n"); 165 goto out_error; 166 } 167 168 seg->offset = first_bex->foff; 169 seg->length = last_bex->foff - first_bex->foff + last_bex->len; 170 return nfs_ok; 171 172 out_error: 173 seg->length = 0; 174 return nfserr; 175 } 176 177 static __be32 178 nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp, 179 struct iomap *iomaps, int nr_iomaps) 180 { 181 struct timespec64 mtime = inode_get_mtime(inode); 182 struct iattr iattr = { .ia_valid = 0 }; 183 int error; 184 185 if (lcp->lc_mtime.tv_nsec == UTIME_NOW || 186 timespec64_compare(&lcp->lc_mtime, &mtime) < 0) 187 lcp->lc_mtime = current_time(inode); 188 iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME; 189 iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime; 190 191 if (lcp->lc_size_chg) { 192 iattr.ia_valid |= ATTR_SIZE; 193 iattr.ia_size = lcp->lc_newsize; 194 } 195 196 error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps, 197 nr_iomaps, &iattr); 198 kfree(iomaps); 199 return nfserrno(error); 200 } 201 202 #ifdef CONFIG_NFSD_BLOCKLAYOUT 203 static int 204 nfsd4_block_get_device_info_simple(struct super_block *sb, 205 struct nfsd4_getdeviceinfo *gdp) 206 { 207 struct pnfs_block_deviceaddr *dev; 208 struct pnfs_block_volume *b; 209 210 dev = kzalloc(struct_size(dev, volumes, 1), GFP_KERNEL); 211 if (!dev) 212 return -ENOMEM; 213 gdp->gd_device = dev; 214 215 dev->nr_volumes = 1; 216 b = &dev->volumes[0]; 217 218 b->type = PNFS_BLOCK_VOLUME_SIMPLE; 219 b->simple.sig_len = PNFS_BLOCK_UUID_LEN; 220 return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len, 221 &b->simple.offset); 222 } 223 224 static __be32 225 nfsd4_block_proc_getdeviceinfo(struct super_block *sb, 226 struct svc_rqst *rqstp, 227 struct nfs4_client *clp, 228 struct nfsd4_getdeviceinfo *gdp) 229 { 230 if (bdev_is_partition(sb->s_bdev)) 231 return nfserr_inval; 232 return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp)); 233 } 234 235 static __be32 236 nfsd4_block_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp, 237 struct nfsd4_layoutcommit *lcp) 238 { 239 struct iomap *iomaps; 240 int nr_iomaps; 241 __be32 nfserr; 242 243 rqstp->rq_arg = lcp->lc_up_layout; 244 svcxdr_init_decode(rqstp); 245 246 nfserr = nfsd4_block_decode_layoutupdate(&rqstp->rq_arg_stream, 247 &iomaps, &nr_iomaps, i_blocksize(inode)); 248 if (nfserr != nfs_ok) 249 return nfserr; 250 251 return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps); 252 } 253 254 const struct nfsd4_layout_ops bl_layout_ops = { 255 /* 256 * Pretend that we send notification to the client. This is a blatant 257 * lie to force recent Linux clients to cache our device IDs. 258 * We rarely ever change the device ID, so the harm of leaking deviceids 259 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess 260 * in this regard, but I filed errata 4119 for this a while ago, and 261 * hopefully the Linux client will eventually start caching deviceids 262 * without this again. 263 */ 264 .notify_types = 265 NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE, 266 .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo, 267 .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo, 268 .proc_layoutget = nfsd4_block_proc_layoutget, 269 .encode_layoutget = nfsd4_block_encode_layoutget, 270 .proc_layoutcommit = nfsd4_block_proc_layoutcommit, 271 }; 272 #endif /* CONFIG_NFSD_BLOCKLAYOUT */ 273 274 #ifdef CONFIG_NFSD_SCSILAYOUT 275 #define NFSD_MDS_PR_KEY 0x0100000000000000ULL 276 277 /* 278 * We use the client ID as a unique key for the reservations. 279 * This allows us to easily fence a client when recalls fail. 280 */ 281 static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp) 282 { 283 return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id; 284 } 285 286 static const u8 designator_types[] = { 287 PS_DESIGNATOR_EUI64, 288 PS_DESIGNATOR_NAA, 289 }; 290 291 static int 292 nfsd4_block_get_unique_id(struct gendisk *disk, struct pnfs_block_volume *b) 293 { 294 int ret, i; 295 296 for (i = 0; i < ARRAY_SIZE(designator_types); i++) { 297 u8 type = designator_types[i]; 298 299 ret = disk->fops->get_unique_id(disk, b->scsi.designator, type); 300 if (ret > 0) { 301 b->scsi.code_set = PS_CODE_SET_BINARY; 302 b->scsi.designator_type = type; 303 b->scsi.designator_len = ret; 304 return 0; 305 } 306 } 307 308 return -EINVAL; 309 } 310 311 static int 312 nfsd4_block_get_device_info_scsi(struct super_block *sb, 313 struct nfs4_client *clp, 314 struct nfsd4_getdeviceinfo *gdp) 315 { 316 struct pnfs_block_deviceaddr *dev; 317 struct pnfs_block_volume *b; 318 const struct pr_ops *ops; 319 int ret; 320 321 dev = kzalloc(struct_size(dev, volumes, 1), GFP_KERNEL); 322 if (!dev) 323 return -ENOMEM; 324 gdp->gd_device = dev; 325 326 dev->nr_volumes = 1; 327 b = &dev->volumes[0]; 328 329 b->type = PNFS_BLOCK_VOLUME_SCSI; 330 b->scsi.pr_key = nfsd4_scsi_pr_key(clp); 331 332 ret = nfsd4_block_get_unique_id(sb->s_bdev->bd_disk, b); 333 if (ret < 0) 334 goto out_free_dev; 335 336 ret = -EINVAL; 337 ops = sb->s_bdev->bd_disk->fops->pr_ops; 338 if (!ops) { 339 pr_err("pNFS: device %s does not support PRs.\n", 340 sb->s_id); 341 goto out_free_dev; 342 } 343 344 ret = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true); 345 if (ret) { 346 pr_err("pNFS: failed to register key for device %s.\n", 347 sb->s_id); 348 goto out_free_dev; 349 } 350 351 ret = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY, 352 PR_EXCLUSIVE_ACCESS_REG_ONLY, 0); 353 if (ret) { 354 pr_err("pNFS: failed to reserve device %s.\n", 355 sb->s_id); 356 goto out_free_dev; 357 } 358 359 return 0; 360 361 out_free_dev: 362 kfree(dev); 363 gdp->gd_device = NULL; 364 return ret; 365 } 366 367 static __be32 368 nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb, 369 struct svc_rqst *rqstp, 370 struct nfs4_client *clp, 371 struct nfsd4_getdeviceinfo *gdp) 372 { 373 if (bdev_is_partition(sb->s_bdev)) 374 return nfserr_inval; 375 return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp)); 376 } 377 static __be32 378 nfsd4_scsi_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp, 379 struct nfsd4_layoutcommit *lcp) 380 { 381 struct iomap *iomaps; 382 int nr_iomaps; 383 __be32 nfserr; 384 385 rqstp->rq_arg = lcp->lc_up_layout; 386 svcxdr_init_decode(rqstp); 387 388 nfserr = nfsd4_scsi_decode_layoutupdate(&rqstp->rq_arg_stream, 389 &iomaps, &nr_iomaps, i_blocksize(inode)); 390 if (nfserr != nfs_ok) 391 return nfserr; 392 393 return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps); 394 } 395 396 static void 397 nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls, struct nfsd_file *file) 398 { 399 struct nfs4_client *clp = ls->ls_stid.sc_client; 400 struct block_device *bdev = file->nf_file->f_path.mnt->mnt_sb->s_bdev; 401 402 bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY, 403 nfsd4_scsi_pr_key(clp), 0, true); 404 } 405 406 const struct nfsd4_layout_ops scsi_layout_ops = { 407 /* 408 * Pretend that we send notification to the client. This is a blatant 409 * lie to force recent Linux clients to cache our device IDs. 410 * We rarely ever change the device ID, so the harm of leaking deviceids 411 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess 412 * in this regard, but I filed errata 4119 for this a while ago, and 413 * hopefully the Linux client will eventually start caching deviceids 414 * without this again. 415 */ 416 .notify_types = 417 NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE, 418 .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo, 419 .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo, 420 .proc_layoutget = nfsd4_block_proc_layoutget, 421 .encode_layoutget = nfsd4_block_encode_layoutget, 422 .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit, 423 .fence_client = nfsd4_scsi_fence_client, 424 }; 425 #endif /* CONFIG_NFSD_SCSILAYOUT */ 426