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