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