Whamcloud - gitweb
6b57a4d641ea5049f2decc5cd86b349fb66093f9
[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/obd_class.h>
31 #include <linux/obd_support.h>
32 #include <linux/lustre_lib.h>
33 #include <linux/lustre_idl.h>
34 #include <linux/lustre_fsfilt.h>
35 #include <linux/lustre_lite.h>
36 #include <linux/lustre_mds.h>
37 #include <linux/lustre_smfs.h>
38 #include "smfs_internal.h"
39
40 static int mds_rec_link_pack(char *buffer, struct dentry *dentry,
41                              struct inode *dir, void *data1, void *data2)
42 {
43         struct dentry *src = (struct dentry *)data1;
44         struct dentry *tgt = (struct dentry *)data2;
45         struct mds_kml_pack_info *mkpi;
46         struct lustre_msg *msg = NULL;
47         struct mdc_op_data *op_data;
48         void *tmp = NULL;
49         int rc = 0;
50
51         OBD_ALLOC(op_data, sizeof(*op_data));
52         if (op_data == NULL)
53                 return -ENOMEM;
54         
55         mdc_prepare_mdc_data(op_data, src->d_inode, dir,
56                              tgt->d_name.name, tgt->d_name.len, 0);
57
58         PACK_KML_REC_INIT(buffer, MDS_REINT);
59         mkpi = (struct mds_kml_pack_info *)buffer;
60
61         mkpi->mpi_bufcount = 2;
62         mkpi->mpi_size[0] = sizeof(struct mds_rec_link);
63         mkpi->mpi_size[1] = op_data->namelen + 1;
64
65         /* the mds reint log format is: opcode + mkpi + request  */
66         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
67         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
68
69         tmp = mdc_link_pack(msg, 0, op_data);
70         OBD_FREE(op_data, sizeof(*op_data));
71         mkpi->mpi_total_size = tmp - (void *)msg;
72         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
73         return rc;
74 }
75
76 /* FIXME-WANGDI: did not think about EA situation. */
77 static int mds_rec_setattr_pack(char *buffer, struct dentry *dentry,
78                                 struct inode *dir, void *data1, void *data2)
79 {
80         struct iattr *iattr = (struct iattr *)data1;
81         struct mds_rec_setattr *rec = NULL;
82         struct mds_kml_pack_info *mkpi;
83         struct lustre_msg *msg = NULL;
84         struct mdc_op_data *op_data;
85         int rc = 0, ealen = 0;
86         char *ea = NULL;
87         void *tmp = NULL;
88
89         OBD_ALLOC(op_data, sizeof(*op_data));
90         if (op_data == NULL)
91                 return -ENOMEM;
92         mdc_prepare_mdc_data(op_data, dir, NULL, NULL, 0, 0);
93
94         PACK_KML_REC_INIT(buffer, MDS_REINT);
95         mkpi = (struct mds_kml_pack_info*)buffer;
96
97         mkpi->mpi_bufcount = 1;
98         mkpi->mpi_size[0] = sizeof(struct mds_rec_setattr);
99         if (data2) {
100                 mkpi->mpi_bufcount++;
101                 mkpi->mpi_size[1] = *(int *)data2;
102                 ealen = *(int *)data2;
103                 ea = data2 + sizeof(ealen);
104         }
105
106         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
107         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
108
109         tmp = mdc_setattr_pack(msg, 0, op_data, iattr, ea, ealen, NULL, 0);
110         OBD_FREE(op_data, sizeof(*op_data));
111
112         /* FIXME-WANGDI: there are maybe some better ways to set the time
113          * attr. */
114         rec = (struct mds_rec_setattr *)lustre_msg_buf(msg, 0, 0);
115         if (rec->sa_valid & ATTR_CTIME)
116                 rec->sa_valid |= ATTR_CTIME_SET;
117         if (rec->sa_valid & ATTR_MTIME)
118                 rec->sa_valid |= ATTR_MTIME_SET;
119         if (rec->sa_valid & ATTR_ATIME)
120                 rec->sa_valid |= ATTR_ATIME_SET;
121
122         mkpi->mpi_total_size = tmp - (void *)msg;
123         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
124
125         return rc;
126 }
127
128 static int mds_rec_create_pack(char *buffer, struct dentry *dentry,
129                                struct inode *dir, void *data1,
130                                void *data2)
131 {
132         struct mds_kml_pack_info *mkpi;
133         struct lustre_msg *msg = NULL;
134         struct mdc_op_data *op_data;
135         struct mds_rec_create *rec;
136         int rc = 0, tgt_len = 0;
137         void *tmp = NULL;
138
139         ENTRY;
140         
141         OBD_ALLOC(op_data, sizeof(*op_data));
142         if (op_data == NULL)
143                 return -ENOMEM;
144         mdc_prepare_mdc_data(op_data, dir, dentry->d_inode,
145                              dentry->d_name.name, dentry->d_name.len, 0);
146
147         PACK_KML_REC_INIT(buffer, MDS_REINT);
148         mkpi = (struct mds_kml_pack_info *)buffer;
149
150         mkpi->mpi_bufcount = 2;
151         mkpi->mpi_size[0] = sizeof(struct mds_rec_create);
152         mkpi->mpi_size[1] = op_data->namelen + 1;
153
154         if (data1 && data2) {
155                 mkpi->mpi_size[2] = *(int *)data2;
156                 mkpi->mpi_bufcount++;
157         }
158
159         if (data1) {
160                 /* for symlink, data1 will be the tgt name. */
161                 tgt_len = *(int *)data2;
162         }
163         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
164         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
165
166         tmp = mdc_create_pack(msg, 0, op_data, dentry->d_inode->i_mode,
167                               dentry->d_inode->i_mode, data1, tgt_len);
168
169         rec = (struct mds_rec_create *)lustre_msg_buf(msg, 0, 0);
170         rec->cr_replayid = op_data->id2;
171         rec->cr_flags |= REC_REINT_CREATE; 
172         mkpi->mpi_total_size = tmp - (void *)msg;
173         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
174         OBD_FREE(op_data, sizeof(*op_data));
175         
176         return rc;
177 }
178
179 static int mds_rec_unlink_pack(char *buffer, struct dentry *dentry,
180                                struct inode *dir, void *data1,
181                                void *data2)
182 {
183         struct lustre_msg *msg = NULL;
184         struct mds_kml_pack_info *mkpi;
185         struct mdc_op_data *op_data;
186         int mode = *(int*)data1;
187         void *tmp = NULL;
188         int rc = 0;
189
190         OBD_ALLOC(op_data, sizeof(*op_data));
191         if (op_data == NULL)
192                 return -ENOMEM;
193         mdc_prepare_mdc_data(op_data, dir, NULL,
194                              dentry->d_name.name,
195                              dentry->d_name.len, mode);
196
197         PACK_KML_REC_INIT(buffer, MDS_REINT);
198         mkpi = (struct mds_kml_pack_info*)buffer;
199
200         mkpi->mpi_bufcount = 2;
201         mkpi->mpi_size[0] = sizeof(struct mds_rec_unlink);
202         mkpi->mpi_size[1] = op_data->namelen + 1;
203
204         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
205         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
206
207         tmp = mdc_unlink_pack(msg, 0, op_data);
208
209         mkpi->mpi_total_size = tmp - (void*)msg;
210         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
211         OBD_FREE(op_data, sizeof(*op_data));
212
213         return rc;
214 }
215
216 static int mds_rec_rename_pack(char *buffer, struct dentry *dentry,
217                                struct inode *dir, void *data1, void *data2)
218 {
219         struct dentry *new_dentry = (struct dentry *)data2;
220         struct inode *new_dir = (struct inode *)data1;
221         struct mds_kml_pack_info *mkpi;
222         struct lustre_msg *msg = NULL;
223         struct mdc_op_data *op_data;
224         struct mds_rec_rename *rec;
225         void *tmp = NULL;
226         int rc = 0;
227
228         OBD_ALLOC(op_data, sizeof(*op_data));
229         if (op_data == NULL)
230                 return -ENOMEM;
231         mdc_prepare_mdc_data(op_data, dir, new_dir, NULL, 0, 0);
232
233         PACK_KML_REC_INIT(buffer, MDS_REINT);
234         mkpi = (struct mds_kml_pack_info*)buffer;
235
236         mkpi->mpi_bufcount = 3;
237         mkpi->mpi_size[0] = sizeof(struct mds_rec_rename);
238         mkpi->mpi_size[1] = dentry->d_name.len + 1;
239         mkpi->mpi_size[2] = new_dentry->d_name.len + 1;
240
241         rec = (struct mds_rec_rename *)(buffer + sizeof(*mkpi));
242
243
244         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
245         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
246
247         tmp = mdc_rename_pack(msg, 0, op_data, dentry->d_name.name,
248                               dentry->d_name.len, new_dentry->d_name.name,
249                               new_dentry->d_name.len);
250
251         mkpi->mpi_total_size = tmp - (void*)msg;
252         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
253         OBD_FREE(op_data, sizeof(*op_data));
254         return rc;
255 }
256
257 typedef int (*mds_pack_rec_func)(char *, struct dentry*, struct inode *, void *, void*);
258
259 static mds_pack_rec_func mds_kml_pack[REINT_MAX + 1] = {
260         [REINT_LINK]    mds_rec_link_pack,
261         [REINT_SETATTR] mds_rec_setattr_pack,
262         [REINT_CREATE]  mds_rec_create_pack,
263         [REINT_UNLINK]  mds_rec_unlink_pack,
264         [REINT_RENAME]  mds_rec_rename_pack,
265 };
266
267 int mds_rec_pack(int op, char *buffer, struct dentry *dentry, 
268                  struct inode *dir, void * arg, void * arg2)
269 {
270         return mds_kml_pack[op](buffer, dentry, dir, arg, arg2);
271 }
272
273