4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_open.c
38 * Lustre Metadata Target (mdt) open/close file handling
40 * Author: Huang Hua <huanghua@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <lustre_acl.h>
46 #include <lustre_mds.h>
47 #include "mdt_internal.h"
49 /* we do nothing because we do not have refcount now */
50 static void mdt_mfd_get(void *mfdp)
54 static struct portals_handle_ops mfd_handle_ops = {
55 .hop_addref = mdt_mfd_get,
59 /* Create a new mdt_file_data struct, initialize it,
60 * and insert it to global hash table */
61 struct mdt_file_data *mdt_mfd_new(void)
63 struct mdt_file_data *mfd;
68 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
69 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
70 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
76 * Find the mfd pointed to by handle in global hash table.
77 * In case of replay the handle is obsoleted
78 * but mfd can be found in mfd list by that handle
80 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
81 const struct lustre_handle *handle)
83 struct ptlrpc_request *req = mdt_info_req(info);
84 struct mdt_file_data *mfd;
87 LASSERT(handle != NULL);
88 mfd = class_handle2object(handle->cookie);
89 /* during dw/setattr replay the mfd can be found by old handle */
90 if (mfd == NULL && req_is_replay(req)) {
91 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
92 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
93 if (mfd->mfd_old_handle.cookie == handle->cookie)
102 void mdt_mfd_free(struct mdt_file_data *mfd)
104 LASSERT(cfs_list_empty(&mfd->mfd_list));
105 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
108 static int mdt_create_data(struct mdt_thread_info *info,
109 struct mdt_object *p, struct mdt_object *o)
111 struct md_op_spec *spec = &info->mti_spec;
112 struct md_attr *ma = &info->mti_attr;
116 if (!md_should_create(spec->sp_cr_flags))
119 ma->ma_need = MA_INODE | MA_LOV;
121 mutex_lock(&o->mot_lov_mutex);
122 if (!(o->mot_flags & MOF_LOV_CREATED)) {
123 rc = mdo_create_data(info->mti_env,
124 p ? mdt_object_child(p) : NULL,
125 mdt_object_child(o), spec, ma);
127 rc = mdt_attr_get_complex(info, o, ma);
129 if (rc == 0 && ma->ma_valid & MA_LOV)
130 o->mot_flags |= MOF_LOV_CREATED;
132 mutex_unlock(&o->mot_lov_mutex);
136 static int mdt_ioepoch_opened(struct mdt_object *mo)
138 return mo->mot_ioepoch_count;
141 int mdt_object_is_som_enabled(struct mdt_object *mo)
143 return !mo->mot_ioepoch;
147 * Re-enable Size-on-MDS.
148 * Call under ->mot_ioepoch_mutex.
150 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
152 if (ioepoch == mo->mot_ioepoch) {
153 LASSERT(!mdt_ioepoch_opened(mo));
160 * Open the IOEpoch. It is allowed if @writecount is not negative.
161 * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
163 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
166 struct mdt_device *mdt = info->mti_mdt;
171 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
172 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
175 mutex_lock(&o->mot_ioepoch_mutex);
176 if (mdt_ioepoch_opened(o)) {
177 /* Epoch continues even if there is no writers yet. */
178 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
179 o->mot_ioepoch, PFID(mdt_object_fid(o)));
181 /* XXX: ->mdt_ioepoch is not initialized at the mount */
182 spin_lock(&mdt->mdt_ioepoch_lock);
183 if (mdt->mdt_ioepoch < info->mti_replayepoch)
184 mdt->mdt_ioepoch = info->mti_replayepoch;
186 if (info->mti_replayepoch)
187 o->mot_ioepoch = info->mti_replayepoch;
188 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
189 o->mot_ioepoch = ++mdt->mdt_ioepoch;
191 o->mot_ioepoch = mdt->mdt_ioepoch;
193 spin_unlock(&mdt->mdt_ioepoch_lock);
195 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
196 o->mot_ioepoch, PFID(mdt_object_fid(o)));
198 o->mot_flags |= MOF_SOM_CREATED;
201 o->mot_ioepoch_count++;
202 mutex_unlock(&o->mot_ioepoch_mutex);
204 /* Cancel Size-on-MDS attributes cached on clients for the open case.
205 * In the truncate case, see mdt_reint_setattr(). */
206 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
207 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
208 mdt_lock_reg_init(lh, LCK_EX);
209 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
212 mdt_object_unlock(info, o, lh, 1);
218 * Update SOM on-disk attributes.
219 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
220 * mountid and attributes. If disabling, clean SOM xattr.
221 * Call under ->mot_ioepoch_mutex.
223 static int mdt_som_attr_set(struct mdt_thread_info *info,
224 struct mdt_object *obj, __u64 ioepoch, bool enable)
226 struct md_object *next = mdt_object_child(obj);
230 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
231 " on "DFID".\n", enable ? "update" : "disabling",
232 ioepoch, PFID(mdt_object_fid(obj)));
235 struct lu_buf *buf = &info->mti_buf;
236 struct som_attrs *attrs;
237 struct md_attr *ma = &info->mti_attr;
238 struct lu_attr *la = &ma->ma_attr;
239 struct obd_device *obd = info->mti_mdt->mdt_lut.lut_obd;
241 attrs = (struct som_attrs *)info->mti_xattr_buf;
242 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
244 /* pack SOM attributes */
245 memset(attrs, 0, sizeof(*attrs));
246 attrs->som_ioepoch = ioepoch;
247 attrs->som_mountid = obd->u.obt.obt_mount_count;
248 if ((la->la_valid & LA_SIZE) != 0)
249 attrs->som_size = la->la_size;
250 if ((la->la_valid & LA_BLOCKS) != 0)
251 attrs->som_blocks = la->la_blocks;
252 lustre_som_swab(attrs);
254 /* update SOM attributes */
256 buf->lb_len = sizeof(*attrs);
257 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_SOM, 0);
259 /* delete SOM attributes */
260 rc = mo_xattr_del(info->mti_env, next, XATTR_NAME_SOM);
266 /** Perform the eviction specific actions on ioepoch close. */
267 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
268 struct mdt_object *o)
272 mutex_lock(&o->mot_ioepoch_mutex);
273 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
274 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
275 o->mot_ioepoch_count);
276 o->mot_ioepoch_count--;
278 /* If eviction occured set MOF_SOM_RECOV,
279 * if no other epoch holders, disable SOM on disk. */
280 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
281 if (!mdt_ioepoch_opened(o)) {
282 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
283 mdt_object_som_enable(o, o->mot_ioepoch);
285 mutex_unlock(&o->mot_ioepoch_mutex);
290 * Perform the replay specific actions on ioepoch close.
291 * Skip SOM attribute update if obtained and just forget about the inode state
292 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
294 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
295 struct mdt_object *o)
297 int rc = MDT_IOEPOCH_CLOSED;
300 mutex_lock(&o->mot_ioepoch_mutex);
301 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
302 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
303 o->mot_ioepoch_count--;
305 /* Get an info from the replayed request if client is supposed
306 * to send an Attibute Update, reconstruct @rc if so */
307 if (info->mti_ioepoch->flags & MF_SOM_AU)
308 rc = MDT_IOEPOCH_GETATTR;
310 if (!mdt_ioepoch_opened(o))
311 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
312 mutex_unlock(&o->mot_ioepoch_mutex);
318 * Regular file IOepoch close.
319 * Closes the ioepoch, checks the object state, apply obtained attributes and
320 * re-enable SOM on the object, if possible. Also checks if the recovery is
321 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
322 * to obtain SOM attributes under the server-side OST locks.
325 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
326 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
328 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
329 struct mdt_object *o)
331 struct md_attr *tmp_ma;
335 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
338 la = &info->mti_attr.ma_attr;
339 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
341 mutex_lock(&o->mot_ioepoch_mutex);
342 o->mot_ioepoch_count--;
344 tmp_ma = &info->mti_u.som.attr;
345 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
346 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
347 tmp_ma->ma_som = &info->mti_u.som.data;
348 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
349 tmp_ma->ma_valid = 0;
350 rc = mdt_attr_get_complex(info, o, tmp_ma);
354 /* Check the on-disk SOM state. */
355 if (o->mot_flags & MOF_SOM_RECOV)
357 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
358 !(tmp_ma->ma_valid & MA_SOM))
361 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
362 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
364 opened = mdt_ioepoch_opened(o);
366 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
367 * Skip the check for file with no LOV or for unlink files.
369 if (!opened && tmp_ma->ma_valid & MA_LOV &&
370 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
372 /* If some previous writer was evicted, re-ask the
373 * client for attributes. Even if attributes are
374 * provided, we cannot believe in them.
375 * Another use case is that there is no SOM cache on
376 * disk -- first access with SOM or there was an MDS
378 ret = MDT_IOEPOCH_GETATTR;
379 else if (o->mot_flags & MOF_SOM_CHANGE)
380 /* Some previous writer changed the attribute.
381 * Do not believe to the current Size-on-MDS
382 * update, re-ask client. */
383 ret = MDT_IOEPOCH_GETATTR;
384 else if (!(la->la_valid & LA_SIZE) && achange)
385 /* Attributes were changed by the last writer
386 * only but no Size-on-MDS update is received.*/
387 ret = MDT_IOEPOCH_GETATTR;
390 if (achange || ret == MDT_IOEPOCH_GETATTR)
391 o->mot_flags |= MOF_SOM_CHANGE;
393 /* If epoch ends and relable SOM attributes are obtained, update them.
394 * Create SOM ea for new files even if there is no attributes obtained
395 * (0-length file). */
396 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
397 if (achange || o->mot_flags & MOF_SOM_CREATED) {
398 LASSERT(achange || !(la->la_valid & LA_SIZE));
399 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
401 /* Avoid the following setattrs of these attributes,
402 * e.g. for atime update. */
403 info->mti_attr.ma_valid = 0;
405 mdt_object_som_enable(o, o->mot_ioepoch);
408 mutex_unlock(&o->mot_ioepoch_mutex);
409 /* If recovery is needed, tell the client to perform GETATTR under
411 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
412 struct mdt_body *rep;
413 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
414 rep->valid |= OBD_MD_FLGETATTRLOCK;
420 mutex_unlock(&o->mot_ioepoch_mutex);
425 * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
426 * - a client closes the IOEpoch;
427 * - a client eviction occured.
429 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
430 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
431 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
434 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
436 struct ptlrpc_request *req = mdt_info_req(info);
439 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
440 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
443 LASSERT(o->mot_ioepoch_count);
444 LASSERT(info->mti_ioepoch == NULL ||
445 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
447 /* IOEpoch is closed only if client tells about it or eviction occures.
448 * In the replay case, always close the epoch. */
450 RETURN(mdt_ioepoch_close_on_eviction(info, o));
451 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
452 RETURN(mdt_ioepoch_close_on_replay(info, o));
453 if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
454 RETURN(mdt_ioepoch_close_reg(info, o));
455 /* IO epoch is not closed. */
456 RETURN(MDT_IOEPOCH_OPENED);
460 * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
461 * for attribute update. It happens if:
462 * - SOM Attribute Update is obtained;
463 * - the client failed to obtain it and informs MDS about it;
464 * - a client eviction occured.
465 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
468 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
470 struct ptlrpc_request *req = mdt_info_req(info);
472 int act = MDT_SOM_ENABLE;
476 LASSERT(!req || info->mti_ioepoch);
477 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
478 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
481 /* No size whereas MF_SOM_CHANGE is set means client failed to
482 * obtain ost attributes, drop the SOM cache on disk if so. */
484 (info->mti_ioepoch &&
485 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
486 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
487 act = MDT_SOM_DISABLE;
489 mutex_lock(&o->mot_ioepoch_mutex);
490 /* Mark the object it is the recovery state if we failed to obtain
492 if (act == MDT_SOM_DISABLE)
493 o->mot_flags |= MOF_SOM_RECOV;
495 if (!mdt_ioepoch_opened(o)) {
496 ioepoch = info->mti_ioepoch ?
497 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
499 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
500 rc = mdt_som_attr_set(info, o, ioepoch, act);
501 mdt_object_som_enable(o, ioepoch);
503 mutex_unlock(&o->mot_ioepoch_mutex);
507 int mdt_write_read(struct mdt_object *o)
511 mutex_lock(&o->mot_ioepoch_mutex);
512 rc = o->mot_writecount;
513 mutex_unlock(&o->mot_ioepoch_mutex);
517 int mdt_write_get(struct mdt_object *o)
521 mutex_lock(&o->mot_ioepoch_mutex);
522 if (o->mot_writecount < 0)
526 mutex_unlock(&o->mot_ioepoch_mutex);
530 void mdt_write_put(struct mdt_object *o)
533 mutex_lock(&o->mot_ioepoch_mutex);
535 mutex_unlock(&o->mot_ioepoch_mutex);
539 static int mdt_write_deny(struct mdt_object *o)
543 mutex_lock(&o->mot_ioepoch_mutex);
544 if (o->mot_writecount > 0)
548 mutex_unlock(&o->mot_ioepoch_mutex);
552 static void mdt_write_allow(struct mdt_object *o)
555 mutex_lock(&o->mot_ioepoch_mutex);
557 mutex_unlock(&o->mot_ioepoch_mutex);
561 /* there can be no real transaction so prepare the fake one */
562 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
564 struct mdt_device *mdt = info->mti_mdt;
565 struct ptlrpc_request *req = mdt_info_req(info);
566 struct tg_export_data *ted;
567 struct lsd_client_data *lcd;
570 /* transaction has occurred already */
571 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
574 spin_lock(&mdt->mdt_lut.lut_translock);
576 if (info->mti_transno != 0) {
577 struct obd_export *exp = req->rq_export;
579 CERROR("%s: replay trans "LPU64" NID %s: rc = %d\n",
580 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name,
582 libcfs_nid2str(exp->exp_connection->c_peer.nid),
586 } else if (info->mti_transno == 0) {
587 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
589 /* should be replay */
590 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
591 mdt->mdt_lut.lut_last_transno = info->mti_transno;
593 spin_unlock(&mdt->mdt_lut.lut_translock);
595 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
597 req->rq_export->exp_obd->obd_last_committed);
599 req->rq_transno = info->mti_transno;
600 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
602 /* update lcd in memory only for resent cases */
603 ted = &req->rq_export->exp_target_data;
605 mutex_lock(&ted->ted_lcd_lock);
607 if (info->mti_transno < lcd->lcd_last_transno &&
608 info->mti_transno != 0) {
609 /* This should happen during replay. Do not update
610 * last rcvd info if replay req transno < last transno,
611 * otherwise the following resend(after replay) can not
612 * be checked correctly by xid */
613 mutex_unlock(&ted->ted_lcd_lock);
614 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
615 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name,
616 info->mti_transno, lcd->lcd_last_transno);
620 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
621 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
622 if (info->mti_transno != 0)
623 lcd->lcd_last_close_transno = info->mti_transno;
624 lcd->lcd_last_close_xid = req->rq_xid;
625 lcd->lcd_last_close_result = rc;
627 /* VBR: save versions in last_rcvd for reconstruct. */
628 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
630 lcd->lcd_pre_versions[0] = pre_versions[0];
631 lcd->lcd_pre_versions[1] = pre_versions[1];
632 lcd->lcd_pre_versions[2] = pre_versions[2];
633 lcd->lcd_pre_versions[3] = pre_versions[3];
635 if (info->mti_transno != 0)
636 lcd->lcd_last_transno = info->mti_transno;
638 lcd->lcd_last_xid = req->rq_xid;
639 lcd->lcd_last_result = rc;
640 lcd->lcd_last_data = info->mti_opdata;
642 mutex_unlock(&ted->ted_lcd_lock);
647 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
649 LASSERT(mfd != NULL);
651 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
652 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
654 mfd->mfd_mode = mode;
657 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
658 struct mdt_object *o, __u64 flags, int created)
660 struct ptlrpc_request *req = mdt_info_req(info);
661 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
662 struct mdt_file_data *mfd;
663 struct md_attr *ma = &info->mti_attr;
664 struct lu_attr *la = &ma->ma_attr;
665 struct mdt_body *repbody;
666 int rc = 0, isdir, isreg;
669 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
671 isreg = S_ISREG(la->la_mode);
672 isdir = S_ISDIR(la->la_mode);
673 if (isreg && !(ma->ma_valid & MA_LOV)) {
675 * No EA, check whether it is will set regEA and dirEA since in
676 * above attr get, these size might be zero, so reset it, to
677 * retrieve the MD after create obj.
679 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
682 /* in replay case, p == NULL */
683 rc = mdt_create_data(info, p, o);
688 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
689 ma->ma_valid, ma->ma_lmm_size);
691 if (ma->ma_valid & MA_LOV) {
692 LASSERT(ma->ma_lmm_size != 0);
693 repbody->eadatasize = ma->ma_lmm_size;
695 repbody->valid |= OBD_MD_FLDIREA;
697 repbody->valid |= OBD_MD_FLEASIZE;
700 if (flags & FMODE_WRITE) {
701 rc = mdt_write_get(o);
703 mdt_ioepoch_open(info, o, created);
704 repbody->ioepoch = o->mot_ioepoch;
706 } else if (flags & MDS_FMODE_EXEC) {
707 rc = mdt_write_deny(o);
712 rc = mo_open(info->mti_env, mdt_object_child(o),
713 created ? flags | MDS_OPEN_CREATED : flags);
720 * Keep a reference on this object for this open, and is
721 * released by mdt_mfd_close().
723 mdt_object_get(info->mti_env, o);
726 * @flags is always not zero. At least it should be FMODE_READ,
727 * FMODE_WRITE or MDS_FMODE_EXEC.
732 mdt_mfd_set_mode(mfd, flags);
735 mfd->mfd_xid = req->rq_xid;
738 if (req_is_replay(req)) {
739 struct mdt_file_data *old_mfd;
740 /* Check wheather old cookie already exist in
741 * the list, becasue when do recovery, client
742 * might be disconnected from server, and
743 * restart replay, so there maybe some orphan
744 * mfd here, we should remove them */
745 LASSERT(info->mti_rr.rr_handle != NULL);
746 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
748 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
749 "cookie=" LPX64"\n", mfd,
750 PFID(mdt_object_fid(mfd->mfd_object)),
751 info->mti_rr.rr_handle->cookie);
752 spin_lock(&med->med_open_lock);
753 class_handle_unhash(&old_mfd->mfd_handle);
754 cfs_list_del_init(&old_mfd->mfd_list);
755 spin_unlock(&med->med_open_lock);
756 /* no attr update for that close */
758 ma->ma_valid |= MA_FLAGS;
759 ma->ma_attr_flags |= MDS_RECOV_OPEN;
760 mdt_mfd_close(info, old_mfd);
761 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
762 ma->ma_valid &= ~MA_FLAGS;
764 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
765 info->mti_rr.rr_handle->cookie);
766 mfd->mfd_old_handle.cookie =
767 info->mti_rr.rr_handle->cookie;
769 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
771 if (req->rq_export->exp_disconnected) {
772 spin_lock(&med->med_open_lock);
773 class_handle_unhash(&mfd->mfd_handle);
774 cfs_list_del_init(&mfd->mfd_list);
775 spin_unlock(&med->med_open_lock);
776 mdt_mfd_close(info, mfd);
778 spin_lock(&med->med_open_lock);
779 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
780 spin_unlock(&med->med_open_lock);
783 mdt_empty_transno(info, rc);
785 GOTO(err_out, rc = -ENOMEM);
791 if (flags & FMODE_WRITE)
792 /* XXX We also need to close io epoch here.
793 * See LU-1220 - green */
795 else if (flags & FMODE_EXEC)
800 int mdt_finish_open(struct mdt_thread_info *info,
801 struct mdt_object *p, struct mdt_object *o,
802 __u64 flags, int created, struct ldlm_reply *rep)
804 struct ptlrpc_request *req = mdt_info_req(info);
805 struct obd_export *exp = req->rq_export;
806 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
807 struct md_attr *ma = &info->mti_attr;
808 struct lu_attr *la = &ma->ma_attr;
809 struct mdt_file_data *mfd;
810 struct mdt_body *repbody;
812 int isreg, isdir, islnk;
816 LASSERT(ma->ma_valid & MA_INODE);
818 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
820 isreg = S_ISREG(la->la_mode);
821 isdir = S_ISDIR(la->la_mode);
822 islnk = S_ISLNK(la->la_mode);
823 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
825 /* LU-2275, simulate broken behaviour (esp. prevalent in
826 * pre-2.4 servers where a very strange reply is sent on error
827 * that looks like it was actually almost succesful and a failure at the
829 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
830 mdt_set_disposition(info, rep, DISP_OPEN_LOCK | \
831 DISP_OPEN_OPEN | DISP_LOOKUP_NEG | \
836 if (exp_connect_rmtclient(exp)) {
837 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
839 rc = mdt_pack_remote_perm(info, o, buf);
841 repbody->valid &= ~OBD_MD_FLRMTPERM;
842 repbody->aclsize = 0;
844 repbody->valid |= OBD_MD_FLRMTPERM;
845 repbody->aclsize = sizeof(struct mdt_remote_perm);
848 #ifdef CONFIG_FS_POSIX_ACL
849 else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
850 const struct lu_env *env = info->mti_env;
851 struct md_object *next = mdt_object_child(o);
852 struct lu_buf *buf = &info->mti_buf;
854 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
855 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
857 if (buf->lb_len > 0) {
858 rc = mo_xattr_get(env, next, buf,
859 XATTR_NAME_ACL_ACCESS);
861 if (rc == -ENODATA) {
862 repbody->aclsize = 0;
863 repbody->valid |= OBD_MD_FLACL;
865 } else if (rc == -EOPNOTSUPP) {
868 CERROR("got acl size: %d\n", rc);
871 repbody->aclsize = rc;
872 repbody->valid |= OBD_MD_FLACL;
879 if (info->mti_mdt->mdt_opts.mo_mds_capa &&
880 exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
881 struct lustre_capa *capa;
883 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
885 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
886 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
889 repbody->valid |= OBD_MD_FLMDSCAPA;
891 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
892 exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
893 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
894 struct lustre_capa *capa;
896 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
898 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
899 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
902 repbody->valid |= OBD_MD_FLOSSCAPA;
906 * If we are following a symlink, don't open; and do not return open
907 * handle for special nodes as client required.
909 if (islnk || (!isreg && !isdir &&
910 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
911 lustre_msg_set_transno(req->rq_repmsg, 0);
915 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
918 * We need to return the existing object's fid back, so it is done here,
919 * after preparing the reply.
921 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
924 /* This can't be done earlier, we need to return reply body */
926 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
927 /* We are trying to create or write an existing dir. */
930 } else if (flags & MDS_OPEN_DIRECTORY)
933 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
934 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
939 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
940 spin_lock(&med->med_open_lock);
941 cfs_list_for_each(t, &med->med_open_head) {
942 mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
943 if (mfd->mfd_xid == req->rq_xid)
947 spin_unlock(&med->med_open_lock);
950 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
951 /*set repbody->ea_size for resent case*/
952 if (ma->ma_valid & MA_LOV) {
953 LASSERT(ma->ma_lmm_size != 0);
954 repbody->eadatasize = ma->ma_lmm_size;
956 repbody->valid |= OBD_MD_FLDIREA;
958 repbody->valid |= OBD_MD_FLEASIZE;
964 rc = mdt_mfd_open(info, p, o, flags, created);
968 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
969 struct lsd_client_data *lcd);
971 void mdt_reconstruct_open(struct mdt_thread_info *info,
972 struct mdt_lock_handle *lhc)
974 const struct lu_env *env = info->mti_env;
975 struct mdt_device *mdt = info->mti_mdt;
976 struct req_capsule *pill = info->mti_pill;
977 struct ptlrpc_request *req = mdt_info_req(info);
978 struct tg_export_data *ted = &req->rq_export->exp_target_data;
979 struct lsd_client_data *lcd = ted->ted_lcd;
980 struct md_attr *ma = &info->mti_attr;
981 struct mdt_reint_record *rr = &info->mti_rr;
982 __u32 flags = info->mti_spec.sp_cr_flags;
983 struct ldlm_reply *ldlm_rep;
984 struct mdt_object *parent;
985 struct mdt_object *child;
986 struct mdt_body *repbody;
990 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
991 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
992 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
994 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
995 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
997 ma->ma_need = MA_INODE;
998 if (ma->ma_lmm_size > 0)
999 ma->ma_need |= MA_LOV;
1003 mdt_req_from_lcd(req, lcd);
1004 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1006 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1007 ldlm_rep->lock_policy_res1, req->rq_status);
1009 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1010 req->rq_status != 0)
1011 /* We did not create successfully, return error to client. */
1012 GOTO(out, rc = req->rq_status);
1014 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1015 struct obd_export *exp = req->rq_export;
1017 * We failed after creation, but we do not know in which step
1018 * we failed. So try to check the child object.
1020 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1021 if (IS_ERR(parent)) {
1022 rc = PTR_ERR(parent);
1023 LCONSOLE_WARN("Parent "DFID" lookup error %d."
1024 " Evicting client %s with export %s.\n",
1025 PFID(rr->rr_fid1), rc,
1026 obd_uuid2str(&exp->exp_client_uuid),
1027 obd_export_nid2str(exp));
1028 mdt_export_evict(exp);
1031 child = mdt_object_find(env, mdt, rr->rr_fid2);
1032 if (IS_ERR(child)) {
1033 rc = PTR_ERR(child);
1034 LCONSOLE_WARN("Child "DFID" lookup error %d."
1035 " Evicting client %s with export %s.\n",
1036 PFID(mdt_object_fid(child)), rc,
1037 obd_uuid2str(&exp->exp_client_uuid),
1038 obd_export_nid2str(exp));
1039 mdt_object_put(env, parent);
1040 mdt_export_evict(exp);
1043 rc = mdt_object_exists(child);
1046 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1047 rc = mdt_attr_get_complex(info, child, ma);
1049 rc = mdt_finish_open(info, parent, child,
1050 flags, 1, ldlm_rep);
1051 } else if (rc < 0) {
1052 /* the child object was created on remote server */
1053 repbody->fid1 = *rr->rr_fid2;
1054 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1056 } else if (rc == 0) {
1057 /* the child does not exist, we should do regular open */
1058 mdt_object_put(env, parent);
1059 mdt_object_put(env, child);
1060 GOTO(regular_open, 0);
1062 mdt_object_put(env, parent);
1063 mdt_object_put(env, child);
1067 /* We did not try to create, so we are a pure open */
1068 rc = mdt_reint_open(info, lhc);
1073 req->rq_status = rc;
1074 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1075 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1078 int mdt_open_by_fid(struct mdt_thread_info* info,
1079 struct ldlm_reply *rep)
1081 __u32 flags = info->mti_spec.sp_cr_flags;
1082 struct mdt_reint_record *rr = &info->mti_rr;
1083 struct md_attr *ma = &info->mti_attr;
1084 struct mdt_object *o;
1088 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1090 RETURN(rc = PTR_ERR(o));
1092 rc = mdt_object_exists(o);
1094 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1098 rc = mdt_attr_get_complex(info, o, ma);
1100 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1101 } else if (rc == 0) {
1104 /* the child object was created on remote server */
1105 struct mdt_body *repbody;
1106 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1107 repbody->fid1 = *rr->rr_fid2;
1108 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1112 mdt_object_put(info->mti_env, o);
1116 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1117 struct mdt_lock_handle *lhc)
1119 const struct lu_env *env = info->mti_env;
1120 struct mdt_device *mdt = info->mti_mdt;
1121 __u32 flags = info->mti_spec.sp_cr_flags;
1122 struct mdt_reint_record *rr = &info->mti_rr;
1123 struct md_attr *ma = &info->mti_attr;
1124 struct mdt_object *parent= NULL;
1125 struct mdt_object *o;
1130 if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1131 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1132 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1133 if (IS_ERR(parent)) {
1134 CDEBUG(D_INODE, "Fail to find parent "DFID
1135 " for anonymous created %ld, try to"
1136 " use server-side parent.\n",
1137 PFID(rr->rr_fid1), PTR_ERR(parent));
1142 ma->ma_need |= MA_PFID;
1145 o = mdt_object_find(env, mdt, rr->rr_fid2);
1147 RETURN(rc = PTR_ERR(o));
1149 rc = mdt_object_exists(o);
1151 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1153 GOTO(out, rc = -ENOENT);
1154 } else if (rc < 0) {
1155 CERROR("NFS remote open shouldn't happen.\n");
1158 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1162 if (flags & FMODE_WRITE)
1164 else if (flags & MDS_FMODE_EXEC)
1169 mdt_lock_handle_init(lhc);
1170 mdt_lock_reg_init(lhc, lm);
1171 rc = mdt_object_lock(info, o, lhc,
1172 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1177 rc = mdt_attr_get_complex(info, o, ma);
1181 if (ma->ma_valid & MA_PFID) {
1182 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1183 if (IS_ERR(parent)) {
1184 CDEBUG(D_INODE, "Fail to find parent "DFID
1185 " for anonymous created %ld, try to"
1186 " use system default.\n",
1187 PFID(&ma->ma_pfid), PTR_ERR(parent));
1192 if (flags & MDS_OPEN_LOCK)
1193 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1194 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1196 if (!(flags & MDS_OPEN_LOCK) || rc)
1197 mdt_object_unlock(info, o, lhc, 1);
1201 mdt_object_put(env, o);
1203 mdt_object_put(env, parent);
1207 int mdt_pin(struct mdt_thread_info* info)
1210 RETURN(err_serious(-EOPNOTSUPP));
1213 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1214 int mdt_cross_open(struct mdt_thread_info* info,
1215 const struct lu_fid *fid,
1216 struct ldlm_reply *rep, __u32 flags)
1218 struct md_attr *ma = &info->mti_attr;
1219 struct mdt_object *o;
1223 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1225 RETURN(rc = PTR_ERR(o));
1227 rc = mdt_object_exists(o);
1229 /* Do permission check for cross-open. */
1230 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1231 NULL, flags | MDS_OPEN_CROSS);
1235 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1236 rc = mdt_attr_get_complex(info, o, ma);
1238 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1239 } else if (rc == 0) {
1241 * Something is wrong here. lookup was positive but there is
1244 CERROR("Cross-ref object doesn't exist!\n");
1247 /* Something is wrong here, the object is on another MDS! */
1248 CERROR("The object isn't on this server! FLD error?\n");
1249 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1251 "Object isn't on this server! FLD error?\n");
1257 mdt_object_put(info->mti_env, o);
1261 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1263 struct mdt_device *mdt = info->mti_mdt;
1264 struct ptlrpc_request *req = mdt_info_req(info);
1265 struct mdt_object *parent;
1266 struct mdt_object *child;
1267 struct mdt_lock_handle *lh;
1268 struct ldlm_reply *ldlm_rep;
1269 struct mdt_body *repbody;
1270 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1271 struct md_attr *ma = &info->mti_attr;
1272 __u64 create_flags = info->mti_spec.sp_cr_flags;
1273 struct mdt_reint_record *rr = &info->mti_rr;
1274 struct lu_name *lname;
1280 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1281 (obd_timeout + 1) / 4);
1283 mdt_counter_incr(req, LPROC_MDT_OPEN);
1284 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1286 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1287 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1289 ma->ma_need = MA_INODE;
1290 if (ma->ma_lmm_size > 0)
1291 ma->ma_need |= MA_LOV;
1295 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1296 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1298 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1299 CERROR("file join is not supported anymore.\n");
1300 GOTO(out, result = err_serious(-EOPNOTSUPP));
1302 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1304 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1305 info->mti_spec.u.sp_ea.eadata == NULL)
1306 GOTO(out, result = err_serious(-EINVAL));
1308 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1309 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1310 PFID(rr->rr_fid1), rr->rr_name,
1311 PFID(rr->rr_fid2), create_flags,
1312 ma->ma_attr.la_mode, msg_flags);
1314 if (req_is_replay(req) ||
1315 (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1316 /* This is a replay request or from liblustre with ea. */
1317 result = mdt_open_by_fid(info, ldlm_rep);
1319 if (result != -ENOENT) {
1320 if (req->rq_export->exp_libclient &&
1321 create_flags & MDS_OPEN_HAS_EA)
1322 GOTO(out, result = 0);
1325 /* We didn't find the correct object, so we need to re-create it
1326 * via a regular replay. */
1327 if (!(create_flags & MDS_OPEN_CREAT)) {
1328 DEBUG_REQ(D_ERROR, req,
1329 "OPEN & CREAT not in open replay/by_fid.");
1330 GOTO(out, result = -EFAULT);
1332 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1333 } else if ((rr->rr_namelen == 0 && !info->mti_cross_ref &&
1334 create_flags & MDS_OPEN_LOCK) ||
1335 (create_flags & MDS_OPEN_BY_FID)) {
1336 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1337 if (result != -ENOENT && !(create_flags & MDS_OPEN_CREAT))
1339 if (unlikely(rr->rr_namelen == 0))
1340 GOTO(out, result = -EINVAL);
1341 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1344 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1345 GOTO(out, result = err_serious(-ENOMEM));
1347 mdt_set_disposition(info, ldlm_rep,
1348 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1350 if (info->mti_cross_ref) {
1351 /* This is cross-ref open */
1352 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1353 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1358 lh = &info->mti_lh[MDT_LH_PARENT];
1359 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1360 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1362 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1363 MDS_INODELOCK_UPDATE);
1365 GOTO(out, result = PTR_ERR(parent));
1367 /* get and check version of parent */
1368 result = mdt_version_get_check(info, parent, 0);
1370 GOTO(out_parent, result);
1372 fid_zero(child_fid);
1374 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1375 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1376 lname, child_fid, &info->mti_spec);
1377 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1378 "looking for "DFID"/%s, result fid="DFID"\n",
1379 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1381 if (result != 0 && result != -ENOENT && result != -ESTALE)
1382 GOTO(out_parent, result);
1384 if (result == -ENOENT || result == -ESTALE) {
1385 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1386 if (result == -ESTALE) {
1388 * -ESTALE means the parent is a dead(unlinked) dir, so
1389 * it should return -ENOENT to in accordance with the
1390 * original mds implementaion.
1392 GOTO(out_parent, result = -ENOENT);
1394 if (!(create_flags & MDS_OPEN_CREAT))
1395 GOTO(out_parent, result);
1396 *child_fid = *info->mti_rr.rr_fid2;
1397 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1399 /* In the function below, .hs_keycmp resolves to
1400 * lu_obj_hop_keycmp() */
1401 /* coverity[overrun-buffer-val] */
1402 child = mdt_object_new(info->mti_env, mdt, child_fid);
1405 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1406 * return FID back in that case.
1408 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1409 child = mdt_object_find(info->mti_env, mdt, child_fid);
1412 GOTO(out_parent, result = PTR_ERR(child));
1414 /** check version of child */
1415 rc = mdt_version_get_check(info, child, 1);
1417 GOTO(out_child, result = rc);
1419 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1420 if (result == -ENOENT) {
1421 if (mdt_object_obf(parent))
1422 GOTO(out_child, result = -EPERM);
1424 /* save versions in reply */
1425 mdt_version_get_save(info, parent, 0);
1426 mdt_version_get_save(info, child, 1);
1428 /* version of child will be changed */
1429 info->mti_mos = child;
1431 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1432 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1434 /* Let lower layers know what is lock mode on directory. */
1435 info->mti_spec.sp_cr_mode =
1436 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1439 * Do not perform lookup sanity check. We know that name does
1442 info->mti_spec.sp_cr_lookup = 0;
1443 info->mti_spec.sp_feat = &dt_directory_features;
1445 result = mdo_create(info->mti_env,
1446 mdt_object_child(parent),
1448 mdt_object_child(child),
1451 if (result == -ERESTART) {
1452 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1453 GOTO(out_child, result);
1456 /* XXX: we should call this once, see few lines below */
1458 result = mdt_attr_get_complex(info, child, ma);
1461 GOTO(out_child, result);
1465 /* We have to get attr & lov ea for this object */
1466 result = mdt_attr_get_complex(info, child, ma);
1468 * The object is on remote node, return its FID for remote open.
1470 if (result == -EREMOTE) {
1472 * Check if this lock already was sent to client and
1473 * this is resent case. For resent case do not take lock
1474 * again, use what is already granted.
1476 LASSERT(lhc != NULL);
1478 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1479 struct ldlm_lock *lock;
1481 LASSERT(msg_flags & MSG_RESENT);
1483 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1485 CERROR("Invalid lock handle "LPX64"\n",
1486 lhc->mlh_reg_lh.cookie);
1489 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1490 &lock->l_resource->lr_name));
1491 LDLM_LOCK_PUT(lock);
1494 mdt_lock_handle_init(lhc);
1495 mdt_lock_reg_init(lhc, LCK_PR);
1497 rc = mdt_object_lock(info, child, lhc,
1498 MDS_INODELOCK_LOOKUP,
1501 repbody->fid1 = *mdt_object_fid(child);
1502 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1505 GOTO(out_child, result);
1509 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1511 /* get openlock if this is not replay and if a client requested it */
1512 if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
1515 if (create_flags & FMODE_WRITE)
1517 else if (create_flags & MDS_FMODE_EXEC)
1521 mdt_lock_handle_init(lhc);
1522 mdt_lock_reg_init(lhc, lm);
1523 rc = mdt_object_lock(info, child, lhc,
1524 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1528 GOTO(out_child, result);
1531 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1535 /* Try to open it now. */
1536 rc = mdt_finish_open(info, parent, child, create_flags,
1540 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1541 /* openlock was acquired and mdt_finish_open failed -
1542 drop the openlock */
1543 mdt_object_unlock(info, child, lhc, 1);
1547 ma->ma_cookie_size = 0;
1548 rc = mdo_unlink(info->mti_env,
1549 mdt_object_child(parent),
1550 mdt_object_child(child),
1554 CERROR("Error in cleanup of open\n");
1559 mdt_object_put(info->mti_env, child);
1561 mdt_object_unlock_put(info, parent, lh, result || !created);
1563 if (result && result != -EREMOTE)
1564 lustre_msg_set_transno(req->rq_repmsg, 0);
1568 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1569 MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1571 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1573 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1576 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1578 struct mdt_object *o = mfd->mfd_object;
1579 struct md_object *next = mdt_object_child(o);
1580 struct md_attr *ma = &info->mti_attr;
1581 int ret = MDT_IOEPOCH_CLOSED;
1586 mode = mfd->mfd_mode;
1588 if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1590 ret = mdt_ioepoch_close(info, o);
1591 } else if (mode & MDS_FMODE_EXEC) {
1593 } else if (mode & MDS_FMODE_EPOCH) {
1594 ret = mdt_ioepoch_close(info, o);
1595 } else if (mode & MDS_FMODE_SOM) {
1596 ret = mdt_som_au_close(info, o);
1599 /* Update atime on close only. */
1600 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1601 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1602 /* Set the atime only. */
1603 ma->ma_valid = MA_INODE;
1604 ma->ma_attr.la_valid = LA_ATIME;
1605 rc = mo_attr_set(info->mti_env, next, ma);
1608 ma->ma_need |= MA_INODE;
1609 ma->ma_valid &= ~MA_INODE;
1611 if (!MFD_CLOSED(mode))
1612 rc = mo_close(info->mti_env, next, ma, mode);
1614 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1615 struct mdt_export_data *med;
1617 /* The IOepoch is still opened or SOM update is needed.
1618 * Put mfd back into the list. */
1619 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1620 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1621 MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1623 LASSERT(mdt_info_req(info));
1624 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1625 spin_lock(&med->med_open_lock);
1626 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1627 class_handle_hash_back(&mfd->mfd_handle);
1628 spin_unlock(&med->med_open_lock);
1630 if (ret == MDT_IOEPOCH_OPENED) {
1634 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1635 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1639 mdt_object_put(info->mti_env, o);
1642 RETURN(rc ? rc : ret);
1645 int mdt_close(struct mdt_thread_info *info)
1647 struct mdt_export_data *med;
1648 struct mdt_file_data *mfd;
1649 struct mdt_object *o;
1650 struct md_attr *ma = &info->mti_attr;
1651 struct mdt_body *repbody = NULL;
1652 struct ptlrpc_request *req = mdt_info_req(info);
1656 mdt_counter_incr(req, LPROC_MDT_CLOSE);
1657 /* Close may come with the Size-on-MDS update. Unpack it. */
1658 rc = mdt_close_unpack(info);
1660 RETURN(err_serious(rc));
1662 LASSERT(info->mti_ioepoch);
1664 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1665 info->mti_mdt->mdt_max_mdsize);
1666 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1667 info->mti_mdt->mdt_max_cookiesize);
1668 rc = req_capsule_server_pack(info->mti_pill);
1669 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1670 mdt_client_compatibility(info);
1672 mdt_fix_reply(info);
1673 mdt_exit_ucred(info);
1674 RETURN(lustre_msg_get_status(req->rq_repmsg));
1677 /* Continue to close handle even if we can not pack reply */
1679 repbody = req_capsule_server_get(info->mti_pill,
1681 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1683 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1686 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1688 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1691 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1692 repbody->eadatasize = 0;
1693 repbody->aclsize = 0;
1695 rc = err_serious(rc);
1698 med = &req->rq_export->exp_mdt_data;
1699 spin_lock(&med->med_open_lock);
1700 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1701 if (mdt_mfd_closed(mfd)) {
1702 spin_unlock(&med->med_open_lock);
1703 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1704 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1705 info->mti_ioepoch->handle.cookie);
1706 /** not serious error since bug 3633 */
1709 class_handle_unhash(&mfd->mfd_handle);
1710 cfs_list_del_init(&mfd->mfd_list);
1711 spin_unlock(&med->med_open_lock);
1713 /* Do not lose object before last unlink. */
1714 o = mfd->mfd_object;
1715 mdt_object_get(info->mti_env, o);
1716 ret = mdt_mfd_close(info, mfd);
1717 if (repbody != NULL)
1718 rc = mdt_handle_last_unlink(info, o, ma);
1719 mdt_empty_transno(info, rc);
1720 mdt_object_put(info->mti_env, o);
1722 if (repbody != NULL) {
1723 mdt_client_compatibility(info);
1724 rc = mdt_fix_reply(info);
1727 mdt_exit_ucred(info);
1728 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1729 RETURN(err_serious(-ENOMEM));
1731 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1732 OBD_FAIL_MDS_CLOSE_NET_REP))
1733 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1734 RETURN(rc ? rc : ret);
1738 * DONE_WRITING rpc handler.
1740 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1741 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1742 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1743 * skip attributes and reconstruct the reply here.
1745 int mdt_done_writing(struct mdt_thread_info *info)
1747 struct ptlrpc_request *req = mdt_info_req(info);
1748 struct mdt_body *repbody = NULL;
1749 struct mdt_export_data *med;
1750 struct mdt_file_data *mfd;
1754 rc = req_capsule_server_pack(info->mti_pill);
1756 RETURN(err_serious(rc));
1758 repbody = req_capsule_server_get(info->mti_pill,
1760 repbody->eadatasize = 0;
1761 repbody->aclsize = 0;
1763 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1764 rc = mdt_close_unpack(info);
1766 RETURN(err_serious(rc));
1768 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1769 mdt_exit_ucred(info);
1770 RETURN(lustre_msg_get_status(req->rq_repmsg));
1773 med = &info->mti_exp->exp_mdt_data;
1774 spin_lock(&med->med_open_lock);
1775 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1777 spin_unlock(&med->med_open_lock);
1778 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1779 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1780 PFID(info->mti_rr.rr_fid1),
1781 info->mti_ioepoch->handle.cookie,
1782 info->mti_ioepoch->ioepoch);
1783 /* If this is a replay, reconstruct the transno. */
1784 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1785 rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1787 mdt_empty_transno(info, rc);
1790 GOTO(error_ucred, rc);
1793 LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1794 mfd->mfd_mode == MDS_FMODE_TRUNC);
1795 class_handle_unhash(&mfd->mfd_handle);
1796 cfs_list_del_init(&mfd->mfd_list);
1797 spin_unlock(&med->med_open_lock);
1799 /* Set EPOCH CLOSE flag if not set by client. */
1800 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1801 info->mti_attr.ma_valid = 0;
1803 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1804 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1805 if (info->mti_attr.ma_lmm == NULL)
1806 GOTO(error_ucred, rc = -ENOMEM);
1808 rc = mdt_mfd_close(info, mfd);
1810 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1811 mdt_empty_transno(info, rc);
1813 mdt_exit_ucred(info);