Whamcloud - gitweb
53c4af6e2ea4c89d42f60a29ecd07f5d4b0c3539
[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
443                 rc = obd_setattr_rqset(mds->mds_osc_exp, &oinfo, &oti);
444                 if (rc) {
445                         CERROR("error setting attrs for inode %lu: rc %d\n",
446                                inode->i_ino, rc);
447                         if (rc > 0) {
448                                 CERROR("obd_setattr_async returned bad rc %d\n",
449                                        rc);
450                                 rc = -EIO;
451                         }
452                         GOTO(out_oa, rc);
453                 }
454         }
455
456         body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
457         obdo_refresh_inode(inode, oinfo.oi_oa, OBD_MD_FLBLKSZ);
458
459         LASSERT(oinfo.oi_md && oinfo.oi_md->lsm_object_id);
460         lmm = NULL;
461         rc = obd_packmd(mds->mds_osc_exp, &lmm, oinfo.oi_md);
462         if (rc < 0) {
463                 CERROR("cannot pack lsm, err = %d\n", rc);
464                 GOTO(out_oa, rc);
465         }
466         lmm_size = rc;
467         body->eadatasize = rc;
468
469         if (*handle == NULL)
470                 *handle = fsfilt_start(obd, inode, FSFILT_OP_CREATE, NULL);
471         if (IS_ERR(*handle)) {
472                 rc = PTR_ERR(*handle);
473                 *handle = NULL;
474                 GOTO(free_diskmd, rc);
475         }
476
477         rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
478         lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
479         LASSERT(lmm_buf);
480         memcpy(lmm_buf, lmm, lmm_size);
481         *objid = lmm_buf; /* save for mds_lov_update_objid */
482
483  free_diskmd:
484         obd_free_diskmd(mds->mds_osc_exp, &lmm);
485  out_oa:
486         oti_free_cookies(&oti);
487         OBDO_FREE(oinfo.oi_oa);
488  out_ids:
489         if (oinfo.oi_md)
490                 obd_free_memmd(mds->mds_osc_exp, &oinfo.oi_md);
491         RETURN(rc);
492 }
493
494 static void reconstruct_open(struct mds_update_record *rec, int offset,
495                              struct ptlrpc_request *req,
496                              struct lustre_handle *child_lockh)
497 {
498         struct mds_export_data *med = &req->rq_export->exp_mds_data;
499         struct mds_client_data *mcd = med->med_mcd;
500         struct mds_obd *mds = mds_req2mds(req);
501         struct mds_file_data *mfd;
502         struct obd_device *obd = req->rq_export->exp_obd;
503         struct dentry *parent, *dchild;
504         struct ldlm_reply *rep;
505         struct mds_body *body;
506         int rc;
507         struct list_head *t;
508         int put_child = 1;
509         ENTRY;
510
511         LASSERT(offset == DLM_INTENT_REC_OFF); /* only called via intent */
512         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
513         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
514
515         /* copy rc, transno and disp; steal locks */
516         mds_req_from_mcd(req, mcd);
517         intent_set_disposition(rep, le32_to_cpu(mcd->mcd_last_data));
518
519         /* Only replay if create or open actually happened. */
520         if (!intent_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
521                 EXIT;
522                 return; /* error looking up parent or child */
523         }
524
525         parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
526         LASSERT(!IS_ERR(parent));
527
528         dchild = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
529         LASSERT(!IS_ERR(dchild));
530
531         if (!dchild->d_inode)
532                 GOTO(out_dput, 0); /* child not present to open */
533
534         /* At this point, we know we have a child. We'll send
535          * it back _unless_ it not created and open failed.
536          */
537         if (intent_disposition(rep, DISP_OPEN_OPEN) &&
538             !intent_disposition(rep, DISP_OPEN_CREATE) &&
539             req->rq_status) {
540                 GOTO(out_dput, 0);
541         }
542
543         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
544         mds_pack_inode2body(body, dchild->d_inode);
545         if (S_ISREG(dchild->d_inode->i_mode)) {
546                 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
547                                  body, dchild->d_inode, 1, 0);
548
549                 if (rc)
550                         LASSERT(rc == req->rq_status);
551
552                 /* If we have LOV EA data, the OST holds size, mtime */
553                 if (!(body->valid & OBD_MD_FLEASIZE))
554                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
555                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
556         }
557
558         if (!(rec->ur_flags & MDS_OPEN_JOIN_FILE))
559                 lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
560                                     body->eadatasize, 0);
561
562         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
563             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
564                 int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
565
566                 rc = mds_pack_acl(med, dchild->d_inode, req->rq_repmsg,
567                                   body, acl_off);
568                 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
569                 if (!req->rq_status && rc)
570                         req->rq_status = rc;
571         }
572
573         /* If we have -EEXIST as the status, and we were asked to create
574          * exclusively, we can tell we failed because the file already existed.
575          */
576         if (req->rq_status == -EEXIST &&
577             ((rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_EXCL)) ==
578              (MDS_OPEN_CREAT | MDS_OPEN_EXCL))) {
579                 GOTO(out_dput, 0);
580         }
581
582         /* If we didn't get as far as trying to open, then some locking thing
583          * probably went wrong, and we'll just bail here.
584          */
585         if (!intent_disposition(rep, DISP_OPEN_OPEN))
586                 GOTO(out_dput, 0);
587
588         /* If we failed, then we must have failed opening, so don't look for
589          * file descriptor or anything, just give the client the bad news.
590          */
591         if (req->rq_status)
592                 GOTO(out_dput, 0);
593
594         mfd = NULL;
595         spin_lock(&med->med_open_lock);
596         list_for_each(t, &med->med_open_head) {
597                 mfd = list_entry(t, struct mds_file_data, mfd_list);
598                 if (mfd->mfd_xid == req->rq_xid) {
599                         mds_mfd_addref(mfd);
600                         break;
601                 }
602                 mfd = NULL;
603         }
604         spin_unlock(&med->med_open_lock);
605
606         /* #warning "XXX fixme" bug 2991 */
607         /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
608          * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
609          * to detect a re-open */
610         if (mfd == NULL) {
611                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
612                         rc = mds_join_file(rec, req, dchild, NULL);
613                         if (rc)
614                                 GOTO(out_dput, rc);
615                 }
616                 mntget(mds->mds_vfsmnt);
617                 CERROR("Re-opened file \n");
618                 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt,
619                                       rec->ur_flags & ~MDS_OPEN_TRUNC, req);
620                 if (!mfd) {
621                         CERROR("mds: out of memory\n");
622                         GOTO(out_dput, req->rq_status = -ENOMEM);
623                 }
624                 put_child = 0;
625         } else {
626                 body->handle.cookie = mfd->mfd_handle.h_cookie;
627                 CDEBUG(D_INODE, "resend mfd %p, cookie "LPX64"\n", mfd,
628                        mfd->mfd_handle.h_cookie);
629         }
630
631         mds_mfd_put(mfd);
632
633  out_dput:
634         if (put_child)
635                 l_dput(dchild);
636         l_dput(parent);
637         EXIT;
638 }
639
640 /* do NOT or the MAY_*'s, you'll get the weakest */
641 static int accmode(struct inode *inode, int flags)
642 {
643         int res = 0;
644
645         /* Sadly, NFSD reopens a file repeatedly during operation, so the
646          * "acc_mode = 0" allowance for newly-created files isn't honoured.
647          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
648          * owner can write to a file even if it is marked readonly to hide
649          * its brokenness. (bug 5781) */
650         if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current->fsuid)
651                 return 0;
652
653         if (flags & FMODE_READ)
654                 res = MAY_READ;
655         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
656                 res |= MAY_WRITE;
657         if (flags & MDS_FMODE_EXEC)
658                 res = MAY_EXEC;
659         return res;
660 }
661
662 /* Handles object creation, actual opening, and I/O epoch */
663 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
664                            struct mds_body *body, int flags, void **handle,
665                            struct mds_update_record *rec,struct ldlm_reply *rep,
666                            struct lustre_handle *lockh)
667 {
668         struct mds_obd *mds = mds_req2mds(req);
669         struct obd_device *obd = req->rq_export->exp_obd;
670         struct mds_file_data *mfd = NULL;
671         struct lov_mds_md *lmm = NULL; /* object IDs created */
672         int rc = 0;
673         ENTRY;
674
675         /* atomically create objects if necessary */
676         LOCK_INODE_MUTEX(dchild->d_inode);
677
678         if (S_ISREG(dchild->d_inode->i_mode) &&
679             !(body->valid & OBD_MD_FLEASIZE)) {
680                 rc = mds_pack_md(obd, req->rq_repmsg, DLM_REPLY_REC_OFF + 1,
681                                  body, dchild->d_inode, 0, 0);
682                 if (rc) {
683                         UNLOCK_INODE_MUTEX(dchild->d_inode);
684                         RETURN(rc);
685                 }
686         }
687         if (rec != NULL) {
688                 if ((body->valid & OBD_MD_FLEASIZE) &&
689                     (rec->ur_flags & MDS_OPEN_HAS_EA)) {
690                         UNLOCK_INODE_MUTEX(dchild->d_inode);
691                         RETURN(-EEXIST);
692                 }
693                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
694                         UNLOCK_INODE_MUTEX(dchild->d_inode);
695                         rc = mds_join_file(rec, req, dchild, lockh);
696                         if (rc)
697                                 RETURN(rc);
698                         LOCK_INODE_MUTEX(dchild->d_inode);
699                 }
700                 if (!(body->valid & OBD_MD_FLEASIZE) &&
701                     !(body->valid & OBD_MD_FLMODEASIZE)) {
702                         /* no EA: create objects */
703                         rc = mds_create_objects(req, DLM_REPLY_REC_OFF + 1, rec,
704                                                 mds, obd, dchild, handle, &lmm);
705                         if (rc) {
706                                 CERROR("mds_create_objects: rc = %d\n", rc);
707                                 UNLOCK_INODE_MUTEX(dchild->d_inode);
708                                 RETURN(rc);
709                         }
710                 }
711         }
712         /* If the inode has no EA data, then MDS holds size, mtime */
713         if (S_ISREG(dchild->d_inode->i_mode) &&
714             !(body->valid & OBD_MD_FLEASIZE)) {
715                 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
716                                 OBD_MD_FLATIME | OBD_MD_FLMTIME);
717         }
718         UNLOCK_INODE_MUTEX(dchild->d_inode);
719
720         if (rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE))
721                 lustre_shrink_reply(req, DLM_REPLY_REC_OFF + 1,
722                                     body->eadatasize, 0);
723
724         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
725             rec && !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
726                 int acl_off = DLM_REPLY_REC_OFF + (body->eadatasize ? 2 : 1);
727
728                 rc = mds_pack_acl(&req->rq_export->exp_mds_data,
729                                   dchild->d_inode, req->rq_repmsg,
730                                   body, acl_off);
731                 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
732                 if (rc)
733                         RETURN(rc);
734         }
735
736         intent_set_disposition(rep, DISP_OPEN_OPEN);
737         mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
738         if (IS_ERR(mfd))
739                 RETURN(PTR_ERR(mfd));
740
741         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
742                mfd->mfd_handle.h_cookie);
743
744         mds_lov_update_objids(obd, lmm);
745
746         if (rc) /* coverity[deadcode] */
747                 mds_mfd_unlink(mfd, 1);
748
749         mds_mfd_put(mfd);
750         RETURN(rc);
751 }
752
753 static int mds_open_by_fid(struct ptlrpc_request *req, struct ll_fid *fid,
754                            struct mds_body *body, int flags,
755                            struct mds_update_record *rec,struct ldlm_reply *rep)
756 {
757         struct mds_obd *mds = mds_req2mds(req);
758         struct dentry *dchild;
759         char fidname[LL_FID_NAMELEN];
760         int fidlen = 0, rc;
761         void *handle = NULL;
762         ENTRY;
763
764         fidlen = ll_fid2str(fidname, fid->id, fid->generation);
765         dchild = ll_lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
766         if (IS_ERR(dchild)) {
767                 rc = PTR_ERR(dchild);
768                 CERROR("error looking up %s in PENDING: rc = %d\n",fidname, rc);
769                 RETURN(rc);
770         }
771
772         if (dchild->d_inode != NULL) {
773                 mds_inode_set_orphan(dchild->d_inode);
774                 CWARN("Orphan %s found and opened in PENDING directory\n",
775                        fidname);
776         } else {
777                 l_dput(dchild);
778
779                 /* We didn't find it in PENDING so it isn't an orphan.  See
780                  * if it was a regular inode that was previously created. */
781                 dchild = mds_fid2dentry(mds, fid, NULL);
782                 if (IS_ERR(dchild))
783                         RETURN(PTR_ERR(dchild));
784         }
785
786         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
787         mds_pack_inode2body(body, dchild->d_inode);
788         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
789         intent_set_disposition(rep, DISP_LOOKUP_POS);
790
791         rc = mds_finish_open(req, dchild, body, flags, &handle, rec, rep, NULL);
792         rc = mds_finish_transno(mds, dchild->d_inode, handle,
793                                 req, rc, rep ? rep->lock_policy_res1 : 0, 0);
794         /* XXX what do we do here if mds_finish_transno itself failed? */
795
796         l_dput(dchild);
797         RETURN(rc);
798 }
799
800 int mds_pin(struct ptlrpc_request *req, int offset)
801 {
802         struct obd_device *obd = req->rq_export->exp_obd;
803         struct mds_body *reqbody, *repbody;
804         struct lvfs_run_ctxt saved;
805         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
806         ENTRY;
807
808         reqbody = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*reqbody));
809
810         rc = lustre_pack_reply(req, 2, size, NULL);
811         if (rc)
812                 RETURN(rc);
813
814         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
815                                  sizeof(*repbody));
816
817         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
818         rc = mds_open_by_fid(req, &reqbody->fid1, repbody, reqbody->flags, NULL,
819                              NULL);
820         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
821
822         RETURN(rc);
823 }
824
825 /*  Get an internal lock on the inode number (but not generation) to sync
826  *  new inode creation with inode unlink (bug 2029).  If child_lockh is NULL
827  *  we just get the lock as a barrier to wait for other holders of this lock,
828  *  and drop it right away again. */
829 int mds_lock_new_child(struct obd_device *obd, struct inode *inode,
830                        struct lustre_handle *child_lockh)
831 {
832         struct ldlm_res_id child_res_id = { .name = { inode->i_ino, 0, 1, 0 } };
833         struct lustre_handle lockh;
834         int lock_flags = LDLM_FL_ATOMIC_CB;
835         int rc;
836
837         if (child_lockh == NULL)
838                 child_lockh = &lockh;
839
840         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
841                                     LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
842                                     ldlm_blocking_ast, ldlm_completion_ast,
843                                     NULL, NULL, 0, NULL, child_lockh);
844         if (rc != ELDLM_OK)
845                 CERROR("ldlm_cli_enqueue_local: %d\n", rc);
846         else if (child_lockh == &lockh)
847                 ldlm_lock_decref(child_lockh, LCK_EX);
848
849         RETURN(rc);
850 }
851
852 int mds_open(struct mds_update_record *rec, int offset,
853              struct ptlrpc_request *req, struct lustre_handle *child_lockh)
854 {
855         /* XXX ALLOCATE _something_ - 464 bytes on stack here */
856         struct obd_device *obd = req->rq_export->exp_obd;
857         struct mds_obd *mds = mds_req2mds(req);
858         struct ldlm_reply *rep = NULL;
859         struct mds_body *body = NULL;
860         struct dentry *dchild = NULL, *dparent = NULL;
861         struct mds_export_data *med;
862         struct lustre_handle parent_lockh;
863         int rc = 0, cleanup_phase = 0, acc_mode, created = 0;
864         int parent_mode = LCK_CR;
865         void *handle = NULL;
866         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
867         unsigned int qcids[MAXQUOTAS] = { current->fsuid, current->fsgid };
868         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
869         int child_mode = LCK_CR;
870         /* Always returning LOOKUP lock if open succesful to guard
871            dentry on client. */
872         ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_LOOKUP}};
873         struct ldlm_res_id child_res_id = { .name = {0}};
874         int lock_flags = 0;
875         int rec_pending = 0;
876         unsigned int gid = current->fsgid;
877         ENTRY;
878
879         mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
880
881         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PAUSE_OPEN | OBD_FAIL_ONCE,
882                          (obd_timeout + 1) / 4);
883
884         CLASSERT(MAXQUOTAS < 4);
885         if (offset == DLM_INTENT_REC_OFF) { /* intent */
886                 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
887                                      sizeof(*rep));
888                 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
889                                       sizeof(*body));
890         } else if (offset == REQ_REC_OFF) { /* non-intent reint */
891                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
892                                       sizeof(*body));
893                 LBUG(); /* XXX: not supported yet? */
894         } else {
895                 body = NULL;
896                 LBUG();
897         }
898
899         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
900
901         /* Step 0: If we are passed a fid, then we assume the client already
902          * opened this file and is only replaying the RPC, so we open the
903          * inode by fid (at some large expense in security). */
904         /*XXX liblustre use mds_open_by_fid to implement LL_IOC_LOV_SETSTRIPE */
905         if (((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
906              (req->rq_export->exp_libclient && rec->ur_flags&MDS_OPEN_HAS_EA))&&
907             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
908                 if (rec->ur_fid2->id == 0) {
909                         struct ldlm_lock *lock = ldlm_handle2lock(child_lockh);
910                         if (lock) {
911                                 LDLM_ERROR(lock, "fid2 not set on open replay");
912                                 LDLM_LOCK_PUT(lock);
913                         }
914                         DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
915                         RETURN(-EFAULT);
916                 }
917
918                 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
919                                      rec, rep);
920                 if (rc != -ENOENT) {
921                         if (req->rq_export->exp_libclient &&
922                             rec->ur_flags & MDS_OPEN_HAS_EA)
923                                 RETURN(0);
924
925                         RETURN(rc);
926                 }
927
928                 /* We didn't find the correct inode on disk either, so we
929                  * need to re-create it via a regular replay. */
930                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
931                         DEBUG_REQ(D_ERROR, req,"OPEN_CREAT not in open replay");
932                         RETURN(-EFAULT);
933                 }
934         } else if (rec->ur_fid2->id) {
935                 DEBUG_REQ(D_ERROR, req, "fid2 "LPU64"/%u on open non-replay",
936                           rec->ur_fid2->id, rec->ur_fid2->generation);
937                 RETURN(-EFAULT);
938         }
939
940         /* If we got here, we must be called via intent */
941         LASSERT(offset == DLM_INTENT_REC_OFF);
942
943         med = &req->rq_export->exp_mds_data;
944         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
945                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
946                 RETURN(-ENOMEM);
947         }
948
949         /* Step 1: Find and lock the parent */
950         if (rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_JOIN_FILE))
951                 parent_mode = LCK_EX;
952         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
953                                         &parent_lockh, rec->ur_name,
954                                         rec->ur_namelen - 1,
955                                         MDS_INODELOCK_UPDATE);
956         if (IS_ERR(dparent)) {
957                 rc = PTR_ERR(dparent);
958                 if (rc != -ENOENT) {
959                         CERROR("parent "LPU64"/%u lookup error %d\n",
960                                rec->ur_fid1->id, rec->ur_fid1->generation, rc);
961                 } else {
962                         /* Just cannot find parent - make it look like
963                          * usual negative lookup to avoid extra MDS RPC */
964                         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
965                         intent_set_disposition(rep, DISP_LOOKUP_NEG);
966                 }
967                 GOTO(cleanup, rc);
968         }
969         LASSERT(dparent->d_inode != NULL);
970
971         cleanup_phase = 1; /* parent dentry and lock */
972
973         if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
974                 dchild = dget(dparent);
975                 cleanup_phase = 2; /* child dentry */
976                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
977                 GOTO(found_child, rc);
978         }
979
980         /* Step 2: Lookup the child */
981       
982         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
983             (rec->ur_flags & MDS_OPEN_LOCK) && (rec->ur_namelen == 1)) {
984                 /* hack for nfsd with no_subtree_check, it will use anon
985                  * dentry w/o filename to open the file. the anon dentry's
986                  * parent was set to itself, so rec->ur_fid1 is the file.
987                  * And in MDC it cannot derive the dentry's parent dentry,
988                  * hence the file's name, so we hack here in MDS, 
989                  * refer to bug 13030. */
990                 dchild = mds_fid2dentry(mds, rec->ur_fid1, NULL);
991         } else {
992                 dchild = ll_lookup_one_len(rec->ur_name, dparent,
993                                            rec->ur_namelen - 1);
994         }
995         if (IS_ERR(dchild)) {
996                 rc = PTR_ERR(dchild);
997                 dchild = NULL; /* don't confuse mds_finish_transno() below */
998                 GOTO(cleanup, rc);
999         }
1000
1001         cleanup_phase = 2; /* child dentry */
1002
1003         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
1004         if (dchild->d_inode)
1005                 intent_set_disposition(rep, DISP_LOOKUP_POS);
1006         else
1007                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1008
1009         /*Step 3: If the child was negative, and we're supposed to, create it.*/
1010         if (dchild->d_inode == NULL) {
1011                 unsigned long ino = rec->ur_fid2->id;
1012                 struct iattr iattr;
1013                 struct inode *inode;
1014
1015                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
1016                         /* It's negative and we weren't supposed to create it */
1017                         GOTO(cleanup, rc = -ENOENT);
1018                 }
1019
1020                 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1021                         GOTO(cleanup, rc = -EROFS);
1022
1023                 if (dparent->d_inode->i_mode & S_ISGID)
1024                         gid = dparent->d_inode->i_gid;
1025                 else
1026                         gid = current->fsgid;
1027                 rc = lquota_chkquota(mds_quota_interface_ref, obd,
1028                                      current->fsuid, gid, 1, &rec_pending);
1029
1030                 if (rc < 0)
1031                         GOTO(cleanup, rc);
1032
1033                 intent_set_disposition(rep, DISP_OPEN_CREATE);
1034                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
1035                                       NULL);
1036                 if (IS_ERR(handle)) {
1037                         rc = PTR_ERR(handle);
1038                         handle = NULL;
1039                         GOTO(cleanup, rc);
1040                 }
1041                 dchild->d_fsdata = (void *) &dp;
1042                 dp.ldp_ptr = req;
1043                 dp.ldp_inum = ino;
1044
1045                 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
1046                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
1047                         dchild->d_fsdata = NULL;
1048
1049                 if (rc) {
1050                         CDEBUG(D_INODE, "error during create: %d\n", rc);
1051                         GOTO(cleanup, rc);
1052                 }
1053                 inode = dchild->d_inode;
1054                 if (ino) {
1055                         LASSERT(ino == inode->i_ino);
1056                         /* Written as part of setattr */
1057                         inode->i_generation = rec->ur_fid2->generation;
1058                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
1059                                inode->i_ino, inode->i_generation);
1060                 }
1061
1062                 created = 1;
1063                 LTIME_S(iattr.ia_atime) = rec->ur_time;
1064                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1065                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1066
1067                 iattr.ia_uid = current->fsuid;  /* set by push_ctxt already */
1068                 iattr.ia_gid = gid;
1069
1070                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1071                         ATTR_MTIME | ATTR_CTIME;
1072
1073                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1074                 if (rc)
1075                         CERROR("error on child setattr: rc = %d\n", rc);
1076
1077                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1078
1079                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1080                 if (rc)
1081                         CERROR("error on parent setattr: rc = %d\n", rc);
1082
1083                 rc = fsfilt_commit(obd, dchild->d_inode, handle, 0);
1084                 handle = NULL;
1085                 acc_mode = 0;           /* Don't check for permissions */
1086         } else {
1087                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1088         }
1089
1090         LASSERTF(!mds_inode_is_orphan(dchild->d_inode),
1091                  "dchild %.*s (%p) inode %p/%lu/%u\n", dchild->d_name.len,
1092                  dchild->d_name.name, dchild, dchild->d_inode,
1093                  dchild->d_inode->i_ino, dchild->d_inode->i_generation);
1094
1095 found_child:
1096         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
1097         mds_pack_inode2body(body, dchild->d_inode);
1098
1099         if (S_ISREG(dchild->d_inode->i_mode)) {
1100                 /* Check permissions etc */
1101                 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
1102                 if (rc != 0)
1103                         GOTO(cleanup, rc);
1104
1105                 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) &&
1106                     (acc_mode & MAY_WRITE))
1107                         GOTO(cleanup, rc = -EROFS);
1108
1109                 /* An append-only file must be opened in append mode for
1110                  * writing */
1111                 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
1112                     ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
1113                      (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
1114                         GOTO(cleanup, rc = -EPERM);
1115         }
1116
1117         if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
1118             (rec->ur_flags & MDS_OPEN_EXCL)) {
1119                 /* File already exists, we didn't just create it, and we
1120                  * were passed O_EXCL; err-or. */
1121                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
1122         }
1123
1124         /* if we are following a symlink, don't open */
1125         if (S_ISLNK(dchild->d_inode->i_mode))
1126                 GOTO(cleanup_no_trans, rc = 0);
1127
1128         if (S_ISDIR(dchild->d_inode->i_mode)) {
1129                 if (rec->ur_flags & MDS_OPEN_CREAT ||
1130                     rec->ur_flags & FMODE_WRITE) {
1131                         /* we are trying to create or write a exist dir */
1132                         GOTO(cleanup, rc = -EISDIR);
1133                 }
1134                 if (rec->ur_flags & MDS_FMODE_EXEC) {
1135                         /* we are trying to exec a directory */
1136                         GOTO(cleanup, rc = -EACCES);
1137                 }
1138                 if (ll_permission(dchild->d_inode, acc_mode, NULL)) {
1139                         intent_set_disposition(rep, DISP_OPEN_OPEN);
1140                         GOTO(cleanup, rc = -EACCES);
1141                 }
1142         } else if (rec->ur_flags & MDS_OPEN_DIRECTORY) {
1143                 GOTO(cleanup, rc = -ENOTDIR);
1144         }
1145
1146         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
1147                 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
1148                 GOTO(cleanup, rc = -EAGAIN);
1149         }
1150
1151         /* Obtain OPEN lock as well */
1152         policy.l_inodebits.bits |= MDS_INODELOCK_OPEN;
1153
1154         /* We cannot use acc_mode here, because it is zeroed in case of
1155            creating a file, so we get wrong lockmode */
1156         if (rec->ur_flags & FMODE_WRITE)
1157                 child_mode = LCK_CW;
1158         else if (rec->ur_flags & MDS_FMODE_EXEC)
1159                 child_mode = LCK_PR;
1160         else
1161                 child_mode = LCK_CR;
1162
1163         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) && 
1164              (rec->ur_flags & MDS_OPEN_LOCK)) {
1165                 /* In case of replay we do not get a lock assuming that the
1166                    caller has it already */
1167                 child_res_id.name[0] = dchild->d_inode->i_ino;
1168                 child_res_id.name[1] = dchild->d_inode->i_generation;
1169
1170                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &child_res_id,
1171                                             LDLM_IBITS, &policy, child_mode, 
1172                                             &lock_flags, ldlm_blocking_ast, 
1173                                             ldlm_completion_ast, NULL, NULL,
1174                                             0, NULL, child_lockh);
1175                 if (rc != ELDLM_OK)
1176                         GOTO(cleanup, rc);
1177
1178                 /* Let mds_intent_policy know that we have a lock to return */
1179                 intent_set_disposition(rep, DISP_OPEN_LOCK);
1180                 cleanup_phase = 3;
1181         }
1182
1183         if (!S_ISREG(dchild->d_inode->i_mode) &&
1184             !S_ISDIR(dchild->d_inode->i_mode) &&
1185             (req->rq_export->exp_connect_flags & OBD_CONNECT_NODEVOH)) {
1186                 /* If client supports this, do not return open handle for
1187                  * special device nodes */
1188                 GOTO(cleanup_no_trans, rc = 0);
1189         }
1190
1191         /* Step 5: mds_open it */
1192         rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
1193                              rep, &parent_lockh);
1194         GOTO(cleanup, rc);
1195
1196  cleanup:
1197         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
1198                                 req, rc, rep ? rep->lock_policy_res1 : 0, 0);
1199
1200  cleanup_no_trans:
1201         if (rec_pending)
1202                 lquota_pending_commit(mds_quota_interface_ref, obd,
1203                                       current->fsuid, gid, 1);
1204         switch (cleanup_phase) {
1205         case 3:
1206                 if (rc)
1207                         /* It is safe to leave IT_OPEN_LOCK set, if rc is not 0,
1208                          * mds_intent_policy won't try to return any locks */
1209                         ldlm_lock_decref(child_lockh, child_mode);
1210         case 2:
1211                 if (rc && created) {
1212                         int err = vfs_unlink(dparent->d_inode, dchild);
1213                         if (err) {
1214                                 CERROR("unlink(%.*s) in error path: %d\n",
1215                                        dchild->d_name.len, dchild->d_name.name,
1216                                        err);
1217                         }
1218                 } else if (created) {
1219                         mds_lock_new_child(obd, dchild->d_inode, NULL);
1220                         /* save uid/gid for quota acquire/release */
1221                         qpids[USRQUOTA] = dparent->d_inode->i_uid;
1222                         qpids[GRPQUOTA] = dparent->d_inode->i_gid;
1223                 }
1224                 l_dput(dchild);
1225         case 1:
1226                 if (dparent == NULL)
1227                         break;
1228
1229                 l_dput(dparent);
1230                 if (rc)
1231                         ldlm_lock_decref(&parent_lockh, parent_mode);
1232                 else
1233                         ptlrpc_save_lock(req, &parent_lockh, parent_mode);
1234         }
1235         /* trigger dqacq on the owner of child and parent */
1236         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1237                       FSFILT_OP_CREATE);
1238         RETURN(rc);
1239 }
1240
1241 /* Close a "file descriptor" and possibly unlink an orphan from the
1242  * PENDING directory.  Caller must hold child->i_mutex, this drops it.
1243  *
1244  * If we are being called from mds_disconnect() because the client has
1245  * disappeared, then req == NULL and we do not update last_rcvd because
1246  * there is nothing that could be recovered by the client at this stage
1247  * (it will not even _have_ an entry in last_rcvd anymore).
1248  *
1249  * Returns EAGAIN if the client needs to get more data and re-close. */
1250 int mds_mfd_close(struct ptlrpc_request *req, int offset,
1251                   struct obd_device *obd, struct mds_file_data *mfd,
1252                   int unlink_orphan, struct lov_mds_md *lmm, int lmm_size,
1253                   struct llog_cookie *logcookies, int cookies_size,
1254                   __u64 *valid)
1255 {
1256         struct inode *inode = mfd->mfd_dentry->d_inode;
1257         char fidname[LL_FID_NAMELEN];
1258         int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
1259         struct dentry *pending_child = NULL;
1260         struct mds_obd *mds = &obd->u.mds;
1261         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1262         void *handle = NULL;
1263         struct mds_body *request_body = NULL, *reply_body = NULL;
1264         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
1265         struct iattr iattr = { 0 };
1266         ENTRY;
1267
1268         if (req && req->rq_reqmsg != NULL)
1269                 request_body = lustre_msg_buf(req->rq_reqmsg, offset,
1270                                               sizeof(*request_body));
1271         if (req && req->rq_repmsg != NULL)
1272                 reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1273                                             sizeof(*reply_body));
1274
1275         fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1276
1277         CDEBUG(D_INODE, "inode %p ino %s nlink %d orphan %d\n", inode, fidname,
1278                inode->i_nlink, mds_orphan_open_count(inode));
1279
1280         last_orphan = mds_orphan_open_dec_test(inode) &&
1281                 mds_inode_is_orphan(inode);
1282         MDS_UP_WRITE_ORPHAN_SEM(inode);
1283
1284         /* this is half of the actual "close" */
1285         if (mfd->mfd_mode & FMODE_WRITE) {
1286                 rc = mds_put_write_access(mds, inode, request_body,
1287                                           last_orphan && unlink_orphan);
1288         } else if (mfd->mfd_mode & MDS_FMODE_EXEC) {
1289                 mds_allow_write_access(inode);
1290         }
1291
1292         if (last_orphan && unlink_orphan) {
1293                 int stripe_count = 0;
1294                 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
1295
1296                 CDEBUG(D_INODE, "destroying orphan object %s\n", fidname);
1297
1298                 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
1299                     (S_ISDIR(inode->i_mode) && inode->i_nlink != 2))
1300                         CERROR("found \"orphan\" %s %s with link count %d\n",
1301                                S_ISREG(inode->i_mode) ? "file" : "dir",
1302                                fidname, inode->i_nlink);
1303
1304                 /* Sadly, there is no easy way to save pending_child from
1305                  * mds_reint_unlink() into mfd, so we need to re-lookup,
1306                  * but normally it will still be in the dcache. */
1307                 LOCK_INODE_MUTEX(pending_dir);
1308                 cleanup_phase = 1; /* UNLOCK_INODE_MUTEX(pending_dir) when finished */
1309                 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1310                                                fidlen);
1311                 if (IS_ERR(pending_child))
1312                         GOTO(cleanup, rc = PTR_ERR(pending_child));
1313                 LASSERT(pending_child->d_inode != NULL);
1314
1315                 cleanup_phase = 2; /* dput(pending_child) when finished */
1316                 if (S_ISDIR(pending_child->d_inode->i_mode)) {
1317                         rc = vfs_rmdir(pending_dir, pending_child);
1318                         if (rc)
1319                                 CERROR("error unlinking orphan dir %s: rc %d\n",
1320                                        fidname,rc);
1321                         goto out;
1322                 }
1323
1324                 if (lmm != NULL) {
1325                         stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
1326                 }
1327
1328                 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK,
1329                                           NULL, stripe_count);
1330                 if (IS_ERR(handle)) {
1331                         rc = PTR_ERR(handle);
1332                         handle = NULL;
1333                         GOTO(cleanup, rc);
1334                 }
1335                 if (lmm != NULL && (*valid & OBD_MD_FLEASIZE) &&
1336                     mds_log_op_unlink(obd, lmm, lmm_size,
1337                                       logcookies, cookies_size) > 0) {
1338                         *valid |= OBD_MD_FLCOOKIE;
1339                 }
1340
1341                 dp.ldp_inum = 0;
1342                 dp.ldp_ptr = req;
1343                 pending_child->d_fsdata = (void *) &dp;
1344                 rc = vfs_unlink(pending_dir, pending_child);
1345                 if (rc)
1346                         CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1347
1348                 goto out; /* Don't bother updating attrs on unlinked inode */
1349         }
1350
1351 #if 0
1352         if (request_body != NULL && mfd->mfd_mode & FMODE_WRITE && rc == 0) {
1353                 /* Update the on-disk attributes if this was the last write
1354                  * close, and all information was provided (i.e., rc == 0)
1355                  *
1356                  * XXX this should probably be abstracted with mds_reint_setattr
1357                  */
1358
1359                 if (request_body->valid & OBD_MD_FLMTIME &&
1360                     LTIME_S(iattr.ia_mtime) > LTIME_S(inode->i_mtime)) {
1361                         LTIME_S(iattr.ia_mtime) = request_body->mtime;
1362                         iattr.ia_valid |= ATTR_MTIME;
1363                 }
1364                 if (request_body->valid & OBD_MD_FLCTIME &&
1365                     LTIME_S(iattr.ia_ctime) > LTIME_S(inode->i_ctime)) {
1366                         LTIME_S(iattr.ia_ctime) = request_body->ctime;
1367                         iattr.ia_valid |= ATTR_CTIME;
1368                 }
1369
1370                 /* XXX can't set block count with fsfilt_setattr (!) */
1371                 if (request_body->valid & OBD_MD_FLSIZE) {
1372                         iattr.ia_valid |= ATTR_SIZE;
1373                         iattr.ia_size = request_body->size;
1374                 }
1375                 /* iattr.ia_blocks = request_body->blocks */
1376
1377         }
1378 #endif
1379         if (request_body != NULL && request_body->valid & OBD_MD_FLATIME) {
1380                 /* Only start a transaction to write out only the atime if
1381                  * it is more out-of-date than the specified limit.  If we
1382                  * are already going to write out the atime then do it anyway.
1383                  * */
1384                 LTIME_S(iattr.ia_atime) = request_body->atime;
1385                 if ((LTIME_S(iattr.ia_atime) >
1386                      LTIME_S(inode->i_atime) + mds->mds_atime_diff) ||
1387                     (iattr.ia_valid != 0 &&
1388                      LTIME_S(iattr.ia_atime) > LTIME_S(inode->i_atime)))
1389                         iattr.ia_valid |= ATTR_ATIME;
1390         }
1391
1392         if (iattr.ia_valid != 0) {
1393                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1394                 if (IS_ERR(handle)) {
1395                         rc = PTR_ERR(handle);
1396                         handle = NULL;
1397                         GOTO(cleanup, rc);
1398                 }
1399                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1400                 if (rc)
1401                         CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1402         }
1403 out:
1404         /* If other clients have this file open for write, rc will be > 0 */
1405         if (rc > 0)
1406                 rc = 0;
1407         l_dput(mfd->mfd_dentry);
1408         mds_mfd_put(mfd);
1409
1410  cleanup:
1411         if (req != NULL && reply_body != NULL) {
1412                 rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0, 0);
1413         } else if (handle) {
1414                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1415                 if (err) {
1416                         CERROR("error committing close: %d\n", err);
1417                         if (!rc)
1418                                 rc = err;
1419                 }
1420         }
1421
1422         switch (cleanup_phase) {
1423         case 2:
1424                 dput(pending_child);
1425         case 1:
1426                 UNLOCK_INODE_MUTEX(pending_dir);
1427         }
1428         RETURN(rc);
1429 }
1430
1431 int mds_close(struct ptlrpc_request *req, int offset)
1432 {
1433         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1434         struct obd_device *obd = req->rq_export->exp_obd;
1435         struct mds_body *body;
1436         struct mds_file_data *mfd;
1437         struct lvfs_run_ctxt saved;
1438         struct inode *inode;
1439         int rc, repsize[4] = { sizeof(struct ptlrpc_body),
1440                                sizeof(struct mds_body),
1441                                obd->u.mds.mds_max_mdsize,
1442                                obd->u.mds.mds_max_cookiesize };
1443         struct mds_body *reply_body;
1444         struct lov_mds_md *lmm;
1445         int lmm_size;
1446         struct llog_cookie *logcookies;
1447         int cookies_size;
1448         ENTRY;
1449
1450         rc = lustre_pack_reply(req, 4, repsize, NULL);
1451         if (rc)
1452                 req->rq_status = rc;
1453                 /* continue on to drop local open even if we can't send reply */
1454         else
1455                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1456
1457         CDEBUG(D_INODE, "close req->rep_len %d mdsize %d cookiesize %d\n",
1458                req->rq_replen,
1459                obd->u.mds.mds_max_mdsize, obd->u.mds.mds_max_cookiesize);
1460         mds_counter_incr(req->rq_export, LPROC_MDS_CLOSE);
1461
1462         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1463                                   lustre_swab_mds_body);
1464         if (body == NULL) {
1465                 CERROR("Can't unpack body\n");
1466                 req->rq_status = -EFAULT;
1467                 RETURN(-EFAULT);
1468         }
1469
1470         if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1471                 /* do some stuff */ ;
1472
1473         spin_lock(&med->med_open_lock);
1474         mfd = mds_handle2mfd(&body->handle);
1475         if (mfd == NULL) {
1476                 spin_unlock(&med->med_open_lock);
1477                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1478                           ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1479                 req->rq_status = -ESTALE;
1480                 RETURN(-ESTALE);
1481         }
1482         /* Remove mfd handle so it can't be found again.  We consume mfd_list
1483          * reference here, but still have mds_handle2mfd ref until mfd_close. */
1484         mds_mfd_unlink(mfd, 1);
1485         spin_unlock(&med->med_open_lock);
1486
1487         inode = mfd->mfd_dentry->d_inode;
1488         /* child orphan sem protects orphan_dec_test && is_orphan race */
1489         MDS_DOWN_WRITE_ORPHAN_SEM(inode); /* mds_mfd_close drops this */
1490         if (mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
1491                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1492                                       sizeof(*body));
1493                 LASSERT(body != NULL);
1494
1495                 mds_pack_inode2fid(&body->fid1, inode);
1496                 mds_pack_inode2body(body, inode);
1497                 mds_pack_md(obd, req->rq_repmsg, REPLY_REC_OFF + 1, body, inode,
1498                             MDS_PACK_MD_LOCK, 0);
1499         }
1500
1501         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1502         reply_body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*reply_body));
1503         lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, 0);
1504         lmm_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 1),
1505         logcookies = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 2, 0);
1506         cookies_size = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF + 2);
1507         req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1,
1508                                        lmm, lmm_size, logcookies, cookies_size,
1509                                        &reply_body->valid);
1510         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1511
1512         mds_shrink_reply(obd, req, body, REPLY_REC_OFF + 1);
1513         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1514                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1515                 req->rq_status = -ENOMEM;
1516                 RETURN(-ENOMEM);
1517         }
1518
1519         RETURN(rc);
1520 }
1521
1522 int mds_done_writing(struct ptlrpc_request *req, int offset)
1523 {
1524         struct mds_body *body;
1525         int rc, size[2] = { sizeof(struct ptlrpc_body),
1526                             sizeof(struct mds_body) };
1527         ENTRY;
1528
1529         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1530
1531         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1532                                   lustre_swab_mds_body);
1533         if (body == NULL) {
1534                 CERROR("Can't unpack body\n");
1535                 req->rq_status = -EFAULT;
1536                 RETURN(-EFAULT);
1537         }
1538
1539         rc = lustre_pack_reply(req, 2, size, NULL);
1540         if (rc)
1541                 req->rq_status = rc;
1542
1543         RETURN(0);
1544 }