Whamcloud - gitweb
e860ad93e888e6bd1e9e4c6c966b1022c8278dcd
[fs/lustre-release.git] / lustre / mdt / mdt_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/mdt/mdt_lib.c
5  *  Lustre Metadata Target (mdt) request unpacking helper.
6  *
7  *  Copyright (c) 2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *   Author: Nikita Danilov <nikita@clusterfs.com>
13  *   Author: Huang Hua <huanghua@clusterfs.com>
14  *
15  *
16  *   This file is part of the Lustre file system, http://www.lustre.org
17  *   Lustre is a trademark of Cluster File Systems, Inc.
18  *
19  *   You may have signed or agreed to another license before downloading
20  *   this software.  If so, you are bound by the terms and conditions
21  *   of that agreement, and the following does not apply to you.  See the
22  *   LICENSE file included with this distribution for more information.
23  *
24  *   If you did not agree to a different license, then this copy of Lustre
25  *   is open source software; you can redistribute it and/or modify it
26  *   under the terms of version 2 of the GNU General Public License as
27  *   published by the Free Software Foundation.
28  *
29  *   In either case, Lustre is distributed in the hope that it will be
30  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
31  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  *   license text for more details.
33  */
34
35
36 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include "mdt_internal.h"
42
43
44 /* copied from lov/lov_ea.c, just for debugging, will be removed later */
45 void mdt_dump_lmm(int level, struct lov_mds_md *lmm)
46 {
47         struct lov_ost_data_v1 *lod;
48         int i;
49         __s16 stripe_count =
50                 le16_to_cpu(((struct lov_user_md*)lmm)->lmm_stripe_count);
51
52         CDEBUG_EX(level, "objid "LPX64", magic 0x%08X, pattern %#X\n",
53                le64_to_cpu(lmm->lmm_object_id), le32_to_cpu(lmm->lmm_magic),
54                le32_to_cpu(lmm->lmm_pattern));
55         CDEBUG_EX(level,"stripe_size=0x%x, stripe_count=0x%x\n",
56                le32_to_cpu(lmm->lmm_stripe_size),
57                le32_to_cpu(lmm->lmm_stripe_count));
58         LASSERT(stripe_count < (__s16)LOV_MAX_STRIPE_COUNT);
59         for (i = 0, lod = lmm->lmm_objects; i < stripe_count; i++, lod++) {
60                 CDEBUG_EX(level, "stripe %u idx %u subobj "LPX64"/"LPX64"\n",
61                        i, le32_to_cpu(lod->l_ost_idx),
62                        le64_to_cpu(lod->l_object_gr),
63                        le64_to_cpu(lod->l_object_id));
64         }
65 }
66
67 void mdt_shrink_reply(struct mdt_thread_info *info, int offset)
68 {
69         struct ptlrpc_request *req = mdt_info_req(info);
70         struct mdt_body *body;
71         int acl_size = 0;
72         int md_size = 0;
73
74         body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
75         LASSERT(body != NULL);
76
77         md_size = body->eadatasize;
78         acl_size = body->aclsize;
79
80         CDEBUG(D_INFO, "Shrink to md_size %d cookie_size %d \n",
81                        md_size, acl_size);
82
83         lustre_shrink_reply(req, offset, md_size, 1);
84         lustre_shrink_reply(req, md_size? offset + 1: offset, acl_size, 0);
85 }
86
87
88 /* if object is dying, pack the lov/llog data,
89  * parameter info->mti_attr should be valid at this point! */
90 int mdt_handle_last_unlink(struct mdt_thread_info *info, struct mdt_object *mo,
91                            const struct md_attr *ma)
92 {
93         struct mdt_body       *repbody;
94         const struct lu_attr *la = &ma->ma_attr;
95         ENTRY;
96
97         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
98
99         if (ma->ma_valid & MA_INODE)
100                 mdt_pack_attr2body(repbody, la, mdt_object_fid(mo));
101
102         if (ma->ma_valid & MA_LOV) {
103                 __u32 mode;
104
105                 mode = lu_object_attr(info->mti_ctxt, &mo->mot_obj.mo_lu);
106                 LASSERT(ma->ma_lmm_size);
107                 mdt_dump_lmm(D_INFO, ma->ma_lmm);
108                 repbody->eadatasize = ma->ma_lmm_size;
109                 if (S_ISREG(mode))
110                         repbody->valid |= OBD_MD_FLEASIZE;
111                 else if (S_ISDIR(mode))
112                         repbody->valid |= OBD_MD_FLDIREA;
113                 else
114                         LBUG();
115         }
116
117         if (ma->ma_cookie_size && (ma->ma_valid & MA_COOKIE))
118                 repbody->valid |= OBD_MD_FLCOOKIE;
119
120         RETURN(0);
121 }
122
123 static __u64 mdt_attr_valid_xlate(__u64 in, struct mdt_reint_record *rr,
124                                   struct md_attr *ma, __u32 attr_flags)
125 {
126         __u64 out;
127
128         out = 0;
129         if (in & ATTR_MODE)
130                 out |= LA_MODE;
131         if (in & ATTR_UID)
132                 out |= LA_UID;
133         if (in & ATTR_GID)
134                 out |= LA_GID;
135         if (in & ATTR_SIZE)
136                 out |= LA_SIZE;
137
138         if (in & ATTR_FROM_OPEN)
139                 rr->rr_flags |= MRF_SETATTR_LOCKED;
140
141         if (in & ATTR_ATIME_SET)
142                 out |= LA_ATIME;
143
144         if (in & ATTR_CTIME_SET)
145                 out |= LA_CTIME;
146
147         if (in & ATTR_MTIME_SET)
148                 out |= LA_MTIME;
149
150         if (in & ATTR_ATTR_FLAG) {
151                 ma->ma_valid |= MA_FLAGS;
152                 ma->ma_attr_flags = attr_flags;
153         }
154         /*XXX need ATTR_RAW?*/
155         in &= ~(ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|
156                 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME|ATTR_FROM_OPEN|
157                 ATTR_ATIME_SET|ATTR_CTIME_SET|ATTR_MTIME_SET|
158                 ATTR_ATTR_FLAG|ATTR_RAW);
159         if (in != 0)
160                 CERROR("Unknown attr bits: %#llx\n", in);
161         return out;
162 }
163 /* unpacking */
164 static int mdt_setattr_unpack(struct mdt_thread_info *info)
165 {
166         struct mdt_rec_setattr  *rec;
167         struct md_attr          *ma = &info->mti_attr;
168         struct lu_attr          *la = &ma->ma_attr;
169         struct mdt_reint_record *rr = &info->mti_rr;
170         struct req_capsule      *pill = &info->mti_pill;
171         ENTRY;
172
173         rec = req_capsule_client_get(pill, &RMF_REC_SETATTR);
174
175         if (rec == NULL)
176                 RETURN(-EFAULT);
177
178         rr->rr_fid1 = &rec->sa_fid;
179         la->la_valid = mdt_attr_valid_xlate(rec->sa_valid, rr, ma,
180                                             rec->sa_attr_flags);
181         la->la_mode  = rec->sa_mode;
182         la->la_uid   = rec->sa_uid;
183         la->la_gid   = rec->sa_gid;
184         la->la_size  = rec->sa_size;
185         la->la_ctime = rec->sa_ctime;
186         la->la_atime = rec->sa_atime;
187         la->la_mtime = rec->sa_mtime;
188         ma->ma_valid = MA_INODE;
189
190         if (req_capsule_field_present(pill, &RMF_EADATA)) {
191                 ma->ma_lmm = req_capsule_client_get(pill, &RMF_EADATA);
192                 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_EADATA,
193                                                        RCL_CLIENT);
194                 ma->ma_valid |= MA_LOV;
195         }
196         if (req_capsule_field_present(pill, &RMF_LOGCOOKIES)) {
197                 ma->ma_cookie = req_capsule_client_get(pill,
198                                                        &RMF_LOGCOOKIES);
199                 ma->ma_cookie_size = req_capsule_get_size(pill,
200                                                           &RMF_LOGCOOKIES,
201                                                           RCL_CLIENT);
202                 ma->ma_valid |= MA_COOKIE;
203         }
204
205         RETURN(0);
206 }
207
208 static int mdt_create_unpack(struct mdt_thread_info *info)
209 {
210         struct mdt_rec_create   *rec;
211         struct lu_attr          *attr = &info->mti_attr.ma_attr;
212         struct mdt_reint_record *rr = &info->mti_rr;
213         struct req_capsule      *pill = &info->mti_pill;
214         int                     result = 0;
215         ENTRY;
216
217         rec = req_capsule_client_get(pill, &RMF_REC_CREATE);
218         if (rec != NULL) {
219                 rr->rr_fid1 = &rec->cr_fid1;
220                 rr->rr_fid2 = &rec->cr_fid2;
221                 attr->la_mode = rec->cr_mode;
222                 attr->la_rdev  = rec->cr_rdev;
223                 attr->la_uid   = rec->cr_fsuid;
224                 attr->la_gid   = rec->cr_fsgid;
225                 attr->la_ctime = rec->cr_time;
226                 attr->la_mtime = rec->cr_time;
227                 attr->la_atime = rec->cr_time;
228                 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID |
229                                  LA_CTIME | LA_MTIME | LA_ATIME;
230                 info->mti_spec.sp_cr_flags = rec->cr_flags;
231
232                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
233                 if (rr->rr_name) {
234                         if (req_capsule_field_present(pill, &RMF_SYMTGT)) {
235                                 const char *tgt;
236                                 tgt = req_capsule_client_get(pill,
237                                                              &RMF_SYMTGT);
238                                 if (tgt == NULL)
239                                         result = -EFAULT;
240                                 info->mti_spec.u.sp_symname = tgt;
241                         }
242                 } else
243                         result = -EFAULT;
244         } else
245                 result = -EFAULT;
246         RETURN(result);
247 }
248
249 static int mdt_link_unpack(struct mdt_thread_info *info)
250 {
251         struct mdt_rec_link     *rec;
252         struct lu_attr          *attr = &info->mti_attr.ma_attr;
253         struct mdt_reint_record *rr = &info->mti_rr;
254         struct req_capsule      *pill = &info->mti_pill;
255         int                      result = 0;
256         ENTRY;
257
258         rec = req_capsule_client_get(pill, &RMF_REC_LINK);
259         if (rec != NULL) {
260                 attr->la_uid = rec->lk_fsuid;
261                 attr->la_gid = rec->lk_fsgid;
262                 rr->rr_fid1 = &rec->lk_fid1;
263                 rr->rr_fid2 = &rec->lk_fid2;
264                 attr->la_ctime = rec->lk_time;
265                 attr->la_mtime = rec->lk_time;
266                 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
267                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
268                 if (rr->rr_name == NULL)
269                         result = -EFAULT;
270         } else
271                 result = -EFAULT;
272         RETURN(result);
273 }
274
275 static int mdt_unlink_unpack(struct mdt_thread_info *info)
276 {
277         struct mdt_rec_unlink   *rec;
278         struct lu_attr          *attr = &info->mti_attr.ma_attr;
279         struct mdt_reint_record *rr = &info->mti_rr;
280         struct req_capsule      *pill = &info->mti_pill;
281         int                      result = 0;
282         ENTRY;
283
284         rec = req_capsule_client_get(pill, &RMF_REC_UNLINK);
285         if (rec != NULL) {
286                 attr->la_uid = rec->ul_fsuid;
287                 attr->la_gid = rec->ul_fsgid;
288                 rr->rr_fid1 = &rec->ul_fid1;
289                 rr->rr_fid2 = &rec->ul_fid2;
290                 attr->la_ctime = rec->ul_time;
291                 attr->la_mtime = rec->ul_time;
292                 attr->la_mode  = rec->ul_mode;
293
294                 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME | LA_MODE;
295                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
296                 if (rr->rr_name == NULL)
297                         result = -EFAULT;
298         } else
299                 result = -EFAULT;
300         RETURN(result);
301 }
302
303 static int mdt_rename_unpack(struct mdt_thread_info *info)
304 {
305         struct mdt_rec_rename   *rec;
306         struct lu_attr          *attr = &info->mti_attr.ma_attr;
307         struct mdt_reint_record *rr = &info->mti_rr;
308         struct req_capsule      *pill = &info->mti_pill;
309         int                      result = 0;
310         ENTRY;
311
312         rec = req_capsule_client_get(pill, &RMF_REC_RENAME);
313         if (rec != NULL) {
314                 attr->la_uid = rec->rn_fsuid;
315                 attr->la_gid = rec->rn_fsgid;
316                 rr->rr_fid1 = &rec->rn_fid1;
317                 rr->rr_fid2 = &rec->rn_fid2;
318                 attr->la_ctime = rec->rn_time;
319                 attr->la_mtime = rec->rn_time;
320                 attr->la_valid = LA_UID | LA_GID | LA_CTIME | LA_MTIME;
321                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
322                 rr->rr_tgt = req_capsule_client_get(pill, &RMF_SYMTGT);
323                 if (rr->rr_name == NULL || rr->rr_tgt == NULL)
324                         result = -EFAULT;
325         } else
326                 result = -EFAULT;
327         RETURN(result);
328 }
329
330 static int mdt_open_unpack(struct mdt_thread_info *info)
331 {
332         struct mdt_rec_create   *rec;
333         struct lu_attr          *attr = &info->mti_attr.ma_attr;
334         struct req_capsule      *pill = &info->mti_pill;
335         struct mdt_reint_record *rr   = &info->mti_rr;
336         int                     result;
337         ENTRY;
338
339         rec = req_capsule_client_get(pill, &RMF_REC_CREATE);
340         if (rec != NULL) {
341                 rr->rr_fid1   = &rec->cr_fid1;
342                 rr->rr_fid2   = &rec->cr_fid2;
343                 attr->la_mode = rec->cr_mode;
344                 attr->la_rdev  = rec->cr_rdev;
345                 attr->la_uid   = rec->cr_fsuid;
346                 attr->la_gid   = rec->cr_fsgid;
347                 attr->la_ctime = rec->cr_time;
348                 attr->la_mtime = rec->cr_time;
349                 attr->la_atime = rec->cr_time;
350                 attr->la_valid = LA_MODE | LA_RDEV | LA_UID | LA_GID | LA_CTIME
351                                  | LA_MTIME | LA_ATIME;
352                 info->mti_spec.sp_cr_flags = rec->cr_flags;
353                 rr->rr_name = req_capsule_client_get(pill, &RMF_NAME);
354                 if (rr->rr_name == NULL)
355                         /*XXX: what about open by FID? */
356                         result = -EFAULT;
357                 else
358                         result = 0;
359         } else
360                 result = -EFAULT;
361
362         if (req_capsule_field_present(pill, &RMF_EADATA)) {
363                 struct md_create_spec *sp = &info->mti_spec;
364                 sp->u.sp_ea.eadata = req_capsule_client_get(pill,
365                                                             &RMF_EADATA);
366                 sp->u.sp_ea.eadatalen = req_capsule_get_size(pill,
367                                                              &RMF_EADATA,
368                                                              RCL_CLIENT);
369         }
370
371         RETURN(result);
372 }
373
374 typedef int (*reint_unpacker)(struct mdt_thread_info *info);
375
376 static reint_unpacker mdt_reint_unpackers[REINT_MAX] = {
377         [REINT_SETATTR]  = mdt_setattr_unpack,
378         [REINT_CREATE]   = mdt_create_unpack,
379         [REINT_LINK]     = mdt_link_unpack,
380         [REINT_UNLINK]   = mdt_unlink_unpack,
381         [REINT_RENAME]   = mdt_rename_unpack,
382         [REINT_OPEN]     = mdt_open_unpack
383 };
384
385 int mdt_reint_unpack(struct mdt_thread_info *info, __u32 op)
386 {
387         int rc;
388
389         ENTRY;
390
391         if (op < REINT_MAX && mdt_reint_unpackers[op] != NULL) {
392                 info->mti_rr.rr_opcode = op;
393                 rc = mdt_reint_unpackers[op](info);
394         } else {
395                 CERROR("Unexpected opcode %d\n", op);
396                 rc = -EFAULT;
397         }
398         RETURN(rc);
399 }