4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_open.c
33 * Lustre Metadata Target (mdt) open/close file handling
35 * Author: Huang Hua <huanghua@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <lustre_acl.h>
41 #include <lustre_mds.h>
42 #include <lustre_swab.h>
43 #include "mdt_internal.h"
44 #include <lustre_nodemap.h>
46 static const char mfd_open_handle_owner[] = "mdt";
48 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep,
49 struct mdt_lock_handle *lhc);
51 /* Create a new mdt_file_data struct, initialize it,
52 * and insert it to global hash table */
53 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med)
55 struct mdt_file_data *mfd;
60 refcount_set(&mfd->mfd_open_handle.h_ref, 1);
61 INIT_HLIST_NODE(&mfd->mfd_open_handle.h_link);
63 INIT_LIST_HEAD(&mfd->mfd_list);
64 class_handle_hash(&mfd->mfd_open_handle, mfd_open_handle_owner);
71 * Find the mfd pointed to by handle in global hash table.
72 * In case of replay the handle is obsoleted
73 * but mfd can be found in mfd list by that handle.
74 * Callers need to be holding med_open_lock.
76 struct mdt_file_data *mdt_open_handle2mfd(struct mdt_export_data *med,
77 const struct lustre_handle *open_handle,
78 bool is_replay_or_resent)
80 struct mdt_file_data *mfd;
83 LASSERT(open_handle != NULL);
84 mfd = class_handle2object(open_handle->cookie, mfd_open_handle_owner);
86 refcount_dec(&mfd->mfd_open_handle.h_ref);
88 /* during dw/setattr replay the mfd can be found by old handle */
89 if ((!mfd || mfd->mfd_owner != med) && is_replay_or_resent) {
90 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
91 if (mfd->mfd_open_handle_old.cookie ==
102 void mdt_mfd_free(struct mdt_file_data *mfd)
104 LASSERT(refcount_read(&mfd->mfd_open_handle.h_ref) == 1);
105 LASSERT(list_empty(&mfd->mfd_list));
106 OBD_FREE_PRE(mfd, sizeof(*mfd), "kfree_rcu");
107 kfree_rcu(mfd, mfd_open_handle.h_rcu);
110 static int mdt_create_data(struct mdt_thread_info *info,
111 struct mdt_object *p, struct mdt_object *o)
113 struct md_op_spec *spec = &info->mti_spec;
114 struct md_attr *ma = &info->mti_attr;
118 if (!md_should_create(spec->sp_cr_flags))
121 ma->ma_need = MA_INODE | MA_LOV;
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);
129 rc = mdt_attr_get_complex(info, o, ma);
131 if (rc == 0 && ma->ma_valid & MA_LOV)
132 o->mot_lov_created = 1;
135 mutex_unlock(&o->mot_lov_mutex);
139 int mdt_write_read(struct mdt_object *o)
144 spin_lock(&o->mot_write_lock);
145 rc = o->mot_write_count;
146 spin_unlock(&o->mot_write_lock);
150 int mdt_write_get(struct mdt_object *o)
155 spin_lock(&o->mot_write_lock);
156 if (o->mot_write_count < 0)
159 o->mot_write_count++;
160 spin_unlock(&o->mot_write_lock);
165 void mdt_write_put(struct mdt_object *o)
168 spin_lock(&o->mot_write_lock);
169 o->mot_write_count--;
170 spin_unlock(&o->mot_write_lock);
174 static int mdt_write_deny(struct mdt_object *o)
179 spin_lock(&o->mot_write_lock);
180 if (o->mot_write_count > 0)
183 o->mot_write_count--;
184 spin_unlock(&o->mot_write_lock);
188 static void mdt_write_allow(struct mdt_object *o)
191 spin_lock(&o->mot_write_lock);
192 o->mot_write_count++;
193 spin_unlock(&o->mot_write_lock);
197 /* there can be no real transaction so prepare the fake one */
198 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
200 struct mdt_device *mdt = info->mti_mdt;
201 struct ptlrpc_request *req = mdt_info_req(info);
202 struct tg_export_data *ted;
203 struct lsd_client_data *lcd;
206 if (mdt_rdonly(req->rq_export))
209 /* transaction has occurred already */
210 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
213 if (tgt_is_multimodrpcs_client(req->rq_export)) {
216 /* generate an empty transaction to get a transno
218 th = dt_trans_create(info->mti_env, mdt->mdt_bottom);
220 rc = dt_trans_start(info->mti_env, mdt->mdt_bottom, th);
221 dt_trans_stop(info->mti_env, mdt->mdt_bottom, th);
226 spin_lock(&mdt->mdt_lut.lut_translock);
228 if (info->mti_transno != 0) {
229 struct obd_export *exp = req->rq_export;
231 CERROR("%s: replay trans %llu NID %s: rc = %d\n",
232 mdt_obd_name(mdt), info->mti_transno,
233 obd_export_nid2str(exp), rc);
234 spin_unlock(&mdt->mdt_lut.lut_translock);
237 } else if (info->mti_transno == 0) {
238 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
240 /* should be replay */
241 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
242 mdt->mdt_lut.lut_last_transno = info->mti_transno;
244 spin_unlock(&mdt->mdt_lut.lut_translock);
246 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
248 req->rq_export->exp_obd->obd_last_committed);
250 req->rq_transno = info->mti_transno;
251 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
253 /* update lcd in memory only for resent cases */
254 ted = &req->rq_export->exp_target_data;
256 mutex_lock(&ted->ted_lcd_lock);
258 if (info->mti_transno < lcd->lcd_last_transno &&
259 info->mti_transno != 0) {
260 /* This should happen during replay. Do not update
261 * last rcvd info if replay req transno < last transno,
262 * otherwise the following resend(after replay) can not
263 * be checked correctly by xid */
264 mutex_unlock(&ted->ted_lcd_lock);
265 CDEBUG(D_HA, "%s: transno = %llu < last_transno = %llu\n",
266 mdt_obd_name(mdt), info->mti_transno,
267 lcd->lcd_last_transno);
271 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
272 if (info->mti_transno != 0)
273 lcd->lcd_last_close_transno = info->mti_transno;
274 lcd->lcd_last_close_xid = req->rq_xid;
275 lcd->lcd_last_close_result = rc;
277 /* VBR: save versions in last_rcvd for reconstruct. */
278 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
281 lcd->lcd_pre_versions[0] = pre_versions[0];
282 lcd->lcd_pre_versions[1] = pre_versions[1];
283 lcd->lcd_pre_versions[2] = pre_versions[2];
284 lcd->lcd_pre_versions[3] = pre_versions[3];
286 if (info->mti_transno != 0)
287 lcd->lcd_last_transno = info->mti_transno;
289 lcd->lcd_last_xid = req->rq_xid;
290 lcd->lcd_last_result = rc;
291 lcd->lcd_last_data = info->mti_opdata;
293 mutex_unlock(&ted->ted_lcd_lock);
298 void mdt_mfd_set_mode(struct mdt_file_data *mfd, u64 open_flags)
300 LASSERT(mfd != NULL);
302 CDEBUG(D_DENTRY, DFID " Change mfd open_flags %#llo -> %#llo.\n",
303 PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_open_flags,
306 mfd->mfd_open_flags = open_flags;
310 * prep ma_lmm/ma_lmv for md_attr from reply
312 static void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
313 struct mdt_object *obj,
314 struct md_attr *ma, __u64 open_flags)
316 struct req_capsule *pill;
318 if (ma->ma_lmv || ma->ma_lmm) {
319 CDEBUG(D_INFO, DFID " %s already set.\n",
320 PFID(mdt_object_fid(obj)),
321 ma->ma_lmv ? (ma->ma_lmm ? "ma_lmv and ma_lmm"
327 pill = info->mti_pill;
328 if (S_ISDIR(obj->mot_header.loh_attr)) {
329 ma->ma_lmv = req_capsule_server_get(pill, &RMF_MDT_MD);
330 ma->ma_lmv_size = req_capsule_get_size(pill, &RMF_MDT_MD,
332 if (ma->ma_lmv_size > 0)
333 ma->ma_need |= MA_LMV;
335 if (open_flags & MDS_OPEN_DEFAULT_LMV) {
336 ma->ma_default_lmv = req_capsule_server_get(pill,
337 &RMF_DEFAULT_MDT_MD);
338 ma->ma_default_lmv_size = req_capsule_get_size(pill,
341 if (ma->ma_default_lmv_size > 0)
342 ma->ma_need |= MA_LMV_DEF;
345 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
346 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
348 if (ma->ma_lmm_size > 0)
349 ma->ma_need |= MA_LOV;
353 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
354 struct mdt_object *o, u64 open_flags, int created,
355 struct ldlm_reply *rep)
357 struct ptlrpc_request *req = mdt_info_req(info);
358 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
359 struct mdt_file_data *mfd;
360 struct md_attr *ma = &info->mti_attr;
361 struct lu_attr *la = &ma->ma_attr;
362 struct mdt_body *repbody;
367 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
369 isreg = S_ISREG(la->la_mode);
370 isdir = S_ISDIR(la->la_mode);
371 if (isreg && !(ma->ma_valid & MA_LOV) &&
372 !(open_flags & MDS_OPEN_RELEASE)) {
374 * No EA, check whether it is will set regEA and dirEA since in
375 * above attr get, these size might be zero, so reset it, to
376 * retrieve the MD after create obj.
378 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
381 /* in replay case, p == NULL */
382 rc = mdt_create_data(info, p, o);
386 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
387 mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
390 CDEBUG(D_INODE, "after open, ma_valid bit = %#llx lmm_size = %d\n",
391 ma->ma_valid, ma->ma_lmm_size);
393 if (ma->ma_valid & MA_LOV) {
394 LASSERT(ma->ma_lmm_size != 0);
395 repbody->mbo_eadatasize = ma->ma_lmm_size;
397 repbody->mbo_valid |= OBD_MD_FLDIREA;
399 repbody->mbo_valid |= OBD_MD_FLEASIZE;
402 if (ma->ma_valid & MA_LMV) {
403 LASSERT(ma->ma_lmv_size != 0);
404 repbody->mbo_eadatasize = ma->ma_lmv_size;
406 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
409 if (ma->ma_valid & MA_LMV_DEF) {
410 LASSERT(ma->ma_default_lmv_size != 0);
412 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_DEFAULT_MEA;
415 if (open_flags & MDS_FMODE_WRITE)
416 rc = mdt_write_get(o);
417 else if (open_flags & MDS_FMODE_EXEC)
418 rc = mdt_write_deny(o);
423 rc = mo_open(info->mti_env, mdt_object_child(o),
424 created ? open_flags | MDS_OPEN_CREATED : open_flags,
427 /* If we allow the client to chgrp (CFS_SETGRP_PERM), but the
428 * client does not know which suppgid should be sent to the MDS,
429 * or some other(s) changed the target file's GID after this RPC
430 * sent to the MDS with the suppgid as the original GID, then we
431 * give the client another chance to send the right suppgid. */
433 allow_client_chgrp(info, lu_ucred(info->mti_env)))
434 mdt_set_disposition(info, rep, DISP_OPEN_DENY);
439 mfd = mdt_mfd_new(med);
441 GOTO(err_out, rc = -ENOMEM);
444 * Keep a reference on this object for this open, and is
445 * released by mdt_mfd_close().
447 mdt_object_get(info->mti_env, o);
449 mfd->mfd_xid = req->rq_xid;
452 * @open_flags is always not zero. At least it should be FMODE_READ,
453 * FMODE_WRITE or MDS_FMODE_EXEC.
455 LASSERT(open_flags != 0);
458 mdt_mfd_set_mode(mfd, open_flags);
460 atomic_inc(&o->mot_open_count);
461 if (open_flags & MDS_OPEN_LEASE)
462 atomic_inc(&o->mot_lease_count);
465 if (req_is_replay(req)) {
466 struct mdt_file_data *old_mfd;
467 /* Check wheather old cookie already exist in
468 * the list, becasue when do recovery, client
469 * might be disconnected from server, and
470 * restart replay, so there maybe some orphan
471 * mfd here, we should remove them */
472 LASSERT(info->mti_rr.rr_open_handle != NULL);
473 spin_lock(&med->med_open_lock);
474 old_mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
476 if (old_mfd != NULL) {
477 CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
478 "cookie = %#llx\n", mfd,
479 PFID(mdt_object_fid(mfd->mfd_object)),
480 info->mti_rr.rr_open_handle->cookie);
481 class_handle_unhash(&old_mfd->mfd_open_handle);
482 list_del_init(&old_mfd->mfd_list);
483 spin_unlock(&med->med_open_lock);
484 /* no attr update for that close */
486 ma->ma_valid |= MA_FLAGS;
487 ma->ma_attr_flags |= MDS_RECOV_OPEN;
488 mdt_mfd_close(info, old_mfd);
489 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
490 ma->ma_valid &= ~MA_FLAGS;
492 spin_unlock(&med->med_open_lock);
493 CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
495 PFID(mdt_object_fid(mfd->mfd_object)),
496 info->mti_rr.rr_open_handle->cookie);
499 CDEBUG(D_HA, "Store old cookie %#llx in new mfd\n",
500 info->mti_rr.rr_open_handle->cookie);
502 mfd->mfd_open_handle_old = *info->mti_rr.rr_open_handle;
505 repbody->mbo_open_handle.cookie = mfd->mfd_open_handle.h_cookie;
507 if (req->rq_export->exp_disconnected) {
508 spin_lock(&med->med_open_lock);
509 class_handle_unhash(&mfd->mfd_open_handle);
510 list_del_init(&mfd->mfd_list);
511 spin_unlock(&med->med_open_lock);
512 mdt_mfd_close(info, mfd);
514 spin_lock(&med->med_open_lock);
515 list_add_tail(&mfd->mfd_list, &med->med_open_head);
516 spin_unlock(&med->med_open_lock);
519 mdt_empty_transno(info, rc);
524 if (open_flags & MDS_FMODE_WRITE)
526 else if (open_flags & MDS_FMODE_EXEC)
532 static int mdt_finish_open(struct mdt_thread_info *info,
533 struct mdt_object *p, struct mdt_object *o,
535 struct ldlm_reply *rep)
537 struct ptlrpc_request *req = mdt_info_req(info);
538 struct obd_export *exp = req->rq_export;
539 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
540 struct md_attr *ma = &info->mti_attr;
541 struct lu_attr *la = &ma->ma_attr;
542 struct mdt_file_data *mfd;
543 struct mdt_body *repbody;
546 int isreg, isdir, islnk;
550 LASSERT(ma->ma_valid & MA_INODE);
551 created = mdt_get_disposition(rep, DISP_OPEN_CREATE);
553 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
555 isreg = S_ISREG(la->la_mode);
556 isdir = S_ISDIR(la->la_mode);
557 islnk = S_ISLNK(la->la_mode);
558 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
560 /* compatibility check for 2.10 clients when it tries to open mirrored
561 * files. 2.10 clients don't verify overlapping components so they
562 * would read and write mirrored files just as if they were normal
563 * PFL files, which will cause the problem that sycned mirrors actually
564 * contain different data.
565 * Older clients are not a concern here because they don't even
566 * understand PFL layout. */
567 if (isreg && !exp_connect_flr(exp) && ma->ma_valid & MA_LOV &&
568 mdt_lmm_is_flr(ma->ma_lmm)) {
569 /* LU-10286: for simplicity clients who don't understand
570 * mirrored layout(with connect flag OBD_CONNECT2_FLR) won't
571 * be able to open mirrored files */
575 /* Overstriped files can crash older clients */
576 if (isreg && !exp_connect_overstriping(exp) &&
577 mdt_lmm_is_overstriping(ma->ma_lmm))
580 /* LU-2275, simulate broken behaviour (esp. prevalent in
581 * pre-2.4 servers where a very strange reply is sent on error
582 * that looks like it was actually almost successful and a
583 * failure at the same time.) */
584 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
585 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
589 if (open_flags & MDS_OPEN_LOCK)
590 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
595 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
596 if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
597 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
599 RETURN(PTR_ERR(nodemap));
601 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
602 nodemap_putref(nodemap);
609 * If we are following a symlink, don't open; and do not return open
610 * handle for special nodes as client required.
612 if (islnk || (!isreg && !isdir &&
613 (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
614 lustre_msg_set_transno(req->rq_repmsg, 0);
619 * We need to return the existing object's fid back, so it is done here,
620 * after preparing the reply.
622 if (!created && (open_flags & MDS_OPEN_EXCL) &&
623 (open_flags & MDS_OPEN_CREAT))
626 /* This can't be done earlier, we need to return reply body */
628 if (open_flags & (MDS_OPEN_CREAT | MDS_FMODE_WRITE)) {
629 /* We are trying to create or write an existing dir. */
632 } else if (open_flags & MDS_OPEN_DIRECTORY)
635 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
636 OBD_FAIL_MDS_LDLM_REPLY_NET | OBD_FAIL_ONCE))
640 if (info->mti_rr.rr_flags & MRF_OPEN_RESEND) {
641 spin_lock(&med->med_open_lock);
642 list_for_each(t, &med->med_open_head) {
643 mfd = list_entry(t, struct mdt_file_data, mfd_list);
644 if (mfd->mfd_xid == req->rq_xid) {
645 repbody->mbo_open_handle.cookie =
646 mfd->mfd_open_handle.h_cookie;
651 spin_unlock(&med->med_open_lock);
654 /* set repbody->ea_size for resent case */
655 if (ma->ma_valid & MA_LOV) {
656 LASSERT(ma->ma_lmm_size != 0);
657 repbody->mbo_eadatasize = ma->ma_lmm_size;
659 repbody->mbo_valid |= OBD_MD_FLDIREA;
661 repbody->mbo_valid |= OBD_MD_FLEASIZE;
663 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
666 /* if we have a real resend (not a resend afrer failover), it
667 * means close is already happend, so lets return error
672 rc = mdt_mfd_open(info, p, o, open_flags, created, rep);
674 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
679 void mdt_reconstruct_open(struct mdt_thread_info *info,
680 struct mdt_lock_handle *lhc)
682 struct req_capsule *pill = info->mti_pill;
683 struct ptlrpc_request *req = mdt_info_req(info);
684 struct mdt_reint_record *rr = &info->mti_rr;
685 struct md_attr *ma = &info->mti_attr;
686 struct ldlm_reply *ldlm_rep;
691 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
692 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
694 ma->ma_need = MA_INODE | MA_HSM;
696 opdata = mdt_req_from_lrd(req, info->mti_reply_data);
697 mdt_set_disposition(info, ldlm_rep, opdata);
699 CDEBUG(D_INODE, "This is reconstruct open: disp=%#llx, result=%d\n",
700 ldlm_rep->lock_policy_res1, req->rq_status);
702 /* We did not create successfully, return error to client. */
703 GOTO(out, rc = req->rq_status);
705 /* tg_reply_data is just memory only structure, so any non zero fid
706 * means a real resend not a resend after recovery which need to be
707 * handled as regular open
709 if (likely(!fid_is_zero(&info->mti_reply_data->trd_object))) {
710 rr->rr_fid2 = &info->mti_reply_data->trd_object;
711 rr->rr_flags |= MRF_OPEN_RESEND;
712 rc = mdt_open_by_fid(info, ldlm_rep, lhc);
714 lustre_msg_set_transno(req->rq_repmsg, 0);
716 /* We did not try to create, so we are a pure open */
717 rc = mdt_reint_open(info, lhc);
722 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
723 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
726 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep,
727 struct mdt_lock_handle *lhc)
729 u64 open_flags = info->mti_spec.sp_cr_flags;
730 struct mdt_reint_record *rr = &info->mti_rr;
731 struct md_attr *ma = &info->mti_attr;
732 struct mdt_object *o;
736 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
738 RETURN(rc = PTR_ERR(o));
740 rc = mdt_check_enc(info, o);
744 if (unlikely(mdt_object_remote(o))) {
745 /* the child object was created on remote server */
746 struct mdt_body *repbody;
748 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
751 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
752 repbody->mbo_fid1 = *rr->rr_fid2;
753 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
756 if (mdt_object_exists(o)) {
757 tgt_open_obj_set(info->mti_env, mdt_obj2dt(o));
758 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
761 if ((open_flags & MDS_OPEN_EXCL) &&
762 (open_flags & MDS_OPEN_CREAT))
763 mdt_set_disposition(info, rep,
766 mdt_prep_ma_buf_from_rep(info, o, ma, open_flags);
767 rc = mdt_attr_get_complex(info, o, ma);
770 rc = mdt_finish_open(info, NULL, o, open_flags, rep);
773 mdt_pack_size2body(info, rr->rr_fid2, &lhc->mlh_reg_lh);
780 mdt_object_put(info->mti_env, o);
784 /* lock object for open */
785 static int mdt_object_open_lock(struct mdt_thread_info *info,
786 struct mdt_object *obj,
787 struct mdt_lock_handle *lhc,
790 struct md_attr *ma = &info->mti_attr;
791 __u64 open_flags = info->mti_spec.sp_cr_flags;
793 enum ldlm_mode lm = LCK_CR;
794 bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
795 bool try_layout = false;
796 bool create_layout = false;
798 __u32 dom_stripe = 0;
799 unsigned int dom_only = 0;
800 unsigned int dom_lock = 0;
804 mdt_lock_handle_init(lhc);
806 if (req_is_replay(mdt_info_req(info)))
809 if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
810 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
811 md_should_create(open_flags))
812 create_layout = true;
813 if (exp_connect_layout(info->mti_exp) && !create_layout &&
814 ma->ma_need & MA_LOV)
817 /* DoM files can take IO lock at OPEN when it makes sense,
818 * check if file has DoM stripe and ask for lock if client
819 * no lock on that resource yet.
821 if (ma->ma_valid & MA_LOV && ma->ma_lmm != NULL)
822 dom_stripe = mdt_lmm_dom_entry_check(ma->ma_lmm,
824 /* If only DOM stripe is being used then we can expect IO
825 * to it after OPEN and will return corresponding DOM ibit
826 * using default strategy from mdt_opts.mo_dom_lock.
827 * Otherwise trylock mode is used always and DOM ibit will
828 * be returned optionally.
831 !mdt_dom_client_has_lock(info, mdt_object_fid(obj)))
832 dom_lock = !dom_only ? TRYLOCK_DOM_ON_OPEN :
833 info->mti_mdt->mdt_opts.mo_dom_lock;
837 /* lease open, acquire write mode of open sem */
838 down_write(&obj->mot_open_sem);
840 /* Lease exists and ask for new lease */
841 if (atomic_read(&obj->mot_lease_count) > 0) {
842 /* only exclusive open is supported, so lease
843 * are conflicted to each other */
844 GOTO(out, rc = -EBUSY);
847 /* Lease must be with open lock */
848 if (!(open_flags & MDS_OPEN_LOCK)) {
849 CERROR("%s: Request lease for file:"DFID ", but open lock "
850 "is missed, open_flags = %#llo : rc = %d\n",
851 mdt_obd_name(info->mti_mdt),
852 PFID(mdt_object_fid(obj)), open_flags, -EPROTO);
853 GOTO(out, rc = -EPROTO);
856 /* should conflict with new opens for write/execute */
858 *ibits = MDS_INODELOCK_OPEN;
860 /* never grant LCK_EX layout lock to client */
862 } else { /* normal open */
863 /* normal open holds read mode of open sem */
864 down_read(&obj->mot_open_sem);
866 if (open_flags & MDS_OPEN_LOCK) {
867 if (open_flags & MDS_FMODE_WRITE)
869 else if (open_flags & MDS_FMODE_EXEC)
874 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
875 } else if (atomic_read(&obj->mot_lease_count) > 0) {
876 if (open_flags & MDS_FMODE_WRITE)
882 *ibits = MDS_INODELOCK_OPEN;
885 lhc = &info->mti_lh[MDT_LH_LOCAL];
886 } else if (dom_lock) {
887 lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR;
888 trybits |= MDS_INODELOCK_DOM | MDS_INODELOCK_LAYOUT;
891 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
892 PFID(mdt_object_fid(obj)),
893 atomic_read(&obj->mot_lease_count), lm);
896 mdt_lock_reg_init(lhc, lm);
898 /* Return lookup lock to validate inode at the client side.
899 * This is pretty important otherwise MDT will return layout
900 * lock for each open.
901 * However this is a double-edged sword because changing
902 * permission will revoke a huge number of LOOKUP locks.
904 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
905 if (!(*ibits & MDS_INODELOCK_LOOKUP))
906 trybits |= MDS_INODELOCK_LOOKUP;
907 trybits |= MDS_INODELOCK_LAYOUT;
910 if (*ibits | trybits)
911 rc = mdt_object_lock_try(info, obj, lhc, ibits, trybits, false);
913 CDEBUG(D_INODE, "%s: Requested bits lock:"DFID ", ibits = %#llx/%#llx"
914 ", open_flags = %#llo, try_layout = %d : rc = %d\n",
915 mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(obj)),
916 *ibits, trybits, open_flags, try_layout, rc);
918 /* will change layout, revoke layout locks by enqueuing EX lock. */
919 if (rc == 0 && create_layout) {
920 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
922 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
923 ", open_flags = %#llo\n",
924 PFID(mdt_object_fid(obj)), open_flags);
926 /* We cannot enqueue another lock for the same resource we
927 * already have a lock for, due to mechanics of waiting list
928 * iterating in ldlm, see LU-3601.
929 * As such we'll drop the open lock we just got above here,
930 * it's ok not to have this open lock as it's main purpose is to
931 * flush unused cached client open handles. */
932 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
933 mdt_object_unlock(info, obj, lhc, 1);
935 LASSERT(!try_layout);
936 mdt_lock_handle_init(ll);
937 mdt_lock_reg_init(ll, LCK_EX);
938 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT);
940 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
943 /* Check if there is any other open handles after acquiring
944 * open lock. At this point, caching open handles have been revoked
946 * XXX: Now only exclusive open is supported. Need to check the
947 * type of open for generic lease support. */
948 if (rc == 0 && acq_lease) {
949 struct ptlrpc_request *req = mdt_info_req(info);
950 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
951 struct mdt_file_data *mfd;
952 bool is_replay_or_resent;
955 /* For lease: application can open a file and then apply lease,
956 * @handle contains original open handle in that case.
957 * In recovery, open REQ will be replayed and the lease REQ may
958 * be resent that means the open handle is already stale, so we
959 * need to fix it up here by finding new handle. */
960 is_replay_or_resent = req_is_replay(req) ||
961 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
963 /* if the request is _not_ a replay request, rr_open_handle
964 * may be used to hold an open file handle which is issuing the
965 * lease request, so that this openhandle doesn't count. */
966 mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
967 is_replay_or_resent);
971 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
972 PFID(mdt_object_fid(obj)),
973 atomic_read(&obj->mot_open_count), open_count);
975 if (atomic_read(&obj->mot_open_count) > open_count) {
976 /* fail if anyone *else* has opened file for write */
977 if (mdt_write_read(obj) > 1)
978 GOTO(out, rc = -EBUSY);
987 static void mdt_object_open_unlock(struct mdt_thread_info *info,
988 struct mdt_object *obj,
989 struct mdt_lock_handle *lhc,
992 __u64 open_flags = info->mti_spec.sp_cr_flags;
993 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
996 if (req_is_replay(mdt_info_req(info)))
999 /* Release local lock - the lock put in MDT_LH_LOCAL will never
1000 * return to client side. */
1001 if (lustre_handle_is_used(&ll->mlh_reg_lh))
1002 mdt_object_unlock(info, obj, ll, 1);
1004 ll = &info->mti_lh[MDT_LH_LAYOUT];
1005 /* Release local layout lock, layout was created */
1006 if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1007 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1008 mdt_object_unlock(info, obj, ll, 1);
1011 if (open_flags & MDS_OPEN_LEASE)
1012 up_write(&obj->mot_open_sem);
1014 up_read(&obj->mot_open_sem);
1016 /* Cross-ref case, the lock should be returned to the client */
1017 if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
1020 if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT) &&
1021 !(ibits & MDS_INODELOCK_DOM)) {
1022 /* for the open request, the lock will only return to client
1023 * if open or layout lock is granted. */
1027 if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1028 struct ldlm_reply *ldlm_rep;
1030 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1031 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1032 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1033 mdt_object_unlock(info, obj, lhc, 1);
1039 * Check release is permitted for the current HSM flags.
1041 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1043 if (!(ma->ma_valid & MA_HSM))
1046 if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1049 if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1055 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1056 struct ldlm_reply *rep,
1057 struct mdt_lock_handle *lhc)
1059 const struct lu_env *env = info->mti_env;
1060 struct mdt_device *mdt = info->mti_mdt;
1061 u64 open_flags = info->mti_spec.sp_cr_flags;
1062 struct mdt_reint_record *rr = &info->mti_rr;
1063 struct md_attr *ma = &info->mti_attr;
1064 struct mdt_object *parent = NULL;
1065 struct mdt_object *o;
1066 bool object_locked = false;
1071 if (md_should_create(open_flags)) {
1072 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1073 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1074 if (IS_ERR(parent)) {
1075 CDEBUG(D_INODE, "Fail to find parent "DFID
1076 " for anonymous created %ld, try to"
1077 " use server-side parent.\n",
1078 PFID(rr->rr_fid1), PTR_ERR(parent));
1083 ma->ma_need |= MA_PFID;
1086 o = mdt_object_find(env, mdt, rr->rr_fid2);
1088 GOTO(out_parent_put, rc = PTR_ERR(o));
1090 if (mdt_object_remote(o)) {
1091 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1092 mdt_obd_name(info->mti_mdt),
1094 GOTO(out, rc = -EREMOTE);
1095 } else if (!mdt_object_exists(o)) {
1096 mdt_set_disposition(info, rep,
1100 GOTO(out, rc = -ENOENT);
1103 /* do not check enc for directory: always allow open */
1104 if (!S_ISDIR(lu_object_attr(&o->mot_obj))) {
1105 rc = mdt_check_enc(info, o);
1110 mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1112 mdt_prep_ma_buf_from_rep(info, o, ma, open_flags);
1113 if (open_flags & MDS_OPEN_RELEASE)
1114 ma->ma_need |= MA_HSM;
1115 rc = mdt_attr_get_complex(info, o, ma);
1119 /* We should not change file's existing LOV EA */
1120 if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
1121 open_flags & MDS_OPEN_HAS_EA && ma->ma_valid & MA_LOV)
1122 GOTO(out, rc = -EEXIST);
1124 /* If a release request, check file open flags are fine and ask for an
1125 * exclusive open access. */
1126 if (open_flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1127 GOTO(out, rc = -EPERM);
1129 rc = mdt_check_resent_lock(info, o, lhc);
1132 } else if (rc > 0) {
1133 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1134 object_locked = true;
1136 GOTO(out_unlock, rc);
1139 if (ma->ma_valid & MA_PFID) {
1140 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1141 if (IS_ERR(parent)) {
1142 CDEBUG(D_INODE, "Fail to find parent "DFID
1143 " for anonymous created %ld, try to"
1144 " use system default.\n",
1145 PFID(&ma->ma_pfid), PTR_ERR(parent));
1150 tgt_open_obj_set(info->mti_env, mdt_obj2dt(o));
1151 rc = mdt_finish_open(info, parent, o, open_flags, rep);
1153 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1154 if (open_flags & MDS_OPEN_LOCK)
1155 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1156 if (open_flags & MDS_OPEN_LEASE)
1157 mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1159 GOTO(out_unlock, rc);
1163 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1165 mdt_object_put(env, o);
1167 mdt_pack_size2body(info, rr->rr_fid2, &lhc->mlh_reg_lh);
1170 mdt_object_put(env, parent);
1174 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1175 static int mdt_cross_open(struct mdt_thread_info *info,
1176 const struct lu_fid *parent_fid,
1177 const struct lu_fid *fid,
1178 struct ldlm_reply *rep, u64 open_flags)
1180 struct md_attr *ma = &info->mti_attr;
1181 struct mdt_object *o;
1185 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1187 RETURN(rc = PTR_ERR(o));
1189 rc = mdt_check_enc(info, o);
1193 if (mdt_object_remote(o)) {
1194 /* Something is wrong here, the object is on another MDS! */
1195 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1196 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1197 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1199 "Object isn't on this server! FLD error?");
1202 if (mdt_object_exists(o)) {
1205 /* Do permission check for cross-open after converting
1206 * MDS_OPEN_* flags to MAY_* permission mask.
1208 mask = mds_accmode(open_flags);
1210 rc = mo_permission(info->mti_env, NULL,
1211 mdt_object_child(o), NULL, mask);
1215 mdt_prep_ma_buf_from_rep(info, o, ma, open_flags);
1216 rc = mdt_attr_get_complex(info, o, ma);
1220 rc = mdt_pack_secctx_in_reply(info, o);
1224 rc = mdt_pack_encctx_in_reply(info, o);
1228 rc = mdt_finish_open(info, NULL, o, open_flags, rep);
1231 * Something is wrong here. lookup was positive but
1232 * there is no object!
1234 CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1235 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1240 mdt_object_put(info->mti_env, o);
1246 * find root object and take its xattr lock if it's on remote MDT, later create
1247 * may use fs default striping (which is stored in root xattr).
1249 static int mdt_lock_root_xattr(struct mdt_thread_info *info,
1250 struct mdt_device *mdt)
1252 struct mdt_object *md_root = mdt->mdt_md_root;
1253 struct lustre_handle lhroot;
1256 if (md_root == NULL) {
1257 lu_root_fid(&info->mti_tmp_fid1);
1258 md_root = mdt_object_find(info->mti_env, mdt,
1259 &info->mti_tmp_fid1);
1260 if (IS_ERR(md_root))
1261 return PTR_ERR(md_root);
1263 spin_lock(&mdt->mdt_lock);
1264 if (mdt->mdt_md_root != NULL) {
1265 spin_unlock(&mdt->mdt_lock);
1267 LASSERTF(mdt->mdt_md_root == md_root,
1268 "Different root object ("
1269 DFID") instances, %p, %p\n",
1270 PFID(&info->mti_tmp_fid1),
1271 mdt->mdt_md_root, md_root);
1272 LASSERT(atomic_read(
1273 &md_root->mot_obj.lo_header->loh_ref) > 1);
1275 mdt_object_put(info->mti_env, md_root);
1277 mdt->mdt_md_root = md_root;
1278 spin_unlock(&mdt->mdt_lock);
1282 if (md_root->mot_cache_attr || !mdt_object_remote(md_root))
1285 rc = mdt_remote_object_lock(info, md_root, mdt_object_fid(md_root),
1286 &lhroot, LCK_PR, MDS_INODELOCK_XATTR,
1291 md_root->mot_cache_attr = 1;
1293 /* don't cancel this lock, so that we know the cached xattr is valid. */
1294 ldlm_lock_decref(&lhroot, LCK_PR);
1299 static inline enum ldlm_mode mdt_open_lock_mode(struct mdt_thread_info *info,
1300 struct mdt_object *p,
1301 struct lu_name *name,
1307 /* We don't need to take the DLM lock for a volatile */
1308 if (open_flags & MDS_OPEN_VOLATILE)
1311 if (!(open_flags & MDS_OPEN_CREAT))
1314 result = mdo_lookup(info->mti_env, mdt_object_child(p), name, &fid,
1317 /* If the file exists we only need a read lock on the parent */
1318 return (result == 0) ? LCK_PR : LCK_PW;
1321 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1323 struct mdt_device *mdt = info->mti_mdt;
1324 struct ptlrpc_request *req = mdt_info_req(info);
1325 struct mdt_object *parent;
1326 struct mdt_object *child;
1327 struct mdt_lock_handle *lh = NULL;
1328 struct ldlm_reply *ldlm_rep;
1329 struct mdt_body *repbody;
1330 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1331 struct lu_ucred *uc = mdt_ucred(info);
1332 struct md_attr *ma = &info->mti_attr;
1333 u64 open_flags = info->mti_spec.sp_cr_flags;
1335 struct mdt_reint_record *rr = &info->mti_rr;
1338 int object_locked = 0;
1339 enum ldlm_mode lock_mode;
1341 ktime_t kstart = ktime_get();
1344 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1345 (obd_timeout + 1) / 4);
1347 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1349 ma->ma_need = MA_INODE;
1352 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1353 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1355 if (unlikely(open_flags & MDS_OPEN_JOIN_FILE)) {
1356 CERROR("file join is not supported anymore.\n");
1357 GOTO(out, result = err_serious(-EOPNOTSUPP));
1359 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1361 if ((open_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1362 info->mti_spec.u.sp_ea.eadata == NULL)
1363 GOTO(out, result = err_serious(-EINVAL));
1365 if (open_flags & MDS_FMODE_WRITE &&
1366 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1367 GOTO(out, result = -EROFS);
1369 CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1370 "cr_flag=%#llo mode=0%06o msg_flag=0x%x\n",
1371 PFID(rr->rr_fid1), PNAME(&rr->rr_name), PFID(rr->rr_fid2),
1372 open_flags, ma->ma_attr.la_mode, msg_flags);
1374 /* Prevent by-fid operation if parent fid is .lustre/fid.
1375 * Also, we want rbac roles to have precedence over any other
1376 * permission or capability checks
1378 if (lu_fid_eq(rr->rr_fid1, &LU_OBF_FID) && !uc->uc_rbac_byfid_ops)
1379 GOTO(out, result = -EPERM);
1381 if (info->mti_cross_ref) {
1382 /* This is cross-ref open */
1383 mdt_set_disposition(info, ldlm_rep,
1384 (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1386 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1387 ldlm_rep, open_flags);
1389 } else if (req_is_replay(req)) {
1390 result = mdt_open_by_fid(info, ldlm_rep, lhc);
1392 if (result != -ENOENT)
1395 /* We didn't find the correct object, so we need to re-create it
1396 * via a regular replay. */
1397 if (!(open_flags & MDS_OPEN_CREAT)) {
1398 DEBUG_REQ(D_ERROR, req,
1399 "OPEN & CREAT not in open replay/by_fid");
1400 GOTO(out, result = -EFAULT);
1402 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1403 } else if (open_flags & MDS_OPEN_BY_FID) {
1404 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1406 CDEBUG(D_INFO, "no object for "DFID": %d\n",
1407 PFID(rr->rr_fid2), result);
1411 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1412 GOTO(out, result = err_serious(-ENOMEM));
1414 mdt_set_disposition(info, ldlm_rep,
1415 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1417 if (!lu_name_is_valid(&rr->rr_name))
1418 GOTO(out, result = -EPROTO);
1420 result = mdt_lock_root_xattr(info, mdt);
1424 parent = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
1426 GOTO(out, result = PTR_ERR(parent));
1428 /* get and check version of parent */
1429 result = mdt_version_get_check(info, parent, 0);
1431 mdt_object_put(info->mti_env, parent);
1435 if (!uc->uc_rbac_fscrypt_admin &&
1436 parent->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD &&
1437 open_flags & MDS_OPEN_CREAT)
1438 GOTO(out_parent, result = -EPERM);
1440 result = mdt_check_enc(info, parent);
1442 GOTO(out_parent, result);
1444 fid_zero(child_fid);
1446 lock_mode = mdt_open_lock_mode(info, parent, &rr->rr_name, open_flags);
1448 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
1450 if (lock_mode != LCK_NL) {
1451 lh = &info->mti_lh[MDT_LH_PARENT];
1452 mdt_lock_pdo_init(lh, lock_mode, &rr->rr_name);
1453 result = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
1455 GOTO(out_parent, result);
1457 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1458 &rr->rr_name, child_fid, &info->mti_spec);
1461 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1462 "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1463 PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1466 if (result != 0 && result != -ENOENT)
1467 GOTO(out_parent_unlock, result);
1469 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
1471 if (result == -ENOENT) {
1472 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1473 if (!(open_flags & MDS_OPEN_CREAT))
1474 GOTO(out_parent_unlock, result);
1475 if (mdt_rdonly(req->rq_export))
1476 GOTO(out_parent_unlock, result = -EROFS);
1478 LASSERT(equi(lh == NULL, lock_mode == LCK_NL));
1480 if (lock_mode == LCK_PR) {
1481 /* unlink vs create race: get write lock and restart */
1482 mdt_object_unlock(info, parent, lh, 1);
1483 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1484 mdt_lock_handle_init(lh);
1489 *child_fid = *info->mti_rr.rr_fid2;
1490 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1492 /* In the function below, .hs_keycmp resolves to
1493 * lu_obj_hop_keycmp() */
1494 /* coverity[overrun-buffer-val] */
1495 child = mdt_object_new(info->mti_env, mdt, child_fid);
1498 * Check for O_EXCL is moved to the mdt_finish_open(),
1499 * we need to return FID back in that case.
1501 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1502 child = mdt_object_find(info->mti_env, mdt, child_fid);
1505 GOTO(out_parent_unlock, result = PTR_ERR(child));
1507 /** check version of child */
1508 rc = mdt_version_get_check(info, child, 1);
1510 GOTO(out_child, result = rc);
1512 tgt_open_obj_set(info->mti_env, mdt_obj2dt(child));
1514 if (result == -ENOENT) {
1515 /* Create under OBF and .lustre is not permitted */
1516 if (!fid_is_md_operative(rr->rr_fid1) &&
1517 (open_flags & MDS_OPEN_VOLATILE) == 0)
1518 GOTO(out_child, result = -EPERM);
1520 /* save versions in reply */
1521 mdt_version_get_save(info, parent, 0);
1522 mdt_version_get_save(info, child, 1);
1524 /* version of child will be changed */
1525 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1527 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1528 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1530 /* Don't do lookup sanity check. We know name doesn't exist. */
1531 info->mti_spec.sp_cr_lookup = 0;
1532 info->mti_spec.sp_feat = &dt_directory_features;
1534 result = mdo_create(info->mti_env, mdt_object_child(parent),
1535 &rr->rr_name, mdt_object_child(child),
1536 &info->mti_spec, &info->mti_attr);
1537 if (result == -ERESTART) {
1538 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1539 GOTO(out_child, result);
1541 mdt_prep_ma_buf_from_rep(info, child, ma, open_flags);
1542 /* XXX: we should call this once, see few lines below */
1544 result = mdt_attr_get_complex(info, child, ma);
1547 GOTO(out_child, result);
1550 mdt_counter_incr(req, LPROC_MDT_MKNOD,
1551 ktime_us_delta(ktime_get(), kstart));
1554 * The object is on remote node, return its FID for remote open.
1556 if (mdt_object_remote(child)) {
1558 * Check if this lock already was sent to client and
1559 * this is resent case. For resent case do not take lock
1560 * again, use what is already granted.
1562 LASSERT(lhc != NULL);
1564 rc = mdt_check_resent_lock(info, child, lhc);
1566 GOTO(out_child, result = rc);
1567 } else if (rc > 0) {
1568 mdt_lock_handle_init(lhc);
1569 mdt_lock_reg_init(lhc, LCK_PR);
1571 rc = mdt_object_lock(info, child, lhc,
1572 MDS_INODELOCK_LOOKUP);
1574 repbody->mbo_fid1 = *mdt_object_fid(child);
1575 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1579 result = -MDT_EREMOTE_OPEN;
1580 GOTO(out_child, result);
1581 } else if (mdt_object_exists(child)) {
1582 /* Check early for MDS_OPEN_DIRECTORY/O_DIRECTORY to
1583 * avoid opening regular files from lfs getstripe
1584 * since doing so breaks the leases used by lfs
1585 * mirror. See LU-13693. */
1586 if (open_flags & MDS_OPEN_DIRECTORY &&
1587 S_ISREG(lu_object_attr(&child->mot_obj)))
1588 GOTO(out_child, result = -ENOTDIR);
1590 /* We have to get attr & LOV EA & HSM for this
1592 mdt_prep_ma_buf_from_rep(info, child, ma, open_flags);
1593 ma->ma_need |= MA_HSM;
1594 result = mdt_attr_get_complex(info, child, ma);
1596 GOTO(out_child, result);
1598 /* Object does not exist. Likely FS corruption. */
1599 CERROR("%s: name '"DNAME"' present, but FID "
1600 DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1601 PNAME(&rr->rr_name), PFID(child_fid));
1602 GOTO(out_child, result = -EIO);
1606 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1607 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1609 rc = mdt_pack_secctx_in_reply(info, child);
1611 GOTO(out_child, result = rc);
1613 rc = mdt_pack_encctx_in_reply(info, child);
1615 GOTO(out_child, result = rc);
1617 rc = mdt_check_resent_lock(info, child, lhc);
1619 GOTO(out_child, result = rc);
1620 } else if (rc == 0) {
1621 /* the open lock might already be gotten in
1622 * ldlm_handle_enqueue() */
1623 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1624 if (open_flags & MDS_OPEN_LOCK)
1625 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1627 /* get openlock if this isn't replay and client requested it */
1628 if (!req_is_replay(req)) {
1629 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1632 GOTO(out_child_unlock, result = rc);
1633 else if (open_flags & MDS_OPEN_LOCK)
1634 mdt_set_disposition(info, ldlm_rep,
1638 /* Try to open it now. */
1639 rc = mdt_finish_open(info, parent, child, open_flags, ldlm_rep);
1642 /* openlock will be released if mdt_finish_open() failed */
1643 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1645 if (created && (open_flags & MDS_OPEN_VOLATILE)) {
1646 CERROR("%s: cannot open volatile file "DFID", orphan "
1647 "file will be left in PENDING directory until "
1648 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1649 PFID(mdt_object_fid(child)), rc);
1650 GOTO(out_child_unlock, result);
1656 rc = mdo_unlink(info->mti_env,
1657 mdt_object_child(parent),
1658 mdt_object_child(child),
1660 &info->mti_attr, 0);
1662 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1663 mdt_obd_name(info->mti_mdt),
1664 PFID(mdt_object_fid(child)), rc);
1665 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1669 mdt_counter_incr(req, LPROC_MDT_OPEN,
1670 ktime_us_delta(ktime_get(), kstart));
1675 mdt_object_open_unlock(info, child, lhc, ibits, result);
1677 mdt_object_put(info->mti_env, child);
1679 mdt_pack_size2body(info, child_fid, &lhc->mlh_reg_lh);
1682 mdt_object_unlock(info, parent, lh, result || !created);
1685 mdt_object_put(info->mti_env, parent);
1688 lustre_msg_set_transno(req->rq_repmsg, 0);
1693 * Create an orphan object use local root.
1695 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1696 struct mdt_device *mdt,
1697 const struct lu_fid *fid,
1698 struct md_attr *attr, fmode_t fmode)
1700 const struct lu_env *env = info->mti_env;
1701 struct md_op_spec *spec = &info->mti_spec;
1702 struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1703 struct mdt_object *obj = NULL;
1704 struct mdt_object *local_root;
1705 static const struct lu_name lname = {
1706 .ln_name = "i_am_nobody",
1707 .ln_namelen = sizeof("i_am_nobody") - 1,
1709 struct lu_ucred *uc;
1710 kernel_cap_t uc_cap_save;
1714 rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1716 RETURN(ERR_PTR(rc));
1718 local_root = mdt_object_find(env, mdt, local_root_fid);
1719 if (IS_ERR(local_root))
1722 obj = mdt_object_new(env, mdt, fid);
1724 GOTO(out, rc = PTR_ERR(obj));
1726 spec->sp_cr_lookup = 0;
1727 spec->sp_feat = &dt_directory_features;
1728 spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1729 if (attr->ma_valid & MA_LOV) {
1730 spec->u.sp_ea.eadata = attr->ma_lmm;
1731 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1732 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1734 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1738 uc_cap_save = uc->uc_cap;
1739 cap_raise(uc->uc_cap, CAP_DAC_OVERRIDE);
1740 rc = mdo_create(env, mdt_object_child(local_root), &lname,
1741 mdt_object_child(obj), spec, attr);
1742 uc->uc_cap = uc_cap_save;
1744 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1745 mdt_obd_name(mdt), PFID(fid), rc);
1749 rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED, spec);
1751 CERROR("%s: cannot open volatile file "DFID", orphan "
1752 "file will be left in PENDING directory until "
1753 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1760 mdt_object_put(env, obj);
1763 mdt_object_put(env, local_root);
1767 /* XXX Look into layout in MDT layer. */
1768 static inline int mdt_hsm_set_released(struct lov_mds_md *lmm)
1770 struct lov_comp_md_v1 *comp_v1;
1771 struct lov_mds_md *v1;
1775 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1_DEFINED)) {
1776 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1778 if (comp_v1->lcm_entry_count == 0)
1781 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
1782 off = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1783 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1784 v1->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1787 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1792 static inline int mdt_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
1794 struct lov_comp_md_v1 *comp_v1;
1796 if (le32_to_cpu(lmm->lmm_magic == LOV_MAGIC_COMP_V1)) {
1797 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1798 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
1799 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
1800 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
1801 *gen = le16_to_cpu(lmm->lmm_layout_gen);
1808 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1811 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1812 struct lu_ucred *uc = mdt_ucred(info);
1813 struct close_data *data;
1814 struct ldlm_lock *lease;
1815 struct mdt_object *orphan;
1816 struct md_attr *orp_ma;
1824 if (mdt_rdonly(info->mti_exp))
1827 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1831 lease = ldlm_handle2lock(&data->cd_handle);
1835 /* try to hold open_sem so that nobody else can open the file */
1836 if (!down_write_trylock(&o->mot_open_sem)) {
1837 ldlm_lock_cancel(lease);
1838 GOTO(out_reprocess, rc = -EBUSY);
1841 /* Check if the lease open lease has already canceled */
1842 lock_res_and_lock(lease);
1843 lease_broken = ldlm_is_cancel(lease);
1844 unlock_res_and_lock(lease);
1846 LDLM_DEBUG(lease, DFID " lease broken? %d",
1847 PFID(mdt_object_fid(o)), lease_broken);
1849 /* Cancel server side lease. Client side counterpart should
1850 * have been cancelled. It's okay to cancel it now as we've
1851 * held mot_open_sem. */
1852 ldlm_lock_cancel(lease);
1854 if (lease_broken) /* don't perform release task */
1855 GOTO(out_unlock, rc = -ESTALE);
1857 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1858 GOTO(out_unlock, rc = -EINVAL);
1860 /* ma_need was set before but it seems fine to change it in order to
1861 * avoid modifying the one from RPC */
1862 ma->ma_need = MA_HSM;
1863 rc = mdt_attr_get_complex(info, o, ma);
1865 GOTO(out_unlock, rc);
1867 if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
1868 if (ma->ma_valid & MA_HSM) {
1869 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1870 GOTO(out_unlock, rc = -EALREADY);
1872 if (ma->ma_hsm.mh_arch_id != data->cd_archive_id)
1874 DFID" archive id diff: %llu:%u\n",
1875 PFID(mdt_object_fid(o)),
1876 ma->ma_hsm.mh_arch_id,
1877 data->cd_archive_id);
1879 if (!(ma->ma_hsm.mh_flags & HS_DIRTY) &&
1880 ma->ma_hsm.mh_arch_ver == data->cd_data_version) {
1882 DFID" data version matches: packed=%llu "
1883 "and on-disk=%llu\n",
1884 PFID(mdt_object_fid(o)),
1885 data->cd_data_version,
1886 ma->ma_hsm.mh_arch_ver);
1887 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1890 if (ma->ma_hsm.mh_flags & HS_DIRTY)
1891 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1893 /* Set up HSM attribte for PCC archived object */
1894 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1895 sizeof(info->mti_xattr_buf));
1896 buf = &info->mti_buf;
1897 buf->lb_buf = info->mti_xattr_buf;
1898 buf->lb_len = sizeof(struct hsm_attrs);
1899 memset(&ma->ma_hsm, 0, sizeof(ma->ma_hsm));
1900 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1901 ma->ma_hsm.mh_arch_id = data->cd_archive_id;
1902 ma->ma_hsm.mh_arch_ver = data->cd_data_version;
1903 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1905 rc = mo_xattr_set(info->mti_env, mdt_object_child(o),
1906 buf, XATTR_NAME_HSM, 0);
1908 GOTO(out_unlock, rc);
1911 if (!mdt_hsm_release_allow(ma))
1912 GOTO(out_unlock, rc = -EPERM);
1914 /* already released? */
1915 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1916 GOTO(out_unlock, rc = 0);
1918 /* Compare on-disk and packed data_version */
1919 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
1920 CDEBUG(D_HSM, DFID" data_version mismatches: "
1921 "packed=%llu and on-disk=%llu\n",
1922 PFID(mdt_object_fid(o)),
1923 data->cd_data_version,
1924 ma->ma_hsm.mh_arch_ver);
1925 GOTO(out_unlock, rc = -EPERM);
1929 ma->ma_valid = MA_INODE;
1930 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
1931 rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
1933 GOTO(out_unlock, rc);
1935 mutex_lock(&o->mot_som_mutex);
1936 rc2 = mdt_set_som(info, o, SOM_FL_STRICT, ma->ma_attr.la_size,
1937 ma->ma_attr.la_blocks);
1938 mutex_unlock(&o->mot_som_mutex);
1941 "%s: File "DFID" SOM update failed: rc = %d\n",
1942 mdt_obd_name(info->mti_mdt),
1943 PFID(mdt_object_fid(o)), rc2);
1946 ma->ma_need = MA_INODE | MA_LOV;
1947 rc = mdt_attr_get_complex(info, o, ma);
1949 GOTO(out_unlock, rc);
1951 if (!(ma->ma_valid & MA_LOV)) {
1952 /* Even empty file are released */
1953 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
1954 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEFINED);
1955 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1956 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
1957 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
1959 /* Magic must be LOV_MAGIC_*_DEFINED or LOD will interpret
1960 * ma_lmm as lov_user_md, then it will be confused by union of
1961 * layout_gen and stripe_offset. */
1962 if ((le32_to_cpu(ma->ma_lmm->lmm_magic) & LOV_MAGIC_MASK) ==
1964 ma->ma_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
1966 GOTO(out_unlock, rc = -EINVAL);
1969 /* Set file as released. */
1970 rc = mdt_hsm_set_released(ma->ma_lmm);
1972 GOTO(out_unlock, rc);
1974 orp_ma = &info->mti_u.hsm.attr;
1975 orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
1976 /* We use root ownership to bypass potential quota
1977 * restrictions on the user and group of the file. */
1978 orp_ma->ma_attr.la_uid = 0;
1979 orp_ma->ma_attr.la_gid = 0;
1980 orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
1981 orp_ma->ma_lmm = ma->ma_lmm;
1982 orp_ma->ma_lmm_size = ma->ma_lmm_size;
1983 orp_ma->ma_valid = MA_INODE | MA_LOV;
1984 orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
1986 if (IS_ERR(orphan)) {
1987 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
1988 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
1990 GOTO(out_unlock, rc = PTR_ERR(orphan));
1993 /* Set up HSM attribute for orphan object */
1994 BUILD_BUG_ON(sizeof(struct hsm_attrs) > sizeof(info->mti_xattr_buf));
1995 buf = &info->mti_buf;
1996 buf->lb_buf = info->mti_xattr_buf;
1997 buf->lb_len = sizeof(struct hsm_attrs);
1998 ma->ma_hsm.mh_flags |= HS_RELEASED;
1999 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
2000 ma->ma_hsm.mh_flags &= ~HS_RELEASED;
2002 mdt_lock_reg_init(lh, LCK_EX);
2003 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
2004 MDS_INODELOCK_XATTR);
2006 GOTO(out_close, rc);
2008 /* The orphan has root ownership so we need to raise
2009 * CAP_FOWNER to set the HSM attributes. */
2011 cap_raise(uc->uc_cap, CAP_FOWNER);
2012 rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
2016 GOTO(out_layout_lock, rc);
2018 /* Swap layout with orphan objects. */
2019 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
2020 mdt_object_child(orphan),
2021 SWAP_LAYOUTS_MDS_HSM);
2023 if (!rc && ma->ma_attr_flags & MDS_PCC_ATTACH) {
2024 ma->ma_need = MA_LOV;
2025 rc = mdt_attr_get_complex(info, o, ma);
2031 /* Release exclusive LL */
2032 mdt_object_unlock(info, o, lh, 1);
2034 /* Close orphan object anyway */
2035 rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
2038 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
2039 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
2040 LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
2042 mdt_object_put(info->mti_env, orphan);
2045 up_write(&o->mot_open_sem);
2047 /* already released */
2049 struct mdt_body *repbody;
2051 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2052 LASSERT(repbody != NULL);
2053 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2054 if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
2055 LASSERT(ma->ma_valid & MA_LOV);
2056 rc = mdt_get_lmm_gen(ma->ma_lmm,
2057 &repbody->mbo_layout_gen);
2059 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
2064 ldlm_reprocess_all(lease->l_resource,
2065 lease->l_policy_data.l_inodebits.bits);
2066 LDLM_LOCK_PUT(lease);
2074 int mdt_close_handle_layouts(struct mdt_thread_info *info,
2075 struct mdt_object *o, struct md_attr *ma)
2077 struct mdt_lock_handle *lh1 = &info->mti_lh[MDT_LH_NEW];
2078 struct mdt_lock_handle *lh2 = &info->mti_lh[MDT_LH_OLD];
2079 struct close_data *data;
2080 struct ldlm_lock *lease;
2081 struct mdt_object *o1 = o, *o2 = NULL;
2083 bool swap_objects = false;
2087 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2090 if (!S_ISREG(lu_object_attr(&o1->mot_obj)))
2093 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2097 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2100 rc = lu_fid_cmp(&data->cd_fid, mdt_object_fid(o));
2103 * only MDS_CLOSE_LAYOUT_SPLIT use the same fid to indicate
2104 * mirror deletion, so we'd zero cd_fid, and keeps o2 be NULL.
2106 if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT))
2109 /* zero cd_fid to keeps o2 be NULL */
2110 fid_zero(&data->cd_fid);
2111 } else if (rc < 0) {
2112 /* Exchange o1 and o2, to enforce locking order */
2113 swap_objects = true;
2116 lease = ldlm_handle2lock(&data->cd_handle);
2120 if (!fid_is_zero(&data->cd_fid)) {
2121 o2 = mdt_object_find(info->mti_env, info->mti_mdt,
2124 GOTO(out_lease, rc = PTR_ERR(o2));
2126 if (!mdt_object_exists(o2))
2127 GOTO(out_obj, rc = -ENOENT);
2129 if (!S_ISREG(lu_object_attr(&o2->mot_obj)))
2130 GOTO(out_obj, rc = -EINVAL);
2136 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
2142 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2),
2148 /* try to hold open_sem so that nobody else can open the file */
2149 if (!down_write_trylock(&o->mot_open_sem)) {
2150 ldlm_lock_cancel(lease);
2151 GOTO(out_obj, rc = -EBUSY);
2154 /* Check if the lease open lease has already canceled */
2155 lock_res_and_lock(lease);
2156 lease_broken = ldlm_is_cancel(lease);
2157 unlock_res_and_lock(lease);
2159 LDLM_DEBUG(lease, DFID " lease broken? %d",
2160 PFID(mdt_object_fid(o)), lease_broken);
2162 /* Cancel server side lease. Client side counterpart should
2163 * have been cancelled. It's okay to cancel it now as we've
2164 * held mot_open_sem. */
2165 ldlm_lock_cancel(lease);
2168 GOTO(out_unlock_sem, rc = -ESTALE);
2170 mdt_lock_reg_init(lh1, LCK_EX);
2171 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
2172 MDS_INODELOCK_XATTR);
2174 GOTO(out_unlock_sem, rc);
2177 mdt_lock_reg_init(lh2, LCK_EX);
2178 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
2179 MDS_INODELOCK_XATTR);
2181 GOTO(out_unlock1, rc);
2184 /* Swap layout with orphan object */
2185 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SWAP) {
2186 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
2187 mdt_object_child(o2), 0);
2188 } else if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_MERGE ||
2189 ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2190 struct lu_buf *buf = &info->mti_buf;
2191 struct md_rejig_data mrd;
2194 mrd.mrd_obj = mdt_object_child(o == o1 ? o2 : o1);
2196 if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT)) {
2197 /* paranoid check again */
2199 ":only mirror split support NULL o2 object\n",
2200 PFID(mdt_object_fid(o)));
2201 GOTO(out_unlock1, rc = -EINVAL);
2204 /* set NULL mrd_obj for deleting mirror objects */
2208 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2209 mrd.mrd_mirror_id = data->cd_mirror_id;
2210 /* set a small enough blocks in the SoM */
2211 ma->ma_attr.la_blocks >>= 1;
2214 buf->lb_len = sizeof(mrd);
2216 rc = mo_xattr_set(info->mti_env, mdt_object_child(o), buf,
2218 ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT ?
2219 LU_XATTR_SPLIT : LU_XATTR_MERGE);
2220 if (rc == 0 && ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS |
2221 LA_LSIZE | LA_LBLOCKS)) {
2223 enum lustre_som_flags lsf;
2225 if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS))
2226 lsf = SOM_FL_STRICT;
2230 mutex_lock(&o->mot_som_mutex);
2231 rc2 = mdt_set_som(info, o, lsf,
2232 ma->ma_attr.la_size,
2233 ma->ma_attr.la_blocks);
2234 mutex_unlock(&o->mot_som_mutex);
2236 CERROR(DFID": Setting i_blocks error: %d, "
2237 "i_blocks will be reported wrongly and "
2238 "can only be fixed in next resync\n",
2239 PFID(mdt_object_fid(o)), rc2);
2243 GOTO(out_unlock2, rc);
2248 /* Release exclusive LL */
2250 mdt_object_unlock(info, o2, lh2, 1);
2253 mdt_object_unlock(info, o1, lh1, 1);
2256 up_write(&o->mot_open_sem);
2258 /* already swapped */
2260 struct mdt_body *repbody;
2262 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2263 LASSERT(repbody != NULL);
2264 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2269 /* the 2nd object has been used, and swapped to o1 */
2270 mdt_object_put(info->mti_env, o1);
2272 /* the 2nd object has been used, and not swapped */
2273 mdt_object_put(info->mti_env, o2);
2275 ldlm_reprocess_all(lease->l_resource,
2276 lease->l_policy_data.l_inodebits.bits);
2279 LDLM_LOCK_PUT(lease);
2289 static int mdt_close_resync_done(struct mdt_thread_info *info,
2290 struct mdt_object *o, struct md_attr *ma)
2292 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LOCAL];
2293 struct close_data *data;
2294 struct ldlm_lock *lease;
2295 struct md_layout_change layout = { 0 };
2296 __u32 *resync_ids = NULL;
2297 size_t resync_count = 0;
2302 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2305 if (!S_ISREG(lu_object_attr(&o->mot_obj)))
2308 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2312 if (req_capsule_req_need_swab(info->mti_pill))
2313 lustre_swab_close_data_resync_done(&data->cd_resync);
2315 if (!fid_is_zero(&data->cd_fid))
2318 lease = ldlm_handle2lock(&data->cd_handle);
2322 /* try to hold open_sem so that nobody else can open the file */
2323 if (!down_write_trylock(&o->mot_open_sem)) {
2324 ldlm_lock_cancel(lease);
2325 GOTO(out_reprocess, rc = -EBUSY);
2328 /* Check if the lease open lease has already canceled */
2329 lock_res_and_lock(lease);
2330 lease_broken = ldlm_is_cancel(lease);
2331 unlock_res_and_lock(lease);
2333 LDLM_DEBUG(lease, DFID " lease broken? %d",
2334 PFID(mdt_object_fid(o)), lease_broken);
2336 /* Cancel server side lease. Client side counterpart should
2337 * have been cancelled. It's okay to cancel it now as we've
2338 * held mot_open_sem. */
2339 ldlm_lock_cancel(lease);
2341 if (lease_broken) /* don't perform release task */
2342 GOTO(out_unlock, rc = -ESTALE);
2344 resync_count = data->cd_resync.resync_count;
2346 if (resync_count > INLINE_RESYNC_ARRAY_SIZE) {
2349 if (!req_capsule_has_field(info->mti_pill, &RMF_U32,
2351 GOTO(out_unlock, rc = -EPROTO);
2353 OBD_ALLOC_PTR_ARRAY(resync_ids, resync_count);
2355 GOTO(out_unlock, rc = -ENOMEM);
2357 data = req_capsule_client_get(info->mti_pill, &RMF_U32);
2358 memcpy(resync_ids, data, resync_count * sizeof(__u32));
2360 layout.mlc_resync_ids = resync_ids;
2362 layout.mlc_resync_ids = data->cd_resync.resync_ids_inline;
2365 layout.mlc_opc = MD_LAYOUT_RESYNC_DONE;
2366 layout.mlc_resync_count = resync_count;
2367 if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2368 layout.mlc_som.lsa_valid = SOM_FL_STRICT;
2369 layout.mlc_som.lsa_size = ma->ma_attr.la_size;
2370 layout.mlc_som.lsa_blocks = ma->ma_attr.la_blocks;
2372 rc = mdt_layout_change(info, o, lhc, &layout);
2374 GOTO(out_unlock, rc);
2376 mdt_object_unlock(info, o, lhc, 0);
2381 up_write(&o->mot_open_sem);
2383 /* already released */
2385 struct mdt_body *repbody;
2387 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2388 LASSERT(repbody != NULL);
2389 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2393 OBD_FREE_PTR_ARRAY(resync_ids, resync_count);
2396 ldlm_reprocess_all(lease->l_resource,
2397 lease->l_policy_data.l_inodebits.bits);
2398 LDLM_LOCK_PUT(lease);
2406 #define MFD_CLOSED(open_flags) ((open_flags) == MDS_FMODE_CLOSED)
2408 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2410 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_open_flags));
2413 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2415 struct mdt_object *o = mfd->mfd_object;
2416 struct md_object *next = mdt_object_child(o);
2417 struct md_attr *ma = &info->mti_attr;
2418 struct lu_fid *ofid = &info->mti_tmp_fid1;
2425 open_flags = mfd->mfd_open_flags;
2426 intent = ma->ma_attr_flags & MDS_CLOSE_INTENT;
2427 *ofid = *mdt_object_fid(o);
2429 /* the below message is checked in replay-single.sh test_46 */
2430 CDEBUG(D_INODE, "%s: %sclosing file handle "DFID" with intent: %llx\n",
2431 mdt_obd_name(info->mti_mdt),
2432 ma->ma_valid & MA_FORCE_LOG ? "force " : "", PFID(ofid), intent);
2435 case MDS_HSM_RELEASE: {
2436 rc = mdt_hsm_release(info, o, ma);
2438 CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2439 mdt_obd_name(info->mti_mdt),
2441 /* continue to close even error occurred. */
2445 case MDS_CLOSE_LAYOUT_MERGE:
2446 case MDS_CLOSE_LAYOUT_SPLIT:
2447 case MDS_CLOSE_LAYOUT_SWAP: {
2448 rc = mdt_close_handle_layouts(info, o, ma);
2451 "%s: cannot %s layout of "DFID": rc = %d\n",
2452 mdt_obd_name(info->mti_mdt),
2453 intent == MDS_CLOSE_LAYOUT_MERGE ? "merge" :
2454 intent == MDS_CLOSE_LAYOUT_SPLIT ? "split" :
2457 /* continue to close even if error occurred. */
2461 case MDS_CLOSE_RESYNC_DONE:
2462 rc = mdt_close_resync_done(info, o, ma);
2465 "%s: cannot resync layout of "DFID": rc = %d\n",
2466 mdt_obd_name(info->mti_mdt),
2468 /* continue to close even if error occurred. */
2476 if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
2477 ma->ma_attr.la_valid & (LA_LSIZE | LA_LBLOCKS)) {
2478 rc2 = mdt_lsom_update(info, o, false);
2481 "%s: File " DFID " LSOM failed: rc = %d\n",
2482 mdt_obd_name(info->mti_mdt),
2486 /* continue to close even if error occurred. */
2490 if (open_flags & MDS_FMODE_WRITE)
2492 else if (open_flags & MDS_FMODE_EXEC)
2495 /* Update atime|mtime|ctime on close. */
2496 if ((open_flags & MDS_FMODE_EXEC || open_flags & MDS_FMODE_READ ||
2497 open_flags & MDS_FMODE_WRITE) && (ma->ma_valid & MA_INODE) &&
2498 (ma->ma_attr.la_valid & LA_ATIME ||
2499 ma->ma_attr.la_valid & LA_MTIME ||
2500 ma->ma_attr.la_valid & LA_CTIME)) {
2501 ma->ma_valid = MA_INODE;
2502 ma->ma_attr_flags |= MDS_CLOSE_UPDATE_TIMES;
2503 ma->ma_attr.la_valid &= (LA_ATIME | LA_MTIME | LA_CTIME);
2505 if (ma->ma_attr.la_valid & LA_MTIME) {
2506 if (mdt_attr_get_pfid(info, o, &ma->ma_pfid) == 0)
2507 ma->ma_valid |= MA_PFID;
2510 rc2 = mo_attr_set(info->mti_env, next, ma);
2513 "%s: File "DFID" set attr (%#llx) failed: rc = %d\n",
2514 mdt_obd_name(info->mti_mdt), PFID(ofid),
2515 ma->ma_attr.la_valid, rc2);
2521 /* If file data is modified, add the dirty flag. */
2522 if (ma->ma_attr_flags & MDS_DATA_MODIFIED) {
2523 rc2 = mdt_add_dirty_flag(info, o, ma);
2526 "%s: File "DFID" add dirty flag failed: rc = %d\n",
2527 mdt_obd_name(info->mti_mdt), PFID(ofid), rc2);
2533 ma->ma_need |= MA_INODE;
2534 ma->ma_valid &= ~MA_INODE;
2536 LASSERT(atomic_read(&o->mot_open_count) > 0);
2537 atomic_dec(&o->mot_open_count);
2538 mdt_handle_last_unlink(info, o, ma);
2540 if (!MFD_CLOSED(open_flags)) {
2541 rc2 = mo_close(info->mti_env, next, ma, open_flags);
2544 "%s: File "DFID" close failed: rc = %d\n",
2545 mdt_obd_name(info->mti_mdt), PFID(ofid), rc2);
2549 if (mdt_dom_check_for_discard(info, o))
2550 mdt_dom_discard_data(info, o);
2553 /* adjust open and lease count */
2554 if (open_flags & MDS_OPEN_LEASE) {
2555 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2556 atomic_dec(&o->mot_lease_count);
2560 mdt_object_put(info->mti_env, o);
2565 int mdt_close_internal(struct mdt_thread_info *info, struct ptlrpc_request *req,
2566 struct mdt_body *repbody)
2568 struct mdt_export_data *med;
2569 struct mdt_file_data *mfd;
2574 med = &req->rq_export->exp_mdt_data;
2575 spin_lock(&med->med_open_lock);
2576 mfd = mdt_open_handle2mfd(med, &info->mti_open_handle,
2577 req_is_replay(req));
2578 if (mdt_mfd_closed(mfd)) {
2579 spin_unlock(&med->med_open_lock);
2580 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2581 ": cookie = %#llx\n", PFID(info->mti_rr.rr_fid1),
2582 info->mti_open_handle.cookie);
2583 /** not serious error since bug 3633 */
2586 class_handle_unhash(&mfd->mfd_open_handle);
2587 list_del_init(&mfd->mfd_list);
2588 spin_unlock(&med->med_open_lock);
2589 ret = mdt_mfd_close(info, mfd);
2592 RETURN(rc ? rc : ret);
2595 int mdt_close(struct tgt_session_info *tsi)
2597 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2598 struct ptlrpc_request *req = tgt_ses_req(tsi);
2599 struct md_attr *ma = &info->mti_attr;
2600 struct mdt_body *repbody = NULL;
2601 ktime_t kstart = ktime_get();
2606 /* Close may come with the Size-on-MDS update. Unpack it. */
2607 rc = mdt_close_unpack(info);
2609 GOTO(out, rc = err_serious(rc));
2611 /* These fields are no longer used and are left for compatibility.
2612 * size is always zero */
2613 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2615 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2617 rc = req_capsule_server_pack(info->mti_pill);
2618 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2619 mdt_client_compatibility(info);
2621 mdt_fix_reply(info);
2622 mdt_exit_ucred(info);
2623 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2626 /* Continue to close handle even if we can not pack reply */
2628 repbody = req_capsule_server_get(info->mti_pill,
2630 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2632 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2635 ma->ma_need = MA_INODE | MA_LOV;
2636 repbody->mbo_eadatasize = 0;
2637 repbody->mbo_aclsize = 0;
2639 rc = err_serious(rc);
2642 rc2 = mdt_close_internal(info, req, repbody);
2644 mdt_empty_transno(info, rc2);
2645 if (rc2 != 0 && rc == 0)
2648 if (repbody != NULL) {
2649 mdt_client_compatibility(info);
2650 rc2 = mdt_fix_reply(info);
2651 if (rc2 != 0 && rc == 0)
2655 mdt_exit_ucred(info);
2656 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2657 GOTO(out, rc = err_serious(-ENOMEM));
2659 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2660 OBD_FAIL_MDS_CLOSE_NET_REP))
2661 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2663 mdt_thread_info_fini(info);
2665 mdt_counter_incr(req, LPROC_MDT_CLOSE,
2666 ktime_us_delta(ktime_get(), kstart));