Whamcloud - gitweb
Branch: b_new_cmd
[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 <linux/obd_class.h>
46 #include <linux/obd_lov.h>
47 #include <linux/lustre_fsfilt.h>
48 #include <linux/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, 1, 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 & 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_sem 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_stripe_md *lsm = NULL;
316         struct lov_mds_md *lmm = NULL;
317         int rc, lmm_size;
318         struct mds_body *body;
319         struct obdo *oa;
320         void *lmm_buf;
321         ENTRY;
322
323         if (!S_ISREG(inode->i_mode))
324                 RETURN(0);
325         if (rec->ur_flags & MDS_OPEN_DELAY_CREATE ||
326             !(rec->ur_flags & FMODE_WRITE))
327                 RETURN(0);
328
329         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
330
331         if (body->valid & OBD_MD_FLEASIZE)
332                 RETURN(0);
333
334         OBD_ALLOC(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
335         if (*ids == NULL)
336                 RETURN(-ENOMEM);
337         oti_init(&oti, req);
338         oti.oti_objid = *ids;
339
340         /* replay case */
341         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
342                 if (rec->ur_fid2->id == 0) {
343                         DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
344                         RETURN(-EFAULT);
345                 }
346
347                 body->valid |= OBD_MD_FLBLKSZ | OBD_MD_FLEASIZE;
348                 lmm_size = rec->ur_eadatalen;
349                 lmm = rec->ur_eadata;
350                 LASSERT(lmm);
351
352                 if (*handle == NULL)
353                         *handle = fsfilt_start(obd,inode,FSFILT_OP_CREATE,NULL);
354                 if (IS_ERR(*handle)) {
355                         rc = PTR_ERR(*handle);
356                         *handle = NULL;
357                         GOTO(out_ids, rc);
358                 }
359
360                 mds_objids_from_lmm(*ids, lmm, &mds->mds_lov_desc);
361
362                 rc = fsfilt_set_md(obd, inode, *handle, lmm, lmm_size, "lov");
363                 lmm_buf = lustre_msg_buf(req->rq_repmsg, offset, lmm_size);
364                 if (!lmm_buf) {
365                         if (!rc) rc = -ENOMEM;
366                 } else {
367                         memcpy(lmm_buf, lmm, lmm_size);
368                 }
369                 if (rc)
370                         CERROR("open replay failed to set md:%d\n", rc);
371                 RETURN(rc);
372         }
373
374         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_ALLOC_OBDO))
375                 GOTO(out_ids, rc = -ENOMEM);
376
377         oa = obdo_alloc();
378         if (oa == NULL)
379                 GOTO(out_ids, rc = -ENOMEM);
380         oa->o_uid = 0; /* must have 0 uid / gid on OST */
381         oa->o_gid = 0;
382         oa->o_mode = S_IFREG | 0600;
383         oa->o_id = inode->i_ino;
384         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
385                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
386         oa->o_size = 0;
387
388         obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
389                         OBD_MD_FLMTIME | OBD_MD_FLCTIME);
390
391         if (!(rec->ur_flags & MDS_OPEN_HAS_OBJS)) {
392                 /* check if things like lfs setstripe are sending us the ea */
393                 if (rec->ur_flags & MDS_OPEN_HAS_EA) {
394                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
395                                            mds->mds_osc_exp,
396                                            0, &lsm, rec->ur_eadata);
397                         if (rc)
398                                 GOTO(out_oa, rc);
399                 } else {
400                         OBD_ALLOC(lmm, mds->mds_max_mdsize);
401                         if (lmm == NULL)
402                                 GOTO(out_oa, rc = -ENOMEM);
403
404                         lmm_size = mds->mds_max_mdsize;
405                         rc = mds_get_md(obd, dchild->d_parent->d_inode,
406                                         lmm, &lmm_size, 1);
407                         if (rc > 0)
408                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
409                                                    mds->mds_osc_exp,
410                                                    0, &lsm, lmm);
411                         OBD_FREE(lmm, mds->mds_max_mdsize);
412                         if (rc)
413                                 GOTO(out_oa, rc);
414                 }
415                 rc = obd_create(mds->mds_osc_exp, oa, &lsm, &oti);
416                 if (rc) {
417                         int level = D_ERROR;
418                         if (rc == -ENOSPC)
419                                 level = D_INODE;
420                         CDEBUG(level, "error creating objects for "
421                                       "inode %lu: rc = %d\n",
422                                inode->i_ino, rc);
423                         if (rc > 0) {
424                                 CERROR("obd_create returned invalid "
425                                        "rc %d\n", rc);
426                                 rc = -EIO;
427                         }
428                         GOTO(out_oa, rc);
429                 }
430         } else {
431                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, mds->mds_osc_exp,
432                                    0, &lsm, rec->ur_eadata);
433                 if (rc) {
434                         GOTO(out_oa, rc);
435                 }
436                 lsm->lsm_object_id = oa->o_id;
437         }
438         if (inode->i_size) {
439                 oa->o_size = inode->i_size;
440                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
441                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME | OBD_MD_FLSIZE);
442
443                 /* pack lustre id to OST */
444                 oa->o_fid = body->fid1.id;
445                 oa->o_generation = body->fid1.generation;
446                 oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
447
448                 rc = obd_setattr(mds->mds_osc_exp, oa, lsm, &oti);
449                 if (rc) {
450                         CERROR("error setting attrs for inode %lu: rc %d\n",
451                                inode->i_ino, rc);
452                         if (rc > 0) {
453                                 CERROR("obd_setattr returned bad rc %d\n", 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, oa, OBD_MD_FLBLKSZ);
462
463         LASSERT(lsm && lsm->lsm_object_id);
464         lmm = NULL;
465         rc = obd_packmd(mds->mds_osc_exp, &lmm, lsm);
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         if (!lmm_buf) {
484                 if (!rc) rc = -ENOMEM;
485         } else {
486                 memcpy(lmm_buf, lmm, lmm_size);
487         }
488         obd_free_diskmd(mds->mds_osc_exp, &lmm);
489  out_oa:
490         oti_free_cookies(&oti);
491         obdo_free(oa);
492  out_ids:
493         if (rc) {
494                 OBD_FREE(*ids, mds->mds_lov_desc.ld_tgt_count * sizeof(**ids));
495                 *ids = NULL;
496         }
497         if (lsm)
498                 obd_free_memmd(mds->mds_osc_exp, &lsm);
499         RETURN(rc);
500 }
501
502 static void reconstruct_open(struct mds_update_record *rec, int offset,
503                              struct ptlrpc_request *req,
504                              struct lustre_handle *child_lockh)
505 {
506         struct mds_export_data *med = &req->rq_export->exp_mds_data;
507         struct mds_client_data *mcd = med->med_mcd;
508         struct mds_obd *mds = mds_req2mds(req);
509         struct mds_file_data *mfd;
510         struct obd_device *obd = req->rq_export->exp_obd;
511         struct dentry *parent, *dchild;
512         struct ldlm_reply *rep;
513         struct mds_body *body;
514         int rc;
515         struct list_head *t;
516         int put_child = 1;
517         ENTRY;
518
519         LASSERT(offset == 2);                  /* only called via intent */
520         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
521         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
522
523         /* copy rc, transno and disp; steal locks */
524         mds_req_from_mcd(req, mcd);
525         intent_set_disposition(rep, mcd->mcd_last_data);
526
527         /* Only replay if create or open actually happened. */
528         if (!intent_disposition(rep, DISP_OPEN_CREATE | DISP_OPEN_OPEN) ) {
529                 EXIT;
530                 return; /* error looking up parent or child */
531         }
532
533         parent = mds_fid2dentry(mds, rec->ur_fid1, NULL);
534         LASSERT(!IS_ERR(parent));
535
536         dchild = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
537         LASSERT(!IS_ERR(dchild));
538
539         if (!dchild->d_inode)
540                 GOTO(out_dput, 0); /* child not present to open */
541
542         /* At this point, we know we have a child. We'll send
543          * it back _unless_ it not created and open failed.
544          */
545         if (intent_disposition(rep, DISP_OPEN_OPEN) &&
546             !intent_disposition(rep, DISP_OPEN_CREATE) &&
547             req->rq_status) {
548                 GOTO(out_dput, 0);
549         }
550
551         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
552         mds_pack_inode2body(body, dchild->d_inode);
553         if (S_ISREG(dchild->d_inode->i_mode)) {
554                 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
555                                  dchild->d_inode, 1);
556
557                 if (rc)
558                         LASSERT(rc == req->rq_status);
559
560                 /* If we have LOV EA data, the OST holds size, mtime */
561                 if (!(body->valid & OBD_MD_FLEASIZE))
562                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
563                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
564         }
565
566         if (!(rec->ur_flags & MDS_OPEN_JOIN_FILE))
567                 lustre_shrink_reply(req, 2, body->eadatasize, 0);
568
569         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
570             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
571                 int acl_off = body->eadatasize ? 3 : 2;
572
573                 rc = mds_pack_acl(med, dchild->d_inode, req->rq_repmsg,
574                                   body, acl_off);
575                 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
576                 if (!req->rq_status && rc)
577                         req->rq_status = rc;
578         }
579
580         /* If we have -EEXIST as the status, and we were asked to create
581          * exclusively, we can tell we failed because the file already existed.
582          */
583         if (req->rq_status == -EEXIST &&
584             ((rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_EXCL)) ==
585              (MDS_OPEN_CREAT | MDS_OPEN_EXCL))) {
586                 GOTO(out_dput, 0);
587         }
588
589         /* If we didn't get as far as trying to open, then some locking thing
590          * probably went wrong, and we'll just bail here.
591          */
592         if (!intent_disposition(rep, DISP_OPEN_OPEN))
593                 GOTO(out_dput, 0);
594
595         /* If we failed, then we must have failed opening, so don't look for
596          * file descriptor or anything, just give the client the bad news.
597          */
598         if (req->rq_status)
599                 GOTO(out_dput, 0);
600
601         mfd = NULL;
602         spin_lock(&med->med_open_lock);
603         list_for_each(t, &med->med_open_head) {
604                 mfd = list_entry(t, struct mds_file_data, mfd_list);
605                 if (mfd->mfd_xid == req->rq_xid) {
606                         mds_mfd_addref(mfd);
607                         break;
608                 }
609                 mfd = NULL;
610         }
611         spin_unlock(&med->med_open_lock);
612
613         /* #warning "XXX fixme" bug 2991 */
614         /* Here it used to LASSERT(mfd) if exp_outstanding_reply != NULL.
615          * Now that exp_outstanding_reply is a list, it's just using mfd != NULL
616          * to detect a re-open */
617         if (mfd == NULL) {
618                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
619                         rc = mds_join_file(rec, req, dchild, NULL);
620                         if (rc)
621                                 GOTO(out_dput, rc);
622                 }
623                 mntget(mds->mds_vfsmnt);
624                 CERROR("Re-opened file \n");
625                 mfd = mds_dentry_open(dchild, mds->mds_vfsmnt,
626                                       rec->ur_flags & ~MDS_OPEN_TRUNC, req);
627                 if (!mfd) {
628                         CERROR("mds: out of memory\n");
629                         GOTO(out_dput, req->rq_status = -ENOMEM);
630                 }
631                 put_child = 0;
632         } else {
633                 body->handle.cookie = mfd->mfd_handle.h_cookie;
634                 CDEBUG(D_INODE, "resend mfd %p, cookie "LPX64"\n", mfd,
635                        mfd->mfd_handle.h_cookie);
636         }
637
638         mds_mfd_put(mfd);
639
640  out_dput:
641         if (put_child)
642                 l_dput(dchild);
643         l_dput(parent);
644         EXIT;
645 }
646
647 /* do NOT or the MAY_*'s, you'll get the weakest */
648 static int accmode(struct inode *inode, int flags)
649 {
650         int res = 0;
651
652         /* Sadly, NFSD reopens a file repeatedly during operation, so the
653          * "acc_mode = 0" allowance for newly-created files isn't honoured.
654          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
655          * owner can write to a file even if it is marked readonly to hide
656          * its brokenness. (bug 5781) */
657         if (flags & MDS_OPEN_OWNEROVERRIDE && inode->i_uid == current->fsuid)
658                 return 0;
659
660         if (flags & FMODE_READ)
661                 res = MAY_READ;
662         if (flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
663                 res |= MAY_WRITE;
664         if (flags & FMODE_EXEC)
665                 res = MAY_EXEC;
666         return res;
667 }
668
669 /* Handles object creation, actual opening, and I/O epoch */
670 static int mds_finish_open(struct ptlrpc_request *req, struct dentry *dchild,
671                            struct mds_body *body, int flags, void **handle,
672                            struct mds_update_record *rec,struct ldlm_reply *rep,
673                            struct lustre_handle *lockh)
674 {
675         struct mds_obd *mds = mds_req2mds(req);
676         struct obd_device *obd = req->rq_export->exp_obd;
677         struct mds_file_data *mfd = NULL;
678         obd_id *ids = NULL; /* object IDs created */
679         int rc = 0;
680         ENTRY;
681
682         /* atomically create objects if necessary */
683         down(&dchild->d_inode->i_sem);
684
685         if (S_ISREG(dchild->d_inode->i_mode) &&
686             !(body->valid & OBD_MD_FLEASIZE)) {
687                 rc = mds_pack_md(obd, req->rq_repmsg, 2, body,
688                                  dchild->d_inode, 0);
689                 if (rc) {
690                         up(&dchild->d_inode->i_sem);
691                         RETURN(rc);
692                 }
693         }
694         if (rec != NULL) {
695                 if ((body->valid & OBD_MD_FLEASIZE) &&
696                     (rec->ur_flags & MDS_OPEN_HAS_EA)) {
697                         up(&dchild->d_inode->i_sem);
698                         RETURN(-EEXIST);
699                 }
700                 if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
701                         up(&dchild->d_inode->i_sem);
702                         rc = mds_join_file(rec, req, dchild, lockh);
703                         if (rc)
704                                 RETURN(rc);
705                         down(&dchild->d_inode->i_sem);
706                 }
707                 if (!(body->valid & OBD_MD_FLEASIZE) &&
708                     !(body->valid & OBD_MD_FLMODEASIZE)) {
709                         /* no EA: create objects */
710                         rc = mds_create_objects(req, 2, rec, mds, obd,
711                                                 dchild, handle, &ids);
712                         if (rc) {
713                                 CERROR("mds_create_objects: rc = %d\n", rc);
714                                 up(&dchild->d_inode->i_sem);
715                                 RETURN(rc);
716                         }
717                 }
718         }
719         /* If the inode has no EA data, then MDS holds size, mtime */
720         if (S_ISREG(dchild->d_inode->i_mode) &&
721             !(body->valid & OBD_MD_FLEASIZE)) {
722                 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
723                                 OBD_MD_FLATIME | OBD_MD_FLMTIME);
724         }
725         up(&dchild->d_inode->i_sem);
726
727         if (!(rec->ur_flags & MDS_OPEN_JOIN_FILE))
728                 lustre_shrink_reply(req, 2, body->eadatasize, 0);
729
730         if (req->rq_export->exp_connect_flags & OBD_CONNECT_ACL &&
731             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
732                 int acl_off = body->eadatasize ? 3 : 2;
733
734                 rc = mds_pack_acl(&req->rq_export->exp_mds_data,
735                                   dchild->d_inode, req->rq_repmsg,
736                                   body, acl_off);
737                 lustre_shrink_reply(req, acl_off, body->aclsize, 0);
738                 if (rc)
739                         RETURN(rc);
740         }
741
742         intent_set_disposition(rep, DISP_OPEN_OPEN);
743         mfd = mds_dentry_open(dchild, mds->mds_vfsmnt, flags, req);
744         if (IS_ERR(mfd))
745                 RETURN(PTR_ERR(mfd));
746
747         CDEBUG(D_INODE, "mfd %p, cookie "LPX64"\n", mfd,
748                mfd->mfd_handle.h_cookie);
749
750         if (ids != NULL) {
751                 mds_lov_update_objids(obd, ids);
752                 OBD_FREE(ids, sizeof(*ids) * mds->mds_lov_desc.ld_tgt_count);
753         }
754         if (rc)
755                 mds_mfd_unlink(mfd, 1);
756         mds_mfd_put(mfd);
757         RETURN(rc);
758 }
759
760 static int mds_open_by_fid(struct ptlrpc_request *req, struct lu_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[LU_FID_NAMELEN];
767         int fidlen = 0, rc;
768         void *handle = NULL;
769         ENTRY;
770
771         fidlen = lu_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 *request_body, *reply_body;
811         struct lvfs_run_ctxt saved;
812         int rc, size = sizeof(*reply_body);
813         ENTRY;
814
815         request_body = lustre_msg_buf(req->rq_reqmsg, offset,
816                                       sizeof(*request_body));
817
818         rc = lustre_pack_reply(req, 1, &size, NULL);
819         if (rc)
820                 RETURN(rc);
821         reply_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply_body));
822
823         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
824         rc = mds_open_by_fid(req, &request_body->fid1, reply_body,
825                              request_body->flags, NULL, 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 = 0;
841         int rc;
842
843         if (child_lockh == NULL)
844                 child_lockh = &lockh;
845
846         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, child_res_id,
847                               LDLM_PLAIN, NULL, LCK_EX, &lock_flags,
848                               ldlm_blocking_ast, ldlm_completion_ast,
849                               NULL, NULL, NULL, 0, NULL, child_lockh);
850         if (rc != ELDLM_OK)
851                 CERROR("ldlm_cli_enqueue: %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         ENTRY;
876
877         CLASSERT(MAXQUOTAS < 4);
878         if (offset == 2) { /* intent */
879                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
880                 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
881         } else if (offset == MDS_REQ_REC_OFF) { /* non-intent reint */
882                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
883         } else {
884                 body = NULL;
885                 LBUG();
886         }
887
888         MDS_CHECK_RESENT(req, reconstruct_open(rec, offset, req, child_lockh));
889
890         /* Step 0: If we are passed a fid, then we assume the client already
891          * opened this file and is only replaying the RPC, so we open the
892          * inode by fid (at some large expense in security). */
893         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY &&
894             !(rec->ur_flags & MDS_OPEN_JOIN_FILE)) {
895                 if (rec->ur_fid2->id == 0) {
896                         struct ldlm_lock *lock = ldlm_handle2lock(child_lockh);
897                         if (lock) {
898                                 LDLM_ERROR(lock, "fid2 not set on open replay");
899                                 LDLM_LOCK_PUT(lock);
900                         }
901                         DEBUG_REQ(D_ERROR, req, "fid2 not set on open replay");
902                         RETURN(-EFAULT);
903                 }
904
905                 rc = mds_open_by_fid(req, rec->ur_fid2, body, rec->ur_flags,
906                                      rec, rep);
907                 if (rc != -ENOENT)
908                         RETURN(rc);
909
910                 /* We didn't find the correct inode on disk either, so we
911                  * need to re-create it via a regular replay. */
912                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
913                         DEBUG_REQ(D_ERROR, req,"OPEN_CREAT not in open replay");
914                         RETURN(-EFAULT);
915                 }
916         } else if (rec->ur_fid2->id) {
917                 DEBUG_REQ(D_ERROR, req, "fid2 "LPU64"/%u on open non-replay",
918                           rec->ur_fid2->id, rec->ur_fid2->generation);
919                 RETURN(-EFAULT);
920         }
921
922         LASSERT(offset == 2); /* If we got here, we must be called via intent */
923
924         med = &req->rq_export->exp_mds_data;
925         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
926                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
927                 RETURN(-ENOMEM);
928         }
929
930         /* Step 1: Find and lock the parent */
931         if (rec->ur_flags & (MDS_OPEN_CREAT | MDS_OPEN_JOIN_FILE))
932                 parent_mode = LCK_EX;
933         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, parent_mode,
934                                         &parent_lockh, MDS_INODELOCK_UPDATE);
935         if (IS_ERR(dparent)) {
936                 rc = PTR_ERR(dparent);
937                 if (rc != -ENOENT) {
938                         CERROR("parent "LPU64"/%u lookup error %d\n",
939                                rec->ur_fid1->id, rec->ur_fid1->generation, rc);
940                 } else {
941                         /* Just cannot find parent - make it look like
942                          * usual negative lookup to avoid extra MDS RPC */
943                         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
944                         intent_set_disposition(rep, DISP_LOOKUP_NEG);
945                 }
946                 GOTO(cleanup, rc);
947         }
948         LASSERT(dparent->d_inode != NULL);
949
950         cleanup_phase = 1; /* parent dentry and lock */
951
952         if (rec->ur_flags & MDS_OPEN_JOIN_FILE) {
953                 dchild = dget(dparent);
954                 cleanup_phase = 2; /* child dentry */
955                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
956                 GOTO(found_child, rc);
957         }
958
959         /* Step 2: Lookup the child */
960         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
961         if (IS_ERR(dchild)) {
962                 rc = PTR_ERR(dchild);
963                 dchild = NULL; /* don't confuse mds_finish_transno() below */
964                 GOTO(cleanup, rc);
965         }
966
967         cleanup_phase = 2; /* child dentry */
968
969         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
970         if (dchild->d_inode)
971                 intent_set_disposition(rep, DISP_LOOKUP_POS);
972         else
973                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
974
975         /*Step 3: If the child was negative, and we're supposed to, create it.*/
976         if (dchild->d_inode == NULL) {
977                 unsigned long ino = rec->ur_fid2->id;
978                 struct iattr iattr;
979                 struct inode *inode;
980
981                 if (!(rec->ur_flags & MDS_OPEN_CREAT)) {
982                         /* It's negative and we weren't supposed to create it */
983                         GOTO(cleanup, rc = -ENOENT);
984                 }
985
986                 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
987                         GOTO(cleanup, rc = -EROFS);
988
989                 intent_set_disposition(rep, DISP_OPEN_CREATE);
990                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_CREATE,
991                                       NULL);
992                 if (IS_ERR(handle)) {
993                         rc = PTR_ERR(handle);
994                         handle = NULL;
995                         GOTO(cleanup, rc);
996                 }
997                 dchild->d_fsdata = (void *) &dp;
998                 dp.p_ptr = req;
999                 dp.p_inum = ino;
1000
1001                 rc = ll_vfs_create(dparent->d_inode, dchild, rec->ur_mode,NULL);
1002                 if (dchild->d_fsdata == (void *)(unsigned long)ino)
1003                         dchild->d_fsdata = NULL;
1004
1005                 if (rc) {
1006                         CDEBUG(D_INODE, "error during create: %d\n", rc);
1007                         GOTO(cleanup, rc);
1008                 }
1009                 inode = dchild->d_inode;
1010                 if (ino) {
1011                         LASSERT(ino == inode->i_ino);
1012                         /* Written as part of setattr */
1013                         inode->i_generation = rec->ur_fid2->generation;
1014                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
1015                                inode->i_ino, inode->i_generation);
1016                 }
1017
1018                 created = 1;
1019                 LTIME_S(iattr.ia_atime) = rec->ur_time;
1020                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1021                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1022
1023                 iattr.ia_uid = current->fsuid;  /* set by push_ctxt already */
1024                 if (dparent->d_inode->i_mode & S_ISGID)
1025                         iattr.ia_gid = dparent->d_inode->i_gid;
1026                 else
1027                         iattr.ia_gid = current->fsgid;
1028
1029                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1030                         ATTR_MTIME | ATTR_CTIME;
1031
1032                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1033                 if (rc)
1034                         CERROR("error on child setattr: rc = %d\n", rc);
1035
1036                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1037
1038                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1039                 if (rc)
1040                         CERROR("error on parent setattr: rc = %d\n", rc);
1041
1042                 rc = fsfilt_commit(obd, dchild->d_inode, handle, 0);
1043                 handle = NULL;
1044                 acc_mode = 0;           /* Don't check for permissions */
1045         } else {
1046                 acc_mode = accmode(dchild->d_inode, rec->ur_flags);
1047         }
1048
1049         LASSERTF(!mds_inode_is_orphan(dchild->d_inode),
1050                  "dchild %.*s (%p) inode %p/%lu/%u\n", dchild->d_name.len,
1051                  dchild->d_name.name, dchild, dchild->d_inode,
1052                  dchild->d_inode->i_ino, dchild->d_inode->i_generation);
1053
1054 found_child:
1055         mds_pack_inode2fid(&body->fid1, dchild->d_inode);
1056         mds_pack_inode2body(body, dchild->d_inode);
1057
1058         if (S_ISREG(dchild->d_inode->i_mode)) {
1059                 /* Check permissions etc */
1060                 rc = ll_permission(dchild->d_inode, acc_mode, NULL);
1061                 if (rc != 0)
1062                         GOTO(cleanup, rc);
1063
1064                 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) &&
1065                     (acc_mode & MAY_WRITE))
1066                         GOTO(cleanup, rc = -EROFS);
1067
1068                 /* An append-only file must be opened in append mode for
1069                  * writing */
1070                 if (IS_APPEND(dchild->d_inode) && (acc_mode & MAY_WRITE) != 0 &&
1071                     ((rec->ur_flags & MDS_OPEN_APPEND) == 0 ||
1072                      (rec->ur_flags & MDS_OPEN_TRUNC) != 0))
1073                         GOTO(cleanup, rc = -EPERM);
1074         }
1075
1076         if (!created && (rec->ur_flags & MDS_OPEN_CREAT) &&
1077             (rec->ur_flags & MDS_OPEN_EXCL)) {
1078                 /* File already exists, we didn't just create it, and we
1079                  * were passed O_EXCL; err-or. */
1080                 GOTO(cleanup, rc = -EEXIST); // returns a lock to the client
1081         }
1082
1083         /* if we are following a symlink, don't open */
1084         if (S_ISLNK(dchild->d_inode->i_mode))
1085                 GOTO(cleanup_no_trans, rc = 0);
1086
1087         if (S_ISDIR(dchild->d_inode->i_mode)) {
1088                 if (rec->ur_flags & MDS_OPEN_CREAT ||
1089                     rec->ur_flags & FMODE_WRITE) {
1090                         /* we are trying to create or write a exist dir */
1091                         GOTO(cleanup, rc = -EISDIR);
1092                 }
1093                 if (ll_permission(dchild->d_inode, acc_mode, NULL)) {
1094                         intent_set_disposition(rep, DISP_OPEN_OPEN);
1095                         GOTO(cleanup, rc = -EACCES);
1096                 }
1097         } else if (rec->ur_flags & MDS_OPEN_DIRECTORY) {
1098                 GOTO(cleanup, rc = -ENOTDIR);
1099         }
1100
1101         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_CREATE)) {
1102                 obd_fail_loc = OBD_FAIL_LDLM_REPLY | OBD_FAIL_ONCE;
1103                 GOTO(cleanup, rc = -EAGAIN);
1104         }
1105
1106         /* Step 5: mds_open it */
1107         rc = mds_finish_open(req, dchild, body, rec->ur_flags, &handle, rec,
1108                              rep, &parent_lockh);
1109         GOTO(cleanup, rc);
1110
1111  cleanup:
1112         rc = mds_finish_transno(mds, dchild ? dchild->d_inode : NULL, handle,
1113                                 req, rc, rep ? rep->lock_policy_res1 : 0);
1114
1115  cleanup_no_trans:
1116         switch (cleanup_phase) {
1117         case 2:
1118                 if (rc && created) {
1119                         int err = vfs_unlink(dparent->d_inode, dchild);
1120                         if (err) {
1121                                 CERROR("unlink(%.*s) in error path: %d\n",
1122                                        dchild->d_name.len, dchild->d_name.name,
1123                                        err);
1124                         }
1125                 } else if (created) {
1126                         mds_lock_new_child(obd, dchild->d_inode, NULL);
1127                         /* save uid/gid for quota acquire/release */
1128                         qpids[USRQUOTA] = dparent->d_inode->i_uid;
1129                         qpids[GRPQUOTA] = dparent->d_inode->i_gid;
1130                 }
1131                 l_dput(dchild);
1132         case 1:
1133                 if (dparent == NULL)
1134                         break;
1135
1136                 l_dput(dparent);
1137                 if (rc)
1138                         ldlm_lock_decref(&parent_lockh, parent_mode);
1139                 else
1140                         ptlrpc_save_lock (req, &parent_lockh, parent_mode);
1141         }
1142
1143         /* trigger dqacq on the owner of child and parent */
1144         lquota_adjust(quota_interface, obd, qcids, qpids, rc, FSFILT_OP_CREATE);
1145         RETURN(rc);
1146 }
1147
1148 /* Close a "file descriptor" and possibly unlink an orphan from the
1149  * PENDING directory.  Caller must hold child->i_sem, this drops it.
1150  *
1151  * If we are being called from mds_disconnect() because the client has
1152  * disappeared, then req == NULL and we do not update last_rcvd because
1153  * there is nothing that could be recovered by the client at this stage
1154  * (it will not even _have_ an entry in last_rcvd anymore).
1155  *
1156  * Returns EAGAIN if the client needs to get more data and re-close. */
1157 int mds_mfd_close(struct ptlrpc_request *req, int offset,struct obd_device *obd,
1158                   struct mds_file_data *mfd, int unlink_orphan)
1159 {
1160         struct inode *inode = mfd->mfd_dentry->d_inode;
1161         char fidname[LU_FID_NAMELEN];
1162         int last_orphan, fidlen, rc = 0, cleanup_phase = 0;
1163         struct dentry *pending_child = NULL;
1164         struct mds_obd *mds = &obd->u.mds;
1165         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1166         void *handle = NULL;
1167         struct mds_body *request_body = NULL, *reply_body = NULL;
1168         struct dentry_params dp;
1169         struct iattr iattr = { 0 };
1170         ENTRY;
1171
1172         if (req && req->rq_reqmsg != NULL)
1173                 request_body = lustre_msg_buf(req->rq_reqmsg, offset,
1174                                               sizeof(*request_body));
1175         if (req && req->rq_repmsg != NULL)
1176                 reply_body = lustre_msg_buf(req->rq_repmsg, 0,
1177                                             sizeof(*reply_body));
1178
1179         fidlen = lu_fid2str(fidname, inode->i_ino, inode->i_generation);
1180
1181         CDEBUG(D_INODE, "inode %p ino %s nlink %d orphan %d\n", inode, fidname,
1182                inode->i_nlink, mds_orphan_open_count(inode));
1183
1184         last_orphan = mds_orphan_open_dec_test(inode) &&
1185                 mds_inode_is_orphan(inode);
1186         MDS_UP_WRITE_ORPHAN_SEM(inode);
1187
1188         /* this is half of the actual "close" */
1189         if (mfd->mfd_mode & FMODE_WRITE) {
1190                 rc = mds_put_write_access(mds, inode, request_body,
1191                                           last_orphan && unlink_orphan);
1192         } else if (mfd->mfd_mode & FMODE_EXEC) {
1193                 mds_allow_write_access(inode);
1194         }
1195
1196         if (last_orphan && unlink_orphan) {
1197                 struct lov_mds_md *lmm = NULL;
1198                 int stripe_count = 0;
1199                 LASSERT(rc == 0); /* mds_put_write_access must have succeeded */
1200
1201                 CDEBUG(D_HA, "destroying orphan object %s\n", fidname);
1202
1203                 if ((S_ISREG(inode->i_mode) && inode->i_nlink != 1) ||
1204                     (S_ISDIR(inode->i_mode) && inode->i_nlink != 2))
1205                         CERROR("found \"orphan\" %s %s with link count %d\n",
1206                                S_ISREG(inode->i_mode) ? "file" : "dir",
1207                                fidname, inode->i_nlink);
1208
1209                 /* Sadly, there is no easy way to save pending_child from
1210                  * mds_reint_unlink() into mfd, so we need to re-lookup,
1211                  * but normally it will still be in the dcache. */
1212                 down(&pending_dir->i_sem);
1213                 cleanup_phase = 1; /* up(&pending_dir->i_sem) when finished */
1214                 pending_child = lookup_one_len(fidname, mds->mds_pending_dir,
1215                                                fidlen);
1216                 if (IS_ERR(pending_child))
1217                         GOTO(cleanup, rc = PTR_ERR(pending_child));
1218                 LASSERT(pending_child->d_inode != NULL);
1219
1220                 cleanup_phase = 2; /* dput(pending_child) when finished */
1221                 if (S_ISDIR(pending_child->d_inode->i_mode)) {
1222                         rc = vfs_rmdir(pending_dir, pending_child);
1223                         if (rc)
1224                                 CERROR("error unlinking orphan dir %s: rc %d\n",
1225                                        fidname,rc);
1226                         goto out;
1227                 }
1228
1229                 if (req != NULL && req->rq_repmsg != NULL) {
1230                         lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
1231                         stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
1232                 }
1233
1234                 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK,
1235                                           NULL, stripe_count);
1236                 if (IS_ERR(handle)) {
1237                         rc = PTR_ERR(handle);
1238                         handle = NULL;
1239                         GOTO(cleanup, rc);
1240                 }
1241
1242                 if (req != NULL && req->rq_repmsg != NULL &&
1243                     (reply_body->valid & OBD_MD_FLEASIZE) &&
1244                     mds_log_op_unlink(obd, pending_child->d_inode, lmm,
1245                                       req->rq_repmsg->buflens[1],
1246                                       lustre_msg_buf(req->rq_repmsg, 2, 0),
1247                                       req->rq_repmsg->buflens[2]) > 0) {
1248                         reply_body->valid |= OBD_MD_FLCOOKIE;
1249                 }
1250
1251                 pending_child->d_fsdata = (void *) &dp;
1252                 dp.p_inum = 0;
1253                 dp.p_ptr = req;
1254                 rc = vfs_unlink(pending_dir, pending_child);
1255                 if (rc)
1256                         CERROR("error unlinking orphan %s: rc %d\n",fidname,rc);
1257
1258                 goto out; /* Don't bother updating attrs on unlinked inode */
1259         }
1260
1261 #if 0
1262         if (request_body != NULL && mfd->mfd_mode & FMODE_WRITE && rc == 0) {
1263                 /* Update the on-disk attributes if this was the last write
1264                  * close, and all information was provided (i.e., rc == 0)
1265                  *
1266                  * XXX this should probably be abstracted with mds_reint_setattr
1267                  */
1268
1269                 if (request_body->valid & OBD_MD_FLMTIME &&
1270                     LTIME_S(iattr.ia_mtime) > LTIME_S(inode->i_mtime)) {
1271                         LTIME_S(iattr.ia_mtime) = request_body->mtime;
1272                         iattr.ia_valid |= ATTR_MTIME;
1273                 }
1274                 if (request_body->valid & OBD_MD_FLCTIME &&
1275                     LTIME_S(iattr.ia_ctime) > LTIME_S(inode->i_ctime)) {
1276                         LTIME_S(iattr.ia_ctime) = request_body->ctime;
1277                         iattr.ia_valid |= ATTR_CTIME;
1278                 }
1279
1280                 /* XXX can't set block count with fsfilt_setattr (!) */
1281                 if (request_body->valid & OBD_MD_FLSIZE) {
1282                         iattr.ia_valid |= ATTR_SIZE;
1283                         iattr.ia_size = request_body->size;
1284                 }
1285                 /* iattr.ia_blocks = request_body->blocks */
1286
1287         }
1288 #endif
1289         if (request_body != NULL && request_body->valid & OBD_MD_FLATIME) {
1290                 /* Only start a transaction to write out only the atime if
1291                  * it is more out-of-date than the specified limit.  If we
1292                  * are already going to write out the atime then do it anyway.
1293                  * */
1294                 LTIME_S(iattr.ia_atime) = request_body->atime;
1295                 if ((LTIME_S(iattr.ia_atime) >
1296                      LTIME_S(inode->i_atime) + mds->mds_atime_diff) ||
1297                     (iattr.ia_valid != 0 &&
1298                      LTIME_S(iattr.ia_atime) > LTIME_S(inode->i_atime)))
1299                         iattr.ia_valid |= ATTR_ATIME;
1300         }
1301
1302         if (iattr.ia_valid != 0) {
1303                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1304                 if (IS_ERR(handle))
1305                         GOTO(cleanup, rc = PTR_ERR(handle));
1306                 rc = fsfilt_setattr(obd, mfd->mfd_dentry, handle, &iattr, 0);
1307                 if (rc)
1308                         CERROR("error in setattr(%s): rc %d\n", fidname, rc);
1309         }
1310 out:
1311         /* If other clients have this file open for write, rc will be > 0 */
1312         if (rc > 0)
1313                 rc = 0;
1314         l_dput(mfd->mfd_dentry);
1315         mds_mfd_put(mfd);
1316
1317  cleanup:
1318         if (req != NULL && reply_body != NULL) {
1319                 rc = mds_finish_transno(mds, pending_dir, handle, req, rc, 0);
1320         } else if (handle) {
1321                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
1322                 if (err) {
1323                         CERROR("error committing close: %d\n", err);
1324                         if (!rc)
1325                                 rc = err;
1326                 }
1327         }
1328
1329         switch (cleanup_phase) {
1330         case 2:
1331                 dput(pending_child);
1332         case 1:
1333                 up(&pending_dir->i_sem);
1334         }
1335         RETURN(rc);
1336 }
1337
1338 int mds_close(struct ptlrpc_request *req, int offset)
1339 {
1340         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1341         struct obd_device *obd = req->rq_export->exp_obd;
1342         struct mds_body *body;
1343         struct mds_file_data *mfd;
1344         struct lvfs_run_ctxt saved;
1345         struct inode *inode;
1346         int rc, repsize[3] = {sizeof(struct mds_body),
1347                               obd->u.mds.mds_max_mdsize,
1348                               obd->u.mds.mds_max_cookiesize};
1349         ENTRY;
1350
1351         rc = lustre_pack_reply(req, 3, repsize, NULL);
1352         if (rc) {
1353                 CERROR("lustre_pack_reply: rc = %d\n", rc);
1354                 req->rq_status = rc;
1355                 /* continue on to drop local open even if we can't send reply */
1356         } else {
1357                 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1358         }
1359
1360         CDEBUG(D_HA, "close req->rep_len %d mdsize %d cookiesize %d\n",
1361                req->rq_replen,
1362                obd->u.mds.mds_max_mdsize, obd->u.mds.mds_max_cookiesize);
1363
1364         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1365                                   lustre_swab_mds_body);
1366         if (body == NULL) {
1367                 CERROR("Can't unpack body\n");
1368                 req->rq_status = -EFAULT;
1369                 RETURN(-EFAULT);
1370         }
1371
1372         if (body->flags & MDS_BFLAG_UNCOMMITTED_WRITES)
1373                 /* do some stuff */ ;
1374
1375         spin_lock(&med->med_open_lock);
1376         mfd = mds_handle2mfd(&body->handle);
1377         if (mfd == NULL) {
1378                 spin_unlock(&med->med_open_lock);
1379                 DEBUG_REQ(D_ERROR, req, "no handle for file close ino "LPD64
1380                           ": cookie "LPX64, body->fid1.id, body->handle.cookie);
1381                 req->rq_status = -ESTALE;
1382                 RETURN(-ESTALE);
1383         }
1384         /* Remove mfd handle so it can't be found again.  We consume mfd_list
1385          * reference here, but still have mds_handle2mfd ref until mfd_close. */
1386         mds_mfd_unlink(mfd, 1);
1387         spin_unlock(&med->med_open_lock);
1388
1389         inode = mfd->mfd_dentry->d_inode;
1390         /* child orphan sem protects orphan_dec_test && is_orphan race */
1391         MDS_DOWN_WRITE_ORPHAN_SEM(inode); /* mds_mfd_close drops this */
1392         if (mds_inode_is_orphan(inode) && mds_orphan_open_count(inode) == 1) {
1393                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1394                 LASSERT(body != NULL);
1395
1396                 mds_pack_inode2fid(&body->fid1, inode);
1397                 mds_pack_inode2body(body, inode);
1398                 mds_pack_md(obd, req->rq_repmsg, 1,body,inode,MDS_PACK_MD_LOCK);
1399         }
1400
1401         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1402         req->rq_status = mds_mfd_close(req, offset, obd, mfd, 1);
1403         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1404
1405         mds_shrink_reply(obd, req, body);
1406         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
1407                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
1408                 req->rq_status = -ENOMEM;
1409                 RETURN(-ENOMEM);
1410         }
1411
1412         RETURN(rc);
1413 }
1414
1415 int mds_done_writing(struct ptlrpc_request *req, int offset)
1416 {
1417         struct mds_body *body;
1418         int rc, size = sizeof(struct mds_body);
1419         ENTRY;
1420
1421         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1422
1423         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1424                                   lustre_swab_mds_body);
1425         if (body == NULL) {
1426                 CERROR("Can't unpack body\n");
1427                 req->rq_status = -EFAULT;
1428                 RETURN(-EFAULT);
1429         }
1430
1431         rc = lustre_pack_reply(req, 1, &size, NULL);
1432         if (rc) {
1433                 CERROR("lustre_pack_reply: rc = %d\n", rc);
1434                 req->rq_status = rc;
1435         }
1436
1437         RETURN(0);
1438 }