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, 2014, Intel Corporation.
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"
48 #include <lustre_nodemap.h>
50 /* we do nothing because we do not have refcount now */
51 static void mdt_mfd_get(void *mfdp)
55 static struct portals_handle_ops mfd_handle_ops = {
56 .hop_addref = mdt_mfd_get,
60 /* Create a new mdt_file_data struct, initialize it,
61 * and insert it to global hash table */
62 struct mdt_file_data *mdt_mfd_new(const struct mdt_export_data *med)
64 struct mdt_file_data *mfd;
69 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
70 mfd->mfd_handle.h_owner = med;
71 INIT_LIST_HEAD(&mfd->mfd_list);
72 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
79 * Find the mfd pointed to by handle in global hash table.
80 * In case of replay the handle is obsoleted
81 * but mfd can be found in mfd list by that handle.
82 * Callers need to be holding med_open_lock.
84 struct mdt_file_data *mdt_handle2mfd(struct mdt_export_data *med,
85 const struct lustre_handle *handle,
86 bool is_replay_or_resent)
88 struct mdt_file_data *mfd;
91 LASSERT(handle != NULL);
92 mfd = class_handle2object(handle->cookie, med);
93 /* during dw/setattr replay the mfd can be found by old handle */
94 if (mfd == NULL && is_replay_or_resent) {
95 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
96 if (mfd->mfd_old_handle.cookie == handle->cookie)
106 void mdt_mfd_free(struct mdt_file_data *mfd)
108 LASSERT(list_empty(&mfd->mfd_list));
109 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
112 static int mdt_create_data(struct mdt_thread_info *info,
113 struct mdt_object *p, struct mdt_object *o)
115 struct md_op_spec *spec = &info->mti_spec;
116 struct md_attr *ma = &info->mti_attr;
120 if (!md_should_create(spec->sp_cr_flags))
123 ma->ma_need = MA_INODE | MA_LOV;
125 mutex_lock(&o->mot_lov_mutex);
126 if (!(o->mot_flags & MOF_LOV_CREATED)) {
127 rc = mdo_create_data(info->mti_env,
128 p ? mdt_object_child(p) : NULL,
129 mdt_object_child(o), spec, ma);
131 rc = mdt_attr_get_complex(info, o, ma);
133 if (rc == 0 && ma->ma_valid & MA_LOV)
134 o->mot_flags |= MOF_LOV_CREATED;
137 mutex_unlock(&o->mot_lov_mutex);
141 static int mdt_ioepoch_opened(struct mdt_object *mo)
143 return mo->mot_ioepoch_count;
146 int mdt_object_is_som_enabled(struct mdt_object *mo)
148 return !mo->mot_ioepoch;
152 * Re-enable Size-on-MDS.
153 * Call under ->mot_ioepoch_mutex.
155 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
157 if (ioepoch == mo->mot_ioepoch) {
158 LASSERT(!mdt_ioepoch_opened(mo));
165 * Open the IOEpoch. It is allowed if @writecount is not negative.
166 * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
168 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
171 struct mdt_device *mdt = info->mti_mdt;
176 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
177 !S_ISREG(lu_object_attr(&o->mot_obj)))
180 mutex_lock(&o->mot_ioepoch_mutex);
181 if (mdt_ioepoch_opened(o)) {
182 /* Epoch continues even if there is no writers yet. */
183 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
184 o->mot_ioepoch, PFID(mdt_object_fid(o)));
186 /* XXX: ->mdt_ioepoch is not initialized at the mount */
187 spin_lock(&mdt->mdt_ioepoch_lock);
188 if (mdt->mdt_ioepoch < info->mti_replayepoch)
189 mdt->mdt_ioepoch = info->mti_replayepoch;
191 if (info->mti_replayepoch)
192 o->mot_ioepoch = info->mti_replayepoch;
193 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
194 o->mot_ioepoch = ++mdt->mdt_ioepoch;
196 o->mot_ioepoch = mdt->mdt_ioepoch;
198 spin_unlock(&mdt->mdt_ioepoch_lock);
200 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
201 o->mot_ioepoch, PFID(mdt_object_fid(o)));
203 o->mot_flags |= MOF_SOM_CREATED;
206 o->mot_ioepoch_count++;
207 mutex_unlock(&o->mot_ioepoch_mutex);
209 /* Cancel Size-on-MDS attributes cached on clients for the open case.
210 * In the truncate case, see mdt_reint_setattr(). */
211 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
212 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
213 mdt_lock_reg_init(lh, LCK_EX);
214 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
217 mdt_object_unlock(info, o, lh, 1);
223 * Update SOM on-disk attributes.
224 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
225 * mountid and attributes. If disabling, clean SOM xattr.
226 * Call under ->mot_ioepoch_mutex.
228 static int mdt_som_attr_set(struct mdt_thread_info *info,
229 struct mdt_object *obj, __u64 ioepoch, bool enable)
231 struct md_object *next = mdt_object_child(obj);
235 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
236 " on "DFID".\n", enable ? "update" : "disabling",
237 ioepoch, PFID(mdt_object_fid(obj)));
240 struct lu_buf *buf = &info->mti_buf;
241 struct som_attrs *attrs;
242 struct md_attr *ma = &info->mti_attr;
243 struct lu_attr *la = &ma->ma_attr;
244 struct obd_device *obd = info->mti_mdt->mdt_lut.lut_obd;
246 attrs = (struct som_attrs *)info->mti_xattr_buf;
247 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
249 /* pack SOM attributes */
250 memset(attrs, 0, sizeof(*attrs));
251 attrs->som_ioepoch = ioepoch;
252 attrs->som_mountid = obd->u.obt.obt_mount_count;
253 if ((la->la_valid & LA_SIZE) != 0)
254 attrs->som_size = la->la_size;
255 if ((la->la_valid & LA_BLOCKS) != 0)
256 attrs->som_blocks = la->la_blocks;
257 lustre_som_swab(attrs);
259 /* update SOM attributes */
261 buf->lb_len = sizeof(*attrs);
262 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_SOM, 0);
264 /* delete SOM attributes */
265 rc = mo_xattr_del(info->mti_env, next, XATTR_NAME_SOM);
271 /** Perform the eviction specific actions on ioepoch close. */
272 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
273 struct mdt_object *o)
277 mutex_lock(&o->mot_ioepoch_mutex);
278 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
279 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
280 o->mot_ioepoch_count);
281 o->mot_ioepoch_count--;
283 /* If eviction occured set MOF_SOM_RECOV,
284 * if no other epoch holders, disable SOM on disk. */
285 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
286 if (!mdt_ioepoch_opened(o)) {
287 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
288 mdt_object_som_enable(o, o->mot_ioepoch);
290 mutex_unlock(&o->mot_ioepoch_mutex);
295 * Perform the replay specific actions on ioepoch close.
296 * Skip SOM attribute update if obtained and just forget about the inode state
297 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
299 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
300 struct mdt_object *o)
302 int rc = MDT_IOEPOCH_CLOSED;
305 mutex_lock(&o->mot_ioepoch_mutex);
306 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
307 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
308 o->mot_ioepoch_count--;
310 /* Get an info from the replayed request if client is supposed
311 * to send an Attibute Update, reconstruct @rc if so */
312 if (info->mti_ioepoch->flags & MF_SOM_AU)
313 rc = MDT_IOEPOCH_GETATTR;
315 if (!mdt_ioepoch_opened(o))
316 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
317 mutex_unlock(&o->mot_ioepoch_mutex);
323 * Regular file IOepoch close.
324 * Closes the ioepoch, checks the object state, apply obtained attributes and
325 * re-enable SOM on the object, if possible. Also checks if the recovery is
326 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
327 * to obtain SOM attributes under the server-side OST locks.
330 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
331 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
333 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
334 struct mdt_object *o)
336 struct md_attr *tmp_ma;
340 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
343 la = &info->mti_attr.ma_attr;
344 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
346 mutex_lock(&o->mot_ioepoch_mutex);
347 o->mot_ioepoch_count--;
349 tmp_ma = &info->mti_u.som.attr;
350 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
351 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
352 tmp_ma->ma_som = &info->mti_u.som.data;
353 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
354 tmp_ma->ma_valid = 0;
355 rc = mdt_attr_get_complex(info, o, tmp_ma);
359 /* Check the on-disk SOM state. */
360 if (o->mot_flags & MOF_SOM_RECOV)
362 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
363 !(tmp_ma->ma_valid & MA_SOM))
366 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
367 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
369 opened = mdt_ioepoch_opened(o);
371 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
372 * Skip the check for file with no LOV or for unlink files.
374 if (!opened && tmp_ma->ma_valid & MA_LOV &&
375 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
377 /* If some previous writer was evicted, re-ask the
378 * client for attributes. Even if attributes are
379 * provided, we cannot believe in them.
380 * Another use case is that there is no SOM cache on
381 * disk -- first access with SOM or there was an MDS
383 ret = MDT_IOEPOCH_GETATTR;
384 else if (o->mot_flags & MOF_SOM_CHANGE)
385 /* Some previous writer changed the attribute.
386 * Do not believe to the current Size-on-MDS
387 * update, re-ask client. */
388 ret = MDT_IOEPOCH_GETATTR;
389 else if (!(la->la_valid & LA_SIZE) && achange)
390 /* Attributes were changed by the last writer
391 * only but no Size-on-MDS update is received.*/
392 ret = MDT_IOEPOCH_GETATTR;
395 if (achange || ret == MDT_IOEPOCH_GETATTR)
396 o->mot_flags |= MOF_SOM_CHANGE;
398 /* If epoch ends and relable SOM attributes are obtained, update them.
399 * Create SOM ea for new files even if there is no attributes obtained
400 * (0-length file). */
401 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
402 if (achange || o->mot_flags & MOF_SOM_CREATED) {
403 LASSERT(achange || !(la->la_valid & LA_SIZE));
404 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
406 /* Avoid the following setattrs of these attributes,
407 * e.g. for atime update. */
408 info->mti_attr.ma_valid = 0;
410 mdt_object_som_enable(o, o->mot_ioepoch);
413 mutex_unlock(&o->mot_ioepoch_mutex);
414 /* If recovery is needed, tell the client to perform GETATTR under
416 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
417 struct mdt_body *rep;
418 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
419 rep->mbo_valid |= OBD_MD_FLGETATTRLOCK;
425 mutex_unlock(&o->mot_ioepoch_mutex);
430 * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
431 * - a client closes the IOEpoch;
432 * - a client eviction occured.
434 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
435 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
436 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
439 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
441 struct ptlrpc_request *req = mdt_info_req(info);
444 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
445 !S_ISREG(lu_object_attr(&o->mot_obj)))
448 LASSERT(o->mot_ioepoch_count);
449 LASSERT(info->mti_ioepoch == NULL ||
450 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
452 /* IOEpoch is closed only if client tells about it or eviction occures.
453 * In the replay case, always close the epoch. */
455 RETURN(mdt_ioepoch_close_on_eviction(info, o));
456 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
457 RETURN(mdt_ioepoch_close_on_replay(info, o));
458 if (info->mti_ioepoch && (info->mti_ioepoch->flags & MF_EPOCH_CLOSE))
459 RETURN(mdt_ioepoch_close_reg(info, o));
460 /* IO epoch is not closed. */
461 RETURN(MDT_IOEPOCH_OPENED);
465 * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
466 * for attribute update. It happens if:
467 * - SOM Attribute Update is obtained;
468 * - the client failed to obtain it and informs MDS about it;
469 * - a client eviction occured.
470 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
473 static int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
475 struct ptlrpc_request *req = mdt_info_req(info);
477 int act = MDT_SOM_ENABLE;
481 LASSERT(!req || info->mti_ioepoch);
482 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
483 !S_ISREG(lu_object_attr(&o->mot_obj)))
486 /* No size whereas MF_SOM_CHANGE is set means client failed to
487 * obtain ost attributes, drop the SOM cache on disk if so. */
489 (info->mti_ioepoch &&
490 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
491 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
492 act = MDT_SOM_DISABLE;
494 mutex_lock(&o->mot_ioepoch_mutex);
495 /* Mark the object it is the recovery state if we failed to obtain
497 if (act == MDT_SOM_DISABLE)
498 o->mot_flags |= MOF_SOM_RECOV;
500 if (!mdt_ioepoch_opened(o)) {
501 ioepoch = info->mti_ioepoch ?
502 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
505 && !(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
506 rc = mdt_som_attr_set(info, o, ioepoch, act);
507 mdt_object_som_enable(o, ioepoch);
509 mutex_unlock(&o->mot_ioepoch_mutex);
513 int mdt_write_read(struct mdt_object *o)
517 mutex_lock(&o->mot_ioepoch_mutex);
518 rc = o->mot_writecount;
519 mutex_unlock(&o->mot_ioepoch_mutex);
523 int mdt_write_get(struct mdt_object *o)
527 mutex_lock(&o->mot_ioepoch_mutex);
528 if (o->mot_writecount < 0)
532 mutex_unlock(&o->mot_ioepoch_mutex);
536 void mdt_write_put(struct mdt_object *o)
539 mutex_lock(&o->mot_ioepoch_mutex);
541 mutex_unlock(&o->mot_ioepoch_mutex);
545 static int mdt_write_deny(struct mdt_object *o)
549 mutex_lock(&o->mot_ioepoch_mutex);
550 if (o->mot_writecount > 0)
554 mutex_unlock(&o->mot_ioepoch_mutex);
558 static void mdt_write_allow(struct mdt_object *o)
561 mutex_lock(&o->mot_ioepoch_mutex);
563 mutex_unlock(&o->mot_ioepoch_mutex);
567 /* there can be no real transaction so prepare the fake one */
568 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
570 struct mdt_device *mdt = info->mti_mdt;
571 struct ptlrpc_request *req = mdt_info_req(info);
572 struct tg_export_data *ted;
573 struct lsd_client_data *lcd;
576 /* transaction has occurred already */
577 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
580 spin_lock(&mdt->mdt_lut.lut_translock);
582 if (info->mti_transno != 0) {
583 struct obd_export *exp = req->rq_export;
585 CERROR("%s: replay trans "LPU64" NID %s: rc = %d\n",
586 mdt_obd_name(mdt), info->mti_transno,
587 libcfs_nid2str(exp->exp_connection->c_peer.nid),
589 spin_unlock(&mdt->mdt_lut.lut_translock);
592 } else if (info->mti_transno == 0) {
593 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
595 /* should be replay */
596 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
597 mdt->mdt_lut.lut_last_transno = info->mti_transno;
599 spin_unlock(&mdt->mdt_lut.lut_translock);
601 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
603 req->rq_export->exp_obd->obd_last_committed);
605 req->rq_transno = info->mti_transno;
606 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
608 /* update lcd in memory only for resent cases */
609 ted = &req->rq_export->exp_target_data;
611 mutex_lock(&ted->ted_lcd_lock);
613 if (info->mti_transno < lcd->lcd_last_transno &&
614 info->mti_transno != 0) {
615 /* This should happen during replay. Do not update
616 * last rcvd info if replay req transno < last transno,
617 * otherwise the following resend(after replay) can not
618 * be checked correctly by xid */
619 mutex_unlock(&ted->ted_lcd_lock);
620 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
621 mdt_obd_name(mdt), info->mti_transno,
622 lcd->lcd_last_transno);
626 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
627 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
628 if (info->mti_transno != 0)
629 lcd->lcd_last_close_transno = info->mti_transno;
630 lcd->lcd_last_close_xid = req->rq_xid;
631 lcd->lcd_last_close_result = rc;
633 /* VBR: save versions in last_rcvd for reconstruct. */
634 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
636 lcd->lcd_pre_versions[0] = pre_versions[0];
637 lcd->lcd_pre_versions[1] = pre_versions[1];
638 lcd->lcd_pre_versions[2] = pre_versions[2];
639 lcd->lcd_pre_versions[3] = pre_versions[3];
641 if (info->mti_transno != 0)
642 lcd->lcd_last_transno = info->mti_transno;
644 lcd->lcd_last_xid = req->rq_xid;
645 lcd->lcd_last_result = rc;
646 lcd->lcd_last_data = info->mti_opdata;
648 mutex_unlock(&ted->ted_lcd_lock);
653 void mdt_mfd_set_mode(struct mdt_file_data *mfd, __u64 mode)
655 LASSERT(mfd != NULL);
657 CDEBUG(D_HA, DFID " Change mfd mode "LPO64" -> "LPO64".\n",
658 PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_mode, mode);
660 mfd->mfd_mode = mode;
664 * prep ma_lmm/ma_lmv for md_attr from reply
666 static void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
667 struct mdt_object *obj,
670 LASSERT(ma->ma_lmv == NULL && ma->ma_lmm == NULL);
671 if (S_ISDIR(obj->mot_header.loh_attr)) {
672 ma->ma_lmv = req_capsule_server_get(info->mti_pill,
674 ma->ma_lmv_size = req_capsule_get_size(info->mti_pill,
677 if (ma->ma_lmv_size > 0)
678 ma->ma_need |= MA_LMV;
680 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
682 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
685 if (ma->ma_lmm_size > 0)
686 ma->ma_need |= MA_LOV;
690 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
691 struct mdt_object *o, __u64 flags, int created,
692 struct ldlm_reply *rep)
694 struct ptlrpc_request *req = mdt_info_req(info);
695 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
696 struct mdt_file_data *mfd;
697 struct md_attr *ma = &info->mti_attr;
698 struct lu_attr *la = &ma->ma_attr;
699 struct mdt_body *repbody;
700 int rc = 0, isdir, isreg;
703 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
705 isreg = S_ISREG(la->la_mode);
706 isdir = S_ISDIR(la->la_mode);
707 if (isreg && !(ma->ma_valid & MA_LOV) && !(flags & MDS_OPEN_RELEASE)) {
709 * No EA, check whether it is will set regEA and dirEA since in
710 * above attr get, these size might be zero, so reset it, to
711 * retrieve the MD after create obj.
713 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
716 /* in replay case, p == NULL */
717 rc = mdt_create_data(info, p, o);
721 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
722 mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
725 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
726 ma->ma_valid, ma->ma_lmm_size);
728 if (ma->ma_valid & MA_LOV) {
729 LASSERT(ma->ma_lmm_size != 0);
730 repbody->mbo_eadatasize = ma->ma_lmm_size;
732 repbody->mbo_valid |= OBD_MD_FLDIREA;
734 repbody->mbo_valid |= OBD_MD_FLEASIZE;
737 if (ma->ma_valid & MA_LMV) {
738 LASSERT(ma->ma_lmv_size != 0);
739 repbody->mbo_eadatasize = ma->ma_lmv_size;
741 repbody->mbo_valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
744 if (flags & FMODE_WRITE) {
745 rc = mdt_write_get(o);
747 mdt_ioepoch_open(info, o, created);
748 repbody->mbo_ioepoch = o->mot_ioepoch;
750 } else if (flags & MDS_FMODE_EXEC) {
751 rc = mdt_write_deny(o);
756 rc = mo_open(info->mti_env, mdt_object_child(o),
757 created ? flags | MDS_OPEN_CREATED : flags);
759 /* If we allow the client to chgrp (CFS_SETGRP_PERM), but the
760 * client does not know which suppgid should be sent to the MDS,
761 * or some other(s) changed the target file's GID after this RPC
762 * sent to the MDS with the suppgid as the original GID, then we
763 * give the client another chance to send the right suppgid. */
765 allow_client_chgrp(info, lu_ucred(info->mti_env)))
766 mdt_set_disposition(info, rep, DISP_OPEN_DENY);
771 mfd = mdt_mfd_new(med);
773 GOTO(err_out, rc = -ENOMEM);
776 * Keep a reference on this object for this open, and is
777 * released by mdt_mfd_close().
779 mdt_object_get(info->mti_env, o);
781 mfd->mfd_xid = req->rq_xid;
784 * @flags is always not zero. At least it should be FMODE_READ,
785 * FMODE_WRITE or MDS_FMODE_EXEC.
790 mdt_mfd_set_mode(mfd, flags);
792 atomic_inc(&o->mot_open_count);
793 if (flags & MDS_OPEN_LEASE)
794 atomic_inc(&o->mot_lease_count);
797 if (req_is_replay(req)) {
798 struct mdt_file_data *old_mfd;
799 /* Check wheather old cookie already exist in
800 * the list, becasue when do recovery, client
801 * might be disconnected from server, and
802 * restart replay, so there maybe some orphan
803 * mfd here, we should remove them */
804 LASSERT(info->mti_rr.rr_handle != NULL);
805 spin_lock(&med->med_open_lock);
806 old_mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle, true);
807 if (old_mfd != NULL) {
808 CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
809 "cookie = "LPX64"\n", mfd,
810 PFID(mdt_object_fid(mfd->mfd_object)),
811 info->mti_rr.rr_handle->cookie);
812 class_handle_unhash(&old_mfd->mfd_handle);
813 list_del_init(&old_mfd->mfd_list);
814 spin_unlock(&med->med_open_lock);
815 /* no attr update for that close */
817 ma->ma_valid |= MA_FLAGS;
818 ma->ma_attr_flags |= MDS_RECOV_OPEN;
819 mdt_mfd_close(info, old_mfd);
820 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
821 ma->ma_valid &= ~MA_FLAGS;
823 spin_unlock(&med->med_open_lock);
824 CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
825 "cookie = "LPX64"\n",
826 PFID(mdt_object_fid(mfd->mfd_object)),
827 info->mti_rr.rr_handle->cookie);
830 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
831 info->mti_rr.rr_handle->cookie);
833 mfd->mfd_old_handle.cookie = info->mti_rr.rr_handle->cookie;
836 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
838 if (req->rq_export->exp_disconnected) {
839 spin_lock(&med->med_open_lock);
840 class_handle_unhash(&mfd->mfd_handle);
841 list_del_init(&mfd->mfd_list);
842 spin_unlock(&med->med_open_lock);
843 mdt_mfd_close(info, mfd);
845 spin_lock(&med->med_open_lock);
846 list_add(&mfd->mfd_list, &med->med_open_head);
847 spin_unlock(&med->med_open_lock);
850 mdt_empty_transno(info, rc);
855 if (flags & FMODE_WRITE)
856 /* XXX We also need to close io epoch here.
857 * See LU-1220 - green */
859 else if (flags & MDS_FMODE_EXEC)
865 static int mdt_finish_open(struct mdt_thread_info *info,
866 struct mdt_object *p, struct mdt_object *o,
867 __u64 flags, int created, struct ldlm_reply *rep)
869 struct ptlrpc_request *req = mdt_info_req(info);
870 struct obd_export *exp = req->rq_export;
871 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
872 struct md_attr *ma = &info->mti_attr;
873 struct lu_attr *la = &ma->ma_attr;
874 struct mdt_file_data *mfd;
875 struct mdt_body *repbody;
877 int isreg, isdir, islnk;
881 LASSERT(ma->ma_valid & MA_INODE);
883 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
885 isreg = S_ISREG(la->la_mode);
886 isdir = S_ISDIR(la->la_mode);
887 islnk = S_ISLNK(la->la_mode);
888 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
890 /* LU-2275, simulate broken behaviour (esp. prevalent in
891 * pre-2.4 servers where a very strange reply is sent on error
892 * that looks like it was actually almost succesful and a failure at the
894 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
895 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
899 if (flags & MDS_OPEN_LOCK)
900 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
905 if (exp_connect_rmtclient(exp)) {
906 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
908 rc = mdt_pack_remote_perm(info, o, buf);
910 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
911 repbody->mbo_aclsize = 0;
913 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
914 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
917 #ifdef CONFIG_FS_POSIX_ACL
918 else if (exp_connect_flags(exp) & OBD_CONNECT_ACL)
919 rc = mdt_pack_acl2body(info, repbody, o,
920 exp->exp_target_data.ted_nodemap);
923 if (info->mti_mdt->mdt_lut.lut_mds_capa &&
924 exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
925 struct lustre_capa *capa;
927 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
929 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
930 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
933 repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
935 if (info->mti_mdt->mdt_lut.lut_oss_capa &&
936 exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
937 S_ISREG(lu_object_attr(&o->mot_obj))) {
938 struct lustre_capa *capa;
940 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
942 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
943 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
946 repbody->mbo_valid |= OBD_MD_FLOSSCAPA;
950 * If we are following a symlink, don't open; and do not return open
951 * handle for special nodes as client required.
953 if (islnk || (!isreg && !isdir &&
954 (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
955 lustre_msg_set_transno(req->rq_repmsg, 0);
960 * We need to return the existing object's fid back, so it is done here,
961 * after preparing the reply.
963 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
966 /* This can't be done earlier, we need to return reply body */
968 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
969 /* We are trying to create or write an existing dir. */
972 } else if (flags & MDS_OPEN_DIRECTORY)
975 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
976 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
981 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
982 spin_lock(&med->med_open_lock);
983 list_for_each(t, &med->med_open_head) {
984 mfd = list_entry(t, struct mdt_file_data, mfd_list);
985 if (mfd->mfd_xid == req->rq_xid)
989 spin_unlock(&med->med_open_lock);
992 repbody->mbo_handle.cookie = mfd->mfd_handle.h_cookie;
993 /* set repbody->ea_size for resent case */
994 if (ma->ma_valid & MA_LOV) {
995 LASSERT(ma->ma_lmm_size != 0);
996 repbody->mbo_eadatasize = ma->ma_lmm_size;
998 repbody->mbo_valid |= OBD_MD_FLDIREA;
1000 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1002 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
1007 rc = mdt_mfd_open(info, p, o, flags, created, rep);
1009 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
1014 void mdt_reconstruct_open(struct mdt_thread_info *info,
1015 struct mdt_lock_handle *lhc)
1017 const struct lu_env *env = info->mti_env;
1018 struct mdt_device *mdt = info->mti_mdt;
1019 struct req_capsule *pill = info->mti_pill;
1020 struct ptlrpc_request *req = mdt_info_req(info);
1021 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1022 struct lsd_client_data *lcd = ted->ted_lcd;
1023 struct md_attr *ma = &info->mti_attr;
1024 struct mdt_reint_record *rr = &info->mti_rr;
1025 __u64 flags = info->mti_spec.sp_cr_flags;
1026 struct ldlm_reply *ldlm_rep;
1027 struct mdt_object *parent;
1028 struct mdt_object *child;
1029 struct mdt_body *repbody;
1033 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1034 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
1035 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1037 ma->ma_need = MA_INODE | MA_HSM;
1040 mdt_req_from_lcd(req, lcd);
1041 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1043 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1044 ldlm_rep->lock_policy_res1, req->rq_status);
1046 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1047 req->rq_status != 0)
1048 /* We did not create successfully, return error to client. */
1049 GOTO(out, rc = req->rq_status);
1051 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1052 struct obd_export *exp = req->rq_export;
1054 * We failed after creation, but we do not know in which step
1055 * we failed. So try to check the child object.
1057 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1058 if (IS_ERR(parent)) {
1059 rc = PTR_ERR(parent);
1060 LCONSOLE_WARN("Parent "DFID" lookup error %d."
1061 " Evicting client %s with export %s.\n",
1062 PFID(rr->rr_fid1), rc,
1063 obd_uuid2str(&exp->exp_client_uuid),
1064 obd_export_nid2str(exp));
1065 mdt_export_evict(exp);
1069 child = mdt_object_find(env, mdt, rr->rr_fid2);
1070 if (IS_ERR(child)) {
1071 rc = PTR_ERR(child);
1072 LCONSOLE_WARN("cannot lookup child "DFID": rc = %d; "
1073 "evicting client %s with export %s\n",
1074 PFID(rr->rr_fid2), rc,
1075 obd_uuid2str(&exp->exp_client_uuid),
1076 obd_export_nid2str(exp));
1077 mdt_object_put(env, parent);
1078 mdt_export_evict(exp);
1082 if (unlikely(mdt_object_remote(child))) {
1083 /* the child object was created on remote server */
1084 if (!mdt_is_dne_client(exp)) {
1085 /* Return -EIO for old client */
1086 mdt_object_put(env, parent);
1087 mdt_object_put(env, child);
1088 GOTO(out, rc = -EIO);
1090 repbody->mbo_fid1 = *rr->rr_fid2;
1091 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1094 if (mdt_object_exists(child)) {
1095 mdt_set_capainfo(info, 1, rr->rr_fid2,
1097 mdt_prep_ma_buf_from_rep(info, child, ma);
1098 rc = mdt_attr_get_complex(info, child, ma);
1100 rc = mdt_finish_open(info, parent,
1104 /* the child does not exist, we should do
1106 mdt_object_put(env, parent);
1107 mdt_object_put(env, child);
1108 GOTO(regular_open, 0);
1111 mdt_object_put(env, parent);
1112 mdt_object_put(env, child);
1116 /* We did not try to create, so we are a pure open */
1117 rc = mdt_reint_open(info, lhc);
1122 req->rq_status = rc;
1123 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1124 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1127 static int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep)
1129 __u64 flags = info->mti_spec.sp_cr_flags;
1130 struct mdt_reint_record *rr = &info->mti_rr;
1131 struct md_attr *ma = &info->mti_attr;
1132 struct mdt_object *o;
1136 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1138 RETURN(rc = PTR_ERR(o));
1140 if (unlikely(mdt_object_remote(o))) {
1141 /* the child object was created on remote server */
1142 struct mdt_body *repbody;
1144 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1147 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1148 repbody->mbo_fid1 = *rr->rr_fid2;
1149 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1152 if (mdt_object_exists(o)) {
1153 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1156 mdt_prep_ma_buf_from_rep(info, o, ma);
1157 rc = mdt_attr_get_complex(info, o, ma);
1159 rc = mdt_finish_open(info, NULL, o, flags, 0,
1166 mdt_object_put(info->mti_env, o);
1170 /* lock object for open */
1171 static int mdt_object_open_lock(struct mdt_thread_info *info,
1172 struct mdt_object *obj,
1173 struct mdt_lock_handle *lhc,
1176 struct md_attr *ma = &info->mti_attr;
1177 __u64 open_flags = info->mti_spec.sp_cr_flags;
1178 ldlm_mode_t lm = LCK_CR;
1179 bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
1180 bool try_layout = false;
1181 bool create_layout = false;
1186 mdt_lock_handle_init(lhc);
1188 if (req_is_replay(mdt_info_req(info)))
1191 if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
1192 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
1193 md_should_create(open_flags))
1194 create_layout = true;
1195 if (exp_connect_layout(info->mti_exp) && !create_layout &&
1196 ma->ma_need & MA_LOV)
1201 /* lease open, acquire write mode of open sem */
1202 down_write(&obj->mot_open_sem);
1204 /* Lease exists and ask for new lease */
1205 if (atomic_read(&obj->mot_lease_count) > 0) {
1206 /* only exclusive open is supported, so lease
1207 * are conflicted to each other */
1208 GOTO(out, rc = -EBUSY);
1211 /* Lease must be with open lock */
1212 if (!(open_flags & MDS_OPEN_LOCK)) {
1213 CERROR("Request lease for file:"DFID ", but open lock "
1214 "is missed, open_flags = "LPO64".\n",
1215 PFID(mdt_object_fid(obj)), open_flags);
1216 GOTO(out, rc = -EPROTO);
1219 /* XXX: only exclusive open is supported. */
1221 *ibits = MDS_INODELOCK_OPEN;
1223 /* never grant LCK_EX layout lock to client */
1225 } else { /* normal open */
1226 /* normal open holds read mode of open sem */
1227 down_read(&obj->mot_open_sem);
1229 if (open_flags & MDS_OPEN_LOCK) {
1230 if (open_flags & FMODE_WRITE)
1232 else if (open_flags & MDS_FMODE_EXEC)
1237 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
1238 } else if (atomic_read(&obj->mot_lease_count) > 0) {
1239 if (open_flags & FMODE_WRITE)
1245 *ibits = MDS_INODELOCK_OPEN;
1248 lhc = &info->mti_lh[MDT_LH_LOCAL];
1250 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
1251 PFID(mdt_object_fid(obj)),
1252 atomic_read(&obj->mot_open_count), lm);
1255 mdt_lock_reg_init(lhc, lm);
1257 /* one problem to return layout lock on open is that it may result
1258 * in too many layout locks cached on the client side. */
1259 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
1260 /* return lookup lock to validate inode at the client side,
1261 * this is pretty important otherwise mdt will return layout
1262 * lock for each open.
1263 * However this is a double-edged sword because changing
1264 * permission will revoke huge # of LOOKUP locks. */
1265 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
1266 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
1268 *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
1270 rc = mdt_object_lock(info, obj, lhc, *ibits,
1273 } else if (*ibits != 0) {
1274 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
1277 CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
1278 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
1279 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
1281 /* will change layout, revoke layout locks by enqueuing EX lock. */
1282 if (rc == 0 && create_layout) {
1283 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1285 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
1286 ", open_flags = "LPO64"\n",
1287 PFID(mdt_object_fid(obj)), open_flags);
1289 /* We cannot enqueue another lock for the same resource we
1290 * already have a lock for, due to mechanics of waiting list
1291 * iterating in ldlm, see LU-3601.
1292 * As such we'll drop the open lock we just got above here,
1293 * it's ok not to have this open lock as it's main purpose is to
1294 * flush unused cached client open handles. */
1295 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1296 mdt_object_unlock(info, obj, lhc, 1);
1298 LASSERT(!try_layout);
1299 mdt_lock_handle_init(ll);
1300 mdt_lock_reg_init(ll, LCK_EX);
1301 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
1304 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
1307 /* Check if there is any other open handles after acquiring
1308 * open lock. At this point, caching open handles have been revoked
1310 * XXX: Now only exclusive open is supported. Need to check the
1311 * type of open for generic lease support. */
1312 if (rc == 0 && acq_lease) {
1313 struct ptlrpc_request *req = mdt_info_req(info);
1314 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1315 struct mdt_file_data *mfd;
1316 bool is_replay_or_resent;
1319 /* For lease: application can open a file and then apply lease,
1320 * @handle contains original open handle in that case.
1321 * In recovery, open REQ will be replayed and the lease REQ may
1322 * be resent that means the open handle is already stale, so we
1323 * need to fix it up here by finding new handle. */
1324 is_replay_or_resent = req_is_replay(req) ||
1325 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
1327 /* if the request is _not_ a replay request, rr_handle
1328 * may be used to hold an openhandle which is issuing the
1329 * lease request, so that this openhandle doesn't count. */
1330 mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
1331 is_replay_or_resent);
1335 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
1336 PFID(mdt_object_fid(obj)),
1337 atomic_read(&obj->mot_open_count), open_count);
1339 if (atomic_read(&obj->mot_open_count) > open_count)
1340 GOTO(out, rc = -EBUSY);
1348 static void mdt_object_open_unlock(struct mdt_thread_info *info,
1349 struct mdt_object *obj,
1350 struct mdt_lock_handle *lhc,
1351 __u64 ibits, int rc)
1353 __u64 open_flags = info->mti_spec.sp_cr_flags;
1354 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
1357 if (req_is_replay(mdt_info_req(info)))
1360 /* Release local lock - the lock put in MDT_LH_LOCAL will never
1361 * return to client side. */
1362 if (lustre_handle_is_used(&ll->mlh_reg_lh))
1363 mdt_object_unlock(info, obj, ll, 1);
1365 ll = &info->mti_lh[MDT_LH_LAYOUT];
1366 /* Release local layout lock, layout was created */
1367 if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1368 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1369 mdt_object_unlock(info, obj, ll, 1);
1372 if (open_flags & MDS_OPEN_LEASE)
1373 up_write(&obj->mot_open_sem);
1375 up_read(&obj->mot_open_sem);
1377 /* Cross-ref case, the lock should be returned to the client */
1378 if (ibits == 0 || rc == -MDT_EREMOTE_OPEN)
1381 if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1382 /* for the open request, the lock will only return to client
1383 * if open or layout lock is granted. */
1387 if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1388 struct ldlm_reply *ldlm_rep;
1390 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1391 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1392 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1393 mdt_object_unlock(info, obj, lhc, 1);
1399 * Check release is permitted for the current HSM flags.
1401 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1403 if (!(ma->ma_valid & MA_HSM))
1406 if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1409 if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1415 static int mdt_open_by_fid_lock(struct mdt_thread_info *info,
1416 struct ldlm_reply *rep,
1417 struct mdt_lock_handle *lhc)
1419 const struct lu_env *env = info->mti_env;
1420 struct mdt_device *mdt = info->mti_mdt;
1421 __u64 flags = info->mti_spec.sp_cr_flags;
1422 struct mdt_reint_record *rr = &info->mti_rr;
1423 struct md_attr *ma = &info->mti_attr;
1424 struct mdt_object *parent= NULL;
1425 struct mdt_object *o;
1427 int object_locked = 0;
1431 if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1432 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1433 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1434 if (IS_ERR(parent)) {
1435 CDEBUG(D_INODE, "Fail to find parent "DFID
1436 " for anonymous created %ld, try to"
1437 " use server-side parent.\n",
1438 PFID(rr->rr_fid1), PTR_ERR(parent));
1443 ma->ma_need |= MA_PFID;
1446 o = mdt_object_find(env, mdt, rr->rr_fid2);
1448 GOTO(out_parent_put, rc = PTR_ERR(o));
1450 if (mdt_object_remote(o)) {
1451 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1452 mdt_obd_name(info->mti_mdt),
1454 GOTO(out, rc = -EREMOTE);
1455 } else if (!mdt_object_exists(o)) {
1456 mdt_set_disposition(info, rep,
1460 GOTO(out, rc = -ENOENT);
1463 mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1465 mdt_prep_ma_buf_from_rep(info, o, ma);
1466 if (flags & MDS_OPEN_RELEASE)
1467 ma->ma_need |= MA_HSM;
1468 rc = mdt_attr_get_complex(info, o, ma);
1472 /* If a release request, check file flags are fine and ask for an
1473 * exclusive open access. */
1474 if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1475 GOTO(out, rc = -EPERM);
1477 rc = mdt_check_resent_lock(info, o, lhc);
1480 } else if (rc > 0) {
1481 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1484 GOTO(out_unlock, rc);
1487 if (ma->ma_valid & MA_PFID) {
1488 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1489 if (IS_ERR(parent)) {
1490 CDEBUG(D_INODE, "Fail to find parent "DFID
1491 " for anonymous created %ld, try to"
1492 " use system default.\n",
1493 PFID(&ma->ma_pfid), PTR_ERR(parent));
1498 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1500 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1501 if (flags & MDS_OPEN_LOCK)
1502 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1503 if (flags & MDS_OPEN_LEASE)
1504 mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1506 GOTO(out_unlock, rc);
1510 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1512 mdt_object_put(env, o);
1515 mdt_object_put(env, parent);
1519 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1520 static int mdt_cross_open(struct mdt_thread_info *info,
1521 const struct lu_fid *parent_fid,
1522 const struct lu_fid *fid,
1523 struct ldlm_reply *rep, __u32 flags)
1525 struct md_attr *ma = &info->mti_attr;
1526 struct mdt_object *o;
1530 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1532 RETURN(rc = PTR_ERR(o));
1534 if (mdt_object_remote(o)) {
1535 /* Something is wrong here, the object is on another MDS! */
1536 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1537 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1538 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1540 "Object isn't on this server! FLD error?\n");
1543 if (mdt_object_exists(o)) {
1544 /* Do permission check for cross-open. */
1545 rc = mo_permission(info->mti_env, NULL,
1546 mdt_object_child(o),
1547 NULL, flags | MDS_OPEN_CROSS);
1551 mdt_prep_ma_buf_from_rep(info, o, ma);
1552 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1553 rc = mdt_attr_get_complex(info, o, ma);
1557 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1560 * Something is wrong here. lookup was positive but
1561 * there is no object!
1563 CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1564 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1569 mdt_object_put(info->mti_env, o);
1573 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1575 struct mdt_device *mdt = info->mti_mdt;
1576 struct ptlrpc_request *req = mdt_info_req(info);
1577 struct mdt_object *parent;
1578 struct mdt_object *child;
1579 struct mdt_lock_handle *lh;
1580 struct ldlm_reply *ldlm_rep;
1581 struct mdt_body *repbody;
1582 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1583 struct md_attr *ma = &info->mti_attr;
1584 __u64 create_flags = info->mti_spec.sp_cr_flags;
1586 struct mdt_reint_record *rr = &info->mti_rr;
1589 int object_locked = 0;
1593 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1594 (obd_timeout + 1) / 4);
1596 mdt_counter_incr(req, LPROC_MDT_OPEN);
1597 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1599 ma->ma_need = MA_INODE;
1602 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1603 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1605 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1606 CERROR("file join is not supported anymore.\n");
1607 GOTO(out, result = err_serious(-EOPNOTSUPP));
1609 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1611 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1612 info->mti_spec.u.sp_ea.eadata == NULL)
1613 GOTO(out, result = err_serious(-EINVAL));
1615 CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1616 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1617 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1618 PFID(rr->rr_fid2), create_flags,
1619 ma->ma_attr.la_mode, msg_flags);
1621 if (info->mti_cross_ref) {
1622 /* This is cross-ref open */
1623 mdt_set_disposition(info, ldlm_rep,
1624 (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1626 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1627 ldlm_rep, create_flags);
1629 } else if (req_is_replay(req) ||
1630 (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1631 /* This is a replay request or from liblustre with ea. */
1632 result = mdt_open_by_fid(info, ldlm_rep);
1634 if (result != -ENOENT) {
1635 if (req->rq_export->exp_libclient &&
1636 create_flags & MDS_OPEN_HAS_EA)
1637 GOTO(out, result = 0);
1640 /* We didn't find the correct object, so we need to re-create it
1641 * via a regular replay. */
1642 if (!(create_flags & MDS_OPEN_CREAT)) {
1643 DEBUG_REQ(D_ERROR, req,
1644 "OPEN & CREAT not in open replay/by_fid.");
1645 GOTO(out, result = -EFAULT);
1647 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1648 } else if (create_flags & (MDS_OPEN_BY_FID | MDS_OPEN_LOCK)) {
1650 * MDS_OPEN_LOCK is checked for backward compatibility with 2.1
1653 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1655 CDEBUG(D_INFO, "no object for "DFID": %d\n",
1656 PFID(rr->rr_fid2), result);
1660 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1661 GOTO(out, result = err_serious(-ENOMEM));
1663 mdt_set_disposition(info, ldlm_rep,
1664 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1666 if (!lu_name_is_valid(&rr->rr_name))
1667 GOTO(out, result = -EPROTO);
1669 lh = &info->mti_lh[MDT_LH_PARENT];
1670 mdt_lock_pdo_init(lh,
1671 (create_flags & MDS_OPEN_CREAT) ? LCK_PW : LCK_PR,
1674 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1675 MDS_INODELOCK_UPDATE);
1677 GOTO(out, result = PTR_ERR(parent));
1679 /* get and check version of parent */
1680 result = mdt_version_get_check(info, parent, 0);
1682 GOTO(out_parent, result);
1684 fid_zero(child_fid);
1686 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1687 &rr->rr_name, child_fid, &info->mti_spec);
1689 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1690 "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1691 PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1694 if (result != 0 && result != -ENOENT && result != -ESTALE)
1695 GOTO(out_parent, result);
1697 if (result == -ENOENT || result == -ESTALE) {
1698 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1699 if (result == -ESTALE) {
1701 * -ESTALE means the parent is a dead(unlinked) dir, so
1702 * it should return -ENOENT to in accordance with the
1703 * original mds implementaion.
1705 GOTO(out_parent, result = -ENOENT);
1707 if (!(create_flags & MDS_OPEN_CREAT))
1708 GOTO(out_parent, result);
1709 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1710 GOTO(out_parent, result = -EROFS);
1711 *child_fid = *info->mti_rr.rr_fid2;
1712 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1714 /* In the function below, .hs_keycmp resolves to
1715 * lu_obj_hop_keycmp() */
1716 /* coverity[overrun-buffer-val] */
1717 child = mdt_object_new(info->mti_env, mdt, child_fid);
1720 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1721 * return FID back in that case.
1723 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1724 child = mdt_object_find(info->mti_env, mdt, child_fid);
1727 GOTO(out_parent, result = PTR_ERR(child));
1729 /** check version of child */
1730 rc = mdt_version_get_check(info, child, 1);
1732 GOTO(out_child, result = rc);
1734 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1735 if (result == -ENOENT) {
1736 /* Create under OBF and .lustre is not permitted */
1737 if (!fid_is_md_operative(rr->rr_fid1))
1738 GOTO(out_child, result = -EPERM);
1740 /* save versions in reply */
1741 mdt_version_get_save(info, parent, 0);
1742 mdt_version_get_save(info, child, 1);
1744 /* version of child will be changed */
1745 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1747 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1748 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1750 /* Let lower layers know what is lock mode on directory. */
1751 info->mti_spec.sp_cr_mode =
1752 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1755 * Do not perform lookup sanity check. We know that name does
1758 info->mti_spec.sp_cr_lookup = 0;
1759 info->mti_spec.sp_feat = &dt_directory_features;
1761 result = mdo_create(info->mti_env,
1762 mdt_object_child(parent),
1764 mdt_object_child(child),
1767 if (result == -ERESTART) {
1768 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1769 GOTO(out_child, result);
1771 mdt_prep_ma_buf_from_rep(info, child, ma);
1772 /* XXX: we should call this once, see few lines below */
1774 result = mdt_attr_get_complex(info, child, ma);
1777 GOTO(out_child, result);
1782 * The object is on remote node, return its FID for remote open.
1784 if (mdt_object_remote(child)) {
1786 * Check if this lock already was sent to client and
1787 * this is resent case. For resent case do not take lock
1788 * again, use what is already granted.
1790 LASSERT(lhc != NULL);
1792 rc = mdt_check_resent_lock(info, child, lhc);
1794 GOTO(out_child, result = rc);
1795 } else if (rc > 0) {
1796 mdt_lock_handle_init(lhc);
1797 mdt_lock_reg_init(lhc, LCK_PR);
1799 rc = mdt_object_lock(info, child, lhc,
1800 MDS_INODELOCK_LOOKUP,
1803 repbody->mbo_fid1 = *mdt_object_fid(child);
1804 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1808 result = -MDT_EREMOTE_OPEN;
1809 GOTO(out_child, result);
1810 } else if (mdt_object_exists(child)) {
1811 /* We have to get attr & LOV EA & HSM for this
1813 mdt_prep_ma_buf_from_rep(info, child, ma);
1814 ma->ma_need |= MA_HSM;
1815 result = mdt_attr_get_complex(info, child, ma);
1817 GOTO(out_child, result);
1819 /* Object does not exist. Likely FS corruption. */
1820 CERROR("%s: name '"DNAME"' present, but FID "
1821 DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1822 PNAME(&rr->rr_name), PFID(child_fid));
1823 GOTO(out_child, result = -EIO);
1827 rc = mdt_check_resent_lock(info, child, lhc);
1829 GOTO(out_child, result = rc);
1830 } else if (rc == 0) {
1831 /* the open lock might already be gotten in
1832 * ldlm_handle_enqueue() */
1833 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1834 if (create_flags & MDS_OPEN_LOCK)
1835 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1837 /* get openlock if this isn't replay and client requested it */
1838 if (!req_is_replay(req)) {
1839 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1842 GOTO(out_child_unlock, result = rc);
1843 else if (create_flags & MDS_OPEN_LOCK)
1844 mdt_set_disposition(info, ldlm_rep,
1848 /* Try to open it now. */
1849 rc = mdt_finish_open(info, parent, child, create_flags,
1853 /* openlock will be released if mdt_finish_open failed */
1854 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1856 if (created && create_flags & MDS_OPEN_VOLATILE) {
1857 CERROR("%s: cannot open volatile file "DFID", orphan "
1858 "file will be left in PENDING directory until "
1859 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1860 PFID(mdt_object_fid(child)), rc);
1861 GOTO(out_child_unlock, result);
1867 rc = mdo_unlink(info->mti_env,
1868 mdt_object_child(parent),
1869 mdt_object_child(child),
1871 &info->mti_attr, 0);
1873 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1874 mdt_obd_name(info->mti_mdt),
1875 PFID(mdt_object_fid(child)), rc);
1876 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1882 mdt_object_open_unlock(info, child, lhc, ibits, result);
1884 mdt_object_put(info->mti_env, child);
1886 mdt_object_unlock_put(info, parent, lh, result || !created);
1889 lustre_msg_set_transno(req->rq_repmsg, 0);
1894 * Create an orphan object use local root.
1896 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1897 struct mdt_device *mdt,
1898 const struct lu_fid *fid,
1899 struct md_attr *attr, fmode_t fmode)
1901 const struct lu_env *env = info->mti_env;
1902 struct md_op_spec *spec = &info->mti_spec;
1903 struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1904 struct mdt_object *obj = NULL;
1905 struct mdt_object *local_root;
1906 static const struct lu_name lname = {
1907 .ln_name = "i_am_nobody",
1908 .ln_namelen = sizeof("i_am_nobody") - 1,
1910 struct lu_ucred *uc;
1911 cfs_cap_t uc_cap_save;
1915 rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1917 RETURN(ERR_PTR(rc));
1919 local_root = mdt_object_find(env, mdt, local_root_fid);
1920 if (IS_ERR(local_root))
1923 obj = mdt_object_new(env, mdt, fid);
1925 GOTO(out, rc = PTR_ERR(obj));
1927 spec->sp_cr_lookup = 0;
1928 spec->sp_feat = &dt_directory_features;
1929 spec->sp_cr_mode = MDL_MINMODE; /* no lock */
1930 spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1931 if (attr->ma_valid & MA_LOV) {
1932 spec->u.sp_ea.eadata = attr->ma_lmm;
1933 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1934 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1936 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1940 uc_cap_save = uc->uc_cap;
1941 uc->uc_cap |= 1 << CFS_CAP_DAC_OVERRIDE;
1942 rc = mdo_create(env, mdt_object_child(local_root), &lname,
1943 mdt_object_child(obj), spec, attr);
1944 uc->uc_cap = uc_cap_save;
1946 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1947 mdt_obd_name(mdt), PFID(fid), rc);
1951 rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
1953 CERROR("%s: cannot open volatile file "DFID", orphan "
1954 "file will be left in PENDING directory until "
1955 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1962 mdt_object_put(env, obj);
1965 mdt_object_put(env, local_root);
1969 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
1972 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
1973 struct close_data *data;
1974 struct ldlm_lock *lease;
1975 struct mdt_object *orphan;
1976 struct md_attr *orp_ma;
1983 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
1986 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1990 lease = ldlm_handle2lock(&data->cd_handle);
1994 /* try to hold open_sem so that nobody else can open the file */
1995 if (!down_write_trylock(&o->mot_open_sem)) {
1996 ldlm_lock_cancel(lease);
1997 GOTO(out_reprocess, rc = -EBUSY);
2000 /* Check if the lease open lease has already canceled */
2001 lock_res_and_lock(lease);
2002 lease_broken = ldlm_is_cancel(lease);
2003 unlock_res_and_lock(lease);
2005 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2006 PFID(mdt_object_fid(o)), lease_broken);
2008 /* Cancel server side lease. Client side counterpart should
2009 * have been cancelled. It's okay to cancel it now as we've
2010 * held mot_open_sem. */
2011 ldlm_lock_cancel(lease);
2013 if (lease_broken) /* don't perform release task */
2014 GOTO(out_unlock, rc = -ESTALE);
2016 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2017 GOTO(out_unlock, rc = -EINVAL);
2019 /* ma_need was set before but it seems fine to change it in order to
2020 * avoid modifying the one from RPC */
2021 ma->ma_need = MA_HSM;
2022 rc = mdt_attr_get_complex(info, o, ma);
2024 GOTO(out_unlock, rc);
2026 if (!mdt_hsm_release_allow(ma))
2027 GOTO(out_unlock, rc = -EPERM);
2029 /* already released? */
2030 if (ma->ma_hsm.mh_flags & HS_RELEASED)
2031 GOTO(out_unlock, rc = 0);
2033 /* Compare on-disk and packed data_version */
2034 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
2035 CDEBUG(D_HSM, DFID" data_version mismatches: packed="LPU64
2036 " and on-disk="LPU64"\n", PFID(mdt_object_fid(o)),
2037 data->cd_data_version, ma->ma_hsm.mh_arch_ver);
2038 GOTO(out_unlock, rc = -EPERM);
2041 ma->ma_valid = MA_INODE;
2042 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
2043 rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
2045 GOTO(out_unlock, rc);
2047 ma->ma_need = MA_INODE | MA_LOV;
2048 rc = mdt_attr_get_complex(info, o, ma);
2050 GOTO(out_unlock, rc);
2052 if (!(ma->ma_valid & MA_LOV)) {
2053 /* Even empty file are released */
2054 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
2055 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2056 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
2057 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
2058 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
2060 /* Magic must be LOV_MAGIC_Vx_DEF otherwise LOD will interpret
2061 * ma_lmm as lov_user_md, then it will be confused by union of
2062 * layout_gen and stripe_offset. */
2063 if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1)
2064 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2065 else if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3)
2066 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
2068 GOTO(out_unlock, rc = -EINVAL);
2071 /* Set file as released */
2072 ma->ma_lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
2074 /* Hopefully it's not used in this call path */
2075 orp_ma = &info->mti_u.som.attr;
2076 orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
2077 orp_ma->ma_attr.la_uid = ma->ma_attr.la_uid;
2078 orp_ma->ma_attr.la_gid = ma->ma_attr.la_gid;
2079 orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
2080 orp_ma->ma_lmm = ma->ma_lmm;
2081 orp_ma->ma_lmm_size = ma->ma_lmm_size;
2082 orp_ma->ma_valid = MA_INODE | MA_LOV;
2083 orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
2085 if (IS_ERR(orphan)) {
2086 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
2087 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
2089 GOTO(out_unlock, rc = PTR_ERR(orphan));
2092 /* Set up HSM attribute for orphan object */
2093 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
2094 buf = &info->mti_buf;
2095 buf->lb_buf = info->mti_xattr_buf;
2096 buf->lb_len = sizeof(struct hsm_attrs);
2097 ma->ma_hsm.mh_flags |= HS_RELEASED;
2098 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
2099 ma->ma_hsm.mh_flags &= ~HS_RELEASED;
2101 mdt_lock_reg_init(lh, LCK_EX);
2102 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
2103 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
2105 GOTO(out_close, rc);
2107 rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
2111 /* Swap layout with orphan object */
2112 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
2113 mdt_object_child(orphan),
2114 SWAP_LAYOUTS_MDS_HSM);
2116 /* Release exclusive LL */
2117 mdt_object_unlock(info, o, lh, 1);
2122 /* Close orphan object anyway */
2123 rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
2126 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
2127 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
2128 LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
2130 mdt_object_put(info->mti_env, orphan);
2133 up_write(&o->mot_open_sem);
2135 if (rc == 0) { /* already released */
2136 struct mdt_body *repbody;
2137 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2138 LASSERT(repbody != NULL);
2139 repbody->mbo_valid |= OBD_MD_FLRELEASED;
2143 ldlm_reprocess_all(lease->l_resource);
2144 LDLM_LOCK_PUT(lease);
2152 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
2153 MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
2155 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2157 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
2160 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2162 struct mdt_object *o = mfd->mfd_object;
2163 struct md_object *next = mdt_object_child(o);
2164 struct md_attr *ma = &info->mti_attr;
2165 int ret = MDT_IOEPOCH_CLOSED;
2170 mode = mfd->mfd_mode;
2172 if (ma->ma_attr_flags & MDS_HSM_RELEASE) {
2173 rc = mdt_hsm_release(info, o, ma);
2175 CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2176 mdt_obd_name(info->mti_mdt),
2177 PFID(mdt_object_fid(o)), rc);
2178 /* continue to close even error occurred. */
2182 if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
2184 ret = mdt_ioepoch_close(info, o);
2185 } else if (mode & MDS_FMODE_EXEC) {
2187 } else if (mode & MDS_FMODE_EPOCH) {
2188 ret = mdt_ioepoch_close(info, o);
2189 } else if (mode & MDS_FMODE_SOM) {
2190 ret = mdt_som_au_close(info, o);
2193 /* Update atime on close only. */
2194 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
2195 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
2196 /* Set the atime only. */
2197 ma->ma_valid = MA_INODE;
2198 ma->ma_attr.la_valid = LA_ATIME;
2199 rc = mo_attr_set(info->mti_env, next, ma);
2202 /* If file data is modified, add the dirty flag. */
2203 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2204 rc = mdt_add_dirty_flag(info, o, ma);
2206 ma->ma_need |= MA_INODE;
2207 ma->ma_valid &= ~MA_INODE;
2209 if (!MFD_CLOSED(mode))
2210 rc = mo_close(info->mti_env, next, ma, mode);
2212 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
2213 struct mdt_export_data *med;
2215 /* The IOepoch is still opened or SOM update is needed.
2216 * Put mfd back into the list. */
2217 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
2218 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
2219 MDS_FMODE_EPOCH : MDS_FMODE_SOM);
2221 LASSERT(mdt_info_req(info));
2222 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
2223 spin_lock(&med->med_open_lock);
2224 list_add(&mfd->mfd_list, &med->med_open_head);
2225 class_handle_hash_back(&mfd->mfd_handle);
2226 spin_unlock(&med->med_open_lock);
2228 if (ret == MDT_IOEPOCH_OPENED) {
2232 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
2233 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
2236 /* adjust open and lease count */
2237 if (mode & MDS_OPEN_LEASE) {
2238 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2239 atomic_dec(&o->mot_lease_count);
2241 LASSERT(atomic_read(&o->mot_open_count) > 0);
2242 atomic_dec(&o->mot_open_count);
2245 mdt_object_put(info->mti_env, o);
2248 RETURN(rc ? rc : ret);
2251 int mdt_close(struct tgt_session_info *tsi)
2253 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2254 struct ptlrpc_request *req = tgt_ses_req(tsi);
2255 struct mdt_export_data *med;
2256 struct mdt_file_data *mfd;
2257 struct mdt_object *o;
2258 struct md_attr *ma = &info->mti_attr;
2259 struct mdt_body *repbody = NULL;
2263 mdt_counter_incr(req, LPROC_MDT_CLOSE);
2264 /* Close may come with the Size-on-MDS update. Unpack it. */
2265 rc = mdt_close_unpack(info);
2267 GOTO(out, rc = err_serious(rc));
2269 LASSERT(info->mti_ioepoch);
2271 /* These fields are no longer used and are left for compatibility.
2272 * size is always zero */
2273 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2275 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2277 rc = req_capsule_server_pack(info->mti_pill);
2278 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2279 mdt_client_compatibility(info);
2281 mdt_fix_reply(info);
2282 mdt_exit_ucred(info);
2283 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2286 /* Continue to close handle even if we can not pack reply */
2288 repbody = req_capsule_server_get(info->mti_pill,
2290 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2292 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2295 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
2296 repbody->mbo_eadatasize = 0;
2297 repbody->mbo_aclsize = 0;
2299 rc = err_serious(rc);
2302 med = &req->rq_export->exp_mdt_data;
2303 spin_lock(&med->med_open_lock);
2304 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2305 req_is_replay(req));
2306 if (mdt_mfd_closed(mfd)) {
2307 spin_unlock(&med->med_open_lock);
2308 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2309 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
2310 info->mti_ioepoch->handle.cookie);
2311 /** not serious error since bug 3633 */
2314 class_handle_unhash(&mfd->mfd_handle);
2315 list_del_init(&mfd->mfd_list);
2316 spin_unlock(&med->med_open_lock);
2318 /* Do not lose object before last unlink. */
2319 o = mfd->mfd_object;
2320 mdt_object_get(info->mti_env, o);
2321 ret = mdt_mfd_close(info, mfd);
2322 if (repbody != NULL)
2323 rc = mdt_handle_last_unlink(info, o, ma);
2324 mdt_empty_transno(info, rc);
2325 mdt_object_put(info->mti_env, o);
2327 if (repbody != NULL) {
2328 mdt_client_compatibility(info);
2329 rc = mdt_fix_reply(info);
2332 mdt_exit_ucred(info);
2333 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2334 GOTO(out, rc = err_serious(-ENOMEM));
2336 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2337 OBD_FAIL_MDS_CLOSE_NET_REP))
2338 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2340 mdt_thread_info_fini(info);
2341 RETURN(rc ? rc : ret);
2345 * DONE_WRITING rpc handler.
2347 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
2348 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
2349 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
2350 * skip attributes and reconstruct the reply here.
2352 int mdt_done_writing(struct tgt_session_info *tsi)
2354 struct ptlrpc_request *req = tgt_ses_req(tsi);
2355 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2356 struct mdt_body *repbody = NULL;
2357 struct mdt_export_data *med;
2358 struct mdt_file_data *mfd;
2362 rc = req_capsule_server_pack(tsi->tsi_pill);
2364 GOTO(out, rc = err_serious(rc));
2366 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2367 repbody->mbo_eadatasize = 0;
2368 repbody->mbo_aclsize = 0;
2370 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
2371 rc = mdt_close_unpack(info);
2373 GOTO(out, rc = err_serious(rc));
2375 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2376 mdt_exit_ucred(info);
2377 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2380 med = &info->mti_exp->exp_mdt_data;
2381 spin_lock(&med->med_open_lock);
2382 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2383 req_is_replay(req));
2385 spin_unlock(&med->med_open_lock);
2386 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
2387 ": cookie = "LPX64" ioepoch = "LPU64"\n",
2388 PFID(info->mti_rr.rr_fid1),
2389 info->mti_ioepoch->handle.cookie,
2390 info->mti_ioepoch->ioepoch);
2391 /* If this is a replay, reconstruct the transno. */
2392 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2393 rc = info->mti_ioepoch->flags & MF_SOM_AU ?
2395 mdt_empty_transno(info, rc);
2398 GOTO(error_ucred, rc);
2401 LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
2402 mfd->mfd_mode == MDS_FMODE_TRUNC);
2403 class_handle_unhash(&mfd->mfd_handle);
2404 list_del_init(&mfd->mfd_list);
2405 spin_unlock(&med->med_open_lock);
2407 /* Set EPOCH CLOSE flag if not set by client. */
2408 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
2409 info->mti_attr.ma_valid = 0;
2411 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
2412 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2413 if (info->mti_attr.ma_lmm == NULL)
2414 GOTO(error_ucred, rc = -ENOMEM);
2416 rc = mdt_mfd_close(info, mfd);
2418 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2419 mdt_empty_transno(info, rc);
2421 mdt_exit_ucred(info);
2423 mdt_thread_info_fini(info);