Whamcloud - gitweb
fixed some defects according to inspection.
[fs/lustre-release.git] / lustre / mdt / mdt_open.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mdt/mdt_open.c
5  *  Lustre Metadata Target (mdt) open/close file handling
6  *
7  *  Copyright (C) 2002-2006 Cluster File Systems, Inc.
8  *   Author: Huang Hua <huanghua@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 "mdt_internal.h"
35
36 /*
37  * MDS file data handling: file data holds a handle for a file opened
38  * by a client.
39  */
40
41 static void mdt_mfd_get(void *mfdp)
42 {
43         struct mdt_file_data *mfd = mfdp;
44
45         atomic_inc(&mfd->mfd_refcount);
46         CDEBUG(D_INFO, "GETting mfd %p : new refcount %d\n", mfd,
47                atomic_read(&mfd->mfd_refcount));
48 }
49
50 /* Create a new mdt_file_data struct. 
51  * reference is set to 1 */
52 static struct mdt_file_data *mdt_mfd_new(void)
53 {
54         struct mdt_file_data *mfd;
55
56         OBD_ALLOC_PTR(mfd);
57         if (mfd == NULL) {
58                 CERROR("mds: out of memory\n");
59                 return NULL;
60         }
61
62         atomic_set(&mfd->mfd_refcount, 1);
63
64         INIT_LIST_HEAD(&mfd->mfd_handle.h_link);
65         INIT_LIST_HEAD(&mfd->mfd_list);
66         class_handle_hash(&mfd->mfd_handle, mdt_mfd_get);
67
68         return mfd;
69 }
70
71 /* Get a new reference on the mfd pointed to by handle, if handle is still
72  * valid.  Caller must drop reference with mdt_mfd_put(). */
73 static struct mdt_file_data *mdt_handle2mfd(const struct lustre_handle *handle)
74 {
75         ENTRY;
76         LASSERT(handle != NULL);
77         RETURN(class_handle2object(handle->cookie));
78 }
79
80 /* Drop mfd reference, freeing struct if this is the last one. */
81 static void mdt_mfd_put(struct mdt_file_data *mfd)
82 {
83         CDEBUG(D_INFO, "PUTting mfd %p : new refcount %d\n", mfd,
84                atomic_read(&mfd->mfd_refcount) - 1);
85         LASSERT(atomic_read(&mfd->mfd_refcount) > 0 &&
86                 atomic_read(&mfd->mfd_refcount) < 0x5a5a);
87         if (atomic_dec_and_test(&mfd->mfd_refcount)) {
88                 LASSERT(list_empty(&mfd->mfd_handle.h_link));
89                 OBD_FREE_PTR(mfd);
90         }
91 }
92
93 static int mdt_object_open(struct mdt_thread_info *info,
94                            struct mdt_object *o, 
95                            int flags)
96 {
97         struct mdt_export_data *med;
98         struct mdt_file_data   *mfd;
99         struct mdt_body        *repbody;
100         struct lov_mds_md      *lmm;
101         int                     rc = 0;
102         ENTRY;
103
104         med = &mdt_info_req(info)->rq_export->exp_mdt_data;
105         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
106         lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
107
108         rc = mo_attr_get(info->mti_ctxt, mdt_object_child(o),
109                          &info->mti_attr);
110         if (rc == -EREMOTE) {
111                 repbody->fid1 = *mdt_object_fid(o);
112                 repbody->valid |= OBD_MD_FLID;
113                 RETURN(-EREMOTE);
114         }
115         if (rc != 0)
116                 RETURN(rc);
117
118         mdt_pack_attr2body(repbody, &info->mti_attr, mdt_object_fid(o));
119
120 /*
121         rc = mo_xattr_get(info->mti_ctxt, mdt_object_child(o),
122                           lmm, info->mti_mdt->mdt_max_mdsize, "lov");
123         if (rc < 0)
124                 RETURN(-EINVAL);
125         if (S_ISDIR(info->mti_attr.la_mode))
126                 repbody->valid |= OBD_MD_FLDIREA;
127         else
128                 repbody->valid |= OBD_MD_FLEASIZE;
129         repbody->eadatasize = rc;
130         rc = 0;
131 */
132         mfd = mdt_mfd_new();
133         if (mfd == NULL) {
134                 CERROR("mds: out of memory\n");
135                 RETURN(-ENOMEM);
136         }
137
138         if (flags & FMODE_WRITE) {
139                 /*mds_get_write_access*/
140         } else if (flags & MDS_FMODE_EXEC) {
141                 /*mds_deny_write_access*/
142         }
143
144         /* keep a reference on this object for this open,
145          * and is released by mdt_mfd_close() */
146         mdt_object_get(info->mti_ctxt, o);
147
148         mfd->mfd_mode = flags;
149         mfd->mfd_object = o;
150         mfd->mfd_xid = mdt_info_req(info)->rq_xid;
151
152         spin_lock(&med->med_open_lock);
153         list_add(&mfd->mfd_list, &med->med_open_head);
154         spin_unlock(&med->med_open_lock);
155
156         repbody->handle.cookie = mfd->mfd_handle.h_cookie;
157
158         RETURN(rc);
159 }
160
161 int mdt_open_by_fid(struct mdt_thread_info* info, const struct lu_fid *fid,
162                     __u32 flags, struct ldlm_reply *rep)
163 {
164         struct mdt_object *o;
165         int rc;
166         ENTRY;
167
168         o = mdt_object_find(info->mti_ctxt, info->mti_mdt, fid);
169         if (!IS_ERR(o)) {
170                 if (mdt_object_exists(info->mti_ctxt, &o->mot_obj.mo_lu)) {
171                         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
172                         intent_set_disposition(rep, DISP_LOOKUP_POS);
173                         rc = mdt_object_open(info, o, flags);
174                         intent_set_disposition(rep, DISP_OPEN_OPEN);
175                         mdt_object_put(info->mti_ctxt, o);
176                 } else {
177                         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
178                         intent_set_disposition(rep, DISP_LOOKUP_NEG);
179                         rc = -ENOENT;
180                 }
181         } else
182                 rc = PTR_ERR(o);
183
184         RETURN(rc);
185 }
186
187 int mdt_pin(struct mdt_thread_info* info)
188 {
189         int rc;
190         ENTRY;
191         rc = mdt_open_by_fid(info, &info->mti_body->fid1, 
192                              info->mti_body->flags, NULL);
193         RETURN(rc);
194 }
195
196 /*  Get an internal lock on the inode number (but not generation) to sync
197  *  new inode creation with inode unlink (bug 2029).  If child_lockh is NULL
198  *  we just get the lock as a barrier to wait for other holders of this lock,
199  *  and drop it right away again. */
200 int mdt_lock_new_child(struct mdt_thread_info *info, 
201                        struct mdt_object *o,
202                        struct mdt_lock_handle *child_lockh)
203 {
204         struct mdt_lock_handle lockh;
205         int rc;
206         
207         if (child_lockh == NULL)
208                 child_lockh = &lockh;
209
210         mdt_lock_handle_init(&lockh);
211         lockh.mlh_mode = LCK_EX;
212         rc = mdt_object_lock(info, o, &lockh, MDS_INODELOCK_UPDATE);
213
214         if (rc != ELDLM_OK)
215                 CERROR("can not mdt_object_lock: %d\n", rc);
216         else if (child_lockh == &lockh)
217                 mdt_object_unlock(info, o, &lockh);
218
219         RETURN(rc);
220 }
221
222 int mdt_reint_open(struct mdt_thread_info *info)
223 {
224         struct mdt_device      *mdt = info->mti_mdt;
225         struct mdt_object      *parent;
226         struct mdt_object      *child;
227         struct mdt_lock_handle *lh;
228         struct ldlm_reply      *ldlm_rep;
229         struct ptlrpc_request  *req = mdt_info_req(info);
230         struct mdt_body        *body;
231         struct lu_fid          *child_fid = &info->mti_tmp_fid1;
232         int                     result;
233         int                     created = 0;
234         struct mdt_reint_record *rr = &info->mti_rr;
235         ENTRY;
236
237         /* we now have no resent message, so it must be an intent */
238         LASSERT(info->mti_pill.rc_fmt == &RQF_LDLM_INTENT_OPEN);
239
240         /*TODO: MDS_CHECK_RESENT */;
241         ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
242         body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
243
244         if (strlen(rr->rr_name) == 0) {
245                 /* partial remote open */
246                 RETURN(mdt_open_by_fid(info, rr->rr_fid1, 
247                                        info->mti_attr.la_flags, ldlm_rep));
248         }
249
250         intent_set_disposition(ldlm_rep, DISP_LOOKUP_EXECD);
251         lh = &info->mti_lh[MDT_LH_PARENT];
252         lh->mlh_mode = LCK_PW;
253         parent = mdt_object_find_lock(info, rr->rr_fid1, lh, 
254                                       MDS_INODELOCK_UPDATE);
255         if (IS_ERR(parent)) {
256                 /* FIXME: just simulate child not exist */
257                 intent_set_disposition(ldlm_rep, DISP_LOOKUP_NEG);
258                 GOTO(out, result = PTR_ERR(parent));
259         }
260
261         result = mdo_lookup(info->mti_ctxt, mdt_object_child(parent),
262                             rr->rr_name, child_fid);
263         if (result != 0 && result != -ENOENT) {
264                 GOTO(out_parent, result);
265         }
266
267         if (result == -ENOENT) {
268                 intent_set_disposition(ldlm_rep, DISP_LOOKUP_NEG);
269                 if (!(info->mti_attr.la_flags & MDS_OPEN_CREAT))
270                         GOTO(out_parent, result);
271                 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
272                         GOTO(out_parent, result = -EROFS);
273                 *child_fid = *info->mti_rr.rr_fid2;
274         } else {
275                 intent_set_disposition(ldlm_rep, DISP_LOOKUP_POS);
276                 if (info->mti_attr.la_flags & MDS_OPEN_EXCL &&
277                     info->mti_attr.la_flags & MDS_OPEN_CREAT)
278                         GOTO(out_parent, result = -EEXIST);
279                 /* child_fid is filled by mdo_lookup(). */
280                 LASSERT(lu_fid_eq(child_fid, info->mti_rr.rr_fid2));
281         }
282
283         child = mdt_object_find(info->mti_ctxt, mdt, child_fid);
284         if (IS_ERR(child))
285                 GOTO(out_parent, result = PTR_ERR(child));
286
287         if (result == -ENOENT) {
288                 /* not found and with MDS_OPEN_CREAT: let's create something */
289                 result = mdo_create(info->mti_ctxt,
290                                     mdt_object_child(parent),
291                                     rr->rr_name,
292                                     mdt_object_child(child),
293                                     &info->mti_attr);
294                 intent_set_disposition(ldlm_rep, DISP_OPEN_CREATE);
295                 if (result != 0)
296                         GOTO(out_child, result);
297                 created = 1;
298         }
299
300         /* Open it now. */
301         result = mdt_object_open(info, child, info->mti_attr.la_flags);
302         intent_set_disposition(ldlm_rep, DISP_OPEN_OPEN);
303         GOTO(destroy_child, result);
304
305 destroy_child:
306         if (created && result != 0 && result != -EREMOTE) {
307                 mdo_unlink(info->mti_ctxt, mdt_object_child(parent),
308                            mdt_object_child(child), rr->rr_name);
309         } else if (created) {
310                 /* barrier with other thread */
311                 mdt_lock_new_child(info, child, NULL);
312         }
313 out_child:
314         mdt_object_put(info->mti_ctxt, child);
315 out_parent:
316         mdt_object_unlock_put(info, parent, lh);
317 out:
318         return result;
319 }
320
321 int mdt_mfd_close(const struct lu_context *ctxt,
322                   struct mdt_file_data *mfd, 
323                   int unlink_orphan)
324 {
325         ENTRY;
326
327         if (mfd->mfd_mode & FMODE_WRITE) {
328                 /*mdt_put_write_access*/
329         } else if (mfd->mfd_mode & MDS_FMODE_EXEC) {
330                 /*mdt_allow_write_access*/
331         }
332
333         /* release reference on this object.
334          * it will be destroyed by lower layer if necessary.
335          */
336         mdt_object_put(ctxt, mfd->mfd_object);
337
338         mdt_mfd_put(mfd);
339         RETURN(0);
340 }
341
342 int mdt_close(struct mdt_thread_info *info)
343 {
344         struct mdt_export_data *med;
345         struct mdt_body        *repbody;
346         struct mdt_file_data   *mfd;
347         struct mdt_object      *o;
348         struct lov_mds_md      *lmm;
349         int rc;
350         ENTRY;
351
352         med = &mdt_info_req(info)->rq_export->exp_mdt_data;
353         LASSERT(med);        
354
355         spin_lock(&med->med_open_lock);
356         mfd = mdt_handle2mfd(&(info->mti_body->handle));
357         if (mfd == NULL) {
358                 spin_unlock(&med->med_open_lock);
359                 CDEBUG(D_INODE, "no handle for file close ino "DFID3
360                        ": cookie "LPX64, PFID3(&info->mti_body->fid1), 
361                        info->mti_body->handle.cookie);
362                 RETURN(-ESTALE);
363         }
364         class_handle_unhash(&mfd->mfd_handle);
365         list_del_init(&mfd->mfd_list);
366         spin_unlock(&med->med_open_lock);
367         /* mdt_handle2mfd increase reference count, we must drop it here */
368         mdt_mfd_put(mfd);
369
370         o = mfd->mfd_object;
371         if (lu_object_is_dying(&o->mot_header)) {
372                 repbody = req_capsule_server_get(&info->mti_pill, 
373                                                  &RMF_MDT_BODY);
374                 lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
375
376                 rc = mo_attr_get(info->mti_ctxt, mdt_object_child(o),
377                                  &info->mti_attr);
378                 if (rc == 0) {
379                         mdt_pack_attr2body(repbody, &info->mti_attr, 
380                                            mdt_object_fid(o));
381 /*
382                         rc = mo_xattr_get(info->mti_ctxt, mdt_object_child(o),
383                                           lmm, info->mti_mdt->mdt_max_mdsize, 
384                                           XATTR_NAME_LOV);
385                         if (rc >= 0) {
386                                 if (S_ISDIR(info->mti_attr.la_mode))
387                                         repbody->valid |= OBD_MD_FLDIREA;
388                                 else
389                                         repbody->valid |= OBD_MD_FLEASIZE;
390                                 repbody->eadatasize = rc;
391                                 rc = 0;
392                         }
393 */
394                }
395         }
396         rc = mdt_mfd_close(info->mti_ctxt, mfd, 1);
397
398         RETURN(rc);
399 }
400
401 int mdt_done_writing(struct mdt_thread_info *info)
402 {
403         ENTRY;
404
405         RETURN(0);
406 }