4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_open.c
38 * Lustre Metadata Target (mdt) open/close file handling
40 * Author: Huang Hua <huanghua@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <lustre_acl.h>
46 #include <lustre_mds.h>
47 #include "mdt_internal.h"
49 /* we do nothing because we do not have refcount now */
50 static void mdt_mfd_get(void *mfdp)
54 static struct portals_handle_ops mfd_handle_ops = {
55 .hop_addref = mdt_mfd_get,
59 /* Create a new mdt_file_data struct, initialize it,
60 * and insert it to global hash table */
61 struct mdt_file_data *mdt_mfd_new(void)
63 struct mdt_file_data *mfd;
68 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
69 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
70 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
76 * Find the mfd pointed to by handle in global hash table.
77 * In case of replay the handle is obsoleted
78 * but mfd can be found in mfd list by that handle
80 struct mdt_file_data *mdt_handle2mfd(struct mdt_thread_info *info,
81 const struct lustre_handle *handle)
83 struct ptlrpc_request *req = mdt_info_req(info);
84 struct mdt_file_data *mfd;
87 LASSERT(handle != NULL);
88 mfd = class_handle2object(handle->cookie);
89 /* during dw/setattr replay the mfd can be found by old handle */
90 if (mfd == NULL && req_is_replay(req)) {
91 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
92 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
93 if (mfd->mfd_old_handle.cookie == handle->cookie)
102 void mdt_mfd_free(struct mdt_file_data *mfd)
104 LASSERT(cfs_list_empty(&mfd->mfd_list));
105 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
108 static int mdt_create_data(struct mdt_thread_info *info,
109 struct mdt_object *p, struct mdt_object *o)
111 struct md_op_spec *spec = &info->mti_spec;
112 struct md_attr *ma = &info->mti_attr;
116 if (!md_should_create(spec->sp_cr_flags))
119 ma->ma_need = MA_INODE | MA_LOV;
121 cfs_mutex_lock(&o->mot_lov_mutex);
122 if (!(o->mot_flags & MOF_LOV_CREATED)) {
123 rc = mdo_create_data(info->mti_env,
124 p ? mdt_object_child(p) : NULL,
125 mdt_object_child(o), spec, ma);
126 if (rc == 0 && ma->ma_valid & MA_LOV)
127 o->mot_flags |= MOF_LOV_CREATED;
129 cfs_mutex_unlock(&o->mot_lov_mutex);
133 static int mdt_ioepoch_opened(struct mdt_object *mo)
135 return mo->mot_ioepoch_count;
138 int mdt_object_is_som_enabled(struct mdt_object *mo)
140 return !mo->mot_ioepoch;
144 * Re-enable Size-on-MDS.
145 * Call under ->mot_ioepoch_mutex.
147 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
149 if (ioepoch == mo->mot_ioepoch) {
150 LASSERT(!mdt_ioepoch_opened(mo));
157 * Open the IOEpoch. It is allowed if @writecount is not negative.
158 * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
160 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
163 struct mdt_device *mdt = info->mti_mdt;
168 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
169 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
172 cfs_mutex_lock(&o->mot_ioepoch_mutex);
173 if (mdt_ioepoch_opened(o)) {
174 /* Epoch continues even if there is no writers yet. */
175 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
176 o->mot_ioepoch, PFID(mdt_object_fid(o)));
178 /* XXX: ->mdt_ioepoch is not initialized at the mount */
179 cfs_spin_lock(&mdt->mdt_ioepoch_lock);
180 if (mdt->mdt_ioepoch < info->mti_replayepoch)
181 mdt->mdt_ioepoch = info->mti_replayepoch;
183 if (info->mti_replayepoch)
184 o->mot_ioepoch = info->mti_replayepoch;
185 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
186 o->mot_ioepoch = ++mdt->mdt_ioepoch;
188 o->mot_ioepoch = mdt->mdt_ioepoch;
190 cfs_spin_unlock(&mdt->mdt_ioepoch_lock);
192 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
193 o->mot_ioepoch, PFID(mdt_object_fid(o)));
195 o->mot_flags |= MOF_SOM_CREATED;
198 o->mot_ioepoch_count++;
199 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
201 /* Cancel Size-on-MDS attributes cached on clients for the open case.
202 * In the truncate case, see mdt_reint_setattr(). */
203 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
204 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
205 mdt_lock_reg_init(lh, LCK_EX);
206 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
209 mdt_object_unlock(info, o, lh, 1);
215 * Update SOM on-disk attributes.
216 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
217 * mountid and attributes. If disabling, zero IOEpoch id in lustre-ea.
218 * Call under ->mot_ioepoch_mutex.
220 static int mdt_som_attr_set(struct mdt_thread_info *info,
221 struct mdt_object *obj, __u64 ioepoch, int enable)
223 struct md_attr *ma = &info->mti_attr;
227 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
228 " on "DFID".\n", enable ? "update" : "disabling",
229 ioepoch, PFID(mdt_object_fid(obj)));
231 ma->ma_valid |= MA_SOM;
232 ma->ma_som = &info->mti_u.som.data;
234 struct mdt_device *mdt = info->mti_mdt;
235 struct lu_attr *la = &ma->ma_attr;
237 ma->ma_som->msd_ioepoch = ioepoch;
238 ma->ma_som->msd_size = la->la_valid & LA_SIZE ? la->la_size : 0;
239 ma->ma_som->msd_blocks = la->la_valid & LA_BLOCKS ?
241 ma->ma_som->msd_mountid = mdt->mdt_lut.lut_obd->u.obt.obt_mount_count;
242 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
244 ma->ma_som->msd_ioepoch = IOEPOCH_INVAL;
245 ma->ma_attr.la_valid &= LA_ATIME;
248 /* Since we have opened the file, it is unnecessary
249 * to check permission when close it. Between the "open"
250 * and "close", maybe someone has changed the file mode
251 * or flags, or the file created mode do not permit wirte,
252 * and so on. Just set MDS_PERM_BYPASS for all the cases. */
253 ma->ma_attr_flags |= MDS_PERM_BYPASS | MDS_SOM;
255 rc = mdt_attr_set(info, obj, ma, 0);
259 /** Perform the eviction specific actions on ioepoch close. */
260 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
261 struct mdt_object *o)
265 cfs_mutex_lock(&o->mot_ioepoch_mutex);
266 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
267 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
268 o->mot_ioepoch_count);
269 o->mot_ioepoch_count--;
271 /* If eviction occured set MOF_SOM_RECOV,
272 * if no other epoch holders, disable SOM on disk. */
273 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
274 if (!mdt_ioepoch_opened(o)) {
275 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
276 mdt_object_som_enable(o, o->mot_ioepoch);
278 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
283 * Perform the replay specific actions on ioepoch close.
284 * Skip SOM attribute update if obtained and just forget about the inode state
285 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
287 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
288 struct mdt_object *o)
290 int rc = MDT_IOEPOCH_CLOSED;
293 cfs_mutex_lock(&o->mot_ioepoch_mutex);
294 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
295 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
296 o->mot_ioepoch_count--;
298 /* Get an info from the replayed request if client is supposed
299 * to send an Attibute Update, reconstruct @rc if so */
300 if (info->mti_ioepoch->flags & MF_SOM_AU)
301 rc = MDT_IOEPOCH_GETATTR;
303 if (!mdt_ioepoch_opened(o))
304 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
305 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
311 * Regular file IOepoch close.
312 * Closes the ioepoch, checks the object state, apply obtained attributes and
313 * re-enable SOM on the object, if possible. Also checks if the recovery is
314 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
315 * to obtain SOM attributes under the server-side OST locks.
318 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
319 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
321 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
322 struct mdt_object *o)
324 struct md_attr *tmp_ma;
328 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
331 la = &info->mti_attr.ma_attr;
332 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
334 cfs_mutex_lock(&o->mot_ioepoch_mutex);
335 o->mot_ioepoch_count--;
337 tmp_ma = &info->mti_u.som.attr;
338 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
339 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
340 tmp_ma->ma_som = &info->mti_u.som.data;
341 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
342 tmp_ma->ma_valid = 0;
343 rc = mo_attr_get(info->mti_env, mdt_object_child(o), tmp_ma);
347 /* Check the on-disk SOM state. */
348 if (o->mot_flags & MOF_SOM_RECOV)
350 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
351 !(tmp_ma->ma_valid & MA_SOM))
354 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
355 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
357 opened = mdt_ioepoch_opened(o);
359 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
360 * Skip the check for file with no LOV or for unlink files.
362 if (!opened && tmp_ma->ma_valid & MA_LOV &&
363 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
365 /* If some previous writer was evicted, re-ask the
366 * client for attributes. Even if attributes are
367 * provided, we cannot believe in them.
368 * Another use case is that there is no SOM cache on
369 * disk -- first access with SOM or there was an MDS
371 ret = MDT_IOEPOCH_GETATTR;
372 else if (o->mot_flags & MOF_SOM_CHANGE)
373 /* Some previous writer changed the attribute.
374 * Do not believe to the current Size-on-MDS
375 * update, re-ask client. */
376 ret = MDT_IOEPOCH_GETATTR;
377 else if (!(la->la_valid & LA_SIZE) && achange)
378 /* Attributes were changed by the last writer
379 * only but no Size-on-MDS update is received.*/
380 ret = MDT_IOEPOCH_GETATTR;
383 if (achange || ret == MDT_IOEPOCH_GETATTR)
384 o->mot_flags |= MOF_SOM_CHANGE;
386 /* If epoch ends and relable SOM attributes are obtained, update them.
387 * Create SOM ea for new files even if there is no attributes obtained
388 * (0-length file). */
389 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
390 if (achange || o->mot_flags & MOF_SOM_CREATED) {
391 LASSERT(achange || !(la->la_valid & LA_SIZE));
392 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
394 /* Avoid the following setattrs of these attributes,
395 * e.g. for atime update. */
396 info->mti_attr.ma_valid = 0;
398 mdt_object_som_enable(o, o->mot_ioepoch);
401 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
402 /* If recovery is needed, tell the client to perform GETATTR under
404 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
405 struct mdt_body *rep;
406 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
407 rep->valid |= OBD_MD_FLGETATTRLOCK;
413 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
418 * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
419 * - a client closes the IOEpoch;
420 * - a client eviction occured.
422 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
423 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
424 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
427 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
429 struct ptlrpc_request *req = mdt_info_req(info);
432 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
433 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
436 LASSERT(o->mot_ioepoch_count);
437 LASSERT(info->mti_ioepoch == NULL ||
438 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
440 /* IOEpoch is closed only if client tells about it or eviction occures.
441 * In the replay case, always close the epoch. */
443 RETURN(mdt_ioepoch_close_on_eviction(info, o));
444 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
445 RETURN(mdt_ioepoch_close_on_replay(info, o));
446 if (info->mti_ioepoch->flags & MF_EPOCH_CLOSE)
447 RETURN(mdt_ioepoch_close_reg(info, o));
448 /* IO epoch is not closed. */
449 RETURN(MDT_IOEPOCH_OPENED);
453 * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
454 * for attribute update. It happens if:
455 * - SOM Attribute Update is obtained;
456 * - the client failed to obtain it and informs MDS about it;
457 * - a client eviction occured.
458 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
461 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
463 struct ptlrpc_request *req = mdt_info_req(info);
465 int act = MDT_SOM_ENABLE;
469 LASSERT(!req || info->mti_ioepoch);
470 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
471 !S_ISREG(lu_object_attr(&o->mot_obj.mo_lu)))
474 /* No size whereas MF_SOM_CHANGE is set means client failed to
475 * obtain ost attributes, drop the SOM cache on disk if so. */
477 (info->mti_ioepoch &&
478 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
479 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
480 act = MDT_SOM_DISABLE;
482 cfs_mutex_lock(&o->mot_ioepoch_mutex);
483 /* Mark the object it is the recovery state if we failed to obtain
485 if (act == MDT_SOM_DISABLE)
486 o->mot_flags |= MOF_SOM_RECOV;
488 if (!mdt_ioepoch_opened(o)) {
489 ioepoch = info->mti_ioepoch ?
490 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
492 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
493 rc = mdt_som_attr_set(info, o, ioepoch, act);
494 mdt_object_som_enable(o, ioepoch);
496 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
500 int mdt_write_read(struct mdt_object *o)
504 cfs_mutex_lock(&o->mot_ioepoch_mutex);
505 rc = o->mot_writecount;
506 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
510 int mdt_write_get(struct mdt_object *o)
514 cfs_mutex_lock(&o->mot_ioepoch_mutex);
515 if (o->mot_writecount < 0)
519 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
523 void mdt_write_put(struct mdt_object *o)
526 cfs_mutex_lock(&o->mot_ioepoch_mutex);
528 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
532 static int mdt_write_deny(struct mdt_object *o)
536 cfs_mutex_lock(&o->mot_ioepoch_mutex);
537 if (o->mot_writecount > 0)
541 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
545 static void mdt_write_allow(struct mdt_object *o)
548 cfs_mutex_lock(&o->mot_ioepoch_mutex);
550 cfs_mutex_unlock(&o->mot_ioepoch_mutex);
554 /* there can be no real transaction so prepare the fake one */
555 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
557 struct mdt_device *mdt = info->mti_mdt;
558 struct ptlrpc_request *req = mdt_info_req(info);
559 struct tg_export_data *ted;
560 struct lsd_client_data *lcd;
563 /* transaction has occurred already */
564 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
567 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
568 if (info->mti_transno == 0) {
569 info->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
571 /* should be replay */
572 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
573 mdt->mdt_lut.lut_last_transno = info->mti_transno;
575 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
577 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
579 req->rq_export->exp_obd->obd_last_committed);
581 req->rq_transno = info->mti_transno;
582 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
584 /* update lcd in memory only for resent cases */
585 ted = &req->rq_export->exp_target_data;
587 cfs_mutex_lock(&ted->ted_lcd_lock);
589 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
590 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
591 if (info->mti_transno != 0)
592 lcd->lcd_last_close_transno = info->mti_transno;
593 lcd->lcd_last_close_xid = req->rq_xid;
594 lcd->lcd_last_close_result = rc;
596 /* VBR: save versions in last_rcvd for reconstruct. */
597 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
599 lcd->lcd_pre_versions[0] = pre_versions[0];
600 lcd->lcd_pre_versions[1] = pre_versions[1];
601 lcd->lcd_pre_versions[2] = pre_versions[2];
602 lcd->lcd_pre_versions[3] = pre_versions[3];
604 if (info->mti_transno != 0)
605 lcd->lcd_last_transno = info->mti_transno;
606 lcd->lcd_last_xid = req->rq_xid;
607 lcd->lcd_last_result = rc;
608 lcd->lcd_last_data = info->mti_opdata;
610 cfs_mutex_unlock(&ted->ted_lcd_lock);
615 void mdt_mfd_set_mode(struct mdt_file_data *mfd, int mode)
617 LASSERT(mfd != NULL);
619 CDEBUG(D_HA, "Change mfd %p mode 0x%x->0x%x\n",
620 mfd, (unsigned int)mfd->mfd_mode, (unsigned int)mode);
622 mfd->mfd_mode = mode;
625 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
626 struct mdt_object *o, __u64 flags, int created)
628 struct ptlrpc_request *req = mdt_info_req(info);
629 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
630 struct mdt_file_data *mfd;
631 struct md_attr *ma = &info->mti_attr;
632 struct lu_attr *la = &ma->ma_attr;
633 struct mdt_body *repbody;
634 int rc = 0, isdir, isreg;
637 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
639 isreg = S_ISREG(la->la_mode);
640 isdir = S_ISDIR(la->la_mode);
641 if (isreg && !(ma->ma_valid & MA_LOV)) {
643 * No EA, check whether it is will set regEA and dirEA since in
644 * above attr get, these size might be zero, so reset it, to
645 * retrieve the MD after create obj.
647 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
650 /* in replay case, p == NULL */
651 rc = mdt_create_data(info, p, o);
656 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
657 ma->ma_valid, ma->ma_lmm_size);
659 if (ma->ma_valid & MA_LOV) {
660 LASSERT(ma->ma_lmm_size != 0);
661 repbody->eadatasize = ma->ma_lmm_size;
663 repbody->valid |= OBD_MD_FLDIREA;
665 repbody->valid |= OBD_MD_FLEASIZE;
668 if (flags & FMODE_WRITE) {
669 rc = mdt_write_get(o);
671 mdt_ioepoch_open(info, o, created);
672 repbody->ioepoch = o->mot_ioepoch;
674 } else if (flags & MDS_FMODE_EXEC) {
675 rc = mdt_write_deny(o);
680 rc = mo_open(info->mti_env, mdt_object_child(o),
681 created ? flags | MDS_OPEN_CREATED : flags);
688 * Keep a reference on this object for this open, and is
689 * released by mdt_mfd_close().
691 mdt_object_get(info->mti_env, o);
694 * @flags is always not zero. At least it should be FMODE_READ,
695 * FMODE_WRITE or MDS_FMODE_EXEC.
700 mdt_mfd_set_mode(mfd, flags);
703 mfd->mfd_xid = req->rq_xid;
706 if (req_is_replay(req)) {
707 struct mdt_file_data *old_mfd;
708 /* Check wheather old cookie already exist in
709 * the list, becasue when do recovery, client
710 * might be disconnected from server, and
711 * restart replay, so there maybe some orphan
712 * mfd here, we should remove them */
713 LASSERT(info->mti_rr.rr_handle != NULL);
714 old_mfd = mdt_handle2mfd(info, info->mti_rr.rr_handle);
716 CDEBUG(D_HA, "del orph mfd %p fid=("DFID") "
717 "cookie=" LPX64"\n", mfd,
718 PFID(mdt_object_fid(mfd->mfd_object)),
719 info->mti_rr.rr_handle->cookie);
720 cfs_spin_lock(&med->med_open_lock);
721 class_handle_unhash(&old_mfd->mfd_handle);
722 cfs_list_del_init(&old_mfd->mfd_list);
723 cfs_spin_unlock(&med->med_open_lock);
724 /* no attr update for that close */
726 ma->ma_valid |= MA_FLAGS;
727 ma->ma_attr_flags |= MDS_RECOV_OPEN;
728 mdt_mfd_close(info, old_mfd);
729 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
730 ma->ma_valid &= ~MA_FLAGS;
732 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
733 info->mti_rr.rr_handle->cookie);
734 mfd->mfd_old_handle.cookie =
735 info->mti_rr.rr_handle->cookie;
737 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
739 if (req->rq_export->exp_disconnected) {
740 cfs_spin_lock(&med->med_open_lock);
741 class_handle_unhash(&mfd->mfd_handle);
742 cfs_list_del_init(&mfd->mfd_list);
743 cfs_spin_unlock(&med->med_open_lock);
744 mdt_mfd_close(info, mfd);
746 cfs_spin_lock(&med->med_open_lock);
747 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
748 cfs_spin_unlock(&med->med_open_lock);
751 mdt_empty_transno(info, rc);
753 GOTO(err_out, rc = -ENOMEM);
759 if (flags & FMODE_WRITE)
760 /* XXX We also need to close io epoch here.
761 * See LU-1220 - green */
763 else if (flags & FMODE_EXEC)
768 int mdt_finish_open(struct mdt_thread_info *info,
769 struct mdt_object *p, struct mdt_object *o,
770 __u64 flags, int created, struct ldlm_reply *rep)
772 struct ptlrpc_request *req = mdt_info_req(info);
773 struct obd_export *exp = req->rq_export;
774 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
775 struct md_attr *ma = &info->mti_attr;
776 struct lu_attr *la = &ma->ma_attr;
777 struct mdt_file_data *mfd;
778 struct mdt_body *repbody;
780 int isreg, isdir, islnk;
784 LASSERT(ma->ma_valid & MA_INODE);
786 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
788 isreg = S_ISREG(la->la_mode);
789 isdir = S_ISDIR(la->la_mode);
790 islnk = S_ISLNK(la->la_mode);
791 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
793 if (exp_connect_rmtclient(exp)) {
794 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
796 rc = mdt_pack_remote_perm(info, o, buf);
798 repbody->valid &= ~OBD_MD_FLRMTPERM;
799 repbody->aclsize = 0;
801 repbody->valid |= OBD_MD_FLRMTPERM;
802 repbody->aclsize = sizeof(struct mdt_remote_perm);
805 #ifdef CONFIG_FS_POSIX_ACL
806 else if (exp->exp_connect_flags & OBD_CONNECT_ACL) {
807 const struct lu_env *env = info->mti_env;
808 struct md_object *next = mdt_object_child(o);
809 struct lu_buf *buf = &info->mti_buf;
811 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
812 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
814 if (buf->lb_len > 0) {
815 rc = mo_xattr_get(env, next, buf,
816 XATTR_NAME_ACL_ACCESS);
818 if (rc == -ENODATA) {
819 repbody->aclsize = 0;
820 repbody->valid |= OBD_MD_FLACL;
822 } else if (rc == -EOPNOTSUPP) {
825 CERROR("got acl size: %d\n", rc);
828 repbody->aclsize = rc;
829 repbody->valid |= OBD_MD_FLACL;
836 if (info->mti_mdt->mdt_opts.mo_mds_capa &&
837 exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
838 struct lustre_capa *capa;
840 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
842 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
843 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
846 repbody->valid |= OBD_MD_FLMDSCAPA;
848 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
849 exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
850 S_ISREG(lu_object_attr(&o->mot_obj.mo_lu))) {
851 struct lustre_capa *capa;
853 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
855 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
856 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
859 repbody->valid |= OBD_MD_FLOSSCAPA;
863 * If we are following a symlink, don't open; and do not return open
864 * handle for special nodes as client required.
866 if (islnk || (!isreg && !isdir &&
867 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH))) {
868 lustre_msg_set_transno(req->rq_repmsg, 0);
872 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
875 * We need to return the existing object's fid back, so it is done here,
876 * after preparing the reply.
878 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
881 /* This can't be done earlier, we need to return reply body */
883 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
884 /* We are trying to create or write an existing dir. */
887 } else if (flags & MDS_OPEN_DIRECTORY)
890 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
891 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
896 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
897 cfs_spin_lock(&med->med_open_lock);
898 cfs_list_for_each(t, &med->med_open_head) {
899 mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
900 if (mfd->mfd_xid == req->rq_xid) {
905 cfs_spin_unlock(&med->med_open_lock);
908 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
909 /*set repbody->ea_size for resent case*/
910 if (ma->ma_valid & MA_LOV) {
911 LASSERT(ma->ma_lmm_size != 0);
912 repbody->eadatasize = ma->ma_lmm_size;
914 repbody->valid |= OBD_MD_FLDIREA;
916 repbody->valid |= OBD_MD_FLEASIZE;
922 rc = mdt_mfd_open(info, p, o, flags, created);
926 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
927 struct lsd_client_data *lcd);
929 void mdt_reconstruct_open(struct mdt_thread_info *info,
930 struct mdt_lock_handle *lhc)
932 const struct lu_env *env = info->mti_env;
933 struct mdt_device *mdt = info->mti_mdt;
934 struct req_capsule *pill = info->mti_pill;
935 struct ptlrpc_request *req = mdt_info_req(info);
936 struct tg_export_data *ted = &req->rq_export->exp_target_data;
937 struct lsd_client_data *lcd = ted->ted_lcd;
938 struct md_attr *ma = &info->mti_attr;
939 struct mdt_reint_record *rr = &info->mti_rr;
940 __u32 flags = info->mti_spec.sp_cr_flags;
941 struct ldlm_reply *ldlm_rep;
942 struct mdt_object *parent;
943 struct mdt_object *child;
944 struct mdt_body *repbody;
948 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
949 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
950 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
952 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
953 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
955 ma->ma_need = MA_INODE;
956 if (ma->ma_lmm_size > 0)
957 ma->ma_need |= MA_LOV;
961 mdt_req_from_lcd(req, lcd);
962 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
964 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
965 ldlm_rep->lock_policy_res1, req->rq_status);
967 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
969 /* We did not create successfully, return error to client. */
970 GOTO(out, rc = req->rq_status);
972 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
973 struct obd_export *exp = req->rq_export;
975 * We failed after creation, but we do not know in which step
976 * we failed. So try to check the child object.
978 parent = mdt_object_find(env, mdt, rr->rr_fid1);
979 if (IS_ERR(parent)) {
980 rc = PTR_ERR(parent);
981 LCONSOLE_WARN("Parent "DFID" lookup error %d."
982 " Evicting client %s with export %s.\n",
983 PFID(rr->rr_fid1), rc,
984 obd_uuid2str(&exp->exp_client_uuid),
985 obd_export_nid2str(exp));
986 mdt_export_evict(exp);
989 child = mdt_object_find(env, mdt, rr->rr_fid2);
992 LCONSOLE_WARN("Child "DFID" lookup error %d."
993 " Evicting client %s with export %s.\n",
994 PFID(mdt_object_fid(child)), rc,
995 obd_uuid2str(&exp->exp_client_uuid),
996 obd_export_nid2str(exp));
997 mdt_object_put(env, parent);
998 mdt_export_evict(exp);
1001 rc = mdt_object_exists(child);
1003 struct md_object *next;
1005 mdt_set_capainfo(info, 1, rr->rr_fid2, BYPASS_CAPA);
1006 next = mdt_object_child(child);
1007 rc = mo_attr_get(env, next, ma);
1009 rc = mdt_finish_open(info, parent, child,
1010 flags, 1, ldlm_rep);
1011 } else if (rc < 0) {
1012 /* the child object was created on remote server */
1013 repbody->fid1 = *rr->rr_fid2;
1014 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1016 } else if (rc == 0) {
1017 /* the child does not exist, we should do regular open */
1018 mdt_object_put(env, parent);
1019 mdt_object_put(env, child);
1020 GOTO(regular_open, 0);
1022 mdt_object_put(env, parent);
1023 mdt_object_put(env, child);
1027 /* We did not try to create, so we are a pure open */
1028 rc = mdt_reint_open(info, lhc);
1033 req->rq_status = rc;
1034 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1035 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1038 int mdt_open_by_fid(struct mdt_thread_info* info,
1039 struct ldlm_reply *rep)
1041 const struct lu_env *env = info->mti_env;
1042 __u32 flags = info->mti_spec.sp_cr_flags;
1043 struct mdt_reint_record *rr = &info->mti_rr;
1044 struct md_attr *ma = &info->mti_attr;
1045 struct mdt_object *o;
1049 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1051 RETURN(rc = PTR_ERR(o));
1053 rc = mdt_object_exists(o);
1055 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1059 rc = mo_attr_get(env, mdt_object_child(o), ma);
1061 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1062 } else if (rc == 0) {
1065 /* the child object was created on remote server */
1066 struct mdt_body *repbody;
1067 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1068 repbody->fid1 = *rr->rr_fid2;
1069 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1073 mdt_object_put(info->mti_env, o);
1077 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1078 struct mdt_lock_handle *lhc)
1080 const struct lu_env *env = info->mti_env;
1081 struct mdt_device *mdt = info->mti_mdt;
1082 __u32 flags = info->mti_spec.sp_cr_flags;
1083 struct mdt_reint_record *rr = &info->mti_rr;
1084 struct md_attr *ma = &info->mti_attr;
1085 struct mdt_object *parent= NULL;
1086 struct mdt_object *o;
1091 if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1092 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1093 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1094 if (IS_ERR(parent)) {
1095 CDEBUG(D_INODE, "Fail to find parent "DFID
1096 " for anonymous created %ld, try to"
1097 " use server-side parent.\n",
1098 PFID(rr->rr_fid1), PTR_ERR(parent));
1103 ma->ma_need |= MA_PFID;
1106 o = mdt_object_find(env, mdt, rr->rr_fid2);
1108 RETURN(rc = PTR_ERR(o));
1110 rc = mdt_object_exists(o);
1112 mdt_set_disposition(info, rep, (DISP_LOOKUP_EXECD |
1114 GOTO(out, rc = -ENOENT);
1115 } else if (rc < 0) {
1116 CERROR("NFS remote open shouldn't happen.\n");
1119 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1123 if (flags & FMODE_WRITE)
1125 else if (flags & MDS_FMODE_EXEC)
1130 mdt_lock_handle_init(lhc);
1131 mdt_lock_reg_init(lhc, lm);
1132 rc = mdt_object_lock(info, o, lhc,
1133 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1138 rc = mo_attr_get(env, mdt_object_child(o), ma);
1142 if (ma->ma_valid & MA_PFID) {
1143 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1144 if (IS_ERR(parent)) {
1145 CDEBUG(D_INODE, "Fail to find parent "DFID
1146 " for anonymous created %ld, try to"
1147 " use system default.\n",
1148 PFID(&ma->ma_pfid), PTR_ERR(parent));
1153 if (flags & MDS_OPEN_LOCK)
1154 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1155 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1157 if (!(flags & MDS_OPEN_LOCK) || rc)
1158 mdt_object_unlock(info, o, lhc, 1);
1162 mdt_object_put(env, o);
1164 mdt_object_put(env, parent);
1168 int mdt_pin(struct mdt_thread_info* info)
1171 RETURN(err_serious(-EOPNOTSUPP));
1174 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1175 int mdt_cross_open(struct mdt_thread_info* info,
1176 const struct lu_fid *fid,
1177 struct ldlm_reply *rep, __u32 flags)
1179 struct md_attr *ma = &info->mti_attr;
1180 struct mdt_object *o;
1184 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1186 RETURN(rc = PTR_ERR(o));
1188 rc = mdt_object_exists(o);
1190 /* Do permission check for cross-open. */
1191 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o),
1192 NULL, flags | MDS_OPEN_CROSS);
1196 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1197 rc = mo_attr_get(info->mti_env, mdt_object_child(o), ma);
1199 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1200 } else if (rc == 0) {
1202 * Something is wrong here. lookup was positive but there is
1205 CERROR("Cross-ref object doesn't exist!\n");
1208 /* Something is wrong here, the object is on another MDS! */
1209 CERROR("The object isn't on this server! FLD error?\n");
1210 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1212 "Object isn't on this server! FLD error?\n");
1218 mdt_object_put(info->mti_env, o);
1222 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1224 struct mdt_device *mdt = info->mti_mdt;
1225 struct ptlrpc_request *req = mdt_info_req(info);
1226 struct mdt_object *parent;
1227 struct mdt_object *child;
1228 struct mdt_lock_handle *lh;
1229 struct ldlm_reply *ldlm_rep;
1230 struct mdt_body *repbody;
1231 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1232 struct md_attr *ma = &info->mti_attr;
1233 __u64 create_flags = info->mti_spec.sp_cr_flags;
1234 struct mdt_reint_record *rr = &info->mti_rr;
1235 struct lu_name *lname;
1241 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1242 (obd_timeout + 1) / 4);
1244 mdt_counter_incr(req, LPROC_MDT_OPEN);
1245 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1247 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
1248 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill, &RMF_MDT_MD,
1250 ma->ma_need = MA_INODE;
1251 if (ma->ma_lmm_size > 0)
1252 ma->ma_need |= MA_LOV;
1256 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1257 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1259 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1260 CERROR("file join is not supported anymore.\n");
1261 GOTO(out, result = err_serious(-EOPNOTSUPP));
1263 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1265 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1266 info->mti_spec.u.sp_ea.eadata == NULL)
1267 GOTO(out, result = err_serious(-EINVAL));
1269 CDEBUG(D_INODE, "I am going to open "DFID"/(%s->"DFID") "
1270 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1271 PFID(rr->rr_fid1), rr->rr_name,
1272 PFID(rr->rr_fid2), create_flags,
1273 ma->ma_attr.la_mode, msg_flags);
1275 if (req_is_replay(req) ||
1276 (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1277 /* This is a replay request or from liblustre with ea. */
1278 result = mdt_open_by_fid(info, ldlm_rep);
1280 if (result != -ENOENT) {
1281 if (req->rq_export->exp_libclient &&
1282 create_flags & MDS_OPEN_HAS_EA)
1283 GOTO(out, result = 0);
1286 /* We didn't find the correct object, so we need to re-create it
1287 * via a regular replay. */
1288 if (!(create_flags & MDS_OPEN_CREAT)) {
1289 DEBUG_REQ(D_ERROR, req,
1290 "OPEN & CREAT not in open replay/by_fid.");
1291 GOTO(out, result = -EFAULT);
1293 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1294 } else if ((rr->rr_namelen == 0 && !info->mti_cross_ref &&
1295 create_flags & MDS_OPEN_LOCK) ||
1296 (create_flags & MDS_OPEN_BY_FID)) {
1297 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1298 if (result != -ENOENT && !(create_flags & MDS_OPEN_CREAT))
1300 if (unlikely(rr->rr_namelen == 0))
1301 GOTO(out, result = -EINVAL);
1302 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1305 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1306 GOTO(out, result = err_serious(-ENOMEM));
1308 mdt_set_disposition(info, ldlm_rep,
1309 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1311 if (info->mti_cross_ref) {
1312 /* This is cross-ref open */
1313 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1314 result = mdt_cross_open(info, rr->rr_fid1, ldlm_rep,
1319 lh = &info->mti_lh[MDT_LH_PARENT];
1320 mdt_lock_pdo_init(lh, (create_flags & MDS_OPEN_CREAT) ?
1321 LCK_PW : LCK_PR, rr->rr_name, rr->rr_namelen);
1323 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1324 MDS_INODELOCK_UPDATE);
1326 GOTO(out, result = PTR_ERR(parent));
1328 /* get and check version of parent */
1329 result = mdt_version_get_check(info, parent, 0);
1331 GOTO(out_parent, result);
1333 fid_zero(child_fid);
1335 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1336 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1337 lname, child_fid, &info->mti_spec);
1338 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1339 "looking for "DFID"/%s, result fid="DFID"\n",
1340 PFID(mdt_object_fid(parent)), rr->rr_name, PFID(child_fid));
1342 if (result != 0 && result != -ENOENT && result != -ESTALE)
1343 GOTO(out_parent, result);
1345 if (result == -ENOENT || result == -ESTALE) {
1346 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1347 if (result == -ESTALE) {
1349 * -ESTALE means the parent is a dead(unlinked) dir, so
1350 * it should return -ENOENT to in accordance with the
1351 * original mds implementaion.
1353 GOTO(out_parent, result = -ENOENT);
1355 if (!(create_flags & MDS_OPEN_CREAT))
1356 GOTO(out_parent, result);
1357 *child_fid = *info->mti_rr.rr_fid2;
1358 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1360 child = mdt_object_new(info->mti_env, mdt, child_fid);
1363 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1364 * return FID back in that case.
1366 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1367 child = mdt_object_find(info->mti_env, mdt, child_fid);
1370 GOTO(out_parent, result = PTR_ERR(child));
1372 /** check version of child */
1373 rc = mdt_version_get_check(info, child, 1);
1375 GOTO(out_child, result = rc);
1377 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1378 if (result == -ENOENT) {
1379 if (mdt_object_obf(parent))
1380 GOTO(out_child, result = -EPERM);
1382 /* save versions in reply */
1383 mdt_version_get_save(info, parent, 0);
1384 mdt_version_get_save(info, child, 1);
1386 /* version of child will be changed */
1387 info->mti_mos = child;
1389 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1390 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1392 /* Let lower layers know what is lock mode on directory. */
1393 info->mti_spec.sp_cr_mode =
1394 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1397 * Do not perform lookup sanity check. We know that name does
1400 info->mti_spec.sp_cr_lookup = 0;
1401 info->mti_spec.sp_feat = &dt_directory_features;
1403 result = mdo_create(info->mti_env,
1404 mdt_object_child(parent),
1406 mdt_object_child(child),
1409 if (result == -ERESTART) {
1410 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1411 GOTO(out_child, result);
1414 GOTO(out_child, result);
1418 /* We have to get attr & lov ea for this object */
1419 result = mo_attr_get(info->mti_env, mdt_object_child(child),
1422 * The object is on remote node, return its FID for remote open.
1424 if (result == -EREMOTE) {
1426 * Check if this lock already was sent to client and
1427 * this is resent case. For resent case do not take lock
1428 * again, use what is already granted.
1430 LASSERT(lhc != NULL);
1432 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1433 struct ldlm_lock *lock;
1435 LASSERT(msg_flags & MSG_RESENT);
1437 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1439 CERROR("Invalid lock handle "LPX64"\n",
1440 lhc->mlh_reg_lh.cookie);
1443 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1444 &lock->l_resource->lr_name));
1445 LDLM_LOCK_PUT(lock);
1448 mdt_lock_handle_init(lhc);
1449 mdt_lock_reg_init(lhc, LCK_PR);
1451 rc = mdt_object_lock(info, child, lhc,
1452 MDS_INODELOCK_LOOKUP,
1455 repbody->fid1 = *mdt_object_fid(child);
1456 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1459 GOTO(out_child, result);
1463 LASSERT(!lustre_handle_is_used(&lhc->mlh_reg_lh));
1465 /* get openlock if this is not replay and if a client requested it */
1466 if (!req_is_replay(req) && create_flags & MDS_OPEN_LOCK) {
1469 if (create_flags & FMODE_WRITE)
1471 else if (create_flags & MDS_FMODE_EXEC)
1475 mdt_lock_handle_init(lhc);
1476 mdt_lock_reg_init(lhc, lm);
1477 rc = mdt_object_lock(info, child, lhc,
1478 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN,
1482 GOTO(out_child, result);
1485 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1489 /* Try to open it now. */
1490 rc = mdt_finish_open(info, parent, child, create_flags,
1494 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1495 /* openlock was acquired and mdt_finish_open failed -
1496 drop the openlock */
1497 mdt_object_unlock(info, child, lhc, 1);
1501 ma->ma_cookie_size = 0;
1502 info->mti_no_need_trans = 1;
1503 rc = mdo_unlink(info->mti_env,
1504 mdt_object_child(parent),
1505 mdt_object_child(child),
1509 CERROR("Error in cleanup of open\n");
1514 mdt_object_put(info->mti_env, child);
1516 mdt_object_unlock_put(info, parent, lh, result || !created);
1518 if (result && result != -EREMOTE)
1519 lustre_msg_set_transno(req->rq_repmsg, 0);
1523 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
1524 MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
1526 static int mdt_mfd_closed(struct mdt_file_data *mfd)
1528 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
1531 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
1533 struct mdt_object *o = mfd->mfd_object;
1534 struct md_object *next = mdt_object_child(o);
1535 struct md_attr *ma = &info->mti_attr;
1536 int ret = MDT_IOEPOCH_CLOSED;
1541 mode = mfd->mfd_mode;
1543 if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
1545 ret = mdt_ioepoch_close(info, o);
1546 } else if (mode & MDS_FMODE_EXEC) {
1548 } else if (mode & MDS_FMODE_EPOCH) {
1549 ret = mdt_ioepoch_close(info, o);
1550 } else if (mode & MDS_FMODE_SOM) {
1551 ret = mdt_som_au_close(info, o);
1554 /* Update atime on close only. */
1555 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
1556 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
1557 /* Set the atime only. */
1558 ma->ma_valid = MA_INODE;
1559 ma->ma_attr.la_valid = LA_ATIME;
1560 rc = mo_attr_set(info->mti_env, next, ma);
1563 ma->ma_need |= MA_INODE;
1564 ma->ma_valid &= ~MA_INODE;
1566 if (!MFD_CLOSED(mode))
1567 rc = mo_close(info->mti_env, next, ma, mode);
1569 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
1570 struct mdt_export_data *med;
1572 /* The IOepoch is still opened or SOM update is needed.
1573 * Put mfd back into the list. */
1574 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
1575 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
1576 MDS_FMODE_EPOCH : MDS_FMODE_SOM);
1578 LASSERT(mdt_info_req(info));
1579 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
1580 cfs_spin_lock(&med->med_open_lock);
1581 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
1582 class_handle_hash_back(&mfd->mfd_handle);
1583 cfs_spin_unlock(&med->med_open_lock);
1585 if (ret == MDT_IOEPOCH_OPENED) {
1589 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
1590 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
1594 mdt_object_put(info->mti_env, o);
1597 RETURN(rc ? rc : ret);
1600 int mdt_close(struct mdt_thread_info *info)
1602 struct mdt_export_data *med;
1603 struct mdt_file_data *mfd;
1604 struct mdt_object *o;
1605 struct md_attr *ma = &info->mti_attr;
1606 struct mdt_body *repbody = NULL;
1607 struct ptlrpc_request *req = mdt_info_req(info);
1611 mdt_counter_incr(req, LPROC_MDT_CLOSE);
1612 /* Close may come with the Size-on-MDS update. Unpack it. */
1613 rc = mdt_close_unpack(info);
1615 RETURN(err_serious(rc));
1617 LASSERT(info->mti_ioepoch);
1619 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
1620 info->mti_mdt->mdt_max_mdsize);
1621 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
1622 info->mti_mdt->mdt_max_cookiesize);
1623 rc = req_capsule_server_pack(info->mti_pill);
1624 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
1625 mdt_client_compatibility(info);
1627 mdt_fix_reply(info);
1628 RETURN(lustre_msg_get_status(req->rq_repmsg));
1631 /* Continue to close handle even if we can not pack reply */
1633 repbody = req_capsule_server_get(info->mti_pill,
1635 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
1637 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
1640 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
1642 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
1645 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
1646 repbody->eadatasize = 0;
1647 repbody->aclsize = 0;
1649 rc = err_serious(rc);
1652 med = &req->rq_export->exp_mdt_data;
1653 cfs_spin_lock(&med->med_open_lock);
1654 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1655 if (mdt_mfd_closed(mfd)) {
1656 cfs_spin_unlock(&med->med_open_lock);
1657 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
1658 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
1659 info->mti_ioepoch->handle.cookie);
1660 /** not serious error since bug 3633 */
1663 class_handle_unhash(&mfd->mfd_handle);
1664 cfs_list_del_init(&mfd->mfd_list);
1665 cfs_spin_unlock(&med->med_open_lock);
1667 /* Do not lose object before last unlink. */
1668 o = mfd->mfd_object;
1669 mdt_object_get(info->mti_env, o);
1670 ret = mdt_mfd_close(info, mfd);
1671 if (repbody != NULL)
1672 rc = mdt_handle_last_unlink(info, o, ma);
1673 mdt_empty_transno(info, rc);
1674 mdt_object_put(info->mti_env, o);
1676 if (repbody != NULL) {
1677 mdt_client_compatibility(info);
1678 rc = mdt_fix_reply(info);
1681 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
1682 RETURN(err_serious(-ENOMEM));
1684 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
1685 OBD_FAIL_MDS_CLOSE_NET_REP))
1686 info->mti_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
1687 RETURN(rc ? rc : ret);
1691 * DONE_WRITING rpc handler.
1693 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
1694 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
1695 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
1696 * skip attributes and reconstruct the reply here.
1698 int mdt_done_writing(struct mdt_thread_info *info)
1700 struct ptlrpc_request *req = mdt_info_req(info);
1701 struct mdt_body *repbody = NULL;
1702 struct mdt_export_data *med;
1703 struct mdt_file_data *mfd;
1707 rc = req_capsule_server_pack(info->mti_pill);
1709 RETURN(err_serious(rc));
1711 repbody = req_capsule_server_get(info->mti_pill,
1713 repbody->eadatasize = 0;
1714 repbody->aclsize = 0;
1716 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
1717 rc = mdt_close_unpack(info);
1719 RETURN(err_serious(rc));
1721 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL))
1722 RETURN(lustre_msg_get_status(req->rq_repmsg));
1724 med = &info->mti_exp->exp_mdt_data;
1725 cfs_spin_lock(&med->med_open_lock);
1726 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
1728 cfs_spin_unlock(&med->med_open_lock);
1729 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
1730 ": cookie = "LPX64" ioepoch = "LPU64"\n",
1731 PFID(info->mti_rr.rr_fid1),
1732 info->mti_ioepoch->handle.cookie,
1733 info->mti_ioepoch->ioepoch);
1734 /* If this is a replay, reconstruct the transno. */
1735 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1736 rc = info->mti_ioepoch->flags & MF_SOM_AU ?
1738 mdt_empty_transno(info, rc);
1744 LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
1745 mfd->mfd_mode == MDS_FMODE_TRUNC);
1746 class_handle_unhash(&mfd->mfd_handle);
1747 cfs_list_del_init(&mfd->mfd_list);
1748 cfs_spin_unlock(&med->med_open_lock);
1750 /* Set EPOCH CLOSE flag if not set by client. */
1751 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
1752 info->mti_attr.ma_valid = 0;
1754 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
1755 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1756 if (info->mti_attr.ma_lmm == NULL)
1759 rc = mdt_mfd_close(info, mfd);
1761 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
1762 mdt_empty_transno(info, rc);