1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/mdt/mdt_open.c
41 * Lustre Metadata Target (mdt) open/close file handling
43 * Author: Huang Hua <huanghua@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <lustre_acl.h>
52 #include <lustre_mds.h>
53 #include "mdt_internal.h"
55 /* we do nothing because we do not have refcount now */
56 static void mdt_mfd_get(void *mfdp)
60 /* Create a new mdt_file_data struct, initialize it,
61 * and insert it to global hash table */
62 struct mdt_file_data *mdt_mfd_new(void)
64 struct mdt_file_data *mfd;
69 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
70 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
71 class_handle_hash(&mfd->mfd_handle, mdt_mfd_get);
77 * Find the mfd pointed to by handle in global hash table.
78 * In case of replay the handle is obsoleted
79 * but mfd can be found in mfd list by that handle
81 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
82 const struct lustre_handle *handle)
84 struct ptlrpc_request *req = mdt_info_req(info);
85 struct mdt_file_data *mfd;
88 LASSERT(handle != NULL);
89 mfd = class_handle2object(handle->cookie);
90 /* during dw/setattr replay the mfd can be found by old handle */
91 if (mfd == NULL && req_is_replay(req)) {
92 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
93 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
94 if (mfd->mfd_old_handle.cookie == handle->cookie)
103 void mdt_mfd_free(struct mdt_file_data *mfd)
105 LASSERT(cfs_list_empty(&mfd->mfd_list));
106 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
109 static int mdt_create_data(struct mdt_thread_info *info,
110 struct mdt_object *p, struct mdt_object *o)
112 struct md_op_spec *spec = &info->mti_spec;
113 struct md_attr *ma = &info->mti_attr;
117 if (!md_should_create(spec->sp_cr_flags))
120 ma->ma_need = MA_INODE | MA_LOV;
122 cfs_down(&o->mot_lov_sem);
123 if (!(o->mot_flags & MOF_LOV_CREATED)) {
124 rc = mdo_create_data(info->mti_env,
125 p ? mdt_object_child(p) : NULL,
126 mdt_object_child(o), spec, ma);
127 if (rc == 0 && ma->ma_valid & MA_LOV)
128 o->mot_flags |= MOF_LOV_CREATED;
130 cfs_up(&o->mot_lov_sem);
134 static int mdt_ioepoch_opened(struct mdt_object *mo)
136 return mo->mot_ioepoch_count;
139 int mdt_object_is_som_enabled(struct mdt_object *mo)
141 return !mo->mot_ioepoch;
145 * Re-enable Size-on-MDS.
146 * Call under ->mot_ioepoch_sem.
148 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
150 if (ioepoch == mo->mot_ioepoch) {
151 LASSERT(!mdt_ioepoch_opened(mo));
158 * Open the IOEpoch. It is allowed if @writecount is not negative.
159 * The epoch and writecount handling is performed under the mot_ioepoch_sem.
161 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
164 struct mdt_device *mdt = info->mti_mdt;
169 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
170 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
173 cfs_down(&o->mot_ioepoch_sem);
174 if (mdt_ioepoch_opened(o)) {
175 /* Epoch continues even if there is no writers yet. */
176 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
177 o->mot_ioepoch, PFID(mdt_object_fid(o)));
179 /* XXX: ->mdt_ioepoch is not initialized at the mount */
180 cfs_spin_lock(&mdt->mdt_ioepoch_lock);
181 if (mdt->mdt_ioepoch < info->mti_replayepoch)
182 mdt->mdt_ioepoch = info->mti_replayepoch;
184 if (info->mti_replayepoch)
185 o->mot_ioepoch = info->mti_replayepoch;
186 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
187 o->mot_ioepoch = ++mdt->mdt_ioepoch;
189 o->mot_ioepoch = mdt->mdt_ioepoch;
191 cfs_spin_unlock(&mdt->mdt_ioepoch_lock);
193 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
194 o->mot_ioepoch, PFID(mdt_object_fid(o)));
196 o->mot_flags |= MOF_SOM_CREATED;
199 o->mot_ioepoch_count++;
200 cfs_up(&o->mot_ioepoch_sem);
202 /* Cancel Size-on-MDS attributes cached on clients for the open case.
203 * In the truncate case, see mdt_reint_setattr(). */
204 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
205 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
206 mdt_lock_reg_init(lh, LCK_EX);
207 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
210 mdt_object_unlock(info, o, lh, 1);
216 * Update SOM on-disk attributes.
217 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
218 * mountid and attributes. If disabling, zero IOEpoch id in lustre-ea.
219 * Call under ->mot_ioepoch_sem.
221 static int mdt_som_attr_set(struct mdt_thread_info *info,
222 struct mdt_object *obj, __u64 ioepoch, int enable)
224 struct md_attr *ma = &info->mti_attr;
228 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
229 " on "DFID".\n", enable ? "update" : "disabling",
230 ioepoch, PFID(mdt_object_fid(obj)));
232 ma->ma_valid |= MA_SOM;
233 ma->ma_som = &info->mti_u.som.data;
235 struct mdt_device *mdt = info->mti_mdt;
236 struct lu_attr *la = &ma->ma_attr;
238 ma->ma_som->msd_ioepoch = ioepoch;
239 ma->ma_som->msd_size = la->la_valid & LA_SIZE ? la->la_size : 0;
240 ma->ma_som->msd_blocks = la->la_valid & LA_BLOCKS ?
242 ma->ma_som->msd_mountid = mdt->mdt_lut.lut_obd->u.obt.obt_mount_count;
243 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
245 ma->ma_som->msd_ioepoch = IOEPOCH_INVAL;
246 ma->ma_attr.la_valid &= LA_ATIME;
249 /* Since we have opened the file, it is unnecessary
250 * to check permission when close it. Between the "open"
251 * and "close", maybe someone has changed the file mode
252 * or flags, or the file created mode do not permit wirte,
253 * and so on. Just set MDS_PERM_BYPASS for all the cases. */
254 ma->ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
256 rc = mdt_attr_set(info, obj, ma, 0);
260 /** Perform the eviction specific actions on ioepoch close. */
261 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
262 struct mdt_object *o)
266 cfs_down(&o->mot_ioepoch_sem);
267 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
268 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
269 o->mot_ioepoch_count);
270 o->mot_ioepoch_count--;
272 /* If eviction occured set MOF_SOM_RECOV,
273 * if no other epoch holders, disable SOM on disk. */
274 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
275 if (!mdt_ioepoch_opened(o)) {
276 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
277 mdt_object_som_enable(o, o->mot_ioepoch);
279 cfs_up(&o->mot_ioepoch_sem);
284 * Perform the replay specific actions on ioepoch close.
285 * Skip SOM attribute update if obtained and just forget about the inode state
286 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
288 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
289 struct mdt_object *o)
291 int rc = MDT_IOEPOCH_CLOSED;
294 cfs_down(&o->mot_ioepoch_sem);
295 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
296 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
297 o->mot_ioepoch_count--;
299 /* Get an info from the replayed request if client is supposed
300 * to send an Attibute Update, reconstruct @rc if so */
301 if (info->mti_ioepoch->flags & MF_SOM_AU)
302 rc = MDT_IOEPOCH_GETATTR;
304 if (!mdt_ioepoch_opened(o))
305 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
306 cfs_up(&o->mot_ioepoch_sem);
312 * Regular file IOepoch close.
313 * Closes the ioepoch, checks the object state, apply obtained attributes and
314 * re-enable SOM on the object, if possible. Also checks if the recovery is
315 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
316 * to obtain SOM attributes under the server-side OST locks.
319 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
320 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
322 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
323 struct mdt_object *o)
325 struct md_attr *tmp_ma;
329 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
332 la = &info->mti_attr.ma_attr;
333 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
335 cfs_down(&o->mot_ioepoch_sem);
336 o->mot_ioepoch_count--;
338 tmp_ma = &info->mti_u.som.attr;
339 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
340 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
341 tmp_ma->ma_som = &info->mti_u.som.data;
342 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
343 tmp_ma->ma_valid = 0;
344 rc = mo_attr_get(info->mti_env, mdt_object_child(o), tmp_ma);
348 /* Check the on-disk SOM state. */
349 if (o->mot_flags & MOF_SOM_RECOV)
351 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
352 !(tmp_ma->ma_valid & MA_SOM))
355 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
356 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
358 opened = mdt_ioepoch_opened(o);
360 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
361 * Skip the check for file with no LOV or for unlink files.
363 if (!opened && tmp_ma->ma_valid & MA_LOV &&
364 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
366 /* If some previous writer was evicted, re-ask the
367 * client for attributes. Even if attributes are
368 * provided, we cannot believe in them.
369 * Another use case is that there is no SOM cache on
370 * disk -- first access with SOM or there was an MDS
372 ret = MDT_IOEPOCH_GETATTR;
373 else if (o->mot_flags & MOF_SOM_CHANGE)
374 /* Some previous writer changed the attribute.
375 * Do not believe to the current Size-on-MDS
376 * update, re-ask client. */
377 ret = MDT_IOEPOCH_GETATTR;
378 else if (!(la->la_valid & LA_SIZE) && achange)
379 /* Attributes were changed by the last writer
380 * only but no Size-on-MDS update is received.*/
381 ret = MDT_IOEPOCH_GETATTR;
384 if (achange || ret == MDT_IOEPOCH_GETATTR)
385 o->mot_flags |= MOF_SOM_CHANGE;
387 /* If epoch ends and relable SOM attributes are obtained, update them.
388 * Create SOM ea for new files even if there is no attributes obtained
389 * (0-length file). */
390 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
391 if (achange || o->mot_flags & MOF_SOM_CREATED) {
392 LASSERT(achange || !(la->la_valid & LA_SIZE));
393 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
395 /* Avoid the following setattrs of these attributes,
396 * e.g. for atime update. */
397 info->mti_attr.ma_valid = 0;
399 mdt_object_som_enable(o, o->mot_ioepoch);
402 cfs_up(&o->mot_ioepoch_sem);
403 /* If recovery is needed, tell the client to perform GETATTR under
405 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
406 struct mdt_body *rep;
407 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
408 rep->valid |= OBD_MD_FLGETATTRLOCK;
414 cfs_up(&o->mot_ioepoch_sem);
419 * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
420 * - a client closes the IOEpoch;
421 * - a client eviction occured.
423 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
424 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
425 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
428 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
430 struct ptlrpc_request *req = mdt_info_req(info);
433 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
434 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
437 LASSERT(o->mot_ioepoch_count);
438 LASSERT(info->mti_ioepoch == NULL ||
439 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
441 /* IOEpoch is closed only if client tells about it or eviction occures.
442 * In the replay case, always close the epoch. */
444 RETURN(mdt_ioepoch_close_on_eviction(info, o));
445 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
446 RETURN(mdt_ioepoch_close_on_replay(info, o));
447 if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
448 RETURN(mdt_ioepoch_close_reg(info, o));
449 /* IO epoch is not closed. */
450 RETURN(MDT_IOEPOCH_OPENED);
454 * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
455 * for attribute update. It happens if:
456 * - SOM Attribute Update is obtained;
457 * - the client failed to obtain it and informs MDS about it;
458 * - a client eviction occured.
459 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
462 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
464 struct ptlrpc_request *req = mdt_info_req(info);
466 int act = MDT_SOM_ENABLE;
470 LASSERT(!req || info->mti_ioepoch);
471 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
472 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
475 /* No size whereas MF_SOM_CHANGE is set means client failed to
476 * obtain ost attributes, drop the SOM cache on disk if so. */
478 (info->mti_ioepoch &&
479 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
480 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
481 act = MDT_SOM_DISABLE;
483 cfs_down(&o->mot_ioepoch_sem);
484 /* Mark the object it is the recovery state if we failed to obtain
486 if (act == MDT_SOM_DISABLE)
487 o->mot_flags |= MOF_SOM_RECOV;
489 if (!mdt_ioepoch_opened(o)) {
490 ioepoch = info->mti_ioepoch ?
491 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
493 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
494 rc = mdt_som_attr_set(info, o, ioepoch, act);
495 mdt_object_som_enable(o, ioepoch);
497 cfs_up(&o->mot_ioepoch_sem);
501 int mdt_write_read(struct mdt_object *o)
505 cfs_down(&o->mot_ioepoch_sem);
506 rc = o->mot_writecount;
507 cfs_up(&o->mot_ioepoch_sem);
511 int mdt_write_get(struct mdt_object *o)
515 cfs_down(&o->mot_ioepoch_sem);
516 if (o->mot_writecount < 0)
520 cfs_up(&o->mot_ioepoch_sem);
524 void mdt_write_put(struct mdt_object *o)
527 cfs_down(&o->mot_ioepoch_sem);
529 cfs_up(&o->mot_ioepoch_sem);
533 static int mdt_write_deny(struct mdt_object *o)
537 cfs_down(&o->mot_ioepoch_sem);
538 if (o->mot_writecount > 0)
542 cfs_up(&o->mot_ioepoch_sem);
546 static void mdt_write_allow(struct mdt_object *o)
549 cfs_down(&o->mot_ioepoch_sem);
551 cfs_up(&o->mot_ioepoch_sem);
555 /* there can be no real transaction so prepare the fake one */
556 static void mdt_empty_transno(struct mdt_thread_info* info)
558 struct mdt_device *mdt = info->mti_mdt;
559 struct ptlrpc_request *req = mdt_info_req(info);
562 /* transaction has occurred already */
563 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
566 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
567 if (info->mti_transno == 0) {
568 info->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
570 /* should be replay */
571 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
572 mdt->mdt_lut.lut_last_transno = info->mti_transno;
574 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
576 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
578 req->rq_export->exp_obd->obd_last_committed);
580 req->rq_transno = info->mti_transno;
581 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
585 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
587 LASSERT(mfd != NULL);
589 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
590 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
592 mfd->mfd_mode = mode;
595 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
596 struct mdt_object *o, __u64 flags, int created)
598 struct ptlrpc_request *req = mdt_info_req(info);
599 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
600 struct mdt_file_data *mfd;
601 struct md_attr *ma = &info->mti_attr;
602 struct lu_attr *la = &ma->ma_attr;
603 struct mdt_body *repbody;
604 int rc = 0, isdir, isreg;
607 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
609 isreg = S_ISREG(la->la_mode);
610 isdir = S_ISDIR(la->la_mode);
611 if (isreg && !(ma->ma_valid & MA_LOV)) {
613 * No EA, check whether it is will set regEA and dirEA since in
614 * above attr get, these size might be zero, so reset it, to
615 * retrieve the MD after create obj.
617 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
620 /* in replay case, p == NULL */
621 rc = mdt_create_data(info, p, o);
626 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
627 ma->ma_valid, ma->ma_lmm_size);
629 if (ma->ma_valid & MA_LOV) {
630 LASSERT(ma->ma_lmm_size != 0);
631 repbody->eadatasize = ma->ma_lmm_size;
633 repbody->valid |= OBD_MD_FLDIREA;
635 repbody->valid |= OBD_MD_FLEASIZE;
638 if (flags & FMODE_WRITE) {
639 rc = mdt_write_get(o);
641 mdt_ioepoch_open(info, o, created);
642 repbody->ioepoch = o->mot_ioepoch;
644 } else if (flags & MDS_FMODE_EXEC) {
645 rc = mdt_write_deny(o);
650 rc = mo_open(info->mti_env, mdt_object_child(o),
651 created ? flags | MDS_OPEN_CREATED : flags);
658 * Keep a reference on this object for this open, and is
659 * released by mdt_mfd_close().
661 mdt_object_get(info->mti_env, o);
664 * @flags is always not zero. At least it should be FMODE_READ,
665 * FMODE_WRITE or MDS_FMODE_EXEC.
670 mdt_mfd_set_mode(mfd, flags);
673 mfd->mfd_xid = req->rq_xid;
676 if (req_is_replay(req)) {
677 struct mdt_file_data *old_mfd;
678 /* Check wheather old cookie already exist in
679 * the list, becasue when do recovery, client
680 * might be disconnected from server, and
681 * restart replay, so there maybe some orphan
682 * mfd here, we should remove them */
683 LASSERT(info->mti_rr.rr_handle != NULL);
684 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
686 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
687 "cookie=" LPX64"\n", mfd,
688 PFID(mdt_object_fid(mfd->mfd_object)),
689 info->mti_rr.rr_handle->cookie);
690 cfs_spin_lock(&med->med_open_lock);
691 class_handle_unhash(&old_mfd->mfd_handle);
692 cfs_list_del_init(&old_mfd->mfd_list);
693 cfs_spin_unlock(&med->med_open_lock);
694 /* no attr update for that close */
696 ma->ma_valid |= MA_FLAGS;
697 ma->ma_attr_flags |= MDS_RECOV_OPEN;
698 mdt_mfd_close(info, old_mfd);
699 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
700 ma->ma_valid &= ~MA_FLAGS;
702 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
703 info->mti_rr.rr_handle->cookie);
704 mfd->mfd_old_handle.cookie =
705 info->mti_rr.rr_handle->cookie;
707 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
709 if (req->rq_export->exp_disconnected) {
710 cfs_spin_lock(&med->med_open_lock);
711 class_handle_unhash(&mfd->mfd_handle);
712 cfs_list_del_init(&mfd->mfd_list);
713 cfs_spin_unlock(&med->med_open_lock);
714 mdt_mfd_close(info, mfd);
716 cfs_spin_lock(&med->med_open_lock);
717 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
718 cfs_spin_unlock(&med->med_open_lock);
721 mdt_empty_transno(info);
729 int mdt_finish_open(struct mdt_thread_info *info,
730 struct mdt_object *p, struct mdt_object *o,
731 __u64 flags, int created, struct ldlm_reply *rep)
733 struct ptlrpc_request *req = mdt_info_req(info);
734 struct obd_export *exp = req->rq_export;
735 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
736 struct md_attr *ma = &info->mti_attr;
737 struct lu_attr *la = &ma->ma_attr;
738 struct mdt_file_data *mfd;
739 struct mdt_body *repbody;
741 int isreg, isdir, islnk;
745 LASSERT(ma->ma_valid & MA_INODE);
747 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
749 isreg = S_ISREG(la->la_mode);
750 isdir = S_ISDIR(la->la_mode);
751 islnk = S_ISLNK(la->la_mode);
752 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
754 if (exp_connect_rmtclient(exp)) {
755 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
757 rc = mdt_pack_remote_perm(info, o, buf);
759 repbody->valid &= ~OBD_MD_FLRMTPERM;
760 repbody->aclsize = 0;
762 repbody->valid |= OBD_MD_FLRMTPERM;
763 repbody->aclsize = sizeof(struct mdt_remote_perm);
766 #ifdef CONFIG_FS_POSIX_ACL
767 else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
768 const struct lu_env *env = info->mti_env;
769 struct md_object *next = mdt_object_child(o);
770 struct lu_buf *buf = &info->mti_buf;
772 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
773 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
775 if (buf->lb_len > 0) {
776 rc = mo_xattr_get(env, next, buf,
777 XATTR_NAME_ACL_ACCESS);
779 if (rc == -ENODATA) {
780 repbody->aclsize = 0;
781 repbody->valid |= OBD_MD_FLACL;
783 } else if (rc == -EOPNOTSUPP) {
786 CERROR("got acl size: %d\n", rc);
789 repbody->aclsize = rc;
790 repbody->valid |= OBD_MD_FLACL;
797 if (info->mti_mdt->mdt_opts.mo_mds_capa &&
798 exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
799 struct lustre_capa *capa;
801 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
803 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
804 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
807 repbody->valid |= OBD_MD_FLMDSCAPA;
809 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
810 exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
811 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
812 struct lustre_capa *capa;
814 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
816 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
817 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
820 repbody->valid |= OBD_MD_FLOSSCAPA;
824 * If we are following a symlink, don't open; and do not return open
825 * handle for special nodes as client required.
827 if (islnk || (!isreg && !isdir &&
828 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
829 lustre_msg_set_transno(req->rq_repmsg, 0);
833 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
836 * We need to return the existing object's fid back, so it is done here,
837 * after preparing the reply.
839 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
842 /* This can't be done earlier, we need to return reply body */
844 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
845 /* We are trying to create or write an existing dir. */
848 } else if (flags & MDS_OPEN_DIRECTORY)
851 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
852 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
857 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
858 cfs_spin_lock(&med->med_open_lock);
859 cfs_list_for_each(t, &med->med_open_head) {
860 mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
861 if (mfd->mfd_xid == req->rq_xid) {
866 cfs_spin_unlock(&med->med_open_lock);
869 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
870 /*set repbody->ea_size for resent case*/
871 if (ma->ma_valid & MA_LOV) {
872 LASSERT(ma->ma_lmm_size != 0);
873 repbody->eadatasize = ma->ma_lmm_size;
875 repbody->valid |= OBD_MD_FLDIREA;
877 repbody->valid |= OBD_MD_FLEASIZE;
883 rc = mdt_mfd_open(info, p, o, flags, created);
887 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
888 struct lsd_client_data *lcd);
890 void mdt_reconstruct_open(struct mdt_thread_info *info,
891 struct mdt_lock_handle *lhc)
893 const struct lu_env *env = info->mti_env;
894 struct mdt_device *mdt = info->mti_mdt;
895 struct req_capsule *pill = info->mti_pill;
896 struct ptlrpc_request *req = mdt_info_req(info);
897 struct tg_export_data *ted = &req->rq_export->exp_target_data;
898 struct lsd_client_data *lcd = ted->ted_lcd;
899 struct md_attr *ma = &info->mti_attr;
900 struct mdt_reint_record *rr = &info->mti_rr;
901 __u32 flags = info->mti_spec.sp_cr_flags;
902 struct ldlm_reply *ldlm_rep;
903 struct mdt_object *parent;
904 struct mdt_object *child;
905 struct mdt_body *repbody;
909 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
910 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
911 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
913 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
914 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
916 ma->ma_need = MA_INODE;
917 if (ma->ma_lmm_size > 0)
918 ma->ma_need |= MA_LOV;
922 mdt_req_from_lcd(req, lcd);
923 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
925 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
926 ldlm_rep->lock_policy_res1, req->rq_status);
928 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
930 /* We did not create successfully, return error to client. */
931 GOTO(out, rc = req->rq_status);
933 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
934 struct obd_export *exp = req->rq_export;
936 * We failed after creation, but we do not know in which step
937 * we failed. So try to check the child object.
939 parent = mdt_object_find(env, mdt, rr->rr_fid1,
941 if (IS_ERR(parent)) {
942 rc = PTR_ERR(parent);
943 LCONSOLE_WARN("Parent "DFID" lookup error %d."
944 " Evicting client %s with export %s.\n",
945 PFID(rr->rr_fid1), rc,
946 obd_uuid2str(&exp->exp_client_uuid),
947 obd_export_nid2str(exp));
948 mdt_export_evict(exp);
951 child = mdt_object_find(env, mdt, rr->rr_fid2,
952 MDT_OBJ_MAY_NOT_EXIST);
955 LCONSOLE_WARN("Child "DFID" lookup error %d."
956 " Evicting client %s with export %s.\n",
957 PFID(mdt_object_fid(child)), rc,
958 obd_uuid2str(&exp->exp_client_uuid),
959 obd_export_nid2str(exp));
960 mdt_object_put(env, parent);
961 mdt_export_evict(exp);
964 rc = mdt_object_exists(child);
966 struct md_object *next;
968 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
969 next = mdt_object_child(child);
970 rc = mo_attr_get(env, next, ma);
972 rc = mdt_finish_open(info, parent, child,
975 /* the child object was created on remote server */
976 repbody->fid1 = *rr->rr_fid2;
977 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
979 } else if (rc == 0) {
980 /* the child does not exist, we should do regular open */
981 mdt_object_put(env, parent);
982 mdt_object_put(env, child);
983 GOTO(regular_open, 0);
985 mdt_object_put(env, parent);
986 mdt_object_put(env, child);
990 /* We did not try to create, so we are a pure open */
991 rc = mdt_reint_open(info, lhc);
997 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
998 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1001 int mdt_open_by_fid(struct mdt_thread_info* info,
1002 struct ldlm_reply *rep)
1004 const struct lu_env *env = info->mti_env;
1005 __u32 flags = info->mti_spec.sp_cr_flags;
1006 struct mdt_reint_record *rr = &info->mti_rr;
1007 struct md_attr *ma = &info->mti_attr;
1008 struct mdt_object *o;
1012 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2,
1013 MDT_OBJ_MUST_EXIST);
1017 rc = mdt_object_exists(o);
1019 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1023 rc = mo_attr_get(env, mdt_object_child(o), ma);
1025 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1026 } else if (rc < 0) {
1027 /* the child object was created on remote server */
1028 struct mdt_body *repbody;
1029 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1030 repbody->fid1 = *rr->rr_fid2;
1031 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1035 mdt_object_put(info->mti_env, o);
1039 int mdt_open_anon_by_fid(struct mdt_thread_info *info,
1040 struct ldlm_reply *rep,
1041 struct mdt_lock_handle *lhc)
1043 const struct lu_env *env = info->mti_env;
1044 struct mdt_device *mdt = info->mti_mdt;
1045 __u32 flags = info->mti_spec.sp_cr_flags;
1046 struct mdt_reint_record *rr = &info->mti_rr;
1047 struct md_attr *ma = &info->mti_attr;
1048 struct mdt_object *parent= NULL;
1049 struct mdt_object *o;
1054 if (md_should_create(flags)) {
1055 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1056 parent = mdt_object_find(env, mdt, rr->rr_fid1,
1057 MDT_OBJ_MUST_EXIST);
1058 if (IS_ERR(parent)) {
1059 CDEBUG(D_INODE, "Fail to find parent "DFID
1060 " for anonymous created %ld, try to"
1061 " use server-side parent.\n",
1062 PFID(rr->rr_fid1), PTR_ERR(parent));
1067 ma->ma_need |= MA_PFID;
1070 o = mdt_object_find(env, mdt, rr->rr_fid2, MDT_OBJ_MUST_EXIST);
1072 if (PTR_ERR(o) == -ENOENT)
1073 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1075 GOTO(out_parent, rc = PTR_ERR(o));
1078 rc = mdt_object_exists(o);
1080 CERROR("NFS remote open shouldn't happen.\n");
1081 GOTO(out_child, rc);
1083 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1087 if (flags & FMODE_WRITE)
1089 else if (flags & MDS_FMODE_EXEC)
1094 mdt_lock_handle_init(lhc);
1095 mdt_lock_reg_init(lhc, lm);
1096 rc = mdt_object_lock(info, o, lhc,
1097 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1100 GOTO(out_child, rc);
1102 rc = mo_attr_get(env, mdt_object_child(o), ma);
1104 GOTO(out_child, rc);
1106 if (ma->ma_valid & MA_PFID) {
1107 parent = mdt_object_find(env, mdt, &ma->ma_pfid,
1108 MDT_OBJ_MUST_EXIST);
1109 if (IS_ERR(parent)) {
1110 CDEBUG(D_INODE, "Fail to find parent "DFID
1111 " for anonymous created %ld, try to"
1112 " use system default.\n",
1113 PFID(&ma->ma_pfid), PTR_ERR(parent));
1118 if (flags & MDS_OPEN_LOCK)
1119 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1120 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1122 if (!(flags & MDS_OPEN_LOCK) || rc)
1123 mdt_object_unlock(info, o, lhc, 1);
1125 GOTO(out_child, rc);
1127 mdt_object_put(env, o);
1130 mdt_object_put(env, parent);
1134 int mdt_pin(struct mdt_thread_info* info)
1137 RETURN(err_serious(-EOPNOTSUPP));
1140 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1141 int mdt_cross_open(struct mdt_thread_info* info,
1142 const struct lu_fid *fid,
1143 struct ldlm_reply *rep, __u32 flags)
1145 struct md_attr *ma = &info->mti_attr;
1146 struct mdt_object *o;
1150 o = mdt_object_find(info->mti_env, info->mti_mdt, fid,
1151 MDT_OBJ_MAY_NOT_EXIST);
1153 RETURN(rc = PTR_ERR(o));
1155 rc = mdt_object_exists(o);
1157 /* Do permission check for cross-open. */
1158 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1159 NULL, flags | MDS_OPEN_CROSS);
1163 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1164 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
1166 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1167 } else if (rc == 0) {
1169 * Something is wrong here. lookup was positive but there is
1172 CERROR("Cross-ref object doesn't exist!\n");
1175 /* Something is wrong here, the object is on another MDS! */
1176 CERROR("The object isn't on this server! FLD error?\n");
1177 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1179 "Object isn't on this server! FLD error?\n");
1185 mdt_object_put(info->mti_env, o);
1189 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1191 struct mdt_device *mdt = info->mti_mdt;
1192 struct ptlrpc_request *req = mdt_info_req(info);
1193 struct mdt_object *parent;
1194 struct mdt_object *child;
1195 struct mdt_lock_handle *lh;
1196 struct ldlm_reply *ldlm_rep;
1197 struct mdt_body *repbody;
1198 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1199 struct md_attr *ma = &info->mti_attr;
1200 __u64 create_flags = info->mti_spec.sp_cr_flags;
1201 struct mdt_reint_record *rr = &info->mti_rr;
1202 struct lu_name *lname;
1208 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1209 (obd_timeout + 1) / 4);
1211 mdt_counter_incr(req->rq_export, LPROC_MDT_OPEN);
1212 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1214 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1215 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1217 ma->ma_need = MA_INODE;
1218 if (ma->ma_lmm_size > 0)
1219 ma->ma_need |= MA_LOV;
1223 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1224 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1226 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1227 CERROR("file join is not supported anymore.\n");
1228 GOTO(out, result = err_serious(-EOPNOTSUPP));
1230 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1232 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1233 info->mti_spec.u.sp_ea.eadata == NULL)
1234 GOTO(out, result = err_serious(-EINVAL));
1236 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1237 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1238 PFID(rr->rr_fid1), rr->rr_name,
1239 PFID(rr->rr_fid2), create_flags,
1240 ma->ma_attr.la_mode, msg_flags);
1242 if (req_is_replay(req) ||
1243 (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1244 /* This is a replay request or from liblustre with ea. */
1245 result = mdt_open_by_fid(info, ldlm_rep);
1247 if (result != -ENOENT) {
1248 if (req->rq_export->exp_libclient &&
1249 create_flags&MDS_OPEN_HAS_EA)
1250 GOTO(out, result = 0);
1254 * We didn't find the correct object, so we need to re-create it
1255 * via a regular replay.
1257 if (!(create_flags & MDS_OPEN_CREAT)) {
1258 DEBUG_REQ(D_ERROR, req,
1259 "OPEN & CREAT not in open replay.");
1260 GOTO(out, result = -EFAULT);
1262 CDEBUG(D_INFO, "Open replay did find object, continue as "
1264 } else if (rr->rr_namelen == 0 && !info->mti_cross_ref &&
1265 create_flags & MDS_OPEN_LOCK) {
1266 result = mdt_open_anon_by_fid(info, ldlm_rep, lhc);
1270 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1271 GOTO(out, result = err_serious(-ENOMEM));
1273 mdt_set_disposition(info, ldlm_rep,
1274 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1276 if (info->mti_cross_ref) {
1277 /* This is cross-ref open */
1278 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1279 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1284 lh = &info->mti_lh[MDT_LH_PARENT];
1285 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1286 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1288 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1289 MDS_INODELOCK_UPDATE, MDT_OBJ_MUST_EXIST);
1291 GOTO(out, result = PTR_ERR(parent));
1293 /* get and check version of parent */
1294 result = mdt_version_get_check(info, parent, 0);
1296 GOTO(out_parent, result);
1298 fid_zero(child_fid);
1300 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1301 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1302 lname, child_fid, &info->mti_spec);
1303 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1304 "looking for "DFID"/%s, result fid="DFID"\n",
1305 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1307 if (result != 0 && result != -ENOENT && result != -ESTALE)
1308 GOTO(out_parent, result);
1310 if (result == -ENOENT || result == -ESTALE) {
1311 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1312 if (result == -ESTALE) {
1314 * -ESTALE means the parent is a dead(unlinked) dir, so
1315 * it should return -ENOENT to in accordance with the
1316 * original mds implementaion.
1318 GOTO(out_parent, result = -ENOENT);
1320 if (!(create_flags & MDS_OPEN_CREAT))
1321 GOTO(out_parent, result);
1322 *child_fid = *info->mti_rr.rr_fid2;
1323 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1327 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1328 * return FID back in that case.
1330 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1333 child = mdt_object_find(info->mti_env, mdt, child_fid,
1334 MDT_OBJ_MAY_NOT_EXIST);
1336 GOTO(out_parent, result = PTR_ERR(child));
1338 /** check version of child */
1339 rc = mdt_version_get_check(info, child, 1);
1341 GOTO(out_child, result = rc);
1343 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1344 if (result == -ENOENT) {
1345 /* save versions in reply */
1346 mdt_version_get_save(info, parent, 0);
1347 mdt_version_get_save(info, child, 1);
1349 /* version of child will be changed */
1350 info->mti_mos = child;
1352 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1353 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1355 /* Let lower layers know what is lock mode on directory. */
1356 info->mti_spec.sp_cr_mode =
1357 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1360 * Do not perform lookup sanity check. We know that name does
1363 info->mti_spec.sp_cr_lookup = 0;
1364 info->mti_spec.sp_feat = &dt_directory_features;
1366 result = mdo_create(info->mti_env,
1367 mdt_object_child(parent),
1369 mdt_object_child(child),
1372 if (result == -ERESTART) {
1373 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1374 GOTO(out_child, result);
1377 GOTO(out_child, result);
1381 /* We have to get attr & lov ea for this object */
1382 result = mo_attr_get(info->mti_env, mdt_object_child(child),
1385 * The object is on remote node, return its FID for remote open.
1387 if (result == -EREMOTE) {
1389 * Check if this lock already was sent to client and
1390 * this is resent case. For resent case do not take lock
1391 * again, use what is already granted.
1393 LASSERT(lhc != NULL);
1395 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1396 struct ldlm_lock *lock;
1398 LASSERT(msg_flags & MSG_RESENT);
1400 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1402 CERROR("Invalid lock handle "LPX64"\n",
1403 lhc->mlh_reg_lh.cookie);
1406 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1407 &lock->l_resource->lr_name));
1408 LDLM_LOCK_PUT(lock);
1411 mdt_lock_handle_init(lhc);
1412 mdt_lock_reg_init(lhc, LCK_PR);
1414 rc = mdt_object_lock(info, child, lhc,
1415 MDS_INODELOCK_LOOKUP,
1418 repbody->fid1 = *mdt_object_fid(child);
1419 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1422 GOTO(out_child, result);
1426 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1428 /* get openlock if this is not replay and if a client requested it */
1429 if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
1432 if (create_flags & FMODE_WRITE)
1434 else if (create_flags & MDS_FMODE_EXEC)
1438 mdt_lock_handle_init(lhc);
1439 mdt_lock_reg_init(lhc, lm);
1440 rc = mdt_object_lock(info, child, lhc,
1441 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1445 GOTO(out_child, result);
1448 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1452 /* Try to open it now. */
1453 rc = mdt_finish_open(info, parent, child, create_flags,
1457 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1458 /* openlock was acquired and mdt_finish_open failed -
1459 drop the openlock */
1460 mdt_object_unlock(info, child, lhc, 1);
1464 ma->ma_cookie_size = 0;
1465 info->mti_no_need_trans = 1;
1466 rc = mdo_unlink(info->mti_env,
1467 mdt_object_child(parent),
1468 mdt_object_child(child),
1472 CERROR("Error in cleanup of open\n");
1477 mdt_object_put(info->mti_env, child);
1479 mdt_object_unlock_put(info, parent, lh, result || !created);
1481 if (result && result != -EREMOTE)
1482 lustre_msg_set_transno(req->rq_repmsg, 0);
1486 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1487 MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1489 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1491 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1494 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1496 struct mdt_object *o = mfd->mfd_object;
1497 struct md_object *next = mdt_object_child(o);
1498 struct md_attr *ma = &info->mti_attr;
1499 int ret = MDT_IOEPOCH_CLOSED;
1504 mode = mfd->mfd_mode;
1506 if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1508 ret = mdt_ioepoch_close(info, o);
1509 } else if (mode & MDS_FMODE_EXEC) {
1511 } else if (mode & MDS_FMODE_EPOCH) {
1512 ret = mdt_ioepoch_close(info, o);
1513 } else if (mode & MDS_FMODE_SOM) {
1514 ret = mdt_som_au_close(info, o);
1517 /* Update atime on close only. */
1518 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1519 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1520 /* Set the atime only. */
1521 ma->ma_valid = MA_INODE;
1522 ma->ma_attr.la_valid = LA_ATIME;
1523 rc = mo_attr_set(info->mti_env, next, ma);
1526 ma->ma_need |= MA_INODE;
1527 ma->ma_valid &= ~MA_INODE;
1529 if (!MFD_CLOSED(mode))
1530 rc = mo_close(info->mti_env, next, ma, mode);
1532 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1533 struct mdt_export_data *med;
1535 /* The IOepoch is still opened or SOM update is needed.
1536 * Put mfd back into the list. */
1537 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1538 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1539 MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1541 LASSERT(mdt_info_req(info));
1542 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1543 cfs_spin_lock(&med->med_open_lock);
1544 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1545 class_handle_hash_back(&mfd->mfd_handle);
1546 cfs_spin_unlock(&med->med_open_lock);
1548 if (ret == MDT_IOEPOCH_OPENED) {
1552 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1553 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1557 mdt_object_put(info->mti_env, o);
1560 RETURN(rc ? rc : ret);
1563 int mdt_close(struct mdt_thread_info *info)
1565 struct mdt_export_data *med;
1566 struct mdt_file_data *mfd;
1567 struct mdt_object *o;
1568 struct md_attr *ma = &info->mti_attr;
1569 struct mdt_body *repbody = NULL;
1570 struct ptlrpc_request *req = mdt_info_req(info);
1574 mdt_counter_incr(req->rq_export, LPROC_MDT_CLOSE);
1575 /* Close may come with the Size-on-MDS update. Unpack it. */
1576 rc = mdt_close_unpack(info);
1578 RETURN(err_serious(rc));
1580 LASSERT(info->mti_ioepoch);
1582 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1583 info->mti_mdt->mdt_max_mdsize);
1584 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1585 info->mti_mdt->mdt_max_cookiesize);
1586 rc = req_capsule_server_pack(info->mti_pill);
1587 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1588 mdt_client_compatibility(info);
1590 mdt_shrink_reply(info);
1591 RETURN(lustre_msg_get_status(req->rq_repmsg));
1594 /* Continue to close handle even if we can not pack reply */
1596 repbody = req_capsule_server_get(info->mti_pill,
1598 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1600 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1603 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1605 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1608 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1609 repbody->eadatasize = 0;
1610 repbody->aclsize = 0;
1612 rc = err_serious(rc);
1615 med = &req->rq_export->exp_mdt_data;
1616 cfs_spin_lock(&med->med_open_lock);
1617 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1618 if (mdt_mfd_closed(mfd)) {
1619 cfs_spin_unlock(&med->med_open_lock);
1620 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1621 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1622 info->mti_ioepoch->handle.cookie);
1623 /** not serious error since bug 3633 */
1626 class_handle_unhash(&mfd->mfd_handle);
1627 cfs_list_del_init(&mfd->mfd_list);
1628 cfs_spin_unlock(&med->med_open_lock);
1630 /* Do not lose object before last unlink. */
1631 o = mfd->mfd_object;
1632 mdt_object_get(info->mti_env, o);
1633 ret = mdt_mfd_close(info, mfd);
1634 if (repbody != NULL)
1635 rc = mdt_handle_last_unlink(info, o, ma);
1636 mdt_empty_transno(info);
1637 mdt_object_put(info->mti_env, o);
1639 if (repbody != NULL) {
1640 mdt_client_compatibility(info);
1641 mdt_shrink_reply(info);
1644 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1645 RETURN(err_serious(-ENOMEM));
1647 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1648 OBD_FAIL_MDS_CLOSE_NET_REP))
1649 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1650 RETURN(rc ? rc : ret);
1654 * DONE_WRITING rpc handler.
1656 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1657 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1658 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1659 * skip attributes and reconstruct the reply here.
1661 int mdt_done_writing(struct mdt_thread_info *info)
1663 struct ptlrpc_request *req = mdt_info_req(info);
1664 struct mdt_body *repbody = NULL;
1665 struct mdt_export_data *med;
1666 struct mdt_file_data *mfd;
1670 rc = req_capsule_server_pack(info->mti_pill);
1672 RETURN(err_serious(rc));
1674 repbody = req_capsule_server_get(info->mti_pill,
1676 repbody->eadatasize = 0;
1677 repbody->aclsize = 0;
1679 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1680 rc = mdt_close_unpack(info);
1682 RETURN(err_serious(rc));
1684 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1685 RETURN(lustre_msg_get_status(req->rq_repmsg));
1687 med = &info->mti_exp->exp_mdt_data;
1688 cfs_spin_lock(&med->med_open_lock);
1689 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1691 cfs_spin_unlock(&med->med_open_lock);
1692 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1693 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1694 PFID(info->mti_rr.rr_fid1),
1695 info->mti_ioepoch->handle.cookie,
1696 info->mti_ioepoch->ioepoch);
1697 /* If this is a replay, reconstruct the transno. */
1698 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1699 mdt_empty_transno(info);
1700 RETURN(info->mti_ioepoch->flags & MF_SOM_AU ?
1706 LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1707 mfd->mfd_mode == MDS_FMODE_TRUNC);
1708 class_handle_unhash(&mfd->mfd_handle);
1709 cfs_list_del_init(&mfd->mfd_list);
1710 cfs_spin_unlock(&med->med_open_lock);
1712 /* Set EPOCH CLOSE flag if not set by client. */
1713 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1714 info->mti_attr.ma_valid = 0;
1716 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1717 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1718 if (info->mti_attr.ma_lmm == NULL)
1721 rc = mdt_mfd_close(info, mfd);
1723 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1724 mdt_empty_transno(info);