linux-bk/fs/libfs.c
<<
>>
Prefs
   1/*
   2 *      fs/libfs.c
   3 *      Library for filesystems writers.
   4 */
   5
   6#include <linux/module.h>
   7#include <linux/pagemap.h>
   8#include <linux/mount.h>
   9#include <linux/vfs.h>
  10
  11int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
  12                   struct kstat *stat)
  13{
  14        struct inode *inode = dentry->d_inode;
  15        generic_fillattr(inode, stat);
  16        stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
  17        return 0;
  18}
  19
  20int simple_statfs(struct super_block *sb, struct kstatfs *buf)
  21{
  22        buf->f_type = sb->s_magic;
  23        buf->f_bsize = PAGE_CACHE_SIZE;
  24        buf->f_namelen = NAME_MAX;
  25        return 0;
  26}
  27
  28/*
  29 * Lookup the data. This is trivial - if the dentry didn't already
  30 * exist, we know it is negative.
  31 */
  32
  33struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  34{
  35        if (dentry->d_name.len > NAME_MAX)
  36                return ERR_PTR(-ENAMETOOLONG);
  37        d_add(dentry, NULL);
  38        return NULL;
  39}
  40
  41int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
  42{
  43        return 0;
  44}
  45 
  46int dcache_dir_open(struct inode *inode, struct file *file)
  47{
  48        static struct qstr cursor_name = {.len = 1, .name = "."};
  49
  50        file->private_data = d_alloc(file->f_dentry, &cursor_name);
  51
  52        return file->private_data ? 0 : -ENOMEM;
  53}
  54
  55int dcache_dir_close(struct inode *inode, struct file *file)
  56{
  57        dput(file->private_data);
  58        return 0;
  59}
  60
  61loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
  62{
  63        down(&file->f_dentry->d_inode->i_sem);
  64        switch (origin) {
  65                case 1:
  66                        offset += file->f_pos;
  67                case 0:
  68                        if (offset >= 0)
  69                                break;
  70                default:
  71                        up(&file->f_dentry->d_inode->i_sem);
  72                        return -EINVAL;
  73        }
  74        if (offset != file->f_pos) {
  75                file->f_pos = offset;
  76                if (file->f_pos >= 2) {
  77                        struct list_head *p;
  78                        struct dentry *cursor = file->private_data;
  79                        loff_t n = file->f_pos - 2;
  80
  81                        spin_lock(&dcache_lock);
  82                        list_del(&cursor->d_child);
  83                        p = file->f_dentry->d_subdirs.next;
  84                        while (n && p != &file->f_dentry->d_subdirs) {
  85                                struct dentry *next;
  86                                next = list_entry(p, struct dentry, d_child);
  87                                if (!d_unhashed(next) && next->d_inode)
  88                                        n--;
  89                                p = p->next;
  90                        }
  91                        list_add_tail(&cursor->d_child, p);
  92                        spin_unlock(&dcache_lock);
  93                }
  94        }
  95        up(&file->f_dentry->d_inode->i_sem);
  96        return offset;
  97}
  98
  99/* Relationship between i_mode and the DT_xxx types */
 100static inline unsigned char dt_type(struct inode *inode)
 101{
 102        return (inode->i_mode >> 12) & 15;
 103}
 104
 105/*
 106 * Directory is locked and all positive dentries in it are safe, since
 107 * for ramfs-type trees they can't go away without unlink() or rmdir(),
 108 * both impossible due to the lock on directory.
 109 */
 110
 111int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
 112{
 113        struct dentry *dentry = filp->f_dentry;
 114        struct dentry *cursor = filp->private_data;
 115        struct list_head *p, *q = &cursor->d_child;
 116        ino_t ino;
 117        int i = filp->f_pos;
 118
 119        switch (i) {
 120                case 0:
 121                        ino = dentry->d_inode->i_ino;
 122                        if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
 123                                break;
 124                        filp->f_pos++;
 125                        i++;
 126                        /* fallthrough */
 127                case 1:
 128                        ino = parent_ino(dentry);
 129                        if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
 130                                break;
 131                        filp->f_pos++;
 132                        i++;
 133                        /* fallthrough */
 134                default:
 135                        spin_lock(&dcache_lock);
 136                        if (filp->f_pos == 2) {
 137                                list_del(q);
 138                                list_add(q, &dentry->d_subdirs);
 139                        }
 140                        for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
 141                                struct dentry *next;
 142                                next = list_entry(p, struct dentry, d_child);
 143                                if (d_unhashed(next) || !next->d_inode)
 144                                        continue;
 145
 146                                spin_unlock(&dcache_lock);
 147                                if (filldir(dirent, next->d_name.name, next->d_name.len, filp->f_pos, next->d_inode->i_ino, dt_type(next->d_inode)) < 0)
 148                                        return 0;
 149                                spin_lock(&dcache_lock);
 150                                /* next is still alive */
 151                                list_del(q);
 152                                list_add(q, p);
 153                                p = q;
 154                                filp->f_pos++;
 155                        }
 156                        spin_unlock(&dcache_lock);
 157        }
 158        return 0;
 159}
 160
 161ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
 162{
 163        return -EISDIR;
 164}
 165
 166struct file_operations simple_dir_operations = {
 167        .open           = dcache_dir_open,
 168        .release        = dcache_dir_close,
 169        .llseek         = dcache_dir_lseek,
 170        .read           = generic_read_dir,
 171        .readdir        = dcache_readdir,
 172};
 173
 174struct inode_operations simple_dir_inode_operations = {
 175        .lookup         = simple_lookup,
 176};
 177
 178/*
 179 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
 180 * will never be mountable)
 181 */
 182struct super_block *
 183get_sb_pseudo(struct file_system_type *fs_type, char *name,
 184        struct super_operations *ops, unsigned long magic)
 185{
 186        struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
 187        static struct super_operations default_ops = {.statfs = simple_statfs};
 188        struct dentry *dentry;
 189        struct inode *root;
 190        struct qstr d_name = {.name = name, .len = strlen(name)};
 191
 192        if (IS_ERR(s))
 193                return s;
 194
 195        s->s_flags = MS_NOUSER;
 196        s->s_maxbytes = ~0ULL;
 197        s->s_blocksize = 1024;
 198        s->s_blocksize_bits = 10;
 199        s->s_magic = magic;
 200        s->s_op = ops ? ops : &default_ops;
 201        root = new_inode(s);
 202        if (!root)
 203                goto Enomem;
 204        root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
 205        root->i_uid = root->i_gid = 0;
 206        root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
 207        dentry = d_alloc(NULL, &d_name);
 208        if (!dentry) {
 209                iput(root);
 210                goto Enomem;
 211        }
 212        dentry->d_sb = s;
 213        dentry->d_parent = dentry;
 214        d_instantiate(dentry, root);
 215        s->s_root = dentry;
 216        s->s_flags |= MS_ACTIVE;
 217        return s;
 218
 219Enomem:
 220        up_write(&s->s_umount);
 221        deactivate_super(s);
 222        return ERR_PTR(-ENOMEM);
 223}
 224
 225int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
 226{
 227        struct inode *inode = old_dentry->d_inode;
 228
 229        inode->i_nlink++;
 230        atomic_inc(&inode->i_count);
 231        dget(dentry);
 232        d_instantiate(dentry, inode);
 233        return 0;
 234}
 235
 236static inline int simple_positive(struct dentry *dentry)
 237{
 238        return dentry->d_inode && !d_unhashed(dentry);
 239}
 240
 241int simple_empty(struct dentry *dentry)
 242{
 243        struct dentry *child;
 244        int ret = 0;
 245
 246        spin_lock(&dcache_lock);
 247        list_for_each_entry(child, &dentry->d_subdirs, d_child)
 248                if (simple_positive(child))
 249                        goto out;
 250        ret = 1;
 251out:
 252        spin_unlock(&dcache_lock);
 253        return ret;
 254}
 255
 256int simple_unlink(struct inode *dir, struct dentry *dentry)
 257{
 258        struct inode *inode = dentry->d_inode;
 259
 260        inode->i_nlink--;
 261        dput(dentry);
 262        return 0;
 263}
 264
 265int simple_rmdir(struct inode *dir, struct dentry *dentry)
 266{
 267        if (!simple_empty(dentry))
 268                return -ENOTEMPTY;
 269
 270        dentry->d_inode->i_nlink--;
 271        simple_unlink(dir, dentry);
 272        dir->i_nlink--;
 273        return 0;
 274}
 275
 276int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
 277                struct inode *new_dir, struct dentry *new_dentry)
 278{
 279        int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
 280
 281        if (!simple_empty(new_dentry))
 282                return -ENOTEMPTY;
 283
 284        if (new_dentry->d_inode) {
 285                simple_unlink(new_dir, new_dentry);
 286                if (they_are_dirs)
 287                        old_dir->i_nlink--;
 288        } else if (they_are_dirs) {
 289                old_dir->i_nlink--;
 290                new_dir->i_nlink++;
 291        }
 292        return 0;
 293}
 294
 295int simple_readpage(struct file *file, struct page *page)
 296{
 297        void *kaddr;
 298
 299        if (PageUptodate(page))
 300                goto out;
 301
 302        kaddr = kmap_atomic(page, KM_USER0);
 303        memset(kaddr, 0, PAGE_CACHE_SIZE);
 304        kunmap_atomic(kaddr, KM_USER0);
 305        flush_dcache_page(page);
 306        SetPageUptodate(page);
 307out:
 308        unlock_page(page);
 309        return 0;
 310}
 311
 312int simple_prepare_write(struct file *file, struct page *page,
 313                        unsigned from, unsigned to)
 314{
 315        if (!PageUptodate(page)) {
 316                if (to - from != PAGE_CACHE_SIZE) {
 317                        void *kaddr = kmap_atomic(page, KM_USER0);
 318                        memset(kaddr, 0, from);
 319                        memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
 320                        flush_dcache_page(page);
 321                        kunmap_atomic(kaddr, KM_USER0);
 322                }
 323                SetPageUptodate(page);
 324        }
 325        return 0;
 326}
 327
 328int simple_commit_write(struct file *file, struct page *page,
 329                        unsigned offset, unsigned to)
 330{
 331        struct inode *inode = page->mapping->host;
 332        loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
 333
 334        /*
 335         * No need to use i_size_read() here, the i_size
 336         * cannot change under us because we hold the i_sem.
 337         */
 338        if (pos > inode->i_size)
 339                i_size_write(inode, pos);
 340        set_page_dirty(page);
 341        return 0;
 342}
 343
 344int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
 345{
 346        static struct super_operations s_ops = {.statfs = simple_statfs};
 347        struct inode *inode;
 348        struct dentry *root;
 349        struct dentry *dentry;
 350        int i;
 351
 352        s->s_blocksize = PAGE_CACHE_SIZE;
 353        s->s_blocksize_bits = PAGE_CACHE_SHIFT;
 354        s->s_magic = magic;
 355        s->s_op = &s_ops;
 356
 357        inode = new_inode(s);
 358        if (!inode)
 359                return -ENOMEM;
 360        inode->i_mode = S_IFDIR | 0755;
 361        inode->i_uid = inode->i_gid = 0;
 362        inode->i_blksize = PAGE_CACHE_SIZE;
 363        inode->i_blocks = 0;
 364        inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 365        inode->i_op = &simple_dir_inode_operations;
 366        inode->i_fop = &simple_dir_operations;
 367        root = d_alloc_root(inode);
 368        if (!root) {
 369                iput(inode);
 370                return -ENOMEM;
 371        }
 372        for (i = 0; !files->name || files->name[0]; i++, files++) {
 373                struct qstr name;
 374                if (!files->name)
 375                        continue;
 376                name.name = files->name;
 377                name.len = strlen(name.name);
 378                name.hash = full_name_hash(name.name, name.len);
 379                dentry = d_alloc(root, &name);
 380                if (!dentry)
 381                        goto out;
 382                inode = new_inode(s);
 383                if (!inode)
 384                        goto out;
 385                inode->i_mode = S_IFREG | files->mode;
 386                inode->i_uid = inode->i_gid = 0;
 387                inode->i_blksize = PAGE_CACHE_SIZE;
 388                inode->i_blocks = 0;
 389                inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 390                inode->i_fop = files->ops;
 391                inode->i_ino = i;
 392                d_add(dentry, inode);
 393        }
 394        s->s_root = root;
 395        return 0;
 396out:
 397        d_genocide(root);
 398        dput(root);
 399        return -ENOMEM;
 400}
 401
 402static spinlock_t pin_fs_lock = SPIN_LOCK_UNLOCKED;
 403
 404int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
 405{
 406        struct vfsmount *mnt = NULL;
 407        spin_lock(&pin_fs_lock);
 408        if (unlikely(!*mount)) {
 409                spin_unlock(&pin_fs_lock);
 410                mnt = do_kern_mount(name, 0, name, NULL);
 411                if (IS_ERR(mnt))
 412                        return PTR_ERR(mnt);
 413                spin_lock(&pin_fs_lock);
 414                if (!*mount)
 415                        *mount = mnt;
 416        }
 417        mntget(*mount);
 418        ++*count;
 419        spin_unlock(&pin_fs_lock);
 420        mntput(mnt);
 421        return 0;
 422}
 423
 424void simple_release_fs(struct vfsmount **mount, int *count)
 425{
 426        struct vfsmount *mnt;
 427        spin_lock(&pin_fs_lock);
 428        mnt = *mount;
 429        if (!--*count)
 430                *mount = NULL;
 431        spin_unlock(&pin_fs_lock);
 432        mntput(mnt);
 433}
 434
 435EXPORT_SYMBOL(dcache_dir_close);
 436EXPORT_SYMBOL(dcache_dir_lseek);
 437EXPORT_SYMBOL(dcache_dir_open);
 438EXPORT_SYMBOL(dcache_readdir);
 439EXPORT_SYMBOL(generic_read_dir);
 440EXPORT_SYMBOL(simple_commit_write);
 441EXPORT_SYMBOL(simple_dir_inode_operations);
 442EXPORT_SYMBOL(simple_dir_operations);
 443EXPORT_SYMBOL(simple_empty);
 444EXPORT_SYMBOL(simple_fill_super);
 445EXPORT_SYMBOL(simple_getattr);
 446EXPORT_SYMBOL(simple_link);
 447EXPORT_SYMBOL(simple_lookup);
 448EXPORT_SYMBOL(simple_pin_fs);
 449EXPORT_SYMBOL(simple_prepare_write);
 450EXPORT_SYMBOL(simple_readpage);
 451EXPORT_SYMBOL(simple_release_fs);
 452EXPORT_SYMBOL(simple_rename);
 453EXPORT_SYMBOL(simple_rmdir);
 454EXPORT_SYMBOL(simple_statfs);
 455EXPORT_SYMBOL(simple_sync_file);
 456EXPORT_SYMBOL(simple_unlink);
 457
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.