Whamcloud - gitweb
branch: HEAD
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #ifdef __KERNEL__
31 #ifndef AUTOCONF_INCLUDED
32 # include <linux/config.h>
33 #endif
34 # include <linux/module.h>
35 # include <linux/kernel.h>
36 #else
37 # include <liblustre.h>
38 #endif
39
40 #include <obd_class.h>
41 #include "mdc_internal.h"
42 #include <lustre_fid.h>
43
44 /* mdc_setattr does its own semaphore handling */
45 static int mdc_reint(struct ptlrpc_request *request,
46                      struct mdc_rpc_lock *rpc_lock,
47                      int level)
48 {
49         int rc;
50
51         request->rq_send_state = level;
52
53         mdc_get_rpc_lock(rpc_lock, NULL);
54         rc = ptlrpc_queue_wait(request);
55         mdc_put_rpc_lock(rpc_lock, NULL);
56         if (rc)
57                 CDEBUG(D_INFO, "error in handling %d\n", rc);
58         else if (!req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY)) {
59                 rc = -EPROTO;
60         }
61         return rc;
62 }
63
64 /* Find and cancel locally locks matched by inode @bits & @mode in the resource
65  * found by @fid. Found locks are added into @cancel list. Returns the amount of
66  * locks added to @cancels list. */
67 int mdc_resource_get_unused(struct obd_export *exp, struct lu_fid *fid,
68                             struct list_head *cancels, ldlm_mode_t mode,
69                             __u64 bits)
70 {
71         ldlm_policy_data_t policy = {{0}};
72         struct ldlm_res_id res_id;
73         struct ldlm_resource *res;
74         int count;
75         ENTRY;
76
77         fid_build_reg_res_name(fid, &res_id);
78         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
79                                 NULL, &res_id, 0, 0);
80         if (res == NULL)
81                 RETURN(0);
82
83         /* Initialize ibits lock policy. */
84         policy.l_inodebits.bits = bits;
85         count = ldlm_cancel_resource_local(res, cancels, &policy,
86                                            mode, 0, 0, NULL);
87         ldlm_resource_putref(res);
88         RETURN(count);
89 }
90
91 static int mdc_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
92                             struct list_head *cancels, int count)
93 {
94         return ldlm_prep_elc_req(exp, req, LUSTRE_MDS_VERSION, MDS_REINT,
95                                  0, cancels, count);
96 }
97
98 /* If mdc_setattr is called with an 'iattr', then it is a normal RPC that
99  * should take the normal semaphore and go to the normal portal.
100  *
101  * If it is called with iattr->ia_valid & ATTR_FROM_OPEN, then it is a
102  * magic open-path setattr that should take the setattr semaphore and
103  * go to the setattr portal. */
104 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
105                 void *ea, int ealen, void *ea2, int ea2len,
106                 struct ptlrpc_request **request, struct md_open_data **mod)
107 {
108         CFS_LIST_HEAD(cancels);
109         struct ptlrpc_request *req;
110         struct mdc_rpc_lock *rpc_lock;
111         struct obd_device *obd = exp->exp_obd;
112         int count = 0, rc;
113         __u64 bits;
114         ENTRY;
115
116         LASSERT(op_data != NULL);
117
118         bits = MDS_INODELOCK_UPDATE;
119         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
120                 bits |= MDS_INODELOCK_LOOKUP;
121         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
122             (fid_is_sane(&op_data->op_fid1)))
123                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
124                                                 &cancels, LCK_EX, bits);
125         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
126                                    &RQF_MDS_REINT_SETATTR);
127         if (req == NULL) {
128                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
129                 RETURN(-ENOMEM);
130         }
131         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
132         if ((op_data->op_flags & (MF_SOM_CHANGE | MF_EPOCH_OPEN)) == 0)
133                 req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT,
134                                      0);
135         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
136         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT,
137                              ea2len);
138
139         rc = mdc_prep_elc_req(exp, req, &cancels, count);
140         if (rc) {
141                 ptlrpc_request_free(req);
142                 RETURN(rc);
143         }
144
145         if (op_data->op_attr.ia_valid & ATTR_FROM_OPEN) {
146                 req->rq_request_portal = MDS_SETATTR_PORTAL;
147                 ptlrpc_at_set_req_timeout(req);
148                 rpc_lock = obd->u.cli.cl_setattr_lock;
149         } else {
150                 rpc_lock = obd->u.cli.cl_rpc_lock;
151         }
152
153         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
154                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
155                        ", ctime "CFS_TIME_T"\n",
156                        LTIME_S(op_data->op_attr.ia_mtime),
157                        LTIME_S(op_data->op_attr.ia_ctime));
158         mdc_setattr_pack(req, op_data, ea, ealen, ea2, ea2len);
159
160         ptlrpc_request_set_replen(req);
161         if (mod && (op_data->op_flags & MF_EPOCH_OPEN) &&
162             req->rq_import->imp_replayable)
163         {
164                 LASSERT(*mod == NULL);
165
166                 OBD_ALLOC_PTR(*mod);
167                 if (*mod == NULL) {
168                         DEBUG_REQ(D_ERROR, req, "Can't allocate "
169                                   "md_open_data");
170                 } else {
171                         CFS_INIT_LIST_HEAD(&(*mod)->mod_replay_list);
172                 }
173         }
174         if (mod && *mod) {
175                 req->rq_cb_data = *mod;
176                 req->rq_commit_cb = mdc_commit_delayed;
177                 list_add_tail(&req->rq_mod_list, &(*mod)->mod_replay_list);
178                 /* This is not the last request in sequence for truncate. */
179                 if (op_data->op_flags & MF_EPOCH_OPEN)
180                         req->rq_replay = 1;
181                 else
182                         req->rq_sequence = 1;
183         }
184
185         rc = mdc_reint(req, rpc_lock, LUSTRE_IMP_FULL);
186         *request = req;
187         if (rc == -ERESTARTSYS)
188                 rc = 0;
189         if (rc && req->rq_commit_cb)
190                 req->rq_commit_cb(req);
191         RETURN(rc);
192 }
193
194 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
195                const void *data, int datalen, int mode, __u32 uid, __u32 gid,
196                __u32 cap_effective, __u64 rdev, struct ptlrpc_request **request)
197 {
198         struct ptlrpc_request *req;
199         int level, rc;
200         int count = 0;
201         CFS_LIST_HEAD(cancels);
202         ENTRY;
203
204         /* For case if upper layer did not alloc fid, do it now. */
205         if (!fid_is_sane(&op_data->op_fid2)) {
206                 /*
207                  * mdc_fid_alloc() may return errno 1 in case of switch to new
208                  * sequence, handle this.
209                  */
210                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
211                 if (rc < 0) {
212                         CERROR("Can't alloc new fid, rc %d\n", rc);
213                         RETURN(rc);
214                 }
215         }
216
217         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
218             (fid_is_sane(&op_data->op_fid1)))
219                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
220                                                 &cancels, LCK_EX,
221                                                 MDS_INODELOCK_UPDATE);
222
223         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
224                                    &RQF_MDS_REINT_CREATE_RMT_ACL);
225         if (req == NULL) {
226                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
227                 RETURN(-ENOMEM);
228         }
229         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
230         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
231                              op_data->op_namelen + 1);
232         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
233                              data && datalen ? datalen : 0);
234
235         rc = mdc_prep_elc_req(exp, req, &cancels, count);
236         if (rc) {
237                 ptlrpc_request_free(req);
238                 RETURN(rc);
239         }
240
241         /*
242          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
243          * tgt, for symlinks or lov MD data.
244          */
245         mdc_create_pack(req, op_data, data, datalen, mode, uid,
246                         gid, cap_effective, rdev);
247
248         ptlrpc_request_set_replen(req);
249
250         level = LUSTRE_IMP_FULL;
251  resend:
252         rc = mdc_reint(req, exp->exp_obd->u.cli.cl_rpc_lock, level);
253         
254         /* Resend if we were told to. */
255         if (rc == -ERESTARTSYS) {
256                 level = LUSTRE_IMP_RECOVER;
257                 goto resend;
258         } else if (rc == 0) {
259                 struct mdt_body *body;
260                 struct lustre_capa *capa;
261
262                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
263                 LASSERT(body);
264                 if (body->valid & OBD_MD_FLMDSCAPA) {
265                         capa = req_capsule_server_get(&req->rq_pill,
266                                                       &RMF_CAPA1);
267                         if (capa == NULL)
268                                 rc = -EPROTO;
269                 }
270         }
271
272         *request = req;
273         RETURN(rc);
274 }
275
276 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
277                struct ptlrpc_request **request)
278 {
279         CFS_LIST_HEAD(cancels);
280         struct obd_device *obd = class_exp2obd(exp);
281         struct ptlrpc_request *req = *request;
282         int count = 0, rc;
283         ENTRY;
284
285         LASSERT(req == NULL);
286
287         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) && 
288             (fid_is_sane(&op_data->op_fid1)))
289                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
290                                                 &cancels, LCK_EX,
291                                                 MDS_INODELOCK_UPDATE);
292         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
293             (fid_is_sane(&op_data->op_fid3)))
294                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
295                                                  &cancels, LCK_EX,
296                                                  MDS_INODELOCK_FULL);
297         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
298                                    &RQF_MDS_REINT_UNLINK);
299         if (req == NULL) {
300                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
301                 RETURN(-ENOMEM);
302         }
303         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
304         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
305                              op_data->op_namelen + 1);
306
307         rc = mdc_prep_elc_req(exp, req, &cancels, count);
308         if (rc) {
309                 ptlrpc_request_free(req);
310                 RETURN(rc);
311         }
312
313         mdc_unlink_pack(req, op_data);
314
315         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
316                              obd->u.cli.cl_max_mds_easize);
317         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
318                              obd->u.cli.cl_max_mds_cookiesize);
319         ptlrpc_request_set_replen(req);
320
321         *request = req;
322
323         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
324         if (rc == -ERESTARTSYS)
325                 rc = 0;
326         RETURN(rc);
327 }
328
329 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
330              struct ptlrpc_request **request)
331 {
332         CFS_LIST_HEAD(cancels);
333         struct obd_device *obd = exp->exp_obd;
334         struct ptlrpc_request *req;
335         int count = 0, rc;
336         ENTRY;
337
338         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
339             (fid_is_sane(&op_data->op_fid2)))
340                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
341                                                 &cancels, LCK_EX,
342                                                 MDS_INODELOCK_UPDATE);
343         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
344             (fid_is_sane(&op_data->op_fid1)))
345                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
346                                                  &cancels, LCK_EX,
347                                                  MDS_INODELOCK_UPDATE);
348
349         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
350         if (req == NULL) {
351                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
352                 RETURN(-ENOMEM);
353         }
354         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
355         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
356         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
357                              op_data->op_namelen + 1);
358
359         rc = mdc_prep_elc_req(exp, req, &cancels, count);
360         if (rc) {
361                 ptlrpc_request_free(req);
362                 RETURN(rc);
363         }
364
365         mdc_link_pack(req, op_data);
366         ptlrpc_request_set_replen(req);
367
368         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
369         *request = req;
370         if (rc == -ERESTARTSYS)
371                 rc = 0;
372
373         RETURN(rc);
374 }
375
376 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
377                const char *old, int oldlen, const char *new, int newlen,
378                struct ptlrpc_request **request)
379 {
380         CFS_LIST_HEAD(cancels);
381         struct obd_device *obd = exp->exp_obd;
382         struct ptlrpc_request *req;
383         int count = 0, rc;
384         ENTRY;
385
386         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
387             (fid_is_sane(&op_data->op_fid1)))
388                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
389                                                 &cancels, LCK_EX,
390                                                 MDS_INODELOCK_UPDATE);
391         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
392             (fid_is_sane(&op_data->op_fid2)))
393                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
394                                                  &cancels, LCK_EX,
395                                                  MDS_INODELOCK_UPDATE);
396         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) && 
397             (fid_is_sane(&op_data->op_fid3)))
398                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
399                                                  &cancels, LCK_EX,
400                                                  MDS_INODELOCK_LOOKUP);
401         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
402              (fid_is_sane(&op_data->op_fid4)))
403                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
404                                                  &cancels, LCK_EX,
405                                                  MDS_INODELOCK_FULL);
406
407         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
408                                    &RQF_MDS_REINT_RENAME);
409         if (req == NULL) {
410                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
411                 RETURN(-ENOMEM);
412         }
413
414         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
415         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
416         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
417         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
418
419         rc = mdc_prep_elc_req(exp, req, &cancels, count);
420         if (rc) {
421                 ptlrpc_request_free(req);
422                 RETURN(rc);
423         }
424
425         if (exp_connect_cancelset(exp) && req)
426                 ldlm_cli_cancel_list(&cancels, count, req, 0);
427
428         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
429
430         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
431                              obd->u.cli.cl_max_mds_easize);
432         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
433                              obd->u.cli.cl_max_mds_cookiesize);
434         ptlrpc_request_set_replen(req);
435
436         rc = mdc_reint(req, obd->u.cli.cl_rpc_lock, LUSTRE_IMP_FULL);
437         *request = req;
438         if (rc == -ERESTARTSYS)
439                 rc = 0;
440
441         RETURN(rc);
442 }