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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_open.c
38 * Lustre Metadata Target (mdt) open/close file handling
40 * Author: Huang Hua <huanghua@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <lustre_acl.h>
46 #include <lustre_mds.h>
47 #include "mdt_internal.h"
48 #include <lustre_nodemap.h>
50 /* we do nothing because we do not have refcount now */
51 static void mdt_mfd_get(void *mfdp)
55 static struct portals_handle_ops mfd_handle_ops = {
56 .hop_addref = mdt_mfd_get,
60 /* Create a new mdt_file_data struct, initialize it,
61 * and insert it to global hash table */
62 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med)
64 struct mdt_file_data *mfd;
69 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
70 mfd->mfd_handle.h_owner = med;
71 INIT_LIST_HEAD(&mfd->mfd_list);
72 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
79 * Find the mfd pointed to by handle in global hash table.
80 * In case of replay the handle is obsoleted
81 * but mfd can be found in mfd list by that handle.
82 * Callers need to be holding med_open_lock.
84 struct mdt_file_data *mdt_handle2mfd(struct mdt_export_data *med,
85 const struct lustre_handle *handle,
86 bool is_replay_or_resent)
88 struct mdt_file_data *mfd;
91 LASSERT(handle != NULL);
92 mfd = class_handle2object(handle->cookie, med);
93 /* during dw/setattr replay the mfd can be found by old handle */
94 if (mfd == NULL && is_replay_or_resent) {
95 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
96 if (mfd->mfd_old_handle.cookie == handle->cookie)
106 void mdt_mfd_free(struct mdt_file_data *mfd)
108 LASSERT(list_empty(&mfd->mfd_list));
109 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
112 static int mdt_create_data(struct mdt_thread_info *info,
113 struct mdt_object *p, struct mdt_object *o)
115 struct md_op_spec *spec = &info->mti_spec;
116 struct md_attr *ma = &info->mti_attr;
120 if (!md_should_create(spec->sp_cr_flags))
123 ma->ma_need = MA_INODE | MA_LOV;
125 mutex_lock(&o->mot_lov_mutex);
126 if (!(o->mot_flags & MOF_LOV_CREATED)) {
127 rc = mdo_create_data(info->mti_env,
128 p ? mdt_object_child(p) : NULL,
129 mdt_object_child(o), spec, ma);
131 rc = mdt_attr_get_complex(info, o, ma);
133 if (rc == 0 && ma->ma_valid & MA_LOV)
134 o->mot_flags |= MOF_LOV_CREATED;
137 mutex_unlock(&o->mot_lov_mutex);
141 int mdt_write_read(struct mdt_object *o)
145 spin_lock(&o->mot_write_lock);
146 rc = o->mot_write_count;
147 spin_unlock(&o->mot_write_lock);
151 int mdt_write_get(struct mdt_object *o)
155 spin_lock(&o->mot_write_lock);
156 if (o->mot_write_count < 0)
159 o->mot_write_count++;
160 spin_unlock(&o->mot_write_lock);
165 void mdt_write_put(struct mdt_object *o)
168 spin_lock(&o->mot_write_lock);
169 o->mot_write_count--;
170 spin_unlock(&o->mot_write_lock);
174 static int mdt_write_deny(struct mdt_object *o)
178 spin_lock(&o->mot_write_lock);
179 if (o->mot_write_count > 0)
182 o->mot_write_count--;
183 spin_unlock(&o->mot_write_lock);
187 static void mdt_write_allow(struct mdt_object *o)
190 spin_lock(&o->mot_write_lock);
191 o->mot_write_count++;
192 spin_unlock(&o->mot_write_lock);
196 /* there can be no real transaction so prepare the fake one */
197 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
199 struct mdt_device *mdt = info->mti_mdt;
200 struct ptlrpc_request *req = mdt_info_req(info);
201 struct tg_export_data *ted;
202 struct lsd_client_data *lcd;
205 /* transaction has occurred already */
206 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
209 spin_lock(&mdt->mdt_lut.lut_translock);
211 if (info->mti_transno != 0) {
212 struct obd_export *exp = req->rq_export;
214 CERROR("%s: replay trans "LPU64" NID %s: rc = %d\n",
215 mdt_obd_name(mdt), info->mti_transno,
216 libcfs_nid2str(exp->exp_connection->c_peer.nid),
218 spin_unlock(&mdt->mdt_lut.lut_translock);
221 } else if (info->mti_transno == 0) {
222 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
224 /* should be replay */
225 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
226 mdt->mdt_lut.lut_last_transno = info->mti_transno;
228 spin_unlock(&mdt->mdt_lut.lut_translock);
230 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
232 req->rq_export->exp_obd->obd_last_committed);
234 req->rq_transno = info->mti_transno;
235 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
237 /* update lcd in memory only for resent cases */
238 ted = &req->rq_export->exp_target_data;
240 mutex_lock(&ted->ted_lcd_lock);
242 if (info->mti_transno < lcd->lcd_last_transno &&
243 info->mti_transno != 0) {
244 /* This should happen during replay. Do not update
245 * last rcvd info if replay req transno < last transno,
246 * otherwise the following resend(after replay) can not
247 * be checked correctly by xid */
248 mutex_unlock(&ted->ted_lcd_lock);
249 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
250 mdt_obd_name(mdt), info->mti_transno,
251 lcd->lcd_last_transno);
255 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
256 if (info->mti_transno != 0)
257 lcd->lcd_last_close_transno = info->mti_transno;
258 lcd->lcd_last_close_xid = req->rq_xid;
259 lcd->lcd_last_close_result = rc;
261 /* VBR: save versions in last_rcvd for reconstruct. */
262 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
264 lcd->lcd_pre_versions[0] = pre_versions[0];
265 lcd->lcd_pre_versions[1] = pre_versions[1];
266 lcd->lcd_pre_versions[2] = pre_versions[2];
267 lcd->lcd_pre_versions[3] = pre_versions[3];
269 if (info->mti_transno != 0)
270 lcd->lcd_last_transno = info->mti_transno;
272 lcd->lcd_last_xid = req->rq_xid;
273 lcd->lcd_last_result = rc;
274 lcd->lcd_last_data = info->mti_opdata;
276 mutex_unlock(&ted->ted_lcd_lock);
281 void mdt_mfd_set_mode(struct mdt_file_data *mfd, __u64 mode)
283 LASSERT(mfd != NULL);
285 CDEBUG(D_HA, DFID " Change mfd mode "LPO64" -> "LPO64".\n",
286 PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_mode, mode);
288 mfd->mfd_mode = mode;
292 * prep ma_lmm/ma_lmv for md_attr from reply
294 static void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
295 struct mdt_object *obj,
298 LASSERT(ma->ma_lmv == NULL && ma->ma_lmm == NULL);
299 if (S_ISDIR(obj->mot_header.loh_attr)) {
300 ma->ma_lmv = req_capsule_server_get(info->mti_pill,
302 ma->ma_lmv_size = req_capsule_get_size(info->mti_pill,
305 if (ma->ma_lmv_size > 0)
306 ma->ma_need |= MA_LMV;
308 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
310 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
313 if (ma->ma_lmm_size > 0)
314 ma->ma_need |= MA_LOV;
318 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
319 struct mdt_object *o, __u64 flags, int created,
320 struct ldlm_reply *rep)
322 struct ptlrpc_request *req = mdt_info_req(info);
323 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
324 struct mdt_file_data *mfd;
325 struct md_attr *ma = &info->mti_attr;
326 struct lu_attr *la = &ma->ma_attr;
327 struct mdt_body *repbody;
328 int rc = 0, isdir, isreg;
331 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
333 isreg = S_ISREG(la->la_mode);
334 isdir = S_ISDIR(la->la_mode);
335 if (isreg && !(ma->ma_valid & MA_LOV) && !(flags & MDS_OPEN_RELEASE)) {
337 * No EA, check whether it is will set regEA and dirEA since in
338 * above attr get, these size might be zero, so reset it, to
339 * retrieve the MD after create obj.
341 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
344 /* in replay case, p == NULL */
345 rc = mdt_create_data(info, p, o);
349 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
350 mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
353 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
354 ma->ma_valid, ma->ma_lmm_size);
356 if (ma->ma_valid & MA_LOV) {
357 LASSERT(ma->ma_lmm_size != 0);
358 repbody->mbo_eadatasize = ma->ma_lmm_size;
360 repbody->mbo_valid |= OBD_MD_FLDIREA;
362 repbody->mbo_valid |= OBD_MD_FLEASIZE;
365 if (ma->ma_valid & MA_LMV) {
366 LASSERT(ma->ma_lmv_size != 0);
367 repbody->mbo_eadatasize = ma->ma_lmv_size;
369 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
372 if (flags & FMODE_WRITE)
373 rc = mdt_write_get(o);
374 else if (flags & MDS_FMODE_EXEC)
375 rc = mdt_write_deny(o);
380 rc = mo_open(info->mti_env, mdt_object_child(o),
381 created ? flags | MDS_OPEN_CREATED : flags);
383 /* If we allow the client to chgrp (CFS_SETGRP_PERM), but the
384 * client does not know which suppgid should be sent to the MDS,
385 * or some other(s) changed the target file's GID after this RPC
386 * sent to the MDS with the suppgid as the original GID, then we
387 * give the client another chance to send the right suppgid. */
389 allow_client_chgrp(info, lu_ucred(info->mti_env)))
390 mdt_set_disposition(info, rep, DISP_OPEN_DENY);
395 mfd = mdt_mfd_new(med);
397 GOTO(err_out, rc = -ENOMEM);
400 * Keep a reference on this object for this open, and is
401 * released by mdt_mfd_close().
403 mdt_object_get(info->mti_env, o);
405 mfd->mfd_xid = req->rq_xid;
408 * @flags is always not zero. At least it should be FMODE_READ,
409 * FMODE_WRITE or MDS_FMODE_EXEC.
414 mdt_mfd_set_mode(mfd, flags);
416 atomic_inc(&o->mot_open_count);
417 if (flags & MDS_OPEN_LEASE)
418 atomic_inc(&o->mot_lease_count);
421 if (req_is_replay(req)) {
422 struct mdt_file_data *old_mfd;
423 /* Check wheather old cookie already exist in
424 * the list, becasue when do recovery, client
425 * might be disconnected from server, and
426 * restart replay, so there maybe some orphan
427 * mfd here, we should remove them */
428 LASSERT(info->mti_rr.rr_handle != NULL);
429 spin_lock(&med->med_open_lock);
430 old_mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle, true);
431 if (old_mfd != NULL) {
432 CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
433 "cookie = "LPX64"\n", mfd,
434 PFID(mdt_object_fid(mfd->mfd_object)),
435 info->mti_rr.rr_handle->cookie);
436 class_handle_unhash(&old_mfd->mfd_handle);
437 list_del_init(&old_mfd->mfd_list);
438 spin_unlock(&med->med_open_lock);
439 /* no attr update for that close */
441 ma->ma_valid |= MA_FLAGS;
442 ma->ma_attr_flags |= MDS_RECOV_OPEN;
443 mdt_mfd_close(info, old_mfd);
444 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
445 ma->ma_valid &= ~MA_FLAGS;
447 spin_unlock(&med->med_open_lock);
448 CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
449 "cookie = "LPX64"\n",
450 PFID(mdt_object_fid(mfd->mfd_object)),
451 info->mti_rr.rr_handle->cookie);
454 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
455 info->mti_rr.rr_handle->cookie);
457 mfd->mfd_old_handle.cookie = info->mti_rr.rr_handle->cookie;
460 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
462 if (req->rq_export->exp_disconnected) {
463 spin_lock(&med->med_open_lock);
464 class_handle_unhash(&mfd->mfd_handle);
465 list_del_init(&mfd->mfd_list);
466 spin_unlock(&med->med_open_lock);
467 mdt_mfd_close(info, mfd);
469 spin_lock(&med->med_open_lock);
470 list_add(&mfd->mfd_list, &med->med_open_head);
471 spin_unlock(&med->med_open_lock);
474 mdt_empty_transno(info, rc);
479 if (flags & FMODE_WRITE)
481 else if (flags & MDS_FMODE_EXEC)
487 static int mdt_finish_open(struct mdt_thread_info *info,
488 struct mdt_object *p, struct mdt_object *o,
489 __u64 flags, int created, struct ldlm_reply *rep)
491 struct ptlrpc_request *req = mdt_info_req(info);
492 struct obd_export *exp = req->rq_export;
493 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
494 struct md_attr *ma = &info->mti_attr;
495 struct lu_attr *la = &ma->ma_attr;
496 struct mdt_file_data *mfd;
497 struct mdt_body *repbody;
499 int isreg, isdir, islnk;
503 LASSERT(ma->ma_valid & MA_INODE);
505 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
507 isreg = S_ISREG(la->la_mode);
508 isdir = S_ISDIR(la->la_mode);
509 islnk = S_ISLNK(la->la_mode);
510 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
512 /* LU-2275, simulate broken behaviour (esp. prevalent in
513 * pre-2.4 servers where a very strange reply is sent on error
514 * that looks like it was actually almost succesful and a failure at the
516 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
517 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
521 if (flags & MDS_OPEN_LOCK)
522 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
527 if (exp_connect_rmtclient(exp)) {
528 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
530 rc = mdt_pack_remote_perm(info, o, buf);
532 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
533 repbody->mbo_aclsize = 0;
535 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
536 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
539 #ifdef CONFIG_FS_POSIX_ACL
540 else if (exp_connect_flags(exp) & OBD_CONNECT_ACL)
541 rc = mdt_pack_acl2body(info, repbody, o,
542 exp->exp_target_data.ted_nodemap);
545 if (info->mti_mdt->mdt_lut.lut_mds_capa &&
546 exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
547 struct lustre_capa *capa;
549 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
551 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
552 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
555 repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
557 if (info->mti_mdt->mdt_lut.lut_oss_capa &&
558 exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
559 S_ISREG(lu_object_attr(&o->mot_obj))) {
560 struct lustre_capa *capa;
562 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
564 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
565 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
568 repbody->mbo_valid |= OBD_MD_FLOSSCAPA;
572 * If we are following a symlink, don't open; and do not return open
573 * handle for special nodes as client required.
575 if (islnk || (!isreg && !isdir &&
576 (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
577 lustre_msg_set_transno(req->rq_repmsg, 0);
582 * We need to return the existing object's fid back, so it is done here,
583 * after preparing the reply.
585 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
588 /* This can't be done earlier, we need to return reply body */
590 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
591 /* We are trying to create or write an existing dir. */
594 } else if (flags & MDS_OPEN_DIRECTORY)
597 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
598 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
603 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
604 spin_lock(&med->med_open_lock);
605 list_for_each(t, &med->med_open_head) {
606 mfd = list_entry(t, struct mdt_file_data, mfd_list);
607 if (mfd->mfd_xid == req->rq_xid)
611 spin_unlock(&med->med_open_lock);
614 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
615 /* set repbody->ea_size for resent case */
616 if (ma->ma_valid & MA_LOV) {
617 LASSERT(ma->ma_lmm_size != 0);
618 repbody->mbo_eadatasize = ma->ma_lmm_size;
620 repbody->mbo_valid |= OBD_MD_FLDIREA;
622 repbody->mbo_valid |= OBD_MD_FLEASIZE;
624 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
629 rc = mdt_mfd_open(info, p, o, flags, created, rep);
631 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
636 void mdt_reconstruct_open(struct mdt_thread_info *info,
637 struct mdt_lock_handle *lhc)
639 const struct lu_env *env = info->mti_env;
640 struct mdt_device *mdt = info->mti_mdt;
641 struct req_capsule *pill = info->mti_pill;
642 struct ptlrpc_request *req = mdt_info_req(info);
643 struct tg_export_data *ted = &req->rq_export->exp_target_data;
644 struct lsd_client_data *lcd = ted->ted_lcd;
645 struct md_attr *ma = &info->mti_attr;
646 struct mdt_reint_record *rr = &info->mti_rr;
647 __u64 flags = info->mti_spec.sp_cr_flags;
648 struct ldlm_reply *ldlm_rep;
649 struct mdt_object *parent;
650 struct mdt_object *child;
651 struct mdt_body *repbody;
655 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
656 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
657 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
659 ma->ma_need = MA_INODE | MA_HSM;
662 mdt_req_from_lcd(req, lcd);
663 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
665 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
666 ldlm_rep->lock_policy_res1, req->rq_status);
668 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
670 /* We did not create successfully, return error to client. */
671 GOTO(out, rc = req->rq_status);
673 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
674 struct obd_export *exp = req->rq_export;
676 * We failed after creation, but we do not know in which step
677 * we failed. So try to check the child object.
679 parent = mdt_object_find(env, mdt, rr->rr_fid1);
680 if (IS_ERR(parent)) {
681 rc = PTR_ERR(parent);
682 LCONSOLE_WARN("Parent "DFID" lookup error %d."
683 " Evicting client %s with export %s.\n",
684 PFID(rr->rr_fid1), rc,
685 obd_uuid2str(&exp->exp_client_uuid),
686 obd_export_nid2str(exp));
687 mdt_export_evict(exp);
691 child = mdt_object_find(env, mdt, rr->rr_fid2);
694 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
695 "evicting client %s with export %s\n",
696 PFID(rr->rr_fid2), rc,
697 obd_uuid2str(&exp->exp_client_uuid),
698 obd_export_nid2str(exp));
699 mdt_object_put(env, parent);
700 mdt_export_evict(exp);
704 if (unlikely(mdt_object_remote(child))) {
705 /* the child object was created on remote server */
706 if (!mdt_is_dne_client(exp)) {
707 /* Return -EIO for old client */
708 mdt_object_put(env, parent);
709 mdt_object_put(env, child);
710 GOTO(out, rc = -EIO);
712 repbody->mbo_fid1 = *rr->rr_fid2;
713 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
716 if (mdt_object_exists(child)) {
717 mdt_set_capainfo(info, 1, rr->rr_fid2,
719 mdt_prep_ma_buf_from_rep(info, child, ma);
720 rc = mdt_attr_get_complex(info, child, ma);
722 rc = mdt_finish_open(info, parent,
726 /* the child does not exist, we should do
728 mdt_object_put(env, parent);
729 mdt_object_put(env, child);
730 GOTO(regular_open, 0);
733 mdt_object_put(env, parent);
734 mdt_object_put(env, child);
738 /* We did not try to create, so we are a pure open */
739 rc = mdt_reint_open(info, lhc);
745 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
746 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
749 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep)
751 __u64 flags = info->mti_spec.sp_cr_flags;
752 struct mdt_reint_record *rr = &info->mti_rr;
753 struct md_attr *ma = &info->mti_attr;
754 struct mdt_object *o;
758 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
760 RETURN(rc = PTR_ERR(o));
762 if (unlikely(mdt_object_remote(o))) {
763 /* the child object was created on remote server */
764 struct mdt_body *repbody;
766 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
769 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
770 repbody->mbo_fid1 = *rr->rr_fid2;
771 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
774 if (mdt_object_exists(o)) {
775 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
778 mdt_prep_ma_buf_from_rep(info, o, ma);
779 rc = mdt_attr_get_complex(info, o, ma);
781 rc = mdt_finish_open(info, NULL, o, flags, 0,
788 mdt_object_put(info->mti_env, o);
792 /* lock object for open */
793 static int mdt_object_open_lock(struct mdt_thread_info *info,
794 struct mdt_object *obj,
795 struct mdt_lock_handle *lhc,
798 struct md_attr *ma = &info->mti_attr;
799 __u64 open_flags = info->mti_spec.sp_cr_flags;
800 ldlm_mode_t lm = LCK_CR;
801 bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
802 bool try_layout = false;
803 bool create_layout = false;
808 mdt_lock_handle_init(lhc);
810 if (req_is_replay(mdt_info_req(info)))
813 if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
814 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
815 md_should_create(open_flags))
816 create_layout = true;
817 if (exp_connect_layout(info->mti_exp) && !create_layout &&
818 ma->ma_need & MA_LOV)
823 /* lease open, acquire write mode of open sem */
824 down_write(&obj->mot_open_sem);
826 /* Lease exists and ask for new lease */
827 if (atomic_read(&obj->mot_lease_count) > 0) {
828 /* only exclusive open is supported, so lease
829 * are conflicted to each other */
830 GOTO(out, rc = -EBUSY);
833 /* Lease must be with open lock */
834 if (!(open_flags & MDS_OPEN_LOCK)) {
835 CERROR("Request lease for file:"DFID ", but open lock "
836 "is missed, open_flags = "LPO64".\n",
837 PFID(mdt_object_fid(obj)), open_flags);
838 GOTO(out, rc = -EPROTO);
841 /* XXX: only exclusive open is supported. */
843 *ibits = MDS_INODELOCK_OPEN;
845 /* never grant LCK_EX layout lock to client */
847 } else { /* normal open */
848 /* normal open holds read mode of open sem */
849 down_read(&obj->mot_open_sem);
851 if (open_flags & MDS_OPEN_LOCK) {
852 if (open_flags & FMODE_WRITE)
854 else if (open_flags & MDS_FMODE_EXEC)
859 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
860 } else if (atomic_read(&obj->mot_lease_count) > 0) {
861 if (open_flags & FMODE_WRITE)
867 *ibits = MDS_INODELOCK_OPEN;
870 lhc = &info->mti_lh[MDT_LH_LOCAL];
872 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
873 PFID(mdt_object_fid(obj)),
874 atomic_read(&obj->mot_open_count), lm);
877 mdt_lock_reg_init(lhc, lm);
879 /* one problem to return layout lock on open is that it may result
880 * in too many layout locks cached on the client side. */
881 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
882 /* return lookup lock to validate inode at the client side,
883 * this is pretty important otherwise mdt will return layout
884 * lock for each open.
885 * However this is a double-edged sword because changing
886 * permission will revoke huge # of LOOKUP locks. */
887 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
888 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
890 *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
892 rc = mdt_object_lock(info, obj, lhc, *ibits,
895 } else if (*ibits != 0) {
896 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
899 CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
900 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
901 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
903 /* will change layout, revoke layout locks by enqueuing EX lock. */
904 if (rc == 0 && create_layout) {
905 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
907 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
908 ", open_flags = "LPO64"\n",
909 PFID(mdt_object_fid(obj)), open_flags);
911 /* We cannot enqueue another lock for the same resource we
912 * already have a lock for, due to mechanics of waiting list
913 * iterating in ldlm, see LU-3601.
914 * As such we'll drop the open lock we just got above here,
915 * it's ok not to have this open lock as it's main purpose is to
916 * flush unused cached client open handles. */
917 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
918 mdt_object_unlock(info, obj, lhc, 1);
920 LASSERT(!try_layout);
921 mdt_lock_handle_init(ll);
922 mdt_lock_reg_init(ll, LCK_EX);
923 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
926 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
929 /* Check if there is any other open handles after acquiring
930 * open lock. At this point, caching open handles have been revoked
932 * XXX: Now only exclusive open is supported. Need to check the
933 * type of open for generic lease support. */
934 if (rc == 0 && acq_lease) {
935 struct ptlrpc_request *req = mdt_info_req(info);
936 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
937 struct mdt_file_data *mfd;
938 bool is_replay_or_resent;
941 /* For lease: application can open a file and then apply lease,
942 * @handle contains original open handle in that case.
943 * In recovery, open REQ will be replayed and the lease REQ may
944 * be resent that means the open handle is already stale, so we
945 * need to fix it up here by finding new handle. */
946 is_replay_or_resent = req_is_replay(req) ||
947 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
949 /* if the request is _not_ a replay request, rr_handle
950 * may be used to hold an openhandle which is issuing the
951 * lease request, so that this openhandle doesn't count. */
952 mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
953 is_replay_or_resent);
957 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
958 PFID(mdt_object_fid(obj)),
959 atomic_read(&obj->mot_open_count), open_count);
961 if (atomic_read(&obj->mot_open_count) > open_count)
962 GOTO(out, rc = -EBUSY);
970 static void mdt_object_open_unlock(struct mdt_thread_info *info,
971 struct mdt_object *obj,
972 struct mdt_lock_handle *lhc,
975 __u64 open_flags = info->mti_spec.sp_cr_flags;
976 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
979 if (req_is_replay(mdt_info_req(info)))
982 /* Release local lock - the lock put in MDT_LH_LOCAL will never
983 * return to client side. */
984 if (lustre_handle_is_used(&ll->mlh_reg_lh))
985 mdt_object_unlock(info, obj, ll, 1);
987 ll = &info->mti_lh[MDT_LH_LAYOUT];
988 /* Release local layout lock, layout was created */
989 if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
990 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
991 mdt_object_unlock(info, obj, ll, 1);
994 if (open_flags & MDS_OPEN_LEASE)
995 up_write(&obj->mot_open_sem);
997 up_read(&obj->mot_open_sem);
999 /* Cross-ref case, the lock should be returned to the client */
1000 if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
1003 if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1004 /* for the open request, the lock will only return to client
1005 * if open or layout lock is granted. */
1009 if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1010 struct ldlm_reply *ldlm_rep;
1012 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1013 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1014 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1015 mdt_object_unlock(info, obj, lhc, 1);
1021 * Check release is permitted for the current HSM flags.
1023 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1025 if (!(ma->ma_valid & MA_HSM))
1028 if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1031 if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1037 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1038 struct ldlm_reply *rep,
1039 struct mdt_lock_handle *lhc)
1041 const struct lu_env *env = info->mti_env;
1042 struct mdt_device *mdt = info->mti_mdt;
1043 __u64 flags = info->mti_spec.sp_cr_flags;
1044 struct mdt_reint_record *rr = &info->mti_rr;
1045 struct md_attr *ma = &info->mti_attr;
1046 struct mdt_object *parent= NULL;
1047 struct mdt_object *o;
1049 int object_locked = 0;
1053 if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1054 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1055 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1056 if (IS_ERR(parent)) {
1057 CDEBUG(D_INODE, "Fail to find parent "DFID
1058 " for anonymous created %ld, try to"
1059 " use server-side parent.\n",
1060 PFID(rr->rr_fid1), PTR_ERR(parent));
1065 ma->ma_need |= MA_PFID;
1068 o = mdt_object_find(env, mdt, rr->rr_fid2);
1070 GOTO(out_parent_put, rc = PTR_ERR(o));
1072 if (mdt_object_remote(o)) {
1073 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1074 mdt_obd_name(info->mti_mdt),
1076 GOTO(out, rc = -EREMOTE);
1077 } else if (!mdt_object_exists(o)) {
1078 mdt_set_disposition(info, rep,
1082 GOTO(out, rc = -ENOENT);
1085 mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1087 mdt_prep_ma_buf_from_rep(info, o, ma);
1088 if (flags & MDS_OPEN_RELEASE)
1089 ma->ma_need |= MA_HSM;
1090 rc = mdt_attr_get_complex(info, o, ma);
1094 /* If a release request, check file flags are fine and ask for an
1095 * exclusive open access. */
1096 if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1097 GOTO(out, rc = -EPERM);
1099 rc = mdt_check_resent_lock(info, o, lhc);
1102 } else if (rc > 0) {
1103 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1106 GOTO(out_unlock, rc);
1109 if (ma->ma_valid & MA_PFID) {
1110 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1111 if (IS_ERR(parent)) {
1112 CDEBUG(D_INODE, "Fail to find parent "DFID
1113 " for anonymous created %ld, try to"
1114 " use system default.\n",
1115 PFID(&ma->ma_pfid), PTR_ERR(parent));
1120 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1122 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1123 if (flags & MDS_OPEN_LOCK)
1124 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1125 if (flags & MDS_OPEN_LEASE)
1126 mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1128 GOTO(out_unlock, rc);
1132 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1134 mdt_object_put(env, o);
1137 mdt_object_put(env, parent);
1141 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1142 static int mdt_cross_open(struct mdt_thread_info *info,
1143 const struct lu_fid *parent_fid,
1144 const struct lu_fid *fid,
1145 struct ldlm_reply *rep, __u32 flags)
1147 struct md_attr *ma = &info->mti_attr;
1148 struct mdt_object *o;
1152 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1154 RETURN(rc = PTR_ERR(o));
1156 if (mdt_object_remote(o)) {
1157 /* Something is wrong here, the object is on another MDS! */
1158 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1159 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1160 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1162 "Object isn't on this server! FLD error?\n");
1165 if (mdt_object_exists(o)) {
1166 /* Do permission check for cross-open. */
1167 rc = mo_permission(info->mti_env, NULL,
1168 mdt_object_child(o),
1169 NULL, flags | MDS_OPEN_CROSS);
1173 mdt_prep_ma_buf_from_rep(info, o, ma);
1174 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1175 rc = mdt_attr_get_complex(info, o, ma);
1179 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1182 * Something is wrong here. lookup was positive but
1183 * there is no object!
1185 CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1186 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1191 mdt_object_put(info->mti_env, o);
1195 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1197 struct mdt_device *mdt = info->mti_mdt;
1198 struct ptlrpc_request *req = mdt_info_req(info);
1199 struct mdt_object *parent;
1200 struct mdt_object *child;
1201 struct mdt_lock_handle *lh;
1202 struct ldlm_reply *ldlm_rep;
1203 struct mdt_body *repbody;
1204 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1205 struct md_attr *ma = &info->mti_attr;
1206 __u64 create_flags = info->mti_spec.sp_cr_flags;
1208 struct mdt_reint_record *rr = &info->mti_rr;
1211 int object_locked = 0;
1215 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1216 (obd_timeout + 1) / 4);
1218 mdt_counter_incr(req, LPROC_MDT_OPEN);
1219 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1221 ma->ma_need = MA_INODE;
1224 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1225 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1227 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1228 CERROR("file join is not supported anymore.\n");
1229 GOTO(out, result = err_serious(-EOPNOTSUPP));
1231 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1233 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1234 info->mti_spec.u.sp_ea.eadata == NULL)
1235 GOTO(out, result = err_serious(-EINVAL));
1237 CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1238 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1239 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1240 PFID(rr->rr_fid2), create_flags,
1241 ma->ma_attr.la_mode, msg_flags);
1243 if (info->mti_cross_ref) {
1244 /* This is cross-ref open */
1245 mdt_set_disposition(info, ldlm_rep,
1246 (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1248 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1249 ldlm_rep, create_flags);
1251 } else if (req_is_replay(req) ||
1252 (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1253 /* This is a replay request or from liblustre with ea. */
1254 result = mdt_open_by_fid(info, ldlm_rep);
1256 if (result != -ENOENT) {
1257 if (req->rq_export->exp_libclient &&
1258 create_flags & MDS_OPEN_HAS_EA)
1259 GOTO(out, result = 0);
1262 /* We didn't find the correct object, so we need to re-create it
1263 * via a regular replay. */
1264 if (!(create_flags & MDS_OPEN_CREAT)) {
1265 DEBUG_REQ(D_ERROR, req,
1266 "OPEN & CREAT not in open replay/by_fid.");
1267 GOTO(out, result = -EFAULT);
1269 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1270 } else if (create_flags & (MDS_OPEN_BY_FID | MDS_OPEN_LOCK)) {
1272 * MDS_OPEN_LOCK is checked for backward compatibility with 2.1
1275 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1277 CDEBUG(D_INFO, "no object for "DFID": %d\n",
1278 PFID(rr->rr_fid2), result);
1282 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1283 GOTO(out, result = err_serious(-ENOMEM));
1285 mdt_set_disposition(info, ldlm_rep,
1286 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1288 if (!lu_name_is_valid(&rr->rr_name))
1289 GOTO(out, result = -EPROTO);
1291 lh = &info->mti_lh[MDT_LH_PARENT];
1292 mdt_lock_pdo_init(lh,
1293 (create_flags & MDS_OPEN_CREAT) ? LCK_PW : LCK_PR,
1296 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1297 MDS_INODELOCK_UPDATE);
1299 GOTO(out, result = PTR_ERR(parent));
1301 /* get and check version of parent */
1302 result = mdt_version_get_check(info, parent, 0);
1304 GOTO(out_parent, result);
1306 fid_zero(child_fid);
1308 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1309 &rr->rr_name, child_fid, &info->mti_spec);
1311 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1312 "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1313 PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1316 if (result != 0 && result != -ENOENT && result != -ESTALE)
1317 GOTO(out_parent, result);
1319 if (result == -ENOENT || result == -ESTALE) {
1320 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1321 if (result == -ESTALE) {
1323 * -ESTALE means the parent is a dead(unlinked) dir, so
1324 * it should return -ENOENT to in accordance with the
1325 * original mds implementaion.
1327 GOTO(out_parent, result = -ENOENT);
1329 if (!(create_flags & MDS_OPEN_CREAT))
1330 GOTO(out_parent, result);
1331 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1332 GOTO(out_parent, result = -EROFS);
1333 *child_fid = *info->mti_rr.rr_fid2;
1334 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1336 /* In the function below, .hs_keycmp resolves to
1337 * lu_obj_hop_keycmp() */
1338 /* coverity[overrun-buffer-val] */
1339 child = mdt_object_new(info->mti_env, mdt, child_fid);
1342 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1343 * return FID back in that case.
1345 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1346 child = mdt_object_find(info->mti_env, mdt, child_fid);
1349 GOTO(out_parent, result = PTR_ERR(child));
1351 /** check version of child */
1352 rc = mdt_version_get_check(info, child, 1);
1354 GOTO(out_child, result = rc);
1356 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1357 if (result == -ENOENT) {
1358 /* Create under OBF and .lustre is not permitted */
1359 if (!fid_is_md_operative(rr->rr_fid1))
1360 GOTO(out_child, result = -EPERM);
1362 /* save versions in reply */
1363 mdt_version_get_save(info, parent, 0);
1364 mdt_version_get_save(info, child, 1);
1366 /* version of child will be changed */
1367 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1369 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1370 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1372 /* Let lower layers know what is lock mode on directory. */
1373 info->mti_spec.sp_cr_mode =
1374 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1377 * Do not perform lookup sanity check. We know that name does
1380 info->mti_spec.sp_cr_lookup = 0;
1381 info->mti_spec.sp_feat = &dt_directory_features;
1383 result = mdo_create(info->mti_env,
1384 mdt_object_child(parent),
1386 mdt_object_child(child),
1389 if (result == -ERESTART) {
1390 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1391 GOTO(out_child, result);
1393 mdt_prep_ma_buf_from_rep(info, child, ma);
1394 /* XXX: we should call this once, see few lines below */
1396 result = mdt_attr_get_complex(info, child, ma);
1399 GOTO(out_child, result);
1404 * The object is on remote node, return its FID for remote open.
1406 if (mdt_object_remote(child)) {
1408 * Check if this lock already was sent to client and
1409 * this is resent case. For resent case do not take lock
1410 * again, use what is already granted.
1412 LASSERT(lhc != NULL);
1414 rc = mdt_check_resent_lock(info, child, lhc);
1416 GOTO(out_child, result = rc);
1417 } else if (rc > 0) {
1418 mdt_lock_handle_init(lhc);
1419 mdt_lock_reg_init(lhc, LCK_PR);
1421 rc = mdt_object_lock(info, child, lhc,
1422 MDS_INODELOCK_LOOKUP,
1425 repbody->mbo_fid1 = *mdt_object_fid(child);
1426 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1430 result = -MDT_EREMOTE_OPEN;
1431 GOTO(out_child, result);
1432 } else if (mdt_object_exists(child)) {
1433 /* We have to get attr & LOV EA & HSM for this
1435 mdt_prep_ma_buf_from_rep(info, child, ma);
1436 ma->ma_need |= MA_HSM;
1437 result = mdt_attr_get_complex(info, child, ma);
1439 GOTO(out_child, result);
1441 /* Object does not exist. Likely FS corruption. */
1442 CERROR("%s: name '"DNAME"' present, but FID "
1443 DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1444 PNAME(&rr->rr_name), PFID(child_fid));
1445 GOTO(out_child, result = -EIO);
1449 rc = mdt_check_resent_lock(info, child, lhc);
1451 GOTO(out_child, result = rc);
1452 } else if (rc == 0) {
1453 /* the open lock might already be gotten in
1454 * ldlm_handle_enqueue() */
1455 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1456 if (create_flags & MDS_OPEN_LOCK)
1457 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1459 /* get openlock if this isn't replay and client requested it */
1460 if (!req_is_replay(req)) {
1461 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1464 GOTO(out_child_unlock, result = rc);
1465 else if (create_flags & MDS_OPEN_LOCK)
1466 mdt_set_disposition(info, ldlm_rep,
1470 /* Try to open it now. */
1471 rc = mdt_finish_open(info, parent, child, create_flags,
1475 /* openlock will be released if mdt_finish_open failed */
1476 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1478 if (created && create_flags & MDS_OPEN_VOLATILE) {
1479 CERROR("%s: cannot open volatile file "DFID", orphan "
1480 "file will be left in PENDING directory until "
1481 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1482 PFID(mdt_object_fid(child)), rc);
1483 GOTO(out_child_unlock, result);
1489 rc = mdo_unlink(info->mti_env,
1490 mdt_object_child(parent),
1491 mdt_object_child(child),
1493 &info->mti_attr, 0);
1495 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1496 mdt_obd_name(info->mti_mdt),
1497 PFID(mdt_object_fid(child)), rc);
1498 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1504 mdt_object_open_unlock(info, child, lhc, ibits, result);
1506 mdt_object_put(info->mti_env, child);
1508 mdt_object_unlock_put(info, parent, lh, result || !created);
1511 lustre_msg_set_transno(req->rq_repmsg, 0);
1516 * Create an orphan object use local root.
1518 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1519 struct mdt_device *mdt,
1520 const struct lu_fid *fid,
1521 struct md_attr *attr, fmode_t fmode)
1523 const struct lu_env *env = info->mti_env;
1524 struct md_op_spec *spec = &info->mti_spec;
1525 struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1526 struct mdt_object *obj = NULL;
1527 struct mdt_object *local_root;
1528 static const struct lu_name lname = {
1529 .ln_name = "i_am_nobody",
1530 .ln_namelen = sizeof("i_am_nobody") - 1,
1532 struct lu_ucred *uc;
1533 cfs_cap_t uc_cap_save;
1537 rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1539 RETURN(ERR_PTR(rc));
1541 local_root = mdt_object_find(env, mdt, local_root_fid);
1542 if (IS_ERR(local_root))
1545 obj = mdt_object_new(env, mdt, fid);
1547 GOTO(out, rc = PTR_ERR(obj));
1549 spec->sp_cr_lookup = 0;
1550 spec->sp_feat = &dt_directory_features;
1551 spec->sp_cr_mode = MDL_MINMODE; /* no lock */
1552 spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1553 if (attr->ma_valid & MA_LOV) {
1554 spec->u.sp_ea.eadata = attr->ma_lmm;
1555 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1556 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1558 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1562 uc_cap_save = uc->uc_cap;
1563 uc->uc_cap |= 1 << CFS_CAP_DAC_OVERRIDE;
1564 rc = mdo_create(env, mdt_object_child(local_root), &lname,
1565 mdt_object_child(obj), spec, attr);
1566 uc->uc_cap = uc_cap_save;
1568 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1569 mdt_obd_name(mdt), PFID(fid), rc);
1573 rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
1575 CERROR("%s: cannot open volatile file "DFID", orphan "
1576 "file will be left in PENDING directory until "
1577 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1584 mdt_object_put(env, obj);
1587 mdt_object_put(env, local_root);
1591 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1594 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1595 struct close_data *data;
1596 struct ldlm_lock *lease;
1597 struct mdt_object *orphan;
1598 struct md_attr *orp_ma;
1605 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
1608 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1612 lease = ldlm_handle2lock(&data->cd_handle);
1616 /* try to hold open_sem so that nobody else can open the file */
1617 if (!down_write_trylock(&o->mot_open_sem)) {
1618 ldlm_lock_cancel(lease);
1619 GOTO(out_reprocess, rc = -EBUSY);
1622 /* Check if the lease open lease has already canceled */
1623 lock_res_and_lock(lease);
1624 lease_broken = ldlm_is_cancel(lease);
1625 unlock_res_and_lock(lease);
1627 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
1628 PFID(mdt_object_fid(o)), lease_broken);
1630 /* Cancel server side lease. Client side counterpart should
1631 * have been cancelled. It's okay to cancel it now as we've
1632 * held mot_open_sem. */
1633 ldlm_lock_cancel(lease);
1635 if (lease_broken) /* don't perform release task */
1636 GOTO(out_unlock, rc = -ESTALE);
1638 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
1639 GOTO(out_unlock, rc = -EINVAL);
1641 /* ma_need was set before but it seems fine to change it in order to
1642 * avoid modifying the one from RPC */
1643 ma->ma_need = MA_HSM;
1644 rc = mdt_attr_get_complex(info, o, ma);
1646 GOTO(out_unlock, rc);
1648 if (!mdt_hsm_release_allow(ma))
1649 GOTO(out_unlock, rc = -EPERM);
1651 /* already released? */
1652 if (ma->ma_hsm.mh_flags & HS_RELEASED)
1653 GOTO(out_unlock, rc = 0);
1655 /* Compare on-disk and packed data_version */
1656 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
1657 CDEBUG(D_HSM, DFID" data_version mismatches: packed="LPU64
1658 " and on-disk="LPU64"\n", PFID(mdt_object_fid(o)),
1659 data->cd_data_version, ma->ma_hsm.mh_arch_ver);
1660 GOTO(out_unlock, rc = -EPERM);
1663 ma->ma_valid = MA_INODE;
1664 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
1665 rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
1667 GOTO(out_unlock, rc);
1669 ma->ma_need = MA_INODE | MA_LOV;
1670 rc = mdt_attr_get_complex(info, o, ma);
1672 GOTO(out_unlock, rc);
1674 if (!(ma->ma_valid & MA_LOV)) {
1675 /* Even empty file are released */
1676 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
1677 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1678 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
1679 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
1680 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
1682 /* Magic must be LOV_MAGIC_Vx_DEF otherwise LOD will interpret
1683 * ma_lmm as lov_user_md, then it will be confused by union of
1684 * layout_gen and stripe_offset. */
1685 if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1)
1686 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1687 else if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3)
1688 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
1690 GOTO(out_unlock, rc = -EINVAL);
1693 /* Set file as released */
1694 ma->ma_lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
1696 orp_ma = &info->mti_u.hsm.attr;
1697 orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
1698 orp_ma->ma_attr.la_uid = ma->ma_attr.la_uid;
1699 orp_ma->ma_attr.la_gid = ma->ma_attr.la_gid;
1700 orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
1701 orp_ma->ma_lmm = ma->ma_lmm;
1702 orp_ma->ma_lmm_size = ma->ma_lmm_size;
1703 orp_ma->ma_valid = MA_INODE | MA_LOV;
1704 orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
1706 if (IS_ERR(orphan)) {
1707 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
1708 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
1710 GOTO(out_unlock, rc = PTR_ERR(orphan));
1713 /* Set up HSM attribute for orphan object */
1714 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
1715 buf = &info->mti_buf;
1716 buf->lb_buf = info->mti_xattr_buf;
1717 buf->lb_len = sizeof(struct hsm_attrs);
1718 ma->ma_hsm.mh_flags |= HS_RELEASED;
1719 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
1720 ma->ma_hsm.mh_flags &= ~HS_RELEASED;
1722 mdt_lock_reg_init(lh, LCK_EX);
1723 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
1724 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1726 GOTO(out_close, rc);
1728 rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
1732 /* Swap layout with orphan object */
1733 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
1734 mdt_object_child(orphan),
1735 SWAP_LAYOUTS_MDS_HSM);
1737 /* Release exclusive LL */
1738 mdt_object_unlock(info, o, lh, 1);
1743 /* Close orphan object anyway */
1744 rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
1747 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
1748 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
1749 LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
1751 mdt_object_put(info->mti_env, orphan);
1754 up_write(&o->mot_open_sem);
1756 if (rc == 0) { /* already released */
1757 struct mdt_body *repbody;
1758 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1759 LASSERT(repbody != NULL);
1760 repbody->mbo_valid |= OBD_MD_FLRELEASED;
1764 ldlm_reprocess_all(lease->l_resource);
1765 LDLM_LOCK_PUT(lease);
1773 #define MFD_CLOSED(mode) ((mode) == MDS_FMODE_CLOSED)
1775 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1777 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1780 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1782 struct mdt_object *o = mfd->mfd_object;
1783 struct md_object *next = mdt_object_child(o);
1784 struct md_attr *ma = &info->mti_attr;
1789 mode = mfd->mfd_mode;
1791 if (ma->ma_attr_flags & MDS_HSM_RELEASE) {
1792 rc = mdt_hsm_release(info, o, ma);
1794 CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
1795 mdt_obd_name(info->mti_mdt),
1796 PFID(mdt_object_fid(o)), rc);
1797 /* continue to close even error occurred. */
1801 if (mode & FMODE_WRITE)
1803 else if (mode & MDS_FMODE_EXEC)
1806 /* Update atime on close only. */
1807 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1808 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1809 /* Set the atime only. */
1810 ma->ma_valid = MA_INODE;
1811 ma->ma_attr.la_valid = LA_ATIME;
1812 rc = mo_attr_set(info->mti_env, next, ma);
1815 /* If file data is modified, add the dirty flag. */
1816 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
1817 rc = mdt_add_dirty_flag(info, o, ma);
1819 ma->ma_need |= MA_INODE;
1820 ma->ma_valid &= ~MA_INODE;
1822 if (!MFD_CLOSED(mode))
1823 rc = mo_close(info->mti_env, next, ma, mode);
1825 /* adjust open and lease count */
1826 if (mode & MDS_OPEN_LEASE) {
1827 LASSERT(atomic_read(&o->mot_lease_count) > 0);
1828 atomic_dec(&o->mot_lease_count);
1831 LASSERT(atomic_read(&o->mot_open_count) > 0);
1832 atomic_dec(&o->mot_open_count);
1834 mdt_object_put(info->mti_env, o);
1839 int mdt_close(struct tgt_session_info *tsi)
1841 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1842 struct ptlrpc_request *req = tgt_ses_req(tsi);
1843 struct mdt_export_data *med;
1844 struct mdt_file_data *mfd;
1845 struct mdt_object *o;
1846 struct md_attr *ma = &info->mti_attr;
1847 struct mdt_body *repbody = NULL;
1851 mdt_counter_incr(req, LPROC_MDT_CLOSE);
1852 /* Close may come with the Size-on-MDS update. Unpack it. */
1853 rc = mdt_close_unpack(info);
1855 GOTO(out, rc = err_serious(rc));
1857 /* These fields are no longer used and are left for compatibility.
1858 * size is always zero */
1859 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1861 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1863 rc = req_capsule_server_pack(info->mti_pill);
1864 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1865 mdt_client_compatibility(info);
1867 mdt_fix_reply(info);
1868 mdt_exit_ucred(info);
1869 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
1872 /* Continue to close handle even if we can not pack reply */
1874 repbody = req_capsule_server_get(info->mti_pill,
1876 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1878 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1881 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1882 repbody->mbo_eadatasize = 0;
1883 repbody->mbo_aclsize = 0;
1885 rc = err_serious(rc);
1888 med = &req->rq_export->exp_mdt_data;
1889 spin_lock(&med->med_open_lock);
1890 mfd = mdt_handle2mfd(med, &info->mti_close_handle, req_is_replay(req));
1891 if (mdt_mfd_closed(mfd)) {
1892 spin_unlock(&med->med_open_lock);
1893 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1894 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1895 info->mti_close_handle.cookie);
1896 /** not serious error since bug 3633 */
1899 class_handle_unhash(&mfd->mfd_handle);
1900 list_del_init(&mfd->mfd_list);
1901 spin_unlock(&med->med_open_lock);
1903 /* Do not lose object before last unlink. */
1904 o = mfd->mfd_object;
1905 mdt_object_get(info->mti_env, o);
1906 ret = mdt_mfd_close(info, mfd);
1907 if (repbody != NULL)
1908 rc = mdt_handle_last_unlink(info, o, ma);
1909 mdt_empty_transno(info, rc);
1910 mdt_object_put(info->mti_env, o);
1912 if (repbody != NULL) {
1913 mdt_client_compatibility(info);
1914 rc = mdt_fix_reply(info);
1917 mdt_exit_ucred(info);
1918 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1919 GOTO(out, rc = err_serious(-ENOMEM));
1921 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1922 OBD_FAIL_MDS_CLOSE_NET_REP))
1923 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1925 mdt_thread_info_fini(info);
1926 RETURN(rc ? rc : ret);