]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/cachefiles/xattr.c
ubifs: Reject unsupported ioctl flags explicitly
[mirror_ubuntu-bionic-kernel.git] / fs / cachefiles / xattr.c
1 /* CacheFiles extended attribute management
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/file.h>
15 #include <linux/fs.h>
16 #include <linux/fsnotify.h>
17 #include <linux/quotaops.h>
18 #include <linux/xattr.h>
19 #include <linux/slab.h>
20 #include "internal.h"
21
22 static const char cachefiles_xattr_cache[] =
23 XATTR_USER_PREFIX "CacheFiles.cache";
24
25 /*
26 * check the type label on an object
27 * - done using xattrs
28 */
29 int cachefiles_check_object_type(struct cachefiles_object *object)
30 {
31 struct dentry *dentry = object->dentry;
32 char type[3], xtype[3];
33 int ret;
34
35 ASSERT(dentry);
36 ASSERT(d_backing_inode(dentry));
37
38 if (!object->fscache.cookie)
39 strcpy(type, "C3");
40 else
41 snprintf(type, 3, "%02x", object->fscache.cookie->def->type);
42
43 _enter("%p{%s}", object, type);
44
45 /* attempt to install a type label directly */
46 ret = vfs_setxattr(dentry, cachefiles_xattr_cache, type, 2,
47 XATTR_CREATE);
48 if (ret == 0) {
49 _debug("SET"); /* we succeeded */
50 goto error;
51 }
52
53 if (ret != -EEXIST) {
54 pr_err("Can't set xattr on %pd [%lu] (err %d)\n",
55 dentry, d_backing_inode(dentry)->i_ino,
56 -ret);
57 goto error;
58 }
59
60 /* read the current type label */
61 ret = vfs_getxattr(dentry, cachefiles_xattr_cache, xtype, 3);
62 if (ret < 0) {
63 if (ret == -ERANGE)
64 goto bad_type_length;
65
66 pr_err("Can't read xattr on %pd [%lu] (err %d)\n",
67 dentry, d_backing_inode(dentry)->i_ino,
68 -ret);
69 goto error;
70 }
71
72 /* check the type is what we're expecting */
73 if (ret != 2)
74 goto bad_type_length;
75
76 if (xtype[0] != type[0] || xtype[1] != type[1])
77 goto bad_type;
78
79 ret = 0;
80
81 error:
82 _leave(" = %d", ret);
83 return ret;
84
85 bad_type_length:
86 pr_err("Cache object %lu type xattr length incorrect\n",
87 d_backing_inode(dentry)->i_ino);
88 ret = -EIO;
89 goto error;
90
91 bad_type:
92 xtype[2] = 0;
93 pr_err("Cache object %pd [%lu] type %s not %s\n",
94 dentry, d_backing_inode(dentry)->i_ino,
95 xtype, type);
96 ret = -EIO;
97 goto error;
98 }
99
100 /*
101 * set the state xattr on a cache file
102 */
103 int cachefiles_set_object_xattr(struct cachefiles_object *object,
104 struct cachefiles_xattr *auxdata)
105 {
106 struct dentry *dentry = object->dentry;
107 int ret;
108
109 ASSERT(dentry);
110
111 _enter("%p,#%d", object, auxdata->len);
112
113 /* attempt to install the cache metadata directly */
114 _debug("SET #%u", auxdata->len);
115
116 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
117 &auxdata->type, auxdata->len,
118 XATTR_CREATE);
119 if (ret < 0 && ret != -ENOMEM)
120 cachefiles_io_error_obj(
121 object,
122 "Failed to set xattr with error %d", ret);
123
124 _leave(" = %d", ret);
125 return ret;
126 }
127
128 /*
129 * update the state xattr on a cache file
130 */
131 int cachefiles_update_object_xattr(struct cachefiles_object *object,
132 struct cachefiles_xattr *auxdata)
133 {
134 struct dentry *dentry = object->dentry;
135 int ret;
136
137 if (!dentry)
138 return -ESTALE;
139
140 _enter("%p,#%d", object, auxdata->len);
141
142 /* attempt to install the cache metadata directly */
143 _debug("SET #%u", auxdata->len);
144
145 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
146 &auxdata->type, auxdata->len,
147 XATTR_REPLACE);
148 if (ret < 0 && ret != -ENOMEM)
149 cachefiles_io_error_obj(
150 object,
151 "Failed to update xattr with error %d", ret);
152
153 _leave(" = %d", ret);
154 return ret;
155 }
156
157 /*
158 * check the consistency between the backing cache and the FS-Cache cookie
159 */
160 int cachefiles_check_auxdata(struct cachefiles_object *object)
161 {
162 struct cachefiles_xattr *auxbuf;
163 enum fscache_checkaux validity;
164 struct dentry *dentry = object->dentry;
165 ssize_t xlen;
166 int ret;
167
168 ASSERT(dentry);
169 ASSERT(d_backing_inode(dentry));
170 ASSERT(object->fscache.cookie->def->check_aux);
171
172 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
173 if (!auxbuf)
174 return -ENOMEM;
175
176 xlen = vfs_getxattr(dentry, cachefiles_xattr_cache,
177 &auxbuf->type, 512 + 1);
178 ret = -ESTALE;
179 if (xlen < 1 ||
180 auxbuf->type != object->fscache.cookie->def->type)
181 goto error;
182
183 xlen--;
184 validity = fscache_check_aux(&object->fscache, &auxbuf->data, xlen);
185 if (validity != FSCACHE_CHECKAUX_OKAY)
186 goto error;
187
188 ret = 0;
189 error:
190 kfree(auxbuf);
191 return ret;
192 }
193
194 /*
195 * check the state xattr on a cache file
196 * - return -ESTALE if the object should be deleted
197 */
198 int cachefiles_check_object_xattr(struct cachefiles_object *object,
199 struct cachefiles_xattr *auxdata)
200 {
201 struct cachefiles_xattr *auxbuf;
202 struct dentry *dentry = object->dentry;
203 int ret;
204
205 _enter("%p,#%d", object, auxdata->len);
206
207 ASSERT(dentry);
208 ASSERT(d_backing_inode(dentry));
209
210 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp);
211 if (!auxbuf) {
212 _leave(" = -ENOMEM");
213 return -ENOMEM;
214 }
215
216 /* read the current type label */
217 ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
218 &auxbuf->type, 512 + 1);
219 if (ret < 0) {
220 if (ret == -ENODATA)
221 goto stale; /* no attribute - power went off
222 * mid-cull? */
223
224 if (ret == -ERANGE)
225 goto bad_type_length;
226
227 cachefiles_io_error_obj(object,
228 "Can't read xattr on %lu (err %d)",
229 d_backing_inode(dentry)->i_ino, -ret);
230 goto error;
231 }
232
233 /* check the on-disk object */
234 if (ret < 1)
235 goto bad_type_length;
236
237 if (auxbuf->type != auxdata->type)
238 goto stale;
239
240 auxbuf->len = ret;
241
242 /* consult the netfs */
243 if (object->fscache.cookie->def->check_aux) {
244 enum fscache_checkaux result;
245 unsigned int dlen;
246
247 dlen = auxbuf->len - 1;
248
249 _debug("checkaux %s #%u",
250 object->fscache.cookie->def->name, dlen);
251
252 result = fscache_check_aux(&object->fscache,
253 &auxbuf->data, dlen);
254
255 switch (result) {
256 /* entry okay as is */
257 case FSCACHE_CHECKAUX_OKAY:
258 goto okay;
259
260 /* entry requires update */
261 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
262 break;
263
264 /* entry requires deletion */
265 case FSCACHE_CHECKAUX_OBSOLETE:
266 goto stale;
267
268 default:
269 BUG();
270 }
271
272 /* update the current label */
273 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
274 &auxdata->type, auxdata->len,
275 XATTR_REPLACE);
276 if (ret < 0) {
277 cachefiles_io_error_obj(object,
278 "Can't update xattr on %lu"
279 " (error %d)",
280 d_backing_inode(dentry)->i_ino, -ret);
281 goto error;
282 }
283 }
284
285 okay:
286 ret = 0;
287
288 error:
289 kfree(auxbuf);
290 _leave(" = %d", ret);
291 return ret;
292
293 bad_type_length:
294 pr_err("Cache object %lu xattr length incorrect\n",
295 d_backing_inode(dentry)->i_ino);
296 ret = -EIO;
297 goto error;
298
299 stale:
300 ret = -ESTALE;
301 goto error;
302 }
303
304 /*
305 * remove the object's xattr to mark it stale
306 */
307 int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
308 struct dentry *dentry)
309 {
310 int ret;
311
312 ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
313 if (ret < 0) {
314 if (ret == -ENOENT || ret == -ENODATA)
315 ret = 0;
316 else if (ret != -ENOMEM)
317 cachefiles_io_error(cache,
318 "Can't remove xattr from %lu"
319 " (error %d)",
320 d_backing_inode(dentry)->i_ino, -ret);
321 }
322
323 _leave(" = %d", ret);
324 return ret;
325 }