Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

target: make target db location configurable

This commit adds the read-write attribute "dbroot",
in the top-level CONFIGFS (core) target directory,
normally /sys/kernel/config/target. This attribute
defaults to "/var/target" but can be changed by
writing a new pathname string to it. Changing this
attribute is only allowed when no fabric drivers
are loaded and the supplied value specifies an
existing directory.

Target modules that care about the target database
root directory will be modified to use this
attribute in a future commit.

Signed-off-by: Lee Duncan <lduncan@suse.com>
Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>

authored by

Lee Duncan and committed by
Nicholas Bellinger
a96e9783 44549e8f

+68
+62
drivers/target/target_core_configfs.c
··· 99 99 100 100 CONFIGFS_ATTR_RO(target_core_item_, version); 101 101 102 + char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT; 103 + static char db_root_stage[DB_ROOT_LEN]; 104 + 105 + static ssize_t target_core_item_dbroot_show(struct config_item *item, 106 + char *page) 107 + { 108 + return sprintf(page, "%s\n", db_root); 109 + } 110 + 111 + static ssize_t target_core_item_dbroot_store(struct config_item *item, 112 + const char *page, size_t count) 113 + { 114 + ssize_t read_bytes; 115 + struct file *fp; 116 + 117 + mutex_lock(&g_tf_lock); 118 + if (!list_empty(&g_tf_list)) { 119 + mutex_unlock(&g_tf_lock); 120 + pr_err("db_root: cannot be changed: target drivers registered"); 121 + return -EINVAL; 122 + } 123 + 124 + if (count > (DB_ROOT_LEN - 1)) { 125 + mutex_unlock(&g_tf_lock); 126 + pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n", 127 + (int)count, DB_ROOT_LEN - 1); 128 + return -EINVAL; 129 + } 130 + 131 + read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page); 132 + if (!read_bytes) { 133 + mutex_unlock(&g_tf_lock); 134 + return -EINVAL; 135 + } 136 + if (db_root_stage[read_bytes - 1] == '\n') 137 + db_root_stage[read_bytes - 1] = '\0'; 138 + 139 + /* validate new db root before accepting it */ 140 + fp = filp_open(db_root_stage, O_RDONLY, 0); 141 + if (IS_ERR(fp)) { 142 + mutex_unlock(&g_tf_lock); 143 + pr_err("db_root: cannot open: %s\n", db_root_stage); 144 + return -EINVAL; 145 + } 146 + if (!S_ISDIR(fp->f_inode->i_mode)) { 147 + filp_close(fp, 0); 148 + mutex_unlock(&g_tf_lock); 149 + pr_err("db_root: not a directory: %s\n", db_root_stage); 150 + return -EINVAL; 151 + } 152 + filp_close(fp, 0); 153 + 154 + strncpy(db_root, db_root_stage, read_bytes); 155 + 156 + mutex_unlock(&g_tf_lock); 157 + 158 + return read_bytes; 159 + } 160 + 161 + CONFIGFS_ATTR(target_core_item_, dbroot); 162 + 102 163 static struct target_fabric_configfs *target_core_get_fabric( 103 164 const char *name) 104 165 { ··· 300 239 */ 301 240 static struct configfs_attribute *target_core_fabric_item_attrs[] = { 302 241 &target_core_item_attr_version, 242 + &target_core_item_attr_dbroot, 303 243 NULL, 304 244 }; 305 245
+6
drivers/target/target_core_internal.h
··· 155 155 /* target_core_xcopy.c */ 156 156 extern struct se_portal_group xcopy_pt_tpg; 157 157 158 + /* target_core_configfs.c */ 159 + #define DB_ROOT_LEN 4096 160 + #define DB_ROOT_DEFAULT "/var/target" 161 + 162 + extern char db_root[]; 163 + 158 164 #endif /* TARGET_CORE_INTERNAL_H */