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