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