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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
/*
* Copyright (c) 2003-2005 Erez Zadok
* Copyright (c) 2003-2005 Charles P. Wright
* Copyright (c) 2003-2005 Mohammad Nayyer Zubair
* Copyright (c) 2003-2005 Puja Gupta
* Copyright (c) 2003-2005 Harikesavan Krishnan
* Copyright (c) 2003-2005 Stony Brook University
* Copyright (c) 2003-2005 The Research Foundation of State University of New York
*
* For specific licensing information, see the COPYING file distributed with
* this package.
*
* This Copyright notice must be kept intact and distributed with all sources.
*/
/*
* $Id: branchman.c,v 1.23 2005/02/08 15:17:38 cwright Exp $
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include "fist.h"
#include "unionfs.h"
#include <linux/dcache.h>
int unionfs_ioctl_branchcount(inode_t *inode, file_t *file, unsigned int cmd, unsigned long arg)
{
struct unionfs_sb_info *spd;
int err = 0;
int bstart, bend;
int i;
print_entry_location();
spd = stopd(inode->i_sb);
bstart = spd->b_start;
bend = spd->b_end;
err = bend + 1;
if (!arg) {
goto out;
}
fist_dprint(1, "b_start = %d, b_end = %d\n", bstart, bend);
for (i = bstart; i <= bend; i++) {
if (put_user(atomic_read(&spd->usi_sbcount[i]), ((int *)arg) + i)) {
err = -EFAULT;
goto out;
}
}
out:
print_exit_status(err);
return err;
}
int unionfs_ioctl_incgen(inode_t *inode, file_t *file, unsigned int cmd, unsigned long arg)
{
int err = 0;
print_entry_location();
lock_super(inode->i_sb);
atomic_inc(&stopd(inode->i_sb)->usi_generation);
err = atomic_read(&stopd(inode->i_sb)->usi_generation);
atomic_set(&dtopd(inode->i_sb->s_root)->udi_generation, err);
atomic_set(&itopd(inode->i_sb->s_root->d_inode)->uii_generation, err);
unlock_super(inode->i_sb);
print_exit_status(err);
return err;
}
int unionfs_ioctl_addbranch(inode_t *inode, file_t *unused_file, unsigned int cmd, unsigned long arg)
{
int err = 0;
struct unionfs_sb_info *spd;
struct unionfs_dentry_info *dpd;
struct unionfs_inode_info *ipd;
struct unionfs_addbranch_args *addargs = NULL;
struct nameidata nd;
struct vfsmount **new_hidden_mnt = NULL;
struct inode **new_uii_inode = NULL;
struct dentry **new_udi_dentry = NULL;
struct super_block **new_usi_sb = NULL;
int *new_branchperms = NULL;
atomic_t *new_counts = NULL;
char *path = NULL;
int gen;
int i;
int count;
print_entry_location();
/* If we ever use this we have problems! */
unused_file = EXPLOSIVE;
lock_super(inode->i_sb);
spd = stopd(inode->i_sb);
dpd = dtopd(inode->i_sb->s_root);
ipd = itopd(inode->i_sb->s_root->d_inode);
addargs = KMALLOC(sizeof(struct unionfs_addbranch_args), GFP_UNIONFS);
if (!addargs) {
err = -ENOMEM;
goto out;
}
if (copy_from_user(addargs, (void *)arg, sizeof(struct unionfs_addbranch_args))) {
err = -EFAULT;
goto out;
}
path = getname(addargs->ab_path);
if (!path) {
err = -ENOMEM;
goto out;
}
/* Add a branch. */
if (addargs->ab_branch < 0 || (addargs->ab_branch > (spd->b_end + 1))) {
err = -EINVAL;
goto out;
}
if ((spd->b_end + 1) > FD_SETSIZE) {
err = -E2BIG;
goto out;
}
if (addargs->ab_perms & ~(MAY_READ|MAY_WRITE)) {
err = -EINVAL;
goto out;
}
if (!(addargs->ab_perms & MAY_READ)) {
err = -EINVAL;
goto out;
}
//DQ: 2.6 has a different way of doing this
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
/* Look it up */
if (path_init(path, LOOKUP_FOLLOW, &nd)) {
err = path_walk(path, &nd);
}
#else
err = path_lookup(path, LOOKUP_FOLLOW, &nd);
#endif
if (err) {
goto out;
}
if (!nd.dentry->d_inode) {
path_release(&nd);
err = -ENOENT;
goto out;
}
if (!S_ISDIR(nd.dentry->d_inode->i_mode)) {
path_release(&nd);
err = -ENOTDIR;
goto out;
}
spd->b_end++;
dtopd(inode->i_sb->s_root)->udi_bcount++;
set_dbend(inode->i_sb->s_root, dbend(inode->i_sb->s_root) + 1);
ipd->b_end++;
atomic_inc(&spd->usi_generation);
gen = atomic_read(&spd->usi_generation);
/* Reallocate the dynamic structures. */
new_hidden_mnt = KMALLOC(sizeof(struct hidden_mnt *) * (spd->b_end + 1), GFP_UNIONFS);
new_udi_dentry = KMALLOC(sizeof(struct dentry *) * (spd->b_end + 1), GFP_UNIONFS);
new_uii_inode = KMALLOC(sizeof(struct inode *) * (spd->b_end + 1), GFP_UNIONFS);
new_usi_sb = KMALLOC(sizeof(struct super_block *) * (spd->b_end + 1), GFP_UNIONFS);
new_counts = KMALLOC(sizeof(atomic_t) * (spd->b_end + 1), GFP_UNIONFS);
new_branchperms = KMALLOC(sizeof(int) * (spd->b_end + 1), GFP_UNIONFS);
if (!new_hidden_mnt || !new_udi_dentry || !new_uii_inode || !new_counts || !new_usi_sb || !new_branchperms) {
err = -ENOMEM;
goto out;
}
/* Copy the values to our new structure, but shift some to the right. */
for (i = 0; i < addargs->ab_branch; i++) {
count = atomic_read(&(spd->usi_sbcount[i]));
atomic_set(&(new_counts[i]), count);
new_branchperms[i] = spd->usi_branchperms[i];
new_hidden_mnt[i] = spd->usi_hidden_mnt[i];
new_usi_sb[i] = spd->usi_sb[i];
new_udi_dentry[i] = dpd->udi_dentry[i];
new_uii_inode[i] = ipd->uii_inode[i];
}
for (i = addargs->ab_branch; i < spd->b_end; i++) {
count = atomic_read(&(spd->usi_sbcount[i]));
atomic_set(&(new_counts[i + 1]), count);
new_branchperms[i + 1] = spd->usi_branchperms[i];
new_hidden_mnt[i + 1] = spd->usi_hidden_mnt[i];
new_usi_sb[i + 1] = spd->usi_sb[i];
new_udi_dentry[i + 1] = dpd->udi_dentry[i];
new_uii_inode[i + 1] = ipd->uii_inode[i];
}
/* Put the new dentry information into it's slot. */
new_udi_dentry[addargs->ab_branch] = nd.dentry;
new_uii_inode[addargs->ab_branch] = igrab(nd.dentry->d_inode);
new_hidden_mnt[addargs->ab_branch] = nd.mnt;
new_usi_sb[addargs->ab_branch] = nd.dentry->d_sb;
new_branchperms[addargs->ab_branch] = addargs->ab_perms;
atomic_set(&new_counts[addargs->ab_branch], 0);
/* Free the pointers. */
KFREE(dpd->udi_dentry);
KFREE(ipd->uii_inode);
KFREE(spd->usi_hidden_mnt);
KFREE(spd->usi_sb);
KFREE(spd->usi_branchperms);
/* Update the real pointers. */
dpd->udi_dentry = new_udi_dentry;
ipd->uii_inode = new_uii_inode;
spd->usi_hidden_mnt = new_hidden_mnt;
spd->usi_sb = new_usi_sb;
spd->usi_sbcount = new_counts;
spd->usi_branchperms = new_branchperms;
/* Re-NULL the new ones so we don't try to free them. */
new_hidden_mnt = NULL;
new_udi_dentry = NULL;
new_usi_sb = NULL;
new_uii_inode = NULL;
new_counts = NULL;
new_branchperms = NULL;
atomic_set(&dpd->udi_generation, gen);
atomic_set(&ipd->uii_generation, gen);
out:
unlock_super(inode->i_sb);
if (new_hidden_mnt) {
KFREE(new_hidden_mnt);
}
if (new_udi_dentry) {
KFREE(new_udi_dentry);
}
if (new_uii_inode) {
KFREE(new_uii_inode);
}
if (new_usi_sb) {
KFREE(new_usi_sb);
}
if (new_counts) {
KFREE(new_counts);
}
if (new_branchperms) {
KFREE(new_branchperms);
}
if (addargs) {
KFREE(addargs);
}
if (path) {
putname(path);
}
print_exit_status(err);
return err;
}
int unionfs_ioctl_delbranch(inode_t *inode, file_t *unused_file, unsigned int cmd, unsigned long arg)
{
struct dentry *hidden_dentry;
struct inode *hidden_inode;
struct super_block *hidden_sb;
vfs_mount_t *hidden_mnt;
struct unionfs_sb_info *spd;
struct unionfs_dentry_info *dpd;
struct unionfs_inode_info *ipd;
int err = 0;
int i;
int gen;
int count;
print_entry("branch = %lu ", arg); /* Delete a branch. */
/* If we ever use this we have problems! */
unused_file = EXPLOSIVE;
lock_super(inode->i_sb);
spd = stopd(inode->i_sb);
dpd = dtopd(inode->i_sb->s_root);
ipd = itopd(inode->i_sb->s_root->d_inode);
if (sbmax(inode->i_sb) == 1) {
err = -EBUSY;
goto out;
}
/* Delete a branch. */
if (arg < 0 || arg > spd->b_end) {
err = -EINVAL;
goto out;
}
if (atomic_read(&(spd->usi_sbcount[arg]))) {
err = -EBUSY;
goto out;
}
atomic_inc(&spd->usi_generation);
gen = atomic_read(&spd->usi_generation);
hidden_dentry = dpd->udi_dentry[arg];
hidden_mnt = spd->usi_hidden_mnt[arg];
hidden_inode = ipd->uii_inode[arg];
hidden_sb = spd->usi_sb[arg];
dput(hidden_dentry);
iput(hidden_inode);
mntput(hidden_mnt);
//XXX: Leak! put_super(hidden_sb);
for (i = arg; i <= (spd->b_end - 1); i++) {
count = atomic_read(&(spd->usi_sbcount[i]));
atomic_set(&(spd->usi_sbcount[i]), count);
spd->usi_hidden_mnt[i] = spd->usi_hidden_mnt[i + 1];
spd->usi_sb[i] = spd->usi_sb[i + 1];
spd->usi_branchperms[i] = spd->usi_branchperms[i + 1];
dpd->udi_dentry[i] = dpd->udi_dentry[i + 1];
ipd->uii_inode[i] = ipd->uii_inode[i + 1];
}
dpd->udi_dentry[spd->b_end] = EXPLOSIVE;
ipd->uii_inode[spd->b_end] = EXPLOSIVE;
spd->usi_hidden_mnt[spd->b_end] = EXPLOSIVE;
spd->b_end--;
set_dbend(inode->i_sb->s_root, dbend(inode->i_sb->s_root) - 1);
dtopd(inode->i_sb->s_root)->udi_bcount--;
ipd->b_end--;
atomic_set(&dpd->udi_generation, gen);
atomic_set(&ipd->uii_generation, gen);
out:
unlock_super(inode->i_sb);
print_exit_status(err);
return err;
}
int unionfs_ioctl_rdwrbranch(inode_t *inode, file_t *unused_file, unsigned int cmd, unsigned long arg)
{
int err = 0;
struct unionfs_sb_info *spd;
struct unionfs_dentry_info *dpd;
struct unionfs_inode_info *ipd;
struct unionfs_rdwrbranch_args *rdwrargs = NULL;
int gen;
print_entry_location();
/* If we ever use this we have problems! */
unused_file = EXPLOSIVE;
lock_super(inode->i_sb);
spd = stopd(inode->i_sb);
dpd = dtopd(inode->i_sb->s_root);
ipd = itopd(inode->i_sb->s_root->d_inode);
rdwrargs = KMALLOC(sizeof(struct unionfs_rdwrbranch_args), GFP_UNIONFS);
if (!rdwrargs) {
err = -ENOMEM;
goto out;
}
if (copy_from_user(rdwrargs, (void *)arg, sizeof(struct unionfs_rdwrbranch_args))) {
err = -EFAULT;
goto out;
}
if (rdwrargs->rwb_branch < 0 || (rdwrargs->rwb_branch > (spd->b_end + 1))) {
err = -EINVAL;
goto out;
}
if (rdwrargs->rwb_perms & ~(MAY_READ|MAY_WRITE)) {
err = -EINVAL;
goto out;
}
if (!(rdwrargs->rwb_perms & MAY_READ)) {
err = -EINVAL;
goto out;
}
spd->usi_branchperms[rdwrargs->rwb_branch] = rdwrargs->rwb_perms;
atomic_inc(&spd->usi_generation);
gen = atomic_read(&spd->usi_generation);
atomic_set(&dpd->udi_generation, gen);
atomic_set(&ipd->uii_generation, gen);
out:
unlock_super(inode->i_sb);
if (rdwrargs) {
KFREE(rdwrargs);
}
print_exit_status(err);
return err;
}
int unionfs_ioctl_superduper(inode_t *inode, file_t *file, unsigned int cmd, unsigned long arg) {
int err = 0;
#ifdef SPLIT_VIEW_CACHES
struct super_block *ret_sb;
#endif
print_entry_location();
#ifdef SPLIT_VIEW_CACHES
lock_super(inode->i_sb);
ret_sb = unionfs_duplicate_super(inode->i_sb);
if (IS_ERR(ret_sb)) {
err = PTR_ERR(ret_sb);
}
unlock_super(inode->i_sb);
#else
err = -ENOSYS;
#endif
print_exit_status(err);
return err;
}
|