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