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 cfs_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 cfs_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 cfs_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 cfs_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 cfs_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 cfs_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, zero IOEpoch id in lustre-ea.
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, int enable)
226 struct lustre_mdt_attrs *lma;
227 struct md_attr *ma = &info->mti_attr;
228 struct lu_buf *buf = &info->mti_buf;
229 struct md_object *next = mdt_object_child(obj);
230 struct mdt_device *mdt = info->mti_mdt;
231 struct lu_attr *la = &ma->ma_attr;
235 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
236 " on "DFID".\n", enable ? "update" : "disabling",
237 ioepoch, PFID(mdt_object_fid(obj)));
239 lma = (struct lustre_mdt_attrs *) info->mti_xattr_buf;
240 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*lma));
243 buf->lb_len = sizeof(info->mti_xattr_buf);
244 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LMA);
246 lustre_lma_swab(lma);
247 } else if (rc == -ENODATA) {
248 memset(lma, 0, sizeof(*lma));
254 memcpy(&lma->lma_self_fid, mdt_object_fid(obj), sizeof(lma->lma_self_fid));
257 lma->lma_ioepoch = ioepoch;
258 lma->lma_som_size = la->la_valid & LA_SIZE ? la->la_size : 0;
259 lma->lma_som_blocks = la->la_valid & LA_BLOCKS ? la->la_blocks : 0;
260 lma->lma_som_mountid = mdt->mdt_lut.lut_obd->u.obt.obt_mount_count;
262 lma->lma_compat |= LMAC_SOM;
264 lma->lma_compat &= ~LMAC_SOM;
266 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_LMA, 0);
271 /** Perform the eviction specific actions on ioepoch close. */
272 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
273 struct mdt_object *o)
277 cfs_mutex_lock(&o->mot_ioepoch_mutex);
278 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
279 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
280 o->mot_ioepoch_count);
281 o->mot_ioepoch_count--;
283 /* If eviction occured set MOF_SOM_RECOV,
284 * if no other epoch holders, disable SOM on disk. */
285 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
286 if (!mdt_ioepoch_opened(o)) {
287 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
288 mdt_object_som_enable(o, o->mot_ioepoch);
290 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
295 * Perform the replay specific actions on ioepoch close.
296 * Skip SOM attribute update if obtained and just forget about the inode state
297 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
299 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
300 struct mdt_object *o)
302 int rc = MDT_IOEPOCH_CLOSED;
305 cfs_mutex_lock(&o->mot_ioepoch_mutex);
306 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
307 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
308 o->mot_ioepoch_count--;
310 /* Get an info from the replayed request if client is supposed
311 * to send an Attibute Update, reconstruct @rc if so */
312 if (info->mti_ioepoch->flags & MF_SOM_AU)
313 rc = MDT_IOEPOCH_GETATTR;
315 if (!mdt_ioepoch_opened(o))
316 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
317 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
323 * Regular file IOepoch close.
324 * Closes the ioepoch, checks the object state, apply obtained attributes and
325 * re-enable SOM on the object, if possible. Also checks if the recovery is
326 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
327 * to obtain SOM attributes under the server-side OST locks.
330 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
331 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
333 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
334 struct mdt_object *o)
336 struct md_attr *tmp_ma;
340 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
343 la = &info->mti_attr.ma_attr;
344 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
346 cfs_mutex_lock(&o->mot_ioepoch_mutex);
347 o->mot_ioepoch_count--;
349 tmp_ma = &info->mti_u.som.attr;
350 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
351 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
352 tmp_ma->ma_som = &info->mti_u.som.data;
353 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
354 tmp_ma->ma_valid = 0;
355 rc = mdt_attr_get_complex(info, o, tmp_ma);
359 /* Check the on-disk SOM state. */
360 if (o->mot_flags & MOF_SOM_RECOV)
362 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
363 !(tmp_ma->ma_valid & MA_SOM))
366 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
367 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
369 opened = mdt_ioepoch_opened(o);
371 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
372 * Skip the check for file with no LOV or for unlink files.
374 if (!opened && tmp_ma->ma_valid & MA_LOV &&
375 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
377 /* If some previous writer was evicted, re-ask the
378 * client for attributes. Even if attributes are
379 * provided, we cannot believe in them.
380 * Another use case is that there is no SOM cache on
381 * disk -- first access with SOM or there was an MDS
383 ret = MDT_IOEPOCH_GETATTR;
384 else if (o->mot_flags & MOF_SOM_CHANGE)
385 /* Some previous writer changed the attribute.
386 * Do not believe to the current Size-on-MDS
387 * update, re-ask client. */
388 ret = MDT_IOEPOCH_GETATTR;
389 else if (!(la->la_valid & LA_SIZE) && achange)
390 /* Attributes were changed by the last writer
391 * only but no Size-on-MDS update is received.*/
392 ret = MDT_IOEPOCH_GETATTR;
395 if (achange || ret == MDT_IOEPOCH_GETATTR)
396 o->mot_flags |= MOF_SOM_CHANGE;
398 /* If epoch ends and relable SOM attributes are obtained, update them.
399 * Create SOM ea for new files even if there is no attributes obtained
400 * (0-length file). */
401 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
402 if (achange || o->mot_flags & MOF_SOM_CREATED) {
403 LASSERT(achange || !(la->la_valid & LA_SIZE));
404 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
406 /* Avoid the following setattrs of these attributes,
407 * e.g. for atime update. */
408 info->mti_attr.ma_valid = 0;
410 mdt_object_som_enable(o, o->mot_ioepoch);
413 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
414 /* If recovery is needed, tell the client to perform GETATTR under
416 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
417 struct mdt_body *rep;
418 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
419 rep->valid |= OBD_MD_FLGETATTRLOCK;
425 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
430 * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
431 * - a client closes the IOEpoch;
432 * - a client eviction occured.
434 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
435 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
436 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
439 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
441 struct ptlrpc_request *req = mdt_info_req(info);
444 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
445 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
448 LASSERT(o->mot_ioepoch_count);
449 LASSERT(info->mti_ioepoch == NULL ||
450 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
452 /* IOEpoch is closed only if client tells about it or eviction occures.
453 * In the replay case, always close the epoch. */
455 RETURN(mdt_ioepoch_close_on_eviction(info, o));
456 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
457 RETURN(mdt_ioepoch_close_on_replay(info, o));
458 if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
459 RETURN(mdt_ioepoch_close_reg(info, o));
460 /* IO epoch is not closed. */
461 RETURN(MDT_IOEPOCH_OPENED);
465 * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
466 * for attribute update. It happens if:
467 * - SOM Attribute Update is obtained;
468 * - the client failed to obtain it and informs MDS about it;
469 * - a client eviction occured.
470 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
473 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
475 struct ptlrpc_request *req = mdt_info_req(info);
477 int act = MDT_SOM_ENABLE;
481 LASSERT(!req || info->mti_ioepoch);
482 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
483 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
486 /* No size whereas MF_SOM_CHANGE is set means client failed to
487 * obtain ost attributes, drop the SOM cache on disk if so. */
489 (info->mti_ioepoch &&
490 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
491 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
492 act = MDT_SOM_DISABLE;
494 cfs_mutex_lock(&o->mot_ioepoch_mutex);
495 /* Mark the object it is the recovery state if we failed to obtain
497 if (act == MDT_SOM_DISABLE)
498 o->mot_flags |= MOF_SOM_RECOV;
500 if (!mdt_ioepoch_opened(o)) {
501 ioepoch = info->mti_ioepoch ?
502 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
504 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
505 rc = mdt_som_attr_set(info, o, ioepoch, act);
506 mdt_object_som_enable(o, ioepoch);
508 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
512 int mdt_write_read(struct mdt_object *o)
516 cfs_mutex_lock(&o->mot_ioepoch_mutex);
517 rc = o->mot_writecount;
518 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
522 int mdt_write_get(struct mdt_object *o)
526 cfs_mutex_lock(&o->mot_ioepoch_mutex);
527 if (o->mot_writecount < 0)
531 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
535 void mdt_write_put(struct mdt_object *o)
538 cfs_mutex_lock(&o->mot_ioepoch_mutex);
540 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
544 static int mdt_write_deny(struct mdt_object *o)
548 cfs_mutex_lock(&o->mot_ioepoch_mutex);
549 if (o->mot_writecount > 0)
553 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
557 static void mdt_write_allow(struct mdt_object *o)
560 cfs_mutex_lock(&o->mot_ioepoch_mutex);
562 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
566 /* there can be no real transaction so prepare the fake one */
567 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
569 struct mdt_device *mdt = info->mti_mdt;
570 struct ptlrpc_request *req = mdt_info_req(info);
571 struct tg_export_data *ted;
572 struct lsd_client_data *lcd;
575 /* transaction has occurred already */
576 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
579 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
580 if (info->mti_transno == 0) {
581 info->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
583 /* should be replay */
584 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
585 mdt->mdt_lut.lut_last_transno = info->mti_transno;
587 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
589 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
591 req->rq_export->exp_obd->obd_last_committed);
593 req->rq_transno = info->mti_transno;
594 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
596 /* update lcd in memory only for resent cases */
597 ted = &req->rq_export->exp_target_data;
599 cfs_mutex_lock(&ted->ted_lcd_lock);
601 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
602 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
603 if (info->mti_transno != 0)
604 lcd->lcd_last_close_transno = info->mti_transno;
605 lcd->lcd_last_close_xid = req->rq_xid;
606 lcd->lcd_last_close_result = rc;
608 /* VBR: save versions in last_rcvd for reconstruct. */
609 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
611 lcd->lcd_pre_versions[0] = pre_versions[0];
612 lcd->lcd_pre_versions[1] = pre_versions[1];
613 lcd->lcd_pre_versions[2] = pre_versions[2];
614 lcd->lcd_pre_versions[3] = pre_versions[3];
616 if (info->mti_transno != 0)
617 lcd->lcd_last_transno = info->mti_transno;
618 lcd->lcd_last_xid = req->rq_xid;
619 lcd->lcd_last_result = rc;
620 lcd->lcd_last_data = info->mti_opdata;
622 cfs_mutex_unlock(&ted->ted_lcd_lock);
627 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
629 LASSERT(mfd != NULL);
631 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
632 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
634 mfd->mfd_mode = mode;
637 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
638 struct mdt_object *o, __u64 flags, int created)
640 struct ptlrpc_request *req = mdt_info_req(info);
641 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
642 struct mdt_file_data *mfd;
643 struct md_attr *ma = &info->mti_attr;
644 struct lu_attr *la = &ma->ma_attr;
645 struct mdt_body *repbody;
646 int rc = 0, isdir, isreg;
649 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
651 isreg = S_ISREG(la->la_mode);
652 isdir = S_ISDIR(la->la_mode);
653 if (isreg && !(ma->ma_valid & MA_LOV)) {
655 * No EA, check whether it is will set regEA and dirEA since in
656 * above attr get, these size might be zero, so reset it, to
657 * retrieve the MD after create obj.
659 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
662 /* in replay case, p == NULL */
663 rc = mdt_create_data(info, p, o);
668 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
669 ma->ma_valid, ma->ma_lmm_size);
671 if (ma->ma_valid & MA_LOV) {
672 LASSERT(ma->ma_lmm_size != 0);
673 repbody->eadatasize = ma->ma_lmm_size;
675 repbody->valid |= OBD_MD_FLDIREA;
677 repbody->valid |= OBD_MD_FLEASIZE;
680 if (flags & FMODE_WRITE) {
681 rc = mdt_write_get(o);
683 mdt_ioepoch_open(info, o, created);
684 repbody->ioepoch = o->mot_ioepoch;
686 } else if (flags & MDS_FMODE_EXEC) {
687 rc = mdt_write_deny(o);
692 rc = mo_open(info->mti_env, mdt_object_child(o),
693 created ? flags | MDS_OPEN_CREATED : flags);
700 * Keep a reference on this object for this open, and is
701 * released by mdt_mfd_close().
703 mdt_object_get(info->mti_env, o);
706 * @flags is always not zero. At least it should be FMODE_READ,
707 * FMODE_WRITE or MDS_FMODE_EXEC.
712 mdt_mfd_set_mode(mfd, flags);
715 mfd->mfd_xid = req->rq_xid;
718 if (req_is_replay(req)) {
719 struct mdt_file_data *old_mfd;
720 /* Check wheather old cookie already exist in
721 * the list, becasue when do recovery, client
722 * might be disconnected from server, and
723 * restart replay, so there maybe some orphan
724 * mfd here, we should remove them */
725 LASSERT(info->mti_rr.rr_handle != NULL);
726 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
728 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
729 "cookie=" LPX64"\n", mfd,
730 PFID(mdt_object_fid(mfd->mfd_object)),
731 info->mti_rr.rr_handle->cookie);
732 cfs_spin_lock(&med->med_open_lock);
733 class_handle_unhash(&old_mfd->mfd_handle);
734 cfs_list_del_init(&old_mfd->mfd_list);
735 cfs_spin_unlock(&med->med_open_lock);
736 /* no attr update for that close */
738 ma->ma_valid |= MA_FLAGS;
739 ma->ma_attr_flags |= MDS_RECOV_OPEN;
740 mdt_mfd_close(info, old_mfd);
741 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
742 ma->ma_valid &= ~MA_FLAGS;
744 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
745 info->mti_rr.rr_handle->cookie);
746 mfd->mfd_old_handle.cookie =
747 info->mti_rr.rr_handle->cookie;
749 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
751 if (req->rq_export->exp_disconnected) {
752 cfs_spin_lock(&med->med_open_lock);
753 class_handle_unhash(&mfd->mfd_handle);
754 cfs_list_del_init(&mfd->mfd_list);
755 cfs_spin_unlock(&med->med_open_lock);
756 mdt_mfd_close(info, mfd);
758 cfs_spin_lock(&med->med_open_lock);
759 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
760 cfs_spin_unlock(&med->med_open_lock);
763 mdt_empty_transno(info, rc);
765 GOTO(err_out, rc = -ENOMEM);
771 if (flags & FMODE_WRITE)
772 /* XXX We also need to close io epoch here.
773 * See LU-1220 - green */
775 else if (flags & FMODE_EXEC)
780 int mdt_finish_open(struct mdt_thread_info *info,
781 struct mdt_object *p, struct mdt_object *o,
782 __u64 flags, int created, struct ldlm_reply *rep)
784 struct ptlrpc_request *req = mdt_info_req(info);
785 struct obd_export *exp = req->rq_export;
786 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
787 struct md_attr *ma = &info->mti_attr;
788 struct lu_attr *la = &ma->ma_attr;
789 struct mdt_file_data *mfd;
790 struct mdt_body *repbody;
792 int isreg, isdir, islnk;
796 LASSERT(ma->ma_valid & MA_INODE);
798 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
800 isreg = S_ISREG(la->la_mode);
801 isdir = S_ISDIR(la->la_mode);
802 islnk = S_ISLNK(la->la_mode);
803 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
805 if (exp_connect_rmtclient(exp)) {
806 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
808 rc = mdt_pack_remote_perm(info, o, buf);
810 repbody->valid &= ~OBD_MD_FLRMTPERM;
811 repbody->aclsize = 0;
813 repbody->valid |= OBD_MD_FLRMTPERM;
814 repbody->aclsize = sizeof(struct mdt_remote_perm);
817 #ifdef CONFIG_FS_POSIX_ACL
818 else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
819 const struct lu_env *env = info->mti_env;
820 struct md_object *next = mdt_object_child(o);
821 struct lu_buf *buf = &info->mti_buf;
823 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
824 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
826 if (buf->lb_len > 0) {
827 rc = mo_xattr_get(env, next, buf,
828 XATTR_NAME_ACL_ACCESS);
830 if (rc == -ENODATA) {
831 repbody->aclsize = 0;
832 repbody->valid |= OBD_MD_FLACL;
834 } else if (rc == -EOPNOTSUPP) {
837 CERROR("got acl size: %d\n", rc);
840 repbody->aclsize = rc;
841 repbody->valid |= OBD_MD_FLACL;
848 if (info->mti_mdt->mdt_opts.mo_mds_capa &&
849 exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
850 struct lustre_capa *capa;
852 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
854 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
855 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
858 repbody->valid |= OBD_MD_FLMDSCAPA;
860 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
861 exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
862 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
863 struct lustre_capa *capa;
865 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
867 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
868 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
871 repbody->valid |= OBD_MD_FLOSSCAPA;
875 * If we are following a symlink, don't open; and do not return open
876 * handle for special nodes as client required.
878 if (islnk || (!isreg && !isdir &&
879 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
880 lustre_msg_set_transno(req->rq_repmsg, 0);
884 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
887 * We need to return the existing object's fid back, so it is done here,
888 * after preparing the reply.
890 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
893 /* This can't be done earlier, we need to return reply body */
895 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
896 /* We are trying to create or write an existing dir. */
899 } else if (flags & MDS_OPEN_DIRECTORY)
902 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
903 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
908 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
909 cfs_spin_lock(&med->med_open_lock);
910 cfs_list_for_each(t, &med->med_open_head) {
911 mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
912 if (mfd->mfd_xid == req->rq_xid) {
917 cfs_spin_unlock(&med->med_open_lock);
920 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
921 /*set repbody->ea_size for resent case*/
922 if (ma->ma_valid & MA_LOV) {
923 LASSERT(ma->ma_lmm_size != 0);
924 repbody->eadatasize = ma->ma_lmm_size;
926 repbody->valid |= OBD_MD_FLDIREA;
928 repbody->valid |= OBD_MD_FLEASIZE;
934 rc = mdt_mfd_open(info, p, o, flags, created);
938 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
939 struct lsd_client_data *lcd);
941 void mdt_reconstruct_open(struct mdt_thread_info *info,
942 struct mdt_lock_handle *lhc)
944 const struct lu_env *env = info->mti_env;
945 struct mdt_device *mdt = info->mti_mdt;
946 struct req_capsule *pill = info->mti_pill;
947 struct ptlrpc_request *req = mdt_info_req(info);
948 struct tg_export_data *ted = &req->rq_export->exp_target_data;
949 struct lsd_client_data *lcd = ted->ted_lcd;
950 struct md_attr *ma = &info->mti_attr;
951 struct mdt_reint_record *rr = &info->mti_rr;
952 __u32 flags = info->mti_spec.sp_cr_flags;
953 struct ldlm_reply *ldlm_rep;
954 struct mdt_object *parent;
955 struct mdt_object *child;
956 struct mdt_body *repbody;
960 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
961 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
962 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
964 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
965 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
967 ma->ma_need = MA_INODE;
968 if (ma->ma_lmm_size > 0)
969 ma->ma_need |= MA_LOV;
973 mdt_req_from_lcd(req, lcd);
974 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
976 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
977 ldlm_rep->lock_policy_res1, req->rq_status);
979 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
981 /* We did not create successfully, return error to client. */
982 GOTO(out, rc = req->rq_status);
984 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
985 struct obd_export *exp = req->rq_export;
987 * We failed after creation, but we do not know in which step
988 * we failed. So try to check the child object.
990 parent = mdt_object_find(env, mdt, rr->rr_fid1);
991 if (IS_ERR(parent)) {
992 rc = PTR_ERR(parent);
993 LCONSOLE_WARN("Parent "DFID" lookup error %d."
994 " Evicting client %s with export %s.\n",
995 PFID(rr->rr_fid1), rc,
996 obd_uuid2str(&exp->exp_client_uuid),
997 obd_export_nid2str(exp));
998 mdt_export_evict(exp);
1001 child = mdt_object_find(env, mdt, rr->rr_fid2);
1002 if (IS_ERR(child)) {
1003 rc = PTR_ERR(child);
1004 LCONSOLE_WARN("Child "DFID" lookup error %d."
1005 " Evicting client %s with export %s.\n",
1006 PFID(mdt_object_fid(child)), rc,
1007 obd_uuid2str(&exp->exp_client_uuid),
1008 obd_export_nid2str(exp));
1009 mdt_object_put(env, parent);
1010 mdt_export_evict(exp);
1013 rc = mdt_object_exists(child);
1016 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1017 rc = mdt_attr_get_complex(info, child, ma);
1019 rc = mdt_finish_open(info, parent, child,
1020 flags, 1, ldlm_rep);
1021 } else if (rc < 0) {
1022 /* the child object was created on remote server */
1023 repbody->fid1 = *rr->rr_fid2;
1024 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1026 } else if (rc == 0) {
1027 /* the child does not exist, we should do regular open */
1028 mdt_object_put(env, parent);
1029 mdt_object_put(env, child);
1030 GOTO(regular_open, 0);
1032 mdt_object_put(env, parent);
1033 mdt_object_put(env, child);
1037 /* We did not try to create, so we are a pure open */
1038 rc = mdt_reint_open(info, lhc);
1043 req->rq_status = rc;
1044 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1045 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1048 int mdt_open_by_fid(struct mdt_thread_info* info,
1049 struct ldlm_reply *rep)
1051 __u32 flags = info->mti_spec.sp_cr_flags;
1052 struct mdt_reint_record *rr = &info->mti_rr;
1053 struct md_attr *ma = &info->mti_attr;
1054 struct mdt_object *o;
1058 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1060 RETURN(rc = PTR_ERR(o));
1062 rc = mdt_object_exists(o);
1064 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1068 rc = mdt_attr_get_complex(info, o, ma);
1070 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1071 } else if (rc == 0) {
1074 /* the child object was created on remote server */
1075 struct mdt_body *repbody;
1076 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1077 repbody->fid1 = *rr->rr_fid2;
1078 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1082 mdt_object_put(info->mti_env, o);
1086 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1087 struct mdt_lock_handle *lhc)
1089 const struct lu_env *env = info->mti_env;
1090 struct mdt_device *mdt = info->mti_mdt;
1091 __u32 flags = info->mti_spec.sp_cr_flags;
1092 struct mdt_reint_record *rr = &info->mti_rr;
1093 struct md_attr *ma = &info->mti_attr;
1094 struct mdt_object *parent= NULL;
1095 struct mdt_object *o;
1100 if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1101 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1102 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1103 if (IS_ERR(parent)) {
1104 CDEBUG(D_INODE, "Fail to find parent "DFID
1105 " for anonymous created %ld, try to"
1106 " use server-side parent.\n",
1107 PFID(rr->rr_fid1), PTR_ERR(parent));
1112 ma->ma_need |= MA_PFID;
1115 o = mdt_object_find(env, mdt, rr->rr_fid2);
1117 RETURN(rc = PTR_ERR(o));
1119 rc = mdt_object_exists(o);
1121 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1123 GOTO(out, rc = -ENOENT);
1124 } else if (rc < 0) {
1125 CERROR("NFS remote open shouldn't happen.\n");
1128 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1132 if (flags & FMODE_WRITE)
1134 else if (flags & MDS_FMODE_EXEC)
1139 mdt_lock_handle_init(lhc);
1140 mdt_lock_reg_init(lhc, lm);
1141 rc = mdt_object_lock(info, o, lhc,
1142 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1147 rc = mdt_attr_get_complex(info, o, ma);
1151 if (ma->ma_valid & MA_PFID) {
1152 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1153 if (IS_ERR(parent)) {
1154 CDEBUG(D_INODE, "Fail to find parent "DFID
1155 " for anonymous created %ld, try to"
1156 " use system default.\n",
1157 PFID(&ma->ma_pfid), PTR_ERR(parent));
1162 if (flags & MDS_OPEN_LOCK)
1163 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1164 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1166 if (!(flags & MDS_OPEN_LOCK) || rc)
1167 mdt_object_unlock(info, o, lhc, 1);
1171 mdt_object_put(env, o);
1173 mdt_object_put(env, parent);
1177 int mdt_pin(struct mdt_thread_info* info)
1180 RETURN(err_serious(-EOPNOTSUPP));
1183 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1184 int mdt_cross_open(struct mdt_thread_info* info,
1185 const struct lu_fid *fid,
1186 struct ldlm_reply *rep, __u32 flags)
1188 struct md_attr *ma = &info->mti_attr;
1189 struct mdt_object *o;
1193 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1195 RETURN(rc = PTR_ERR(o));
1197 rc = mdt_object_exists(o);
1199 /* Do permission check for cross-open. */
1200 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1201 NULL, flags | MDS_OPEN_CROSS);
1205 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1206 rc = mdt_attr_get_complex(info, o, ma);
1208 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1209 } else if (rc == 0) {
1211 * Something is wrong here. lookup was positive but there is
1214 CERROR("Cross-ref object doesn't exist!\n");
1217 /* Something is wrong here, the object is on another MDS! */
1218 CERROR("The object isn't on this server! FLD error?\n");
1219 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1221 "Object isn't on this server! FLD error?\n");
1227 mdt_object_put(info->mti_env, o);
1231 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1233 struct mdt_device *mdt = info->mti_mdt;
1234 struct ptlrpc_request *req = mdt_info_req(info);
1235 struct mdt_object *parent;
1236 struct mdt_object *child;
1237 struct mdt_lock_handle *lh;
1238 struct ldlm_reply *ldlm_rep;
1239 struct mdt_body *repbody;
1240 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1241 struct md_attr *ma = &info->mti_attr;
1242 __u64 create_flags = info->mti_spec.sp_cr_flags;
1243 struct mdt_reint_record *rr = &info->mti_rr;
1244 struct lu_name *lname;
1250 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1251 (obd_timeout + 1) / 4);
1253 mdt_counter_incr(req, LPROC_MDT_OPEN);
1254 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1256 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1257 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1259 ma->ma_need = MA_INODE;
1260 if (ma->ma_lmm_size > 0)
1261 ma->ma_need |= MA_LOV;
1265 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1266 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1268 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1269 CERROR("file join is not supported anymore.\n");
1270 GOTO(out, result = err_serious(-EOPNOTSUPP));
1272 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1274 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1275 info->mti_spec.u.sp_ea.eadata == NULL)
1276 GOTO(out, result = err_serious(-EINVAL));
1278 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1279 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1280 PFID(rr->rr_fid1), rr->rr_name,
1281 PFID(rr->rr_fid2), create_flags,
1282 ma->ma_attr.la_mode, msg_flags);
1284 if (req_is_replay(req) ||
1285 (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1286 /* This is a replay request or from liblustre with ea. */
1287 result = mdt_open_by_fid(info, ldlm_rep);
1289 if (result != -ENOENT) {
1290 if (req->rq_export->exp_libclient &&
1291 create_flags & MDS_OPEN_HAS_EA)
1292 GOTO(out, result = 0);
1295 /* We didn't find the correct object, so we need to re-create it
1296 * via a regular replay. */
1297 if (!(create_flags & MDS_OPEN_CREAT)) {
1298 DEBUG_REQ(D_ERROR, req,
1299 "OPEN & CREAT not in open replay/by_fid.");
1300 GOTO(out, result = -EFAULT);
1302 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1303 } else if ((rr->rr_namelen == 0 && !info->mti_cross_ref &&
1304 create_flags & MDS_OPEN_LOCK) ||
1305 (create_flags & MDS_OPEN_BY_FID)) {
1306 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1307 if (result != -ENOENT && !(create_flags & MDS_OPEN_CREAT))
1309 if (unlikely(rr->rr_namelen == 0))
1310 GOTO(out, result = -EINVAL);
1311 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1314 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1315 GOTO(out, result = err_serious(-ENOMEM));
1317 mdt_set_disposition(info, ldlm_rep,
1318 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1320 if (info->mti_cross_ref) {
1321 /* This is cross-ref open */
1322 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1323 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1328 lh = &info->mti_lh[MDT_LH_PARENT];
1329 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1330 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1332 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1333 MDS_INODELOCK_UPDATE);
1335 GOTO(out, result = PTR_ERR(parent));
1337 /* get and check version of parent */
1338 result = mdt_version_get_check(info, parent, 0);
1340 GOTO(out_parent, result);
1342 fid_zero(child_fid);
1344 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1345 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1346 lname, child_fid, &info->mti_spec);
1347 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1348 "looking for "DFID"/%s, result fid="DFID"\n",
1349 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1351 if (result != 0 && result != -ENOENT && result != -ESTALE)
1352 GOTO(out_parent, result);
1354 if (result == -ENOENT || result == -ESTALE) {
1355 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1356 if (result == -ESTALE) {
1358 * -ESTALE means the parent is a dead(unlinked) dir, so
1359 * it should return -ENOENT to in accordance with the
1360 * original mds implementaion.
1362 GOTO(out_parent, result = -ENOENT);
1364 if (!(create_flags & MDS_OPEN_CREAT))
1365 GOTO(out_parent, result);
1366 *child_fid = *info->mti_rr.rr_fid2;
1367 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1369 /* In the function below, .hs_keycmp resolves to
1370 * lu_obj_hop_keycmp() */
1371 /* coverity[overrun-buffer-val] */
1372 child = mdt_object_new(info->mti_env, mdt, child_fid);
1375 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1376 * return FID back in that case.
1378 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1379 child = mdt_object_find(info->mti_env, mdt, child_fid);
1382 GOTO(out_parent, result = PTR_ERR(child));
1384 /** check version of child */
1385 rc = mdt_version_get_check(info, child, 1);
1387 GOTO(out_child, result = rc);
1389 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1390 if (result == -ENOENT) {
1391 if (mdt_object_obf(parent))
1392 GOTO(out_child, result = -EPERM);
1394 /* save versions in reply */
1395 mdt_version_get_save(info, parent, 0);
1396 mdt_version_get_save(info, child, 1);
1398 /* version of child will be changed */
1399 info->mti_mos = child;
1401 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1402 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1404 /* Let lower layers know what is lock mode on directory. */
1405 info->mti_spec.sp_cr_mode =
1406 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1409 * Do not perform lookup sanity check. We know that name does
1412 info->mti_spec.sp_cr_lookup = 0;
1413 info->mti_spec.sp_feat = &dt_directory_features;
1415 result = mdo_create(info->mti_env,
1416 mdt_object_child(parent),
1418 mdt_object_child(child),
1421 if (result == -ERESTART) {
1422 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1423 GOTO(out_child, result);
1426 /* XXX: we should call this once, see few lines below */
1428 result = mdt_attr_get_complex(info, child, ma);
1431 GOTO(out_child, result);
1435 /* We have to get attr & lov ea for this object */
1436 result = mdt_attr_get_complex(info, child, ma);
1438 * The object is on remote node, return its FID for remote open.
1440 if (result == -EREMOTE) {
1442 * Check if this lock already was sent to client and
1443 * this is resent case. For resent case do not take lock
1444 * again, use what is already granted.
1446 LASSERT(lhc != NULL);
1448 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1449 struct ldlm_lock *lock;
1451 LASSERT(msg_flags & MSG_RESENT);
1453 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1455 CERROR("Invalid lock handle "LPX64"\n",
1456 lhc->mlh_reg_lh.cookie);
1459 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1460 &lock->l_resource->lr_name));
1461 LDLM_LOCK_PUT(lock);
1464 mdt_lock_handle_init(lhc);
1465 mdt_lock_reg_init(lhc, LCK_PR);
1467 rc = mdt_object_lock(info, child, lhc,
1468 MDS_INODELOCK_LOOKUP,
1471 repbody->fid1 = *mdt_object_fid(child);
1472 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1475 GOTO(out_child, result);
1479 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1481 /* get openlock if this is not replay and if a client requested it */
1482 if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
1485 if (create_flags & FMODE_WRITE)
1487 else if (create_flags & MDS_FMODE_EXEC)
1491 mdt_lock_handle_init(lhc);
1492 mdt_lock_reg_init(lhc, lm);
1493 rc = mdt_object_lock(info, child, lhc,
1494 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1498 GOTO(out_child, result);
1501 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1505 /* Try to open it now. */
1506 rc = mdt_finish_open(info, parent, child, create_flags,
1510 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1511 /* openlock was acquired and mdt_finish_open failed -
1512 drop the openlock */
1513 mdt_object_unlock(info, child, lhc, 1);
1517 ma->ma_cookie_size = 0;
1518 info->mti_no_need_trans = 1;
1519 rc = mdo_unlink(info->mti_env,
1520 mdt_object_child(parent),
1521 mdt_object_child(child),
1525 CERROR("Error in cleanup of open\n");
1530 mdt_object_put(info->mti_env, child);
1532 mdt_object_unlock_put(info, parent, lh, result || !created);
1534 if (result && result != -EREMOTE)
1535 lustre_msg_set_transno(req->rq_repmsg, 0);
1539 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1540 MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1542 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1544 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1547 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1549 struct mdt_object *o = mfd->mfd_object;
1550 struct md_object *next = mdt_object_child(o);
1551 struct md_attr *ma = &info->mti_attr;
1552 int ret = MDT_IOEPOCH_CLOSED;
1557 mode = mfd->mfd_mode;
1559 if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1561 ret = mdt_ioepoch_close(info, o);
1562 } else if (mode & MDS_FMODE_EXEC) {
1564 } else if (mode & MDS_FMODE_EPOCH) {
1565 ret = mdt_ioepoch_close(info, o);
1566 } else if (mode & MDS_FMODE_SOM) {
1567 ret = mdt_som_au_close(info, o);
1570 /* Update atime on close only. */
1571 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1572 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1573 /* Set the atime only. */
1574 ma->ma_valid = MA_INODE;
1575 ma->ma_attr.la_valid = LA_ATIME;
1576 rc = mo_attr_set(info->mti_env, next, ma);
1579 ma->ma_need |= MA_INODE;
1580 ma->ma_valid &= ~MA_INODE;
1582 if (!MFD_CLOSED(mode))
1583 rc = mo_close(info->mti_env, next, ma, mode);
1585 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1586 struct mdt_export_data *med;
1588 /* The IOepoch is still opened or SOM update is needed.
1589 * Put mfd back into the list. */
1590 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1591 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1592 MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1594 LASSERT(mdt_info_req(info));
1595 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1596 cfs_spin_lock(&med->med_open_lock);
1597 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1598 class_handle_hash_back(&mfd->mfd_handle);
1599 cfs_spin_unlock(&med->med_open_lock);
1601 if (ret == MDT_IOEPOCH_OPENED) {
1605 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1606 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1610 mdt_object_put(info->mti_env, o);
1613 RETURN(rc ? rc : ret);
1616 int mdt_close(struct mdt_thread_info *info)
1618 struct mdt_export_data *med;
1619 struct mdt_file_data *mfd;
1620 struct mdt_object *o;
1621 struct md_attr *ma = &info->mti_attr;
1622 struct mdt_body *repbody = NULL;
1623 struct ptlrpc_request *req = mdt_info_req(info);
1627 mdt_counter_incr(req, LPROC_MDT_CLOSE);
1628 /* Close may come with the Size-on-MDS update. Unpack it. */
1629 rc = mdt_close_unpack(info);
1631 RETURN(err_serious(rc));
1633 LASSERT(info->mti_ioepoch);
1635 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1636 info->mti_mdt->mdt_max_mdsize);
1637 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1638 info->mti_mdt->mdt_max_cookiesize);
1639 rc = req_capsule_server_pack(info->mti_pill);
1640 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1641 mdt_client_compatibility(info);
1643 mdt_fix_reply(info);
1644 RETURN(lustre_msg_get_status(req->rq_repmsg));
1647 /* Continue to close handle even if we can not pack reply */
1649 repbody = req_capsule_server_get(info->mti_pill,
1651 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1653 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1656 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1658 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1661 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1662 repbody->eadatasize = 0;
1663 repbody->aclsize = 0;
1665 rc = err_serious(rc);
1668 med = &req->rq_export->exp_mdt_data;
1669 cfs_spin_lock(&med->med_open_lock);
1670 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1671 if (mdt_mfd_closed(mfd)) {
1672 cfs_spin_unlock(&med->med_open_lock);
1673 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1674 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1675 info->mti_ioepoch->handle.cookie);
1676 /** not serious error since bug 3633 */
1679 class_handle_unhash(&mfd->mfd_handle);
1680 cfs_list_del_init(&mfd->mfd_list);
1681 cfs_spin_unlock(&med->med_open_lock);
1683 /* Do not lose object before last unlink. */
1684 o = mfd->mfd_object;
1685 mdt_object_get(info->mti_env, o);
1686 ret = mdt_mfd_close(info, mfd);
1687 if (repbody != NULL)
1688 rc = mdt_handle_last_unlink(info, o, ma);
1689 mdt_empty_transno(info, rc);
1690 mdt_object_put(info->mti_env, o);
1692 if (repbody != NULL) {
1693 mdt_client_compatibility(info);
1694 rc = mdt_fix_reply(info);
1697 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1698 RETURN(err_serious(-ENOMEM));
1700 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1701 OBD_FAIL_MDS_CLOSE_NET_REP))
1702 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1703 RETURN(rc ? rc : ret);
1707 * DONE_WRITING rpc handler.
1709 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1710 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1711 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1712 * skip attributes and reconstruct the reply here.
1714 int mdt_done_writing(struct mdt_thread_info *info)
1716 struct ptlrpc_request *req = mdt_info_req(info);
1717 struct mdt_body *repbody = NULL;
1718 struct mdt_export_data *med;
1719 struct mdt_file_data *mfd;
1723 rc = req_capsule_server_pack(info->mti_pill);
1725 RETURN(err_serious(rc));
1727 repbody = req_capsule_server_get(info->mti_pill,
1729 repbody->eadatasize = 0;
1730 repbody->aclsize = 0;
1732 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1733 rc = mdt_close_unpack(info);
1735 RETURN(err_serious(rc));
1737 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1738 RETURN(lustre_msg_get_status(req->rq_repmsg));
1740 med = &info->mti_exp->exp_mdt_data;
1741 cfs_spin_lock(&med->med_open_lock);
1742 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1744 cfs_spin_unlock(&med->med_open_lock);
1745 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1746 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1747 PFID(info->mti_rr.rr_fid1),
1748 info->mti_ioepoch->handle.cookie,
1749 info->mti_ioepoch->ioepoch);
1750 /* If this is a replay, reconstruct the transno. */
1751 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1752 rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1754 mdt_empty_transno(info, rc);
1760 LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1761 mfd->mfd_mode == MDS_FMODE_TRUNC);
1762 class_handle_unhash(&mfd->mfd_handle);
1763 cfs_list_del_init(&mfd->mfd_list);
1764 cfs_spin_unlock(&med->med_open_lock);
1766 /* Set EPOCH CLOSE flag if not set by client. */
1767 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1768 info->mti_attr.ma_valid = 0;
1770 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1771 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1772 if (info->mti_attr.ma_lmm == NULL)
1775 rc = mdt_mfd_close(info, mfd);
1777 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1778 mdt_empty_transno(info, rc);