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