Whamcloud - gitweb
added dependency listing of IOR for ior-survey
[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         OBD_ALLOC(op_data, sizeof(*op_data));
140         if (op_data == NULL)
141                 return -ENOMEM;
142         mdc_prepare_mdc_data(op_data, dir, dentry->d_inode,
143                              dentry->d_name.name, dentry->d_name.len, 0);
144
145         PACK_KML_REC_INIT(buffer, MDS_REINT);
146         mkpi = (struct mds_kml_pack_info *)buffer;
147
148         mkpi->mpi_bufcount = 2;
149         mkpi->mpi_size[0] = sizeof(struct mds_rec_create);
150         mkpi->mpi_size[1] = op_data->namelen + 1;
151
152         if (data1 && data2) {
153                 mkpi->mpi_size[2] = *(int *)data2;
154                 mkpi->mpi_bufcount++;
155         }
156
157         if (data1) {
158                 /* for symlink, data1 will be the tgt name. */
159                 tgt_len = *(int *)data2;
160         }
161         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
162         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
163
164         tmp = mdc_create_pack(msg, 0, op_data, dentry->d_inode->i_mode,
165                               dentry->d_inode->i_mode, data1, tgt_len);
166
167         rec = (struct mds_rec_create *)lustre_msg_buf(msg, 0, 0);
168         rec->cr_replayid = op_data->id2;
169         rec->cr_flags |= REC_REINT_CREATE; 
170         mkpi->mpi_total_size = tmp - (void *)msg;
171         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
172         OBD_FREE(op_data, sizeof(*op_data));
173         
174         return rc;
175 }
176
177 static int mds_rec_unlink_pack(char *buffer, struct dentry *dentry,
178                                struct inode *dir, void *data1,
179                                void *data2)
180 {
181         struct lustre_msg *msg = NULL;
182         struct mds_kml_pack_info *mkpi;
183         struct mdc_op_data *op_data;
184         int mode = *(int*)data1;
185         void *tmp = NULL;
186         int rc = 0;
187
188         OBD_ALLOC(op_data, sizeof(*op_data));
189         if (op_data == NULL)
190                 return -ENOMEM;
191         mdc_prepare_mdc_data(op_data, dir, NULL,
192                              dentry->d_name.name,
193                              dentry->d_name.len, mode);
194
195         PACK_KML_REC_INIT(buffer, MDS_REINT);
196         mkpi = (struct mds_kml_pack_info*)buffer;
197
198         mkpi->mpi_bufcount = 2;
199         mkpi->mpi_size[0] = sizeof(struct mds_rec_unlink);
200         mkpi->mpi_size[1] = op_data->namelen + 1;
201
202         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
203         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
204
205         tmp = mdc_unlink_pack(msg, 0, op_data);
206
207         mkpi->mpi_total_size = tmp - (void*)msg;
208         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
209         OBD_FREE(op_data, sizeof(*op_data));
210
211         return rc;
212 }
213
214 static int mds_rec_rename_pack(char *buffer, struct dentry *dentry,
215                                struct inode *dir, void *data1, void *data2)
216 {
217         struct dentry *new_dentry = (struct dentry *)data2;
218         struct inode *new_dir = (struct inode *)data1;
219         struct mds_kml_pack_info *mkpi;
220         struct lustre_msg *msg = NULL;
221         struct mdc_op_data *op_data;
222         struct mds_rec_rename *rec;
223         void *tmp = NULL;
224         int rc = 0;
225
226         OBD_ALLOC(op_data, sizeof(*op_data));
227         if (op_data == NULL)
228                 return -ENOMEM;
229         mdc_prepare_mdc_data(op_data, dir, new_dir, NULL, 0, 0);
230
231         PACK_KML_REC_INIT(buffer, MDS_REINT);
232         mkpi = (struct mds_kml_pack_info*)buffer;
233
234         mkpi->mpi_bufcount = 3;
235         mkpi->mpi_size[0] = sizeof(struct mds_rec_rename);
236         mkpi->mpi_size[1] = dentry->d_name.len + 1;
237         mkpi->mpi_size[2] = new_dentry->d_name.len + 1;
238
239         rec = (struct mds_rec_rename *)(buffer + sizeof(*mkpi));
240
241
242         msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
243         lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
244
245         tmp = mdc_rename_pack(msg, 0, op_data, dentry->d_name.name,
246                               dentry->d_name.len, new_dentry->d_name.name,
247                               new_dentry->d_name.len);
248
249         mkpi->mpi_total_size = tmp - (void*)msg;
250         rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
251         OBD_FREE(op_data, sizeof(*op_data));
252         return rc;
253 }
254
255 typedef int (*mds_pack_rec_func)(char *buffer, struct dentry *dentry,
256                                  struct inode *dir, void *data1, void *data2);
257
258 static mds_pack_rec_func mds_kml_pack[REINT_MAX + 1] = {
259         [REINT_LINK]    mds_rec_link_pack,
260         [REINT_SETATTR] mds_rec_setattr_pack,
261         [REINT_CREATE]  mds_rec_create_pack,
262         [REINT_UNLINK]  mds_rec_unlink_pack,
263         [REINT_RENAME]  mds_rec_rename_pack,
264 };
265
266 int mds_rec_pack(char *buffer, struct dentry *dentry, struct inode *dir,
267                  void *data1, void *data2, int op)
268 {
269         return mds_kml_pack[op](buffer, dentry, dir, data1, data2);
270 }
271
272 int mds_rec_pack_init(struct smfs_super_info *smsi)
273 {
274         
275         smsi->smsi_pack_rec[PACK_MDS] = mds_rec_pack;
276         return 0;
277 }
278