Whamcloud - gitweb
LU-11952 mdt: fix reconstruct open
[fs/lustre-release.git] / lustre / mdt / mdt_open.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdt/mdt_open.c
32  *
33  * Lustre Metadata Target (mdt) open/close file handling
34  *
35  * Author: Huang Hua <huanghua@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <lustre_acl.h>
41 #include <lustre_mds.h>
42 #include <lustre_swab.h>
43 #include "mdt_internal.h"
44 #include <lustre_nodemap.h>
45
46 static const char mfd_open_handle_owner[] = "mdt";
47
48 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep,
49                            struct mdt_lock_handle *lhc);
50
51 /* Create a new mdt_file_data struct, initialize it,
52  * and insert it to global hash table */
53 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med)
54 {
55         struct mdt_file_data *mfd;
56         ENTRY;
57
58         OBD_ALLOC_PTR(mfd);
59         if (mfd != NULL) {
60                 refcount_set(&mfd->mfd_open_handle.h_ref, 1);
61                 INIT_HLIST_NODE(&mfd->mfd_open_handle.h_link);
62                 mfd->mfd_owner = med;
63                 INIT_LIST_HEAD(&mfd->mfd_list);
64                 class_handle_hash(&mfd->mfd_open_handle, mfd_open_handle_owner);
65         }
66
67         RETURN(mfd);
68 }
69
70 /*
71  * Find the mfd pointed to by handle in global hash table.
72  * In case of replay the handle is obsoleted
73  * but mfd can be found in mfd list by that handle.
74  * Callers need to be holding med_open_lock.
75  */
76 struct mdt_file_data *mdt_open_handle2mfd(struct mdt_export_data *med,
77                                         const struct lustre_handle *open_handle,
78                                         bool is_replay_or_resent)
79 {
80         struct mdt_file_data   *mfd;
81         ENTRY;
82
83         LASSERT(open_handle != NULL);
84         mfd = class_handle2object(open_handle->cookie, mfd_open_handle_owner);
85         if (mfd)
86                 refcount_dec(&mfd->mfd_open_handle.h_ref);
87
88         /* during dw/setattr replay the mfd can be found by old handle */
89         if ((!mfd || mfd->mfd_owner != med) && is_replay_or_resent) {
90                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
91                         if (mfd->mfd_open_handle_old.cookie ==
92                             open_handle->cookie)
93                                 RETURN(mfd);
94                 }
95                 mfd = NULL;
96         }
97
98         RETURN(mfd);
99 }
100
101 /* free mfd */
102 void mdt_mfd_free(struct mdt_file_data *mfd)
103 {
104         LASSERT(refcount_read(&mfd->mfd_open_handle.h_ref) == 1);
105         LASSERT(list_empty(&mfd->mfd_list));
106         OBD_FREE_PRE(mfd, sizeof(*mfd), "rcu");
107         kfree_rcu(mfd, mfd_open_handle.h_rcu);
108 }
109
110 static int mdt_create_data(struct mdt_thread_info *info,
111                            struct mdt_object *p, struct mdt_object *o)
112 {
113         struct md_op_spec     *spec = &info->mti_spec;
114         struct md_attr        *ma   = &info->mti_attr;
115         int                    rc   = 0;
116         ENTRY;
117
118         if (!md_should_create(spec->sp_cr_flags))
119                 RETURN(0);
120
121         ma->ma_need = MA_INODE | MA_LOV;
122         ma->ma_valid = 0;
123         mutex_lock(&o->mot_lov_mutex);
124         if (!o->mot_lov_created) {
125                 rc = mdo_create_data(info->mti_env,
126                                      p ? mdt_object_child(p) : NULL,
127                                      mdt_object_child(o), spec, ma);
128                 if (rc == 0)
129                         rc = mdt_attr_get_complex(info, o, ma);
130
131                 if (rc == 0 && ma->ma_valid & MA_LOV)
132                         o->mot_lov_created = 1;
133         }
134
135         mutex_unlock(&o->mot_lov_mutex);
136         RETURN(rc);
137 }
138
139 int mdt_write_read(struct mdt_object *o)
140 {
141         int rc = 0;
142         ENTRY;
143         spin_lock(&o->mot_write_lock);
144         rc = o->mot_write_count;
145         spin_unlock(&o->mot_write_lock);
146         RETURN(rc);
147 }
148
149 int mdt_write_get(struct mdt_object *o)
150 {
151         int rc = 0;
152         ENTRY;
153         spin_lock(&o->mot_write_lock);
154         if (o->mot_write_count < 0)
155                 rc = -ETXTBSY;
156         else
157                 o->mot_write_count++;
158         spin_unlock(&o->mot_write_lock);
159
160         RETURN(rc);
161 }
162
163 void mdt_write_put(struct mdt_object *o)
164 {
165         ENTRY;
166         spin_lock(&o->mot_write_lock);
167         o->mot_write_count--;
168         spin_unlock(&o->mot_write_lock);
169         EXIT;
170 }
171
172 static int mdt_write_deny(struct mdt_object *o)
173 {
174         int rc = 0;
175         ENTRY;
176         spin_lock(&o->mot_write_lock);
177         if (o->mot_write_count > 0)
178                 rc = -ETXTBSY;
179         else
180                 o->mot_write_count--;
181         spin_unlock(&o->mot_write_lock);
182         RETURN(rc);
183 }
184
185 static void mdt_write_allow(struct mdt_object *o)
186 {
187         ENTRY;
188         spin_lock(&o->mot_write_lock);
189         o->mot_write_count++;
190         spin_unlock(&o->mot_write_lock);
191         EXIT;
192 }
193
194 /* there can be no real transaction so prepare the fake one */
195 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
196 {
197         struct mdt_device      *mdt = info->mti_mdt;
198         struct ptlrpc_request  *req = mdt_info_req(info);
199         struct tg_export_data  *ted;
200         struct lsd_client_data *lcd;
201         ENTRY;
202
203         if (mdt_rdonly(req->rq_export))
204                 RETURN_EXIT;
205
206         /* transaction has occurred already */
207         if (lustre_msg_get_transno(req->rq_repmsg) != 0)
208                 RETURN_EXIT;
209
210         if (tgt_is_multimodrpcs_client(req->rq_export)) {
211                 struct thandle         *th;
212
213                 /* generate an empty transaction to get a transno
214                  * and reply data */
215                 th = dt_trans_create(info->mti_env, mdt->mdt_bottom);
216                 if (!IS_ERR(th)) {
217                         rc = dt_trans_start(info->mti_env, mdt->mdt_bottom, th);
218                         dt_trans_stop(info->mti_env, mdt->mdt_bottom, th);
219                 }
220                 RETURN_EXIT;
221         }
222
223         spin_lock(&mdt->mdt_lut.lut_translock);
224         if (rc != 0) {
225                 if (info->mti_transno != 0) {
226                         struct obd_export *exp = req->rq_export;
227
228                         CERROR("%s: replay trans %llu NID %s: rc = %d\n",
229                                mdt_obd_name(mdt), info->mti_transno,
230                                obd_export_nid2str(exp), rc);
231                         spin_unlock(&mdt->mdt_lut.lut_translock);
232                         RETURN_EXIT;
233                 }
234         } else if (info->mti_transno == 0) {
235                 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
236         } else {
237                 /* should be replay */
238                 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
239                         mdt->mdt_lut.lut_last_transno = info->mti_transno;
240         }
241         spin_unlock(&mdt->mdt_lut.lut_translock);
242
243         CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
244                info->mti_transno,
245                req->rq_export->exp_obd->obd_last_committed);
246
247         req->rq_transno = info->mti_transno;
248         lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
249
250         /* update lcd in memory only for resent cases */
251         ted = &req->rq_export->exp_target_data;
252         LASSERT(ted);
253         mutex_lock(&ted->ted_lcd_lock);
254         lcd = ted->ted_lcd;
255         if (info->mti_transno < lcd->lcd_last_transno &&
256             info->mti_transno != 0) {
257                 /* This should happen during replay. Do not update
258                  * last rcvd info if replay req transno < last transno,
259                  * otherwise the following resend(after replay) can not
260                  * be checked correctly by xid */
261                 mutex_unlock(&ted->ted_lcd_lock);
262                 CDEBUG(D_HA, "%s: transno = %llu < last_transno = %llu\n",
263                        mdt_obd_name(mdt), info->mti_transno,
264                        lcd->lcd_last_transno);
265                 RETURN_EXIT;
266         }
267
268         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
269                 if (info->mti_transno != 0)
270                         lcd->lcd_last_close_transno = info->mti_transno;
271                 lcd->lcd_last_close_xid = req->rq_xid;
272                 lcd->lcd_last_close_result = rc;
273         } else {
274                 /* VBR: save versions in last_rcvd for reconstruct. */
275                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
276                 if (pre_versions) {
277                         lcd->lcd_pre_versions[0] = pre_versions[0];
278                         lcd->lcd_pre_versions[1] = pre_versions[1];
279                         lcd->lcd_pre_versions[2] = pre_versions[2];
280                         lcd->lcd_pre_versions[3] = pre_versions[3];
281                 }
282                 if (info->mti_transno != 0)
283                         lcd->lcd_last_transno = info->mti_transno;
284
285                 lcd->lcd_last_xid = req->rq_xid;
286                 lcd->lcd_last_result = rc;
287                 lcd->lcd_last_data = info->mti_opdata;
288         }
289         mutex_unlock(&ted->ted_lcd_lock);
290
291         EXIT;
292 }
293
294 void mdt_mfd_set_mode(struct mdt_file_data *mfd, u64 open_flags)
295 {
296         LASSERT(mfd != NULL);
297
298         CDEBUG(D_DENTRY, DFID " Change mfd open_flags %#llo -> %#llo.\n",
299                PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_open_flags,
300                open_flags);
301
302         mfd->mfd_open_flags = open_flags;
303 }
304
305 /**
306  * prep ma_lmm/ma_lmv for md_attr from reply
307  */
308 static void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
309                                      struct mdt_object *obj,
310                                      struct md_attr *ma)
311 {
312         if (ma->ma_lmv || ma->ma_lmm) {
313                 CDEBUG(D_INFO, DFID " %s already set.\n",
314                        PFID(mdt_object_fid(obj)),
315                        ma->ma_lmv ? (ma->ma_lmm ? "ma_lmv and ma_lmm"
316                                                 : "ma_lmv")
317                                   : "ma_lmm");
318                 return;
319         }
320
321         if (S_ISDIR(obj->mot_header.loh_attr)) {
322                 ma->ma_lmv = req_capsule_server_get(info->mti_pill,
323                                                     &RMF_MDT_MD);
324                 ma->ma_lmv_size = req_capsule_get_size(info->mti_pill,
325                                                        &RMF_MDT_MD,
326                                                        RCL_SERVER);
327                 if (ma->ma_lmv_size > 0)
328                         ma->ma_need |= MA_LMV;
329         } else {
330                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
331                                                     &RMF_MDT_MD);
332                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
333                                                        &RMF_MDT_MD,
334                                                        RCL_SERVER);
335                 if (ma->ma_lmm_size > 0)
336                         ma->ma_need |= MA_LOV;
337         }
338 }
339
340 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
341                         struct mdt_object *o, u64 open_flags, int created,
342                         struct ldlm_reply *rep)
343 {
344         struct ptlrpc_request *req = mdt_info_req(info);
345         struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
346         struct mdt_file_data *mfd;
347         struct md_attr *ma  = &info->mti_attr;
348         struct lu_attr *la  = &ma->ma_attr;
349         struct mdt_body *repbody;
350         bool isdir, isreg;
351         int rc = 0;
352
353         ENTRY;
354         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
355
356         isreg = S_ISREG(la->la_mode);
357         isdir = S_ISDIR(la->la_mode);
358         if (isreg && !(ma->ma_valid & MA_LOV) &&
359             !(open_flags & MDS_OPEN_RELEASE)) {
360                 /*
361                  * No EA, check whether it is will set regEA and dirEA since in
362                  * above attr get, these size might be zero, so reset it, to
363                  * retrieve the MD after create obj.
364                  */
365                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
366                                                        &RMF_MDT_MD,
367                                                        RCL_SERVER);
368                 /* in replay case, p == NULL */
369                 rc = mdt_create_data(info, p, o);
370                 if (rc)
371                         RETURN(rc);
372
373                 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
374                         mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
375         }
376
377         CDEBUG(D_INODE, "after open, ma_valid bit = %#llx lmm_size = %d\n",
378                ma->ma_valid, ma->ma_lmm_size);
379
380         if (ma->ma_valid & MA_LOV) {
381                 LASSERT(ma->ma_lmm_size != 0);
382                 repbody->mbo_eadatasize = ma->ma_lmm_size;
383                 if (isdir)
384                         repbody->mbo_valid |= OBD_MD_FLDIREA;
385                 else
386                         repbody->mbo_valid |= OBD_MD_FLEASIZE;
387         }
388
389         if (ma->ma_valid & MA_LMV) {
390                 LASSERT(ma->ma_lmv_size != 0);
391                 repbody->mbo_eadatasize = ma->ma_lmv_size;
392                 LASSERT(isdir);
393                 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
394         }
395
396         if (open_flags & MDS_FMODE_WRITE)
397                 rc = mdt_write_get(o);
398         else if (open_flags & MDS_FMODE_EXEC)
399                 rc = mdt_write_deny(o);
400
401         if (rc)
402                 RETURN(rc);
403
404         rc = mo_open(info->mti_env, mdt_object_child(o),
405                      created ? open_flags | MDS_OPEN_CREATED : open_flags,
406                      &info->mti_spec);
407         if (rc != 0) {
408                 /* If we allow the client to chgrp (CFS_SETGRP_PERM), but the
409                  * client does not know which suppgid should be sent to the MDS,
410                  * or some other(s) changed the target file's GID after this RPC
411                  * sent to the MDS with the suppgid as the original GID, then we
412                  * give the client another chance to send the right suppgid. */
413                 if (rc == -EACCES &&
414                     allow_client_chgrp(info, lu_ucred(info->mti_env)))
415                         mdt_set_disposition(info, rep, DISP_OPEN_DENY);
416
417                 GOTO(err_out, rc);
418         }
419
420         mfd = mdt_mfd_new(med);
421         if (mfd == NULL)
422                 GOTO(err_out, rc = -ENOMEM);
423
424         /*
425          * Keep a reference on this object for this open, and is
426          * released by mdt_mfd_close().
427          */
428         mdt_object_get(info->mti_env, o);
429         mfd->mfd_object = o;
430         mfd->mfd_xid = req->rq_xid;
431
432         /*
433          * @open_flags is always not zero. At least it should be FMODE_READ,
434          * FMODE_WRITE or MDS_FMODE_EXEC.
435          */
436         LASSERT(open_flags != 0);
437
438         /* Open handling. */
439         mdt_mfd_set_mode(mfd, open_flags);
440
441         atomic_inc(&o->mot_open_count);
442         if (open_flags & MDS_OPEN_LEASE)
443                 atomic_inc(&o->mot_lease_count);
444
445         /* replay handle */
446         if (req_is_replay(req)) {
447                 struct mdt_file_data *old_mfd;
448                 /* Check wheather old cookie already exist in
449                  * the list, becasue when do recovery, client
450                  * might be disconnected from server, and
451                  * restart replay, so there maybe some orphan
452                  * mfd here, we should remove them */
453                 LASSERT(info->mti_rr.rr_open_handle != NULL);
454                 spin_lock(&med->med_open_lock);
455                 old_mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
456                                               true);
457                 if (old_mfd != NULL) {
458                         CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
459                                "cookie = %#llx\n", mfd,
460                                PFID(mdt_object_fid(mfd->mfd_object)),
461                                info->mti_rr.rr_open_handle->cookie);
462                         class_handle_unhash(&old_mfd->mfd_open_handle);
463                         list_del_init(&old_mfd->mfd_list);
464                         spin_unlock(&med->med_open_lock);
465                         /* no attr update for that close */
466                         la->la_valid = 0;
467                         ma->ma_valid |= MA_FLAGS;
468                         ma->ma_attr_flags |= MDS_RECOV_OPEN;
469                         mdt_mfd_close(info, old_mfd);
470                         ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
471                         ma->ma_valid &= ~MA_FLAGS;
472                 } else {
473                         spin_unlock(&med->med_open_lock);
474                         CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
475                                "cookie = %#llx\n",
476                                PFID(mdt_object_fid(mfd->mfd_object)),
477                                info->mti_rr.rr_open_handle->cookie);
478                 }
479
480                 CDEBUG(D_HA, "Store old cookie %#llx in new mfd\n",
481                        info->mti_rr.rr_open_handle->cookie);
482
483                 mfd->mfd_open_handle_old = *info->mti_rr.rr_open_handle;
484         }
485
486         repbody->mbo_open_handle.cookie = mfd->mfd_open_handle.h_cookie;
487
488         if (req->rq_export->exp_disconnected) {
489                 spin_lock(&med->med_open_lock);
490                 class_handle_unhash(&mfd->mfd_open_handle);
491                 list_del_init(&mfd->mfd_list);
492                 spin_unlock(&med->med_open_lock);
493                 mdt_mfd_close(info, mfd);
494         } else {
495                 spin_lock(&med->med_open_lock);
496                 list_add_tail(&mfd->mfd_list, &med->med_open_head);
497                 spin_unlock(&med->med_open_lock);
498         }
499
500         mdt_empty_transno(info, rc);
501
502         RETURN(rc);
503
504 err_out:
505         if (open_flags & MDS_FMODE_WRITE)
506                 mdt_write_put(o);
507         else if (open_flags & MDS_FMODE_EXEC)
508                 mdt_write_allow(o);
509
510         return rc;
511 }
512
513 static int mdt_finish_open(struct mdt_thread_info *info,
514                            struct mdt_object *p, struct mdt_object *o,
515                            u64 open_flags,
516                            struct ldlm_reply *rep)
517 {
518         struct ptlrpc_request   *req = mdt_info_req(info);
519         struct obd_export       *exp = req->rq_export;
520         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
521         struct md_attr          *ma  = &info->mti_attr;
522         struct lu_attr          *la  = &ma->ma_attr;
523         struct mdt_file_data    *mfd;
524         struct mdt_body         *repbody;
525         int                      created;
526         int                      rc = 0;
527         int                      isreg, isdir, islnk;
528         struct list_head        *t;
529         ENTRY;
530
531         LASSERT(ma->ma_valid & MA_INODE);
532         created = mdt_get_disposition(rep, DISP_OPEN_CREATE);
533
534         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
535
536         isreg = S_ISREG(la->la_mode);
537         isdir = S_ISDIR(la->la_mode);
538         islnk = S_ISLNK(la->la_mode);
539         mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
540
541         /* compatibility check for 2.10 clients when it tries to open mirrored
542          * files. 2.10 clients don't verify overlapping components so they
543          * would read and write mirrored files just as if they were normal
544          * PFL files, which will cause the problem that sycned mirrors actually
545          * contain different data.
546          * Older clients are not a concern here because they don't even
547          * understand PFL layout. */
548         if (isreg && !exp_connect_flr(exp) && ma->ma_valid & MA_LOV &&
549             mdt_lmm_is_flr(ma->ma_lmm)) {
550                 /* LU-10286: for simplicity clients who don't understand
551                  * mirrored layout(with connect flag OBD_CONNECT2_FLR) won't
552                  * be able to open mirrored files */
553                 RETURN(-EOPNOTSUPP);
554         }
555
556         /* Overstriped files can crash older clients */
557         if (isreg && !exp_connect_overstriping(exp) &&
558             mdt_lmm_is_overstriping(ma->ma_lmm))
559                 RETURN(-EOPNOTSUPP);
560
561         /* LU-2275, simulate broken behaviour (esp. prevalent in
562          * pre-2.4 servers where a very strange reply is sent on error
563          * that looks like it was actually almost successful and a
564          * failure at the same time.) */
565         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
566                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
567                                                DISP_LOOKUP_NEG |
568                                                DISP_LOOKUP_POS);
569
570                 if (open_flags & MDS_OPEN_LOCK)
571                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
572
573                 RETURN(-ENOENT);
574         }
575
576 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
577         if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
578                 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
579                 if (IS_ERR(nodemap))
580                         RETURN(PTR_ERR(nodemap));
581
582                 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
583                 nodemap_putref(nodemap);
584                 if (rc)
585                         RETURN(rc);
586         }
587 #endif
588
589         /*
590          * If we are following a symlink, don't open; and do not return open
591          * handle for special nodes as client required.
592          */
593         if (islnk || (!isreg && !isdir &&
594             (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
595                 lustre_msg_set_transno(req->rq_repmsg, 0);
596                 RETURN(0);
597         }
598
599         /*
600          * We need to return the existing object's fid back, so it is done here,
601          * after preparing the reply.
602          */
603         if (!created && (open_flags & MDS_OPEN_EXCL) &&
604             (open_flags & MDS_OPEN_CREAT))
605                 RETURN(-EEXIST);
606
607         /* This can't be done earlier, we need to return reply body */
608         if (isdir) {
609                 if (open_flags & (MDS_OPEN_CREAT | MDS_FMODE_WRITE)) {
610                         /* We are trying to create or write an existing dir. */
611                         RETURN(-EISDIR);
612                 }
613         } else if (open_flags & MDS_OPEN_DIRECTORY)
614                 RETURN(-ENOTDIR);
615
616         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
617                                  OBD_FAIL_MDS_LDLM_REPLY_NET | OBD_FAIL_ONCE))
618                 RETURN(-EAGAIN);
619
620         mfd = NULL;
621         if (info->mti_rr.rr_flags & MRF_OPEN_RESEND) {
622                 spin_lock(&med->med_open_lock);
623                 list_for_each(t, &med->med_open_head) {
624                         mfd = list_entry(t, struct mdt_file_data, mfd_list);
625                         if (mfd->mfd_xid == req->rq_xid) {
626                                 repbody->mbo_open_handle.cookie =
627                                                 mfd->mfd_open_handle.h_cookie;
628                                 break;
629                         }
630                         mfd = NULL;
631                 }
632                 spin_unlock(&med->med_open_lock);
633
634                 if (mfd != NULL) {
635                         /* set repbody->ea_size for resent case */
636                         if (ma->ma_valid & MA_LOV) {
637                                 LASSERT(ma->ma_lmm_size != 0);
638                                 repbody->mbo_eadatasize = ma->ma_lmm_size;
639                                 if (isdir)
640                                         repbody->mbo_valid |= OBD_MD_FLDIREA;
641                                 else
642                                         repbody->mbo_valid |= OBD_MD_FLEASIZE;
643                         }
644                         mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
645                         RETURN(0);
646                 }
647                 /* if we have a real resend (not a resend afrer failover), it
648                  * means close is already happend, so lets return error
649                  */
650                 RETURN(-ESTALE);
651         }
652
653         rc = mdt_mfd_open(info, p, o, open_flags, created, rep);
654         if (!rc)
655                 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
656
657         RETURN(rc);
658 }
659
660 void mdt_reconstruct_open(struct mdt_thread_info *info,
661                           struct mdt_lock_handle *lhc)
662 {
663         struct req_capsule *pill = info->mti_pill;
664         struct ptlrpc_request *req = mdt_info_req(info);
665         struct mdt_reint_record *rr = &info->mti_rr;
666         struct md_attr          *ma   = &info->mti_attr;
667         struct ldlm_reply *ldlm_rep;
668         u64 opdata;
669         int rc;
670         ENTRY;
671
672         LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
673         ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
674
675         ma->ma_need = MA_INODE | MA_HSM;
676         ma->ma_valid = 0;
677         opdata = mdt_req_from_lrd(req, info->mti_reply_data);
678         mdt_set_disposition(info, ldlm_rep, opdata);
679
680         CDEBUG(D_INODE, "This is reconstruct open: disp=%#llx, result=%d\n",
681                ldlm_rep->lock_policy_res1, req->rq_status);
682         if (req->rq_status)
683                 /* We did not create successfully, return error to client. */
684                 GOTO(out, rc = req->rq_status);
685
686         /* tg_reply_data is just memory only  structure, so any non zero fid
687          * means a real resend not a resend after recovery which need to be
688          * handled as regular open
689          */
690         if (likely(!fid_is_zero(&info->mti_reply_data->trd_object))) {
691                 rr->rr_fid2 = &info->mti_reply_data->trd_object;
692                 rr->rr_flags |= MRF_OPEN_RESEND;
693                 rc = mdt_open_by_fid(info, ldlm_rep, lhc);
694                 if (rc)
695                         lustre_msg_set_transno(req->rq_repmsg, 0);
696         } else {
697                 /* We did not try to create, so we are a pure open */
698                 rc = mdt_reint_open(info, lhc);
699         }
700         EXIT;
701 out:
702         req->rq_status = rc;
703         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
704         LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
705 }
706
707 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep,
708                            struct mdt_lock_handle *lhc)
709 {
710         u64 open_flags = info->mti_spec.sp_cr_flags;
711         struct mdt_reint_record *rr = &info->mti_rr;
712         struct md_attr *ma = &info->mti_attr;
713         struct mdt_object *o;
714         int rc;
715
716         ENTRY;
717         o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
718         if (IS_ERR(o))
719                 RETURN(rc = PTR_ERR(o));
720
721         if (unlikely(mdt_object_remote(o))) {
722                 /* the child object was created on remote server */
723                 struct mdt_body *repbody;
724
725                 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
726                                                 DISP_LOOKUP_EXECD |
727                                                 DISP_LOOKUP_POS));
728                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
729                 repbody->mbo_fid1 = *rr->rr_fid2;
730                 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
731                 rc = 0;
732         } else {
733                 if (mdt_object_exists(o)) {
734                         tgt_open_obj_set(info->mti_env, mdt_obj2dt(o));
735                         mdt_set_disposition(info, rep, (DISP_IT_EXECD |
736                                                         DISP_LOOKUP_EXECD |
737                                                         DISP_LOOKUP_POS));
738                         if ((open_flags & MDS_OPEN_EXCL) &&
739                             (open_flags & MDS_OPEN_CREAT))
740                                 mdt_set_disposition(info, rep,
741                                                     DISP_OPEN_CREATE);
742
743                         mdt_prep_ma_buf_from_rep(info, o, ma);
744                         rc = mdt_attr_get_complex(info, o, ma);
745                         if (rc)
746                                 GOTO(out, rc);
747                         rc = mdt_finish_open(info, NULL, o, open_flags, rep);
748                         if (rc)
749                                 GOTO(out, rc);
750                         mdt_pack_size2body(info, rr->rr_fid2, &lhc->mlh_reg_lh);
751                 } else {
752                         rc = -ENOENT;
753                 }
754         }
755
756 out:
757         mdt_object_put(info->mti_env, o);
758         RETURN(rc);
759 }
760
761 /* lock object for open */
762 static int mdt_object_open_lock(struct mdt_thread_info *info,
763                                 struct mdt_object *obj,
764                                 struct mdt_lock_handle *lhc,
765                                 __u64 *ibits)
766 {
767         struct md_attr *ma = &info->mti_attr;
768         __u64 open_flags = info->mti_spec.sp_cr_flags;
769         __u64 trybits = 0;
770         enum ldlm_mode lm = LCK_CR;
771         bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
772         bool try_layout = false;
773         bool create_layout = false;
774         int rc = 0;
775         __u32 dom_stripe = 0;
776         unsigned int dom_only = 0;
777         unsigned int dom_lock = 0;
778
779         ENTRY;
780
781         *ibits = 0;
782         mdt_lock_handle_init(lhc);
783
784         if (req_is_replay(mdt_info_req(info)))
785                 RETURN(0);
786
787         if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
788                 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
789                     md_should_create(open_flags))
790                         create_layout = true;
791                 if (exp_connect_layout(info->mti_exp) && !create_layout &&
792                     ma->ma_need & MA_LOV)
793                         try_layout = true;
794
795                 /* DoM files can take IO lock at OPEN when it makes sense,
796                  * check if file has DoM stripe and ask for lock if client
797                  * no lock on that resource yet.
798                  */
799                 if (ma->ma_valid & MA_LOV && ma->ma_lmm != NULL)
800                         dom_stripe = mdt_lmm_dom_entry_check(ma->ma_lmm,
801                                                              &dom_only);
802                 /* If only DOM stripe is being used then we can expect IO
803                  * to it after OPEN and will return corresponding DOM ibit
804                  * using default strategy from mdt_opts.mo_dom_lock.
805                  * Otherwise trylock mode is used always and DOM ibit will
806                  * be returned optionally.
807                  */
808                 if (dom_stripe &&
809                     !mdt_dom_client_has_lock(info, mdt_object_fid(obj)))
810                         dom_lock = !dom_only ? TRYLOCK_DOM_ON_OPEN :
811                                    info->mti_mdt->mdt_opts.mo_dom_lock;
812         }
813
814         if (acq_lease) {
815                 /* lease open, acquire write mode of open sem */
816                 down_write(&obj->mot_open_sem);
817
818                 /* Lease exists and ask for new lease */
819                 if (atomic_read(&obj->mot_lease_count) > 0) {
820                         /* only exclusive open is supported, so lease
821                          * are conflicted to each other */
822                         GOTO(out, rc = -EBUSY);
823                 }
824
825                 /* Lease must be with open lock */
826                 if (!(open_flags & MDS_OPEN_LOCK)) {
827                         CERROR("%s: Request lease for file:"DFID ", but open lock "
828                                "is missed, open_flags = %#llo : rc = %d\n",
829                                mdt_obd_name(info->mti_mdt),
830                                PFID(mdt_object_fid(obj)), open_flags, -EPROTO);
831                         GOTO(out, rc = -EPROTO);
832                 }
833
834                 /* should conflict with new opens for write/execute */
835                 lm = LCK_PW;
836                 *ibits = MDS_INODELOCK_OPEN;
837
838                 /* never grant LCK_EX layout lock to client */
839                 try_layout = false;
840         } else { /* normal open */
841                 /* normal open holds read mode of open sem */
842                 down_read(&obj->mot_open_sem);
843
844                 if (open_flags & MDS_OPEN_LOCK) {
845                         if (open_flags & MDS_FMODE_WRITE)
846                                 lm = LCK_CW;
847                         else if (open_flags & MDS_FMODE_EXEC)
848                                 lm = LCK_PR;
849                         else
850                                 lm = LCK_CR;
851
852                         *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
853                 } else if (atomic_read(&obj->mot_lease_count) > 0) {
854                         if (open_flags & MDS_FMODE_WRITE)
855                                 lm = LCK_CW;
856                         else
857                                 lm = LCK_CR;
858
859                         /* revoke lease */
860                         *ibits = MDS_INODELOCK_OPEN;
861                         try_layout = false;
862
863                         lhc = &info->mti_lh[MDT_LH_LOCAL];
864                 } else if (dom_lock) {
865                         lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR;
866                         trybits |= MDS_INODELOCK_DOM | MDS_INODELOCK_LAYOUT;
867                 }
868
869                 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
870                         PFID(mdt_object_fid(obj)),
871                         atomic_read(&obj->mot_lease_count), lm);
872         }
873
874         mdt_lock_reg_init(lhc, lm);
875
876         /* Return lookup lock to validate inode at the client side.
877          * This is pretty important otherwise MDT will return layout
878          * lock for each open.
879          * However this is a double-edged sword because changing
880          * permission will revoke a huge number of LOOKUP locks.
881          */
882         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
883                 if (!(*ibits & MDS_INODELOCK_LOOKUP))
884                         trybits |= MDS_INODELOCK_LOOKUP;
885                 trybits |= MDS_INODELOCK_LAYOUT;
886         }
887
888         if (*ibits | trybits)
889                 rc = mdt_object_lock_try(info, obj, lhc, ibits, trybits, false);
890
891         CDEBUG(D_INODE, "%s: Requested bits lock:"DFID ", ibits = %#llx/%#llx"
892                ", open_flags = %#llo, try_layout = %d : rc = %d\n",
893                mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(obj)),
894                *ibits, trybits, open_flags, try_layout, rc);
895
896         /* will change layout, revoke layout locks by enqueuing EX lock. */
897         if (rc == 0 && create_layout) {
898                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
899
900                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
901                         ", open_flags = %#llo\n",
902                         PFID(mdt_object_fid(obj)), open_flags);
903
904                 /* We cannot enqueue another lock for the same resource we
905                  * already have a lock for, due to mechanics of waiting list
906                  * iterating in ldlm, see LU-3601.
907                  * As such we'll drop the open lock we just got above here,
908                  * it's ok not to have this open lock as it's main purpose is to
909                  * flush unused cached client open handles. */
910                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
911                         mdt_object_unlock(info, obj, lhc, 1);
912
913                 LASSERT(!try_layout);
914                 mdt_lock_handle_init(ll);
915                 mdt_lock_reg_init(ll, LCK_EX);
916                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT);
917
918                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
919         }
920
921         /* Check if there is any other open handles after acquiring
922          * open lock. At this point, caching open handles have been revoked
923          * by open lock.
924          * XXX: Now only exclusive open is supported. Need to check the
925          * type of open for generic lease support. */
926         if (rc == 0 && acq_lease) {
927                 struct ptlrpc_request *req = mdt_info_req(info);
928                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
929                 struct mdt_file_data *mfd;
930                 bool is_replay_or_resent;
931                 int open_count = 0;
932
933                 /* For lease: application can open a file and then apply lease,
934                  * @handle contains original open handle in that case.
935                  * In recovery, open REQ will be replayed and the lease REQ may
936                  * be resent that means the open handle is already stale, so we
937                  * need to fix it up here by finding new handle. */
938                 is_replay_or_resent = req_is_replay(req) ||
939                         lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
940
941                 /* if the request is _not_ a replay request, rr_open_handle
942                  * may be used to hold an open file handle which is issuing the
943                  * lease request, so that this openhandle doesn't count. */
944                 mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
945                                           is_replay_or_resent);
946                 if (mfd != NULL)
947                         ++open_count;
948
949                 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
950                         PFID(mdt_object_fid(obj)),
951                         atomic_read(&obj->mot_open_count), open_count);
952
953                 if (atomic_read(&obj->mot_open_count) > open_count) {
954                         /* fail if anyone *else* has opened file for write */
955                         if (mdt_write_read(obj) > 1)
956                                 GOTO(out, rc = -EBUSY);
957                 }
958         }
959         GOTO(out, rc);
960
961 out:
962         RETURN(rc);
963 }
964
965 static void mdt_object_open_unlock(struct mdt_thread_info *info,
966                                    struct mdt_object *obj,
967                                    struct mdt_lock_handle *lhc,
968                                    __u64 ibits, int rc)
969 {
970         __u64 open_flags = info->mti_spec.sp_cr_flags;
971         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
972         ENTRY;
973
974         if (req_is_replay(mdt_info_req(info)))
975                 RETURN_EXIT;
976
977         /* Release local lock - the lock put in MDT_LH_LOCAL will never
978          * return to client side. */
979         if (lustre_handle_is_used(&ll->mlh_reg_lh))
980                 mdt_object_unlock(info, obj, ll, 1);
981
982         ll = &info->mti_lh[MDT_LH_LAYOUT];
983         /* Release local layout lock, layout was created */
984         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
985                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
986                 mdt_object_unlock(info, obj, ll, 1);
987         }
988
989         if (open_flags & MDS_OPEN_LEASE)
990                 up_write(&obj->mot_open_sem);
991         else
992                 up_read(&obj->mot_open_sem);
993
994         /* Cross-ref case, the lock should be returned to the client */
995         if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
996                 RETURN_EXIT;
997
998         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT) &&
999             !(ibits & MDS_INODELOCK_DOM)) {
1000                 /* for the open request, the lock will only return to client
1001                  * if open or layout lock is granted. */
1002                 rc = 1;
1003         }
1004
1005         if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1006                 struct ldlm_reply       *ldlm_rep;
1007
1008                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1009                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1010                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1011                         mdt_object_unlock(info, obj, lhc, 1);
1012         }
1013         RETURN_EXIT;
1014 }
1015
1016 /**
1017  * Check release is permitted for the current HSM flags.
1018  */
1019 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1020 {
1021         if (!(ma->ma_valid & MA_HSM))
1022                 return false;
1023
1024         if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1025                 return false;
1026
1027         if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1028                 return false;
1029
1030         return true;
1031 }
1032
1033 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1034                                 struct ldlm_reply *rep,
1035                                 struct mdt_lock_handle *lhc)
1036 {
1037         const struct lu_env *env = info->mti_env;
1038         struct mdt_device *mdt = info->mti_mdt;
1039         u64 open_flags = info->mti_spec.sp_cr_flags;
1040         struct mdt_reint_record *rr = &info->mti_rr;
1041         struct md_attr *ma = &info->mti_attr;
1042         struct mdt_object *parent = NULL;
1043         struct mdt_object *o;
1044         bool object_locked = false;
1045         u64 ibits = 0;
1046         int rc;
1047
1048         ENTRY;
1049         if (md_should_create(open_flags)) {
1050                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1051                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1052                         if (IS_ERR(parent)) {
1053                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1054                                        " for anonymous created %ld, try to"
1055                                        " use server-side parent.\n",
1056                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1057                                 parent = NULL;
1058                         }
1059                 }
1060                 if (parent == NULL)
1061                         ma->ma_need |= MA_PFID;
1062         }
1063
1064         o = mdt_object_find(env, mdt, rr->rr_fid2);
1065         if (IS_ERR(o))
1066                 GOTO(out_parent_put, rc = PTR_ERR(o));
1067
1068         if (mdt_object_remote(o)) {
1069                 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1070                        mdt_obd_name(info->mti_mdt),
1071                        PFID(rr->rr_fid2));
1072                 GOTO(out, rc = -EREMOTE);
1073         } else if (!mdt_object_exists(o)) {
1074                 mdt_set_disposition(info, rep,
1075                                     DISP_IT_EXECD |
1076                                     DISP_LOOKUP_EXECD |
1077                                     DISP_LOOKUP_NEG);
1078                 GOTO(out, rc = -ENOENT);
1079         }
1080
1081         mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1082
1083         mdt_prep_ma_buf_from_rep(info, o, ma);
1084         if (open_flags & MDS_OPEN_RELEASE)
1085                 ma->ma_need |= MA_HSM;
1086         rc = mdt_attr_get_complex(info, o, ma);
1087         if (rc)
1088                 GOTO(out, rc);
1089
1090         /* We should not change file's existing LOV EA */
1091         if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
1092             open_flags & MDS_OPEN_HAS_EA && ma->ma_valid & MA_LOV)
1093                 GOTO(out, rc = -EEXIST);
1094
1095         /* If a release request, check file open flags are fine and ask for an
1096          * exclusive open access. */
1097         if (open_flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1098                 GOTO(out, rc = -EPERM);
1099
1100         rc = mdt_check_resent_lock(info, o, lhc);
1101         if (rc < 0) {
1102                 GOTO(out, rc);
1103         } else if (rc > 0) {
1104                 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1105                 object_locked = true;
1106                 if (rc)
1107                         GOTO(out_unlock, rc);
1108         }
1109
1110         if (ma->ma_valid & MA_PFID) {
1111                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1112                 if (IS_ERR(parent)) {
1113                         CDEBUG(D_INODE, "Fail to find parent "DFID
1114                                " for anonymous created %ld, try to"
1115                                " use system default.\n",
1116                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1117                         parent = NULL;
1118                 }
1119         }
1120
1121         tgt_open_obj_set(info->mti_env, mdt_obj2dt(o));
1122         rc = mdt_finish_open(info, parent, o, open_flags, rep);
1123         if (!rc) {
1124                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1125                 if (open_flags & MDS_OPEN_LOCK)
1126                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1127                 if (open_flags & MDS_OPEN_LEASE)
1128                         mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1129         }
1130         GOTO(out_unlock, rc);
1131
1132 out_unlock:
1133         if (object_locked)
1134                 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1135 out:
1136         mdt_object_put(env, o);
1137         if (rc == 0)
1138                 mdt_pack_size2body(info, rr->rr_fid2, &lhc->mlh_reg_lh);
1139 out_parent_put:
1140         if (parent != NULL)
1141                 mdt_object_put(env, parent);
1142         return rc;
1143 }
1144
1145 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1146 static int mdt_cross_open(struct mdt_thread_info *info,
1147                           const struct lu_fid *parent_fid,
1148                           const struct lu_fid *fid,
1149                           struct ldlm_reply *rep, u64 open_flags)
1150 {
1151         struct md_attr *ma = &info->mti_attr;
1152         struct mdt_object *o;
1153         int rc;
1154         ENTRY;
1155
1156         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1157         if (IS_ERR(o))
1158                 RETURN(rc = PTR_ERR(o));
1159
1160         if (mdt_object_remote(o)) {
1161                 /* Something is wrong here, the object is on another MDS! */
1162                 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1163                        mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1164                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1165                                 &o->mot_obj,
1166                                 "Object isn't on this server! FLD error?");
1167                 rc = -EFAULT;
1168         } else {
1169                 if (mdt_object_exists(o)) {
1170                         int mask;
1171
1172                         /* Do permission check for cross-open after converting
1173                          * MDS_OPEN_* flags to MAY_* permission mask.
1174                          */
1175                         mask = mds_accmode(open_flags);
1176
1177                         rc = mo_permission(info->mti_env, NULL,
1178                                            mdt_object_child(o), NULL, mask);
1179                         if (rc)
1180                                 goto out;
1181
1182                         mdt_prep_ma_buf_from_rep(info, o, ma);
1183                         rc = mdt_attr_get_complex(info, o, ma);
1184                         if (rc != 0)
1185                                 GOTO(out, rc);
1186
1187                         rc = mdt_pack_secctx_in_reply(info, o);
1188                         if (unlikely(rc))
1189                                 GOTO(out, rc);
1190
1191                         rc = mdt_pack_encctx_in_reply(info, o);
1192                         if (unlikely(rc))
1193                                 GOTO(out, rc);
1194
1195                         rc = mdt_finish_open(info, NULL, o, open_flags, rep);
1196                 } else {
1197                         /*
1198                          * Something is wrong here. lookup was positive but
1199                          * there is no object!
1200                          */
1201                         CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1202                               mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1203                         rc = -EFAULT;
1204                 }
1205         }
1206 out:
1207         mdt_object_put(info->mti_env, o);
1208
1209         RETURN(rc);
1210 }
1211
1212 /*
1213  * find root object and take its xattr lock if it's on remote MDT, later create
1214  * may use fs default striping (which is stored in root xattr).
1215  */
1216 static int mdt_lock_root_xattr(struct mdt_thread_info *info,
1217                                struct mdt_device *mdt)
1218 {
1219         struct mdt_object *md_root = mdt->mdt_md_root;
1220         struct lustre_handle lhroot;
1221         int rc;
1222
1223         if (md_root == NULL) {
1224                 lu_root_fid(&info->mti_tmp_fid1);
1225                 md_root = mdt_object_find(info->mti_env, mdt,
1226                                           &info->mti_tmp_fid1);
1227                 if (IS_ERR(md_root))
1228                         return PTR_ERR(md_root);
1229
1230                 spin_lock(&mdt->mdt_lock);
1231                 if (mdt->mdt_md_root != NULL) {
1232                         spin_unlock(&mdt->mdt_lock);
1233
1234                         LASSERTF(mdt->mdt_md_root == md_root,
1235                                  "Different root object ("
1236                                  DFID") instances, %p, %p\n",
1237                                  PFID(&info->mti_tmp_fid1),
1238                                  mdt->mdt_md_root, md_root);
1239                         LASSERT(atomic_read(
1240                                 &md_root->mot_obj.lo_header->loh_ref) > 1);
1241
1242                         mdt_object_put(info->mti_env, md_root);
1243                 } else {
1244                         mdt->mdt_md_root = md_root;
1245                         spin_unlock(&mdt->mdt_lock);
1246                 }
1247         }
1248
1249         if (md_root->mot_cache_attr || !mdt_object_remote(md_root))
1250                 return 0;
1251
1252         rc = mdt_remote_object_lock(info, md_root, mdt_object_fid(md_root),
1253                                     &lhroot, LCK_PR, MDS_INODELOCK_XATTR,
1254                                     true);
1255         if (rc < 0)
1256                 return rc;
1257
1258         md_root->mot_cache_attr = 1;
1259
1260         /* don't cancel this lock, so that we know the cached xattr is valid. */
1261         ldlm_lock_decref(&lhroot, LCK_PR);
1262
1263         return 0;
1264 }
1265
1266 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1267 {
1268         struct mdt_device *mdt = info->mti_mdt;
1269         struct ptlrpc_request *req = mdt_info_req(info);
1270         struct mdt_object *parent;
1271         struct mdt_object *child;
1272         struct mdt_lock_handle *lh = NULL;
1273         struct ldlm_reply *ldlm_rep;
1274         struct mdt_body *repbody;
1275         struct lu_fid *child_fid = &info->mti_tmp_fid1;
1276         struct md_attr *ma = &info->mti_attr;
1277         u64 open_flags = info->mti_spec.sp_cr_flags;
1278         u64 ibits = 0;
1279         struct mdt_reint_record *rr = &info->mti_rr;
1280         int result, rc;
1281         int created = 0;
1282         int object_locked = 0;
1283         enum ldlm_mode lock_mode = LCK_PR;
1284         u32 msg_flags;
1285         ktime_t kstart = ktime_get();
1286
1287         ENTRY;
1288         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1289                                (obd_timeout + 1) / 4);
1290
1291         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1292
1293         ma->ma_need = MA_INODE;
1294         ma->ma_valid = 0;
1295
1296         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1297         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1298
1299         if (unlikely(open_flags & MDS_OPEN_JOIN_FILE)) {
1300                 CERROR("file join is not supported anymore.\n");
1301                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1302         }
1303         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1304
1305         if ((open_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1306             info->mti_spec.u.sp_ea.eadata == NULL)
1307                 GOTO(out, result = err_serious(-EINVAL));
1308
1309         if (open_flags & MDS_FMODE_WRITE &&
1310             exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1311                 GOTO(out, result = -EROFS);
1312
1313         CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1314                "cr_flag=%#llo mode=0%06o msg_flag=0x%x\n",
1315                PFID(rr->rr_fid1), PNAME(&rr->rr_name), PFID(rr->rr_fid2),
1316                open_flags, ma->ma_attr.la_mode, msg_flags);
1317
1318         if (info->mti_cross_ref) {
1319                 /* This is cross-ref open */
1320                 mdt_set_disposition(info, ldlm_rep,
1321                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1322                              DISP_LOOKUP_POS));
1323                 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1324                                         ldlm_rep, open_flags);
1325                 GOTO(out, result);
1326         } else if (req_is_replay(req)) {
1327                 result = mdt_open_by_fid(info, ldlm_rep, lhc);
1328
1329                 if (result != -ENOENT)
1330                         GOTO(out, result);
1331
1332                 /* We didn't find the correct object, so we need to re-create it
1333                  * via a regular replay. */
1334                 if (!(open_flags & MDS_OPEN_CREAT)) {
1335                         DEBUG_REQ(D_ERROR, req,
1336                                   "OPEN & CREAT not in open replay/by_fid");
1337                         GOTO(out, result = -EFAULT);
1338                 }
1339                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1340         } else if (open_flags & MDS_OPEN_BY_FID) {
1341                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1342                 if (result < 0)
1343                         CDEBUG(D_INFO, "no object for "DFID": %d\n",
1344                                PFID(rr->rr_fid2), result);
1345                 GOTO(out, result);
1346         }
1347
1348         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1349                 GOTO(out, result = err_serious(-ENOMEM));
1350
1351         mdt_set_disposition(info, ldlm_rep,
1352                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1353
1354         if (!lu_name_is_valid(&rr->rr_name))
1355                 GOTO(out, result = -EPROTO);
1356
1357         result = mdt_lock_root_xattr(info, mdt);
1358         if (result < 0)
1359                 GOTO(out, result);
1360
1361         parent = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
1362         if (IS_ERR(parent))
1363                 GOTO(out, result = PTR_ERR(parent));
1364
1365         /* get and check version of parent */
1366         result = mdt_version_get_check(info, parent, 0);
1367         if (result) {
1368                 mdt_object_put(info->mti_env, parent);
1369                 GOTO(out, result);
1370         }
1371
1372         OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
1373 again_pw:
1374         fid_zero(child_fid);
1375
1376         if (open_flags & MDS_OPEN_VOLATILE) {
1377                 lh = NULL;
1378                 result = -ENOENT;
1379         } else {
1380                 lh = &info->mti_lh[MDT_LH_PARENT];
1381                 mdt_lock_pdo_init(lh, lock_mode, &rr->rr_name);
1382                 result = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
1383                 if (result != 0) {
1384                         mdt_object_put(info->mti_env, parent);
1385                         GOTO(out, result);
1386                 }
1387
1388                 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1389                                     &rr->rr_name, child_fid, &info->mti_spec);
1390         }
1391
1392         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1393                  "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1394                  PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1395                  PFID(child_fid));
1396
1397         if (result != 0 && result != -ENOENT)
1398                 GOTO(out_parent, result);
1399
1400         OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
1401
1402         if (result == -ENOENT) {
1403                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1404                 if (!(open_flags & MDS_OPEN_CREAT))
1405                         GOTO(out_parent, result);
1406                 if (mdt_rdonly(req->rq_export))
1407                         GOTO(out_parent, result = -EROFS);
1408
1409                 LASSERT(equi(lh == NULL, open_flags & MDS_OPEN_VOLATILE));
1410
1411                 if (lh != NULL && lock_mode == LCK_PR) {
1412                         /* first pass: get write lock and restart */
1413                         mdt_object_unlock(info, parent, lh, 1);
1414                         mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1415                         mdt_lock_handle_init(lh);
1416                         lock_mode = LCK_PW;
1417                         goto again_pw;
1418                 }
1419
1420                 *child_fid = *info->mti_rr.rr_fid2;
1421                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1422                          PFID(child_fid));
1423                 /* In the function below, .hs_keycmp resolves to
1424                  * lu_obj_hop_keycmp() */
1425                 /* coverity[overrun-buffer-val] */
1426                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1427         } else {
1428                 /*
1429                  * Check for O_EXCL is moved to the mdt_finish_open(),
1430                  * we need to return FID back in that case.
1431                  */
1432                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1433                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1434         }
1435         if (IS_ERR(child))
1436                 GOTO(out_parent, result = PTR_ERR(child));
1437
1438         /** check version of child  */
1439         rc = mdt_version_get_check(info, child, 1);
1440         if (rc)
1441                 GOTO(out_child, result = rc);
1442
1443         tgt_open_obj_set(info->mti_env, mdt_obj2dt(child));
1444
1445         if (result == -ENOENT) {
1446                 /* Create under OBF and .lustre is not permitted */
1447                 if (!fid_is_md_operative(rr->rr_fid1) &&
1448                     (open_flags & MDS_OPEN_VOLATILE) == 0)
1449                         GOTO(out_child, result = -EPERM);
1450
1451                 /* save versions in reply */
1452                 mdt_version_get_save(info, parent, 0);
1453                 mdt_version_get_save(info, child, 1);
1454
1455                 /* version of child will be changed */
1456                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1457
1458                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1459                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1460
1461                 /* Don't do lookup sanity check. We know name doesn't exist. */
1462                 info->mti_spec.sp_cr_lookup = 0;
1463                 info->mti_spec.sp_feat = &dt_directory_features;
1464
1465                 result = mdo_create(info->mti_env, mdt_object_child(parent),
1466                                     &rr->rr_name, mdt_object_child(child),
1467                                     &info->mti_spec, &info->mti_attr);
1468                 if (result == -ERESTART) {
1469                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1470                         GOTO(out_child, result);
1471                 } else {
1472                         mdt_prep_ma_buf_from_rep(info, child, ma);
1473                         /* XXX: we should call this once, see few lines below */
1474                         if (result == 0)
1475                                 result = mdt_attr_get_complex(info, child, ma);
1476
1477                         if (result != 0)
1478                                 GOTO(out_child, result);
1479                 }
1480                 created = 1;
1481                 mdt_counter_incr(req, LPROC_MDT_MKNOD,
1482                                  ktime_us_delta(ktime_get(), kstart));
1483         } else {
1484                 /*
1485                  * The object is on remote node, return its FID for remote open.
1486                  */
1487                 if (mdt_object_remote(child)) {
1488                         /*
1489                          * Check if this lock already was sent to client and
1490                          * this is resent case. For resent case do not take lock
1491                          * again, use what is already granted.
1492                          */
1493                         LASSERT(lhc != NULL);
1494
1495                         rc = mdt_check_resent_lock(info, child, lhc);
1496                         if (rc < 0) {
1497                                 GOTO(out_child, result = rc);
1498                         } else if (rc > 0) {
1499                                 mdt_lock_handle_init(lhc);
1500                                 mdt_lock_reg_init(lhc, LCK_PR);
1501
1502                                 rc = mdt_object_lock(info, child, lhc,
1503                                                      MDS_INODELOCK_LOOKUP);
1504                         }
1505                         repbody->mbo_fid1 = *mdt_object_fid(child);
1506                         repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1507                         if (rc != 0)
1508                                 result = rc;
1509                         else
1510                                 result = -MDT_EREMOTE_OPEN;
1511                         GOTO(out_child, result);
1512                 } else if (mdt_object_exists(child)) {
1513                         /* Check early for MDS_OPEN_DIRECTORY/O_DIRECTORY to
1514                          * avoid opening regular files from lfs getstripe
1515                          * since doing so breaks the leases used by lfs
1516                          * mirror. See LU-13693. */
1517                         if (open_flags & MDS_OPEN_DIRECTORY &&
1518                             S_ISREG(lu_object_attr(&child->mot_obj)))
1519                                 GOTO(out_child, result = -ENOTDIR);
1520
1521                         /* We have to get attr & LOV EA & HSM for this
1522                          * object. */
1523                         mdt_prep_ma_buf_from_rep(info, child, ma);
1524                         ma->ma_need |= MA_HSM;
1525                         result = mdt_attr_get_complex(info, child, ma);
1526                         if (result != 0)
1527                                 GOTO(out_child, result);
1528                 } else {
1529                         /* Object does not exist. Likely FS corruption. */
1530                         CERROR("%s: name '"DNAME"' present, but FID "
1531                                DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1532                                PNAME(&rr->rr_name), PFID(child_fid));
1533                         GOTO(out_child, result = -EIO);
1534                 }
1535         }
1536
1537         repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1538         repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1539
1540         rc = mdt_pack_secctx_in_reply(info, child);
1541         if (unlikely(rc))
1542                 GOTO(out_child, result = rc);
1543
1544         rc = mdt_pack_encctx_in_reply(info, child);
1545         if (unlikely(rc))
1546                 GOTO(out_child, result = rc);
1547
1548         rc = mdt_check_resent_lock(info, child, lhc);
1549         if (rc < 0) {
1550                 GOTO(out_child, result = rc);
1551         } else if (rc == 0) {
1552                 /* the open lock might already be gotten in
1553                  * ldlm_handle_enqueue() */
1554                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1555                 if (open_flags & MDS_OPEN_LOCK)
1556                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1557         } else {
1558                 /* get openlock if this isn't replay and client requested it */
1559                 if (!req_is_replay(req)) {
1560                         rc = mdt_object_open_lock(info, child, lhc, &ibits);
1561                         object_locked = 1;
1562                         if (rc != 0)
1563                                 GOTO(out_child_unlock, result = rc);
1564                         else if (open_flags & MDS_OPEN_LOCK)
1565                                 mdt_set_disposition(info, ldlm_rep,
1566                                                     DISP_OPEN_LOCK);
1567                 }
1568         }
1569         /* Try to open it now. */
1570         rc = mdt_finish_open(info, parent, child, open_flags, ldlm_rep);
1571         if (rc) {
1572                 result = rc;
1573                 /* openlock will be released if mdt_finish_open() failed */
1574                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1575
1576                 if (created && (open_flags & MDS_OPEN_VOLATILE)) {
1577                         CERROR("%s: cannot open volatile file "DFID", orphan "
1578                                "file will be left in PENDING directory until "
1579                                "next reboot, rc = %d\n", mdt_obd_name(mdt),
1580                                PFID(mdt_object_fid(child)), rc);
1581                         GOTO(out_child_unlock, result);
1582                 }
1583
1584                 if (created) {
1585                         ma->ma_need = 0;
1586                         ma->ma_valid = 0;
1587                         rc = mdo_unlink(info->mti_env,
1588                                         mdt_object_child(parent),
1589                                         mdt_object_child(child),
1590                                         &rr->rr_name,
1591                                         &info->mti_attr, 0);
1592                         if (rc != 0)
1593                                 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1594                                        mdt_obd_name(info->mti_mdt),
1595                                        PFID(mdt_object_fid(child)), rc);
1596                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1597                 }
1598         }
1599
1600         mdt_counter_incr(req, LPROC_MDT_OPEN,
1601                          ktime_us_delta(ktime_get(), kstart));
1602
1603         EXIT;
1604 out_child_unlock:
1605         if (object_locked)
1606                 mdt_object_open_unlock(info, child, lhc, ibits, result);
1607 out_child:
1608         mdt_object_put(info->mti_env, child);
1609         if (result == 0)
1610                 mdt_pack_size2body(info, child_fid, &lhc->mlh_reg_lh);
1611 out_parent:
1612         if (lh != NULL)
1613                 mdt_object_unlock(info, parent, lh, result || !created);
1614
1615         mdt_object_put(info->mti_env, parent);
1616 out:
1617         if (result)
1618                 lustre_msg_set_transno(req->rq_repmsg, 0);
1619         return result;
1620 }
1621
1622 /**
1623  * Create an orphan object use local root.
1624  */
1625 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1626                                           struct mdt_device *mdt,
1627                                           const struct lu_fid *fid,
1628                                           struct md_attr *attr, fmode_t fmode)
1629 {
1630         const struct lu_env *env = info->mti_env;
1631         struct md_op_spec *spec = &info->mti_spec;
1632         struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1633         struct mdt_object *obj = NULL;
1634         struct mdt_object *local_root;
1635         static const struct lu_name lname = {
1636                 .ln_name = "i_am_nobody",
1637                 .ln_namelen = sizeof("i_am_nobody") - 1,
1638         };
1639         struct lu_ucred *uc;
1640         kernel_cap_t uc_cap_save;
1641         int rc;
1642         ENTRY;
1643
1644         rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1645         if (rc != 0)
1646                 RETURN(ERR_PTR(rc));
1647
1648         local_root = mdt_object_find(env, mdt, local_root_fid);
1649         if (IS_ERR(local_root))
1650                 RETURN(local_root);
1651
1652         obj = mdt_object_new(env, mdt, fid);
1653         if (IS_ERR(obj))
1654                 GOTO(out, rc = PTR_ERR(obj));
1655
1656         spec->sp_cr_lookup = 0;
1657         spec->sp_feat = &dt_directory_features;
1658         spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1659         if (attr->ma_valid & MA_LOV) {
1660                 spec->u.sp_ea.eadata = attr->ma_lmm;
1661                 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1662                 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1663         } else {
1664                 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1665         }
1666
1667         uc = lu_ucred(env);
1668         uc_cap_save = uc->uc_cap;
1669         cap_raise(uc->uc_cap, CAP_DAC_OVERRIDE);
1670         rc = mdo_create(env, mdt_object_child(local_root), &lname,
1671                         mdt_object_child(obj), spec, attr);
1672         uc->uc_cap = uc_cap_save;
1673         if (rc < 0) {
1674                 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1675                        mdt_obd_name(mdt), PFID(fid), rc);
1676                 GOTO(out, rc);
1677         }
1678
1679         rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED, spec);
1680         if (rc < 0)
1681                 CERROR("%s: cannot open volatile file "DFID", orphan "
1682                        "file will be left in PENDING directory until "
1683                        "next reboot, rc = %d\n", mdt_obd_name(mdt),
1684                        PFID(fid), rc);
1685         GOTO(out, rc);
1686
1687 out:
1688         if (rc < 0) {
1689                 if (!IS_ERR(obj))
1690                         mdt_object_put(env, obj);
1691                 obj = ERR_PTR(rc);
1692         }
1693         mdt_object_put(env, local_root);
1694         return obj;
1695 }
1696
1697 /* XXX Look into layout in MDT layer. */
1698 static inline int mdt_hsm_set_released(struct lov_mds_md *lmm)
1699 {
1700         struct lov_comp_md_v1   *comp_v1;
1701         struct lov_mds_md       *v1;
1702         __u32   off;
1703         int     i;
1704
1705         if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1_DEFINED)) {
1706                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1707
1708                 if (comp_v1->lcm_entry_count == 0)
1709                         return -EINVAL;
1710
1711                 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
1712                         off = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1713                         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1714                         v1->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1715                 }
1716         } else {
1717                 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1718         }
1719         return 0;
1720 }
1721
1722 static inline int mdt_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
1723 {
1724         struct lov_comp_md_v1 *comp_v1;
1725
1726         if (le32_to_cpu(lmm->lmm_magic == LOV_MAGIC_COMP_V1)) {
1727                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1728                 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
1729         } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
1730                    le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
1731                 *gen = le16_to_cpu(lmm->lmm_layout_gen);
1732         } else {
1733                 return -EINVAL;
1734         }
1735         return 0;
1736 }
1737
1738 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1739                            struct md_attr *ma)
1740 {
1741         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1742         struct lu_ucred        *uc = mdt_ucred(info);
1743         struct close_data      *data;
1744         struct ldlm_lock       *lease;
1745         struct mdt_object      *orphan;
1746         struct md_attr         *orp_ma;
1747         struct lu_buf          *buf;
1748         kernel_cap_t cap;
1749         bool                    lease_broken;
1750         int                     rc;
1751         int                     rc2;
1752         ENTRY;
1753
1754         if (mdt_rdonly(info->mti_exp))
1755                 RETURN(-EROFS);
1756
1757         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1758         if (data == NULL)
1759                 RETURN(-EPROTO);
1760
1761         lease = ldlm_handle2lock(&data->cd_handle);
1762         if (lease == NULL)
1763                 RETURN(-ESTALE);
1764
1765         /* try to hold open_sem so that nobody else can open the file */
1766         if (!down_write_trylock(&o->mot_open_sem)) {
1767                 ldlm_lock_cancel(lease);
1768                 GOTO(out_reprocess, rc = -EBUSY);
1769         }
1770
1771         /* Check if the lease open lease has already canceled */
1772         lock_res_and_lock(lease);
1773         lease_broken = ldlm_is_cancel(lease);
1774         unlock_res_and_lock(lease);
1775
1776         LDLM_DEBUG(lease, DFID " lease broken? %d",
1777                    PFID(mdt_object_fid(o)), lease_broken);
1778
1779         /* Cancel server side lease. Client side counterpart should
1780          * have been cancelled. It's okay to cancel it now as we've
1781          * held mot_open_sem. */
1782         ldlm_lock_cancel(lease);
1783
1784         if (lease_broken) /* don't perform release task */
1785                 GOTO(out_unlock, rc = -ESTALE);
1786
1787         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1788                 GOTO(out_unlock, rc = -EINVAL);
1789
1790         /* ma_need was set before but it seems fine to change it in order to
1791          * avoid modifying the one from RPC */
1792         ma->ma_need = MA_HSM;
1793         rc = mdt_attr_get_complex(info, o, ma);
1794         if (rc != 0)
1795                 GOTO(out_unlock, rc);
1796
1797         if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
1798                 if (ma->ma_valid & MA_HSM) {
1799                         if (ma->ma_hsm.mh_flags & HS_RELEASED)
1800                                 GOTO(out_unlock, rc = -EALREADY);
1801
1802                         if (ma->ma_hsm.mh_arch_id != data->cd_archive_id)
1803                                 CDEBUG(D_CACHE,
1804                                        DFID" archive id diff: %llu:%u\n",
1805                                        PFID(mdt_object_fid(o)),
1806                                        ma->ma_hsm.mh_arch_id,
1807                                        data->cd_archive_id);
1808
1809                         if (!(ma->ma_hsm.mh_flags & HS_DIRTY) &&
1810                             ma->ma_hsm.mh_arch_ver == data->cd_data_version) {
1811                                 CDEBUG(D_CACHE,
1812                                        DFID" data version matches: packed=%llu "
1813                                        "and on-disk=%llu\n",
1814                                        PFID(mdt_object_fid(o)),
1815                                        data->cd_data_version,
1816                                        ma->ma_hsm.mh_arch_ver);
1817                                 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1818                         }
1819
1820                         if (ma->ma_hsm.mh_flags & HS_DIRTY)
1821                                 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1822                 } else {
1823                         /* Set up HSM attribte for PCC archived object */
1824                         BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1825                                      sizeof(info->mti_xattr_buf));
1826                         buf = &info->mti_buf;
1827                         buf->lb_buf = info->mti_xattr_buf;
1828                         buf->lb_len = sizeof(struct hsm_attrs);
1829                         memset(&ma->ma_hsm, 0, sizeof(ma->ma_hsm));
1830                         ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1831                         ma->ma_hsm.mh_arch_id = data->cd_archive_id;
1832                         ma->ma_hsm.mh_arch_ver = data->cd_data_version;
1833                         lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1834
1835                         rc = mo_xattr_set(info->mti_env, mdt_object_child(o),
1836                                           buf, XATTR_NAME_HSM, 0);
1837                         if (rc)
1838                                 GOTO(out_unlock, rc);
1839                 }
1840         } else {
1841                 if (!mdt_hsm_release_allow(ma))
1842                         GOTO(out_unlock, rc = -EPERM);
1843
1844                 /* already released? */
1845                 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1846                         GOTO(out_unlock, rc = 0);
1847
1848                 /* Compare on-disk and packed data_version */
1849                 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
1850                         CDEBUG(D_HSM, DFID" data_version mismatches: "
1851                                "packed=%llu and on-disk=%llu\n",
1852                                PFID(mdt_object_fid(o)),
1853                                data->cd_data_version,
1854                                ma->ma_hsm.mh_arch_ver);
1855                         GOTO(out_unlock, rc = -EPERM);
1856                 }
1857         }
1858
1859         ma->ma_valid = MA_INODE;
1860         ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
1861         rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
1862         if (rc < 0)
1863                 GOTO(out_unlock, rc);
1864
1865         mutex_lock(&o->mot_som_mutex);
1866         rc2 = mdt_set_som(info, o, SOM_FL_STRICT, ma->ma_attr.la_size,
1867                            ma->ma_attr.la_blocks);
1868         mutex_unlock(&o->mot_som_mutex);
1869         if (rc2 < 0)
1870                 CDEBUG(D_INODE,
1871                        "%s: File "DFID" SOM update failed: rc = %d\n",
1872                        mdt_obd_name(info->mti_mdt),
1873                        PFID(mdt_object_fid(o)), rc2);
1874
1875
1876         ma->ma_need = MA_INODE | MA_LOV;
1877         rc = mdt_attr_get_complex(info, o, ma);
1878         if (rc < 0)
1879                 GOTO(out_unlock, rc);
1880
1881         if (!(ma->ma_valid & MA_LOV)) {
1882                 /* Even empty file are released */
1883                 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
1884                 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEFINED);
1885                 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1886                 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
1887                 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
1888         } else {
1889                 /* Magic must be LOV_MAGIC_*_DEFINED or LOD will interpret
1890                  * ma_lmm as lov_user_md, then it will be confused by union of
1891                  * layout_gen and stripe_offset. */
1892                 if ((le32_to_cpu(ma->ma_lmm->lmm_magic) & LOV_MAGIC_MASK) ==
1893                     LOV_MAGIC_MAGIC)
1894                         ma->ma_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
1895                 else
1896                         GOTO(out_unlock, rc = -EINVAL);
1897         }
1898
1899         /* Set file as released. */
1900         rc = mdt_hsm_set_released(ma->ma_lmm);
1901         if (rc)
1902                 GOTO(out_unlock, rc);
1903
1904         orp_ma = &info->mti_u.hsm.attr;
1905         orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
1906         /* We use root ownership to bypass potential quota
1907          * restrictions on the user and group of the file. */
1908         orp_ma->ma_attr.la_uid = 0;
1909         orp_ma->ma_attr.la_gid = 0;
1910         orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
1911         orp_ma->ma_lmm = ma->ma_lmm;
1912         orp_ma->ma_lmm_size = ma->ma_lmm_size;
1913         orp_ma->ma_valid = MA_INODE | MA_LOV;
1914         orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
1915                                  MDS_FMODE_WRITE);
1916         if (IS_ERR(orphan)) {
1917                 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
1918                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
1919                        PTR_ERR(orphan));
1920                 GOTO(out_unlock, rc = PTR_ERR(orphan));
1921         }
1922
1923         /* Set up HSM attribute for orphan object */
1924         BUILD_BUG_ON(sizeof(struct hsm_attrs) > sizeof(info->mti_xattr_buf));
1925         buf = &info->mti_buf;
1926         buf->lb_buf = info->mti_xattr_buf;
1927         buf->lb_len = sizeof(struct hsm_attrs);
1928         ma->ma_hsm.mh_flags |= HS_RELEASED;
1929         lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1930         ma->ma_hsm.mh_flags &= ~HS_RELEASED;
1931
1932         mdt_lock_reg_init(lh, LCK_EX);
1933         rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
1934                              MDS_INODELOCK_XATTR);
1935         if (rc != 0)
1936                 GOTO(out_close, rc);
1937
1938         /* The orphan has root ownership so we need to raise
1939          * CAP_FOWNER to set the HSM attributes. */
1940         cap = uc->uc_cap;
1941         cap_raise(uc->uc_cap, CAP_FOWNER);
1942         rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
1943                           XATTR_NAME_HSM, 0);
1944         uc->uc_cap = cap;
1945         if (rc != 0)
1946                 GOTO(out_layout_lock, rc);
1947
1948         /* Swap layout with orphan objects. */
1949         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
1950                              mdt_object_child(orphan),
1951                              SWAP_LAYOUTS_MDS_HSM);
1952
1953         if (!rc && ma->ma_attr_flags & MDS_PCC_ATTACH) {
1954                 ma->ma_need = MA_LOV;
1955                 rc = mdt_attr_get_complex(info, o, ma);
1956         }
1957
1958         EXIT;
1959
1960 out_layout_lock:
1961         /* Release exclusive LL */
1962         mdt_object_unlock(info, o, lh, 1);
1963 out_close:
1964         /* Close orphan object anyway */
1965         rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
1966                        MDS_FMODE_WRITE);
1967         if (rc2 < 0)
1968                 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
1969                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
1970         LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
1971                         "object closed");
1972         mdt_object_put(info->mti_env, orphan);
1973
1974 out_unlock:
1975         up_write(&o->mot_open_sem);
1976
1977         /* already released */
1978         if (rc == 0) {
1979                 struct mdt_body *repbody;
1980
1981                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1982                 LASSERT(repbody != NULL);
1983                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1984                 if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
1985                         LASSERT(ma->ma_valid & MA_LOV);
1986                         rc = mdt_get_lmm_gen(ma->ma_lmm,
1987                                              &repbody->mbo_layout_gen);
1988                         if (!rc)
1989                                 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
1990                 }
1991         }
1992
1993 out_reprocess:
1994         ldlm_reprocess_all(lease->l_resource,
1995                            lease->l_policy_data.l_inodebits.bits);
1996         LDLM_LOCK_PUT(lease);
1997
1998         ma->ma_valid = 0;
1999         ma->ma_need = 0;
2000
2001         return rc;
2002 }
2003
2004 int mdt_close_handle_layouts(struct mdt_thread_info *info,
2005                              struct mdt_object *o, struct md_attr *ma)
2006 {
2007         struct mdt_lock_handle  *lh1 = &info->mti_lh[MDT_LH_NEW];
2008         struct mdt_lock_handle  *lh2 = &info->mti_lh[MDT_LH_OLD];
2009         struct close_data       *data;
2010         struct ldlm_lock        *lease;
2011         struct mdt_object       *o1 = o, *o2 = NULL;
2012         bool                     lease_broken;
2013         bool                     swap_objects = false;
2014         int                      rc;
2015         ENTRY;
2016
2017         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2018                 RETURN(-EROFS);
2019
2020         if (!S_ISREG(lu_object_attr(&o1->mot_obj)))
2021                 RETURN(-EINVAL);
2022
2023         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2024         if (data == NULL)
2025                 RETURN(-EPROTO);
2026
2027         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2028                 RETURN(-EINVAL);
2029
2030         rc = lu_fid_cmp(&data->cd_fid, mdt_object_fid(o));
2031         if (rc == 0) {
2032                 /**
2033                  * only MDS_CLOSE_LAYOUT_SPLIT use the same fid to indicate
2034                  * mirror deletion, so we'd zero cd_fid, and keeps o2 be NULL.
2035                  */
2036                 if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT))
2037                         RETURN(-EINVAL);
2038
2039                 /* zero cd_fid to keeps o2 be NULL */
2040                 fid_zero(&data->cd_fid);
2041         } else if (rc < 0) {
2042                 /* Exchange o1 and o2, to enforce locking order */
2043                 swap_objects = true;
2044         }
2045
2046         lease = ldlm_handle2lock(&data->cd_handle);
2047         if (lease == NULL)
2048                 RETURN(-ESTALE);
2049
2050         if (!fid_is_zero(&data->cd_fid)) {
2051                 o2 = mdt_object_find(info->mti_env, info->mti_mdt,
2052                                      &data->cd_fid);
2053                 if (IS_ERR(o2))
2054                         GOTO(out_lease, rc = PTR_ERR(o2));
2055
2056                 if (!mdt_object_exists(o2))
2057                         GOTO(out_obj, rc = -ENOENT);
2058
2059                 if (!S_ISREG(lu_object_attr(&o2->mot_obj)))
2060                         GOTO(out_obj, rc = -EINVAL);
2061
2062                 if (swap_objects)
2063                         swap(o1, o2);
2064         }
2065
2066         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
2067                            MAY_WRITE);
2068         if (rc < 0)
2069                 GOTO(out_obj, rc);
2070
2071         if (o2) {
2072                 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2),
2073                                    NULL, MAY_WRITE);
2074                 if (rc < 0)
2075                         GOTO(out_obj, rc);
2076         }
2077
2078         /* try to hold open_sem so that nobody else can open the file */
2079         if (!down_write_trylock(&o->mot_open_sem)) {
2080                 ldlm_lock_cancel(lease);
2081                 GOTO(out_obj, rc = -EBUSY);
2082         }
2083
2084         /* Check if the lease open lease has already canceled */
2085         lock_res_and_lock(lease);
2086         lease_broken = ldlm_is_cancel(lease);
2087         unlock_res_and_lock(lease);
2088
2089         LDLM_DEBUG(lease, DFID " lease broken? %d",
2090                    PFID(mdt_object_fid(o)), lease_broken);
2091
2092         /* Cancel server side lease. Client side counterpart should
2093          * have been cancelled. It's okay to cancel it now as we've
2094          * held mot_open_sem. */
2095         ldlm_lock_cancel(lease);
2096
2097         if (lease_broken)
2098                 GOTO(out_unlock_sem, rc = -ESTALE);
2099
2100         mdt_lock_reg_init(lh1, LCK_EX);
2101         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
2102                              MDS_INODELOCK_XATTR);
2103         if (rc < 0)
2104                 GOTO(out_unlock_sem, rc);
2105
2106         if (o2) {
2107                 mdt_lock_reg_init(lh2, LCK_EX);
2108                 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
2109                                      MDS_INODELOCK_XATTR);
2110                 if (rc < 0)
2111                         GOTO(out_unlock1, rc);
2112         }
2113
2114         /* Swap layout with orphan object */
2115         if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SWAP) {
2116                 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
2117                                      mdt_object_child(o2), 0);
2118         } else if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_MERGE ||
2119                    ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2120                 struct lu_buf *buf = &info->mti_buf;
2121                 struct md_rejig_data mrd;
2122
2123                 if (o2) {
2124                         mrd.mrd_obj = mdt_object_child(o == o1 ? o2 : o1);
2125                 } else {
2126                         if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT)) {
2127                                 /* paranoid check again */
2128                                 CERROR(DFID
2129                                   ":only mirror split support NULL o2 object\n",
2130                                         PFID(mdt_object_fid(o)));
2131                                 GOTO(out_unlock1, rc = -EINVAL);
2132                         }
2133
2134                         /* set NULL mrd_obj for deleting mirror objects */
2135                         mrd.mrd_obj = NULL;
2136                 }
2137
2138                 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2139                         mrd.mrd_mirror_id = data->cd_mirror_id;
2140                         /* set a small enough blocks in the SoM */
2141                         ma->ma_attr.la_blocks >>= 1;
2142                 }
2143
2144                 buf->lb_len = sizeof(mrd);
2145                 buf->lb_buf = &mrd;
2146                 rc = mo_xattr_set(info->mti_env, mdt_object_child(o), buf,
2147                                   XATTR_LUSTRE_LOV,
2148                                   ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT ?
2149                                   LU_XATTR_SPLIT : LU_XATTR_MERGE);
2150                 if (rc == 0 && ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS |
2151                                                        LA_LSIZE | LA_LBLOCKS)) {
2152                         int rc2;
2153                         enum lustre_som_flags lsf;
2154
2155                         if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS))
2156                                 lsf = SOM_FL_STRICT;
2157                         else
2158                                 lsf = SOM_FL_LAZY;
2159
2160                         mutex_lock(&o->mot_som_mutex);
2161                         rc2 = mdt_set_som(info, o, lsf,
2162                                           ma->ma_attr.la_size,
2163                                           ma->ma_attr.la_blocks);
2164                         mutex_unlock(&o->mot_som_mutex);
2165                         if (rc2 < 0)
2166                                 CERROR(DFID": Setting i_blocks error: %d, "
2167                                        "i_blocks will be reported wrongly and "
2168                                        "can only be fixed in next resync\n",
2169                                        PFID(mdt_object_fid(o)), rc2);
2170                 }
2171         }
2172         if (rc < 0)
2173                 GOTO(out_unlock2, rc);
2174
2175         EXIT;
2176
2177 out_unlock2:
2178         /* Release exclusive LL */
2179         if (o2)
2180                 mdt_object_unlock(info, o2, lh2, 1);
2181
2182 out_unlock1:
2183         mdt_object_unlock(info, o1, lh1, 1);
2184
2185 out_unlock_sem:
2186         up_write(&o->mot_open_sem);
2187
2188         /* already swapped */
2189         if (rc == 0) {
2190                 struct mdt_body *repbody;
2191
2192                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2193                 LASSERT(repbody != NULL);
2194                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2195         }
2196
2197 out_obj:
2198         if (o1 != o)
2199                 /* the 2nd object has been used, and swapped to o1 */
2200                 mdt_object_put(info->mti_env, o1);
2201         else if (o2)
2202                 /* the 2nd object has been used, and not swapped */
2203                 mdt_object_put(info->mti_env, o2);
2204
2205         ldlm_reprocess_all(lease->l_resource,
2206                            lease->l_policy_data.l_inodebits.bits);
2207
2208 out_lease:
2209         LDLM_LOCK_PUT(lease);
2210
2211         if (ma != NULL) {
2212                 ma->ma_valid = 0;
2213                 ma->ma_need = 0;
2214         }
2215
2216         return rc;
2217 }
2218
2219 static int mdt_close_resync_done(struct mdt_thread_info *info,
2220                                  struct mdt_object *o, struct md_attr *ma)
2221 {
2222         struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LOCAL];
2223         struct close_data *data;
2224         struct ldlm_lock *lease;
2225         struct md_layout_change layout = { 0 };
2226         __u32 *resync_ids = NULL;
2227         size_t resync_count = 0;
2228         bool lease_broken;
2229         int rc;
2230
2231         ENTRY;
2232
2233         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2234                 RETURN(-EROFS);
2235
2236         if (!S_ISREG(lu_object_attr(&o->mot_obj)))
2237                 RETURN(-EINVAL);
2238
2239         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2240         if (data == NULL)
2241                 RETURN(-EPROTO);
2242
2243         if (req_capsule_req_need_swab(info->mti_pill))
2244                 lustre_swab_close_data_resync_done(&data->cd_resync);
2245
2246         if (!fid_is_zero(&data->cd_fid))
2247                 RETURN(-EPROTO);
2248
2249         lease = ldlm_handle2lock(&data->cd_handle);
2250         if (lease == NULL)
2251                 RETURN(-ESTALE);
2252
2253         /* try to hold open_sem so that nobody else can open the file */
2254         if (!down_write_trylock(&o->mot_open_sem)) {
2255                 ldlm_lock_cancel(lease);
2256                 GOTO(out_reprocess, rc = -EBUSY);
2257         }
2258
2259         /* Check if the lease open lease has already canceled */
2260         lock_res_and_lock(lease);
2261         lease_broken = ldlm_is_cancel(lease);
2262         unlock_res_and_lock(lease);
2263
2264         LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2265                    PFID(mdt_object_fid(o)), lease_broken);
2266
2267         /* Cancel server side lease. Client side counterpart should
2268          * have been cancelled. It's okay to cancel it now as we've
2269          * held mot_open_sem. */
2270         ldlm_lock_cancel(lease);
2271
2272         if (lease_broken) /* don't perform release task */
2273                 GOTO(out_unlock, rc = -ESTALE);
2274
2275         resync_count = data->cd_resync.resync_count;
2276
2277         if (resync_count > INLINE_RESYNC_ARRAY_SIZE) {
2278                 void *data;
2279
2280                 if (!req_capsule_has_field(info->mti_pill, &RMF_U32,
2281                                            RCL_CLIENT))
2282                         GOTO(out_unlock, rc = -EPROTO);
2283
2284                 OBD_ALLOC_PTR_ARRAY(resync_ids, resync_count);
2285                 if (!resync_ids)
2286                         GOTO(out_unlock, rc = -ENOMEM);
2287
2288                 data = req_capsule_client_get(info->mti_pill, &RMF_U32);
2289                 memcpy(resync_ids, data, resync_count * sizeof(__u32));
2290
2291                 layout.mlc_resync_ids = resync_ids;
2292         } else {
2293                 layout.mlc_resync_ids = data->cd_resync.resync_ids_inline;
2294         }
2295
2296         layout.mlc_opc = MD_LAYOUT_RESYNC_DONE;
2297         layout.mlc_resync_count = resync_count;
2298         if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2299                 layout.mlc_som.lsa_valid = SOM_FL_STRICT;
2300                 layout.mlc_som.lsa_size = ma->ma_attr.la_size;
2301                 layout.mlc_som.lsa_blocks = ma->ma_attr.la_blocks;
2302         }
2303         rc = mdt_layout_change(info, o, lhc, &layout);
2304         if (rc)
2305                 GOTO(out_unlock, rc);
2306
2307         mdt_object_unlock(info, o, lhc, 0);
2308
2309         EXIT;
2310
2311 out_unlock:
2312         up_write(&o->mot_open_sem);
2313
2314         /* already released */
2315         if (rc == 0) {
2316                 struct mdt_body *repbody;
2317
2318                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2319                 LASSERT(repbody != NULL);
2320                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2321         }
2322
2323         if (resync_ids)
2324                 OBD_FREE_PTR_ARRAY(resync_ids, resync_count);
2325
2326 out_reprocess:
2327         ldlm_reprocess_all(lease->l_resource,
2328                            lease->l_policy_data.l_inodebits.bits);
2329         LDLM_LOCK_PUT(lease);
2330
2331         ma->ma_valid = 0;
2332         ma->ma_need = 0;
2333
2334         return rc;
2335 }
2336
2337 #define MFD_CLOSED(open_flags) ((open_flags) == MDS_FMODE_CLOSED)
2338
2339 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2340 {
2341         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_open_flags));
2342 }
2343
2344 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2345 {
2346         struct mdt_object *o = mfd->mfd_object;
2347         struct md_object *next = mdt_object_child(o);
2348         struct md_attr *ma = &info->mti_attr;
2349         struct lu_fid *ofid = &info->mti_tmp_fid1;
2350         int rc = 0;
2351         u64 open_flags;
2352         u64 intent;
2353
2354         ENTRY;
2355
2356         open_flags = mfd->mfd_open_flags;
2357         intent = ma->ma_attr_flags & MDS_CLOSE_INTENT;
2358         *ofid = *mdt_object_fid(o);
2359
2360         /* the below message is checked in replay-single.sh test_46 */
2361         CDEBUG(D_INODE, "%s: %sclosing file handle "DFID" with intent: %llx\n",
2362                mdt_obd_name(info->mti_mdt),
2363                ma->ma_valid & MA_FORCE_LOG ? "force " : "", PFID(ofid), intent);
2364
2365         switch (intent) {
2366         case MDS_HSM_RELEASE: {
2367                 rc = mdt_hsm_release(info, o, ma);
2368                 if (rc < 0) {
2369                         CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2370                                mdt_obd_name(info->mti_mdt),
2371                                PFID(ofid), rc);
2372                         /* continue to close even error occurred. */
2373                 }
2374                 break;
2375         }
2376         case MDS_CLOSE_LAYOUT_MERGE:
2377         case MDS_CLOSE_LAYOUT_SPLIT:
2378         case MDS_CLOSE_LAYOUT_SWAP: {
2379                 rc = mdt_close_handle_layouts(info, o, ma);
2380                 if (rc < 0) {
2381                         CDEBUG(D_INODE,
2382                                "%s: cannot swap layout of "DFID": rc = %d\n",
2383                                mdt_obd_name(info->mti_mdt),
2384                                PFID(ofid), rc);
2385                         /* continue to close even if error occurred. */
2386                 }
2387                 break;
2388         }
2389         case MDS_CLOSE_RESYNC_DONE:
2390                 rc = mdt_close_resync_done(info, o, ma);
2391                 break;
2392         default:
2393                 /* nothing */
2394                 break;
2395         }
2396
2397         if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
2398             ma->ma_attr.la_valid & (LA_LSIZE | LA_LBLOCKS)) {
2399                 int rc2;
2400
2401                 rc2 = mdt_lsom_update(info, o, false);
2402                 if (rc2 < 0)
2403                         CDEBUG(D_INODE,
2404                                "%s: File " DFID " LSOM failed: rc = %d\n",
2405                                mdt_obd_name(info->mti_mdt),
2406                                PFID(ofid), rc2);
2407                         /* continue to close even if error occured. */
2408         }
2409
2410         if (open_flags & MDS_FMODE_WRITE)
2411                 mdt_write_put(o);
2412         else if (open_flags & MDS_FMODE_EXEC)
2413                 mdt_write_allow(o);
2414
2415         /* Update atime|mtime|ctime on close. */
2416         if ((open_flags & MDS_FMODE_EXEC || open_flags & MDS_FMODE_READ ||
2417              open_flags & MDS_FMODE_WRITE) && (ma->ma_valid & MA_INODE) &&
2418             (ma->ma_attr.la_valid & LA_ATIME ||
2419              ma->ma_attr.la_valid & LA_MTIME ||
2420              ma->ma_attr.la_valid & LA_CTIME)) {
2421                 ma->ma_valid = MA_INODE;
2422                 ma->ma_attr_flags |= MDS_CLOSE_UPDATE_TIMES;
2423                 ma->ma_attr.la_valid &= (LA_ATIME | LA_MTIME | LA_CTIME);
2424
2425                 if (ma->ma_attr.la_valid & LA_MTIME) {
2426                         rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
2427                         if (!rc)
2428                                 ma->ma_valid |= MA_PFID;
2429                 }
2430
2431                 rc = mo_attr_set(info->mti_env, next, ma);
2432         }
2433
2434         /* If file data is modified, add the dirty flag. */
2435         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2436                 rc = mdt_add_dirty_flag(info, o, ma);
2437
2438         ma->ma_need |= MA_INODE;
2439         ma->ma_valid &= ~MA_INODE;
2440
2441         LASSERT(atomic_read(&o->mot_open_count) > 0);
2442         atomic_dec(&o->mot_open_count);
2443         mdt_handle_last_unlink(info, o, ma);
2444
2445         if (!MFD_CLOSED(open_flags)) {
2446                 rc = mo_close(info->mti_env, next, ma, open_flags);
2447                 if (mdt_dom_check_for_discard(info, o))
2448                         mdt_dom_discard_data(info, o);
2449         }
2450
2451         /* adjust open and lease count */
2452         if (open_flags & MDS_OPEN_LEASE) {
2453                 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2454                 atomic_dec(&o->mot_lease_count);
2455         }
2456
2457         mdt_mfd_free(mfd);
2458         mdt_object_put(info->mti_env, o);
2459
2460         RETURN(rc);
2461 }
2462
2463 int mdt_close_internal(struct mdt_thread_info *info, struct ptlrpc_request *req,
2464                        struct mdt_body *repbody)
2465 {
2466         struct mdt_export_data *med;
2467         struct mdt_file_data   *mfd;
2468         int                     ret = 0;
2469         int                     rc = 0;
2470         ENTRY;
2471
2472         med = &req->rq_export->exp_mdt_data;
2473         spin_lock(&med->med_open_lock);
2474         mfd = mdt_open_handle2mfd(med, &info->mti_open_handle,
2475                                   req_is_replay(req));
2476         if (mdt_mfd_closed(mfd)) {
2477                 spin_unlock(&med->med_open_lock);
2478                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2479                        ": cookie = %#llx\n", PFID(info->mti_rr.rr_fid1),
2480                        info->mti_open_handle.cookie);
2481                 /** not serious error since bug 3633 */
2482                 rc = -ESTALE;
2483         } else {
2484                 class_handle_unhash(&mfd->mfd_open_handle);
2485                 list_del_init(&mfd->mfd_list);
2486                 spin_unlock(&med->med_open_lock);
2487                 ret = mdt_mfd_close(info, mfd);
2488         }
2489
2490         RETURN(rc ? rc : ret);
2491 }
2492
2493 int mdt_close(struct tgt_session_info *tsi)
2494 {
2495         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2496         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2497         struct md_attr         *ma = &info->mti_attr;
2498         struct mdt_body        *repbody = NULL;
2499         ktime_t                 kstart = ktime_get();
2500         int rc, ret = 0;
2501         ENTRY;
2502
2503         /* Close may come with the Size-on-MDS update. Unpack it. */
2504         rc = mdt_close_unpack(info);
2505         if (rc)
2506                 GOTO(out, rc = err_serious(rc));
2507
2508         /* These fields are no longer used and are left for compatibility.
2509          * size is always zero */
2510         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2511                              0);
2512         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2513                              0);
2514         rc = req_capsule_server_pack(info->mti_pill);
2515         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2516                 mdt_client_compatibility(info);
2517                 if (rc == 0)
2518                         mdt_fix_reply(info);
2519                 mdt_exit_ucred(info);
2520                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2521         }
2522
2523         /* Continue to close handle even if we can not pack reply */
2524         if (rc == 0) {
2525                 repbody = req_capsule_server_get(info->mti_pill,
2526                                                  &RMF_MDT_BODY);
2527                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2528                                                     &RMF_MDT_MD);
2529                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2530                                                        &RMF_MDT_MD,
2531                                                        RCL_SERVER);
2532                 ma->ma_need = MA_INODE | MA_LOV;
2533                 repbody->mbo_eadatasize = 0;
2534                 repbody->mbo_aclsize = 0;
2535         } else {
2536                 rc = err_serious(rc);
2537         }
2538
2539         rc = mdt_close_internal(info, req, repbody);
2540         if (rc != -ESTALE)
2541                 mdt_empty_transno(info, rc);
2542
2543         if (repbody != NULL) {
2544                 mdt_client_compatibility(info);
2545                 rc = mdt_fix_reply(info);
2546         }
2547
2548         mdt_exit_ucred(info);
2549         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2550                 GOTO(out, rc = err_serious(-ENOMEM));
2551
2552         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2553                                  OBD_FAIL_MDS_CLOSE_NET_REP))
2554                 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2555 out:
2556         mdt_thread_info_fini(info);
2557         if (rc == 0)
2558                 mdt_counter_incr(req, LPROC_MDT_CLOSE,
2559                                  ktime_us_delta(ktime_get(), kstart));
2560         RETURN(rc ? rc : ret);
2561 }