Whamcloud - gitweb
- add an argument to select which tests to run
[fs/lustre-release.git] / lustre / mdc / mdc_locks.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
32 #else
33 # include <liblustre.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/lprocfs_status.h>
40 #include "mdc_internal.h"
41
42 int it_disposition(struct lookup_intent *it, int flag)
43 {
44         return it->d.lustre.it_disposition & flag;
45 }
46 EXPORT_SYMBOL(it_disposition);
47
48 void it_set_disposition(struct lookup_intent *it, int flag)
49 {
50         it->d.lustre.it_disposition |= flag;
51 }
52 EXPORT_SYMBOL(it_set_disposition);
53
54 static void mdc_fid2mdc_op_data(struct mdc_op_data *data, struct ll_uctxt *ctxt,
55                                 struct ll_fid *f1, struct ll_fid *f2,
56                                 const char *name, int namelen, int mode)
57 {
58         LASSERT(data);
59         LASSERT(ctxt);
60         LASSERT(f1);
61
62         data->ctxt = *ctxt;
63         data->fid1 = *f1;
64         if (f2)
65                 data->fid2 = *f2;
66         else
67                 memset(&data->fid2, 0, sizeof(data->fid2));
68         data->name = name;
69         data->namelen = namelen;
70         data->create_mode = mode;
71         data->mod_time = LTIME_S(CURRENT_TIME);
72 }
73
74 static int it_to_lock_mode(struct lookup_intent *it)
75 {
76         /* CREAT needs to be tested before open (both could be set) */
77         if (it->it_op & IT_CREAT)
78                 return LCK_PW;
79         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP))
80                 return LCK_PR;
81
82         LBUG();
83         RETURN(-EINVAL);
84 }
85
86 int it_open_error(int phase, struct lookup_intent *it)
87 {
88         if (it_disposition(it, DISP_OPEN_OPEN)) {
89                 if (phase == DISP_OPEN_OPEN)
90                         return it->d.lustre.it_status;
91                 else
92                         return 0;
93         }
94
95         if (it_disposition(it, DISP_OPEN_CREATE)) {
96                 if (phase == DISP_OPEN_CREATE)
97                         return it->d.lustre.it_status;
98                 else
99                         return 0;
100         }
101
102         if (it_disposition(it, DISP_LOOKUP_EXECD)) {
103                 if (phase == DISP_LOOKUP_EXECD)
104                         return it->d.lustre.it_status;
105                 else
106                         return 0;
107         }
108
109         if (it_disposition(it, DISP_IT_EXECD)) {
110                 if (phase == DISP_IT_EXECD)
111                         return it->d.lustre.it_status;
112                 else
113                         return 0;
114         }
115         CERROR("it disp: %X, status: %d\n", it->d.lustre.it_disposition,
116                it->d.lustre.it_status);
117         LBUG();
118         return 0;
119 }
120 EXPORT_SYMBOL(it_open_error);
121
122 /* this must be called on a lockh that is known to have a referenced lock */
123 void mdc_set_lock_data(__u64 *l, void *data)
124 {
125         struct ldlm_lock *lock;
126         struct lustre_handle *lockh = (struct lustre_handle *)l;
127         ENTRY;
128
129         if (!*l) {
130                 EXIT;
131                 return;
132         }
133
134         lock = ldlm_handle2lock(lockh);
135
136         LASSERT(lock != NULL);
137         l_lock(&lock->l_resource->lr_namespace->ns_lock);
138 #ifdef __KERNEL__
139         if (lock->l_ast_data && lock->l_ast_data != data) {
140                 struct inode *new_inode = data;
141                 struct inode *old_inode = lock->l_ast_data;
142                 unsigned long state = old_inode->i_state & I_FREEING;
143                 CERROR("Found existing inode %p/%lu/%u state %lu in lock: "
144                        "setting data to %p/%lu/%u\n", old_inode,
145                        old_inode->i_ino, old_inode->i_generation, state,
146                        new_inode, new_inode->i_ino, new_inode->i_generation);
147                 LASSERT(state);
148         }
149 #endif
150         lock->l_ast_data = data;
151         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
152         LDLM_LOCK_PUT(lock);
153
154         EXIT;
155 }
156 EXPORT_SYMBOL(mdc_set_lock_data);
157
158 int mdc_change_cbdata(struct obd_export *exp, struct ll_fid *fid, 
159                       ldlm_iterator_t it, void *data)
160 {
161         struct ldlm_res_id res_id = { .name = {0} };
162         ENTRY;
163
164         res_id.name[0] = fid->id;
165         res_id.name[1] = fid->generation;
166
167         ldlm_change_cbdata(class_exp2obd(exp)->obd_namespace, &res_id, it, 
168                            data);
169         EXIT;
170         return 0;
171 }
172
173
174
175 /* We always reserve enough space in the reply packet for a stripe MD, because
176  * we don't know in advance the file type. */
177 int mdc_enqueue(struct obd_export *exp,
178                 int lock_type,
179                 struct lookup_intent *it,
180                 int lock_mode,
181                 struct mdc_op_data *data,
182                 struct lustre_handle *lockh,
183                 void *lmm,
184                 int lmmsize,
185                 ldlm_completion_callback cb_completion,
186                 ldlm_blocking_callback cb_blocking,
187                 void *cb_data)
188 {
189         struct ptlrpc_request *req;
190         struct obd_device *obddev = class_exp2obd(exp);
191         struct ldlm_res_id res_id =
192                 { .name = {data->fid1.id, data->fid1.generation} };
193         int size[6] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
194         int rc, flags = LDLM_FL_HAS_INTENT;
195         int repsize[4] = {sizeof(struct ldlm_reply),
196                           sizeof(struct mds_body),
197                           obddev->u.cli.cl_max_mds_easize,
198                           obddev->u.cli.cl_max_mds_cookiesize};
199         struct ldlm_reply *dlm_rep;
200         struct ldlm_intent *lit;
201         struct ldlm_request *lockreq;
202         void *eadata;
203         unsigned long irqflags;
204         int   reply_buffers = 0;
205         ENTRY;
206
207 //        LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
208 //                          ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
209
210         if (it->it_op & IT_OPEN) {
211                 it->it_create_mode |= S_IFREG;
212                 it->it_create_mode &= ~current->fs->umask;
213
214                 size[2] = sizeof(struct mds_rec_create);
215                 size[3] = data->namelen + 1;
216                 size[4] = obddev->u.cli.cl_max_mds_easize;
217                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 
218                                       5, size, NULL);
219                 if (!req)
220                         RETURN(-ENOMEM);
221
222                 spin_lock_irqsave (&req->rq_lock, irqflags);
223                 req->rq_replay = 1;
224                 spin_unlock_irqrestore (&req->rq_lock, irqflags);
225
226                 /* pack the intent */
227                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
228                 lit->opc = (__u64)it->it_op;
229
230                 /* pack the intended request */
231                 mdc_open_pack(req, 2, data, it->it_create_mode, 0,
232                               it->it_flags, lmm, lmmsize);
233                 /* get ready for the reply */
234                 reply_buffers = 3;
235                 req->rq_replen = lustre_msg_size(3, repsize);
236         } else if (it->it_op & IT_UNLINK) {
237                 size[2] = sizeof(struct mds_rec_unlink);
238                 size[3] = data->namelen + 1;
239                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
240                                       size, NULL);
241                 if (!req)
242                         RETURN(-ENOMEM);
243
244                 /* pack the intent */
245                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
246                 lit->opc = (__u64)it->it_op;
247
248                 /* pack the intended request */
249                 mdc_unlink_pack(req, 2, data);
250                 /* get ready for the reply */
251                 reply_buffers = 4;
252                 req->rq_replen = lustre_msg_size(4, repsize);
253         } else if (it->it_op & (IT_GETATTR | IT_LOOKUP)) {
254                 int valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
255                 size[2] = sizeof(struct mds_body);
256                 size[3] = data->namelen + 1;
257
258                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
259                                       size, NULL);
260                 if (!req)
261                         RETURN(-ENOMEM);
262
263                 /* pack the intent */
264                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
265                 lit->opc = (__u64)it->it_op;
266
267                 /* pack the intended request */
268                 mdc_getattr_pack(req, valid, 2, it->it_flags, data);
269                 /* get ready for the reply */
270                 reply_buffers = 3;
271                 req->rq_replen = lustre_msg_size(3, repsize);
272         } else if (it->it_op == IT_READDIR) {
273                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
274                                       size, NULL);
275                 if (!req)
276                         RETURN(-ENOMEM);
277
278                 /* get ready for the reply */
279                 reply_buffers = 1;
280                 req->rq_replen = lustre_msg_size(1, repsize);
281         }  else {
282                 LBUG();
283                 RETURN(-EINVAL);
284         }
285
286         mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
287         rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, NULL, res_id,
288                               lock_type, NULL, 0, lock_mode, &flags,
289                               cb_completion, cb_blocking, cb_data, lockh);
290         mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
291
292         /* Similarly, if we're going to replay this request, we don't want to
293          * actually get a lock, just perform the intent. */
294         if (req->rq_transno || req->rq_replay) {
295                 lockreq = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*lockreq));
296                 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
297         }
298
299         /* This can go when we're sure that this can never happen */
300         LASSERT(rc != -ENOENT);
301         if (rc == ELDLM_LOCK_ABORTED) {
302                 lock_mode = 0;
303                 memset(lockh, 0, sizeof(*lockh));
304                 rc = 0;
305         } else if (rc != 0) {
306                 CERROR("ldlm_cli_enqueue: %d\n", rc);
307                 LASSERT (rc < 0);
308                 ptlrpc_req_finished(req);
309                 RETURN(rc);
310         } else { /* rc = 0 */
311                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
312                 LASSERT(lock);
313
314                 /* If the server gave us back a different lock mode, we should
315                  * fix up our variables. */
316                 if (lock->l_req_mode != lock_mode) {
317                         ldlm_lock_addref(lockh, lock->l_req_mode);
318                         ldlm_lock_decref(lockh, lock_mode);
319                         lock_mode = lock->l_req_mode;
320                 }
321
322                 LDLM_LOCK_PUT(lock);
323         }
324
325         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
326         LASSERT(dlm_rep != NULL);           /* checked by ldlm_cli_enqueue() */
327         LASSERT_REPSWABBED(req, 0);         /* swabbed by ldlm_cli_enqueue() */
328
329         it->d.lustre.it_disposition = (int) dlm_rep->lock_policy_res1;
330         it->d.lustre.it_status = (int) dlm_rep->lock_policy_res2;
331         it->d.lustre.it_lock_mode = lock_mode;
332         it->d.lustre.it_data = req;
333
334         /* We know what to expect, so we do any byte flipping required here */
335         LASSERT(reply_buffers == 4 || reply_buffers == 3 || reply_buffers == 1);
336         if (reply_buffers >= 3) {
337                 struct mds_body *body;
338
339                 body = lustre_swab_repbuf(req, 1, sizeof (*body),
340                                            lustre_swab_mds_body);
341                 if (body == NULL) {
342                         CERROR ("Can't swab mds_body\n");
343                         RETURN (-EPROTO);
344                 }
345
346                 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
347                         void *replayea;
348                         /* The eadata is opaque; just check that it is
349                          * there.  Eventually, obd_unpackmd() will check
350                          * the contents */
351                         eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
352                                                     NULL);
353                         if (eadata == NULL) {
354                                 CERROR ("Missing/short eadata\n");
355                                 RETURN (-EPROTO);
356                         }
357                         if (it->it_op & IT_OPEN) {
358                                 replayea = lustre_msg_buf(req->rq_reqmsg, 4, 
359                                                           obddev->u.cli.cl_max_mds_easize);
360                                 LASSERT(replayea);
361                                 memcpy(replayea, eadata, body->eadatasize);
362                         }
363                 }
364         }
365
366         RETURN(rc);
367 }
368 EXPORT_SYMBOL(mdc_enqueue);
369
370 /* 
371  * This long block is all about fixing up the lock and request state
372  * so that it is correct as of the moment _before_ the operation was
373  * applied; that way, the VFS will think that everything is normal and
374  * call Lustre's regular VFS methods.
375  *
376  * If we're performing a creation, that means that unless the creation
377  * failed with EEXIST, we should fake up a negative dentry.
378  *
379  * For everything else, we want to lookup to succeed.
380  *
381  * One additional note: if CREATE or OPEN succeeded, we add an extra
382  * reference to the request because we need to keep it around until
383  * ll_create/ll_open gets called.
384  *
385  * The server will return to us, in it_disposition, an indication of
386  * exactly what d.lustre.it_status refers to.
387  *
388  * If DISP_OPEN_OPEN is set, then d.lustre.it_status refers to the open() call,
389  * otherwise if DISP_OPEN_CREATE is set, then it status is the
390  * creation failure mode.  In either case, one of DISP_LOOKUP_NEG or
391  * DISP_LOOKUP_POS will be set, indicating whether the child lookup
392  * was successful.
393  *
394  * Else, if DISP_LOOKUP_EXECD then d.lustre.it_status is the rc of the
395  * child lookup.
396  */
397 int mdc_intent_lock(struct obd_export *exp, struct ll_uctxt *uctxt,
398                     struct ll_fid *pfid, const char *name, int len,
399                     void *lmm, int lmmsize,
400                     struct ll_fid *cfid, struct lookup_intent *it, int flags,
401                     struct ptlrpc_request **reqp,
402                     ldlm_blocking_callback cb_blocking)
403 {
404         struct lustre_handle lockh;
405         struct ptlrpc_request *request;
406         int rc = 0;
407         struct mds_body *mds_body;
408         struct lustre_handle old_lock;
409         struct ldlm_lock *lock;
410         ENTRY;
411         LASSERT(it);
412
413         CDEBUG(D_DLMTRACE, "name: %*s in %ld, intent: %s\n", len, name,
414                (unsigned long) pfid->id, ldlm_it2str(it->it_op));
415
416         if (cfid && (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)) {
417                 /* We could just return 1 immediately, but since we should only
418                  * be called in revalidate_it if we already have a lock, let's
419                  * verify that. */
420                 struct ldlm_res_id res_id ={.name = {cfid->id, 
421                                                      cfid->generation}};
422                 struct lustre_handle lockh;
423                 int mode, flags = LDLM_FL_BLOCK_GRANTED;
424
425                 mode = LCK_PR;
426                 rc = ldlm_lock_match(exp->exp_obd->obd_namespace, flags,
427                                      &res_id, LDLM_PLAIN, NULL, 0, LCK_PR,
428                                      &lockh);
429                 if (!rc) {
430                         mode = LCK_PW;
431                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace, flags,
432                                              &res_id, LDLM_PLAIN, NULL, 0,
433                                              LCK_PW, &lockh);
434                 }
435                 if (rc) {
436                         memcpy(&it->d.lustre.it_lock_handle, &lockh, 
437                                sizeof(lockh));
438                         it->d.lustre.it_lock_mode = mode;
439                 }
440                 RETURN(rc);
441         }
442
443         /* lookup_it may be called only after revalidate_it has run, because
444          * revalidate_it cannot return errors, only zero.  Returning zero causes
445          * this call to lookup, which *can* return an error.
446          *
447          * We only want to execute the request associated with the intent one
448          * time, however, so don't send the request again.  Instead, skip past
449          * this and use the request from revalidate.  In this case, revalidate
450          * never dropped its reference, so the refcounts are all OK */
451         if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
452                 struct mdc_op_data op_data;
453                 mdc_fid2mdc_op_data(&op_data, uctxt, pfid, cfid, name, len, 0);
454
455                 rc = mdc_enqueue(exp, LDLM_PLAIN, it, it_to_lock_mode(it),
456                                  &op_data, &lockh, lmm, lmmsize,
457                                  ldlm_completion_ast, cb_blocking, NULL);
458                 if (rc < 0)
459                         RETURN(rc);
460                 memcpy(&it->d.lustre.it_lock_handle, &lockh, sizeof(lockh));
461         }
462         request = *reqp = it->d.lustre.it_data;
463         LASSERT(request != NULL);
464
465         if (!it_disposition(it, DISP_IT_EXECD)) {
466                 /* The server failed before it even started executing the
467                  * intent, i.e. because it couldn't unpack the request. */
468                 LASSERT(it->d.lustre.it_status != 0);
469                 RETURN(it->d.lustre.it_status);
470         }
471         rc = it_open_error(DISP_IT_EXECD, it);
472         if (rc)
473                 RETURN(rc);
474
475         mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
476         LASSERT(mds_body != NULL);           /* mdc_enqueue checked */
477         LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
478
479         /* If we were revalidating a fid/name pair, mark the intent in
480          * case we fail and get called again from lookup */
481         if (cfid != NULL) {
482                 it_set_disposition(it, DISP_ENQ_COMPLETE);
483                 /* Also: did we find the same inode? */
484                 if (memcmp(cfid, &mds_body->fid1, sizeof(*cfid)))
485                         RETURN(-ESTALE);
486         }
487
488         /* If we're doing an IT_OPEN which did not result in an actual
489          * successful open, then we need to remove the bit which saves
490          * this request for unconditional replay. */
491         if (it->it_op & IT_OPEN) {
492                 if (!it_disposition(it, DISP_OPEN_OPEN) ||
493                     it->d.lustre.it_status != 0) {
494                         unsigned long flags;
495
496                         spin_lock_irqsave(&request->rq_lock, flags);
497                         request->rq_replay = 0;
498                         spin_unlock_irqrestore(&request->rq_lock, flags);
499                 }
500         }
501
502         rc = it_open_error(DISP_LOOKUP_EXECD, it);
503         if (rc)
504                 RETURN(rc);
505
506         /* keep requests around for the multiple phases of the call
507          * this shows the DISP_XX must guarantee we make it into the call
508          */
509         if (it_disposition(it, DISP_OPEN_CREATE) &&
510             !it_open_error(DISP_OPEN_CREATE, it))
511                 ptlrpc_request_addref(request);
512         if (it_disposition(it, DISP_OPEN_OPEN) &&
513             !it_open_error(DISP_OPEN_OPEN, it))
514                 ptlrpc_request_addref(request);
515
516         if (it->it_op & IT_CREAT) {
517                 /* XXX this belongs in ll_create_iit */
518         } else if (it->it_op == IT_OPEN) {
519                 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
520         } else {
521                 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP));
522         }
523
524         /* If we already have a matching lock, then cancel the new
525          * one.  We have to set the data here instead of in
526          * mdc_enqueue, because we need to use the child's inode as
527          * the l_ast_data to match, and that's not available until
528          * intent_finish has performed the iget().) */
529         lock = ldlm_handle2lock(&lockh);
530         if (lock) {
531                 LDLM_DEBUG(lock, "matching against this");
532                 LDLM_LOCK_PUT(lock);
533                 memcpy(&old_lock, &lockh, sizeof(lockh));
534                 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
535                                     LDLM_PLAIN, NULL, 0, LCK_NL, &old_lock)) {
536                         ldlm_lock_decref_and_cancel(&lockh,
537                                                     it->d.lustre.it_lock_mode);
538                         memcpy(&lockh, &old_lock, sizeof(old_lock));
539                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
540                                sizeof(lockh));
541                 }
542         }
543         CDEBUG(D_DENTRY, "D_IT dentry %*s intent: %s status %d disp %x rc %d\n",
544                len, name, ldlm_it2str(it->it_op), it->d.lustre.it_status,
545                it->d.lustre.it_disposition, rc);
546
547         RETURN(rc);
548 }
549 EXPORT_SYMBOL(mdc_intent_lock);