Whamcloud - gitweb
e7d4023325c823c91b025ba4373426ab58f92dd1
[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 the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_MDS
33
34 #include <linux/version.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/version.h>
38 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
39 # include <linux/buffer_head.h>
40 # include <linux/workqueue.h>
41 #else
42 # include <linux/locks.h>
43 #endif
44
45 #include <obd_class.h>
46 #include <obd_lov.h>
47 #include <lustre_fsfilt.h>
48 #include <lprocfs_status.h>
49
50 #include "mds_internal.h"
51
52 /* Exported function from this file are:
53  *
54  * mds_open - called by the intent handler
55  * mds_close - an rpc handling function
56  * mds_pin - an rpc handling function - which will go away
57  * mds_mfd_close - for force closing files when a client dies
58  */
59
60 /*
61  * MDS file data handling: file data holds a handle for a file opened
62  * by a client.
63  */
64
65 static void mds_mfd_addref(void *mfdp)
66 {
67         struct mds_file_data *mfd = mfdp;
68
69         atomic_inc(&mfd->mfd_refcount);
70         CDEBUG(D_INFO, "GETting mfd %p : new refcount %d\n", mfd,
71                atomic_read(&mfd->mfd_refcount));
72 }
73
74 /* Create a new mds_file_data struct.
75  * One reference for handle+med_open_head list and dropped by mds_mfd_unlink(),
76  * one reference for the caller of this function. */
77 struct mds_file_data *mds_mfd_new(void)
78 {
79         struct mds_file_data *mfd;
80
81         OBD_ALLOC(mfd, sizeof *mfd);
82         if (mfd == NULL) {
83                 CERROR("mds: out of memory\n");
84                 return NULL;
85         }
86
87         atomic_set(&mfd->mfd_refcount, 2);
88
89         INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
90         INIT_LIST_HEAD(&mfd->mfd_list);
91         class_handle_hash(&mfd->mfd_handle, mds_mfd_addref);
92
93         return mfd;
94 }
95
96 /* Get a new reference on the mfd pointed to by handle, if handle is still
97  * valid.  Caller must drop reference with mds_mfd_put(). */
98 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
99 {
100         ENTRY;
101         LASSERT(handle != NULL);
102         RETURN(class_handle2object(handle->cookie));
103 }
104
105 /* Drop mfd reference, freeing struct if this is the last one. */
106 static void mds_mfd_put(struct mds_file_data *mfd)
107 {
108         CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
109                atomic_read(&mfd->mfd_refcount) - 1);
110         LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
111                 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
112         if (atomic_dec_and_test(&mfd->mfd_refcount)) {
113                 OBD_FREE_RCU(mfd, sizeof *mfd, &mfd->mfd_handle);
114         }
115 }
116
117 /* Remove the mfd handle so that it cannot be found by open/close again.
118  * Caller must hold med_open_lock for mfd_list manipulation. */
119 void mds_mfd_unlink(struct mds_file_data *mfd, int decref)
120 {
121         class_handle_unhash(&mfd->mfd_handle);
122         list_del_init(&mfd->mfd_list);
123         if (decref)
124                 mds_mfd_put(mfd);
125 }
126
127 /* Caller must hold mds->mds_epoch_sem */
128 static int mds_alloc_filterdata(struct inode *inode)
129 {
130         LASSERT(inode->i_filterdata == NULL);
131         OBD_ALLOC(inode->i_filterdata, sizeof(struct mds_filter_data));
132         if (inode->i_filterdata == NULL)
133                 return -ENOMEM;
134         LASSERT(igrab(inode) == inode);
135         return 0;
136 }
137
138 /* Caller must hold mds->mds_epoch_sem */
139 static void mds_free_filterdata(struct inode *inode)
140 {
141         LASSERT(inode->i_filterdata != NULL);
142         OBD_FREE(inode->i_filterdata, sizeof(struct mds_filter_data));
143         inode->i_filterdata = NULL;
144         iput(inode);
145 }
146
147 /* Write access to a file: executors cause a negative count,
148  * writers a positive count.  The semaphore is needed to perform
149  * a check for the sign and then increment or decrement atomically.
150  *
151  * This code is closely tied to the allocation of the d_fsdata and the
152  * MDS epoch, so we use the same semaphore for the whole lot.
153  *
154  * We could use a different semaphore for each file, if it ever shows
155  * up in a profile, which it won't.
156  *
157  * epoch argument is nonzero during recovery */
158 static int mds_get_write_access(struct mds_obd *mds, struct inode *inode,
159                                 __u64 epoch)
160 {
161         int rc = 0;
162
163         down(&mds->mds_epoch_sem);
164
165         if (atomic_read(&inode->i_writecount) < 0) {
166                 up(&mds->mds_epoch_sem);
167                 RETURN(-ETXTBSY);
168         }
169
170
171         if (MDS_FILTERDATA(inode) && MDS_FILTERDATA(inode)->io_epoch != 0) {
172                 CDEBUG(D_INODE, "continuing MDS epoch "LPU64" for ino %lu/%u\n",
173                        MDS_FILTERDATA(inode)->io_epoch, inode->i_ino,
174                        inode->i_generation);
175                 goto out;
176         }
177
178         if (inode->i_filterdata == NULL)
179                 mds_alloc_filterdata(inode);
180         if (inode->i_filterdata == NULL) {
181                 rc = -ENOMEM;
182                 goto out;
183         }
184         if (epoch > mds->mds_io_epoch)
185                 mds->mds_io_epoch = epoch;
186         else
187                 mds->mds_io_epoch++;
188         MDS_FILTERDATA(inode)->io_epoch = mds->mds_io_epoch;
189         CDEBUG(D_INODE, "starting MDS epoch "LPU64" for ino %lu/%u\n",
190                mds->mds_io_epoch, inode->i_ino, inode->i_generation);
191  out:
192         if (rc == 0)
193                 atomic_inc(&inode->i_writecount);
194         up(&mds->mds_epoch_sem);
195         return rc;
196 }
197
198 /* Returns EAGAIN if the client needs to get size and/or cookies and close
199  * again -- which is never true if the file is about to be unlinked.  Otherwise
200  * returns the number of remaining writers. */
201 static int mds_put_write_access(struct mds_obd *mds, struct inode *inode,
202                                 struct mds_body *body, int unlinking)
203 {
204         int rc = 0;
205         ENTRY;
206
207         down(&mds->mds_epoch_sem);
208         atomic_dec(&inode->i_writecount);
209         rc = atomic_read(&inode->i_writecount);
210         if (rc > 0)
211                 GOTO(out, rc);
212 #if 0
213         if (!unlinking && !(body->valid & OBD_MD_FLSIZE))
214                 GOTO(out, rc = EAGAIN);
215 #endif
216         mds_free_filterdata(inode);
217  out:
218         up(&mds->mds_epoch_sem);
219         return rc;
220 }
221
222 static int mds_deny_write_access(struct mds_obd *mds, struct inode *inode)
223 {
224         ENTRY;
225         down(&mds->mds_epoch_sem);
226         if (atomic_read(&inode->i_writecount) > 0) {
227                 up(&mds->mds_epoch_sem);
228                 RETURN(-ETXTBSY);
229         }
230         atomic_dec(&inode->i_writecount);
231         up(&mds->mds_epoch_sem);
232         RETURN(0);
233 }
234
235 static void mds_allow_write_access(struct inode *inode)
236 {
237         ENTRY;
238         atomic_inc(&inode->i_writecount);
239 }
240
241 int mds_query_write_access(struct inode *inode)
242 {
243         ENTRY;
244         RETURN(atomic_read(&inode->i_writecount));
245 }
246
247 /* This replaces the VFS dentry_open, it manages mfd and writecount */
248 static struct mds_file_data *mds_dentry_open(struct dentry *dentry,
249                                              struct vfsmount *mnt, int flags,
250                                              struct ptlrpc_request *req)
251 {
252         struct mds_export_data *med = &req->rq_export->exp_mds_data;
253         struct mds_obd *mds = mds_req2mds(req);
254         struct mds_file_data *mfd;
255         struct mds_body *body;
256         int error;
257         ENTRY;
258
259         mfd = mds_mfd_new();
260         if (mfd == NULL) {
261                 CERROR("mds: out of memory\n");
262                 GOTO(cleanup_dentry, error = -ENOMEM);
263         }
264
265         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
266
267         if (flags & FMODE_WRITE) {
268                 /* FIXME: in recovery, need to pass old epoch here */
269                 error = mds_get_write_access(mds, dentry->d_inode, 0);
270                 if (error)
271                         GOTO(cleanup_mfd, error);
272                 body->io_epoch = MDS_FILTERDATA(dentry->d_inode)->io_epoch;
273         } else if (flags & MDS_FMODE_EXEC) {
274                 error = mds_deny_write_access(mds, dentry->d_inode);
275                 if (error)
276                         GOTO(cleanup_mfd, error);
277         }
278
279         dget(dentry);
280
281         /* Mark the file as open to handle open-unlink. */
282         MDS_DOWN_WRITE_ORPHAN_SEM(dentry->d_inode);
283         mds_orphan_open_inc(dentry->d_inode);
284         MDS_UP_WRITE_ORPHAN_SEM(dentry->d_inode);
285
286         mfd->mfd_mode = flags;
287         mfd->mfd_dentry = dentry;
288         mfd->mfd_xid = req->rq_xid;
289
290         spin_lock(&med->med_open_lock);
291         list_add(&mfd->mfd_list, &med->med_open_head);
292         spin_unlock(&med->med_open_lock);
293
294         body->handle.cookie = mfd->mfd_handle.h_cookie;
295
296         RETURN(mfd);
297
298 cleanup_mfd:
299         mds_mfd_put(mfd);
300         mds_mfd_unlink(mfd, 1);
301 cleanup_dentry:
302         return ERR_PTR(error);
303 }
304
305 /* Must be called with i_mutex held */
306 static int mds_create_objects(struct ptlrpc_request *req, int offset,
307                               struct mds_update_record *rec,
308                               struct mds_obd *mds, struct obd_device *obd,
309                               struct dentry *dchild, void **handle,
310                               struct lov_mds_md **objid)
311 {
312         struct inode *inode = dchild->d_inode;
313         struct obd_trans_info oti = { 0 };
314         struct lov_mds_md *lmm = NULL;
315         int rc, lmm_size;
316         struct mds_body *body;
317         struct obd_info oinfo = { { { 0 } } };
318         void *lmm_buf;
319         ENTRY;
320
321         *objid = NULL;
322
323         if (!S_ISREG(inode->i_mode))
324                 RETURN(0);
325         if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
326             !(rec->ur_flags & FMODE_WRITE))
327                 RETURN(0);
328
329         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
330
331         if (body->valid & OBD_MD_FLEASIZE)
332                 RETURN(0);
333
334         oti_init(&oti, req);
335
336         /* replay case */
337         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
338                 if (rec->ur_fid2->id == 0) {
339                         DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
340                         RETURN(-EFAULT);
341                 }
342
343                 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
344                 lmm_size = rec->ur_eadatalen;
345                 lmm = rec->ur_eadata;
346                 LASSERT(lmm);
347
348                 if (*handle == NULL)
349                         *handle = fsfilt_start(obd,inode,FSFILT_OP_CREATE,NULL);
350                 if (IS_ERR(*handle)) {
351                         rc = PTR_ERR(*handle);
352                         *handle = NULL;
353                         GOTO(out_ids, rc);
354                 }
355
356                 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
357                 if (rc)
358                         CERROR("open replay failed to set md:%d\n", rc);
359                 lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
360                 LASSERT(lmm_buf);
361                 memcpy(lmm_buf, lmm, lmm_size);
362
363                 *objid = lmm_buf;
364                 RETURN(rc);
365         }
366
367         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
368                 GOTO(out_ids, rc = -ENOMEM);
369
370         OBDO_ALLOC(oinfo.oi_oa);
371         if (oinfo.oi_oa == NULL)
372                 GOTO(out_ids, rc = -ENOMEM);
373         oinfo.oi_oa->o_uid = 0; /* must have 0 uid / gid on OST */
374         oinfo.oi_oa->o_gid = 0;
375         oinfo.oi_oa->o_mode = S_IFREG | 0600;
376         oinfo.oi_oa->o_id = inode->i_ino;
377         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
378                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
379         oinfo.oi_oa->o_size = 0;
380
381         obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
382                         OBD_MD_FLMTIME | OBD_MD_FLCTIME);
383
384         if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
385                 /* check if things like lfs setstripe are sending us the ea */
386                 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
387                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
388                                            mds->mds_osc_exp,
389                                            0, &oinfo.oi_md, rec->ur_eadata);
390                         if (rc)
391                                 GOTO(out_oa, rc);
392                 } else {
393                         OBD_ALLOC(lmm, mds->mds_max_mdsize);
394                         if (lmm == NULL)
395                                 GOTO(out_oa, rc = -ENOMEM);
396
397                         lmm_size = mds->mds_max_mdsize;
398                         rc = mds_get_md(obd, dchild->d_parent->d_inode,
399                                         lmm, &lmm_size, 1, 0);
400                         if (rc > 0)
401                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
402                                                    mds->mds_osc_exp,
403                                                    0, &oinfo.oi_md, lmm);
404                         OBD_FREE(lmm, mds->mds_max_mdsize);
405                         if (rc)
406                                 GOTO(out_oa, rc);
407                 }
408                 rc = obd_create(mds->mds_osc_exp, oinfo.oi_oa, 
409                                 &oinfo.oi_md, &oti);
410                 if (rc) {
411                         int level = D_ERROR;
412                         if (rc == -ENOSPC)
413                                 level = D_INODE;
414                         CDEBUG(level, "error creating objects for "
415                                       "inode %lu: rc = %d\n",
416                                inode->i_ino, rc);
417                         if (rc > 0) {
418                                 CERROR("obd_create returned invalid "
419                                        "rc %d\n", rc);
420                                 rc = -EIO;
421                         }
422                         GOTO(out_oa, rc);
423                 }
424         } else {
425                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_osc_exp,
426                                    0, &oinfo.oi_md, rec->ur_eadata);
427                 if (rc) {
428                         GOTO(out_oa, rc);
429                 }
430                 oinfo.oi_md->lsm_object_id = oinfo.oi_oa->o_id;
431         }
432         if (i_size_read(inode)) {
433                 oinfo.oi_oa->o_size = i_size_read(inode);
434                 obdo_from_inode(oinfo.oi_oa, inode, OBD_MD_FLTYPE |
435                                 OBD_MD_FLATIME | OBD_MD_FLMTIME |
436                                 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
437
438                 /* pack lustre id to OST */
439                 oinfo.oi_oa->o_fid = body->fid1.id;
440                 oinfo.oi_oa->o_generation = body->fid1.generation;
441                 oinfo.oi_oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
442                 oinfo.oi_policy.l_extent.start = i_size_read(inode);
443                 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
444
445                 rc = obd_punch_rqset(mds->mds_osc_exp, &oinfo, &oti);
446                 if (rc) {
447                         CERROR("error setting attrs for inode %lu: rc %d\n",
448                                inode->i_ino, rc);
449                         if (rc > 0) {
450                                 CERROR("obd_setattr_async returned bad rc %d\n",
451                                        rc);
452                                 rc = -EIO;
453                         }
454                         GOTO(out_oa, rc);
455                 }
456         }
457
458         body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
459         obdo_refresh_inode(inode, oinfo.oi_oa, OBD_MD_FLBLKSZ);
460
461         LASSERT(oinfo.oi_md && oinfo.oi_md->lsm_object_id);
462         lmm = NULL;
463         rc = obd_packmd(mds->mds_osc_exp, &lmm, oinfo.oi_md);
464         if (rc < 0) {
465                 CERROR("cannot pack lsm, err = %d\n", rc);
466                 GOTO(out_oa, rc);
467         }
468         lmm_size = rc;
469         body->eadatasize = rc;
470
471         if (*handle == NULL)
472                 *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
473         if (IS_ERR(*handle)) {
474                 rc = PTR_ERR(*handle);
475                 *handle = NULL;
476                 GOTO(free_diskmd, rc);
477         }
478
479         rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
480         lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
481         LASSERT(lmm_buf);
482         memcpy(lmm_buf, lmm, lmm_size);
483         *objid = lmm_buf; /* save for mds_lov_update_objid */
484
485  free_diskmd:
486         obd_free_diskmd(mds->mds_osc_exp, &lmm);
487  out_oa:
488         oti_free_cookies(&oti);
489         OBDO_FREE(oinfo.oi_oa);
490  out_ids:
491         if (oinfo.oi_md)
492                 obd_free_memmd(mds->mds_osc_exp, &oinfo.oi_md);
493         RETURN(rc);
494 }
495
496 static void reconstruct_open(struct mds_update_record *rec, int offset,
497                              struct ptlrpc_request *req,
498                              struct lustre_handle *child_lockh)
499 {
500         struct mds_export_data *med = &req->rq_export->exp_mds_data;
501         struct lsd_client_data *lcd = med->med_lcd;
502         struct mds_obd *mds = mds_req2mds(req);
503         struct mds_file_data *mfd;
504         struct obd_export *exp = req->rq_export;
505         struct obd_device *obd = exp->exp_obd;
506         struct dentry *parent, *dchild;
507         struct ldlm_reply *rep;
508         struct mds_body *body;
509         int rc;
510         struct list_head *t;
511         int put_child = 1;
512         ENTRY;
513
514         LASSERT(offset == DLM_INTENT_REC_OFF); /* only called via intent */
515         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
516         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
517
518         /* copy rc, transno and disp; steal locks */
519         mds_req_from_lcd(req, lcd);
520         intent_set_disposition(rep, le32_to_cpu(lcd->lcd_last_data));
521
522         /* Only replay if create or open actually happened. */
523         if (!intent_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
524                 EXIT;
525                 return; /* error looking up parent or child */
526         }
527
528         parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
529         if (IS_ERR(parent)) {
530                 rc = PTR_ERR(parent);
531                 LCONSOLE_WARN("Parent "LPU64"/%u lookup error %d." 
532                               " Evicting client %s with export %s.\n",
533                               rec->ur_fid1->id, rec->ur_fid1->generation, rc,
534                               obd_uuid2str(&exp->exp_client_uuid),
535                               obd_export_nid2str(exp));
536                 mds_export_evict(exp);
537                 EXIT;
538                 return;
539         }
540
541         dchild = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
542         if (IS_ERR(dchild)) {
543                 rc = PTR_ERR(dchild);
544                 LCONSOLE_WARN("Child "LPU64"/%u lookup error %d." 
545                               " Evicting client %s with export %s.\n",
546                               rec->ur_fid1->id, rec->ur_fid1->generation, rc,
547                               obd_uuid2str(&exp->exp_client_uuid),
548                               obd_export_nid2str(exp));
549                 mds_export_evict(exp);
550                 EXIT;
551                 return;
552         }
553
554         if (!dchild->d_inode)
555                 GOTO(out_dput, 0); /* child not present to open */
556
557         /* At this point, we know we have a child. We'll send
558          * it back _unless_ it not created and open failed.
559          */
560         if (intent_disposition(rep, DISP_OPEN_OPEN) &&
561             !intent_disposition(rep, DISP_OPEN_CREATE) &&
562             req->rq_status) {
563                 GOTO(out_dput, 0);
564         }
565
566         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
567         mds_pack_inode2body(body, dchild->d_inode);
568         if (S_ISREG(dchild->d_inode->i_mode)) {
569                 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
570                                  body, dchild->d_inode, 1, 0);
571
572                 if (rc)
573                         LASSERT(rc == req->rq_status);
574
575                 /* If we have LOV EA data, the OST holds size, mtime */
576                 if (!(body->valid & OBD_MD_FLEASIZE))
577                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
578                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
579         }
580
581         if (!(rec->ur_flags & MDS_OPEN_JOIN_FILE))
582                 lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
583                                     body->eadatasize, 0);
584
585         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
586             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
587                 int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
588
589                 rc = mds_pack_acl(med, dchild->d_inode, req->rq_repmsg,
590                                   body, acl_off);
591                 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
592                 if (!req->rq_status && rc)
593                         req->rq_status = rc;
594         }
595
596         /* If we have -EEXIST as the status, and we were asked to create
597          * exclusively, we can tell we failed because the file already existed.
598          */
599         if (req->rq_status == -EEXIST &&
600             ((rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_EXCL)) ==
601              (MDS_OPEN_CREAT | MDS_OPEN_EXCL))) {
602                 GOTO(out_dput, 0);
603         }
604
605         /* If we didn't get as far as trying to open, then some locking thing
606          * probably went wrong, and we'll just bail here.
607          */
608         if (!intent_disposition(rep, DISP_OPEN_OPEN))
609                 GOTO(out_dput, 0);
610
611         /* If we failed, then we must have failed opening, so don't look for
612          * file descriptor or anything, just give the client the bad news.
613          */
614         if (req->rq_status)
615                 GOTO(out_dput, 0);
616
617         mfd = NULL;
618         spin_lock(&med->med_open_lock);
619         list_for_each(t, &med->med_open_head) {
620                 mfd = list_entry(t, struct mds_file_data, mfd_list);
621                 if (mfd->mfd_xid == req->rq_xid) {
622                         mds_mfd_addref(mfd);
623                         break;
624                 }
625                 mfd = NULL;
626         }
627         spin_unlock(&med->med_open_lock);
628
629         /* #warning "XXX fixme" bug 2991 */
630         /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
631          * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
632          * to detect a re-open */
633         if (mfd == NULL) {
634                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
635                         rc = mds_join_file(rec, req, dchild, NULL);
636                         if (rc)
637                                 GOTO(out_dput, rc);
638                 }
639                 mntget(mds->mds_vfsmnt);
640                 CERROR("Re-opened file \n");
641                 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt,
642                                       rec->ur_flags & ~MDS_OPEN_TRUNC, req);
643                 mntput(mds->mds_vfsmnt);
644                 if (IS_ERR(mfd)) {
645                         req->rq_status = PTR_ERR(mfd);
646                         mfd = NULL;
647                         CERROR("%s: opening inode "LPU64" failed: rc %d\n",
648                                req->rq_export->exp_obd->obd_name,
649                                (__u64)dchild->d_inode->i_ino, req->rq_status);
650                         GOTO(out_dput, req->rq_status);
651                 }
652         } else {
653                 body->handle.cookie = mfd->mfd_handle.h_cookie;
654                 CDEBUG(D_INODE, "resend mfd %p, cookie "LPX64"\n", mfd,
655                        mfd->mfd_handle.h_cookie);
656         }
657
658         mds_mfd_put(mfd);
659
660  out_dput:
661         if (put_child)
662                 l_dput(dchild);
663         l_dput(parent);
664         EXIT;
665 }
666
667 /* do NOT or the MAY_*'s, you'll get the weakest */
668 static int accmode(struct inode *inode, int flags)
669 {
670         int res = 0;
671
672         /* Sadly, NFSD reopens a file repeatedly during operation, so the
673          * "acc_mode = 0" allowance for newly-created files isn't honoured.
674          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
675          * owner can write to a file even if it is marked readonly to hide
676          * its brokenness. (bug 5781) */
677         if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current->fsuid)
678                 return 0;
679
680         if (flags & FMODE_READ)
681                 res = MAY_READ;
682         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
683                 res |= MAY_WRITE;
684         if (flags & MDS_FMODE_EXEC)
685                 res = MAY_EXEC;
686         return res;
687 }
688
689 /* Handles object creation, actual opening, and I/O epoch */
690 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
691                            struct mds_body *body, int flags, void **handle,
692                            struct mds_update_record *rec,struct ldlm_reply *rep,
693                            struct lustre_handle *lockh)
694 {
695         struct mds_obd *mds = mds_req2mds(req);
696         struct obd_device *obd = req->rq_export->exp_obd;
697         struct mds_file_data *mfd = NULL;
698         struct lov_mds_md *lmm = NULL; /* object IDs created */
699         int rc = 0;
700         ENTRY;
701
702         /* atomically create objects if necessary */
703         LOCK_INODE_MUTEX(dchild->d_inode);
704
705         if (S_ISREG(dchild->d_inode->i_mode) &&
706             !(body->valid & OBD_MD_FLEASIZE)) {
707                 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
708                                  body, dchild->d_inode, 0, 0);
709                 if (rc) {
710                         UNLOCK_INODE_MUTEX(dchild->d_inode);
711                         RETURN(rc);
712                 }
713         }
714         if (rec != NULL) {
715                 if ((body->valid & OBD_MD_FLEASIZE) &&
716                     (rec->ur_flags & MDS_OPEN_HAS_EA)) {
717                         UNLOCK_INODE_MUTEX(dchild->d_inode);
718                         RETURN(-EEXIST);
719                 }
720                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
721                         UNLOCK_INODE_MUTEX(dchild->d_inode);
722                         rc = mds_join_file(rec, req, dchild, lockh);
723                         if (rc)
724                                 RETURN(rc);
725                         LOCK_INODE_MUTEX(dchild->d_inode);
726                 }
727                 if (!(body->valid & OBD_MD_FLEASIZE) &&
728                     !(body->valid & OBD_MD_FLMODEASIZE)) {
729                         /* no EA: create objects */
730                         rc = mds_create_objects(req, DLM_REPLY_REC_OFF + 1, rec,
731                                                 mds, obd, dchild, handle, &lmm);
732                         if (rc) {
733                                 CERROR("mds_create_objects: rc = %d\n", rc);
734                                 UNLOCK_INODE_MUTEX(dchild->d_inode);
735                                 RETURN(rc);
736                         }
737                 }
738         }
739         /* If the inode has no EA data, then MDS holds size, mtime */
740         if (S_ISREG(dchild->d_inode->i_mode) &&
741             !(body->valid & OBD_MD_FLEASIZE)) {
742                 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
743                                 OBD_MD_FLATIME | OBD_MD_FLMTIME);
744         }
745         UNLOCK_INODE_MUTEX(dchild->d_inode);
746
747         if (rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE))
748                 lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
749                                     body->eadatasize, 0);
750
751         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
752             rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
753                 int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
754
755                 rc = mds_pack_acl(&req->rq_export->exp_mds_data,
756                                   dchild->d_inode, req->rq_repmsg,
757                                   body, acl_off);
758                 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
759                 if (rc)
760                         RETURN(rc);
761         }
762
763         intent_set_disposition(rep, DISP_OPEN_OPEN);
764         mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
765         if (IS_ERR(mfd))
766                 RETURN(PTR_ERR(mfd));
767
768         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
769                mfd->mfd_handle.h_cookie);
770
771         mds_lov_update_objids(obd, lmm);
772
773         if (rc) /* coverity[deadcode] */
774                 mds_mfd_unlink(mfd, 1);
775
776         mds_mfd_put(mfd);
777         RETURN(rc);
778 }
779
780 static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
781                            struct mds_body *body, int flags,
782                            struct mds_update_record *rec,struct ldlm_reply *rep)
783 {
784         struct mds_obd *mds = mds_req2mds(req);
785         struct dentry *dchild;
786         char fidname[LL_FID_NAMELEN];
787         int fidlen = 0, rc;
788         void *handle = NULL;
789         ENTRY;
790
791         fidlen = ll_fid2str(fidname, fid->id, fid->generation);
792         dchild = ll_lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
793         if (IS_ERR(dchild)) {
794                 rc = PTR_ERR(dchild);
795                 CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
796                 RETURN(rc);
797         }
798
799         if (dchild->d_inode != NULL) {
800                 mds_inode_set_orphan(dchild->d_inode);
801                 CWARN("Orphan %s found and opened in PENDING directory\n",
802                        fidname);
803         } else {
804                 l_dput(dchild);
805
806                 /* We didn't find it in PENDING so it isn't an orphan.  See
807                  * if it was a regular inode that was previously created. */
808                 dchild = mds_fid2dentry(mds, fid, NULL);
809                 if (IS_ERR(dchild))
810                         RETURN(PTR_ERR(dchild));
811         }
812
813         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
814         mds_pack_inode2body(body, dchild->d_inode);
815         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
816         intent_set_disposition(rep, DISP_LOOKUP_POS);
817
818         rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep, NULL);
819         rc = mds_finish_transno(mds, dchild->d_inode, handle,
820                                 req, rc, rep ? rep->lock_policy_res1 : 0, 0);
821         /* XXX what do we do here if mds_finish_transno itself failed? */
822
823         l_dput(dchild);
824         RETURN(rc);
825 }
826
827 int mds_pin(struct ptlrpc_request *req, int offset)
828 {
829         struct obd_device *obd = req->rq_export->exp_obd;
830         struct mds_body *reqbody, *repbody;
831         struct lvfs_run_ctxt saved;
832         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
833         ENTRY;
834
835         reqbody = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*reqbody));
836
837         rc = lustre_pack_reply(req, 2, size, NULL);
838         if (rc)
839                 RETURN(rc);
840
841         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
842                                  sizeof(*repbody));
843
844         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
845         rc = mds_open_by_fid(req, &reqbody->fid1, repbody, reqbody->flags, NULL,
846                              NULL);
847         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
848
849         RETURN(rc);
850 }
851
852 /*  Get an internal lock on the inode number (but not generation) to sync
853  *  new inode creation with inode unlink (bug 2029).  If child_lockh is NULL
854  *  we just get the lock as a barrier to wait for other holders of this lock,
855  *  and drop it right away again. */
856 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
857                        struct lustre_handle *child_lockh)
858 {
859         struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
860         struct lustre_handle lockh;
861         int lock_flags = LDLM_FL_ATOMIC_CB;
862         int rc;
863
864         if (child_lockh == NULL)
865                 child_lockh = &lockh;
866
867         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
868                                     LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
869                                     ldlm_blocking_ast, ldlm_completion_ast,
870                                     NULL, NULL, 0, NULL, child_lockh);
871         if (rc != ELDLM_OK)
872                 CERROR("ldlm_cli_enqueue_local: %d\n", rc);
873         else if (child_lockh == &lockh)
874                 ldlm_lock_decref(child_lockh, LCK_EX);
875
876         RETURN(rc);
877 }
878
879 int mds_open(struct mds_update_record *rec, int offset,
880              struct ptlrpc_request *req, struct lustre_handle *child_lockh)
881 {
882         /* XXX ALLOCATE _something_ - 464 bytes on stack here */
883         struct obd_device *obd = req->rq_export->exp_obd;
884         struct mds_obd *mds = mds_req2mds(req);
885         struct ldlm_reply *rep = NULL;
886         struct mds_body *body = NULL;
887         struct dentry *dchild = NULL, *dparent = NULL;
888         struct mds_export_data *med;
889         struct lustre_handle parent_lockh;
890         int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
891         int parent_mode = LCK_CR;
892         void *handle = NULL;
893         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
894         unsigned int qcids[MAXQUOTAS] = { current->fsuid, current->fsgid };
895         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
896         int child_mode = LCK_CR;
897         /* Always returning LOOKUP lock if open succesful to guard
898            dentry on client. */
899         ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_LOOKUP}};
900         struct ldlm_res_id child_res_id = { .name = {0}};
901         int lock_flags = 0;
902         int rec_pending = 0;
903         unsigned int gid = current->fsgid;
904         ENTRY;
905
906         mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
907
908         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
909                          (obd_timeout + 1) / 4);
910
911         CLASSERT(MAXQUOTAS < 4);
912         if (offset == DLM_INTENT_REC_OFF) { /* intent */
913                 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
914                                      sizeof(*rep));
915                 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
916                                       sizeof(*body));
917         } else if (offset == REQ_REC_OFF) { /* non-intent reint */
918                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
919                                       sizeof(*body));
920                 LBUG(); /* XXX: not supported yet? */
921         } else {
922                 body = NULL;
923                 LBUG();
924         }
925
926         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
927
928         /* Step 0: If we are passed a fid, then we assume the client already
929          * opened this file and is only replaying the RPC, so we open the
930          * inode by fid (at some large expense in security). */
931         /*XXX liblustre use mds_open_by_fid to implement LL_IOC_LOV_SETSTRIPE */
932         if (((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
933              (req->rq_export->exp_libclient && rec->ur_flags&MDS_OPEN_HAS_EA))&&
934             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
935                 if (rec->ur_fid2->id == 0) {
936                         struct ldlm_lock *lock = ldlm_handle2lock(child_lockh);
937                         if (lock) {
938                                 LDLM_ERROR(lock, "fid2 not set on open replay");
939                                 LDLM_LOCK_PUT(lock);
940                         }
941                         DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
942                         RETURN(-EFAULT);
943                 }
944
945                 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
946                                      rec, rep);
947                 if (rc != -ENOENT) {
948                         if (req->rq_export->exp_libclient &&
949                             rec->ur_flags & MDS_OPEN_HAS_EA)
950                                 RETURN(0);
951
952                         RETURN(rc);
953                 }
954
955                 /* We didn't find the correct inode on disk either, so we
956                  * need to re-create it via a regular replay. */
957                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
958                         DEBUG_REQ(D_ERROR, req,"OPEN_CREAT not in open replay");
959                         RETURN(-EFAULT);
960                 }
961         } else if (rec->ur_fid2->id) {
962                 DEBUG_REQ(D_ERROR, req, "fid2 "LPU64"/%u on open non-replay",
963                           rec->ur_fid2->id, rec->ur_fid2->generation);
964                 RETURN(-EFAULT);
965         }
966
967         /* If we got here, we must be called via intent */
968         LASSERT(offset == DLM_INTENT_REC_OFF);
969
970         med = &req->rq_export->exp_mds_data;
971         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
972                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
973                 RETURN(-ENOMEM);
974         }
975
976         /* Step 1: Find and lock the parent */
977         if (rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_JOIN_FILE))
978                 parent_mode = LCK_EX;
979         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
980                                         &parent_lockh, rec->ur_name,
981                                         rec->ur_namelen - 1,
982                                         MDS_INODELOCK_UPDATE);
983         if (IS_ERR(dparent)) {
984                 rc = PTR_ERR(dparent);
985                 if (rc != -ENOENT) {
986                         CERROR("parent "LPU64"/%u lookup error %d\n",
987                                rec->ur_fid1->id, rec->ur_fid1->generation, rc);
988                 } else {
989                         /* Just cannot find parent - make it look like
990                          * usual negative lookup to avoid extra MDS RPC */
991                         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
992                         intent_set_disposition(rep, DISP_LOOKUP_NEG);
993                 }
994                 GOTO(cleanup, rc);
995         }
996         LASSERT(dparent->d_inode != NULL);
997
998         cleanup_phase = 1; /* parent dentry and lock */
999
1000         if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
1001                 dchild = dget(dparent);
1002                 cleanup_phase = 2; /* child dentry */
1003                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1004                 GOTO(found_child, rc);
1005         }
1006
1007         /* Step 2: Lookup the child */
1008       
1009         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
1010             (rec->ur_flags & MDS_OPEN_LOCK) && (rec->ur_namelen == 1)) {
1011                 /* hack for nfsd with no_subtree_check, it will use anon
1012                  * dentry w/o filename to open the file. the anon dentry's
1013                  * parent was set to itself, so rec->ur_fid1 is the file.
1014                  * And in MDC it cannot derive the dentry's parent dentry,
1015                  * hence the file's name, so we hack here in MDS, 
1016                  * refer to bug 13030. */
1017                 dchild = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1018         } else {
1019                 dchild = ll_lookup_one_len(rec->ur_name, dparent,
1020                                            rec->ur_namelen - 1);
1021         }
1022         if (IS_ERR(dchild)) {
1023                 rc = PTR_ERR(dchild);
1024                 dchild = NULL; /* don't confuse mds_finish_transno() below */
1025                 GOTO(cleanup, rc);
1026         }
1027
1028         cleanup_phase = 2; /* child dentry */
1029
1030         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
1031         if (dchild->d_inode)
1032                 intent_set_disposition(rep, DISP_LOOKUP_POS);
1033         else
1034                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1035
1036         /*Step 3: If the child was negative, and we're supposed to, create it.*/
1037         if (dchild->d_inode == NULL) {
1038                 unsigned long ino = rec->ur_fid2->id;
1039                 struct iattr iattr;
1040                 struct inode *inode;
1041
1042                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
1043                         /* It's negative and we weren't supposed to create it */
1044                         GOTO(cleanup, rc = -ENOENT);
1045                 }
1046
1047                 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1048                         GOTO(cleanup, rc = -EROFS);
1049
1050                 if (dparent->d_inode->i_mode & S_ISGID)
1051                         gid = dparent->d_inode->i_gid;
1052                 else
1053                         gid = current->fsgid;
1054
1055                 /* we try to get enough quota to write here, and let ldiskfs
1056                  * decide if it is out of quota or not b=14783 */
1057                 lquota_chkquota(mds_quota_interface_ref, obd,
1058                                 current->fsuid, gid, 1, &rec_pending);
1059
1060                 intent_set_disposition(rep, DISP_OPEN_CREATE);
1061                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
1062                                       NULL);
1063                 if (IS_ERR(handle)) {
1064                         rc = PTR_ERR(handle);
1065                         handle = NULL;
1066                         GOTO(cleanup, rc);
1067                 }
1068                 dchild->d_fsdata = (void *) &dp;
1069                 dp.ldp_ptr = req;
1070                 dp.ldp_inum = ino;
1071
1072                 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
1073                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
1074                         dchild->d_fsdata = NULL;
1075
1076                 if (rc) {
1077                         CDEBUG(D_INODE, "error during create: %d\n", rc);
1078                         GOTO(cleanup, rc);
1079                 }
1080                 inode = dchild->d_inode;
1081                 if (ino) {
1082                         LASSERT(ino == inode->i_ino);
1083                         /* Written as part of setattr */
1084                         inode->i_generation = rec->ur_fid2->generation;
1085                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
1086                                inode->i_ino, inode->i_generation);
1087                 }
1088
1089                 created = 1;
1090                 LTIME_S(iattr.ia_atime) = rec->ur_time;
1091                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1092                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1093
1094                 iattr.ia_uid = current->fsuid;  /* set by push_ctxt already */
1095                 iattr.ia_gid = gid;
1096
1097                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1098                         ATTR_MTIME | ATTR_CTIME;
1099
1100                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1101                 if (rc)
1102                         CERROR("error on child setattr: rc = %d\n", rc);
1103
1104                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1105
1106                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1107                 if (rc)
1108                         CERROR("error on parent setattr: rc = %d\n", rc);
1109
1110                 rc = fsfilt_commit(obd, dchild->d_inode, handle, 0);
1111                 handle = NULL;
1112                 acc_mode = 0;           /* Don't check for permissions */
1113         } else {
1114                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1115         }
1116
1117         LASSERTF(!mds_inode_is_orphan(dchild->d_inode),
1118                  "dchild %.*s (%p) inode %p/%lu/%u\n", dchild->d_name.len,
1119                  dchild->d_name.name, dchild, dchild->d_inode,
1120                  dchild->d_inode->i_ino, dchild->d_inode->i_generation);
1121
1122 found_child:
1123         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
1124         mds_pack_inode2body(body, dchild->d_inode);
1125
1126         if (S_ISREG(dchild->d_inode->i_mode)) {
1127                 /* Check permissions etc */
1128                 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
1129                 if (rc != 0)
1130                         GOTO(cleanup, rc);
1131
1132                 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) &&
1133                     (acc_mode & MAY_WRITE))
1134                         GOTO(cleanup, rc = -EROFS);
1135
1136                 /* An append-only file must be opened in append mode for
1137                  * writing */
1138                 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
1139                     ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
1140                      (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
1141                         GOTO(cleanup, rc = -EPERM);
1142         }
1143
1144         if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
1145             (rec->ur_flags & MDS_OPEN_EXCL)) {
1146                 /* File already exists, we didn't just create it, and we
1147                  * were passed O_EXCL; err-or. */
1148                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
1149         }
1150
1151         /* if we are following a symlink, don't open */
1152         if (S_ISLNK(dchild->d_inode->i_mode))
1153                 GOTO(cleanup_no_trans, rc = 0);
1154
1155         if (S_ISDIR(dchild->d_inode->i_mode)) {
1156                 if (rec->ur_flags & MDS_OPEN_CREAT ||
1157                     rec->ur_flags & FMODE_WRITE) {
1158                         /* we are trying to create or write a exist dir */
1159                         GOTO(cleanup, rc = -EISDIR);
1160                 }
1161                 if (rec->ur_flags & MDS_FMODE_EXEC) {
1162                         /* we are trying to exec a directory */
1163                         GOTO(cleanup, rc = -EACCES);
1164                 }
1165                 if (ll_permission(dchild->d_inode, acc_mode, NULL)) {
1166                         intent_set_disposition(rep, DISP_OPEN_OPEN);
1167                         GOTO(cleanup, rc = -EACCES);
1168                 }
1169         } else if (rec->ur_flags & MDS_OPEN_DIRECTORY) {
1170                 GOTO(cleanup, rc = -ENOTDIR);
1171         }
1172
1173         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
1174                 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
1175                 GOTO(cleanup, rc = -EAGAIN);
1176         }
1177
1178         /* Obtain OPEN lock as well */
1179         policy.l_inodebits.bits |= MDS_INODELOCK_OPEN;
1180
1181         /* We cannot use acc_mode here, because it is zeroed in case of
1182            creating a file, so we get wrong lockmode */
1183         if (rec->ur_flags & FMODE_WRITE)
1184                 child_mode = LCK_CW;
1185         else if (rec->ur_flags & MDS_FMODE_EXEC)
1186                 child_mode = LCK_PR;
1187         else
1188                 child_mode = LCK_CR;
1189
1190         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) && 
1191              (rec->ur_flags & MDS_OPEN_LOCK)) {
1192                 /* In case of replay we do not get a lock assuming that the
1193                    caller has it already */
1194                 child_res_id.name[0] = dchild->d_inode->i_ino;
1195                 child_res_id.name[1] = dchild->d_inode->i_generation;
1196
1197                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
1198                                             LDLM_IBITS, &policy, child_mode, 
1199                                             &lock_flags, ldlm_blocking_ast, 
1200                                             ldlm_completion_ast, NULL, NULL,
1201                                             0, NULL, child_lockh);
1202                 if (rc != ELDLM_OK)
1203                         GOTO(cleanup, rc);
1204
1205                 /* Let mds_intent_policy know that we have a lock to return */
1206                 intent_set_disposition(rep, DISP_OPEN_LOCK);
1207                 cleanup_phase = 3;
1208         }
1209
1210         if (!S_ISREG(dchild->d_inode->i_mode) &&
1211             !S_ISDIR(dchild->d_inode->i_mode) &&
1212             (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH)) {
1213                 /* If client supports this, do not return open handle for
1214                  * special device nodes */
1215                 GOTO(cleanup_no_trans, rc = 0);
1216         }
1217
1218         /* Step 5: mds_open it */
1219         rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
1220                              rep, &parent_lockh);
1221         GOTO(cleanup, rc);
1222
1223  cleanup:
1224         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
1225                                 req, rc, rep ? rep->lock_policy_res1 : 0, 0);
1226
1227  cleanup_no_trans:
1228         if (rec_pending)
1229                 lquota_pending_commit(mds_quota_interface_ref, obd,
1230                                       current->fsuid, gid, 1);
1231         switch (cleanup_phase) {
1232         case 3:
1233                 if (rc)
1234                         /* It is safe to leave IT_OPEN_LOCK set, if rc is not 0,
1235                          * mds_intent_policy won't try to return any locks */
1236                         ldlm_lock_decref(child_lockh, child_mode);
1237         case 2:
1238                 if (rc && created) {
1239                         int err = vfs_unlink(dparent->d_inode, dchild);
1240                         if (err) {
1241                                 CERROR("unlink(%.*s) in error path: %d\n",
1242                                        dchild->d_name.len, dchild->d_name.name,
1243                                        err);
1244                         }
1245                 } else if (created) {
1246                         mds_lock_new_child(obd, dchild->d_inode, NULL);
1247                         /* save uid/gid for quota acquire/release */
1248                         qpids[USRQUOTA] = dparent->d_inode->i_uid;
1249                         qpids[GRPQUOTA] = dparent->d_inode->i_gid;
1250                 }
1251                 l_dput(dchild);
1252         case 1:
1253                 if (dparent == NULL)
1254                         break;
1255
1256                 l_dput(dparent);
1257                 if (rc)
1258                         ldlm_lock_decref(&parent_lockh, parent_mode);
1259                 else
1260                         ptlrpc_save_lock(req, &parent_lockh, parent_mode);
1261         }
1262         /* trigger dqacq on the owner of child and parent */
1263         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1264                       FSFILT_OP_CREATE);
1265         RETURN(rc);
1266 }
1267
1268 /* Close a "file descriptor" and possibly unlink an orphan from the
1269  * PENDING directory.  Caller must hold child->i_mutex, this drops it.
1270  *
1271  * If we are being called from mds_disconnect() because the client has
1272  * disappeared, then req == NULL and we do not update last_rcvd because
1273  * there is nothing that could be recovered by the client at this stage
1274  * (it will not even _have_ an entry in last_rcvd anymore).
1275  *
1276  * Returns EAGAIN if the client needs to get more data and re-close. */
1277 int mds_mfd_close(struct ptlrpc_request *req, int offset,
1278                   struct obd_device *obd, struct mds_file_data *mfd,
1279                   int unlink_orphan, struct lov_mds_md *lmm, int lmm_size,
1280                   struct llog_cookie *logcookies, int cookies_size,
1281                   __u64 *valid)
1282 {
1283         struct inode *inode = mfd->mfd_dentry->d_inode;
1284         char fidname[LL_FID_NAMELEN];
1285         int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
1286         struct dentry *pending_child = NULL;
1287         struct mds_obd *mds = &obd->u.mds;
1288         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1289         void *handle = NULL;
1290         struct mds_body *request_body = NULL, *reply_body = NULL;
1291         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
1292         struct iattr iattr = { 0 };
1293         ENTRY;
1294
1295         if (req && req->rq_reqmsg != NULL)
1296                 request_body = lustre_msg_buf(req->rq_reqmsg, offset,
1297                                               sizeof(*request_body));
1298         if (req && req->rq_repmsg != NULL)
1299                 reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1300                                             sizeof(*reply_body));
1301
1302         fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1303
1304         CDEBUG(D_INODE, "inode %p ino %s nlink %d orphan %d\n", inode, fidname,
1305                inode->i_nlink, mds_orphan_open_count(inode));
1306
1307         last_orphan = mds_orphan_open_dec_test(inode) &&
1308                       mds_inode_is_orphan(inode);
1309
1310         /* this is half of the actual "close" */
1311         if (mfd->mfd_mode & FMODE_WRITE) {
1312                 rc = mds_put_write_access(mds, inode, request_body,
1313                                           last_orphan && unlink_orphan);
1314         } else if (mfd->mfd_mode & MDS_FMODE_EXEC) {
1315                 mds_allow_write_access(inode);
1316         }
1317         /* here writecount change also needs protection from orphan write sem. 
1318          * so drop orphan write sem after mds_put_write_access, bz 12888. */
1319         MDS_UP_WRITE_ORPHAN_SEM(inode);
1320
1321         if (last_orphan && unlink_orphan) {
1322                 int stripe_count = 0;
1323                 /* mds_put_write_access must have succeeded */
1324                 LASSERTF(rc == 0, "inode %lu/%u: rc %d",
1325                          inode->i_ino, inode->i_generation, rc);
1326
1327                 CDEBUG(D_INODE, "destroying orphan object %s\n", fidname);
1328
1329                 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
1330                     (S_ISDIR(inode->i_mode) && inode->i_nlink != 2))
1331                         CERROR("found \"orphan\" %s %s with link count %d\n",
1332                                S_ISREG(inode->i_mode) ? "file" : "dir",
1333                                fidname, inode->i_nlink);
1334
1335                 /* Sadly, there is no easy way to save pending_child from
1336                  * mds_reint_unlink() into mfd, so we need to re-lookup,
1337                  * but normally it will still be in the dcache. */
1338                 LOCK_INODE_MUTEX(pending_dir);
1339                 cleanup_phase = 1; /* UNLOCK_INODE_MUTEX(pending_dir) when finished */
1340                 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1341                                                fidlen);
1342                 if (IS_ERR(pending_child))
1343                         GOTO(cleanup, rc = PTR_ERR(pending_child));
1344                 LASSERT(pending_child->d_inode != NULL);
1345
1346                 cleanup_phase = 2; /* dput(pending_child) when finished */
1347                 if (S_ISDIR(pending_child->d_inode->i_mode)) {
1348                         rc = vfs_rmdir(pending_dir, pending_child);
1349                         if (rc)
1350                                 CERROR("error unlinking orphan dir %s: rc %d\n",
1351                                        fidname,rc);
1352                         goto out;
1353                 }
1354
1355                 if (lmm != NULL) {
1356                         stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
1357                 }
1358
1359                 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK,
1360                                           NULL, stripe_count);
1361                 if (IS_ERR(handle)) {
1362                         rc = PTR_ERR(handle);
1363                         handle = NULL;
1364                         GOTO(cleanup, rc);
1365                 }
1366                 if (lmm != NULL && (*valid & OBD_MD_FLEASIZE) &&
1367                     mds_log_op_unlink(obd, lmm, lmm_size,
1368                                       logcookies, cookies_size) > 0) {
1369                         *valid |= OBD_MD_FLCOOKIE;
1370                 }
1371
1372                 dp.ldp_inum = 0;
1373                 dp.ldp_ptr = req;
1374                 pending_child->d_fsdata = (void *) &dp;
1375                 rc = vfs_unlink(pending_dir, pending_child);
1376                 if (rc)
1377                         CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1378
1379                 goto out; /* Don't bother updating attrs on unlinked inode */
1380         }
1381
1382 #if 0
1383         if (request_body != NULL && mfd->mfd_mode & FMODE_WRITE && rc == 0) {
1384                 /* Update the on-disk attributes if this was the last write
1385                  * close, and all information was provided (i.e., rc == 0)
1386                  *
1387                  * XXX this should probably be abstracted with mds_reint_setattr
1388                  */
1389
1390                 if (request_body->valid & OBD_MD_FLMTIME &&
1391                     LTIME_S(iattr.ia_mtime) > LTIME_S(inode->i_mtime)) {
1392                         LTIME_S(iattr.ia_mtime) = request_body->mtime;
1393                         iattr.ia_valid |= ATTR_MTIME;
1394                 }
1395                 if (request_body->valid & OBD_MD_FLCTIME &&
1396                     LTIME_S(iattr.ia_ctime) > LTIME_S(inode->i_ctime)) {
1397                         LTIME_S(iattr.ia_ctime) = request_body->ctime;
1398                         iattr.ia_valid |= ATTR_CTIME;
1399                 }
1400
1401                 /* XXX can't set block count with fsfilt_setattr (!) */
1402                 if (request_body->valid & OBD_MD_FLSIZE) {
1403                         iattr.ia_valid |= ATTR_SIZE;
1404                         iattr.ia_size = request_body->size;
1405                 }
1406                 /* iattr.ia_blocks = request_body->blocks */
1407
1408         }
1409 #endif
1410         if (request_body != NULL && request_body->valid & OBD_MD_FLATIME) {
1411                 /* Only start a transaction to write out only the atime if
1412                  * it is more out-of-date than the specified limit.  If we
1413                  * are already going to write out the atime then do it anyway.
1414                  * */
1415                 LTIME_S(iattr.ia_atime) = request_body->atime;
1416                 if ((LTIME_S(iattr.ia_atime) >
1417                      LTIME_S(inode->i_atime) + mds->mds_atime_diff) ||
1418                     (iattr.ia_valid != 0 &&
1419                      LTIME_S(iattr.ia_atime) > LTIME_S(inode->i_atime)))
1420                         iattr.ia_valid |= ATTR_ATIME;
1421         }
1422
1423         if (iattr.ia_valid != 0) {
1424                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1425                 if (IS_ERR(handle)) {
1426                         rc = PTR_ERR(handle);
1427                         handle = NULL;
1428                         GOTO(cleanup, rc);
1429                 }
1430                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1431                 if (rc)
1432                         CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1433         }
1434 out:
1435         /* If other clients have this file open for write, rc will be > 0 */
1436         if (rc > 0)
1437                 rc = 0;
1438         l_dput(mfd->mfd_dentry);
1439         mds_mfd_put(mfd);
1440
1441  cleanup:
1442         if (req != NULL && reply_body != NULL) {
1443                 rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0, 0);
1444         } else if (handle) {
1445                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1446                 if (err) {
1447                         CERROR("error committing close: %d\n", err);
1448                         if (!rc)
1449                                 rc = err;
1450                 }
1451         }
1452
1453         switch (cleanup_phase) {
1454         case 2:
1455                 dput(pending_child);
1456         case 1:
1457                 UNLOCK_INODE_MUTEX(pending_dir);
1458         }
1459         RETURN(rc);
1460 }
1461
1462 int mds_close(struct ptlrpc_request *req, int offset)
1463 {
1464         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1465         struct obd_device *obd = req->rq_export->exp_obd;
1466         struct mds_body *body;
1467         struct mds_file_data *mfd;
1468         struct lvfs_run_ctxt saved;
1469         struct inode *inode;
1470         int rc, repsize[4] = { sizeof(struct ptlrpc_body),
1471                                sizeof(struct mds_body),
1472                                obd->u.mds.mds_max_mdsize,
1473                                obd->u.mds.mds_max_cookiesize };
1474         struct mds_body *reply_body;
1475         struct lov_mds_md *lmm;
1476         int lmm_size;
1477         struct llog_cookie *logcookies;
1478         int cookies_size;
1479         ENTRY;
1480
1481         rc = lustre_pack_reply(req, 4, repsize, NULL);
1482         if (rc)
1483                 req->rq_status = rc;
1484                 /* continue on to drop local open even if we can't send reply */
1485         else
1486                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1487
1488         CDEBUG(D_INODE, "close req->rep_len %d mdsize %d cookiesize %d\n",
1489                req->rq_replen,
1490                obd->u.mds.mds_max_mdsize, obd->u.mds.mds_max_cookiesize);
1491         mds_counter_incr(req->rq_export, LPROC_MDS_CLOSE);
1492
1493         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1494                                   lustre_swab_mds_body);
1495         if (body == NULL) {
1496                 CERROR("Can't unpack body\n");
1497                 req->rq_status = -EFAULT;
1498                 RETURN(-EFAULT);
1499         }
1500
1501         if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1502                 /* do some stuff */ ;
1503
1504         spin_lock(&med->med_open_lock);
1505         mfd = mds_handle2mfd(&body->handle);
1506         if (mfd == NULL) {
1507                 spin_unlock(&med->med_open_lock);
1508                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1509                           ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1510                 req->rq_status = -ESTALE;
1511                 RETURN(-ESTALE);
1512         }
1513         /* Remove mfd handle so it can't be found again.  We consume mfd_list
1514          * reference here, but still have mds_handle2mfd ref until mfd_close. */
1515         mds_mfd_unlink(mfd, 1);
1516         spin_unlock(&med->med_open_lock);
1517
1518         inode = mfd->mfd_dentry->d_inode;
1519         /* child orphan sem protects orphan_dec_test && is_orphan race */
1520         MDS_DOWN_WRITE_ORPHAN_SEM(inode); /* mds_mfd_close drops this */
1521         if (mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
1522                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1523                                       sizeof(*body));
1524                 LASSERT(body != NULL);
1525
1526                 mds_pack_inode2fid(&body->fid1, inode);
1527                 mds_pack_inode2body(body, inode);
1528                 mds_pack_md(obd, req->rq_repmsg, REPLY_REC_OFF + 1, body, inode,
1529                             MDS_PACK_MD_LOCK, 0);
1530         }
1531
1532         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1533         reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply_body));
1534         lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, 0);
1535         lmm_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1),
1536         logcookies = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 2, 0);
1537         cookies_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 2);
1538         req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1,
1539                                        lmm, lmm_size, logcookies, cookies_size,
1540                                        &reply_body->valid);
1541         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1542
1543         mds_shrink_reply(obd, req, body, REPLY_REC_OFF + 1);
1544         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1545                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1546                 req->rq_status = -ENOMEM;
1547                 RETURN(-ENOMEM);
1548         }
1549
1550         RETURN(rc);
1551 }
1552
1553 int mds_done_writing(struct ptlrpc_request *req, int offset)
1554 {
1555         struct mds_body *body;
1556         int rc, size[2] = { sizeof(struct ptlrpc_body),
1557                             sizeof(struct mds_body) };
1558         ENTRY;
1559
1560         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1561
1562         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1563                                   lustre_swab_mds_body);
1564         if (body == NULL) {
1565                 CERROR("Can't unpack body\n");
1566                 req->rq_status = -EFAULT;
1567                 RETURN(-EFAULT);
1568         }
1569
1570         rc = lustre_pack_reply(req, 2, size, NULL);
1571         if (rc)
1572                 req->rq_status = rc;
1573
1574         RETURN(0);
1575 }