GNU Linux-libre 5.19-rc6-gnu
[releases.git] / fs / nfsd / blocklayout.c
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
16 #define NFSDDBG_FACILITY        NFSDDBG_PNFS
17
18
19 static __be32
20 nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
21                 struct nfsd4_layoutget *args)
22 {
23         struct nfsd4_layout_seg *seg = &args->lg_seg;
24         struct super_block *sb = inode->i_sb;
25         u32 block_size = i_blocksize(inode);
26         struct pnfs_block_extent *bex;
27         struct iomap iomap;
28         u32 device_generation = 0;
29         int error;
30
31         if (seg->offset & (block_size - 1)) {
32                 dprintk("pnfsd: I/O misaligned\n");
33                 goto out_layoutunavailable;
34         }
35
36         /*
37          * Some clients barf on non-zero block numbers for NONE or INVALID
38          * layouts, so make sure to zero the whole structure.
39          */
40         error = -ENOMEM;
41         bex = kzalloc(sizeof(*bex), GFP_KERNEL);
42         if (!bex)
43                 goto out_error;
44         args->lg_content = bex;
45
46         error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
47                                             &iomap, seg->iomode != IOMODE_READ,
48                                             &device_generation);
49         if (error) {
50                 if (error == -ENXIO)
51                         goto out_layoutunavailable;
52                 goto out_error;
53         }
54
55         if (iomap.length < args->lg_minlength) {
56                 dprintk("pnfsd: extent smaller than minlength\n");
57                 goto out_layoutunavailable;
58         }
59
60         switch (iomap.type) {
61         case IOMAP_MAPPED:
62                 if (seg->iomode == IOMODE_READ)
63                         bex->es = PNFS_BLOCK_READ_DATA;
64                 else
65                         bex->es = PNFS_BLOCK_READWRITE_DATA;
66                 bex->soff = iomap.addr;
67                 break;
68         case IOMAP_UNWRITTEN:
69                 if (seg->iomode & IOMODE_RW) {
70                         /*
71                          * Crack monkey special case from section 2.3.1.
72                          */
73                         if (args->lg_minlength == 0) {
74                                 dprintk("pnfsd: no soup for you!\n");
75                                 goto out_layoutunavailable;
76                         }
77
78                         bex->es = PNFS_BLOCK_INVALID_DATA;
79                         bex->soff = iomap.addr;
80                         break;
81                 }
82                 fallthrough;
83         case IOMAP_HOLE:
84                 if (seg->iomode == IOMODE_READ) {
85                         bex->es = PNFS_BLOCK_NONE_DATA;
86                         break;
87                 }
88                 fallthrough;
89         case IOMAP_DELALLOC:
90         default:
91                 WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
92                 goto out_layoutunavailable;
93         }
94
95         error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
96         if (error)
97                 goto out_error;
98         bex->foff = iomap.offset;
99         bex->len = iomap.length;
100
101         seg->offset = iomap.offset;
102         seg->length = iomap.length;
103
104         dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
105         return 0;
106
107 out_error:
108         seg->length = 0;
109         return nfserrno(error);
110 out_layoutunavailable:
111         seg->length = 0;
112         return nfserr_layoutunavailable;
113 }
114
115 static __be32
116 nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
117                 struct iomap *iomaps, int nr_iomaps)
118 {
119         loff_t new_size = lcp->lc_last_wr + 1;
120         struct iattr iattr = { .ia_valid = 0 };
121         int error;
122
123         if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
124             timespec64_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
125                 lcp->lc_mtime = current_time(inode);
126         iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
127         iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
128
129         if (new_size > i_size_read(inode)) {
130                 iattr.ia_valid |= ATTR_SIZE;
131                 iattr.ia_size = new_size;
132         }
133
134         error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
135                         nr_iomaps, &iattr);
136         kfree(iomaps);
137         return nfserrno(error);
138 }
139
140 #ifdef CONFIG_NFSD_BLOCKLAYOUT
141 static int
142 nfsd4_block_get_device_info_simple(struct super_block *sb,
143                 struct nfsd4_getdeviceinfo *gdp)
144 {
145         struct pnfs_block_deviceaddr *dev;
146         struct pnfs_block_volume *b;
147
148         dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
149                       sizeof(struct pnfs_block_volume), GFP_KERNEL);
150         if (!dev)
151                 return -ENOMEM;
152         gdp->gd_device = dev;
153
154         dev->nr_volumes = 1;
155         b = &dev->volumes[0];
156
157         b->type = PNFS_BLOCK_VOLUME_SIMPLE;
158         b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
159         return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
160                         &b->simple.offset);
161 }
162
163 static __be32
164 nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
165                 struct svc_rqst *rqstp,
166                 struct nfs4_client *clp,
167                 struct nfsd4_getdeviceinfo *gdp)
168 {
169         if (bdev_is_partition(sb->s_bdev))
170                 return nfserr_inval;
171         return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
172 }
173
174 static __be32
175 nfsd4_block_proc_layoutcommit(struct inode *inode,
176                 struct nfsd4_layoutcommit *lcp)
177 {
178         struct iomap *iomaps;
179         int nr_iomaps;
180
181         nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
182                         lcp->lc_up_len, &iomaps, i_blocksize(inode));
183         if (nr_iomaps < 0)
184                 return nfserrno(nr_iomaps);
185
186         return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
187 }
188
189 const struct nfsd4_layout_ops bl_layout_ops = {
190         /*
191          * Pretend that we send notification to the client.  This is a blatant
192          * lie to force recent Linux clients to cache our device IDs.
193          * We rarely ever change the device ID, so the harm of leaking deviceids
194          * for a while isn't too bad.  Unfortunately RFC5661 is a complete mess
195          * in this regard, but I filed errata 4119 for this a while ago, and
196          * hopefully the Linux client will eventually start caching deviceids
197          * without this again.
198          */
199         .notify_types           =
200                         NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
201         .proc_getdeviceinfo     = nfsd4_block_proc_getdeviceinfo,
202         .encode_getdeviceinfo   = nfsd4_block_encode_getdeviceinfo,
203         .proc_layoutget         = nfsd4_block_proc_layoutget,
204         .encode_layoutget       = nfsd4_block_encode_layoutget,
205         .proc_layoutcommit      = nfsd4_block_proc_layoutcommit,
206 };
207 #endif /* CONFIG_NFSD_BLOCKLAYOUT */
208
209 #ifdef CONFIG_NFSD_SCSILAYOUT
210 #define NFSD_MDS_PR_KEY         0x0100000000000000ULL
211
212 /*
213  * We use the client ID as a unique key for the reservations.
214  * This allows us to easily fence a client when recalls fail.
215  */
216 static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
217 {
218         return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
219 }
220
221 static const u8 designator_types[] = {
222         PS_DESIGNATOR_EUI64,
223         PS_DESIGNATOR_NAA,
224 };
225
226 static int
227 nfsd4_block_get_unique_id(struct gendisk *disk, struct pnfs_block_volume *b)
228 {
229         int ret, i;
230
231         for (i = 0; i < ARRAY_SIZE(designator_types); i++) {
232                 u8 type = designator_types[i];
233
234                 ret = disk->fops->get_unique_id(disk, b->scsi.designator, type);
235                 if (ret > 0) {
236                         b->scsi.code_set = PS_CODE_SET_BINARY;
237                         b->scsi.designator_type = type;
238                         b->scsi.designator_len = ret;
239                         return 0;
240                 }
241         }
242
243         return -EINVAL;
244 }
245
246 static int
247 nfsd4_block_get_device_info_scsi(struct super_block *sb,
248                 struct nfs4_client *clp,
249                 struct nfsd4_getdeviceinfo *gdp)
250 {
251         struct pnfs_block_deviceaddr *dev;
252         struct pnfs_block_volume *b;
253         const struct pr_ops *ops;
254         int ret;
255
256         dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
257                       sizeof(struct pnfs_block_volume), GFP_KERNEL);
258         if (!dev)
259                 return -ENOMEM;
260         gdp->gd_device = dev;
261
262         dev->nr_volumes = 1;
263         b = &dev->volumes[0];
264
265         b->type = PNFS_BLOCK_VOLUME_SCSI;
266         b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
267
268         ret = nfsd4_block_get_unique_id(sb->s_bdev->bd_disk, b);
269         if (ret < 0)
270                 goto out_free_dev;
271
272         ret = -EINVAL;
273         ops = sb->s_bdev->bd_disk->fops->pr_ops;
274         if (!ops) {
275                 pr_err("pNFS: device %s does not support PRs.\n",
276                         sb->s_id);
277                 goto out_free_dev;
278         }
279
280         ret = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
281         if (ret) {
282                 pr_err("pNFS: failed to register key for device %s.\n",
283                         sb->s_id);
284                 goto out_free_dev;
285         }
286
287         ret = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
288                         PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
289         if (ret) {
290                 pr_err("pNFS: failed to reserve device %s.\n",
291                         sb->s_id);
292                 goto out_free_dev;
293         }
294
295         return 0;
296
297 out_free_dev:
298         kfree(dev);
299         return ret;
300 }
301
302 static __be32
303 nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
304                 struct svc_rqst *rqstp,
305                 struct nfs4_client *clp,
306                 struct nfsd4_getdeviceinfo *gdp)
307 {
308         if (bdev_is_partition(sb->s_bdev))
309                 return nfserr_inval;
310         return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
311 }
312 static __be32
313 nfsd4_scsi_proc_layoutcommit(struct inode *inode,
314                 struct nfsd4_layoutcommit *lcp)
315 {
316         struct iomap *iomaps;
317         int nr_iomaps;
318
319         nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
320                         lcp->lc_up_len, &iomaps, i_blocksize(inode));
321         if (nr_iomaps < 0)
322                 return nfserrno(nr_iomaps);
323
324         return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
325 }
326
327 static void
328 nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
329 {
330         struct nfs4_client *clp = ls->ls_stid.sc_client;
331         struct block_device *bdev = ls->ls_file->nf_file->f_path.mnt->mnt_sb->s_bdev;
332
333         bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
334                         nfsd4_scsi_pr_key(clp), 0, true);
335 }
336
337 const struct nfsd4_layout_ops scsi_layout_ops = {
338         /*
339          * Pretend that we send notification to the client.  This is a blatant
340          * lie to force recent Linux clients to cache our device IDs.
341          * We rarely ever change the device ID, so the harm of leaking deviceids
342          * for a while isn't too bad.  Unfortunately RFC5661 is a complete mess
343          * in this regard, but I filed errata 4119 for this a while ago, and
344          * hopefully the Linux client will eventually start caching deviceids
345          * without this again.
346          */
347         .notify_types           =
348                         NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
349         .proc_getdeviceinfo     = nfsd4_scsi_proc_getdeviceinfo,
350         .encode_getdeviceinfo   = nfsd4_block_encode_getdeviceinfo,
351         .proc_layoutget         = nfsd4_block_proc_layoutget,
352         .encode_layoutget       = nfsd4_block_encode_layoutget,
353         .proc_layoutcommit      = nfsd4_scsi_proc_layoutcommit,
354         .fence_client           = nfsd4_scsi_fence_client,
355 };
356 #endif /* CONFIG_NFSD_SCSILAYOUT */