}
 EXPORT_SYMBOL(path_put);
 
+#define EMBEDDED_LEVELS 2
 struct nameidata {
        struct path     path;
        union {
                struct path link;
                void *cookie;
                const char *name;
-       } stack[MAX_NESTED_LINKS + 1];
+       } *stack, internal[EMBEDDED_LEVELS];
 };
 
+static void set_nameidata(struct nameidata *nd)
+{
+       nd->stack = nd->internal;
+}
+
+static void restore_nameidata(struct nameidata *nd)
+{
+       if (nd->stack != nd->internal) {
+               kfree(nd->stack);
+               nd->stack = nd->internal;
+       }
+}
+
+static int __nd_alloc_stack(struct nameidata *nd)
+{
+       struct saved *p = kmalloc((MAXSYMLINKS + 1) * sizeof(struct saved),
+                                 GFP_KERNEL);
+       if (unlikely(!p))
+               return -ENOMEM;
+       memcpy(p, nd->internal, sizeof(nd->internal));
+       nd->stack = p;
+       return 0;
+}
+
+static inline int nd_alloc_stack(struct nameidata *nd)
+{
+       if (likely(nd->depth != EMBEDDED_LEVELS - 1))
+               return 0;
+       if (likely(nd->stack != nd->internal))
+               return 0;
+       return __nd_alloc_stack(nd);
+}
+
 /*
  * Path walking has 2 modes, rcu-walk and ref-walk (see
  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
        if (nd->link.mnt == nd->path.mnt)
                mntget(nd->link.mnt);
 
-       if (unlikely(current->total_link_count >= 40)) {
+       if (unlikely(current->total_link_count >= MAXSYMLINKS)) {
                path_put(&nd->path);
                path_put(&nd->link);
                return ERR_PTR(-ELOOP);
                if (err) {
                        const char *s;
 
-                       if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
-                               path_put_conditional(&nd->link, nd);
-                               path_put(&nd->path);
-                               err = -ELOOP;
-                               goto Err;
+                       err = nd_alloc_stack(nd);
+                       if (unlikely(err)) {
+                               path_to_nameidata(&nd->link, nd);
+                               break;
                        }
-                       BUG_ON(nd->depth >= MAX_NESTED_LINKS);
 
                        nd->depth++;
-                       current->link_count++;
 
                        s = get_link(nd);
 
                        if (unlikely(IS_ERR(s))) {
                                err = PTR_ERR(s);
-                               current->link_count--;
                                nd->depth--;
                                goto Err;
                        }
                        if (unlikely(!s)) {
                                /* jumped */
                                put_link(nd);
-                               current->link_count--;
                                nd->depth--;
                        } else {
                                if (*s == '/') {
 Err:
        while (unlikely(nd->depth)) {
                put_link(nd);
-               current->link_count--;
                nd->depth--;
        }
        return err;
                name = nd->stack[nd->depth].name;
                err = walk_component(nd, LOOKUP_FOLLOW);
                put_link(nd);
-               current->link_count--;
                nd->depth--;
                goto Walked;
        }
 static int filename_lookup(int dfd, struct filename *name,
                                unsigned int flags, struct nameidata *nd)
 {
-       int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
+       int retval;
+
+       set_nameidata(nd);
+       retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
+
        if (unlikely(retval == -ECHILD))
                retval = path_lookupat(dfd, name, flags, nd);
        if (unlikely(retval == -ESTALE))
 
        if (likely(!retval))
                audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
+       restore_nameidata(nd);
        return retval;
 }
 
        int error;
        if (IS_ERR(name))
                return PTR_ERR(name);
+       set_nameidata(&nd);
        error = path_mountpoint(dfd, name, path, &nd, flags | LOOKUP_RCU);
        if (unlikely(error == -ECHILD))
                error = path_mountpoint(dfd, name, path, &nd, flags);
                error = path_mountpoint(dfd, name, path, &nd, flags | LOOKUP_REVAL);
        if (likely(!error))
                audit_inode(name, path->dentry, 0);
+       restore_nameidata(&nd);
        putname(name);
        return error;
 }
        int flags = op->lookup_flags;
        struct file *filp;
 
+       set_nameidata(&nd);
        filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
        if (unlikely(filp == ERR_PTR(-ECHILD)))
                filp = path_openat(dfd, pathname, &nd, op, flags);
        if (unlikely(filp == ERR_PTR(-ESTALE)))
                filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
+       restore_nameidata(&nd);
        return filp;
 }
 
 
        nd.root.mnt = mnt;
        nd.root.dentry = dentry;
+       set_nameidata(&nd);
 
        if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
                return ERR_PTR(-ELOOP);
                file = path_openat(-1, filename, &nd, op, flags);
        if (unlikely(file == ERR_PTR(-ESTALE)))
                file = path_openat(-1, filename, &nd, op, flags | LOOKUP_REVAL);
+       restore_nameidata(&nd);
        putname(filename);
        return file;
 }