Whamcloud - gitweb
Land b_smallfix onto HEAD (20040428_2142)
[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                 LASSERTF(old_inode->i_state & I_FREEING,
143                          "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,
146                          old_inode->i_state,
147                          new_inode, new_inode->i_ino, new_inode->i_generation);
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
170         EXIT;
171         return 0;
172 }
173
174
175
176 /* We always reserve enough space in the reply packet for a stripe MD, because
177  * we don't know in advance the file type. */
178 int mdc_enqueue(struct obd_export *exp,
179                 int lock_type,
180                 struct lookup_intent *it,
181                 int lock_mode,
182                 struct mdc_op_data *data,
183                 struct lustre_handle *lockh,
184                 void *lmm,
185                 int lmmsize,
186                 ldlm_completion_callback cb_completion,
187                 ldlm_blocking_callback cb_blocking,
188                 void *cb_data)
189 {
190         struct ptlrpc_request *req;
191         struct obd_device *obddev = class_exp2obd(exp);
192         struct ldlm_res_id res_id =
193                 { .name = {data->fid1.id, data->fid1.generation} };
194         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP } };
195         int size[6] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
196         int rc, flags = LDLM_FL_HAS_INTENT;
197         int repsize[4] = {sizeof(struct ldlm_reply),
198                           sizeof(struct mds_body),
199                           obddev->u.cli.cl_max_mds_easize,
200                           obddev->u.cli.cl_max_mds_cookiesize};
201         struct ldlm_reply *dlm_rep;
202         struct ldlm_intent *lit;
203         struct ldlm_request *lockreq;
204         void *eadata;
205         unsigned long irqflags;
206         int   reply_buffers = 0;
207         ENTRY;
208
209 //        LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
210 //                          ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
211
212         if (it->it_op & IT_OPEN) {
213                 it->it_create_mode |= S_IFREG;
214                 it->it_create_mode &= ~current->fs->umask;
215
216                 size[2] = sizeof(struct mds_rec_create);
217                 size[3] = data->namelen + 1;
218                 size[4] = obddev->u.cli.cl_max_mds_easize;
219                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 
220                                       5, size, NULL);
221                 if (!req)
222                         RETURN(-ENOMEM);
223
224                 spin_lock_irqsave (&req->rq_lock, irqflags);
225                 req->rq_replay = 1;
226                 spin_unlock_irqrestore (&req->rq_lock, irqflags);
227
228                 /* pack the intent */
229                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
230                 lit->opc = (__u64)it->it_op;
231
232                 /* pack the intended request */
233                 mdc_open_pack(req, 2, data, it->it_create_mode, 0,
234                               it->it_flags, lmm, lmmsize);
235                 /* get ready for the reply */
236                 reply_buffers = 3;
237                 req->rq_replen = lustre_msg_size(3, repsize);
238         } else if (it->it_op & IT_UNLINK) {
239                 size[2] = sizeof(struct mds_rec_unlink);
240                 size[3] = data->namelen + 1;
241                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
242                                       size, NULL);
243                 if (!req)
244                         RETURN(-ENOMEM);
245
246                 /* pack the intent */
247                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
248                 lit->opc = (__u64)it->it_op;
249
250                 /* pack the intended request */
251                 mdc_unlink_pack(req, 2, data);
252                 /* get ready for the reply */
253                 reply_buffers = 4;
254                 req->rq_replen = lustre_msg_size(4, repsize);
255         } else if (it->it_op & (IT_GETATTR | IT_LOOKUP)) {
256                 int valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
257                 size[2] = sizeof(struct mds_body);
258                 size[3] = data->namelen + 1;
259
260                 if (it->it_op & IT_GETATTR)
261                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
262
263                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
264                                       size, NULL);
265                 if (!req)
266                         RETURN(-ENOMEM);
267
268                 /* pack the intent */
269                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
270                 lit->opc = (__u64)it->it_op;
271
272                 /* pack the intended request */
273                 mdc_getattr_pack(req, valid, 2, it->it_flags, data);
274                 /* get ready for the reply */
275                 reply_buffers = 3;
276                 req->rq_replen = lustre_msg_size(3, repsize);
277         } else if (it->it_op == IT_READDIR) {
278                policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
279                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
280                                       size, NULL);
281                 if (!req)
282                         RETURN(-ENOMEM);
283
284                 /* get ready for the reply */
285                 reply_buffers = 1;
286                 req->rq_replen = lustre_msg_size(1, repsize);
287         }  else {
288                 LBUG();
289                 RETURN(-EINVAL);
290         }
291         mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
292         rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, res_id,
293                               lock_type, &policy, lock_mode, &flags,cb_blocking,
294                               cb_completion, NULL, cb_data, NULL, 0, NULL,
295                               lockh);
296         mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
297
298         /* Similarly, if we're going to replay this request, we don't want to
299          * actually get a lock, just perform the intent. */
300         if (req->rq_transno || req->rq_replay) {
301                 lockreq = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*lockreq));
302                 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
303         }
304
305         /* This can go when we're sure that this can never happen */
306         LASSERT(rc != -ENOENT);
307         if (rc == ELDLM_LOCK_ABORTED) {
308                 lock_mode = 0;
309                 memset(lockh, 0, sizeof(*lockh));
310                 rc = 0;
311         } else if (rc != 0) {
312                 CERROR("ldlm_cli_enqueue: %d\n", rc);
313                 LASSERT (rc < 0);
314                 ptlrpc_req_finished(req);
315                 RETURN(rc);
316         } else { /* rc = 0 */
317                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
318                 LASSERT(lock);
319
320                 /* If the server gave us back a different lock mode, we should
321                  * fix up our variables. */
322                 if (lock->l_req_mode != lock_mode) {
323                         ldlm_lock_addref(lockh, lock->l_req_mode);
324                         ldlm_lock_decref(lockh, lock_mode);
325                         lock_mode = lock->l_req_mode;
326                 }
327
328                 ldlm_lock_allow_match(lock);
329                 LDLM_LOCK_PUT(lock);
330         }
331
332         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
333         LASSERT(dlm_rep != NULL);           /* checked by ldlm_cli_enqueue() */
334         LASSERT_REPSWABBED(req, 0);         /* swabbed by ldlm_cli_enqueue() */
335
336         it->d.lustre.it_disposition = (int) dlm_rep->lock_policy_res1;
337         it->d.lustre.it_status = (int) dlm_rep->lock_policy_res2;
338         it->d.lustre.it_lock_mode = lock_mode;
339         it->d.lustre.it_data = req;
340
341         if (it->d.lustre.it_status < 0 && req->rq_replay) {
342                 LASSERT(req->rq_transno == 0);
343                 /* Don't hold error requests for replay. */
344                 spin_lock(&req->rq_lock);
345                 req->rq_replay = 0;
346                 spin_unlock(&req->rq_lock);
347         }
348
349         DEBUG_REQ(D_RPCTRACE, req, "disposition: %x, status: %d",
350                   it->d.lustre.it_disposition, it->d.lustre.it_status);
351
352         /* We know what to expect, so we do any byte flipping required here */
353         LASSERT(reply_buffers == 4 || reply_buffers == 3 || reply_buffers == 1);
354         if (reply_buffers >= 3) {
355                 struct mds_body *body;
356
357                 body = lustre_swab_repbuf(req, 1, sizeof (*body),
358                                            lustre_swab_mds_body);
359                 if (body == NULL) {
360                         CERROR ("Can't swab mds_body\n");
361                         RETURN (-EPROTO);
362                 }
363
364                 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
365                         void *replayea;
366                         /* The eadata is opaque; just check that it is
367                          * there.  Eventually, obd_unpackmd() will check
368                          * the contents */
369                         eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
370                                                     NULL);
371                         if (eadata == NULL) {
372                                 CERROR ("Missing/short eadata\n");
373                                 RETURN (-EPROTO);
374                         }
375                         if (it->it_op & IT_OPEN) {
376                                 replayea = lustre_msg_buf(req->rq_reqmsg, 4, 
377                                                           obddev->u.cli.cl_max_mds_easize);
378                                 LASSERT(replayea);
379                                 memcpy(replayea, eadata, body->eadatasize);
380                         }
381                 }
382         }
383
384         RETURN(rc);
385 }
386 EXPORT_SYMBOL(mdc_enqueue);
387
388 /* 
389  * This long block is all about fixing up the lock and request state
390  * so that it is correct as of the moment _before_ the operation was
391  * applied; that way, the VFS will think that everything is normal and
392  * call Lustre's regular VFS methods.
393  *
394  * If we're performing a creation, that means that unless the creation
395  * failed with EEXIST, we should fake up a negative dentry.
396  *
397  * For everything else, we want to lookup to succeed.
398  *
399  * One additional note: if CREATE or OPEN succeeded, we add an extra
400  * reference to the request because we need to keep it around until
401  * ll_create/ll_open gets called.
402  *
403  * The server will return to us, in it_disposition, an indication of
404  * exactly what d.lustre.it_status refers to.
405  *
406  * If DISP_OPEN_OPEN is set, then d.lustre.it_status refers to the open() call,
407  * otherwise if DISP_OPEN_CREATE is set, then it status is the
408  * creation failure mode.  In either case, one of DISP_LOOKUP_NEG or
409  * DISP_LOOKUP_POS will be set, indicating whether the child lookup
410  * was successful.
411  *
412  * Else, if DISP_LOOKUP_EXECD then d.lustre.it_status is the rc of the
413  * child lookup.
414  */
415 int mdc_intent_lock(struct obd_export *exp, struct ll_uctxt *uctxt,
416                     struct ll_fid *pfid, const char *name, int len,
417                     void *lmm, int lmmsize,
418                     struct ll_fid *cfid, struct lookup_intent *it,
419                     int lookup_flags, struct ptlrpc_request **reqp,
420                     ldlm_blocking_callback cb_blocking)
421 {
422         struct lustre_handle lockh;
423         struct ptlrpc_request *request;
424         int rc = 0;
425         struct mds_body *mds_body;
426         struct lustre_handle old_lock;
427         struct ldlm_lock *lock;
428         ENTRY;
429         LASSERT(it);
430
431         CDEBUG(D_DLMTRACE, "name: %*s in %ld, intent: %s\n", len, name,
432                (unsigned long)pfid->id, ldlm_it2str(it->it_op));
433
434         if (cfid && (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)) {
435                 /* We could just return 1 immediately, but since we should only
436                  * be called in revalidate_it if we already have a lock, let's
437                  * verify that. */
438                 struct ldlm_res_id res_id ={.name = {cfid->id,
439                                                      cfid->generation}};
440                 struct lustre_handle lockh;
441                 ldlm_policy_data_t policy;
442                 int mode = LCK_PR;
443
444                 /* For the GETATTR case, ll_revalidate_it issues two separate
445                    queries - for LOOKUP and for UPDATE lock because if cannot
446                    check them together - we might have those two bits to be
447                    present in two separate granted locks */
448                 policy.l_inodebits.bits = 
449                                  (it->it_op == IT_GETATTR)?MDS_INODELOCK_UPDATE:
450                                                            MDS_INODELOCK_LOOKUP;
451                 mode = LCK_PR;
452                 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
453                                      LDLM_FL_BLOCK_GRANTED, &res_id,
454                                      LDLM_IBITS, &policy, LCK_PR, &lockh);
455                 if (!rc) {
456                         mode = LCK_PW;
457                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
458                                              LDLM_FL_BLOCK_GRANTED, &res_id,
459                                              LDLM_IBITS, &policy, LCK_PW,
460                                              &lockh);
461                 }
462                 if (rc) {
463                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
464                                sizeof(lockh));
465                         it->d.lustre.it_lock_mode = mode;
466                 }
467                 RETURN(rc);
468         }
469
470         /* lookup_it may be called only after revalidate_it has run, because
471          * revalidate_it cannot return errors, only zero.  Returning zero causes
472          * this call to lookup, which *can* return an error.
473          *
474          * We only want to execute the request associated with the intent one
475          * time, however, so don't send the request again.  Instead, skip past
476          * this and use the request from revalidate.  In this case, revalidate
477          * never dropped its reference, so the refcounts are all OK */
478         if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
479                 struct mdc_op_data op_data;
480                 mdc_fid2mdc_op_data(&op_data, uctxt, pfid, cfid, name, len, 0);
481
482                 rc = mdc_enqueue(exp, LDLM_IBITS, it, it_to_lock_mode(it),
483                                  &op_data, &lockh, lmm, lmmsize,
484                                  ldlm_completion_ast, cb_blocking, NULL);
485                 if (rc < 0)
486                         RETURN(rc);
487                 memcpy(&it->d.lustre.it_lock_handle, &lockh, sizeof(lockh));
488         }
489         request = *reqp = it->d.lustre.it_data;
490         LASSERT(request != NULL);
491
492         if (!it_disposition(it, DISP_IT_EXECD)) {
493                 /* The server failed before it even started executing the
494                  * intent, i.e. because it couldn't unpack the request. */
495                 LASSERT(it->d.lustre.it_status != 0);
496                 RETURN(it->d.lustre.it_status);
497         }
498         rc = it_open_error(DISP_IT_EXECD, it);
499         if (rc)
500                 RETURN(rc);
501
502         mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
503         LASSERT(mds_body != NULL);           /* mdc_enqueue checked */
504         LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
505
506         /* If we were revalidating a fid/name pair, mark the intent in
507          * case we fail and get called again from lookup */
508         if (cfid != NULL) {
509                 it_set_disposition(it, DISP_ENQ_COMPLETE);
510                 /* Also: did we find the same inode? */
511                 if (memcmp(cfid, &mds_body->fid1, sizeof(*cfid)))
512                         RETURN(-ESTALE);
513         }
514
515         /* If we're doing an IT_OPEN which did not result in an actual
516          * successful open, then we need to remove the bit which saves
517          * this request for unconditional replay. */
518         if (it->it_op & IT_OPEN) {
519                 if (!it_disposition(it, DISP_OPEN_OPEN) ||
520                     it->d.lustre.it_status != 0) {
521                         unsigned long irqflags;
522
523                         spin_lock_irqsave(&request->rq_lock, irqflags);
524                         request->rq_replay = 0;
525                         spin_unlock_irqrestore(&request->rq_lock, irqflags);
526                 }
527         }
528
529         rc = it_open_error(DISP_LOOKUP_EXECD, it);
530         if (rc)
531                 RETURN(rc);
532
533         /* keep requests around for the multiple phases of the call
534          * this shows the DISP_XX must guarantee we make it into the call
535          */
536         if (it_disposition(it, DISP_OPEN_CREATE) &&
537             !it_open_error(DISP_OPEN_CREATE, it))
538                 ptlrpc_request_addref(request);
539         if (it_disposition(it, DISP_OPEN_OPEN) &&
540             !it_open_error(DISP_OPEN_OPEN, it))
541                 ptlrpc_request_addref(request);
542
543         if (it->it_op & IT_CREAT) {
544                 /* XXX this belongs in ll_create_iit */
545         } else if (it->it_op == IT_OPEN) {
546                 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
547         } else {
548                 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP));
549         }
550
551         /* If we already have a matching lock, then cancel the new
552          * one.  We have to set the data here instead of in
553          * mdc_enqueue, because we need to use the child's inode as
554          * the l_ast_data to match, and that's not available until
555          * intent_finish has performed the iget().) */
556         lock = ldlm_handle2lock(&lockh);
557         if (lock) {
558                 ldlm_policy_data_t policy = lock->l_policy_data;
559                 LDLM_DEBUG(lock, "matching against this");
560                 LDLM_LOCK_PUT(lock);
561                 memcpy(&old_lock, &lockh, sizeof(lockh));
562                 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
563                                     LDLM_IBITS, &policy, LCK_NL, &old_lock)) {
564                         ldlm_lock_decref_and_cancel(&lockh,
565                                                     it->d.lustre.it_lock_mode);
566                         memcpy(&lockh, &old_lock, sizeof(old_lock));
567                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
568                                sizeof(lockh));
569                 }
570         }
571         CDEBUG(D_DENTRY, "D_IT dentry %*s intent: %s status %d disp %x rc %d\n",
572                len, name, ldlm_it2str(it->it_op), it->d.lustre.it_status,
573                it->d.lustre.it_disposition, rc);
574
575         RETURN(rc);
576 }
577 EXPORT_SYMBOL(mdc_intent_lock);