4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_open.c
38 * Lustre Metadata Target (mdt) open/close file handling
40 * Author: Huang Hua <huanghua@clusterfs.com>
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 cfs_mutex_lock(&o->mot_lov_mutex);
120 if (!(o->mot_flags & MOF_LOV_CREATED)) {
121 rc = mdo_create_data(info->mti_env,
122 p ? mdt_object_child(p) : NULL,
123 mdt_object_child(o), spec, ma);
124 if (rc == 0 && ma->ma_valid & MA_LOV)
125 o->mot_flags |= MOF_LOV_CREATED;
127 cfs_mutex_unlock(&o->mot_lov_mutex);
131 static int mdt_ioepoch_opened(struct mdt_object *mo)
133 return mo->mot_ioepoch_count;
136 int mdt_object_is_som_enabled(struct mdt_object *mo)
138 return !mo->mot_ioepoch;
142 * Re-enable Size-on-MDS.
143 * Call under ->mot_ioepoch_mutex.
145 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
147 if (ioepoch == mo->mot_ioepoch) {
148 LASSERT(!mdt_ioepoch_opened(mo));
155 * Open the IOEpoch. It is allowed if @writecount is not negative.
156 * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
158 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
161 struct mdt_device *mdt = info->mti_mdt;
166 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
167 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
170 cfs_mutex_lock(&o->mot_ioepoch_mutex);
171 if (mdt_ioepoch_opened(o)) {
172 /* Epoch continues even if there is no writers yet. */
173 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
174 o->mot_ioepoch, PFID(mdt_object_fid(o)));
176 /* XXX: ->mdt_ioepoch is not initialized at the mount */
177 cfs_spin_lock(&mdt->mdt_ioepoch_lock);
178 if (mdt->mdt_ioepoch < info->mti_replayepoch)
179 mdt->mdt_ioepoch = info->mti_replayepoch;
181 if (info->mti_replayepoch)
182 o->mot_ioepoch = info->mti_replayepoch;
183 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
184 o->mot_ioepoch = ++mdt->mdt_ioepoch;
186 o->mot_ioepoch = mdt->mdt_ioepoch;
188 cfs_spin_unlock(&mdt->mdt_ioepoch_lock);
190 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
191 o->mot_ioepoch, PFID(mdt_object_fid(o)));
193 o->mot_flags |= MOF_SOM_CREATED;
196 o->mot_ioepoch_count++;
197 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
199 /* Cancel Size-on-MDS attributes cached on clients for the open case.
200 * In the truncate case, see mdt_reint_setattr(). */
201 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
202 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
203 mdt_lock_reg_init(lh, LCK_EX);
204 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
207 mdt_object_unlock(info, o, lh, 1);
213 * Update SOM on-disk attributes.
214 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
215 * mountid and attributes. If disabling, zero IOEpoch id in lustre-ea.
216 * Call under ->mot_ioepoch_mutex.
218 static int mdt_som_attr_set(struct mdt_thread_info *info,
219 struct mdt_object *obj, __u64 ioepoch, int enable)
221 struct md_attr *ma = &info->mti_attr;
225 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
226 " on "DFID".\n", enable ? "update" : "disabling",
227 ioepoch, PFID(mdt_object_fid(obj)));
229 ma->ma_valid |= MA_SOM;
230 ma->ma_som = &info->mti_u.som.data;
232 struct mdt_device *mdt = info->mti_mdt;
233 struct lu_attr *la = &ma->ma_attr;
235 ma->ma_som->msd_ioepoch = ioepoch;
236 ma->ma_som->msd_size = la->la_valid & LA_SIZE ? la->la_size : 0;
237 ma->ma_som->msd_blocks = la->la_valid & LA_BLOCKS ?
239 ma->ma_som->msd_mountid = mdt->mdt_lut.lut_obd->u.obt.obt_mount_count;
240 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
242 ma->ma_som->msd_ioepoch = IOEPOCH_INVAL;
243 ma->ma_attr.la_valid &= LA_ATIME;
246 /* Since we have opened the file, it is unnecessary
247 * to check permission when close it. Between the "open"
248 * and "close", maybe someone has changed the file mode
249 * or flags, or the file created mode do not permit wirte,
250 * and so on. Just set MDS_PERM_BYPASS for all the cases. */
251 ma->ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
253 rc = mdt_attr_set(info, obj, ma, 0);
257 /** Perform the eviction specific actions on ioepoch close. */
258 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
259 struct mdt_object *o)
263 cfs_mutex_lock(&o->mot_ioepoch_mutex);
264 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
265 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
266 o->mot_ioepoch_count);
267 o->mot_ioepoch_count--;
269 /* If eviction occured set MOF_SOM_RECOV,
270 * if no other epoch holders, disable SOM on disk. */
271 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
272 if (!mdt_ioepoch_opened(o)) {
273 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
274 mdt_object_som_enable(o, o->mot_ioepoch);
276 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
281 * Perform the replay specific actions on ioepoch close.
282 * Skip SOM attribute update if obtained and just forget about the inode state
283 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
285 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
286 struct mdt_object *o)
288 int rc = MDT_IOEPOCH_CLOSED;
291 cfs_mutex_lock(&o->mot_ioepoch_mutex);
292 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
293 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
294 o->mot_ioepoch_count--;
296 /* Get an info from the replayed request if client is supposed
297 * to send an Attibute Update, reconstruct @rc if so */
298 if (info->mti_ioepoch->flags & MF_SOM_AU)
299 rc = MDT_IOEPOCH_GETATTR;
301 if (!mdt_ioepoch_opened(o))
302 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
303 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
309 * Regular file IOepoch close.
310 * Closes the ioepoch, checks the object state, apply obtained attributes and
311 * re-enable SOM on the object, if possible. Also checks if the recovery is
312 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
313 * to obtain SOM attributes under the server-side OST locks.
316 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
317 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
319 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
320 struct mdt_object *o)
322 struct md_attr *tmp_ma;
326 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
329 la = &info->mti_attr.ma_attr;
330 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
332 cfs_mutex_lock(&o->mot_ioepoch_mutex);
333 o->mot_ioepoch_count--;
335 tmp_ma = &info->mti_u.som.attr;
336 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
337 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
338 tmp_ma->ma_som = &info->mti_u.som.data;
339 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
340 tmp_ma->ma_valid = 0;
341 rc = mo_attr_get(info->mti_env, mdt_object_child(o), tmp_ma);
345 /* Check the on-disk SOM state. */
346 if (o->mot_flags & MOF_SOM_RECOV)
348 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
349 !(tmp_ma->ma_valid & MA_SOM))
352 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
353 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
355 opened = mdt_ioepoch_opened(o);
357 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
358 * Skip the check for file with no LOV or for unlink files.
360 if (!opened && tmp_ma->ma_valid & MA_LOV &&
361 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
363 /* If some previous writer was evicted, re-ask the
364 * client for attributes. Even if attributes are
365 * provided, we cannot believe in them.
366 * Another use case is that there is no SOM cache on
367 * disk -- first access with SOM or there was an MDS
369 ret = MDT_IOEPOCH_GETATTR;
370 else if (o->mot_flags & MOF_SOM_CHANGE)
371 /* Some previous writer changed the attribute.
372 * Do not believe to the current Size-on-MDS
373 * update, re-ask client. */
374 ret = MDT_IOEPOCH_GETATTR;
375 else if (!(la->la_valid & LA_SIZE) && achange)
376 /* Attributes were changed by the last writer
377 * only but no Size-on-MDS update is received.*/
378 ret = MDT_IOEPOCH_GETATTR;
381 if (achange || ret == MDT_IOEPOCH_GETATTR)
382 o->mot_flags |= MOF_SOM_CHANGE;
384 /* If epoch ends and relable SOM attributes are obtained, update them.
385 * Create SOM ea for new files even if there is no attributes obtained
386 * (0-length file). */
387 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
388 if (achange || o->mot_flags & MOF_SOM_CREATED) {
389 LASSERT(achange || !(la->la_valid & LA_SIZE));
390 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
392 /* Avoid the following setattrs of these attributes,
393 * e.g. for atime update. */
394 info->mti_attr.ma_valid = 0;
396 mdt_object_som_enable(o, o->mot_ioepoch);
399 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
400 /* If recovery is needed, tell the client to perform GETATTR under
402 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
403 struct mdt_body *rep;
404 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
405 rep->valid |= OBD_MD_FLGETATTRLOCK;
411 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
416 * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
417 * - a client closes the IOEpoch;
418 * - a client eviction occured.
420 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
421 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
422 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
425 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
427 struct ptlrpc_request *req = mdt_info_req(info);
430 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
431 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
434 LASSERT(o->mot_ioepoch_count);
435 LASSERT(info->mti_ioepoch == NULL ||
436 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
438 /* IOEpoch is closed only if client tells about it or eviction occures.
439 * In the replay case, always close the epoch. */
441 RETURN(mdt_ioepoch_close_on_eviction(info, o));
442 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
443 RETURN(mdt_ioepoch_close_on_replay(info, o));
444 if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
445 RETURN(mdt_ioepoch_close_reg(info, o));
446 /* IO epoch is not closed. */
447 RETURN(MDT_IOEPOCH_OPENED);
451 * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
452 * for attribute update. It happens if:
453 * - SOM Attribute Update is obtained;
454 * - the client failed to obtain it and informs MDS about it;
455 * - a client eviction occured.
456 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
459 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
461 struct ptlrpc_request *req = mdt_info_req(info);
463 int act = MDT_SOM_ENABLE;
467 LASSERT(!req || info->mti_ioepoch);
468 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
469 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
472 /* No size whereas MF_SOM_CHANGE is set means client failed to
473 * obtain ost attributes, drop the SOM cache on disk if so. */
475 (info->mti_ioepoch &&
476 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
477 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
478 act = MDT_SOM_DISABLE;
480 cfs_mutex_lock(&o->mot_ioepoch_mutex);
481 /* Mark the object it is the recovery state if we failed to obtain
483 if (act == MDT_SOM_DISABLE)
484 o->mot_flags |= MOF_SOM_RECOV;
486 if (!mdt_ioepoch_opened(o)) {
487 ioepoch = info->mti_ioepoch ?
488 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
490 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
491 rc = mdt_som_attr_set(info, o, ioepoch, act);
492 mdt_object_som_enable(o, ioepoch);
494 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
498 int mdt_write_read(struct mdt_object *o)
502 cfs_mutex_lock(&o->mot_ioepoch_mutex);
503 rc = o->mot_writecount;
504 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
508 int mdt_write_get(struct mdt_object *o)
512 cfs_mutex_lock(&o->mot_ioepoch_mutex);
513 if (o->mot_writecount < 0)
517 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
521 void mdt_write_put(struct mdt_object *o)
524 cfs_mutex_lock(&o->mot_ioepoch_mutex);
526 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
530 static int mdt_write_deny(struct mdt_object *o)
534 cfs_mutex_lock(&o->mot_ioepoch_mutex);
535 if (o->mot_writecount > 0)
539 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
543 static void mdt_write_allow(struct mdt_object *o)
546 cfs_mutex_lock(&o->mot_ioepoch_mutex);
548 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
552 /* there can be no real transaction so prepare the fake one */
553 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
555 struct mdt_device *mdt = info->mti_mdt;
556 struct ptlrpc_request *req = mdt_info_req(info);
557 struct tg_export_data *ted;
558 struct lsd_client_data *lcd;
561 /* transaction has occurred already */
562 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
565 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
566 if (info->mti_transno == 0) {
567 info->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
569 /* should be replay */
570 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
571 mdt->mdt_lut.lut_last_transno = info->mti_transno;
573 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
575 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
577 req->rq_export->exp_obd->obd_last_committed);
579 req->rq_transno = info->mti_transno;
580 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
582 /* update lcd in memory only for resent cases */
583 ted = &req->rq_export->exp_target_data;
585 cfs_mutex_lock(&ted->ted_lcd_lock);
587 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
588 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
589 if (info->mti_transno != 0)
590 lcd->lcd_last_close_transno = info->mti_transno;
591 lcd->lcd_last_close_xid = req->rq_xid;
592 lcd->lcd_last_close_result = rc;
594 /* VBR: save versions in last_rcvd for reconstruct. */
595 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
597 lcd->lcd_pre_versions[0] = pre_versions[0];
598 lcd->lcd_pre_versions[1] = pre_versions[1];
599 lcd->lcd_pre_versions[2] = pre_versions[2];
600 lcd->lcd_pre_versions[3] = pre_versions[3];
602 if (info->mti_transno != 0)
603 lcd->lcd_last_transno = info->mti_transno;
604 lcd->lcd_last_xid = req->rq_xid;
605 lcd->lcd_last_result = rc;
606 lcd->lcd_last_data = info->mti_opdata;
608 cfs_mutex_unlock(&ted->ted_lcd_lock);
613 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
615 LASSERT(mfd != NULL);
617 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
618 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
620 mfd->mfd_mode = mode;
623 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
624 struct mdt_object *o, __u64 flags, int created)
626 struct ptlrpc_request *req = mdt_info_req(info);
627 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
628 struct mdt_file_data *mfd;
629 struct md_attr *ma = &info->mti_attr;
630 struct lu_attr *la = &ma->ma_attr;
631 struct mdt_body *repbody;
632 int rc = 0, isdir, isreg;
635 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
637 isreg = S_ISREG(la->la_mode);
638 isdir = S_ISDIR(la->la_mode);
639 if (isreg && !(ma->ma_valid & MA_LOV)) {
641 * No EA, check whether it is will set regEA and dirEA since in
642 * above attr get, these size might be zero, so reset it, to
643 * retrieve the MD after create obj.
645 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
648 /* in replay case, p == NULL */
649 rc = mdt_create_data(info, p, o);
654 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
655 ma->ma_valid, ma->ma_lmm_size);
657 if (ma->ma_valid & MA_LOV) {
658 LASSERT(ma->ma_lmm_size != 0);
659 repbody->eadatasize = ma->ma_lmm_size;
661 repbody->valid |= OBD_MD_FLDIREA;
663 repbody->valid |= OBD_MD_FLEASIZE;
666 if (flags & FMODE_WRITE) {
667 rc = mdt_write_get(o);
669 mdt_ioepoch_open(info, o, created);
670 repbody->ioepoch = o->mot_ioepoch;
672 } else if (flags & MDS_FMODE_EXEC) {
673 rc = mdt_write_deny(o);
678 rc = mo_open(info->mti_env, mdt_object_child(o),
679 created ? flags | MDS_OPEN_CREATED : flags);
686 * Keep a reference on this object for this open, and is
687 * released by mdt_mfd_close().
689 mdt_object_get(info->mti_env, o);
692 * @flags is always not zero. At least it should be FMODE_READ,
693 * FMODE_WRITE or MDS_FMODE_EXEC.
698 mdt_mfd_set_mode(mfd, flags);
701 mfd->mfd_xid = req->rq_xid;
704 if (req_is_replay(req)) {
705 struct mdt_file_data *old_mfd;
706 /* Check wheather old cookie already exist in
707 * the list, becasue when do recovery, client
708 * might be disconnected from server, and
709 * restart replay, so there maybe some orphan
710 * mfd here, we should remove them */
711 LASSERT(info->mti_rr.rr_handle != NULL);
712 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
714 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
715 "cookie=" LPX64"\n", mfd,
716 PFID(mdt_object_fid(mfd->mfd_object)),
717 info->mti_rr.rr_handle->cookie);
718 cfs_spin_lock(&med->med_open_lock);
719 class_handle_unhash(&old_mfd->mfd_handle);
720 cfs_list_del_init(&old_mfd->mfd_list);
721 cfs_spin_unlock(&med->med_open_lock);
722 /* no attr update for that close */
724 ma->ma_valid |= MA_FLAGS;
725 ma->ma_attr_flags |= MDS_RECOV_OPEN;
726 mdt_mfd_close(info, old_mfd);
727 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
728 ma->ma_valid &= ~MA_FLAGS;
730 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
731 info->mti_rr.rr_handle->cookie);
732 mfd->mfd_old_handle.cookie =
733 info->mti_rr.rr_handle->cookie;
735 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
737 if (req->rq_export->exp_disconnected) {
738 cfs_spin_lock(&med->med_open_lock);
739 class_handle_unhash(&mfd->mfd_handle);
740 cfs_list_del_init(&mfd->mfd_list);
741 cfs_spin_unlock(&med->med_open_lock);
742 mdt_mfd_close(info, mfd);
744 cfs_spin_lock(&med->med_open_lock);
745 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
746 cfs_spin_unlock(&med->med_open_lock);
749 mdt_empty_transno(info, rc);
751 GOTO(err_out, rc = -ENOMEM);
757 if (flags & FMODE_WRITE)
758 /* XXX We also need to close io epoch here.
759 * See LU-1220 - green */
761 else if (flags & FMODE_EXEC)
766 int mdt_finish_open(struct mdt_thread_info *info,
767 struct mdt_object *p, struct mdt_object *o,
768 __u64 flags, int created, struct ldlm_reply *rep)
770 struct ptlrpc_request *req = mdt_info_req(info);
771 struct obd_export *exp = req->rq_export;
772 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
773 struct md_attr *ma = &info->mti_attr;
774 struct lu_attr *la = &ma->ma_attr;
775 struct mdt_file_data *mfd;
776 struct mdt_body *repbody;
778 int isreg, isdir, islnk;
782 LASSERT(ma->ma_valid & MA_INODE);
784 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
786 isreg = S_ISREG(la->la_mode);
787 isdir = S_ISDIR(la->la_mode);
788 islnk = S_ISLNK(la->la_mode);
789 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
791 if (exp_connect_rmtclient(exp)) {
792 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
794 rc = mdt_pack_remote_perm(info, o, buf);
796 repbody->valid &= ~OBD_MD_FLRMTPERM;
797 repbody->aclsize = 0;
799 repbody->valid |= OBD_MD_FLRMTPERM;
800 repbody->aclsize = sizeof(struct mdt_remote_perm);
803 #ifdef CONFIG_FS_POSIX_ACL
804 else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
805 const struct lu_env *env = info->mti_env;
806 struct md_object *next = mdt_object_child(o);
807 struct lu_buf *buf = &info->mti_buf;
809 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
810 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
812 if (buf->lb_len > 0) {
813 rc = mo_xattr_get(env, next, buf,
814 XATTR_NAME_ACL_ACCESS);
816 if (rc == -ENODATA) {
817 repbody->aclsize = 0;
818 repbody->valid |= OBD_MD_FLACL;
820 } else if (rc == -EOPNOTSUPP) {
823 CERROR("got acl size: %d\n", rc);
826 repbody->aclsize = rc;
827 repbody->valid |= OBD_MD_FLACL;
834 if (info->mti_mdt->mdt_opts.mo_mds_capa &&
835 exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
836 struct lustre_capa *capa;
838 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
840 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
841 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
844 repbody->valid |= OBD_MD_FLMDSCAPA;
846 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
847 exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
848 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
849 struct lustre_capa *capa;
851 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
853 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
854 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
857 repbody->valid |= OBD_MD_FLOSSCAPA;
861 * If we are following a symlink, don't open; and do not return open
862 * handle for special nodes as client required.
864 if (islnk || (!isreg && !isdir &&
865 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
866 lustre_msg_set_transno(req->rq_repmsg, 0);
870 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
873 * We need to return the existing object's fid back, so it is done here,
874 * after preparing the reply.
876 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
879 /* This can't be done earlier, we need to return reply body */
881 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
882 /* We are trying to create or write an existing dir. */
885 } else if (flags & MDS_OPEN_DIRECTORY)
888 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
889 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
894 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
895 cfs_spin_lock(&med->med_open_lock);
896 cfs_list_for_each(t, &med->med_open_head) {
897 mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
898 if (mfd->mfd_xid == req->rq_xid) {
903 cfs_spin_unlock(&med->med_open_lock);
906 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
907 /*set repbody->ea_size for resent case*/
908 if (ma->ma_valid & MA_LOV) {
909 LASSERT(ma->ma_lmm_size != 0);
910 repbody->eadatasize = ma->ma_lmm_size;
912 repbody->valid |= OBD_MD_FLDIREA;
914 repbody->valid |= OBD_MD_FLEASIZE;
920 rc = mdt_mfd_open(info, p, o, flags, created);
924 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
925 struct lsd_client_data *lcd);
927 void mdt_reconstruct_open(struct mdt_thread_info *info,
928 struct mdt_lock_handle *lhc)
930 const struct lu_env *env = info->mti_env;
931 struct mdt_device *mdt = info->mti_mdt;
932 struct req_capsule *pill = info->mti_pill;
933 struct ptlrpc_request *req = mdt_info_req(info);
934 struct tg_export_data *ted = &req->rq_export->exp_target_data;
935 struct lsd_client_data *lcd = ted->ted_lcd;
936 struct md_attr *ma = &info->mti_attr;
937 struct mdt_reint_record *rr = &info->mti_rr;
938 __u32 flags = info->mti_spec.sp_cr_flags;
939 struct ldlm_reply *ldlm_rep;
940 struct mdt_object *parent;
941 struct mdt_object *child;
942 struct mdt_body *repbody;
946 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
947 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
948 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
950 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
951 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
953 ma->ma_need = MA_INODE;
954 if (ma->ma_lmm_size > 0)
955 ma->ma_need |= MA_LOV;
959 mdt_req_from_lcd(req, lcd);
960 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
962 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
963 ldlm_rep->lock_policy_res1, req->rq_status);
965 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
967 /* We did not create successfully, return error to client. */
968 GOTO(out, rc = req->rq_status);
970 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
971 struct obd_export *exp = req->rq_export;
973 * We failed after creation, but we do not know in which step
974 * we failed. So try to check the child object.
976 parent = mdt_object_find(env, mdt, rr->rr_fid1);
977 if (IS_ERR(parent)) {
978 rc = PTR_ERR(parent);
979 LCONSOLE_WARN("Parent "DFID" lookup error %d."
980 " Evicting client %s with export %s.\n",
981 PFID(rr->rr_fid1), rc,
982 obd_uuid2str(&exp->exp_client_uuid),
983 obd_export_nid2str(exp));
984 mdt_export_evict(exp);
987 child = mdt_object_find(env, mdt, rr->rr_fid2);
990 LCONSOLE_WARN("Child "DFID" lookup error %d."
991 " Evicting client %s with export %s.\n",
992 PFID(mdt_object_fid(child)), rc,
993 obd_uuid2str(&exp->exp_client_uuid),
994 obd_export_nid2str(exp));
995 mdt_object_put(env, parent);
996 mdt_export_evict(exp);
999 rc = mdt_object_exists(child);
1001 struct md_object *next;
1003 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1004 next = mdt_object_child(child);
1005 rc = mo_attr_get(env, next, ma);
1007 rc = mdt_finish_open(info, parent, child,
1008 flags, 1, ldlm_rep);
1009 } else if (rc < 0) {
1010 /* the child object was created on remote server */
1011 repbody->fid1 = *rr->rr_fid2;
1012 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1014 } else if (rc == 0) {
1015 /* the child does not exist, we should do regular open */
1016 mdt_object_put(env, parent);
1017 mdt_object_put(env, child);
1018 GOTO(regular_open, 0);
1020 mdt_object_put(env, parent);
1021 mdt_object_put(env, child);
1025 /* We did not try to create, so we are a pure open */
1026 rc = mdt_reint_open(info, lhc);
1031 req->rq_status = rc;
1032 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1033 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1036 int mdt_open_by_fid(struct mdt_thread_info* info,
1037 struct ldlm_reply *rep)
1039 const struct lu_env *env = info->mti_env;
1040 __u32 flags = info->mti_spec.sp_cr_flags;
1041 struct mdt_reint_record *rr = &info->mti_rr;
1042 struct md_attr *ma = &info->mti_attr;
1043 struct mdt_object *o;
1047 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1049 RETURN(rc = PTR_ERR(o));
1051 rc = mdt_object_exists(o);
1053 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1057 rc = mo_attr_get(env, mdt_object_child(o), ma);
1059 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1060 } else if (rc == 0) {
1063 /* the child object was created on remote server */
1064 struct mdt_body *repbody;
1065 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1066 repbody->fid1 = *rr->rr_fid2;
1067 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1071 mdt_object_put(info->mti_env, o);
1075 int mdt_open_anon_by_fid(struct mdt_thread_info *info,
1076 struct ldlm_reply *rep,
1077 struct mdt_lock_handle *lhc)
1079 const struct lu_env *env = info->mti_env;
1080 struct mdt_device *mdt = info->mti_mdt;
1081 __u32 flags = info->mti_spec.sp_cr_flags;
1082 struct mdt_reint_record *rr = &info->mti_rr;
1083 struct md_attr *ma = &info->mti_attr;
1084 struct mdt_object *parent= NULL;
1085 struct mdt_object *o;
1090 if (md_should_create(flags)) {
1091 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1092 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1093 if (IS_ERR(parent)) {
1094 CDEBUG(D_INODE, "Fail to find parent "DFID
1095 " for anonymous created %ld, try to"
1096 " use server-side parent.\n",
1097 PFID(rr->rr_fid1), PTR_ERR(parent));
1102 ma->ma_need |= MA_PFID;
1105 o = mdt_object_find(env, mdt, rr->rr_fid2);
1107 RETURN(rc = PTR_ERR(o));
1109 rc = mdt_object_exists(o);
1111 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1113 GOTO(out, rc = -ENOENT);
1114 } else if (rc < 0) {
1115 CERROR("NFS remote open shouldn't happen.\n");
1118 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1122 if (flags & FMODE_WRITE)
1124 else if (flags & MDS_FMODE_EXEC)
1129 mdt_lock_handle_init(lhc);
1130 mdt_lock_reg_init(lhc, lm);
1131 rc = mdt_object_lock(info, o, lhc,
1132 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1137 rc = mo_attr_get(env, mdt_object_child(o), ma);
1141 if (ma->ma_valid & MA_PFID) {
1142 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1143 if (IS_ERR(parent)) {
1144 CDEBUG(D_INODE, "Fail to find parent "DFID
1145 " for anonymous created %ld, try to"
1146 " use system default.\n",
1147 PFID(&ma->ma_pfid), PTR_ERR(parent));
1152 if (flags & MDS_OPEN_LOCK)
1153 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1154 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1156 if (!(flags & MDS_OPEN_LOCK) || rc)
1157 mdt_object_unlock(info, o, lhc, 1);
1161 mdt_object_put(env, o);
1163 mdt_object_put(env, parent);
1167 int mdt_pin(struct mdt_thread_info* info)
1170 RETURN(err_serious(-EOPNOTSUPP));
1173 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1174 int mdt_cross_open(struct mdt_thread_info* info,
1175 const struct lu_fid *fid,
1176 struct ldlm_reply *rep, __u32 flags)
1178 struct md_attr *ma = &info->mti_attr;
1179 struct mdt_object *o;
1183 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1185 RETURN(rc = PTR_ERR(o));
1187 rc = mdt_object_exists(o);
1189 /* Do permission check for cross-open. */
1190 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1191 NULL, flags | MDS_OPEN_CROSS);
1195 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1196 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
1198 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1199 } else if (rc == 0) {
1201 * Something is wrong here. lookup was positive but there is
1204 CERROR("Cross-ref object doesn't exist!\n");
1207 /* Something is wrong here, the object is on another MDS! */
1208 CERROR("The object isn't on this server! FLD error?\n");
1209 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1211 "Object isn't on this server! FLD error?\n");
1217 mdt_object_put(info->mti_env, o);
1221 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1223 struct mdt_device *mdt = info->mti_mdt;
1224 struct ptlrpc_request *req = mdt_info_req(info);
1225 struct mdt_object *parent;
1226 struct mdt_object *child;
1227 struct mdt_lock_handle *lh;
1228 struct ldlm_reply *ldlm_rep;
1229 struct mdt_body *repbody;
1230 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1231 struct md_attr *ma = &info->mti_attr;
1232 __u64 create_flags = info->mti_spec.sp_cr_flags;
1233 struct mdt_reint_record *rr = &info->mti_rr;
1234 struct lu_name *lname;
1240 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1241 (obd_timeout + 1) / 4);
1243 mdt_counter_incr(req->rq_export, LPROC_MDT_OPEN);
1244 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1246 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1247 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1249 ma->ma_need = MA_INODE;
1250 if (ma->ma_lmm_size > 0)
1251 ma->ma_need |= MA_LOV;
1255 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1256 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1258 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1259 CERROR("file join is not supported anymore.\n");
1260 GOTO(out, result = err_serious(-EOPNOTSUPP));
1262 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1264 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1265 info->mti_spec.u.sp_ea.eadata == NULL)
1266 GOTO(out, result = err_serious(-EINVAL));
1268 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1269 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1270 PFID(rr->rr_fid1), rr->rr_name,
1271 PFID(rr->rr_fid2), create_flags,
1272 ma->ma_attr.la_mode, msg_flags);
1274 if (req_is_replay(req) ||
1275 (req->rq_export->exp_libclient && create_flags&MDS_OPEN_HAS_EA)) {
1276 /* This is a replay request or from liblustre with ea. */
1277 result = mdt_open_by_fid(info, ldlm_rep);
1279 if (result != -ENOENT) {
1280 if (req->rq_export->exp_libclient &&
1281 create_flags & MDS_OPEN_HAS_EA)
1282 GOTO(out, result = 0);
1286 * We didn't find the correct object, so we need to re-create it
1287 * via a regular replay.
1289 if (!(create_flags & MDS_OPEN_CREAT)) {
1290 DEBUG_REQ(D_ERROR, req,
1291 "OPEN & CREAT not in open replay.");
1292 GOTO(out, result = -EFAULT);
1294 CDEBUG(D_INFO, "Open replay did find object, continue as "
1296 } else if (rr->rr_namelen == 0 && !info->mti_cross_ref &&
1297 create_flags & MDS_OPEN_LOCK) {
1298 result = mdt_open_anon_by_fid(info, ldlm_rep, lhc);
1302 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1303 GOTO(out, result = err_serious(-ENOMEM));
1305 mdt_set_disposition(info, ldlm_rep,
1306 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1308 if (info->mti_cross_ref) {
1309 /* This is cross-ref open */
1310 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1311 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1316 lh = &info->mti_lh[MDT_LH_PARENT];
1317 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1318 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1320 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1321 MDS_INODELOCK_UPDATE);
1323 GOTO(out, result = PTR_ERR(parent));
1325 /* get and check version of parent */
1326 result = mdt_version_get_check(info, parent, 0);
1328 GOTO(out_parent, result);
1330 fid_zero(child_fid);
1332 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1333 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1334 lname, child_fid, &info->mti_spec);
1335 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1336 "looking for "DFID"/%s, result fid="DFID"\n",
1337 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1339 if (result != 0 && result != -ENOENT && result != -ESTALE)
1340 GOTO(out_parent, result);
1342 if (result == -ENOENT || result == -ESTALE) {
1343 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1344 if (result == -ESTALE) {
1346 * -ESTALE means the parent is a dead(unlinked) dir, so
1347 * it should return -ENOENT to in accordance with the
1348 * original mds implementaion.
1350 GOTO(out_parent, result = -ENOENT);
1352 if (!(create_flags & MDS_OPEN_CREAT))
1353 GOTO(out_parent, result);
1354 *child_fid = *info->mti_rr.rr_fid2;
1355 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1359 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1360 * return FID back in that case.
1362 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1365 child = mdt_object_find(info->mti_env, mdt, child_fid);
1367 GOTO(out_parent, result = PTR_ERR(child));
1369 /** check version of child */
1370 rc = mdt_version_get_check(info, child, 1);
1372 GOTO(out_child, result = rc);
1374 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1375 if (result == -ENOENT) {
1376 if (mdt_object_obf(parent))
1377 GOTO(out_child, result = -EPERM);
1379 /* save versions in reply */
1380 mdt_version_get_save(info, parent, 0);
1381 mdt_version_get_save(info, child, 1);
1383 /* version of child will be changed */
1384 info->mti_mos = child;
1386 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1387 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1389 /* Let lower layers know what is lock mode on directory. */
1390 info->mti_spec.sp_cr_mode =
1391 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1394 * Do not perform lookup sanity check. We know that name does
1397 info->mti_spec.sp_cr_lookup = 0;
1398 info->mti_spec.sp_feat = &dt_directory_features;
1400 result = mdo_create(info->mti_env,
1401 mdt_object_child(parent),
1403 mdt_object_child(child),
1406 if (result == -ERESTART) {
1407 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1408 GOTO(out_child, result);
1411 GOTO(out_child, result);
1415 /* We have to get attr & lov ea for this object */
1416 result = mo_attr_get(info->mti_env, mdt_object_child(child),
1419 * The object is on remote node, return its FID for remote open.
1421 if (result == -EREMOTE) {
1423 * Check if this lock already was sent to client and
1424 * this is resent case. For resent case do not take lock
1425 * again, use what is already granted.
1427 LASSERT(lhc != NULL);
1429 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1430 struct ldlm_lock *lock;
1432 LASSERT(msg_flags & MSG_RESENT);
1434 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1436 CERROR("Invalid lock handle "LPX64"\n",
1437 lhc->mlh_reg_lh.cookie);
1440 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1441 &lock->l_resource->lr_name));
1442 LDLM_LOCK_PUT(lock);
1445 mdt_lock_handle_init(lhc);
1446 mdt_lock_reg_init(lhc, LCK_PR);
1448 rc = mdt_object_lock(info, child, lhc,
1449 MDS_INODELOCK_LOOKUP,
1452 repbody->fid1 = *mdt_object_fid(child);
1453 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1456 GOTO(out_child, result);
1460 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1462 /* get openlock if this is not replay and if a client requested it */
1463 if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
1466 if (create_flags & FMODE_WRITE)
1468 else if (create_flags & MDS_FMODE_EXEC)
1472 mdt_lock_handle_init(lhc);
1473 mdt_lock_reg_init(lhc, lm);
1474 rc = mdt_object_lock(info, child, lhc,
1475 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1479 GOTO(out_child, result);
1482 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1486 /* Try to open it now. */
1487 rc = mdt_finish_open(info, parent, child, create_flags,
1491 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1492 /* openlock was acquired and mdt_finish_open failed -
1493 drop the openlock */
1494 mdt_object_unlock(info, child, lhc, 1);
1498 ma->ma_cookie_size = 0;
1499 info->mti_no_need_trans = 1;
1500 rc = mdo_unlink(info->mti_env,
1501 mdt_object_child(parent),
1502 mdt_object_child(child),
1506 CERROR("Error in cleanup of open\n");
1511 mdt_object_put(info->mti_env, child);
1513 mdt_object_unlock_put(info, parent, lh, result || !created);
1515 if (result && result != -EREMOTE)
1516 lustre_msg_set_transno(req->rq_repmsg, 0);
1520 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1521 MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1523 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1525 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1528 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1530 struct mdt_object *o = mfd->mfd_object;
1531 struct md_object *next = mdt_object_child(o);
1532 struct md_attr *ma = &info->mti_attr;
1533 int ret = MDT_IOEPOCH_CLOSED;
1538 mode = mfd->mfd_mode;
1540 if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1542 ret = mdt_ioepoch_close(info, o);
1543 } else if (mode & MDS_FMODE_EXEC) {
1545 } else if (mode & MDS_FMODE_EPOCH) {
1546 ret = mdt_ioepoch_close(info, o);
1547 } else if (mode & MDS_FMODE_SOM) {
1548 ret = mdt_som_au_close(info, o);
1551 /* Update atime on close only. */
1552 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1553 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1554 /* Set the atime only. */
1555 ma->ma_valid = MA_INODE;
1556 ma->ma_attr.la_valid = LA_ATIME;
1557 rc = mo_attr_set(info->mti_env, next, ma);
1560 ma->ma_need |= MA_INODE;
1561 ma->ma_valid &= ~MA_INODE;
1563 if (!MFD_CLOSED(mode))
1564 rc = mo_close(info->mti_env, next, ma, mode);
1566 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1567 struct mdt_export_data *med;
1569 /* The IOepoch is still opened or SOM update is needed.
1570 * Put mfd back into the list. */
1571 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1572 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1573 MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1575 LASSERT(mdt_info_req(info));
1576 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1577 cfs_spin_lock(&med->med_open_lock);
1578 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1579 class_handle_hash_back(&mfd->mfd_handle);
1580 cfs_spin_unlock(&med->med_open_lock);
1582 if (ret == MDT_IOEPOCH_OPENED) {
1586 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1587 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1591 mdt_object_put(info->mti_env, o);
1594 RETURN(rc ? rc : ret);
1597 int mdt_close(struct mdt_thread_info *info)
1599 struct mdt_export_data *med;
1600 struct mdt_file_data *mfd;
1601 struct mdt_object *o;
1602 struct md_attr *ma = &info->mti_attr;
1603 struct mdt_body *repbody = NULL;
1604 struct ptlrpc_request *req = mdt_info_req(info);
1608 mdt_counter_incr(req->rq_export, LPROC_MDT_CLOSE);
1609 /* Close may come with the Size-on-MDS update. Unpack it. */
1610 rc = mdt_close_unpack(info);
1612 RETURN(err_serious(rc));
1614 LASSERT(info->mti_ioepoch);
1616 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1617 info->mti_mdt->mdt_max_mdsize);
1618 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1619 info->mti_mdt->mdt_max_cookiesize);
1620 rc = req_capsule_server_pack(info->mti_pill);
1621 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1622 mdt_client_compatibility(info);
1624 mdt_fix_reply(info);
1625 RETURN(lustre_msg_get_status(req->rq_repmsg));
1628 /* Continue to close handle even if we can not pack reply */
1630 repbody = req_capsule_server_get(info->mti_pill,
1632 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1634 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1637 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1639 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1642 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1643 repbody->eadatasize = 0;
1644 repbody->aclsize = 0;
1646 rc = err_serious(rc);
1649 med = &req->rq_export->exp_mdt_data;
1650 cfs_spin_lock(&med->med_open_lock);
1651 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1652 if (mdt_mfd_closed(mfd)) {
1653 cfs_spin_unlock(&med->med_open_lock);
1654 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1655 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1656 info->mti_ioepoch->handle.cookie);
1657 /** not serious error since bug 3633 */
1660 class_handle_unhash(&mfd->mfd_handle);
1661 cfs_list_del_init(&mfd->mfd_list);
1662 cfs_spin_unlock(&med->med_open_lock);
1664 /* Do not lose object before last unlink. */
1665 o = mfd->mfd_object;
1666 mdt_object_get(info->mti_env, o);
1667 ret = mdt_mfd_close(info, mfd);
1668 if (repbody != NULL)
1669 rc = mdt_handle_last_unlink(info, o, ma);
1670 mdt_empty_transno(info, rc);
1671 mdt_object_put(info->mti_env, o);
1673 if (repbody != NULL) {
1674 mdt_client_compatibility(info);
1675 rc = mdt_fix_reply(info);
1678 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1679 RETURN(err_serious(-ENOMEM));
1681 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1682 OBD_FAIL_MDS_CLOSE_NET_REP))
1683 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1684 RETURN(rc ? rc : ret);
1688 * DONE_WRITING rpc handler.
1690 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1691 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1692 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1693 * skip attributes and reconstruct the reply here.
1695 int mdt_done_writing(struct mdt_thread_info *info)
1697 struct ptlrpc_request *req = mdt_info_req(info);
1698 struct mdt_body *repbody = NULL;
1699 struct mdt_export_data *med;
1700 struct mdt_file_data *mfd;
1704 rc = req_capsule_server_pack(info->mti_pill);
1706 RETURN(err_serious(rc));
1708 repbody = req_capsule_server_get(info->mti_pill,
1710 repbody->eadatasize = 0;
1711 repbody->aclsize = 0;
1713 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1714 rc = mdt_close_unpack(info);
1716 RETURN(err_serious(rc));
1718 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1719 RETURN(lustre_msg_get_status(req->rq_repmsg));
1721 med = &info->mti_exp->exp_mdt_data;
1722 cfs_spin_lock(&med->med_open_lock);
1723 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1725 cfs_spin_unlock(&med->med_open_lock);
1726 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1727 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1728 PFID(info->mti_rr.rr_fid1),
1729 info->mti_ioepoch->handle.cookie,
1730 info->mti_ioepoch->ioepoch);
1731 /* If this is a replay, reconstruct the transno. */
1732 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1733 rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1735 mdt_empty_transno(info, rc);
1741 LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1742 mfd->mfd_mode == MDS_FMODE_TRUNC);
1743 class_handle_unhash(&mfd->mfd_handle);
1744 cfs_list_del_init(&mfd->mfd_list);
1745 cfs_spin_unlock(&med->med_open_lock);
1747 /* Set EPOCH CLOSE flag if not set by client. */
1748 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1749 info->mti_attr.ma_valid = 0;
1751 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1752 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1753 if (info->mti_attr.ma_lmm == NULL)
1756 rc = mdt_mfd_close(info, mfd);
1758 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1759 mdt_empty_transno(info, rc);