1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre filesystem abstraction routines
6 * Copyright (C) 2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
24 #define DEBUG_SUBSYSTEM S_SM
26 #include <linux/kmod.h>
27 #include <linux/init.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"
40 static int mds_rec_link_pack(char *buffer, struct dentry *dentry,
41 struct inode *dir, void *data1, void *data2)
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;
51 OBD_ALLOC(op_data, sizeof(*op_data));
55 mdc_prepare_mdc_data(op_data, src->d_inode, dir,
56 tgt->d_name.name, tgt->d_name.len, 0);
58 PACK_KML_REC_INIT(buffer, MDS_REINT);
59 mkpi = (struct mds_kml_pack_info *)buffer;
61 mkpi->mpi_bufcount = 2;
62 mkpi->mpi_size[0] = sizeof(struct mds_rec_link);
63 mkpi->mpi_size[1] = op_data->namelen + 1;
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);
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);
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)
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;
89 OBD_ALLOC(op_data, sizeof(*op_data));
92 mdc_prepare_mdc_data(op_data, dir, NULL, NULL, 0, 0);
94 PACK_KML_REC_INIT(buffer, MDS_REINT);
95 mkpi = (struct mds_kml_pack_info*)buffer;
97 mkpi->mpi_bufcount = 1;
98 mkpi->mpi_size[0] = sizeof(struct mds_rec_setattr);
100 mkpi->mpi_bufcount++;
101 mkpi->mpi_size[1] = *(int *)data2;
102 ealen = *(int *)data2;
103 ea = data2 + sizeof(ealen);
106 msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
107 lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
109 tmp = mdc_setattr_pack(msg, 0, op_data, iattr, ea, ealen, NULL, 0);
110 OBD_FREE(op_data, sizeof(*op_data));
112 /* FIXME-WANGDI: there are maybe some better ways to set the time
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;
122 mkpi->mpi_total_size = tmp - (void *)msg;
123 rc = mkpi->mpi_total_size + sizeof(*mkpi) + sizeof(int);
128 static int mds_rec_create_pack(char *buffer, struct dentry *dentry,
129 struct inode *dir, void *data1,
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;
141 OBD_ALLOC(op_data, sizeof(*op_data));
144 mdc_prepare_mdc_data(op_data, dir, dentry->d_inode,
145 dentry->d_name.name, dentry->d_name.len, 0);
147 PACK_KML_REC_INIT(buffer, MDS_REINT);
148 mkpi = (struct mds_kml_pack_info *)buffer;
150 mkpi->mpi_bufcount = 2;
151 mkpi->mpi_size[0] = sizeof(struct mds_rec_create);
152 mkpi->mpi_size[1] = op_data->namelen + 1;
154 if (data1 && data2) {
155 mkpi->mpi_size[2] = *(int *)data2;
156 mkpi->mpi_bufcount++;
160 /* for symlink, data1 will be the tgt name. */
161 tgt_len = *(int *)data2;
163 msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
164 lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
166 tmp = mdc_create_pack(msg, 0, op_data, dentry->d_inode->i_mode,
167 dentry->d_inode->i_mode, data1, tgt_len);
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));
179 static int mds_rec_unlink_pack(char *buffer, struct dentry *dentry,
180 struct inode *dir, void *data1,
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;
190 OBD_ALLOC(op_data, sizeof(*op_data));
193 mdc_prepare_mdc_data(op_data, dir, NULL,
195 dentry->d_name.len, mode);
197 PACK_KML_REC_INIT(buffer, MDS_REINT);
198 mkpi = (struct mds_kml_pack_info*)buffer;
200 mkpi->mpi_bufcount = 2;
201 mkpi->mpi_size[0] = sizeof(struct mds_rec_unlink);
202 mkpi->mpi_size[1] = op_data->namelen + 1;
204 msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
205 lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
207 tmp = mdc_unlink_pack(msg, 0, op_data);
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));
216 static int mds_rec_rename_pack(char *buffer, struct dentry *dentry,
217 struct inode *dir, void *data1, void *data2)
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;
228 OBD_ALLOC(op_data, sizeof(*op_data));
231 mdc_prepare_mdc_data(op_data, dir, new_dir, NULL, 0, 0);
233 PACK_KML_REC_INIT(buffer, MDS_REINT);
234 mkpi = (struct mds_kml_pack_info*)buffer;
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;
241 rec = (struct mds_rec_rename *)(buffer + sizeof(*mkpi));
244 msg = (struct lustre_msg *)(buffer + sizeof(*mkpi));
245 lustre_init_msg(msg, mkpi->mpi_bufcount, mkpi->mpi_size, NULL);
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);
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));
257 typedef int (*mds_pack_rec_func)(char *, struct dentry*, struct inode *, void *, void*);
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,
267 int mds_rec_pack(int op, char *buffer, struct dentry *dentry,
268 struct inode *dir, void * arg, void * arg2)
270 return mds_kml_pack[op](buffer, dentry, dir, arg, arg2);