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