Whamcloud - gitweb
LU-5560 security: send file security context for creates
[fs/lustre-release.git] / lustre / mdc / mdc_reint.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDC
38
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41
42 #include <obd_class.h>
43 #include "mdc_internal.h"
44 #include <lustre_fid.h>
45
46 /* mdc_setattr does its own semaphore handling */
47 static int mdc_reint(struct ptlrpc_request *request, int level)
48 {
49         int rc;
50
51         request->rq_send_state = level;
52
53         mdc_get_mod_rpc_slot(request, NULL);
54         rc = ptlrpc_queue_wait(request);
55         mdc_put_mod_rpc_slot(request, 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, const struct lu_fid *fid,
68                             struct list_head *cancels, enum ldlm_mode mode,
69                             __u64 bits)
70 {
71         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
72         union ldlm_policy_data policy = { {0} };
73         struct ldlm_res_id res_id;
74         struct ldlm_resource *res;
75         int count;
76         ENTRY;
77
78         /* Return, i.e. cancel nothing, only if ELC is supported (flag in
79          * export) but disabled through procfs (flag in NS).
80          *
81          * This distinguishes from a case when ELC is not supported originally,
82          * when we still want to cancel locks in advance and just cancel them
83          * locally, without sending any RPC. */
84         if (exp_connect_cancelset(exp) && !ns_connect_cancelset(ns))
85                 RETURN(0);
86
87         fid_build_reg_res_name(fid, &res_id);
88         res = ldlm_resource_get(exp->exp_obd->obd_namespace,
89                                 NULL, &res_id, 0, 0);
90         if (IS_ERR(res))
91                 RETURN(0);
92         LDLM_RESOURCE_ADDREF(res);
93         /* Initialize ibits lock policy. */
94         policy.l_inodebits.bits = bits;
95         count = ldlm_cancel_resource_local(res, cancels, &policy,
96                                            mode, 0, 0, NULL);
97         LDLM_RESOURCE_DELREF(res);
98         ldlm_resource_putref(res);
99         RETURN(count);
100 }
101
102 int mdc_setattr(struct obd_export *exp, struct md_op_data *op_data,
103                 void *ea, size_t ealen, struct ptlrpc_request **request)
104 {
105         struct list_head cancels = LIST_HEAD_INIT(cancels);
106         struct ptlrpc_request *req;
107         int count = 0, rc;
108         __u64 bits;
109         ENTRY;
110
111         LASSERT(op_data != NULL);
112
113         bits = MDS_INODELOCK_UPDATE;
114         if (op_data->op_attr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
115                 bits |= MDS_INODELOCK_LOOKUP;
116         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
117             (fid_is_sane(&op_data->op_fid1)))
118                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
119                                                 &cancels, LCK_EX, bits);
120         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
121                                    &RQF_MDS_REINT_SETATTR);
122         if (req == NULL) {
123                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
124                 RETURN(-ENOMEM);
125         }
126
127         req_capsule_set_size(&req->rq_pill, &RMF_MDT_EPOCH, RCL_CLIENT, 0);
128         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT, ealen);
129         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_CLIENT, 0);
130
131         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
132         if (rc) {
133                 ptlrpc_request_free(req);
134                 RETURN(rc);
135         }
136
137         if (op_data->op_attr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
138                 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T
139                        ", ctime "CFS_TIME_T"\n",
140                        LTIME_S(op_data->op_attr.ia_mtime),
141                        LTIME_S(op_data->op_attr.ia_ctime));
142         mdc_setattr_pack(req, op_data, ea, ealen);
143
144         ptlrpc_request_set_replen(req);
145
146         rc = mdc_reint(req, LUSTRE_IMP_FULL);
147         if (rc == -ERESTARTSYS)
148                 rc = 0;
149
150         *request = req;
151
152         RETURN(rc);
153 }
154
155 int mdc_create(struct obd_export *exp, struct md_op_data *op_data,
156                 const void *data, size_t datalen,
157                 umode_t mode, uid_t uid, gid_t gid,
158                 cfs_cap_t cap_effective, __u64 rdev,
159                 struct ptlrpc_request **request)
160 {
161         struct ptlrpc_request *req;
162         int level, rc;
163         int count, resends = 0;
164         struct obd_import *import = exp->exp_obd->u.cli.cl_import;
165         int generation = import->imp_generation;
166         struct list_head cancels = LIST_HEAD_INIT(cancels);
167         ENTRY;
168
169         /* For case if upper layer did not alloc fid, do it now. */
170         if (!fid_is_sane(&op_data->op_fid2)) {
171                 /*
172                  * mdc_fid_alloc() may return errno 1 in case of switch to new
173                  * sequence, handle this.
174                  */
175                 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
176                 if (rc < 0)
177                         RETURN(rc);
178         }
179
180 rebuild:
181         count = 0;
182         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
183             (fid_is_sane(&op_data->op_fid1)))
184                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
185                                                 &cancels, LCK_EX,
186                                                 MDS_INODELOCK_UPDATE);
187
188         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
189                                    &RQF_MDS_REINT_CREATE_ACL);
190         if (req == NULL) {
191                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
192                 RETURN(-ENOMEM);
193         }
194
195         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
196                              op_data->op_namelen + 1);
197         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
198                              data && datalen ? datalen : 0);
199
200         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX_NAME,
201                              RCL_CLIENT, op_data->op_file_secctx_name != NULL ?
202                              strlen(op_data->op_file_secctx_name) + 1 : 0);
203
204         req_capsule_set_size(&req->rq_pill, &RMF_FILE_SECCTX, RCL_CLIENT,
205                              op_data->op_file_secctx_size);
206
207         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
208         if (rc) {
209                 ptlrpc_request_free(req);
210                 RETURN(rc);
211         }
212
213         /*
214          * mdc_create_pack() fills msg->bufs[1] with name and msg->bufs[2] with
215          * tgt, for symlinks or lov MD data.
216          */
217         mdc_create_pack(req, op_data, data, datalen, mode, uid,
218                         gid, cap_effective, rdev);
219
220         ptlrpc_request_set_replen(req);
221
222         /* ask ptlrpc not to resend on EINPROGRESS since we have our own retry
223          * logic here */
224         req->rq_no_retry_einprogress = 1;
225
226         if (resends) {
227                 req->rq_generation_set = 1;
228                 req->rq_import_generation = generation;
229                 req->rq_sent = cfs_time_current_sec() + resends;
230         }
231         level = LUSTRE_IMP_FULL;
232  resend:
233         rc = mdc_reint(req, level);
234
235         /* Resend if we were told to. */
236         if (rc == -ERESTARTSYS) {
237                 level = LUSTRE_IMP_RECOVER;
238                 goto resend;
239         } else if (rc == -EINPROGRESS) {
240                 /* Retry create infinitely until succeed or get other
241                  * error code. */
242                 ptlrpc_req_finished(req);
243                 resends++;
244
245                 CDEBUG(D_HA, "%s: resend:%d create on "DFID"/"DFID"\n",
246                        exp->exp_obd->obd_name, resends,
247                        PFID(&op_data->op_fid1), PFID(&op_data->op_fid2));
248
249                 if (generation == import->imp_generation) {
250                         goto rebuild;
251                 } else {
252                         CDEBUG(D_HA, "resend cross eviction\n");
253                         RETURN(-EIO);
254                 }
255         }
256
257         *request = req;
258         RETURN(rc);
259 }
260
261 int mdc_unlink(struct obd_export *exp, struct md_op_data *op_data,
262                struct ptlrpc_request **request)
263 {
264         struct list_head cancels = LIST_HEAD_INIT(cancels);
265         struct obd_device *obd = class_exp2obd(exp);
266         struct ptlrpc_request *req = *request;
267         int count = 0, rc;
268         ENTRY;
269
270         LASSERT(req == NULL);
271
272         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
273             (fid_is_sane(&op_data->op_fid1)))
274                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
275                                                 &cancels, LCK_EX,
276                                                 MDS_INODELOCK_UPDATE);
277         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
278             (fid_is_sane(&op_data->op_fid3)))
279                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
280                                                  &cancels, LCK_EX,
281                                                  MDS_INODELOCK_FULL);
282         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
283                                    &RQF_MDS_REINT_UNLINK);
284         if (req == NULL) {
285                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
286                 RETURN(-ENOMEM);
287         }
288
289         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
290                              op_data->op_namelen + 1);
291
292         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
293         if (rc) {
294                 ptlrpc_request_free(req);
295                 RETURN(rc);
296         }
297
298         mdc_unlink_pack(req, op_data);
299
300         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
301                              obd->u.cli.cl_default_mds_easize);
302         ptlrpc_request_set_replen(req);
303
304         *request = req;
305
306         rc = mdc_reint(req, LUSTRE_IMP_FULL);
307         if (rc == -ERESTARTSYS)
308                 rc = 0;
309         RETURN(rc);
310 }
311
312 int mdc_link(struct obd_export *exp, struct md_op_data *op_data,
313              struct ptlrpc_request **request)
314 {
315         struct list_head cancels = LIST_HEAD_INIT(cancels);
316         struct ptlrpc_request *req;
317         int count = 0, rc;
318         ENTRY;
319
320         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
321             (fid_is_sane(&op_data->op_fid2)))
322                 count = mdc_resource_get_unused(exp, &op_data->op_fid2,
323                                                 &cancels, LCK_EX,
324                                                 MDS_INODELOCK_UPDATE);
325         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
326             (fid_is_sane(&op_data->op_fid1)))
327                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
328                                                  &cancels, LCK_EX,
329                                                  MDS_INODELOCK_UPDATE);
330
331         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_REINT_LINK);
332         if (req == NULL) {
333                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
334                 RETURN(-ENOMEM);
335         }
336
337         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
338                              op_data->op_namelen + 1);
339
340         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
341         if (rc) {
342                 ptlrpc_request_free(req);
343                 RETURN(rc);
344         }
345
346         mdc_link_pack(req, op_data);
347         ptlrpc_request_set_replen(req);
348
349         rc = mdc_reint(req, LUSTRE_IMP_FULL);
350         *request = req;
351         if (rc == -ERESTARTSYS)
352                 rc = 0;
353
354         RETURN(rc);
355 }
356
357 int mdc_rename(struct obd_export *exp, struct md_op_data *op_data,
358                 const char *old, size_t oldlen, const char *new, size_t newlen,
359                 struct ptlrpc_request **request)
360 {
361         struct list_head cancels = LIST_HEAD_INIT(cancels);
362         struct obd_device *obd = exp->exp_obd;
363         struct ptlrpc_request *req;
364         int count = 0, rc;
365         ENTRY;
366
367         if ((op_data->op_flags & MF_MDC_CANCEL_FID1) &&
368             (fid_is_sane(&op_data->op_fid1)))
369                 count = mdc_resource_get_unused(exp, &op_data->op_fid1,
370                                                 &cancels, LCK_EX,
371                                                 MDS_INODELOCK_UPDATE);
372         if ((op_data->op_flags & MF_MDC_CANCEL_FID2) &&
373             (fid_is_sane(&op_data->op_fid2)))
374                 count += mdc_resource_get_unused(exp, &op_data->op_fid2,
375                                                  &cancels, LCK_EX,
376                                                  MDS_INODELOCK_UPDATE);
377         if ((op_data->op_flags & MF_MDC_CANCEL_FID3) &&
378             (fid_is_sane(&op_data->op_fid3)))
379                 count += mdc_resource_get_unused(exp, &op_data->op_fid3,
380                                                  &cancels, LCK_EX,
381                                                  MDS_INODELOCK_LOOKUP);
382         if ((op_data->op_flags & MF_MDC_CANCEL_FID4) &&
383              (fid_is_sane(&op_data->op_fid4)))
384                 count += mdc_resource_get_unused(exp, &op_data->op_fid4,
385                                                  &cancels, LCK_EX,
386                                                  MDS_INODELOCK_FULL);
387
388         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
389                            op_data->op_cli_flags & CLI_MIGRATE ?
390                            &RQF_MDS_REINT_MIGRATE : &RQF_MDS_REINT_RENAME);
391         if (req == NULL) {
392                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
393                 RETURN(-ENOMEM);
394         }
395
396         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, oldlen + 1);
397         req_capsule_set_size(&req->rq_pill, &RMF_SYMTGT, RCL_CLIENT, newlen+1);
398
399         rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
400         if (rc) {
401                 ptlrpc_request_free(req);
402                 RETURN(rc);
403         }
404
405         if (op_data->op_cli_flags & CLI_MIGRATE && op_data->op_data != NULL) {
406                 struct md_open_data *mod = op_data->op_data;
407
408                 LASSERTF(mod->mod_open_req != NULL &&
409                          mod->mod_open_req->rq_type != LI_POISON,
410                          "POISONED open %p!\n", mod->mod_open_req);
411
412                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
413                 /* We no longer want to preserve this open for replay even
414                  * though the open was committed. b=3632, b=3633 */
415                 spin_lock(&mod->mod_open_req->rq_lock);
416                 mod->mod_open_req->rq_replay = 0;
417                 spin_unlock(&mod->mod_open_req->rq_lock);
418         }
419
420         if (exp_connect_cancelset(exp) && req)
421                 ldlm_cli_cancel_list(&cancels, count, req, 0);
422
423         mdc_rename_pack(req, op_data, old, oldlen, new, newlen);
424
425         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
426                              obd->u.cli.cl_default_mds_easize);
427         ptlrpc_request_set_replen(req);
428
429         rc = mdc_reint(req, LUSTRE_IMP_FULL);
430         *request = req;
431         if (rc == -ERESTARTSYS)
432                 rc = 0;
433
434         RETURN(rc);
435 }