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