Whamcloud - gitweb
1)do precreate record in obdfilter
[fs/lustre-release.git] / lustre / smfs / kml.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/kml.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27
28 #define DEBUG_SUBSYSTEM S_SM
29
30 #include <linux/kmod.h>
31 #include <linux/init.h>
32 #include <linux/fs.h>
33 #include <linux/slab.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_support.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_idl.h>
38 #include <linux/lustre_fsfilt.h>
39 #include <linux/lustre_smfs.h>
40 #include "smfs_internal.h"
41
42 smfs_pack_rec_func smfs_get_rec_pack_type(struct super_block *sb)
43 {
44         struct smfs_super_info *smsi = S2SMI(sb);
45
46         int index = GET_REC_PACK_TYPE_INDEX(smsi->smsi_flags);
47
48         return smsi->smsi_pack_rec[index];
49 }
50
51 int smfs_rec_init(struct super_block *sb)
52 {
53         struct smfs_super_info *smfs_info = S2SMI(sb);
54         int rc = 0;
55
56         SMFS_SET_REC(smfs_info);
57
58         ost_rec_pack_init(sb);
59         mds_rec_pack_init(sb);
60
61         RETURN(rc);
62 }
63
64 int smfs_rec_cleanup(struct super_block *sb)
65 {
66         int rc = 0;
67
68         SMFS_CLEAN_REC(S2SMI(sb));
69         RETURN(rc);
70 }
71
72 static inline void copy_inode_attr(struct iattr *iattr, struct inode *inode)
73 {
74         iattr->ia_mode = inode->i_mode;
75         iattr->ia_uid  = inode->i_uid;
76         iattr->ia_gid  = inode->i_gid;
77         iattr->ia_atime = inode->i_atime;
78         iattr->ia_ctime = inode->i_ctime;
79         iattr->ia_mtime = inode->i_mtime;
80         iattr->ia_size = inode->i_size;
81 }
82
83 void smfs_rec_pack(struct update_record *rec, struct inode *dst,
84                    void *data, int op)
85 {
86         rec->ur_fsuid = current->fsuid;
87         rec->ur_fsgid = current->fsgid;
88         rec->ur_rdev = dst->i_rdev;
89         rec->ur_opcode = op;
90         copy_inode_attr(&rec->ur_iattr, dst);
91         if (data) {
92                 switch (op) {
93                 case REINT_CREATE:
94                 case REINT_LINK:
95                 case REINT_UNLINK:
96                 case REINT_RENAME: {
97                         struct inode *dir = (struct inode *)data;
98                         copy_inode_attr(&rec->ur_pattr, dir);
99                         break;
100                 }
101                 case REINT_SETATTR: {
102                         struct iattr *attr = (struct iattr *)data;
103                         memcpy(&rec->ur_pattr, attr, sizeof(struct iattr));
104                         break;
105                 }
106                 }
107         }
108         return;
109 }
110
111 static inline int unpack_rec_data(char **p_buffer, int *size,
112                                   char *in_data, char *args_data)
113 {
114         int args_len = 0;
115         int rc = 0;
116
117         if (args_data)
118                 args_len = strlen(args_data);
119
120         *size = *((int*)(in_data));
121         rc = *size + sizeof(int);
122
123         OBD_ALLOC(*p_buffer, *size + args_len + 1);
124         if (!*p_buffer)
125                 RETURN(-ENOMEM);
126         /*First copy reint dir */
127         if (args_data)
128                 memcpy(*p_buffer, args_data, args_len);
129
130         /*then copy the node name */
131         memcpy(*p_buffer + args_len,
132                       (in_data + sizeof(int)), *size);
133
134         *size += args_len;
135
136         RETURN(rc);
137 }
138
139 int smfs_rec_unpack(struct smfs_proc_args *args, char *record, 
140                     char **pbuf, int *opcode)
141 {
142         int offset = *(int *)(record);
143         char *tmp = record + offset + sizeof(int);
144         int rc = 0;
145         *opcode = *(int *)tmp;
146         *pbuf = tmp + sizeof(*opcode);
147         RETURN(rc);
148 }
149 EXPORT_SYMBOL(smfs_rec_unpack);
150
151 int smfs_start_rec(struct super_block *sb, struct vfsmount *mnt)
152 {
153         struct dentry *dentry;
154         struct lvfs_run_ctxt saved;
155         int rc = 0;
156         ENTRY;
157
158         if (SMFS_INIT_REC(S2SMI(sb)) ||
159             (!SMFS_DO_REC(S2SMI(sb)) && !SMFS_CACHE_HOOK(S2SMI(sb))))
160                 RETURN(rc);
161         
162         rc = smfs_llog_setup(sb, mnt);
163         if (rc)
164                 RETURN(rc); 
165         push_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
166         dentry = simple_mkdir(current->fs->pwd, "DELETE", 0777, 1);
167         if (IS_ERR(dentry)) {
168                 rc = PTR_ERR(dentry);
169                 CERROR("cannot create DELETE directory: rc = %d\n", rc);
170                 GOTO(err_exit, rc = -EINVAL);
171         }
172         S2SMI(sb)->smsi_delete_dir = dentry;
173
174         if (!rc)
175                 SMFS_SET_INIT_REC(S2SMI(sb));
176 exit:
177         pop_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
178         RETURN(rc);
179 err_exit:
180         if (S2SMI(sb)->smsi_ctxt)
181                 OBD_FREE(S2SMI(sb)->smsi_ctxt, sizeof(struct lvfs_run_ctxt));
182         goto exit;
183 }
184 EXPORT_SYMBOL(smfs_start_rec);
185
186 int smfs_stop_rec(struct super_block *sb)
187 {
188         int rc = 0;
189
190         if (!SMFS_INIT_REC(S2SMI(sb)) ||
191             (!SMFS_DO_REC(S2SMI(sb)) && !SMFS_CACHE_HOOK(S2SMI(sb))))
192                 RETURN(rc);
193
194         rc = smfs_llog_cleanup(sb);
195
196         SMFS_CLEAN_INIT_REC(S2SMI(sb));
197
198         if (S2SMI(sb)->smsi_delete_dir) {
199                 l_dput(S2SMI(sb)->smsi_delete_dir);
200                 S2SMI(sb)->smsi_delete_dir = NULL;
201         }
202         RETURN(rc);
203 }
204 EXPORT_SYMBOL(smfs_stop_rec);
205
206 int smfs_write_extents(struct inode *dir, struct dentry *dentry,
207                        unsigned long from, unsigned long num)
208 {
209         return smfs_post_rec_write(dir, dentry, &from, &num);
210 }
211 EXPORT_SYMBOL(smfs_write_extents);
212
213 int smfs_rec_setattr(struct inode *dir, struct dentry *dentry,
214                      struct iattr *attr)
215 {
216         return smfs_post_rec_setattr(dir, dentry, attr, NULL);
217 }
218 EXPORT_SYMBOL(smfs_rec_setattr);
219
220 int smfs_rec_md(struct inode *inode, void * lmm, int lmm_size)
221 {
222         char *set_lmm = NULL;
223         int  rc = 0;
224         ENTRY;
225
226         if (lmm) {
227                 OBD_ALLOC(set_lmm, lmm_size + sizeof(lmm_size));
228                 if (!set_lmm)
229                         RETURN(-ENOMEM);
230                 memcpy(set_lmm, &lmm_size, sizeof(lmm_size));
231                 memcpy(set_lmm + sizeof(lmm_size), lmm, lmm_size);
232                 rc = smfs_post_rec_setattr(inode, NULL, NULL, set_lmm);
233                 if (rc) {
234                         CERROR("Error: Record md for inode %lu rc=%d\n",
235                                 inode->i_ino, rc);
236                 }
237         }
238         if (set_lmm)
239                 OBD_FREE(set_lmm, lmm_size + sizeof(lmm_size));
240         return rc;
241 }
242 EXPORT_SYMBOL(smfs_rec_md);
243
244 int smfs_rec_precreate(struct dentry *dentry, int *num, struct obdo *oa)
245 {
246        return smfs_post_rec_create(dentry->d_inode, dentry, num, oa);
247 }
248 EXPORT_SYMBOL(smfs_rec_precreate);
249
250 int smfs_process_rec(struct super_block *sb,
251                      int count, char *dir, int flags)
252 {
253         struct llog_ctxt *ctxt;
254         struct llog_handle *loghandle;
255         struct smfs_proc_args args;
256         int rc = 0;
257
258         if (!SMFS_INIT_REC(S2SMI(sb))) {
259                 CWARN("Did not start up rec server \n");
260                 RETURN(rc);
261         }
262
263         memset(&args, 0, sizeof(struct smfs_proc_args));
264         args.sr_sb = sb;
265         args.sr_count = count;
266         args.sr_data = dir;
267         args.sr_flags = flags ;
268         ctxt = S2SMI(sb)->smsi_rec_log;
269         loghandle = ctxt->loc_handle;
270
271         if (count == 0) {
272                 if (SMFS_DO_REINT_REC(flags)) {
273                         struct llog_gen_rec *lgr;
274
275                         /*For reint rec, we need insert
276                           *a gen rec to identify the end
277                           *of the rec.*/
278                         OBD_ALLOC(lgr, sizeof(*lgr));
279                         if (!lgr)
280                                 RETURN(-ENOMEM);
281                         lgr->lgr_hdr.lrh_len = lgr->lgr_tail.lrt_len = sizeof(*lgr);
282                         lgr->lgr_hdr.lrh_type = LLOG_GEN_REC;
283                         lgr->lgr_gen = ctxt->loc_gen;
284                         rc = llog_add(ctxt, &lgr->lgr_hdr, NULL, NULL, 1, NULL);
285                         OBD_FREE(lgr, sizeof(*lgr));
286                         if (rc != 1)
287                                 RETURN(rc);
288                 }
289         } else {
290                 SET_REC_COUNT_FLAGS(args.sr_flags, SMFS_REC_ALL);
291         }
292         if (loghandle) {
293                 if (SMFS_DO_REINT_REC(flags))
294                         rc = llog_cat_process(loghandle, ctxt->llog_proc_cb,
295                                               (void *)&args);
296                 else
297                         rc = llog_cat_reverse_process(loghandle,
298                                                       ctxt->llog_proc_cb,
299                                                       (void *)&args);
300                 if (rc == LLOG_PROC_BREAK)
301                         rc = 0;
302         }
303         RETURN(rc);
304 }