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