Skip to content

Commit 2daf18a

Browse files
Hugh Dickinsbrauner
authored andcommitted
tmpfs,xattr: enable limited user extended attributes
Enable "user." extended attributes on tmpfs, limiting them by tracking the space they occupy, and deducting that space from the limited ispace (unless tmpfs mounted with nr_inodes=0 to leave that ispace unlimited). tmpfs inodes and simple xattrs are both unswappable, and have to be in lowmem on a 32-bit highmem kernel: so the ispace limit is appropriate for xattrs, without any need for a further mount option. Add simple_xattr_space() to give approximate but deterministic estimate of the space taken up by each xattr: with simple_xattrs_free() outputting the space freed if required (but kernfs and even some tmpfs usages do not require that, so don't waste time on strlen'ing if not needed). Security and trusted xattrs were already supported: for consistency and simplicity, account them from the same pool; though there's a small risk that a tmpfs with enough space before would now be considered too small. When extended attributes are used, "df -i" does show more IUsed and less IFree than can be explained by the inodes: document that (manpage later). xfstests tests/generic which were not run on tmpfs before but now pass: 020 037 062 070 077 097 103 117 337 377 454 486 523 533 611 618 728 with no new failures. Signed-off-by: Hugh Dickins <[email protected]> Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Carlos Maiolino <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent e07c469 commit 2daf18a

File tree

6 files changed

+106
-16
lines changed

6 files changed

+106
-16
lines changed

Documentation/filesystems/tmpfs.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ explained further below, some of which can be reconfigured dynamically on the
2121
fly using a remount ('mount -o remount ...') of the filesystem. A tmpfs
2222
filesystem can be resized but it cannot be resized to a size below its current
2323
usage. tmpfs also supports POSIX ACLs, and extended attributes for the
24-
trusted.* and security.* namespaces. ramfs does not use swap and you cannot
25-
modify any parameter for a ramfs filesystem. The size limit of a ramfs
24+
trusted.*, security.* and user.* namespaces. ramfs does not use swap and you
25+
cannot modify any parameter for a ramfs filesystem. The size limit of a ramfs
2626
filesystem is how much memory you have available, and so care must be taken if
2727
used so to not run out of memory.
2828

@@ -97,6 +97,9 @@ mount with such options, since it allows any user with write access to
9797
use up all the memory on the machine; but enhances the scalability of
9898
that instance in a system with many CPUs making intensive use of it.
9999

100+
If nr_inodes is not 0, that limited space for inodes is also used up by
101+
extended attributes: "df -i"'s IUsed and IUse% increase, IFree decreases.
102+
100103
tmpfs blocks may be swapped out, when there is a shortage of memory.
101104
tmpfs has a mount option to disable its use of swap:
102105

fs/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ config TMPFS_XATTR
205205
Extended attributes are name:value pairs associated with inodes by
206206
the kernel or by users (see the attr(5) manual page for details).
207207

208-
Currently this enables support for the trusted.* and
209-
security.* namespaces.
208+
This enables support for the trusted.*, security.* and user.*
209+
namespaces.
210210

211211
You need this for POSIX ACL support on tmpfs.
212212

fs/kernfs/dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ void kernfs_put(struct kernfs_node *kn)
556556
kfree_const(kn->name);
557557

558558
if (kn->iattr) {
559-
simple_xattrs_free(&kn->iattr->xattrs);
559+
simple_xattrs_free(&kn->iattr->xattrs, NULL);
560560
kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
561561
}
562562
spin_lock(&kernfs_idr_lock);

fs/xattr.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,26 @@ const char *xattr_full_name(const struct xattr_handler *handler,
10391039
}
10401040
EXPORT_SYMBOL(xattr_full_name);
10411041

1042+
/**
1043+
* simple_xattr_space - estimate the memory used by a simple xattr
1044+
* @name: the full name of the xattr
1045+
* @size: the size of its value
1046+
*
1047+
* This takes no account of how much larger the two slab objects actually are:
1048+
* that would depend on the slab implementation, when what is required is a
1049+
* deterministic number, which grows with name length and size and quantity.
1050+
*
1051+
* Return: The approximate number of bytes of memory used by such an xattr.
1052+
*/
1053+
size_t simple_xattr_space(const char *name, size_t size)
1054+
{
1055+
/*
1056+
* Use "40" instead of sizeof(struct simple_xattr), to return the
1057+
* same result on 32-bit and 64-bit, and even if simple_xattr grows.
1058+
*/
1059+
return 40 + size + strlen(name);
1060+
}
1061+
10421062
/**
10431063
* simple_xattr_free - free an xattr object
10441064
* @xattr: the xattr object
@@ -1363,14 +1383,17 @@ void simple_xattrs_init(struct simple_xattrs *xattrs)
13631383
/**
13641384
* simple_xattrs_free - free xattrs
13651385
* @xattrs: xattr header whose xattrs to destroy
1386+
* @freed_space: approximate number of bytes of memory freed from @xattrs
13661387
*
13671388
* Destroy all xattrs in @xattr. When this is called no one can hold a
13681389
* reference to any of the xattrs anymore.
13691390
*/
1370-
void simple_xattrs_free(struct simple_xattrs *xattrs)
1391+
void simple_xattrs_free(struct simple_xattrs *xattrs, size_t *freed_space)
13711392
{
13721393
struct rb_node *rbp;
13731394

1395+
if (freed_space)
1396+
*freed_space = 0;
13741397
rbp = rb_first(&xattrs->rb_root);
13751398
while (rbp) {
13761399
struct simple_xattr *xattr;
@@ -1379,6 +1402,9 @@ void simple_xattrs_free(struct simple_xattrs *xattrs)
13791402
rbp_next = rb_next(rbp);
13801403
xattr = rb_entry(rbp, struct simple_xattr, rb_node);
13811404
rb_erase(&xattr->rb_node, &xattrs->rb_root);
1405+
if (freed_space)
1406+
*freed_space += simple_xattr_space(xattr->name,
1407+
xattr->size);
13821408
simple_xattr_free(xattr);
13831409
rbp = rbp_next;
13841410
}

include/linux/xattr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ struct simple_xattr {
114114
};
115115

116116
void simple_xattrs_init(struct simple_xattrs *xattrs);
117-
void simple_xattrs_free(struct simple_xattrs *xattrs);
117+
void simple_xattrs_free(struct simple_xattrs *xattrs, size_t *freed_space);
118+
size_t simple_xattr_space(const char *name, size_t size);
118119
struct simple_xattr *simple_xattr_alloc(const void *value, size_t size);
119120
void simple_xattr_free(struct simple_xattr *xattr);
120121
int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,

mm/shmem.c

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,12 @@ static int shmem_reserve_inode(struct super_block *sb, ino_t *inop)
393393
return 0;
394394
}
395395

396-
static void shmem_free_inode(struct super_block *sb)
396+
static void shmem_free_inode(struct super_block *sb, size_t freed_ispace)
397397
{
398398
struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
399399
if (sbinfo->max_inodes) {
400400
raw_spin_lock(&sbinfo->stat_lock);
401-
sbinfo->free_ispace += BOGO_INODE_SIZE;
401+
sbinfo->free_ispace += BOGO_INODE_SIZE + freed_ispace;
402402
raw_spin_unlock(&sbinfo->stat_lock);
403403
}
404404
}
@@ -1232,6 +1232,7 @@ static void shmem_evict_inode(struct inode *inode)
12321232
{
12331233
struct shmem_inode_info *info = SHMEM_I(inode);
12341234
struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1235+
size_t freed = 0;
12351236

12361237
if (shmem_mapping(inode->i_mapping)) {
12371238
shmem_unacct_size(info->flags, inode->i_size);
@@ -1258,9 +1259,9 @@ static void shmem_evict_inode(struct inode *inode)
12581259
}
12591260
}
12601261

1261-
simple_xattrs_free(&info->xattrs);
1262+
simple_xattrs_free(&info->xattrs, sbinfo->max_inodes ? &freed : NULL);
1263+
shmem_free_inode(inode->i_sb, freed);
12621264
WARN_ON(inode->i_blocks);
1263-
shmem_free_inode(inode->i_sb);
12641265
clear_inode(inode);
12651266
#ifdef CONFIG_TMPFS_QUOTA
12661267
dquot_free_inode(inode);
@@ -2440,7 +2441,7 @@ static struct inode *__shmem_get_inode(struct mnt_idmap *idmap,
24402441
inode = new_inode(sb);
24412442

24422443
if (!inode) {
2443-
shmem_free_inode(sb);
2444+
shmem_free_inode(sb, 0);
24442445
return ERR_PTR(-ENOSPC);
24452446
}
24462447

@@ -3284,7 +3285,7 @@ static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentr
32843285
ret = simple_offset_add(shmem_get_offset_ctx(dir), dentry);
32853286
if (ret) {
32863287
if (inode->i_nlink)
3287-
shmem_free_inode(inode->i_sb);
3288+
shmem_free_inode(inode->i_sb, 0);
32883289
goto out;
32893290
}
32903291

@@ -3304,7 +3305,7 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry)
33043305
struct inode *inode = d_inode(dentry);
33053306

33063307
if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
3307-
shmem_free_inode(inode->i_sb);
3308+
shmem_free_inode(inode->i_sb, 0);
33083309

33093310
simple_offset_remove(shmem_get_offset_ctx(dir), dentry);
33103311

@@ -3557,21 +3558,40 @@ static int shmem_initxattrs(struct inode *inode,
35573558
void *fs_info)
35583559
{
35593560
struct shmem_inode_info *info = SHMEM_I(inode);
3561+
struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
35603562
const struct xattr *xattr;
35613563
struct simple_xattr *new_xattr;
3564+
size_t ispace = 0;
35623565
size_t len;
35633566

3567+
if (sbinfo->max_inodes) {
3568+
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
3569+
ispace += simple_xattr_space(xattr->name,
3570+
xattr->value_len + XATTR_SECURITY_PREFIX_LEN);
3571+
}
3572+
if (ispace) {
3573+
raw_spin_lock(&sbinfo->stat_lock);
3574+
if (sbinfo->free_ispace < ispace)
3575+
ispace = 0;
3576+
else
3577+
sbinfo->free_ispace -= ispace;
3578+
raw_spin_unlock(&sbinfo->stat_lock);
3579+
if (!ispace)
3580+
return -ENOSPC;
3581+
}
3582+
}
3583+
35643584
for (xattr = xattr_array; xattr->name != NULL; xattr++) {
35653585
new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
35663586
if (!new_xattr)
3567-
return -ENOMEM;
3587+
break;
35683588

35693589
len = strlen(xattr->name) + 1;
35703590
new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
35713591
GFP_KERNEL);
35723592
if (!new_xattr->name) {
35733593
kvfree(new_xattr);
3574-
return -ENOMEM;
3594+
break;
35753595
}
35763596

35773597
memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
@@ -3582,6 +3602,16 @@ static int shmem_initxattrs(struct inode *inode,
35823602
simple_xattr_add(&info->xattrs, new_xattr);
35833603
}
35843604

3605+
if (xattr->name != NULL) {
3606+
if (ispace) {
3607+
raw_spin_lock(&sbinfo->stat_lock);
3608+
sbinfo->free_ispace += ispace;
3609+
raw_spin_unlock(&sbinfo->stat_lock);
3610+
}
3611+
simple_xattrs_free(&info->xattrs, NULL);
3612+
return -ENOMEM;
3613+
}
3614+
35853615
return 0;
35863616
}
35873617

@@ -3602,16 +3632,39 @@ static int shmem_xattr_handler_set(const struct xattr_handler *handler,
36023632
size_t size, int flags)
36033633
{
36043634
struct shmem_inode_info *info = SHMEM_I(inode);
3635+
struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
36053636
struct simple_xattr *old_xattr;
3637+
size_t ispace = 0;
36063638

36073639
name = xattr_full_name(handler, name);
3640+
if (value && sbinfo->max_inodes) {
3641+
ispace = simple_xattr_space(name, size);
3642+
raw_spin_lock(&sbinfo->stat_lock);
3643+
if (sbinfo->free_ispace < ispace)
3644+
ispace = 0;
3645+
else
3646+
sbinfo->free_ispace -= ispace;
3647+
raw_spin_unlock(&sbinfo->stat_lock);
3648+
if (!ispace)
3649+
return -ENOSPC;
3650+
}
3651+
36083652
old_xattr = simple_xattr_set(&info->xattrs, name, value, size, flags);
36093653
if (!IS_ERR(old_xattr)) {
3654+
ispace = 0;
3655+
if (old_xattr && sbinfo->max_inodes)
3656+
ispace = simple_xattr_space(old_xattr->name,
3657+
old_xattr->size);
36103658
simple_xattr_free(old_xattr);
36113659
old_xattr = NULL;
36123660
inode->i_ctime = current_time(inode);
36133661
inode_inc_iversion(inode);
36143662
}
3663+
if (ispace) {
3664+
raw_spin_lock(&sbinfo->stat_lock);
3665+
sbinfo->free_ispace += ispace;
3666+
raw_spin_unlock(&sbinfo->stat_lock);
3667+
}
36153668
return PTR_ERR(old_xattr);
36163669
}
36173670

@@ -3627,9 +3680,16 @@ static const struct xattr_handler shmem_trusted_xattr_handler = {
36273680
.set = shmem_xattr_handler_set,
36283681
};
36293682

3683+
static const struct xattr_handler shmem_user_xattr_handler = {
3684+
.prefix = XATTR_USER_PREFIX,
3685+
.get = shmem_xattr_handler_get,
3686+
.set = shmem_xattr_handler_set,
3687+
};
3688+
36303689
static const struct xattr_handler *shmem_xattr_handlers[] = {
36313690
&shmem_security_xattr_handler,
36323691
&shmem_trusted_xattr_handler,
3692+
&shmem_user_xattr_handler,
36333693
NULL
36343694
};
36353695

0 commit comments

Comments
 (0)