Merge branch 'for-linus-4.12' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[sfrench/cifs-2.6.git] / fs / ubifs / xattr.c
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём)
20  *          Adrian Hunter
21  */
22
23 /*
24  * This file implements UBIFS extended attributes support.
25  *
26  * Extended attributes are implemented as regular inodes with attached data,
27  * which limits extended attribute size to UBIFS block size (4KiB). Names of
28  * extended attributes are described by extended attribute entries (xentries),
29  * which are almost identical to directory entries, but have different key type.
30  *
31  * In other words, the situation with extended attributes is very similar to
32  * directories. Indeed, any inode (but of course not xattr inodes) may have a
33  * number of associated xentries, just like directory inodes have associated
34  * directory entries. Extended attribute entries store the name of the extended
35  * attribute, the host inode number, and the extended attribute inode number.
36  * Similarly, direntries store the name, the parent and the target inode
37  * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
38  * extended attributes.
39  *
40  * The number of extended attributes is not limited, but there is Linux
41  * limitation on the maximum possible size of the list of all extended
42  * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
43  * the sum of all extended attribute names of the inode does not exceed that
44  * limit.
45  *
46  * Extended attributes are synchronous, which means they are written to the
47  * flash media synchronously and there is no write-back for extended attribute
48  * inodes. The extended attribute values are not stored in compressed form on
49  * the media.
50  *
51  * Since extended attributes are represented by regular inodes, they are cached
52  * in the VFS inode cache. The xentries are cached in the LNC cache (see
53  * tnc.c).
54  *
55  * ACL support is not implemented.
56  */
57
58 #include "ubifs.h"
59 #include <linux/fs.h>
60 #include <linux/slab.h>
61 #include <linux/xattr.h>
62
63 /*
64  * Limit the number of extended attributes per inode so that the total size
65  * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
66  */
67 #define MAX_XATTRS_PER_INODE 65535
68
69 /*
70  * Extended attribute type constants.
71  *
72  * USER_XATTR: user extended attribute ("user.*")
73  * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
74  * SECURITY_XATTR: security extended attribute ("security.*")
75  */
76 enum {
77         USER_XATTR,
78         TRUSTED_XATTR,
79         SECURITY_XATTR,
80 };
81
82 static const struct inode_operations empty_iops;
83 static const struct file_operations empty_fops;
84
85 /**
86  * create_xattr - create an extended attribute.
87  * @c: UBIFS file-system description object
88  * @host: host inode
89  * @nm: extended attribute name
90  * @value: extended attribute value
91  * @size: size of extended attribute value
92  *
93  * This is a helper function which creates an extended attribute of name @nm
94  * and value @value for inode @host. The host inode is also updated on flash
95  * because the ctime and extended attribute accounting data changes. This
96  * function returns zero in case of success and a negative error code in case
97  * of failure.
98  */
99 static int create_xattr(struct ubifs_info *c, struct inode *host,
100                         const struct fscrypt_name *nm, const void *value, int size)
101 {
102         int err, names_len;
103         struct inode *inode;
104         struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
105         struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
106                                 .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
107                                 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
108
109         if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE) {
110                 ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
111                           host->i_ino, host_ui->xattr_cnt);
112                 return -ENOSPC;
113         }
114         /*
115          * Linux limits the maximum size of the extended attribute names list
116          * to %XATTR_LIST_MAX. This means we should not allow creating more
117          * extended attributes if the name list becomes larger. This limitation
118          * is artificial for UBIFS, though.
119          */
120         names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
121         if (names_len > XATTR_LIST_MAX) {
122                 ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
123                           host->i_ino, names_len, XATTR_LIST_MAX);
124                 return -ENOSPC;
125         }
126
127         err = ubifs_budget_space(c, &req);
128         if (err)
129                 return err;
130
131         inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
132         if (IS_ERR(inode)) {
133                 err = PTR_ERR(inode);
134                 goto out_budg;
135         }
136
137         /* Re-define all operations to be "nothing" */
138         inode->i_mapping->a_ops = &empty_aops;
139         inode->i_op = &empty_iops;
140         inode->i_fop = &empty_fops;
141
142         inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
143         ui = ubifs_inode(inode);
144         ui->xattr = 1;
145         ui->flags |= UBIFS_XATTR_FL;
146         ui->data = kmemdup(value, size, GFP_NOFS);
147         if (!ui->data) {
148                 err = -ENOMEM;
149                 goto out_free;
150         }
151         inode->i_size = ui->ui_size = size;
152         ui->data_len = size;
153
154         mutex_lock(&host_ui->ui_mutex);
155         host->i_ctime = current_time(host);
156         host_ui->xattr_cnt += 1;
157         host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
158         host_ui->xattr_size += CALC_XATTR_BYTES(size);
159         host_ui->xattr_names += fname_len(nm);
160
161         /*
162          * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
163          * have to set the UBIFS_CRYPT_FL flag on the host inode.
164          * To avoid multiple updates of the same inode in the same operation,
165          * let's do it here.
166          */
167         if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
168                 host_ui->flags |= UBIFS_CRYPT_FL;
169
170         err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
171         if (err)
172                 goto out_cancel;
173         mutex_unlock(&host_ui->ui_mutex);
174
175         ubifs_release_budget(c, &req);
176         insert_inode_hash(inode);
177         iput(inode);
178         return 0;
179
180 out_cancel:
181         host_ui->xattr_cnt -= 1;
182         host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
183         host_ui->xattr_size -= CALC_XATTR_BYTES(size);
184         host_ui->xattr_names -= fname_len(nm);
185         host_ui->flags &= ~UBIFS_CRYPT_FL;
186         mutex_unlock(&host_ui->ui_mutex);
187 out_free:
188         make_bad_inode(inode);
189         iput(inode);
190 out_budg:
191         ubifs_release_budget(c, &req);
192         return err;
193 }
194
195 /**
196  * change_xattr - change an extended attribute.
197  * @c: UBIFS file-system description object
198  * @host: host inode
199  * @inode: extended attribute inode
200  * @value: extended attribute value
201  * @size: size of extended attribute value
202  *
203  * This helper function changes the value of extended attribute @inode with new
204  * data from @value. Returns zero in case of success and a negative error code
205  * in case of failure.
206  */
207 static int change_xattr(struct ubifs_info *c, struct inode *host,
208                         struct inode *inode, const void *value, int size)
209 {
210         int err;
211         struct ubifs_inode *host_ui = ubifs_inode(host);
212         struct ubifs_inode *ui = ubifs_inode(inode);
213         void *buf = NULL;
214         int old_size;
215         struct ubifs_budget_req req = { .dirtied_ino = 2,
216                 .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
217
218         ubifs_assert(ui->data_len == inode->i_size);
219         err = ubifs_budget_space(c, &req);
220         if (err)
221                 return err;
222
223         buf = kmemdup(value, size, GFP_NOFS);
224         if (!buf) {
225                 err = -ENOMEM;
226                 goto out_free;
227         }
228         mutex_lock(&ui->ui_mutex);
229         kfree(ui->data);
230         ui->data = buf;
231         inode->i_size = ui->ui_size = size;
232         old_size = ui->data_len;
233         ui->data_len = size;
234         mutex_unlock(&ui->ui_mutex);
235
236         mutex_lock(&host_ui->ui_mutex);
237         host->i_ctime = current_time(host);
238         host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
239         host_ui->xattr_size += CALC_XATTR_BYTES(size);
240
241         /*
242          * It is important to write the host inode after the xattr inode
243          * because if the host inode gets synchronized (via 'fsync()'), then
244          * the extended attribute inode gets synchronized, because it goes
245          * before the host inode in the write-buffer.
246          */
247         err = ubifs_jnl_change_xattr(c, inode, host);
248         if (err)
249                 goto out_cancel;
250         mutex_unlock(&host_ui->ui_mutex);
251
252         ubifs_release_budget(c, &req);
253         return 0;
254
255 out_cancel:
256         host_ui->xattr_size -= CALC_XATTR_BYTES(size);
257         host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
258         mutex_unlock(&host_ui->ui_mutex);
259         make_bad_inode(inode);
260 out_free:
261         ubifs_release_budget(c, &req);
262         return err;
263 }
264
265 static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
266 {
267         struct inode *inode;
268
269         inode = ubifs_iget(c->vfs_sb, inum);
270         if (IS_ERR(inode)) {
271                 ubifs_err(c, "dead extended attribute entry, error %d",
272                           (int)PTR_ERR(inode));
273                 return inode;
274         }
275         if (ubifs_inode(inode)->xattr)
276                 return inode;
277         ubifs_err(c, "corrupt extended attribute entry");
278         iput(inode);
279         return ERR_PTR(-EINVAL);
280 }
281
282 int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
283                     size_t size, int flags)
284 {
285         struct inode *inode;
286         struct ubifs_info *c = host->i_sb->s_fs_info;
287         struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
288         struct ubifs_dent_node *xent;
289         union ubifs_key key;
290         int err;
291
292         /*
293          * Creating an encryption context is done unlocked since we
294          * operate on a new inode which is not visible to other users
295          * at this point.
296          */
297         if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) != 0)
298                 ubifs_assert(inode_is_locked(host));
299
300         if (size > UBIFS_MAX_INO_DATA)
301                 return -ERANGE;
302
303         if (fname_len(&nm) > UBIFS_MAX_NLEN)
304                 return -ENAMETOOLONG;
305
306         xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
307         if (!xent)
308                 return -ENOMEM;
309
310         /*
311          * The extended attribute entries are stored in LNC, so multiple
312          * look-ups do not involve reading the flash.
313          */
314         xent_key_init(c, &key, host->i_ino, &nm);
315         err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
316         if (err) {
317                 if (err != -ENOENT)
318                         goto out_free;
319
320                 if (flags & XATTR_REPLACE)
321                         /* We are asked not to create the xattr */
322                         err = -ENODATA;
323                 else
324                         err = create_xattr(c, host, &nm, value, size);
325                 goto out_free;
326         }
327
328         if (flags & XATTR_CREATE) {
329                 /* We are asked not to replace the xattr */
330                 err = -EEXIST;
331                 goto out_free;
332         }
333
334         inode = iget_xattr(c, le64_to_cpu(xent->inum));
335         if (IS_ERR(inode)) {
336                 err = PTR_ERR(inode);
337                 goto out_free;
338         }
339
340         err = change_xattr(c, host, inode, value, size);
341         iput(inode);
342
343 out_free:
344         kfree(xent);
345         return err;
346 }
347
348 ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
349                         size_t size)
350 {
351         struct inode *inode;
352         struct ubifs_info *c = host->i_sb->s_fs_info;
353         struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
354         struct ubifs_inode *ui;
355         struct ubifs_dent_node *xent;
356         union ubifs_key key;
357         int err;
358
359         if (fname_len(&nm) > UBIFS_MAX_NLEN)
360                 return -ENAMETOOLONG;
361
362         xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
363         if (!xent)
364                 return -ENOMEM;
365
366         xent_key_init(c, &key, host->i_ino, &nm);
367         err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
368         if (err) {
369                 if (err == -ENOENT)
370                         err = -ENODATA;
371                 goto out_unlock;
372         }
373
374         inode = iget_xattr(c, le64_to_cpu(xent->inum));
375         if (IS_ERR(inode)) {
376                 err = PTR_ERR(inode);
377                 goto out_unlock;
378         }
379
380         ui = ubifs_inode(inode);
381         ubifs_assert(inode->i_size == ui->data_len);
382         ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
383
384         mutex_lock(&ui->ui_mutex);
385         if (buf) {
386                 /* If @buf is %NULL we are supposed to return the length */
387                 if (ui->data_len > size) {
388                         ubifs_err(c, "buffer size %zd, xattr len %d",
389                                   size, ui->data_len);
390                         err = -ERANGE;
391                         goto out_iput;
392                 }
393
394                 memcpy(buf, ui->data, ui->data_len);
395         }
396         err = ui->data_len;
397
398 out_iput:
399         mutex_unlock(&ui->ui_mutex);
400         iput(inode);
401 out_unlock:
402         kfree(xent);
403         return err;
404 }
405
406 static bool xattr_visible(const char *name)
407 {
408         /* File encryption related xattrs are for internal use only */
409         if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
410                 return false;
411
412         /* Show trusted namespace only for "power" users */
413         if (strncmp(name, XATTR_TRUSTED_PREFIX,
414                     XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
415                 return false;
416
417         return true;
418 }
419
420 ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
421 {
422         union ubifs_key key;
423         struct inode *host = d_inode(dentry);
424         struct ubifs_info *c = host->i_sb->s_fs_info;
425         struct ubifs_inode *host_ui = ubifs_inode(host);
426         struct ubifs_dent_node *xent, *pxent = NULL;
427         int err, len, written = 0;
428         struct fscrypt_name nm = {0};
429
430         dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
431                 dentry, size);
432
433         len = host_ui->xattr_names + host_ui->xattr_cnt;
434         if (!buffer)
435                 /*
436                  * We should return the minimum buffer size which will fit a
437                  * null-terminated list of all the extended attribute names.
438                  */
439                 return len;
440
441         if (len > size)
442                 return -ERANGE;
443
444         lowest_xent_key(c, &key, host->i_ino);
445         while (1) {
446                 xent = ubifs_tnc_next_ent(c, &key, &nm);
447                 if (IS_ERR(xent)) {
448                         err = PTR_ERR(xent);
449                         break;
450                 }
451
452                 fname_name(&nm) = xent->name;
453                 fname_len(&nm) = le16_to_cpu(xent->nlen);
454
455                 if (xattr_visible(xent->name)) {
456                         memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
457                         written += fname_len(&nm) + 1;
458                 }
459
460                 kfree(pxent);
461                 pxent = xent;
462                 key_read(c, &xent->key, &key);
463         }
464
465         kfree(pxent);
466         if (err != -ENOENT) {
467                 ubifs_err(c, "cannot find next direntry, error %d", err);
468                 return err;
469         }
470
471         ubifs_assert(written <= size);
472         return written;
473 }
474
475 static int remove_xattr(struct ubifs_info *c, struct inode *host,
476                         struct inode *inode, const struct fscrypt_name *nm)
477 {
478         int err;
479         struct ubifs_inode *host_ui = ubifs_inode(host);
480         struct ubifs_inode *ui = ubifs_inode(inode);
481         struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
482                                 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
483
484         ubifs_assert(ui->data_len == inode->i_size);
485
486         err = ubifs_budget_space(c, &req);
487         if (err)
488                 return err;
489
490         mutex_lock(&host_ui->ui_mutex);
491         host->i_ctime = current_time(host);
492         host_ui->xattr_cnt -= 1;
493         host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
494         host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
495         host_ui->xattr_names -= fname_len(nm);
496
497         err = ubifs_jnl_delete_xattr(c, host, inode, nm);
498         if (err)
499                 goto out_cancel;
500         mutex_unlock(&host_ui->ui_mutex);
501
502         ubifs_release_budget(c, &req);
503         return 0;
504
505 out_cancel:
506         host_ui->xattr_cnt += 1;
507         host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
508         host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
509         host_ui->xattr_names += fname_len(nm);
510         mutex_unlock(&host_ui->ui_mutex);
511         ubifs_release_budget(c, &req);
512         make_bad_inode(inode);
513         return err;
514 }
515
516 static int ubifs_xattr_remove(struct inode *host, const char *name)
517 {
518         struct inode *inode;
519         struct ubifs_info *c = host->i_sb->s_fs_info;
520         struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
521         struct ubifs_dent_node *xent;
522         union ubifs_key key;
523         int err;
524
525         ubifs_assert(inode_is_locked(host));
526
527         if (fname_len(&nm) > UBIFS_MAX_NLEN)
528                 return -ENAMETOOLONG;
529
530         xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
531         if (!xent)
532                 return -ENOMEM;
533
534         xent_key_init(c, &key, host->i_ino, &nm);
535         err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
536         if (err) {
537                 if (err == -ENOENT)
538                         err = -ENODATA;
539                 goto out_free;
540         }
541
542         inode = iget_xattr(c, le64_to_cpu(xent->inum));
543         if (IS_ERR(inode)) {
544                 err = PTR_ERR(inode);
545                 goto out_free;
546         }
547
548         ubifs_assert(inode->i_nlink == 1);
549         clear_nlink(inode);
550         err = remove_xattr(c, host, inode, &nm);
551         if (err)
552                 set_nlink(inode, 1);
553
554         /* If @i_nlink is 0, 'iput()' will delete the inode */
555         iput(inode);
556
557 out_free:
558         kfree(xent);
559         return err;
560 }
561
562 #ifdef CONFIG_UBIFS_FS_SECURITY
563 static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
564                       void *fs_info)
565 {
566         const struct xattr *xattr;
567         char *name;
568         int err = 0;
569
570         for (xattr = xattr_array; xattr->name != NULL; xattr++) {
571                 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
572                                strlen(xattr->name) + 1, GFP_NOFS);
573                 if (!name) {
574                         err = -ENOMEM;
575                         break;
576                 }
577                 strcpy(name, XATTR_SECURITY_PREFIX);
578                 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
579                 err = ubifs_xattr_set(inode, name, xattr->value,
580                                       xattr->value_len, 0);
581                 kfree(name);
582                 if (err < 0)
583                         break;
584         }
585
586         return err;
587 }
588
589 int ubifs_init_security(struct inode *dentry, struct inode *inode,
590                         const struct qstr *qstr)
591 {
592         int err;
593
594         err = security_inode_init_security(inode, dentry, qstr,
595                                            &init_xattrs, 0);
596         if (err) {
597                 struct ubifs_info *c = dentry->i_sb->s_fs_info;
598                 ubifs_err(c, "cannot initialize security for inode %lu, error %d",
599                           inode->i_ino, err);
600         }
601         return err;
602 }
603 #endif
604
605 static int xattr_get(const struct xattr_handler *handler,
606                            struct dentry *dentry, struct inode *inode,
607                            const char *name, void *buffer, size_t size)
608 {
609         dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
610                 inode->i_ino, dentry, size);
611
612         name = xattr_full_name(handler, name);
613         return ubifs_xattr_get(inode, name, buffer, size);
614 }
615
616 static int xattr_set(const struct xattr_handler *handler,
617                            struct dentry *dentry, struct inode *inode,
618                            const char *name, const void *value,
619                            size_t size, int flags)
620 {
621         dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
622                 name, inode->i_ino, dentry, size);
623
624         name = xattr_full_name(handler, name);
625
626         if (value)
627                 return ubifs_xattr_set(inode, name, value, size, flags);
628         else
629                 return ubifs_xattr_remove(inode, name);
630 }
631
632 static const struct xattr_handler ubifs_user_xattr_handler = {
633         .prefix = XATTR_USER_PREFIX,
634         .get = xattr_get,
635         .set = xattr_set,
636 };
637
638 static const struct xattr_handler ubifs_trusted_xattr_handler = {
639         .prefix = XATTR_TRUSTED_PREFIX,
640         .get = xattr_get,
641         .set = xattr_set,
642 };
643
644 #ifdef CONFIG_UBIFS_FS_SECURITY
645 static const struct xattr_handler ubifs_security_xattr_handler = {
646         .prefix = XATTR_SECURITY_PREFIX,
647         .get = xattr_get,
648         .set = xattr_set,
649 };
650 #endif
651
652 const struct xattr_handler *ubifs_xattr_handlers[] = {
653         &ubifs_user_xattr_handler,
654         &ubifs_trusted_xattr_handler,
655 #ifdef CONFIG_UBIFS_FS_SECURITY
656         &ubifs_security_xattr_handler,
657 #endif
658         NULL
659 };