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