Home | History | Annotate | Download | only in fsck
      1 /**
      2  * xattr.c
      3  *
      4  * Many parts of codes are copied from Linux kernel/fs/f2fs.
      5  *
      6  * Copyright (C) 2015 Huawei Ltd.
      7  * Witten by:
      8  *   Hou Pengyang <houpengyang (at) huawei.com>
      9  *   Liu Shuoran <liushuoran (at) huawei.com>
     10  *   Jaegeuk Kim <jaegeuk (at) kernel.org>
     11  *
     12  * This program is free software; you can redistribute it and/or modify
     13  * it under the terms of the GNU General Public License version 2 as
     14  * published by the Free Software Foundation.
     15  */
     16 #include "fsck.h"
     17 #include "node.h"
     18 #include "xattr.h"
     19 
     20 void *read_all_xattrs(struct f2fs_sb_info *sbi, struct f2fs_node *inode)
     21 {
     22 	struct f2fs_xattr_header *header;
     23 	void *txattr_addr;
     24 	u64 inline_size = inline_xattr_size(&inode->i);
     25 
     26 	txattr_addr = calloc(inline_size + BLOCK_SZ, 1);
     27 	ASSERT(txattr_addr);
     28 
     29 	if (inline_size)
     30 		memcpy(txattr_addr, inline_xattr_addr(&inode->i), inline_size);
     31 
     32 	/* Read from xattr node block. */
     33 	if (inode->i.i_xattr_nid) {
     34 		struct node_info ni;
     35 		int ret;
     36 
     37 		get_node_info(sbi, le32_to_cpu(inode->i.i_xattr_nid), &ni);
     38 		ret = dev_read_block(txattr_addr + inline_size, ni.blk_addr);
     39 		ASSERT(ret >= 0);
     40 	}
     41 
     42 	header = XATTR_HDR(txattr_addr);
     43 
     44 	/* Never been allocated xattrs */
     45 	if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
     46 		header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
     47 		header->h_refcount = cpu_to_le32(1);
     48 	}
     49 	return txattr_addr;
     50 }
     51 
     52 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
     53 		size_t len, const char *name)
     54 {
     55 	struct f2fs_xattr_entry *entry;
     56 	list_for_each_xattr(entry, base_addr) {
     57 		if (entry->e_name_index != index)
     58 			continue;
     59 		if (entry->e_name_len != len)
     60 			continue;
     61 		if (!memcmp(entry->e_name, name, len))
     62 			break;
     63 	}
     64 	return entry;
     65 }
     66 
     67 static void write_all_xattrs(struct f2fs_sb_info *sbi,
     68 		struct f2fs_node *inode, __u32 hsize, void *txattr_addr)
     69 {
     70 	void *xattr_addr;
     71 	struct dnode_of_data dn;
     72 	struct node_info ni;
     73 	struct f2fs_node *xattr_node;
     74 	nid_t new_nid = 0;
     75 	block_t blkaddr;
     76 	nid_t xnid = le32_to_cpu(inode->i.i_xattr_nid);
     77 	u64 inline_size = inline_xattr_size(&inode->i);
     78 	int ret;
     79 
     80 	memcpy(inline_xattr_addr(&inode->i), txattr_addr, inline_size);
     81 
     82 	if (hsize <= inline_size)
     83 		return;
     84 
     85 	if (!xnid) {
     86 		f2fs_alloc_nid(sbi, &new_nid, 0);
     87 
     88 		set_new_dnode(&dn, inode, NULL, new_nid);
     89 		/* NAT entry would be updated by new_node_page. */
     90 		blkaddr = new_node_block(sbi, &dn, XATTR_NODE_OFFSET);
     91 		ASSERT(dn.node_blk);
     92 		xattr_node = dn.node_blk;
     93 		inode->i.i_xattr_nid = cpu_to_le32(new_nid);
     94 	} else {
     95 		set_new_dnode(&dn, inode, NULL, xnid);
     96 		get_node_info(sbi, xnid, &ni);
     97 		blkaddr = ni.blk_addr;
     98 		xattr_node = calloc(BLOCK_SZ, 1);
     99 		ASSERT(xattr_node);
    100 		ret = dev_read_block(xattr_node, ni.blk_addr);
    101 		ASSERT(ret >= 0);
    102 	}
    103 
    104 	/* write to xattr node block */
    105 	xattr_addr = (void *)xattr_node;
    106 	memcpy(xattr_addr, txattr_addr + inline_size,
    107 			PAGE_SIZE - sizeof(struct node_footer));
    108 
    109 	ret = dev_write_block(xattr_node, blkaddr);
    110 	ASSERT(ret >= 0);
    111 }
    112 
    113 int f2fs_setxattr(struct f2fs_sb_info *sbi, nid_t ino, int index, const char *name,
    114 		const void *value, size_t size, int flags)
    115 {
    116 	struct f2fs_node *inode;
    117 	void *base_addr;
    118 	struct f2fs_xattr_entry *here, *last;
    119 	struct node_info ni;
    120 	int error = 0;
    121 	int len;
    122 	int found, newsize;
    123 	__u32 new_hsize;
    124 	int ret;
    125 
    126 	if (name == NULL)
    127 		return -EINVAL;
    128 
    129 	if (value == NULL)
    130 		return -EINVAL;
    131 
    132 	len = strlen(name);
    133 
    134 	if (len > F2FS_NAME_LEN || size > MAX_VALUE_LEN)
    135 		return -ERANGE;
    136 
    137 	if (ino < 3)
    138 		return -EINVAL;
    139 
    140 	/* Now We just support selinux */
    141 	ASSERT(index == F2FS_XATTR_INDEX_SECURITY);
    142 
    143 	get_node_info(sbi, ino, &ni);
    144 	inode = calloc(BLOCK_SZ, 1);
    145 	ASSERT(inode);
    146 	ret = dev_read_block(inode, ni.blk_addr);
    147 	ASSERT(ret >= 0);
    148 
    149 	base_addr = read_all_xattrs(sbi, inode);
    150 	ASSERT(base_addr);
    151 
    152 	here = __find_xattr(base_addr, index, len, name);
    153 
    154 	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
    155 
    156 	if ((flags & XATTR_REPLACE) && !found) {
    157 		error = -ENODATA;
    158 		goto exit;
    159 	} else if ((flags & XATTR_CREATE) && found) {
    160 		error = -EEXIST;
    161 		goto exit;
    162 	}
    163 
    164 	last = here;
    165 	while (!IS_XATTR_LAST_ENTRY(last))
    166 		last = XATTR_NEXT_ENTRY(last);
    167 
    168 	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
    169 
    170 	/* 1. Check space */
    171 	if (value) {
    172 		int free;
    173 		/*
    174 		 * If value is NULL, it is remove operation.
    175 		 * In case of update operation, we calculate free.
    176 		 */
    177 		free = MIN_OFFSET - ((char *)last - (char *)base_addr);
    178 		if (found)
    179 			free = free + ENTRY_SIZE(here);
    180 		if (free < newsize) {
    181 			error = -ENOSPC;
    182 			goto exit;
    183 		}
    184 	}
    185 
    186 	/* 2. Remove old entry */
    187 	if (found) {
    188 		/*
    189 		 * If entry if sound, remove old entry.
    190 		 * If not found, remove operation is not needed
    191 		 */
    192 		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
    193 		int oldsize = ENTRY_SIZE(here);
    194 
    195 		memmove(here, next, (char *)last - (char *)next);
    196 		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
    197 		memset(last, 0, oldsize);
    198 
    199 	}
    200 
    201 	new_hsize = (char *)last - (char *)base_addr;
    202 
    203 	/* 3. Write new entry */
    204 	if (value) {
    205 		char *pval;
    206 		/*
    207 		 * Before we come here, old entry is removed.
    208 		 * We just write new entry.
    209 		 */
    210 		memset(last, 0, newsize);
    211 		last->e_name_index = index;
    212 		last->e_name_len = len;
    213 		memcpy(last->e_name, name, len);
    214 		pval = last->e_name + len;
    215 		memcpy(pval, value, size);
    216 		last->e_value_size = cpu_to_le16(size);
    217 		new_hsize += newsize;
    218 	}
    219 
    220 	write_all_xattrs(sbi, inode, new_hsize, base_addr);
    221 
    222 	/* inode need update */
    223 	ret = dev_write_block(inode, ni.blk_addr);
    224 	ASSERT(ret >= 0);
    225 exit:
    226 	free(base_addr);
    227 	return error;
    228 }
    229 
    230 int inode_set_selinux(struct f2fs_sb_info *sbi, u32 ino, const char *secon)
    231 {
    232 	if (!secon)
    233 		return 0;
    234 
    235 	return f2fs_setxattr(sbi, ino, F2FS_XATTR_INDEX_SECURITY,
    236 			XATTR_SELINUX_SUFFIX, secon, strlen(secon), 1);
    237 }
    238