linux-bk/fs/namei.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/namei.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 */
   6
   7/*
   8 * Some corrections by tytso.
   9 */
  10
  11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  12 * lookup logic.
  13 */
  14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  15 */
  16
  17#include <linux/init.h>
  18#include <linux/slab.h>
  19#include <linux/fs.h>
  20#include <linux/namei.h>
  21#include <linux/quotaops.h>
  22#include <linux/pagemap.h>
  23#include <linux/dnotify.h>
  24#include <linux/smp_lock.h>
  25#include <linux/personality.h>
  26#include <linux/security.h>
  27
  28#include <asm/namei.h>
  29#include <asm/uaccess.h>
  30
  31#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  32
  33/* [Feb-1997 T. Schoebel-Theuer]
  34 * Fundamental changes in the pathname lookup mechanisms (namei)
  35 * were necessary because of omirr.  The reason is that omirr needs
  36 * to know the _real_ pathname, not the user-supplied one, in case
  37 * of symlinks (and also when transname replacements occur).
  38 *
  39 * The new code replaces the old recursive symlink resolution with
  40 * an iterative one (in case of non-nested symlink chains).  It does
  41 * this with calls to <fs>_follow_link().
  42 * As a side effect, dir_namei(), _namei() and follow_link() are now 
  43 * replaced with a single function lookup_dentry() that can handle all 
  44 * the special cases of the former code.
  45 *
  46 * With the new dcache, the pathname is stored at each inode, at least as
  47 * long as the refcount of the inode is positive.  As a side effect, the
  48 * size of the dcache depends on the inode cache and thus is dynamic.
  49 *
  50 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  51 * resolution to correspond with current state of the code.
  52 *
  53 * Note that the symlink resolution is not *completely* iterative.
  54 * There is still a significant amount of tail- and mid- recursion in
  55 * the algorithm.  Also, note that <fs>_readlink() is not used in
  56 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  57 * may return different results than <fs>_follow_link().  Many virtual
  58 * filesystems (including /proc) exhibit this behavior.
  59 */
  60
  61/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  62 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  63 * and the name already exists in form of a symlink, try to create the new
  64 * name indicated by the symlink. The old code always complained that the
  65 * name already exists, due to not following the symlink even if its target
  66 * is nonexistent.  The new semantics affects also mknod() and link() when
  67 * the name is a symlink pointing to a non-existant name.
  68 *
  69 * I don't know which semantics is the right one, since I have no access
  70 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  71 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  72 * "old" one. Personally, I think the new semantics is much more logical.
  73 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  74 * file does succeed in both HP-UX and SunOs, but not in Solaris
  75 * and in the old Linux semantics.
  76 */
  77
  78/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  79 * semantics.  See the comments in "open_namei" and "do_link" below.
  80 *
  81 * [10-Sep-98 Alan Modra] Another symlink change.
  82 */
  83
  84/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  85 *      inside the path - always follow.
  86 *      in the last component in creation/removal/renaming - never follow.
  87 *      if LOOKUP_FOLLOW passed - follow.
  88 *      if the pathname has trailing slashes - follow.
  89 *      otherwise - don't follow.
  90 * (applied in that order).
  91 *
  92 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  93 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  94 * During the 2.4 we need to fix the userland stuff depending on it -
  95 * hopefully we will be able to get rid of that wart in 2.5. So far only
  96 * XEmacs seems to be relying on it...
  97 */
  98/*
  99 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
 100 * implemented.  Let's see if raised priority of ->s_vfs_rename_sem gives
 101 * any extra contention...
 102 */
 103
 104/* In order to reduce some races, while at the same time doing additional
 105 * checking and hopefully speeding things up, we copy filenames to the
 106 * kernel data space before using them..
 107 *
 108 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
 109 * PATH_MAX includes the nul terminator --RR.
 110 */
 111static inline int do_getname(const char *filename, char *page)
 112{
 113        int retval;
 114        unsigned long len = PATH_MAX;
 115
 116        if ((unsigned long) filename >= TASK_SIZE) {
 117                if (!segment_eq(get_fs(), KERNEL_DS))
 118                        return -EFAULT;
 119        } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
 120                len = TASK_SIZE - (unsigned long) filename;
 121
 122        retval = strncpy_from_user((char *)page, filename, len);
 123        if (retval > 0) {
 124                if (retval < len)
 125                        return 0;
 126                return -ENAMETOOLONG;
 127        } else if (!retval)
 128                retval = -ENOENT;
 129        return retval;
 130}
 131
 132char * getname(const char * filename)
 133{
 134        char *tmp, *result;
 135
 136        result = ERR_PTR(-ENOMEM);
 137        tmp = __getname();
 138        if (tmp)  {
 139                int retval = do_getname(filename, tmp);
 140
 141                result = tmp;
 142                if (retval < 0) {
 143                        putname(tmp);
 144                        result = ERR_PTR(retval);
 145                }
 146        }
 147        return result;
 148}
 149
 150/*
 151 *      vfs_permission()
 152 *
 153 * is used to check for read/write/execute permissions on a file.
 154 * We use "fsuid" for this, letting us set arbitrary permissions
 155 * for filesystem access without changing the "normal" uids which
 156 * are used for other things..
 157 */
 158int vfs_permission(struct inode * inode, int mask)
 159{
 160        umode_t                 mode = inode->i_mode;
 161
 162        if (mask & MAY_WRITE) {
 163                /*
 164                 * Nobody gets write access to a read-only fs.
 165                 */
 166                if (IS_RDONLY(inode) &&
 167                    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
 168                        return -EROFS;
 169
 170                /*
 171                 * Nobody gets write access to an immutable file.
 172                 */
 173                if (IS_IMMUTABLE(inode))
 174                        return -EACCES;
 175        }
 176
 177        if (current->fsuid == inode->i_uid)
 178                mode >>= 6;
 179        else if (in_group_p(inode->i_gid))
 180                mode >>= 3;
 181
 182        /*
 183         * If the DACs are ok we don't need any capability check.
 184         */
 185        if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
 186                return 0;
 187
 188        /*
 189         * Read/write DACs are always overridable.
 190         * Executable DACs are overridable if at least one exec bit is set.
 191         */
 192        if ((mask & (MAY_READ|MAY_WRITE)) || (inode->i_mode & S_IXUGO))
 193                if (capable(CAP_DAC_OVERRIDE))
 194                        return 0;
 195
 196        /*
 197         * Searching includes executable on directories, else just read.
 198         */
 199        if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
 200                if (capable(CAP_DAC_READ_SEARCH))
 201                        return 0;
 202
 203        return -EACCES;
 204}
 205
 206int permission(struct inode * inode,int mask)
 207{
 208        int retval;
 209        int submask;
 210
 211        /* Ordinary permission routines do not understand MAY_APPEND. */
 212        submask = mask & ~MAY_APPEND;
 213
 214        if (inode->i_op && inode->i_op->permission)
 215                retval = inode->i_op->permission(inode, submask);
 216        else
 217                retval = vfs_permission(inode, submask);
 218        if (retval)
 219                return retval;
 220
 221        return security_ops->inode_permission(inode, mask);
 222}
 223
 224/*
 225 * get_write_access() gets write permission for a file.
 226 * put_write_access() releases this write permission.
 227 * This is used for regular files.
 228 * We cannot support write (and maybe mmap read-write shared) accesses and
 229 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
 230 * can have the following values:
 231 * 0: no writers, no VM_DENYWRITE mappings
 232 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
 233 * > 0: (i_writecount) users are writing to the file.
 234 *
 235 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
 236 * except for the cases where we don't hold i_writecount yet. Then we need to
 237 * use {get,deny}_write_access() - these functions check the sign and refuse
 238 * to do the change if sign is wrong. Exclusion between them is provided by
 239 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
 240 * who will try to move it in struct inode - just leave it here.
 241 */
 242static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
 243int get_write_access(struct inode * inode)
 244{
 245        spin_lock(&arbitration_lock);
 246        if (atomic_read(&inode->i_writecount) < 0) {
 247                spin_unlock(&arbitration_lock);
 248                return -ETXTBSY;
 249        }
 250        atomic_inc(&inode->i_writecount);
 251        spin_unlock(&arbitration_lock);
 252        return 0;
 253}
 254int deny_write_access(struct file * file)
 255{
 256        spin_lock(&arbitration_lock);
 257        if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
 258                spin_unlock(&arbitration_lock);
 259                return -ETXTBSY;
 260        }
 261        atomic_dec(&file->f_dentry->d_inode->i_writecount);
 262        spin_unlock(&arbitration_lock);
 263        return 0;
 264}
 265
 266void path_release(struct nameidata *nd)
 267{
 268        dput(nd->dentry);
 269        mntput(nd->mnt);
 270}
 271
 272/*
 273 * Internal lookup() using the new generic dcache.
 274 * SMP-safe
 275 */
 276static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, int flags)
 277{
 278        struct dentry * dentry = d_lookup(parent, name);
 279        
 280        if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
 281                if (!dentry->d_op->d_revalidate(dentry, flags) && !d_invalidate(dentry)) {
 282                        dput(dentry);
 283                        dentry = NULL;
 284                }
 285        }
 286        return dentry;
 287}
 288
 289/*for fastwalking*/
 290static inline void unlock_nd(struct nameidata *nd)
 291{
 292        struct vfsmount *mnt = nd->old_mnt;
 293        struct dentry *dentry = nd->old_dentry;
 294        mntget(nd->mnt);
 295        dget_locked(nd->dentry);
 296        nd->old_mnt = NULL;
 297        nd->old_dentry = NULL;
 298        spin_unlock(&dcache_lock);
 299        dput(dentry);
 300        mntput(mnt);
 301}
 302
 303static inline void lock_nd(struct nameidata *nd)
 304{
 305        spin_lock(&dcache_lock);
 306        nd->old_mnt = nd->mnt;
 307        nd->old_dentry = nd->dentry;
 308}
 309
 310/*
 311 * Short-cut version of permission(), for calling by
 312 * path_walk(), when dcache lock is held.  Combines parts
 313 * of permission() and vfs_permission(), and tests ONLY for
 314 * MAY_EXEC permission.
 315 *
 316 * If appropriate, check DAC only.  If not appropriate, or
 317 * short-cut DAC fails, then call permission() to do more
 318 * complete permission check.
 319 */
 320static inline int exec_permission_lite(struct inode *inode)
 321{
 322        umode_t mode = inode->i_mode;
 323
 324        if ((inode->i_op && inode->i_op->permission))
 325                return -EAGAIN;
 326
 327        if (current->fsuid == inode->i_uid)
 328                mode >>= 6;
 329        else if (in_group_p(inode->i_gid))
 330                mode >>= 3;
 331
 332        if (mode & MAY_EXEC)
 333                goto ok;
 334
 335        if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
 336                goto ok;
 337
 338        if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
 339                goto ok;
 340
 341        return -EACCES;
 342ok:
 343        return security_ops->inode_permission_lite(inode, MAY_EXEC);
 344}
 345
 346/*
 347 * This is called when everything else fails, and we actually have
 348 * to go to the low-level filesystem to find out what we should do..
 349 *
 350 * We get the directory semaphore, and after getting that we also
 351 * make sure that nobody added the entry to the dcache in the meantime..
 352 * SMP-safe
 353 */
 354static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, int flags)
 355{
 356        struct dentry * result;
 357        struct inode *dir = parent->d_inode;
 358
 359        down(&dir->i_sem);
 360        /*
 361         * First re-do the cached lookup just in case it was created
 362         * while we waited for the directory semaphore..
 363         *
 364         * FIXME! This could use version numbering or similar to
 365         * avoid unnecessary cache lookups.
 366         */
 367        result = d_lookup(parent, name);
 368        if (!result) {
 369                struct dentry * dentry = d_alloc(parent, name);
 370                result = ERR_PTR(-ENOMEM);
 371                if (dentry) {
 372                        result = dir->i_op->lookup(dir, dentry);
 373                        if (result)
 374                                dput(dentry);
 375                        else {
 376                                result = dentry;
 377                                security_ops->inode_post_lookup(dir, result);
 378                        }
 379                }
 380                up(&dir->i_sem);
 381                return result;
 382        }
 383
 384        /*
 385         * Uhhuh! Nasty case: the cache was re-populated while
 386         * we waited on the semaphore. Need to revalidate.
 387         */
 388        up(&dir->i_sem);
 389        if (result->d_op && result->d_op->d_revalidate) {
 390                if (!result->d_op->d_revalidate(result, flags) && !d_invalidate(result)) {
 391                        dput(result);
 392                        result = ERR_PTR(-ENOENT);
 393                }
 394        }
 395        return result;
 396}
 397
 398/*
 399 * This limits recursive symlink follows to 8, while
 400 * limiting consecutive symlinks to 40.
 401 *
 402 * Without that kind of total limit, nasty chains of consecutive
 403 * symlinks can cause almost arbitrarily long lookups. 
 404 */
 405static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
 406{
 407        int err = -ELOOP;
 408        if (current->link_count >= 5)
 409                goto loop;
 410        if (current->total_link_count >= 40)
 411                goto loop;
 412        if (need_resched()) {
 413                current->state = TASK_RUNNING;
 414                schedule();
 415        }
 416        err = security_ops->inode_follow_link(dentry, nd);
 417        if (err)
 418                goto loop;
 419        current->link_count++;
 420        current->total_link_count++;
 421        UPDATE_ATIME(dentry->d_inode);
 422        err = dentry->d_inode->i_op->follow_link(dentry, nd);
 423        current->link_count--;
 424        return err;
 425loop:
 426        path_release(nd);
 427        return err;
 428}
 429
 430int follow_up(struct vfsmount **mnt, struct dentry **dentry)
 431{
 432        struct vfsmount *parent;
 433        struct dentry *mountpoint;
 434        spin_lock(&dcache_lock);
 435        parent=(*mnt)->mnt_parent;
 436        if (parent == *mnt) {
 437                spin_unlock(&dcache_lock);
 438                return 0;
 439        }
 440        mntget(parent);
 441        mountpoint=dget((*mnt)->mnt_mountpoint);
 442        spin_unlock(&dcache_lock);
 443        dput(*dentry);
 444        *dentry = mountpoint;
 445        mntput(*mnt);
 446        *mnt = parent;
 447        return 1;
 448}
 449
 450static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
 451{
 452        int res = 0;
 453        while (d_mountpoint(*dentry)) {
 454                struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
 455                if (!mounted)
 456                        break;
 457                *mnt = mounted;
 458                *dentry = mounted->mnt_root;
 459                res = 1;
 460        }
 461        return res;
 462}
 463
 464static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
 465{
 466        struct vfsmount *mounted;
 467
 468        spin_lock(&dcache_lock);
 469        mounted = lookup_mnt(*mnt, *dentry);
 470        if (mounted) {
 471                *mnt = mntget(mounted);
 472                spin_unlock(&dcache_lock);
 473                dput(*dentry);
 474                mntput(mounted->mnt_parent);
 475                *dentry = dget(mounted->mnt_root);
 476                return 1;
 477        }
 478        spin_unlock(&dcache_lock);
 479        return 0;
 480}
 481
 482int follow_down(struct vfsmount **mnt, struct dentry **dentry)
 483{
 484        return __follow_down(mnt,dentry);
 485}
 486 
 487static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
 488{
 489        while(1) {
 490                struct vfsmount *parent;
 491                if (*dentry == current->fs->root &&
 492                    *mnt == current->fs->rootmnt)
 493                        break;
 494                if (*dentry != (*mnt)->mnt_root) {
 495                        *dentry = (*dentry)->d_parent;
 496                        break;
 497                }
 498                parent=(*mnt)->mnt_parent;
 499                if (parent == *mnt)
 500                        break;
 501                *dentry=(*mnt)->mnt_mountpoint;
 502                *mnt = parent;
 503        }
 504        follow_mount(mnt, dentry);
 505}
 506
 507struct path {
 508        struct vfsmount *mnt;
 509        struct dentry *dentry;
 510};
 511
 512/*
 513 *  It's more convoluted than I'd like it to be, but... it's still fairly
 514 *  small and for now I'd prefer to have fast path as straight as possible.
 515 *  It _is_ time-critical.
 516 */
 517static int do_lookup(struct nameidata *nd, struct qstr *name,
 518                     struct path *path, struct path *cached_path,
 519                     int flags)
 520{
 521        struct vfsmount *mnt = nd->mnt;
 522        struct dentry *dentry = __d_lookup(nd->dentry, name);
 523
 524        if (!dentry)
 525                goto dcache_miss;
 526        if (dentry->d_op && dentry->d_op->d_revalidate)
 527                goto need_revalidate;
 528done:
 529        path->mnt = mnt;
 530        path->dentry = dentry;
 531        return 0;
 532
 533dcache_miss:
 534        unlock_nd(nd);
 535
 536need_lookup:
 537        dentry = real_lookup(nd->dentry, name, LOOKUP_CONTINUE);
 538        if (IS_ERR(dentry))
 539                goto fail;
 540        mntget(mnt);
 541relock:
 542        dput(cached_path->dentry);
 543        mntput(cached_path->mnt);
 544        cached_path->mnt = mnt;
 545        cached_path->dentry = dentry;
 546        lock_nd(nd);
 547        goto done;
 548
 549need_revalidate:
 550        mntget(mnt);
 551        dget_locked(dentry);
 552        unlock_nd(nd);
 553        if (dentry->d_op->d_revalidate(dentry, flags))
 554                goto relock;
 555        if (d_invalidate(dentry))
 556                goto relock;
 557        dput(dentry);
 558        mntput(mnt);
 559        goto need_lookup;
 560
 561fail:
 562        lock_nd(nd);
 563        return PTR_ERR(dentry);
 564}
 565
 566/*
 567 * Name resolution.
 568 *
 569 * This is the basic name resolution function, turning a pathname
 570 * into the final dentry.
 571 *
 572 * We expect 'base' to be positive and a directory.
 573 */
 574int link_path_walk(const char * name, struct nameidata *nd)
 575{
 576        struct path next, pinned = {NULL, NULL};
 577        struct inode *inode;
 578        int err;
 579        unsigned int lookup_flags = nd->flags;
 580        
 581        while (*name=='/')
 582                name++;
 583        if (!*name)
 584                goto return_base;
 585
 586        inode = nd->dentry->d_inode;
 587        if (current->link_count)
 588                lookup_flags = LOOKUP_FOLLOW;
 589
 590        /* At this point we know we have a real path component. */
 591        for(;;) {
 592                unsigned long hash;
 593                struct qstr this;
 594                unsigned int c;
 595
 596                err = exec_permission_lite(inode);
 597                if (err == -EAGAIN) {
 598                        unlock_nd(nd);
 599                        err = permission(inode, MAY_EXEC);
 600                        lock_nd(nd);
 601                }
 602                if (err)
 603                        break;
 604
 605                this.name = name;
 606                c = *(const unsigned char *)name;
 607
 608                hash = init_name_hash();
 609                do {
 610                        name++;
 611                        hash = partial_name_hash(c, hash);
 612                        c = *(const unsigned char *)name;
 613                } while (c && (c != '/'));
 614                this.len = name - (const char *) this.name;
 615                this.hash = end_name_hash(hash);
 616
 617                /* remove trailing slashes? */
 618                if (!c)
 619                        goto last_component;
 620                while (*++name == '/');
 621                if (!*name)
 622                        goto last_with_slashes;
 623
 624                /*
 625                 * "." and ".." are special - ".." especially so because it has
 626                 * to be able to know about the current root directory and
 627                 * parent relationships.
 628                 */
 629                if (this.name[0] == '.') switch (this.len) {
 630                        default:
 631                                break;
 632                        case 2: 
 633                                if (this.name[1] != '.')
 634                                        break;
 635                                follow_dotdot(&nd->mnt, &nd->dentry);
 636                                inode = nd->dentry->d_inode;
 637                                /* fallthrough */
 638                        case 1:
 639                                continue;
 640                }
 641                /*
 642                 * See if the low-level filesystem might want
 643                 * to use its own hash..
 644                 */
 645                if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
 646                        err = nd->dentry->d_op->d_hash(nd->dentry, &this);
 647                        if (err < 0)
 648                                break;
 649                }
 650                /* This does the actual lookups.. */
 651                err = do_lookup(nd, &this, &next, &pinned, LOOKUP_CONTINUE);
 652                if (err)
 653                        break;
 654                /* Check mountpoints.. */
 655                follow_mount(&next.mnt, &next.dentry);
 656
 657                err = -ENOENT;
 658                inode = next.dentry->d_inode;
 659                if (!inode)
 660                        break;
 661                err = -ENOTDIR; 
 662                if (!inode->i_op)
 663                        break;
 664
 665                if (inode->i_op->follow_link) {
 666                        mntget(next.mnt);
 667                        dget_locked(next.dentry);
 668                        unlock_nd(nd);
 669                        err = do_follow_link(next.dentry, nd);
 670                        dput(next.dentry);
 671                        mntput(next.mnt);
 672                        if (err)
 673                                goto return_err;
 674                        lock_nd(nd);
 675                        err = -ENOENT;
 676                        inode = nd->dentry->d_inode;
 677                        if (!inode)
 678                                break;
 679                        err = -ENOTDIR; 
 680                        if (!inode->i_op)
 681                                break;
 682                } else {
 683                        nd->mnt = next.mnt;
 684                        nd->dentry = next.dentry;
 685                }
 686                err = -ENOTDIR; 
 687                if (!inode->i_op->lookup)
 688                        break;
 689                continue;
 690                /* here ends the main loop */
 691
 692last_with_slashes:
 693                lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
 694last_component:
 695                if (lookup_flags & LOOKUP_PARENT)
 696                        goto lookup_parent;
 697                if (this.name[0] == '.') switch (this.len) {
 698                        default:
 699                                break;
 700                        case 2: 
 701                                if (this.name[1] != '.')
 702                                        break;
 703                                follow_dotdot(&nd->mnt, &nd->dentry);
 704                                inode = nd->dentry->d_inode;
 705                                /* fallthrough */
 706                        case 1:
 707                                goto return_base;
 708                }
 709                if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
 710                        err = nd->dentry->d_op->d_hash(nd->dentry, &this);
 711                        if (err < 0)
 712                                break;
 713                }
 714                err = do_lookup(nd, &this, &next, &pinned, 0);
 715                if (err)
 716                        break;
 717                follow_mount(&next.mnt, &next.dentry);
 718                inode = next.dentry->d_inode;
 719                if ((lookup_flags & LOOKUP_FOLLOW)
 720                    && inode && inode->i_op && inode->i_op->follow_link) {
 721                        mntget(next.mnt);
 722                        dget_locked(next.dentry);
 723                        unlock_nd(nd);
 724                        err = do_follow_link(next.dentry, nd);
 725                        dput(next.dentry);
 726                        mntput(next.mnt);
 727                        if (err)
 728                                goto return_err;
 729                        inode = nd->dentry->d_inode;
 730                        lock_nd(nd);
 731                } else {
 732                        nd->mnt = next.mnt;
 733                        nd->dentry = next.dentry;
 734                }
 735                err = -ENOENT;
 736                if (!inode)
 737                        break;
 738                if (lookup_flags & LOOKUP_DIRECTORY) {
 739                        err = -ENOTDIR; 
 740                        if (!inode->i_op || !inode->i_op->lookup)
 741                                break;
 742                }
 743                goto return_base;
 744lookup_parent:
 745                nd->last = this;
 746                nd->last_type = LAST_NORM;
 747                if (this.name[0] != '.')
 748                        goto return_base;
 749                if (this.len == 1)
 750                        nd->last_type = LAST_DOT;
 751                else if (this.len == 2 && this.name[1] == '.')
 752                        nd->last_type = LAST_DOTDOT;
 753return_base:
 754                unlock_nd(nd);
 755                dput(pinned.dentry);
 756                mntput(pinned.mnt);
 757                return 0;
 758        }
 759        unlock_nd(nd);
 760        path_release(nd);
 761return_err:
 762        dput(pinned.dentry);
 763        mntput(pinned.mnt);
 764        return err;
 765}
 766
 767int path_walk(const char * name, struct nameidata *nd)
 768{
 769        current->total_link_count = 0;
 770        lock_nd(nd);
 771        return link_path_walk(name, nd);
 772}
 773
 774/* SMP-safe */
 775/* returns 1 if everything is done */
 776static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
 777{
 778        if (path_walk(name, nd))
 779                return 0;               /* something went wrong... */
 780
 781        if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
 782                struct nameidata nd_root;
 783                /*
 784                 * NAME was not found in alternate root or it's a directory.  Try to find
 785                 * it in the normal root:
 786                 */
 787                nd_root.last_type = LAST_ROOT;
 788                nd_root.flags = nd->flags;
 789                read_lock(&current->fs->lock);
 790                nd_root.mnt = mntget(current->fs->rootmnt);
 791                nd_root.dentry = dget(current->fs->root);
 792                read_unlock(&current->fs->lock);
 793                if (path_walk(name, &nd_root))
 794                        return 1;
 795                if (nd_root.dentry->d_inode) {
 796                        path_release(nd);
 797                        nd->dentry = nd_root.dentry;
 798                        nd->mnt = nd_root.mnt;
 799                        nd->last = nd_root.last;
 800                        return 1;
 801                }
 802                path_release(&nd_root);
 803        }
 804        return 1;
 805}
 806
 807void set_fs_altroot(void)
 808{
 809        char *emul = __emul_prefix();
 810        struct nameidata nd;
 811        struct vfsmount *mnt = NULL, *oldmnt;
 812        struct dentry *dentry = NULL, *olddentry;
 813        int err;
 814
 815        if (!emul)
 816                goto set_it;
 817        err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
 818        if (!err) {
 819                mnt = nd.mnt;
 820                dentry = nd.dentry;
 821        }
 822set_it:
 823        write_lock(&current->fs->lock);
 824        oldmnt = current->fs->altrootmnt;
 825        olddentry = current->fs->altroot;
 826        current->fs->altrootmnt = mnt;
 827        current->fs->altroot = dentry;
 828        write_unlock(&current->fs->lock);
 829        if (olddentry) {
 830                dput(olddentry);
 831                mntput(oldmnt);
 832        }
 833}
 834
 835/* SMP-safe */
 836static inline int
 837walk_init_root(const char *name, struct nameidata *nd)
 838{
 839        read_lock(&current->fs->lock);
 840        if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
 841                nd->mnt = mntget(current->fs->altrootmnt);
 842                nd->dentry = dget(current->fs->altroot);
 843                read_unlock(&current->fs->lock);
 844                if (__emul_lookup_dentry(name,nd))
 845                        return 0;
 846                read_lock(&current->fs->lock);
 847        }
 848        nd->mnt = mntget(current->fs->rootmnt);
 849        nd->dentry = dget(current->fs->root);
 850        read_unlock(&current->fs->lock);
 851        return 1;
 852}
 853
 854int path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
 855{
 856        nd->last_type = LAST_ROOT; /* if there are only slashes... */
 857        nd->flags = flags;
 858        if (*name=='/') {
 859                read_lock(&current->fs->lock);
 860                if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
 861                        nd->mnt = mntget(current->fs->altrootmnt);
 862                        nd->dentry = dget(current->fs->altroot);
 863                        read_unlock(&current->fs->lock);
 864                        if (__emul_lookup_dentry(name,nd))
 865                                return 0;
 866                        read_lock(&current->fs->lock);
 867                }
 868                read_unlock(&current->fs->lock);
 869                spin_lock(&dcache_lock);
 870                nd->mnt = current->fs->rootmnt;
 871                nd->dentry = current->fs->root;
 872        }
 873        else{
 874                spin_lock(&dcache_lock);
 875                nd->mnt = current->fs->pwdmnt;
 876                nd->dentry = current->fs->pwd;
 877        }
 878        nd->old_mnt = NULL;
 879        nd->old_dentry = NULL;
 880        current->total_link_count = 0;
 881        return link_path_walk(name, nd);
 882}
 883
 884/*
 885 * Restricted form of lookup. Doesn't follow links, single-component only,
 886 * needs parent already locked. Doesn't follow mounts.
 887 * SMP-safe.
 888 */
 889struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
 890{
 891        struct dentry * dentry;
 892        struct inode *inode;
 893        int err;
 894
 895        inode = base->d_inode;
 896        err = permission(inode, MAY_EXEC);
 897        dentry = ERR_PTR(err);
 898        if (err)
 899                goto out;
 900
 901        /*
 902         * See if the low-level filesystem might want
 903         * to use its own hash..
 904         */
 905        if (base->d_op && base->d_op->d_hash) {
 906                err = base->d_op->d_hash(base, name);
 907                dentry = ERR_PTR(err);
 908                if (err < 0)
 909                        goto out;
 910        }
 911
 912        dentry = cached_lookup(base, name, 0);
 913        if (!dentry) {
 914                struct dentry *new = d_alloc(base, name);
 915                dentry = ERR_PTR(-ENOMEM);
 916                if (!new)
 917                        goto out;
 918                dentry = inode->i_op->lookup(inode, new);
 919                if (!dentry) {
 920                        dentry = new;
 921                        security_ops->inode_post_lookup(inode, dentry);
 922                } else
 923                        dput(new);
 924        }
 925out:
 926        return dentry;
 927}
 928
 929/* SMP-safe */
 930struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
 931{
 932        unsigned long hash;
 933        struct qstr this;
 934        unsigned int c;
 935
 936        this.name = name;
 937        this.len = len;
 938        if (!len)
 939                goto access;
 940
 941        hash = init_name_hash();
 942        while (len--) {
 943                c = *(const unsigned char *)name++;
 944                if (c == '/' || c == '\0')
 945                        goto access;
 946                hash = partial_name_hash(c, hash);
 947        }
 948        this.hash = end_name_hash(hash);
 949
 950        return lookup_hash(&this, base);
 951access:
 952        return ERR_PTR(-EACCES);
 953}
 954
 955/*
 956 *      namei()
 957 *
 958 * is used by most simple commands to get the inode of a specified name.
 959 * Open, link etc use their own routines, but this is enough for things
 960 * like 'chmod' etc.
 961 *
 962 * namei exists in two versions: namei/lnamei. The only difference is
 963 * that namei follows links, while lnamei does not.
 964 * SMP-safe
 965 */
 966int __user_walk(const char *name, unsigned flags, struct nameidata *nd)
 967{
 968        char *tmp = getname(name);
 969        int err = PTR_ERR(tmp);
 970
 971        if (!IS_ERR(tmp)) {
 972                err = path_lookup(tmp, flags, nd);
 973                putname(tmp);
 974        }
 975        return err;
 976}
 977
 978/*
 979 * It's inline, so penalty for filesystems that don't use sticky bit is
 980 * minimal.
 981 */
 982static inline int check_sticky(struct inode *dir, struct inode *inode)
 983{
 984        if (!(dir->i_mode & S_ISVTX))
 985                return 0;
 986        if (inode->i_uid == current->fsuid)
 987                return 0;
 988        if (dir->i_uid == current->fsuid)
 989                return 0;
 990        return !capable(CAP_FOWNER);
 991}
 992
 993/*
 994 *      Check whether we can remove a link victim from directory dir, check
 995 *  whether the type of victim is right.
 996 *  1. We can't do it if dir is read-only (done in permission())
 997 *  2. We should have write and exec permissions on dir
 998 *  3. We can't remove anything from append-only dir
 999 *  4. We can't do anything with immutable dir (done in permission())
1000 *  5. If the sticky bit on dir is set we should either
1001 *      a. be owner of dir, or
1002 *      b. be owner of victim, or
1003 *      c. have CAP_FOWNER capability
1004 *  6. If the victim is append-only or immutable we can't do antyhing with
1005 *     links pointing to it.
1006 *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1007 *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1008 *  9. We can't remove a root or mountpoint.
1009 */
1010static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
1011{
1012        int error;
1013        if (!victim->d_inode || victim->d_parent->d_inode != dir)
1014                return -ENOENT;
1015        error = permission(dir,MAY_WRITE | MAY_EXEC);
1016        if (error)
1017                return error;
1018        if (IS_APPEND(dir))
1019                return -EPERM;
1020        if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1021            IS_IMMUTABLE(victim->d_inode))
1022                return -EPERM;
1023        if (isdir) {
1024                if (!S_ISDIR(victim->d_inode->i_mode))
1025                        return -ENOTDIR;
1026                if (IS_ROOT(victim))
1027                        return -EBUSY;
1028        } else if (S_ISDIR(victim->d_inode->i_mode))
1029                return -EISDIR;
1030        if (IS_DEADDIR(dir))
1031                return -ENOENT;
1032        return 0;
1033}
1034
1035/*      Check whether we can create an object with dentry child in directory
1036 *  dir.
1037 *  1. We can't do it if child already exists (open has special treatment for
1038 *     this case, but since we are inlined it's OK)
1039 *  2. We can't do it if dir is read-only (done in permission())
1040 *  3. We should have write and exec permissions on dir
1041 *  4. We can't do it if dir is immutable (done in permission())
1042 */
1043static inline int may_create(struct inode *dir, struct dentry *child) {
1044        if (child->d_inode)
1045                return -EEXIST;
1046        if (IS_DEADDIR(dir))
1047                return -ENOENT;
1048        return permission(dir,MAY_WRITE | MAY_EXEC);
1049}
1050
1051/* 
1052 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1053 * reasons.
1054 *
1055 * O_DIRECTORY translates into forcing a directory lookup.
1056 */
1057static inline int lookup_flags(unsigned int f)
1058{
1059        unsigned long retval = LOOKUP_FOLLOW;
1060
1061        if (f & O_NOFOLLOW)
1062                retval &= ~LOOKUP_FOLLOW;
1063        
1064        if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1065                retval &= ~LOOKUP_FOLLOW;
1066        
1067        if (f & O_DIRECTORY)
1068                retval |= LOOKUP_DIRECTORY;
1069
1070        return retval;
1071}
1072
1073/*
1074 * p1 and p2 should be directories on the same fs.
1075 */
1076struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1077{
1078        struct dentry *p;
1079
1080        if (p1 == p2) {
1081                down(&p1->d_inode->i_sem);
1082                return NULL;
1083        }
1084
1085        down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1086
1087        for (p = p1; p->d_parent != p; p = p->d_parent) {
1088                if (p->d_parent == p2) {
1089                        down(&p2->d_inode->i_sem);
1090                        down(&p1->d_inode->i_sem);
1091                        return p;
1092                }
1093        }
1094
1095        for (p = p2; p->d_parent != p; p = p->d_parent) {
1096                if (p->d_parent == p1) {
1097                        down(&p1->d_inode->i_sem);
1098                        down(&p2->d_inode->i_sem);
1099                        return p;
1100                }
1101        }
1102
1103        down(&p1->d_inode->i_sem);
1104        down(&p2->d_inode->i_sem);
1105        return NULL;
1106}
1107
1108void unlock_rename(struct dentry *p1, struct dentry *p2)
1109{
1110        up(&p1->d_inode->i_sem);
1111        if (p1 != p2) {
1112                up(&p2->d_inode->i_sem);
1113                up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1114        }
1115}
1116
1117int vfs_create(struct inode *dir, struct dentry *dentry, int mode)
1118{
1119        int error = may_create(dir, dentry);
1120
1121        if (error)
1122                return error;
1123
1124        if (!dir->i_op || !dir->i_op->create)
1125                return -EACCES; /* shouldn't it be ENOSYS? */
1126        mode &= S_IALLUGO;
1127        mode |= S_IFREG;
1128        error = security_ops->inode_create(dir, dentry, mode);
1129        if (error)
1130                return error;
1131        DQUOT_INIT(dir);
1132        error = dir->i_op->create(dir, dentry, mode);
1133        if (!error) {
1134                inode_dir_notify(dir, DN_CREATE);
1135                security_ops->inode_post_create(dir, dentry, mode);
1136        }
1137        return error;
1138}
1139
1140int may_open(struct nameidata *nd, int acc_mode, int flag)
1141{
1142        struct dentry *dentry = nd->dentry;
1143        struct inode *inode = dentry->d_inode;
1144        int error;
1145
1146        if (!inode)
1147                return -ENOENT;
1148
1149        if (S_ISLNK(inode->i_mode))
1150                return -ELOOP;
1151        
1152        if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1153                return -EISDIR;
1154
1155        error = permission(inode, acc_mode);
1156        if (error)
1157                return error;
1158
1159        /*
1160         * FIFO's, sockets and device files are special: they don't
1161         * actually live on the filesystem itself, and as such you
1162         * can write to them even if the filesystem is read-only.
1163         */
1164        if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1165                flag &= ~O_TRUNC;
1166        } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1167                if (nd->mnt->mnt_flags & MNT_NODEV)
1168                        return -EACCES;
1169
1170                flag &= ~O_TRUNC;
1171        } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1172                return -EROFS;
1173        /*
1174         * An append-only file must be opened in append mode for writing.
1175         */
1176        if (IS_APPEND(inode)) {
1177                if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1178                        return -EPERM;
1179                if (flag & O_TRUNC)
1180                        return -EPERM;
1181        }
1182
1183        /*
1184         * Ensure there are no outstanding leases on the file.
1185         */
1186        error = get_lease(inode, flag);
1187        if (error)
1188                return error;
1189
1190        if (flag & O_TRUNC) {
1191                error = get_write_access(inode);
1192                if (error)
1193                        return error;
1194
1195                /*
1196                 * Refuse to truncate files with mandatory locks held on them.
1197                 */
1198                error = locks_verify_locked(inode);
1199                if (!error) {
1200                        DQUOT_INIT(inode);
1201                        
1202                        error = do_truncate(dentry, 0);
1203                }
1204                put_write_access(inode);
1205                if (error)
1206                        return error;
1207        } else
1208                if (flag & FMODE_WRITE)
1209                        DQUOT_INIT(inode);
1210
1211        return 0;
1212}
1213
1214/*
1215 *      open_namei()
1216 *
1217 * namei for open - this is in fact almost the whole open-routine.
1218 *
1219 * Note that the low bits of "flag" aren't the same as in the open
1220 * system call - they are 00 - no permissions needed
1221 *                        01 - read permission needed
1222 *                        10 - write permission needed
1223 *                        11 - read/write permissions needed
1224 * which is a lot more logical, and also allows the "no perm" needed
1225 * for symlinks (where the permissions are checked later).
1226 * SMP-safe
1227 */
1228int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1229{
1230        int acc_mode, error = 0;
1231        struct dentry *dentry;
1232        struct dentry *dir;
1233        int count = 0;
1234
1235        acc_mode = ACC_MODE(flag);
1236
1237        /* Allow the LSM permission hook to distinguish append 
1238           access from general write access. */
1239        if (flag & O_APPEND)
1240                acc_mode |= MAY_APPEND;
1241
1242        /*
1243         * The simplest case - just a plain lookup.
1244         */
1245        if (!(flag & O_CREAT)) {
1246                error = path_lookup(pathname, lookup_flags(flag), nd);
1247                if (error)
1248                        return error;
1249                dentry = nd->dentry;
1250                goto ok;
1251        }
1252
1253        /*
1254         * Create - we need to know the parent.
1255         */
1256        error = path_lookup(pathname, LOOKUP_PARENT, nd);
1257        if (error)
1258                return error;
1259
1260        /*
1261         * We have the parent and last component. First of all, check
1262         * that we are not asked to creat(2) an obvious directory - that
1263         * will not do.
1264         */
1265        error = -EISDIR;
1266        if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1267                goto exit;
1268
1269        dir = nd->dentry;
1270        down(&dir->d_inode->i_sem);
1271        dentry = lookup_hash(&nd->last, nd->dentry);
1272
1273do_last:
1274        error = PTR_ERR(dentry);
1275        if (IS_ERR(dentry)) {
1276                up(&dir->d_inode->i_sem);
1277                goto exit;
1278        }
1279
1280        /* Negative dentry, just create the file */
1281        if (!dentry->d_inode) {
1282                error = vfs_create(dir->d_inode, dentry,
1283                                   mode & ~current->fs->umask);
1284                up(&dir->d_inode->i_sem);
1285                dput(nd->dentry);
1286                nd->dentry = dentry;
1287                if (error)
1288                        goto exit;
1289                /* Don't check for write permission, don't truncate */
1290                acc_mode = 0;
1291                flag &= ~O_TRUNC;
1292                goto ok;
1293        }
1294
1295        /*
1296         * It already exists.
1297         */
1298        up(&dir->d_inode->i_sem);
1299
1300        error = -EEXIST;
1301        if (flag & O_EXCL)
1302                goto exit_dput;
1303
1304        if (d_mountpoint(dentry)) {
1305                error = -ELOOP;
1306                if (flag & O_NOFOLLOW)
1307                        goto exit_dput;
1308                while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1309        }
1310        error = -ENOENT;
1311        if (!dentry->d_inode)
1312                goto exit_dput;
1313        if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1314                goto do_link;
1315
1316        dput(nd->dentry);
1317        nd->dentry = dentry;
1318        error = -EISDIR;
1319        if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1320                goto exit;
1321ok:
1322        error = may_open(nd, acc_mode, flag);
1323        if (error)
1324                goto exit;
1325        return 0;
1326
1327exit_dput:
1328        dput(dentry);
1329exit:
1330        path_release(nd);
1331        return error;
1332
1333do_link:
1334        error = -ELOOP;
1335        if (flag & O_NOFOLLOW)
1336                goto exit_dput;
1337        /*
1338         * This is subtle. Instead of calling do_follow_link() we do the
1339         * thing by hands. The reason is that this way we have zero link_count
1340         * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1341         * After that we have the parent and last component, i.e.
1342         * we are in the same situation as after the first path_walk().
1343         * Well, almost - if the last component is normal we get its copy
1344         * stored in nd->last.name and we will have to putname() it when we
1345         * are done. Procfs-like symlinks just set LAST_BIND.
1346         */
1347        error = security_ops->inode_follow_link(dentry, nd);
1348        if (error)
1349                goto exit_dput;
1350        UPDATE_ATIME(dentry->d_inode);
1351        error = dentry->d_inode->i_op->follow_link(dentry, nd);
1352        dput(dentry);
1353        if (error)
1354                return error;
1355        if (nd->last_type == LAST_BIND) {
1356                dentry = nd->dentry;
1357                goto ok;
1358        }
1359        error = -EISDIR;
1360        if (nd->last_type != LAST_NORM)
1361                goto exit;
1362        if (nd->last.name[nd->last.len]) {
1363                putname(nd->last.name);
1364                goto exit;
1365        }
1366        error = -ELOOP;
1367        if (count++==32) {
1368                putname(nd->last.name);
1369                goto exit;
1370        }
1371        dir = nd->dentry;
1372        down(&dir->d_inode->i_sem);
1373        dentry = lookup_hash(&nd->last, nd->dentry);
1374        putname(nd->last.name);
1375        goto do_last;
1376}
1377
1378/* SMP-safe */
1379static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1380{
1381        struct dentry *dentry;
1382
1383        down(&nd->dentry->d_inode->i_sem);
1384        dentry = ERR_PTR(-EEXIST);
1385        if (nd->last_type != LAST_NORM)
1386                goto fail;
1387        dentry = lookup_hash(&nd->last, nd->dentry);
1388        if (IS_ERR(dentry))
1389                goto fail;
1390        if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1391                goto enoent;
1392        return dentry;
1393enoent:
1394        dput(dentry);
1395        dentry = ERR_PTR(-ENOENT);
1396fail:
1397        return dentry;
1398}
1399
1400int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1401{
1402        int error = may_create(dir, dentry);
1403
1404        if (error)
1405                return error;
1406
1407        if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1408                return -EPERM;
1409
1410        if (!dir->i_op || !dir->i_op->mknod)
1411                return -EPERM;
1412
1413        error = security_ops->inode_mknod(dir, dentry, mode, dev);
1414        if (error)
1415                return error;
1416
1417        DQUOT_INIT(dir);
1418        error = dir->i_op->mknod(dir, dentry, mode, dev);
1419        if (!error) {
1420                inode_dir_notify(dir, DN_CREATE);
1421                security_ops->inode_post_mknod(dir, dentry, mode, dev);
1422        }
1423        return error;
1424}
1425
1426asmlinkage long sys_mknod(const char * filename, int mode, dev_t dev)
1427{
1428        int error = 0;
1429        char * tmp;
1430        struct dentry * dentry;
1431        struct nameidata nd;
1432
1433        if (S_ISDIR(mode))
1434                return -EPERM;
1435        tmp = getname(filename);
1436        if (IS_ERR(tmp))
1437                return PTR_ERR(tmp);
1438
1439        error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1440        if (error)
1441                goto out;
1442        dentry = lookup_create(&nd, 0);
1443        error = PTR_ERR(dentry);
1444
1445        mode &= ~current->fs->umask;
1446        if (!IS_ERR(dentry)) {
1447                switch (mode & S_IFMT) {
1448                case 0: case S_IFREG:
1449                        error = vfs_create(nd.dentry->d_inode,dentry,mode);
1450                        break;
1451                case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1452                        error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1453                        break;
1454                case S_IFDIR:
1455                        error = -EPERM;
1456                        break;
1457                default:
1458                        error = -EINVAL;
1459                }
1460                dput(dentry);
1461        }
1462        up(&nd.dentry->d_inode->i_sem);
1463        path_release(&nd);
1464out:
1465        putname(tmp);
1466
1467        return error;
1468}
1469
1470int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1471{
1472        int error = may_create(dir, dentry);
1473
1474        if (error)
1475                return error;
1476
1477        if (!dir->i_op || !dir->i_op->mkdir)
1478                return -EPERM;
1479
1480        mode &= (S_IRWXUGO|S_ISVTX);
1481        error = security_ops->inode_mkdir(dir, dentry, mode);
1482        if (error)
1483                return error;
1484
1485        DQUOT_INIT(dir);
1486        error = dir->i_op->mkdir(dir, dentry, mode);
1487        if (!error) {
1488                inode_dir_notify(dir, DN_CREATE);
1489                security_ops->inode_post_mkdir(dir,dentry, mode);
1490        }
1491        return error;
1492}
1493
1494asmlinkage long sys_mkdir(const char * pathname, int mode)
1495{
1496        int error = 0;
1497        char * tmp;
1498
1499        tmp = getname(pathname);
1500        error = PTR_ERR(tmp);
1501        if (!IS_ERR(tmp)) {
1502                struct dentry *dentry;
1503                struct nameidata nd;
1504
1505                error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1506                if (error)
1507                        goto out;
1508                dentry = lookup_create(&nd, 1);
1509                error = PTR_ERR(dentry);
1510                if (!IS_ERR(dentry)) {
1511                        error = vfs_mkdir(nd.dentry->d_inode, dentry,
1512                                          mode & ~current->fs->umask);
1513                        dput(dentry);
1514                }
1515                up(&nd.dentry->d_inode->i_sem);
1516                path_release(&nd);
1517out:
1518                putname(tmp);
1519        }
1520
1521        return error;
1522}
1523
1524/*
1525 * We try to drop the dentry early: we should have
1526 * a usage count of 2 if we're the only user of this
1527 * dentry, and if that is true (possibly after pruning
1528 * the dcache), then we drop the dentry now.
1529 *
1530 * A low-level filesystem can, if it choses, legally
1531 * do a
1532 *
1533 *      if (!d_unhashed(dentry))
1534 *              return -EBUSY;
1535 *
1536 * if it cannot handle the case of removing a directory
1537 * that is still in use by something else..
1538 */
1539static void d_unhash(struct dentry *dentry)
1540{
1541        dget(dentry);
1542        spin_lock(&dcache_lock);
1543        switch (atomic_read(&dentry->d_count)) {
1544        default:
1545                spin_unlock(&dcache_lock);
1546                shrink_dcache_parent(dentry);
1547                spin_lock(&dcache_lock);
1548                if (atomic_read(&dentry->d_count) != 2)
1549                        break;
1550        case 2:
1551                list_del_init(&dentry->d_hash);
1552        }
1553        spin_unlock(&dcache_lock);
1554}
1555
1556int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1557{
1558        int error = may_delete(dir, dentry, 1);
1559
1560        if (error)
1561                return error;
1562
1563        if (!dir->i_op || !dir->i_op->rmdir)
1564                return -EPERM;
1565
1566        DQUOT_INIT(dir);
1567
1568        down(&dentry->d_inode->i_sem);
1569        d_unhash(dentry);
1570        if (d_mountpoint(dentry))
1571                error = -EBUSY;
1572        else {
1573                error = security_ops->inode_rmdir(dir, dentry);
1574                if (!error) {
1575                        error = dir->i_op->rmdir(dir, dentry);
1576                        if (!error)
1577                                dentry->d_inode->i_flags |= S_DEAD;
1578                }
1579        }
1580        up(&dentry->d_inode->i_sem);
1581        if (!error) {
1582                inode_dir_notify(dir, DN_DELETE);
1583                d_delete(dentry);
1584        }
1585        dput(dentry);
1586
1587        return error;
1588}
1589
1590asmlinkage long sys_rmdir(const char * pathname)
1591{
1592        int error = 0;
1593        char * name;
1594        struct dentry *dentry;
1595        struct nameidata nd;
1596
1597        name = getname(pathname);
1598        if(IS_ERR(name))
1599                return PTR_ERR(name);
1600
1601        error = path_lookup(name, LOOKUP_PARENT, &nd);
1602        if (error)
1603                goto exit;
1604
1605        switch(nd.last_type) {
1606                case LAST_DOTDOT:
1607                        error = -ENOTEMPTY;
1608                        goto exit1;
1609                case LAST_DOT:
1610                        error = -EINVAL;
1611                        goto exit1;
1612                case LAST_ROOT:
1613                        error = -EBUSY;
1614                        goto exit1;
1615        }
1616        down(&nd.dentry->d_inode->i_sem);
1617        dentry = lookup_hash(&nd.last, nd.dentry);
1618        error = PTR_ERR(dentry);
1619        if (!IS_ERR(dentry)) {
1620                error = vfs_rmdir(nd.dentry->d_inode, dentry);
1621                dput(dentry);
1622        }
1623        up(&nd.dentry->d_inode->i_sem);
1624exit1:
1625        path_release(&nd);
1626exit:
1627        putname(name);
1628        return error;
1629}
1630
1631int vfs_unlink(struct inode *dir, struct dentry *dentry)
1632{
1633        int error = may_delete(dir, dentry, 0);
1634
1635        if (error)
1636                return error;
1637
1638        if (!dir->i_op || !dir->i_op->unlink)
1639                return -EPERM;
1640
1641        DQUOT_INIT(dir);
1642
1643        down(&dentry->d_inode->i_sem);
1644        if (d_mountpoint(dentry))
1645                error = -EBUSY;
1646        else {
1647                error = security_ops->inode_unlink(dir, dentry);
1648                if (!error) {
1649                        error = dir->i_op->unlink(dir, dentry);
1650                }
1651        }
1652        up(&dentry->d_inode->i_sem);
1653        if (!error) {
1654                d_delete(dentry);
1655                inode_dir_notify(dir, DN_DELETE);
1656        }
1657        return error;
1658}
1659
1660asmlinkage long sys_unlink(const char * pathname)
1661{
1662        int error = 0;
1663        char * name;
1664        struct dentry *dentry;
1665        struct nameidata nd;
1666
1667        name = getname(pathname);
1668        if(IS_ERR(name))
1669                return PTR_ERR(name);
1670
1671        error = path_lookup(name, LOOKUP_PARENT, &nd);
1672        if (error)
1673                goto exit;
1674        error = -EISDIR;
1675        if (nd.last_type != LAST_NORM)
1676                goto exit1;
1677        down(&nd.dentry->d_inode->i_sem);
1678        dentry = lookup_hash(&nd.last, nd.dentry);
1679        error = PTR_ERR(dentry);
1680        if (!IS_ERR(dentry)) {
1681                /* Why not before? Because we want correct error value */
1682                if (nd.last.name[nd.last.len])
1683                        goto slashes;
1684                error = vfs_unlink(nd.dentry->d_inode, dentry);
1685        exit2:
1686                dput(dentry);
1687        }
1688        up(&nd.dentry->d_inode->i_sem);
1689exit1:
1690        path_release(&nd);
1691exit:
1692        putname(name);
1693
1694        return error;
1695
1696slashes:
1697        error = !dentry->d_inode ? -ENOENT :
1698                S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1699        goto exit2;
1700}
1701
1702int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1703{
1704        int error = may_create(dir, dentry);
1705
1706        if (error)
1707                return error;
1708
1709        if (!dir->i_op || !dir->i_op->symlink)
1710                return -EPERM;
1711
1712        error = security_ops->inode_symlink(dir, dentry, oldname);
1713        if (error)
1714                return error;
1715
1716        DQUOT_INIT(dir);
1717        error = dir->i_op->symlink(dir, dentry, oldname);
1718        if (!error) {
1719                inode_dir_notify(dir, DN_CREATE);
1720                security_ops->inode_post_symlink(dir, dentry, oldname);
1721        }
1722        return error;
1723}
1724
1725asmlinkage long sys_symlink(const char * oldname, const char * newname)
1726{
1727        int error = 0;
1728        char * from;
1729        char * to;
1730
1731        from = getname(oldname);
1732        if(IS_ERR(from))
1733                return PTR_ERR(from);
1734        to = getname(newname);
1735        error = PTR_ERR(to);
1736        if (!IS_ERR(to)) {
1737                struct dentry *dentry;
1738                struct nameidata nd;
1739
1740                error = path_lookup(to, LOOKUP_PARENT, &nd);
1741                if (error)
1742                        goto out;
1743                dentry = lookup_create(&nd, 0);
1744                error = PTR_ERR(dentry);
1745                if (!IS_ERR(dentry)) {
1746                        error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1747                        dput(dentry);
1748                }
1749                up(&nd.dentry->d_inode->i_sem);
1750                path_release(&nd);
1751out:
1752                putname(to);
1753        }
1754        putname(from);
1755        return error;
1756}
1757
1758int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1759{
1760        struct inode *inode = old_dentry->d_inode;
1761        int error;
1762
1763        if (!inode)
1764                return -ENOENT;
1765
1766        error = may_create(dir, new_dentry);
1767        if (error)
1768                return error;
1769
1770        if (dir->i_sb != inode->i_sb)
1771                return -EXDEV;
1772
1773        /*
1774         * A link to an append-only or immutable file cannot be created.
1775         */
1776        if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1777                return -EPERM;
1778        if (!dir->i_op || !dir->i_op->link)
1779                return -EPERM;
1780        if (S_ISDIR(old_dentry->d_inode->i_mode))
1781                return -EPERM;
1782
1783        error = security_ops->inode_link(old_dentry, dir, new_dentry);
1784        if (error)
1785                return error;
1786
1787        down(&old_dentry->d_inode->i_sem);
1788        DQUOT_INIT(dir);
1789        error = dir->i_op->link(old_dentry, dir, new_dentry);
1790        up(&old_dentry->d_inode->i_sem);
1791        if (!error) {
1792                inode_dir_notify(dir, DN_CREATE);
1793                security_ops->inode_post_link(old_dentry, dir, new_dentry);
1794        }
1795        return error;
1796}
1797
1798/*
1799 * Hardlinks are often used in delicate situations.  We avoid
1800 * security-related surprises by not following symlinks on the
1801 * newname.  --KAB
1802 *
1803 * We don't follow them on the oldname either to be compatible
1804 * with linux 2.0, and to avoid hard-linking to directories
1805 * and other special files.  --ADM
1806 */
1807asmlinkage long sys_link(const char * oldname, const char * newname)
1808{
1809        struct dentry *new_dentry;
1810        struct nameidata nd, old_nd;
1811        int error;
1812        char * to;
1813
1814        to = getname(newname);
1815        if (IS_ERR(to))
1816                return PTR_ERR(to);
1817
1818        error = __user_walk(oldname, 0, &old_nd);
1819        if (error)
1820                goto exit;
1821        error = path_lookup(to, LOOKUP_PARENT, &nd);
1822        if (error)
1823                goto out;
1824        error = -EXDEV;
1825        if (old_nd.mnt != nd.mnt)
1826                goto out_release;
1827        new_dentry = lookup_create(&nd, 0);
1828        error = PTR_ERR(new_dentry);
1829        if (!IS_ERR(new_dentry)) {
1830                error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1831                dput(new_dentry);
1832        }
1833        up(&nd.dentry->d_inode->i_sem);
1834out_release:
1835        path_release(&nd);
1836out:
1837        path_release(&old_nd);
1838exit:
1839        putname(to);
1840
1841        return error;
1842}
1843
1844/*
1845 * The worst of all namespace operations - renaming directory. "Perverted"
1846 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1847 * Problems:
1848 *      a) we can get into loop creation. Check is done in is_subdir().
1849 *      b) race potential - two innocent renames can create a loop together.
1850 *         That's where 4.4 screws up. Current fix: serialization on
1851 *         sb->s_vfs_rename_sem. We might be more accurate, but that's another
1852 *         story.
1853 *      c) we have to lock _three_ objects - parents and victim (if it exists).
1854 *         And that - after we got ->i_sem on parents (until then we don't know
1855 *         whether the target exists).  Solution: try to be smart with locking
1856 *         order for inodes.  We rely on the fact that tree topology may change
1857 *         only under ->s_vfs_rename_sem _and_ that parent of the object we
1858 *         move will be locked.  Thus we can rank directories by the tree
1859 *         (ancestors first) and rank all non-directories after them.
1860 *         That works since everybody except rename does "lock parent, lookup,
1861 *         lock child" and rename is under ->s_vfs_rename_sem.
1862 *         HOWEVER, it relies on the assumption that any object with ->lookup()
1863 *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
1864 *         we'd better make sure that there's no link(2) for them.
1865 *      d) some filesystems don't support opened-but-unlinked directories,
1866 *         either because of layout or because they are not ready to deal with
1867 *         all cases correctly. The latter will be fixed (taking this sort of
1868 *         stuff into VFS), but the former is not going away. Solution: the same
1869 *         trick as in rmdir().
1870 *      e) conversion from fhandle to dentry may come in the wrong moment - when
1871 *         we are removing the target. Solution: we will have to grab ->i_sem
1872 *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1873 *         ->i_sem on parents, which works but leads to some truely excessive
1874 *         locking].
1875 */
1876int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1877               struct inode *new_dir, struct dentry *new_dentry)
1878{
1879        int error = 0;
1880        struct inode *target;
1881
1882        /*
1883         * If we are going to change the parent - check write permissions,
1884         * we'll need to flip '..'.
1885         */
1886        if (new_dir != old_dir) {
1887                error = permission(old_dentry->d_inode, MAY_WRITE);
1888                if (error)
1889                        return error;
1890        }
1891
1892        error = security_ops->inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1893        if (error)
1894                return error;
1895
1896        target = new_dentry->d_inode;
1897        if (target) {
1898                down(&target->i_sem);
1899                d_unhash(new_dentry);
1900        }
1901        if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1902                error = -EBUSY;
1903        else 
1904                error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1905        if (target) {
1906                if (!error)
1907                        target->i_flags |= S_DEAD;
1908                up(&target->i_sem);
1909                if (d_unhashed(new_dentry))
1910                        d_rehash(new_dentry);
1911                dput(new_dentry);
1912        }
1913        if (!error) {
1914                d_move(old_dentry,new_dentry);
1915                security_ops->inode_post_rename(old_dir, old_dentry,
1916                                                        new_dir, new_dentry);
1917        }
1918        return error;
1919}
1920
1921int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1922               struct inode *new_dir, struct dentry *new_dentry)
1923{
1924        struct inode *target;
1925        int error;
1926
1927        error = security_ops->inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1928        if (error)
1929                return error;
1930
1931        dget(new_dentry);
1932        target = new_dentry->d_inode;
1933        if (target)
1934                down(&target->i_sem);
1935        if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1936                error = -EBUSY;
1937        else
1938                error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1939        if (!error) {
1940                /* The following d_move() should become unconditional */
1941                if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
1942                        d_move(old_dentry, new_dentry);
1943                security_ops->inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
1944        }
1945        if (target)
1946                up(&target->i_sem);
1947        dput(new_dentry);
1948        return error;
1949}
1950
1951int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1952               struct inode *new_dir, struct dentry *new_dentry)
1953{
1954        int error;
1955        int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1956
1957        if (old_dentry->d_inode == new_dentry->d_inode)
1958                return 0;
1959 
1960        error = may_delete(old_dir, old_dentry, is_dir);
1961        if (error)
1962                return error;
1963
1964        if (!new_dentry->d_inode)
1965                error = may_create(new_dir, new_dentry);
1966        else
1967                error = may_delete(new_dir, new_dentry, is_dir);
1968        if (error)
1969                return error;
1970
1971        if (!old_dir->i_op || !old_dir->i_op->rename)
1972                return -EPERM;
1973
1974        DQUOT_INIT(old_dir);
1975        DQUOT_INIT(new_dir);
1976
1977        if (is_dir)
1978                error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1979        else
1980                error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1981        if (!error) {
1982                if (old_dir == new_dir)
1983                        inode_dir_notify(old_dir, DN_RENAME);
1984                else {
1985                        inode_dir_notify(old_dir, DN_DELETE);
1986                        inode_dir_notify(new_dir, DN_CREATE);
1987                }
1988        }
1989        return error;
1990}
1991
1992static inline int do_rename(const char * oldname, const char * newname)
1993{
1994        int error = 0;
1995        struct dentry * old_dir, * new_dir;
1996        struct dentry * old_dentry, *new_dentry;
1997        struct dentry * trap;
1998        struct nameidata oldnd, newnd;
1999
2000        error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2001        if (error)
2002                goto exit;
2003
2004        error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2005        if (error)
2006                goto exit1;
2007
2008        error = -EXDEV;
2009        if (oldnd.mnt != newnd.mnt)
2010                goto exit2;
2011
2012        old_dir = oldnd.dentry;
2013        error = -EBUSY;
2014        if (oldnd.last_type != LAST_NORM)
2015                goto exit2;
2016
2017        new_dir = newnd.dentry;
2018        if (newnd.last_type != LAST_NORM)
2019                goto exit2;
2020
2021        trap = lock_rename(new_dir, old_dir);
2022
2023        old_dentry = lookup_hash(&oldnd.last, old_dir);
2024        error = PTR_ERR(old_dentry);
2025        if (IS_ERR(old_dentry))
2026                goto exit3;
2027        /* source must exist */
2028        error = -ENOENT;
2029        if (!old_dentry->d_inode)
2030                goto exit4;
2031        /* unless the source is a directory trailing slashes give -ENOTDIR */
2032        if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2033                error = -ENOTDIR;
2034                if (oldnd.last.name[oldnd.last.len])
2035                        goto exit4;
2036                if (newnd.last.name[newnd.last.len])
2037                        goto exit4;
2038        }
2039        /* source should not be ancestor of target */
2040        error = -EINVAL;
2041        if (old_dentry == trap)
2042                goto exit4;
2043        new_dentry = lookup_hash(&newnd.last, new_dir);
2044        error = PTR_ERR(new_dentry);
2045        if (IS_ERR(new_dentry))
2046                goto exit4;
2047        /* target should not be an ancestor of source */
2048        error = -ENOTEMPTY;
2049        if (new_dentry == trap)
2050                goto exit5;
2051
2052        error = vfs_rename(old_dir->d_inode, old_dentry,
2053                                   new_dir->d_inode, new_dentry);
2054exit5:
2055        dput(new_dentry);
2056exit4:
2057        dput(old_dentry);
2058exit3:
2059        unlock_rename(new_dir, old_dir);
2060exit2:
2061        path_release(&newnd);
2062exit1:
2063        path_release(&oldnd);
2064exit:
2065        return error;
2066}
2067
2068asmlinkage long sys_rename(const char * oldname, const char * newname)
2069{
2070        int error;
2071        char * from;
2072        char * to;
2073
2074        from = getname(oldname);
2075        if(IS_ERR(from))
2076                return PTR_ERR(from);
2077        to = getname(newname);
2078        error = PTR_ERR(to);
2079        if (!IS_ERR(to)) {
2080                error = do_rename(from,to);
2081                putname(to);
2082        }
2083        putname(from);
2084        return error;
2085}
2086
2087int vfs_readlink(struct dentry *dentry, char *buffer, int buflen, const char *link)
2088{
2089        int len;
2090
2091        len = PTR_ERR(link);
2092        if (IS_ERR(link))
2093                goto out;
2094
2095        len = strlen(link);
2096        if (len > (unsigned) buflen)
2097                len = buflen;
2098        if (copy_to_user(buffer, link, len))
2099                len = -EFAULT;
2100out:
2101        return len;
2102}
2103
2104static inline int
2105__vfs_follow_link(struct nameidata *nd, const char *link)
2106{
2107        int res = 0;
2108        char *name;
2109        if (IS_ERR(link))
2110                goto fail;
2111
2112        if (*link == '/') {
2113                path_release(nd);
2114                if (!walk_init_root(link, nd))
2115                        /* weird __emul_prefix() stuff did it */
2116                        goto out;
2117        }
2118        lock_nd(nd);
2119        res = link_path_walk(link, nd);
2120out:
2121        if (current->link_count || res || nd->last_type!=LAST_NORM)
2122                return res;
2123        /*
2124         * If it is an iterative symlinks resolution in open_namei() we
2125         * have to copy the last component. And all that crap because of
2126         * bloody create() on broken symlinks. Furrfu...
2127         */
2128        name = __getname();
2129        if (!name)
2130                return -ENOMEM;
2131        strcpy(name, nd->last.name);
2132        nd->last.name = name;
2133        return 0;
2134fail:
2135        path_release(nd);
2136        return PTR_ERR(link);
2137}
2138
2139int vfs_follow_link(struct nameidata *nd, const char *link)
2140{
2141        return __vfs_follow_link(nd, link);
2142}
2143
2144/* get the link contents into pagecache */
2145static char *page_getlink(struct dentry * dentry, struct page **ppage)
2146{
2147        struct page * page;
2148        struct address_space *mapping = dentry->d_inode->i_mapping;
2149        page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2150                                NULL);
2151        if (IS_ERR(page))
2152                goto sync_fail;
2153        wait_on_page_locked(page);
2154        if (!PageUptodate(page))
2155                goto async_fail;
2156        *ppage = page;
2157        return kmap(page);
2158
2159async_fail:
2160        page_cache_release(page);
2161        return ERR_PTR(-EIO);
2162
2163sync_fail:
2164        return (char*)page;
2165}
2166
2167int page_readlink(struct dentry *dentry, char *buffer, int buflen)
2168{
2169        struct page *page = NULL;
2170        char *s = page_getlink(dentry, &page);
2171        int res = vfs_readlink(dentry,buffer,buflen,s);
2172        if (page) {
2173                kunmap(page);
2174                page_cache_release(page);
2175        }
2176        return res;
2177}
2178
2179int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2180{
2181        struct page *page = NULL;
2182        char *s = page_getlink(dentry, &page);
2183        int res = __vfs_follow_link(nd, s);
2184        if (page) {
2185                kunmap(page);
2186                page_cache_release(page);
2187        }
2188        return res;
2189}
2190
2191int page_symlink(struct inode *inode, const char *symname, int len)
2192{
2193        struct address_space *mapping = inode->i_mapping;
2194        struct page *page = grab_cache_page(mapping, 0);
2195        int err = -ENOMEM;
2196        char *kaddr;
2197
2198        if (!page)
2199                goto fail;
2200        err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2201        if (err)
2202                goto fail_map;
2203        kaddr = kmap_atomic(page, KM_USER0);
2204        memcpy(kaddr, symname, len-1);
2205        kunmap_atomic(kaddr, KM_USER0);
2206        mapping->a_ops->commit_write(NULL, page, 0, len-1);
2207        /*
2208         * Notice that we are _not_ going to block here - end of page is
2209         * unmapped, so this will only try to map the rest of page, see
2210         * that it is unmapped (typically even will not look into inode -
2211         * ->i_size will be enough for everything) and zero it out.
2212         * OTOH it's obviously correct and should make the page up-to-date.
2213         */
2214        if (!PageUptodate(page)) {
2215                err = mapping->a_ops->readpage(NULL, page);
2216                wait_on_page_locked(page);
2217        } else {
2218                unlock_page(page);
2219        }
2220        page_cache_release(page);
2221        if (err < 0)
2222                goto fail;
2223        mark_inode_dirty(inode);
2224        return 0;
2225fail_map:
2226        unlock_page(page);
2227        page_cache_release(page);
2228fail:
2229        return err;
2230}
2231
2232struct inode_operations page_symlink_inode_operations = {
2233        .readlink       = page_readlink,
2234        .follow_link    = page_follow_link,
2235};
2236
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.