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