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);
60 * Find the mfd pointed to by handle in global hash table.
61 * In case of replay the handle is obsoleted
62 * but mfd can be found in mfd list by that handle
64 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
65 const struct lustre_handle *handle)
67 struct ptlrpc_request *req = mdt_info_req(info);
68 struct mdt_file_data *mfd;
71 LASSERT(handle != NULL);
72 mfd = class_handle2object(handle->cookie);
73 /* during dw/setattr replay the mfd can be found by old handle */
75 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
76 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
77 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
78 if (mfd->mfd_old_handle.cookie == handle->cookie)
87 void mdt_mfd_free(struct mdt_file_data *mfd)
89 LASSERT(list_empty(&mfd->mfd_list));
90 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
93 static int mdt_create_data(struct mdt_thread_info *info,
94 struct mdt_object *p, struct mdt_object *o)
96 struct md_op_spec *spec = &info->mti_spec;
97 struct md_attr *ma = &info->mti_attr;
101 if ((spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) ||
102 !(spec->sp_cr_flags & FMODE_WRITE))
105 ma->ma_need = MA_INODE | MA_LOV;
107 rc = mdo_create_data(info->mti_env,
108 p ? mdt_object_child(p) : NULL,
109 mdt_object_child(o), spec, ma);
113 static int mdt_epoch_opened(struct mdt_object *mo)
115 return mo->mot_epochcount;
118 int mdt_sizeonmds_enabled(struct mdt_object *mo)
120 return !mo->mot_ioepoch;
123 /* Re-enable Size-on-MDS. */
124 void mdt_sizeonmds_enable(struct mdt_thread_info *info,
125 struct mdt_object *mo)
127 spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
128 if (info->mti_epoch->ioepoch == mo->mot_ioepoch) {
129 LASSERT(!mdt_epoch_opened(mo));
133 spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
136 /* Open the epoch. Epoch open is allowed if @writecount is not negative.
137 * The epoch and writecount handling is performed under the mdt_ioepoch_lock. */
138 int mdt_epoch_open(struct mdt_thread_info *info, struct mdt_object *o)
140 struct mdt_device *mdt = info->mti_mdt;
145 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
146 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
149 spin_lock(&mdt->mdt_ioepoch_lock);
150 if (mdt_epoch_opened(o)) {
151 /* Epoch continues even if there is no writers yet. */
152 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
153 o->mot_ioepoch, PFID(mdt_object_fid(o)));
155 if (info->mti_replayepoch > mdt->mdt_ioepoch)
156 mdt->mdt_ioepoch = info->mti_replayepoch;
159 o->mot_ioepoch = info->mti_replayepoch ?
160 info->mti_replayepoch : mdt->mdt_ioepoch;
161 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
162 mdt->mdt_ioepoch, PFID(mdt_object_fid(o)));
166 spin_unlock(&mdt->mdt_ioepoch_lock);
168 /* Cancel Size-on-MDS attributes on clients if not truncate.
169 * In the later case, mdt_reint_setattr will do it. */
170 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
171 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
172 mdt_lock_reg_init(lh, LCK_EX);
173 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
176 mdt_object_unlock(info, o, lh, 1);
181 /* Update the on-disk attributes if needed and re-enable Size-on-MDS caching. */
182 static int mdt_sizeonmds_update(struct mdt_thread_info *info,
183 struct mdt_object *o)
187 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
188 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_epochcount);
190 if (info->mti_attr.ma_attr.la_valid & LA_SIZE) {
191 /* Do Size-on-MDS attribute update.
192 * Size-on-MDS is re-enabled inside. */
193 /* XXX: since we have opened the file, it is unnecessary
194 * to check permission when close it. Between the "open"
195 * and "close", maybe someone has changed the file mode
196 * or flags, or the file created mode do not permit wirte,
197 * and so on. Just set MDS_PERM_BYPASS for all the cases. */
198 info->mti_attr.ma_attr_flags |= MDS_PERM_BYPASS;
199 info->mti_attr.ma_attr.la_valid &= LA_SIZE | LA_BLOCKS |
200 LA_ATIME | LA_MTIME | LA_CTIME;
201 RETURN(mdt_attr_set(info, o, 0));
203 mdt_sizeonmds_enable(info, o);
208 * Returns 1 if epoch does not close.
209 * Returns 0 if epoch closes.
210 * Returns -EAGAIN if epoch closes but an Size-on-MDS Update is still needed
211 * from the client. */
212 static int mdt_epoch_close(struct mdt_thread_info *info, struct mdt_object *o)
214 int eviction = (mdt_info_req(info) == NULL ? 1 : 0);
215 struct lu_attr *la = &info->mti_attr.ma_attr;
221 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
222 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
225 spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
227 /* Epoch closes only if client tells about it or eviction occures. */
228 if (eviction || (info->mti_epoch->flags & MF_EPOCH_CLOSE)) {
229 LASSERT(o->mot_epochcount);
232 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
233 o->mot_ioepoch, PFID(mdt_object_fid(o)),
237 achange = (info->mti_epoch->flags & MF_SOM_CHANGE);
240 if (!eviction && !mdt_epoch_opened(o)) {
241 /* Epoch ends. Is an Size-on-MDS update needed? */
242 if (o->mot_flags & MF_SOM_CHANGE) {
243 /* Some previous writer changed the attribute.
244 * Do not believe to the current Size-on-MDS
245 * update, re-ask client. */
247 } else if (!(la->la_valid & LA_SIZE) && achange) {
248 /* Attributes were changed by the last writer
249 * only but no Size-on-MDS update is received.*/
254 if (achange || eviction)
255 o->mot_flags |= MF_SOM_CHANGE;
258 opened = mdt_epoch_opened(o);
259 spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
261 /* If eviction occurred, do nothing. */
262 if ((rc == 0) && !opened && !eviction) {
263 /* Epoch ends and wanted Size-on-MDS update is obtained. */
264 rc = mdt_sizeonmds_update(info, o);
265 /* Avoid the following setattrs of these attributes, e.g.
266 * for atime update. */
267 info->mti_attr.ma_valid = 0;
272 int mdt_write_read(struct mdt_device *mdt, struct mdt_object *o)
276 spin_lock(&mdt->mdt_ioepoch_lock);
277 rc = o->mot_writecount;
278 spin_unlock(&mdt->mdt_ioepoch_lock);
282 int mdt_write_get(struct mdt_device *mdt, struct mdt_object *o)
286 spin_lock(&mdt->mdt_ioepoch_lock);
287 if (o->mot_writecount < 0)
291 spin_unlock(&mdt->mdt_ioepoch_lock);
295 static void mdt_write_put(struct mdt_device *mdt, struct mdt_object *o)
298 spin_lock(&mdt->mdt_ioepoch_lock);
300 spin_unlock(&mdt->mdt_ioepoch_lock);
304 static int mdt_write_deny(struct mdt_device *mdt, struct mdt_object *o)
308 spin_lock(&mdt->mdt_ioepoch_lock);
309 if (o->mot_writecount > 0)
313 spin_unlock(&mdt->mdt_ioepoch_lock);
317 static void mdt_write_allow(struct mdt_device *mdt, struct mdt_object *o)
320 spin_lock(&mdt->mdt_ioepoch_lock);
322 spin_unlock(&mdt->mdt_ioepoch_lock);
326 /* there can be no real transaction so prepare the fake one */
327 static void mdt_empty_transno(struct mdt_thread_info* info)
329 struct mdt_device *mdt = info->mti_mdt;
330 struct ptlrpc_request *req = mdt_info_req(info);
333 /* transaction is occured already */
334 if (lustre_msg_get_transno(req->rq_repmsg) != 0) {
339 spin_lock(&mdt->mdt_transno_lock);
340 if (info->mti_transno == 0) {
341 info->mti_transno = ++ mdt->mdt_last_transno;
343 /* should be replay */
344 if (info->mti_transno > mdt->mdt_last_transno)
345 mdt->mdt_last_transno = info->mti_transno;
347 spin_unlock(&mdt->mdt_transno_lock);
349 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
351 req->rq_export->exp_obd->obd_last_committed);
353 req->rq_transno = info->mti_transno;
354 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
355 lustre_msg_set_last_xid(req->rq_repmsg, req->rq_xid);
359 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
361 LASSERT(mfd != NULL);
363 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
364 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
366 mfd->mfd_mode = mode;
369 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
370 struct mdt_object *o, int flags, int created)
372 struct ptlrpc_request *req = mdt_info_req(info);
373 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
374 struct mdt_file_data *mfd;
375 struct md_attr *ma = &info->mti_attr;
376 struct lu_attr *la = &ma->ma_attr;
377 struct mdt_body *repbody;
378 int rc = 0, isdir, isreg;
381 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
383 isreg = S_ISREG(la->la_mode);
384 isdir = S_ISDIR(la->la_mode);
385 if ((isreg && !(ma->ma_valid & MA_LOV))) {
387 * No EA, check whether it is will set regEA and dirEA since in
388 * above attr get, these size might be zero, so reset it, to
389 * retrieve the MD after create obj.
391 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
394 /* in replay case, p == NULL */
395 rc = mdt_create_data(info, p, o);
400 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
401 ma->ma_valid, ma->ma_lmm_size);
403 if (ma->ma_valid & MA_LOV) {
404 LASSERT(ma->ma_lmm_size != 0);
405 repbody->eadatasize = ma->ma_lmm_size;
407 repbody->valid |= OBD_MD_FLDIREA;
409 repbody->valid |= OBD_MD_FLEASIZE;
412 if (flags & FMODE_WRITE) {
413 rc = mdt_write_get(info->mti_mdt, o);
415 mdt_epoch_open(info, o);
416 repbody->ioepoch = o->mot_ioepoch;
418 } else if (flags & MDS_FMODE_EXEC) {
419 rc = mdt_write_deny(info->mti_mdt, o);
424 rc = mo_open(info->mti_env, mdt_object_child(o),
425 created ? flags | MDS_OPEN_CREATED : flags);
432 * Keep a reference on this object for this open, and is
433 * released by mdt_mfd_close().
435 mdt_object_get(info->mti_env, o);
438 * @flags is always not zero. At least it should be FMODE_READ,
439 * FMODE_WRITE or FMODE_EXEC.
444 mdt_mfd_set_mode(mfd, flags);
447 mfd->mfd_xid = req->rq_xid;
450 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
451 struct mdt_file_data *old_mfd;
452 /* Check wheather old cookie already exist in
453 * the list, becasue when do recovery, client
454 * might be disconnected from server, and
455 * restart replay, so there maybe some orphan
456 * mfd here, we should remove them */
457 LASSERT(info->mti_rr.rr_handle != NULL);
458 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
460 CDEBUG(D_HA, "del orph mfd %p cookie" LPX64"\n",
461 mfd, info->mti_rr.rr_handle->cookie);
462 spin_lock(&med->med_open_lock);
463 class_handle_unhash(&old_mfd->mfd_handle);
464 list_del_init(&old_mfd->mfd_list);
465 spin_unlock(&med->med_open_lock);
466 mdt_mfd_free(old_mfd);
468 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
469 info->mti_rr.rr_handle->cookie);
470 mfd->mfd_old_handle.cookie =
471 info->mti_rr.rr_handle->cookie;
473 spin_lock(&med->med_open_lock);
474 list_add(&mfd->mfd_list, &med->med_open_head);
475 spin_unlock(&med->med_open_lock);
477 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
478 mdt_empty_transno(info);
486 static int mdt_finish_open(struct mdt_thread_info *info,
487 struct mdt_object *p, struct mdt_object *o,
488 int flags, int created, struct ldlm_reply *rep)
490 struct ptlrpc_request *req = mdt_info_req(info);
491 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
492 struct mdt_device *mdt = info->mti_mdt;
493 struct md_attr *ma = &info->mti_attr;
494 struct lu_attr *la = &ma->ma_attr;
495 struct mdt_file_data *mfd;
496 struct mdt_body *repbody;
498 int isreg, isdir, islnk;
502 LASSERT(ma->ma_valid & MA_INODE);
504 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
506 isreg = S_ISREG(la->la_mode);
507 isdir = S_ISDIR(la->la_mode);
508 islnk = S_ISLNK(la->la_mode);
509 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
511 if (med->med_rmtclient) {
512 void *buf = req_capsule_server_get(&info->mti_pill, &RMF_ACL);
514 rc = mdt_pack_remote_perm(info, o, buf);
516 repbody->valid &= ~OBD_MD_FLRMTPERM;
517 repbody->aclsize = 0;
519 repbody->valid |= OBD_MD_FLRMTPERM;
520 repbody->aclsize = sizeof(struct mdt_remote_perm);
523 #ifdef CONFIG_FS_POSIX_ACL
524 else if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) {
525 const struct lu_env *env = info->mti_env;
526 struct md_object *next = mdt_object_child(o);
527 struct lu_buf *buf = &info->mti_buf;
529 buf->lb_buf = req_capsule_server_get(&info->mti_pill, &RMF_ACL);
530 buf->lb_len = req_capsule_get_size(&info->mti_pill, &RMF_ACL,
532 if (buf->lb_len > 0) {
533 rc = mo_xattr_get(env, next, buf,
534 XATTR_NAME_ACL_ACCESS);
536 if (rc == -ENODATA) {
537 repbody->aclsize = 0;
538 repbody->valid |= OBD_MD_FLACL;
540 } else if (rc == -EOPNOTSUPP) {
543 CERROR("got acl size: %d\n", rc);
546 repbody->aclsize = rc;
547 repbody->valid |= OBD_MD_FLACL;
554 if (mdt->mdt_opts.mo_mds_capa) {
555 struct lustre_capa *capa;
557 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
559 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
561 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
564 repbody->valid |= OBD_MD_FLMDSCAPA;
566 if (mdt->mdt_opts.mo_oss_capa &&
567 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
568 struct lustre_capa *capa;
569 struct md_ucred *ucred = mdt_ucred(info);
571 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA2);
573 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
574 capa->lc_uid = ucred->mu_o_fsuid;
575 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
578 repbody->valid |= OBD_MD_FLOSSCAPA;
582 * If we are following a symlink, don't open; and do not return open
583 * handle for special nodes as client required.
585 if (islnk || (!isreg && !isdir &&
586 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
587 lustre_msg_set_transno(req->rq_repmsg, 0);
591 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
594 * We need to return the existing object's fid back, so it is done here,
595 * after preparing the reply.
597 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
600 /* This can't be done earlier, we need to return reply body */
602 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
603 /* We are trying to create or write an existing dir. */
606 } else if (flags & MDS_OPEN_DIRECTORY)
609 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
610 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
615 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
616 spin_lock(&med->med_open_lock);
617 list_for_each(t, &med->med_open_head) {
618 mfd = list_entry(t, struct mdt_file_data, mfd_list);
619 if (mfd->mfd_xid == req->rq_xid) {
624 spin_unlock(&med->med_open_lock);
627 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
628 /*set repbody->ea_size for resent case*/
629 if (ma->ma_valid & MA_LOV) {
630 LASSERT(ma->ma_lmm_size != 0);
631 repbody->eadatasize = ma->ma_lmm_size;
633 repbody->valid |= OBD_MD_FLDIREA;
635 repbody->valid |= OBD_MD_FLEASIZE;
641 rc = mdt_mfd_open(info, p, o, flags, created);
645 extern void mdt_req_from_mcd(struct ptlrpc_request *req,
646 struct mdt_client_data *mcd);
648 void mdt_reconstruct_open(struct mdt_thread_info *info,
649 struct mdt_lock_handle *lhc)
651 const struct lu_env *env = info->mti_env;
652 struct mdt_device *mdt = info->mti_mdt;
653 struct req_capsule *pill = &info->mti_pill;
654 struct ptlrpc_request *req = mdt_info_req(info);
655 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
656 struct mdt_client_data *mcd = med->med_mcd;
657 struct md_attr *ma = &info->mti_attr;
658 struct mdt_reint_record *rr = &info->mti_rr;
659 __u32 flags = info->mti_spec.sp_cr_flags;
660 struct ldlm_reply *ldlm_rep;
661 struct mdt_object *parent;
662 struct mdt_object *child;
663 struct mdt_body *repbody;
667 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
668 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
669 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
671 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
672 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
674 ma->ma_need = MA_INODE | MA_LOV;
677 mdt_req_from_mcd(req, med->med_mcd);
678 mdt_set_disposition(info, ldlm_rep, mcd->mcd_last_data);
680 CERROR("This is reconstruct open: disp="LPX64", result=%d\n",
681 ldlm_rep->lock_policy_res1, req->rq_status);
683 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
684 req->rq_status != 0) {
685 /* We did not create successfully, return error to client. */
686 mdt_shrink_reply(info);
687 GOTO(out, rc = req->rq_status);
690 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
692 * We failed after creation, but we do not know in which step
693 * we failed. So try to check the child object.
695 parent = mdt_object_find(env, mdt, rr->rr_fid1);
696 LASSERT(!IS_ERR(parent));
698 child = mdt_object_find(env, mdt, rr->rr_fid2);
699 LASSERT(!IS_ERR(child));
701 rc = mdt_object_exists(child);
703 struct md_object *next;
705 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
706 next = mdt_object_child(child);
707 rc = mo_attr_get(env, next, ma);
709 rc = mdt_finish_open(info, parent, child,
712 /* the child object was created on remote server */
713 repbody->fid1 = *rr->rr_fid2;
714 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
716 } else if (rc == 0) {
717 /* the child does not exist, we should do regular open */
718 mdt_object_put(env, parent);
719 mdt_object_put(env, child);
720 GOTO(regular_open, 0);
722 mdt_object_put(env, parent);
723 mdt_object_put(env, child);
724 mdt_shrink_reply(info);
728 /* We did not try to create, so we are a pure open */
729 rc = mdt_reint_open(info, lhc);
735 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
736 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
739 static int mdt_open_by_fid(struct mdt_thread_info* info,
740 struct ldlm_reply *rep)
742 const struct lu_env *env = info->mti_env;
743 __u32 flags = info->mti_spec.sp_cr_flags;
744 struct mdt_reint_record *rr = &info->mti_rr;
745 struct md_attr *ma = &info->mti_attr;
746 struct mdt_object *o;
750 LASSERT(info->mti_spec.u.sp_ea.no_lov_create);
751 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
753 RETURN(rc = PTR_ERR(o));
755 rc = mdt_object_exists(o);
757 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
761 rc = mo_attr_get(env, mdt_object_child(o), ma);
763 rc = mdt_mfd_open(info, NULL, o, flags, 0);
764 } else if (rc == 0) {
767 /* the child object was created on remote server */
768 struct mdt_body *repbody;
769 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
770 repbody->fid1 = *rr->rr_fid2;
771 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
775 mdt_object_put(info->mti_env, o);
779 int mdt_pin(struct mdt_thread_info* info)
782 RETURN(err_serious(-EOPNOTSUPP));
785 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
786 static int mdt_cross_open(struct mdt_thread_info* info,
787 const struct lu_fid *fid,
788 struct ldlm_reply *rep, __u32 flags)
790 struct md_attr *ma = &info->mti_attr;
791 struct mdt_object *o;
795 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
797 RETURN(rc = PTR_ERR(o));
799 rc = mdt_object_exists(o);
801 /* Do permission check for cross-open. */
802 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
803 NULL, flags | MDS_OPEN_CROSS);
807 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
808 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
810 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
811 } else if (rc == 0) {
813 * Something is wrong here. lookup was positive but there is
816 CERROR("Cross-ref object doesn't exist!\n");
819 /* Something is wrong here, the object is on another MDS! */
820 CERROR("The object isn't on this server! FLD error?\n");
821 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
823 "Object isn't on this server! FLD error?\n");
829 mdt_object_put(info->mti_env, o);
833 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
835 struct mdt_device *mdt = info->mti_mdt;
836 struct ptlrpc_request *req = mdt_info_req(info);
837 struct mdt_object *parent;
838 struct mdt_object *child;
839 struct mdt_lock_handle *lh;
840 struct ldlm_reply *ldlm_rep;
841 struct mdt_body *repbody;
842 struct lu_fid *child_fid = &info->mti_tmp_fid1;
843 struct md_attr *ma = &info->mti_attr;
844 __u32 create_flags = info->mti_spec.sp_cr_flags;
845 struct mdt_reint_record *rr = &info->mti_rr;
846 struct lu_name *lname;
851 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
852 (obd_timeout + 1) / 4);
854 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
856 ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
857 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill, &RMF_MDT_MD,
859 ma->ma_need = MA_INODE | MA_LOV;
862 LASSERT(info->mti_pill.rc_fmt == &RQF_LDLM_INTENT_OPEN);
863 ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
865 /* TODO: JOIN file */
866 if (create_flags & MDS_OPEN_JOIN_FILE) {
867 CERROR("JOIN file will be supported soon\n");
868 GOTO(out, result = err_serious(-EOPNOTSUPP));
871 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
872 "cr_flag=0%o mode=0%06o msg_flag=0x%x\n",
873 PFID(rr->rr_fid1), rr->rr_name,
874 PFID(rr->rr_fid2), create_flags,
875 ma->ma_attr.la_mode, lustre_msg_get_flags(req->rq_reqmsg));
877 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
878 /* This is a replay request. */
879 result = mdt_open_by_fid(info, ldlm_rep);
881 if (result != -ENOENT)
885 * We didn't find the correct object, so we need to re-create it
886 * via a regular replay.
888 if (!(create_flags & MDS_OPEN_CREAT)) {
889 DEBUG_REQ(D_ERROR, req,"OPEN & CREAT not in open replay.");
890 GOTO(out, result = -EFAULT);
892 CDEBUG(D_INFO, "Open replay did find object, continue as "
896 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
897 GOTO(out, result = err_serious(-ENOMEM));
899 mdt_set_disposition(info, ldlm_rep,
900 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
902 if (info->mti_cross_ref) {
903 /* This is cross-ref open */
904 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
905 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
910 lh = &info->mti_lh[MDT_LH_PARENT];
911 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
912 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
914 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
915 MDS_INODELOCK_UPDATE);
917 GOTO(out, result = PTR_ERR(parent));
921 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
923 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
924 lname, child_fid, &info->mti_spec);
925 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
926 "looking for "DFID"/%s, result fid="DFID"\n",
927 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
929 if (result != 0 && result != -ENOENT && result != -ESTALE)
930 GOTO(out_parent, result);
932 if (result == -ENOENT || result == -ESTALE) {
933 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
934 if (result == -ESTALE) {
936 * -ESTALE means the parent is a dead(unlinked) dir, so
937 * it should return -ENOENT to in accordance with the
938 * original mds implementaion.
940 GOTO(out_parent, result = -ENOENT);
942 if (!(create_flags & MDS_OPEN_CREAT))
943 GOTO(out_parent, result);
944 *child_fid = *info->mti_rr.rr_fid2;
945 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
949 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
950 * return FID back in that case.
952 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
955 child = mdt_object_find(info->mti_env, mdt, child_fid);
957 GOTO(out_parent, result = PTR_ERR(child));
959 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
960 if (result == -ENOENT) {
961 /* Not found and with MDS_OPEN_CREAT: let's create it. */
962 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
964 /* Let lower layers know what is lock mode on directory. */
965 info->mti_spec.sp_cr_mode =
966 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
969 * Do not perform lookup sanity check. We know that name does
972 info->mti_spec.sp_cr_lookup = 0;
974 result = mdo_create(info->mti_env,
975 mdt_object_child(parent),
977 mdt_object_child(child),
980 if (result == -ERESTART) {
981 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
982 GOTO(out_child, result);
985 GOTO(out_child, result);
989 /* We have to get attr & lov ea for this object */
990 result = mo_attr_get(info->mti_env, mdt_object_child(child),
993 * The object is on remote node, return its FID for remote open.
995 if (result == -EREMOTE) {
999 * Check if this lock already was sent to client and
1000 * this is resent case. For resent case do not take lock
1001 * again, use what is already granted.
1003 LASSERT(lhc != NULL);
1005 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1006 struct ldlm_lock *lock;
1008 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
1011 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1013 CERROR("Invalid lock handle "LPX64"\n",
1014 lhc->mlh_reg_lh.cookie);
1017 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1018 &lock->l_resource->lr_name));
1019 LDLM_LOCK_PUT(lock);
1022 mdt_lock_handle_init(lhc);
1023 mdt_lock_reg_init(lhc, LCK_PR);
1025 rc = mdt_object_lock(info, child, lhc,
1026 MDS_INODELOCK_LOOKUP,
1029 repbody->fid1 = *mdt_object_fid(child);
1030 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1033 GOTO(out_child, result);
1037 /* Try to open it now. */
1038 result = mdt_finish_open(info, parent, child, create_flags,
1041 if (result != 0 && created) {
1045 ma->ma_cookie_size = 0;
1046 info->mti_no_need_trans = 1;
1047 rc2 = mdo_unlink(info->mti_env,
1048 mdt_object_child(parent),
1049 mdt_object_child(child),
1053 CERROR("Error in cleanup of open\n");
1057 mdt_object_put(info->mti_env, child);
1059 mdt_object_unlock_put(info, parent, lh, result);
1061 mdt_shrink_reply(info);
1063 lustre_msg_set_transno(req->rq_repmsg, 0);
1067 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
1068 FMODE_EPOCHLCK)) == FMODE_CLOSED)
1070 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1072 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1075 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1077 struct mdt_object *o = mfd->mfd_object;
1078 struct md_object *next = mdt_object_child(o);
1079 struct md_attr *ma = &info->mti_attr;
1080 int rc = 0, ret = 0;
1084 mode = mfd->mfd_mode;
1086 if ((mode & FMODE_WRITE) || (mode & FMODE_EPOCHLCK)) {
1087 mdt_write_put(info->mti_mdt, o);
1088 ret = mdt_epoch_close(info, o);
1089 } else if (mode & MDS_FMODE_EXEC) {
1090 mdt_write_allow(info->mti_mdt, o);
1091 } else if (mode & FMODE_EPOCH) {
1092 ret = mdt_epoch_close(info, o);
1095 /* Update atime on close only. */
1096 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1097 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1098 /* Set the atime only. */
1099 ma->ma_attr.la_valid = LA_ATIME;
1100 rc = mo_attr_set(info->mti_env, next, ma);
1103 ma->ma_need |= MA_INODE;
1106 if (!MFD_CLOSED(mode))
1107 rc = mo_close(info->mti_env, next, ma);
1108 else if (ret == -EAGAIN)
1109 rc = mo_attr_get(info->mti_env, next, ma);
1111 /* If the object is unlinked, do not try to re-enable SIZEONMDS */
1112 if ((ret == -EAGAIN) && (ma->ma_valid & MA_INODE) &&
1113 (ma->ma_attr.la_nlink == 0)) {
1117 if ((ret == -EAGAIN) || (ret == 1)) {
1118 struct mdt_export_data *med;
1120 /* The epoch has not closed or Size-on-MDS update is needed.
1121 * Put mfd back into the list. */
1122 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1123 mdt_mfd_set_mode(mfd, (ret == 1 ? FMODE_EPOCH : FMODE_SOM));
1125 LASSERT(mdt_info_req(info));
1126 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1127 spin_lock(&med->med_open_lock);
1128 list_add(&mfd->mfd_list, &med->med_open_head);
1129 class_handle_hash_back(&mfd->mfd_handle);
1130 spin_unlock(&med->med_open_lock);
1135 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1136 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1140 mdt_object_put(info->mti_env, o);
1143 RETURN(rc ? rc : ret);
1146 int mdt_close(struct mdt_thread_info *info)
1148 struct mdt_export_data *med;
1149 struct mdt_file_data *mfd;
1150 struct mdt_object *o;
1151 struct md_attr *ma = &info->mti_attr;
1152 struct mdt_body *repbody = NULL;
1156 /* Close may come with the Size-on-MDS update. Unpack it. */
1157 rc = mdt_close_unpack(info);
1159 RETURN(err_serious(rc));
1161 LASSERT(info->mti_epoch);
1163 req_capsule_set_size(&info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1164 info->mti_mdt->mdt_max_mdsize);
1165 req_capsule_set_size(&info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1166 info->mti_mdt->mdt_max_cookiesize);
1167 rc = req_capsule_pack(&info->mti_pill);
1168 /* Continue to close handle even if we can not pack reply */
1170 repbody = req_capsule_server_get(&info->mti_pill,
1172 ma->ma_lmm = req_capsule_server_get(&info->mti_pill,
1174 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
1177 ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
1179 ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
1182 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1183 repbody->eadatasize = 0;
1184 repbody->aclsize = 0;
1186 rc = err_serious(rc);
1188 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1189 spin_lock(&med->med_open_lock);
1190 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1191 if (mdt_mfd_closed(mfd)) {
1192 spin_unlock(&med->med_open_lock);
1193 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1194 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1195 info->mti_epoch->handle.cookie);
1196 rc = err_serious(-ESTALE);
1198 class_handle_unhash(&mfd->mfd_handle);
1199 list_del_init(&mfd->mfd_list);
1200 spin_unlock(&med->med_open_lock);
1202 /* Do not lose object before last unlink. */
1203 o = mfd->mfd_object;
1204 mdt_object_get(info->mti_env, o);
1205 ret = mdt_mfd_close(info, mfd);
1206 if (repbody != NULL)
1207 rc = mdt_handle_last_unlink(info, o, ma);
1208 mdt_empty_transno(info);
1209 mdt_object_put(info->mti_env, o);
1211 if (repbody != NULL)
1212 mdt_shrink_reply(info);
1214 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1215 RETURN(err_serious(-ENOMEM));
1217 RETURN(rc ? rc : ret);
1220 int mdt_done_writing(struct mdt_thread_info *info)
1222 struct mdt_body *repbody = NULL;
1223 struct ptlrpc_request *req = mdt_info_req(info);
1224 struct mdt_export_data *med;
1225 struct mdt_file_data *mfd;
1229 rc = req_capsule_pack(&info->mti_pill);
1231 RETURN(err_serious(rc));
1233 repbody = req_capsule_server_get(&info->mti_pill,
1235 repbody->eadatasize = 0;
1236 repbody->aclsize = 0;
1238 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1239 rc = mdt_close_unpack(info);
1241 RETURN(err_serious(rc));
1243 med = &info->mti_exp->exp_mdt_data;
1244 spin_lock(&med->med_open_lock);
1245 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1247 spin_unlock(&med->med_open_lock);
1248 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1249 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1250 info->mti_epoch->handle.cookie);
1254 if (!(mfd->mfd_mode == FMODE_EPOCH ||
1255 mfd->mfd_mode == FMODE_EPOCHLCK)) {
1256 spin_unlock(&med->med_open_lock);
1257 DEBUG_REQ(D_WARNING, req, "req should be resent req");
1258 LASSERT(mfd->mfd_mode == FMODE_SOM);
1259 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
1260 (MSG_RESENT | MSG_REPLAY));
1261 /*Since we did not bond this req with open/close,
1262 *Why we should keep this req as replay req XXX*/
1263 GOTO(empty_transno, rc);
1265 class_handle_unhash(&mfd->mfd_handle);
1266 list_del_init(&mfd->mfd_list);
1267 spin_unlock(&med->med_open_lock);
1269 /* Set EPOCH CLOSE flag if not set by client. */
1270 info->mti_epoch->flags |= MF_EPOCH_CLOSE;
1271 info->mti_attr.ma_valid = 0;
1272 rc = mdt_mfd_close(info, mfd);
1274 mdt_empty_transno(info);