| 1 |
#include <linux/module.h> |
| 2 |
#include <linux/fs.h> |
| 3 |
#include <linux/file.h> |
| 4 |
#include <linux/namei.h> |
| 5 |
#include <linux/init.h> |
| 6 |
#include <linux/mount.h> |
| 7 |
#include <linux/security.h> |
| 8 |
|
| 9 |
#include <asm/uaccess.h> |
| 10 |
|
| 11 |
/* some random number */ |
| 12 |
#define GLASSFS_MAGIC 0x28476c61 |
| 13 |
#define FUSE_MAGIC 0x65735546 |
| 14 |
|
| 15 |
#define AVFS_MAGIC_CHAR '#' |
| 16 |
#define OVERLAY_DIR "/overlay" |
| 17 |
#define OVERLAY_DIR_LEN 8 |
| 18 |
|
| 19 |
struct base_entry { |
| 20 |
struct dentry *dentry; |
| 21 |
struct vfsmount *mnt; |
| 22 |
int is_avfs; |
| 23 |
}; |
| 24 |
|
| 25 |
/* Rules for storing underlying filesystem data: |
| 26 |
* |
| 27 |
* inode->u.generic_ip: contains a kmalloced base_entry struct |
| 28 |
* sb->s_fs_info: contains the underlying superblock |
| 29 |
*/ |
| 30 |
|
| 31 |
static struct super_operations glassfs_ops; |
| 32 |
static struct inode_operations glassfs_file_inode_operations; |
| 33 |
static struct file_operations glassfs_file_operations; |
| 34 |
static struct file_operations glassfs_dir_operations; |
| 35 |
static struct inode_operations glassfs_dir_inode_operations; |
| 36 |
static struct inode_operations glassfs_symlink_inode_operations; |
| 37 |
static struct dentry_operations glassfs_dentry_operations; |
| 38 |
|
| 39 |
static struct inode *glassfs_alloc_inode(struct super_block *sb) |
| 40 |
{ |
| 41 |
struct base_entry *be; |
| 42 |
struct inode * inode = new_inode(sb); |
| 43 |
if (!inode) |
| 44 |
return NULL; |
| 45 |
|
| 46 |
be = kmalloc(sizeof(struct base_entry), GFP_KERNEL); |
| 47 |
if (!be) { |
| 48 |
iput(inode); |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
inode->u.generic_ip = be; |
| 52 |
be->mnt = NULL; |
| 53 |
be->dentry = NULL; |
| 54 |
|
| 55 |
return inode; |
| 56 |
} |
| 57 |
|
| 58 |
static void glassfs_fill_inode(struct inode *inode, int mode, dev_t dev, |
| 59 |
struct dentry *ndentry, struct vfsmount *nmnt, |
| 60 |
int is_avfs) |
| 61 |
{ |
| 62 |
struct base_entry *be = inode->u.generic_ip; |
| 63 |
|
| 64 |
be->dentry = dget(ndentry); |
| 65 |
be->mnt = mntget(nmnt); |
| 66 |
be->is_avfs = is_avfs; |
| 67 |
|
| 68 |
inode->i_mode = mode; |
| 69 |
inode->i_rdev = dev; |
| 70 |
|
| 71 |
switch (mode & S_IFMT) { |
| 72 |
default: |
| 73 |
inode->i_op = &glassfs_file_inode_operations; |
| 74 |
inode->i_fop = &glassfs_file_operations; |
| 75 |
break; |
| 76 |
case S_IFDIR: |
| 77 |
inode->i_op = &glassfs_dir_inode_operations; |
| 78 |
inode->i_fop = &glassfs_dir_operations; |
| 79 |
break; |
| 80 |
case S_IFLNK: |
| 81 |
inode->i_op = &glassfs_symlink_inode_operations; |
| 82 |
inode->i_fop = &glassfs_file_operations; |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static void change_list(struct list_head *oldlist, struct list_head *newlist) |
| 88 |
{ |
| 89 |
struct list_head *prev = oldlist->prev; |
| 90 |
struct list_head *next = oldlist->next; |
| 91 |
prev->next = newlist; |
| 92 |
next->prev = newlist; |
| 93 |
} |
| 94 |
|
| 95 |
static void exchange_lists(struct list_head *list1, struct list_head *list2) |
| 96 |
{ |
| 97 |
change_list(list1, list2); |
| 98 |
change_list(list2, list1); |
| 99 |
} |
| 100 |
|
| 101 |
static void exchange_files(struct file *file1, struct file *file2) |
| 102 |
{ |
| 103 |
struct file tmp; |
| 104 |
|
| 105 |
exchange_lists(&file1->f_list, &file2->f_list); |
| 106 |
exchange_lists(&file1->f_ep_links, &file2->f_ep_links); |
| 107 |
|
| 108 |
tmp = *file1; |
| 109 |
*file1 = *file2; |
| 110 |
*file2 = tmp; |
| 111 |
} |
| 112 |
|
| 113 |
static int glassfs_open(struct inode *inode, struct file *file) |
| 114 |
{ |
| 115 |
struct base_entry *be = inode->u.generic_ip; |
| 116 |
struct vfsmount *nmnt = mntget(be->mnt); |
| 117 |
struct dentry *ndentry = dget(be->dentry); |
| 118 |
struct file *nfile; |
| 119 |
//printk("glassfs_open %.*s\n", ndentry->d_name.len, ndentry->d_name.name); |
| 120 |
nfile = dentry_open(ndentry, nmnt, file->f_flags); |
| 121 |
if (IS_ERR(nfile)) |
| 122 |
return PTR_ERR(nfile); |
| 123 |
|
| 124 |
exchange_files(file, nfile); |
| 125 |
fput(nfile); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static int glassfs_open_dir(struct inode *inode, struct file *file) |
| 131 |
{ |
| 132 |
struct base_entry *be = inode->u.generic_ip; |
| 133 |
struct file *nfile; |
| 134 |
//printk("glassfs_open_dir\n") |
| 135 |
nfile = dentry_open(dget(be->dentry), mntget(be->mnt), file->f_flags); |
| 136 |
if (IS_ERR(nfile)) |
| 137 |
return PTR_ERR(nfile); |
| 138 |
|
| 139 |
file->private_data = nfile; |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
static int glassfs_release(struct inode *inode, struct file *file) |
| 144 |
{ |
| 145 |
struct file *nfile = file->private_data; |
| 146 |
return filp_close(nfile, current->files); |
| 147 |
} |
| 148 |
|
| 149 |
static int glassfs_readdir(struct file *file, void *buf, filldir_t filler) |
| 150 |
{ |
| 151 |
struct file *nfile = file->private_data; |
| 152 |
return vfs_readdir(nfile, filler, buf); |
| 153 |
} |
| 154 |
|
| 155 |
static loff_t glassfs_llseek(struct file *file, loff_t offset, int origin) |
| 156 |
{ |
| 157 |
struct file *nfile = file->private_data; |
| 158 |
loff_t (*fn)(struct file *, loff_t, int); |
| 159 |
|
| 160 |
fn = default_llseek; |
| 161 |
if (nfile->f_op && nfile->f_op->llseek) |
| 162 |
fn = nfile->f_op->llseek; |
| 163 |
return fn(nfile, offset, origin); |
| 164 |
} |
| 165 |
|
| 166 |
static int glassfs_ioctl(struct inode *inode, struct file *file, |
| 167 |
unsigned int cmd, unsigned long arg) |
| 168 |
{ |
| 169 |
int err; |
| 170 |
struct file *nfile = file->private_data; |
| 171 |
|
| 172 |
err = security_file_ioctl(nfile, cmd, arg); |
| 173 |
if (err) |
| 174 |
return err; |
| 175 |
|
| 176 |
err = -ENOTTY; |
| 177 |
if (nfile->f_op && nfile->f_op->ioctl) |
| 178 |
err = nfile->f_op->ioctl(nfile->f_dentry->d_inode, nfile, |
| 179 |
cmd, arg); |
| 180 |
return err; |
| 181 |
} |
| 182 |
|
| 183 |
static int glassfs_fsync(struct file *file, struct dentry *dentry, |
| 184 |
int datasync) |
| 185 |
{ |
| 186 |
int err; |
| 187 |
struct file *nfile = file->private_data; |
| 188 |
struct address_space *mapping = nfile->f_mapping; |
| 189 |
|
| 190 |
if (!nfile->f_op || !nfile->f_op->fsync) |
| 191 |
return -EINVAL; |
| 192 |
down(&mapping->host->i_sem); |
| 193 |
err = nfile->f_op->fsync(nfile, nfile->f_dentry, datasync); |
| 194 |
up(&mapping->host->i_sem); |
| 195 |
return err; |
| 196 |
} |
| 197 |
|
| 198 |
static int glassfs_flush(struct file *file) |
| 199 |
{ |
| 200 |
//printk("glassfs_flush\n"); |
| 201 |
struct file *nfile = file->private_data; |
| 202 |
if (nfile->f_op && nfile->f_op->flush) |
| 203 |
return nfile->f_op->flush(nfile); |
| 204 |
else |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
static int is_avfs(const unsigned char *name, unsigned int len) |
| 210 |
{ |
| 211 |
for (; len--; name++) |
| 212 |
if (*name == (unsigned char) AVFS_MAGIC_CHAR) |
| 213 |
return 1; |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
static char * my_d_path( struct dentry *dentry, struct vfsmount *vfsmnt, |
| 219 |
struct dentry *root, struct vfsmount *rootmnt, |
| 220 |
char *buffer, int buflen) |
| 221 |
{ |
| 222 |
char * end = buffer+buflen; |
| 223 |
char * retval; |
| 224 |
int namelen; |
| 225 |
|
| 226 |
*--end = '\0'; |
| 227 |
buflen--; |
| 228 |
if (!IS_ROOT(dentry) && d_unhashed(dentry)) { |
| 229 |
buflen -= 10; |
| 230 |
end -= 10; |
| 231 |
if (buflen < 0) |
| 232 |
goto Elong; |
| 233 |
memcpy(end, " (deleted)", 10); |
| 234 |
} |
| 235 |
|
| 236 |
if (buflen < 1) |
| 237 |
goto Elong; |
| 238 |
/* Get '/' right */ |
| 239 |
retval = end-1; |
| 240 |
*retval = '/'; |
| 241 |
|
| 242 |
for (;;) { |
| 243 |
struct dentry * parent; |
| 244 |
|
| 245 |
if (dentry == root && vfsmnt == rootmnt) |
| 246 |
break; |
| 247 |
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
| 248 |
/* Global root? */ |
| 249 |
spin_lock(&vfsmount_lock); |
| 250 |
if (vfsmnt->mnt_parent == vfsmnt) { |
| 251 |
spin_unlock(&vfsmount_lock); |
| 252 |
goto global_root; |
| 253 |
} |
| 254 |
dentry = vfsmnt->mnt_mountpoint; |
| 255 |
vfsmnt = vfsmnt->mnt_parent; |
| 256 |
spin_unlock(&vfsmount_lock); |
| 257 |
continue; |
| 258 |
} |
| 259 |
parent = dentry->d_parent; |
| 260 |
prefetch(parent); |
| 261 |
namelen = dentry->d_name.len; |
| 262 |
buflen -= namelen + 1; |
| 263 |
if (buflen < 0) |
| 264 |
goto Elong; |
| 265 |
end -= namelen; |
| 266 |
memcpy(end, dentry->d_name.name, namelen); |
| 267 |
*--end = '/'; |
| 268 |
retval = end; |
| 269 |
dentry = parent; |
| 270 |
} |
| 271 |
|
| 272 |
return retval; |
| 273 |
|
| 274 |
global_root: |
| 275 |
namelen = dentry->d_name.len; |
| 276 |
buflen -= namelen; |
| 277 |
if (buflen < 0) |
| 278 |
goto Elong; |
| 279 |
retval -= namelen-1; /* hit the slash */ |
| 280 |
memcpy(retval, dentry->d_name.name, namelen); |
| 281 |
return retval; |
| 282 |
Elong: |
| 283 |
return ERR_PTR(-ENAMETOOLONG); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
static int lookup_avfs(struct dentry *dentry, struct nameidata *nd, |
| 288 |
struct vfsmount *nmnt, struct base_entry *be) |
| 289 |
{ |
| 290 |
int err; |
| 291 |
char *page; |
| 292 |
char *path; |
| 293 |
|
| 294 |
err = -ENOMEM; |
| 295 |
page = (char *) __get_free_page(GFP_KERNEL); |
| 296 |
if (page) { |
| 297 |
struct nameidata avfsnd; |
| 298 |
unsigned int offset = PAGE_SIZE - dentry->d_name.len - 2; |
| 299 |
spin_lock(&dcache_lock); |
| 300 |
path = my_d_path(nd->dentry,nd->mnt, nd->mnt->mnt_sb->s_root, nd->mnt, page, offset); |
| 301 |
spin_unlock(&dcache_lock); |
| 302 |
err = -ENAMETOOLONG; |
| 303 |
if (!IS_ERR(path) && page + OVERLAY_DIR_LEN < path) { |
| 304 |
unsigned int pathlen = strlen(path); |
| 305 |
path[pathlen] = '/'; |
| 306 |
memcpy(path + pathlen + 1, dentry->d_name.name, |
| 307 |
dentry->d_name.len); |
| 308 |
path[1 + pathlen + dentry->d_name.len] = '\0'; |
| 309 |
path -= OVERLAY_DIR_LEN; |
| 310 |
memcpy(path, OVERLAY_DIR, OVERLAY_DIR_LEN); |
| 311 |
|
| 312 |
//printk("lookup_avfs: '%s'\n", path); |
| 313 |
avfsnd.last_type = LAST_ROOT; |
| 314 |
avfsnd.flags = 0; |
| 315 |
avfsnd.mnt = mntget(nmnt); |
| 316 |
avfsnd.dentry = dget(nmnt->mnt_sb->s_root); |
| 317 |
|
| 318 |
err = path_walk(path, &avfsnd); |
| 319 |
if (!err) { |
| 320 |
if (avfsnd.dentry->d_sb->s_magic != FUSE_MAGIC) { |
| 321 |
printk("glassfs: not an AVFS inode\n"); |
| 322 |
path_release(&avfsnd); |
| 323 |
err = -ENOENT; |
| 324 |
} else { |
| 325 |
be->mnt = avfsnd.mnt; |
| 326 |
be->dentry = avfsnd.dentry; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
free_page((unsigned long) page); |
| 331 |
} |
| 332 |
return err; |
| 333 |
} |
| 334 |
|
| 335 |
static inline int is_create(struct nameidata *nd) |
| 336 |
{ |
| 337 |
if (!nd) |
| 338 |
return 1; |
| 339 |
if ((nd->flags & LOOKUP_CREATE) && !(nd->flags & LOOKUP_CONTINUE)) |
| 340 |
return 1; |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
static struct dentry *glassfs_lookup(struct inode *dir, struct dentry *dentry, |
| 345 |
struct nameidata *nd) |
| 346 |
{ |
| 347 |
struct base_entry *be = dir->u.generic_ip; |
| 348 |
struct dentry *ndentry; |
| 349 |
struct vfsmount *nmnt; |
| 350 |
struct inode *inode = NULL; |
| 351 |
int curravfs = be->is_avfs; |
| 352 |
|
| 353 |
// printk("glassfs_lookup\n"); |
| 354 |
down(&be->dentry->d_inode->i_sem); |
| 355 |
ndentry = lookup_hash(&dentry->d_name, be->dentry); |
| 356 |
up(&be->dentry->d_inode->i_sem); |
| 357 |
if (IS_ERR(ndentry)) |
| 358 |
return ndentry; |
| 359 |
|
| 360 |
nmnt = mntget(be->mnt); |
| 361 |
if (!is_create(nd) && !ndentry->d_inode && !curravfs && |
| 362 |
is_avfs(dentry->d_name.name, dentry->d_name.len)) { |
| 363 |
struct base_entry avfsbe; |
| 364 |
int err; |
| 365 |
int total_link_count_save = current->total_link_count; |
| 366 |
err = lookup_avfs(dentry, nd, nmnt, &avfsbe); |
| 367 |
current->total_link_count = total_link_count_save; |
| 368 |
if (!err) { |
| 369 |
if (avfsbe.dentry->d_inode) { |
| 370 |
dput(ndentry); |
| 371 |
mntput(nmnt); |
| 372 |
ndentry = avfsbe.dentry; |
| 373 |
nmnt = avfsbe.mnt; |
| 374 |
curravfs = 1; |
| 375 |
} else { |
| 376 |
dput(avfsbe.dentry); |
| 377 |
mntput(avfsbe.mnt); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
if (ndentry->d_inode) { |
| 383 |
struct inode *ninode = ndentry->d_inode; |
| 384 |
inode = glassfs_alloc_inode(dir->i_sb); |
| 385 |
if (!inode) { |
| 386 |
dput(ndentry); |
| 387 |
mntput(nmnt); |
| 388 |
return ERR_PTR(-ENOMEM); |
| 389 |
} |
| 390 |
glassfs_fill_inode(inode, ninode->i_mode, ninode->i_rdev, |
| 391 |
ndentry, nmnt, curravfs); |
| 392 |
} |
| 393 |
|
| 394 |
dentry->d_op = &glassfs_dentry_operations; |
| 395 |
dput(ndentry); |
| 396 |
mntput(nmnt); |
| 397 |
|
| 398 |
return d_splice_alias(inode, dentry); |
| 399 |
} |
| 400 |
|
| 401 |
static int glassfs_permission(struct inode *inode, int mask, |
| 402 |
struct nameidata *nd) |
| 403 |
{ |
| 404 |
struct base_entry *be = inode->u.generic_ip; |
| 405 |
struct inode *ninode = be->dentry->d_inode; |
| 406 |
// printk("glassfs_permission\n"); |
| 407 |
/* Exec needs i_mode to contain the correct permission bits */ |
| 408 |
inode->i_mode = ninode->i_mode; |
| 409 |
return permission(ninode, mask, NULL); |
| 410 |
} |
| 411 |
|
| 412 |
static int glassfs_create(struct inode *dir, struct dentry *dentry, |
| 413 |
int mode, struct nameidata *nd) |
| 414 |
{ |
| 415 |
int err; |
| 416 |
struct base_entry *be = dir->u.generic_ip; |
| 417 |
struct inode *ndir = be->dentry->d_inode; |
| 418 |
struct dentry *ndentry; |
| 419 |
struct inode *inode = glassfs_alloc_inode(dir->i_sb); |
| 420 |
if (!inode) |
| 421 |
return -ENOMEM; |
| 422 |
|
| 423 |
// printk("glassfs_create\n"); |
| 424 |
down(&ndir->i_sem); |
| 425 |
ndentry = lookup_hash(&dentry->d_name, be->dentry); |
| 426 |
if (IS_ERR(ndentry)) { |
| 427 |
err = PTR_ERR(ndentry); |
| 428 |
ndentry = NULL; |
| 429 |
} |
| 430 |
else |
| 431 |
err = vfs_create(ndir, ndentry, mode, NULL); |
| 432 |
up(&ndir->i_sem); |
| 433 |
if (!err && ndentry->d_inode) { |
| 434 |
struct inode *ninode = ndentry->d_inode; |
| 435 |
glassfs_fill_inode(inode, ninode->i_mode, ninode->i_rdev, |
| 436 |
ndentry, be->mnt, be->is_avfs); |
| 437 |
d_instantiate(dentry, inode); |
| 438 |
} else |
| 439 |
iput(inode); |
| 440 |
|
| 441 |
dput(ndentry); |
| 442 |
|
| 443 |
return err; |
| 444 |
} |
| 445 |
|
| 446 |
static int glassfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 447 |
{ |
| 448 |
int err; |
| 449 |
struct base_entry *be = dir->u.generic_ip; |
| 450 |
struct inode *ndir = be->dentry->d_inode; |
| 451 |
struct dentry *ndentry; |
| 452 |
struct inode *inode = glassfs_alloc_inode(dir->i_sb); |
| 453 |
if (!inode) |
| 454 |
return -ENOMEM; |
| 455 |
|
| 456 |
// printk("glassfs_mkdir\n"); |
| 457 |
down(&ndir->i_sem); |
| 458 |
ndentry = lookup_hash(&dentry->d_name, be->dentry); |
| 459 |
if (IS_ERR(ndentry)) { |
| 460 |
err = PTR_ERR(ndentry); |
| 461 |
ndentry = NULL; |
| 462 |
} else |
| 463 |
err = vfs_mkdir(ndir, ndentry, mode); |
| 464 |
up(&ndir->i_sem); |
| 465 |
if (!err && ndentry->d_inode) { |
| 466 |
struct inode *ninode = ndentry->d_inode; |
| 467 |
glassfs_fill_inode(inode, ninode->i_mode, ninode->i_rdev, |
| 468 |
ndentry, be->mnt, be->is_avfs); |
| 469 |
d_instantiate(dentry, inode); |
| 470 |
} else |
| 471 |
iput(inode); |
| 472 |
|
| 473 |
dput(ndentry); |
| 474 |
|
| 475 |
return err; |
| 476 |
} |
| 477 |
|
| 478 |
static int glassfs_link(struct dentry *old_dentry, struct inode *dir, |
| 479 |
struct dentry *new_dentry) |
| 480 |
{ |
| 481 |
int err; |
| 482 |
struct base_entry *be = dir->u.generic_ip; |
| 483 |
struct base_entry *old_be = old_dentry->d_inode->u.generic_ip; |
| 484 |
struct inode *ndir = be->dentry->d_inode; |
| 485 |
struct dentry *new_ndentry; |
| 486 |
struct inode *inode; |
| 487 |
|
| 488 |
if (be->mnt != old_be->mnt) |
| 489 |
return -EXDEV; |
| 490 |
|
| 491 |
inode = glassfs_alloc_inode(dir->i_sb); |
| 492 |
if (!inode) |
| 493 |
return -ENOMEM; |
| 494 |
|
| 495 |
// printk("glassfs_link\n"); |
| 496 |
down(&ndir->i_sem); |
| 497 |
new_ndentry = lookup_hash(&new_dentry->d_name, be->dentry); |
| 498 |
if (IS_ERR(new_ndentry)) { |
| 499 |
err = PTR_ERR(new_ndentry); |
| 500 |
new_ndentry = NULL; |
| 501 |
} else |
| 502 |
err = vfs_link(old_be->dentry, ndir, new_ndentry); |
| 503 |
up(&ndir->i_sem); |
| 504 |
if (!err && new_ndentry->d_inode) { |
| 505 |
struct inode *ninode = new_ndentry->d_inode; |
| 506 |
glassfs_fill_inode(inode, ninode->i_mode, ninode->i_rdev, |
| 507 |
new_ndentry, be->mnt, be->is_avfs); |
| 508 |
d_instantiate(new_dentry, inode); |
| 509 |
} else |
| 510 |
iput(inode); |
| 511 |
|
| 512 |
dput(new_ndentry); |
| 513 |
|
| 514 |
return err; |
| 515 |
} |
| 516 |
|
| 517 |
static int glassfs_symlink(struct inode *dir, struct dentry *dentry, |
| 518 |
const char *oldname) |
| 519 |
{ |
| 520 |
int err; |
| 521 |
struct base_entry *be = dir->u.generic_ip; |
| 522 |
struct inode *ndir = be->dentry->d_inode; |
| 523 |
struct dentry *ndentry; |
| 524 |
struct inode *inode = glassfs_alloc_inode(dir->i_sb); |
| 525 |
if (!inode) |
| 526 |
return -ENOMEM; |
| 527 |
|
| 528 |
// printk("glassfs_symlink\n"); |
| 529 |
down(&ndir->i_sem); |
| 530 |
ndentry = lookup_hash(&dentry->d_name, be->dentry); |
| 531 |
if (IS_ERR(ndentry)) { |
| 532 |
err = PTR_ERR(ndentry); |
| 533 |
ndentry = NULL; |
| 534 |
} else |
| 535 |
err = vfs_symlink(ndir, ndentry, oldname); |
| 536 |
up(&ndir->i_sem); |
| 537 |
if (!err && ndentry->d_inode) { |
| 538 |
struct inode *ninode = ndentry->d_inode; |
| 539 |
glassfs_fill_inode(inode, ninode->i_mode, ninode->i_rdev, |
| 540 |
ndentry, be->mnt, be->is_avfs); |
| 541 |
d_instantiate(dentry, inode); |
| 542 |
} else |
| 543 |
iput(inode); |
| 544 |
|
| 545 |
dput(ndentry); |
| 546 |
|
| 547 |
return err; |
| 548 |
} |
| 549 |
|
| 550 |
static int glassfs_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 551 |
dev_t dev) |
| 552 |
{ |
| 553 |
int err; |
| 554 |
struct base_entry *be = dir->u.generic_ip; |
| 555 |
struct inode *ndir = be->dentry->d_inode; |
| 556 |
struct dentry *ndentry; |
| 557 |
struct inode *inode = glassfs_alloc_inode(dir->i_sb); |
| 558 |
if (!inode) |
| 559 |
return -ENOMEM; |
| 560 |
|
| 561 |
// printk("glassfs_mknod\n"); |
| 562 |
down(&ndir->i_sem); |
| 563 |
ndentry = lookup_hash(&dentry->d_name, be->dentry); |
| 564 |
if (IS_ERR(ndentry)) { |
| 565 |
err = PTR_ERR(ndentry); |
| 566 |
ndentry = NULL; |
| 567 |
} else |
| 568 |
err = vfs_mknod(ndir, ndentry, mode, dev); |
| 569 |
up(&ndir->i_sem); |
| 570 |
if (!err && ndentry->d_inode) { |
| 571 |
struct inode *ninode = ndentry->d_inode; |
| 572 |
glassfs_fill_inode(inode, ninode->i_mode, ninode->i_rdev, |
| 573 |
ndentry, be->mnt, be->is_avfs); |
| 574 |
d_instantiate(dentry, inode); |
| 575 |
} else |
| 576 |
iput(inode); |
| 577 |
|
| 578 |
dput(ndentry); |
| 579 |
|
| 580 |
return err; |
| 581 |
} |
| 582 |
|
| 583 |
static int glassfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 584 |
struct inode *new_dir, struct dentry *new_dentry) |
| 585 |
{ |
| 586 |
int err; |
| 587 |
struct base_entry *old_be = old_dir->u.generic_ip; |
| 588 |
struct base_entry *new_be = new_dir->u.generic_ip; |
| 589 |
struct inode *old_ndir = old_be->dentry->d_inode; |
| 590 |
struct inode *new_ndir = new_be->dentry->d_inode; |
| 591 |
struct dentry *old_ndentry = NULL; |
| 592 |
struct dentry *new_ndentry = NULL; |
| 593 |
struct dentry *trap; |
| 594 |
|
| 595 |
if (old_be->mnt != new_be->mnt) |
| 596 |
return -EXDEV; |
| 597 |
|
| 598 |
trap = lock_rename(new_be->dentry, old_be->dentry); |
| 599 |
old_ndentry = lookup_hash(&old_dentry->d_name, old_be->dentry); |
| 600 |
if (IS_ERR(old_ndentry)) { |
| 601 |
err = PTR_ERR(old_ndentry); |
| 602 |
old_ndentry = NULL; |
| 603 |
goto out; |
| 604 |
} |
| 605 |
if (old_ndentry == trap) { |
| 606 |
err = -EINVAL; |
| 607 |
goto out; |
| 608 |
} |
| 609 |
if (!old_ndentry->d_inode) { |
| 610 |
err = -ENOENT; |
| 611 |
goto out; |
| 612 |
} |
| 613 |
|
| 614 |
new_ndentry = lookup_hash(&new_dentry->d_name, new_be->dentry); |
| 615 |
if (IS_ERR(new_ndentry)) { |
| 616 |
err = PTR_ERR(new_ndentry); |
| 617 |
new_ndentry = NULL; |
| 618 |
goto out; |
| 619 |
} |
| 620 |
if (new_ndentry == trap) { |
| 621 |
err = -ENOTEMPTY; |
| 622 |
goto out; |
| 623 |
} |
| 624 |
err = vfs_rename(old_ndir, old_ndentry, new_ndir, new_ndentry); |
| 625 |
|
| 626 |
out: |
| 627 |
unlock_rename(new_be->dentry, old_be->dentry); |
| 628 |
dput(new_ndentry); |
| 629 |
dput(old_ndentry); |
| 630 |
|
| 631 |
return err; |
| 632 |
} |
| 633 |
|
| 634 |
static int glassfs_unlink(struct inode *dir, struct dentry *dentry) |
| 635 |
{ |
| 636 |
int err; |
| 637 |
struct base_entry *dir_be = dir->u.generic_ip; |
| 638 |
struct inode *ndir = dir_be->dentry->d_inode; |
| 639 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 640 |
|
| 641 |
// printk("glassfs_unlink\n"); |
| 642 |
down(&ndir->i_sem); |
| 643 |
err = vfs_unlink(ndir, be->dentry); |
| 644 |
up(&ndir->i_sem); |
| 645 |
if (!err) |
| 646 |
dentry->d_inode->i_nlink = 0; |
| 647 |
|
| 648 |
return err; |
| 649 |
} |
| 650 |
|
| 651 |
static int glassfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 652 |
{ |
| 653 |
int err; |
| 654 |
struct base_entry *dir_be = dir->u.generic_ip; |
| 655 |
struct inode *ndir = dir_be->dentry->d_inode; |
| 656 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 657 |
|
| 658 |
// printk("glassfs_rmdir\n"); |
| 659 |
down(&ndir->i_sem); |
| 660 |
err = vfs_rmdir(ndir, be->dentry); |
| 661 |
up(&ndir->i_sem); |
| 662 |
if (!err) |
| 663 |
dentry->d_inode->i_nlink = 0; |
| 664 |
return err; |
| 665 |
} |
| 666 |
|
| 667 |
static int glassfs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 668 |
struct kstat *stat) |
| 669 |
{ |
| 670 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 671 |
//printk("glassfs_getattr (is avfs: %i)\n", be->is_avfs); |
| 672 |
return vfs_getattr(be->mnt, be->dentry, stat); |
| 673 |
} |
| 674 |
|
| 675 |
static int glassfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 676 |
{ |
| 677 |
int err; |
| 678 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 679 |
struct inode *ninode = be->dentry->d_inode; |
| 680 |
|
| 681 |
if (IS_RDONLY(ninode)) |
| 682 |
return -EROFS; |
| 683 |
|
| 684 |
if (IS_IMMUTABLE(ninode) || IS_APPEND(ninode)) |
| 685 |
return -EPERM; |
| 686 |
|
| 687 |
down(&ninode->i_sem); |
| 688 |
err = notify_change(be->dentry, attr); |
| 689 |
up(&ninode->i_sem); |
| 690 |
return err; |
| 691 |
} |
| 692 |
|
| 693 |
static int glassfs_setxattr(struct dentry *dentry, const char *name, |
| 694 |
const void *value, size_t size, int flags) |
| 695 |
{ |
| 696 |
int err; |
| 697 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 698 |
struct dentry *ndentry = be->dentry; |
| 699 |
struct inode *ninode = ndentry->d_inode; |
| 700 |
|
| 701 |
err = -EOPNOTSUPP; |
| 702 |
if (ninode->i_op && ninode->i_op->setxattr) { |
| 703 |
down(&ninode->i_sem); |
| 704 |
err = security_inode_setxattr(ndentry, (char *) name, (void *) value, size, flags); |
| 705 |
if (!err) { |
| 706 |
err = ninode->i_op->setxattr(ndentry, name, value, size, flags); |
| 707 |
if (!err) |
| 708 |
security_inode_post_setxattr(ndentry, (char *) name, (void *) value, size, flags); |
| 709 |
} |
| 710 |
up(&ninode->i_sem); |
| 711 |
} |
| 712 |
return err; |
| 713 |
} |
| 714 |
|
| 715 |
static ssize_t glassfs_getxattr(struct dentry *dentry, const char *name, |
| 716 |
void *value, size_t size) |
| 717 |
{ |
| 718 |
ssize_t err; |
| 719 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 720 |
struct dentry *ndentry = be->dentry; |
| 721 |
struct inode *ninode = ndentry->d_inode; |
| 722 |
|
| 723 |
err = -EOPNOTSUPP; |
| 724 |
if (ninode->i_op && ninode->i_op->getxattr) { |
| 725 |
err = security_inode_getxattr(ndentry, (char *) name); |
| 726 |
if (!err) |
| 727 |
err = ninode->i_op->getxattr(ndentry, name, value, size); |
| 728 |
} |
| 729 |
return err; |
| 730 |
} |
| 731 |
static ssize_t glassfs_listxattr(struct dentry *dentry, char *list, size_t size) |
| 732 |
{ |
| 733 |
ssize_t err; |
| 734 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 735 |
struct dentry *ndentry = be->dentry; |
| 736 |
struct inode *ninode = ndentry->d_inode; |
| 737 |
|
| 738 |
err = -EOPNOTSUPP; |
| 739 |
if (ninode->i_op && ninode->i_op->listxattr) { |
| 740 |
err = security_inode_listxattr(ndentry); |
| 741 |
if (!err) |
| 742 |
err = ninode->i_op->listxattr(ndentry, list, size); |
| 743 |
} |
| 744 |
return err; |
| 745 |
} |
| 746 |
|
| 747 |
static int glassfs_removexattr(struct dentry *dentry, const char *name) |
| 748 |
{ |
| 749 |
int err; |
| 750 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 751 |
struct dentry *ndentry = be->dentry; |
| 752 |
struct inode *ninode = ndentry->d_inode; |
| 753 |
|
| 754 |
err = -EOPNOTSUPP; |
| 755 |
if (ninode->i_op && ninode->i_op->removexattr) { |
| 756 |
err = security_inode_removexattr(ndentry, (char *) name); |
| 757 |
if (!err) { |
| 758 |
/* strange: the security... usually is within |
| 759 |
the locked region */ |
| 760 |
down(&ninode->i_sem); |
| 761 |
err = ninode->i_op->removexattr(ndentry, name); |
| 762 |
up(&ninode->i_sem); |
| 763 |
} |
| 764 |
} |
| 765 |
return err; |
| 766 |
} |
| 767 |
|
| 768 |
static int glassfs_statfs(struct super_block *sb, struct kstatfs *buf) |
| 769 |
{ |
| 770 |
struct super_block *nsb = sb->s_fs_info; |
| 771 |
// printk("glassfs_statfs\n"); |
| 772 |
return vfs_statfs(nsb, buf); |
| 773 |
} |
| 774 |
|
| 775 |
static int glassfs_dentry_revalidate(struct dentry *dentry, |
| 776 |
struct nameidata *nd) |
| 777 |
{ |
| 778 |
struct base_entry *be; |
| 779 |
//printk("glassfs_dentry_revalidate %.*s\n", dentry->d_name.len, dentry->d_name.name); |
| 780 |
|
| 781 |
if (!dentry->d_inode) |
| 782 |
return 0; |
| 783 |
|
| 784 |
be = dentry->d_inode->u.generic_ip; |
| 785 |
if (d_unhashed(be->dentry)) |
| 786 |
return 0; |
| 787 |
|
| 788 |
if (be->dentry->d_op && be->dentry->d_op->d_revalidate) |
| 789 |
return be->dentry->d_op->d_revalidate(be->dentry, NULL); |
| 790 |
return 1; |
| 791 |
} |
| 792 |
|
| 793 |
static int glassfs_readlink (struct dentry *dentry, char __user *buf, |
| 794 |
int buflen) |
| 795 |
{ |
| 796 |
|
| 797 |
int err; |
| 798 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 799 |
struct dentry *ndentry = be->dentry; |
| 800 |
struct inode *ninode = ndentry->d_inode; |
| 801 |
|
| 802 |
err = -EINVAL; |
| 803 |
if (ninode->i_op && ninode->i_op->readlink) { |
| 804 |
err = security_inode_readlink(ndentry); |
| 805 |
if (!err) { |
| 806 |
update_atime(ninode); |
| 807 |
err = ninode->i_op->readlink(ndentry, buf, buflen); |
| 808 |
} |
| 809 |
} |
| 810 |
return err; |
| 811 |
} |
| 812 |
|
| 813 |
static int glassfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 814 |
{ |
| 815 |
int err = 0; |
| 816 |
struct base_entry *be = dentry->d_inode->u.generic_ip; |
| 817 |
struct dentry *ndentry = be->dentry; |
| 818 |
struct inode *ninode = ndentry->d_inode; |
| 819 |
|
| 820 |
if (ninode->i_op && ninode->i_op->follow_link) { |
| 821 |
err = security_inode_follow_link(ndentry, nd); |
| 822 |
if (!err) { |
| 823 |
update_atime(ninode); |
| 824 |
err = ninode->i_op->follow_link(ndentry, nd); |
| 825 |
} |
| 826 |
} |
| 827 |
return err; |
| 828 |
} |
| 829 |
|
| 830 |
static void glassfs_clear_inode(struct inode *inode) |
| 831 |
{ |
| 832 |
struct base_entry *be = inode->u.generic_ip; |
| 833 |
//printk("glassfs_clear_inode\n"); |
| 834 |
dput(be->dentry); |
| 835 |
mntput(be->mnt); |
| 836 |
kfree(be); |
| 837 |
} |
| 838 |
|
| 839 |
static struct dentry_operations glassfs_dentry_operations = { |
| 840 |
.d_revalidate = glassfs_dentry_revalidate, |
| 841 |
}; |
| 842 |
|
| 843 |
static struct file_operations glassfs_dir_operations = { |
| 844 |
.llseek = glassfs_llseek, |
| 845 |
.read = generic_read_dir, /* Every FS does this */ |
| 846 |
.readdir = glassfs_readdir, |
| 847 |
.ioctl = glassfs_ioctl, |
| 848 |
.open = glassfs_open_dir, |
| 849 |
.flush = glassfs_flush, |
| 850 |
.release = glassfs_release, |
| 851 |
.fsync = glassfs_fsync, |
| 852 |
}; |
| 853 |
|
| 854 |
static struct file_operations glassfs_file_operations = { |
| 855 |
.open = glassfs_open, |
| 856 |
}; |
| 857 |
|
| 858 |
static struct inode_operations glassfs_dir_inode_operations = { |
| 859 |
.create = glassfs_create, |
| 860 |
.lookup = glassfs_lookup, |
| 861 |
.link = glassfs_link, |
| 862 |
.unlink = glassfs_unlink, |
| 863 |
.symlink = glassfs_symlink, |
| 864 |
.mkdir = glassfs_mkdir, |
| 865 |
.rmdir = glassfs_rmdir, |
| 866 |
.mknod = glassfs_mknod, |
| 867 |
.rename = glassfs_rename, |
| 868 |
.permission = glassfs_permission, |
| 869 |
.getattr = glassfs_getattr, |
| 870 |
.setattr = glassfs_setattr, |
| 871 |
.setxattr = glassfs_setxattr, |
| 872 |
.getxattr = glassfs_getxattr, |
| 873 |
.listxattr = glassfs_listxattr, |
| 874 |
.removexattr = glassfs_removexattr, |
| 875 |
}; |
| 876 |
|
| 877 |
static struct inode_operations glassfs_file_inode_operations = { |
| 878 |
.permission = glassfs_permission, |
| 879 |
.getattr = glassfs_getattr, |
| 880 |
.setattr = glassfs_setattr, |
| 881 |
.setxattr = glassfs_setxattr, |
| 882 |
.getxattr = glassfs_getxattr, |
| 883 |
.listxattr = glassfs_listxattr, |
| 884 |
.removexattr = glassfs_removexattr, |
| 885 |
}; |
| 886 |
|
| 887 |
static struct inode_operations glassfs_symlink_inode_operations = { |
| 888 |
.readlink = glassfs_readlink, |
| 889 |
.follow_link = glassfs_follow_link, |
| 890 |
.permission = glassfs_permission, |
| 891 |
.getattr = glassfs_getattr, |
| 892 |
.setattr = glassfs_setattr, |
| 893 |
.setxattr = glassfs_setxattr, |
| 894 |
.getxattr = glassfs_getxattr, |
| 895 |
.listxattr = glassfs_listxattr, |
| 896 |
.removexattr = glassfs_removexattr, |
| 897 |
}; |
| 898 |
|
| 899 |
|
| 900 |
static struct super_operations glassfs_ops = { |
| 901 |
.statfs = glassfs_statfs, |
| 902 |
.clear_inode = glassfs_clear_inode, |
| 903 |
}; |
| 904 |
|
| 905 |
|
| 906 |
static int glassfs_fill_super(struct super_block * sb, void * data, int silent) |
| 907 |
{ |
| 908 |
struct inode * inode; |
| 909 |
struct dentry * root; |
| 910 |
struct super_block *nsb; |
| 911 |
struct dentry * nroot; |
| 912 |
struct vfsmount *nmnt; |
| 913 |
|
| 914 |
inode = glassfs_alloc_inode(sb); |
| 915 |
if (!inode) |
| 916 |
return -ENOMEM; |
| 917 |
read_lock(¤t->fs->lock); |
| 918 |
nroot = current->fs->root; |
| 919 |
nmnt = current->fs->rootmnt; |
| 920 |
glassfs_fill_inode(inode, S_IFDIR | 0755, 0, nroot, nmnt, 0); |
| 921 |
read_unlock(¤t->fs->lock); |
| 922 |
|
| 923 |
nsb = nmnt->mnt_sb; |
| 924 |
sb->s_blocksize = nsb->s_blocksize; |
| 925 |
sb->s_blocksize_bits = nsb->s_blocksize_bits; |
| 926 |
sb->s_magic = GLASSFS_MAGIC; |
| 927 |
sb->s_op = &glassfs_ops; |
| 928 |
|
| 929 |
root = d_alloc_root(inode); |
| 930 |
if (!root) { |
| 931 |
iput(inode); |
| 932 |
return -ENOMEM; |
| 933 |
} |
| 934 |
sb->s_fs_info = nsb; |
| 935 |
root->d_op = &glassfs_dentry_operations; |
| 936 |
|
| 937 |
sb->s_root = root; |
| 938 |
return 0; |
| 939 |
} |
| 940 |
|
| 941 |
static struct super_block *glassfs_get_sb(struct file_system_type *fs_type, |
| 942 |
int flags, const char *dev_name, void *data) |
| 943 |
{ |
| 944 |
// printk("glassfs_get_sb\n"); |
| 945 |
return get_sb_nodev(fs_type, flags, data, glassfs_fill_super); |
| 946 |
} |
| 947 |
|
| 948 |
static struct file_system_type glassfs_fs_type = { |
| 949 |
.owner = THIS_MODULE, |
| 950 |
.name = "glassfs", |
| 951 |
.get_sb = glassfs_get_sb, |
| 952 |
.kill_sb = kill_anon_super, |
| 953 |
}; |
| 954 |
|
| 955 |
static int __init init_glassfs_fs(void) |
| 956 |
{ |
| 957 |
return register_filesystem(&glassfs_fs_type); |
| 958 |
} |
| 959 |
|
| 960 |
static void __exit exit_glassfs_fs(void) |
| 961 |
{ |
| 962 |
unregister_filesystem(&glassfs_fs_type); |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
module_init(init_glassfs_fs) |
| 967 |
module_exit(exit_glassfs_fs) |
| 968 |
|
| 969 |
|
| 970 |
MODULE_LICENSE("GPL"); |
| 971 |
|
| 972 |
/* |
| 973 |
* Local Variables: |
| 974 |
* indent-tabs-mode: t |
| 975 |
* c-basic-offset: 8 |
| 976 |
* End: |
| 977 |
*/ |