~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfsd/blocklayout.c

Version: ~ [ linux-5.0-rc6 ] ~ [ linux-4.20.10 ] ~ [ linux-4.19.23 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.101 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.158 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.174 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.134 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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         struct timespec ts;
125         int error;
126 
127         ts = timespec64_to_timespec(inode->i_mtime);
128         if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
129             timespec_compare(&lcp->lc_mtime, &ts) < 0)
130                 lcp->lc_mtime = timespec64_to_timespec(current_time(inode));
131         iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
132         iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = timespec_to_timespec64(lcp->lc_mtime);
133 
134         if (new_size > i_size_read(inode)) {
135                 iattr.ia_valid |= ATTR_SIZE;
136                 iattr.ia_size = new_size;
137         }
138 
139         error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
140                         nr_iomaps, &iattr);
141         kfree(iomaps);
142         return nfserrno(error);
143 }
144 
145 #ifdef CONFIG_NFSD_BLOCKLAYOUT
146 static int
147 nfsd4_block_get_device_info_simple(struct super_block *sb,
148                 struct nfsd4_getdeviceinfo *gdp)
149 {
150         struct pnfs_block_deviceaddr *dev;
151         struct pnfs_block_volume *b;
152 
153         dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
154                       sizeof(struct pnfs_block_volume), GFP_KERNEL);
155         if (!dev)
156                 return -ENOMEM;
157         gdp->gd_device = dev;
158 
159         dev->nr_volumes = 1;
160         b = &dev->volumes[0];
161 
162         b->type = PNFS_BLOCK_VOLUME_SIMPLE;
163         b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
164         return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
165                         &b->simple.offset);
166 }
167 
168 static __be32
169 nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
170                 struct svc_rqst *rqstp,
171                 struct nfs4_client *clp,
172                 struct nfsd4_getdeviceinfo *gdp)
173 {
174         if (sb->s_bdev != sb->s_bdev->bd_contains)
175                 return nfserr_inval;
176         return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
177 }
178 
179 static __be32
180 nfsd4_block_proc_layoutcommit(struct inode *inode,
181                 struct nfsd4_layoutcommit *lcp)
182 {
183         struct iomap *iomaps;
184         int nr_iomaps;
185 
186         nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
187                         lcp->lc_up_len, &iomaps, i_blocksize(inode));
188         if (nr_iomaps < 0)
189                 return nfserrno(nr_iomaps);
190 
191         return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
192 }
193 
194 const struct nfsd4_layout_ops bl_layout_ops = {
195         /*
196          * Pretend that we send notification to the client.  This is a blatant
197          * lie to force recent Linux clients to cache our device IDs.
198          * We rarely ever change the device ID, so the harm of leaking deviceids
199          * for a while isn't too bad.  Unfortunately RFC5661 is a complete mess
200          * in this regard, but I filed errata 4119 for this a while ago, and
201          * hopefully the Linux client will eventually start caching deviceids
202          * without this again.
203          */
204         .notify_types           =
205                         NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
206         .proc_getdeviceinfo     = nfsd4_block_proc_getdeviceinfo,
207         .encode_getdeviceinfo   = nfsd4_block_encode_getdeviceinfo,
208         .proc_layoutget         = nfsd4_block_proc_layoutget,
209         .encode_layoutget       = nfsd4_block_encode_layoutget,
210         .proc_layoutcommit      = nfsd4_block_proc_layoutcommit,
211 };
212 #endif /* CONFIG_NFSD_BLOCKLAYOUT */
213 
214 #ifdef CONFIG_NFSD_SCSILAYOUT
215 static int nfsd4_scsi_identify_device(struct block_device *bdev,
216                 struct pnfs_block_volume *b)
217 {
218         struct request_queue *q = bdev->bd_disk->queue;
219         struct request *rq;
220         struct scsi_request *req;
221         /*
222          * The allocation length (passed in bytes 3 and 4 of the INQUIRY
223          * command descriptor block) specifies the number of bytes that have
224          * been allocated for the data-in buffer.
225          * 252 is the highest one-byte value that is a multiple of 4.
226          * 65532 is the highest two-byte value that is a multiple of 4.
227          */
228         size_t bufflen = 252, maxlen = 65532, len, id_len;
229         u8 *buf, *d, type, assoc;
230         int retries = 1, error;
231 
232         if (WARN_ON_ONCE(!blk_queue_scsi_passthrough(q)))
233                 return -EINVAL;
234 
235 again:
236         buf = kzalloc(bufflen, GFP_KERNEL);
237         if (!buf)
238                 return -ENOMEM;
239 
240         rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
241         if (IS_ERR(rq)) {
242                 error = -ENOMEM;
243                 goto out_free_buf;
244         }
245         req = scsi_req(rq);
246 
247         error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL);
248         if (error)
249                 goto out_put_request;
250 
251         req->cmd[0] = INQUIRY;
252         req->cmd[1] = 1;
253         req->cmd[2] = 0x83;
254         req->cmd[3] = bufflen >> 8;
255         req->cmd[4] = bufflen & 0xff;
256         req->cmd_len = COMMAND_SIZE(INQUIRY);
257 
258         blk_execute_rq(rq->q, NULL, rq, 1);
259         if (req->result) {
260                 pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
261                         req->result);
262                 error = -EIO;
263                 goto out_put_request;
264         }
265 
266         len = (buf[2] << 8) + buf[3] + 4;
267         if (len > bufflen) {
268                 if (len <= maxlen && retries--) {
269                         blk_put_request(rq);
270                         kfree(buf);
271                         bufflen = len;
272                         goto again;
273                 }
274                 pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
275                         len);
276                 goto out_put_request;
277         }
278 
279         d = buf + 4;
280         for (d = buf + 4; d < buf + len; d += id_len + 4) {
281                 id_len = d[3];
282                 type = d[1] & 0xf;
283                 assoc = (d[1] >> 4) & 0x3;
284 
285                 /*
286                  * We only care about a EUI-64 and NAA designator types
287                  * with LU association.
288                  */
289                 if (assoc != 0x00)
290                         continue;
291                 if (type != 0x02 && type != 0x03)
292                         continue;
293                 if (id_len != 8 && id_len != 12 && id_len != 16)
294                         continue;
295 
296                 b->scsi.code_set = PS_CODE_SET_BINARY;
297                 b->scsi.designator_type = type == 0x02 ?
298                         PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA;
299                 b->scsi.designator_len = id_len;
300                 memcpy(b->scsi.designator, d + 4, id_len);
301 
302                 /*
303                  * If we found a 8 or 12 byte descriptor continue on to
304                  * see if a 16 byte one is available.  If we find a
305                  * 16 byte descriptor we're done.
306                  */
307                 if (id_len == 16)
308                         break;
309         }
310 
311 out_put_request:
312         blk_put_request(rq);
313 out_free_buf:
314         kfree(buf);
315         return error;
316 }
317 
318 #define NFSD_MDS_PR_KEY         0x0100000000000000ULL
319 
320 /*
321  * We use the client ID as a unique key for the reservations.
322  * This allows us to easily fence a client when recalls fail.
323  */
324 static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
325 {
326         return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
327 }
328 
329 static int
330 nfsd4_block_get_device_info_scsi(struct super_block *sb,
331                 struct nfs4_client *clp,
332                 struct nfsd4_getdeviceinfo *gdp)
333 {
334         struct pnfs_block_deviceaddr *dev;
335         struct pnfs_block_volume *b;
336         const struct pr_ops *ops;
337         int error;
338 
339         dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
340                       sizeof(struct pnfs_block_volume), GFP_KERNEL);
341         if (!dev)
342                 return -ENOMEM;
343         gdp->gd_device = dev;
344 
345         dev->nr_volumes = 1;
346         b = &dev->volumes[0];
347 
348         b->type = PNFS_BLOCK_VOLUME_SCSI;
349         b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
350 
351         error = nfsd4_scsi_identify_device(sb->s_bdev, b);
352         if (error)
353                 return error;
354 
355         ops = sb->s_bdev->bd_disk->fops->pr_ops;
356         if (!ops) {
357                 pr_err("pNFS: device %s does not support PRs.\n",
358                         sb->s_id);
359                 return -EINVAL;
360         }
361 
362         error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
363         if (error) {
364                 pr_err("pNFS: failed to register key for device %s.\n",
365                         sb->s_id);
366                 return -EINVAL;
367         }
368 
369         error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
370                         PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
371         if (error) {
372                 pr_err("pNFS: failed to reserve device %s.\n",
373                         sb->s_id);
374                 return -EINVAL;
375         }
376 
377         return 0;
378 }
379 
380 static __be32
381 nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
382                 struct svc_rqst *rqstp,
383                 struct nfs4_client *clp,
384                 struct nfsd4_getdeviceinfo *gdp)
385 {
386         if (sb->s_bdev != sb->s_bdev->bd_contains)
387                 return nfserr_inval;
388         return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
389 }
390 static __be32
391 nfsd4_scsi_proc_layoutcommit(struct inode *inode,
392                 struct nfsd4_layoutcommit *lcp)
393 {
394         struct iomap *iomaps;
395         int nr_iomaps;
396 
397         nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
398                         lcp->lc_up_len, &iomaps, i_blocksize(inode));
399         if (nr_iomaps < 0)
400                 return nfserrno(nr_iomaps);
401 
402         return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
403 }
404 
405 static void
406 nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
407 {
408         struct nfs4_client *clp = ls->ls_stid.sc_client;
409         struct block_device *bdev = ls->ls_file->f_path.mnt->mnt_sb->s_bdev;
410 
411         bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
412                         nfsd4_scsi_pr_key(clp), 0, true);
413 }
414 
415 const struct nfsd4_layout_ops scsi_layout_ops = {
416         /*
417          * Pretend that we send notification to the client.  This is a blatant
418          * lie to force recent Linux clients to cache our device IDs.
419          * We rarely ever change the device ID, so the harm of leaking deviceids
420          * for a while isn't too bad.  Unfortunately RFC5661 is a complete mess
421          * in this regard, but I filed errata 4119 for this a while ago, and
422          * hopefully the Linux client will eventually start caching deviceids
423          * without this again.
424          */
425         .notify_types           =
426                         NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
427         .proc_getdeviceinfo     = nfsd4_scsi_proc_getdeviceinfo,
428         .encode_getdeviceinfo   = nfsd4_block_encode_getdeviceinfo,
429         .proc_layoutget         = nfsd4_block_proc_layoutget,
430         .encode_layoutget       = nfsd4_block_encode_layoutget,
431         .proc_layoutcommit      = nfsd4_scsi_proc_layoutcommit,
432         .fence_client           = nfsd4_scsi_fence_client,
433 };
434 #endif /* CONFIG_NFSD_SCSILAYOUT */
435 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp