/usr/src/open-vm-tools-10.0.7/vmblock/linux/control.c is in open-vm-tools-dkms 2:10.0.7-3227872-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | /*********************************************************
* Copyright (C) 2006 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*********************************************************/
/*
* control.c --
*
* Control operations for the vmblock driver.
*
*/
#include "driver-config.h"
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include "vmblockInt.h"
#include "block.h"
/* procfs initialization/cleanup functions */
static int SetupProcDevice(void);
static int CleanupProcDevice(void);
/* procfs entry file operations */
ssize_t ControlFileOpWrite(struct file *filp, const char __user *buf,
size_t cmd, loff_t *ppos);
static int ControlFileOpRelease(struct inode *inode, struct file *file);
static struct proc_dir_entry *controlProcDirEntry;
struct file_operations ControlFileOps = {
.owner = THIS_MODULE,
.write = ControlFileOpWrite,
.release = ControlFileOpRelease,
};
/* Public initialization/cleanup routines */
/*
*----------------------------------------------------------------------------
*
* VMBlockInitControlOps --
*
* Sets up state for control operations.
*
* Results:
* Zero on success, negative error code on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
int
VMBlockInitControlOps(void)
{
int ret;
ret = BlockInit();
if (ret < 0) {
Warning("VMBlockInitControlOps: could not initialize blocking ops.\n");
return ret;
}
ret = SetupProcDevice();
if (ret < 0) {
Warning("VMBlockInitControlOps: could not setup proc device.\n");
BlockCleanup();
return ret;
}
return 0;
}
/*
*----------------------------------------------------------------------------
*
* VMBlockCleanupControlOps --
*
* Cleans up state for control operations.
*
* Results:
* Zero on success, negative error code on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
int
VMBlockCleanupControlOps(void)
{
int ret;
ret = CleanupProcDevice();
if (ret < 0) {
Warning("VMBlockCleanupControlOps: could not cleanup proc device.\n");
return ret;
}
BlockCleanup();
return 0;
}
/* Private initialization/cleanup routines */
/*
*----------------------------------------------------------------------------
*
* VMBlockSetProcEntryOwner --
*
* Sets proc_dir_entry owner if necessary:
*
* Before version 2.6.24 kernel prints nasty warning when in-use
* directory entry is destroyed, which happens when module is unloaded.
* We try to prevent this warning in most cases by setting owner to point
* to our module, so long operations (like current directory pointing to
* directory we created) prevent module from unloading. Since 2.6.24 this
* situation is handled without nastygrams, allowing module unload even
* when current directory points to directory created by unloaded module,
* so we do not have to set owner anymore. And since 2.6.29 we must not
* set owner at all, as there is none...
*
* Results:
* None. Always succeeds.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
static void
VMBlockSetProcEntryOwner(struct proc_dir_entry *entry) // IN/OUT: directory entry
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
entry->owner = THIS_MODULE;
#endif
}
/*
*----------------------------------------------------------------------------
*
* SetupProcDevice --
*
* Adds entries to /proc used to control file blocks.
*
* Results:
* Zero on success, negative error code on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
static int
SetupProcDevice(void)
{
struct proc_dir_entry *controlProcEntry;
struct proc_dir_entry *controlProcMountpoint;
/* Create /proc/fs/vmblock */
controlProcDirEntry = proc_mkdir(VMBLOCK_CONTROL_PROC_DIRNAME, NULL);
if (!controlProcDirEntry) {
Warning("SetupProcDevice: could not create /proc/"
VMBLOCK_CONTROL_PROC_DIRNAME "\n");
return -EINVAL;
}
VMBlockSetProcEntryOwner(controlProcDirEntry);
/* Create /proc/fs/vmblock/mountPoint */
controlProcMountpoint = proc_mkdir(VMBLOCK_CONTROL_MOUNTPOINT,
controlProcDirEntry);
if (!controlProcMountpoint) {
Warning("SetupProcDevice: could not create "
VMBLOCK_MOUNT_POINT "\n");
remove_proc_entry(VMBLOCK_CONTROL_PROC_DIRNAME, NULL);
return -EINVAL;
}
VMBlockSetProcEntryOwner(controlProcMountpoint);
/* Create /proc/fs/vmblock/dev */
controlProcEntry = create_proc_entry(VMBLOCK_CONTROL_DEVNAME,
VMBLOCK_CONTROL_MODE,
controlProcDirEntry);
if (!controlProcEntry) {
Warning("SetupProcDevice: could not create " VMBLOCK_DEVICE "\n");
remove_proc_entry(VMBLOCK_CONTROL_MOUNTPOINT, controlProcDirEntry);
remove_proc_entry(VMBLOCK_CONTROL_PROC_DIRNAME, NULL);
return -EINVAL;
}
controlProcEntry->proc_fops = &ControlFileOps;
return 0;
}
/*
*----------------------------------------------------------------------------
*
* CleanupProcDevice --
*
* Removes /proc entries for controlling file blocks.
*
* Results:
* Zero on success, negative error code on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
static int
CleanupProcDevice(void)
{
if (controlProcDirEntry) {
remove_proc_entry(VMBLOCK_CONTROL_MOUNTPOINT, controlProcDirEntry);
remove_proc_entry(VMBLOCK_CONTROL_DEVNAME, controlProcDirEntry);
remove_proc_entry(VMBLOCK_CONTROL_PROC_DIRNAME, NULL);
}
return 0;
}
/* procfs file operations */
/*
*----------------------------------------------------------------------------
*
* ExecuteBlockOp --
*
* Copy block name from user buffer into kernel space, canonicalize it
* by removing all trailing path separators, and execute desired block
* operation.
*
* Results:
* 0 on success, negative error code on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
static int
ExecuteBlockOp(const char __user *buf, // IN: buffer with name
const os_blocker_id_t blocker, // IN: blocker ID (file)
int (*blockOp)(const char *filename, // IN: block operation
const os_blocker_id_t blocker))
{
char *name;
int i;
int retval;
name = getname(buf);
if (IS_ERR(name)) {
return PTR_ERR(name);
}
for (i = strlen(name) - 1; i >= 0 && name[i] == '/'; i--) {
name[i] = '\0';
}
retval = i < 0 ? -EINVAL : blockOp(name, blocker);
putname(name);
return retval;
}
/*
*----------------------------------------------------------------------------
*
* ControlFileOpWrite --
*
* write implementation for our control file. This accepts either add or
* delete commands and the buffer contains the file to block.
*
* Results:
* Zero on success, negative error code on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
ssize_t
ControlFileOpWrite(struct file *file, // IN: Opened file, used for ID
const char __user *buf, // IN: NUL-terminated filename
size_t cmd, // IN: VMBlock command (usually count)
loff_t *ppos) // IN/OUT: File offset (unused)
{
int ret;
switch (cmd) {
case VMBLOCK_ADD_FILEBLOCK:
ret = ExecuteBlockOp(buf, file, BlockAddFileBlock);
break;
case VMBLOCK_DEL_FILEBLOCK:
ret = ExecuteBlockOp(buf, file, BlockRemoveFileBlock);
break;
#ifdef VMX86_DEVEL
case VMBLOCK_LIST_FILEBLOCKS:
BlockListFileBlocks();
ret = 0;
break;
#endif
default:
Warning("ControlFileOpWrite: unrecognized command (%u) recieved\n",
(unsigned)cmd);
ret = -EINVAL;
break;
}
return ret;
}
/*
*----------------------------------------------------------------------------
*
* ControlFileOpRelease --
*
* Called when the file is closed.
*
* Results:
* Zero on success, negative error code on failure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
static int
ControlFileOpRelease(struct inode *inode, // IN
struct file *file) // IN
{
BlockRemoveAllBlocks(file);
return 0;
}
|