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, 2013, 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"
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(const struct mdt_export_data *med)
63 struct mdt_file_data *mfd;
68 CFS_INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
69 mfd->mfd_handle.h_owner = med;
70 CFS_INIT_LIST_HEAD(&mfd->mfd_list);
71 class_handle_hash(&mfd->mfd_handle, &mfd_handle_ops);
78 * Find the mfd pointed to by handle in global hash table.
79 * In case of replay the handle is obsoleted
80 * but mfd can be found in mfd list by that handle.
81 * Callers need to be holding med_open_lock.
83 struct mdt_file_data *mdt_handle2mfd(struct mdt_export_data *med,
84 const struct lustre_handle *handle,
85 bool is_replay_or_resent)
87 struct mdt_file_data *mfd;
90 LASSERT(handle != NULL);
91 mfd = class_handle2object(handle->cookie, med);
92 /* during dw/setattr replay the mfd can be found by old handle */
93 if (mfd == NULL && is_replay_or_resent) {
94 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
95 if (mfd->mfd_old_handle.cookie == handle->cookie)
105 void mdt_mfd_free(struct mdt_file_data *mfd)
107 LASSERT(cfs_list_empty(&mfd->mfd_list));
108 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
111 static int mdt_create_data(struct mdt_thread_info *info,
112 struct mdt_object *p, struct mdt_object *o)
114 struct md_op_spec *spec = &info->mti_spec;
115 struct md_attr *ma = &info->mti_attr;
119 if (!md_should_create(spec->sp_cr_flags))
122 ma->ma_need = MA_INODE | MA_LOV;
124 mutex_lock(&o->mot_lov_mutex);
125 if (!(o->mot_flags & MOF_LOV_CREATED)) {
126 if (p != NULL && (fid_is_obf(mdt_object_fid(p)) ||
127 fid_is_dot_lustre(mdt_object_fid(p))))
128 GOTO(unlock, rc = -EPERM);
130 rc = mdo_create_data(info->mti_env,
131 p ? mdt_object_child(p) : NULL,
132 mdt_object_child(o), spec, ma);
134 rc = mdt_attr_get_complex(info, o, ma);
136 if (rc == 0 && ma->ma_valid & MA_LOV)
137 o->mot_flags |= MOF_LOV_CREATED;
140 mutex_unlock(&o->mot_lov_mutex);
144 static int mdt_ioepoch_opened(struct mdt_object *mo)
146 return mo->mot_ioepoch_count;
149 int mdt_object_is_som_enabled(struct mdt_object *mo)
151 return !mo->mot_ioepoch;
155 * Re-enable Size-on-MDS.
156 * Call under ->mot_ioepoch_mutex.
158 static void mdt_object_som_enable(struct mdt_object *mo, __u64 ioepoch)
160 if (ioepoch == mo->mot_ioepoch) {
161 LASSERT(!mdt_ioepoch_opened(mo));
168 * Open the IOEpoch. It is allowed if @writecount is not negative.
169 * The epoch and writecount handling is performed under the mot_ioepoch_mutex.
171 int mdt_ioepoch_open(struct mdt_thread_info *info, struct mdt_object *o,
174 struct mdt_device *mdt = info->mti_mdt;
179 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
180 !S_ISREG(lu_object_attr(&o->mot_obj)))
183 mutex_lock(&o->mot_ioepoch_mutex);
184 if (mdt_ioepoch_opened(o)) {
185 /* Epoch continues even if there is no writers yet. */
186 CDEBUG(D_INODE, "continue epoch "LPU64" for "DFID"\n",
187 o->mot_ioepoch, PFID(mdt_object_fid(o)));
189 /* XXX: ->mdt_ioepoch is not initialized at the mount */
190 spin_lock(&mdt->mdt_ioepoch_lock);
191 if (mdt->mdt_ioepoch < info->mti_replayepoch)
192 mdt->mdt_ioepoch = info->mti_replayepoch;
194 if (info->mti_replayepoch)
195 o->mot_ioepoch = info->mti_replayepoch;
196 else if (++mdt->mdt_ioepoch == IOEPOCH_INVAL)
197 o->mot_ioepoch = ++mdt->mdt_ioepoch;
199 o->mot_ioepoch = mdt->mdt_ioepoch;
201 spin_unlock(&mdt->mdt_ioepoch_lock);
203 CDEBUG(D_INODE, "starting epoch "LPU64" for "DFID"\n",
204 o->mot_ioepoch, PFID(mdt_object_fid(o)));
206 o->mot_flags |= MOF_SOM_CREATED;
209 o->mot_ioepoch_count++;
210 mutex_unlock(&o->mot_ioepoch_mutex);
212 /* Cancel Size-on-MDS attributes cached on clients for the open case.
213 * In the truncate case, see mdt_reint_setattr(). */
214 if (cancel && (info->mti_rr.rr_fid1 != NULL)) {
215 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
216 mdt_lock_reg_init(lh, LCK_EX);
217 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_UPDATE,
220 mdt_object_unlock(info, o, lh, 1);
226 * Update SOM on-disk attributes.
227 * If enabling, write update inodes and lustre-ea with the proper IOEpoch,
228 * mountid and attributes. If disabling, clean SOM xattr.
229 * Call under ->mot_ioepoch_mutex.
231 static int mdt_som_attr_set(struct mdt_thread_info *info,
232 struct mdt_object *obj, __u64 ioepoch, bool enable)
234 struct md_object *next = mdt_object_child(obj);
238 CDEBUG(D_INODE, "Size-on-MDS attribute %s for epoch "LPU64
239 " on "DFID".\n", enable ? "update" : "disabling",
240 ioepoch, PFID(mdt_object_fid(obj)));
243 struct lu_buf *buf = &info->mti_buf;
244 struct som_attrs *attrs;
245 struct md_attr *ma = &info->mti_attr;
246 struct lu_attr *la = &ma->ma_attr;
247 struct obd_device *obd = info->mti_mdt->mdt_lut.lut_obd;
249 attrs = (struct som_attrs *)info->mti_xattr_buf;
250 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
252 /* pack SOM attributes */
253 memset(attrs, 0, sizeof(*attrs));
254 attrs->som_ioepoch = ioepoch;
255 attrs->som_mountid = obd->u.obt.obt_mount_count;
256 if ((la->la_valid & LA_SIZE) != 0)
257 attrs->som_size = la->la_size;
258 if ((la->la_valid & LA_BLOCKS) != 0)
259 attrs->som_blocks = la->la_blocks;
260 lustre_som_swab(attrs);
262 /* update SOM attributes */
264 buf->lb_len = sizeof(*attrs);
265 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_SOM, 0);
267 /* delete SOM attributes */
268 rc = mo_xattr_del(info->mti_env, next, XATTR_NAME_SOM);
274 /** Perform the eviction specific actions on ioepoch close. */
275 static inline int mdt_ioepoch_close_on_eviction(struct mdt_thread_info *info,
276 struct mdt_object *o)
280 mutex_lock(&o->mot_ioepoch_mutex);
281 CDEBUG(D_INODE, "Eviction. Closing IOepoch "LPU64" on "DFID". "
282 "Count %d\n", o->mot_ioepoch, PFID(mdt_object_fid(o)),
283 o->mot_ioepoch_count);
284 o->mot_ioepoch_count--;
286 /* If eviction occured set MOF_SOM_RECOV,
287 * if no other epoch holders, disable SOM on disk. */
288 o->mot_flags |= MOF_SOM_CHANGE | MOF_SOM_RECOV;
289 if (!mdt_ioepoch_opened(o)) {
290 rc = mdt_som_attr_set(info, o, o->mot_ioepoch, MDT_SOM_DISABLE);
291 mdt_object_som_enable(o, o->mot_ioepoch);
293 mutex_unlock(&o->mot_ioepoch_mutex);
298 * Perform the replay specific actions on ioepoch close.
299 * Skip SOM attribute update if obtained and just forget about the inode state
300 * for the last ioepoch holder. The SOM cache is invalidated on MDS failure.
302 static inline int mdt_ioepoch_close_on_replay(struct mdt_thread_info *info,
303 struct mdt_object *o)
305 int rc = MDT_IOEPOCH_CLOSED;
308 mutex_lock(&o->mot_ioepoch_mutex);
309 CDEBUG(D_INODE, "Replay. Closing epoch "LPU64" on "DFID". Count %d\n",
310 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
311 o->mot_ioepoch_count--;
313 /* Get an info from the replayed request if client is supposed
314 * to send an Attibute Update, reconstruct @rc if so */
315 if (info->mti_ioepoch->flags & MF_SOM_AU)
316 rc = MDT_IOEPOCH_GETATTR;
318 if (!mdt_ioepoch_opened(o))
319 mdt_object_som_enable(o, info->mti_ioepoch->ioepoch);
320 mutex_unlock(&o->mot_ioepoch_mutex);
326 * Regular file IOepoch close.
327 * Closes the ioepoch, checks the object state, apply obtained attributes and
328 * re-enable SOM on the object, if possible. Also checks if the recovery is
329 * needed and packs OBD_MD_FLGETATTRLOCK flag into the reply to force the client
330 * to obtain SOM attributes under the server-side OST locks.
333 * MDT_IOEPOCH_CLOSED if ioepoch is closed.
334 * MDT_IOEPOCH_GETATTR if ioepoch is closed but another SOM update is needed.
336 static inline int mdt_ioepoch_close_reg(struct mdt_thread_info *info,
337 struct mdt_object *o)
339 struct md_attr *tmp_ma;
343 int rc = 0, ret = MDT_IOEPOCH_CLOSED;
346 la = &info->mti_attr.ma_attr;
347 achange = (info->mti_ioepoch->flags & MF_SOM_CHANGE);
349 mutex_lock(&o->mot_ioepoch_mutex);
350 o->mot_ioepoch_count--;
352 tmp_ma = &info->mti_u.som.attr;
353 tmp_ma->ma_lmm = info->mti_attr.ma_lmm;
354 tmp_ma->ma_lmm_size = info->mti_attr.ma_lmm_size;
355 tmp_ma->ma_som = &info->mti_u.som.data;
356 tmp_ma->ma_need = MA_INODE | MA_LOV | MA_SOM;
357 tmp_ma->ma_valid = 0;
358 rc = mdt_attr_get_complex(info, o, tmp_ma);
362 /* Check the on-disk SOM state. */
363 if (o->mot_flags & MOF_SOM_RECOV)
365 else if (!(o->mot_flags & MOF_SOM_CREATED) &&
366 !(tmp_ma->ma_valid & MA_SOM))
369 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
370 o->mot_ioepoch, PFID(mdt_object_fid(o)), o->mot_ioepoch_count);
372 opened = mdt_ioepoch_opened(o);
374 * If IOEpoch is not opened, check if a Size-on-MDS update is needed.
375 * Skip the check for file with no LOV or for unlink files.
377 if (!opened && tmp_ma->ma_valid & MA_LOV &&
378 !(tmp_ma->ma_valid & MA_INODE && tmp_ma->ma_attr.la_nlink == 0)) {
380 /* If some previous writer was evicted, re-ask the
381 * client for attributes. Even if attributes are
382 * provided, we cannot believe in them.
383 * Another use case is that there is no SOM cache on
384 * disk -- first access with SOM or there was an MDS
386 ret = MDT_IOEPOCH_GETATTR;
387 else if (o->mot_flags & MOF_SOM_CHANGE)
388 /* Some previous writer changed the attribute.
389 * Do not believe to the current Size-on-MDS
390 * update, re-ask client. */
391 ret = MDT_IOEPOCH_GETATTR;
392 else if (!(la->la_valid & LA_SIZE) && achange)
393 /* Attributes were changed by the last writer
394 * only but no Size-on-MDS update is received.*/
395 ret = MDT_IOEPOCH_GETATTR;
398 if (achange || ret == MDT_IOEPOCH_GETATTR)
399 o->mot_flags |= MOF_SOM_CHANGE;
401 /* If epoch ends and relable SOM attributes are obtained, update them.
402 * Create SOM ea for new files even if there is no attributes obtained
403 * (0-length file). */
404 if (ret == MDT_IOEPOCH_CLOSED && !opened) {
405 if (achange || o->mot_flags & MOF_SOM_CREATED) {
406 LASSERT(achange || !(la->la_valid & LA_SIZE));
407 rc = mdt_som_attr_set(info, o, o->mot_ioepoch,
409 /* Avoid the following setattrs of these attributes,
410 * e.g. for atime update. */
411 info->mti_attr.ma_valid = 0;
413 mdt_object_som_enable(o, o->mot_ioepoch);
416 mutex_unlock(&o->mot_ioepoch_mutex);
417 /* If recovery is needed, tell the client to perform GETATTR under
419 if (ret == MDT_IOEPOCH_GETATTR && recovery) {
420 struct mdt_body *rep;
421 rep = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
422 rep->valid |= OBD_MD_FLGETATTRLOCK;
428 mutex_unlock(&o->mot_ioepoch_mutex);
433 * Close IOEpoch (opened file or MDS_FMODE_EPOCH state). It happens if:
434 * - a client closes the IOEpoch;
435 * - a client eviction occured.
437 * MDT_IOEPOCH_OPENED if the client does not close IOEpoch.
438 * MDT_IOEPOCH_CLOSED if the client closes IOEpoch.
439 * MDT_IOEPOCH_GETATTR if the client closes IOEpoch but another SOM attribute
442 static int mdt_ioepoch_close(struct mdt_thread_info *info, struct mdt_object *o)
444 struct ptlrpc_request *req = mdt_info_req(info);
447 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
448 !S_ISREG(lu_object_attr(&o->mot_obj)))
451 LASSERT(o->mot_ioepoch_count);
452 LASSERT(info->mti_ioepoch == NULL ||
453 info->mti_ioepoch->ioepoch == o->mot_ioepoch);
455 /* IOEpoch is closed only if client tells about it or eviction occures.
456 * In the replay case, always close the epoch. */
458 RETURN(mdt_ioepoch_close_on_eviction(info, o));
459 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
460 RETURN(mdt_ioepoch_close_on_replay(info, o));
461 if (info->mti_ioepoch && (info->mti_ioepoch->flags & MF_EPOCH_CLOSE))
462 RETURN(mdt_ioepoch_close_reg(info, o));
463 /* IO epoch is not closed. */
464 RETURN(MDT_IOEPOCH_OPENED);
468 * Close MDS_FMODE_SOM state, when IOEpoch is already closed and we are waiting
469 * for attribute update. It happens if:
470 * - SOM Attribute Update is obtained;
471 * - the client failed to obtain it and informs MDS about it;
472 * - a client eviction occured.
473 * Apply obtained attributes for the 1st case, wipe out the on-disk SOM
476 int mdt_som_au_close(struct mdt_thread_info *info, struct mdt_object *o)
478 struct ptlrpc_request *req = mdt_info_req(info);
480 int act = MDT_SOM_ENABLE;
484 LASSERT(!req || info->mti_ioepoch);
485 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
486 !S_ISREG(lu_object_attr(&o->mot_obj)))
489 /* No size whereas MF_SOM_CHANGE is set means client failed to
490 * obtain ost attributes, drop the SOM cache on disk if so. */
492 (info->mti_ioepoch &&
493 info->mti_ioepoch->flags & MF_SOM_CHANGE &&
494 !(info->mti_attr.ma_attr.la_valid & LA_SIZE)))
495 act = MDT_SOM_DISABLE;
497 mutex_lock(&o->mot_ioepoch_mutex);
498 /* Mark the object it is the recovery state if we failed to obtain
500 if (act == MDT_SOM_DISABLE)
501 o->mot_flags |= MOF_SOM_RECOV;
503 if (!mdt_ioepoch_opened(o)) {
504 ioepoch = info->mti_ioepoch ?
505 info->mti_ioepoch->ioepoch : o->mot_ioepoch;
508 && !(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
509 rc = mdt_som_attr_set(info, o, ioepoch, act);
510 mdt_object_som_enable(o, ioepoch);
512 mutex_unlock(&o->mot_ioepoch_mutex);
516 int mdt_write_read(struct mdt_object *o)
520 mutex_lock(&o->mot_ioepoch_mutex);
521 rc = o->mot_writecount;
522 mutex_unlock(&o->mot_ioepoch_mutex);
526 int mdt_write_get(struct mdt_object *o)
530 mutex_lock(&o->mot_ioepoch_mutex);
531 if (o->mot_writecount < 0)
535 mutex_unlock(&o->mot_ioepoch_mutex);
539 void mdt_write_put(struct mdt_object *o)
542 mutex_lock(&o->mot_ioepoch_mutex);
544 mutex_unlock(&o->mot_ioepoch_mutex);
548 static int mdt_write_deny(struct mdt_object *o)
552 mutex_lock(&o->mot_ioepoch_mutex);
553 if (o->mot_writecount > 0)
557 mutex_unlock(&o->mot_ioepoch_mutex);
561 static void mdt_write_allow(struct mdt_object *o)
564 mutex_lock(&o->mot_ioepoch_mutex);
566 mutex_unlock(&o->mot_ioepoch_mutex);
570 /* there can be no real transaction so prepare the fake one */
571 static void mdt_empty_transno(struct mdt_thread_info *info, int rc)
573 struct mdt_device *mdt = info->mti_mdt;
574 struct ptlrpc_request *req = mdt_info_req(info);
575 struct tg_export_data *ted;
576 struct lsd_client_data *lcd;
579 /* transaction has occurred already */
580 if (lustre_msg_get_transno(req->rq_repmsg) != 0)
583 spin_lock(&mdt->mdt_lut.lut_translock);
585 if (info->mti_transno != 0) {
586 struct obd_export *exp = req->rq_export;
588 CERROR("%s: replay trans "LPU64" NID %s: rc = %d\n",
589 mdt_obd_name(mdt), info->mti_transno,
590 libcfs_nid2str(exp->exp_connection->c_peer.nid),
592 spin_unlock(&mdt->mdt_lut.lut_translock);
595 } else if (info->mti_transno == 0) {
596 info->mti_transno = ++mdt->mdt_lut.lut_last_transno;
598 /* should be replay */
599 if (info->mti_transno > mdt->mdt_lut.lut_last_transno)
600 mdt->mdt_lut.lut_last_transno = info->mti_transno;
602 spin_unlock(&mdt->mdt_lut.lut_translock);
604 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
606 req->rq_export->exp_obd->obd_last_committed);
608 req->rq_transno = info->mti_transno;
609 lustre_msg_set_transno(req->rq_repmsg, info->mti_transno);
611 /* update lcd in memory only for resent cases */
612 ted = &req->rq_export->exp_target_data;
614 mutex_lock(&ted->ted_lcd_lock);
616 if (info->mti_transno < lcd->lcd_last_transno &&
617 info->mti_transno != 0) {
618 /* This should happen during replay. Do not update
619 * last rcvd info if replay req transno < last transno,
620 * otherwise the following resend(after replay) can not
621 * be checked correctly by xid */
622 mutex_unlock(&ted->ted_lcd_lock);
623 CDEBUG(D_HA, "%s: transno = "LPU64" < last_transno = "LPU64"\n",
624 mdt_obd_name(mdt), info->mti_transno,
625 lcd->lcd_last_transno);
629 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
630 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
631 if (info->mti_transno != 0)
632 lcd->lcd_last_close_transno = info->mti_transno;
633 lcd->lcd_last_close_xid = req->rq_xid;
634 lcd->lcd_last_close_result = rc;
636 /* VBR: save versions in last_rcvd for reconstruct. */
637 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
639 lcd->lcd_pre_versions[0] = pre_versions[0];
640 lcd->lcd_pre_versions[1] = pre_versions[1];
641 lcd->lcd_pre_versions[2] = pre_versions[2];
642 lcd->lcd_pre_versions[3] = pre_versions[3];
644 if (info->mti_transno != 0)
645 lcd->lcd_last_transno = info->mti_transno;
647 lcd->lcd_last_xid = req->rq_xid;
648 lcd->lcd_last_result = rc;
649 lcd->lcd_last_data = info->mti_opdata;
651 mutex_unlock(&ted->ted_lcd_lock);
656 void mdt_mfd_set_mode(struct mdt_file_data *mfd, __u64 mode)
658 LASSERT(mfd != NULL);
660 CDEBUG(D_HA, DFID " Change mfd mode "LPO64" -> "LPO64".\n",
661 PFID(mdt_object_fid(mfd->mfd_object)), mfd->mfd_mode, mode);
663 mfd->mfd_mode = mode;
667 * prep ma_lmm/ma_lmv for md_attr from reply
669 void mdt_prep_ma_buf_from_rep(struct mdt_thread_info *info,
670 struct mdt_object *obj,
673 LASSERT(ma->ma_lmv == NULL && ma->ma_lmm == NULL);
674 if (S_ISDIR(obj->mot_header.loh_attr)) {
675 ma->ma_lmv = req_capsule_server_get(info->mti_pill,
677 ma->ma_lmv_size = req_capsule_get_size(info->mti_pill,
680 if (ma->ma_lmv_size > 0)
681 ma->ma_need |= MA_LMV;
683 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
685 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
688 if (ma->ma_lmm_size > 0)
689 ma->ma_need |= MA_LOV;
693 static int mdt_mfd_open(struct mdt_thread_info *info, struct mdt_object *p,
694 struct mdt_object *o, __u64 flags, int created,
695 struct ldlm_reply *rep)
697 struct ptlrpc_request *req = mdt_info_req(info);
698 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
699 struct mdt_file_data *mfd;
700 struct md_attr *ma = &info->mti_attr;
701 struct lu_attr *la = &ma->ma_attr;
702 struct mdt_body *repbody;
703 int rc = 0, isdir, isreg;
706 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
708 isreg = S_ISREG(la->la_mode);
709 isdir = S_ISDIR(la->la_mode);
710 if (isreg && !(ma->ma_valid & MA_LOV) && !(flags & MDS_OPEN_RELEASE)) {
712 * No EA, check whether it is will set regEA and dirEA since in
713 * above attr get, these size might be zero, so reset it, to
714 * retrieve the MD after create obj.
716 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
719 /* in replay case, p == NULL */
720 rc = mdt_create_data(info, p, o);
724 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_DISP_STRIPE)
725 mdt_set_disposition(info, rep, DISP_OPEN_STRIPE);
728 CDEBUG(D_INODE, "after open, ma_valid bit = "LPX64" lmm_size = %d\n",
729 ma->ma_valid, ma->ma_lmm_size);
731 if (ma->ma_valid & MA_LOV) {
732 LASSERT(ma->ma_lmm_size != 0);
733 repbody->eadatasize = ma->ma_lmm_size;
735 repbody->valid |= OBD_MD_FLDIREA;
737 repbody->valid |= OBD_MD_FLEASIZE;
740 if (ma->ma_valid & MA_LMV) {
741 LASSERT(ma->ma_lmv_size != 0);
742 repbody->eadatasize = ma->ma_lmv_size;
744 repbody->valid |= OBD_MD_FLDIREA | OBD_MD_MEA;
747 if (flags & FMODE_WRITE) {
748 rc = mdt_write_get(o);
750 mdt_ioepoch_open(info, o, created);
751 repbody->ioepoch = o->mot_ioepoch;
753 } else if (flags & MDS_FMODE_EXEC) {
754 rc = mdt_write_deny(o);
759 rc = mo_open(info->mti_env, mdt_object_child(o),
760 created ? flags | MDS_OPEN_CREATED : flags);
764 mfd = mdt_mfd_new(med);
766 GOTO(err_out, rc = -ENOMEM);
769 * Keep a reference on this object for this open, and is
770 * released by mdt_mfd_close().
772 mdt_object_get(info->mti_env, o);
774 mfd->mfd_xid = req->rq_xid;
777 * @flags is always not zero. At least it should be FMODE_READ,
778 * FMODE_WRITE or MDS_FMODE_EXEC.
783 mdt_mfd_set_mode(mfd, flags);
785 atomic_inc(&o->mot_open_count);
786 if (flags & MDS_OPEN_LEASE)
787 atomic_inc(&o->mot_lease_count);
790 if (req_is_replay(req)) {
791 struct mdt_file_data *old_mfd;
792 /* Check wheather old cookie already exist in
793 * the list, becasue when do recovery, client
794 * might be disconnected from server, and
795 * restart replay, so there maybe some orphan
796 * mfd here, we should remove them */
797 LASSERT(info->mti_rr.rr_handle != NULL);
798 spin_lock(&med->med_open_lock);
799 old_mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle, true);
800 if (old_mfd != NULL) {
801 CDEBUG(D_HA, "delete orphan mfd = %p, fid = "DFID", "
802 "cookie = "LPX64"\n", mfd,
803 PFID(mdt_object_fid(mfd->mfd_object)),
804 info->mti_rr.rr_handle->cookie);
805 class_handle_unhash(&old_mfd->mfd_handle);
806 cfs_list_del_init(&old_mfd->mfd_list);
807 spin_unlock(&med->med_open_lock);
808 /* no attr update for that close */
810 ma->ma_valid |= MA_FLAGS;
811 ma->ma_attr_flags |= MDS_RECOV_OPEN;
812 mdt_mfd_close(info, old_mfd);
813 ma->ma_attr_flags &= ~MDS_RECOV_OPEN;
814 ma->ma_valid &= ~MA_FLAGS;
816 spin_unlock(&med->med_open_lock);
817 CDEBUG(D_HA, "orphan mfd not found, fid = "DFID", "
818 "cookie = "LPX64"\n",
819 PFID(mdt_object_fid(mfd->mfd_object)),
820 info->mti_rr.rr_handle->cookie);
823 CDEBUG(D_HA, "Store old cookie "LPX64" in new mfd\n",
824 info->mti_rr.rr_handle->cookie);
826 mfd->mfd_old_handle.cookie = info->mti_rr.rr_handle->cookie;
829 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
831 if (req->rq_export->exp_disconnected) {
832 spin_lock(&med->med_open_lock);
833 class_handle_unhash(&mfd->mfd_handle);
834 cfs_list_del_init(&mfd->mfd_list);
835 spin_unlock(&med->med_open_lock);
836 mdt_mfd_close(info, mfd);
838 spin_lock(&med->med_open_lock);
839 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
840 spin_unlock(&med->med_open_lock);
843 mdt_empty_transno(info, rc);
848 if (flags & FMODE_WRITE)
849 /* XXX We also need to close io epoch here.
850 * See LU-1220 - green */
852 else if (flags & MDS_FMODE_EXEC)
858 int mdt_finish_open(struct mdt_thread_info *info,
859 struct mdt_object *p, struct mdt_object *o,
860 __u64 flags, int created, struct ldlm_reply *rep)
862 struct ptlrpc_request *req = mdt_info_req(info);
863 struct obd_export *exp = req->rq_export;
864 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
865 struct md_attr *ma = &info->mti_attr;
866 struct lu_attr *la = &ma->ma_attr;
867 struct mdt_file_data *mfd;
868 struct mdt_body *repbody;
870 int isreg, isdir, islnk;
874 LASSERT(ma->ma_valid & MA_INODE);
876 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
878 isreg = S_ISREG(la->la_mode);
879 isdir = S_ISDIR(la->la_mode);
880 islnk = S_ISLNK(la->la_mode);
881 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
883 /* LU-2275, simulate broken behaviour (esp. prevalent in
884 * pre-2.4 servers where a very strange reply is sent on error
885 * that looks like it was actually almost succesful and a failure at the
887 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_NEGATIVE_POSITIVE)) {
888 mdt_set_disposition(info, rep, DISP_OPEN_OPEN |
892 if (flags & MDS_OPEN_LOCK)
893 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
898 if (exp_connect_rmtclient(exp)) {
899 void *buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
901 rc = mdt_pack_remote_perm(info, o, buf);
903 repbody->valid &= ~OBD_MD_FLRMTPERM;
904 repbody->aclsize = 0;
906 repbody->valid |= OBD_MD_FLRMTPERM;
907 repbody->aclsize = sizeof(struct mdt_remote_perm);
910 #ifdef CONFIG_FS_POSIX_ACL
911 else if (exp_connect_flags(exp) & OBD_CONNECT_ACL) {
912 const struct lu_env *env = info->mti_env;
913 struct md_object *next = mdt_object_child(o);
914 struct lu_buf *buf = &info->mti_buf;
916 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
917 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
919 if (buf->lb_len > 0) {
920 rc = mo_xattr_get(env, next, buf,
921 XATTR_NAME_ACL_ACCESS);
923 if (rc == -ENODATA) {
924 repbody->aclsize = 0;
925 repbody->valid |= OBD_MD_FLACL;
927 } else if (rc == -EOPNOTSUPP) {
930 CERROR("got acl size: %d\n", rc);
933 repbody->aclsize = rc;
934 repbody->valid |= OBD_MD_FLACL;
941 if (info->mti_mdt->mdt_lut.lut_mds_capa &&
942 exp_connect_flags(exp) & OBD_CONNECT_MDS_CAPA) {
943 struct lustre_capa *capa;
945 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
947 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
948 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
951 repbody->valid |= OBD_MD_FLMDSCAPA;
953 if (info->mti_mdt->mdt_lut.lut_oss_capa &&
954 exp_connect_flags(exp) & OBD_CONNECT_OSS_CAPA &&
955 S_ISREG(lu_object_attr(&o->mot_obj))) {
956 struct lustre_capa *capa;
958 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
960 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | capa_open_opc(flags);
961 rc = mo_capa_get(info->mti_env, mdt_object_child(o), capa, 0);
964 repbody->valid |= OBD_MD_FLOSSCAPA;
968 * If we are following a symlink, don't open; and do not return open
969 * handle for special nodes as client required.
971 if (islnk || (!isreg && !isdir &&
972 (exp_connect_flags(req->rq_export) & OBD_CONNECT_NODEVOH))) {
973 lustre_msg_set_transno(req->rq_repmsg, 0);
978 * We need to return the existing object's fid back, so it is done here,
979 * after preparing the reply.
981 if (!created && (flags & MDS_OPEN_EXCL) && (flags & MDS_OPEN_CREAT))
984 /* This can't be done earlier, we need to return reply body */
986 if (flags & (MDS_OPEN_CREAT | FMODE_WRITE)) {
987 /* We are trying to create or write an existing dir. */
990 } else if (flags & MDS_OPEN_DIRECTORY)
993 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_OPEN_CREATE,
994 OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE)) {
999 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1000 spin_lock(&med->med_open_lock);
1001 cfs_list_for_each(t, &med->med_open_head) {
1002 mfd = cfs_list_entry(t, struct mdt_file_data, mfd_list);
1003 if (mfd->mfd_xid == req->rq_xid)
1007 spin_unlock(&med->med_open_lock);
1010 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1011 /*set repbody->ea_size for resent case*/
1012 if (ma->ma_valid & MA_LOV) {
1013 LASSERT(ma->ma_lmm_size != 0);
1014 repbody->eadatasize = ma->ma_lmm_size;
1016 repbody->valid |= OBD_MD_FLDIREA;
1018 repbody->valid |= OBD_MD_FLEASIZE;
1020 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
1025 rc = mdt_mfd_open(info, p, o, flags, created, rep);
1027 mdt_set_disposition(info, rep, DISP_OPEN_OPEN);
1032 extern void mdt_req_from_lcd(struct ptlrpc_request *req,
1033 struct lsd_client_data *lcd);
1035 void mdt_reconstruct_open(struct mdt_thread_info *info,
1036 struct mdt_lock_handle *lhc)
1038 const struct lu_env *env = info->mti_env;
1039 struct mdt_device *mdt = info->mti_mdt;
1040 struct req_capsule *pill = info->mti_pill;
1041 struct ptlrpc_request *req = mdt_info_req(info);
1042 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1043 struct lsd_client_data *lcd = ted->ted_lcd;
1044 struct md_attr *ma = &info->mti_attr;
1045 struct mdt_reint_record *rr = &info->mti_rr;
1046 __u64 flags = info->mti_spec.sp_cr_flags;
1047 struct ldlm_reply *ldlm_rep;
1048 struct mdt_object *parent;
1049 struct mdt_object *child;
1050 struct mdt_body *repbody;
1054 LASSERT(pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1055 ldlm_rep = req_capsule_server_get(pill, &RMF_DLM_REP);
1056 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1058 ma->ma_need = MA_INODE | MA_HSM;
1061 mdt_req_from_lcd(req, lcd);
1062 mdt_set_disposition(info, ldlm_rep, lcd->lcd_last_data);
1064 CDEBUG(D_INODE, "This is reconstruct open: disp="LPX64", result=%d\n",
1065 ldlm_rep->lock_policy_res1, req->rq_status);
1067 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE) &&
1068 req->rq_status != 0)
1069 /* We did not create successfully, return error to client. */
1070 GOTO(out, rc = req->rq_status);
1072 if (mdt_get_disposition(ldlm_rep, DISP_OPEN_CREATE)) {
1073 struct obd_export *exp = req->rq_export;
1075 * We failed after creation, but we do not know in which step
1076 * we failed. So try to check the child object.
1078 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1079 if (IS_ERR(parent)) {
1080 rc = PTR_ERR(parent);
1081 LCONSOLE_WARN("Parent "DFID" lookup error %d."
1082 " Evicting client %s with export %s.\n",
1083 PFID(rr->rr_fid1), rc,
1084 obd_uuid2str(&exp->exp_client_uuid),
1085 obd_export_nid2str(exp));
1086 mdt_export_evict(exp);
1089 child = mdt_object_find(env, mdt, rr->rr_fid2);
1090 if (IS_ERR(child)) {
1091 rc = PTR_ERR(child);
1092 LCONSOLE_WARN("Child "DFID" lookup error %d."
1093 " Evicting client %s with export %s.\n",
1094 PFID(mdt_object_fid(child)), rc,
1095 obd_uuid2str(&exp->exp_client_uuid),
1096 obd_export_nid2str(exp));
1097 mdt_object_put(env, parent);
1098 mdt_export_evict(exp);
1102 if (unlikely(mdt_object_remote(child))) {
1103 /* the child object was created on remote server */
1104 if (!mdt_is_dne_client(exp)) {
1105 /* Return -EIO for old client */
1106 mdt_object_put(env, parent);
1107 mdt_object_put(env, child);
1108 GOTO(out, rc = -EIO);
1110 repbody->fid1 = *rr->rr_fid2;
1111 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1114 if (mdt_object_exists(child)) {
1115 mdt_set_capainfo(info, 1, rr->rr_fid2,
1117 mdt_prep_ma_buf_from_rep(info, child, ma);
1118 rc = mdt_attr_get_complex(info, child, ma);
1120 rc = mdt_finish_open(info, parent,
1124 /* the child does not exist, we should do
1126 mdt_object_put(env, parent);
1127 mdt_object_put(env, child);
1128 GOTO(regular_open, 0);
1131 mdt_object_put(env, parent);
1132 mdt_object_put(env, child);
1136 /* We did not try to create, so we are a pure open */
1137 rc = mdt_reint_open(info, lhc);
1142 req->rq_status = rc;
1143 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1144 LASSERT(ergo(rc < 0, lustre_msg_get_transno(req->rq_repmsg) == 0));
1147 int mdt_open_by_fid(struct mdt_thread_info *info, struct ldlm_reply *rep)
1149 __u64 flags = info->mti_spec.sp_cr_flags;
1150 struct mdt_reint_record *rr = &info->mti_rr;
1151 struct md_attr *ma = &info->mti_attr;
1152 struct mdt_object *o;
1156 o = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1158 RETURN(rc = PTR_ERR(o));
1160 if (unlikely(mdt_object_remote(o))) {
1161 /* the child object was created on remote server */
1162 struct mdt_body *repbody;
1164 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1167 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1168 repbody->fid1 = *rr->rr_fid2;
1169 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1172 if (mdt_object_exists(o)) {
1173 mdt_set_disposition(info, rep, (DISP_IT_EXECD |
1176 mdt_prep_ma_buf_from_rep(info, o, ma);
1177 rc = mdt_attr_get_complex(info, o, ma);
1179 rc = mdt_finish_open(info, NULL, o, flags, 0,
1186 mdt_object_put(info->mti_env, o);
1190 /* lock object for open */
1191 static int mdt_object_open_lock(struct mdt_thread_info *info,
1192 struct mdt_object *obj,
1193 struct mdt_lock_handle *lhc,
1196 struct md_attr *ma = &info->mti_attr;
1197 __u64 open_flags = info->mti_spec.sp_cr_flags;
1198 ldlm_mode_t lm = LCK_CR;
1199 bool acq_lease = !!(open_flags & MDS_OPEN_LEASE);
1200 bool try_layout = false;
1201 bool create_layout = false;
1206 mdt_lock_handle_init(lhc);
1208 if (req_is_replay(mdt_info_req(info)))
1211 if (S_ISREG(lu_object_attr(&obj->mot_obj))) {
1212 if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV) &&
1213 md_should_create(open_flags))
1214 create_layout = true;
1215 if (exp_connect_layout(info->mti_exp) && !create_layout &&
1216 ma->ma_need & MA_LOV)
1221 /* lease open, acquire write mode of open sem */
1222 down_write(&obj->mot_open_sem);
1224 /* Lease exists and ask for new lease */
1225 if (atomic_read(&obj->mot_lease_count) > 0) {
1226 /* only exclusive open is supported, so lease
1227 * are conflicted to each other */
1228 GOTO(out, rc = -EBUSY);
1231 /* Lease must be with open lock */
1232 if (!(open_flags & MDS_OPEN_LOCK)) {
1233 CERROR("Request lease for file:"DFID ", but open lock "
1234 "is missed, open_flags = "LPO64".\n",
1235 PFID(mdt_object_fid(obj)), open_flags);
1236 GOTO(out, rc = -EPROTO);
1239 /* XXX: only exclusive open is supported. */
1241 *ibits = MDS_INODELOCK_OPEN;
1243 /* never grant LCK_EX layout lock to client */
1245 } else { /* normal open */
1246 /* normal open holds read mode of open sem */
1247 down_read(&obj->mot_open_sem);
1249 if (open_flags & MDS_OPEN_LOCK) {
1250 if (open_flags & FMODE_WRITE)
1252 else if (open_flags & MDS_FMODE_EXEC)
1257 *ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN;
1258 } else if (atomic_read(&obj->mot_lease_count) > 0) {
1259 if (open_flags & FMODE_WRITE)
1265 *ibits = MDS_INODELOCK_OPEN;
1268 lhc = &info->mti_lh[MDT_LH_LOCAL];
1270 CDEBUG(D_INODE, "normal open:"DFID" lease count: %d, lm: %d\n",
1271 PFID(mdt_object_fid(obj)),
1272 atomic_read(&obj->mot_open_count), lm);
1275 mdt_lock_reg_init(lhc, lm);
1277 /* one problem to return layout lock on open is that it may result
1278 * in too many layout locks cached on the client side. */
1279 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_OPEN) && try_layout) {
1280 /* return lookup lock to validate inode at the client side,
1281 * this is pretty important otherwise mdt will return layout
1282 * lock for each open.
1283 * However this is a double-edged sword because changing
1284 * permission will revoke huge # of LOOKUP locks. */
1285 *ibits |= MDS_INODELOCK_LAYOUT | MDS_INODELOCK_LOOKUP;
1286 if (!mdt_object_lock_try(info, obj, lhc, *ibits,
1288 *ibits &= ~(MDS_INODELOCK_LAYOUT|MDS_INODELOCK_LOOKUP);
1290 rc = mdt_object_lock(info, obj, lhc, *ibits,
1293 } else if (*ibits != 0) {
1294 rc = mdt_object_lock(info, obj, lhc, *ibits, MDT_CROSS_LOCK);
1297 CDEBUG(D_INODE, "Requested bits lock:"DFID ", ibits = "LPX64
1298 ", open_flags = "LPO64", try_layout = %d, rc = %d\n",
1299 PFID(mdt_object_fid(obj)), *ibits, open_flags, try_layout, rc);
1301 /* will change layout, revoke layout locks by enqueuing EX lock. */
1302 if (rc == 0 && create_layout) {
1303 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LAYOUT];
1305 CDEBUG(D_INODE, "Will create layout, get EX layout lock:"DFID
1306 ", open_flags = "LPO64"\n",
1307 PFID(mdt_object_fid(obj)), open_flags);
1309 /* We cannot enqueue another lock for the same resource we
1310 * already have a lock for, due to mechanics of waiting list
1311 * iterating in ldlm, see LU-3601.
1312 * As such we'll drop the open lock we just got above here,
1313 * it's ok not to have this open lock as it's main purpose is to
1314 * flush unused cached client open handles. */
1315 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1316 mdt_object_unlock(info, obj, lhc, 1);
1318 LASSERT(!try_layout);
1319 mdt_lock_handle_init(ll);
1320 mdt_lock_reg_init(ll, LCK_EX);
1321 rc = mdt_object_lock(info, obj, ll, MDS_INODELOCK_LAYOUT,
1324 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LL_BLOCK, 2);
1327 /* Check if there is any other open handles after acquiring
1328 * open lock. At this point, caching open handles have been revoked
1330 * XXX: Now only exclusive open is supported. Need to check the
1331 * type of open for generic lease support. */
1332 if (rc == 0 && acq_lease) {
1333 struct ptlrpc_request *req = mdt_info_req(info);
1334 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1335 struct mdt_file_data *mfd;
1336 bool is_replay_or_resent;
1339 /* For lease: application can open a file and then apply lease,
1340 * @handle contains original open handle in that case.
1341 * In recovery, open REQ will be replayed and the lease REQ may
1342 * be resent that means the open handle is already stale, so we
1343 * need to fix it up here by finding new handle. */
1344 is_replay_or_resent = req_is_replay(req) ||
1345 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT;
1347 /* if the request is _not_ a replay request, rr_handle
1348 * may be used to hold an openhandle which is issuing the
1349 * lease request, so that this openhandle doesn't count. */
1350 mfd = mdt_handle2mfd(med, info->mti_rr.rr_handle,
1351 is_replay_or_resent);
1355 CDEBUG(D_INODE, "acq_lease "DFID": openers: %d, want: %d\n",
1356 PFID(mdt_object_fid(obj)),
1357 atomic_read(&obj->mot_open_count), open_count);
1359 if (atomic_read(&obj->mot_open_count) > open_count)
1360 GOTO(out, rc = -EBUSY);
1368 static void mdt_object_open_unlock(struct mdt_thread_info *info,
1369 struct mdt_object *obj,
1370 struct mdt_lock_handle *lhc,
1371 __u64 ibits, int rc)
1373 __u64 open_flags = info->mti_spec.sp_cr_flags;
1374 struct mdt_lock_handle *ll = &info->mti_lh[MDT_LH_LOCAL];
1377 if (req_is_replay(mdt_info_req(info)))
1380 /* Release local lock - the lock put in MDT_LH_LOCAL will never
1381 * return to client side. */
1382 if (lustre_handle_is_used(&ll->mlh_reg_lh))
1383 mdt_object_unlock(info, obj, ll, 1);
1385 ll = &info->mti_lh[MDT_LH_LAYOUT];
1386 /* Release local layout lock, layout was created */
1387 if (lustre_handle_is_used(&ll->mlh_reg_lh)) {
1388 LASSERT(!(ibits & MDS_INODELOCK_LAYOUT));
1389 mdt_object_unlock(info, obj, ll, 1);
1392 if (open_flags & MDS_OPEN_LEASE)
1393 up_write(&obj->mot_open_sem);
1395 up_read(&obj->mot_open_sem);
1397 /* Cross-ref case, the lock should be returned to the client */
1398 if (ibits == 0 || rc == -EREMOTE)
1401 if (!(open_flags & MDS_OPEN_LOCK) && !(ibits & MDS_INODELOCK_LAYOUT)) {
1402 /* for the open request, the lock will only return to client
1403 * if open or layout lock is granted. */
1407 if (rc != 0 || !lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1408 struct ldlm_reply *ldlm_rep;
1410 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1411 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1412 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
1413 mdt_object_unlock(info, obj, lhc, 1);
1419 * Check release is permitted for the current HSM flags.
1421 static bool mdt_hsm_release_allow(const struct md_attr *ma)
1423 if (!(ma->ma_valid & MA_HSM))
1426 if (ma->ma_hsm.mh_flags & (HS_DIRTY|HS_NORELEASE|HS_LOST))
1429 if (!(ma->ma_hsm.mh_flags & HS_ARCHIVED))
1435 int mdt_open_by_fid_lock(struct mdt_thread_info *info, struct ldlm_reply *rep,
1436 struct mdt_lock_handle *lhc)
1438 const struct lu_env *env = info->mti_env;
1439 struct mdt_device *mdt = info->mti_mdt;
1440 __u64 flags = info->mti_spec.sp_cr_flags;
1441 struct mdt_reint_record *rr = &info->mti_rr;
1442 struct md_attr *ma = &info->mti_attr;
1443 struct mdt_object *parent= NULL;
1444 struct mdt_object *o;
1449 if (md_should_create(flags) && !(flags & MDS_OPEN_HAS_EA)) {
1450 if (!lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1451 parent = mdt_object_find(env, mdt, rr->rr_fid1);
1452 if (IS_ERR(parent)) {
1453 CDEBUG(D_INODE, "Fail to find parent "DFID
1454 " for anonymous created %ld, try to"
1455 " use server-side parent.\n",
1456 PFID(rr->rr_fid1), PTR_ERR(parent));
1461 ma->ma_need |= MA_PFID;
1464 o = mdt_object_find(env, mdt, rr->rr_fid2);
1466 RETURN(rc = PTR_ERR(o));
1468 if (mdt_object_remote(o)) {
1469 CDEBUG(D_INFO, "%s: "DFID" is on remote MDT.\n",
1470 mdt_obd_name(info->mti_mdt),
1472 GOTO(out, rc = -EREMOTE);
1473 } else if (!mdt_object_exists(o)) {
1474 mdt_set_disposition(info, rep,
1478 GOTO(out, rc = -ENOENT);
1481 mdt_set_disposition(info, rep, (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1483 mdt_prep_ma_buf_from_rep(info, o, ma);
1484 if (flags & MDS_OPEN_RELEASE)
1485 ma->ma_need |= MA_HSM;
1486 rc = mdt_attr_get_complex(info, o, ma);
1490 /* If a release request, check file flags are fine and ask for an
1491 * exclusive open access. */
1492 if (flags & MDS_OPEN_RELEASE && !mdt_hsm_release_allow(ma))
1493 GOTO(out, rc = -EPERM);
1495 rc = mdt_object_open_lock(info, o, lhc, &ibits);
1497 GOTO(out_unlock, rc);
1499 if (ma->ma_valid & MA_PFID) {
1500 parent = mdt_object_find(env, mdt, &ma->ma_pfid);
1501 if (IS_ERR(parent)) {
1502 CDEBUG(D_INODE, "Fail to find parent "DFID
1503 " for anonymous created %ld, try to"
1504 " use system default.\n",
1505 PFID(&ma->ma_pfid), PTR_ERR(parent));
1510 rc = mdt_finish_open(info, parent, o, flags, 0, rep);
1512 mdt_set_disposition(info, rep, DISP_LOOKUP_POS);
1513 if (flags & MDS_OPEN_LOCK)
1514 mdt_set_disposition(info, rep, DISP_OPEN_LOCK);
1515 if (flags & MDS_OPEN_LEASE)
1516 mdt_set_disposition(info, rep, DISP_OPEN_LEASE);
1518 GOTO(out_unlock, rc);
1521 mdt_object_open_unlock(info, o, lhc, ibits, rc);
1523 mdt_object_put(env, o);
1525 mdt_object_put(env, parent);
1529 /* Cross-ref request. Currently it can only be a pure open (w/o create) */
1530 static int mdt_cross_open(struct mdt_thread_info *info,
1531 const struct lu_fid *parent_fid,
1532 const struct lu_fid *fid,
1533 struct ldlm_reply *rep, __u32 flags)
1535 struct md_attr *ma = &info->mti_attr;
1536 struct mdt_object *o;
1540 o = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1542 RETURN(rc = PTR_ERR(o));
1544 if (mdt_object_remote(o)) {
1545 /* Something is wrong here, the object is on another MDS! */
1546 CERROR("%s: "DFID" isn't on this server!: rc = %d\n",
1547 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1548 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
1550 "Object isn't on this server! FLD error?\n");
1553 if (mdt_object_exists(o)) {
1554 /* Do permission check for cross-open. */
1555 rc = mo_permission(info->mti_env, NULL,
1556 mdt_object_child(o),
1557 NULL, flags | MDS_OPEN_CROSS);
1561 mdt_prep_ma_buf_from_rep(info, o, ma);
1562 mdt_set_capainfo(info, 0, fid, BYPASS_CAPA);
1563 rc = mdt_attr_get_complex(info, o, ma);
1567 /* Do not create lov object if the fid is opened
1569 if (S_ISREG(ma->ma_attr.la_mode) &&
1570 !(ma->ma_valid & MA_LOV) && (flags & FMODE_WRITE) &&
1571 fid_is_obf(parent_fid))
1572 GOTO(out, rc = -EPERM);
1574 rc = mdt_finish_open(info, NULL, o, flags, 0, rep);
1577 * Something is wrong here. lookup was positive but
1578 * there is no object!
1580 CERROR("%s: "DFID" doesn't exist!: rc = %d\n",
1581 mdt_obd_name(info->mti_mdt), PFID(fid), -EFAULT);
1586 mdt_object_put(info->mti_env, o);
1590 int mdt_reint_open(struct mdt_thread_info *info, struct mdt_lock_handle *lhc)
1592 struct mdt_device *mdt = info->mti_mdt;
1593 struct ptlrpc_request *req = mdt_info_req(info);
1594 struct mdt_object *parent;
1595 struct mdt_object *child;
1596 struct mdt_lock_handle *lh;
1597 struct ldlm_reply *ldlm_rep;
1598 struct mdt_body *repbody;
1599 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1600 struct md_attr *ma = &info->mti_attr;
1601 __u64 create_flags = info->mti_spec.sp_cr_flags;
1603 struct mdt_reint_record *rr = &info->mti_rr;
1609 OBD_FAIL_TIMEOUT_ORSET(OBD_FAIL_MDS_PAUSE_OPEN, OBD_FAIL_ONCE,
1610 (obd_timeout + 1) / 4);
1612 mdt_counter_incr(req, LPROC_MDT_OPEN);
1613 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1615 ma->ma_need = MA_INODE;
1618 LASSERT(info->mti_pill->rc_fmt == &RQF_LDLM_INTENT_OPEN);
1619 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
1621 if (unlikely(create_flags & MDS_OPEN_JOIN_FILE)) {
1622 CERROR("file join is not supported anymore.\n");
1623 GOTO(out, result = err_serious(-EOPNOTSUPP));
1625 msg_flags = lustre_msg_get_flags(req->rq_reqmsg);
1627 if ((create_flags & (MDS_OPEN_HAS_EA | MDS_OPEN_HAS_OBJS)) &&
1628 info->mti_spec.u.sp_ea.eadata == NULL)
1629 GOTO(out, result = err_serious(-EINVAL));
1631 CDEBUG(D_INODE, "I am going to open "DFID"/("DNAME"->"DFID") "
1632 "cr_flag="LPO64" mode=0%06o msg_flag=0x%x\n",
1633 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1634 PFID(rr->rr_fid2), create_flags,
1635 ma->ma_attr.la_mode, msg_flags);
1637 if (info->mti_cross_ref) {
1638 /* This is cross-ref open */
1639 mdt_set_disposition(info, ldlm_rep,
1640 (DISP_IT_EXECD | DISP_LOOKUP_EXECD |
1642 result = mdt_cross_open(info, rr->rr_fid2, rr->rr_fid1,
1643 ldlm_rep, create_flags);
1645 } else if (req_is_replay(req) ||
1646 (req->rq_export->exp_libclient && create_flags & MDS_OPEN_HAS_EA)) {
1647 /* This is a replay request or from liblustre with ea. */
1648 result = mdt_open_by_fid(info, ldlm_rep);
1650 if (result != -ENOENT) {
1651 if (req->rq_export->exp_libclient &&
1652 create_flags & MDS_OPEN_HAS_EA)
1653 GOTO(out, result = 0);
1656 /* We didn't find the correct object, so we need to re-create it
1657 * via a regular replay. */
1658 if (!(create_flags & MDS_OPEN_CREAT)) {
1659 DEBUG_REQ(D_ERROR, req,
1660 "OPEN & CREAT not in open replay/by_fid.");
1661 GOTO(out, result = -EFAULT);
1663 CDEBUG(D_INFO, "No object(1), continue as regular open.\n");
1664 } else if ((!lu_name_is_valid(&rr->rr_name) &&
1665 (create_flags & MDS_OPEN_LOCK)) ||
1666 (create_flags & MDS_OPEN_BY_FID)) {
1667 result = mdt_open_by_fid_lock(info, ldlm_rep, lhc);
1668 /* If result is 0 then open by FID has found the file
1669 * and there is nothing left for us to do here. More
1670 * generally if it is anything other than -ENOENT or
1671 * -EREMOTE then we return that now. If -ENOENT and
1672 * MDS_OPEN_CREAT is set then we must create the file
1673 * below. If -EREMOTE then we need to return a LOOKUP
1674 * lock to the client, which we do below. Hence this
1675 * odd looking condition. See LU-2523. */
1676 if (!(result == -ENOENT && (create_flags & MDS_OPEN_CREAT)) &&
1680 CDEBUG(D_INFO, "No object(2), continue as regular open.\n");
1683 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK))
1684 GOTO(out, result = err_serious(-ENOMEM));
1686 mdt_set_disposition(info, ldlm_rep,
1687 (DISP_IT_EXECD | DISP_LOOKUP_EXECD));
1689 if (!lu_name_is_valid(&rr->rr_name))
1690 GOTO(out, result = -EPROTO);
1692 lh = &info->mti_lh[MDT_LH_PARENT];
1693 mdt_lock_pdo_init(lh,
1694 (create_flags & MDS_OPEN_CREAT) ? LCK_PW : LCK_PR,
1697 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
1698 MDS_INODELOCK_UPDATE);
1700 GOTO(out, result = PTR_ERR(parent));
1702 /* get and check version of parent */
1703 result = mdt_version_get_check(info, parent, 0);
1705 GOTO(out_parent, result);
1707 fid_zero(child_fid);
1709 result = mdo_lookup(info->mti_env, mdt_object_child(parent),
1710 &rr->rr_name, child_fid, &info->mti_spec);
1712 LASSERTF(ergo(result == 0, fid_is_sane(child_fid)),
1713 "looking for "DFID"/"DNAME", found FID = "DFID"\n",
1714 PFID(mdt_object_fid(parent)), PNAME(&rr->rr_name),
1717 if (result != 0 && result != -ENOENT && result != -ESTALE)
1718 GOTO(out_parent, result);
1720 if (result == -ENOENT || result == -ESTALE) {
1721 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1722 if (result == -ESTALE) {
1724 * -ESTALE means the parent is a dead(unlinked) dir, so
1725 * it should return -ENOENT to in accordance with the
1726 * original mds implementaion.
1728 GOTO(out_parent, result = -ENOENT);
1730 if (!(create_flags & MDS_OPEN_CREAT))
1731 GOTO(out_parent, result);
1732 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
1733 GOTO(out_parent, result = -EROFS);
1734 *child_fid = *info->mti_rr.rr_fid2;
1735 LASSERTF(fid_is_sane(child_fid), "fid="DFID"\n",
1737 /* In the function below, .hs_keycmp resolves to
1738 * lu_obj_hop_keycmp() */
1739 /* coverity[overrun-buffer-val] */
1740 child = mdt_object_new(info->mti_env, mdt, child_fid);
1743 * Check for O_EXCL is moved to the mdt_finish_open(), we need to
1744 * return FID back in that case.
1746 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1747 child = mdt_object_find(info->mti_env, mdt, child_fid);
1750 GOTO(out_parent, result = PTR_ERR(child));
1752 /** check version of child */
1753 rc = mdt_version_get_check(info, child, 1);
1755 GOTO(out_child, result = rc);
1757 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1758 if (result == -ENOENT) {
1759 /* Create under OBF and .lustre is not permitted */
1760 if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
1761 GOTO(out_child, result = -EPERM);
1763 /* save versions in reply */
1764 mdt_version_get_save(info, parent, 0);
1765 mdt_version_get_save(info, child, 1);
1767 /* version of child will be changed */
1768 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
1770 /* Not found and with MDS_OPEN_CREAT: let's create it. */
1771 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1773 /* Let lower layers know what is lock mode on directory. */
1774 info->mti_spec.sp_cr_mode =
1775 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
1778 * Do not perform lookup sanity check. We know that name does
1781 info->mti_spec.sp_cr_lookup = 0;
1782 info->mti_spec.sp_feat = &dt_directory_features;
1784 result = mdo_create(info->mti_env,
1785 mdt_object_child(parent),
1787 mdt_object_child(child),
1790 if (result == -ERESTART) {
1791 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1792 GOTO(out_child, result);
1794 mdt_prep_ma_buf_from_rep(info, child, ma);
1795 /* XXX: we should call this once, see few lines below */
1797 result = mdt_attr_get_complex(info, child, ma);
1800 GOTO(out_child, result);
1805 * The object is on remote node, return its FID for remote open.
1807 if (mdt_object_remote(child)) {
1809 * Check if this lock already was sent to client and
1810 * this is resent case. For resent case do not take lock
1811 * again, use what is already granted.
1813 LASSERT(lhc != NULL);
1815 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1816 struct ldlm_lock *lock;
1818 LASSERT(msg_flags & MSG_RESENT);
1820 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1822 CERROR("Invalid lock handle "LPX64"\n",
1823 lhc->mlh_reg_lh.cookie);
1826 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1827 &lock->l_resource->lr_name));
1828 LDLM_LOCK_PUT(lock);
1831 mdt_lock_handle_init(lhc);
1832 mdt_lock_reg_init(lhc, LCK_PR);
1834 rc = mdt_object_lock(info, child, lhc,
1835 MDS_INODELOCK_LOOKUP,
1838 repbody->fid1 = *mdt_object_fid(child);
1839 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
1844 GOTO(out_child, result);
1845 } else if (mdt_object_exists(child)) {
1846 /* We have to get attr & LOV EA & HSM for this
1848 mdt_prep_ma_buf_from_rep(info, child, ma);
1849 ma->ma_need |= MA_HSM;
1850 result = mdt_attr_get_complex(info, child, ma);
1852 /* Object does not exist. Likely FS corruption. */
1853 CERROR("%s: name '"DNAME"' present, but FID "
1854 DFID" is invalid\n", mdt_obd_name(info->mti_mdt),
1855 PNAME(&rr->rr_name), PFID(child_fid));
1856 GOTO(out_child, result = -EIO);
1860 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1861 /* the open lock might already be gotten in
1862 * mdt_intent_fixup_resent */
1863 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1864 if (create_flags & MDS_OPEN_LOCK)
1865 mdt_set_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1867 /* get openlock if this isn't replay and client requested it */
1868 if (!req_is_replay(req)) {
1869 rc = mdt_object_open_lock(info, child, lhc, &ibits);
1871 GOTO(out_child_unlock, result = rc);
1872 else if (create_flags & MDS_OPEN_LOCK)
1873 mdt_set_disposition(info, ldlm_rep,
1877 /* Try to open it now. */
1878 rc = mdt_finish_open(info, parent, child, create_flags,
1882 /* openlock will be released if mdt_finish_open failed */
1883 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_LOCK);
1885 if (created && create_flags & MDS_OPEN_VOLATILE) {
1886 CERROR("%s: cannot open volatile file "DFID", orphan "
1887 "file will be left in PENDING directory until "
1888 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1889 PFID(mdt_object_fid(child)), rc);
1890 GOTO(out_child_unlock, result);
1896 ma->ma_cookie_size = 0;
1897 rc = mdo_unlink(info->mti_env,
1898 mdt_object_child(parent),
1899 mdt_object_child(child),
1901 &info->mti_attr, 0);
1903 CERROR("%s: "DFID" cleanup of open: rc = %d\n",
1904 mdt_obd_name(info->mti_mdt),
1905 PFID(mdt_object_fid(child)), rc);
1906 mdt_clear_disposition(info, ldlm_rep, DISP_OPEN_CREATE);
1911 mdt_object_open_unlock(info, child, lhc, ibits, result);
1913 mdt_object_put(info->mti_env, child);
1915 mdt_object_unlock_put(info, parent, lh, result || !created);
1918 lustre_msg_set_transno(req->rq_repmsg, 0);
1923 * Create an orphan object use local root.
1925 static struct mdt_object *mdt_orphan_open(struct mdt_thread_info *info,
1926 struct mdt_device *mdt,
1927 const struct lu_fid *fid,
1928 struct md_attr *attr, fmode_t fmode)
1930 const struct lu_env *env = info->mti_env;
1931 struct md_op_spec *spec = &info->mti_spec;
1932 struct lu_fid *local_root_fid = &info->mti_tmp_fid1;
1933 struct mdt_object *obj = NULL;
1934 struct mdt_object *local_root;
1935 static const struct lu_name lname = {
1936 .ln_name = "i_am_nobody",
1937 .ln_namelen = sizeof("i_am_nobody") - 1,
1939 struct lu_ucred *uc;
1940 cfs_cap_t uc_cap_save;
1944 rc = dt_root_get(env, mdt->mdt_bottom, local_root_fid);
1946 RETURN(ERR_PTR(rc));
1948 local_root = mdt_object_find(env, mdt, local_root_fid);
1949 if (IS_ERR(local_root))
1952 obj = mdt_object_new(env, mdt, fid);
1954 GOTO(out, rc = PTR_ERR(obj));
1956 spec->sp_cr_lookup = 0;
1957 spec->sp_feat = &dt_directory_features;
1958 spec->sp_cr_mode = MDL_MINMODE; /* no lock */
1959 spec->sp_cr_flags = MDS_OPEN_VOLATILE | fmode;
1960 if (attr->ma_valid & MA_LOV) {
1961 spec->u.sp_ea.eadata = attr->ma_lmm;
1962 spec->u.sp_ea.eadatalen = attr->ma_lmm_size;
1963 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1965 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
1969 uc_cap_save = uc->uc_cap;
1970 uc->uc_cap |= 1 << CFS_CAP_DAC_OVERRIDE;
1971 rc = mdo_create(env, mdt_object_child(local_root), &lname,
1972 mdt_object_child(obj), spec, attr);
1973 uc->uc_cap = uc_cap_save;
1975 CERROR("%s: cannot create volatile file "DFID": rc = %d\n",
1976 mdt_obd_name(mdt), PFID(fid), rc);
1980 rc = mo_open(env, mdt_object_child(obj), MDS_OPEN_CREATED);
1982 CERROR("%s: cannot open volatile file "DFID", orphan "
1983 "file will be left in PENDING directory until "
1984 "next reboot, rc = %d\n", mdt_obd_name(mdt),
1991 mdt_object_put(env, obj);
1994 mdt_object_put(env, local_root);
1998 static int mdt_hsm_release(struct mdt_thread_info *info, struct mdt_object *o,
2001 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LAYOUT];
2002 struct close_data *data;
2003 struct ldlm_lock *lease;
2004 struct mdt_object *orphan;
2005 struct md_attr *orp_ma;
2012 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_RDONLY)
2015 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2019 lease = ldlm_handle2lock(&data->cd_handle);
2023 /* try to hold open_sem so that nobody else can open the file */
2024 if (!down_write_trylock(&o->mot_open_sem)) {
2025 ldlm_lock_cancel(lease);
2026 GOTO(out_reprocess, rc = -EBUSY);
2029 /* Check if the lease open lease has already canceled */
2030 lock_res_and_lock(lease);
2031 lease_broken = ldlm_is_cancel(lease);
2032 unlock_res_and_lock(lease);
2034 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
2035 PFID(mdt_object_fid(o)), lease_broken);
2037 /* Cancel server side lease. Client side counterpart should
2038 * have been cancelled. It's okay to cancel it now as we've
2039 * held mot_open_sem. */
2040 ldlm_lock_cancel(lease);
2042 if (lease_broken) /* don't perform release task */
2043 GOTO(out_unlock, rc = -ESTALE);
2045 if (fid_is_zero(&data->cd_fid) || !fid_is_sane(&data->cd_fid))
2046 GOTO(out_unlock, rc = -EINVAL);
2048 /* ma_need was set before but it seems fine to change it in order to
2049 * avoid modifying the one from RPC */
2050 ma->ma_need = MA_HSM;
2051 rc = mdt_attr_get_complex(info, o, ma);
2053 GOTO(out_unlock, rc);
2055 if (!mdt_hsm_release_allow(ma))
2056 GOTO(out_unlock, rc = -EPERM);
2058 /* already released? */
2059 if (ma->ma_hsm.mh_flags & HS_RELEASED)
2060 GOTO(out_unlock, rc = 0);
2062 /* Compare on-disk and packed data_version */
2063 if (data->cd_data_version != ma->ma_hsm.mh_arch_ver) {
2064 CDEBUG(D_HSM, DFID" data_version mismatches: packed="LPU64
2065 " and on-disk="LPU64"\n", PFID(mdt_object_fid(o)),
2066 data->cd_data_version, ma->ma_hsm.mh_arch_ver);
2067 GOTO(out_unlock, rc = -EPERM);
2070 ma->ma_valid = MA_INODE;
2071 ma->ma_attr.la_valid &= LA_ATIME | LA_MTIME | LA_CTIME | LA_SIZE;
2072 rc = mo_attr_set(info->mti_env, mdt_object_child(o), ma);
2074 GOTO(out_unlock, rc);
2076 ma->ma_need = MA_INODE | MA_LOV;
2077 rc = mdt_attr_get_complex(info, o, ma);
2079 GOTO(out_unlock, rc);
2081 if (!(ma->ma_valid & MA_LOV)) {
2082 /* Even empty file are released */
2083 memset(ma->ma_lmm, 0, sizeof(*ma->ma_lmm));
2084 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2085 ma->ma_lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
2086 ma->ma_lmm->lmm_stripe_size = cpu_to_le32(LOV_MIN_STRIPE_SIZE);
2087 ma->ma_lmm_size = sizeof(*ma->ma_lmm);
2089 /* Magic must be LOV_MAGIC_Vx_DEF otherwise LOD will interpret
2090 * ma_lmm as lov_user_md, then it will be confused by union of
2091 * layout_gen and stripe_offset. */
2092 if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1)
2093 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
2094 else if (le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3)
2095 ma->ma_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
2097 GOTO(out_unlock, rc = -EINVAL);
2100 /* Set file as released */
2101 ma->ma_lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_RELEASED);
2103 /* Hopefully it's not used in this call path */
2104 orp_ma = &info->mti_u.som.attr;
2105 orp_ma->ma_attr.la_mode = S_IFREG | S_IWUSR;
2106 orp_ma->ma_attr.la_uid = ma->ma_attr.la_uid;
2107 orp_ma->ma_attr.la_gid = ma->ma_attr.la_gid;
2108 orp_ma->ma_attr.la_valid = LA_MODE | LA_UID | LA_GID;
2109 orp_ma->ma_lmm = ma->ma_lmm;
2110 orp_ma->ma_lmm_size = ma->ma_lmm_size;
2111 orp_ma->ma_valid = MA_INODE | MA_LOV;
2112 orphan = mdt_orphan_open(info, info->mti_mdt, &data->cd_fid, orp_ma,
2114 if (IS_ERR(orphan)) {
2115 CERROR("%s: cannot open orphan file "DFID": rc = %ld\n",
2116 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid),
2118 GOTO(out_unlock, rc = PTR_ERR(orphan));
2121 /* Set up HSM attribute for orphan object */
2122 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
2123 buf = &info->mti_buf;
2124 buf->lb_buf = info->mti_xattr_buf;
2125 buf->lb_len = sizeof(struct hsm_attrs);
2126 ma->ma_hsm.mh_flags |= HS_RELEASED;
2127 lustre_hsm2buf(buf->lb_buf, &ma->ma_hsm);
2128 ma->ma_hsm.mh_flags &= ~HS_RELEASED;
2130 mdt_lock_reg_init(lh, LCK_EX);
2131 rc = mdt_object_lock(info, o, lh, MDS_INODELOCK_LAYOUT |
2132 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
2134 GOTO(out_close, rc);
2136 rc = mo_xattr_set(info->mti_env, mdt_object_child(orphan), buf,
2140 /* Swap layout with orphan object */
2141 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o),
2142 mdt_object_child(orphan),
2143 SWAP_LAYOUTS_MDS_HSM);
2145 /* Release exclusive LL */
2146 mdt_object_unlock(info, o, lh, 1);
2151 /* Close orphan object anyway */
2152 rc2 = mo_close(info->mti_env, mdt_object_child(orphan), orp_ma,
2155 CERROR("%s: error closing volatile file "DFID": rc = %d\n",
2156 mdt_obd_name(info->mti_mdt), PFID(&data->cd_fid), rc2);
2157 LU_OBJECT_DEBUG(D_HSM, info->mti_env, &orphan->mot_obj,
2159 mdt_object_put(info->mti_env, orphan);
2162 up_write(&o->mot_open_sem);
2164 if (rc == 0) { /* already released */
2165 struct mdt_body *repbody;
2166 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2167 LASSERT(repbody != NULL);
2168 repbody->valid |= OBD_MD_FLRELEASED;
2172 ldlm_reprocess_all(lease->l_resource);
2173 LDLM_LOCK_PUT(lease);
2181 #define MFD_CLOSED(mode) (((mode) & ~(MDS_FMODE_EPOCH | MDS_FMODE_SOM | \
2182 MDS_FMODE_TRUNC)) == MDS_FMODE_CLOSED)
2184 static int mdt_mfd_closed(struct mdt_file_data *mfd)
2186 return ((mfd == NULL) || MFD_CLOSED(mfd->mfd_mode));
2189 int mdt_mfd_close(struct mdt_thread_info *info, struct mdt_file_data *mfd)
2191 struct mdt_object *o = mfd->mfd_object;
2192 struct md_object *next = mdt_object_child(o);
2193 struct md_attr *ma = &info->mti_attr;
2194 int ret = MDT_IOEPOCH_CLOSED;
2199 mode = mfd->mfd_mode;
2201 if (ma->ma_attr_flags & MDS_HSM_RELEASE) {
2202 rc = mdt_hsm_release(info, o, ma);
2204 CDEBUG(D_HSM, "%s: File " DFID " release failed: %d\n",
2205 mdt_obd_name(info->mti_mdt),
2206 PFID(mdt_object_fid(o)), rc);
2207 /* continue to close even error occurred. */
2211 if ((mode & FMODE_WRITE) || (mode & MDS_FMODE_TRUNC)) {
2213 ret = mdt_ioepoch_close(info, o);
2214 } else if (mode & MDS_FMODE_EXEC) {
2216 } else if (mode & MDS_FMODE_EPOCH) {
2217 ret = mdt_ioepoch_close(info, o);
2218 } else if (mode & MDS_FMODE_SOM) {
2219 ret = mdt_som_au_close(info, o);
2222 /* Update atime on close only. */
2223 if ((mode & MDS_FMODE_EXEC || mode & FMODE_READ || mode & FMODE_WRITE)
2224 && (ma->ma_valid & MA_INODE) && (ma->ma_attr.la_valid & LA_ATIME)) {
2225 /* Set the atime only. */
2226 ma->ma_valid = MA_INODE;
2227 ma->ma_attr.la_valid = LA_ATIME;
2228 rc = mo_attr_set(info->mti_env, next, ma);
2231 /* If file data is modified, add the dirty flag. */
2232 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
2233 rc = mdt_add_dirty_flag(info, o, ma);
2235 ma->ma_need |= MA_INODE;
2236 ma->ma_valid &= ~MA_INODE;
2238 if (!MFD_CLOSED(mode))
2239 rc = mo_close(info->mti_env, next, ma, mode);
2241 if (ret == MDT_IOEPOCH_GETATTR || ret == MDT_IOEPOCH_OPENED) {
2242 struct mdt_export_data *med;
2244 /* The IOepoch is still opened or SOM update is needed.
2245 * Put mfd back into the list. */
2246 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
2247 mdt_mfd_set_mode(mfd, ret == MDT_IOEPOCH_OPENED ?
2248 MDS_FMODE_EPOCH : MDS_FMODE_SOM);
2250 LASSERT(mdt_info_req(info));
2251 med = &mdt_info_req(info)->rq_export->exp_mdt_data;
2252 spin_lock(&med->med_open_lock);
2253 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
2254 class_handle_hash_back(&mfd->mfd_handle);
2255 spin_unlock(&med->med_open_lock);
2257 if (ret == MDT_IOEPOCH_OPENED) {
2261 CDEBUG(D_INODE, "Size-on-MDS attribute update is "
2262 "needed on "DFID"\n", PFID(mdt_object_fid(o)));
2265 /* adjust open and lease count */
2266 if (mode & MDS_OPEN_LEASE) {
2267 LASSERT(atomic_read(&o->mot_lease_count) > 0);
2268 atomic_dec(&o->mot_lease_count);
2270 LASSERT(atomic_read(&o->mot_open_count) > 0);
2271 atomic_dec(&o->mot_open_count);
2274 mdt_object_put(info->mti_env, o);
2277 RETURN(rc ? rc : ret);
2280 int mdt_close(struct tgt_session_info *tsi)
2282 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2283 struct ptlrpc_request *req = tgt_ses_req(tsi);
2284 struct mdt_export_data *med;
2285 struct mdt_file_data *mfd;
2286 struct mdt_object *o;
2287 struct md_attr *ma = &info->mti_attr;
2288 struct mdt_body *repbody = NULL;
2292 mdt_counter_incr(req, LPROC_MDT_CLOSE);
2293 /* Close may come with the Size-on-MDS update. Unpack it. */
2294 rc = mdt_close_unpack(info);
2296 GOTO(out, rc = err_serious(rc));
2298 LASSERT(info->mti_ioepoch);
2300 req_capsule_set_size(info->mti_pill, &RMF_MDT_MD, RCL_SERVER,
2301 info->mti_mdt->mdt_max_mdsize);
2302 req_capsule_set_size(info->mti_pill, &RMF_LOGCOOKIES, RCL_SERVER,
2303 info->mti_mdt->mdt_max_cookiesize);
2304 rc = req_capsule_server_pack(info->mti_pill);
2305 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2306 mdt_client_compatibility(info);
2308 mdt_fix_reply(info);
2309 mdt_exit_ucred(info);
2310 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2313 /* Continue to close handle even if we can not pack reply */
2315 repbody = req_capsule_server_get(info->mti_pill,
2317 ma->ma_lmm = req_capsule_server_get(info->mti_pill,
2319 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
2322 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
2324 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
2327 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
2328 repbody->eadatasize = 0;
2329 repbody->aclsize = 0;
2331 rc = err_serious(rc);
2334 med = &req->rq_export->exp_mdt_data;
2335 spin_lock(&med->med_open_lock);
2336 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2337 req_is_replay(req));
2338 if (mdt_mfd_closed(mfd)) {
2339 spin_unlock(&med->med_open_lock);
2340 CDEBUG(D_INODE, "no handle for file close: fid = "DFID
2341 ": cookie = "LPX64"\n", PFID(info->mti_rr.rr_fid1),
2342 info->mti_ioepoch->handle.cookie);
2343 /** not serious error since bug 3633 */
2346 class_handle_unhash(&mfd->mfd_handle);
2347 cfs_list_del_init(&mfd->mfd_list);
2348 spin_unlock(&med->med_open_lock);
2350 /* Do not lose object before last unlink. */
2351 o = mfd->mfd_object;
2352 mdt_object_get(info->mti_env, o);
2353 ret = mdt_mfd_close(info, mfd);
2354 if (repbody != NULL)
2355 rc = mdt_handle_last_unlink(info, o, ma);
2356 mdt_empty_transno(info, rc);
2357 mdt_object_put(info->mti_env, o);
2359 if (repbody != NULL) {
2360 mdt_client_compatibility(info);
2361 rc = mdt_fix_reply(info);
2364 mdt_exit_ucred(info);
2365 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK))
2366 GOTO(out, rc = err_serious(-ENOMEM));
2368 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_CLOSE_NET_REP,
2369 OBD_FAIL_MDS_CLOSE_NET_REP))
2370 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_CLOSE_NET_REP;
2372 mdt_thread_info_fini(info);
2373 RETURN(rc ? rc : ret);
2377 * DONE_WRITING rpc handler.
2379 * As mfd is not kept after replayed CLOSE (see mdt_ioepoch_close_on_replay()),
2380 * only those DONE_WRITING rpc will be replayed which really wrote smth on disk,
2381 * and got a trasid. Waiting for such DONE_WRITING is not reliable, so just
2382 * skip attributes and reconstruct the reply here.
2384 int mdt_done_writing(struct tgt_session_info *tsi)
2386 struct ptlrpc_request *req = tgt_ses_req(tsi);
2387 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2388 struct mdt_body *repbody = NULL;
2389 struct mdt_export_data *med;
2390 struct mdt_file_data *mfd;
2394 rc = req_capsule_server_pack(tsi->tsi_pill);
2396 GOTO(out, rc = err_serious(rc));
2398 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2399 repbody->eadatasize = 0;
2400 repbody->aclsize = 0;
2402 /* Done Writing may come with the Size-on-MDS update. Unpack it. */
2403 rc = mdt_close_unpack(info);
2405 GOTO(out, rc = err_serious(rc));
2407 if (mdt_check_resent(info, mdt_reconstruct_generic, NULL)) {
2408 mdt_exit_ucred(info);
2409 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2412 med = &info->mti_exp->exp_mdt_data;
2413 spin_lock(&med->med_open_lock);
2414 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
2415 req_is_replay(req));
2417 spin_unlock(&med->med_open_lock);
2418 CDEBUG(D_INODE, "no handle for done write: fid = "DFID
2419 ": cookie = "LPX64" ioepoch = "LPU64"\n",
2420 PFID(info->mti_rr.rr_fid1),
2421 info->mti_ioepoch->handle.cookie,
2422 info->mti_ioepoch->ioepoch);
2423 /* If this is a replay, reconstruct the transno. */
2424 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2425 rc = info->mti_ioepoch->flags & MF_SOM_AU ?
2427 mdt_empty_transno(info, rc);
2430 GOTO(error_ucred, rc);
2433 LASSERT(mfd->mfd_mode == MDS_FMODE_EPOCH ||
2434 mfd->mfd_mode == MDS_FMODE_TRUNC);
2435 class_handle_unhash(&mfd->mfd_handle);
2436 cfs_list_del_init(&mfd->mfd_list);
2437 spin_unlock(&med->med_open_lock);
2439 /* Set EPOCH CLOSE flag if not set by client. */
2440 info->mti_ioepoch->flags |= MF_EPOCH_CLOSE;
2441 info->mti_attr.ma_valid = 0;
2443 info->mti_attr.ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
2444 OBD_ALLOC_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2445 if (info->mti_attr.ma_lmm == NULL)
2446 GOTO(error_ucred, rc = -ENOMEM);
2448 rc = mdt_mfd_close(info, mfd);
2450 OBD_FREE_LARGE(info->mti_attr.ma_lmm, info->mti_mdt->mdt_max_mdsize);
2451 mdt_empty_transno(info, rc);
2453 mdt_exit_ucred(info);
2455 mdt_thread_info_fini(info);