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