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