1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
9 #define DEBUG_SUBSYSTEM S_FILTER
11 #include <linux/version.h>
13 #include <asm/unistd.h>
14 #include <linux/jbd.h>
15 #include <linux/slab.h>
16 #include <linux/pagemap.h>
17 #include <linux/quotaops.h>
18 #include <linux/version.h>
19 #include <libcfs/kp30.h>
20 #include <linux/lustre_fsfilt.h>
21 #include <linux/obd.h>
22 #include <linux/obd_class.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/lustre_compat25.h>
26 #include <linux/lvfs.h>
27 #include <linux/lustre_smfs.h>
28 #include "lvfs_internal.h"
30 #include <linux/obd.h>
31 #include <linux/lustre_lib.h>
33 static int lvfs_undo_create(struct super_block *sb,
34 struct reint_record *r_rec)
36 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
37 char *path = r_rec->name.path_name;
38 int type = r_rec->u_rec.ur_iattr.ia_mode & S_IFMT;
40 struct dentry *dparent = NULL;
41 struct dentry *dentry = NULL;
42 struct inode *dir = NULL;
46 rc = lookup_by_path(path, LOOKUP_PARENT, &nd);
51 dir = dparent->d_inode;
54 dentry = lookup_one_len(nd.last.name, dparent, nd.last.len);
56 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
63 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
64 SMFS_CLEAN_INODE_REC(dir);
73 handle = fsfilt->fs_start(dir, FSFILT_OP_UNLINK, NULL, 0);
75 GOTO(cleanup, rc = PTR_ERR(handle));
76 rc = vfs_unlink(dir, dentry);
78 CERROR("unlink in error path: %d\n", rc);
81 handle = fsfilt->fs_start(dir, FSFILT_OP_RMDIR, NULL, 0);
83 GOTO(cleanup, rc = PTR_ERR(handle));
85 rc = vfs_rmdir(dir, dentry);
87 CERROR("rmdir in error path: %d\n", rc);
90 CERROR("Error type %d in create\n", type);
96 CERROR("Error for undo node %s\n", path);
99 /*restore time attr of dir inode*/
100 struct iattr *iattr = &r_rec->u_rec.ur_pattr;
102 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
103 rc = fsfilt->fs_setattr(dparent, handle, iattr, 0);
105 CERROR("error on parent setattr: rc = %d\n", rc);
111 rc = fsfilt->fs_commit(dparent->d_inode, handle, 0);
112 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
113 SMFS_SET_INODE_REC(dir);
121 static int lvfs_undo_link(struct super_block *sb,
122 struct reint_record *r_rec)
124 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
125 char *new_path = r_rec->u.re_name.path_name;
127 struct dentry *dparent = NULL;
128 struct dentry *dentry = NULL;
129 struct inode *dir = NULL;
133 rc = lookup_by_path(new_path, LOOKUP_PARENT, &nd);
138 dir = dparent->d_inode;
141 dentry = lookup_one_len(nd.last.name, dparent, nd.last.len);
143 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
148 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
149 SMFS_CLEAN_INODE_REC(dir);
151 handle = fsfilt->fs_start(dir, FSFILT_OP_UNLINK, NULL, 0);
153 GOTO(cleanup, rc = PTR_ERR(handle));
154 rc = vfs_unlink(dir, dentry);
156 CERROR("unlink in error path: %d\n", rc);
159 CERROR("Error for undo node %s\n", new_path);
162 struct iattr *iattr = &r_rec->u_rec.ur_pattr;
164 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
165 rc = fsfilt->fs_setattr(dparent, handle, iattr, 0);
167 CERROR("error on parent setattr: rc = %d\n", rc);
173 rc = fsfilt->fs_commit(dir, handle, 0);
174 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
175 SMFS_SET_INODE_REC(dir);
183 static int lvfs_undo_unlink(struct super_block *sb,
184 struct reint_record *r_rec)
186 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
187 char *path = r_rec->name.path_name;
189 struct dentry *dparent;
190 struct dentry *dentry = NULL;
191 struct nameidata del_nd;
192 struct dentry *del_dparent = NULL;
193 struct dentry *del_dentry = NULL;
195 struct inode *dir = NULL;
198 /*get parent dentry*/
199 rc = lookup_by_path(path, LOOKUP_PARENT, &nd);
204 dir = dparent->d_inode;
206 dentry = lookup_create(&nd, 0);
208 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
209 SMFS_CLEAN_INODE_REC(dir);
210 if (SMFS_DO_DEC_LINK(r_rec->u_rec.ur_flags)) {
211 ino_t ino = *((ino_t *)r_rec->u.re_name.path_name);
212 struct inode* inode = iget(dir->i_sb, ino);
214 GOTO(cleanup1, rc = -EINVAL);
215 handle = fsfilt->fs_start(dir, FSFILT_OP_LINK, NULL, 0);
217 GOTO(cleanup1, rc = PTR_ERR(handle));
219 del_dentry = pre_smfs_dentry(NULL, inode, dentry);
220 rc = vfs_link(del_dentry, dir, dentry);
222 post_smfs_dentry(del_dentry);
225 char *del_path = r_rec->u.re_name.path_name;
227 rc = lookup_by_path(del_path, LOOKUP_PARENT, &del_nd);
229 GOTO(cleanup, rc = -ENODEV);
230 del_dparent = del_nd.dentry;
231 del_dentry = lookup_one_len(del_nd.last.name, del_dparent,
234 if (! del_dentry || !del_dentry->d_inode
235 || is_bad_inode(del_dentry->d_inode))
236 GOTO(cleanup2, rc = -ENODEV);
238 handle = fsfilt->fs_start(dir, FSFILT_OP_RENAME, NULL, 0);
240 GOTO(cleanup2, rc = PTR_ERR(handle));
243 /*move the del dentry back to the original palace*/
244 rc = vfs_rename(del_dparent->d_inode, del_dentry, dir, dentry);
246 if (!rc && S_ISDIR(del_dentry->d_inode->i_mode))
247 del_dentry->d_inode->i_flags &=~S_DEAD;
251 path_release(&del_nd);
255 struct iattr *iattr = &r_rec->u_rec.ur_pattr;
257 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
258 rc = fsfilt->fs_setattr(dparent, handle, iattr, 0);
260 CERROR("error on parent setattr: rc = %d\n", rc);
266 fsfilt->fs_commit(dir, handle, 0);
270 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
271 SMFS_SET_INODE_REC(dir);
275 static int lvfs_undo_rename(struct super_block *sb,
276 struct reint_record *r_rec)
278 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
279 char *path = r_rec->name.path_name;
280 char *new_path = r_rec->u.re_name.path_name;
282 struct nameidata new_nd;
283 struct dentry *dparent;
284 struct dentry *new_dparent;
285 struct dentry *dentry = NULL;
286 struct dentry *new_dentry = NULL;
288 struct inode *dir = NULL;
289 struct inode *new_dir = NULL;
292 /*get parent dentry*/
293 rc = lookup_by_path(path, LOOKUP_PARENT, &nd);
298 dir = dparent->d_inode;
299 dentry = lookup_create(&nd, 0);
301 rc = lookup_by_path(new_path, LOOKUP_PARENT, &new_nd);
306 new_dparent = new_nd.dentry;
307 new_dir = new_dparent->d_inode;
308 new_dentry = lookup_one_len(new_nd.last.name, new_dparent,
311 if (! new_dentry || !new_dentry->d_inode
312 || is_bad_inode(new_dentry->d_inode)) {
313 GOTO(cleanup, rc = -ENODEV);
316 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
317 SMFS_CLEAN_INODE_REC(new_dir);
319 handle = fsfilt->fs_start(new_dir, FSFILT_OP_RENAME, NULL, 0);
321 GOTO(cleanup, rc = PTR_ERR(handle));
324 /*move the del dentry back to the original palace*/
325 rc = vfs_rename(new_dir, new_dentry, dir, dentry);
328 CERROR("Error for undo node %s\n", new_path);
331 struct iattr *iattr = &r_rec->u_rec.ur_pattr;
333 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
334 rc = fsfilt->fs_setattr(dparent, handle, iattr, 0);
336 CERROR("error on parent setattr: rc = %d\n", rc);
342 rc = fsfilt->fs_commit(new_dir, handle, 0);
347 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
348 SMFS_SET_INODE_REC(new_dir);
350 path_release(&new_nd);
354 static int lvfs_undo_setattr(struct super_block *sb,
355 struct reint_record *r_rec)
357 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
358 char *path = r_rec->name.path_name;
360 struct dentry *dparent = NULL;
361 struct dentry *dentry = NULL;
362 struct inode *dir = NULL;
366 rc = lookup_by_path(path, LOOKUP_PARENT, &nd);
371 dir = dparent->d_inode;
372 dentry = lookup_one_len(nd.last.name, dparent, nd.last.len);
374 if (!dentry || !dentry->d_inode
375 || is_bad_inode(dentry->d_inode)) {
379 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
380 SMFS_CLEAN_INODE_REC(dir);
381 handle = fsfilt->fs_start(dir, FSFILT_OP_SETATTR, NULL, 0);
383 GOTO(cleanup, rc = PTR_ERR(handle));
385 /* FIXME later, did not set parent attr. */
386 r_rec->u_rec.ur_iattr.ia_valid = r_rec->u_rec.ur_pattr.ia_valid;
387 rc = fsfilt->fs_setattr(dentry, handle, &r_rec->u_rec.ur_iattr, 0);
390 fsfilt->fs_commit(dir, handle, 0);
391 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
392 SMFS_SET_INODE_REC(dir);
402 typedef int (*lvfs_undoer)(struct super_block *sb, struct reint_record *);
404 static lvfs_undoer undoers[REINT_MAX + 1] = {
405 [REINT_SETATTR] lvfs_undo_setattr,
406 [REINT_CREATE] lvfs_undo_create,
407 [REINT_LINK] lvfs_undo_link,
408 [REINT_UNLINK] lvfs_undo_unlink,
409 [REINT_RENAME] lvfs_undo_rename,
412 int lvfs_undo(struct super_block *sb,
415 return undoers[((struct reint_record*)r_rec)->u_rec.ur_opcode](sb,
416 (struct reint_record *)r_rec);
419 EXPORT_SYMBOL(lvfs_undo);