Whamcloud - gitweb
Land b_smallfix onto HEAD (20040428_2142)
[fs/lustre-release.git] / lustre / mds / mds_open.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
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>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
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.
15  *
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.
20  *
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.
24  */
25
26 #ifndef EXPORT_SYMTAB
27 # define EXPORT_SYMTAB
28 #endif
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/version.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/version.h>
35 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
36 # include <linux/buffer_head.h>
37 # include <linux/workqueue.h>
38 #else
39 # include <linux/locks.h>
40 #endif
41
42 #include <linux/obd_class.h>
43 #include <linux/lustre_mds.h>
44 #include <linux/obd_lov.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/lprocfs_status.h>
47
48 #include "mds_internal.h"
49
50 /* Exported function from this file are:
51  *
52  * mds_open - called by the intent handler
53  * mds_close - an rpc handling function
54  * mds_pin - an rpc handling function - which will go away
55  * mds_mfd_close - for force closing files when a client dies
56  */
57
58 /*
59  * MDS file data handling: file data holds a handle for a file opened
60  * by a client.
61  */
62
63 static void mds_mfd_addref(void *mfdp)
64 {
65         struct mds_file_data *mfd = mfdp;
66
67         atomic_inc(&mfd->mfd_refcount);
68         CDEBUG(D_INFO, "GETting mfd %p : new refcount %d\n", mfd,
69                atomic_read(&mfd->mfd_refcount));
70 }
71
72 struct mds_file_data *mds_mfd_new(void)
73 {
74         struct mds_file_data *mfd;
75
76         OBD_ALLOC(mfd, sizeof *mfd);
77         if (mfd == NULL) {
78                 CERROR("mds: out of memory\n");
79                 return NULL;
80         }
81
82         atomic_set(&mfd->mfd_refcount, 2);
83
84         INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
85         class_handle_hash(&mfd->mfd_handle, mds_mfd_addref);
86
87         return mfd;
88 }
89
90 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
91 {
92         ENTRY;
93         LASSERT(handle != NULL);
94         RETURN(class_handle2object(handle->cookie));
95 }
96
97 static void mds_mfd_put(struct mds_file_data *mfd)
98 {
99         CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
100                atomic_read(&mfd->mfd_refcount) - 1);
101         LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
102                 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
103         if (atomic_dec_and_test(&mfd->mfd_refcount)) {
104                 LASSERT(list_empty(&mfd->mfd_handle.h_link));
105                 OBD_FREE(mfd, sizeof *mfd);
106         }
107 }
108
109 static void mds_mfd_destroy(struct mds_file_data *mfd)
110 {
111         class_handle_unhash(&mfd->mfd_handle);
112         mds_mfd_put(mfd);
113 }
114
115
116 /* Caller must hold mds->mds_epoch_sem */
117 static int mds_alloc_filterdata(struct inode *inode)
118 {
119         LASSERT(inode->i_filterdata == NULL);
120         OBD_ALLOC(inode->i_filterdata, sizeof(struct mds_filter_data));
121         if (inode->i_filterdata == NULL)
122                 return -ENOMEM;
123         LASSERT(igrab(inode) == inode);
124         return 0;
125 }
126
127 /* Caller must hold mds->mds_epoch_sem */
128 static void mds_free_filterdata(struct inode *inode)
129 {
130         LASSERT(inode->i_filterdata != NULL);
131         OBD_FREE(inode->i_filterdata, sizeof(struct mds_filter_data));
132         inode->i_filterdata = NULL;
133         iput(inode);
134 }
135
136 /* Write access to a file: executors cause a negative count,
137  * writers a positive count.  The semaphore is needed to perform
138  * a check for the sign and then increment or decrement atomically.
139  *
140  * This code is closely tied to the allocation of the d_fsdata and the
141  * MDS epoch, so we use the same semaphore for the whole lot.
142  *
143  * We could use a different semaphore for each file, if it ever shows
144  * up in a profile, which it won't.
145  *
146  * epoch argument is nonzero during recovery */
147 static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
148                                 __u64 epoch)
149 {
150         int rc = 0;
151
152         down(&mds->mds_epoch_sem);
153
154         if (atomic_read(&inode->i_writecount) < 0) {
155                 up(&mds->mds_epoch_sem);
156                 RETURN(-ETXTBSY);
157         }
158
159
160         if (MDS_FILTERDATA(inode) && MDS_FILTERDATA(inode)->io_epoch != 0) {
161                 CDEBUG(D_INODE, "continuing MDS epoch "LPU64" for ino %lu/%u\n",
162                        MDS_FILTERDATA(inode)->io_epoch, inode->i_ino,
163                        inode->i_generation);
164                 goto out;
165         }
166
167         if (inode->i_filterdata == NULL)
168                 mds_alloc_filterdata(inode);
169         if (inode->i_filterdata == NULL) {
170                 rc = -ENOMEM;
171                 goto out;
172         }
173         if (epoch > mds->mds_io_epoch)
174                 mds->mds_io_epoch = epoch;
175         else
176                 mds->mds_io_epoch++;
177         MDS_FILTERDATA(inode)->io_epoch = mds->mds_io_epoch;
178         CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
179                mds->mds_io_epoch, inode->i_ino, inode->i_generation);
180  out:
181         if (rc == 0)
182                 atomic_inc(&inode->i_writecount);
183         up(&mds->mds_epoch_sem);
184         return rc;
185 }
186
187 /* Returns EAGAIN if the client needs to get size and/or cookies and close
188  * again -- which is never true if the file is about to be unlinked.  Otherwise
189  * returns the number of remaining writers. */
190 static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
191                                 struct mds_body *body, int unlinking)
192 {
193         int rc = 0;
194         ENTRY;
195
196         down(&mds->mds_epoch_sem);
197         atomic_dec(&inode->i_writecount);
198         rc = atomic_read(&inode->i_writecount);
199         if (rc > 0)
200                 GOTO(out, rc);
201 #if 0
202         if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
203                 GOTO(out, rc = EAGAIN);
204 #endif
205         mds_free_filterdata(inode);
206  out:
207         up(&mds->mds_epoch_sem);
208         return rc;
209 }
210
211 static int mds_deny_write_access(struct mds_obd *mds, struct inode *inode)
212 {
213         ENTRY;
214         down(&mds->mds_epoch_sem);
215         if (atomic_read(&inode->i_writecount) > 0) {
216                 up(&mds->mds_epoch_sem);
217                 RETURN(-ETXTBSY);
218         }
219         atomic_dec(&inode->i_writecount);
220         up(&mds->mds_epoch_sem);
221         RETURN(0);
222 }
223
224 static void mds_allow_write_access(struct inode *inode)
225 {
226         ENTRY;
227         atomic_inc(&inode->i_writecount);
228 }
229
230 int mds_query_write_access(struct inode *inode)
231 {
232         ENTRY;
233         RETURN(atomic_read(&inode->i_writecount));
234 }
235
236 /* This replaces the VFS mds_dentry_open, it manages mfd and writecount */
237 static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
238                                              struct vfsmount *mnt, int flags,
239                                              struct ptlrpc_request *req)
240 {
241         struct mds_export_data *med = &req->rq_export->exp_mds_data;
242         struct mds_obd *mds = mds_req2mds(req);
243         struct mds_file_data *mfd;
244         struct mds_body *body;
245         int error;
246         ENTRY;
247
248         mfd = mds_mfd_new();
249         if (mfd == NULL) {
250                 CERROR("mds: out of memory\n");
251                 GOTO(cleanup_dentry, error = -ENOMEM);
252         }
253
254         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
255
256         if (flags & FMODE_WRITE) {
257                 /* FIXME: in recovery, need to pass old epoch here */
258                 error = mds_get_write_access(mds, dentry->d_inode, 0);
259                 if (error)
260                         GOTO(cleanup_mfd, error);
261                 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
262         } else if (flags & FMODE_EXEC) {
263                 error = mds_deny_write_access(mds, dentry->d_inode);
264                 if (error)
265                         GOTO(cleanup_mfd, error);
266         }
267
268         dget(dentry);
269
270         /* Mark the file as open to handle open-unlink. */
271         mds_open_orphan_inc(dentry->d_inode);
272
273         mfd->mfd_mode = flags;
274         mfd->mfd_dentry = dentry;
275         mfd->mfd_xid = req->rq_xid;
276
277         spin_lock(&med->med_open_lock);
278         list_add(&mfd->mfd_list, &med->med_open_head);
279         spin_unlock(&med->med_open_lock);
280         mds_mfd_put(mfd);
281
282         body->handle.cookie = mfd->mfd_handle.h_cookie;
283
284         RETURN(mfd);
285
286 cleanup_mfd:
287         mds_mfd_put(mfd);
288         mds_mfd_destroy(mfd);
289 cleanup_dentry:
290         return ERR_PTR(error);
291 }
292
293 static void mds_objids_from_lmm(obd_id *ids, struct lov_mds_md *lmm,
294                                 struct lov_desc *desc)
295 {
296         int i;
297         for (i = 0; i < le32_to_cpu(lmm->lmm_stripe_count); i++) {
298                 ids[le32_to_cpu(lmm->lmm_objects[i].l_ost_idx)] =
299                         le64_to_cpu(lmm->lmm_objects[i].l_object_id);
300         }
301 }
302
303 /* Must be called with i_sem held */
304 static int mds_create_objects(struct ptlrpc_request *req, int offset,
305                               struct mds_update_record *rec,
306                               struct mds_obd *mds, struct obd_device *obd,
307                               struct dentry *dchild, void **handle, 
308                               obd_id **ids)
309 {
310         struct obdo *oa;
311         struct obd_trans_info oti = { 0 };
312         struct mds_body *body;
313         struct lov_stripe_md *lsm = NULL;
314         struct lov_mds_md *lmm = NULL;
315         struct inode *inode = dchild->d_inode;
316         void *lmm_buf;
317         int rc, lmm_bufsize, lmm_size;
318         ENTRY;
319
320         if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
321             !(rec->ur_flags & FMODE_WRITE))
322                 RETURN(0);
323
324         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
325
326         if (!S_ISREG(inode->i_mode))
327                 RETURN(0);
328         if (body->valid & OBD_MD_FLEASIZE)
329                 RETURN(0);
330
331         OBD_ALLOC(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
332         if (*ids == NULL)
333                 RETURN(-ENOMEM);
334         oti.oti_objid = *ids;
335
336         if (*handle == NULL)
337                 *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
338         if (IS_ERR(*handle)) {
339                 rc = PTR_ERR(*handle);
340                 *handle = NULL;
341                 GOTO(out_ids, rc);
342         }
343
344         /* replay case */
345         if(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
346                 LASSERT (rec->ur_fid2->id);
347                 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
348                 lmm_size = rec->ur_eadatalen;
349                 lmm = rec->ur_eadata;
350                 LASSERT(lmm);
351
352                 mds_objids_from_lmm(*ids, lmm, &mds->mds_lov_desc);
353
354                 lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
355                 lmm_bufsize = req->rq_repmsg->buflens[offset];
356                 LASSERT(lmm_buf);
357                 LASSERT(lmm_bufsize >= lmm_size);
358                 memcpy(lmm_buf, lmm, lmm_size);
359                 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size);
360                 if (rc)
361                         CERROR("open replay failed to set md:%d\n", rc);
362                 RETURN(0);
363         }
364
365         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
366                 GOTO(out_ids, rc = -ENOMEM);
367
368         oa = obdo_alloc();
369         if (oa == NULL)
370                 GOTO(out_ids, rc = -ENOMEM);
371         oa->o_mode = S_IFREG | 0600;
372         oa->o_id = inode->i_ino;
373         oa->o_generation = inode->i_generation;
374         oa->o_uid = 0; /* must have 0 uid / gid on OST */
375         oa->o_gid = 0;
376         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
377                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
378         oa->o_size = 0;
379
380         obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|OBD_MD_FLMTIME|
381                         OBD_MD_FLCTIME);
382
383         if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
384                 /* check if things like lfs setstripe are sending us the ea */
385                 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
386                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
387                                            mds->mds_osc_exp,
388                                            0, &lsm, rec->ur_eadata);
389                         if (rc)
390                                 GOTO(out_oa, rc);
391                 } else {
392                         OBD_ALLOC(lmm, mds->mds_max_mdsize);
393                         if (lmm == NULL)
394                                 GOTO(out_oa, rc = -ENOMEM);
395
396                         lmm_size = mds->mds_max_mdsize;
397                         rc = mds_get_md(obd, dchild->d_parent->d_inode,
398                                         lmm, &lmm_size, 1);
399                         if (rc > 0)
400                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
401                                                    mds->mds_osc_exp, 
402                                                    0, &lsm, lmm);
403                         OBD_FREE(lmm, mds->mds_max_mdsize);
404                         if (rc)
405                                 GOTO(out_oa, rc);
406                 }
407                 rc = obd_create(mds->mds_osc_exp, oa, &lsm, &oti);
408                 if (rc) {
409                         int level = D_ERROR;
410                         if (rc == -ENOSPC)
411                                 level = D_INODE;
412                         CDEBUG(level, "error creating objects for "
413                                       "inode %lu: rc = %d\n",
414                                inode->i_ino, rc);
415                         if (rc > 0) {
416                                 CERROR("obd_create returned invalid "
417                                        "rc %d\n", rc);
418                                 rc = -EIO;
419                         }
420                         GOTO(out_oa, rc);
421                 }
422         } else {
423                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_osc_exp,
424                                    0, &lsm, rec->ur_eadata);
425                 if (rc) {
426                         GOTO(out_oa, rc);
427                 }
428                 lsm->lsm_object_id = oa->o_id;
429         }
430         if (inode->i_size) {
431                 oa->o_size = inode->i_size;
432                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|
433                                 OBD_MD_FLMTIME| OBD_MD_FLCTIME| OBD_MD_FLSIZE);
434                 rc = obd_setattr(mds->mds_osc_exp, oa, lsm, &oti);
435                 if (rc) {
436                         CERROR("error setting attrs for inode %lu: rc %d\n",
437                                inode->i_ino, rc);
438                         if (rc > 0) {
439                                 CERROR("obd_setattr returned bad rc %d\n", rc);
440                                 rc = -EIO;
441                         }
442                         GOTO(out_oa, rc);
443                 }
444         }
445
446         body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
447         obdo_refresh_inode(inode, oa, OBD_MD_FLBLKSZ);
448
449         LASSERT(lsm && lsm->lsm_object_id);
450         lmm = NULL;
451         rc = obd_packmd(mds->mds_osc_exp, &lmm, lsm);
452         if (!rec->ur_fid2->id)
453                 obd_free_memmd(mds->mds_osc_exp, &lsm);
454         LASSERT(rc >= 0);
455         lmm_size = rc;
456         body->eadatasize = rc;
457         rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size);
458         lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, 0);
459         lmm_bufsize = req->rq_repmsg->buflens[offset];
460         LASSERT(lmm_buf);
461         LASSERT(lmm_bufsize >= lmm_size);
462
463         memcpy(lmm_buf, lmm, lmm_size);
464         obd_free_diskmd(mds->mds_osc_exp, &lmm);
465  out_oa:
466         oti_free_cookies(&oti);
467         obdo_free(oa);
468  out_ids:
469         if (rc) {
470                 OBD_FREE(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
471                 *ids = NULL;
472         }
473         RETURN(rc);
474 }
475
476 static void reconstruct_open(struct mds_update_record *rec, int offset,
477                              struct ptlrpc_request *req,
478                              struct lustre_handle *child_lockh)
479 {
480         struct mds_export_data *med = &req->rq_export->exp_mds_data;
481         struct mds_client_data *mcd = med->med_mcd;
482         struct mds_obd *mds = mds_req2mds(req);
483         struct mds_file_data *mfd;
484         struct obd_device *obd = req->rq_export->exp_obd;
485         struct dentry *parent, *child;
486         struct ldlm_reply *rep;
487         struct mds_body *body;
488         int rc;
489         struct list_head *t;
490         int put_child = 1;
491         ENTRY;
492
493         LASSERT(offset == 2);                  /* only called via intent */
494         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
495         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
496
497         /* copy rc, transno and disp; steal locks */
498         mds_req_from_mcd(req, mcd);
499         intent_set_disposition(rep, mcd->mcd_last_data);
500
501         /* Only replay if create or open actually happened. */
502         if (!intent_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
503                 EXIT;
504                 return; /* error looking up parent or child */
505         }
506
507         parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
508         LASSERT(!IS_ERR(parent));
509
510         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
511         LASSERT(!IS_ERR(child));
512
513         if (!child->d_inode) {
514                 GOTO(out_dput, 0); /* child not present to open */
515         }
516
517         /* At this point, we know we have a child. We'll send
518          * it back _unless_ it not created and open failed.
519          */
520         if (intent_disposition(rep, DISP_OPEN_OPEN) &&
521             !intent_disposition(rep, DISP_OPEN_CREATE) &&
522             req->rq_status) {
523                 GOTO(out_dput, 0);
524         }
525
526         /* get lock (write for O_CREAT, read otherwise) */
527
528         mds_pack_inode2fid(&body->fid1, child->d_inode);
529         mds_pack_inode2body(body, child->d_inode);
530         if (S_ISREG(child->d_inode->i_mode)) {
531                 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
532                                  child->d_inode, 1);
533
534                 if (rc)
535                         LASSERT(rc == req->rq_status);
536
537                 /* If we have LOV EA data, the OST holds size, mtime */
538                 if (!(body->valid & OBD_MD_FLEASIZE))
539                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
540                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
541         } else {
542                 /* XXX need to check this case */
543         }
544
545         /* If we're opening a file without an EA, change to a write
546            lock (unless we already have one). */
547
548         /* If we have -EEXIST as the status, and we were asked to create
549          * exclusively, we can tell we failed because the file already existed.
550          */
551         if (req->rq_status == -EEXIST &&
552             ((rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_EXCL)) ==
553              (MDS_OPEN_CREAT | MDS_OPEN_EXCL))) {
554                 GOTO(out_dput, 0);
555         }
556
557         /* If we didn't get as far as trying to open, then some locking thing
558          * probably went wrong, and we'll just bail here.
559          */
560         if (!intent_disposition(rep, DISP_OPEN_OPEN))
561                 GOTO(out_dput, 0);
562
563         /* If we failed, then we must have failed opening, so don't look for
564          * file descriptor or anything, just give the client the bad news.
565          */
566         if (req->rq_status)
567                 GOTO(out_dput, 0);
568
569         mfd = NULL;
570         list_for_each(t, &med->med_open_head) {
571                 mfd = list_entry(t, struct mds_file_data, mfd_list);
572                 if (mfd->mfd_xid == req->rq_xid)
573                         break;
574                 mfd = NULL;
575         }
576
577         /* #warning "XXX fixme" bug 2991 */
578         /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
579          * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
580          * to detect a re-open */
581         if (mfd == NULL) {
582                 mntget(mds->mds_vfsmnt);
583                 CERROR("Re-opened file \n");
584                 mfd = mds_dentry_open(child, mds->mds_vfsmnt,
585                                       rec->ur_flags & ~MDS_OPEN_TRUNC, req);
586                 if (!mfd) {
587                         CERROR("mds: out of memory\n");
588                         GOTO(out_dput, req->rq_status = -ENOMEM);
589                 }
590                 put_child = 0;
591         }
592
593  out_dput:
594         if (put_child)
595                 l_dput(child);
596         l_dput(parent);
597         EXIT;
598 }
599
600 /* do NOT or the MAY_*'s, you'll get the weakest */
601 static int accmode(int flags)
602 {
603         int res = 0;
604
605         if (flags & FMODE_READ)
606                 res = MAY_READ;
607         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
608                 res |= MAY_WRITE;
609         if (flags & FMODE_EXEC)
610                 res = MAY_EXEC;
611         return res;
612 }
613
614 /* Handles object creation, actual opening, and I/O epoch */
615 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
616                            struct mds_body *body, int flags, void **handle,
617                            struct mds_update_record *rec,struct ldlm_reply *rep)
618 {
619         struct mds_obd *mds = mds_req2mds(req);
620         struct obd_device *obd = req->rq_export->exp_obd;
621         struct mds_file_data *mfd = NULL;
622         obd_id *ids = NULL; /* object IDs created */
623         int rc = 0;
624         ENTRY;
625
626         /* atomically create objects if necessary */
627         down(&dchild->d_inode->i_sem);
628         if (S_ISREG(dchild->d_inode->i_mode) &&
629             !(body->valid & OBD_MD_FLEASIZE)) {
630                 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
631                                  dchild->d_inode, 0);
632                 if (rc) {
633                         up(&dchild->d_inode->i_sem);
634                         RETURN(rc);
635                 }
636         }
637         if (rec != NULL) {
638                 /* no EA: create objects */
639                 rc = mds_create_objects(req, 2, rec, mds, obd,
640                                         dchild, handle, &ids);
641                 if (rc) {
642                         CERROR("mds_create_objects: rc = %d\n", rc);
643                         up(&dchild->d_inode->i_sem);
644                         RETURN(rc);
645                 }
646         }
647         /* If the inode has EA data, then OSTs hold size, mtime */
648         if (S_ISREG(dchild->d_inode->i_mode) &&
649             !(body->valid & OBD_MD_FLEASIZE)) {
650                 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
651                                 OBD_MD_FLATIME | OBD_MD_FLMTIME);
652         }
653         up(&dchild->d_inode->i_sem);
654
655         intent_set_disposition(rep, DISP_OPEN_OPEN);
656         mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
657         if (IS_ERR(mfd))
658                 RETURN(PTR_ERR(mfd));
659
660         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
661                mfd->mfd_handle.h_cookie);
662         if (ids != NULL) {
663                 mds_lov_update_objids(obd, ids);
664                 OBD_FREE(ids, sizeof(*ids) * mds->mds_lov_desc.ld_tgt_count);
665         }
666         //if (rc)
667         //        mds_mfd_destroy(mfd);
668         RETURN(rc);
669 }
670
671 static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
672                            struct mds_body *body, int flags,
673                            struct mds_update_record *rec,struct ldlm_reply *rep)
674 {
675         struct mds_obd *mds = mds_req2mds(req);
676         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
677         struct dentry *dchild;
678         char fidname[LL_FID_NAMELEN];
679         int fidlen = 0, rc;
680         void *handle = NULL;
681         ENTRY;
682
683         down(&pending_dir->i_sem);
684         fidlen = ll_fid2str(fidname, fid->id, fid->generation);
685         dchild = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
686         if (IS_ERR(dchild)) {
687                 up(&pending_dir->i_sem);
688                 rc = PTR_ERR(dchild);
689                 CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
690                 RETURN(rc);
691         }
692
693         if (dchild->d_inode != NULL) {
694                 up(&pending_dir->i_sem);
695                 mds_inode_set_orphan(dchild->d_inode);
696                 mds_pack_inode2fid(&body->fid1, dchild->d_inode);
697                 mds_pack_inode2body(body, dchild->d_inode);
698                 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
699                 intent_set_disposition(rep, DISP_LOOKUP_POS);
700                 CWARN("Orphan %s found and opened in PENDING directory\n",
701                        fidname);
702                 goto open;
703         }
704         dput(dchild);
705         up(&pending_dir->i_sem);
706
707         /* We didn't find it in PENDING so it isn't an orphan.  See
708          * if it was a regular inode that was previously created. */
709         dchild = mds_fid2dentry(mds, fid, NULL);
710         if (IS_ERR(dchild))
711                 RETURN(PTR_ERR(dchild));
712
713         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
714         mds_pack_inode2body(body, dchild->d_inode);
715         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
716         intent_set_disposition(rep, DISP_LOOKUP_POS);
717
718  open:
719         rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep);
720         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
721                                 req, rc, rep ? rep->lock_policy_res1 : 0);
722         /* XXX what do we do here if mds_finish_transno itself failed? */
723
724         l_dput(dchild);
725         RETURN(rc);
726 }
727
728 int mds_pin(struct ptlrpc_request *req)
729 {
730         struct obd_device *obd = req->rq_export->exp_obd;
731         struct mds_body *request_body, *reply_body;
732         struct obd_run_ctxt saved;
733         int rc, size = sizeof(*reply_body);
734         ENTRY;
735
736         request_body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*request_body));
737
738         rc = lustre_pack_reply(req, 1, &size, NULL);
739         if (rc)
740                 RETURN(rc);
741         reply_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply_body));
742
743         push_ctxt(&saved, &obd->obd_ctxt, NULL);
744         rc = mds_open_by_fid(req, &request_body->fid1, reply_body,
745                              request_body->flags, NULL, NULL);
746         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
747
748         RETURN(rc);
749 }
750
751 /*  Get a lock on the ino to sync with creation WRT inode reuse (bug 2029).
752  *  If child_lockh is NULL we just get the lock as a barrier to wait for
753  *  other holders of this lock, and drop it right away again. */
754 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
755                        struct lustre_handle *child_lockh)
756 {
757         struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
758         struct lustre_handle lockh;
759         int lock_flags = 0;
760         int rc;
761
762         if (child_lockh == NULL)
763                 child_lockh = &lockh;
764
765         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, child_res_id,
766                               LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
767                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
768                               NULL, 0, NULL, child_lockh);
769         if (rc != ELDLM_OK)
770                 CERROR("ldlm_cli_enqueue: %d\n", rc);
771         else if (child_lockh == &lockh)
772                 ldlm_lock_decref(child_lockh, LCK_EX);
773
774         RETURN(rc);
775 }
776
777 int mds_open(struct mds_update_record *rec, int offset,
778              struct ptlrpc_request *req, struct lustre_handle *child_lockh)
779 {
780         /* XXX ALLOCATE _something_ - 464 bytes on stack here */
781         struct obd_device *obd = req->rq_export->exp_obd;
782         struct mds_obd *mds = mds_req2mds(req);
783         struct ldlm_reply *rep = NULL;
784         struct mds_body *body = NULL;
785         struct dentry *dchild = NULL, *dparent = NULL;
786         struct mds_export_data *med;
787         struct lustre_handle parent_lockh;
788         int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
789         int parent_mode = LCK_PR;
790         void *handle = NULL;
791         struct dentry_params dp;
792         ENTRY;
793
794         if (offset == 2) { /* intent */
795                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
796                 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
797         } else if (offset == 0) { /* non-intent reint */
798                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
799         } else {
800                 body = NULL;
801                 LBUG();
802         }
803
804         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
805
806         /* Step 0: If we are passed a fid, then we assume the client already
807          * opened this file and is only replaying the RPC, so we open the
808          * inode by fid (at some large expense in security). */
809         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
810                 DEBUG_REQ(D_HA, req, "open replay, disp: "LPX64"\n",
811                           rep->lock_policy_res1);
812
813                 LASSERT(rec->ur_fid2->id);
814
815                 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
816                                      rec, rep);
817                 if (rc != -ENOENT)
818                         RETURN(rc);
819                 /* We didn't find the correct inode on disk either, so we
820                  * need to re-create it via a regular replay. */
821                 LASSERT(rec->ur_flags & MDS_OPEN_CREAT);
822         } else {
823                 LASSERT(!rec->ur_fid2->id);
824         }
825
826         LASSERT(offset == 2); /* If we got here, we must be called via intent */
827
828         med = &req->rq_export->exp_mds_data;
829         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
830                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
831                 RETURN(-ENOMEM);
832         }
833
834         acc_mode = accmode(rec->ur_flags);
835
836         /* Step 1: Find and lock the parent */
837         if (rec->ur_flags & O_CREAT) {
838                 /* XXX Well, in fact we only need this lock mode change if
839                    in addition to O_CREAT, the file does not exist.
840                    But we do not know if it exists or not yet */
841                 parent_mode = LCK_PW;
842         }
843         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
844                                         &parent_lockh, rec->ur_name,
845                                         rec->ur_namelen - 1,
846                                         MDS_INODELOCK_UPDATE);
847         if (IS_ERR(dparent)) {
848                 rc = PTR_ERR(dparent);
849                 CERROR("parent lookup error %d\n", rc);
850                 GOTO(cleanup, rc);
851         }
852         LASSERT(dparent->d_inode != NULL);
853
854         cleanup_phase = 1; /* parent dentry and lock */
855
856         /* Step 2: Lookup the child */
857         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
858         if (IS_ERR(dchild)) {
859                 rc = PTR_ERR(dchild);
860                 dchild = NULL; /* don't confuse mds_finish_transno() below */
861                 GOTO(cleanup, rc);
862         }
863
864         cleanup_phase = 2; /* child dentry */
865
866         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
867         if (dchild->d_inode)
868                 intent_set_disposition(rep, DISP_LOOKUP_POS);
869         else
870                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
871
872         /*Step 3: If the child was negative, and we're supposed to, create it.*/
873         if (dchild->d_inode == NULL) {
874                 unsigned long ino = rec->ur_fid2->id;
875                 struct iattr iattr;
876                 struct inode *inode;
877
878                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
879                         /* It's negative and we weren't supposed to create it */
880                         GOTO(cleanup, rc = -ENOENT);
881                 }
882
883                 intent_set_disposition(rep, DISP_OPEN_CREATE);
884                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
885                                       NULL);
886                 if (IS_ERR(handle)) {
887                         rc = PTR_ERR(handle);
888                         handle = NULL;
889                         GOTO(cleanup, rc);
890                 }
891                 dchild->d_fsdata = (void *) &dp;
892                 dp.p_ptr = req;
893                 dp.p_inum = ino;
894
895                 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
896                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
897                         dchild->d_fsdata = NULL;
898
899                 if (rc) {
900                         CDEBUG(D_INODE, "error during create: %d\n", rc);
901                         GOTO(cleanup, rc);
902                 }
903                 inode = dchild->d_inode;
904                 if (ino) {
905                         LASSERT(ino == inode->i_ino);
906                         /* Written as part of setattr */
907                         inode->i_generation = rec->ur_fid2->generation;
908                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
909                                inode->i_ino, inode->i_generation);
910                 }
911
912                 created = 1;
913                 LTIME_S(iattr.ia_atime) = rec->ur_time;
914                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
915                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
916
917                 iattr.ia_uid = rec->ur_fsuid;
918                 if (dparent->d_inode->i_mode & S_ISGID)
919                         iattr.ia_gid = dparent->d_inode->i_gid;
920                 else
921                         iattr.ia_gid = rec->ur_fsgid;
922
923                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
924                         ATTR_MTIME | ATTR_CTIME;
925
926                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
927                 if (rc)
928                         CERROR("error on child setattr: rc = %d\n", rc);
929
930                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
931
932                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
933                 if (rc)
934                         CERROR("error on parent setattr: rc = %d\n", rc);
935
936                 acc_mode = 0;                  /* Don't check for permissions */
937         }
938
939         LASSERT(!mds_inode_is_orphan(dchild->d_inode));
940
941         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
942         mds_pack_inode2body(body, dchild->d_inode);
943
944         if (S_ISREG(dchild->d_inode->i_mode)) {
945                 /* Check permissions etc */
946                 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
947                 if (rc != 0)
948                         GOTO(cleanup, rc);
949
950                 /* Can't write to a read-only file */
951                 if (IS_RDONLY(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0)
952                         GOTO(cleanup, rc = -EPERM);
953
954                 /* An append-only file must be opened in append mode for
955                  * writing */
956                 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
957                     ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
958                      (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
959                         GOTO(cleanup, rc = -EPERM);
960         }
961
962         if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
963             (rec->ur_flags & MDS_OPEN_EXCL)) {
964                 /* File already exists, we didn't just create it, and we
965                  * were passed O_EXCL; err-or. */
966                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
967         }
968
969         /* if we are following a symlink, don't open */
970         if (S_ISLNK(dchild->d_inode->i_mode))
971                 GOTO(cleanup, rc = 0);
972
973         if ((rec->ur_flags & MDS_OPEN_DIRECTORY) &&
974             !S_ISDIR(dchild->d_inode->i_mode))
975                 GOTO(cleanup, rc = -ENOTDIR);
976
977         if (S_ISDIR(dchild->d_inode->i_mode)) {
978                 if (rec->ur_flags & MDS_OPEN_CREAT ||
979                     rec->ur_flags & FMODE_WRITE) {
980                         /*we are tryying to create or write a exist dir*/
981                         GOTO(cleanup, rc = -EISDIR);
982                 }
983                 if (ll_permission(dchild->d_inode, acc_mode, NULL)) {
984                         intent_set_disposition(rep, DISP_OPEN_OPEN);
985                         GOTO(cleanup, rc = -EACCES);
986                 }
987         }
988
989         /* Step 5: mds_open it */
990         rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
991                              rep);
992         GOTO(cleanup, rc);
993
994  cleanup:
995         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
996                                 req, rc, rep ? rep->lock_policy_res1 : 0);
997
998         switch (cleanup_phase) {
999         case 2:
1000                 if (rc && created) {
1001                         int err = vfs_unlink(dparent->d_inode, dchild);
1002                         if (err) {
1003                                 CERROR("unlink(%*s) in error path: %d\n",
1004                                        dchild->d_name.len, dchild->d_name.name,
1005                                        err);
1006                         }
1007                 } else if (created) {
1008                         mds_lock_new_child(obd, dchild->d_inode, NULL);
1009                 }
1010                 l_dput(dchild);
1011         case 1:
1012                 if (dparent == NULL)
1013                         break;
1014
1015                 l_dput(dparent);
1016                 if (rc)
1017                         ldlm_lock_decref(&parent_lockh, parent_mode);
1018                 else
1019                         ptlrpc_save_lock (req, &parent_lockh, parent_mode);
1020         }
1021         if (rc == 0)
1022                 atomic_inc(&mds->mds_open_count);
1023         RETURN(rc);
1024 }
1025
1026 /* Close a "file descriptor" and possibly unlink an orphan from the
1027  * PENDING directory.
1028  *
1029  * If we are being called from mds_disconnect() because the client has
1030  * disappeared, then req == NULL and we do not update last_rcvd because
1031  * there is nothing that could be recovered by the client at this stage
1032  * (it will not even _have_ an entry in last_rcvd anymore).
1033  *
1034  * Returns EAGAIN if the client needs to get more data and re-close. */
1035 int mds_mfd_close(struct ptlrpc_request *req, struct obd_device *obd,
1036                   struct mds_file_data *mfd, int unlink_orphan)
1037 {
1038         struct inode *inode = mfd->mfd_dentry->d_inode;
1039         char fidname[LL_FID_NAMELEN];
1040         int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
1041         struct dentry *pending_child = NULL;
1042         struct mds_obd *mds = &obd->u.mds;
1043         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1044         void *handle = NULL;
1045         struct mds_body *request_body = NULL, *reply_body = NULL;
1046         struct dentry_params dp;
1047         ENTRY;
1048
1049         if (req != NULL) {
1050                 request_body = lustre_msg_buf(req->rq_reqmsg, 0,
1051                                               sizeof(*request_body));
1052                 reply_body = lustre_msg_buf(req->rq_repmsg, 0,
1053                                             sizeof(*reply_body));
1054         }
1055
1056         fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1057
1058         last_orphan = mds_open_orphan_dec_test(inode) &&
1059                 mds_inode_is_orphan(inode);
1060
1061         /* this is half of the actual "close" */
1062         if (mfd->mfd_mode & FMODE_WRITE) {
1063                 rc = mds_put_write_access(mds, inode, request_body,
1064                                           last_orphan && unlink_orphan);
1065         } else if (mfd->mfd_mode & FMODE_EXEC) {
1066                 mds_allow_write_access(inode);
1067         }
1068
1069         if (last_orphan && unlink_orphan) {
1070                 struct lov_mds_md *lmm = NULL;
1071                 int stripe_count = 0;
1072                 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
1073
1074                 CDEBUG(D_HA, "destroying orphan object %s\n", fidname);
1075
1076                 /* Sadly, there is no easy way to save pending_child from
1077                  * mds_reint_unlink() into mfd, so we need to re-lookup,
1078                  * but normally it will still be in the dcache. */
1079                 pending_dir = mds->mds_pending_dir->d_inode;
1080                 down(&pending_dir->i_sem);
1081                 cleanup_phase = 1; /* up(i_sem) when finished */
1082                 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1083                                                fidlen);
1084                 if (IS_ERR(pending_child))
1085                         GOTO(cleanup, rc = PTR_ERR(pending_child));
1086                 LASSERT(pending_child->d_inode != NULL);
1087
1088                 cleanup_phase = 2; /* dput(pending_child) when finished */
1089                 if (req != NULL) {
1090                         lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
1091                         stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
1092                 }
1093
1094                 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK,
1095                                           NULL, stripe_count);
1096                 if (IS_ERR(handle)) {
1097                         rc = PTR_ERR(handle);
1098                         handle = NULL;
1099                         GOTO(cleanup, rc);
1100                 }
1101
1102                 if (req != NULL && (reply_body->valid & OBD_MD_FLEASIZE) &&
1103                     mds_log_op_unlink(obd, pending_child->d_inode, lmm,
1104                                       req->rq_repmsg->buflens[1],
1105                                       lustre_msg_buf(req->rq_repmsg, 2, 0),
1106                                       req->rq_repmsg->buflens[2]) > 0) {
1107                         reply_body->valid |= OBD_MD_FLCOOKIE;
1108                 }
1109
1110                 pending_child->d_fsdata = (void *) &dp;
1111                 dp.p_inum = 0;
1112                 dp.p_ptr = req;
1113                 if (S_ISDIR(pending_child->d_inode->i_mode))
1114                         rc = vfs_rmdir(pending_dir, pending_child);
1115                 else
1116                         rc = vfs_unlink(pending_dir, pending_child);
1117                 if (rc)
1118                         CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1119         } else if (mfd->mfd_mode & FMODE_WRITE && rc == 0) {
1120                 /* Update the on-disk attributes if this was the last write
1121                  * close, and all information was provided (i.e., rc == 0)
1122                  *
1123                  * XXX this should probably be abstracted with mds_reint_setattr
1124                  */
1125 #if 0
1126                 struct iattr iattr;
1127
1128                 /* XXX can't set block count with fsfilt_setattr (!) */
1129                 iattr.ia_valid = ATTR_CTIME | ATTR_ATIME |
1130                         ATTR_MTIME | ATTR_SIZE;
1131                 iattr.ia_atime = request_body->atime;
1132                 iattr.ia_ctime = request_body->ctime;
1133                 iattr.ia_mtime = request_body->mtime;
1134                 iattr.ia_size = request_body->size;
1135                 /* iattr.ia_blocks = request_body->blocks */
1136
1137                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1138                 if (IS_ERR(handle))
1139                         GOTO(cleanup, rc = PTR_ERR(handle));
1140                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1141                 if (rc)
1142                         CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1143 #endif
1144         }
1145         /* If other clients have this file open for write, rc will be > 0 */
1146         if (rc > 0)
1147                 rc = 0;
1148         l_dput(mfd->mfd_dentry);
1149         mds_mfd_destroy(mfd);
1150
1151  cleanup:
1152         atomic_dec(&mds->mds_open_count);
1153         if (req) {
1154                 rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0);
1155         } else if (handle) {
1156                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1157                 if (err) {
1158                         CERROR("error committing close: %d\n", err);
1159                         if (!rc)
1160                                 rc = err;
1161                 }
1162         }
1163
1164         switch (cleanup_phase) {
1165         case 2:
1166                 dput(pending_child);
1167         case 1:
1168                 up(&pending_dir->i_sem);
1169         }
1170         RETURN(rc);
1171 }
1172
1173 int mds_close(struct ptlrpc_request *req)
1174 {
1175         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1176         struct obd_device *obd = req->rq_export->exp_obd;
1177         struct mds_body *body;
1178         struct mds_file_data *mfd;
1179         struct obd_run_ctxt saved;
1180         struct inode *inode;
1181         int rc, repsize[3] = {sizeof(struct mds_body),
1182                               obd->u.mds.mds_max_mdsize,
1183                               obd->u.mds.mds_max_cookiesize};
1184         ENTRY;
1185
1186         rc = lustre_pack_reply(req, 3, repsize, NULL);
1187         if (rc) {
1188                 CERROR("lustre_pack_reply: rc = %d\n", rc);
1189                 req->rq_status = rc;
1190         } else {
1191                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1192         }
1193
1194         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1195         if (body == NULL) {
1196                 CERROR("Can't unpack body\n");
1197                 req->rq_status = -EFAULT;
1198                 RETURN(-EFAULT);
1199         }
1200
1201         if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1202                 /* do some stuff */ ;
1203
1204         mfd = mds_handle2mfd(&body->handle);
1205         if (mfd == NULL) {
1206                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1207                           ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1208                 req->rq_status = -ESTALE;
1209                 RETURN(-ESTALE);
1210         }
1211
1212         inode = mfd->mfd_dentry->d_inode;
1213         if (mds_inode_is_orphan(inode) && mds_open_orphan_count(inode) == 1) {
1214                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1215                 LASSERT(body != NULL);
1216
1217                 mds_pack_inode2fid(&body->fid1, inode);
1218                 mds_pack_inode2body(body, inode);
1219                 mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
1220         }
1221         spin_lock(&med->med_open_lock);
1222         list_del(&mfd->mfd_list);
1223         spin_unlock(&med->med_open_lock);
1224
1225         push_ctxt(&saved, &obd->obd_ctxt, NULL);
1226         req->rq_status = mds_mfd_close(rc ? NULL : req, obd, mfd, 1);
1227         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1228
1229         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1230                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1231                 req->rq_status = -ENOMEM;
1232                 mds_mfd_put(mfd);
1233                 RETURN(-ENOMEM);
1234         }
1235
1236         mds_mfd_put(mfd);
1237         RETURN(0);
1238 }
1239
1240 int mds_done_writing(struct ptlrpc_request *req)
1241 {
1242         struct mds_body *body;
1243         int rc, size = sizeof(struct mds_body);
1244         ENTRY;
1245
1246         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1247
1248         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1249         if (body == NULL) {
1250                 CERROR("Can't unpack body\n");
1251                 req->rq_status = -EFAULT;
1252                 RETURN(-EFAULT);
1253         }
1254
1255         rc = lustre_pack_reply(req, 1, &size, NULL);
1256         if (rc) {
1257                 CERROR("lustre_pack_reply: rc = %d\n", rc);
1258                 req->rq_status = rc;
1259         }
1260
1261         RETURN(0);
1262 }