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 /* Create a new mdt_file_data struct, initialize it,
49 * and insert it to global hash table */
50 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med)
52 struct mdt_file_data *mfd;
57 refcount_set(&mfd->mfd_open_handle.h_ref, 1);
58 INIT_HLIST_NODE(&mfd->mfd_open_handle.h_link);
60 INIT_LIST_HEAD(&mfd->mfd_list);
61 class_handle_hash(&mfd->mfd_open_handle, mfd_open_handle_owner);
68 * Find the mfd pointed to by handle in global hash table.
69 * In case of replay the handle is obsoleted
70 * but mfd can be found in mfd list by that handle.
71 * Callers need to be holding med_open_lock.
73 struct mdt_file_data *mdt_open_handle2mfd(struct mdt_export_data *med,
74 const struct lustre_handle *open_handle,
75 bool is_replay_or_resent)
77 struct mdt_file_data *mfd;
80 LASSERT(open_handle != NULL);
81 mfd = class_handle2object(open_handle->cookie, mfd_open_handle_owner);
83 refcount_dec(&mfd->mfd_open_handle.h_ref);
85 /* during dw/setattr replay the mfd can be found by old handle */
86 if ((!mfd || mfd->mfd_owner != med) && is_replay_or_resent) {
87 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
88 if (mfd->mfd_open_handle_old.cookie ==
99 void mdt_mfd_free(struct mdt_file_data *mfd)
101 LASSERT(refcount_read(&mfd->mfd_open_handle.h_ref) == 1);
102 LASSERT(list_empty(&mfd->mfd_list));
103 OBD_FREE_PRE(mfd, sizeof(*mfd), "rcu");
104 kfree_rcu(mfd, mfd_open_handle.h_rcu);
107 static int mdt_create_data(struct mdt_thread_info *info,
108 struct mdt_object *p, struct mdt_object *o)
110 struct md_op_spec *spec = &info->mti_spec;
111 struct md_attr *ma = &info->mti_attr;
115 if (!md_should_create(spec->sp_cr_flags))
118 ma->ma_need = MA_INODE | MA_LOV;
120 mutex_lock(&o->mot_lov_mutex);
121 if (!o->mot_lov_created) {
122 rc = mdo_create_data(info->mti_env,
123 p ? mdt_object_child(p) : NULL,
124 mdt_object_child(o), spec, ma);
126 rc = mdt_attr_get_complex(info, o, ma);
128 if (rc == 0 && ma->ma_valid & MA_LOV)
129 o->mot_lov_created = 1;
132 mutex_unlock(&o->mot_lov_mutex);
136 int mdt_write_read(struct mdt_object *o)
140 spin_lock(&o->mot_write_lock);
141 rc = o->mot_write_count;
142 spin_unlock(&o->mot_write_lock);
146 int mdt_write_get(struct mdt_object *o)
150 spin_lock(&o->mot_write_lock);
151 if (o->mot_write_count < 0)
154 o->mot_write_count++;
155 spin_unlock(&o->mot_write_lock);
160 void mdt_write_put(struct mdt_object *o)
163 spin_lock(&o->mot_write_lock);
164 o->mot_write_count--;
165 spin_unlock(&o->mot_write_lock);
169 static int mdt_write_deny(struct mdt_object *o)
173 spin_lock(&o->mot_write_lock);
174 if (o->mot_write_count > 0)
177 o->mot_write_count--;
178 spin_unlock(&o->mot_write_lock);
182 static void mdt_write_allow(struct mdt_object *o)
185 spin_lock(&o->mot_write_lock);
186 o->mot_write_count++;
187 spin_unlock(&o->mot_write_lock);
191 /* there can be no real transaction so prepare the fake one */
192 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
194 struct mdt_device *mdt = info->mti_mdt;
195 struct ptlrpc_request *req = mdt_info_req(info);
196 struct tg_export_data *ted;
197 struct lsd_client_data *lcd;
200 if (mdt_rdonly(req->rq_export))
203 /* transaction has occurred already */
204 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
207 if (tgt_is_multimodrpcs_client(req->rq_export)) {
210 /* generate an empty transaction to get a transno
212 th = dt_trans_create(info->mti_env, mdt->mdt_bottom);
214 rc = dt_trans_start(info->mti_env, mdt->mdt_bottom, th);
215 dt_trans_stop(info->mti_env, mdt->mdt_bottom, th);
220 spin_lock(&mdt->mdt_lut.lut_translock);
222 if (info->mti_transno != 0) {
223 struct obd_export *exp = req->rq_export;
225 CERROR("%s: replay trans %llu NID %s: rc = %d\n",
226 mdt_obd_name(mdt), info->mti_transno,
227 obd_export_nid2str(exp), rc);
228 spin_unlock(&mdt->mdt_lut.lut_translock);
231 } else if (info->mti_transno == 0) {
232 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
234 /* should be replay */
235 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
236 mdt->mdt_lut.lut_last_transno = info->mti_transno;
238 spin_unlock(&mdt->mdt_lut.lut_translock);
240 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
242 req->rq_export->exp_obd->obd_last_committed);
244 req->rq_transno = info->mti_transno;
245 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
247 /* update lcd in memory only for resent cases */
248 ted = &req->rq_export->exp_target_data;
250 mutex_lock(&ted->ted_lcd_lock);
252 if (info->mti_transno < lcd->lcd_last_transno &&
253 info->mti_transno != 0) {
254 /* This should happen during replay. Do not update
255 * last rcvd info if replay req transno < last transno,
256 * otherwise the following resend(after replay) can not
257 * be checked correctly by xid */
258 mutex_unlock(&ted->ted_lcd_lock);
259 CDEBUG(D_HA, "%s: transno = %llu < last_transno = %llu\n",
260 mdt_obd_name(mdt), info->mti_transno,
261 lcd->lcd_last_transno);
265 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
266 if (info->mti_transno != 0)
267 lcd->lcd_last_close_transno = info->mti_transno;
268 lcd->lcd_last_close_xid = req->rq_xid;
269 lcd->lcd_last_close_result = rc;
271 /* VBR: save versions in last_rcvd for reconstruct. */
272 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
274 lcd->lcd_pre_versions[0] = pre_versions[0];
275 lcd->lcd_pre_versions[1] = pre_versions[1];
276 lcd->lcd_pre_versions[2] = pre_versions[2];
277 lcd->lcd_pre_versions[3] = pre_versions[3];
279 if (info->mti_transno != 0)
280 lcd->lcd_last_transno = info->mti_transno;
282 lcd->lcd_last_xid = req->rq_xid;
283 lcd->lcd_last_result = rc;
284 lcd->lcd_last_data = info->mti_opdata;
286 mutex_unlock(&ted->ted_lcd_lock);
291 void mdt_mfd_set_mode(struct mdt_file_data *mfd, u64 open_flags)
293 LASSERT(mfd != NULL);
295 CDEBUG(D_DENTRY, DFID " Change mfd open_flags %#llo -> %#llo.\n",
296 PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_open_flags,
299 mfd->mfd_open_flags = open_flags;
303 * prep ma_lmm/ma_lmv for md_attr from reply
305 static void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
306 struct mdt_object *obj,
309 if (ma->ma_lmv || ma->ma_lmm) {
310 CDEBUG(D_INFO, DFID " %s already set.\n",
311 PFID(mdt_object_fid(obj)),
312 ma->ma_lmv ? (ma->ma_lmm ? "ma_lmv and ma_lmm"
318 if (S_ISDIR(obj->mot_header.loh_attr)) {
319 ma->ma_lmv = req_capsule_server_get(info->mti_pill,
321 ma->ma_lmv_size = req_capsule_get_size(info->mti_pill,
324 if (ma->ma_lmv_size > 0)
325 ma->ma_need |= MA_LMV;
327 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
329 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
332 if (ma->ma_lmm_size > 0)
333 ma->ma_need |= MA_LOV;
337 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
338 struct mdt_object *o, u64 open_flags, int created,
339 struct ldlm_reply *rep)
341 struct ptlrpc_request *req = mdt_info_req(info);
342 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
343 struct mdt_file_data *mfd;
344 struct md_attr *ma = &info->mti_attr;
345 struct lu_attr *la = &ma->ma_attr;
346 struct mdt_body *repbody;
351 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
353 isreg = S_ISREG(la->la_mode);
354 isdir = S_ISDIR(la->la_mode);
355 if (isreg && !(ma->ma_valid & MA_LOV) &&
356 !(open_flags & MDS_OPEN_RELEASE)) {
358 * No EA, check whether it is will set regEA and dirEA since in
359 * above attr get, these size might be zero, so reset it, to
360 * retrieve the MD after create obj.
362 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
365 /* in replay case, p == NULL */
366 rc = mdt_create_data(info, p, o);
370 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
371 mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
374 CDEBUG(D_INODE, "after open, ma_valid bit = %#llx lmm_size = %d\n",
375 ma->ma_valid, ma->ma_lmm_size);
377 if (ma->ma_valid & MA_LOV) {
378 LASSERT(ma->ma_lmm_size != 0);
379 repbody->mbo_eadatasize = ma->ma_lmm_size;
381 repbody->mbo_valid |= OBD_MD_FLDIREA;
383 repbody->mbo_valid |= OBD_MD_FLEASIZE;
386 if (ma->ma_valid & MA_LMV) {
387 LASSERT(ma->ma_lmv_size != 0);
388 repbody->mbo_eadatasize = ma->ma_lmv_size;
390 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
393 if (open_flags & MDS_FMODE_WRITE)
394 rc = mdt_write_get(o);
395 else if (open_flags & MDS_FMODE_EXEC)
396 rc = mdt_write_deny(o);
401 rc = mo_open(info->mti_env, mdt_object_child(o),
402 created ? open_flags | MDS_OPEN_CREATED : open_flags,
405 /* If we allow the client to chgrp (CFS_SETGRP_PERM), but the
406 * client does not know which suppgid should be sent to the MDS,
407 * or some other(s) changed the target file's GID after this RPC
408 * sent to the MDS with the suppgid as the original GID, then we
409 * give the client another chance to send the right suppgid. */
411 allow_client_chgrp(info, lu_ucred(info->mti_env)))
412 mdt_set_disposition(info, rep, DISP_OPEN_DENY);
417 mfd = mdt_mfd_new(med);
419 GOTO(err_out, rc = -ENOMEM);
422 * Keep a reference on this object for this open, and is
423 * released by mdt_mfd_close().
425 mdt_object_get(info->mti_env, o);
427 mfd->mfd_xid = req->rq_xid;
430 * @open_flags is always not zero. At least it should be FMODE_READ,
431 * FMODE_WRITE or MDS_FMODE_EXEC.
433 LASSERT(open_flags != 0);
436 mdt_mfd_set_mode(mfd, open_flags);
438 atomic_inc(&o->mot_open_count);
439 if (open_flags & MDS_OPEN_LEASE)
440 atomic_inc(&o->mot_lease_count);
443 if (req_is_replay(req)) {
444 struct mdt_file_data *old_mfd;
445 /* Check wheather old cookie already exist in
446 * the list, becasue when do recovery, client
447 * might be disconnected from server, and
448 * restart replay, so there maybe some orphan
449 * mfd here, we should remove them */
450 LASSERT(info->mti_rr.rr_open_handle != NULL);
451 spin_lock(&med->med_open_lock);
452 old_mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
454 if (old_mfd != NULL) {
455 CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
456 "cookie = %#llx\n", mfd,
457 PFID(mdt_object_fid(mfd->mfd_object)),
458 info->mti_rr.rr_open_handle->cookie);
459 class_handle_unhash(&old_mfd->mfd_open_handle);
460 list_del_init(&old_mfd->mfd_list);
461 spin_unlock(&med->med_open_lock);
462 /* no attr update for that close */
464 ma->ma_valid |= MA_FLAGS;
465 ma->ma_attr_flags |= MDS_RECOV_OPEN;
466 mdt_mfd_close(info, old_mfd);
467 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
468 ma->ma_valid &= ~MA_FLAGS;
470 spin_unlock(&med->med_open_lock);
471 CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
473 PFID(mdt_object_fid(mfd->mfd_object)),
474 info->mti_rr.rr_open_handle->cookie);
477 CDEBUG(D_HA, "Store old cookie %#llx in new mfd\n",
478 info->mti_rr.rr_open_handle->cookie);
480 mfd->mfd_open_handle_old = *info->mti_rr.rr_open_handle;
483 repbody->mbo_open_handle.cookie = mfd->mfd_open_handle.h_cookie;
485 if (req->rq_export->exp_disconnected) {
486 spin_lock(&med->med_open_lock);
487 class_handle_unhash(&mfd->mfd_open_handle);
488 list_del_init(&mfd->mfd_list);
489 spin_unlock(&med->med_open_lock);
490 mdt_mfd_close(info, mfd);
492 spin_lock(&med->med_open_lock);
493 list_add_tail(&mfd->mfd_list, &med->med_open_head);
494 spin_unlock(&med->med_open_lock);
497 mdt_empty_transno(info, rc);
502 if (open_flags & MDS_FMODE_WRITE)
504 else if (open_flags & MDS_FMODE_EXEC)
510 static int mdt_finish_open(struct mdt_thread_info *info,
511 struct mdt_object *p, struct mdt_object *o,
512 u64 open_flags, int created,
513 struct ldlm_reply *rep)
515 struct ptlrpc_request *req = mdt_info_req(info);
516 struct obd_export *exp = req->rq_export;
517 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
518 struct md_attr *ma = &info->mti_attr;
519 struct lu_attr *la = &ma->ma_attr;
520 struct mdt_file_data *mfd;
521 struct mdt_body *repbody;
523 int isreg, isdir, islnk;
527 LASSERT(ma->ma_valid & MA_INODE);
529 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
531 isreg = S_ISREG(la->la_mode);
532 isdir = S_ISDIR(la->la_mode);
533 islnk = S_ISLNK(la->la_mode);
534 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
536 /* compatibility check for 2.10 clients when it tries to open mirrored
537 * files. 2.10 clients don't verify overlapping components so they
538 * would read and write mirrored files just as if they were normal
539 * PFL files, which will cause the problem that sycned mirrors actually
540 * contain different data.
541 * Older clients are not a concern here because they don't even
542 * understand PFL layout. */
543 if (isreg && !exp_connect_flr(exp) && ma->ma_valid & MA_LOV &&
544 mdt_lmm_is_flr(ma->ma_lmm)) {
545 /* LU-10286: for simplicity clients who don't understand
546 * mirrored layout(with connect flag OBD_CONNECT2_FLR) won't
547 * be able to open mirrored files */
551 /* Overstriped files can crash older clients */
552 if (isreg && !exp_connect_overstriping(exp) &&
553 mdt_lmm_is_overstriping(ma->ma_lmm))
556 /* LU-2275, simulate broken behaviour (esp. prevalent in
557 * pre-2.4 servers where a very strange reply is sent on error
558 * that looks like it was actually almost successful and a
559 * failure at the same time.) */
560 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
561 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
565 if (open_flags & MDS_OPEN_LOCK)
566 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
571 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
572 if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
573 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
575 RETURN(PTR_ERR(nodemap));
577 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
578 nodemap_putref(nodemap);
585 * If we are following a symlink, don't open; and do not return open
586 * handle for special nodes as client required.
588 if (islnk || (!isreg && !isdir &&
589 (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
590 lustre_msg_set_transno(req->rq_repmsg, 0);
595 * We need to return the existing object's fid back, so it is done here,
596 * after preparing the reply.
598 if (!created && (open_flags & MDS_OPEN_EXCL) &&
599 (open_flags & MDS_OPEN_CREAT))
602 /* This can't be done earlier, we need to return reply body */
604 if (open_flags & (MDS_OPEN_CREAT | MDS_FMODE_WRITE)) {
605 /* We are trying to create or write an existing dir. */
608 } else if (open_flags & MDS_OPEN_DIRECTORY)
611 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
612 OBD_FAIL_MDS_LDLM_REPLY_NET | OBD_FAIL_ONCE))
616 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
617 spin_lock(&med->med_open_lock);
618 list_for_each(t, &med->med_open_head) {
619 mfd = list_entry(t, struct mdt_file_data, mfd_list);
620 if (mfd->mfd_xid == req->rq_xid)
624 spin_unlock(&med->med_open_lock);
627 repbody->mbo_open_handle.cookie =
628 mfd->mfd_open_handle.h_cookie;
629 /* set repbody->ea_size for resent case */
630 if (ma->ma_valid & MA_LOV) {
631 LASSERT(ma->ma_lmm_size != 0);
632 repbody->mbo_eadatasize = ma->ma_lmm_size;
634 repbody->mbo_valid |= OBD_MD_FLDIREA;
636 repbody->mbo_valid |= OBD_MD_FLEASIZE;
638 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
643 rc = mdt_mfd_open(info, p, o, open_flags, created, rep);
645 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
650 void mdt_reconstruct_open(struct mdt_thread_info *info,
651 struct mdt_lock_handle *lhc)
653 const struct lu_env *env = info->mti_env;
654 struct mdt_device *mdt = info->mti_mdt;
655 struct req_capsule *pill = info->mti_pill;
656 struct ptlrpc_request *req = mdt_info_req(info);
657 struct md_attr *ma = &info->mti_attr;
658 struct mdt_reint_record *rr = &info->mti_rr;
659 u64 open_flags = info->mti_spec.sp_cr_flags;
660 struct ldlm_reply *ldlm_rep;
661 struct mdt_object *parent;
662 struct mdt_object *child;
663 struct mdt_body *repbody;
668 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
669 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
670 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
672 ma->ma_need = MA_INODE | MA_HSM;
675 opdata = mdt_req_from_lrd(req, info->mti_reply_data);
676 mdt_set_disposition(info, ldlm_rep, opdata);
678 CDEBUG(D_INODE, "This is reconstruct open: disp=%#llx, result=%d\n",
679 ldlm_rep->lock_policy_res1, req->rq_status);
681 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
683 /* We did not create successfully, return error to client. */
684 GOTO(out, rc = req->rq_status);
686 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
687 struct obd_export *exp = req->rq_export;
689 * We failed after creation, but we do not know in which step
690 * we failed. So try to check the child object.
692 parent = mdt_object_find(env, mdt, rr->rr_fid1);
693 if (IS_ERR(parent)) {
694 rc = PTR_ERR(parent);
695 LCONSOLE_WARN("Parent "DFID" lookup error %d."
696 " Evicting client %s with export %s.\n",
697 PFID(rr->rr_fid1), rc,
698 obd_uuid2str(&exp->exp_client_uuid),
699 obd_export_nid2str(exp));
700 mdt_export_evict(exp);
704 child = mdt_object_find(env, mdt, rr->rr_fid2);
707 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
708 "evicting client %s with export %s\n",
709 PFID(rr->rr_fid2), rc,
710 obd_uuid2str(&exp->exp_client_uuid),
711 obd_export_nid2str(exp));
712 mdt_object_put(env, parent);
713 mdt_export_evict(exp);
717 if (unlikely(mdt_object_remote(child))) {
718 mdt_object_put(env, parent);
719 mdt_object_put(env, child);
720 /* the child object was created on remote server */
721 if (!mdt_is_dne_client(exp))
722 /* Return -EIO for old client */
723 GOTO(out, rc = -EIO);
724 repbody->mbo_fid1 = *rr->rr_fid2;
725 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
728 if (mdt_object_exists(child)) {
729 mdt_prep_ma_buf_from_rep(info, child, ma);
730 rc = mdt_attr_get_complex(info, child, ma);
732 rc = mdt_finish_open(info, parent, child,
733 open_flags, 1, ldlm_rep);
734 mdt_object_put(env, parent);
735 mdt_object_put(env, child);
737 mdt_pack_size2body(info, rr->rr_fid2,
741 /* the child does not exist, we should do regular open */
742 mdt_object_put(env, parent);
743 mdt_object_put(env, child);
745 /* We did not try to create, so we are a pure open */
746 rc = mdt_reint_open(info, lhc);
750 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
751 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
754 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep)
756 u64 open_flags = info->mti_spec.sp_cr_flags;
757 struct mdt_reint_record *rr = &info->mti_rr;
758 struct md_attr *ma = &info->mti_attr;
759 struct mdt_object *o;
763 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
765 RETURN(rc = PTR_ERR(o));
767 if (unlikely(mdt_object_remote(o))) {
768 /* the child object was created on remote server */
769 struct mdt_body *repbody;
771 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
774 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
775 repbody->mbo_fid1 = *rr->rr_fid2;
776 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
779 if (mdt_object_exists(o)) {
780 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
783 mdt_prep_ma_buf_from_rep(info, o, ma);
784 rc = mdt_attr_get_complex(info, o, ma);
786 rc = mdt_finish_open(info, NULL, o, open_flags,
793 mdt_object_put(info->mti_env, o);
797 /* lock object for open */
798 static int mdt_object_open_lock(struct mdt_thread_info *info,
799 struct mdt_object *obj,
800 struct mdt_lock_handle *lhc,
803 struct md_attr *ma = &info->mti_attr;
804 __u64 open_flags = info->mti_spec.sp_cr_flags;
806 enum ldlm_mode lm = LCK_CR;
807 bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
808 bool try_layout = false;
809 bool create_layout = false;
811 __u32 dom_stripe = 0;
812 unsigned int dom_only = 0;
813 unsigned int dom_lock = 0;
818 mdt_lock_handle_init(lhc);
820 if (req_is_replay(mdt_info_req(info)))
823 if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
824 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
825 md_should_create(open_flags))
826 create_layout = true;
827 if (exp_connect_layout(info->mti_exp) && !create_layout &&
828 ma->ma_need & MA_LOV)
831 /* DoM files can take IO lock at OPEN when it makes sense,
832 * check if file has DoM stripe and ask for lock if client
833 * no lock on that resource yet.
835 if (ma->ma_valid & MA_LOV && ma->ma_lmm != NULL)
836 dom_stripe = mdt_lmm_dom_entry_check(ma->ma_lmm,
838 /* If only DOM stripe is being used then we can expect IO
839 * to it after OPEN and will return corresponding DOM ibit
840 * using default strategy from mdt_opts.mo_dom_lock.
841 * Otherwise trylock mode is used always and DOM ibit will
842 * be returned optionally.
845 !mdt_dom_client_has_lock(info, mdt_object_fid(obj)))
846 dom_lock = !dom_only ? TRYLOCK_DOM_ON_OPEN :
847 info->mti_mdt->mdt_opts.mo_dom_lock;
851 /* lease open, acquire write mode of open sem */
852 down_write(&obj->mot_open_sem);
854 /* Lease exists and ask for new lease */
855 if (atomic_read(&obj->mot_lease_count) > 0) {
856 /* only exclusive open is supported, so lease
857 * are conflicted to each other */
858 GOTO(out, rc = -EBUSY);
861 /* Lease must be with open lock */
862 if (!(open_flags & MDS_OPEN_LOCK)) {
863 CERROR("%s: Request lease for file:"DFID ", but open lock "
864 "is missed, open_flags = %#llo : rc = %d\n",
865 mdt_obd_name(info->mti_mdt),
866 PFID(mdt_object_fid(obj)), open_flags, -EPROTO);
867 GOTO(out, rc = -EPROTO);
870 /* should conflict with new opens for write/execute */
872 *ibits = MDS_INODELOCK_OPEN;
874 /* never grant LCK_EX layout lock to client */
876 } else { /* normal open */
877 /* normal open holds read mode of open sem */
878 down_read(&obj->mot_open_sem);
880 if (open_flags & MDS_OPEN_LOCK) {
881 if (open_flags & MDS_FMODE_WRITE)
883 else if (open_flags & MDS_FMODE_EXEC)
888 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
889 } else if (atomic_read(&obj->mot_lease_count) > 0) {
890 if (open_flags & MDS_FMODE_WRITE)
896 *ibits = MDS_INODELOCK_OPEN;
899 lhc = &info->mti_lh[MDT_LH_LOCAL];
900 } else if (dom_lock) {
901 lm = (open_flags & MDS_FMODE_WRITE) ? LCK_PW : LCK_PR;
902 trybits |= MDS_INODELOCK_DOM | MDS_INODELOCK_LAYOUT;
905 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
906 PFID(mdt_object_fid(obj)),
907 atomic_read(&obj->mot_lease_count), lm);
910 mdt_lock_reg_init(lhc, lm);
912 /* Return lookup lock to validate inode at the client side.
913 * This is pretty important otherwise MDT will return layout
914 * lock for each open.
915 * However this is a double-edged sword because changing
916 * permission will revoke a huge number of LOOKUP locks.
918 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
919 if (!(*ibits & MDS_INODELOCK_LOOKUP))
920 trybits |= MDS_INODELOCK_LOOKUP;
921 trybits |= MDS_INODELOCK_LAYOUT;
924 if (*ibits | trybits)
925 rc = mdt_object_lock_try(info, obj, lhc, ibits, trybits, false);
927 CDEBUG(D_INODE, "%s: Requested bits lock:"DFID ", ibits = %#llx/%#llx"
928 ", open_flags = %#llo, try_layout = %d : rc = %d\n",
929 mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(obj)),
930 *ibits, trybits, open_flags, try_layout, rc);
932 /* will change layout, revoke layout locks by enqueuing EX lock. */
933 if (rc == 0 && create_layout) {
934 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
936 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
937 ", open_flags = %#llo\n",
938 PFID(mdt_object_fid(obj)), open_flags);
940 /* We cannot enqueue another lock for the same resource we
941 * already have a lock for, due to mechanics of waiting list
942 * iterating in ldlm, see LU-3601.
943 * As such we'll drop the open lock we just got above here,
944 * it's ok not to have this open lock as it's main purpose is to
945 * flush unused cached client open handles. */
946 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
947 mdt_object_unlock(info, obj, lhc, 1);
949 LASSERT(!try_layout);
950 mdt_lock_handle_init(ll);
951 mdt_lock_reg_init(ll, LCK_EX);
952 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT);
954 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
957 /* Check if there is any other open handles after acquiring
958 * open lock. At this point, caching open handles have been revoked
960 * XXX: Now only exclusive open is supported. Need to check the
961 * type of open for generic lease support. */
962 if (rc == 0 && acq_lease) {
963 struct ptlrpc_request *req = mdt_info_req(info);
964 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
965 struct mdt_file_data *mfd;
966 bool is_replay_or_resent;
969 /* For lease: application can open a file and then apply lease,
970 * @handle contains original open handle in that case.
971 * In recovery, open REQ will be replayed and the lease REQ may
972 * be resent that means the open handle is already stale, so we
973 * need to fix it up here by finding new handle. */
974 is_replay_or_resent = req_is_replay(req) ||
975 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
977 /* if the request is _not_ a replay request, rr_open_handle
978 * may be used to hold an open file handle which is issuing the
979 * lease request, so that this openhandle doesn't count. */
980 mfd = mdt_open_handle2mfd(med, info->mti_rr.rr_open_handle,
981 is_replay_or_resent);
985 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
986 PFID(mdt_object_fid(obj)),
987 atomic_read(&obj->mot_open_count), open_count);
989 if (atomic_read(&obj->mot_open_count) > open_count) {
990 /* fail if anyone *else* has opened file for write */
991 if (mdt_write_read(obj) > 1)
992 GOTO(out, rc = -EBUSY);
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)
1006 __u64 open_flags = info->mti_spec.sp_cr_flags;
1007 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
1010 if (req_is_replay(mdt_info_req(info)))
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);
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);
1025 if (open_flags & MDS_OPEN_LEASE)
1026 up_write(&obj->mot_open_sem);
1028 up_read(&obj->mot_open_sem);
1030 /* Cross-ref case, the lock should be returned to the client */
1031 if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
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. */
1041 if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1042 struct ldlm_reply *ldlm_rep;
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);
1053 * Check release is permitted for the current HSM flags.
1055 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1057 if (!(ma->ma_valid & MA_HSM))
1060 if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1063 if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1069 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1070 struct ldlm_reply *rep,
1071 struct mdt_lock_handle *lhc)
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;
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));
1097 ma->ma_need |= MA_PFID;
1100 o = mdt_object_find(env, mdt, rr->rr_fid2);
1102 GOTO(out_parent_put, rc = PTR_ERR(o));
1104 if (mdt_object_remote(o)) {
1105 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1106 mdt_obd_name(info->mti_mdt),
1108 GOTO(out, rc = -EREMOTE);
1109 } else if (!mdt_object_exists(o)) {
1110 mdt_set_disposition(info, rep,
1114 GOTO(out, rc = -ENOENT);
1117 mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
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);
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);
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);
1136 rc = mdt_check_resent_lock(info, o, lhc);
1139 } else if (rc > 0) {
1140 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1141 object_locked = true;
1143 GOTO(out_unlock, rc);
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));
1157 rc = mdt_finish_open(info, parent, o, open_flags, 0, rep);
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);
1165 GOTO(out_unlock, rc);
1169 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1171 mdt_object_put(env, o);
1173 mdt_pack_size2body(info, rr->rr_fid2, &lhc->mlh_reg_lh);
1176 mdt_object_put(env, parent);
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)
1186 struct md_attr *ma = &info->mti_attr;
1187 struct mdt_object *o;
1191 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1193 RETURN(rc = PTR_ERR(o));
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,
1201 "Object isn't on this server! FLD error?");
1204 if (mdt_object_exists(o)) {
1207 /* Do permission check for cross-open after converting
1208 * MDS_OPEN_* flags to MAY_* permission mask.
1210 mask = mds_accmode(open_flags);
1212 rc = mo_permission(info->mti_env, NULL,
1213 mdt_object_child(o), NULL, mask);
1217 mdt_prep_ma_buf_from_rep(info, o, ma);
1218 rc = mdt_attr_get_complex(info, o, ma);
1222 rc = mdt_pack_secctx_in_reply(info, o);
1226 rc = mdt_pack_encctx_in_reply(info, o);
1230 rc = mdt_finish_open(info, NULL, o, open_flags, 0, rep);
1233 * Something is wrong here. lookup was positive but
1234 * there is no object!
1236 CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1237 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1242 mdt_object_put(info->mti_env, o);
1248 * find root object and take its xattr lock if it's on remote MDT, later create
1249 * may use fs default striping (which is stored in root xattr).
1251 static int mdt_lock_root_xattr(struct mdt_thread_info *info,
1252 struct mdt_device *mdt)
1254 struct mdt_object *md_root = mdt->mdt_md_root;
1255 struct lustre_handle lhroot;
1258 if (md_root == NULL) {
1259 lu_root_fid(&info->mti_tmp_fid1);
1260 md_root = mdt_object_find(info->mti_env, mdt,
1261 &info->mti_tmp_fid1);
1262 if (IS_ERR(md_root))
1263 return PTR_ERR(md_root);
1265 spin_lock(&mdt->mdt_lock);
1266 if (mdt->mdt_md_root != NULL) {
1267 spin_unlock(&mdt->mdt_lock);
1269 LASSERTF(mdt->mdt_md_root == md_root,
1270 "Different root object ("
1271 DFID") instances, %p, %p\n",
1272 PFID(&info->mti_tmp_fid1),
1273 mdt->mdt_md_root, md_root);
1274 LASSERT(atomic_read(
1275 &md_root->mot_obj.lo_header->loh_ref) > 1);
1277 mdt_object_put(info->mti_env, md_root);
1279 mdt->mdt_md_root = md_root;
1280 spin_unlock(&mdt->mdt_lock);
1284 if (md_root->mot_cache_attr || !mdt_object_remote(md_root))
1287 rc = mdt_remote_object_lock(info, md_root, mdt_object_fid(md_root),
1288 &lhroot, LCK_PR, MDS_INODELOCK_XATTR,
1293 md_root->mot_cache_attr = 1;
1295 /* don't cancel this lock, so that we know the cached xattr is valid. */
1296 ldlm_lock_decref(&lhroot, LCK_PR);
1301 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1303 struct mdt_device *mdt = info->mti_mdt;
1304 struct ptlrpc_request *req = mdt_info_req(info);
1305 struct mdt_object *parent;
1306 struct mdt_object *child;
1307 struct mdt_lock_handle *lh = NULL;
1308 struct ldlm_reply *ldlm_rep;
1309 struct mdt_body *repbody;
1310 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1311 struct md_attr *ma = &info->mti_attr;
1312 u64 open_flags = info->mti_spec.sp_cr_flags;
1314 struct mdt_reint_record *rr = &info->mti_rr;
1317 int object_locked = 0;
1318 enum ldlm_mode lock_mode = LCK_PR;
1320 ktime_t kstart = ktime_get();
1323 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1324 (obd_timeout + 1) / 4);
1326 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1328 ma->ma_need = MA_INODE;
1331 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1332 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1334 if (unlikely(open_flags & MDS_OPEN_JOIN_FILE)) {
1335 CERROR("file join is not supported anymore.\n");
1336 GOTO(out, result = err_serious(-EOPNOTSUPP));
1338 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1340 if ((open_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1341 info->mti_spec.u.sp_ea.eadata == NULL)
1342 GOTO(out, result = err_serious(-EINVAL));
1344 if (open_flags & MDS_FMODE_WRITE &&
1345 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1346 GOTO(out, result = -EROFS);
1348 CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1349 "cr_flag=%#llo mode=0%06o msg_flag=0x%x\n",
1350 PFID(rr->rr_fid1), PNAME(&rr->rr_name), PFID(rr->rr_fid2),
1351 open_flags, ma->ma_attr.la_mode, msg_flags);
1353 if (info->mti_cross_ref) {
1354 /* This is cross-ref open */
1355 mdt_set_disposition(info, ldlm_rep,
1356 (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1358 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1359 ldlm_rep, open_flags);
1361 } else if (req_is_replay(req)) {
1362 result = mdt_open_by_fid(info, ldlm_rep);
1364 if (result != -ENOENT)
1367 /* We didn't find the correct object, so we need to re-create it
1368 * via a regular replay. */
1369 if (!(open_flags & MDS_OPEN_CREAT)) {
1370 DEBUG_REQ(D_ERROR, req,
1371 "OPEN & CREAT not in open replay/by_fid");
1372 GOTO(out, result = -EFAULT);
1374 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1375 } else if (open_flags & MDS_OPEN_BY_FID) {
1376 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1378 CDEBUG(D_INFO, "no object for "DFID": %d\n",
1379 PFID(rr->rr_fid2), result);
1383 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1384 GOTO(out, result = err_serious(-ENOMEM));
1386 mdt_set_disposition(info, ldlm_rep,
1387 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1389 if (!lu_name_is_valid(&rr->rr_name))
1390 GOTO(out, result = -EPROTO);
1392 result = mdt_lock_root_xattr(info, mdt);
1396 parent = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
1398 GOTO(out, result = PTR_ERR(parent));
1400 /* get and check version of parent */
1401 result = mdt_version_get_check(info, parent, 0);
1403 mdt_object_put(info->mti_env, parent);
1407 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
1409 fid_zero(child_fid);
1411 if (open_flags & MDS_OPEN_VOLATILE) {
1415 lh = &info->mti_lh[MDT_LH_PARENT];
1416 mdt_lock_pdo_init(lh, lock_mode, &rr->rr_name);
1417 result = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
1419 mdt_object_put(info->mti_env, parent);
1423 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1424 &rr->rr_name, child_fid, &info->mti_spec);
1427 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1428 "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1429 PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1432 if (result != 0 && result != -ENOENT)
1433 GOTO(out_parent, result);
1435 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
1437 if (result == -ENOENT) {
1438 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1439 if (!(open_flags & MDS_OPEN_CREAT))
1440 GOTO(out_parent, result);
1441 if (mdt_rdonly(req->rq_export))
1442 GOTO(out_parent, result = -EROFS);
1444 LASSERT(equi(lh == NULL, open_flags & MDS_OPEN_VOLATILE));
1446 if (lh != NULL && lock_mode == LCK_PR) {
1447 /* first pass: get write lock and restart */
1448 mdt_object_unlock(info, parent, lh, 1);
1449 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1450 mdt_lock_handle_init(lh);
1455 *child_fid = *info->mti_rr.rr_fid2;
1456 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1458 /* In the function below, .hs_keycmp resolves to
1459 * lu_obj_hop_keycmp() */
1460 /* coverity[overrun-buffer-val] */
1461 child = mdt_object_new(info->mti_env, mdt, child_fid);
1464 * Check for O_EXCL is moved to the mdt_finish_open(),
1465 * we need to return FID back in that case.
1467 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1468 child = mdt_object_find(info->mti_env, mdt, child_fid);
1471 GOTO(out_parent, result = PTR_ERR(child));
1473 /** check version of child */
1474 rc = mdt_version_get_check(info, child, 1);
1476 GOTO(out_child, result = rc);
1478 if (result == -ENOENT) {
1479 /* Create under OBF and .lustre is not permitted */
1480 if (!fid_is_md_operative(rr->rr_fid1) &&
1481 (open_flags & MDS_OPEN_VOLATILE) == 0)
1482 GOTO(out_child, result = -EPERM);
1484 /* save versions in reply */
1485 mdt_version_get_save(info, parent, 0);
1486 mdt_version_get_save(info, child, 1);
1488 /* version of child will be changed */
1489 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1491 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1492 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1494 /* Don't do lookup sanity check. We know name doesn't exist. */
1495 info->mti_spec.sp_cr_lookup = 0;
1496 info->mti_spec.sp_feat = &dt_directory_features;
1498 result = mdo_create(info->mti_env, mdt_object_child(parent),
1499 &rr->rr_name, mdt_object_child(child),
1500 &info->mti_spec, &info->mti_attr);
1501 if (result == -ERESTART) {
1502 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1503 GOTO(out_child, result);
1505 mdt_prep_ma_buf_from_rep(info, child, ma);
1506 /* XXX: we should call this once, see few lines below */
1508 result = mdt_attr_get_complex(info, child, ma);
1511 GOTO(out_child, result);
1514 mdt_counter_incr(req, LPROC_MDT_MKNOD,
1515 ktime_us_delta(ktime_get(), kstart));
1518 * The object is on remote node, return its FID for remote open.
1520 if (mdt_object_remote(child)) {
1522 * Check if this lock already was sent to client and
1523 * this is resent case. For resent case do not take lock
1524 * again, use what is already granted.
1526 LASSERT(lhc != NULL);
1528 rc = mdt_check_resent_lock(info, child, lhc);
1530 GOTO(out_child, result = rc);
1531 } else if (rc > 0) {
1532 mdt_lock_handle_init(lhc);
1533 mdt_lock_reg_init(lhc, LCK_PR);
1535 rc = mdt_object_lock(info, child, lhc,
1536 MDS_INODELOCK_LOOKUP);
1538 repbody->mbo_fid1 = *mdt_object_fid(child);
1539 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1543 result = -MDT_EREMOTE_OPEN;
1544 GOTO(out_child, result);
1545 } else if (mdt_object_exists(child)) {
1546 /* Check early for MDS_OPEN_DIRECTORY/O_DIRECTORY to
1547 * avoid opening regular files from lfs getstripe
1548 * since doing so breaks the leases used by lfs
1549 * mirror. See LU-13693. */
1550 if (open_flags & MDS_OPEN_DIRECTORY &&
1551 S_ISREG(lu_object_attr(&child->mot_obj)))
1552 GOTO(out_child, result = -ENOTDIR);
1554 /* We have to get attr & LOV EA & HSM for this
1556 mdt_prep_ma_buf_from_rep(info, child, ma);
1557 ma->ma_need |= MA_HSM;
1558 result = mdt_attr_get_complex(info, child, ma);
1560 GOTO(out_child, result);
1562 /* Object does not exist. Likely FS corruption. */
1563 CERROR("%s: name '"DNAME"' present, but FID "
1564 DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1565 PNAME(&rr->rr_name), PFID(child_fid));
1566 GOTO(out_child, result = -EIO);
1570 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1571 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1573 rc = mdt_pack_secctx_in_reply(info, child);
1575 GOTO(out_child, result = rc);
1577 rc = mdt_pack_encctx_in_reply(info, child);
1579 GOTO(out_child, result = rc);
1581 rc = mdt_check_resent_lock(info, child, lhc);
1583 GOTO(out_child, result = rc);
1584 } else if (rc == 0) {
1585 /* the open lock might already be gotten in
1586 * ldlm_handle_enqueue() */
1587 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1588 if (open_flags & MDS_OPEN_LOCK)
1589 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1591 /* get openlock if this isn't replay and client requested it */
1592 if (!req_is_replay(req)) {
1593 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1596 GOTO(out_child_unlock, result = rc);
1597 else if (open_flags & MDS_OPEN_LOCK)
1598 mdt_set_disposition(info, ldlm_rep,
1602 /* Try to open it now. */
1603 rc = mdt_finish_open(info, parent, child, open_flags,
1607 /* openlock will be released if mdt_finish_open() failed */
1608 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1610 if (created && (open_flags & MDS_OPEN_VOLATILE)) {
1611 CERROR("%s: cannot open volatile file "DFID", orphan "
1612 "file will be left in PENDING directory until "
1613 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1614 PFID(mdt_object_fid(child)), rc);
1615 GOTO(out_child_unlock, result);
1621 rc = mdo_unlink(info->mti_env,
1622 mdt_object_child(parent),
1623 mdt_object_child(child),
1625 &info->mti_attr, 0);
1627 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1628 mdt_obd_name(info->mti_mdt),
1629 PFID(mdt_object_fid(child)), rc);
1630 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1634 mdt_counter_incr(req, LPROC_MDT_OPEN,
1635 ktime_us_delta(ktime_get(), kstart));
1640 mdt_object_open_unlock(info, child, lhc, ibits, result);
1642 mdt_object_put(info->mti_env, child);
1644 mdt_pack_size2body(info, child_fid, &lhc->mlh_reg_lh);
1647 mdt_object_unlock(info, parent, lh, result || !created);
1649 mdt_object_put(info->mti_env, parent);
1652 lustre_msg_set_transno(req->rq_repmsg, 0);
1657 * Create an orphan object use local root.
1659 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1660 struct mdt_device *mdt,
1661 const struct lu_fid *fid,
1662 struct md_attr *attr, fmode_t fmode)
1664 const struct lu_env *env = info->mti_env;
1665 struct md_op_spec *spec = &info->mti_spec;
1666 struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1667 struct mdt_object *obj = NULL;
1668 struct mdt_object *local_root;
1669 static const struct lu_name lname = {
1670 .ln_name = "i_am_nobody",
1671 .ln_namelen = sizeof("i_am_nobody") - 1,
1673 struct lu_ucred *uc;
1674 kernel_cap_t uc_cap_save;
1678 rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1680 RETURN(ERR_PTR(rc));
1682 local_root = mdt_object_find(env, mdt, local_root_fid);
1683 if (IS_ERR(local_root))
1686 obj = mdt_object_new(env, mdt, fid);
1688 GOTO(out, rc = PTR_ERR(obj));
1690 spec->sp_cr_lookup = 0;
1691 spec->sp_feat = &dt_directory_features;
1692 spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1693 if (attr->ma_valid & MA_LOV) {
1694 spec->u.sp_ea.eadata = attr->ma_lmm;
1695 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1696 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1698 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1702 uc_cap_save = uc->uc_cap;
1703 cap_raise(uc->uc_cap, CAP_DAC_OVERRIDE);
1704 rc = mdo_create(env, mdt_object_child(local_root), &lname,
1705 mdt_object_child(obj), spec, attr);
1706 uc->uc_cap = uc_cap_save;
1708 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1709 mdt_obd_name(mdt), PFID(fid), rc);
1713 rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED, spec);
1715 CERROR("%s: cannot open volatile file "DFID", orphan "
1716 "file will be left in PENDING directory until "
1717 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1724 mdt_object_put(env, obj);
1727 mdt_object_put(env, local_root);
1731 /* XXX Look into layout in MDT layer. */
1732 static inline int mdt_hsm_set_released(struct lov_mds_md *lmm)
1734 struct lov_comp_md_v1 *comp_v1;
1735 struct lov_mds_md *v1;
1739 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1_DEFINED)) {
1740 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1742 if (comp_v1->lcm_entry_count == 0)
1745 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
1746 off = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1747 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1748 v1->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1751 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1756 static inline int mdt_get_lmm_gen(struct lov_mds_md *lmm, __u32 *gen)
1758 struct lov_comp_md_v1 *comp_v1;
1760 if (le32_to_cpu(lmm->lmm_magic == LOV_MAGIC_COMP_V1)) {
1761 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1762 *gen = le32_to_cpu(comp_v1->lcm_layout_gen);
1763 } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V1 ||
1764 le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_V3) {
1765 *gen = le16_to_cpu(lmm->lmm_layout_gen);
1772 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1775 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1776 struct lu_ucred *uc = mdt_ucred(info);
1777 struct close_data *data;
1778 struct ldlm_lock *lease;
1779 struct mdt_object *orphan;
1780 struct md_attr *orp_ma;
1788 if (mdt_rdonly(info->mti_exp))
1791 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1795 lease = ldlm_handle2lock(&data->cd_handle);
1799 /* try to hold open_sem so that nobody else can open the file */
1800 if (!down_write_trylock(&o->mot_open_sem)) {
1801 ldlm_lock_cancel(lease);
1802 GOTO(out_reprocess, rc = -EBUSY);
1805 /* Check if the lease open lease has already canceled */
1806 lock_res_and_lock(lease);
1807 lease_broken = ldlm_is_cancel(lease);
1808 unlock_res_and_lock(lease);
1810 LDLM_DEBUG(lease, DFID " lease broken? %d",
1811 PFID(mdt_object_fid(o)), lease_broken);
1813 /* Cancel server side lease. Client side counterpart should
1814 * have been cancelled. It's okay to cancel it now as we've
1815 * held mot_open_sem. */
1816 ldlm_lock_cancel(lease);
1818 if (lease_broken) /* don't perform release task */
1819 GOTO(out_unlock, rc = -ESTALE);
1821 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1822 GOTO(out_unlock, rc = -EINVAL);
1824 /* ma_need was set before but it seems fine to change it in order to
1825 * avoid modifying the one from RPC */
1826 ma->ma_need = MA_HSM;
1827 rc = mdt_attr_get_complex(info, o, ma);
1829 GOTO(out_unlock, rc);
1831 if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
1832 if (ma->ma_valid & MA_HSM) {
1833 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1834 GOTO(out_unlock, rc = -EALREADY);
1836 if (ma->ma_hsm.mh_arch_id != data->cd_archive_id)
1838 DFID" archive id diff: %llu:%u\n",
1839 PFID(mdt_object_fid(o)),
1840 ma->ma_hsm.mh_arch_id,
1841 data->cd_archive_id);
1843 if (!(ma->ma_hsm.mh_flags & HS_DIRTY) &&
1844 ma->ma_hsm.mh_arch_ver == data->cd_data_version) {
1846 DFID" data version matches: packed=%llu "
1847 "and on-disk=%llu\n",
1848 PFID(mdt_object_fid(o)),
1849 data->cd_data_version,
1850 ma->ma_hsm.mh_arch_ver);
1851 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1854 if (ma->ma_hsm.mh_flags & HS_DIRTY)
1855 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1857 /* Set up HSM attribte for PCC archived object */
1858 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1859 sizeof(info->mti_xattr_buf));
1860 buf = &info->mti_buf;
1861 buf->lb_buf = info->mti_xattr_buf;
1862 buf->lb_len = sizeof(struct hsm_attrs);
1863 memset(&ma->ma_hsm, 0, sizeof(ma->ma_hsm));
1864 ma->ma_hsm.mh_flags = HS_ARCHIVED | HS_EXISTS;
1865 ma->ma_hsm.mh_arch_id = data->cd_archive_id;
1866 ma->ma_hsm.mh_arch_ver = data->cd_data_version;
1867 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1869 rc = mo_xattr_set(info->mti_env, mdt_object_child(o),
1870 buf, XATTR_NAME_HSM, 0);
1872 GOTO(out_unlock, rc);
1875 if (!mdt_hsm_release_allow(ma))
1876 GOTO(out_unlock, rc = -EPERM);
1878 /* already released? */
1879 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1880 GOTO(out_unlock, rc = 0);
1882 /* Compare on-disk and packed data_version */
1883 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
1884 CDEBUG(D_HSM, DFID" data_version mismatches: "
1885 "packed=%llu and on-disk=%llu\n",
1886 PFID(mdt_object_fid(o)),
1887 data->cd_data_version,
1888 ma->ma_hsm.mh_arch_ver);
1889 GOTO(out_unlock, rc = -EPERM);
1893 ma->ma_valid = MA_INODE;
1894 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
1895 rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
1897 GOTO(out_unlock, rc);
1899 mutex_lock(&o->mot_som_mutex);
1900 rc2 = mdt_set_som(info, o, SOM_FL_STRICT, ma->ma_attr.la_size,
1901 ma->ma_attr.la_blocks);
1902 mutex_unlock(&o->mot_som_mutex);
1905 "%s: File "DFID" SOM update failed: rc = %d\n",
1906 mdt_obd_name(info->mti_mdt),
1907 PFID(mdt_object_fid(o)), rc2);
1910 ma->ma_need = MA_INODE | MA_LOV;
1911 rc = mdt_attr_get_complex(info, o, ma);
1913 GOTO(out_unlock, rc);
1915 if (!(ma->ma_valid & MA_LOV)) {
1916 /* Even empty file are released */
1917 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
1918 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEFINED);
1919 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1920 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
1921 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
1923 /* Magic must be LOV_MAGIC_*_DEFINED or LOD will interpret
1924 * ma_lmm as lov_user_md, then it will be confused by union of
1925 * layout_gen and stripe_offset. */
1926 if ((le32_to_cpu(ma->ma_lmm->lmm_magic) & LOV_MAGIC_MASK) ==
1928 ma->ma_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
1930 GOTO(out_unlock, rc = -EINVAL);
1933 /* Set file as released. */
1934 rc = mdt_hsm_set_released(ma->ma_lmm);
1936 GOTO(out_unlock, rc);
1938 orp_ma = &info->mti_u.hsm.attr;
1939 orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
1940 /* We use root ownership to bypass potential quota
1941 * restrictions on the user and group of the file. */
1942 orp_ma->ma_attr.la_uid = 0;
1943 orp_ma->ma_attr.la_gid = 0;
1944 orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
1945 orp_ma->ma_lmm = ma->ma_lmm;
1946 orp_ma->ma_lmm_size = ma->ma_lmm_size;
1947 orp_ma->ma_valid = MA_INODE | MA_LOV;
1948 orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
1950 if (IS_ERR(orphan)) {
1951 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
1952 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
1954 GOTO(out_unlock, rc = PTR_ERR(orphan));
1957 /* Set up HSM attribute for orphan object */
1958 BUILD_BUG_ON(sizeof(struct hsm_attrs) > sizeof(info->mti_xattr_buf));
1959 buf = &info->mti_buf;
1960 buf->lb_buf = info->mti_xattr_buf;
1961 buf->lb_len = sizeof(struct hsm_attrs);
1962 ma->ma_hsm.mh_flags |= HS_RELEASED;
1963 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1964 ma->ma_hsm.mh_flags &= ~HS_RELEASED;
1966 mdt_lock_reg_init(lh, LCK_EX);
1967 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
1968 MDS_INODELOCK_XATTR);
1970 GOTO(out_close, rc);
1972 /* The orphan has root ownership so we need to raise
1973 * CAP_FOWNER to set the HSM attributes. */
1975 cap_raise(uc->uc_cap, CAP_FOWNER);
1976 rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
1980 GOTO(out_layout_lock, rc);
1982 /* Swap layout with orphan objects. */
1983 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
1984 mdt_object_child(orphan),
1985 SWAP_LAYOUTS_MDS_HSM);
1987 if (!rc && ma->ma_attr_flags & MDS_PCC_ATTACH) {
1988 ma->ma_need = MA_LOV;
1989 rc = mdt_attr_get_complex(info, o, ma);
1995 /* Release exclusive LL */
1996 mdt_object_unlock(info, o, lh, 1);
1998 /* Close orphan object anyway */
1999 rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
2002 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
2003 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
2004 LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
2006 mdt_object_put(info->mti_env, orphan);
2009 up_write(&o->mot_open_sem);
2011 /* already released */
2013 struct mdt_body *repbody;
2015 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2016 LASSERT(repbody != NULL);
2017 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2018 if (ma->ma_attr_flags & MDS_PCC_ATTACH) {
2019 LASSERT(ma->ma_valid & MA_LOV);
2020 rc = mdt_get_lmm_gen(ma->ma_lmm,
2021 &repbody->mbo_layout_gen);
2023 repbody->mbo_valid |= OBD_MD_LAYOUT_VERSION;
2028 ldlm_reprocess_all(lease->l_resource, lease);
2029 LDLM_LOCK_PUT(lease);
2037 int mdt_close_handle_layouts(struct mdt_thread_info *info,
2038 struct mdt_object *o, struct md_attr *ma)
2040 struct mdt_lock_handle *lh1 = &info->mti_lh[MDT_LH_NEW];
2041 struct mdt_lock_handle *lh2 = &info->mti_lh[MDT_LH_OLD];
2042 struct close_data *data;
2043 struct ldlm_lock *lease;
2044 struct mdt_object *o1 = o, *o2 = NULL;
2046 bool swap_objects = false;
2050 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2053 if (!S_ISREG(lu_object_attr(&o1->mot_obj)))
2056 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2060 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2063 rc = lu_fid_cmp(&data->cd_fid, mdt_object_fid(o));
2066 * only MDS_CLOSE_LAYOUT_SPLIT use the same fid to indicate
2067 * mirror deletion, so we'd zero cd_fid, and keeps o2 be NULL.
2069 if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT))
2072 /* zero cd_fid to keeps o2 be NULL */
2073 fid_zero(&data->cd_fid);
2074 } else if (rc < 0) {
2075 /* Exchange o1 and o2, to enforce locking order */
2076 swap_objects = true;
2079 lease = ldlm_handle2lock(&data->cd_handle);
2083 if (!fid_is_zero(&data->cd_fid)) {
2084 o2 = mdt_object_find(info->mti_env, info->mti_mdt,
2087 GOTO(out_lease, rc = PTR_ERR(o2));
2089 if (!mdt_object_exists(o2))
2090 GOTO(out_obj, rc = -ENOENT);
2092 if (!S_ISREG(lu_object_attr(&o2->mot_obj)))
2093 GOTO(out_obj, rc = -EINVAL);
2099 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
2105 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2),
2111 /* try to hold open_sem so that nobody else can open the file */
2112 if (!down_write_trylock(&o->mot_open_sem)) {
2113 ldlm_lock_cancel(lease);
2114 GOTO(out_obj, rc = -EBUSY);
2117 /* Check if the lease open lease has already canceled */
2118 lock_res_and_lock(lease);
2119 lease_broken = ldlm_is_cancel(lease);
2120 unlock_res_and_lock(lease);
2122 LDLM_DEBUG(lease, DFID " lease broken? %d",
2123 PFID(mdt_object_fid(o)), lease_broken);
2125 /* Cancel server side lease. Client side counterpart should
2126 * have been cancelled. It's okay to cancel it now as we've
2127 * held mot_open_sem. */
2128 ldlm_lock_cancel(lease);
2131 GOTO(out_unlock_sem, rc = -ESTALE);
2133 mdt_lock_reg_init(lh1, LCK_EX);
2134 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
2135 MDS_INODELOCK_XATTR);
2137 GOTO(out_unlock_sem, rc);
2140 mdt_lock_reg_init(lh2, LCK_EX);
2141 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
2142 MDS_INODELOCK_XATTR);
2144 GOTO(out_unlock1, rc);
2147 /* Swap layout with orphan object */
2148 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SWAP) {
2149 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
2150 mdt_object_child(o2), 0);
2151 } else if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_MERGE ||
2152 ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2153 struct lu_buf *buf = &info->mti_buf;
2154 struct md_rejig_data mrd;
2157 mrd.mrd_obj = mdt_object_child(o == o1 ? o2 : o1);
2159 if (!(ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT)) {
2160 /* paranoid check again */
2162 ":only mirror split support NULL o2 object\n",
2163 PFID(mdt_object_fid(o)));
2164 GOTO(out_unlock1, rc = -EINVAL);
2167 /* set NULL mrd_obj for deleting mirror objects */
2171 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
2172 mrd.mrd_mirror_id = data->cd_mirror_id;
2173 /* set a small enough blocks in the SoM */
2174 ma->ma_attr.la_blocks >>= 1;
2177 buf->lb_len = sizeof(mrd);
2179 rc = mo_xattr_set(info->mti_env, mdt_object_child(o), buf,
2181 ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT ?
2182 LU_XATTR_SPLIT : LU_XATTR_MERGE);
2183 if (rc == 0 && ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS |
2184 LA_LSIZE | LA_LBLOCKS)) {
2186 enum lustre_som_flags lsf;
2188 if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS))
2189 lsf = SOM_FL_STRICT;
2193 mutex_lock(&o->mot_som_mutex);
2194 rc2 = mdt_set_som(info, o, lsf,
2195 ma->ma_attr.la_size,
2196 ma->ma_attr.la_blocks);
2197 mutex_unlock(&o->mot_som_mutex);
2199 CERROR(DFID": Setting i_blocks error: %d, "
2200 "i_blocks will be reported wrongly and "
2201 "can only be fixed in next resync\n",
2202 PFID(mdt_object_fid(o)), rc2);
2206 GOTO(out_unlock2, rc);
2211 /* Release exclusive LL */
2213 mdt_object_unlock(info, o2, lh2, 1);
2216 mdt_object_unlock(info, o1, lh1, 1);
2219 up_write(&o->mot_open_sem);
2221 /* already swapped */
2223 struct mdt_body *repbody;
2225 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2226 LASSERT(repbody != NULL);
2227 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2232 /* the 2nd object has been used, and swapped to o1 */
2233 mdt_object_put(info->mti_env, o1);
2235 /* the 2nd object has been used, and not swapped */
2236 mdt_object_put(info->mti_env, o2);
2238 ldlm_reprocess_all(lease->l_resource, lease);
2241 LDLM_LOCK_PUT(lease);
2251 static int mdt_close_resync_done(struct mdt_thread_info *info,
2252 struct mdt_object *o, struct md_attr *ma)
2254 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LOCAL];
2255 struct close_data *data;
2256 struct ldlm_lock *lease;
2257 struct md_layout_change layout = { 0 };
2258 __u32 *resync_ids = NULL;
2259 size_t resync_count = 0;
2265 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2268 if (!S_ISREG(lu_object_attr(&o->mot_obj)))
2271 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2275 if (req_capsule_req_need_swab(info->mti_pill))
2276 lustre_swab_close_data_resync_done(&data->cd_resync);
2278 if (!fid_is_zero(&data->cd_fid))
2281 lease = ldlm_handle2lock(&data->cd_handle);
2285 /* try to hold open_sem so that nobody else can open the file */
2286 if (!down_write_trylock(&o->mot_open_sem)) {
2287 ldlm_lock_cancel(lease);
2288 GOTO(out_reprocess, rc = -EBUSY);
2291 /* Check if the lease open lease has already canceled */
2292 lock_res_and_lock(lease);
2293 lease_broken = ldlm_is_cancel(lease);
2294 unlock_res_and_lock(lease);
2296 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2297 PFID(mdt_object_fid(o)), lease_broken);
2299 /* Cancel server side lease. Client side counterpart should
2300 * have been cancelled. It's okay to cancel it now as we've
2301 * held mot_open_sem. */
2302 ldlm_lock_cancel(lease);
2304 if (lease_broken) /* don't perform release task */
2305 GOTO(out_unlock, rc = -ESTALE);
2307 resync_count = data->cd_resync.resync_count;
2309 if (resync_count > INLINE_RESYNC_ARRAY_SIZE) {
2312 if (!req_capsule_has_field(info->mti_pill, &RMF_U32,
2314 GOTO(out_unlock, rc = -EPROTO);
2316 OBD_ALLOC_PTR_ARRAY(resync_ids, resync_count);
2318 GOTO(out_unlock, rc = -ENOMEM);
2320 data = req_capsule_client_get(info->mti_pill, &RMF_U32);
2321 memcpy(resync_ids, data, resync_count * sizeof(__u32));
2323 layout.mlc_resync_ids = resync_ids;
2325 layout.mlc_resync_ids = data->cd_resync.resync_ids_inline;
2328 layout.mlc_opc = MD_LAYOUT_RESYNC_DONE;
2329 layout.mlc_resync_count = resync_count;
2330 if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2331 layout.mlc_som.lsa_valid = SOM_FL_STRICT;
2332 layout.mlc_som.lsa_size = ma->ma_attr.la_size;
2333 layout.mlc_som.lsa_blocks = ma->ma_attr.la_blocks;
2335 rc = mdt_layout_change(info, o, lhc, &layout);
2337 GOTO(out_unlock, rc);
2339 mdt_object_unlock(info, o, lhc, 0);
2344 up_write(&o->mot_open_sem);
2346 /* already released */
2348 struct mdt_body *repbody;
2350 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2351 LASSERT(repbody != NULL);
2352 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2356 OBD_FREE_PTR_ARRAY(resync_ids, resync_count);
2359 ldlm_reprocess_all(lease->l_resource, lease);
2360 LDLM_LOCK_PUT(lease);
2368 #define MFD_CLOSED(open_flags) ((open_flags) == MDS_FMODE_CLOSED)
2370 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2372 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_open_flags));
2375 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2377 struct mdt_object *o = mfd->mfd_object;
2378 struct md_object *next = mdt_object_child(o);
2379 struct md_attr *ma = &info->mti_attr;
2380 struct lu_fid *ofid = &info->mti_tmp_fid1;
2387 open_flags = mfd->mfd_open_flags;
2388 intent = ma->ma_attr_flags & MDS_CLOSE_INTENT;
2389 *ofid = *mdt_object_fid(o);
2391 /* the below message is checked in replay-single.sh test_46 */
2392 CDEBUG(D_INODE, "%s: %sclosing file handle "DFID" with intent: %llx\n",
2393 mdt_obd_name(info->mti_mdt),
2394 ma->ma_valid & MA_FORCE_LOG ? "force " : "", PFID(ofid), intent);
2397 case MDS_HSM_RELEASE: {
2398 rc = mdt_hsm_release(info, o, ma);
2400 CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2401 mdt_obd_name(info->mti_mdt),
2403 /* continue to close even error occurred. */
2407 case MDS_CLOSE_LAYOUT_MERGE:
2408 case MDS_CLOSE_LAYOUT_SPLIT:
2409 case MDS_CLOSE_LAYOUT_SWAP: {
2410 rc = mdt_close_handle_layouts(info, o, ma);
2413 "%s: cannot swap layout of "DFID": rc = %d\n",
2414 mdt_obd_name(info->mti_mdt),
2416 /* continue to close even if error occurred. */
2420 case MDS_CLOSE_RESYNC_DONE:
2421 rc = mdt_close_resync_done(info, o, ma);
2428 if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
2429 ma->ma_attr.la_valid & (LA_LSIZE | LA_LBLOCKS)) {
2432 rc2 = mdt_lsom_update(info, o, false);
2435 "%s: File " DFID " LSOM failed: rc = %d\n",
2436 mdt_obd_name(info->mti_mdt),
2438 /* continue to close even if error occured. */
2441 if (open_flags & MDS_FMODE_WRITE)
2443 else if (open_flags & MDS_FMODE_EXEC)
2446 /* Update atime|mtime|ctime on close. */
2447 if ((open_flags & MDS_FMODE_EXEC || open_flags & MDS_FMODE_READ ||
2448 open_flags & MDS_FMODE_WRITE) && (ma->ma_valid & MA_INODE) &&
2449 (ma->ma_attr.la_valid & LA_ATIME ||
2450 ma->ma_attr.la_valid & LA_MTIME ||
2451 ma->ma_attr.la_valid & LA_CTIME)) {
2452 ma->ma_valid = MA_INODE;
2453 ma->ma_attr_flags |= MDS_CLOSE_UPDATE_TIMES;
2454 ma->ma_attr.la_valid &= (LA_ATIME | LA_MTIME | LA_CTIME);
2456 if (ma->ma_attr.la_valid & LA_MTIME) {
2457 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
2459 ma->ma_valid |= MA_PFID;
2462 rc = mo_attr_set(info->mti_env, next, ma);
2465 /* If file data is modified, add the dirty flag. */
2466 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2467 rc = mdt_add_dirty_flag(info, o, ma);
2469 ma->ma_need |= MA_INODE;
2470 ma->ma_valid &= ~MA_INODE;
2472 LASSERT(atomic_read(&o->mot_open_count) > 0);
2473 atomic_dec(&o->mot_open_count);
2474 mdt_handle_last_unlink(info, o, ma);
2476 if (!MFD_CLOSED(open_flags)) {
2477 rc = mo_close(info->mti_env, next, ma, open_flags);
2478 if (mdt_dom_check_for_discard(info, o))
2479 mdt_dom_discard_data(info, o);
2482 /* adjust open and lease count */
2483 if (open_flags & MDS_OPEN_LEASE) {
2484 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2485 atomic_dec(&o->mot_lease_count);
2489 mdt_object_put(info->mti_env, o);
2494 int mdt_close_internal(struct mdt_thread_info *info, struct ptlrpc_request *req,
2495 struct mdt_body *repbody)
2497 struct mdt_export_data *med;
2498 struct mdt_file_data *mfd;
2503 med = &req->rq_export->exp_mdt_data;
2504 spin_lock(&med->med_open_lock);
2505 mfd = mdt_open_handle2mfd(med, &info->mti_open_handle,
2506 req_is_replay(req));
2507 if (mdt_mfd_closed(mfd)) {
2508 spin_unlock(&med->med_open_lock);
2509 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2510 ": cookie = %#llx\n", PFID(info->mti_rr.rr_fid1),
2511 info->mti_open_handle.cookie);
2512 /** not serious error since bug 3633 */
2515 class_handle_unhash(&mfd->mfd_open_handle);
2516 list_del_init(&mfd->mfd_list);
2517 spin_unlock(&med->med_open_lock);
2518 ret = mdt_mfd_close(info, mfd);
2521 RETURN(rc ? rc : ret);
2524 int mdt_close(struct tgt_session_info *tsi)
2526 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2527 struct ptlrpc_request *req = tgt_ses_req(tsi);
2528 struct md_attr *ma = &info->mti_attr;
2529 struct mdt_body *repbody = NULL;
2530 ktime_t kstart = ktime_get();
2534 /* Close may come with the Size-on-MDS update. Unpack it. */
2535 rc = mdt_close_unpack(info);
2537 GOTO(out, rc = err_serious(rc));
2539 /* These fields are no longer used and are left for compatibility.
2540 * size is always zero */
2541 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2543 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2545 rc = req_capsule_server_pack(info->mti_pill);
2546 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2547 mdt_client_compatibility(info);
2549 mdt_fix_reply(info);
2550 mdt_exit_ucred(info);
2551 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2554 /* Continue to close handle even if we can not pack reply */
2556 repbody = req_capsule_server_get(info->mti_pill,
2558 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2560 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2563 ma->ma_need = MA_INODE | MA_LOV;
2564 repbody->mbo_eadatasize = 0;
2565 repbody->mbo_aclsize = 0;
2567 rc = err_serious(rc);
2570 rc = mdt_close_internal(info, req, repbody);
2572 mdt_empty_transno(info, rc);
2574 if (repbody != NULL) {
2575 mdt_client_compatibility(info);
2576 rc = mdt_fix_reply(info);
2579 mdt_exit_ucred(info);
2580 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2581 GOTO(out, rc = err_serious(-ENOMEM));
2583 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2584 OBD_FAIL_MDS_CLOSE_NET_REP))
2585 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2587 mdt_thread_info_fini(info);
2589 mdt_counter_incr(req, LPROC_MDT_CLOSE,
2590 ktime_us_delta(ktime_get(), kstart));
2591 RETURN(rc ? rc : ret);