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.
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 */
88 if (mfd == NULL && req_is_replay(req)) {
89 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
90 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
91 if (mfd->mfd_old_handle.cookie == handle->cookie)
100 void mdt_mfd_free(struct mdt_file_data *mfd)
102 LASSERT(cfs_list_empty(&mfd->mfd_list));
103 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
106 static int mdt_create_data(struct mdt_thread_info *info,
107 struct mdt_object *p, struct mdt_object *o)
109 struct md_op_spec *spec = &info->mti_spec;
110 struct md_attr *ma = &info->mti_attr;
114 if (!md_should_create(spec->sp_cr_flags))
117 ma->ma_need = MA_INODE | MA_LOV;
119 rc = mdo_create_data(info->mti_env,
120 p ? mdt_object_child(p) : NULL,
121 mdt_object_child(o), spec, ma);
125 static int mdt_ioepoch_opened(struct mdt_object *mo)
127 return mo->mot_ioepoch_count;
130 int mdt_object_is_som_enabled(struct mdt_object *mo)
132 return !mo->mot_ioepoch;
136 * Re-enable Size-on-MDS.
137 * Call under ->mot_ioepoch_sem.
139 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
141 if (ioepoch == mo->mot_ioepoch) {
142 LASSERT(!mdt_ioepoch_opened(mo));
149 * Open the IOEpoch. It is allowed if @writecount is not negative.
150 * The epoch and writecount handling is performed under the mot_ioepoch_sem.
152 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
155 struct mdt_device *mdt = info->mti_mdt;
160 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
161 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
164 cfs_down(&o->mot_ioepoch_sem);
165 if (mdt_ioepoch_opened(o)) {
166 /* Epoch continues even if there is no writers yet. */
167 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
168 o->mot_ioepoch, PFID(mdt_object_fid(o)));
170 /* XXX: ->mdt_ioepoch is not initialized at the mount */
171 cfs_spin_lock(&mdt->mdt_ioepoch_lock);
172 if (mdt->mdt_ioepoch < info->mti_replayepoch)
173 mdt->mdt_ioepoch = info->mti_replayepoch;
175 if (info->mti_replayepoch)
176 o->mot_ioepoch = info->mti_replayepoch;
177 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
178 o->mot_ioepoch = ++mdt->mdt_ioepoch;
180 o->mot_ioepoch = mdt->mdt_ioepoch;
182 cfs_spin_unlock(&mdt->mdt_ioepoch_lock);
184 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
185 o->mot_ioepoch, PFID(mdt_object_fid(o)));
187 o->mot_flags |= MOF_SOM_CREATED;
190 o->mot_ioepoch_count++;
191 cfs_up(&o->mot_ioepoch_sem);
193 /* Cancel Size-on-MDS attributes cached on clients for the open case.
194 * In the truncate case, see mdt_reint_setattr(). */
195 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
196 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
197 mdt_lock_reg_init(lh, LCK_EX);
198 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
201 mdt_object_unlock(info, o, lh, 1);
207 * Update SOM on-disk attributes.
208 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
209 * mountid and attributes. If disabling, zero IOEpoch id in lustre-ea.
210 * Call under ->mot_ioepoch_sem.
212 static int mdt_som_attr_set(struct mdt_thread_info *info,
213 struct mdt_object *obj, __u64 ioepoch, int enable)
215 struct md_attr *ma = &info->mti_attr;
219 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
220 " on "DFID".\n", enable ? "update" : "disabling",
221 ioepoch, PFID(mdt_object_fid(obj)));
223 ma->ma_valid |= MA_SOM;
224 ma->ma_som = &info->mti_u.som.data;
226 struct mdt_device *mdt = info->mti_mdt;
227 struct lu_attr *la = &ma->ma_attr;
229 ma->ma_som->msd_ioepoch = ioepoch;
230 ma->ma_som->msd_size = la->la_valid & LA_SIZE ? la->la_size : 0;
231 ma->ma_som->msd_blocks = la->la_valid & LA_BLOCKS ?
233 ma->ma_som->msd_mountid = mdt->mdt_lut.lut_obd->u.obt.obt_mount_count;
234 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
236 ma->ma_som->msd_ioepoch = IOEPOCH_INVAL;
237 ma->ma_attr.la_valid &= LA_ATIME;
240 /* Since we have opened the file, it is unnecessary
241 * to check permission when close it. Between the "open"
242 * and "close", maybe someone has changed the file mode
243 * or flags, or the file created mode do not permit wirte,
244 * and so on. Just set MDS_PERM_BYPASS for all the cases. */
245 ma->ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
247 rc = mdt_attr_set(info, obj, ma, 0);
251 /** Perform the eviction specific actions on ioepoch close. */
252 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
253 struct mdt_object *o)
257 cfs_down(&o->mot_ioepoch_sem);
258 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
259 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
260 o->mot_ioepoch_count);
261 o->mot_ioepoch_count--;
263 /* If eviction occured set MOF_SOM_RECOV,
264 * if no other epoch holders, disable SOM on disk. */
265 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
266 if (!mdt_ioepoch_opened(o)) {
267 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
268 mdt_object_som_enable(o, o->mot_ioepoch);
270 cfs_up(&o->mot_ioepoch_sem);
275 * Perform the replay specific actions on ioepoch close.
276 * Skip SOM attribute update if obtained and just forget about the inode state
277 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
279 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
280 struct mdt_object *o)
282 int rc = MDT_IOEPOCH_CLOSED;
285 cfs_down(&o->mot_ioepoch_sem);
286 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
287 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
288 o->mot_ioepoch_count--;
290 /* Get an info from the replayed request if client is supposed
291 * to send an Attibute Update, reconstruct @rc if so */
292 if (info->mti_ioepoch->flags & MF_SOM_AU)
293 rc = MDT_IOEPOCH_GETATTR;
295 if (!mdt_ioepoch_opened(o))
296 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
297 cfs_up(&o->mot_ioepoch_sem);
303 * Regular file IOepoch close.
304 * Closes the ioepoch, checks the object state, apply obtained attributes and
305 * re-enable SOM on the object, if possible. Also checks if the recovery is
306 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
307 * to obtain SOM attributes under the server-side OST locks.
310 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
311 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
313 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
314 struct mdt_object *o)
316 struct md_attr *tmp_ma;
320 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
323 la = &info->mti_attr.ma_attr;
324 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
326 cfs_down(&o->mot_ioepoch_sem);
327 o->mot_ioepoch_count--;
329 tmp_ma = &info->mti_u.som.attr;
330 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
331 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
332 tmp_ma->ma_som = &info->mti_u.som.data;
333 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
334 tmp_ma->ma_valid = 0;
335 rc = mo_attr_get(info->mti_env, mdt_object_child(o), tmp_ma);
339 /* Check the on-disk SOM state. */
340 if (o->mot_flags & MOF_SOM_RECOV)
342 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
343 !(tmp_ma->ma_valid & MA_SOM))
346 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
347 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
349 opened = mdt_ioepoch_opened(o);
351 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
352 * Skip the check for file with no LOV or for unlink files.
354 if (!opened && tmp_ma->ma_valid & MA_LOV &&
355 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
357 /* If some previous writer was evicted, re-ask the
358 * client for attributes. Even if attributes are
359 * provided, we cannot believe in them.
360 * Another use case is that there is no SOM cache on
361 * disk -- first access with SOM or there was an MDS
363 ret = MDT_IOEPOCH_GETATTR;
364 else if (o->mot_flags & MOF_SOM_CHANGE)
365 /* Some previous writer changed the attribute.
366 * Do not believe to the current Size-on-MDS
367 * update, re-ask client. */
368 ret = MDT_IOEPOCH_GETATTR;
369 else if (!(la->la_valid & LA_SIZE) && achange)
370 /* Attributes were changed by the last writer
371 * only but no Size-on-MDS update is received.*/
372 ret = MDT_IOEPOCH_GETATTR;
375 if (achange || ret == MDT_IOEPOCH_GETATTR)
376 o->mot_flags |= MOF_SOM_CHANGE;
378 /* If epoch ends and relable SOM attributes are obtained, update them.
379 * Create SOM ea for new files even if there is no attributes obtained
380 * (0-length file). */
381 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
382 if (achange || o->mot_flags & MOF_SOM_CREATED) {
383 LASSERT(achange || !(la->la_valid & LA_SIZE));
384 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
386 /* Avoid the following setattrs of these attributes,
387 * e.g. for atime update. */
388 info->mti_attr.ma_valid = 0;
390 mdt_object_som_enable(o, o->mot_ioepoch);
393 cfs_up(&o->mot_ioepoch_sem);
394 /* If recovery is needed, tell the client to perform GETATTR under
396 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
397 struct mdt_body *rep;
398 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
399 rep->valid |= OBD_MD_FLGETATTRLOCK;
405 cfs_up(&o->mot_ioepoch_sem);
410 * Close IOEpoch (opened file or FMODE_EPOCH state). It happens if:
411 * - a client closes the IOEpoch;
412 * - a client eviction occured.
414 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
415 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
416 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
419 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
421 struct ptlrpc_request *req = mdt_info_req(info);
424 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
425 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
428 LASSERT(o->mot_ioepoch_count);
429 LASSERT(info->mti_ioepoch == NULL ||
430 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
432 /* IOEpoch is closed only if client tells about it or eviction occures.
433 * In the replay case, always close the epoch. */
435 RETURN(mdt_ioepoch_close_on_eviction(info, o));
436 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
437 RETURN(mdt_ioepoch_close_on_replay(info, o));
438 if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
439 RETURN(mdt_ioepoch_close_reg(info, o));
440 /* IO epoch is not closed. */
441 RETURN(MDT_IOEPOCH_OPENED);
445 * Close FMODE_SOM state, when IOEpoch is already closed and we are waiting for
446 * attribute update. It happens if:
447 * - SOM Attribute Update is obtained;
448 * - the client failed to obtain it and informs MDS about it;
449 * - a client eviction occured.
450 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
453 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
455 struct ptlrpc_request *req = mdt_info_req(info);
457 int act = MDT_SOM_ENABLE;
461 LASSERT(!req || info->mti_ioepoch);
462 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
463 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
466 /* No size whereas MF_SOM_CHANGE is set means client failed to
467 * obtain ost attributes, drop the SOM cache on disk if so. */
469 (info->mti_ioepoch &&
470 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
471 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
472 act = MDT_SOM_DISABLE;
474 cfs_down(&o->mot_ioepoch_sem);
475 /* Mark the object it is the recovery state if we failed to obtain
477 if (act == MDT_SOM_DISABLE)
478 o->mot_flags |= MOF_SOM_RECOV;
480 if (!mdt_ioepoch_opened(o)) {
481 ioepoch = info->mti_ioepoch ?
482 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
484 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
485 rc = mdt_som_attr_set(info, o, ioepoch, act);
486 mdt_object_som_enable(o, ioepoch);
488 cfs_up(&o->mot_ioepoch_sem);
492 int mdt_write_read(struct mdt_object *o)
496 cfs_down(&o->mot_ioepoch_sem);
497 rc = o->mot_writecount;
498 cfs_up(&o->mot_ioepoch_sem);
502 int mdt_write_get(struct mdt_object *o)
506 cfs_down(&o->mot_ioepoch_sem);
507 if (o->mot_writecount < 0)
511 cfs_up(&o->mot_ioepoch_sem);
515 void mdt_write_put(struct mdt_object *o)
518 cfs_down(&o->mot_ioepoch_sem);
520 cfs_up(&o->mot_ioepoch_sem);
524 static int mdt_write_deny(struct mdt_object *o)
528 cfs_down(&o->mot_ioepoch_sem);
529 if (o->mot_writecount > 0)
533 cfs_up(&o->mot_ioepoch_sem);
537 static void mdt_write_allow(struct mdt_object *o)
540 cfs_down(&o->mot_ioepoch_sem);
542 cfs_up(&o->mot_ioepoch_sem);
546 /* there can be no real transaction so prepare the fake one */
547 static void mdt_empty_transno(struct mdt_thread_info* info)
549 struct mdt_device *mdt = info->mti_mdt;
550 struct ptlrpc_request *req = mdt_info_req(info);
553 /* transaction has occurred already */
554 if (lustre_msg_get_transno(req->rq_repmsg) != 0) {
559 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
560 if (info->mti_transno == 0) {
561 info->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
563 /* should be replay */
564 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
565 mdt->mdt_lut.lut_last_transno = info->mti_transno;
567 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
569 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
571 req->rq_export->exp_obd->obd_last_committed);
573 req->rq_transno = info->mti_transno;
574 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
578 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
580 LASSERT(mfd != NULL);
582 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
583 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
585 mfd->mfd_mode = mode;
588 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
589 struct mdt_object *o, __u64 flags, int created)
591 struct ptlrpc_request *req = mdt_info_req(info);
592 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
593 struct mdt_file_data *mfd;
594 struct md_attr *ma = &info->mti_attr;
595 struct lu_attr *la = &ma->ma_attr;
596 struct mdt_body *repbody;
597 int rc = 0, isdir, isreg;
600 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
602 isreg = S_ISREG(la->la_mode);
603 isdir = S_ISDIR(la->la_mode);
604 if (isreg && !(ma->ma_valid & MA_LOV)) {
606 * No EA, check whether it is will set regEA and dirEA since in
607 * above attr get, these size might be zero, so reset it, to
608 * retrieve the MD after create obj.
610 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
613 /* in replay case, p == NULL */
614 rc = mdt_create_data(info, p, o);
619 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
620 ma->ma_valid, ma->ma_lmm_size);
622 if (ma->ma_valid & MA_LOV) {
623 LASSERT(ma->ma_lmm_size != 0);
624 repbody->eadatasize = ma->ma_lmm_size;
626 repbody->valid |= OBD_MD_FLDIREA;
628 repbody->valid |= OBD_MD_FLEASIZE;
631 if (flags & FMODE_WRITE) {
632 rc = mdt_write_get(o);
634 mdt_ioepoch_open(info, o, created);
635 repbody->ioepoch = o->mot_ioepoch;
637 } else if (flags & MDS_FMODE_EXEC) {
638 rc = mdt_write_deny(o);
643 rc = mo_open(info->mti_env, mdt_object_child(o),
644 created ? flags | MDS_OPEN_CREATED : flags);
651 * Keep a reference on this object for this open, and is
652 * released by mdt_mfd_close().
654 mdt_object_get(info->mti_env, o);
657 * @flags is always not zero. At least it should be FMODE_READ,
658 * FMODE_WRITE or FMODE_EXEC.
663 mdt_mfd_set_mode(mfd, flags);
666 mfd->mfd_xid = req->rq_xid;
669 if (req_is_replay(req)) {
670 struct mdt_file_data *old_mfd;
671 /* Check wheather old cookie already exist in
672 * the list, becasue when do recovery, client
673 * might be disconnected from server, and
674 * restart replay, so there maybe some orphan
675 * mfd here, we should remove them */
676 LASSERT(info->mti_rr.rr_handle != NULL);
677 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
679 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
680 "cookie=" LPX64"\n", mfd,
681 PFID(mdt_object_fid(mfd->mfd_object)),
682 info->mti_rr.rr_handle->cookie);
683 cfs_spin_lock(&med->med_open_lock);
684 class_handle_unhash(&old_mfd->mfd_handle);
685 cfs_list_del_init(&old_mfd->mfd_list);
686 cfs_spin_unlock(&med->med_open_lock);
687 /* no attr update for that close */
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 cfs_spin_lock(&med->med_open_lock);
700 class_handle_unhash(&mfd->mfd_handle);
701 cfs_list_del_init(&mfd->mfd_list);
702 cfs_spin_unlock(&med->med_open_lock);
703 mdt_mfd_close(info, mfd);
705 cfs_spin_lock(&med->med_open_lock);
706 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
707 cfs_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 __u64 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 cfs_spin_lock(&med->med_open_lock);
848 cfs_list_for_each(t, &med->med_open_head) {
849 mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
850 if (mfd->mfd_xid == req->rq_xid) {
855 cfs_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 tg_export_data *ted = &req->rq_export->exp_target_data;
887 struct lsd_client_data *lcd = ted->ted_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, 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) || rc)
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 __u64 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 mdt_counter_incr(req->rq_export, LPROC_MDT_OPEN);
1168 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1170 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1171 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1173 ma->ma_need = MA_INODE;
1174 if (ma->ma_lmm_size > 0)
1175 ma->ma_need |= MA_LOV;
1179 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1180 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1182 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1183 CERROR("file join is not supported anymore.\n");
1184 GOTO(out, result = err_serious(-EOPNOTSUPP));
1186 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1188 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1189 info->mti_spec.u.sp_ea.eadata == NULL)
1190 GOTO(out, result = err_serious(-EINVAL));
1192 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1193 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1194 PFID(rr->rr_fid1), rr->rr_name,
1195 PFID(rr->rr_fid2), create_flags,
1196 ma->ma_attr.la_mode, msg_flags);
1198 if (req_is_replay(req) ||
1199 (req->rq_export->exp_libclient && create_flags&MDS_OPEN_HAS_EA)) {
1200 /* This is a replay request or from liblustre with ea. */
1201 result = mdt_open_by_fid(info, ldlm_rep);
1203 if (result != -ENOENT) {
1204 if (req->rq_export->exp_libclient &&
1205 create_flags&MDS_OPEN_HAS_EA)
1206 GOTO(out, result = 0);
1210 * We didn't find the correct object, so we need to re-create it
1211 * via a regular replay.
1213 if (!(create_flags & MDS_OPEN_CREAT)) {
1214 DEBUG_REQ(D_ERROR, req,
1215 "OPEN & CREAT not in open replay.");
1216 GOTO(out, result = -EFAULT);
1218 CDEBUG(D_INFO, "Open replay did find object, continue as "
1220 } else if (rr->rr_namelen == 0 && !info->mti_cross_ref &&
1221 create_flags & MDS_OPEN_LOCK) {
1222 result = mdt_open_anon_by_fid(info, ldlm_rep, lhc);
1226 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1227 GOTO(out, result = err_serious(-ENOMEM));
1229 mdt_set_disposition(info, ldlm_rep,
1230 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1232 if (info->mti_cross_ref) {
1233 /* This is cross-ref open */
1234 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1235 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1240 lh = &info->mti_lh[MDT_LH_PARENT];
1241 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1242 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1244 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1245 MDS_INODELOCK_UPDATE);
1247 GOTO(out, result = PTR_ERR(parent));
1249 /* get and check version of parent */
1250 result = mdt_version_get_check(info, parent, 0);
1252 GOTO(out_parent, result);
1254 fid_zero(child_fid);
1256 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1257 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1258 lname, child_fid, &info->mti_spec);
1259 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1260 "looking for "DFID"/%s, result fid="DFID"\n",
1261 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1263 if (result != 0 && result != -ENOENT && result != -ESTALE)
1264 GOTO(out_parent, result);
1266 if (result == -ENOENT || result == -ESTALE) {
1267 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1268 if (result == -ESTALE) {
1270 * -ESTALE means the parent is a dead(unlinked) dir, so
1271 * it should return -ENOENT to in accordance with the
1272 * original mds implementaion.
1274 GOTO(out_parent, result = -ENOENT);
1276 if (!(create_flags & MDS_OPEN_CREAT))
1277 GOTO(out_parent, result);
1278 *child_fid = *info->mti_rr.rr_fid2;
1279 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1283 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1284 * return FID back in that case.
1286 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1289 child = mdt_object_find(info->mti_env, mdt, child_fid);
1291 GOTO(out_parent, result = PTR_ERR(child));
1293 /** check version of child */
1294 rc = mdt_version_get_check(info, child, 1);
1296 GOTO(out_child, result = rc);
1298 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1299 if (result == -ENOENT) {
1300 /* save versions in reply */
1301 mdt_version_get_save(info, parent, 0);
1302 mdt_version_get_save(info, child, 1);
1304 /* version of child will be changed */
1305 info->mti_mos = child;
1307 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1308 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1310 /* Let lower layers know what is lock mode on directory. */
1311 info->mti_spec.sp_cr_mode =
1312 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1315 * Do not perform lookup sanity check. We know that name does
1318 info->mti_spec.sp_cr_lookup = 0;
1319 info->mti_spec.sp_feat = &dt_directory_features;
1321 result = mdo_create(info->mti_env,
1322 mdt_object_child(parent),
1324 mdt_object_child(child),
1327 if (result == -ERESTART) {
1328 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1329 GOTO(out_child, result);
1332 GOTO(out_child, result);
1336 /* We have to get attr & lov ea for this object */
1337 result = mo_attr_get(info->mti_env, mdt_object_child(child),
1340 * The object is on remote node, return its FID for remote open.
1342 if (result == -EREMOTE) {
1344 * Check if this lock already was sent to client and
1345 * this is resent case. For resent case do not take lock
1346 * again, use what is already granted.
1348 LASSERT(lhc != NULL);
1350 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1351 struct ldlm_lock *lock;
1353 LASSERT(msg_flags & MSG_RESENT);
1355 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1357 CERROR("Invalid lock handle "LPX64"\n",
1358 lhc->mlh_reg_lh.cookie);
1361 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1362 &lock->l_resource->lr_name));
1363 LDLM_LOCK_PUT(lock);
1366 mdt_lock_handle_init(lhc);
1367 mdt_lock_reg_init(lhc, LCK_PR);
1369 rc = mdt_object_lock(info, child, lhc,
1370 MDS_INODELOCK_LOOKUP,
1373 repbody->fid1 = *mdt_object_fid(child);
1374 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1377 GOTO(out_child, result);
1381 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1383 /* get openlock if this is not replay and if a client requested it */
1384 if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
1387 if (create_flags & FMODE_WRITE)
1389 else if (create_flags & MDS_FMODE_EXEC)
1393 mdt_lock_handle_init(lhc);
1394 mdt_lock_reg_init(lhc, lm);
1395 rc = mdt_object_lock(info, child, lhc,
1396 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1400 GOTO(out_child, result);
1403 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1407 /* Try to open it now. */
1408 rc = mdt_finish_open(info, parent, child, create_flags,
1412 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1413 /* openlock was acquired and mdt_finish_open failed -
1414 drop the openlock */
1415 mdt_object_unlock(info, child, lhc, 1);
1419 ma->ma_cookie_size = 0;
1420 info->mti_no_need_trans = 1;
1421 rc = mdo_unlink(info->mti_env,
1422 mdt_object_child(parent),
1423 mdt_object_child(child),
1427 CERROR("Error in cleanup of open\n");
1432 mdt_object_put(info->mti_env, child);
1434 mdt_object_unlock_put(info, parent, lh, result || !created);
1436 if (result && result != -EREMOTE)
1437 lustre_msg_set_transno(req->rq_repmsg, 0);
1441 #define MFD_CLOSED(mode) (((mode) & ~(FMODE_EPOCH | FMODE_SOM | \
1442 FMODE_TRUNC)) == FMODE_CLOSED)
1444 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1446 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1449 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1451 struct mdt_object *o = mfd->mfd_object;
1452 struct md_object *next = mdt_object_child(o);
1453 struct md_attr *ma = &info->mti_attr;
1454 int ret = MDT_IOEPOCH_CLOSED;
1459 mode = mfd->mfd_mode;
1461 if ((mode & FMODE_WRITE) || (mode & FMODE_TRUNC)) {
1463 ret = mdt_ioepoch_close(info, o);
1464 } else if (mode & MDS_FMODE_EXEC) {
1466 } else if (mode & FMODE_EPOCH) {
1467 ret = mdt_ioepoch_close(info, o);
1468 } else if (mode & FMODE_SOM) {
1469 ret = mdt_som_au_close(info, o);
1472 /* Update atime on close only. */
1473 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1474 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1475 /* Set the atime only. */
1476 ma->ma_valid = MA_INODE;
1477 ma->ma_attr.la_valid = LA_ATIME;
1478 rc = mo_attr_set(info->mti_env, next, ma);
1481 ma->ma_need |= MA_INODE;
1482 ma->ma_valid &= ~MA_INODE;
1484 if (!MFD_CLOSED(mode))
1485 rc = mo_close(info->mti_env, next, ma);
1487 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1488 struct mdt_export_data *med;
1490 /* The IOepoch is still opened or SOM update is needed.
1491 * Put mfd back into the list. */
1492 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1493 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1494 FMODE_EPOCH : FMODE_SOM);
1496 LASSERT(mdt_info_req(info));
1497 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1498 cfs_spin_lock(&med->med_open_lock);
1499 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1500 class_handle_hash_back(&mfd->mfd_handle);
1501 cfs_spin_unlock(&med->med_open_lock);
1503 if (ret == MDT_IOEPOCH_OPENED) {
1507 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1508 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1512 mdt_object_put(info->mti_env, o);
1515 RETURN(rc ? rc : ret);
1518 int mdt_close(struct mdt_thread_info *info)
1520 struct mdt_export_data *med;
1521 struct mdt_file_data *mfd;
1522 struct mdt_object *o;
1523 struct md_attr *ma = &info->mti_attr;
1524 struct mdt_body *repbody = NULL;
1525 struct ptlrpc_request *req = mdt_info_req(info);
1529 mdt_counter_incr(req->rq_export, LPROC_MDT_CLOSE);
1530 /* Close may come with the Size-on-MDS update. Unpack it. */
1531 rc = mdt_close_unpack(info);
1533 RETURN(err_serious(rc));
1535 LASSERT(info->mti_ioepoch);
1537 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1538 info->mti_mdt->mdt_max_mdsize);
1539 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1540 info->mti_mdt->mdt_max_cookiesize);
1541 rc = req_capsule_server_pack(info->mti_pill);
1542 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1544 mdt_shrink_reply(info);
1545 RETURN(lustre_msg_get_status(req->rq_repmsg));
1548 /* Continue to close handle even if we can not pack reply */
1550 repbody = req_capsule_server_get(info->mti_pill,
1552 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1554 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1557 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1559 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1562 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1563 repbody->eadatasize = 0;
1564 repbody->aclsize = 0;
1566 rc = err_serious(rc);
1569 med = &req->rq_export->exp_mdt_data;
1570 cfs_spin_lock(&med->med_open_lock);
1571 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1572 if (mdt_mfd_closed(mfd)) {
1573 cfs_spin_unlock(&med->med_open_lock);
1574 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1575 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1576 info->mti_ioepoch->handle.cookie);
1577 /** not serious error since bug 3633 */
1580 class_handle_unhash(&mfd->mfd_handle);
1581 cfs_list_del_init(&mfd->mfd_list);
1582 cfs_spin_unlock(&med->med_open_lock);
1584 /* Do not lose object before last unlink. */
1585 o = mfd->mfd_object;
1586 mdt_object_get(info->mti_env, o);
1587 ret = mdt_mfd_close(info, mfd);
1588 if (repbody != NULL)
1589 rc = mdt_handle_last_unlink(info, o, ma);
1590 mdt_empty_transno(info);
1591 mdt_object_put(info->mti_env, o);
1593 if (repbody != NULL)
1594 mdt_shrink_reply(info);
1596 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1597 RETURN(err_serious(-ENOMEM));
1599 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1600 OBD_FAIL_MDS_CLOSE_NET_REP))
1601 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1602 RETURN(rc ? rc : ret);
1606 * DONE_WRITING rpc handler.
1608 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1609 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1610 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1611 * skip attributes and reconstruct the reply here.
1613 int mdt_done_writing(struct mdt_thread_info *info)
1615 struct ptlrpc_request *req = mdt_info_req(info);
1616 struct mdt_body *repbody = NULL;
1617 struct mdt_export_data *med;
1618 struct mdt_file_data *mfd;
1622 rc = req_capsule_server_pack(info->mti_pill);
1624 RETURN(err_serious(rc));
1626 repbody = req_capsule_server_get(info->mti_pill,
1628 repbody->eadatasize = 0;
1629 repbody->aclsize = 0;
1631 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1632 rc = mdt_close_unpack(info);
1634 RETURN(err_serious(rc));
1636 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1637 RETURN(lustre_msg_get_status(req->rq_repmsg));
1639 med = &info->mti_exp->exp_mdt_data;
1640 cfs_spin_lock(&med->med_open_lock);
1641 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1643 cfs_spin_unlock(&med->med_open_lock);
1644 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1645 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1646 PFID(info->mti_rr.rr_fid1),
1647 info->mti_ioepoch->handle.cookie,
1648 info->mti_ioepoch->ioepoch);
1649 /* If this is a replay, reconstruct the transno. */
1650 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1651 mdt_empty_transno(info);
1652 RETURN(info->mti_ioepoch->flags & MF_SOM_AU ?
1658 LASSERT(mfd->mfd_mode == FMODE_EPOCH ||
1659 mfd->mfd_mode == FMODE_TRUNC);
1660 class_handle_unhash(&mfd->mfd_handle);
1661 cfs_list_del_init(&mfd->mfd_list);
1662 cfs_spin_unlock(&med->med_open_lock);
1664 /* Set EPOCH CLOSE flag if not set by client. */
1665 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1666 info->mti_attr.ma_valid = 0;
1668 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1669 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1670 if (info->mti_attr.ma_lmm == NULL)
1673 rc = mdt_mfd_close(info, mfd);
1675 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1676 mdt_empty_transno(info);