1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_open.c
38 * Lustre Metadata Target (mdt) open/close file handling
40 * Author: Huang Hua <huanghua@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <lustre_acl.h>
49 #include <lustre_mds.h>
50 #include "mdt_internal.h"
52 /* we do nothing because we do not have refcount now */
53 static void mdt_mfd_get(void *mfdp)
57 /* Create a new mdt_file_data struct, initialize it,
58 * and insert it to global hash table */
59 struct mdt_file_data *mdt_mfd_new(void)
61 struct mdt_file_data *mfd;
66 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
67 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
68 class_handle_hash(&mfd->mfd_handle, mdt_mfd_get);
74 * Find the mfd pointed to by handle in global hash table.
75 * In case of replay the handle is obsoleted
76 * but mfd can be found in mfd list by that handle
78 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
79 const struct lustre_handle *handle)
81 struct ptlrpc_request *req = mdt_info_req(info);
82 struct mdt_file_data *mfd;
85 LASSERT(handle != NULL);
86 mfd = class_handle2object(handle->cookie);
87 /* during dw/setattr replay the mfd can be found by old handle */
89 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
90 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
91 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
92 if (mfd->mfd_old_handle.cookie == handle->cookie)
101 void mdt_mfd_free(struct mdt_file_data *mfd)
103 LASSERT(list_empty(&mfd->mfd_list));
104 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
107 static int mdt_create_data(struct mdt_thread_info *info,
108 struct mdt_object *p, struct mdt_object *o)
110 struct md_op_spec *spec = &info->mti_spec;
111 struct md_attr *ma = &info->mti_attr;
115 if (!md_should_create(spec->sp_cr_flags))
118 ma->ma_need = MA_INODE | MA_LOV;
120 rc = mdo_create_data(info->mti_env,
121 p ? mdt_object_child(p) : NULL,
122 mdt_object_child(o), spec, ma);
126 static int mdt_ioepoch_opened(struct mdt_object *mo)
128 return mo->mot_ioepoch_count;
131 int mdt_object_is_som_enabled(struct mdt_object *mo)
133 return !mo->mot_ioepoch;
137 * Re-enable Size-on-MDS.
138 * Call under ->mot_ioepoch_sem.
140 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
142 if (ioepoch == mo->mot_ioepoch) {
143 LASSERT(!mdt_ioepoch_opened(mo));
150 * Open the IOEpoch. It is allowed if @writecount is not negative.
151 * The epoch and writecount handling is performed under the mot_ioepoch_sem.
153 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
156 struct mdt_device *mdt = info->mti_mdt;
161 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
162 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
165 down(&o->mot_ioepoch_sem);
166 if (mdt_ioepoch_opened(o)) {
167 /* Epoch continues even if there is no writers yet. */
168 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
169 o->mot_ioepoch, PFID(mdt_object_fid(o)));
171 /* XXX: ->mdt_ioepoch is not initialized at the mount */
172 spin_lock(&mdt->mdt_ioepoch_lock);
173 if (mdt->mdt_ioepoch < info->mti_replayepoch)
174 mdt->mdt_ioepoch = info->mti_replayepoch;
176 if (info->mti_replayepoch)
177 o->mot_ioepoch = info->mti_replayepoch;
178 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
179 o->mot_ioepoch = ++mdt->mdt_ioepoch;
181 o->mot_ioepoch = mdt->mdt_ioepoch;
183 spin_unlock(&mdt->mdt_ioepoch_lock);
185 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
186 o->mot_ioepoch, PFID(mdt_object_fid(o)));
188 o->mot_flags |= MOF_SOM_CREATED;
191 o->mot_ioepoch_count++;
192 up(&o->mot_ioepoch_sem);
194 /* Cancel Size-on-MDS attributes cached on clients for the open case.
195 * In the truncate case, see mdt_reint_setattr(). */
196 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
197 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
198 mdt_lock_reg_init(lh, LCK_EX);
199 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
202 mdt_object_unlock(info, o, lh, 1);
208 * Update SOM on-disk attributes.
209 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
210 * mountid and attributes. If disabling, zero IOEpoch id in lustre-ea.
211 * Call under ->mot_ioepoch_sem.
213 static int mdt_som_attr_set(struct mdt_thread_info *info,
214 struct mdt_object *obj, __u64 ioepoch, int enable)
216 struct md_attr *ma = &info->mti_attr;
220 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
221 " on "DFID".\n", enable ? "update" : "disabling",
222 ioepoch, PFID(mdt_object_fid(obj)));
224 ma->ma_valid |= MA_SOM;
225 ma->ma_som = &info->mti_u.som.data;
227 struct mdt_device *mdt = info->mti_mdt;
228 struct lu_attr *la = &ma->ma_attr;
230 ma->ma_som->msd_ioepoch = ioepoch;
231 ma->ma_som->msd_size = la->la_valid & LA_SIZE ? la->la_size : 0;
232 ma->ma_som->msd_blocks = la->la_valid & LA_BLOCKS ?
234 ma->ma_som->msd_mountid = mdt->mdt_mount_count;
235 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
237 ma->ma_som->msd_ioepoch = IOEPOCH_INVAL;
238 ma->ma_attr.la_valid &= LA_ATIME;
241 /* Since we have opened the file, it is unnecessary
242 * to check permission when close it. Between the "open"
243 * and "close", maybe someone has changed the file mode
244 * or flags, or the file created mode do not permit wirte,
245 * and so on. Just set MDS_PERM_BYPASS for all the cases. */
246 ma->ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
248 rc = mdt_attr_set(info, obj, ma, 0);
252 /** Perform the eviction specific actions on ioepoch close. */
253 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
254 struct mdt_object *o)
258 down(&o->mot_ioepoch_sem);
259 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
260 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
261 o->mot_ioepoch_count);
262 o->mot_ioepoch_count--;
264 /* If eviction occured set MOF_SOM_RECOV,
265 * if no other epoch holders, disable SOM on disk. */
266 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
267 if (!mdt_ioepoch_opened(o)) {
268 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
269 mdt_object_som_enable(o, o->mot_ioepoch);
271 up(&o->mot_ioepoch_sem);
276 * Perform the replay specific actions on ioepoch close.
277 * Skip SOM attribute update if obtained and just forget about the inode state
278 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
280 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
281 struct mdt_object *o)
283 int rc = MDT_IOEPOCH_CLOSED;
286 down(&o->mot_ioepoch_sem);
287 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
288 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
289 o->mot_ioepoch_count--;
291 /* Get an info from the replayed request if client is supposed
292 * to send an Attibute Update, reconstruct @rc if so */
293 if (info->mti_ioepoch->flags & MF_SOM_AU)
294 rc = MDT_IOEPOCH_GETATTR;
296 if (!mdt_ioepoch_opened(o))
297 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
298 up(&o->mot_ioepoch_sem);
304 * Regular file IOepoch close.
305 * Closes the ioepoch, checks the object state, apply obtained attributes and
306 * re-enable SOM on the object, if possible. Also checks if the recovery is
307 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
308 * to obtain SOM attributes under the server-side OST locks.
311 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
312 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
314 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
315 struct mdt_object *o)
317 struct md_attr *tmp_ma;
321 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
324 la = &info->mti_attr.ma_attr;
325 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
327 down(&o->mot_ioepoch_sem);
328 o->mot_ioepoch_count--;
330 tmp_ma = &info->mti_u.som.attr;
331 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
332 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
333 tmp_ma->ma_som = &info->mti_u.som.data;
334 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
335 tmp_ma->ma_valid = 0;
336 rc = mo_attr_get(info->mti_env, mdt_object_child(o), tmp_ma);
340 /* Check the on-disk SOM state. */
341 if (o->mot_flags & MOF_SOM_RECOV)
343 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
344 !(tmp_ma->ma_valid & MA_SOM))
347 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
348 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
350 opened = mdt_ioepoch_opened(o);
352 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
353 * Skip the check for file with no LOV or for unlink files.
355 if (!opened && tmp_ma->ma_valid & MA_LOV &&
356 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
358 /* If some previous writer was evicted, re-ask the
359 * client for attributes. Even if attributes are
360 * provided, we cannot believe in them.
361 * Another use case is that there is no SOM cache on
362 * disk -- first access with SOM or there was an MDS
364 ret = MDT_IOEPOCH_GETATTR;
365 else if (o->mot_flags & MOF_SOM_CHANGE)
366 /* Some previous writer changed the attribute.
367 * Do not believe to the current Size-on-MDS
368 * update, re-ask client. */
369 ret = MDT_IOEPOCH_GETATTR;
370 else if (!(la->la_valid & LA_SIZE) && achange)
371 /* Attributes were changed by the last writer
372 * only but no Size-on-MDS update is received.*/
373 ret = MDT_IOEPOCH_GETATTR;
376 if (achange || ret == MDT_IOEPOCH_GETATTR)
377 o->mot_flags |= MOF_SOM_CHANGE;
379 /* If epoch ends and relable SOM attributes are obtained, update them.
380 * Create SOM ea for new files even if there is no attributes obtained
381 * (0-length file). */
382 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
383 if (achange || o->mot_flags & MOF_SOM_CREATED) {
384 LASSERT(achange || !(la->la_valid & LA_SIZE));
385 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
387 /* Avoid the following setattrs of these attributes,
388 * e.g. for atime update. */
389 info->mti_attr.ma_valid = 0;
391 mdt_object_som_enable(o, o->mot_ioepoch);
394 up(&o->mot_ioepoch_sem);
395 /* If recovery is needed, tell the client to perform GETATTR under
397 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
398 struct mdt_body *rep;
399 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
400 rep->valid |= OBD_MD_FLGETATTRLOCK;
406 up(&o->mot_ioepoch_sem);
411 * Close IOEpoch (opened file or FMODE_EPOCH state). It happens if:
412 * - a client closes the IOEpoch;
413 * - a client eviction occured.
415 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
416 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
417 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
420 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
422 struct ptlrpc_request *req = mdt_info_req(info);
425 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
426 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
429 LASSERT(o->mot_ioepoch_count);
430 LASSERT(info->mti_ioepoch == NULL ||
431 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
433 /* IOEpoch is closed only if client tells about it or eviction occures.
434 * In the replay case, always close the epoch. */
436 RETURN(mdt_ioepoch_close_on_eviction(info, o));
437 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
438 RETURN(mdt_ioepoch_close_on_replay(info, o));
439 if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
440 RETURN(mdt_ioepoch_close_reg(info, o));
441 /* IO epoch is not closed. */
442 RETURN(MDT_IOEPOCH_OPENED);
446 * Close FMODE_SOM state, when IOEpoch is already closed and we are waiting for
447 * attribute update. It happens if:
448 * - SOM Attribute Update is obtained;
449 * - the client failed to obtain it and informs MDS about it;
450 * - a client eviction occured.
451 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
454 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
456 struct ptlrpc_request *req = mdt_info_req(info);
458 int act = MDT_SOM_ENABLE;
462 LASSERT(!req || info->mti_ioepoch);
463 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
464 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
467 /* No size whereas MF_SOM_CHANGE is set means client failed to
468 * obtain ost attributes, drop the SOM cache on disk if so. */
470 (info->mti_ioepoch &&
471 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
472 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
473 act = MDT_SOM_DISABLE;
475 down(&o->mot_ioepoch_sem);
476 /* Mark the object it is the recovery state if we failed to obtain
478 if (act == MDT_SOM_DISABLE)
479 o->mot_flags |= MOF_SOM_RECOV;
481 if (!mdt_ioepoch_opened(o)) {
482 ioepoch = info->mti_ioepoch ?
483 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
485 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
486 rc = mdt_som_attr_set(info, o, ioepoch, act);
487 mdt_object_som_enable(o, ioepoch);
489 up(&o->mot_ioepoch_sem);
493 int mdt_write_read(struct mdt_object *o)
497 down(&o->mot_ioepoch_sem);
498 rc = o->mot_writecount;
499 up(&o->mot_ioepoch_sem);
503 int mdt_write_get(struct mdt_object *o)
507 down(&o->mot_ioepoch_sem);
508 if (o->mot_writecount < 0)
512 up(&o->mot_ioepoch_sem);
516 void mdt_write_put(struct mdt_object *o)
519 down(&o->mot_ioepoch_sem);
521 up(&o->mot_ioepoch_sem);
525 static int mdt_write_deny(struct mdt_object *o)
529 down(&o->mot_ioepoch_sem);
530 if (o->mot_writecount > 0)
534 up(&o->mot_ioepoch_sem);
538 static void mdt_write_allow(struct mdt_object *o)
541 down(&o->mot_ioepoch_sem);
543 up(&o->mot_ioepoch_sem);
547 /* there can be no real transaction so prepare the fake one */
548 static void mdt_empty_transno(struct mdt_thread_info* info)
550 struct mdt_device *mdt = info->mti_mdt;
551 struct ptlrpc_request *req = mdt_info_req(info);
554 /* transaction has occurred already */
555 if (lustre_msg_get_transno(req->rq_repmsg) != 0) {
560 spin_lock(&mdt->mdt_transno_lock);
561 if (info->mti_transno == 0) {
562 info->mti_transno = ++ mdt->mdt_last_transno;
564 /* should be replay */
565 if (info->mti_transno > mdt->mdt_last_transno)
566 mdt->mdt_last_transno = info->mti_transno;
568 spin_unlock(&mdt->mdt_transno_lock);
570 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
572 req->rq_export->exp_obd->obd_last_committed);
574 req->rq_transno = info->mti_transno;
575 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
579 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
581 LASSERT(mfd != NULL);
583 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
584 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
586 mfd->mfd_mode = mode;
589 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
590 struct mdt_object *o, int flags, int created)
592 struct ptlrpc_request *req = mdt_info_req(info);
593 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
594 struct mdt_file_data *mfd;
595 struct md_attr *ma = &info->mti_attr;
596 struct lu_attr *la = &ma->ma_attr;
597 struct mdt_body *repbody;
598 int rc = 0, isdir, isreg;
601 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
603 isreg = S_ISREG(la->la_mode);
604 isdir = S_ISDIR(la->la_mode);
605 if (isreg && !(ma->ma_valid & MA_LOV)) {
607 * No EA, check whether it is will set regEA and dirEA since in
608 * above attr get, these size might be zero, so reset it, to
609 * retrieve the MD after create obj.
611 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
614 /* in replay case, p == NULL */
615 rc = mdt_create_data(info, p, o);
620 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
621 ma->ma_valid, ma->ma_lmm_size);
623 if (ma->ma_valid & MA_LOV) {
624 LASSERT(ma->ma_lmm_size != 0);
625 repbody->eadatasize = ma->ma_lmm_size;
627 repbody->valid |= OBD_MD_FLDIREA;
629 repbody->valid |= OBD_MD_FLEASIZE;
632 if (flags & FMODE_WRITE) {
633 rc = mdt_write_get(o);
635 mdt_ioepoch_open(info, o, created);
636 repbody->ioepoch = o->mot_ioepoch;
638 } else if (flags & MDS_FMODE_EXEC) {
639 rc = mdt_write_deny(o);
644 rc = mo_open(info->mti_env, mdt_object_child(o),
645 created ? flags | MDS_OPEN_CREATED : flags);
652 * Keep a reference on this object for this open, and is
653 * released by mdt_mfd_close().
655 mdt_object_get(info->mti_env, o);
658 * @flags is always not zero. At least it should be FMODE_READ,
659 * FMODE_WRITE or FMODE_EXEC.
664 mdt_mfd_set_mode(mfd, flags);
667 mfd->mfd_xid = req->rq_xid;
670 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
671 struct mdt_file_data *old_mfd;
672 /* Check wheather old cookie already exist in
673 * the list, becasue when do recovery, client
674 * might be disconnected from server, and
675 * restart replay, so there maybe some orphan
676 * mfd here, we should remove them */
677 LASSERT(info->mti_rr.rr_handle != NULL);
678 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
680 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
683 PFID(mdt_object_fid(mfd->mfd_object)),
684 info->mti_rr.rr_handle->cookie);
685 spin_lock(&med->med_open_lock);
686 class_handle_unhash(&old_mfd->mfd_handle);
687 list_del_init(&old_mfd->mfd_list);
688 spin_unlock(&med->med_open_lock);
689 mdt_mfd_close(info, old_mfd);
691 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
692 info->mti_rr.rr_handle->cookie);
693 mfd->mfd_old_handle.cookie =
694 info->mti_rr.rr_handle->cookie;
696 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
698 if (req->rq_export->exp_disconnected) {
699 spin_lock(&med->med_open_lock);
700 class_handle_unhash(&mfd->mfd_handle);
701 list_del_init(&mfd->mfd_list);
702 spin_unlock(&med->med_open_lock);
703 mdt_mfd_close(info, mfd);
705 spin_lock(&med->med_open_lock);
706 list_add(&mfd->mfd_list, &med->med_open_head);
707 spin_unlock(&med->med_open_lock);
710 mdt_empty_transno(info);
718 static int mdt_finish_open(struct mdt_thread_info *info,
719 struct mdt_object *p, struct mdt_object *o,
720 int flags, int created, struct ldlm_reply *rep)
722 struct ptlrpc_request *req = mdt_info_req(info);
723 struct obd_export *exp = req->rq_export;
724 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
725 struct md_attr *ma = &info->mti_attr;
726 struct lu_attr *la = &ma->ma_attr;
727 struct mdt_file_data *mfd;
728 struct mdt_body *repbody;
730 int isreg, isdir, islnk;
734 LASSERT(ma->ma_valid & MA_INODE);
736 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
738 isreg = S_ISREG(la->la_mode);
739 isdir = S_ISDIR(la->la_mode);
740 islnk = S_ISLNK(la->la_mode);
741 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
743 if (exp_connect_rmtclient(exp)) {
744 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
746 rc = mdt_pack_remote_perm(info, o, buf);
748 repbody->valid &= ~OBD_MD_FLRMTPERM;
749 repbody->aclsize = 0;
751 repbody->valid |= OBD_MD_FLRMTPERM;
752 repbody->aclsize = sizeof(struct mdt_remote_perm);
755 #ifdef CONFIG_FS_POSIX_ACL
756 else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
757 const struct lu_env *env = info->mti_env;
758 struct md_object *next = mdt_object_child(o);
759 struct lu_buf *buf = &info->mti_buf;
761 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
762 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
764 if (buf->lb_len > 0) {
765 rc = mo_xattr_get(env, next, buf,
766 XATTR_NAME_ACL_ACCESS);
768 if (rc == -ENODATA) {
769 repbody->aclsize = 0;
770 repbody->valid |= OBD_MD_FLACL;
772 } else if (rc == -EOPNOTSUPP) {
775 CERROR("got acl size: %d\n", rc);
778 repbody->aclsize = rc;
779 repbody->valid |= OBD_MD_FLACL;
786 if (info->mti_mdt->mdt_opts.mo_mds_capa &&
787 exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
788 struct lustre_capa *capa;
790 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
792 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
793 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
796 repbody->valid |= OBD_MD_FLMDSCAPA;
798 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
799 exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
800 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
801 struct lustre_capa *capa;
803 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
805 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
806 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
809 repbody->valid |= OBD_MD_FLOSSCAPA;
813 * If we are following a symlink, don't open; and do not return open
814 * handle for special nodes as client required.
816 if (islnk || (!isreg && !isdir &&
817 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
818 lustre_msg_set_transno(req->rq_repmsg, 0);
822 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
825 * We need to return the existing object's fid back, so it is done here,
826 * after preparing the reply.
828 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
831 /* This can't be done earlier, we need to return reply body */
833 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
834 /* We are trying to create or write an existing dir. */
837 } else if (flags & MDS_OPEN_DIRECTORY)
840 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
841 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
846 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
847 spin_lock(&med->med_open_lock);
848 list_for_each(t, &med->med_open_head) {
849 mfd = list_entry(t, struct mdt_file_data, mfd_list);
850 if (mfd->mfd_xid == req->rq_xid) {
855 spin_unlock(&med->med_open_lock);
858 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
859 /*set repbody->ea_size for resent case*/
860 if (ma->ma_valid & MA_LOV) {
861 LASSERT(ma->ma_lmm_size != 0);
862 repbody->eadatasize = ma->ma_lmm_size;
864 repbody->valid |= OBD_MD_FLDIREA;
866 repbody->valid |= OBD_MD_FLEASIZE;
872 rc = mdt_mfd_open(info, p, o, flags, created);
876 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
877 struct lsd_client_data *lcd);
879 void mdt_reconstruct_open(struct mdt_thread_info *info,
880 struct mdt_lock_handle *lhc)
882 const struct lu_env *env = info->mti_env;
883 struct mdt_device *mdt = info->mti_mdt;
884 struct req_capsule *pill = info->mti_pill;
885 struct ptlrpc_request *req = mdt_info_req(info);
886 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
887 struct lsd_client_data *lcd = med->med_lcd;
888 struct md_attr *ma = &info->mti_attr;
889 struct mdt_reint_record *rr = &info->mti_rr;
890 __u32 flags = info->mti_spec.sp_cr_flags;
891 struct ldlm_reply *ldlm_rep;
892 struct mdt_object *parent;
893 struct mdt_object *child;
894 struct mdt_body *repbody;
898 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
899 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
900 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
902 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
903 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
905 ma->ma_need = MA_INODE;
906 if (ma->ma_lmm_size > 0)
907 ma->ma_need |= MA_LOV;
911 mdt_req_from_lcd(req, med->med_lcd);
912 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
914 CERROR("This is reconstruct open: disp="LPX64", result=%d\n",
915 ldlm_rep->lock_policy_res1, req->rq_status);
917 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
919 /* We did not create successfully, return error to client. */
920 GOTO(out, rc = req->rq_status);
922 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
923 struct obd_export *exp = req->rq_export;
925 * We failed after creation, but we do not know in which step
926 * we failed. So try to check the child object.
928 parent = mdt_object_find(env, mdt, rr->rr_fid1);
929 if (IS_ERR(parent)) {
930 rc = PTR_ERR(parent);
931 LCONSOLE_WARN("Parent "DFID" lookup error %d."
932 " Evicting client %s with export %s.\n",
933 PFID(mdt_object_fid(parent)), rc,
934 obd_uuid2str(&exp->exp_client_uuid),
935 obd_export_nid2str(exp));
936 mdt_export_evict(exp);
940 child = mdt_object_find(env, mdt, rr->rr_fid2);
943 LCONSOLE_WARN("Child "DFID" lookup error %d."
944 " Evicting client %s with export %s.\n",
945 PFID(mdt_object_fid(child)), rc,
946 obd_uuid2str(&exp->exp_client_uuid),
947 obd_export_nid2str(exp));
948 mdt_object_put(env, parent);
949 mdt_export_evict(exp);
953 rc = mdt_object_exists(child);
955 struct md_object *next;
957 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
958 next = mdt_object_child(child);
959 rc = mo_attr_get(env, next, ma);
961 rc = mdt_finish_open(info, parent, child,
964 /* the child object was created on remote server */
965 repbody->fid1 = *rr->rr_fid2;
966 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
968 } else if (rc == 0) {
969 /* the child does not exist, we should do regular open */
970 mdt_object_put(env, parent);
971 mdt_object_put(env, child);
972 GOTO(regular_open, 0);
974 mdt_object_put(env, parent);
975 mdt_object_put(env, child);
979 /* We did not try to create, so we are a pure open */
980 rc = mdt_reint_open(info, lhc);
986 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
987 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
990 static int mdt_open_by_fid(struct mdt_thread_info* info,
991 struct ldlm_reply *rep)
993 const struct lu_env *env = info->mti_env;
994 __u32 flags = info->mti_spec.sp_cr_flags;
995 struct mdt_reint_record *rr = &info->mti_rr;
996 struct md_attr *ma = &info->mti_attr;
997 struct mdt_object *o;
1001 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1003 RETURN(rc = PTR_ERR(o));
1005 rc = mdt_object_exists(o);
1007 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1011 rc = mo_attr_get(env, mdt_object_child(o), ma);
1013 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1014 } else if (rc == 0) {
1017 /* the child object was created on remote server */
1018 struct mdt_body *repbody;
1019 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1020 repbody->fid1 = *rr->rr_fid2;
1021 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1025 mdt_object_put(info->mti_env, o);
1029 static int mdt_open_anon_by_fid(struct mdt_thread_info* info,
1030 struct ldlm_reply *rep,
1031 struct mdt_lock_handle *lhc)
1033 __u32 flags = info->mti_spec.sp_cr_flags;
1034 struct mdt_reint_record *rr = &info->mti_rr;
1035 struct md_attr *ma = &info->mti_attr;
1036 struct mdt_object *o;
1041 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1043 RETURN(rc = PTR_ERR(o));
1045 rc = mdt_object_exists(o);
1047 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1049 GOTO(out, rc = -ENOENT);
1050 } else if (rc < 0) {
1051 CERROR("NFS remote open shouldn't happen.\n");
1055 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1059 if (flags & FMODE_WRITE)
1061 else if (flags & MDS_FMODE_EXEC)
1066 mdt_lock_handle_init(lhc);
1067 mdt_lock_reg_init(lhc, lm);
1068 rc = mdt_object_lock(info, o, lhc,
1069 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1074 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
1078 if (flags & MDS_OPEN_LOCK)
1079 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1080 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1082 if (!(flags & MDS_OPEN_LOCK))
1083 mdt_object_unlock(info, o, lhc, 1);
1087 mdt_object_put(info->mti_env, o);
1091 int mdt_pin(struct mdt_thread_info* info)
1094 RETURN(err_serious(-EOPNOTSUPP));
1097 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1098 static int mdt_cross_open(struct mdt_thread_info* info,
1099 const struct lu_fid *fid,
1100 struct ldlm_reply *rep, __u32 flags)
1102 struct md_attr *ma = &info->mti_attr;
1103 struct mdt_object *o;
1107 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1109 RETURN(rc = PTR_ERR(o));
1111 rc = mdt_object_exists(o);
1113 /* Do permission check for cross-open. */
1114 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1115 NULL, flags | MDS_OPEN_CROSS);
1119 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1120 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
1122 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1123 } else if (rc == 0) {
1125 * Something is wrong here. lookup was positive but there is
1128 CERROR("Cross-ref object doesn't exist!\n");
1131 /* Something is wrong here, the object is on another MDS! */
1132 CERROR("The object isn't on this server! FLD error?\n");
1133 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1135 "Object isn't on this server! FLD error?\n");
1141 mdt_object_put(info->mti_env, o);
1145 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1147 struct mdt_device *mdt = info->mti_mdt;
1148 struct ptlrpc_request *req = mdt_info_req(info);
1149 struct mdt_object *parent;
1150 struct mdt_object *child;
1151 struct mdt_lock_handle *lh;
1152 struct ldlm_reply *ldlm_rep;
1153 struct mdt_body *repbody;
1154 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1155 struct md_attr *ma = &info->mti_attr;
1156 __u32 create_flags = info->mti_spec.sp_cr_flags;
1157 struct mdt_reint_record *rr = &info->mti_rr;
1158 struct lu_name *lname;
1164 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1165 (obd_timeout + 1) / 4);
1167 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1169 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1170 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1172 ma->ma_need = MA_INODE;
1173 if (ma->ma_lmm_size > 0)
1174 ma->ma_need |= MA_LOV;
1178 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1179 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1181 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1182 CERROR("file join is not supported anymore.\n");
1183 GOTO(out, result = err_serious(-EOPNOTSUPP));
1185 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1187 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1188 info->mti_spec.u.sp_ea.eadata == NULL)
1189 GOTO(out, result = err_serious(-EINVAL));
1191 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1192 "cr_flag=0%o mode=0%06o msg_flag=0x%x\n",
1193 PFID(rr->rr_fid1), rr->rr_name,
1194 PFID(rr->rr_fid2), create_flags,
1195 ma->ma_attr.la_mode, msg_flags);
1197 if (msg_flags & MSG_REPLAY ||
1198 (req->rq_export->exp_libclient && create_flags&MDS_OPEN_HAS_EA)) {
1199 /* This is a replay request or from liblustre with ea. */
1200 result = mdt_open_by_fid(info, ldlm_rep);
1202 if (result != -ENOENT) {
1203 if (req->rq_export->exp_libclient &&
1204 create_flags&MDS_OPEN_HAS_EA)
1205 GOTO(out, result = 0);
1209 * We didn't find the correct object, so we need to re-create it
1210 * via a regular replay.
1212 if (!(create_flags & MDS_OPEN_CREAT)) {
1213 DEBUG_REQ(D_ERROR, req,"OPEN & CREAT not in open replay.");
1214 GOTO(out, result = -EFAULT);
1216 CDEBUG(D_INFO, "Open replay did find object, continue as "
1218 } else if (rr->rr_namelen == 0 && !info->mti_cross_ref) {
1219 result = mdt_open_anon_by_fid(info, ldlm_rep, lhc);
1220 if (result != -ENOENT)
1224 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1225 GOTO(out, result = err_serious(-ENOMEM));
1227 mdt_set_disposition(info, ldlm_rep,
1228 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1230 if (info->mti_cross_ref) {
1231 /* This is cross-ref open */
1232 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1233 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1238 lh = &info->mti_lh[MDT_LH_PARENT];
1239 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1240 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1242 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1243 MDS_INODELOCK_UPDATE);
1245 GOTO(out, result = PTR_ERR(parent));
1247 fid_zero(child_fid);
1249 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1251 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1252 lname, child_fid, &info->mti_spec);
1253 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1254 "looking for "DFID"/%s, result fid="DFID"\n",
1255 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1257 if (result != 0 && result != -ENOENT && result != -ESTALE)
1258 GOTO(out_parent, result);
1260 if (result == -ENOENT || result == -ESTALE) {
1261 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1262 if (result == -ESTALE) {
1264 * -ESTALE means the parent is a dead(unlinked) dir, so
1265 * it should return -ENOENT to in accordance with the
1266 * original mds implementaion.
1268 GOTO(out_parent, result = -ENOENT);
1270 if (!(create_flags & MDS_OPEN_CREAT))
1271 GOTO(out_parent, result);
1272 *child_fid = *info->mti_rr.rr_fid2;
1273 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1277 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1278 * return FID back in that case.
1280 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1283 child = mdt_object_find(info->mti_env, mdt, child_fid);
1285 GOTO(out_parent, result = PTR_ERR(child));
1287 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1288 if (result == -ENOENT) {
1289 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1290 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1292 info->mti_mos[0] = parent;
1293 info->mti_mos[1] = child;
1294 result = mdt_version_get_check(info, 0);
1296 GOTO(out_child, result);
1298 /* Let lower layers know what is lock mode on directory. */
1299 info->mti_spec.sp_cr_mode =
1300 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1303 * Do not perform lookup sanity check. We know that name does
1306 info->mti_spec.sp_cr_lookup = 0;
1307 info->mti_spec.sp_feat = &dt_directory_features;
1309 result = mdo_create(info->mti_env,
1310 mdt_object_child(parent),
1312 mdt_object_child(child),
1315 if (result == -ERESTART) {
1316 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1317 GOTO(out_child, result);
1320 GOTO(out_child, result);
1324 /* We have to get attr & lov ea for this object */
1325 result = mo_attr_get(info->mti_env, mdt_object_child(child),
1328 * The object is on remote node, return its FID for remote open.
1330 if (result == -EREMOTE) {
1332 * Check if this lock already was sent to client and
1333 * this is resent case. For resent case do not take lock
1334 * again, use what is already granted.
1336 LASSERT(lhc != NULL);
1338 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1339 struct ldlm_lock *lock;
1341 LASSERT(msg_flags & MSG_RESENT);
1343 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1345 CERROR("Invalid lock handle "LPX64"\n",
1346 lhc->mlh_reg_lh.cookie);
1349 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1350 &lock->l_resource->lr_name));
1351 LDLM_LOCK_PUT(lock);
1354 mdt_lock_handle_init(lhc);
1355 mdt_lock_reg_init(lhc, LCK_PR);
1357 rc = mdt_object_lock(info, child, lhc,
1358 MDS_INODELOCK_LOOKUP,
1361 repbody->fid1 = *mdt_object_fid(child);
1362 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1365 GOTO(out_child, result);
1369 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1371 /* get openlock if this is not replay and if a client requested it */
1372 if (!(msg_flags & MSG_REPLAY) && create_flags & MDS_OPEN_LOCK) {
1376 if (create_flags & FMODE_WRITE)
1378 else if (create_flags & MDS_FMODE_EXEC)
1382 mdt_lock_handle_init(lhc);
1383 mdt_lock_reg_init(lhc, lm);
1384 rc = mdt_object_lock(info, child, lhc,
1385 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1389 GOTO(out_child, result);
1392 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1396 /* Try to open it now. */
1397 rc = mdt_finish_open(info, parent, child, create_flags,
1401 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1402 /* openlock was acquired and mdt_finish_open failed -
1403 drop the openlock */
1404 mdt_object_unlock(info, child, lhc, 1);
1408 ma->ma_cookie_size = 0;
1409 info->mti_no_need_trans = 1;
1410 rc = mdo_unlink(info->mti_env,
1411 mdt_object_child(parent),
1412 mdt_object_child(child),
1416 CERROR("Error in cleanup of open\n");
1421 mdt_object_put(info->mti_env, child);
1423 mdt_object_unlock_put(info, parent, lh, result);
1425 if (result && result != -EREMOTE)
1426 lustre_msg_set_transno(req->rq_repmsg, 0);
1430 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
1431 FMODE_TRUNC)) == FMODE_CLOSED)
1433 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1435 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1438 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1440 struct mdt_object *o = mfd->mfd_object;
1441 struct md_object *next = mdt_object_child(o);
1442 struct md_attr *ma = &info->mti_attr;
1443 int ret = MDT_IOEPOCH_CLOSED;
1448 mode = mfd->mfd_mode;
1450 if ((mode & FMODE_WRITE) || (mode & FMODE_TRUNC)) {
1452 ret = mdt_ioepoch_close(info, o);
1453 } else if (mode & MDS_FMODE_EXEC) {
1455 } else if (mode & FMODE_EPOCH) {
1456 ret = mdt_ioepoch_close(info, o);
1457 } else if (mode & FMODE_SOM) {
1458 ret = mdt_som_au_close(info, o);
1461 /* Update atime on close only. */
1462 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1463 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1464 /* Set the atime only. */
1465 ma->ma_attr.la_valid = LA_ATIME;
1466 rc = mo_attr_set(info->mti_env, next, ma);
1469 ma->ma_need |= MA_INODE;
1472 if (!MFD_CLOSED(mode))
1473 rc = mo_close(info->mti_env, next, ma);
1475 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1476 struct mdt_export_data *med;
1478 /* The IOepoch is still opened or SOM update is needed.
1479 * Put mfd back into the list. */
1480 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1481 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1482 FMODE_EPOCH : FMODE_SOM);
1484 LASSERT(mdt_info_req(info));
1485 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1486 spin_lock(&med->med_open_lock);
1487 list_add(&mfd->mfd_list, &med->med_open_head);
1488 class_handle_hash_back(&mfd->mfd_handle);
1489 spin_unlock(&med->med_open_lock);
1491 if (ret == MDT_IOEPOCH_OPENED) {
1495 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1496 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1500 mdt_object_put(info->mti_env, o);
1503 RETURN(rc ? rc : ret);
1506 int mdt_close(struct mdt_thread_info *info)
1508 struct mdt_export_data *med;
1509 struct mdt_file_data *mfd;
1510 struct mdt_object *o;
1511 struct md_attr *ma = &info->mti_attr;
1512 struct mdt_body *repbody = NULL;
1513 struct ptlrpc_request *req = mdt_info_req(info);
1517 /* Close may come with the Size-on-MDS update. Unpack it. */
1518 rc = mdt_close_unpack(info);
1520 RETURN(err_serious(rc));
1522 LASSERT(info->mti_ioepoch);
1524 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1525 info->mti_mdt->mdt_max_mdsize);
1526 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1527 info->mti_mdt->mdt_max_cookiesize);
1528 rc = req_capsule_server_pack(info->mti_pill);
1529 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1531 mdt_shrink_reply(info);
1532 RETURN(lustre_msg_get_status(req->rq_repmsg));
1535 /* Continue to close handle even if we can not pack reply */
1537 repbody = req_capsule_server_get(info->mti_pill,
1539 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1541 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1544 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1546 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1549 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1550 repbody->eadatasize = 0;
1551 repbody->aclsize = 0;
1553 rc = err_serious(rc);
1556 med = &req->rq_export->exp_mdt_data;
1557 spin_lock(&med->med_open_lock);
1558 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1559 if (mdt_mfd_closed(mfd)) {
1560 spin_unlock(&med->med_open_lock);
1561 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1562 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1563 info->mti_ioepoch->handle.cookie);
1564 /** not serious error since bug 3633 */
1567 class_handle_unhash(&mfd->mfd_handle);
1568 list_del_init(&mfd->mfd_list);
1569 spin_unlock(&med->med_open_lock);
1571 /* Do not lose object before last unlink. */
1572 o = mfd->mfd_object;
1573 mdt_object_get(info->mti_env, o);
1574 ret = mdt_mfd_close(info, mfd);
1575 if (repbody != NULL)
1576 rc = mdt_handle_last_unlink(info, o, ma);
1577 mdt_empty_transno(info);
1578 mdt_object_put(info->mti_env, o);
1580 if (repbody != NULL)
1581 mdt_shrink_reply(info);
1583 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1584 RETURN(err_serious(-ENOMEM));
1586 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1587 OBD_FAIL_MDS_CLOSE_NET_REP))
1588 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1589 RETURN(rc ? rc : ret);
1593 * DONE_WRITING rpc handler.
1595 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1596 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1597 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1598 * skip attributes and reconstruct the reply here.
1600 int mdt_done_writing(struct mdt_thread_info *info)
1602 struct ptlrpc_request *req = mdt_info_req(info);
1603 struct mdt_body *repbody = NULL;
1604 struct mdt_export_data *med;
1605 struct mdt_file_data *mfd;
1609 rc = req_capsule_server_pack(info->mti_pill);
1611 RETURN(err_serious(rc));
1613 repbody = req_capsule_server_get(info->mti_pill,
1615 repbody->eadatasize = 0;
1616 repbody->aclsize = 0;
1618 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1619 rc = mdt_close_unpack(info);
1621 RETURN(err_serious(rc));
1623 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1624 RETURN(lustre_msg_get_status(req->rq_repmsg));
1626 med = &info->mti_exp->exp_mdt_data;
1627 spin_lock(&med->med_open_lock);
1628 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1630 spin_unlock(&med->med_open_lock);
1631 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1632 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1633 PFID(info->mti_rr.rr_fid1),
1634 info->mti_ioepoch->handle.cookie,
1635 info->mti_ioepoch->ioepoch);
1636 /* If this is a replay, reconstruct the transno. */
1637 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1638 mdt_empty_transno(info);
1639 RETURN(info->mti_ioepoch->flags & MF_SOM_AU ?
1645 LASSERT(mfd->mfd_mode == FMODE_EPOCH ||
1646 mfd->mfd_mode == FMODE_TRUNC);
1647 class_handle_unhash(&mfd->mfd_handle);
1648 list_del_init(&mfd->mfd_list);
1649 spin_unlock(&med->med_open_lock);
1651 /* Set EPOCH CLOSE flag if not set by client. */
1652 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1653 info->mti_attr.ma_valid = 0;
1655 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1656 OBD_ALLOC(info->mti_attr.ma_lmm, info->mti_attr.ma_lmm_size);
1657 if (info->mti_attr.ma_lmm == NULL)
1660 rc = mdt_mfd_close(info, mfd);
1662 OBD_FREE(info->mti_attr.ma_lmm, info->mti_attr.ma_lmm_size);
1663 mdt_empty_transno(info);