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;
570 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA2);
572 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
574 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
577 repbody->valid |= OBD_MD_FLOSSCAPA;
581 * If we are following a symlink, don't open; and do not return open
582 * handle for special nodes as client required.
584 if (islnk || (!isreg && !isdir &&
585 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
586 lustre_msg_set_transno(req->rq_repmsg, 0);
590 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
593 * We need to return the existing object's fid back, so it is done here,
594 * after preparing the reply.
596 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
599 /* This can't be done earlier, we need to return reply body */
601 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
602 /* We are trying to create or write an existing dir. */
605 } else if (flags & MDS_OPEN_DIRECTORY)
608 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
609 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
614 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
615 spin_lock(&med->med_open_lock);
616 list_for_each(t, &med->med_open_head) {
617 mfd = list_entry(t, struct mdt_file_data, mfd_list);
618 if (mfd->mfd_xid == req->rq_xid) {
623 spin_unlock(&med->med_open_lock);
626 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
627 /*set repbody->ea_size for resent case*/
628 if (ma->ma_valid & MA_LOV) {
629 LASSERT(ma->ma_lmm_size != 0);
630 repbody->eadatasize = ma->ma_lmm_size;
632 repbody->valid |= OBD_MD_FLDIREA;
634 repbody->valid |= OBD_MD_FLEASIZE;
640 rc = mdt_mfd_open(info, p, o, flags, created);
644 extern void mdt_req_from_mcd(struct ptlrpc_request *req,
645 struct mdt_client_data *mcd);
647 void mdt_reconstruct_open(struct mdt_thread_info *info,
648 struct mdt_lock_handle *lhc)
650 const struct lu_env *env = info->mti_env;
651 struct mdt_device *mdt = info->mti_mdt;
652 struct req_capsule *pill = &info->mti_pill;
653 struct ptlrpc_request *req = mdt_info_req(info);
654 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
655 struct mdt_client_data *mcd = med->med_mcd;
656 struct md_attr *ma = &info->mti_attr;
657 struct mdt_reint_record *rr = &info->mti_rr;
658 __u32 flags = info->mti_spec.sp_cr_flags;
659 struct ldlm_reply *ldlm_rep;
660 struct mdt_object *parent;
661 struct mdt_object *child;
662 struct mdt_body *repbody;
666 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
667 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
668 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
670 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
671 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
673 ma->ma_need = MA_INODE | MA_LOV;
676 mdt_req_from_mcd(req, med->med_mcd);
677 mdt_set_disposition(info, ldlm_rep, mcd->mcd_last_data);
679 CERROR("This is reconstruct open: disp="LPX64", result=%d\n",
680 ldlm_rep->lock_policy_res1, req->rq_status);
682 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
683 req->rq_status != 0) {
684 /* We did not create successfully, return error to client. */
685 mdt_shrink_reply(info);
686 GOTO(out, rc = req->rq_status);
689 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
691 * We failed after creation, but we do not know in which step
692 * we failed. So try to check the child object.
694 parent = mdt_object_find(env, mdt, rr->rr_fid1);
695 LASSERT(!IS_ERR(parent));
697 child = mdt_object_find(env, mdt, rr->rr_fid2);
698 LASSERT(!IS_ERR(child));
700 rc = mdt_object_exists(child);
702 struct md_object *next;
704 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
705 next = mdt_object_child(child);
706 rc = mo_attr_get(env, next, ma);
708 rc = mdt_finish_open(info, parent, child,
711 /* the child object was created on remote server */
712 repbody->fid1 = *rr->rr_fid2;
713 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
715 } else if (rc == 0) {
716 /* the child does not exist, we should do regular open */
717 mdt_object_put(env, parent);
718 mdt_object_put(env, child);
719 GOTO(regular_open, 0);
721 mdt_object_put(env, parent);
722 mdt_object_put(env, child);
723 mdt_shrink_reply(info);
727 /* We did not try to create, so we are a pure open */
728 rc = mdt_reint_open(info, lhc);
734 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
735 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
738 static int mdt_open_by_fid(struct mdt_thread_info* info,
739 struct ldlm_reply *rep)
741 const struct lu_env *env = info->mti_env;
742 __u32 flags = info->mti_spec.sp_cr_flags;
743 struct mdt_reint_record *rr = &info->mti_rr;
744 struct md_attr *ma = &info->mti_attr;
745 struct mdt_object *o;
749 LASSERT(info->mti_spec.u.sp_ea.no_lov_create);
750 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
752 RETURN(rc = PTR_ERR(o));
754 rc = mdt_object_exists(o);
756 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
760 rc = mo_attr_get(env, mdt_object_child(o), ma);
762 rc = mdt_mfd_open(info, NULL, o, flags, 0);
763 } else if (rc == 0) {
766 /* the child object was created on remote server */
767 struct mdt_body *repbody;
768 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
769 repbody->fid1 = *rr->rr_fid2;
770 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
774 mdt_object_put(info->mti_env, o);
778 int mdt_pin(struct mdt_thread_info* info)
781 RETURN(err_serious(-EOPNOTSUPP));
784 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
785 static int mdt_cross_open(struct mdt_thread_info* info,
786 const struct lu_fid *fid,
787 struct ldlm_reply *rep, __u32 flags)
789 struct md_attr *ma = &info->mti_attr;
790 struct mdt_object *o;
794 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
796 RETURN(rc = PTR_ERR(o));
798 rc = mdt_object_exists(o);
800 /* Do permission check for cross-open. */
801 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
802 NULL, flags | MDS_OPEN_CROSS);
806 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
807 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
809 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
810 } else if (rc == 0) {
812 * Something is wrong here. lookup was positive but there is
815 CERROR("Cross-ref object doesn't exist!\n");
818 /* Something is wrong here, the object is on another MDS! */
819 CERROR("The object isn't on this server! FLD error?\n");
820 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
822 "Object isn't on this server! FLD error?\n");
828 mdt_object_put(info->mti_env, o);
832 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
834 struct mdt_device *mdt = info->mti_mdt;
835 struct ptlrpc_request *req = mdt_info_req(info);
836 struct mdt_object *parent;
837 struct mdt_object *child;
838 struct mdt_lock_handle *lh;
839 struct ldlm_reply *ldlm_rep;
840 struct mdt_body *repbody;
841 struct lu_fid *child_fid = &info->mti_tmp_fid1;
842 struct md_attr *ma = &info->mti_attr;
843 __u32 create_flags = info->mti_spec.sp_cr_flags;
844 struct mdt_reint_record *rr = &info->mti_rr;
845 struct lu_name *lname;
850 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
851 (obd_timeout + 1) / 4);
853 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
855 ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
856 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill, &RMF_MDT_MD,
858 ma->ma_need = MA_INODE | MA_LOV;
861 LASSERT(info->mti_pill.rc_fmt == &RQF_LDLM_INTENT_OPEN);
862 ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
864 /* TODO: JOIN file */
865 if (create_flags & MDS_OPEN_JOIN_FILE) {
866 CERROR("JOIN file will be supported soon\n");
867 GOTO(out, result = err_serious(-EOPNOTSUPP));
870 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
871 "cr_flag=0%o mode=0%06o msg_flag=0x%x\n",
872 PFID(rr->rr_fid1), rr->rr_name,
873 PFID(rr->rr_fid2), create_flags,
874 ma->ma_attr.la_mode, lustre_msg_get_flags(req->rq_reqmsg));
876 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
877 /* This is a replay request. */
878 result = mdt_open_by_fid(info, ldlm_rep);
880 if (result != -ENOENT)
884 * We didn't find the correct object, so we need to re-create it
885 * via a regular replay.
887 if (!(create_flags & MDS_OPEN_CREAT)) {
888 DEBUG_REQ(D_ERROR, req,"OPEN & CREAT not in open replay.");
889 GOTO(out, result = -EFAULT);
891 CDEBUG(D_INFO, "Open replay did find object, continue as "
895 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
896 GOTO(out, result = err_serious(-ENOMEM));
898 mdt_set_disposition(info, ldlm_rep,
899 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
901 if (info->mti_cross_ref) {
902 /* This is cross-ref open */
903 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
904 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
909 lh = &info->mti_lh[MDT_LH_PARENT];
910 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
911 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
913 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
914 MDS_INODELOCK_UPDATE);
916 GOTO(out, result = PTR_ERR(parent));
920 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
922 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
923 lname, child_fid, &info->mti_spec);
924 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
925 "looking for "DFID"/%s, result fid="DFID"\n",
926 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
928 if (result != 0 && result != -ENOENT && result != -ESTALE)
929 GOTO(out_parent, result);
931 if (result == -ENOENT || result == -ESTALE) {
932 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
933 if (result == -ESTALE) {
935 * -ESTALE means the parent is a dead(unlinked) dir, so
936 * it should return -ENOENT to in accordance with the
937 * original mds implementaion.
939 GOTO(out_parent, result = -ENOENT);
941 if (!(create_flags & MDS_OPEN_CREAT))
942 GOTO(out_parent, result);
943 *child_fid = *info->mti_rr.rr_fid2;
944 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
948 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
949 * return FID back in that case.
951 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
954 child = mdt_object_find(info->mti_env, mdt, child_fid);
956 GOTO(out_parent, result = PTR_ERR(child));
958 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
959 if (result == -ENOENT) {
960 /* Not found and with MDS_OPEN_CREAT: let's create it. */
961 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
963 /* Let lower layers know what is lock mode on directory. */
964 info->mti_spec.sp_cr_mode =
965 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
968 * Do not perform lookup sanity check. We know that name does
971 info->mti_spec.sp_cr_lookup = 0;
973 result = mdo_create(info->mti_env,
974 mdt_object_child(parent),
976 mdt_object_child(child),
979 if (result == -ERESTART) {
980 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
981 GOTO(out_child, result);
984 GOTO(out_child, result);
988 /* We have to get attr & lov ea for this object */
989 result = mo_attr_get(info->mti_env, mdt_object_child(child),
992 * The object is on remote node, return its FID for remote open.
994 if (result == -EREMOTE) {
998 * Check if this lock already was sent to client and
999 * this is resent case. For resent case do not take lock
1000 * again, use what is already granted.
1002 LASSERT(lhc != NULL);
1004 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1005 struct ldlm_lock *lock;
1007 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
1010 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1012 CERROR("Invalid lock handle "LPX64"\n",
1013 lhc->mlh_reg_lh.cookie);
1016 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1017 &lock->l_resource->lr_name));
1018 LDLM_LOCK_PUT(lock);
1021 mdt_lock_handle_init(lhc);
1022 mdt_lock_reg_init(lhc, LCK_PR);
1024 rc = mdt_object_lock(info, child, lhc,
1025 MDS_INODELOCK_LOOKUP,
1028 repbody->fid1 = *mdt_object_fid(child);
1029 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1032 GOTO(out_child, result);
1036 /* Try to open it now. */
1037 result = mdt_finish_open(info, parent, child, create_flags,
1040 if (result != 0 && created) {
1044 ma->ma_cookie_size = 0;
1045 info->mti_no_need_trans = 1;
1046 rc2 = mdo_unlink(info->mti_env,
1047 mdt_object_child(parent),
1048 mdt_object_child(child),
1052 CERROR("Error in cleanup of open\n");
1056 mdt_object_put(info->mti_env, child);
1058 mdt_object_unlock_put(info, parent, lh, result);
1060 mdt_shrink_reply(info);
1062 lustre_msg_set_transno(req->rq_repmsg, 0);
1066 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
1067 FMODE_EPOCHLCK)) == FMODE_CLOSED)
1069 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1071 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1074 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1076 struct mdt_object *o = mfd->mfd_object;
1077 struct md_object *next = mdt_object_child(o);
1078 struct md_attr *ma = &info->mti_attr;
1079 int rc = 0, ret = 0;
1083 mode = mfd->mfd_mode;
1085 if ((mode & FMODE_WRITE) || (mode & FMODE_EPOCHLCK)) {
1086 mdt_write_put(info->mti_mdt, o);
1087 ret = mdt_epoch_close(info, o);
1088 } else if (mode & MDS_FMODE_EXEC) {
1089 mdt_write_allow(info->mti_mdt, o);
1090 } else if (mode & FMODE_EPOCH) {
1091 ret = mdt_epoch_close(info, o);
1094 /* Update atime on close only. */
1095 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1096 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1097 /* Set the atime only. */
1098 ma->ma_attr.la_valid = LA_ATIME;
1099 rc = mo_attr_set(info->mti_env, next, ma);
1102 ma->ma_need |= MA_INODE;
1105 if (!MFD_CLOSED(mode))
1106 rc = mo_close(info->mti_env, next, ma);
1107 else if (ret == -EAGAIN)
1108 rc = mo_attr_get(info->mti_env, next, ma);
1110 /* If the object is unlinked, do not try to re-enable SIZEONMDS */
1111 if ((ret == -EAGAIN) && (ma->ma_valid & MA_INODE) &&
1112 (ma->ma_attr.la_nlink == 0)) {
1116 if ((ret == -EAGAIN) || (ret == 1)) {
1117 struct mdt_export_data *med;
1119 /* The epoch has not closed or Size-on-MDS update is needed.
1120 * Put mfd back into the list. */
1121 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1122 mdt_mfd_set_mode(mfd, (ret == 1 ? FMODE_EPOCH : FMODE_SOM));
1124 LASSERT(mdt_info_req(info));
1125 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1126 spin_lock(&med->med_open_lock);
1127 list_add(&mfd->mfd_list, &med->med_open_head);
1128 class_handle_hash_back(&mfd->mfd_handle);
1129 spin_unlock(&med->med_open_lock);
1134 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1135 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1139 mdt_object_put(info->mti_env, o);
1142 RETURN(rc ? rc : ret);
1145 int mdt_close(struct mdt_thread_info *info)
1147 struct mdt_export_data *med;
1148 struct mdt_file_data *mfd;
1149 struct mdt_object *o;
1150 struct md_attr *ma = &info->mti_attr;
1151 struct mdt_body *repbody = NULL;
1152 struct ptlrpc_request *req = mdt_info_req(info);
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 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1169 RETURN(lustre_msg_get_status(req->rq_repmsg));
1171 /* Continue to close handle even if we can not pack reply */
1173 repbody = req_capsule_server_get(&info->mti_pill,
1175 ma->ma_lmm = req_capsule_server_get(&info->mti_pill,
1177 ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
1180 ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
1182 ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
1185 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1186 repbody->eadatasize = 0;
1187 repbody->aclsize = 0;
1189 rc = err_serious(rc);
1191 med = &req->rq_export->exp_mdt_data;
1192 spin_lock(&med->med_open_lock);
1193 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1194 if (mdt_mfd_closed(mfd)) {
1195 spin_unlock(&med->med_open_lock);
1196 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1197 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1198 info->mti_epoch->handle.cookie);
1199 rc = err_serious(-ESTALE);
1201 class_handle_unhash(&mfd->mfd_handle);
1202 list_del_init(&mfd->mfd_list);
1203 spin_unlock(&med->med_open_lock);
1205 /* Do not lose object before last unlink. */
1206 o = mfd->mfd_object;
1207 mdt_object_get(info->mti_env, o);
1208 ret = mdt_mfd_close(info, mfd);
1209 if (repbody != NULL)
1210 rc = mdt_handle_last_unlink(info, o, ma);
1211 mdt_empty_transno(info);
1212 mdt_object_put(info->mti_env, o);
1214 if (repbody != NULL)
1215 mdt_shrink_reply(info);
1217 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1218 RETURN(err_serious(-ENOMEM));
1220 RETURN(rc ? rc : ret);
1223 int mdt_done_writing(struct mdt_thread_info *info)
1225 struct mdt_body *repbody = NULL;
1226 struct mdt_export_data *med;
1227 struct mdt_file_data *mfd;
1231 rc = req_capsule_pack(&info->mti_pill);
1233 RETURN(err_serious(rc));
1235 repbody = req_capsule_server_get(&info->mti_pill,
1237 repbody->eadatasize = 0;
1238 repbody->aclsize = 0;
1240 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1241 rc = mdt_close_unpack(info);
1243 RETURN(err_serious(rc));
1245 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1246 RETURN(lustre_msg_get_status(mdt_info_req(info)->rq_repmsg));
1248 med = &info->mti_exp->exp_mdt_data;
1249 spin_lock(&med->med_open_lock);
1250 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1252 spin_unlock(&med->med_open_lock);
1253 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1254 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1255 info->mti_epoch->handle.cookie);
1259 LASSERT(mfd->mfd_mode == FMODE_EPOCH ||
1260 mfd->mfd_mode == FMODE_EPOCHLCK);
1261 class_handle_unhash(&mfd->mfd_handle);
1262 list_del_init(&mfd->mfd_list);
1263 spin_unlock(&med->med_open_lock);
1265 /* Set EPOCH CLOSE flag if not set by client. */
1266 info->mti_epoch->flags |= MF_EPOCH_CLOSE;
1267 info->mti_attr.ma_valid = 0;
1268 rc = mdt_mfd_close(info, mfd);
1269 mdt_empty_transno(info);