Whamcloud - gitweb
Merge "LU-9771 flr: Merge branch 'flr'"
[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, 2016, 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                         try_layout = false;
877                 }
878
879                 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
880                         PFID(mdt_object_fid(obj)),
881                         atomic_read(&obj->mot_open_count), lm);
882         }
883
884         mdt_lock_reg_init(lhc, lm);
885
886         /* one problem to return layout lock on open is that it may result
887          * in too many layout locks cached on the client side. */
888         if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
889                 /* return lookup lock to validate inode at the client side,
890                  * this is pretty important otherwise mdt will return layout
891                  * lock for each open.
892                  * However this is a double-edged sword because changing
893                  * permission will revoke huge # of LOOKUP locks. */
894                 trybits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
895         }
896
897         if (trybits != 0)
898                 rc = mdt_object_lock_try(info, obj, lhc, ibits, trybits, false);
899         else if (*ibits != 0)
900                 rc = mdt_object_lock(info, obj, lhc, *ibits);
901
902         CDEBUG(D_INODE, "%s: Requested bits lock:"DFID ", ibits = %#llx/%#llx"
903                ", open_flags = %#llo, try_layout = %d : rc = %d\n",
904                mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(obj)),
905                *ibits, trybits, open_flags, try_layout, rc);
906
907         /* will change layout, revoke layout locks by enqueuing EX lock. */
908         if (rc == 0 && create_layout) {
909                 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
910
911                 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
912                         ", open_flags = %#llo\n",
913                         PFID(mdt_object_fid(obj)), open_flags);
914
915                 /* We cannot enqueue another lock for the same resource we
916                  * already have a lock for, due to mechanics of waiting list
917                  * iterating in ldlm, see LU-3601.
918                  * As such we'll drop the open lock we just got above here,
919                  * it's ok not to have this open lock as it's main purpose is to
920                  * flush unused cached client open handles. */
921                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
922                         mdt_object_unlock(info, obj, lhc, 1);
923
924                 LASSERT(!try_layout);
925                 mdt_lock_handle_init(ll);
926                 mdt_lock_reg_init(ll, LCK_EX);
927                 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT);
928
929                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
930         }
931
932         /* Check if there is any other open handles after acquiring
933          * open lock. At this point, caching open handles have been revoked
934          * by open lock.
935          * XXX: Now only exclusive open is supported. Need to check the
936          * type of open for generic lease support. */
937         if (rc == 0 && acq_lease) {
938                 struct ptlrpc_request *req = mdt_info_req(info);
939                 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
940                 struct mdt_file_data *mfd;
941                 bool is_replay_or_resent;
942                 int open_count = 0;
943
944                 /* For lease: application can open a file and then apply lease,
945                  * @handle contains original open handle in that case.
946                  * In recovery, open REQ will be replayed and the lease REQ may
947                  * be resent that means the open handle is already stale, so we
948                  * need to fix it up here by finding new handle. */
949                 is_replay_or_resent = req_is_replay(req) ||
950                         lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
951
952                 /* if the request is _not_ a replay request, rr_handle
953                  * may be used to hold an openhandle which is issuing the
954                  * lease request, so that this openhandle doesn't count. */
955                 mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
956                                      is_replay_or_resent);
957                 if (mfd != NULL)
958                         ++open_count;
959
960                 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
961                         PFID(mdt_object_fid(obj)),
962                         atomic_read(&obj->mot_open_count), open_count);
963
964                 if (atomic_read(&obj->mot_open_count) > open_count)
965                         GOTO(out, rc = -EBUSY);
966         }
967         GOTO(out, rc);
968
969 out:
970         RETURN(rc);
971 }
972
973 static void mdt_object_open_unlock(struct mdt_thread_info *info,
974                                    struct mdt_object *obj,
975                                    struct mdt_lock_handle *lhc,
976                                    __u64 ibits, int rc)
977 {
978         __u64 open_flags = info->mti_spec.sp_cr_flags;
979         struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
980         ENTRY;
981
982         if (req_is_replay(mdt_info_req(info)))
983                 RETURN_EXIT;
984
985         /* Release local lock - the lock put in MDT_LH_LOCAL will never
986          * return to client side. */
987         if (lustre_handle_is_used(&ll->mlh_reg_lh))
988                 mdt_object_unlock(info, obj, ll, 1);
989
990         ll = &info->mti_lh[MDT_LH_LAYOUT];
991         /* Release local layout lock, layout was created */
992         if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
993                 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
994                 mdt_object_unlock(info, obj, ll, 1);
995         }
996
997         if (open_flags & MDS_OPEN_LEASE)
998                 up_write(&obj->mot_open_sem);
999         else
1000                 up_read(&obj->mot_open_sem);
1001
1002         /* Cross-ref case, the lock should be returned to the client */
1003         if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
1004                 RETURN_EXIT;
1005
1006         if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT) &&
1007             !(ibits & MDS_INODELOCK_DOM)) {
1008                 /* for the open request, the lock will only return to client
1009                  * if open or layout lock is granted. */
1010                 rc = 1;
1011         }
1012
1013         if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1014                 struct ldlm_reply       *ldlm_rep;
1015
1016                 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1017                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1018                 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1019                         mdt_object_unlock(info, obj, lhc, 1);
1020         }
1021         RETURN_EXIT;
1022 }
1023
1024 /**
1025  * Check release is permitted for the current HSM flags.
1026  */
1027 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1028 {
1029         if (!(ma->ma_valid & MA_HSM))
1030                 return false;
1031
1032         if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1033                 return false;
1034
1035         if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1036                 return false;
1037
1038         return true;
1039 }
1040
1041 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1042                                 struct ldlm_reply *rep,
1043                                 struct mdt_lock_handle *lhc)
1044 {
1045         const struct lu_env     *env   = info->mti_env;
1046         struct mdt_device       *mdt   = info->mti_mdt;
1047         __u64                    flags = info->mti_spec.sp_cr_flags;
1048         struct mdt_reint_record *rr    = &info->mti_rr;
1049         struct md_attr          *ma    = &info->mti_attr;
1050         struct mdt_object       *parent= NULL;
1051         struct mdt_object       *o;
1052         int                      rc;
1053         bool                     object_locked = false;
1054         __u64                    ibits = 0;
1055         ENTRY;
1056
1057         if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1058                 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1059                         parent = mdt_object_find(env, mdt, rr->rr_fid1);
1060                         if (IS_ERR(parent)) {
1061                                 CDEBUG(D_INODE, "Fail to find parent "DFID
1062                                        " for anonymous created %ld, try to"
1063                                        " use server-side parent.\n",
1064                                        PFID(rr->rr_fid1), PTR_ERR(parent));
1065                                 parent = NULL;
1066                         }
1067                 }
1068                 if (parent == NULL)
1069                         ma->ma_need |= MA_PFID;
1070         }
1071
1072         o = mdt_object_find(env, mdt, rr->rr_fid2);
1073         if (IS_ERR(o))
1074                 GOTO(out_parent_put, rc = PTR_ERR(o));
1075
1076         if (mdt_object_remote(o)) {
1077                 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1078                        mdt_obd_name(info->mti_mdt),
1079                        PFID(rr->rr_fid2));
1080                 GOTO(out, rc = -EREMOTE);
1081         } else if (!mdt_object_exists(o)) {
1082                 mdt_set_disposition(info, rep,
1083                                     DISP_IT_EXECD |
1084                                     DISP_LOOKUP_EXECD |
1085                                     DISP_LOOKUP_NEG);
1086                 GOTO(out, rc = -ENOENT);
1087         }
1088
1089         mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1090
1091         mdt_prep_ma_buf_from_rep(info, o, ma);
1092         if (flags & MDS_OPEN_RELEASE)
1093                 ma->ma_need |= MA_HSM;
1094         rc = mdt_attr_get_complex(info, o, ma);
1095         if (rc)
1096                 GOTO(out, rc);
1097
1098         /* We should not change file's existing LOV EA */
1099         if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
1100             flags & MDS_OPEN_HAS_EA && ma->ma_valid & MA_LOV)
1101                 GOTO(out, rc = -EEXIST);
1102
1103         /* If a release request, check file flags are fine and ask for an
1104          * exclusive open access. */
1105         if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1106                 GOTO(out, rc = -EPERM);
1107
1108         rc = mdt_check_resent_lock(info, o, lhc);
1109         if (rc < 0) {
1110                 GOTO(out, rc);
1111         } else if (rc > 0) {
1112                 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1113                 object_locked = true;
1114                 if (rc)
1115                         GOTO(out_unlock, rc);
1116         }
1117
1118         if (ma->ma_valid & MA_PFID) {
1119                 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1120                 if (IS_ERR(parent)) {
1121                         CDEBUG(D_INODE, "Fail to find parent "DFID
1122                                " for anonymous created %ld, try to"
1123                                " use system default.\n",
1124                                PFID(&ma->ma_pfid), PTR_ERR(parent));
1125                         parent = NULL;
1126                 }
1127         }
1128
1129         rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1130         if (!rc) {
1131                 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1132                 if (flags & MDS_OPEN_LOCK)
1133                         mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1134                 if (flags & MDS_OPEN_LEASE)
1135                         mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1136         }
1137         GOTO(out_unlock, rc);
1138
1139 out_unlock:
1140         if (object_locked)
1141                 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1142 out:
1143         mdt_object_put(env, o);
1144         if (rc == 0)
1145                 mdt_pack_size2body(info, rr->rr_fid2,
1146                                    ibits & MDS_INODELOCK_DOM);
1147 out_parent_put:
1148         if (parent != NULL)
1149                 mdt_object_put(env, parent);
1150         return rc;
1151 }
1152
1153 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1154 static int mdt_cross_open(struct mdt_thread_info *info,
1155                           const struct lu_fid *parent_fid,
1156                           const struct lu_fid *fid,
1157                           struct ldlm_reply *rep, __u32 flags)
1158 {
1159         struct md_attr    *ma = &info->mti_attr;
1160         struct mdt_object *o;
1161         int                rc;
1162         ENTRY;
1163
1164         o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1165         if (IS_ERR(o))
1166                 RETURN(rc = PTR_ERR(o));
1167
1168         if (mdt_object_remote(o)) {
1169                 /* Something is wrong here, the object is on another MDS! */
1170                 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1171                        mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1172                 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1173                                 &o->mot_obj,
1174                                 "Object isn't on this server! FLD error?");
1175                 rc = -EFAULT;
1176         } else {
1177                 if (mdt_object_exists(o)) {
1178                         /* Do permission check for cross-open. */
1179                         rc = mo_permission(info->mti_env, NULL,
1180                                            mdt_object_child(o),
1181                                            NULL, flags | MDS_OPEN_CROSS);
1182                         if (rc)
1183                                 goto out;
1184
1185                         mdt_prep_ma_buf_from_rep(info, o, ma);
1186                         rc = mdt_attr_get_complex(info, o, ma);
1187                         if (rc != 0)
1188                                 GOTO(out, rc);
1189
1190                         rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1191                 } else {
1192                         /*
1193                          * Something is wrong here. lookup was positive but
1194                          * there is no object!
1195                          */
1196                         CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1197                               mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1198                         rc = -EFAULT;
1199                 }
1200         }
1201 out:
1202         mdt_object_put(info->mti_env, o);
1203         RETURN(rc);
1204 }
1205
1206 /*
1207  * find root object and take its xattr lock if it's on remote MDT, later create
1208  * may use fs default striping (which is stored in root xattr).
1209  */
1210 static int mdt_lock_root_xattr(struct mdt_thread_info *info,
1211                                struct mdt_device *mdt)
1212 {
1213         struct mdt_object *md_root = mdt->mdt_md_root;
1214         struct lustre_handle lhroot;
1215         int rc;
1216
1217         if (md_root == NULL) {
1218                 lu_root_fid(&info->mti_tmp_fid1);
1219                 md_root = mdt_object_find(info->mti_env, mdt,
1220                                           &info->mti_tmp_fid1);
1221                 if (IS_ERR(md_root))
1222                         return PTR_ERR(md_root);
1223
1224                 spin_lock(&mdt->mdt_lock);
1225                 if (mdt->mdt_md_root != NULL) {
1226                         spin_unlock(&mdt->mdt_lock);
1227
1228                         LASSERTF(mdt->mdt_md_root == md_root,
1229                                  "Different root object ("
1230                                  DFID") instances, %p, %p\n",
1231                                  PFID(&info->mti_tmp_fid1),
1232                                  mdt->mdt_md_root, md_root);
1233                         LASSERT(atomic_read(
1234                                 &md_root->mot_obj.lo_header->loh_ref) > 1);
1235
1236                         mdt_object_put(info->mti_env, md_root);
1237                 } else {
1238                         mdt->mdt_md_root = md_root;
1239                         spin_unlock(&mdt->mdt_lock);
1240                 }
1241         }
1242
1243         if (md_root->mot_cache_attr || !mdt_object_remote(md_root))
1244                 return 0;
1245
1246         rc = mdt_remote_object_lock(info, md_root, mdt_object_fid(md_root),
1247                                     &lhroot, LCK_PR, MDS_INODELOCK_XATTR,
1248                                     true);
1249         if (rc < 0)
1250                 return rc;
1251
1252         md_root->mot_cache_attr = 1;
1253
1254         /* don't cancel this lock, so that we know the cached xattr is valid. */
1255         ldlm_lock_decref(&lhroot, LCK_PR);
1256
1257         return 0;
1258 }
1259
1260 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1261 {
1262         struct mdt_device       *mdt = info->mti_mdt;
1263         struct ptlrpc_request   *req = mdt_info_req(info);
1264         struct mdt_object       *parent;
1265         struct mdt_object       *child;
1266         struct mdt_lock_handle  *lh;
1267         struct ldlm_reply       *ldlm_rep;
1268         struct mdt_body         *repbody;
1269         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
1270         struct md_attr          *ma = &info->mti_attr;
1271         __u64                    create_flags = info->mti_spec.sp_cr_flags;
1272         __u64                    ibits = 0;
1273         struct mdt_reint_record *rr = &info->mti_rr;
1274         int                      result, rc;
1275         int                      created = 0;
1276         int                      object_locked = 0;
1277         __u32                    msg_flags;
1278         ENTRY;
1279
1280         OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1281                                (obd_timeout + 1) / 4);
1282
1283         mdt_counter_incr(req, LPROC_MDT_OPEN);
1284         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1285
1286         ma->ma_need = MA_INODE;
1287         ma->ma_valid = 0;
1288
1289         LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1290         ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1291
1292         if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1293                 CERROR("file join is not supported anymore.\n");
1294                 GOTO(out, result = err_serious(-EOPNOTSUPP));
1295         }
1296         msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1297
1298         if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1299             info->mti_spec.u.sp_ea.eadata == NULL)
1300                 GOTO(out, result = err_serious(-EINVAL));
1301
1302         if (create_flags & FMODE_WRITE &&
1303             exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1304                 GOTO(out, result = -EROFS);
1305
1306         CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1307                "cr_flag=%#llo mode=0%06o msg_flag=0x%x\n",
1308                PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1309                PFID(rr->rr_fid2), create_flags,
1310                ma->ma_attr.la_mode, msg_flags);
1311
1312         if (info->mti_cross_ref) {
1313                 /* This is cross-ref open */
1314                 mdt_set_disposition(info, ldlm_rep,
1315                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1316                              DISP_LOOKUP_POS));
1317                 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1318                                         ldlm_rep, create_flags);
1319                 GOTO(out, result);
1320         } else if (req_is_replay(req)) {
1321                 result = mdt_open_by_fid(info, ldlm_rep);
1322
1323                 if (result != -ENOENT)
1324                         GOTO(out, result);
1325
1326                 /* We didn't find the correct object, so we need to re-create it
1327                  * via a regular replay. */
1328                 if (!(create_flags & MDS_OPEN_CREAT)) {
1329                         DEBUG_REQ(D_ERROR, req,
1330                                   "OPEN & CREAT not in open replay/by_fid.");
1331                         GOTO(out, result = -EFAULT);
1332                 }
1333                 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1334         } else if (create_flags & (MDS_OPEN_BY_FID | MDS_OPEN_LOCK)) {
1335                 /*
1336                  * MDS_OPEN_LOCK is checked for backward compatibility with 2.1
1337                  * client.
1338                  */
1339                 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1340                 if (result < 0)
1341                         CDEBUG(D_INFO, "no object for "DFID": %d\n",
1342                                PFID(rr->rr_fid2), result);
1343                 GOTO(out, result);
1344         }
1345
1346         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1347                 GOTO(out, result = err_serious(-ENOMEM));
1348
1349         mdt_set_disposition(info, ldlm_rep,
1350                             (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1351
1352         if (!lu_name_is_valid(&rr->rr_name))
1353                 GOTO(out, result = -EPROTO);
1354
1355         result = mdt_lock_root_xattr(info, mdt);
1356         if (result < 0)
1357                 GOTO(out, result);
1358
1359 again:
1360         lh = &info->mti_lh[MDT_LH_PARENT];
1361         mdt_lock_pdo_init(lh,
1362                           (create_flags & MDS_OPEN_CREAT) ? LCK_PW : LCK_PR,
1363                           &rr->rr_name);
1364
1365         parent = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
1366         if (IS_ERR(parent))
1367                 GOTO(out, result = PTR_ERR(parent));
1368
1369         result = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
1370         if (result != 0) {
1371                 mdt_object_put(info->mti_env, parent);
1372                 GOTO(out, result);
1373         }
1374
1375         /* get and check version of parent */
1376         result = mdt_version_get_check(info, parent, 0);
1377         if (result)
1378                 GOTO(out_parent, result);
1379
1380         fid_zero(child_fid);
1381
1382         result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1383                             &rr->rr_name, child_fid, &info->mti_spec);
1384
1385         LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1386                  "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1387                  PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1388                  PFID(child_fid));
1389
1390         if (result != 0 && result != -ENOENT && result != -ESTALE)
1391                 GOTO(out_parent, result);
1392
1393         if (result == -ENOENT || result == -ESTALE) {
1394                 /* If the object is dead, let's check if the object
1395                  * is being migrated to a new object */
1396                 if (result == -ESTALE) {
1397                         struct lu_buf lmv_buf;
1398
1399                         lmv_buf.lb_buf = info->mti_xattr_buf;
1400                         lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
1401                         rc = mo_xattr_get(info->mti_env,
1402                                           mdt_object_child(parent),
1403                                           &lmv_buf, XATTR_NAME_LMV);
1404                         if (rc > 0) {
1405                                 struct lmv_mds_md_v1 *lmv;
1406
1407                                 lmv = lmv_buf.lb_buf;
1408                                 if (le32_to_cpu(lmv->lmv_hash_type) &
1409                                                 LMV_HASH_FLAG_MIGRATION) {
1410                                         /* Get the new parent FID and retry */
1411                                         mdt_object_unlock_put(info, parent,
1412                                                               lh, 1);
1413                                         mdt_lock_handle_init(lh);
1414                                         fid_le_to_cpu(
1415                                                 (struct lu_fid *)rr->rr_fid1,
1416                                                 &lmv->lmv_stripe_fids[1]);
1417                                         goto again;
1418                                 }
1419                         }
1420                 }
1421
1422                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1423                 if (result == -ESTALE) {
1424                         /*
1425                          * -ESTALE means the parent is a dead(unlinked) dir, so
1426                          * it should return -ENOENT to in accordance with the
1427                          * original mds implementaion.
1428                          */
1429                         GOTO(out_parent, result = -ENOENT);
1430                 }
1431
1432                 if (!(create_flags & MDS_OPEN_CREAT))
1433                         GOTO(out_parent, result);
1434                 if (mdt_rdonly(req->rq_export))
1435                         GOTO(out_parent, result = -EROFS);
1436                 *child_fid = *info->mti_rr.rr_fid2;
1437                 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1438                          PFID(child_fid));
1439                 /* In the function below, .hs_keycmp resolves to
1440                  * lu_obj_hop_keycmp() */
1441                 /* coverity[overrun-buffer-val] */
1442                 child = mdt_object_new(info->mti_env, mdt, child_fid);
1443         } else {
1444                 /*
1445                  * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1446                  * return FID back in that case.
1447                  */
1448                 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1449                 child = mdt_object_find(info->mti_env, mdt, child_fid);
1450         }
1451         if (IS_ERR(child))
1452                 GOTO(out_parent, result = PTR_ERR(child));
1453
1454         /** check version of child  */
1455         rc = mdt_version_get_check(info, child, 1);
1456         if (rc)
1457                 GOTO(out_child, result = rc);
1458
1459         if (result == -ENOENT) {
1460                 /* Create under OBF and .lustre is not permitted */
1461                 if (!fid_is_md_operative(rr->rr_fid1))
1462                         GOTO(out_child, result = -EPERM);
1463
1464                 /* save versions in reply */
1465                 mdt_version_get_save(info, parent, 0);
1466                 mdt_version_get_save(info, child, 1);
1467
1468                 /* version of child will be changed */
1469                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1470
1471                 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1472                 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1473
1474                 /* Let lower layers know what is lock mode on directory. */
1475                 info->mti_spec.sp_cr_mode =
1476                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1477
1478                 /* Don't do lookup sanity check. We know name doesn't exist. */
1479                 info->mti_spec.sp_cr_lookup = 0;
1480                 info->mti_spec.sp_feat = &dt_directory_features;
1481
1482                 result = mdo_create(info->mti_env, mdt_object_child(parent),
1483                                     &rr->rr_name, mdt_object_child(child),
1484                                     &info->mti_spec, &info->mti_attr);
1485                 if (result == -ERESTART) {
1486                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1487                         GOTO(out_child, result);
1488                 } else {
1489                         mdt_prep_ma_buf_from_rep(info, child, ma);
1490                         /* XXX: we should call this once, see few lines below */
1491                         if (result == 0)
1492                                 result = mdt_attr_get_complex(info, child, ma);
1493
1494                         if (result != 0)
1495                                 GOTO(out_child, result);
1496                 }
1497                 created = 1;
1498                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
1499         } else {
1500                 /*
1501                  * The object is on remote node, return its FID for remote open.
1502                  */
1503                 if (mdt_object_remote(child)) {
1504                         /*
1505                          * Check if this lock already was sent to client and
1506                          * this is resent case. For resent case do not take lock
1507                          * again, use what is already granted.
1508                          */
1509                         LASSERT(lhc != NULL);
1510
1511                         rc = mdt_check_resent_lock(info, child, lhc);
1512                         if (rc < 0) {
1513                                 GOTO(out_child, result = rc);
1514                         } else if (rc > 0) {
1515                                 mdt_lock_handle_init(lhc);
1516                                 mdt_lock_reg_init(lhc, LCK_PR);
1517
1518                                 rc = mdt_object_lock(info, child, lhc,
1519                                                      MDS_INODELOCK_LOOKUP);
1520                         }
1521                         repbody->mbo_fid1 = *mdt_object_fid(child);
1522                         repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1523                         if (rc != 0)
1524                                 result = rc;
1525                         else
1526                                 result = -MDT_EREMOTE_OPEN;
1527                         GOTO(out_child, result);
1528                 } else if (mdt_object_exists(child)) {
1529                         /* We have to get attr & LOV EA & HSM for this
1530                          * object. */
1531                         mdt_prep_ma_buf_from_rep(info, child, ma);
1532                         ma->ma_need |= MA_HSM;
1533                         result = mdt_attr_get_complex(info, child, ma);
1534                         if (result != 0)
1535                                 GOTO(out_child, result);
1536                 } else {
1537                         /* Object does not exist. Likely FS corruption. */
1538                         CERROR("%s: name '"DNAME"' present, but FID "
1539                                DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1540                                PNAME(&rr->rr_name), PFID(child_fid));
1541                         GOTO(out_child, result = -EIO);
1542                 }
1543         }
1544
1545         rc = mdt_check_resent_lock(info, child, lhc);
1546         if (rc < 0) {
1547                 GOTO(out_child, result = rc);
1548         } else if (rc == 0) {
1549                 /* the open lock might already be gotten in
1550                  * ldlm_handle_enqueue() */
1551                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1552                 if (create_flags & MDS_OPEN_LOCK)
1553                         mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1554         } else {
1555                 /* get openlock if this isn't replay and client requested it */
1556                 if (!req_is_replay(req)) {
1557                         rc = mdt_object_open_lock(info, child, lhc, &ibits);
1558                         object_locked = 1;
1559                         if (rc != 0)
1560                                 GOTO(out_child_unlock, result = rc);
1561                         else if (create_flags & MDS_OPEN_LOCK)
1562                                 mdt_set_disposition(info, ldlm_rep,
1563                                                     DISP_OPEN_LOCK);
1564                 }
1565         }
1566         /* Try to open it now. */
1567         rc = mdt_finish_open(info, parent, child, create_flags,
1568                              created, ldlm_rep);
1569         if (rc) {
1570                 result = rc;
1571                 /* openlock will be released if mdt_finish_open failed */
1572                 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1573
1574                 if (created && create_flags & MDS_OPEN_VOLATILE) {
1575                         CERROR("%s: cannot open volatile file "DFID", orphan "
1576                                "file will be left in PENDING directory until "
1577                                "next reboot, rc = %d\n", mdt_obd_name(mdt),
1578                                PFID(mdt_object_fid(child)), rc);
1579                         GOTO(out_child_unlock, result);
1580                 }
1581
1582                 if (created) {
1583                         ma->ma_need = 0;
1584                         ma->ma_valid = 0;
1585                         rc = mdo_unlink(info->mti_env,
1586                                         mdt_object_child(parent),
1587                                         mdt_object_child(child),
1588                                         &rr->rr_name,
1589                                         &info->mti_attr, 0);
1590                         if (rc != 0)
1591                                 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1592                                        mdt_obd_name(info->mti_mdt),
1593                                        PFID(mdt_object_fid(child)), rc);
1594                         mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1595                 }
1596         }
1597         EXIT;
1598 out_child_unlock:
1599         if (object_locked)
1600                 mdt_object_open_unlock(info, child, lhc, ibits, result);
1601 out_child:
1602         mdt_object_put(info->mti_env, child);
1603         if (result == 0)
1604                 mdt_pack_size2body(info, child_fid, ibits & MDS_INODELOCK_DOM);
1605 out_parent:
1606         mdt_object_unlock_put(info, parent, lh, result || !created);
1607 out:
1608         if (result)
1609                 lustre_msg_set_transno(req->rq_repmsg, 0);
1610         return result;
1611 }
1612
1613 /**
1614  * Create an orphan object use local root.
1615  */
1616 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1617                                           struct mdt_device *mdt,
1618                                           const struct lu_fid *fid,
1619                                           struct md_attr *attr, fmode_t fmode)
1620 {
1621         const struct lu_env *env = info->mti_env;
1622         struct md_op_spec *spec = &info->mti_spec;
1623         struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1624         struct mdt_object *obj = NULL;
1625         struct mdt_object *local_root;
1626         static const struct lu_name lname = {
1627                 .ln_name = "i_am_nobody",
1628                 .ln_namelen = sizeof("i_am_nobody") - 1,
1629         };
1630         struct lu_ucred *uc;
1631         cfs_cap_t uc_cap_save;
1632         int rc;
1633         ENTRY;
1634
1635         rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1636         if (rc != 0)
1637                 RETURN(ERR_PTR(rc));
1638
1639         local_root = mdt_object_find(env, mdt, local_root_fid);
1640         if (IS_ERR(local_root))
1641                 RETURN(local_root);
1642
1643         obj = mdt_object_new(env, mdt, fid);
1644         if (IS_ERR(obj))
1645                 GOTO(out, rc = PTR_ERR(obj));
1646
1647         spec->sp_cr_lookup = 0;
1648         spec->sp_feat = &dt_directory_features;
1649         spec->sp_cr_mode = MDL_MINMODE; /* no lock */
1650         spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1651         if (attr->ma_valid & MA_LOV) {
1652                 spec->u.sp_ea.eadata = attr->ma_lmm;
1653                 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1654                 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1655         } else {
1656                 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1657         }
1658
1659         uc = lu_ucred(env);
1660         uc_cap_save = uc->uc_cap;
1661         uc->uc_cap |= 1 << CFS_CAP_DAC_OVERRIDE;
1662         rc = mdo_create(env, mdt_object_child(local_root), &lname,
1663                         mdt_object_child(obj), spec, attr);
1664         uc->uc_cap = uc_cap_save;
1665         if (rc < 0) {
1666                 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1667                        mdt_obd_name(mdt), PFID(fid), rc);
1668                 GOTO(out, rc);
1669         }
1670
1671         rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
1672         if (rc < 0)
1673                 CERROR("%s: cannot open volatile file "DFID", orphan "
1674                        "file will be left in PENDING directory until "
1675                        "next reboot, rc = %d\n", mdt_obd_name(mdt),
1676                        PFID(fid), rc);
1677         GOTO(out, rc);
1678
1679 out:
1680         if (rc < 0) {
1681                 if (!IS_ERR(obj))
1682                         mdt_object_put(env, obj);
1683                 obj = ERR_PTR(rc);
1684         }
1685         mdt_object_put(env, local_root);
1686         return obj;
1687 }
1688
1689 /* XXX Look into layout in MDT layer. */
1690 static inline int mdt_hsm_set_released(struct lov_mds_md *lmm)
1691 {
1692         struct lov_comp_md_v1   *comp_v1;
1693         struct lov_mds_md       *v1;
1694         __u32   off;
1695         int     i;
1696
1697         if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1_DEFINED)) {
1698                 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1699
1700                 if (comp_v1->lcm_entry_count == 0)
1701                         return -EINVAL;
1702
1703                 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
1704                         off = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1705                         v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1706                         v1->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1707                 }
1708         } else {
1709                 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1710         }
1711         return 0;
1712 }
1713
1714 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1715                            struct md_attr *ma)
1716 {
1717         struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1718         struct lu_ucred        *uc = mdt_ucred(info);
1719         struct close_data      *data;
1720         struct ldlm_lock       *lease;
1721         struct mdt_object      *orphan;
1722         struct md_attr         *orp_ma;
1723         struct lu_buf          *buf;
1724         cfs_cap_t               cap;
1725         bool                    lease_broken;
1726         int                     rc;
1727         int                     rc2;
1728         ENTRY;
1729
1730         if (mdt_rdonly(info->mti_exp))
1731                 RETURN(-EROFS);
1732
1733         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1734         if (data == NULL)
1735                 RETURN(-EPROTO);
1736
1737         lease = ldlm_handle2lock(&data->cd_handle);
1738         if (lease == NULL)
1739                 RETURN(-ESTALE);
1740
1741         /* try to hold open_sem so that nobody else can open the file */
1742         if (!down_write_trylock(&o->mot_open_sem)) {
1743                 ldlm_lock_cancel(lease);
1744                 GOTO(out_reprocess, rc = -EBUSY);
1745         }
1746
1747         /* Check if the lease open lease has already canceled */
1748         lock_res_and_lock(lease);
1749         lease_broken = ldlm_is_cancel(lease);
1750         unlock_res_and_lock(lease);
1751
1752         LDLM_DEBUG(lease, DFID " lease broken? %d",
1753                    PFID(mdt_object_fid(o)), lease_broken);
1754
1755         /* Cancel server side lease. Client side counterpart should
1756          * have been cancelled. It's okay to cancel it now as we've
1757          * held mot_open_sem. */
1758         ldlm_lock_cancel(lease);
1759
1760         if (lease_broken) /* don't perform release task */
1761                 GOTO(out_unlock, rc = -ESTALE);
1762
1763         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1764                 GOTO(out_unlock, rc = -EINVAL);
1765
1766         /* ma_need was set before but it seems fine to change it in order to
1767          * avoid modifying the one from RPC */
1768         ma->ma_need = MA_HSM;
1769         rc = mdt_attr_get_complex(info, o, ma);
1770         if (rc != 0)
1771                 GOTO(out_unlock, rc);
1772
1773         if (!mdt_hsm_release_allow(ma))
1774                 GOTO(out_unlock, rc = -EPERM);
1775
1776         /* already released? */
1777         if (ma->ma_hsm.mh_flags & HS_RELEASED)
1778                 GOTO(out_unlock, rc = 0);
1779
1780         /* Compare on-disk and packed data_version */
1781         if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
1782                 CDEBUG(D_HSM, DFID" data_version mismatches: packed=%llu"
1783                        " and on-disk=%llu\n", PFID(mdt_object_fid(o)),
1784                        data->cd_data_version, ma->ma_hsm.mh_arch_ver);
1785                 GOTO(out_unlock, rc = -EPERM);
1786         }
1787
1788         ma->ma_valid = MA_INODE;
1789         ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
1790         rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
1791         if (rc < 0)
1792                 GOTO(out_unlock, rc);
1793
1794         ma->ma_need = MA_INODE | MA_LOV;
1795         rc = mdt_attr_get_complex(info, o, ma);
1796         if (rc < 0)
1797                 GOTO(out_unlock, rc);
1798
1799         if (!(ma->ma_valid & MA_LOV)) {
1800                 /* Even empty file are released */
1801                 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
1802                 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEFINED);
1803                 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1804                 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
1805                 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
1806         } else {
1807                 /* Magic must be LOV_MAGIC_*_DEFINED or LOD will interpret
1808                  * ma_lmm as lov_user_md, then it will be confused by union of
1809                  * layout_gen and stripe_offset. */
1810                 if ((le32_to_cpu(ma->ma_lmm->lmm_magic) & LOV_MAGIC_MASK) ==
1811                     LOV_MAGIC_MAGIC)
1812                         ma->ma_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
1813                 else
1814                         GOTO(out_unlock, rc = -EINVAL);
1815         }
1816
1817         /* Set file as released. */
1818         rc = mdt_hsm_set_released(ma->ma_lmm);
1819         if (rc)
1820                 GOTO(out_unlock, rc);
1821
1822         orp_ma = &info->mti_u.hsm.attr;
1823         orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
1824         /* We use root ownership to bypass potential quota
1825          * restrictions on the user and group of the file. */
1826         orp_ma->ma_attr.la_uid = 0;
1827         orp_ma->ma_attr.la_gid = 0;
1828         orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
1829         orp_ma->ma_lmm = ma->ma_lmm;
1830         orp_ma->ma_lmm_size = ma->ma_lmm_size;
1831         orp_ma->ma_valid = MA_INODE | MA_LOV;
1832         orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
1833                                  FMODE_WRITE);
1834         if (IS_ERR(orphan)) {
1835                 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
1836                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
1837                        PTR_ERR(orphan));
1838                 GOTO(out_unlock, rc = PTR_ERR(orphan));
1839         }
1840
1841         /* Set up HSM attribute for orphan object */
1842         CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
1843         buf = &info->mti_buf;
1844         buf->lb_buf = info->mti_xattr_buf;
1845         buf->lb_len = sizeof(struct hsm_attrs);
1846         ma->ma_hsm.mh_flags |= HS_RELEASED;
1847         lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1848         ma->ma_hsm.mh_flags &= ~HS_RELEASED;
1849
1850         mdt_lock_reg_init(lh, LCK_EX);
1851         rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
1852                              MDS_INODELOCK_XATTR);
1853         if (rc != 0)
1854                 GOTO(out_close, rc);
1855
1856         /* The orphan has root ownership so we need to raise
1857          * CAP_FOWNER to set the HSM attributes. */
1858         cap = uc->uc_cap;
1859         uc->uc_cap |= MD_CAP_TO_MASK(CFS_CAP_FOWNER);
1860         rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
1861                           XATTR_NAME_HSM, 0);
1862         uc->uc_cap = cap;
1863         if (rc != 0)
1864                 GOTO(out_layout_lock, rc);
1865
1866         /* Swap layout with orphan objects. */
1867         rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
1868                              mdt_object_child(orphan),
1869                              SWAP_LAYOUTS_MDS_HSM);
1870         EXIT;
1871
1872 out_layout_lock:
1873         /* Release exclusive LL */
1874         mdt_object_unlock(info, o, lh, 1);
1875 out_close:
1876         /* Close orphan object anyway */
1877         rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
1878                        FMODE_WRITE);
1879         if (rc2 < 0)
1880                 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
1881                        mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
1882         LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
1883                         "object closed");
1884         mdt_object_put(info->mti_env, orphan);
1885
1886 out_unlock:
1887         up_write(&o->mot_open_sem);
1888
1889         /* already released */
1890         if (rc == 0) {
1891                 struct mdt_body *repbody;
1892
1893                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1894                 LASSERT(repbody != NULL);
1895                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1896         }
1897
1898 out_reprocess:
1899         ldlm_reprocess_all(lease->l_resource);
1900         LDLM_LOCK_PUT(lease);
1901
1902         ma->ma_valid = 0;
1903         ma->ma_need = 0;
1904
1905         return rc;
1906 }
1907
1908 int mdt_close_handle_layouts(struct mdt_thread_info *info,
1909                              struct mdt_object *o, struct md_attr *ma)
1910 {
1911         struct mdt_lock_handle  *lh1 = &info->mti_lh[MDT_LH_NEW];
1912         struct mdt_lock_handle  *lh2 = &info->mti_lh[MDT_LH_OLD];
1913         struct close_data       *data;
1914         struct ldlm_lock        *lease;
1915         struct mdt_object       *o1 = o, *o2;
1916         bool                     lease_broken;
1917         bool                     swap_objects;
1918         int                      rc;
1919         ENTRY;
1920
1921         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
1922                 RETURN(-EROFS);
1923
1924         if (!S_ISREG(lu_object_attr(&o1->mot_obj)))
1925                 RETURN(-EINVAL);
1926
1927         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1928         if (data == NULL)
1929                 RETURN(-EPROTO);
1930
1931         if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1932                 RETURN(-EINVAL);
1933
1934         rc = lu_fid_cmp(&data->cd_fid, mdt_object_fid(o));
1935         if (unlikely(rc == 0))
1936                 RETURN(-EINVAL);
1937
1938         /* Exchange o1 and o2, to enforce locking order */
1939         swap_objects = (rc < 0);
1940
1941         lease = ldlm_handle2lock(&data->cd_handle);
1942         if (lease == NULL)
1943                 RETURN(-ESTALE);
1944
1945         o2 = mdt_object_find(info->mti_env, info->mti_mdt, &data->cd_fid);
1946         if (IS_ERR(o2))
1947                 GOTO(out_lease, rc = PTR_ERR(o2));
1948
1949         if (!S_ISREG(lu_object_attr(&o2->mot_obj))) {
1950                 swap_objects = false; /* not swapped yet */
1951                 GOTO(out_obj, rc = -EINVAL);
1952         }
1953
1954         if (swap_objects)
1955                 swap(o1, o2);
1956
1957         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1958                            MAY_WRITE);
1959         if (rc < 0)
1960                 GOTO(out_obj, rc);
1961
1962         rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1963                            MAY_WRITE);
1964         if (rc < 0)
1965                 GOTO(out_obj, rc);
1966
1967         /* try to hold open_sem so that nobody else can open the file */
1968         if (!down_write_trylock(&o->mot_open_sem)) {
1969                 ldlm_lock_cancel(lease);
1970                 GOTO(out_obj, rc = -EBUSY);
1971         }
1972
1973         /* Check if the lease open lease has already canceled */
1974         lock_res_and_lock(lease);
1975         lease_broken = ldlm_is_cancel(lease);
1976         unlock_res_and_lock(lease);
1977
1978         LDLM_DEBUG(lease, DFID " lease broken? %d",
1979                    PFID(mdt_object_fid(o)), lease_broken);
1980
1981         /* Cancel server side lease. Client side counterpart should
1982          * have been cancelled. It's okay to cancel it now as we've
1983          * held mot_open_sem. */
1984         ldlm_lock_cancel(lease);
1985
1986         if (lease_broken)
1987                 GOTO(out_unlock_sem, rc = -ESTALE);
1988
1989         mdt_lock_reg_init(lh1, LCK_EX);
1990         rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1991                              MDS_INODELOCK_XATTR);
1992         if (rc < 0)
1993                 GOTO(out_unlock_sem, rc);
1994
1995         mdt_lock_reg_init(lh2, LCK_EX);
1996         rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1997                              MDS_INODELOCK_XATTR);
1998         if (rc < 0)
1999                 GOTO(out_unlock1, rc);
2000
2001         /* Swap layout with orphan object */
2002         if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SWAP) {
2003                 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
2004                                      mdt_object_child(o2), 0);
2005         } else if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_MERGE) {
2006                 struct lu_buf *buf = &info->mti_buf;
2007
2008                 buf->lb_len = sizeof(void *);
2009                 buf->lb_buf = mdt_object_child(o == o1 ? o2 : o1);
2010                 rc = mo_xattr_set(info->mti_env, mdt_object_child(o), buf,
2011                                   XATTR_LUSTRE_LOV, LU_XATTR_MERGE);
2012                 if (rc == 0 && ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2013                         int rc2;
2014
2015                         rc2 = mdt_set_som(info, o, &ma->ma_attr);
2016                         if (rc2 < 0)
2017                                 CERROR(DFID": Setting i_blocks error: %d, "
2018                                        "i_blocks will be reported wrongly and "
2019                                        "can only be fixed in next resync\n",
2020                                        PFID(mdt_object_fid(o)), rc2);
2021                 }
2022         }
2023         if (rc < 0)
2024                 GOTO(out_unlock2, rc);
2025
2026         EXIT;
2027
2028 out_unlock2:
2029         /* Release exclusive LL */
2030         mdt_object_unlock(info, o2, lh2, 1);
2031
2032 out_unlock1:
2033         mdt_object_unlock(info, o1, lh1, 1);
2034
2035 out_unlock_sem:
2036         up_write(&o->mot_open_sem);
2037
2038         /* already swapped */
2039         if (rc == 0) {
2040                 struct mdt_body *repbody;
2041
2042                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2043                 LASSERT(repbody != NULL);
2044                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2045         }
2046
2047 out_obj:
2048         mdt_object_put(info->mti_env, swap_objects ? o1 : o2);
2049
2050         ldlm_reprocess_all(lease->l_resource);
2051
2052 out_lease:
2053         LDLM_LOCK_PUT(lease);
2054
2055         if (ma != NULL) {
2056                 ma->ma_valid = 0;
2057                 ma->ma_need = 0;
2058         }
2059
2060         return rc;
2061 }
2062
2063 static int mdt_close_resync_done(struct mdt_thread_info *info,
2064                                  struct mdt_object *o, struct md_attr *ma)
2065 {
2066         struct close_data       *data;
2067         struct ldlm_lock        *lease;
2068         struct md_layout_change  layout = { 0 };
2069         __u32                   *resync_ids = NULL;
2070         size_t                   resync_count = 0;
2071         bool                     lease_broken;
2072         int                      rc;
2073         ENTRY;
2074
2075         if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2076                 RETURN(-EROFS);
2077
2078         if (!S_ISREG(lu_object_attr(&o->mot_obj)))
2079                 RETURN(-EINVAL);
2080
2081         data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2082         if (data == NULL)
2083                 RETURN(-EPROTO);
2084
2085         if (ptlrpc_req_need_swab(mdt_info_req(info)))
2086                 lustre_swab_close_data_resync_done(&data->cd_resync);
2087
2088         if (!fid_is_zero(&data->cd_fid))
2089                 RETURN(-EPROTO);
2090
2091         lease = ldlm_handle2lock(&data->cd_handle);
2092         if (lease == NULL)
2093                 RETURN(-ESTALE);
2094
2095         /* try to hold open_sem so that nobody else can open the file */
2096         if (!down_write_trylock(&o->mot_open_sem)) {
2097                 ldlm_lock_cancel(lease);
2098                 GOTO(out_reprocess, rc = -EBUSY);
2099         }
2100
2101         /* Check if the lease open lease has already canceled */
2102         lock_res_and_lock(lease);
2103         lease_broken = ldlm_is_cancel(lease);
2104         unlock_res_and_lock(lease);
2105
2106         LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2107                    PFID(mdt_object_fid(o)), lease_broken);
2108
2109         /* Cancel server side lease. Client side counterpart should
2110          * have been cancelled. It's okay to cancel it now as we've
2111          * held mot_open_sem. */
2112         ldlm_lock_cancel(lease);
2113
2114         if (lease_broken) /* don't perform release task */
2115                 GOTO(out_unlock, rc = -ESTALE);
2116
2117         resync_count = data->cd_resync.resync_count;
2118         if (!resync_count)
2119                 GOTO(out_unlock, rc = 0);
2120
2121         if (resync_count > INLINE_RESYNC_ARRAY_SIZE) {
2122                 void *data;
2123
2124                 if (!req_capsule_has_field(info->mti_pill, &RMF_U32,
2125                                            RCL_CLIENT))
2126                         GOTO(out_unlock, rc = -EPROTO);
2127
2128                 OBD_ALLOC(resync_ids, resync_count * sizeof(__u32));
2129                 if (!resync_ids)
2130                         GOTO(out_unlock, rc = -ENOMEM);
2131
2132                 data = req_capsule_client_get(info->mti_pill, &RMF_U32);
2133                 memcpy(resync_ids, data, resync_count * sizeof(__u32));
2134
2135                 layout.mlc_resync_ids = resync_ids;
2136         } else {
2137                 layout.mlc_resync_ids = data->cd_resync.resync_ids_inline;
2138         }
2139
2140         layout.mlc_opc = MD_LAYOUT_RESYNC_DONE;
2141         layout.mlc_resync_count = resync_count;
2142         if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2143                 layout.mlc_som.lsa_valid = LSOM_FL_VALID;
2144                 layout.mlc_som.lsa_size = ma->ma_attr.la_size;
2145                 layout.mlc_som.lsa_blocks = ma->ma_attr.la_blocks;
2146         }
2147         rc = mdt_layout_change(info, o, &layout);
2148         if (rc)
2149                 GOTO(out_unlock, rc);
2150
2151         EXIT;
2152
2153 out_unlock:
2154         up_write(&o->mot_open_sem);
2155
2156         /* already released */
2157         if (rc == 0) {
2158                 struct mdt_body *repbody;
2159
2160                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2161                 LASSERT(repbody != NULL);
2162                 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2163         }
2164
2165         if (resync_ids)
2166                 OBD_FREE(resync_ids, resync_count * sizeof(__u32));
2167
2168 out_reprocess:
2169         ldlm_reprocess_all(lease->l_resource);
2170         LDLM_LOCK_PUT(lease);
2171
2172         ma->ma_valid = 0;
2173         ma->ma_need = 0;
2174
2175         return rc;
2176 }
2177
2178 #define MFD_CLOSED(mode) ((mode) == MDS_FMODE_CLOSED)
2179 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2180 {
2181         return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
2182 }
2183
2184 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2185 {
2186         struct mdt_object *o = mfd->mfd_object;
2187         struct md_object *next = mdt_object_child(o);
2188         struct md_attr *ma = &info->mti_attr;
2189         int rc = 0;
2190         __u64 mode;
2191         __u64 intent;
2192         ENTRY;
2193
2194         mode = mfd->mfd_mode;
2195
2196         intent = ma->ma_attr_flags & MDS_CLOSE_INTENT;
2197
2198         CDEBUG(D_INODE, "%s: close file "DFID" with intent: %llx\n",
2199                mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(o)), intent);
2200
2201         switch (intent) {
2202         case MDS_HSM_RELEASE: {
2203                 rc = mdt_hsm_release(info, o, ma);
2204                 if (rc < 0) {
2205                         CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2206                                mdt_obd_name(info->mti_mdt),
2207                                PFID(mdt_object_fid(o)), rc);
2208                         /* continue to close even error occurred. */
2209                 }
2210                 break;
2211         }
2212         case MDS_CLOSE_LAYOUT_MERGE:
2213         case MDS_CLOSE_LAYOUT_SWAP: {
2214                 rc = mdt_close_handle_layouts(info, o, ma);
2215                 if (rc < 0) {
2216                         CDEBUG(D_INODE,
2217                                "%s: cannot swap layout of "DFID": rc=%d\n",
2218                                mdt_obd_name(info->mti_mdt),
2219                                PFID(mdt_object_fid(o)), rc);
2220                         /* continue to close even if error occurred. */
2221                 }
2222                 break;
2223         }
2224         case MDS_CLOSE_RESYNC_DONE:
2225                 rc = mdt_close_resync_done(info, o, ma);
2226                 break;
2227         default:
2228                 /* nothing */
2229                 break;
2230         }
2231
2232         if (mode & FMODE_WRITE)
2233                 mdt_write_put(o);
2234         else if (mode & MDS_FMODE_EXEC)
2235                 mdt_write_allow(o);
2236
2237         /* Update atime on close only. */
2238         if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
2239             && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
2240                 /* Set the atime only. */
2241                 ma->ma_valid = MA_INODE;
2242                 ma->ma_attr.la_valid = LA_ATIME;
2243                 rc = mo_attr_set(info->mti_env, next, ma);
2244         }
2245
2246         /* If file data is modified, add the dirty flag. */
2247         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2248                 rc = mdt_add_dirty_flag(info, o, ma);
2249
2250         ma->ma_need |= MA_INODE;
2251         ma->ma_valid &= ~MA_INODE;
2252
2253         LASSERT(atomic_read(&o->mot_open_count) > 0);
2254         atomic_dec(&o->mot_open_count);
2255         mdt_handle_last_unlink(info, o, ma);
2256
2257         if (!MFD_CLOSED(mode)) {
2258                 rc = mo_close(info->mti_env, next, ma, mode);
2259                 mdt_dom_check_and_discard(info, o);
2260         }
2261
2262         /* adjust open and lease count */
2263         if (mode & MDS_OPEN_LEASE) {
2264                 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2265                 atomic_dec(&o->mot_lease_count);
2266         }
2267
2268         mdt_mfd_free(mfd);
2269         mdt_object_put(info->mti_env, o);
2270
2271         RETURN(rc);
2272 }
2273
2274 int mdt_close_internal(struct mdt_thread_info *info, struct ptlrpc_request *req,
2275                        struct mdt_body *repbody)
2276 {
2277         struct mdt_export_data *med;
2278         struct mdt_file_data   *mfd;
2279         int                     ret = 0;
2280         int                     rc = 0;
2281         ENTRY;
2282
2283         med = &req->rq_export->exp_mdt_data;
2284         spin_lock(&med->med_open_lock);
2285         mfd = mdt_handle2mfd(med, &info->mti_close_handle, req_is_replay(req));
2286         if (mdt_mfd_closed(mfd)) {
2287                 spin_unlock(&med->med_open_lock);
2288                 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2289                        ": cookie = %#llx\n", PFID(info->mti_rr.rr_fid1),
2290                        info->mti_close_handle.cookie);
2291                 /** not serious error since bug 3633 */
2292                 rc = -ESTALE;
2293         } else {
2294                 class_handle_unhash(&mfd->mfd_handle);
2295                 list_del_init(&mfd->mfd_list);
2296                 spin_unlock(&med->med_open_lock);
2297                 ret = mdt_mfd_close(info, mfd);
2298         }
2299
2300         RETURN(rc ? rc : ret);
2301 }
2302
2303 int mdt_close(struct tgt_session_info *tsi)
2304 {
2305         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
2306         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2307         struct md_attr         *ma = &info->mti_attr;
2308         struct mdt_body        *repbody = NULL;
2309         int rc, ret = 0;
2310         ENTRY;
2311
2312         mdt_counter_incr(req, LPROC_MDT_CLOSE);
2313         /* Close may come with the Size-on-MDS update. Unpack it. */
2314         rc = mdt_close_unpack(info);
2315         if (rc)
2316                 GOTO(out, rc = err_serious(rc));
2317
2318         /* These fields are no longer used and are left for compatibility.
2319          * size is always zero */
2320         req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2321                              0);
2322         req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2323                              0);
2324         rc = req_capsule_server_pack(info->mti_pill);
2325         if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2326                 mdt_client_compatibility(info);
2327                 if (rc == 0)
2328                         mdt_fix_reply(info);
2329                 mdt_exit_ucred(info);
2330                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2331         }
2332
2333         /* Continue to close handle even if we can not pack reply */
2334         if (rc == 0) {
2335                 repbody = req_capsule_server_get(info->mti_pill,
2336                                                  &RMF_MDT_BODY);
2337                 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2338                                                     &RMF_MDT_MD);
2339                 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2340                                                        &RMF_MDT_MD,
2341                                                        RCL_SERVER);
2342                 ma->ma_need = MA_INODE | MA_LOV;
2343                 repbody->mbo_eadatasize = 0;
2344                 repbody->mbo_aclsize = 0;
2345         } else {
2346                 rc = err_serious(rc);
2347         }
2348
2349         rc = mdt_close_internal(info, req, repbody);
2350         if (rc != -ESTALE)
2351                 mdt_empty_transno(info, rc);
2352
2353         if (repbody != NULL) {
2354                 mdt_client_compatibility(info);
2355                 rc = mdt_fix_reply(info);
2356         }
2357
2358         mdt_exit_ucred(info);
2359         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2360                 GOTO(out, rc = err_serious(-ENOMEM));
2361
2362         if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2363                                  OBD_FAIL_MDS_CLOSE_NET_REP))
2364                 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2365 out:
2366         mdt_thread_info_fini(info);
2367         RETURN(rc ? rc : ret);
2368 }