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