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 <linux/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->rec_data1;
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->rec_data2;
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 /*restore time attr of dir inode*/
163 struct iattr *iattr = &r_rec->u_rec.ur_pattr;
165 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
166 rc = fsfilt->fs_setattr(dparent, handle, iattr, 0);
168 CERROR("error on parent setattr: rc = %d\n", rc);
174 rc = fsfilt->fs_commit(dir, handle, 0);
175 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
176 SMFS_SET_INODE_REC(dir);
184 static int lvfs_undo_unlink(struct super_block *sb,
185 struct reint_record *r_rec)
187 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
188 char *path = r_rec->rec_data1;
190 struct dentry *dparent;
191 struct dentry *dentry = NULL;
192 struct nameidata del_nd;
193 struct dentry *del_dparent = NULL;
194 struct dentry *del_dentry = NULL;
196 struct inode *dir = NULL;
199 /*get parent dentry*/
200 rc = lookup_by_path(path, LOOKUP_PARENT, &nd);
205 dir = dparent->d_inode;
207 dentry = lookup_create(&nd, 0);
209 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
210 SMFS_CLEAN_INODE_REC(dir);
211 if (SMFS_DO_DEC_LINK(r_rec->u_rec.ur_flags)) {
212 ino_t ino = *((ino_t *)r_rec->rec_data2);
213 struct inode* inode = iget(dir->i_sb, ino);
215 GOTO(cleanup1, rc = -EINVAL);
216 handle = fsfilt->fs_start(dir, FSFILT_OP_LINK, NULL, 0);
218 GOTO(cleanup1, rc = PTR_ERR(handle));
220 del_dentry = pre_smfs_dentry(NULL, inode, dentry);
221 rc = vfs_link(del_dentry, dir, dentry);
223 post_smfs_dentry(del_dentry);
226 char *del_path = r_rec->rec_data2;
228 rc = lookup_by_path(del_path, LOOKUP_PARENT, &del_nd);
230 GOTO(cleanup, rc = -ENODEV);
231 del_dparent = del_nd.dentry;
232 del_dentry = lookup_one_len(del_nd.last.name, del_dparent,
235 if (! del_dentry || !del_dentry->d_inode
236 || is_bad_inode(del_dentry->d_inode))
237 GOTO(cleanup2, rc = -ENODEV);
239 handle = fsfilt->fs_start(dir, FSFILT_OP_RENAME, NULL, 0);
241 GOTO(cleanup2, rc = PTR_ERR(handle));
244 /*move the del dentry back to the original palace*/
245 rc = vfs_rename(del_dparent->d_inode, del_dentry, dir, dentry);
247 if (!rc && S_ISDIR(del_dentry->d_inode->i_mode))
248 del_dentry->d_inode->i_flags &=~S_DEAD;
252 path_release(&del_nd);
255 /*restore time attr of dir inode*/
256 struct iattr *iattr = &r_rec->u_rec.ur_pattr;
258 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
259 rc = fsfilt->fs_setattr(dparent, handle, iattr, 0);
261 CERROR("error on parent setattr: rc = %d\n", rc);
267 fsfilt->fs_commit(dir, handle, 0);
271 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
272 SMFS_SET_INODE_REC(dir);
276 static int lvfs_undo_rename(struct super_block *sb,
277 struct reint_record *r_rec)
279 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
280 char *path = r_rec->rec_data1;
281 char *new_path = r_rec->rec_data2;
283 struct nameidata new_nd;
284 struct dentry *dparent;
285 struct dentry *new_dparent;
286 struct dentry *dentry = NULL;
287 struct dentry *new_dentry = NULL;
289 struct inode *dir = NULL;
290 struct inode *new_dir = NULL;
293 /*get parent dentry*/
294 rc = lookup_by_path(path, LOOKUP_PARENT, &nd);
299 dir = dparent->d_inode;
300 dentry = lookup_create(&nd, 0);
302 rc = lookup_by_path(new_path, LOOKUP_PARENT, &new_nd);
307 new_dparent = new_nd.dentry;
308 new_dir = new_dparent->d_inode;
309 new_dentry = lookup_one_len(new_nd.last.name, new_dparent,
312 if (! new_dentry || !new_dentry->d_inode
313 || is_bad_inode(new_dentry->d_inode)) {
314 GOTO(cleanup, rc = -ENODEV);
317 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
318 SMFS_CLEAN_INODE_REC(new_dir);
320 handle = fsfilt->fs_start(new_dir, FSFILT_OP_RENAME, NULL, 0);
322 GOTO(cleanup, rc = PTR_ERR(handle));
325 /*move the del dentry back to the original palace*/
326 rc = vfs_rename(new_dir, new_dentry, dir, dentry);
329 CERROR("Error for undo node %s\n", new_path);
332 /*restore time attr of dir inode*/
333 struct iattr *iattr = &r_rec->u_rec.ur_pattr;
335 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
336 rc = fsfilt->fs_setattr(dparent, handle, iattr, 0);
338 CERROR("error on parent setattr: rc = %d\n", rc);
344 rc = fsfilt->fs_commit(new_dir, handle, 0);
349 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
350 SMFS_SET_INODE_REC(new_dir);
352 path_release(&new_nd);
356 static int lvfs_undo_setattr(struct super_block *sb,
357 struct reint_record *r_rec)
359 struct fsfilt_operations *fsfilt = S2SMI(sb)->sm_fsfilt;
360 char *path = r_rec->rec_data1;
362 struct dentry *dparent = NULL;
363 struct dentry *dentry = NULL;
364 struct inode *dir = NULL;
368 rc = lookup_by_path(path, LOOKUP_PARENT, &nd);
373 dir = dparent->d_inode;
374 dentry = lookup_one_len(nd.last.name, dparent, nd.last.len);
376 if (!dentry || !dentry->d_inode
377 || is_bad_inode(dentry->d_inode)) {
381 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
382 SMFS_CLEAN_INODE_REC(dir);
383 handle = fsfilt->fs_start(dir, FSFILT_OP_SETATTR, NULL, 0);
385 GOTO(cleanup, rc = PTR_ERR(handle));
387 /*FIXME later, did not set parent attr*/
388 r_rec->u_rec.ur_iattr.ia_valid = r_rec->u_rec.ur_pattr.ia_valid;
389 rc = fsfilt->fs_setattr(dentry, handle, &r_rec->u_rec.ur_iattr, 0);
392 fsfilt->fs_commit(dir, handle, 0);
393 if (!SMFS_DO_WRITE_KML(r_rec->u_rec.ur_flags))
394 SMFS_SET_INODE_REC(dir);
404 typedef int (*lvfs_undoer)(struct super_block *sb, struct reint_record *);
406 static lvfs_undoer undoers[REINT_MAX + 1] = {
407 [REINT_SETATTR] lvfs_undo_setattr,
408 [REINT_CREATE] lvfs_undo_create,
409 [REINT_LINK] lvfs_undo_link,
410 [REINT_UNLINK] lvfs_undo_unlink,
411 [REINT_RENAME] lvfs_undo_rename,
414 int lvfs_undo(struct super_block *sb,
417 return undoers[((struct reint_record*)r_rec)->u_rec.ur_opcode](sb,
418 (struct reint_record *)r_rec);
421 EXPORT_SYMBOL(lvfs_undo);