Whamcloud - gitweb
land b_hd_remote_acl: support get/set ACL from remote client.
[fs/lustre-release.git] / lustre / mdc / mdc_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/config.h>
29 # include <linux/module.h>
30 # include <linux/kernel.h>
31 #else
32 # include <liblustre.h>
33 #endif
34
35 #include <linux/obd_class.h>
36 #include <linux/lustre_mds.h>
37 #include <linux/lustre_acl.h>
38 #include "mdc_internal.h"
39
40 /* this function actually sends request to desired target. */
41 static int mdc_reint(struct ptlrpc_request *request,
42                      struct mdc_rpc_lock *rpc_lock,
43                      int level)
44 {
45         int rc;
46
47         request->rq_send_state = level;
48        
49         if (rpc_lock)
50                 mdc_get_rpc_lock(rpc_lock, NULL);
51        
52         rc = ptlrpc_queue_wait(request);
53         
54         if (rpc_lock)
55                 mdc_put_rpc_lock(rpc_lock, NULL);
56         if (rc)
57                 CDEBUG(D_INFO, "error in handling %d\n", rc);
58         else if (!lustre_swab_repbuf(request, 0, sizeof(struct mds_body),
59                                      lustre_swab_mds_body)) {
60                 CERROR ("Can't unpack mds_body\n");
61                 rc = -EPROTO;
62         }
63         return rc;
64 }
65
66 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that should
67  * take the normal semaphore and go to the normal portal.
68  *
69  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a magic
70  * open-path setattr that should take the setattr semaphore and go to the
71  * setattr portal. */
72 int mdc_setattr(struct obd_export *exp, struct mdc_op_data *data,
73                 struct iattr *iattr, void *ea, int ealen, void *ea2,
74                 int ea2len, struct ptlrpc_request **request)
75 {
76         struct ptlrpc_request *req;
77         struct mds_rec_setattr *rec;
78         struct mdc_rpc_lock *rpc_lock;
79         struct obd_device *obd = exp->exp_obd;
80         int rc, bufcount = 2, size[4] = {0, sizeof(*rec), ealen, ea2len};
81         ENTRY;
82
83         LASSERT(iattr != NULL);
84
85         size[0] = lustre_secdesc_size();
86         if (ealen > 0) {
87                 bufcount++;
88                 if (ea2len > 0)
89                         bufcount++;
90         }
91
92         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
93                               MDS_REINT, bufcount, size, NULL);
94         if (req == NULL)
95                 RETURN(-ENOMEM);
96
97         lustre_pack_secdesc(req, size[0]);
98
99         if (iattr->ia_valid & ATTR_FROM_OPEN) {
100                 req->rq_request_portal = MDS_SETATTR_PORTAL; //XXX FIXME bug 249
101                 rpc_lock = obd->u.cli.cl_setattr_lock;
102         } else {
103                 rpc_lock = obd->u.cli.cl_rpc_lock;
104         }
105
106         if (iattr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
107                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
108                        LTIME_S(iattr->ia_mtime), LTIME_S(iattr->ia_ctime));
109         mdc_setattr_pack(req->rq_reqmsg, 1, data, iattr, ea, ealen,
110                          ea2, ea2len);
111
112         /* prepare the reply buffer
113          */
114         bufcount = 1;
115         size[0] = sizeof(struct mds_body);
116
117         /* This is a hack for setfacl remotely. XXX */
118         if (ealen == sizeof(XATTR_NAME_LUSTRE_ACL) &&
119             !strncmp((char *) ea, XATTR_NAME_LUSTRE_ACL, ealen)) {
120                 size[bufcount++] = LUSTRE_ACL_SIZE_MAX;
121         }
122
123         req->rq_replen = lustre_msg_size(bufcount, size);
124
125         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
126         *request = req;
127         if (rc == -ERESTARTSYS)
128                 rc = 0;
129
130         RETURN(rc);
131 }
132
133 int mdc_create(struct obd_export *exp, struct mdc_op_data *op_data,
134                const void *data, int datalen, int mode, __u32 uid,
135                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
136 {
137         struct obd_device *obd = exp->exp_obd;
138         struct ptlrpc_request *req;
139         int rc, size[4] = {0, sizeof(struct mds_rec_create),
140                            op_data->namelen + 1};
141         int level, bufcount = 3;
142         ENTRY;
143
144         size[0] = lustre_secdesc_size();
145         if (data && datalen) {
146                 size[bufcount] = datalen;
147                 bufcount++;
148         }
149
150         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
151                               MDS_REINT, bufcount, size, NULL);
152         if (req == NULL)
153                 RETURN(-ENOMEM);
154
155         lustre_pack_secdesc(req, size[0]);
156
157         /*
158          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
159          * tgt, for symlinks or lov MD data.
160          */
161         mdc_create_pack(req->rq_reqmsg, 1, op_data, mode, rdev, data, datalen);
162
163         size[0] = sizeof(struct mds_body);
164         req->rq_replen = lustre_msg_size(1, size);
165
166         level = LUSTRE_IMP_FULL;
167  resend:
168         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, level);
169
170         /* resend if we were told to. */
171         if (rc == -ERESTARTSYS) {
172                 level = LUSTRE_IMP_RECOVER;
173                 goto resend;
174         }
175
176         if (!rc)
177                 mdc_store_inode_generation(exp, req, MDS_REQ_REC_OFF, 0);
178
179         *request = req;
180         RETURN(rc);
181 }
182
183 int mdc_unlink(struct obd_export *exp, struct mdc_op_data *data,
184                struct ptlrpc_request **request)
185 {
186         struct obd_device *obd = class_exp2obd(exp);
187         struct ptlrpc_request *req = *request;
188         int rc, size[4] = {0, sizeof(struct mds_rec_unlink),
189                            data->namelen + 1,
190                            obd->u.cli.cl_max_mds_cookiesize};
191         ENTRY;
192         LASSERT(req == NULL);
193
194         size[0] = lustre_secdesc_size();
195
196         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
197                               MDS_REINT, 4, size, NULL);
198         if (req == NULL)
199                 RETURN(-ENOMEM);
200
201         lustre_pack_secdesc(req, size[0]);
202         *request = req;
203
204         size[0] = sizeof(struct mds_body);
205         size[1] = obd->u.cli.cl_max_mds_easize;
206         size[2] = obd->u.cli.cl_max_mds_cookiesize;
207
208         req->rq_replen = lustre_msg_size(3, size);
209
210         mdc_unlink_pack(req->rq_reqmsg, 1, data);
211
212         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
213         if (rc == -ERESTARTSYS)
214                 rc = 0;
215         RETURN(rc);
216 }
217
218 int mdc_link(struct obd_export *exp, struct mdc_op_data *data,
219              struct ptlrpc_request **request)
220 {
221         struct obd_device *obd = exp->exp_obd;
222         struct ptlrpc_request *req;
223         int rc, size[3] = {0, sizeof(struct mds_rec_link), data->namelen + 1};
224         ENTRY;
225
226         size[0] = lustre_secdesc_size();
227
228         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
229                               MDS_REINT, 3, size, NULL);
230         if (req == NULL)
231                 RETURN(-ENOMEM);
232
233         lustre_pack_secdesc(req, size[0]);
234
235         mdc_link_pack(req->rq_reqmsg, 1, data);
236
237         size[0] = sizeof(struct mds_body);
238         req->rq_replen = lustre_msg_size(1, size);
239
240         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
241         *request = req;
242         if (rc == -ERESTARTSYS)
243                 rc = 0;
244
245         RETURN(rc);
246 }
247
248 int mdc_rename(struct obd_export *exp, struct mdc_op_data *data,
249                const char *old, int oldlen, const char *new, int newlen,
250                struct ptlrpc_request **request)
251 {
252         struct obd_device *obd = exp->exp_obd;
253         struct ptlrpc_request *req;
254         int rc, size[5] = {0, sizeof(struct mds_rec_rename), oldlen + 1,
255                            newlen + 1, obd->u.cli.cl_max_mds_cookiesize};
256         ENTRY;
257
258         size[0] = lustre_secdesc_size();
259
260         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
261                               MDS_REINT, 5, size, NULL);
262         if (req == NULL)
263                 RETURN(-ENOMEM);
264
265         lustre_pack_secdesc(req, size[0]);
266
267         mdc_rename_pack(req->rq_reqmsg, 1, data, old, oldlen, new, newlen);
268
269         size[0] = sizeof(struct mds_body);
270         size[1] = obd->u.cli.cl_max_mds_easize;
271         size[2] = obd->u.cli.cl_max_mds_cookiesize;
272         req->rq_replen = lustre_msg_size(3, size);
273
274         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
275         *request = req;
276         if (rc == -ERESTARTSYS)
277                 rc = 0;
278
279         RETURN(rc);
280 }