Whamcloud - gitweb
54ad1adeb013bae082159aa91c3278c8a35680a4
[fs/lustre-release.git] / lustre / smfs / mds_kml.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Lustre filesystem abstraction routines
5  *
6  *  Copyright (C) 2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_SM
25
26 #include <linux/kmod.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/slab.h>
30 #include <linux/dcache.h>
31 #include <linux/obd_class.h>
32 #include <linux/obd_support.h>
33 #include <linux/lustre_lib.h>
34 #include <linux/lustre_idl.h>
35 #include <linux/lustre_fsfilt.h>
36 #include <linux/lustre_lite.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_smfs.h>
39 #include "smfs_internal.h"
40
41 static int mds_rec_link_pack(char *buffer, struct dentry *dentry,
42                              struct inode *dir, void *data1, void *data2)
43 {
44         struct dentry *src = (struct dentry *)data1;
45         struct dentry *tgt = (struct dentry *)data2;
46         struct mds_kml_pack_info *mkpi;
47         struct lustre_msg *msg = NULL;
48         struct mdc_op_data *op_data;
49         void *tmp = NULL;
50         int rc = 0;
51
52         OBD_ALLOC(op_data, sizeof(*op_data));
53         if (op_data == NULL)
54                 return -ENOMEM;
55         
56         mdc_prepare_mdc_data(op_data, src->d_inode, dir,
57                              (char *)tgt->d_name.name,
58                              tgt->d_name.len, 0);
59
60         PACK_KML_REC_INIT(buffer, MDS_REINT);
61         mkpi = (struct mds_kml_pack_info *)buffer;
62
63         mkpi->mpi_bufcount = 2;
64         mkpi->mpi_size[0] = sizeof(struct mds_rec_link);
65         mkpi->mpi_size[1] = op_data->namelen + 1;
66
67         /* the mds reint log format is: opcode + mkpi + request  */
68         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
69         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
70
71         tmp = mdc_link_pack(msg, 0, op_data);
72         OBD_FREE(op_data, sizeof(*op_data));
73         mkpi->mpi_total_size = tmp - (void *)msg;
74         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
75         return rc;
76 }
77
78 /* FIXME-WANGDI: did not think about EA situation. */
79 static int mds_rec_setattr_pack(char *buffer, struct dentry *dentry,
80                                 struct inode *dir, void *data1, void *data2)
81 {
82         struct iattr *iattr = (struct iattr *)data1;
83         struct mds_rec_setattr *rec = NULL;
84         struct mds_kml_pack_info *mkpi;
85         struct lustre_msg *msg = NULL;
86         struct mdc_op_data *op_data;
87         int rc = 0, ealen = 0;
88         char *ea = NULL;
89         void *tmp = NULL;
90
91         OBD_ALLOC(op_data, sizeof(*op_data));
92         if (op_data == NULL)
93                 return -ENOMEM;
94         mdc_prepare_mdc_data(op_data, dir, NULL, NULL, 0, 0);
95
96         PACK_KML_REC_INIT(buffer, MDS_REINT);
97         mkpi = (struct mds_kml_pack_info*)buffer;
98
99         mkpi->mpi_bufcount = 1;
100         mkpi->mpi_size[0] = sizeof(struct mds_rec_setattr);
101         if (data2) {
102                 mkpi->mpi_bufcount++;
103                 mkpi->mpi_size[1] = *(int *)data2;
104                 ealen = *(int *)data2;
105                 ea = data2 + sizeof(ealen);
106         }
107
108         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
109         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
110
111         tmp = mdc_setattr_pack(msg, 0, op_data, iattr, ea, ealen, NULL, 0);
112         OBD_FREE(op_data, sizeof(*op_data));
113
114         /* FIXME-WANGDI: there are maybe some better ways to set the time
115          * attr. */
116         rec = (struct mds_rec_setattr *)lustre_msg_buf(msg, 0, 0);
117         if (rec->sa_valid & ATTR_CTIME)
118                 rec->sa_valid |= ATTR_CTIME_SET;
119         if (rec->sa_valid & ATTR_MTIME)
120                 rec->sa_valid |= ATTR_MTIME_SET;
121         if (rec->sa_valid & ATTR_ATIME)
122                 rec->sa_valid |= ATTR_ATIME_SET;
123
124         mkpi->mpi_total_size = tmp - (void *)msg;
125         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
126
127         return rc;
128 }
129
130 static int mds_rec_create_pack(char *buffer, struct dentry *dentry,
131                                struct inode *dir, void *data1,
132                                void *data2)
133 {
134         struct mds_kml_pack_info *mkpi;
135         struct lustre_msg *msg = NULL;
136         struct mdc_op_data *op_data;
137         struct mds_rec_create *rec;
138         struct dentry_params *param = 
139                 (struct dentry_params *) dentry->d_fsdata;
140         int rc = 0, tgt_len = 0;
141         void *tmp = NULL;
142
143         ENTRY;
144         
145         OBD_ALLOC(op_data, sizeof(*op_data));
146         if (op_data == NULL)
147                 return -ENOMEM;
148         
149         mdc_prepare_mdc_data(op_data, dir, dentry->d_inode,
150                              (char *)dentry->d_name.name,
151                              dentry->d_name.len, 0);
152
153         id_fid(&op_data->id1) = param->p_fid;
154         id_group(&op_data->id1) = param->p_group;
155         PACK_KML_REC_INIT(buffer, MDS_REINT);
156         mkpi = (struct mds_kml_pack_info *)buffer;
157
158         mkpi->mpi_bufcount = 2;
159         mkpi->mpi_size[0] = sizeof(struct mds_rec_create);
160         mkpi->mpi_size[1] = op_data->namelen + 1;
161
162         if (data1 && data2) {
163                 mkpi->mpi_size[2] = *(int *)data2;
164                 mkpi->mpi_bufcount++;
165         }
166
167         if (data1) {
168                 /* for symlink, data1 will be the tgt name. */
169                 tgt_len = *(int *)data2;
170         }
171         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
172         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
173
174         tmp = mdc_create_pack(msg, 0, op_data, dentry->d_inode->i_mode,
175                               dentry->d_inode->i_mode, data1, tgt_len);
176
177         rec = (struct mds_rec_create *)lustre_msg_buf(msg, 0, 0);
178         rec->cr_replayid = op_data->id2;
179         rec->cr_flags |= REC_REINT_CREATE; 
180         mkpi->mpi_total_size = tmp - (void *)msg;
181         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
182         OBD_FREE(op_data, sizeof(*op_data));
183         
184         return rc;
185 }
186
187 static int mds_rec_unlink_pack(char *buffer, struct dentry *dentry,
188                                struct inode *dir, void *data1,
189                                void *data2)
190 {
191         struct lustre_msg *msg = NULL;
192         struct mds_kml_pack_info *mkpi;
193         struct mdc_op_data *op_data;
194         int mode = *(int*)data1;
195         void *tmp = NULL;
196         int rc = 0;
197
198         OBD_ALLOC(op_data, sizeof(*op_data));
199         if (op_data == NULL)
200                 return -ENOMEM;
201         
202         mdc_prepare_mdc_data(op_data, dir, NULL,
203                              (char *)dentry->d_name.name,
204                              dentry->d_name.len, mode);
205
206         PACK_KML_REC_INIT(buffer, MDS_REINT);
207         mkpi = (struct mds_kml_pack_info*)buffer;
208
209         mkpi->mpi_bufcount = 2;
210         mkpi->mpi_size[0] = sizeof(struct mds_rec_unlink);
211         mkpi->mpi_size[1] = op_data->namelen + 1;
212
213         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
214         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
215
216         tmp = mdc_unlink_pack(msg, 0, op_data);
217
218         mkpi->mpi_total_size = tmp - (void*)msg;
219         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
220         OBD_FREE(op_data, sizeof(*op_data));
221
222         return rc;
223 }
224
225 static int mds_rec_rename_pack(char *buffer, struct dentry *dentry,
226                                struct inode *dir, void *data1, void *data2)
227 {
228         struct dentry *new_dentry = (struct dentry *)data2;
229         struct inode *new_dir = (struct inode *)data1;
230         struct mds_kml_pack_info *mkpi;
231         struct lustre_msg *msg = NULL;
232         struct mdc_op_data *op_data;
233         struct mds_rec_rename *rec;
234         void *tmp = NULL;
235         int rc = 0;
236
237         OBD_ALLOC(op_data, sizeof(*op_data));
238         if (op_data == NULL)
239                 return -ENOMEM;
240         mdc_prepare_mdc_data(op_data, dir, new_dir, NULL, 0, 0);
241
242         PACK_KML_REC_INIT(buffer, MDS_REINT);
243         mkpi = (struct mds_kml_pack_info*)buffer;
244
245         mkpi->mpi_bufcount = 3;
246         mkpi->mpi_size[0] = sizeof(struct mds_rec_rename);
247         mkpi->mpi_size[1] = dentry->d_name.len + 1;
248         mkpi->mpi_size[2] = new_dentry->d_name.len + 1;
249
250         rec = (struct mds_rec_rename *)(buffer + sizeof(*mkpi));
251
252
253         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
254         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
255
256         tmp = mdc_rename_pack(msg, 0, op_data, (char *)dentry->d_name.name,
257                               dentry->d_name.len, (char *)new_dentry->d_name.name,
258                               new_dentry->d_name.len);
259
260         mkpi->mpi_total_size = tmp - (void*)msg;
261         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
262         OBD_FREE(op_data, sizeof(*op_data));
263         return rc;
264 }
265
266 typedef int (*mds_pack_rec_func)(char *, struct dentry*, struct inode *, void *, void*);
267
268 static mds_pack_rec_func mds_kml_pack[REINT_MAX + 1] = {
269         [REINT_LINK]    mds_rec_link_pack,
270         [REINT_SETATTR] mds_rec_setattr_pack,
271         [REINT_CREATE]  mds_rec_create_pack,
272         [REINT_UNLINK]  mds_rec_unlink_pack,
273         [REINT_RENAME]  mds_rec_rename_pack,
274 };
275
276 int mds_rec_pack(int op, char *buffer, struct dentry *dentry, 
277                  struct inode *dir, void * arg, void * arg2)
278 {
279         return mds_kml_pack[op](buffer, dentry, dir, arg, arg2);
280 }
281
282
283