1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * Author: Phil Schwan <phil@clusterfs.com>
8 * Author: Mike Shaver <shaver@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 # define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
34 # include <linux/buffer_head.h>
35 # include <linux/workqueue.h>
37 # include <linux/locks.h>
40 #include <linux/obd_class.h>
41 #include <linux/lustre_mds.h>
42 #include <linux/obd_lov.h>
43 #include <linux/lustre_fsfilt.h>
44 #include <linux/lprocfs_status.h>
46 #include "mds_internal.h"
48 /* Exported function from this file are:
50 * mds_open - called by the intent handler
51 * mds_close - an rpc handling function
52 * mds_pin - an rpc handling function - which will go away
53 * mds_mfd_close - for force closing files when a client dies
57 * MDS file data handling: file data holds a handle for a file opened
61 static void mds_mfd_addref(void *mfdp)
63 struct mds_file_data *mfd = mfdp;
65 atomic_inc(&mfd->mfd_refcount);
66 CDEBUG(D_INFO, "GETting mfd %p : new refcount %d\n", mfd,
67 atomic_read(&mfd->mfd_refcount));
70 struct mds_file_data *mds_mfd_new(void)
72 struct mds_file_data *mfd;
74 OBD_ALLOC(mfd, sizeof *mfd);
76 CERROR("mds: out of memory\n");
80 atomic_set(&mfd->mfd_refcount, 2);
82 INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
83 class_handle_hash(&mfd->mfd_handle, mds_mfd_addref);
88 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
91 LASSERT(handle != NULL);
92 RETURN(class_handle2object(handle->cookie));
95 static void mds_mfd_put(struct mds_file_data *mfd)
97 CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
98 atomic_read(&mfd->mfd_refcount) - 1);
99 LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
100 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
101 if (atomic_dec_and_test(&mfd->mfd_refcount)) {
102 LASSERT(list_empty(&mfd->mfd_handle.h_link));
103 OBD_FREE(mfd, sizeof *mfd);
107 static void mds_mfd_destroy(struct mds_file_data *mfd)
109 class_handle_unhash(&mfd->mfd_handle);
114 /* Caller must hold mds->mds_epoch_sem */
115 static int mds_alloc_filterdata(struct inode *inode)
117 LASSERT(inode->i_filterdata == NULL);
118 OBD_ALLOC(inode->i_filterdata, sizeof(struct mds_filter_data));
119 if (inode->i_filterdata == NULL)
121 LASSERT(igrab(inode) == inode);
125 /* Caller must hold mds->mds_epoch_sem */
126 static void mds_free_filterdata(struct inode *inode)
128 LASSERT(inode->i_filterdata != NULL);
129 OBD_FREE(inode->i_filterdata, sizeof(struct mds_filter_data));
130 inode->i_filterdata = NULL;
134 /* Write access to a file: executors cause a negative count,
135 * writers a positive count. The semaphore is needed to perform
136 * a check for the sign and then increment or decrement atomically.
138 * This code is closely tied to the allocation of the d_fsdata and the
139 * MDS epoch, so we use the same semaphore for the whole lot.
141 * We could use a different semaphore for each file, if it ever shows
142 * up in a profile, which it won't.
144 * epoch argument is nonzero during recovery */
145 static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
150 down(&mds->mds_epoch_sem);
152 if (atomic_read(&inode->i_writecount) < 0) {
153 up(&mds->mds_epoch_sem);
158 if (MDS_FILTERDATA(inode) && MDS_FILTERDATA(inode)->io_epoch != 0) {
159 CDEBUG(D_INODE, "continuing MDS epoch "LPU64" for ino %lu/%u\n",
160 MDS_FILTERDATA(inode)->io_epoch, inode->i_ino,
161 inode->i_generation);
165 if (inode->i_filterdata == NULL)
166 mds_alloc_filterdata(inode);
167 if (inode->i_filterdata == NULL) {
171 if (epoch > mds->mds_io_epoch)
172 mds->mds_io_epoch = epoch;
175 MDS_FILTERDATA(inode)->io_epoch = mds->mds_io_epoch;
176 CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
177 mds->mds_io_epoch, inode->i_ino, inode->i_generation);
180 atomic_inc(&inode->i_writecount);
181 up(&mds->mds_epoch_sem);
185 /* Returns EAGAIN if the client needs to get size and/or cookies and close
186 * again -- which is never true if the file is about to be unlinked. Otherwise
187 * returns the number of remaining writers. */
188 static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
189 struct mds_body *body, int unlinking)
194 down(&mds->mds_epoch_sem);
195 atomic_dec(&inode->i_writecount);
196 rc = atomic_read(&inode->i_writecount);
200 if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
201 GOTO(out, rc = EAGAIN);
203 mds_free_filterdata(inode);
205 up(&mds->mds_epoch_sem);
209 static int mds_deny_write_access(struct mds_obd *mds, struct inode *inode)
212 down(&mds->mds_epoch_sem);
213 if (atomic_read(&inode->i_writecount) > 0) {
214 up(&mds->mds_epoch_sem);
217 atomic_dec(&inode->i_writecount);
218 up(&mds->mds_epoch_sem);
222 static void mds_allow_write_access(struct inode *inode)
225 atomic_inc(&inode->i_writecount);
228 int mds_query_write_access(struct inode *inode)
231 RETURN(atomic_read(&inode->i_writecount));
234 /* This replaces the VFS mds_dentry_open, it manages mfd and writecount */
235 static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
236 struct vfsmount *mnt, int flags,
237 struct ptlrpc_request *req)
239 struct mds_export_data *med = &req->rq_export->exp_mds_data;
240 struct mds_obd *mds = mds_req2mds(req);
241 struct mds_file_data *mfd;
242 struct mds_body *body;
248 CERROR("mds: out of memory\n");
249 GOTO(cleanup_dentry, error = -ENOMEM);
252 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
254 if (flags & FMODE_WRITE) {
255 /* FIXME: in recovery, need to pass old epoch here */
256 error = mds_get_write_access(mds, dentry->d_inode, 0);
258 GOTO(cleanup_mfd, error);
259 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
260 } else if (flags & FMODE_EXEC) {
261 error = mds_deny_write_access(mds, dentry->d_inode);
263 GOTO(cleanup_mfd, error);
268 /* Mark the file as open to handle open-unlink. */
269 mds_open_orphan_inc(dentry->d_inode);
271 mfd->mfd_mode = flags;
272 mfd->mfd_dentry = dentry;
273 mfd->mfd_xid = req->rq_xid;
275 spin_lock(&med->med_open_lock);
276 list_add(&mfd->mfd_list, &med->med_open_head);
277 spin_unlock(&med->med_open_lock);
280 body->handle.cookie = mfd->mfd_handle.h_cookie;
286 mds_mfd_destroy(mfd);
288 return ERR_PTR(error);
291 static void mds_objids_from_lmm(obd_id *ids, struct lov_mds_md *lmm,
292 struct lov_desc *desc)
295 for (i = 0; i < le32_to_cpu(lmm->lmm_stripe_count); i++) {
296 ids[le32_to_cpu(lmm->lmm_objects[i].l_ost_idx)] =
297 le64_to_cpu(lmm->lmm_objects[i].l_object_id);
301 /* Must be called with i_sem held */
302 static int mds_create_objects(struct ptlrpc_request *req, int offset,
303 struct mds_update_record *rec,
304 struct mds_obd *mds, struct obd_device *obd,
305 struct inode *inode, void **handle, obd_id **ids)
308 struct obd_trans_info oti = { 0 };
309 struct mds_body *body;
310 struct lov_stripe_md *lsm = NULL;
311 struct lov_mds_md *lmm = NULL;
313 int rc, lmm_bufsize, lmm_size;
316 if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
317 !(rec->ur_flags & FMODE_WRITE))
320 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
322 if (!S_ISREG(inode->i_mode))
324 if (body->valid & OBD_MD_FLEASIZE)
327 OBD_ALLOC(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
330 oti.oti_objid = *ids;
333 *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
334 if (IS_ERR(*handle)) {
335 rc = PTR_ERR(*handle);
341 if (rec->ur_fid2->id) {
342 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
343 lmm_size = rec->ur_eadatalen;
344 lmm = rec->ur_eadata;
347 mds_objids_from_lmm(*ids, lmm, &mds->mds_lov_desc);
349 lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
350 lmm_bufsize = req->rq_repmsg->buflens[offset];
352 LASSERT(lmm_bufsize >= lmm_size);
353 memcpy(lmm_buf, lmm, lmm_size);
354 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size);
356 CERROR("open replay failed to set md:%d\n", rc);
362 GOTO(out_ids, rc = -ENOMEM);
363 oa->o_mode = S_IFREG | 0600;
364 oa->o_id = inode->i_ino;
365 oa->o_generation = inode->i_generation;
366 oa->o_uid = 0; /* must have 0 uid / gid on OST */
368 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
369 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
372 obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|OBD_MD_FLMTIME|
375 /* check if things like lstripe/lfs stripe are sending us the ea */
376 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
377 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_osc_exp,
378 0, &lsm, rec->ur_eadata);
383 rc = obd_create(mds->mds_osc_exp, oa, &lsm, &oti);
388 CDEBUG(level, "error creating objects for inode %lu: rc = %d\n",
391 CERROR("obd_create returned invalid rc %d\n", rc);
398 oa->o_size = inode->i_size;
399 obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|
400 OBD_MD_FLMTIME| OBD_MD_FLCTIME| OBD_MD_FLSIZE);
401 rc = obd_setattr(mds->mds_osc_exp, oa, lsm, &oti);
403 CERROR("error setting attrs for inode %lu: rc %d\n",
406 CERROR("obd_setattr returned bad rc %d\n", rc);
413 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
414 obdo_refresh_inode(inode, oa, OBD_MD_FLBLKSZ);
416 LASSERT(lsm && lsm->lsm_object_id);
418 rc = obd_packmd(mds->mds_osc_exp, &lmm, lsm);
419 if (!rec->ur_fid2->id)
420 obd_free_memmd(mds->mds_osc_exp, &lsm);
423 body->eadatasize = rc;
424 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size);
425 lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
426 lmm_bufsize = req->rq_repmsg->buflens[offset];
428 LASSERT(lmm_bufsize >= lmm_size);
430 memcpy(lmm_buf, lmm, lmm_size);
431 obd_free_diskmd(mds->mds_osc_exp, &lmm);
433 oti_free_cookies(&oti);
437 OBD_FREE(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
443 static void reconstruct_open(struct mds_update_record *rec, int offset,
444 struct ptlrpc_request *req,
445 struct lustre_handle *child_lockh)
447 struct ptlrpc_request *oldreq = req->rq_export->exp_outstanding_reply;
448 struct mds_export_data *med = &req->rq_export->exp_mds_data;
449 struct mds_client_data *mcd = med->med_mcd;
450 struct mds_obd *mds = mds_req2mds(req);
451 struct mds_file_data *mfd;
452 struct obd_device *obd = req->rq_export->exp_obd;
453 struct dentry *parent, *child;
454 struct ldlm_reply *rep;
455 struct mds_body *body;
461 LASSERT(offset == 2); /* only called via intent */
462 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
463 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
465 /* copy rc, transno and disp; steal locks */
466 mds_req_from_mcd(req, mcd);
467 intent_set_disposition(rep, mcd->mcd_last_data);
469 /* Only replay if create or open actually happened. */
470 if (!intent_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
472 return; /* error looking up parent or child */
475 parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
476 LASSERT(!IS_ERR(parent));
478 child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
479 LASSERT(!IS_ERR(child));
481 if (!child->d_inode) {
482 GOTO(out_dput, 0); /* child not present to open */
485 /* At this point, we know we have a child. We'll send
486 * it back _unless_ it not created and open failed.
488 if (intent_disposition(rep, DISP_OPEN_OPEN) &&
489 !intent_disposition(rep, DISP_OPEN_CREATE) &&
494 /* get lock (write for O_CREAT, read otherwise) */
496 mds_pack_inode2fid(&body->fid1, child->d_inode);
497 mds_pack_inode2body(body, child->d_inode);
498 if (S_ISREG(child->d_inode->i_mode)) {
499 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
503 LASSERT(rc == req->rq_status);
505 /* If we have LOV EA data, the OST holds size, mtime */
506 if (!(body->valid & OBD_MD_FLEASIZE))
507 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
508 OBD_MD_FLATIME | OBD_MD_FLMTIME);
510 /* XXX need to check this case */
513 /* If we're opening a file without an EA, change to a write
514 lock (unless we already have one). */
516 /* If we have -EEXIST as the status, and we were asked to create
517 * exclusively, we can tell we failed because the file already existed.
519 if (req->rq_status == -EEXIST &&
520 ((rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_EXCL)) ==
521 (MDS_OPEN_CREAT | MDS_OPEN_EXCL))) {
525 /* If we didn't get as far as trying to open, then some locking thing
526 * probably went wrong, and we'll just bail here.
528 if (!intent_disposition(rep, DISP_OPEN_OPEN))
531 /* If we failed, then we must have failed opening, so don't look for
532 * file descriptor or anything, just give the client the bad news.
538 list_for_each(t, &med->med_open_head) {
539 mfd = list_entry(t, struct mds_file_data, mfd_list);
540 if (mfd->mfd_xid == req->rq_xid)
545 if (oldreq != NULL) {
546 /* if we're not recovering, it had better be found */
547 LASSERT(mfd != NULL);
548 } else if (mfd == NULL) {
549 mntget(mds->mds_vfsmnt);
550 CERROR("Re-opened file \n");
551 mfd = mds_dentry_open(child, mds->mds_vfsmnt,
552 rec->ur_flags & ~MDS_OPEN_TRUNC, req);
554 CERROR("mds: out of memory\n");
555 GOTO(out_dput, req->rq_status = -ENOMEM);
567 /* do NOT or the MAY_*'s, you'll get the weakest */
568 static int accmode(int flags)
572 if (flags & FMODE_READ)
574 if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
576 if (flags & FMODE_EXEC)
581 /* Handles object creation, actual opening, and I/O epoch */
582 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
583 struct mds_body *body, int flags, void **handle,
584 struct mds_update_record *rec,struct ldlm_reply *rep)
586 struct mds_obd *mds = mds_req2mds(req);
587 struct obd_device *obd = req->rq_export->exp_obd;
588 struct mds_file_data *mfd = NULL;
589 obd_id *ids = NULL; /* object IDs created */
593 /* atomically create objects if necessary */
594 down(&dchild->d_inode->i_sem);
595 if (S_ISREG(dchild->d_inode->i_mode) &&
596 !(body->valid & OBD_MD_FLEASIZE)) {
597 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
600 up(&dchild->d_inode->i_sem);
605 /* no EA: create objects */
606 rc = mds_create_objects(req, 2, rec, mds, obd,
607 dchild->d_inode, handle, &ids);
609 CERROR("mds_create_objects: rc = %d\n", rc);
610 up(&dchild->d_inode->i_sem);
614 /* If the inode has EA data, then OSTs hold size, mtime */
615 if (S_ISREG(dchild->d_inode->i_mode) &&
616 !(body->valid & OBD_MD_FLEASIZE)) {
617 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
618 OBD_MD_FLATIME | OBD_MD_FLMTIME);
620 up(&dchild->d_inode->i_sem);
622 intent_set_disposition(rep, DISP_OPEN_OPEN);
623 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
625 RETURN(PTR_ERR(mfd));
627 CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
628 mfd->mfd_handle.h_cookie);
630 mds_lov_update_objids(obd, ids);
631 OBD_FREE(ids, sizeof(*ids) * mds->mds_lov_desc.ld_tgt_count);
634 // mds_mfd_destroy(mfd);
638 static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
639 struct mds_body *body, int flags,
640 struct mds_update_record *rec,struct ldlm_reply *rep)
642 struct mds_obd *mds = mds_req2mds(req);
643 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
644 struct dentry *dchild;
645 char fidname[LL_FID_NAMELEN];
650 down(&pending_dir->i_sem);
651 fidlen = ll_fid2str(fidname, fid->id, fid->generation);
652 dchild = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
653 if (IS_ERR(dchild)) {
654 up(&pending_dir->i_sem);
655 rc = PTR_ERR(dchild);
656 CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
660 if (dchild->d_inode != NULL) {
661 up(&pending_dir->i_sem);
662 mds_inode_set_orphan(dchild->d_inode);
663 mds_pack_inode2fid(&body->fid1, dchild->d_inode);
664 mds_pack_inode2body(body, dchild->d_inode);
665 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
666 intent_set_disposition(rep, DISP_LOOKUP_POS);
667 CWARN("Orphan %s found and opened in PENDING directory\n",
672 up(&pending_dir->i_sem);
674 /* We didn't find it in PENDING so it isn't an orphan. See
675 * if it was a regular inode that was previously created. */
676 dchild = mds_fid2dentry(mds, fid, NULL);
678 RETURN(PTR_ERR(dchild));
680 mds_pack_inode2fid(&body->fid1, dchild->d_inode);
681 mds_pack_inode2body(body, dchild->d_inode);
682 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
683 intent_set_disposition(rep, DISP_LOOKUP_POS);
686 rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep);
687 rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
688 req, rc, rep ? rep->lock_policy_res1 : 0);
689 /* XXX what do we do here if mds_finish_transno itself failed? */
695 int mds_pin(struct ptlrpc_request *req)
697 struct obd_device *obd = req->rq_export->exp_obd;
698 struct mds_body *request_body, *reply_body;
699 struct obd_run_ctxt saved;
700 int rc, size = sizeof(*reply_body);
703 request_body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*request_body));
705 rc = lustre_pack_reply(req, 1, &size, NULL);
708 reply_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply_body));
710 push_ctxt(&saved, &obd->obd_ctxt, NULL);
711 rc = mds_open_by_fid(req, &request_body->fid1, reply_body,
712 request_body->flags, NULL, NULL);
713 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
718 /* Get a lock on the ino to sync with creation WRT inode reuse (bug 2029).
719 * If child_lockh is NULL we just get the lock as a barrier to wait for
720 * other holders of this lock, and drop it right away again. */
721 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
722 struct lustre_handle *child_lockh)
724 struct ldlm_res_id child_res_id = { .name = { inode->i_ino } };
725 struct lustre_handle lockh;
729 if (child_lockh == NULL)
730 child_lockh = &lockh;
732 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
733 child_res_id, LDLM_PLAIN, NULL, 0,
734 LCK_EX, &lock_flags, ldlm_completion_ast,
735 mds_blocking_ast, NULL, child_lockh);
737 CERROR("ldlm_cli_enqueue: %d\n", rc);
738 else if (child_lockh == &lockh)
739 ldlm_lock_decref(child_lockh, LCK_EX);
744 int mds_open(struct mds_update_record *rec, int offset,
745 struct ptlrpc_request *req, struct lustre_handle *child_lockh)
747 /* XXX ALLOCATE _something_ - 464 bytes on stack here */
748 struct obd_device *obd = req->rq_export->exp_obd;
749 struct mds_obd *mds = mds_req2mds(req);
750 struct ldlm_reply *rep = NULL;
751 struct mds_body *body = NULL;
752 struct dentry *dchild = NULL, *dparent = NULL;
753 struct mds_export_data *med;
754 struct lustre_handle parent_lockh;
755 int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
756 int parent_mode = LCK_PR;
758 struct dentry_params dp;
761 if (offset == 2) { /* intent */
762 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
763 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
764 } else if (offset == 0) { /* non-intent reint */
765 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
771 MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
773 /* Step 0: If we are passed a fid, then we assume the client already
774 * opened this file and is only replaying the RPC, so we open the
775 * inode by fid (at some large expense in security). */
776 if (rec->ur_fid2->id) {
777 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
781 /* We didn't find the correct inode on disk either, so we
782 * need to re-create it via a regular replay. */
783 LASSERT(rec->ur_flags & MDS_OPEN_CREAT);
785 LASSERT(offset == 2); /* If we got here, we must be called via intent */
787 med = &req->rq_export->exp_mds_data;
788 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
789 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
793 acc_mode = accmode(rec->ur_flags);
795 /* Step 1: Find and lock the parent */
796 if (rec->ur_flags & O_CREAT)
797 parent_mode = LCK_PW;
798 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
799 &parent_lockh, rec->ur_name,
800 rec->ur_namelen - 1);
801 if (IS_ERR(dparent)) {
802 rc = PTR_ERR(dparent);
803 CERROR("parent lookup error %d\n", rc);
806 LASSERT(dparent->d_inode != NULL);
808 cleanup_phase = 1; /* parent dentry and lock */
810 /* Step 2: Lookup the child */
811 dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
812 if (IS_ERR(dchild)) {
813 rc = PTR_ERR(dchild);
814 dchild = NULL; /* don't confuse mds_finish_transno() below */
818 cleanup_phase = 2; /* child dentry */
820 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
822 intent_set_disposition(rep, DISP_LOOKUP_POS);
824 intent_set_disposition(rep, DISP_LOOKUP_NEG);
826 /*Step 3: If the child was negative, and we're supposed to, create it.*/
827 if (dchild->d_inode == NULL) {
828 unsigned long ino = rec->ur_fid2->id;
832 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
833 /* It's negative and we weren't supposed to create it */
834 GOTO(cleanup, rc = -ENOENT);
837 intent_set_disposition(rep, DISP_OPEN_CREATE);
838 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
840 if (IS_ERR(handle)) {
841 rc = PTR_ERR(handle);
845 dchild->d_fsdata = (void *) &dp;
849 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
850 if (dchild->d_fsdata == (void *)(unsigned long)ino)
851 dchild->d_fsdata = NULL;
854 CDEBUG(D_INODE, "error during create: %d\n", rc);
857 inode = dchild->d_inode;
859 LASSERT(ino == inode->i_ino);
860 /* Written as part of setattr */
861 inode->i_generation = rec->ur_fid2->generation;
862 CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
863 inode->i_ino, inode->i_generation);
867 LTIME_S(iattr.ia_atime) = rec->ur_time;
868 LTIME_S(iattr.ia_ctime) = rec->ur_time;
869 LTIME_S(iattr.ia_mtime) = rec->ur_time;
871 iattr.ia_uid = rec->ur_fsuid;
872 if (dparent->d_inode->i_mode & S_ISGID)
873 iattr.ia_gid = dparent->d_inode->i_gid;
875 iattr.ia_gid = rec->ur_fsgid;
877 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
878 ATTR_MTIME | ATTR_CTIME;
880 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
882 CERROR("error on child setattr: rc = %d\n", rc);
884 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
886 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
888 CERROR("error on parent setattr: rc = %d\n", rc);
890 acc_mode = 0; /* Don't check for permissions */
893 LASSERT(!mds_inode_is_orphan(dchild->d_inode));
895 mds_pack_inode2fid(&body->fid1, dchild->d_inode);
896 mds_pack_inode2body(body, dchild->d_inode);
898 if (S_ISREG(dchild->d_inode->i_mode)) {
899 /* Check permissions etc */
900 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
904 /* Can't write to a read-only file */
905 if (IS_RDONLY(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0)
906 GOTO(cleanup, rc = -EPERM);
908 /* An append-only file must be opened in append mode for
910 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
911 ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
912 (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
913 GOTO(cleanup, rc = -EPERM);
916 if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
917 (rec->ur_flags & MDS_OPEN_EXCL)) {
918 /* File already exists, we didn't just create it, and we
919 * were passed O_EXCL; err-or. */
920 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
923 /* if we are following a symlink, don't open */
924 if (S_ISLNK(dchild->d_inode->i_mode))
925 GOTO(cleanup, rc = 0);
927 if ((rec->ur_flags & MDS_OPEN_DIRECTORY) &&
928 !S_ISDIR(dchild->d_inode->i_mode))
929 GOTO(cleanup, rc = -ENOTDIR);
931 /* Step 5: mds_open it */
932 rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
937 rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
938 req, rc, rep ? rep->lock_policy_res1 : 0);
940 switch (cleanup_phase) {
943 int err = vfs_unlink(dparent->d_inode, dchild);
945 CERROR("unlink(%*s) in error path: %d\n",
946 dchild->d_name.len, dchild->d_name.name,
949 } else if (created) {
950 mds_lock_new_child(obd, dchild->d_inode, NULL);
959 ldlm_lock_decref(&parent_lockh, parent_mode);
961 ldlm_put_lock_into_req(req, &parent_lockh, parent_mode);
964 atomic_inc(&mds->mds_open_count);
968 /* Close a "file descriptor" and possibly unlink an orphan from the
971 * If we are being called from mds_disconnect() because the client has
972 * disappeared, then req == NULL and we do not update last_rcvd because
973 * there is nothing that could be recovered by the client at this stage
974 * (it will not even _have_ an entry in last_rcvd anymore).
976 * Returns EAGAIN if the client needs to get more data and re-close. */
977 int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
978 struct mds_file_data *mfd, int unlink_orphan)
980 struct inode *inode = mfd->mfd_dentry->d_inode;
981 char fidname[LL_FID_NAMELEN];
982 int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
983 struct dentry *pending_child = NULL;
984 struct mds_obd *mds = &obd->u.mds;
985 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
987 struct mds_body *request_body = NULL, *reply_body;
988 struct dentry_params dp;
992 request_body = lustre_msg_buf(req->rq_reqmsg, 0,
993 sizeof(*request_body));
994 reply_body = lustre_msg_buf(req->rq_repmsg, 0,
995 sizeof(*reply_body));
998 fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1000 last_orphan = mds_open_orphan_dec_test(inode) &&
1001 mds_inode_is_orphan(inode);
1003 /* this is half of the actual "close" */
1004 if (mfd->mfd_mode & FMODE_WRITE) {
1005 rc = mds_put_write_access(mds, inode, request_body,
1006 last_orphan && unlink_orphan);
1007 } else if (mfd->mfd_mode & FMODE_EXEC) {
1008 mds_allow_write_access(inode);
1011 if (last_orphan && unlink_orphan) {
1012 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
1014 CWARN("destroying orphan object %s\n", fidname);
1016 /* Sadly, there is no easy way to save pending_child from
1017 * mds_reint_unlink() into mfd, so we need to re-lookup,
1018 * but normally it will still be in the dcache. */
1019 pending_dir = mds->mds_pending_dir->d_inode;
1020 down(&pending_dir->i_sem);
1021 cleanup_phase = 1; /* up(i_sem) when finished */
1022 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1024 if (IS_ERR(pending_child))
1025 GOTO(cleanup, rc = PTR_ERR(pending_child));
1026 LASSERT(pending_child->d_inode != NULL);
1028 cleanup_phase = 2; /* dput(pending_child) when finished */
1029 handle = fsfilt_start(obd, pending_dir, FSFILT_OP_UNLINK_LOG,
1031 if (IS_ERR(handle)) {
1032 rc = PTR_ERR(handle);
1037 #ifdef ENABLE_ORPHANS
1039 (reply_body->valid & OBD_MD_FLEASIZE) &&
1040 mds_log_op_unlink(obd, pending_child->d_inode,
1041 req->rq_repmsg, 1) > 0) {
1042 reply_body->valid |= OBD_MD_FLCOOKIE;
1045 pending_child->d_fsdata = (void *) &dp;
1048 if (S_ISDIR(pending_child->d_inode->i_mode))
1049 rc = vfs_rmdir(pending_dir, pending_child);
1051 rc = vfs_unlink(pending_dir, pending_child);
1053 CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1054 } else if (mfd->mfd_mode & FMODE_WRITE && rc == 0) {
1055 /* Update the on-disk attributes if this was the last write
1056 * close, and all information was provided (i.e., rc == 0)
1058 * XXX this should probably be abstracted with mds_reint_setattr
1063 /* XXX can't set block count with fsfilt_setattr (!) */
1064 iattr.ia_valid = ATTR_CTIME | ATTR_ATIME |
1065 ATTR_MTIME | ATTR_SIZE;
1066 iattr.ia_atime = request_body->atime;
1067 iattr.ia_ctime = request_body->ctime;
1068 iattr.ia_mtime = request_body->mtime;
1069 iattr.ia_size = request_body->size;
1070 /* iattr.ia_blocks = request_body->blocks */
1072 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1074 GOTO(cleanup, rc = PTR_ERR(handle));
1075 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1077 CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1080 /* If other clients have this file open for write, rc will be > 0 */
1083 l_dput(mfd->mfd_dentry);
1084 mds_mfd_destroy(mfd);
1087 atomic_dec(&mds->mds_open_count);
1089 rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0);
1090 } else if (handle) {
1091 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1093 CERROR("error committing close: %d\n", err);
1099 switch (cleanup_phase) {
1101 dput(pending_child);
1103 up(&pending_dir->i_sem);
1108 int mds_close(struct ptlrpc_request *req)
1110 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1111 struct obd_device *obd = req->rq_export->exp_obd;
1112 struct mds_body *body;
1113 struct mds_file_data *mfd;
1114 struct obd_run_ctxt saved;
1115 struct inode *inode;
1116 int rc, repsize[3] = {sizeof(struct mds_body),
1117 obd->u.mds.mds_max_mdsize,
1118 obd->u.mds.mds_max_cookiesize};
1121 rc = lustre_pack_reply(req, 3, repsize, NULL);
1123 CERROR("lustre_pack_reply: rc = %d\n", rc);
1124 req->rq_status = rc;
1126 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1129 body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1131 CERROR("Can't unpack body\n");
1132 req->rq_status = -EFAULT;
1136 if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1137 /* do some stuff */ ;
1139 mfd = mds_handle2mfd(&body->handle);
1141 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1142 ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1143 req->rq_status = -ESTALE;
1147 inode = mfd->mfd_dentry->d_inode;
1148 if (mds_inode_is_orphan(inode) && mds_open_orphan_count(inode) == 1) {
1149 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1150 LASSERT(body != NULL);
1152 mds_pack_inode2fid(&body->fid1, inode);
1153 mds_pack_inode2body(body, inode);
1154 mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
1156 spin_lock(&med->med_open_lock);
1157 list_del(&mfd->mfd_list);
1158 spin_unlock(&med->med_open_lock);
1160 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1161 req->rq_status = mds_mfd_close(rc ? NULL : req, obd, mfd, 1);
1162 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1164 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1165 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1166 req->rq_status = -ENOMEM;
1175 int mds_done_writing(struct ptlrpc_request *req)
1177 struct mds_body *body;
1178 int rc, size = sizeof(struct mds_body);
1181 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1183 body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1185 CERROR("Can't unpack body\n");
1186 req->rq_status = -EFAULT;
1190 rc = lustre_pack_reply(req, 1, &size, NULL);
1192 CERROR("lustre_pack_reply: rc = %d\n", rc);
1193 req->rq_status = rc;