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