blob: f00fa918e0f635678cd9780e37da8a63b5fc977f [file] [log] [blame]
Christoph Hellwig8650b8a2015-01-21 11:40:00 +01001/*
2 * Copyright (c) 2014 Christoph Hellwig.
3 */
4#include <linux/exportfs.h>
5#include <linux/genhd.h>
6#include <linux/slab.h>
7
8#include <linux/nfsd/debug.h>
9
10#include "blocklayoutxdr.h"
11#include "pnfs.h"
12
13#define NFSDDBG_FACILITY NFSDDBG_PNFS
14
15
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010016static __be32
17nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
18 struct nfsd4_layoutget *args)
19{
20 struct nfsd4_layout_seg *seg = &args->lg_seg;
21 struct super_block *sb = inode->i_sb;
22 u32 block_size = (1 << inode->i_blkbits);
23 struct pnfs_block_extent *bex;
24 struct iomap iomap;
25 u32 device_generation = 0;
26 int error;
27
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010028 if (seg->offset & (block_size - 1)) {
29 dprintk("pnfsd: I/O misaligned\n");
30 goto out_layoutunavailable;
31 }
32
33 /*
34 * Some clients barf on non-zero block numbers for NONE or INVALID
35 * layouts, so make sure to zero the whole structure.
36 */
37 error = -ENOMEM;
38 bex = kzalloc(sizeof(*bex), GFP_KERNEL);
39 if (!bex)
40 goto out_error;
41 args->lg_content = bex;
42
43 error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
44 &iomap, seg->iomode != IOMODE_READ,
45 &device_generation);
46 if (error) {
47 if (error == -ENXIO)
48 goto out_layoutunavailable;
49 goto out_error;
50 }
51
52 if (iomap.length < args->lg_minlength) {
53 dprintk("pnfsd: extent smaller than minlength\n");
54 goto out_layoutunavailable;
55 }
56
57 switch (iomap.type) {
58 case IOMAP_MAPPED:
59 if (seg->iomode == IOMODE_READ)
60 bex->es = PNFS_BLOCK_READ_DATA;
61 else
62 bex->es = PNFS_BLOCK_READWRITE_DATA;
63 bex->soff = (iomap.blkno << 9);
64 break;
65 case IOMAP_UNWRITTEN:
66 if (seg->iomode & IOMODE_RW) {
67 /*
68 * Crack monkey special case from section 2.3.1.
69 */
70 if (args->lg_minlength == 0) {
71 dprintk("pnfsd: no soup for you!\n");
72 goto out_layoutunavailable;
73 }
74
75 bex->es = PNFS_BLOCK_INVALID_DATA;
76 bex->soff = (iomap.blkno << 9);
77 break;
78 }
79 /*FALLTHRU*/
80 case IOMAP_HOLE:
81 if (seg->iomode == IOMODE_READ) {
82 bex->es = PNFS_BLOCK_NONE_DATA;
83 break;
84 }
85 /*FALLTHRU*/
86 case IOMAP_DELALLOC:
87 default:
88 WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
89 goto out_layoutunavailable;
90 }
91
92 error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
93 if (error)
94 goto out_error;
95 bex->foff = iomap.offset;
96 bex->len = iomap.length;
97
98 seg->offset = iomap.offset;
99 seg->length = iomap.length;
100
Kinglong Mee853695232015-03-22 22:16:40 +0800101 dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100102 return 0;
103
104out_error:
105 seg->length = 0;
106 return nfserrno(error);
107out_layoutunavailable:
108 seg->length = 0;
109 return nfserr_layoutunavailable;
110}
111
112static __be32
Christoph Hellwig368248e2016-03-04 20:46:17 +0100113nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
114 struct iomap *iomaps, int nr_iomaps)
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100115{
116 loff_t new_size = lcp->lc_last_wr + 1;
117 struct iattr iattr = { .ia_valid = 0 };
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100118 int error;
119
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100120 if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
121 timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
122 lcp->lc_mtime = current_fs_time(inode->i_sb);
123 iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
124 iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
125
126 if (new_size > i_size_read(inode)) {
127 iattr.ia_valid |= ATTR_SIZE;
128 iattr.ia_size = new_size;
129 }
130
131 error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
132 nr_iomaps, &iattr);
133 kfree(iomaps);
134 return nfserrno(error);
135}
136
Christoph Hellwig368248e2016-03-04 20:46:17 +0100137#ifdef CONFIG_NFSD_BLOCKLAYOUT
138static int
139nfsd4_block_get_device_info_simple(struct super_block *sb,
140 struct nfsd4_getdeviceinfo *gdp)
141{
142 struct pnfs_block_deviceaddr *dev;
143 struct pnfs_block_volume *b;
144
145 dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
146 sizeof(struct pnfs_block_volume), GFP_KERNEL);
147 if (!dev)
148 return -ENOMEM;
149 gdp->gd_device = dev;
150
151 dev->nr_volumes = 1;
152 b = &dev->volumes[0];
153
154 b->type = PNFS_BLOCK_VOLUME_SIMPLE;
155 b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
156 return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
157 &b->simple.offset);
158}
159
160static __be32
161nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
162 struct nfsd4_getdeviceinfo *gdp)
163{
164 if (sb->s_bdev != sb->s_bdev->bd_contains)
165 return nfserr_inval;
166 return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
167}
168
169static __be32
170nfsd4_block_proc_layoutcommit(struct inode *inode,
171 struct nfsd4_layoutcommit *lcp)
172{
173 struct iomap *iomaps;
174 int nr_iomaps;
175
176 nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
177 lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits);
178 if (nr_iomaps < 0)
179 return nfserrno(nr_iomaps);
180
181 return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
182}
183
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100184const struct nfsd4_layout_ops bl_layout_ops = {
Christoph Hellwig40cdc7a2015-04-27 14:50:13 +0200185 /*
186 * Pretend that we send notification to the client. This is a blatant
187 * lie to force recent Linux clients to cache our device IDs.
188 * We rarely ever change the device ID, so the harm of leaking deviceids
189 * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
190 * in this regard, but I filed errata 4119 for this a while ago, and
191 * hopefully the Linux client will eventually start caching deviceids
192 * without this again.
193 */
194 .notify_types =
195 NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100196 .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
197 .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
198 .proc_layoutget = nfsd4_block_proc_layoutget,
199 .encode_layoutget = nfsd4_block_encode_layoutget,
200 .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
201};
Christoph Hellwig368248e2016-03-04 20:46:17 +0100202#endif /* CONFIG_NFSD_BLOCKLAYOUT */