1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Target (mdt) open/close file handling
7 * Copyright (C) 2002-2006 Cluster File Systems, Inc.
8 * Author: Huang Hua <huanghua@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/lustre_acl.h>
35 #include <lustre_mds.h>
36 #include "mdt_internal.h"
38 /* we do nothing because we do not have refcount now */
39 static void mdt_mfd_get(void *mfdp)
43 /* Create a new mdt_file_data struct, initialize it,
44 * and insert it to global hash table */
45 struct mdt_file_data *mdt_mfd_new(void)
47 struct mdt_file_data *mfd;
52 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
53 INIT_LIST_HEAD(&mfd->mfd_list);
54 class_handle_hash(&mfd->mfd_handle, mdt_mfd_get);
59 /* Find the mfd pointed to by handle in global hash table. */
60 struct mdt_file_data *mdt_handle2mfd(const struct lustre_handle *handle)
63 LASSERT(handle != NULL);
64 RETURN(class_handle2object(handle->cookie));
68 void mdt_mfd_free(struct mdt_file_data *mfd)
70 LASSERT(list_empty(&mfd->mfd_handle.h_link));
71 LASSERT(list_empty(&mfd->mfd_list));
75 static int mdt_create_data(struct mdt_thread_info *info,
76 struct mdt_object *p, struct mdt_object *o)
78 struct md_op_spec *spec = &info->mti_spec;
79 struct md_attr *ma = &info->mti_attr;
83 if ((spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) ||
84 !(spec->sp_cr_flags & FMODE_WRITE))
87 ma->ma_need = MA_INODE | MA_LOV;
89 rc = mdo_create_data(info->mti_env,
90 p ? mdt_object_child(p) : NULL,
91 mdt_object_child(o), spec, ma);
95 static int mdt_epoch_opened(struct mdt_object *mo)
97 return mo->mot_epochcount;
100 int mdt_sizeonmds_enabled(struct mdt_object *mo)
102 return !mo->mot_ioepoch;
105 /* Re-enable Size-on-MDS. */
106 void mdt_sizeonmds_enable(struct mdt_thread_info *info,
107 struct mdt_object *mo)
109 spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
110 if (info->mti_epoch->ioepoch == mo->mot_ioepoch) {
114 spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
117 /* Open the epoch. Epoch open is allowed if @writecount is not negative.
118 * The epoch and writecount handling is performed under the mdt_ioepoch_lock. */
119 int mdt_epoch_open(struct mdt_thread_info *info, struct mdt_object *o)
121 struct mdt_device *mdt = info->mti_mdt;
126 if (!S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
129 spin_lock(&mdt->mdt_ioepoch_lock);
130 if (mdt_epoch_opened(o)) {
131 /* Epoch continues even if there is no writers yet. */
132 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
133 o->mot_ioepoch, PFID(mdt_object_fid(o)));
135 if (info->mti_replayepoch > mdt->mdt_ioepoch)
136 mdt->mdt_ioepoch = info->mti_replayepoch;
139 o->mot_ioepoch = info->mti_replayepoch ?
140 info->mti_replayepoch : mdt->mdt_ioepoch;
141 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
142 mdt->mdt_ioepoch, PFID(mdt_object_fid(o)));
146 spin_unlock(&mdt->mdt_ioepoch_lock);
148 /* Cancel Size-on-MDS attributes on clients if not truncate.
149 * In the later case, mdt_reint_setattr will do it. */
150 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
151 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
152 mdt_lock_reg_init(lh, LCK_EX);
153 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
156 mdt_object_unlock(info, o, lh, 1);
161 /* Update the on-disk attributes if needed and re-enable Size-on-MDS caching. */
162 static int mdt_sizeonmds_update(struct mdt_thread_info *info,
163 struct mdt_object *o)
167 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
168 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_epochcount);
170 if (info->mti_attr.ma_attr.la_valid & LA_SIZE)
171 /* Do Size-on-MDS attribute update.
172 * Size-on-MDS is re-enabled inside. */
173 RETURN(mdt_attr_set(info, o, 0));
175 mdt_sizeonmds_enable(info, o);
180 * Returns 1 if epoch does not close.
181 * Returns 0 if epoch closes.
182 * Returns -EAGAIN if epoch closes but an Size-on-MDS Update is still needed
183 * from the client. */
184 static int mdt_epoch_close(struct mdt_thread_info *info, struct mdt_object *o)
186 int eviction = (mdt_info_req(info) == NULL ? 1 : 0);
187 struct lu_attr *la = &info->mti_attr.ma_attr;
193 if (!S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
196 spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
198 /* Epoch closes only if client tells about it or eviction occures. */
199 if (eviction || (info->mti_epoch->flags & MF_EPOCH_CLOSE)) {
200 LASSERT(o->mot_epochcount);
203 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
204 o->mot_ioepoch, PFID(mdt_object_fid(o)),
208 achange = (info->mti_epoch->flags & MF_SOM_CHANGE);
211 if (!eviction && !mdt_epoch_opened(o)) {
212 /* Epoch ends. Is an Size-on-MDS update needed? */
213 if (o->mot_flags & MF_SOM_CHANGE) {
214 /* Some previous writer changed the attribute.
215 * Do not believe to the current Size-on-MDS
216 * update, re-ask client. */
218 } else if (!(la->la_valid & LA_SIZE) && achange) {
219 /* Attributes were changed by the last writer
220 * only but no Size-on-MDS update is received.*/
225 if (achange || eviction)
226 o->mot_flags |= MF_SOM_CHANGE;
229 opened = mdt_epoch_opened(o);
230 spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
232 /* XXX: if eviction occured, do nothing yet. */
233 if ((rc == 0) && !opened && !eviction) {
234 /* Epoch ends and wanted Size-on-MDS update is obtained. */
235 rc = mdt_sizeonmds_update(info, o);
240 int mdt_write_get(struct mdt_device *mdt, struct mdt_object *o)
244 spin_lock(&mdt->mdt_ioepoch_lock);
245 if (o->mot_writecount < 0)
249 spin_unlock(&mdt->mdt_ioepoch_lock);
253 static void mdt_write_put(struct mdt_device *mdt, struct mdt_object *o)
256 spin_lock(&mdt->mdt_ioepoch_lock);
258 spin_unlock(&mdt->mdt_ioepoch_lock);
262 static int mdt_write_deny(struct mdt_device *mdt, struct mdt_object *o)
266 spin_lock(&mdt->mdt_ioepoch_lock);
267 if (o->mot_writecount > 0)
271 spin_unlock(&mdt->mdt_ioepoch_lock);
275 static void mdt_write_allow(struct mdt_device *mdt, struct mdt_object *o)
278 spin_lock(&mdt->mdt_ioepoch_lock);
280 spin_unlock(&mdt->mdt_ioepoch_lock);
284 /* there can be no real transaction so prepare the fake one */
285 static void mdt_empty_transno(struct mdt_thread_info* info)
287 struct mdt_device *mdt = info->mti_mdt;
288 struct ptlrpc_request *req = mdt_info_req(info);
290 /* transaction is occured already */
291 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
294 spin_lock(&mdt->mdt_transno_lock);
295 if (info->mti_transno == 0) {
296 info->mti_transno = ++ mdt->mdt_last_transno;
298 /* should be replay */
299 if (info->mti_transno > mdt->mdt_last_transno)
300 mdt->mdt_last_transno = info->mti_transno;
302 spin_unlock(&mdt->mdt_transno_lock);
304 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
306 req->rq_export->exp_obd->obd_last_committed);
308 req->rq_transno = info->mti_transno;
309 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
310 lustre_msg_set_last_xid(req->rq_repmsg, req->rq_xid);
313 static int mdt_mfd_open(struct mdt_thread_info *info,
314 struct mdt_object *p,
315 struct mdt_object *o,
318 struct ldlm_reply *rep)
320 struct ptlrpc_request *req = mdt_info_req(info);
321 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
322 struct mdt_device *mdt = info->mti_mdt;
323 struct md_attr *ma = &info->mti_attr;
324 struct lu_attr *la = &ma->ma_attr;
325 struct mdt_file_data *mfd;
326 struct mdt_body *repbody;
328 int isreg, isdir, islnk;
332 LASSERT(ma->ma_valid & MA_INODE);
334 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
336 isreg = S_ISREG(la->la_mode);
337 isdir = S_ISDIR(la->la_mode);
338 islnk = S_ISLNK(la->la_mode);
339 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
341 if (med->med_rmtclient) {
342 void *buf = req_capsule_server_get(&info->mti_pill, &RMF_ACL);
344 rc = mdt_pack_remote_perm(info, o, buf);
346 repbody->valid &= ~OBD_MD_FLRMTPERM;
347 repbody->aclsize = 0;
349 repbody->valid |= OBD_MD_FLRMTPERM;
350 repbody->aclsize = sizeof(struct mdt_remote_perm);
353 #ifdef CONFIG_FS_POSIX_ACL
354 else if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) {
355 const struct lu_env *env = info->mti_env;
356 struct md_object *next = mdt_object_child(o);
357 struct lu_buf *buf = &info->mti_buf;
359 buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_ACL);
360 buf->lb_len = req_capsule_get_size(&info->mti_pill, &RMF_ACL,
362 if (buf->lb_len > 0) {
363 rc = mo_xattr_get(env, next, buf,
364 XATTR_NAME_ACL_ACCESS);
366 if (rc == -ENODATA) {
367 repbody->aclsize = 0;
368 repbody->valid |= OBD_MD_FLACL;
370 } else if (rc == -EOPNOTSUPP) {
373 CERROR("got acl size: %d\n", rc);
376 repbody->aclsize = rc;
377 repbody->valid |= OBD_MD_FLACL;
384 if (mdt->mdt_opts.mo_mds_capa) {
385 struct lustre_capa *capa;
387 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
389 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
390 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
393 repbody->valid |= OBD_MD_FLMDSCAPA;
395 if (mdt->mdt_opts.mo_oss_capa &&
396 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
397 struct lustre_capa *capa;
399 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA2);
401 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
402 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
405 repbody->valid |= OBD_MD_FLOSSCAPA;
409 * If we are following a symlink, don't open; and do not return open
410 * handle for special nodes as client required.
412 if (islnk || (!isreg && !isdir &&
413 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
414 lustre_msg_set_transno(req->rq_repmsg, 0);
418 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
421 * We need to return the existing object's fid back, so it is done here,
422 * after preparing the reply.
424 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
427 /* This can't be done earlier, we need to return reply body */
429 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
430 /* We are trying to create or write an existing dir. */
433 } else if (flags & MDS_OPEN_DIRECTORY)
436 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
437 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
441 if (isreg && !(ma->ma_valid & MA_LOV)) {
443 * No EA, check whether it is will set regEA and dirEA since in
444 * above attr get, these size might be zero, so reset it, to
445 * retrieve the MD after create obj.
447 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
450 /* in replay case, p == NULL */
451 rc = mdt_create_data(info, p, o);
456 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
457 ma->ma_valid, ma->ma_lmm_size);
459 if (ma->ma_valid & MA_LOV) {
460 LASSERT(ma->ma_lmm_size != 0);
461 repbody->eadatasize = ma->ma_lmm_size;
463 repbody->valid |= OBD_MD_FLDIREA;
465 repbody->valid |= OBD_MD_FLEASIZE;
469 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
470 spin_lock(&med->med_open_lock);
471 list_for_each(t, &med->med_open_head) {
472 mfd = list_entry(t, struct mdt_file_data, mfd_list);
473 if (mfd->mfd_xid == req->rq_xid) {
478 spin_unlock(&med->med_open_lock);
481 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
486 if (flags & FMODE_WRITE) {
487 rc = mdt_write_get(info->mti_mdt, o);
489 mdt_epoch_open(info, o);
490 repbody->ioepoch = o->mot_ioepoch;
492 } else if (flags & MDS_FMODE_EXEC) {
493 rc = mdt_write_deny(info->mti_mdt, o);
498 rc = mo_open(info->mti_env, mdt_object_child(o),
499 created ? flags | MDS_OPEN_CREATED : flags);
506 * Keep a reference on this object for this open, and is
507 * released by mdt_mfd_close().
509 mdt_object_get(info->mti_env, o);
512 mfd->mfd_mode = flags;
514 mfd->mfd_xid = req->rq_xid;
516 spin_lock(&med->med_open_lock);
517 list_add(&mfd->mfd_list, &med->med_open_head);
518 spin_unlock(&med->med_open_lock);
520 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
521 mdt_empty_transno(info);
528 extern void mdt_req_from_mcd(struct ptlrpc_request *req,
529 struct mdt_client_data *mcd);
531 void mdt_reconstruct_open(struct mdt_thread_info *info,
532 struct mdt_lock_handle *lhc)
534 const struct lu_env *env = info->mti_env;
535 struct mdt_device *mdt = info->mti_mdt;
536 struct req_capsule *pill = &info->mti_pill;
537 struct ptlrpc_request *req = mdt_info_req(info);
538 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
539 struct mdt_client_data *mcd = med->med_mcd;
540 struct md_attr *ma = &info->mti_attr;
541 struct mdt_reint_record *rr = &info->mti_rr;
542 __u32 flags = info->mti_spec.sp_cr_flags;
543 struct ldlm_reply *ldlm_rep;
544 struct mdt_object *parent;
545 struct mdt_object *child;
546 struct mdt_body *repbody;
550 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
551 ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
552 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
554 ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
555 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill, &RMF_MDT_MD,
557 ma->ma_need = MA_INODE | MA_LOV;
560 mdt_req_from_mcd(req, med->med_mcd);
561 mdt_set_disposition(info, ldlm_rep, mcd->mcd_last_data);
563 CERROR("This is reconstruct open: disp="LPX64", result=%d\n",
564 ldlm_rep->lock_policy_res1, req->rq_status);
566 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
567 req->rq_status != 0) {
568 /* We did not create successfully, return error to client. */
569 mdt_shrink_reply(info, DLM_REPLY_REC_OFF + 1, 1, 1);
570 GOTO(out, rc = req->rq_status);
573 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
575 * We failed after creation, but we do not know in which step
576 * we failed. So try to check the child object.
578 parent = mdt_object_find(env, mdt, rr->rr_fid1);
579 LASSERT(!IS_ERR(parent));
581 child = mdt_object_find(env, mdt, rr->rr_fid2);
582 LASSERT(!IS_ERR(child));
584 rc = mdt_object_exists(child);
586 struct md_object *next;
587 next = mdt_object_child(child);
588 rc = mo_attr_get(env, next, ma);
590 rc = mdt_mfd_open(info, parent, child,
593 /* the child object was created on remote server */
594 repbody->fid1 = *rr->rr_fid2;
595 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
597 } else if (rc == 0) {
598 /* the child does not exist, we should do regular open */
599 mdt_object_put(env, parent);
600 mdt_object_put(env, child);
601 GOTO(regular_open, 0);
603 mdt_object_put(env, parent);
604 mdt_object_put(env, child);
605 mdt_shrink_reply(info, DLM_REPLY_REC_OFF + 1, 1, 1);
609 /* We did not try to create, so we are a pure open */
610 rc = mdt_reint_open(info, lhc);
616 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
619 static int mdt_open_by_fid(struct mdt_thread_info* info,
620 struct ldlm_reply *rep)
622 const struct lu_env *env = info->mti_env;
623 struct mdt_device *mdt = info->mti_mdt;
624 struct md_device *next = mdt->mdt_child;
625 __u32 flags = info->mti_spec.sp_cr_flags;
626 struct mdt_reint_record *rr = &info->mti_rr;
627 struct md_attr *ma = &info->mti_attr;
628 struct mdt_object *o;
632 LASSERT(info->mti_spec.u.sp_ea.no_lov_create);
633 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
635 RETURN(rc = PTR_ERR(o));
638 * XXX: capability in replay open request might have expired, disable
639 * capability check for this kind of request. Security hole?
641 if (mdt->mdt_opts.mo_mds_capa)
642 mdt->mdt_child->md_ops->mdo_init_capa_ctxt(env, next, 0, 0,
645 rc = mdt_object_exists(o);
647 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
651 rc = mo_attr_get(env, mdt_object_child(o), ma);
653 rc = mdt_mfd_open(info, NULL, o, flags, 0, rep);
654 } else if (rc == 0) {
657 /* the child object was created on remote server */
658 struct mdt_body *repbody;
659 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
660 repbody->fid1 = *rr->rr_fid2;
661 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
665 if (mdt->mdt_opts.mo_mds_capa)
666 mdt->mdt_child->md_ops->mdo_init_capa_ctxt(env, next,
667 mdt->mdt_opts.mo_mds_capa,
668 mdt->mdt_capa_timeout,
672 mdt_object_put(info->mti_env, o);
676 int mdt_pin(struct mdt_thread_info* info)
679 RETURN(err_serious(-EOPNOTSUPP));
682 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
683 static int mdt_cross_open(struct mdt_thread_info* info,
684 const struct lu_fid *fid,
685 struct ldlm_reply *rep, __u32 flags)
687 struct md_attr *ma = &info->mti_attr;
688 struct mdt_object *o;
692 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
694 RETURN(rc = PTR_ERR(o));
696 rc = mdt_object_exists(o);
698 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
699 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
701 rc = mdt_mfd_open(info, NULL, o, flags, 0, rep);
702 } else if (rc == 0) {
704 * XXX: Something wrong here, lookup was positive but there is
707 CERROR("Cross-ref object doesn't exist!\n");
710 /* XXX: Something wrong here, the object is on another MDS! */
711 CERROR("The object isn't on this server! FLD error?\n");
714 mdt_object_put(info->mti_env, o);
718 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
720 struct mdt_device *mdt = info->mti_mdt;
721 struct ptlrpc_request *req = mdt_info_req(info);
722 struct mdt_object *parent;
723 struct mdt_object *child;
724 struct mdt_lock_handle *lh;
725 struct ldlm_reply *ldlm_rep;
726 struct mdt_body *repbody;
727 struct lu_fid *child_fid = &info->mti_tmp_fid1;
728 struct md_attr *ma = &info->mti_attr;
729 struct lu_attr *la = &ma->ma_attr;
730 __u32 create_flags = info->mti_spec.sp_cr_flags;
731 struct mdt_reint_record *rr = &info->mti_rr;
732 struct lu_name *lname;
737 mdt_lprocfs_time_start(info->mti_mdt, &info->mti_time,
738 LPROC_MDT_REINT_OPEN);
740 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
741 (obd_timeout + 1) / 4);
743 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
745 ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
746 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill, &RMF_MDT_MD,
748 ma->ma_need = MA_INODE | MA_LOV;
751 LASSERT(info->mti_pill.rc_fmt == &RQF_LDLM_INTENT_OPEN);
752 ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
754 /* TODO: JOIN file */
755 if (create_flags & MDS_OPEN_JOIN_FILE) {
756 CERROR("JOIN file will be supported soon\n");
757 GOTO(out, result = err_serious(-EOPNOTSUPP));
760 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
761 "cr_flag=0%o mode=0%06o msg_flag=0x%x\n",
762 PFID(rr->rr_fid1), rr->rr_name,
763 PFID(rr->rr_fid2), create_flags,
764 la->la_mode, lustre_msg_get_flags(req->rq_reqmsg));
766 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
767 /* This is a replay request. */
768 result = mdt_open_by_fid(info, ldlm_rep);
770 if (result != -ENOENT)
774 * We didn't find the correct object, so we need to re-create it
775 * via a regular replay.
777 if (!(create_flags & MDS_OPEN_CREAT)) {
778 DEBUG_REQ(D_ERROR, req,"OPEN & CREAT not in open replay.");
779 GOTO(out, result = -EFAULT);
781 CDEBUG(D_INFO, "Open replay did find object, continue as "
785 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
786 GOTO(out, result = err_serious(-ENOMEM));
788 mdt_set_disposition(info, ldlm_rep,
789 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
791 if (info->mti_cross_ref) {
792 /* This is cross-ref open */
793 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
794 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
799 lh = &info->mti_lh[MDT_LH_PARENT];
800 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
801 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
803 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
804 MDS_INODELOCK_UPDATE);
806 GOTO(out, result = PTR_ERR(parent));
810 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
812 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
813 lname, child_fid, &info->mti_spec);
814 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
815 "looking for "DFID"/%s, result fid="DFID"\n",
816 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
818 if (result != 0 && result != -ENOENT && result != -ESTALE)
819 GOTO(out_parent, result);
821 if (result == -ENOENT || result == -ESTALE) {
822 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
823 if (result == -ESTALE) {
825 * -ESTALE means the parent is a dead(unlinked) dir, so
826 * it should return -ENOENT to in accordance with the
827 * original mds implementaion.
829 GOTO(out_parent, result = -ENOENT);
831 if (!(create_flags & MDS_OPEN_CREAT))
832 GOTO(out_parent, result);
833 *child_fid = *info->mti_rr.rr_fid2;
834 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
838 * Check for O_EXCL is moved to the mdt_mfd_open(), we need to
839 * return FID back in that case.
841 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
844 child = mdt_object_find(info->mti_env, mdt, child_fid);
846 GOTO(out_parent, result = PTR_ERR(child));
848 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
849 if (result == -ENOENT) {
850 /* Not found and with MDS_OPEN_CREAT: let's create it. */
851 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
853 /* Let lower layers know what is lock mode on directory. */
854 info->mti_spec.sp_cr_mode =
855 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
858 * Do not perform lookup sanity check. We know that name does
861 info->mti_spec.sp_cr_lookup = 0;
863 result = mdo_create(info->mti_env,
864 mdt_object_child(parent),
866 mdt_object_child(child),
869 if (result == -ERESTART) {
870 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
871 GOTO(out_child, result);
874 GOTO(out_child, result);
878 /* We have to get attr & lov ea for this object */
879 result = mo_attr_get(info->mti_env, mdt_object_child(child),
882 * The object is on remote node, return its FID for remote open.
884 if (result == -EREMOTE) {
888 * Check if this lock already was sent to client and
889 * this is resent case. For resent case do not take lock
890 * again, use what is already granted.
892 LASSERT(lhc != NULL);
894 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
895 struct ldlm_lock *lock;
897 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
900 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
902 CERROR("Invalid lock handle "LPX64"\n",
903 lhc->mlh_reg_lh.cookie);
906 LASSERT(fid_res_name_eq(mdt_object_fid(child),
907 &lock->l_resource->lr_name));
911 mdt_lock_handle_init(lhc);
912 mdt_lock_reg_init(lhc, LCK_PR);
914 rc = mdt_object_lock(info, child, lhc,
915 MDS_INODELOCK_LOOKUP,
918 repbody->fid1 = *mdt_object_fid(child);
919 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
922 GOTO(out_child, result);
926 /* Try to open it now. */
927 result = mdt_mfd_open(info, parent, child, create_flags,
930 if (result != 0 && created) {
934 ma->ma_cookie_size = 0;
935 rc2 = mdo_unlink(info->mti_env,
936 mdt_object_child(parent),
937 mdt_object_child(child),
941 CERROR("Error in cleanup of open\n");
945 mdt_object_put(info->mti_env, child);
947 mdt_object_unlock_put(info, parent, lh, result);
949 mdt_shrink_reply(info, DLM_REPLY_REC_OFF + 1, 1, 1);
951 lustre_msg_set_transno(req->rq_repmsg, 0);
952 mdt_lprocfs_time_end(info->mti_mdt, &info->mti_time,
953 LPROC_MDT_REINT_OPEN);
957 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
958 FMODE_EPOCHLCK)) == FMODE_CLOSED)
960 static int mdt_mfd_closed(struct mdt_file_data *mfd)
962 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
965 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
967 struct mdt_object *o = mfd->mfd_object;
968 struct md_object *next = mdt_object_child(o);
969 struct md_attr *ma = &info->mti_attr;
974 mode = mfd->mfd_mode;
975 mfd->mfd_mode = FMODE_CLOSED;
977 if ((mode & FMODE_WRITE) || (mode & FMODE_EPOCHLCK)) {
978 mdt_write_put(info->mti_mdt, o);
979 ret = mdt_epoch_close(info, o);
980 } else if (mode & MDS_FMODE_EXEC) {
981 mdt_write_allow(info->mti_mdt, o);
982 } else if (mode & FMODE_EPOCH) {
983 ret = mdt_epoch_close(info, o);
986 ma->ma_need |= MA_INODE;
989 if (!MFD_CLOSED(mode))
990 rc = mo_close(info->mti_env, next, ma);
991 else if (ret == -EAGAIN)
992 rc = mo_attr_get(info->mti_env, next, ma);
994 /* If the object is unlinked, do not try to re-enable SIZEONMDS */
995 if ((ret == -EAGAIN) && (ma->ma_valid & MA_INODE) &&
996 (ma->ma_attr.la_nlink == 0))
1001 if ((ret == -EAGAIN) || (ret == 1)) {
1002 struct mdt_export_data *med;
1003 /* The epoch has not closed or Size-on-MDS update is needed.
1004 * Put mfd back into the list. */
1005 mfd->mfd_mode = (ret == 1 ? FMODE_EPOCH : FMODE_SOM);
1007 LASSERT(mdt_info_req(info));
1008 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1009 spin_lock(&med->med_open_lock);
1010 list_add(&mfd->mfd_list, &med->med_open_head);
1011 class_handle_hash_back(&mfd->mfd_handle);
1012 spin_unlock(&med->med_open_lock);
1016 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1017 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1021 mdt_object_put(info->mti_env, o);
1024 RETURN(rc ? rc : ret);
1027 int mdt_close(struct mdt_thread_info *info)
1029 struct mdt_export_data *med;
1030 struct mdt_file_data *mfd;
1031 struct mdt_object *o;
1032 struct md_attr *ma = &info->mti_attr;
1033 struct mdt_body *repbody = NULL;
1037 /* Close may come with the Size-on-MDS update. Unpack it. */
1038 rc = mdt_close_unpack(info);
1040 RETURN(err_serious(rc));
1042 LASSERT(info->mti_epoch);
1044 req_capsule_set_size(&info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1045 info->mti_mdt->mdt_max_mdsize);
1046 req_capsule_set_size(&info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1047 info->mti_mdt->mdt_max_cookiesize);
1048 rc = req_capsule_pack(&info->mti_pill);
1049 /* Continue to close handle even if we can not pack reply */
1051 repbody = req_capsule_server_get(&info->mti_pill,
1053 ma->ma_lmm = req_capsule_server_get(&info->mti_pill,
1055 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
1058 ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
1060 ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
1063 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1065 repbody->eadatasize = 0;
1066 repbody->aclsize = 0;
1068 rc = err_serious(rc);
1070 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1072 spin_lock(&med->med_open_lock);
1073 mfd = mdt_handle2mfd(&(info->mti_epoch->handle));
1074 if (mdt_mfd_closed(mfd)) {
1075 spin_unlock(&med->med_open_lock);
1076 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1077 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1078 info->mti_epoch->handle.cookie);
1079 rc = err_serious(-ESTALE);
1081 class_handle_unhash(&mfd->mfd_handle);
1082 list_del_init(&mfd->mfd_list);
1083 spin_unlock(&med->med_open_lock);
1085 /* Do not lose object before last unlink. */
1086 o = mfd->mfd_object;
1087 mdt_object_get(info->mti_env, o);
1088 ret = mdt_mfd_close(info, mfd);
1089 if (repbody != NULL)
1090 rc = mdt_handle_last_unlink(info, o, ma);
1091 mdt_empty_transno(info);
1092 mdt_object_put(info->mti_env, o);
1094 if (repbody != NULL)
1095 mdt_shrink_reply(info, REPLY_REC_OFF + 1, 0, 0);
1097 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1098 RETURN(err_serious(-ENOMEM));
1100 RETURN(rc ? rc : ret);
1103 int mdt_done_writing(struct mdt_thread_info *info)
1105 struct mdt_body *repbody = NULL;
1106 struct mdt_export_data *med;
1107 struct mdt_file_data *mfd;
1111 rc = req_capsule_pack(&info->mti_pill);
1113 RETURN(err_serious(rc));
1115 repbody = req_capsule_server_get(&info->mti_pill,
1117 repbody->eadatasize = 0;
1118 repbody->aclsize = 0;
1120 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1121 rc = mdt_close_unpack(info);
1123 RETURN(err_serious(rc));
1125 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1126 spin_lock(&med->med_open_lock);
1127 mfd = mdt_handle2mfd(&(info->mti_epoch->handle));
1129 spin_unlock(&med->med_open_lock);
1130 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1131 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1132 info->mti_epoch->handle.cookie);
1135 LASSERT((mfd->mfd_mode == FMODE_EPOCH) ||
1136 (mfd->mfd_mode == FMODE_EPOCHLCK));
1137 class_handle_unhash(&mfd->mfd_handle);
1138 list_del_init(&mfd->mfd_list);
1139 spin_unlock(&med->med_open_lock);
1141 /* Set EPOCH CLOSE flag if not set by client. */
1142 info->mti_epoch->flags |= MF_EPOCH_CLOSE;
1143 info->mti_attr.ma_valid = 0;
1144 rc = mdt_mfd_close(info, mfd);
1145 mdt_empty_transno(info);