4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
25 #define DEBUG_SUBSYSTEM S_FILTER
28 #include <libcfs/libcfs.h>
31 /* refine later and change to seqlock or simlar from libcfs */
32 /* Debugging check only needed during development */
34 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
35 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERTF(!segment_eq(get_fs(), get_ds()),\
37 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), get_ds()), msg)
39 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
40 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
41 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
44 /* push / pop to root of obd store */
45 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx)
47 /* if there is underlaying dt_device then push_ctxt is not needed */
48 if (new_ctx->dt != NULL)
51 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
52 ASSERT_CTXT_MAGIC(new_ctx->magic);
53 OBD_SET_CTXT_MAGIC(save);
56 LASSERT(d_count(current->fs->pwd.dentry));
57 LASSERT(d_count(new_ctx->pwd));
58 save->pwd = dget(current->fs->pwd.dentry);
59 save->pwdmnt = mntget(current->fs->pwd.mnt);
60 save->umask = current_umask();
63 LASSERT(save->pwdmnt);
64 LASSERT(new_ctx->pwd);
65 LASSERT(new_ctx->pwdmnt);
67 current->fs->umask = 0; /* umask already applied on client */
69 ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
71 EXPORT_SYMBOL(push_ctxt);
73 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx)
75 /* if there is underlaying dt_device then pop_ctxt is not needed */
76 if (new_ctx->dt != NULL)
79 ASSERT_CTXT_MAGIC(saved->magic);
80 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
82 LASSERTF(current->fs->pwd.dentry == new_ctx->pwd, "%p != %p\n",
83 current->fs->pwd.dentry, new_ctx->pwd);
84 LASSERTF(current->fs->pwd.mnt == new_ctx->pwdmnt, "%p != %p\n",
85 current->fs->pwd.mnt, new_ctx->pwdmnt);
88 ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
91 mntput(saved->pwdmnt);
92 current->fs->umask = saved->umask;
94 EXPORT_SYMBOL(pop_ctxt);
96 /* utility to rename a file */
97 int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
98 char *oldname, char *newname)
100 struct dentry *dchild_old, *dchild_new;
104 ASSERT_KERNEL_CTXT("kernel doing rename outside kernel context\n");
105 CDEBUG(D_INODE, "renaming file %.*s to %.*s\n",
106 (int)strlen(oldname), oldname, (int)strlen(newname), newname);
108 dchild_old = ll_lookup_one_len(oldname, dir, strlen(oldname));
109 if (IS_ERR(dchild_old))
110 RETURN(PTR_ERR(dchild_old));
112 if (!dchild_old->d_inode)
113 GOTO(put_old, err = -ENOENT);
115 dchild_new = ll_lookup_one_len(newname, dir, strlen(newname));
116 if (IS_ERR(dchild_new))
117 GOTO(put_old, err = PTR_ERR(dchild_new));
119 err = vfs_rename(dir->d_inode, dchild_old, dir->d_inode, dchild_new);
126 #endif /* __KERNEL__ */