1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mds/mds_open.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Phil Schwan <phil@clusterfs.com>
41 * Author: Mike Shaver <shaver@clusterfs.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <linux/version.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/version.h>
53 #include <linux/buffer_head.h>
54 #include <linux/workqueue.h>
56 #include <obd_class.h>
58 #include <lustre_fsfilt.h>
59 #include <lprocfs_status.h>
61 #include "mds_internal.h"
63 /* Exported function from this file are:
65 * mds_open - called by the intent handler
66 * mds_close - an rpc handling function
67 * mds_pin - an rpc handling function - which will go away
68 * mds_mfd_close - for force closing files when a client dies
72 * MDS file data handling: file data holds a handle for a file opened
76 static void mds_mfd_addref(void *mfdp)
78 struct mds_file_data *mfd = mfdp;
80 atomic_inc(&mfd->mfd_refcount);
81 CDEBUG(D_INFO, "GETting mfd %p : new refcount %d\n", mfd,
82 atomic_read(&mfd->mfd_refcount));
85 /* Create a new mds_file_data struct.
86 * One reference for handle+med_open_head list and dropped by mds_mfd_unlink(),
87 * one reference for the caller of this function. */
88 struct mds_file_data *mds_mfd_new(void)
90 struct mds_file_data *mfd;
92 OBD_ALLOC(mfd, sizeof *mfd);
94 CERROR("mds: out of memory\n");
98 atomic_set(&mfd->mfd_refcount, 2);
100 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
101 INIT_LIST_HEAD(&mfd->mfd_list);
102 class_handle_hash(&mfd->mfd_handle, mds_mfd_addref);
107 /* Get a new reference on the mfd pointed to by handle, if handle is still
108 * valid. Caller must drop reference with mds_mfd_put(). */
109 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
112 LASSERT(handle != NULL);
113 RETURN(class_handle2object(handle->cookie));
116 /* Drop mfd reference, freeing struct if this is the last one. */
117 static void mds_mfd_put(struct mds_file_data *mfd)
119 CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
120 atomic_read(&mfd->mfd_refcount) - 1);
121 LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
122 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
123 if (atomic_dec_and_test(&mfd->mfd_refcount)) {
124 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
128 /* Remove the mfd handle so that it cannot be found by open/close again.
129 * Caller must hold med_open_lock for mfd_list manipulation. */
130 void mds_mfd_unlink(struct mds_file_data *mfd, int decref)
132 class_handle_unhash(&mfd->mfd_handle);
133 list_del_init(&mfd->mfd_list);
138 /* Caller must hold mds->mds_epoch_sem */
139 static int mds_alloc_filterdata(struct inode *inode)
141 LASSERT(INODE_PRIVATE_DATA(inode) == NULL);
142 OBD_ALLOC(INODE_PRIVATE_DATA(inode), sizeof(struct mds_filter_data));
143 if (INODE_PRIVATE_DATA(inode) == NULL)
145 LASSERT(igrab(inode) == inode);
149 /* Caller must hold mds->mds_epoch_sem */
150 static void mds_free_filterdata(struct inode *inode)
152 LASSERT(INODE_PRIVATE_DATA(inode) != NULL);
153 OBD_FREE(INODE_PRIVATE_DATA(inode), sizeof(struct mds_filter_data));
154 INODE_PRIVATE_DATA(inode) = NULL;
158 /* Write access to a file: executors cause a negative count,
159 * writers a positive count. The semaphore is needed to perform
160 * a check for the sign and then increment or decrement atomically.
162 * This code is closely tied to the allocation of the d_fsdata and the
163 * MDS epoch, so we use the same semaphore for the whole lot.
165 * We could use a different semaphore for each file, if it ever shows
166 * up in a profile, which it won't.
168 * epoch argument is nonzero during recovery */
169 static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
174 down(&mds->mds_epoch_sem);
176 if (atomic_read(&inode->i_writecount) < 0) {
177 up(&mds->mds_epoch_sem);
182 if (MDS_FILTERDATA(inode) && MDS_FILTERDATA(inode)->io_epoch != 0) {
183 CDEBUG(D_INODE, "continuing MDS epoch "LPU64" for ino %lu/%u\n",
184 MDS_FILTERDATA(inode)->io_epoch, inode->i_ino,
185 inode->i_generation);
189 if (MDS_FILTERDATA(inode) == NULL)
190 mds_alloc_filterdata(inode);
191 if (MDS_FILTERDATA(inode) == NULL) {
195 if (epoch > mds->mds_io_epoch)
196 mds->mds_io_epoch = epoch;
199 MDS_FILTERDATA(inode)->io_epoch = mds->mds_io_epoch;
200 CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
201 mds->mds_io_epoch, inode->i_ino, inode->i_generation);
204 atomic_inc(&inode->i_writecount);
205 up(&mds->mds_epoch_sem);
209 /* Returns EAGAIN if the client needs to get size and/or cookies and close
210 * again -- which is never true if the file is about to be unlinked. Otherwise
211 * returns the number of remaining writers. */
212 static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
213 struct mds_body *body, int unlinking)
218 down(&mds->mds_epoch_sem);
219 atomic_dec(&inode->i_writecount);
220 rc = atomic_read(&inode->i_writecount);
224 if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
225 GOTO(out, rc = EAGAIN);
227 mds_free_filterdata(inode);
229 up(&mds->mds_epoch_sem);
233 static int mds_deny_write_access(struct mds_obd *mds, struct inode *inode)
236 down(&mds->mds_epoch_sem);
237 if (atomic_read(&inode->i_writecount) > 0) {
238 up(&mds->mds_epoch_sem);
241 atomic_dec(&inode->i_writecount);
242 up(&mds->mds_epoch_sem);
246 static void mds_allow_write_access(struct inode *inode)
249 atomic_inc(&inode->i_writecount);
252 int mds_query_write_access(struct inode *inode)
255 RETURN(atomic_read(&inode->i_writecount));
258 /* This replaces the VFS dentry_open, it manages mfd and writecount */
259 static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
260 struct vfsmount *mnt, int flags,
261 struct ptlrpc_request *req)
263 struct mds_export_data *med = &req->rq_export->exp_mds_data;
264 struct mds_obd *mds = mds_req2mds(req);
265 struct mds_file_data *mfd;
266 struct mds_body *body;
272 CERROR("mds: out of memory\n");
273 GOTO(cleanup_dentry, error = -ENOMEM);
276 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
278 if (flags & FMODE_WRITE) {
279 /* FIXME: in recovery, need to pass old epoch here */
280 error = mds_get_write_access(mds, dentry->d_inode, 0);
282 GOTO(cleanup_mfd, error);
283 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
284 } else if (flags & MDS_FMODE_EXEC) {
285 error = mds_deny_write_access(mds, dentry->d_inode);
287 GOTO(cleanup_mfd, error);
292 /* Mark the file as open to handle open-unlink. */
293 MDS_DOWN_WRITE_ORPHAN_SEM(dentry->d_inode);
294 mds_orphan_open_inc(dentry->d_inode);
295 MDS_UP_WRITE_ORPHAN_SEM(dentry->d_inode);
297 mfd->mfd_mode = flags;
298 mfd->mfd_dentry = dentry;
299 mfd->mfd_xid = req->rq_xid;
300 body->handle.cookie = mfd->mfd_handle.h_cookie;
302 if (req->rq_export->exp_disconnected) {
303 mds_mfd_unlink(mfd, 0);
304 MDS_DOWN_WRITE_ORPHAN_SEM(dentry->d_inode);
305 mds_mfd_close(NULL, REQ_REC_OFF, req->rq_export->exp_obd,
306 mfd, 0, NULL, 0, NULL, 0, NULL);
308 spin_lock(&med->med_open_lock);
309 list_add(&mfd->mfd_list, &med->med_open_head);
310 spin_unlock(&med->med_open_lock);
317 mds_mfd_unlink(mfd, 1);
319 return ERR_PTR(error);
322 /* Must be called with i_mutex held */
323 static int mds_create_objects(struct ptlrpc_request *req, int offset,
324 struct mds_update_record *rec,
325 struct mds_obd *mds, struct obd_device *obd,
326 struct dentry *dchild, void **handle,
327 struct lov_mds_md **objid)
329 struct inode *inode = dchild->d_inode;
330 struct obd_trans_info oti = { 0 };
331 struct lov_mds_md *lmm = NULL;
333 struct mds_body *body;
334 struct obd_info oinfo = { { { 0 } } };
340 if (!S_ISREG(inode->i_mode))
342 if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
343 !(rec->ur_flags & FMODE_WRITE))
346 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
348 if (body->valid & OBD_MD_FLEASIZE)
354 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
355 if (rec->ur_fid2->id == 0) {
356 DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
360 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
361 lmm_size = rec->ur_eadatalen;
362 lmm = rec->ur_eadata;
364 if (lov_mds_md_size(lmm->lmm_stripe_count,
365 lmm->lmm_magic) != lmm_size)
366 CWARN("Bad lmm_size during open replay for inode "
367 "%lu\n", inode->i_ino);
369 if (*handle == NULL) {
370 int stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
371 *handle = fsfilt_start_log(obd, inode, FSFILT_OP_CREATE,
374 if (IS_ERR(*handle)) {
375 rc = PTR_ERR(*handle);
380 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
382 CERROR("open replay failed to set md:%d\n", rc);
384 /* for replay we not need send lmm to client, this not used now */
385 lustre_shrink_reply(req, offset, 0, 1);
391 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
392 GOTO(out_ids, rc = -ENOMEM);
394 OBDO_ALLOC(oinfo.oi_oa);
395 if (oinfo.oi_oa == NULL)
396 GOTO(out_ids, rc = -ENOMEM);
397 oinfo.oi_oa->o_uid = 0; /* must have 0 uid / gid on OST */
398 oinfo.oi_oa->o_gid = 0;
399 oinfo.oi_oa->o_mode = S_IFREG | 0600;
400 oinfo.oi_oa->o_id = inode->i_ino;
401 oinfo.oi_oa->o_gr = 0;
402 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
403 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID| OBD_MD_FLGROUP;
404 oinfo.oi_oa->o_size = 0;
406 obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
407 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
408 if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
409 /* check if things like lfs setstripe are sending us the ea */
410 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
411 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
413 0, &oinfo.oi_md, rec->ur_eadata);
417 __u32 lmm_sz = mds->mds_max_mdsize;
418 OBD_ALLOC(lmm, lmm_sz);
420 GOTO(out_oa, rc = -ENOMEM);
423 rc = mds_get_md(obd, dchild->d_parent->d_inode,
424 lmm, &lmm_size, 1, 0,
425 req->rq_export->exp_connect_flags);
427 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
429 0, &oinfo.oi_md, lmm);
430 OBD_FREE(lmm, lmm_sz);
434 rc = obd_create(mds->mds_lov_exp, oinfo.oi_oa,
440 CDEBUG_LIMIT(level, "error creating objects for "
441 "inode %lu: rc = %d\n", inode->i_ino, rc);
443 CERROR("obd_create returned invalid "
450 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_lov_exp,
451 0, &oinfo.oi_md, rec->ur_eadata);
455 oinfo.oi_md->lsm_object_id = oinfo.oi_oa->o_id;
457 if (i_size_read(inode)) {
458 oinfo.oi_oa->o_size = i_size_read(inode);
459 obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE |
460 OBD_MD_FLATIME | OBD_MD_FLMTIME |
461 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
463 /* pack lustre id to OST */
464 oinfo.oi_oa->o_fid = body->fid1.id;
465 oinfo.oi_oa->o_generation = body->fid1.generation;
466 oinfo.oi_oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
467 oinfo.oi_policy.l_extent.start = i_size_read(inode);
468 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
470 rc = obd_punch_rqset(mds->mds_lov_exp, &oinfo, &oti);
472 CERROR("error setting attrs for inode %lu: rc %d\n",
475 CERROR("obd_setattr_async returned bad rc %d\n",
483 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
484 obdo_refresh_inode(inode, oinfo.oi_oa, OBD_MD_FLBLKSZ);
486 LASSERT(oinfo.oi_md && oinfo.oi_md->lsm_object_id);
488 rc = obd_packmd(mds->mds_lov_exp, &lmm, oinfo.oi_md);
490 CERROR("cannot pack lsm, err = %d\n", rc);
494 body->eadatasize = rc;
497 *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
498 if (IS_ERR(*handle)) {
499 rc = PTR_ERR(*handle);
501 GOTO(free_diskmd, rc);
504 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
505 lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
507 memcpy(lmm_buf, lmm, lmm_size);
508 *objid = lmm_buf; /* save for mds_lov_update_objid */
511 obd_free_diskmd(mds->mds_lov_exp, &lmm);
513 oti_free_cookies(&oti);
514 OBDO_FREE(oinfo.oi_oa);
517 obd_free_memmd(mds->mds_lov_exp, &oinfo.oi_md);
521 static void reconstruct_open(struct mds_update_record *rec, int offset,
522 struct ptlrpc_request *req,
523 struct lustre_handle *child_lockh)
525 struct mds_export_data *med = &req->rq_export->exp_mds_data;
526 struct lsd_client_data *lcd = med->med_lcd;
527 struct mds_obd *mds = mds_req2mds(req);
528 struct mds_file_data *mfd = NULL;
529 struct obd_export *exp = req->rq_export;
530 struct obd_device *obd = exp->exp_obd;
531 struct dentry *parent, *dchild;
532 struct ldlm_reply *rep;
533 struct mds_body *body;
539 LASSERT(offset == DLM_INTENT_REC_OFF); /* only called via intent */
540 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
541 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
543 /* copy rc, transno and disp; steal locks */
544 mds_req_from_lcd(req, lcd);
545 ldlm_reply_set_disposition(rep, le32_to_cpu(lcd->lcd_last_data));
547 /* Only replay if create or open actually happened. */
548 if (!ldlm_reply_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
550 return; /* error looking up parent or child */
553 /* If we failed, then we must have failed opening, so don't look for
554 * file descriptor or anything, just give the client the bad news.
556 if (req->rq_status) {
561 /* Now let's see if we have file descriptor present.
562 * No need to lookup child as it could be already deleted by another
563 * thread (bug 15010) */
564 spin_lock(&med->med_open_lock);
565 list_for_each(t, &med->med_open_head) {
566 mfd = list_entry(t, struct mds_file_data, mfd_list);
567 if (mfd->mfd_xid == req->rq_xid) {
573 spin_unlock(&med->med_open_lock);
575 if (mfd && mfd->mfd_dentry && mfd->mfd_dentry->d_inode) {
576 dchild = mfd->mfd_dentry;
579 parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
580 if (IS_ERR(parent)) {
581 rc = PTR_ERR(parent);
582 LCONSOLE_WARN("Parent "LPU64"/%u lookup error %d."
583 " Evicting client %s with export %s.\n",
584 rec->ur_fid1->id,rec->ur_fid1->generation,
585 rc, obd_uuid2str(&exp->exp_client_uuid),
586 obd_export_nid2str(exp));
587 mds_export_evict(exp);
592 dchild = mds_lookup(obd, rec->ur_name, parent, rec->ur_namelen - 1);
594 if (IS_ERR(dchild)) {
595 rc = PTR_ERR(dchild);
596 LCONSOLE_WARN("Child "LPU64"/%u lookup error %d."
597 " Evicting client %s with export %s.\n",
598 rec->ur_fid1->id,rec->ur_fid1->generation,
599 rc, obd_uuid2str(&exp->exp_client_uuid),
600 obd_export_nid2str(exp));
601 mds_export_evict(exp);
607 if (!dchild->d_inode)
608 GOTO(out_dput, 0); /* child not present to open */
610 /* At this point, we know we have a child. We'll send
611 * it back _unless_ it not created and open failed.
613 if (ldlm_reply_disposition(rep, DISP_OPEN_OPEN) &&
614 !ldlm_reply_disposition(rep, DISP_OPEN_CREATE) &&
619 mds_pack_inode2body(body, dchild->d_inode);
620 if (S_ISREG(dchild->d_inode->i_mode)) {
621 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
622 body, dchild->d_inode, 1, 0,
623 req->rq_export->exp_connect_flags);
626 LASSERT(rc == req->rq_status);
628 /* If we have LOV EA data, the OST holds size, mtime */
629 if (!(body->valid & OBD_MD_FLEASIZE))
630 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
631 OBD_MD_FLATIME | OBD_MD_FLMTIME);
634 if (!(rec->ur_flags & MDS_OPEN_JOIN_FILE))
635 lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
636 body->eadatasize, 0);
638 if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
639 !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
640 int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
642 rc = mds_pack_acl(med, dchild->d_inode, req->rq_repmsg,
644 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
645 if (!req->rq_status && rc)
649 /* #warning "XXX fixme" bug 2991 */
650 /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
651 * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
652 * to detect a re-open */
654 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
656 /* Allow file join in beta builds to allow debugging */
657 rc = mds_join_file(rec, req, dchild, NULL);
661 CWARN("file join is not supported in this version of "
663 GOTO(out_dput, req->rq_status = rc = -EOPNOTSUPP);
666 mntget(mds->mds_vfsmnt);
667 CERROR("Re-opened file \n");
668 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt,
669 rec->ur_flags & ~MDS_OPEN_TRUNC, req);
670 mntput(mds->mds_vfsmnt);
672 req->rq_status = PTR_ERR(mfd);
674 CERROR("%s: opening inode "LPU64" failed: rc %d\n",
675 req->rq_export->exp_obd->obd_name,
676 (__u64)dchild->d_inode->i_ino, req->rq_status);
677 GOTO(out_dput, req->rq_status);
680 body->handle.cookie = mfd->mfd_handle.h_cookie;
681 CDEBUG(D_INODE, "resend mfd %p, cookie "LPX64"\n", mfd,
682 mfd->mfd_handle.h_cookie);
685 if (!ldlm_reply_disposition(rep, DISP_OPEN_LOCK))
688 /* child_lockh has been set in fixup_handle_for_resent_req called
689 * in mds_intent_policy for resent request */
690 if (child_lockh == NULL || !lustre_handle_is_used(child_lockh)) {
691 /* the lock is already canceled! clear DISP_OPEN_LOCK */
692 ldlm_reply_clear_disposition(rep, DISP_OPEN_LOCK);
704 /* if client disconnects during recovery it may resend opens which were replayed
705 * on server but their transno less then last_transno on server so they will not
706 * be detected as reconstructs */
707 static int open_replay_reconstruct(struct ptlrpc_request *req)
709 struct mds_export_data *med = &req->rq_export->exp_mds_data;
710 struct mds_file_data *mfd = NULL;
713 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
716 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
719 /* if mfd exists then replay was done already */
720 spin_lock(&med->med_open_lock);
721 list_for_each(t, &med->med_open_head) {
722 mfd = list_entry(t, struct mds_file_data, mfd_list);
723 if (mfd->mfd_xid == req->rq_xid) {
729 spin_unlock(&med->med_open_lock);
732 struct mds_body *body = lustre_msg_buf(req->rq_repmsg,
735 __u64 *pre_versions = lustre_msg_get_versions(req->rq_reqmsg);
737 body->handle.cookie = mfd->mfd_handle.h_cookie;
738 CDEBUG(D_INODE, "resend mfd %p, cookie "LPX64"\n", mfd,
739 mfd->mfd_handle.h_cookie);
741 lustre_msg_set_versions(req->rq_repmsg, pre_versions);
742 lustre_msg_set_transno(req->rq_repmsg,
743 lustre_msg_get_transno(req->rq_reqmsg));
744 lustre_msg_set_status(req->rq_repmsg, 0);
750 /* do NOT or the MAY_*'s, you'll get the weakest */
751 static int accmode(struct inode *inode, int flags)
755 /* Sadly, NFSD reopens a file repeatedly during operation, so the
756 * "acc_mode = 0" allowance for newly-created files isn't honoured.
757 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
758 * owner can write to a file even if it is marked readonly to hide
759 * its brokenness. (bug 5781) */
760 if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current_fsuid())
763 if (flags & FMODE_READ)
765 if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
767 if (flags & MDS_FMODE_EXEC)
772 /* Handles object creation, actual opening, and I/O epoch */
773 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
774 struct mds_body *body, int flags, void **handle,
775 struct mds_update_record *rec,struct ldlm_reply *rep,
776 struct lustre_handle *lockh)
778 struct mds_obd *mds = mds_req2mds(req);
779 struct obd_device *obd = req->rq_export->exp_obd;
780 struct mds_file_data *mfd = NULL;
781 struct lov_mds_md *lmm = NULL; /* object IDs created */
785 /* atomically create objects if necessary */
786 LOCK_INODE_MUTEX(dchild->d_inode);
788 if (S_ISREG(dchild->d_inode->i_mode) &&
789 !(body->valid & OBD_MD_FLEASIZE)) {
790 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
791 body, dchild->d_inode, 0, 0,
792 req->rq_export->exp_connect_flags);
794 UNLOCK_INODE_MUTEX(dchild->d_inode);
799 if ((body->valid & OBD_MD_FLEASIZE) &&
800 (rec->ur_flags & MDS_OPEN_HAS_EA)) {
801 UNLOCK_INODE_MUTEX(dchild->d_inode);
804 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
806 /* Allow file join in beta builds to allow debugging */
807 UNLOCK_INODE_MUTEX(dchild->d_inode);
808 rc = mds_join_file(rec, req, dchild, lockh);
811 LOCK_INODE_MUTEX(dchild->d_inode);
813 CWARN("file join is not supported in this version of "
818 if (!(body->valid & OBD_MD_FLEASIZE) &&
819 !(body->valid & OBD_MD_FLMODEASIZE)) {
820 /* split open transactions here */
821 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_SPLIT_OPEN, 10);
822 /* no EA: create objects */
823 rc = mds_create_objects(req, DLM_REPLY_REC_OFF + 1, rec,
824 mds, obd, dchild, handle, &lmm);
826 CERROR("mds_create_objects: rc = %d\n", rc);
827 UNLOCK_INODE_MUTEX(dchild->d_inode);
832 /* If the inode has no EA data, then MDS holds size, mtime */
833 if (S_ISREG(dchild->d_inode->i_mode) &&
834 !(body->valid & OBD_MD_FLEASIZE)) {
835 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
836 OBD_MD_FLATIME | OBD_MD_FLMTIME);
838 UNLOCK_INODE_MUTEX(dchild->d_inode);
840 if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
841 rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
842 int acl_off = DLM_REPLY_REC_OFF + 2;
844 rc = mds_pack_acl(&req->rq_export->exp_mds_data,
845 dchild->d_inode, req->rq_repmsg,
851 if ((rc = mds_lov_prepare_objids(obd,lmm)) != 0)
854 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
856 RETURN(PTR_ERR(mfd));
858 CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
859 mfd->mfd_handle.h_cookie);
861 mds_lov_update_objids(obd, lmm);
864 mds_mfd_unlink(mfd, 1);
870 static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
871 struct mds_body *body, int flags,
872 struct mds_update_record *rec,struct ldlm_reply *rep)
874 struct obd_device *obd = req->rq_export->exp_obd;
875 struct mds_obd *mds = mds_req2mds(req);
876 struct dentry *dchild, *dparent = NULL;
877 char fidname[LL_FID_NAMELEN];
880 struct inode *inodes[PTLRPC_NUM_VERSIONS] = { NULL };
883 ldlm_reply_set_disposition(rep, DISP_LOOKUP_EXECD);
884 fidlen = ll_fid2str(fidname, fid->id, fid->generation);
885 dchild = mds_lookup(obd, fidname, mds->mds_pending_dir, fidlen);
886 if (IS_ERR(dchild)) {
887 rc = PTR_ERR(dchild);
888 CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
892 if (dchild->d_inode != NULL) {
893 mds_inode_set_orphan(dchild->d_inode);
894 CWARN("Orphan %s found and opened in PENDING directory\n",
897 __u64 *pre_versions = lustre_msg_get_versions(req->rq_reqmsg);
900 /* We didn't find it in PENDING so it isn't an orphan. See
901 * if it was a regular inode that was previously created. */
902 dchild = mds_fid2dentry(mds, fid, NULL);
904 RETURN(PTR_ERR(dchild));
907 * this can be replay of partially committed open|create,
908 * the create itself was committed while LOV EA weren't
909 * We need to set versions again if conditions are:
911 * - the transaction is greater than last_committed
912 * - this was open|create
913 * - there was real create so parent pre_version was saved
915 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
916 (lustre_msg_get_transno(req->rq_reqmsg) >
917 req->rq_export->exp_last_committed) &&
918 (rec->ur_flags & MDS_OPEN_CREAT) &&
919 (pre_versions && pre_versions[0] != 0)) {
920 /* need parent to set version */
921 dparent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
922 if (IS_ERR(dparent)) {
923 CERROR("Can't find parent for open replay\n");
925 RETURN(PTR_ERR(dparent));
927 /* though file was created, the versions were not
928 * changed yet, need to replay that too */
929 inodes[0] = dparent->d_inode;
930 inodes[1] = dchild->d_inode;
934 mds_pack_inode2body(body, dchild->d_inode);
935 ldlm_reply_set_disposition(rep, DISP_LOOKUP_POS);
936 ldlm_reply_set_disposition(rep, DISP_OPEN_OPEN);
938 rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep, NULL);
939 rc = mds_finish_transno(mds, inodes, handle,
940 req, rc, rep ? rep->lock_policy_res1 : 0, 0);
941 /* XXX what do we do here if mds_finish_transno itself failed? */
948 int mds_pin(struct ptlrpc_request *req, int offset)
950 struct obd_device *obd = req->rq_export->exp_obd;
951 struct mds_body *reqbody, *repbody;
952 struct lvfs_run_ctxt saved;
953 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
956 reqbody = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*reqbody));
958 rc = lustre_pack_reply(req, 2, size, NULL);
962 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
965 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
966 rc = mds_open_by_fid(req, &reqbody->fid1, repbody, reqbody->flags, NULL,
968 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
973 /* Get an internal lock on the inode number (but not generation) to sync
974 * new inode creation with inode unlink (bug 2029). If child_lockh is NULL
975 * we just get the lock as a barrier to wait for other holders of this lock,
976 * and drop it right away again. */
977 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
978 struct lustre_handle *child_lockh)
980 struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
981 struct lustre_handle lockh;
982 int lock_flags = LDLM_FL_ATOMIC_CB;
985 if (child_lockh == NULL)
986 child_lockh = &lockh;
988 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
989 LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
990 ldlm_blocking_ast, ldlm_completion_ast,
991 NULL, NULL, 0, NULL, child_lockh);
993 CERROR("ldlm_cli_enqueue_local: %d\n", rc);
994 else if (child_lockh == &lockh)
995 ldlm_lock_decref(child_lockh, LCK_EX);
1000 int noinline mds_get_open_lock(struct obd_device *obd, struct dentry *dchild,
1001 int child_mode, struct lustre_handle *child_lockh,
1002 struct ldlm_reply *rep)
1004 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP | MDS_INODELOCK_OPEN } };
1005 struct ldlm_res_id child_res_id;
1006 int rc = 0, lock_flags = 0;
1008 /* In case of replay we do not get a lock assuming that the
1009 caller has it already */
1010 memset(&child_res_id, 0, sizeof(child_res_id));
1011 child_res_id.name[0] = dchild->d_inode->i_ino;
1012 child_res_id.name[1] = dchild->d_inode->i_generation;
1014 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
1015 LDLM_IBITS, &policy, child_mode,
1016 &lock_flags, ldlm_blocking_ast,
1017 ldlm_completion_ast, NULL, NULL,
1018 0, NULL, child_lockh);
1022 /* Let mds_intent_policy know that we have a lock to return */
1023 ldlm_reply_set_disposition(rep, DISP_OPEN_LOCK);
1029 int mds_open(struct mds_update_record *rec, int offset,
1030 struct ptlrpc_request *req, struct lustre_handle *child_lockh)
1032 /* XXX ALLOCATE _something_ - 464 bytes on stack here */
1033 struct obd_device *obd = req->rq_export->exp_obd;
1034 struct mds_obd *mds = mds_req2mds(req);
1035 struct ldlm_reply *rep = NULL;
1036 struct mds_body *body = NULL;
1037 struct dentry *dchild = NULL, *dparent = NULL;
1038 struct inode *inodes[PTLRPC_NUM_VERSIONS] = { NULL };
1039 struct mds_export_data *med;
1040 struct lustre_handle parent_lockh;
1041 int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
1042 int parent_mode = LCK_CR;
1043 void *handle = NULL;
1044 struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
1045 unsigned int qcids[MAXQUOTAS] = { current_fsuid(), current_fsgid() };
1046 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1047 unsigned int ids[MAXQUOTAS] = { 0, 0 };
1048 int child_mode = LCK_CR;
1049 /* Always returning LOOKUP lock if open succesful to guard
1050 dentry on client. */
1051 int quota_pending[2] = {0, 0};
1052 int use_parent, need_open_lock;
1053 unsigned int gid = current_fsgid();
1056 mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
1058 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
1059 (obd_timeout + 1) / 4);
1061 CLASSERT(MAXQUOTAS < 4);
1062 if (offset == DLM_INTENT_REC_OFF) { /* intent */
1063 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1065 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
1067 } else if (offset == REQ_REC_OFF) { /* non-intent reint */
1068 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1070 LBUG(); /* XXX: not supported yet? */
1076 /* check the open resent|replay case */
1077 if (open_replay_reconstruct(req))
1080 MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
1082 /* Step 0: If we are passed a fid, then we assume the client already
1083 * opened this file and is only replaying the RPC, so we open the
1084 * inode by fid (at some large expense in security). */
1085 /*XXX liblustre use mds_open_by_fid to implement LL_IOC_LOV_SETSTRIPE */
1086 if (((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
1087 (req->rq_export->exp_libclient && rec->ur_flags&MDS_OPEN_HAS_EA))&&
1088 !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
1089 if (rec->ur_fid2->id == 0) {
1090 struct ldlm_lock *lock = ldlm_handle2lock(child_lockh);
1092 LDLM_ERROR(lock, "fid2 not set on open replay");
1093 LDLM_LOCK_PUT(lock);
1095 DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
1099 /** check there is no stale orphan with same inode number */
1100 if (rec->ur_flags & MDS_OPEN_CREAT) {
1101 rc = mds_check_stale_orphan(obd, rec->ur_fid2);
1106 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
1108 if (rc != -ENOENT) {
1109 if (req->rq_export->exp_libclient &&
1110 rec->ur_flags & MDS_OPEN_HAS_EA)
1116 /* We didn't find the correct inode on disk either, so we
1117 * need to re-create it via a regular replay. */
1118 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
1119 DEBUG_REQ(D_ERROR, req,"OPEN_CREAT not in open replay");
1122 } else if (rec->ur_fid2->id) {
1123 DEBUG_REQ(D_ERROR, req, "fid2 "LPU64"/%u on open non-replay",
1124 rec->ur_fid2->id, rec->ur_fid2->generation);
1128 /* If we got here, we must be called via intent */
1129 LASSERT(offset == DLM_INTENT_REC_OFF);
1131 med = &req->rq_export->exp_mds_data;
1132 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
1133 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
1137 if (rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_JOIN_FILE))
1138 parent_mode = LCK_EX;
1140 /* We cannot use acc_mode here, because it is zeroed in case of
1141 creating a file, so we get wrong lockmode */
1142 if (rec->ur_flags & FMODE_WRITE)
1143 child_mode = LCK_CW;
1144 else if (rec->ur_flags & MDS_FMODE_EXEC)
1145 child_mode = LCK_PR;
1147 child_mode = LCK_CR;
1149 /* join file and nfsd can't need lookup dchild as use parent for it */
1150 use_parent = (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
1151 (rec->ur_flags & MDS_OPEN_LOCK) && (rec->ur_namelen == 1)) ||
1152 (rec->ur_flags & MDS_OPEN_JOIN_FILE);
1154 need_open_lock = !(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
1155 (rec->ur_flags & MDS_OPEN_LOCK);
1157 /* Try to lock both parent and child first. If child is not found,
1158 * return only locked parent. This is enough to prevent other
1159 * threads from changing this directory until creation is finished. */
1160 rc = mds_get_parent_child_locked(obd, &obd->u.mds,
1163 &dparent, parent_mode,
1164 MDS_INODELOCK_UPDATE,
1165 use_parent ? NULL : rec->ur_name,
1167 (rec->ur_flags & MDS_OPEN_LOCK) ?
1169 &dchild, child_mode,
1170 MDS_INODELOCK_LOOKUP |
1171 MDS_INODELOCK_OPEN);
1174 if (rc != -ENOENT) {
1175 CERROR("parent "LPU64"/%u lookup/take lock error %d\n",
1176 rec->ur_fid1->id, rec->ur_fid1->generation, rc);
1178 /* Just cannot find parent - make it look like
1179 * usual negative lookup to avoid extra MDS RPC */
1180 ldlm_reply_set_disposition(rep, DISP_LOOKUP_EXECD);
1181 ldlm_reply_set_disposition(rep, DISP_LOOKUP_NEG);
1185 LASSERT(dparent->d_inode != NULL);
1187 cleanup_phase = 1; /* parent dentry and lock */
1190 dchild = dget(dparent);
1192 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
1193 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1194 GOTO(found_child, rc);
1197 cleanup_phase = 2; /* child dentry */
1199 ldlm_reply_set_disposition(rep, DISP_LOOKUP_EXECD);
1200 if (dchild->d_inode)
1201 ldlm_reply_set_disposition(rep, DISP_LOOKUP_POS);
1203 ldlm_reply_set_disposition(rep, DISP_LOOKUP_NEG);
1205 /*Step 3: If the child was negative, and we're supposed to, create it.*/
1206 if (dchild->d_inode == NULL) {
1207 unsigned long ino = rec->ur_fid2->id;
1208 struct iattr *iattr;
1209 struct inode *inode;
1211 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
1212 /* It's negative and we weren't supposed to create it */
1213 GOTO(cleanup, rc = -ENOENT);
1216 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1217 GOTO(cleanup, rc = -EROFS);
1219 /** check there is no stale orphan with same inode number */
1220 rc = mds_check_stale_orphan(obd, rec->ur_fid2);
1224 /* version recovery check */
1225 rc = mds_version_get_check(req, dparent->d_inode, 0);
1227 GOTO(cleanup_no_trans, rc);
1229 if (dparent->d_inode->i_mode & S_ISGID)
1230 gid = dparent->d_inode->i_gid;
1232 gid = current_fsgid();
1234 /* we try to get enough quota to write here, and let ldiskfs
1235 * decide if it is out of quota or not b=14783
1236 * FIXME: after CMD is used, pointer to obd_trans_info* couldn't
1237 * be NULL, b=14840 */
1238 ids[0] = current_fsuid();
1240 lquota_chkquota(mds_quota_interface_ref, req->rq_export,
1241 ids[0], ids[1], 1, quota_pending,
1244 ldlm_reply_set_disposition(rep, DISP_OPEN_CREATE);
1245 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
1247 if (IS_ERR(handle)) {
1248 rc = PTR_ERR(handle);
1252 dchild->d_fsdata = (void *) &dp;
1256 LOCK_INODE_MUTEX(dparent->d_inode);
1257 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
1258 UNLOCK_INODE_MUTEX(dparent->d_inode);
1259 if (dchild->d_fsdata == (void *)(unsigned long)ino)
1260 dchild->d_fsdata = NULL;
1263 CDEBUG(D_INODE, "error during create: %d\n", rc);
1266 inode = dchild->d_inode;
1269 if (ino != inode->i_ino) {
1270 /* FID support is needed to replay this
1271 * correctly. Now fail gracefully like there is
1272 * version mismatch */
1273 if (req->rq_export->exp_delayed)
1277 CERROR("file recreated with wrong inode number"
1278 " %lu != %lu\n", ino, inode->i_ino);
1281 /* Written as part of setattr */
1282 inode->i_generation = rec->ur_fid2->generation;
1283 CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
1284 inode->i_ino, inode->i_generation);
1287 OBD_ALLOC_PTR(iattr);
1289 GOTO(cleanup, rc = -ENOMEM);
1291 LTIME_S(iattr->ia_atime) = rec->ur_time;
1292 LTIME_S(iattr->ia_ctime) = rec->ur_time;
1293 LTIME_S(iattr->ia_mtime) = rec->ur_time;
1295 iattr->ia_uid = current_fsuid(); /* set by push_ctxt already */
1296 iattr->ia_gid = gid;
1298 iattr->ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1299 ATTR_MTIME | ATTR_CTIME;
1301 rc = fsfilt_setattr(obd, dchild, handle, iattr, 0);
1303 CERROR("error on child setattr: rc = %d\n", rc);
1305 iattr->ia_valid = ATTR_MTIME | ATTR_CTIME;
1307 rc = fsfilt_setattr(obd, dparent, handle, iattr, 0);
1309 CERROR("error on parent setattr: rc = %d\n", rc);
1311 OBD_FREE_PTR(iattr);
1313 rc = fsfilt_commit(obd, dchild->d_inode, handle, 0);
1315 acc_mode = 0; /* Don't check for permissions */
1317 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1318 /* Child previously existed so the lookup and lock is already
1319 * done, so no further locking is needed. */
1320 /* for nfs and join - we need two locks for same fid, but
1321 * with different mode */
1322 if (need_open_lock && !use_parent) {
1323 ldlm_reply_set_disposition(rep, DISP_OPEN_LOCK);
1328 LASSERTF(!mds_inode_is_orphan(dchild->d_inode),
1329 "dchild %.*s (%p) inode %p/%lu/%u\n", dchild->d_name.len,
1330 dchild->d_name.name, dchild, dchild->d_inode,
1331 dchild->d_inode->i_ino, dchild->d_inode->i_generation);
1334 mds_pack_inode2body(body, dchild->d_inode);
1335 if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
1336 (rec->ur_flags & MDS_OPEN_EXCL)) {
1337 /* File already exists, we didn't just create it, and we
1338 * were passed O_EXCL; err-or. */
1339 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
1342 /* if we are following special file, don't open */
1343 if (!S_ISREG(dchild->d_inode->i_mode) &&
1344 !S_ISDIR(dchild->d_inode->i_mode))
1345 GOTO(cleanup_no_trans, rc = 0);
1347 ldlm_reply_set_disposition(rep, DISP_OPEN_OPEN);
1348 if (S_ISREG(dchild->d_inode->i_mode)) {
1349 /* Check permissions etc */
1350 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
1354 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) &&
1355 (acc_mode & MAY_WRITE))
1356 GOTO(cleanup, rc = -EROFS);
1358 /* An append-only file must be opened in append mode for
1360 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
1361 ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
1362 (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
1363 GOTO(cleanup, rc = -EPERM);
1365 if (S_ISDIR(dchild->d_inode->i_mode)) {
1366 if (rec->ur_flags & MDS_OPEN_CREAT ||
1367 rec->ur_flags & FMODE_WRITE) {
1368 /* we are trying to create or write a exist dir*/
1369 GOTO(cleanup, rc = -EISDIR);
1371 if (rec->ur_flags & MDS_FMODE_EXEC) {
1372 /* we are trying to exec a directory */
1373 GOTO(cleanup, rc = -EACCES);
1375 if (ll_permission(dchild->d_inode, acc_mode, NULL))
1376 GOTO(cleanup, rc = -EACCES);
1377 } else if (rec->ur_flags & MDS_OPEN_DIRECTORY) {
1378 GOTO(cleanup, rc = -ENOTDIR);
1382 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
1383 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
1384 GOTO(cleanup, rc = -EAGAIN);
1387 if (need_open_lock) {
1388 rc = mds_get_open_lock(obd, dchild, child_mode, child_lockh, rep);
1393 if (!S_ISREG(dchild->d_inode->i_mode) &&
1394 !S_ISDIR(dchild->d_inode->i_mode) &&
1395 (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH)) {
1396 /* If client supports this, do not return open handle for
1397 * special device nodes */
1398 GOTO(cleanup_no_trans, rc = 0);
1401 /* Step 5: mds_open it */
1402 rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
1403 rep, &parent_lockh);
1407 inodes[0] = (!created || IS_ERR(dparent)) ? NULL : dparent->d_inode;
1408 inodes[1] = (created && dchild) ? dchild->d_inode : NULL;
1409 rc = mds_finish_transno(mds, inodes, handle, req, rc,
1410 rep ? rep->lock_policy_res1 : 0, 0);
1413 if (quota_pending[0] || quota_pending[1])
1414 lquota_pending_commit(mds_quota_interface_ref, obd,
1415 ids[0], ids[1], quota_pending);
1416 switch (cleanup_phase) {
1418 if (rc && created) {
1420 LOCK_INODE_MUTEX(dparent->d_inode);
1421 err = ll_vfs_unlink(dparent->d_inode, dchild,
1423 UNLOCK_INODE_MUTEX(dparent->d_inode);
1425 CERROR("unlink(%.*s) in error path: %d\n",
1426 dchild->d_name.len, dchild->d_name.name,
1429 } else if (created) {
1430 mds_lock_new_child(obd, dchild->d_inode, NULL);
1431 /* save uid/gid for quota acquire/release */
1432 qpids[USRQUOTA] = dparent->d_inode->i_uid;
1433 qpids[GRPQUOTA] = dparent->d_inode->i_gid;
1438 /* It is safe to leave IT_OPEN_LOCK set, if rc is not 0,
1439 * mds_intent_policy won't try to return any locks */
1440 if (rc && child_lockh->cookie)
1441 ldlm_lock_decref(child_lockh, child_mode);
1443 if (dparent == NULL)
1448 ldlm_lock_decref(&parent_lockh, parent_mode);
1450 ptlrpc_save_lock(req, &parent_lockh, parent_mode);
1452 /* trigger dqacq on the owner of child and parent */
1453 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1459 /* Close a "file descriptor" and possibly unlink an orphan from the
1460 * PENDING directory. Caller must hold child->i_mutex, this drops it.
1462 * If we are being called from mds_disconnect() because the client has
1463 * disappeared, then req == NULL and we do not update last_rcvd because
1464 * there is nothing that could be recovered by the client at this stage
1465 * (it will not even _have_ an entry in last_rcvd anymore). */
1466 int mds_mfd_close(struct ptlrpc_request *req, int offset,
1467 struct obd_device *obd, struct mds_file_data *mfd,
1468 int unlink_orphan, struct lov_mds_md *lmm, int lmm_size,
1469 struct llog_cookie *logcookies, int cookies_size,
1472 struct inode *inode = mfd->mfd_dentry->d_inode;
1473 char fidname[LL_FID_NAMELEN];
1474 int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
1475 struct dentry *pending_child = NULL;
1476 struct mds_obd *mds = &obd->u.mds;
1477 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1478 void *handle = NULL;
1479 struct mds_body *request_body = NULL, *reply_body = NULL;
1480 struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
1481 struct iattr iattr = { 0 };
1484 if (req && req->rq_reqmsg != NULL)
1485 request_body = lustre_msg_buf(req->rq_reqmsg, offset,
1486 sizeof(*request_body));
1487 if (req && req->rq_repmsg != NULL)
1488 reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1489 sizeof(*reply_body));
1491 fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1493 CDEBUG(D_INODE, "inode %p ino %s nlink %d orphan %d\n", inode, fidname,
1494 inode->i_nlink, mds_orphan_open_count(inode));
1496 last_orphan = (mds_orphan_open_dec_test(inode) &&
1497 mds_inode_is_orphan(inode) &&
1498 !obd->obd_recovering);
1500 /* this is half of the actual "close" */
1501 if (mfd->mfd_mode & FMODE_WRITE) {
1502 rc = mds_put_write_access(mds, inode, request_body,
1503 last_orphan && unlink_orphan);
1504 } else if (mfd->mfd_mode & MDS_FMODE_EXEC) {
1505 mds_allow_write_access(inode);
1507 /* here writecount change also needs protection from orphan write sem.
1508 * so drop orphan write sem after mds_put_write_access, bz 12888. */
1509 MDS_UP_WRITE_ORPHAN_SEM(inode);
1511 if (last_orphan && unlink_orphan) {
1512 int stripe_count = 0;
1513 /* mds_put_write_access must have succeeded */
1514 LASSERTF(rc == 0, "inode %lu/%u: rc %d",
1515 inode->i_ino, inode->i_generation, rc);
1517 CDEBUG(D_INODE, "destroying orphan object %s\n", fidname);
1519 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
1520 (S_ISDIR(inode->i_mode) && inode->i_nlink > 2))
1521 CERROR("found \"orphan\" %s %s with link count %d\n",
1522 S_ISREG(inode->i_mode) ? "file" : "dir",
1523 fidname, inode->i_nlink);
1525 /* Sadly, there is no easy way to save pending_child from
1526 * mds_reint_unlink() into mfd, so we need to re-lookup,
1527 * but normally it will still be in the dcache. */
1528 LOCK_INODE_MUTEX(pending_dir);
1529 cleanup_phase = 1; /* UNLOCK_INODE_MUTEX(pending_dir) when finished */
1530 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1532 if (IS_ERR(pending_child))
1533 GOTO(cleanup, rc = PTR_ERR(pending_child));
1534 LASSERT(pending_child->d_inode != NULL);
1536 cleanup_phase = 2; /* dput(pending_child) when finished */
1537 if (S_ISDIR(pending_child->d_inode->i_mode)) {
1538 rc = ll_vfs_rmdir(pending_dir, pending_child,
1541 CERROR("error unlinking orphan dir %s: rc %d\n",
1547 stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
1550 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK,
1551 NULL, stripe_count);
1552 if (IS_ERR(handle)) {
1553 rc = PTR_ERR(handle);
1557 if (lmm != NULL && (*valid & OBD_MD_FLEASIZE) &&
1558 mds_log_op_unlink(obd, lmm, lmm_size,
1559 logcookies, cookies_size) > 0) {
1560 *valid |= OBD_MD_FLCOOKIE;
1565 pending_child->d_fsdata = (void *) &dp;
1566 rc = ll_vfs_unlink(pending_dir, pending_child, mds->mds_vfsmnt);
1568 CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1570 goto out; /* Don't bother updating attrs on unlinked inode */
1573 if (request_body != NULL) {
1574 /* Only start a transaction to write out only the atime if it
1575 * is more out-of-date than the specified limit. If we are
1576 * already going to write out the atime then do it anyway. */
1577 if ((request_body->valid & OBD_MD_FLATIME) &&
1578 ((request_body->atime > LTIME_S(inode->i_atime) +
1579 mds->mds_atime_diff) )) {
1580 LTIME_S(iattr.ia_atime) = request_body->atime;
1581 iattr.ia_valid |= ATTR_ATIME;
1584 /* Store a rough estimate of the file size on the MDS for
1585 * tools like e2scan and HSM that are just using this for *
1586 * rough decision making and will get the proper size later.
1587 * * This is NOT guaranteed to be correct with multiple *
1588 * writers, but is only needed until SOM is done. b=11063 */
1589 if (S_ISREG(inode->i_mode) &&
1590 (request_body->valid & OBD_MD_FLSIZE) &&
1591 (iattr.ia_valid != 0)) {
1592 iattr.ia_size = request_body->size;
1593 iattr.ia_valid |= ATTR_SIZE;
1597 if (iattr.ia_valid != 0) {
1598 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1599 if (IS_ERR(handle)) {
1600 rc = PTR_ERR(handle);
1604 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1606 CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1609 /* If other clients have this file open for write, rc will be > 0 */
1614 l_dput(mfd->mfd_dentry);
1616 if (req != NULL && reply_body != NULL) {
1617 rc = mds_finish_transno(mds, NULL, handle, req, rc, 0, 0);
1618 } else if (handle) {
1619 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1621 CERROR("error committing close: %d\n", err);
1627 switch (cleanup_phase) {
1629 dput(pending_child);
1631 UNLOCK_INODE_MUTEX(pending_dir);
1636 int mds_close(struct ptlrpc_request *req, int offset)
1638 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1639 struct obd_device *obd = req->rq_export->exp_obd;
1640 struct mds_body *body;
1641 struct mds_file_data *mfd;
1642 struct lvfs_run_ctxt saved;
1643 struct inode *inode;
1644 int rc, repsize[4] = { sizeof(struct ptlrpc_body),
1645 sizeof(struct mds_body),
1646 obd->u.mds.mds_max_mdsize,
1647 obd->u.mds.mds_max_cookiesize };
1648 struct mds_body *reply_body;
1649 struct lov_mds_md *lmm;
1651 struct llog_cookie *logcookies;
1655 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1656 lustre_swab_mds_body);
1658 CERROR("Can't unpack body\n");
1659 req->rq_status = -EFAULT;
1660 GOTO(cleanup, rc = -EFAULT);
1662 /*XXX need indicase - close is need to return LOV EA */
1663 body->valid |= OBD_MD_FLEASIZE;
1665 rc = lustre_pack_reply(req, 4, repsize, NULL);
1667 req->rq_status = rc;
1668 /* continue on to drop local open even if we can't send reply */
1670 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1672 CDEBUG(D_INODE, "close req->rep_len %d mdsize %d cookiesize %d\n",
1674 obd->u.mds.mds_max_mdsize, obd->u.mds.mds_max_cookiesize);
1675 mds_counter_incr(req->rq_export, LPROC_MDS_CLOSE);
1677 if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1678 /* do some stuff */ ;
1680 spin_lock(&med->med_open_lock);
1681 mfd = mds_handle2mfd(&body->handle);
1683 spin_unlock(&med->med_open_lock);
1684 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1685 ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1686 req->rq_status = -ESTALE;
1687 GOTO(cleanup, rc = -ESTALE);
1689 /* Remove mfd handle so it can't be found again. We consume mfd_list
1690 * reference here, but still have mds_handle2mfd ref until mfd_close. */
1691 mds_mfd_unlink(mfd, 1);
1692 spin_unlock(&med->med_open_lock);
1694 inode = mfd->mfd_dentry->d_inode;
1695 /* child orphan sem protects orphan_dec_test && is_orphan race */
1696 MDS_DOWN_WRITE_ORPHAN_SEM(inode); /* mds_mfd_close drops this */
1697 if (!obd->obd_recovering &&
1698 mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
1699 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1701 LASSERT(body != NULL);
1703 mds_pack_inode2body(body, inode);
1704 mds_pack_md(obd, req->rq_repmsg, REPLY_REC_OFF + 1, body, inode,
1705 MDS_PACK_MD_LOCK, 0,
1706 req->rq_export->exp_connect_flags);
1709 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1710 reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply_body));
1711 lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, 0);
1712 lmm_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1),
1713 logcookies = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 2, 0);
1714 cookies_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 2);
1715 req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1,
1716 lmm, lmm_size, logcookies, cookies_size,
1717 &reply_body->valid);
1718 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1721 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1722 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1723 req->rq_status = -ENOMEM;
1730 int mds_done_writing(struct ptlrpc_request *req, int offset)
1732 struct mds_body *body;
1733 int rc, size[2] = { sizeof(struct ptlrpc_body),
1734 sizeof(struct mds_body) };
1737 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1739 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1740 lustre_swab_mds_body);
1742 CERROR("Can't unpack body\n");
1743 req->rq_status = -EFAULT;
1747 rc = lustre_pack_reply(req, 2, size, NULL);
1749 req->rq_status = rc;