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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_open.c
34 * Lustre Metadata Target (mdt) open/close file handling
36 * Author: Huang Hua <huanghua@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <lustre_acl.h>
42 #include <lustre_mds.h>
43 #include <lustre_swab.h>
44 #include "mdt_internal.h"
45 #include <lustre_nodemap.h>
47 /* we do nothing because we do not have refcount now */
48 static void mdt_mfd_get(void *mfdp)
52 static struct portals_handle_ops mfd_handle_ops = {
53 .hop_addref = mdt_mfd_get,
57 /* Create a new mdt_file_data struct, initialize it,
58 * and insert it to global hash table */
59 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med)
61 struct mdt_file_data *mfd;
66 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
67 mfd->mfd_handle.h_owner = med;
68 INIT_LIST_HEAD(&mfd->mfd_list);
69 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
76 * Find the mfd pointed to by handle in global hash table.
77 * In case of replay the handle is obsoleted
78 * but mfd can be found in mfd list by that handle.
79 * Callers need to be holding med_open_lock.
81 struct mdt_file_data *mdt_handle2mfd(struct mdt_export_data *med,
82 const struct lustre_handle *handle,
83 bool is_replay_or_resent)
85 struct mdt_file_data *mfd;
88 LASSERT(handle != NULL);
89 mfd = class_handle2object(handle->cookie, med);
90 /* during dw/setattr replay the mfd can be found by old handle */
91 if (mfd == NULL && is_replay_or_resent) {
92 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
93 if (mfd->mfd_old_handle.cookie == handle->cookie)
103 void mdt_mfd_free(struct mdt_file_data *mfd)
105 LASSERT(list_empty(&mfd->mfd_list));
106 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
109 static int mdt_create_data(struct mdt_thread_info *info,
110 struct mdt_object *p, struct mdt_object *o)
112 struct md_op_spec *spec = &info->mti_spec;
113 struct md_attr *ma = &info->mti_attr;
117 if (!md_should_create(spec->sp_cr_flags))
120 ma->ma_need = MA_INODE | MA_LOV;
122 mutex_lock(&o->mot_lov_mutex);
123 if (!o->mot_lov_created) {
124 rc = mdo_create_data(info->mti_env,
125 p ? mdt_object_child(p) : NULL,
126 mdt_object_child(o), spec, ma);
128 rc = mdt_attr_get_complex(info, o, ma);
130 if (rc == 0 && ma->ma_valid & MA_LOV)
131 o->mot_lov_created = 1;
134 mutex_unlock(&o->mot_lov_mutex);
138 int mdt_write_read(struct mdt_object *o)
142 spin_lock(&o->mot_write_lock);
143 rc = o->mot_write_count;
144 spin_unlock(&o->mot_write_lock);
148 int mdt_write_get(struct mdt_object *o)
152 spin_lock(&o->mot_write_lock);
153 if (o->mot_write_count < 0)
156 o->mot_write_count++;
157 spin_unlock(&o->mot_write_lock);
162 void mdt_write_put(struct mdt_object *o)
165 spin_lock(&o->mot_write_lock);
166 o->mot_write_count--;
167 spin_unlock(&o->mot_write_lock);
171 static int mdt_write_deny(struct mdt_object *o)
175 spin_lock(&o->mot_write_lock);
176 if (o->mot_write_count > 0)
179 o->mot_write_count--;
180 spin_unlock(&o->mot_write_lock);
184 static void mdt_write_allow(struct mdt_object *o)
187 spin_lock(&o->mot_write_lock);
188 o->mot_write_count++;
189 spin_unlock(&o->mot_write_lock);
193 /* there can be no real transaction so prepare the fake one */
194 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
196 struct mdt_device *mdt = info->mti_mdt;
197 struct ptlrpc_request *req = mdt_info_req(info);
198 struct tg_export_data *ted;
199 struct lsd_client_data *lcd;
202 if (mdt_rdonly(req->rq_export))
205 /* transaction has occurred already */
206 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
209 if (tgt_is_multimodrpcs_client(req->rq_export)) {
212 /* generate an empty transaction to get a transno
214 th = dt_trans_create(info->mti_env, mdt->mdt_bottom);
216 rc = dt_trans_start(info->mti_env, mdt->mdt_bottom, th);
217 dt_trans_stop(info->mti_env, mdt->mdt_bottom, th);
222 spin_lock(&mdt->mdt_lut.lut_translock);
224 if (info->mti_transno != 0) {
225 struct obd_export *exp = req->rq_export;
227 CERROR("%s: replay trans %llu NID %s: rc = %d\n",
228 mdt_obd_name(mdt), info->mti_transno,
229 libcfs_nid2str(exp->exp_connection->c_peer.nid),
231 spin_unlock(&mdt->mdt_lut.lut_translock);
234 } else if (info->mti_transno == 0) {
235 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
237 /* should be replay */
238 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
239 mdt->mdt_lut.lut_last_transno = info->mti_transno;
241 spin_unlock(&mdt->mdt_lut.lut_translock);
243 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
245 req->rq_export->exp_obd->obd_last_committed);
247 req->rq_transno = info->mti_transno;
248 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
250 /* update lcd in memory only for resent cases */
251 ted = &req->rq_export->exp_target_data;
253 mutex_lock(&ted->ted_lcd_lock);
255 if (info->mti_transno < lcd->lcd_last_transno &&
256 info->mti_transno != 0) {
257 /* This should happen during replay. Do not update
258 * last rcvd info if replay req transno < last transno,
259 * otherwise the following resend(after replay) can not
260 * be checked correctly by xid */
261 mutex_unlock(&ted->ted_lcd_lock);
262 CDEBUG(D_HA, "%s: transno = %llu < last_transno = %llu\n",
263 mdt_obd_name(mdt), info->mti_transno,
264 lcd->lcd_last_transno);
268 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
269 if (info->mti_transno != 0)
270 lcd->lcd_last_close_transno = info->mti_transno;
271 lcd->lcd_last_close_xid = req->rq_xid;
272 lcd->lcd_last_close_result = rc;
274 /* VBR: save versions in last_rcvd for reconstruct. */
275 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
277 lcd->lcd_pre_versions[0] = pre_versions[0];
278 lcd->lcd_pre_versions[1] = pre_versions[1];
279 lcd->lcd_pre_versions[2] = pre_versions[2];
280 lcd->lcd_pre_versions[3] = pre_versions[3];
282 if (info->mti_transno != 0)
283 lcd->lcd_last_transno = info->mti_transno;
285 lcd->lcd_last_xid = req->rq_xid;
286 lcd->lcd_last_result = rc;
287 lcd->lcd_last_data = info->mti_opdata;
289 mutex_unlock(&ted->ted_lcd_lock);
294 void mdt_mfd_set_mode(struct mdt_file_data *mfd, __u64 mode)
296 LASSERT(mfd != NULL);
298 CDEBUG(D_DENTRY, DFID " Change mfd mode %#llo -> %#llo.\n",
299 PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_mode, mode);
301 mfd->mfd_mode = mode;
305 * prep ma_lmm/ma_lmv for md_attr from reply
307 static void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
308 struct mdt_object *obj,
311 LASSERT(ma->ma_lmv == NULL && ma->ma_lmm == NULL);
312 if (S_ISDIR(obj->mot_header.loh_attr)) {
313 ma->ma_lmv = req_capsule_server_get(info->mti_pill,
315 ma->ma_lmv_size = req_capsule_get_size(info->mti_pill,
318 if (ma->ma_lmv_size > 0)
319 ma->ma_need |= MA_LMV;
321 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
323 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
326 if (ma->ma_lmm_size > 0)
327 ma->ma_need |= MA_LOV;
331 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
332 struct mdt_object *o, __u64 flags, int created,
333 struct ldlm_reply *rep)
335 struct ptlrpc_request *req = mdt_info_req(info);
336 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
337 struct mdt_file_data *mfd;
338 struct md_attr *ma = &info->mti_attr;
339 struct lu_attr *la = &ma->ma_attr;
340 struct mdt_body *repbody;
341 int rc = 0, isdir, isreg;
344 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
346 isreg = S_ISREG(la->la_mode);
347 isdir = S_ISDIR(la->la_mode);
348 if (isreg && !(ma->ma_valid & MA_LOV) && !(flags & MDS_OPEN_RELEASE)) {
350 * No EA, check whether it is will set regEA and dirEA since in
351 * above attr get, these size might be zero, so reset it, to
352 * retrieve the MD after create obj.
354 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
357 /* in replay case, p == NULL */
358 rc = mdt_create_data(info, p, o);
362 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
363 mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
366 CDEBUG(D_INODE, "after open, ma_valid bit = %#llx lmm_size = %d\n",
367 ma->ma_valid, ma->ma_lmm_size);
369 if (ma->ma_valid & MA_LOV) {
370 LASSERT(ma->ma_lmm_size != 0);
371 repbody->mbo_eadatasize = ma->ma_lmm_size;
373 repbody->mbo_valid |= OBD_MD_FLDIREA;
375 repbody->mbo_valid |= OBD_MD_FLEASIZE;
378 if (ma->ma_valid & MA_LMV) {
379 LASSERT(ma->ma_lmv_size != 0);
380 repbody->mbo_eadatasize = ma->ma_lmv_size;
382 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
385 if (flags & FMODE_WRITE)
386 rc = mdt_write_get(o);
387 else if (flags & MDS_FMODE_EXEC)
388 rc = mdt_write_deny(o);
393 rc = mo_open(info->mti_env, mdt_object_child(o),
394 created ? flags | MDS_OPEN_CREATED : flags);
396 /* If we allow the client to chgrp (CFS_SETGRP_PERM), but the
397 * client does not know which suppgid should be sent to the MDS,
398 * or some other(s) changed the target file's GID after this RPC
399 * sent to the MDS with the suppgid as the original GID, then we
400 * give the client another chance to send the right suppgid. */
402 allow_client_chgrp(info, lu_ucred(info->mti_env)))
403 mdt_set_disposition(info, rep, DISP_OPEN_DENY);
408 mfd = mdt_mfd_new(med);
410 GOTO(err_out, rc = -ENOMEM);
413 * Keep a reference on this object for this open, and is
414 * released by mdt_mfd_close().
416 mdt_object_get(info->mti_env, o);
418 mfd->mfd_xid = req->rq_xid;
421 * @flags is always not zero. At least it should be FMODE_READ,
422 * FMODE_WRITE or MDS_FMODE_EXEC.
427 mdt_mfd_set_mode(mfd, flags);
429 atomic_inc(&o->mot_open_count);
430 if (flags & MDS_OPEN_LEASE)
431 atomic_inc(&o->mot_lease_count);
434 if (req_is_replay(req)) {
435 struct mdt_file_data *old_mfd;
436 /* Check wheather old cookie already exist in
437 * the list, becasue when do recovery, client
438 * might be disconnected from server, and
439 * restart replay, so there maybe some orphan
440 * mfd here, we should remove them */
441 LASSERT(info->mti_rr.rr_handle != NULL);
442 spin_lock(&med->med_open_lock);
443 old_mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle, true);
444 if (old_mfd != NULL) {
445 CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
446 "cookie = %#llx\n", mfd,
447 PFID(mdt_object_fid(mfd->mfd_object)),
448 info->mti_rr.rr_handle->cookie);
449 class_handle_unhash(&old_mfd->mfd_handle);
450 list_del_init(&old_mfd->mfd_list);
451 spin_unlock(&med->med_open_lock);
452 /* no attr update for that close */
454 ma->ma_valid |= MA_FLAGS;
455 ma->ma_attr_flags |= MDS_RECOV_OPEN;
456 mdt_mfd_close(info, old_mfd);
457 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
458 ma->ma_valid &= ~MA_FLAGS;
460 spin_unlock(&med->med_open_lock);
461 CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
463 PFID(mdt_object_fid(mfd->mfd_object)),
464 info->mti_rr.rr_handle->cookie);
467 CDEBUG(D_HA, "Store old cookie %#llx in new mfd\n",
468 info->mti_rr.rr_handle->cookie);
470 mfd->mfd_old_handle.cookie = info->mti_rr.rr_handle->cookie;
473 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
475 if (req->rq_export->exp_disconnected) {
476 spin_lock(&med->med_open_lock);
477 class_handle_unhash(&mfd->mfd_handle);
478 list_del_init(&mfd->mfd_list);
479 spin_unlock(&med->med_open_lock);
480 mdt_mfd_close(info, mfd);
482 spin_lock(&med->med_open_lock);
483 list_add_tail(&mfd->mfd_list, &med->med_open_head);
484 spin_unlock(&med->med_open_lock);
487 mdt_empty_transno(info, rc);
492 if (flags & FMODE_WRITE)
494 else if (flags & MDS_FMODE_EXEC)
500 static int mdt_finish_open(struct mdt_thread_info *info,
501 struct mdt_object *p, struct mdt_object *o,
502 __u64 flags, int created, struct ldlm_reply *rep)
504 struct ptlrpc_request *req = mdt_info_req(info);
505 struct obd_export *exp = req->rq_export;
506 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
507 struct md_attr *ma = &info->mti_attr;
508 struct lu_attr *la = &ma->ma_attr;
509 struct mdt_file_data *mfd;
510 struct mdt_body *repbody;
512 int isreg, isdir, islnk;
516 LASSERT(ma->ma_valid & MA_INODE);
518 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
520 isreg = S_ISREG(la->la_mode);
521 isdir = S_ISDIR(la->la_mode);
522 islnk = S_ISLNK(la->la_mode);
523 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
525 /* LU-2275, simulate broken behaviour (esp. prevalent in
526 * pre-2.4 servers where a very strange reply is sent on error
527 * that looks like it was actually almost successful and a
528 * failure at the same time.) */
529 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
530 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
534 if (flags & MDS_OPEN_LOCK)
535 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
540 #ifdef CONFIG_FS_POSIX_ACL
541 if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
542 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
544 RETURN(PTR_ERR(nodemap));
546 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
547 nodemap_putref(nodemap);
554 * If we are following a symlink, don't open; and do not return open
555 * handle for special nodes as client required.
557 if (islnk || (!isreg && !isdir &&
558 (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
559 lustre_msg_set_transno(req->rq_repmsg, 0);
564 * We need to return the existing object's fid back, so it is done here,
565 * after preparing the reply.
567 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
570 /* This can't be done earlier, we need to return reply body */
572 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
573 /* We are trying to create or write an existing dir. */
576 } else if (flags & MDS_OPEN_DIRECTORY)
579 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
580 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
585 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
586 spin_lock(&med->med_open_lock);
587 list_for_each(t, &med->med_open_head) {
588 mfd = list_entry(t, struct mdt_file_data, mfd_list);
589 if (mfd->mfd_xid == req->rq_xid)
593 spin_unlock(&med->med_open_lock);
596 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
597 /* set repbody->ea_size for resent case */
598 if (ma->ma_valid & MA_LOV) {
599 LASSERT(ma->ma_lmm_size != 0);
600 repbody->mbo_eadatasize = ma->ma_lmm_size;
602 repbody->mbo_valid |= OBD_MD_FLDIREA;
604 repbody->mbo_valid |= OBD_MD_FLEASIZE;
606 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
611 rc = mdt_mfd_open(info, p, o, flags, created, rep);
613 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
618 void mdt_reconstruct_open(struct mdt_thread_info *info,
619 struct mdt_lock_handle *lhc)
621 const struct lu_env *env = info->mti_env;
622 struct mdt_device *mdt = info->mti_mdt;
623 struct req_capsule *pill = info->mti_pill;
624 struct ptlrpc_request *req = mdt_info_req(info);
625 struct md_attr *ma = &info->mti_attr;
626 struct mdt_reint_record *rr = &info->mti_rr;
627 __u64 flags = info->mti_spec.sp_cr_flags;
628 struct ldlm_reply *ldlm_rep;
629 struct mdt_object *parent;
630 struct mdt_object *child;
631 struct mdt_body *repbody;
636 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
637 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
638 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
640 ma->ma_need = MA_INODE | MA_HSM;
643 opdata = mdt_req_from_lrd(req, info->mti_reply_data);
644 mdt_set_disposition(info, ldlm_rep, opdata);
646 CDEBUG(D_INODE, "This is reconstruct open: disp=%#llx, result=%d\n",
647 ldlm_rep->lock_policy_res1, req->rq_status);
649 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
651 /* We did not create successfully, return error to client. */
652 GOTO(out, rc = req->rq_status);
654 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
655 struct obd_export *exp = req->rq_export;
657 * We failed after creation, but we do not know in which step
658 * we failed. So try to check the child object.
660 parent = mdt_object_find(env, mdt, rr->rr_fid1);
661 if (IS_ERR(parent)) {
662 rc = PTR_ERR(parent);
663 LCONSOLE_WARN("Parent "DFID" lookup error %d."
664 " Evicting client %s with export %s.\n",
665 PFID(rr->rr_fid1), rc,
666 obd_uuid2str(&exp->exp_client_uuid),
667 obd_export_nid2str(exp));
668 mdt_export_evict(exp);
672 child = mdt_object_find(env, mdt, rr->rr_fid2);
675 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
676 "evicting client %s with export %s\n",
677 PFID(rr->rr_fid2), rc,
678 obd_uuid2str(&exp->exp_client_uuid),
679 obd_export_nid2str(exp));
680 mdt_object_put(env, parent);
681 mdt_export_evict(exp);
685 if (unlikely(mdt_object_remote(child))) {
686 /* the child object was created on remote server */
687 if (!mdt_is_dne_client(exp)) {
688 /* Return -EIO for old client */
689 mdt_object_put(env, parent);
690 mdt_object_put(env, child);
691 GOTO(out, rc = -EIO);
693 repbody->mbo_fid1 = *rr->rr_fid2;
694 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
697 if (mdt_object_exists(child)) {
698 mdt_prep_ma_buf_from_rep(info, child, ma);
699 rc = mdt_attr_get_complex(info, child, ma);
701 rc = mdt_finish_open(info, parent,
705 /* the child does not exist, we should do
707 mdt_object_put(env, parent);
708 mdt_object_put(env, child);
709 GOTO(regular_open, 0);
712 mdt_object_put(env, parent);
713 mdt_object_put(env, child);
717 /* We did not try to create, so we are a pure open */
718 rc = mdt_reint_open(info, lhc);
724 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
725 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
728 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep)
730 __u64 flags = info->mti_spec.sp_cr_flags;
731 struct mdt_reint_record *rr = &info->mti_rr;
732 struct md_attr *ma = &info->mti_attr;
733 struct mdt_object *o;
737 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
739 RETURN(rc = PTR_ERR(o));
741 if (unlikely(mdt_object_remote(o))) {
742 /* the child object was created on remote server */
743 struct mdt_body *repbody;
745 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
748 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
749 repbody->mbo_fid1 = *rr->rr_fid2;
750 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
753 if (mdt_object_exists(o)) {
754 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
757 mdt_prep_ma_buf_from_rep(info, o, ma);
758 rc = mdt_attr_get_complex(info, o, ma);
760 rc = mdt_finish_open(info, NULL, o, flags, 0,
767 mdt_object_put(info->mti_env, o);
771 /* lock object for open */
772 static int mdt_object_open_lock(struct mdt_thread_info *info,
773 struct mdt_object *obj,
774 struct mdt_lock_handle *lhc,
777 struct md_attr *ma = &info->mti_attr;
778 __u64 open_flags = info->mti_spec.sp_cr_flags;
780 enum ldlm_mode lm = LCK_CR;
781 bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
782 bool try_layout = false;
783 bool create_layout = false;
785 int dom_stripes = LMM_NO_DOM;
786 bool dom_lock = false;
791 mdt_lock_handle_init(lhc);
793 if (req_is_replay(mdt_info_req(info)))
796 if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
797 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
798 md_should_create(open_flags))
799 create_layout = true;
800 if (exp_connect_layout(info->mti_exp) && !create_layout &&
801 ma->ma_need & MA_LOV)
804 /* DoM files can have just MDT stripe or combined MDT + OST
806 * - In the first case the open for read/write will do IO to
807 * the MDT stripe and it makes sense to take IO lock in
808 * advance along with OPEN even if it is blocking lock.
809 * - In the second case it is just size of MDT stripe and it
810 * is quite unlikely that client will write into it, though
811 * it may read it. So IO lock will be taken optionally if it
812 * is non-blocking one.
814 if (ma->ma_valid & MA_LOV && ma->ma_lmm != NULL)
815 dom_stripes = mdt_lmm_dom_entry(ma->ma_lmm);
817 if (dom_stripes == LMM_DOM_ONLY &&
818 info->mti_mdt->mdt_opts.mo_dom_lock != 0 &&
819 !mdt_dom_client_has_lock(info, mdt_object_fid(obj)))
824 /* lease open, acquire write mode of open sem */
825 down_write(&obj->mot_open_sem);
827 /* Lease exists and ask for new lease */
828 if (atomic_read(&obj->mot_lease_count) > 0) {
829 /* only exclusive open is supported, so lease
830 * are conflicted to each other */
831 GOTO(out, rc = -EBUSY);
834 /* Lease must be with open lock */
835 if (!(open_flags & MDS_OPEN_LOCK)) {
836 CERROR("%s: Request lease for file:"DFID ", but open lock "
837 "is missed, open_flags = %#llo : rc = %d\n",
838 mdt_obd_name(info->mti_mdt),
839 PFID(mdt_object_fid(obj)), open_flags, -EPROTO);
840 GOTO(out, rc = -EPROTO);
843 /* XXX: only exclusive open is supported. */
845 *ibits = MDS_INODELOCK_OPEN;
847 /* never grant LCK_EX layout lock to client */
849 } else { /* normal open */
850 /* normal open holds read mode of open sem */
851 down_read(&obj->mot_open_sem);
853 if (open_flags & MDS_OPEN_LOCK) {
854 if (open_flags & FMODE_WRITE)
856 else if (open_flags & MDS_FMODE_EXEC)
861 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
862 } else if (atomic_read(&obj->mot_lease_count) > 0) {
863 if (open_flags & FMODE_WRITE)
869 *ibits = MDS_INODELOCK_OPEN;
872 lhc = &info->mti_lh[MDT_LH_LOCAL];
873 } else if (dom_lock) {
874 lm = (open_flags & FMODE_WRITE) ? LCK_PW : LCK_PR;
875 *ibits = MDS_INODELOCK_DOM;
878 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
879 PFID(mdt_object_fid(obj)),
880 atomic_read(&obj->mot_open_count), lm);
883 mdt_lock_reg_init(lhc, lm);
885 /* one problem to return layout lock on open is that it may result
886 * in too many layout locks cached on the client side. */
887 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
888 trybits |= MDS_INODELOCK_LAYOUT;
891 if (*ibits | trybits)
892 rc = mdt_object_lock_try(info, obj, lhc, ibits, trybits, false);
894 CDEBUG(D_INODE, "%s: Requested bits lock:"DFID ", ibits = %#llx/%#llx"
895 ", open_flags = %#llo, try_layout = %d : rc = %d\n",
896 mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(obj)),
897 *ibits, trybits, open_flags, try_layout, rc);
899 /* will change layout, revoke layout locks by enqueuing EX lock. */
900 if (rc == 0 && create_layout) {
901 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
903 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
904 ", open_flags = %#llo\n",
905 PFID(mdt_object_fid(obj)), open_flags);
907 /* We cannot enqueue another lock for the same resource we
908 * already have a lock for, due to mechanics of waiting list
909 * iterating in ldlm, see LU-3601.
910 * As such we'll drop the open lock we just got above here,
911 * it's ok not to have this open lock as it's main purpose is to
912 * flush unused cached client open handles. */
913 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
914 mdt_object_unlock(info, obj, lhc, 1);
916 LASSERT(!try_layout);
917 mdt_lock_handle_init(ll);
918 mdt_lock_reg_init(ll, LCK_EX);
919 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT);
921 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
924 /* Check if there is any other open handles after acquiring
925 * open lock. At this point, caching open handles have been revoked
927 * XXX: Now only exclusive open is supported. Need to check the
928 * type of open for generic lease support. */
929 if (rc == 0 && acq_lease) {
930 struct ptlrpc_request *req = mdt_info_req(info);
931 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
932 struct mdt_file_data *mfd;
933 bool is_replay_or_resent;
936 /* For lease: application can open a file and then apply lease,
937 * @handle contains original open handle in that case.
938 * In recovery, open REQ will be replayed and the lease REQ may
939 * be resent that means the open handle is already stale, so we
940 * need to fix it up here by finding new handle. */
941 is_replay_or_resent = req_is_replay(req) ||
942 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
944 /* if the request is _not_ a replay request, rr_handle
945 * may be used to hold an openhandle which is issuing the
946 * lease request, so that this openhandle doesn't count. */
947 mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
948 is_replay_or_resent);
952 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
953 PFID(mdt_object_fid(obj)),
954 atomic_read(&obj->mot_open_count), open_count);
956 if (atomic_read(&obj->mot_open_count) > open_count)
957 GOTO(out, rc = -EBUSY);
965 static void mdt_object_open_unlock(struct mdt_thread_info *info,
966 struct mdt_object *obj,
967 struct mdt_lock_handle *lhc,
970 __u64 open_flags = info->mti_spec.sp_cr_flags;
971 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
974 if (req_is_replay(mdt_info_req(info)))
977 /* Release local lock - the lock put in MDT_LH_LOCAL will never
978 * return to client side. */
979 if (lustre_handle_is_used(&ll->mlh_reg_lh))
980 mdt_object_unlock(info, obj, ll, 1);
982 ll = &info->mti_lh[MDT_LH_LAYOUT];
983 /* Release local layout lock, layout was created */
984 if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
985 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
986 mdt_object_unlock(info, obj, ll, 1);
989 if (open_flags & MDS_OPEN_LEASE)
990 up_write(&obj->mot_open_sem);
992 up_read(&obj->mot_open_sem);
994 /* Cross-ref case, the lock should be returned to the client */
995 if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
998 if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT) &&
999 !(ibits & MDS_INODELOCK_DOM)) {
1000 /* for the open request, the lock will only return to client
1001 * if open or layout lock is granted. */
1005 if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1006 struct ldlm_reply *ldlm_rep;
1008 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1009 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1010 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1011 mdt_object_unlock(info, obj, lhc, 1);
1017 * Check release is permitted for the current HSM flags.
1019 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1021 if (!(ma->ma_valid & MA_HSM))
1024 if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1027 if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1033 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1034 struct ldlm_reply *rep,
1035 struct mdt_lock_handle *lhc)
1037 const struct lu_env *env = info->mti_env;
1038 struct mdt_device *mdt = info->mti_mdt;
1039 __u64 flags = info->mti_spec.sp_cr_flags;
1040 struct mdt_reint_record *rr = &info->mti_rr;
1041 struct md_attr *ma = &info->mti_attr;
1042 struct mdt_object *parent= NULL;
1043 struct mdt_object *o;
1045 bool object_locked = false;
1049 if (md_should_create(flags)) {
1050 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1051 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1052 if (IS_ERR(parent)) {
1053 CDEBUG(D_INODE, "Fail to find parent "DFID
1054 " for anonymous created %ld, try to"
1055 " use server-side parent.\n",
1056 PFID(rr->rr_fid1), PTR_ERR(parent));
1061 ma->ma_need |= MA_PFID;
1064 o = mdt_object_find(env, mdt, rr->rr_fid2);
1066 GOTO(out_parent_put, rc = PTR_ERR(o));
1068 if (mdt_object_remote(o)) {
1069 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1070 mdt_obd_name(info->mti_mdt),
1072 GOTO(out, rc = -EREMOTE);
1073 } else if (!mdt_object_exists(o)) {
1074 mdt_set_disposition(info, rep,
1078 GOTO(out, rc = -ENOENT);
1081 mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1083 mdt_prep_ma_buf_from_rep(info, o, ma);
1084 if (flags & MDS_OPEN_RELEASE)
1085 ma->ma_need |= MA_HSM;
1086 rc = mdt_attr_get_complex(info, o, ma);
1090 /* We should not change file's existing LOV EA */
1091 if (S_ISREG(lu_object_attr(&o->mot_obj)) &&
1092 flags & MDS_OPEN_HAS_EA && ma->ma_valid & MA_LOV)
1093 GOTO(out, rc = -EEXIST);
1095 /* If a release request, check file flags are fine and ask for an
1096 * exclusive open access. */
1097 if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1098 GOTO(out, rc = -EPERM);
1100 rc = mdt_check_resent_lock(info, o, lhc);
1103 } else if (rc > 0) {
1104 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1105 object_locked = true;
1107 GOTO(out_unlock, rc);
1110 if (ma->ma_valid & MA_PFID) {
1111 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1112 if (IS_ERR(parent)) {
1113 CDEBUG(D_INODE, "Fail to find parent "DFID
1114 " for anonymous created %ld, try to"
1115 " use system default.\n",
1116 PFID(&ma->ma_pfid), PTR_ERR(parent));
1121 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1123 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1124 if (flags & MDS_OPEN_LOCK)
1125 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1126 if (flags & MDS_OPEN_LEASE)
1127 mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1129 GOTO(out_unlock, rc);
1133 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1135 mdt_object_put(env, o);
1137 mdt_pack_size2body(info, rr->rr_fid2,
1138 ibits & MDS_INODELOCK_DOM);
1141 mdt_object_put(env, parent);
1145 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1146 static int mdt_cross_open(struct mdt_thread_info *info,
1147 const struct lu_fid *parent_fid,
1148 const struct lu_fid *fid,
1149 struct ldlm_reply *rep, __u32 flags)
1151 struct md_attr *ma = &info->mti_attr;
1152 struct mdt_object *o;
1156 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1158 RETURN(rc = PTR_ERR(o));
1160 if (mdt_object_remote(o)) {
1161 /* Something is wrong here, the object is on another MDS! */
1162 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1163 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1164 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1166 "Object isn't on this server! FLD error?");
1169 if (mdt_object_exists(o)) {
1170 /* Do permission check for cross-open. */
1171 rc = mo_permission(info->mti_env, NULL,
1172 mdt_object_child(o),
1173 NULL, flags | MDS_OPEN_CROSS);
1177 mdt_prep_ma_buf_from_rep(info, o, ma);
1178 rc = mdt_attr_get_complex(info, o, ma);
1182 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1185 * Something is wrong here. lookup was positive but
1186 * there is no object!
1188 CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1189 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1194 mdt_object_put(info->mti_env, o);
1199 * find root object and take its xattr lock if it's on remote MDT, later create
1200 * may use fs default striping (which is stored in root xattr).
1202 static int mdt_lock_root_xattr(struct mdt_thread_info *info,
1203 struct mdt_device *mdt)
1205 struct mdt_object *md_root = mdt->mdt_md_root;
1206 struct lustre_handle lhroot;
1209 if (md_root == NULL) {
1210 lu_root_fid(&info->mti_tmp_fid1);
1211 md_root = mdt_object_find(info->mti_env, mdt,
1212 &info->mti_tmp_fid1);
1213 if (IS_ERR(md_root))
1214 return PTR_ERR(md_root);
1216 spin_lock(&mdt->mdt_lock);
1217 if (mdt->mdt_md_root != NULL) {
1218 spin_unlock(&mdt->mdt_lock);
1220 LASSERTF(mdt->mdt_md_root == md_root,
1221 "Different root object ("
1222 DFID") instances, %p, %p\n",
1223 PFID(&info->mti_tmp_fid1),
1224 mdt->mdt_md_root, md_root);
1225 LASSERT(atomic_read(
1226 &md_root->mot_obj.lo_header->loh_ref) > 1);
1228 mdt_object_put(info->mti_env, md_root);
1230 mdt->mdt_md_root = md_root;
1231 spin_unlock(&mdt->mdt_lock);
1235 if (md_root->mot_cache_attr || !mdt_object_remote(md_root))
1238 rc = mdt_remote_object_lock(info, md_root, mdt_object_fid(md_root),
1239 &lhroot, LCK_PR, MDS_INODELOCK_XATTR,
1244 md_root->mot_cache_attr = 1;
1246 /* don't cancel this lock, so that we know the cached xattr is valid. */
1247 ldlm_lock_decref(&lhroot, LCK_PR);
1252 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1254 struct mdt_device *mdt = info->mti_mdt;
1255 struct ptlrpc_request *req = mdt_info_req(info);
1256 struct mdt_object *parent;
1257 struct mdt_object *child;
1258 struct mdt_lock_handle *lh;
1259 struct ldlm_reply *ldlm_rep;
1260 struct mdt_body *repbody;
1261 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1262 struct md_attr *ma = &info->mti_attr;
1263 __u64 create_flags = info->mti_spec.sp_cr_flags;
1265 struct mdt_reint_record *rr = &info->mti_rr;
1268 int object_locked = 0;
1272 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1273 (obd_timeout + 1) / 4);
1275 mdt_counter_incr(req, LPROC_MDT_OPEN);
1276 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1278 ma->ma_need = MA_INODE;
1281 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1282 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1284 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1285 CERROR("file join is not supported anymore.\n");
1286 GOTO(out, result = err_serious(-EOPNOTSUPP));
1288 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1290 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1291 info->mti_spec.u.sp_ea.eadata == NULL)
1292 GOTO(out, result = err_serious(-EINVAL));
1294 if (create_flags & FMODE_WRITE &&
1295 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1296 GOTO(out, result = -EROFS);
1298 CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1299 "cr_flag=%#llo mode=0%06o msg_flag=0x%x\n",
1300 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1301 PFID(rr->rr_fid2), create_flags,
1302 ma->ma_attr.la_mode, msg_flags);
1304 if (info->mti_cross_ref) {
1305 /* This is cross-ref open */
1306 mdt_set_disposition(info, ldlm_rep,
1307 (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1309 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1310 ldlm_rep, create_flags);
1312 } else if (req_is_replay(req)) {
1313 result = mdt_open_by_fid(info, ldlm_rep);
1315 if (result != -ENOENT)
1318 /* We didn't find the correct object, so we need to re-create it
1319 * via a regular replay. */
1320 if (!(create_flags & MDS_OPEN_CREAT)) {
1321 DEBUG_REQ(D_ERROR, req,
1322 "OPEN & CREAT not in open replay/by_fid.");
1323 GOTO(out, result = -EFAULT);
1325 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1326 } else if (create_flags & (MDS_OPEN_BY_FID | MDS_OPEN_LOCK)) {
1328 * MDS_OPEN_LOCK is checked for backward compatibility with 2.1
1331 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1333 CDEBUG(D_INFO, "no object for "DFID": %d\n",
1334 PFID(rr->rr_fid2), result);
1338 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1339 GOTO(out, result = err_serious(-ENOMEM));
1341 mdt_set_disposition(info, ldlm_rep,
1342 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1344 if (!lu_name_is_valid(&rr->rr_name))
1345 GOTO(out, result = -EPROTO);
1347 result = mdt_lock_root_xattr(info, mdt);
1352 lh = &info->mti_lh[MDT_LH_PARENT];
1353 mdt_lock_pdo_init(lh,
1354 (create_flags & MDS_OPEN_CREAT) ? LCK_PW : LCK_PR,
1357 parent = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
1359 GOTO(out, result = PTR_ERR(parent));
1361 result = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
1363 mdt_object_put(info->mti_env, parent);
1367 /* get and check version of parent */
1368 result = mdt_version_get_check(info, parent, 0);
1370 GOTO(out_parent, result);
1372 fid_zero(child_fid);
1374 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1375 &rr->rr_name, child_fid, &info->mti_spec);
1377 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1378 "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1379 PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1382 if (result != 0 && result != -ENOENT && result != -ESTALE)
1383 GOTO(out_parent, result);
1385 if (result == -ENOENT || result == -ESTALE) {
1386 /* If the object is dead, let's check if the object
1387 * is being migrated to a new object */
1388 if (result == -ESTALE) {
1389 struct lu_buf lmv_buf;
1391 lmv_buf.lb_buf = info->mti_xattr_buf;
1392 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
1393 rc = mo_xattr_get(info->mti_env,
1394 mdt_object_child(parent),
1395 &lmv_buf, XATTR_NAME_LMV);
1397 struct lmv_mds_md_v1 *lmv;
1399 lmv = lmv_buf.lb_buf;
1400 if (le32_to_cpu(lmv->lmv_hash_type) &
1401 LMV_HASH_FLAG_MIGRATION) {
1402 /* Get the new parent FID and retry */
1403 mdt_object_unlock_put(info, parent,
1405 mdt_lock_handle_init(lh);
1407 (struct lu_fid *)rr->rr_fid1,
1408 &lmv->lmv_stripe_fids[1]);
1414 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1415 if (result == -ESTALE) {
1417 * -ESTALE means the parent is a dead(unlinked) dir, so
1418 * it should return -ENOENT to in accordance with the
1419 * original mds implementaion.
1421 GOTO(out_parent, result = -ENOENT);
1424 if (!(create_flags & MDS_OPEN_CREAT))
1425 GOTO(out_parent, result);
1426 if (mdt_rdonly(req->rq_export))
1427 GOTO(out_parent, result = -EROFS);
1428 *child_fid = *info->mti_rr.rr_fid2;
1429 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1431 /* In the function below, .hs_keycmp resolves to
1432 * lu_obj_hop_keycmp() */
1433 /* coverity[overrun-buffer-val] */
1434 child = mdt_object_new(info->mti_env, mdt, child_fid);
1437 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1438 * return FID back in that case.
1440 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1441 child = mdt_object_find(info->mti_env, mdt, child_fid);
1444 GOTO(out_parent, result = PTR_ERR(child));
1446 /** check version of child */
1447 rc = mdt_version_get_check(info, child, 1);
1449 GOTO(out_child, result = rc);
1451 if (result == -ENOENT) {
1452 /* Create under OBF and .lustre is not permitted */
1453 if (!fid_is_md_operative(rr->rr_fid1))
1454 GOTO(out_child, result = -EPERM);
1456 /* save versions in reply */
1457 mdt_version_get_save(info, parent, 0);
1458 mdt_version_get_save(info, child, 1);
1460 /* version of child will be changed */
1461 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1463 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1464 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1466 /* Let lower layers know what is lock mode on directory. */
1467 info->mti_spec.sp_cr_mode =
1468 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1470 /* Don't do lookup sanity check. We know name doesn't exist. */
1471 info->mti_spec.sp_cr_lookup = 0;
1472 info->mti_spec.sp_feat = &dt_directory_features;
1474 result = mdo_create(info->mti_env, mdt_object_child(parent),
1475 &rr->rr_name, mdt_object_child(child),
1476 &info->mti_spec, &info->mti_attr);
1477 if (result == -ERESTART) {
1478 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1479 GOTO(out_child, result);
1481 mdt_prep_ma_buf_from_rep(info, child, ma);
1482 /* XXX: we should call this once, see few lines below */
1484 result = mdt_attr_get_complex(info, child, ma);
1487 GOTO(out_child, result);
1490 mdt_counter_incr(req, LPROC_MDT_MKNOD);
1493 * The object is on remote node, return its FID for remote open.
1495 if (mdt_object_remote(child)) {
1497 * Check if this lock already was sent to client and
1498 * this is resent case. For resent case do not take lock
1499 * again, use what is already granted.
1501 LASSERT(lhc != NULL);
1503 rc = mdt_check_resent_lock(info, child, lhc);
1505 GOTO(out_child, result = rc);
1506 } else if (rc > 0) {
1507 mdt_lock_handle_init(lhc);
1508 mdt_lock_reg_init(lhc, LCK_PR);
1510 rc = mdt_object_lock(info, child, lhc,
1511 MDS_INODELOCK_LOOKUP);
1513 repbody->mbo_fid1 = *mdt_object_fid(child);
1514 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1518 result = -MDT_EREMOTE_OPEN;
1519 GOTO(out_child, result);
1520 } else if (mdt_object_exists(child)) {
1521 /* We have to get attr & LOV EA & HSM for this
1523 mdt_prep_ma_buf_from_rep(info, child, ma);
1524 ma->ma_need |= MA_HSM;
1525 result = mdt_attr_get_complex(info, child, ma);
1527 GOTO(out_child, result);
1529 /* Object does not exist. Likely FS corruption. */
1530 CERROR("%s: name '"DNAME"' present, but FID "
1531 DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1532 PNAME(&rr->rr_name), PFID(child_fid));
1533 GOTO(out_child, result = -EIO);
1537 rc = mdt_check_resent_lock(info, child, lhc);
1539 GOTO(out_child, result = rc);
1540 } else if (rc == 0) {
1541 /* the open lock might already be gotten in
1542 * ldlm_handle_enqueue() */
1543 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1544 if (create_flags & MDS_OPEN_LOCK)
1545 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1547 /* get openlock if this isn't replay and client requested it */
1548 if (!req_is_replay(req)) {
1549 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1552 GOTO(out_child_unlock, result = rc);
1553 else if (create_flags & MDS_OPEN_LOCK)
1554 mdt_set_disposition(info, ldlm_rep,
1558 /* Try to open it now. */
1559 rc = mdt_finish_open(info, parent, child, create_flags,
1563 /* openlock will be released if mdt_finish_open failed */
1564 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1566 if (created && create_flags & MDS_OPEN_VOLATILE) {
1567 CERROR("%s: cannot open volatile file "DFID", orphan "
1568 "file will be left in PENDING directory until "
1569 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1570 PFID(mdt_object_fid(child)), rc);
1571 GOTO(out_child_unlock, result);
1577 rc = mdo_unlink(info->mti_env,
1578 mdt_object_child(parent),
1579 mdt_object_child(child),
1581 &info->mti_attr, 0);
1583 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1584 mdt_obd_name(info->mti_mdt),
1585 PFID(mdt_object_fid(child)), rc);
1586 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1592 mdt_object_open_unlock(info, child, lhc, ibits, result);
1594 mdt_object_put(info->mti_env, child);
1596 mdt_pack_size2body(info, child_fid, ibits & MDS_INODELOCK_DOM);
1598 mdt_object_unlock_put(info, parent, lh, result || !created);
1601 lustre_msg_set_transno(req->rq_repmsg, 0);
1606 * Create an orphan object use local root.
1608 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1609 struct mdt_device *mdt,
1610 const struct lu_fid *fid,
1611 struct md_attr *attr, fmode_t fmode)
1613 const struct lu_env *env = info->mti_env;
1614 struct md_op_spec *spec = &info->mti_spec;
1615 struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1616 struct mdt_object *obj = NULL;
1617 struct mdt_object *local_root;
1618 static const struct lu_name lname = {
1619 .ln_name = "i_am_nobody",
1620 .ln_namelen = sizeof("i_am_nobody") - 1,
1622 struct lu_ucred *uc;
1623 cfs_cap_t uc_cap_save;
1627 rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1629 RETURN(ERR_PTR(rc));
1631 local_root = mdt_object_find(env, mdt, local_root_fid);
1632 if (IS_ERR(local_root))
1635 obj = mdt_object_new(env, mdt, fid);
1637 GOTO(out, rc = PTR_ERR(obj));
1639 spec->sp_cr_lookup = 0;
1640 spec->sp_feat = &dt_directory_features;
1641 spec->sp_cr_mode = MDL_MINMODE; /* no lock */
1642 spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1643 if (attr->ma_valid & MA_LOV) {
1644 spec->u.sp_ea.eadata = attr->ma_lmm;
1645 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1646 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1648 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1652 uc_cap_save = uc->uc_cap;
1653 uc->uc_cap |= 1 << CFS_CAP_DAC_OVERRIDE;
1654 rc = mdo_create(env, mdt_object_child(local_root), &lname,
1655 mdt_object_child(obj), spec, attr);
1656 uc->uc_cap = uc_cap_save;
1658 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1659 mdt_obd_name(mdt), PFID(fid), rc);
1663 rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
1665 CERROR("%s: cannot open volatile file "DFID", orphan "
1666 "file will be left in PENDING directory until "
1667 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1674 mdt_object_put(env, obj);
1677 mdt_object_put(env, local_root);
1681 /* XXX Look into layout in MDT layer. */
1682 static inline int mdt_hsm_set_released(struct lov_mds_md *lmm)
1684 struct lov_comp_md_v1 *comp_v1;
1685 struct lov_mds_md *v1;
1689 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1_DEFINED)) {
1690 comp_v1 = (struct lov_comp_md_v1 *)lmm;
1692 if (comp_v1->lcm_entry_count == 0)
1695 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
1696 off = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1697 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
1698 v1->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1701 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1706 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1709 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1710 struct lu_ucred *uc = mdt_ucred(info);
1711 struct close_data *data;
1712 struct ldlm_lock *lease;
1713 struct mdt_object *orphan;
1714 struct md_attr *orp_ma;
1722 if (mdt_rdonly(info->mti_exp))
1725 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1729 lease = ldlm_handle2lock(&data->cd_handle);
1733 /* try to hold open_sem so that nobody else can open the file */
1734 if (!down_write_trylock(&o->mot_open_sem)) {
1735 ldlm_lock_cancel(lease);
1736 GOTO(out_reprocess, rc = -EBUSY);
1739 /* Check if the lease open lease has already canceled */
1740 lock_res_and_lock(lease);
1741 lease_broken = ldlm_is_cancel(lease);
1742 unlock_res_and_lock(lease);
1744 LDLM_DEBUG(lease, DFID " lease broken? %d",
1745 PFID(mdt_object_fid(o)), lease_broken);
1747 /* Cancel server side lease. Client side counterpart should
1748 * have been cancelled. It's okay to cancel it now as we've
1749 * held mot_open_sem. */
1750 ldlm_lock_cancel(lease);
1752 if (lease_broken) /* don't perform release task */
1753 GOTO(out_unlock, rc = -ESTALE);
1755 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1756 GOTO(out_unlock, rc = -EINVAL);
1758 /* ma_need was set before but it seems fine to change it in order to
1759 * avoid modifying the one from RPC */
1760 ma->ma_need = MA_HSM;
1761 rc = mdt_attr_get_complex(info, o, ma);
1763 GOTO(out_unlock, rc);
1765 if (!mdt_hsm_release_allow(ma))
1766 GOTO(out_unlock, rc = -EPERM);
1768 /* already released? */
1769 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1770 GOTO(out_unlock, rc = 0);
1772 /* Compare on-disk and packed data_version */
1773 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
1774 CDEBUG(D_HSM, DFID" data_version mismatches: packed=%llu"
1775 " and on-disk=%llu\n", PFID(mdt_object_fid(o)),
1776 data->cd_data_version, ma->ma_hsm.mh_arch_ver);
1777 GOTO(out_unlock, rc = -EPERM);
1780 ma->ma_valid = MA_INODE;
1781 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
1782 rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
1784 GOTO(out_unlock, rc);
1786 ma->ma_need = MA_INODE | MA_LOV;
1787 rc = mdt_attr_get_complex(info, o, ma);
1789 GOTO(out_unlock, rc);
1791 if (!(ma->ma_valid & MA_LOV)) {
1792 /* Even empty file are released */
1793 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
1794 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEFINED);
1795 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1796 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
1797 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
1799 /* Magic must be LOV_MAGIC_*_DEFINED or LOD will interpret
1800 * ma_lmm as lov_user_md, then it will be confused by union of
1801 * layout_gen and stripe_offset. */
1802 if ((le32_to_cpu(ma->ma_lmm->lmm_magic) & LOV_MAGIC_MASK) ==
1804 ma->ma_lmm->lmm_magic |= cpu_to_le32(LOV_MAGIC_DEFINED);
1806 GOTO(out_unlock, rc = -EINVAL);
1809 /* Set file as released. */
1810 rc = mdt_hsm_set_released(ma->ma_lmm);
1812 GOTO(out_unlock, rc);
1814 orp_ma = &info->mti_u.hsm.attr;
1815 orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
1816 /* We use root ownership to bypass potential quota
1817 * restrictions on the user and group of the file. */
1818 orp_ma->ma_attr.la_uid = 0;
1819 orp_ma->ma_attr.la_gid = 0;
1820 orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
1821 orp_ma->ma_lmm = ma->ma_lmm;
1822 orp_ma->ma_lmm_size = ma->ma_lmm_size;
1823 orp_ma->ma_valid = MA_INODE | MA_LOV;
1824 orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
1826 if (IS_ERR(orphan)) {
1827 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
1828 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
1830 GOTO(out_unlock, rc = PTR_ERR(orphan));
1833 /* Set up HSM attribute for orphan object */
1834 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
1835 buf = &info->mti_buf;
1836 buf->lb_buf = info->mti_xattr_buf;
1837 buf->lb_len = sizeof(struct hsm_attrs);
1838 ma->ma_hsm.mh_flags |= HS_RELEASED;
1839 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1840 ma->ma_hsm.mh_flags &= ~HS_RELEASED;
1842 mdt_lock_reg_init(lh, LCK_EX);
1843 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
1844 MDS_INODELOCK_XATTR);
1846 GOTO(out_close, rc);
1848 /* The orphan has root ownership so we need to raise
1849 * CAP_FOWNER to set the HSM attributes. */
1851 uc->uc_cap |= MD_CAP_TO_MASK(CFS_CAP_FOWNER);
1852 rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
1856 GOTO(out_layout_lock, rc);
1858 /* Swap layout with orphan objects. */
1859 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
1860 mdt_object_child(orphan),
1861 SWAP_LAYOUTS_MDS_HSM);
1865 /* Release exclusive LL */
1866 mdt_object_unlock(info, o, lh, 1);
1868 /* Close orphan object anyway */
1869 rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
1872 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
1873 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
1874 LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
1876 mdt_object_put(info->mti_env, orphan);
1879 up_write(&o->mot_open_sem);
1881 /* already released */
1883 struct mdt_body *repbody;
1885 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1886 LASSERT(repbody != NULL);
1887 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1891 ldlm_reprocess_all(lease->l_resource);
1892 LDLM_LOCK_PUT(lease);
1900 int mdt_close_handle_layouts(struct mdt_thread_info *info,
1901 struct mdt_object *o, struct md_attr *ma)
1903 struct mdt_lock_handle *lh1 = &info->mti_lh[MDT_LH_NEW];
1904 struct mdt_lock_handle *lh2 = &info->mti_lh[MDT_LH_OLD];
1905 struct close_data *data;
1906 struct ldlm_lock *lease;
1907 struct mdt_object *o1 = o, *o2;
1913 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
1916 if (!S_ISREG(lu_object_attr(&o1->mot_obj)))
1919 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1923 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1926 rc = lu_fid_cmp(&data->cd_fid, mdt_object_fid(o));
1927 if (unlikely(rc == 0))
1930 /* Exchange o1 and o2, to enforce locking order */
1931 swap_objects = (rc < 0);
1933 lease = ldlm_handle2lock(&data->cd_handle);
1937 o2 = mdt_object_find(info->mti_env, info->mti_mdt, &data->cd_fid);
1939 GOTO(out_lease, rc = PTR_ERR(o2));
1941 if (!S_ISREG(lu_object_attr(&o2->mot_obj))) {
1942 swap_objects = false; /* not swapped yet */
1943 GOTO(out_obj, rc = -EINVAL);
1949 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1954 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1959 /* try to hold open_sem so that nobody else can open the file */
1960 if (!down_write_trylock(&o->mot_open_sem)) {
1961 ldlm_lock_cancel(lease);
1962 GOTO(out_obj, rc = -EBUSY);
1965 /* Check if the lease open lease has already canceled */
1966 lock_res_and_lock(lease);
1967 lease_broken = ldlm_is_cancel(lease);
1968 unlock_res_and_lock(lease);
1970 LDLM_DEBUG(lease, DFID " lease broken? %d",
1971 PFID(mdt_object_fid(o)), lease_broken);
1973 /* Cancel server side lease. Client side counterpart should
1974 * have been cancelled. It's okay to cancel it now as we've
1975 * held mot_open_sem. */
1976 ldlm_lock_cancel(lease);
1979 GOTO(out_unlock_sem, rc = -ESTALE);
1981 mdt_lock_reg_init(lh1, LCK_EX);
1982 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1983 MDS_INODELOCK_XATTR);
1985 GOTO(out_unlock_sem, rc);
1987 mdt_lock_reg_init(lh2, LCK_EX);
1988 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1989 MDS_INODELOCK_XATTR);
1991 GOTO(out_unlock1, rc);
1993 /* Swap layout with orphan object */
1994 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SWAP) {
1995 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1996 mdt_object_child(o2), 0);
1997 } else if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_MERGE ||
1998 ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT) {
1999 struct lu_buf *buf = &info->mti_buf;
2000 struct md_rejig_data mrd;
2002 mrd.mrd_obj = mdt_object_child(o == o1 ? o2 : o1);
2003 if (ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT)
2004 mrd.mrd_mirror_id = data->cd_mirror_id;
2006 buf->lb_len = sizeof(mrd);
2008 rc = mo_xattr_set(info->mti_env, mdt_object_child(o), buf,
2010 ma->ma_attr_flags & MDS_CLOSE_LAYOUT_SPLIT ?
2011 LU_XATTR_SPLIT : LU_XATTR_MERGE);
2012 if (rc == 0 && ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2015 rc2 = mdt_set_som(info, o, &ma->ma_attr);
2017 CERROR(DFID": Setting i_blocks error: %d, "
2018 "i_blocks will be reported wrongly and "
2019 "can only be fixed in next resync\n",
2020 PFID(mdt_object_fid(o)), rc2);
2024 GOTO(out_unlock2, rc);
2029 /* Release exclusive LL */
2030 mdt_object_unlock(info, o2, lh2, 1);
2033 mdt_object_unlock(info, o1, lh1, 1);
2036 up_write(&o->mot_open_sem);
2038 /* already swapped */
2040 struct mdt_body *repbody;
2042 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2043 LASSERT(repbody != NULL);
2044 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2048 mdt_object_put(info->mti_env, swap_objects ? o1 : o2);
2050 ldlm_reprocess_all(lease->l_resource);
2053 LDLM_LOCK_PUT(lease);
2063 static int mdt_close_resync_done(struct mdt_thread_info *info,
2064 struct mdt_object *o, struct md_attr *ma)
2066 struct close_data *data;
2067 struct ldlm_lock *lease;
2068 struct md_layout_change layout = { 0 };
2069 __u32 *resync_ids = NULL;
2070 size_t resync_count = 0;
2075 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2078 if (!S_ISREG(lu_object_attr(&o->mot_obj)))
2081 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2085 if (ptlrpc_req_need_swab(mdt_info_req(info)))
2086 lustre_swab_close_data_resync_done(&data->cd_resync);
2088 if (!fid_is_zero(&data->cd_fid))
2091 lease = ldlm_handle2lock(&data->cd_handle);
2095 /* try to hold open_sem so that nobody else can open the file */
2096 if (!down_write_trylock(&o->mot_open_sem)) {
2097 ldlm_lock_cancel(lease);
2098 GOTO(out_reprocess, rc = -EBUSY);
2101 /* Check if the lease open lease has already canceled */
2102 lock_res_and_lock(lease);
2103 lease_broken = ldlm_is_cancel(lease);
2104 unlock_res_and_lock(lease);
2106 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2107 PFID(mdt_object_fid(o)), lease_broken);
2109 /* Cancel server side lease. Client side counterpart should
2110 * have been cancelled. It's okay to cancel it now as we've
2111 * held mot_open_sem. */
2112 ldlm_lock_cancel(lease);
2114 if (lease_broken) /* don't perform release task */
2115 GOTO(out_unlock, rc = -ESTALE);
2117 resync_count = data->cd_resync.resync_count;
2119 GOTO(out_unlock, rc = 0);
2121 if (resync_count > INLINE_RESYNC_ARRAY_SIZE) {
2124 if (!req_capsule_has_field(info->mti_pill, &RMF_U32,
2126 GOTO(out_unlock, rc = -EPROTO);
2128 OBD_ALLOC(resync_ids, resync_count * sizeof(__u32));
2130 GOTO(out_unlock, rc = -ENOMEM);
2132 data = req_capsule_client_get(info->mti_pill, &RMF_U32);
2133 memcpy(resync_ids, data, resync_count * sizeof(__u32));
2135 layout.mlc_resync_ids = resync_ids;
2137 layout.mlc_resync_ids = data->cd_resync.resync_ids_inline;
2140 layout.mlc_opc = MD_LAYOUT_RESYNC_DONE;
2141 layout.mlc_resync_count = resync_count;
2142 if (ma->ma_attr.la_valid & (LA_SIZE | LA_BLOCKS)) {
2143 layout.mlc_som.lsa_valid = LSOM_FL_VALID;
2144 layout.mlc_som.lsa_size = ma->ma_attr.la_size;
2145 layout.mlc_som.lsa_blocks = ma->ma_attr.la_blocks;
2147 rc = mdt_layout_change(info, o, &layout);
2149 GOTO(out_unlock, rc);
2154 up_write(&o->mot_open_sem);
2156 /* already released */
2158 struct mdt_body *repbody;
2160 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2161 LASSERT(repbody != NULL);
2162 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2166 OBD_FREE(resync_ids, resync_count * sizeof(__u32));
2169 ldlm_reprocess_all(lease->l_resource);
2170 LDLM_LOCK_PUT(lease);
2178 #define MFD_CLOSED(mode) ((mode) == MDS_FMODE_CLOSED)
2179 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2181 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
2184 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2186 struct mdt_object *o = mfd->mfd_object;
2187 struct md_object *next = mdt_object_child(o);
2188 struct md_attr *ma = &info->mti_attr;
2194 mode = mfd->mfd_mode;
2196 intent = ma->ma_attr_flags & MDS_CLOSE_INTENT;
2198 CDEBUG(D_INODE, "%s: close file "DFID" with intent: %llx\n",
2199 mdt_obd_name(info->mti_mdt), PFID(mdt_object_fid(o)), intent);
2202 case MDS_HSM_RELEASE: {
2203 rc = mdt_hsm_release(info, o, ma);
2205 CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2206 mdt_obd_name(info->mti_mdt),
2207 PFID(mdt_object_fid(o)), rc);
2208 /* continue to close even error occurred. */
2212 case MDS_CLOSE_LAYOUT_MERGE:
2213 case MDS_CLOSE_LAYOUT_SPLIT:
2214 case MDS_CLOSE_LAYOUT_SWAP: {
2215 rc = mdt_close_handle_layouts(info, o, ma);
2218 "%s: cannot swap layout of "DFID": rc=%d\n",
2219 mdt_obd_name(info->mti_mdt),
2220 PFID(mdt_object_fid(o)), rc);
2221 /* continue to close even if error occurred. */
2225 case MDS_CLOSE_RESYNC_DONE:
2226 rc = mdt_close_resync_done(info, o, ma);
2233 if (mode & FMODE_WRITE)
2235 else if (mode & MDS_FMODE_EXEC)
2238 /* Update atime on close only. */
2239 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
2240 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
2241 /* Set the atime only. */
2242 ma->ma_valid = MA_INODE;
2243 ma->ma_attr.la_valid = LA_ATIME;
2244 rc = mo_attr_set(info->mti_env, next, ma);
2247 /* If file data is modified, add the dirty flag. */
2248 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2249 rc = mdt_add_dirty_flag(info, o, ma);
2251 ma->ma_need |= MA_INODE;
2252 ma->ma_valid &= ~MA_INODE;
2254 LASSERT(atomic_read(&o->mot_open_count) > 0);
2255 atomic_dec(&o->mot_open_count);
2256 mdt_handle_last_unlink(info, o, ma);
2258 if (!MFD_CLOSED(mode)) {
2259 rc = mo_close(info->mti_env, next, ma, mode);
2260 mdt_dom_check_and_discard(info, o);
2263 /* adjust open and lease count */
2264 if (mode & MDS_OPEN_LEASE) {
2265 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2266 atomic_dec(&o->mot_lease_count);
2270 mdt_object_put(info->mti_env, o);
2275 int mdt_close_internal(struct mdt_thread_info *info, struct ptlrpc_request *req,
2276 struct mdt_body *repbody)
2278 struct mdt_export_data *med;
2279 struct mdt_file_data *mfd;
2284 med = &req->rq_export->exp_mdt_data;
2285 spin_lock(&med->med_open_lock);
2286 mfd = mdt_handle2mfd(med, &info->mti_close_handle, req_is_replay(req));
2287 if (mdt_mfd_closed(mfd)) {
2288 spin_unlock(&med->med_open_lock);
2289 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2290 ": cookie = %#llx\n", PFID(info->mti_rr.rr_fid1),
2291 info->mti_close_handle.cookie);
2292 /** not serious error since bug 3633 */
2295 class_handle_unhash(&mfd->mfd_handle);
2296 list_del_init(&mfd->mfd_list);
2297 spin_unlock(&med->med_open_lock);
2298 ret = mdt_mfd_close(info, mfd);
2301 RETURN(rc ? rc : ret);
2304 int mdt_close(struct tgt_session_info *tsi)
2306 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2307 struct ptlrpc_request *req = tgt_ses_req(tsi);
2308 struct md_attr *ma = &info->mti_attr;
2309 struct mdt_body *repbody = NULL;
2313 mdt_counter_incr(req, LPROC_MDT_CLOSE);
2314 /* Close may come with the Size-on-MDS update. Unpack it. */
2315 rc = mdt_close_unpack(info);
2317 GOTO(out, rc = err_serious(rc));
2319 /* These fields are no longer used and are left for compatibility.
2320 * size is always zero */
2321 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2323 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2325 rc = req_capsule_server_pack(info->mti_pill);
2326 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2327 mdt_client_compatibility(info);
2329 mdt_fix_reply(info);
2330 mdt_exit_ucred(info);
2331 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2334 /* Continue to close handle even if we can not pack reply */
2336 repbody = req_capsule_server_get(info->mti_pill,
2338 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2340 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2343 ma->ma_need = MA_INODE | MA_LOV;
2344 repbody->mbo_eadatasize = 0;
2345 repbody->mbo_aclsize = 0;
2347 rc = err_serious(rc);
2350 rc = mdt_close_internal(info, req, repbody);
2352 mdt_empty_transno(info, rc);
2354 if (repbody != NULL) {
2355 mdt_client_compatibility(info);
2356 rc = mdt_fix_reply(info);
2359 mdt_exit_ucred(info);
2360 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2361 GOTO(out, rc = err_serious(-ENOMEM));
2363 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2364 OBD_FAIL_MDS_CLOSE_NET_REP))
2365 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2367 mdt_thread_info_fini(info);
2368 RETURN(rc ? rc : ret);