1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_open.c
38 * Lustre Metadata Target (mdt) open/close file handling
40 * Author: Huang Hua <huanghua@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <lustre_acl.h>
49 #include <lustre_mds.h>
50 #include "mdt_internal.h"
52 /* we do nothing because we do not have refcount now */
53 static void mdt_mfd_get(void *mfdp)
57 /* Create a new mdt_file_data struct, initialize it,
58 * and insert it to global hash table */
59 struct mdt_file_data *mdt_mfd_new(void)
61 struct mdt_file_data *mfd;
66 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
67 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
68 class_handle_hash(&mfd->mfd_handle, mdt_mfd_get);
74 * Find the mfd pointed to by handle in global hash table.
75 * In case of replay the handle is obsoleted
76 * but mfd can be found in mfd list by that handle
78 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
79 const struct lustre_handle *handle)
81 struct ptlrpc_request *req = mdt_info_req(info);
82 struct mdt_file_data *mfd;
85 LASSERT(handle != NULL);
86 mfd = class_handle2object(handle->cookie);
87 /* during dw/setattr replay the mfd can be found by old handle */
89 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
90 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
91 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
92 if (mfd->mfd_old_handle.cookie == handle->cookie)
101 void mdt_mfd_free(struct mdt_file_data *mfd)
103 LASSERT(list_empty(&mfd->mfd_list));
104 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
107 static int mdt_create_data(struct mdt_thread_info *info,
108 struct mdt_object *p, struct mdt_object *o)
110 struct md_op_spec *spec = &info->mti_spec;
111 struct md_attr *ma = &info->mti_attr;
115 if (!md_should_create(spec->sp_cr_flags))
118 ma->ma_need = MA_INODE | MA_LOV;
120 rc = mdo_create_data(info->mti_env,
121 p ? mdt_object_child(p) : NULL,
122 mdt_object_child(o), spec, ma);
126 static int mdt_epoch_opened(struct mdt_object *mo)
128 return mo->mot_epochcount;
131 int mdt_sizeonmds_enabled(struct mdt_object *mo)
133 return !mo->mot_ioepoch;
136 /* Re-enable Size-on-MDS. */
137 void mdt_sizeonmds_enable(struct mdt_thread_info *info,
138 struct mdt_object *mo)
140 spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
141 if (info->mti_epoch->ioepoch == mo->mot_ioepoch) {
142 LASSERT(!mdt_epoch_opened(mo));
146 spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
149 /* Open the epoch. Epoch open is allowed if @writecount is not negative.
150 * The epoch and writecount handling is performed under the mdt_ioepoch_lock. */
151 int mdt_epoch_open(struct mdt_thread_info *info, struct mdt_object *o)
153 struct mdt_device *mdt = info->mti_mdt;
158 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
159 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
162 spin_lock(&mdt->mdt_ioepoch_lock);
163 if (mdt_epoch_opened(o)) {
164 /* Epoch continues even if there is no writers yet. */
165 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
166 o->mot_ioepoch, PFID(mdt_object_fid(o)));
168 if (info->mti_replayepoch > mdt->mdt_ioepoch)
169 mdt->mdt_ioepoch = info->mti_replayepoch;
172 o->mot_ioepoch = info->mti_replayepoch ?
173 info->mti_replayepoch : mdt->mdt_ioepoch;
174 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
175 mdt->mdt_ioepoch, PFID(mdt_object_fid(o)));
179 spin_unlock(&mdt->mdt_ioepoch_lock);
181 /* Cancel Size-on-MDS attributes on clients if not truncate.
182 * In the later case, mdt_reint_setattr will do it. */
183 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
184 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
185 mdt_lock_reg_init(lh, LCK_EX);
186 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
189 mdt_object_unlock(info, o, lh, 1);
194 /* Update the on-disk attributes if needed and re-enable Size-on-MDS caching. */
195 static int mdt_sizeonmds_update(struct mdt_thread_info *info,
196 struct mdt_object *o)
200 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
201 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_epochcount);
203 if (info->mti_attr.ma_attr.la_valid & LA_SIZE) {
204 /* Do Size-on-MDS attribute update.
205 * Size-on-MDS is re-enabled inside. */
206 /* XXX: since we have opened the file, it is unnecessary
207 * to check permission when close it. Between the "open"
208 * and "close", maybe someone has changed the file mode
209 * or flags, or the file created mode do not permit wirte,
210 * and so on. Just set MDS_PERM_BYPASS for all the cases. */
211 info->mti_attr.ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
212 info->mti_attr.ma_attr.la_valid &= LA_SIZE | LA_BLOCKS |
213 LA_ATIME | LA_MTIME | LA_CTIME;
214 RETURN(mdt_attr_set(info, o, 0));
216 mdt_sizeonmds_enable(info, o);
221 * Returns 1 if epoch does not close.
222 * Returns 0 if epoch closes.
223 * Returns -EAGAIN if epoch closes but an Size-on-MDS Update is still needed
224 * from the client. */
225 static int mdt_epoch_close(struct mdt_thread_info *info, struct mdt_object *o)
227 int eviction = (mdt_info_req(info) == NULL ? 1 : 0);
228 struct lu_attr *la = &info->mti_attr.ma_attr;
234 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
235 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
238 spin_lock(&info->mti_mdt->mdt_ioepoch_lock);
240 /* Epoch closes only if client tells about it or eviction occures. */
241 if (eviction || (info->mti_epoch->flags & MF_EPOCH_CLOSE)) {
242 LASSERT(o->mot_epochcount);
245 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
246 o->mot_ioepoch, PFID(mdt_object_fid(o)),
250 achange = (info->mti_epoch->flags & MF_SOM_CHANGE);
253 if (!eviction && !mdt_epoch_opened(o)) {
254 /* Epoch ends. Is an Size-on-MDS update needed? */
255 if (o->mot_flags & MF_SOM_CHANGE) {
256 /* Some previous writer changed the attribute.
257 * Do not believe to the current Size-on-MDS
258 * update, re-ask client. */
260 } else if (!(la->la_valid & LA_SIZE) && achange) {
261 /* Attributes were changed by the last writer
262 * only but no Size-on-MDS update is received.*/
267 if (achange || eviction)
268 o->mot_flags |= MF_SOM_CHANGE;
271 opened = mdt_epoch_opened(o);
272 spin_unlock(&info->mti_mdt->mdt_ioepoch_lock);
274 /* If eviction occurred, do nothing. */
275 if ((rc == 0) && !opened && !eviction) {
276 /* Epoch ends and wanted Size-on-MDS update is obtained. */
277 rc = mdt_sizeonmds_update(info, o);
278 /* Avoid the following setattrs of these attributes, e.g.
279 * for atime update. */
280 info->mti_attr.ma_valid = 0;
285 int mdt_write_read(struct mdt_device *mdt, struct mdt_object *o)
289 spin_lock(&mdt->mdt_ioepoch_lock);
290 rc = o->mot_writecount;
291 spin_unlock(&mdt->mdt_ioepoch_lock);
295 int mdt_write_get(struct mdt_device *mdt, struct mdt_object *o)
299 spin_lock(&mdt->mdt_ioepoch_lock);
300 if (o->mot_writecount < 0)
304 spin_unlock(&mdt->mdt_ioepoch_lock);
308 static void mdt_write_put(struct mdt_device *mdt, struct mdt_object *o)
311 spin_lock(&mdt->mdt_ioepoch_lock);
313 spin_unlock(&mdt->mdt_ioepoch_lock);
317 static int mdt_write_deny(struct mdt_device *mdt, struct mdt_object *o)
321 spin_lock(&mdt->mdt_ioepoch_lock);
322 if (o->mot_writecount > 0)
326 spin_unlock(&mdt->mdt_ioepoch_lock);
330 static void mdt_write_allow(struct mdt_device *mdt, struct mdt_object *o)
333 spin_lock(&mdt->mdt_ioepoch_lock);
335 spin_unlock(&mdt->mdt_ioepoch_lock);
339 /* there can be no real transaction so prepare the fake one */
340 static void mdt_empty_transno(struct mdt_thread_info* info)
342 struct mdt_device *mdt = info->mti_mdt;
343 struct ptlrpc_request *req = mdt_info_req(info);
346 /* transaction is occured already */
347 if (lustre_msg_get_transno(req->rq_repmsg) != 0) {
352 spin_lock(&mdt->mdt_transno_lock);
353 if (info->mti_transno == 0) {
354 info->mti_transno = ++ mdt->mdt_last_transno;
356 /* should be replay */
357 if (info->mti_transno > mdt->mdt_last_transno)
358 mdt->mdt_last_transno = info->mti_transno;
360 spin_unlock(&mdt->mdt_transno_lock);
362 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
364 req->rq_export->exp_obd->obd_last_committed);
366 req->rq_transno = info->mti_transno;
367 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
368 lustre_msg_set_last_xid(req->rq_repmsg, req->rq_xid);
372 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
374 LASSERT(mfd != NULL);
376 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
377 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
379 mfd->mfd_mode = mode;
382 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
383 struct mdt_object *o, int flags, int created)
385 struct ptlrpc_request *req = mdt_info_req(info);
386 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
387 struct mdt_file_data *mfd;
388 struct md_attr *ma = &info->mti_attr;
389 struct lu_attr *la = &ma->ma_attr;
390 struct mdt_body *repbody;
391 int rc = 0, isdir, isreg;
394 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
396 isreg = S_ISREG(la->la_mode);
397 isdir = S_ISDIR(la->la_mode);
398 if (isreg && !(ma->ma_valid & MA_LOV)) {
400 * No EA, check whether it is will set regEA and dirEA since in
401 * above attr get, these size might be zero, so reset it, to
402 * retrieve the MD after create obj.
404 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
407 /* in replay case, p == NULL */
408 rc = mdt_create_data(info, p, o);
413 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
414 ma->ma_valid, ma->ma_lmm_size);
416 if (ma->ma_valid & MA_LOV) {
417 LASSERT(ma->ma_lmm_size != 0);
418 repbody->eadatasize = ma->ma_lmm_size;
420 repbody->valid |= OBD_MD_FLDIREA;
422 repbody->valid |= OBD_MD_FLEASIZE;
425 if (flags & FMODE_WRITE) {
426 rc = mdt_write_get(info->mti_mdt, o);
428 mdt_epoch_open(info, o);
429 repbody->ioepoch = o->mot_ioepoch;
431 } else if (flags & MDS_FMODE_EXEC) {
432 rc = mdt_write_deny(info->mti_mdt, o);
437 rc = mo_open(info->mti_env, mdt_object_child(o),
438 created ? flags | MDS_OPEN_CREATED : flags);
445 * Keep a reference on this object for this open, and is
446 * released by mdt_mfd_close().
448 mdt_object_get(info->mti_env, o);
451 * @flags is always not zero. At least it should be FMODE_READ,
452 * FMODE_WRITE or FMODE_EXEC.
457 mdt_mfd_set_mode(mfd, flags);
460 mfd->mfd_xid = req->rq_xid;
463 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
464 struct mdt_file_data *old_mfd;
465 /* Check wheather old cookie already exist in
466 * the list, becasue when do recovery, client
467 * might be disconnected from server, and
468 * restart replay, so there maybe some orphan
469 * mfd here, we should remove them */
470 LASSERT(info->mti_rr.rr_handle != NULL);
471 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
473 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
476 PFID(mdt_object_fid(mfd->mfd_object)),
477 info->mti_rr.rr_handle->cookie);
478 spin_lock(&med->med_open_lock);
479 class_handle_unhash(&old_mfd->mfd_handle);
480 list_del_init(&old_mfd->mfd_list);
481 spin_unlock(&med->med_open_lock);
482 mdt_mfd_close(info, old_mfd);
484 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
485 info->mti_rr.rr_handle->cookie);
486 mfd->mfd_old_handle.cookie =
487 info->mti_rr.rr_handle->cookie;
489 spin_lock(&med->med_open_lock);
490 list_add(&mfd->mfd_list, &med->med_open_head);
491 spin_unlock(&med->med_open_lock);
493 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
494 mdt_empty_transno(info);
502 static int mdt_finish_open(struct mdt_thread_info *info,
503 struct mdt_object *p, struct mdt_object *o,
504 int flags, int created, struct ldlm_reply *rep)
506 struct ptlrpc_request *req = mdt_info_req(info);
507 struct obd_export *exp = req->rq_export;
508 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
509 struct md_attr *ma = &info->mti_attr;
510 struct lu_attr *la = &ma->ma_attr;
511 struct mdt_file_data *mfd;
512 struct mdt_body *repbody;
514 int isreg, isdir, islnk;
518 LASSERT(ma->ma_valid & MA_INODE);
520 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
522 isreg = S_ISREG(la->la_mode);
523 isdir = S_ISDIR(la->la_mode);
524 islnk = S_ISLNK(la->la_mode);
525 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
527 if (exp_connect_rmtclient(exp)) {
528 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
530 rc = mdt_pack_remote_perm(info, o, buf);
532 repbody->valid &= ~OBD_MD_FLRMTPERM;
533 repbody->aclsize = 0;
535 repbody->valid |= OBD_MD_FLRMTPERM;
536 repbody->aclsize = sizeof(struct mdt_remote_perm);
539 #ifdef CONFIG_FS_POSIX_ACL
540 else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
541 const struct lu_env *env = info->mti_env;
542 struct md_object *next = mdt_object_child(o);
543 struct lu_buf *buf = &info->mti_buf;
545 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
546 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
548 if (buf->lb_len > 0) {
549 rc = mo_xattr_get(env, next, buf,
550 XATTR_NAME_ACL_ACCESS);
552 if (rc == -ENODATA) {
553 repbody->aclsize = 0;
554 repbody->valid |= OBD_MD_FLACL;
556 } else if (rc == -EOPNOTSUPP) {
559 CERROR("got acl size: %d\n", rc);
562 repbody->aclsize = rc;
563 repbody->valid |= OBD_MD_FLACL;
570 if (info->mti_mdt->mdt_opts.mo_mds_capa &&
571 exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
572 struct lustre_capa *capa;
574 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
576 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
577 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
580 repbody->valid |= OBD_MD_FLMDSCAPA;
582 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
583 exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
584 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
585 struct lustre_capa *capa;
587 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
589 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
590 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
593 repbody->valid |= OBD_MD_FLOSSCAPA;
597 * If we are following a symlink, don't open; and do not return open
598 * handle for special nodes as client required.
600 if (islnk || (!isreg && !isdir &&
601 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
602 lustre_msg_set_transno(req->rq_repmsg, 0);
606 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
609 * We need to return the existing object's fid back, so it is done here,
610 * after preparing the reply.
612 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
615 /* This can't be done earlier, we need to return reply body */
617 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
618 /* We are trying to create or write an existing dir. */
621 } else if (flags & MDS_OPEN_DIRECTORY)
624 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
625 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
630 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
631 spin_lock(&med->med_open_lock);
632 list_for_each(t, &med->med_open_head) {
633 mfd = list_entry(t, struct mdt_file_data, mfd_list);
634 if (mfd->mfd_xid == req->rq_xid) {
639 spin_unlock(&med->med_open_lock);
642 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
643 /*set repbody->ea_size for resent case*/
644 if (ma->ma_valid & MA_LOV) {
645 LASSERT(ma->ma_lmm_size != 0);
646 repbody->eadatasize = ma->ma_lmm_size;
648 repbody->valid |= OBD_MD_FLDIREA;
650 repbody->valid |= OBD_MD_FLEASIZE;
656 rc = mdt_mfd_open(info, p, o, flags, created);
660 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
661 struct lsd_client_data *lcd);
663 void mdt_reconstruct_open(struct mdt_thread_info *info,
664 struct mdt_lock_handle *lhc)
666 const struct lu_env *env = info->mti_env;
667 struct mdt_device *mdt = info->mti_mdt;
668 struct req_capsule *pill = info->mti_pill;
669 struct ptlrpc_request *req = mdt_info_req(info);
670 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
671 struct lsd_client_data *lcd = med->med_lcd;
672 struct md_attr *ma = &info->mti_attr;
673 struct mdt_reint_record *rr = &info->mti_rr;
674 __u32 flags = info->mti_spec.sp_cr_flags;
675 struct ldlm_reply *ldlm_rep;
676 struct mdt_object *parent;
677 struct mdt_object *child;
678 struct mdt_body *repbody;
682 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
683 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
684 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
686 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
687 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
689 ma->ma_need = MA_INODE;
690 if (ma->ma_lmm_size > 0)
691 ma->ma_need |= MA_LOV;
695 mdt_req_from_lcd(req, med->med_lcd);
696 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
698 CERROR("This is reconstruct open: disp="LPX64", result=%d\n",
699 ldlm_rep->lock_policy_res1, req->rq_status);
701 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
703 /* We did not create successfully, return error to client. */
704 GOTO(out, rc = req->rq_status);
706 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
707 struct obd_export *exp = req->rq_export;
709 * We failed after creation, but we do not know in which step
710 * we failed. So try to check the child object.
712 parent = mdt_object_find(env, mdt, rr->rr_fid1);
713 if (IS_ERR(parent)) {
714 rc = PTR_ERR(parent);
715 LCONSOLE_WARN("Parent "DFID" lookup error %d."
716 " Evicting client %s with export %s.\n",
717 PFID(mdt_object_fid(parent)), rc,
718 obd_uuid2str(&exp->exp_client_uuid),
719 obd_export_nid2str(exp));
720 mdt_export_evict(exp);
724 child = mdt_object_find(env, mdt, rr->rr_fid2);
727 LCONSOLE_WARN("Child "DFID" lookup error %d."
728 " Evicting client %s with export %s.\n",
729 PFID(mdt_object_fid(child)), rc,
730 obd_uuid2str(&exp->exp_client_uuid),
731 obd_export_nid2str(exp));
732 mdt_export_evict(exp);
736 rc = mdt_object_exists(child);
738 struct md_object *next;
740 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
741 next = mdt_object_child(child);
742 rc = mo_attr_get(env, next, ma);
744 rc = mdt_finish_open(info, parent, child,
747 /* the child object was created on remote server */
748 repbody->fid1 = *rr->rr_fid2;
749 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
751 } else if (rc == 0) {
752 /* the child does not exist, we should do regular open */
753 mdt_object_put(env, parent);
754 mdt_object_put(env, child);
755 GOTO(regular_open, 0);
757 mdt_object_put(env, parent);
758 mdt_object_put(env, child);
762 /* We did not try to create, so we are a pure open */
763 rc = mdt_reint_open(info, lhc);
769 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
770 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
773 static int mdt_open_by_fid(struct mdt_thread_info* info,
774 struct ldlm_reply *rep)
776 const struct lu_env *env = info->mti_env;
777 __u32 flags = info->mti_spec.sp_cr_flags;
778 struct mdt_reint_record *rr = &info->mti_rr;
779 struct md_attr *ma = &info->mti_attr;
780 struct mdt_object *o;
784 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
786 RETURN(rc = PTR_ERR(o));
788 rc = mdt_object_exists(o);
790 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
794 rc = mo_attr_get(env, mdt_object_child(o), ma);
796 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
797 } else if (rc == 0) {
800 /* the child object was created on remote server */
801 struct mdt_body *repbody;
802 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
803 repbody->fid1 = *rr->rr_fid2;
804 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
808 mdt_object_put(info->mti_env, o);
812 int mdt_pin(struct mdt_thread_info* info)
815 RETURN(err_serious(-EOPNOTSUPP));
818 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
819 static int mdt_cross_open(struct mdt_thread_info* info,
820 const struct lu_fid *fid,
821 struct ldlm_reply *rep, __u32 flags)
823 struct md_attr *ma = &info->mti_attr;
824 struct mdt_object *o;
828 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
830 RETURN(rc = PTR_ERR(o));
832 rc = mdt_object_exists(o);
834 /* Do permission check for cross-open. */
835 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
836 NULL, flags | MDS_OPEN_CROSS);
840 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
841 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
843 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
844 } else if (rc == 0) {
846 * Something is wrong here. lookup was positive but there is
849 CERROR("Cross-ref object doesn't exist!\n");
852 /* Something is wrong here, the object is on another MDS! */
853 CERROR("The object isn't on this server! FLD error?\n");
854 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
856 "Object isn't on this server! FLD error?\n");
862 mdt_object_put(info->mti_env, o);
866 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
868 struct mdt_device *mdt = info->mti_mdt;
869 struct ptlrpc_request *req = mdt_info_req(info);
870 struct mdt_object *parent;
871 struct mdt_object *child;
872 struct mdt_lock_handle *lh;
873 struct ldlm_reply *ldlm_rep;
874 struct mdt_body *repbody;
875 struct lu_fid *child_fid = &info->mti_tmp_fid1;
876 struct md_attr *ma = &info->mti_attr;
877 __u32 create_flags = info->mti_spec.sp_cr_flags;
878 struct mdt_reint_record *rr = &info->mti_rr;
879 struct lu_name *lname;
885 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
886 (obd_timeout + 1) / 4);
888 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
890 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
891 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
893 ma->ma_need = MA_INODE;
894 if (ma->ma_lmm_size > 0)
895 ma->ma_need |= MA_LOV;
899 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
900 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
902 /* JOIN file was deprecated since 1.6.5, but may be revived one day */
903 if (create_flags & MDS_OPEN_JOIN_FILE) {
904 CERROR("file join is unsupported in this version of Lustre\n");
905 GOTO(out, result = err_serious(-EOPNOTSUPP));
907 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
909 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
910 "cr_flag=0%o mode=0%06o msg_flag=0x%x\n",
911 PFID(rr->rr_fid1), rr->rr_name,
912 PFID(rr->rr_fid2), create_flags,
913 ma->ma_attr.la_mode, msg_flags);
915 if (msg_flags & MSG_REPLAY ||
916 (req->rq_export->exp_libclient && create_flags&MDS_OPEN_HAS_EA)) {
917 /* This is a replay request or from liblustre with ea. */
918 result = mdt_open_by_fid(info, ldlm_rep);
920 if (result != -ENOENT) {
921 if (req->rq_export->exp_libclient &&
922 create_flags&MDS_OPEN_HAS_EA)
923 GOTO(out, result = 0);
927 * We didn't find the correct object, so we need to re-create it
928 * via a regular replay.
930 if (!(create_flags & MDS_OPEN_CREAT)) {
931 DEBUG_REQ(D_ERROR, req,"OPEN & CREAT not in open replay.");
932 GOTO(out, result = -EFAULT);
934 CDEBUG(D_INFO, "Open replay did find object, continue as "
938 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
939 GOTO(out, result = err_serious(-ENOMEM));
941 mdt_set_disposition(info, ldlm_rep,
942 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
944 if (info->mti_cross_ref) {
945 /* This is cross-ref open */
946 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
947 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
952 lh = &info->mti_lh[MDT_LH_PARENT];
953 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
954 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
956 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
957 MDS_INODELOCK_UPDATE);
959 GOTO(out, result = PTR_ERR(parent));
963 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
965 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
966 lname, child_fid, &info->mti_spec);
967 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
968 "looking for "DFID"/%s, result fid="DFID"\n",
969 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
971 if (result != 0 && result != -ENOENT && result != -ESTALE)
972 GOTO(out_parent, result);
974 if (result == -ENOENT || result == -ESTALE) {
975 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
976 if (result == -ESTALE) {
978 * -ESTALE means the parent is a dead(unlinked) dir, so
979 * it should return -ENOENT to in accordance with the
980 * original mds implementaion.
982 GOTO(out_parent, result = -ENOENT);
984 if (!(create_flags & MDS_OPEN_CREAT))
985 GOTO(out_parent, result);
986 *child_fid = *info->mti_rr.rr_fid2;
987 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
991 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
992 * return FID back in that case.
994 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
997 child = mdt_object_find(info->mti_env, mdt, child_fid);
999 GOTO(out_parent, result = PTR_ERR(child));
1001 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1002 if (result == -ENOENT) {
1003 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1004 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1006 info->mti_mos[0] = parent;
1007 info->mti_mos[1] = child;
1008 result = mdt_version_get_check(info, 0);
1010 GOTO(out_child, result);
1012 /* Let lower layers know what is lock mode on directory. */
1013 info->mti_spec.sp_cr_mode =
1014 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1017 * Do not perform lookup sanity check. We know that name does
1020 info->mti_spec.sp_cr_lookup = 0;
1021 info->mti_spec.sp_feat = &dt_directory_features;
1023 result = mdo_create(info->mti_env,
1024 mdt_object_child(parent),
1026 mdt_object_child(child),
1029 if (result == -ERESTART) {
1030 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1031 GOTO(out_child, result);
1034 GOTO(out_child, result);
1038 /* We have to get attr & lov ea for this object */
1039 result = mo_attr_get(info->mti_env, mdt_object_child(child),
1042 * The object is on remote node, return its FID for remote open.
1044 if (result == -EREMOTE) {
1046 * Check if this lock already was sent to client and
1047 * this is resent case. For resent case do not take lock
1048 * again, use what is already granted.
1050 LASSERT(lhc != NULL);
1052 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1053 struct ldlm_lock *lock;
1055 LASSERT(msg_flags & MSG_RESENT);
1057 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1059 CERROR("Invalid lock handle "LPX64"\n",
1060 lhc->mlh_reg_lh.cookie);
1063 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1064 &lock->l_resource->lr_name));
1065 LDLM_LOCK_PUT(lock);
1068 mdt_lock_handle_init(lhc);
1069 mdt_lock_reg_init(lhc, LCK_PR);
1071 rc = mdt_object_lock(info, child, lhc,
1072 MDS_INODELOCK_LOOKUP,
1075 repbody->fid1 = *mdt_object_fid(child);
1076 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1079 GOTO(out_child, result);
1083 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1085 /* get openlock if this is not replay and if a client requested it */
1086 if (!(msg_flags & MSG_REPLAY) && create_flags & MDS_OPEN_LOCK) {
1090 if (create_flags & FMODE_WRITE)
1092 else if (create_flags & MDS_FMODE_EXEC)
1096 mdt_lock_handle_init(lhc);
1097 mdt_lock_reg_init(lhc, lm);
1098 rc = mdt_object_lock(info, child, lhc,
1099 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1103 GOTO(out_child, result);
1106 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1110 /* Try to open it now. */
1111 rc = mdt_finish_open(info, parent, child, create_flags,
1115 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1116 /* openlock was acquired and mdt_finish_open failed -
1117 drop the openlock */
1118 mdt_object_unlock(info, child, lhc, 1);
1122 ma->ma_cookie_size = 0;
1123 info->mti_no_need_trans = 1;
1124 rc = mdo_unlink(info->mti_env,
1125 mdt_object_child(parent),
1126 mdt_object_child(child),
1130 CERROR("Error in cleanup of open\n");
1135 mdt_object_put(info->mti_env, child);
1137 mdt_object_unlock_put(info, parent, lh, result);
1139 if (result && result != -EREMOTE)
1140 lustre_msg_set_transno(req->rq_repmsg, 0);
1144 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
1145 FMODE_EPOCHLCK)) == FMODE_CLOSED)
1147 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1149 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1152 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1154 struct mdt_object *o = mfd->mfd_object;
1155 struct md_object *next = mdt_object_child(o);
1156 struct md_attr *ma = &info->mti_attr;
1157 int rc = 0, ret = 0;
1161 mode = mfd->mfd_mode;
1163 if ((mode & FMODE_WRITE) || (mode & FMODE_EPOCHLCK)) {
1164 mdt_write_put(info->mti_mdt, o);
1165 ret = mdt_epoch_close(info, o);
1166 } else if (mode & MDS_FMODE_EXEC) {
1167 mdt_write_allow(info->mti_mdt, o);
1168 } else if (mode & FMODE_EPOCH) {
1169 ret = mdt_epoch_close(info, o);
1172 /* Update atime on close only. */
1173 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1174 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1175 /* Set the atime only. */
1176 ma->ma_attr.la_valid = LA_ATIME;
1177 rc = mo_attr_set(info->mti_env, next, ma);
1180 ma->ma_need |= MA_INODE;
1183 if (!MFD_CLOSED(mode))
1184 rc = mo_close(info->mti_env, next, ma);
1185 else if (ret == -EAGAIN)
1186 rc = mo_attr_get(info->mti_env, next, ma);
1188 /* If the object is unlinked, do not try to re-enable SIZEONMDS */
1189 if ((ret == -EAGAIN) && (ma->ma_valid & MA_INODE) &&
1190 (ma->ma_attr.la_nlink == 0)) {
1194 if ((ret == -EAGAIN) || (ret == 1)) {
1195 struct mdt_export_data *med;
1197 /* The epoch has not closed or Size-on-MDS update is needed.
1198 * Put mfd back into the list. */
1199 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1200 mdt_mfd_set_mode(mfd, (ret == 1 ? FMODE_EPOCH : FMODE_SOM));
1202 LASSERT(mdt_info_req(info));
1203 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1204 spin_lock(&med->med_open_lock);
1205 list_add(&mfd->mfd_list, &med->med_open_head);
1206 class_handle_hash_back(&mfd->mfd_handle);
1207 spin_unlock(&med->med_open_lock);
1212 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1213 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1217 mdt_object_put(info->mti_env, o);
1220 RETURN(rc ? rc : ret);
1223 int mdt_close(struct mdt_thread_info *info)
1225 struct mdt_export_data *med;
1226 struct mdt_file_data *mfd;
1227 struct mdt_object *o;
1228 struct md_attr *ma = &info->mti_attr;
1229 struct mdt_body *repbody = NULL;
1230 struct ptlrpc_request *req = mdt_info_req(info);
1234 /* Close may come with the Size-on-MDS update. Unpack it. */
1235 rc = mdt_close_unpack(info);
1237 RETURN(err_serious(rc));
1239 LASSERT(info->mti_epoch);
1241 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1242 info->mti_mdt->mdt_max_mdsize);
1243 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1244 info->mti_mdt->mdt_max_cookiesize);
1245 rc = req_capsule_server_pack(info->mti_pill);
1246 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1248 mdt_shrink_reply(info);
1249 RETURN(lustre_msg_get_status(req->rq_repmsg));
1252 /* Continue to close handle even if we can not pack reply */
1254 repbody = req_capsule_server_get(info->mti_pill,
1256 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1258 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1261 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1263 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1266 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1267 repbody->eadatasize = 0;
1268 repbody->aclsize = 0;
1270 rc = err_serious(rc);
1273 med = &req->rq_export->exp_mdt_data;
1274 spin_lock(&med->med_open_lock);
1275 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1276 if (mdt_mfd_closed(mfd)) {
1277 spin_unlock(&med->med_open_lock);
1278 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1279 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1280 info->mti_epoch->handle.cookie);
1281 rc = err_serious(-ESTALE);
1283 class_handle_unhash(&mfd->mfd_handle);
1284 list_del_init(&mfd->mfd_list);
1285 spin_unlock(&med->med_open_lock);
1287 /* Do not lose object before last unlink. */
1288 o = mfd->mfd_object;
1289 mdt_object_get(info->mti_env, o);
1290 ret = mdt_mfd_close(info, mfd);
1291 if (repbody != NULL)
1292 rc = mdt_handle_last_unlink(info, o, ma);
1293 mdt_empty_transno(info);
1294 mdt_object_put(info->mti_env, o);
1296 if (repbody != NULL)
1297 mdt_shrink_reply(info);
1299 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1300 RETURN(err_serious(-ENOMEM));
1302 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1303 OBD_FAIL_MDS_CLOSE_NET_REP))
1304 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1305 RETURN(rc ? rc : ret);
1308 int mdt_done_writing(struct mdt_thread_info *info)
1310 struct mdt_body *repbody = NULL;
1311 struct mdt_export_data *med;
1312 struct mdt_file_data *mfd;
1316 rc = req_capsule_server_pack(info->mti_pill);
1318 RETURN(err_serious(rc));
1320 repbody = req_capsule_server_get(info->mti_pill,
1322 repbody->eadatasize = 0;
1323 repbody->aclsize = 0;
1325 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1326 rc = mdt_close_unpack(info);
1328 RETURN(err_serious(rc));
1330 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1331 RETURN(lustre_msg_get_status(mdt_info_req(info)->rq_repmsg));
1333 med = &info->mti_exp->exp_mdt_data;
1334 spin_lock(&med->med_open_lock);
1335 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
1337 spin_unlock(&med->med_open_lock);
1338 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1339 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1340 PFID(info->mti_rr.rr_fid1),
1341 info->mti_epoch->handle.cookie,
1342 info->mti_epoch->ioepoch);
1346 LASSERT(mfd->mfd_mode == FMODE_EPOCH ||
1347 mfd->mfd_mode == FMODE_EPOCHLCK);
1348 class_handle_unhash(&mfd->mfd_handle);
1349 list_del_init(&mfd->mfd_list);
1350 spin_unlock(&med->med_open_lock);
1352 /* Set EPOCH CLOSE flag if not set by client. */
1353 info->mti_epoch->flags |= MF_EPOCH_CLOSE;
1354 info->mti_attr.ma_valid = 0;
1355 rc = mdt_mfd_close(info, mfd);
1356 mdt_empty_transno(info);