nd->flags &= ~LOOKUP_PARENT;
        err = walk_component(nd, nd->flags & LOOKUP_FOLLOW);
+       if (nd->depth)
+               put_link(nd);
        if (err < 0)
                terminate_walk(nd);
        return err;
         */
        err = path_init(dfd, name, flags, nd);
        if (!err && !(flags & LOOKUP_PARENT)) {
-               err = lookup_last(nd);
-               while (err > 0) {
+               while ((err = lookup_last(nd)) > 0) {
                        err = trailing_symlink(nd);
                        if (err)
                                break;
-                       err = lookup_last(nd);
-                       put_link(nd);
                }
        }
 
                dput(dentry);
                goto out;
        }
+       if (nd->depth)
+               put_link(nd);
        path->dentry = dentry;
        path->mnt = nd->path.mnt;
        if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW)) {
        error = 0;
 out:
        terminate_walk(nd);
+       if (nd->depth)
+               put_link(nd);
        return error;
 }
 
        if (unlikely(err))
                goto out;
 
-       err = mountpoint_last(nd, path);
-       while (err > 0) {
+       while ((err = mountpoint_last(nd, path)) > 0) {
                err = trailing_symlink(nd);
                if (err)
                        break;
-               err = mountpoint_last(nd, path);
-               put_link(nd);
        }
 out:
        path_cleanup(nd);
                error = handle_dots(nd, nd->last_type);
                if (unlikely(error)) {
                        terminate_walk(nd);
+                       if (nd->depth)
+                               put_link(nd);
                        return error;
                }
                goto finish_open;
                 * about to look up
                 */
                error = complete_walk(nd);
-               if (error)
+               if (error) {
+                       if (nd->depth)
+                               put_link(nd);
                        return error;
+               }
 
                audit_inode(name, dir, LOOKUP_PARENT);
                error = -EISDIR;
                        }
                }
                BUG_ON(inode != path.dentry->d_inode);
+               if (nd->depth)
+                       put_link(nd);
                nd->link = path;
                return 1;
        }
 finish_open:
        error = complete_walk(nd);
        if (error) {
+               if (nd->depth)
+                       put_link(nd);
                path_put(&save_parent);
                return error;
        }
                mnt_drop_write(nd->path.mnt);
        path_put(&save_parent);
        terminate_walk(nd);
+       if (nd->depth)
+               put_link(nd);
        return error;
 
 exit_dput:
        if (unlikely(error))
                goto out;
 
-       error = do_last(nd, file, op, &opened, pathname);
-       while (unlikely(error > 0)) { /* trailing symlink */
+       while ((error = do_last(nd, file, op, &opened, pathname)) > 0) {
                nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
                error = trailing_symlink(nd);
                if (unlikely(error))
                        break;
-               error = do_last(nd, file, op, &opened, pathname);
-               put_link(nd);
        }
 out:
        path_cleanup(nd);